From be6555052532fc201d9e1ea38bc3237693e21f65 Mon Sep 17 00:00:00 2001 From: Alex Weisberger Date: Wed, 1 Oct 2014 18:02:45 -0400 Subject: [PATCH] fetch, decode, execute loop --- src/bin/main.rs | 13 ++++++++----- src/machine.rs | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 6154dee..c264f8f 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -31,10 +31,13 @@ use emu6502::machine; fn main() { let mut machine = machine::Machine::new(); - - println!("A: {}", machine.registers.accumulator); - println!("add_with_carry(1)"); - machine.add_with_carry(1); - println!("A: {}", machine.registers.accumulator); + + for _ in range(0u, 2u) { + let raw_instruction = machine.fetch_instruction(); + let instruction = machine.decode_instruction(raw_instruction); + machine.execute_instruction(instruction); + } + + println!("Ran program, output of Accum is {}", machine.registers.accumulator); } diff --git a/src/machine.rs b/src/machine.rs index 5ae4086..1893ccb 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -25,6 +25,10 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +use address::Address; +use address::AddressDiff; +use instruction::Instruction; +use instruction::ADC; use memory::Memory; use registers::{ Registers, Status, StatusArgs }; use registers::{ ps_negative, ps_overflow, ps_zero, ps_carry }; @@ -45,6 +49,26 @@ impl Machine { pub fn reset(&mut self) { *self = Machine::new(); } + + pub fn fetch_instruction(&mut self) -> u8 { + let instr = self.memory.get_byte(&self.registers.program_counter); + self.registers.program_counter.add(&AddressDiff(1)); + instr + } + + pub fn decode_instruction(&mut self, raw_instruction: u8) -> Instruction { + ADC + } + + pub fn execute_instruction(&mut self, instruction: Instruction) { + match instruction { + ADC => { + println!("executing add with carry"); + self.add_with_carry(1); + } + _ => println!("attempting to execute unimplemented instruction") + }; + } // TODO akeeton: Implement binary-coded decimal. pub fn add_with_carry(&mut self, value: i8) {