From 5755ade839ceab4f788dc72fe820318e8edb9403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20B=C3=B6ving?= Date: Mon, 30 Nov 2020 18:28:25 +0100 Subject: [PATCH] jump indirect seems to be working as well --- src/asm.pest | 2 +- src/asm.rs | 1 + src/generate.rs | 6 ++++++ src/parse.rs | 5 +++++ src/simulate.rs | 10 ++++++++++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/asm.pest b/src/asm.pest index 00d8d99..bec746b 100644 --- a/src/asm.pest +++ b/src/asm.pest @@ -18,7 +18,7 @@ jump_instruction = {"JMP"} arg_instruction = {"LDA" | "ADD" | "SUB"} no_arg_instruction = { "NOP" } -jump_argument = { jump_location | label } +jump_argument = { jump_location | label | memory_location } argument = { memory_location | digit_literal } memory_location = { "(" ~ ASCII_HEX_DIGIT ~")" } digit_literal = {"#" ~ ASCII_HEX_DIGIT} diff --git a/src/asm.rs b/src/asm.rs index ecda5ce..91f9083 100644 --- a/src/asm.rs +++ b/src/asm.rs @@ -14,6 +14,7 @@ pub struct Label<'a> { pub enum JumpArgument<'a> { Location(u8), Label(&'a str), + MemoryLocation(u8), } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/src/generate.rs b/src/generate.rs index cf74cd0..dd49e9f 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -68,6 +68,12 @@ pub fn generate_binary(instructions: Vec) -> Program { } else { panic!("Tried to JMP to label: {}, which does not exist", arg); } + }, + JumpArgument::MemoryLocation(address) => { + BinaryInstruction { + opcode: 12, + argument: *address + } } }, }; diff --git a/src/parse.rs b/src/parse.rs index e9714fb..6fa4085 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -169,6 +169,11 @@ fn parse_jump_argument<'a>(arg: Pair<'a, Rule>) -> JumpArgument<'a> { Rule::label => JumpArgument::Label(arg.as_str()), Rule::jump_location => { 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!(), } diff --git a/src/simulate.rs b/src/simulate.rs index ad84688..a7a2400 100644 --- a/src/simulate.rs +++ b/src/simulate.rs @@ -138,6 +138,12 @@ pub fn simulate<'a>(instructions: Vec>, max_steps: usize) -> Vec } else { 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>, max_steps: usize) -> Vec next_pc = Some(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 { MemoryLocationInstruction::STA(arg) => {