jump indirect seems to be working as well

This commit is contained in:
Henrik Böving
2020-11-30 18:28:25 +01:00
parent 9d650b1f7c
commit 5755ade839
5 changed files with 23 additions and 1 deletions

View File

@@ -18,7 +18,7 @@ jump_instruction = {"JMP"}
arg_instruction = {"LDA" | "ADD" | "SUB"} arg_instruction = {"LDA" | "ADD" | "SUB"}
no_arg_instruction = { "NOP" } no_arg_instruction = { "NOP" }
jump_argument = { jump_location | label } jump_argument = { jump_location | label | memory_location }
argument = { memory_location | digit_literal } argument = { memory_location | digit_literal }
memory_location = { "(" ~ ASCII_HEX_DIGIT ~")" } memory_location = { "(" ~ ASCII_HEX_DIGIT ~")" }
digit_literal = {"#" ~ ASCII_HEX_DIGIT} digit_literal = {"#" ~ ASCII_HEX_DIGIT}

View File

@@ -14,6 +14,7 @@ pub struct Label<'a> {
pub enum JumpArgument<'a> { pub enum JumpArgument<'a> {
Location(u8), Location(u8),
Label(&'a str), Label(&'a str),
MemoryLocation(u8),
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]

View File

@@ -68,6 +68,12 @@ pub fn generate_binary(instructions: Vec<Instruction>) -> Program {
} else { } else {
panic!("Tried to JMP to label: {}, which does not exist", arg); panic!("Tried to JMP to label: {}, which does not exist", arg);
} }
},
JumpArgument::MemoryLocation(address) => {
BinaryInstruction {
opcode: 12,
argument: *address
}
} }
}, },
}; };

View File

@@ -169,6 +169,11 @@ fn parse_jump_argument<'a>(arg: Pair<'a, Rule>) -> JumpArgument<'a> {
Rule::label => JumpArgument::Label(arg.as_str()), Rule::label => JumpArgument::Label(arg.as_str()),
Rule::jump_location => { Rule::jump_location => {
JumpArgument::Location(u8::from_str_radix(arg.as_str(), 16).unwrap()) JumpArgument::Location(u8::from_str_radix(arg.as_str(), 16).unwrap())
},
Rule::memory_location => {
let arg_string = arg.as_str();
let arg_value = u8::from_str_radix(&arg_string[1..arg_string.len() - 1], 16).unwrap();
JumpArgument::MemoryLocation(arg_value)
} }
_ => unreachable!(), _ => unreachable!(),
} }

View File

@@ -138,6 +138,12 @@ pub fn simulate<'a>(instructions: Vec<Instruction<'a>>, max_steps: usize) -> Vec
} else { } else {
panic!("Tried to JMP to label: {}, which does not exist", arg); panic!("Tried to JMP to label: {}, which does not exist", arg);
} }
},
JumpArgument::MemoryLocation(address) => {
BinaryInstruction {
opcode: 12,
argument: address
}
} }
}, },
}; };
@@ -205,6 +211,10 @@ pub fn simulate<'a>(instructions: Vec<Instruction<'a>>, max_steps: usize) -> Vec
next_pc = Some(location); next_pc = Some(location);
addr_bus = location; addr_bus = location;
}, },
JumpArgument::MemoryLocation(location) => {
next_pc = Some(data_memory[location as usize]);
addr_bus = data_memory[location as usize];
}
}, },
Instruction::MemoryLocationInstruction(arg, _) => match arg { Instruction::MemoryLocationInstruction(arg, _) => match arg {
MemoryLocationInstruction::STA(arg) => { MemoryLocationInstruction::STA(arg) => {