From ba69b1070bd638077d17fe71ff3c2c73666ef85f Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 16 Jan 2023 11:46:00 +1100 Subject: [PATCH] ld_immediate_word --- src/main.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8dc46bf..495a58a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -116,23 +116,18 @@ struct CPU { impl CPU { fn exec_next(&mut self) -> u8 { let opcode = self.next_opcode(); - println!("opcode: {:#X}", opcode); - match opcode { 0x0 => { // noop } 0x01 => { - self.state.bc.as_u8s.left = self.next_opcode(); - self.state.bc.as_u8s.right = self.next_opcode(); + self.state.bc = self.ld_immediate_word(); } 0x11 => { - self.state.de.as_u8s.left = self.next_opcode(); - self.state.de.as_u8s.right = self.next_opcode(); + self.state.de = self.ld_immediate_word(); } 0x21 => { - self.state.de.as_u8s.left = self.next_opcode(); - self.state.de.as_u8s.right = self.next_opcode(); + self.state.hl = self.ld_immediate_word(); } 0x2C => { unsafe { @@ -145,9 +140,7 @@ impl CPU { }; } 0xC3 => { - let (a, b) = (self.next_opcode(), self.next_opcode()); - self.state.pc.as_u8s.left = a; - self.state.pc.as_u8s.right = b; + self.state.pc = self.ld_immediate_word(); } _ => { panic!("unimplemented opcode: {:#X}", opcode); @@ -155,6 +148,7 @@ impl CPU { }; return opcode; } + fn next_opcode(&mut self) -> u8 { unsafe { let opcode = self.memory.get(self.state.pc.as_u16); @@ -162,6 +156,15 @@ impl CPU { return opcode; }; } + + fn ld_immediate_word(&mut self) -> Register { + Register { + as_u8s: Inner { + left: self.next_opcode(), + right: self.next_opcode(), + }, + } + } } fn main() { @@ -175,10 +178,10 @@ fn main() { }; loop { cpu.exec_next(); - pause(); } } +#[allow(dead_code)] fn pause() { io::stdin().read_line(&mut String::new()).unwrap(); }