use crate::{Inner, Memory, Register, State}; pub struct CPU { pub memory: Memory, pub state: State, } impl CPU { pub fn exec_next(&mut self) -> u8 { let opcode = self.next_opcode(); match opcode { 0x0 => { // noop } 0x01 => { self.state.bc = self.ld_immediate_word(); } 0x11 => { self.state.de = self.ld_immediate_word(); } 0x21 => { self.state.hl = self.ld_immediate_word(); } 0x2C => { unsafe { self.state.hl.as_u8s.right += 1; }; } 0x3E => { self.state.af.as_u8s.left = self.ld_immediate_byte(); } 0x4A => { unsafe { self.state.bc.as_u8s.right = self.state.de.as_u8s.left; }; } 0x4B => { unsafe { self.state.bc.as_u8s.right = self.state.de.as_u8s.right; }; } 0x53 => { unsafe { self.state.de.as_u8s.left = self.state.de.as_u8s.right; }; } 0x66 => { unsafe { self.state.hl.as_u8s.left = self.memory.get(self.state.hl.as_u16); }; } 0xC3 => { self.state.pc = self.ld_immediate_word(); } 0xEA => { unsafe { let address = self.ld_immediate_word().as_u16; self.memory.set(address, self.state.af.as_u8s.left); }; } _ => { panic!("unimplemented opcode: {:#X}", opcode); } }; return opcode; } fn next_opcode(&mut self) -> u8 { unsafe { let opcode = self.memory.get(self.state.pc.as_u16); self.state.pc.as_u16 += 0x1; return opcode; }; } fn ld_immediate_word(&mut self) -> Register { Register { as_u8s: Inner { left: self.next_opcode(), right: self.next_opcode(), }, } } fn ld_immediate_byte(&mut self) -> u8 { self.next_opcode() } }