diff --git a/src/device/cpu.rs b/src/device/cpu.rs index 2011745..aec0334 100644 --- a/src/device/cpu.rs +++ b/src/device/cpu.rs @@ -1,6 +1,52 @@ +use byteorder::{BigEndian, ByteOrder}; +use crate::device::cpu::Instruction::{ClearScreen, JumpTo}; +#[derive(Eq, PartialEq, Debug)] enum Instruction{ ClearScreen, + /// Jump to location JumpTo(u16), - + SetRegister(usize,u8), + AddValueToRegister(usize,u8), + SetIndex(u16), + /// + DRAW(usize,usize,u8) } +impl Instruction{ + pub fn parse_fetched_instruction(location: &[u8])->Instruction{ + assert_eq!(location.len(),2); + let instruction = BigEndian::read_u16(location); + + if instruction == 0xe0{ + return ClearScreen; + } else if (instruction & 0x1000)==0x1000 { + return JumpTo(instruction & 0xfff); + } + todo!(); + } +} + +#[cfg(test)] +mod tests{ + use crate::device::cpu::Instruction; + use crate::device::cpu::Instruction::{ClearScreen, JumpTo}; + + #[test] + fn test_clear_screen(){ + let instruction_bytes = 0x00e0_u16.to_be_bytes(); + let ins = Instruction::parse_fetched_instruction(&instruction_bytes); + assert_eq!(ins,ClearScreen); + } + #[test] + fn test_jump_to_instruction_1(){ + let instruction_bytes = 0x1123_u16.to_be_bytes(); + let ins = Instruction::parse_fetched_instruction(&instruction_bytes); + assert_eq!(ins,JumpTo(0x123)); + } + #[test] + fn test_jump_to_instruction_2(){ + let instruction_bytes = 0x1faf_u16.to_be_bytes(); + let ins = Instruction::parse_fetched_instruction(&instruction_bytes); + assert_eq!(ins,JumpTo(0xfaf)); + } +} \ No newline at end of file diff --git a/src/device/device.rs b/src/device/device.rs index 668172f..ef5ca57 100644 --- a/src/device/device.rs +++ b/src/device/device.rs @@ -47,13 +47,20 @@ impl Device{ ]; const FONT_DEFAULT_MEM_LOCATION_START:usize = 0x50; const FONT_DEFAULT_MEM_LOCATION_END:usize = 0x9F; + const ROM_START:usize = 0x200; pub fn cycle(&mut self){ } pub fn set_default_font(&mut self){ + log::info!("Loaded default font from memory"); self.memory[Self::FONT_DEFAULT_MEM_LOCATION_START..=Self::FONT_DEFAULT_MEM_LOCATION_END].copy_from_slice(&Self::DEFAULT_FONT); } + /// load a rom from bytes + pub fn load_rom(&mut self,rom: &[u8]){ + log::info!("Loaded ROM from memory"); + self.memory[Self::ROM_START..].copy_from_slice(rom); + } } impl Drop for Device{ @@ -64,17 +71,18 @@ impl Drop for Device{ pub struct RegisterFile { pub v: [u8; 0x10], - // program counter - only u12 technically. + /// program counter - only u12 technically. pub pc: u16, /// stack pointer pub i: u16, } impl RegisterFile { + pub const DEFAULT_PC_VALUE:u16 = Device::ROM_START as u16; pub fn new() -> RegisterFile { RegisterFile { v: [0; 0x10], - pc: 0x200, + pc: Self::DEFAULT_PC_VALUE, i: 0, } } diff --git a/src/main.rs b/src/main.rs index c1bc00e..a3e15ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,6 @@ fn main() { device.set_default_font(); let mut i = 0; - - }