diff --git a/src/emu/cpu.rs b/src/emu/cpu.rs index 37555a6..65274d1 100644 --- a/src/emu/cpu.rs +++ b/src/emu/cpu.rs @@ -8,12 +8,12 @@ pub enum CpuState{ } #[derive(Debug)] -pub struct Cpu{ - mapped_memory: MappedMemory, +pub struct Cpu<'a>{ + mapped_memory:&'a mut MappedMemory<'a>, } -impl Cpu{ - pub fn new(mapped_memory: MappedMemory)->Cpu{ +impl <'a> Cpu<'a>{ + pub fn new(mapped_memory: &'a mut MappedMemory<'a>)->Cpu<'a>{ Cpu{ mapped_memory } diff --git a/src/emu/graphics.rs b/src/emu/graphics.rs index 925e2bc..e3d994a 100644 --- a/src/emu/graphics.rs +++ b/src/emu/graphics.rs @@ -4,6 +4,7 @@ use crate::misc::result::EmulatorResult; pub const DEVICE_FRAMEBUFFER_SIZE: usize = 256 * 256; +#[derive(Debug, Clone)] pub struct GraphicsProcessor { framebuffer: Box<[u8; DEVICE_FRAMEBUFFER_SIZE]>, } @@ -20,10 +21,10 @@ impl GraphicsProcessor { framebuffer }) } - pub fn set_framebuffer(&mut self,memory_slice: &[u8]){ + pub fn set_framebuffer(&mut self, memory_slice: &[u8]) { self.framebuffer.copy_from_slice(memory_slice); } - pub fn get_framebuffer(&self) -> &[u8;DEVICE_FRAMEBUFFER_SIZE] { + pub fn get_framebuffer(&self) -> &[u8; DEVICE_FRAMEBUFFER_SIZE] { &self.framebuffer } } diff --git a/src/emu/iomem.rs b/src/emu/iomem.rs index 8096026..ae8f373 100644 --- a/src/emu/iomem.rs +++ b/src/emu/iomem.rs @@ -5,11 +5,11 @@ use crate::misc::emulator_error::DeviceType::MMU; use crate::misc::endian::{read_big_endian_u16, read_big_endian_u24}; use crate::misc::result::EmulatorResult; -#[derive(Debug, Copy, Clone)] -pub struct MemoryMappedIO { +#[derive(Debug)] +pub struct MemoryMappedIO<'a> { //FIXME use a keyboard keyboard_bytes: [u8; 2], - program_counter: ProgramCounter, + program_counter: &'a mut ProgramCounter, //FIXME use a device pixel_reg: u8, //FIXME use a device @@ -17,7 +17,7 @@ pub struct MemoryMappedIO { } /// Represents the memory mapped segment of IO. Aggregates the mapping logic. -impl MemoryMappedIO { +impl <'a> MemoryMappedIO<'a> { // 2 byte keyboard bits pub const KEYBOARD_BIT_START: u32 = 0; const KEYBOARD_BIT_END: u32 = 1; @@ -35,7 +35,7 @@ impl MemoryMappedIO { pub const AUDIO_SAMPLE_BASE_LEN: u32 = 2; const AUDIO_SAMPLE_BASE_END: u32 = Self::AUDIO_SAMPLE_BASE_START + Self::AUDIO_SAMPLE_BASE_LEN - 1; - pub fn new(program_counter: ProgramCounter) -> MemoryMappedIO { + pub fn new(program_counter: &'a mut ProgramCounter) -> MemoryMappedIO<'a> { MemoryMappedIO { keyboard_bytes: [0, 0], program_counter, @@ -46,7 +46,7 @@ impl MemoryMappedIO { } -impl Memory for MemoryMappedIO { +impl <'a> Memory for MemoryMappedIO<'a> { fn try_get_byte(&self, address: u32) -> EmulatorResult { let byte = match address { Self::KEYBOARD_BIT_START..=Self::KEYBOARD_BIT_END => { diff --git a/src/emu/mmu.rs b/src/emu/mmu.rs index 6827962..e96bf53 100644 --- a/src/emu/mmu.rs +++ b/src/emu/mmu.rs @@ -20,14 +20,14 @@ pub trait Memory { fn try_set_byte(&mut self, address: u32, value: u8) -> EmulatorResult<()>; } -#[derive(Debug, Clone)] -pub struct MappedMemory { - memory_mapped_io: MemoryMappedIO, - ram_memory: RamMemory, +#[derive(Debug)] +pub struct MappedMemory<'a> { + memory_mapped_io: &'a mut MemoryMappedIO<'a>, + ram_memory: &'a mut RamMemory, } -impl MappedMemory { - pub fn new(memory_mapped_io: MemoryMappedIO, ram_memory: RamMemory) -> MappedMemory { +impl <'a> MappedMemory<'a> { + pub fn new(memory_mapped_io: &'a mut MemoryMappedIO<'a>, ram_memory:&'a mut RamMemory) -> MappedMemory<'a> { MappedMemory { memory_mapped_io, ram_memory, @@ -35,7 +35,7 @@ impl MappedMemory { } } -impl Memory for MappedMemory { +impl <'a> Memory for MappedMemory<'a> { fn try_get_byte(&self, address: u32) -> EmulatorResult { let byte_at_addr = match address { 0..=MMAPPEDIO_END => { diff --git a/src/emu/program_counter.rs b/src/emu/program_counter.rs index cb58860..d348f86 100644 --- a/src/emu/program_counter.rs +++ b/src/emu/program_counter.rs @@ -5,7 +5,7 @@ use crate::misc::endian::{read_big_endian_u24, write_big_endian_u24}; use crate::misc::result::EmulatorResult; -#[derive(Debug, Default, Copy, Clone)] +#[derive(Debug, Default, Clone)] pub struct ProgramCounter { /// 24bit location register program_counter_register: [u8; 3], @@ -42,8 +42,11 @@ impl Memory for ProgramCounter { } /// TODO: Set a byte of PC from the memory. fn try_set_byte(&mut self, address: u32, value: u8) -> EmulatorResult<()> { - log::error!("Unsupported Method!"); - todo!() + match address { + 0..=2 => { self.program_counter_register[address as usize] = value } + _ => { return Err(EmulatorError::UnreachableMemory(PC, address)); } + } + Ok(()) } } diff --git a/src/graphics/color.rs b/src/graphics/color.rs index eddd709..00d9ccc 100644 --- a/src/graphics/color.rs +++ b/src/graphics/color.rs @@ -1,6 +1,7 @@ use crate::misc::emulator_error::EmulatorError; use crate::misc::result::EmulatorResult; +#[derive(Debug, Copy, Clone)] pub struct Color(u8); const RED_MULT: u8 = 36; @@ -8,7 +9,7 @@ const GREEN_MULT: u8 = 6; impl Color { /// Only first 216 color indices are used. - const COLOR_MAX:u8 = 215; + const COLOR_MAX: u8 = 215; const COLOR_FACTOR_8_BIT: u8 = 0x33; /// This constructs a valid color from rgb triplet pub fn from_rgb(red: u8, green: u8, blue: u8) -> Color { @@ -19,7 +20,7 @@ impl Color { } pub fn try_new(in_mem_color: u8) -> EmulatorResult { if in_mem_color > Self::COLOR_MAX { - return Err(EmulatorError::InvalidColor(in_mem_color)) + return Err(EmulatorError::InvalidColor(in_mem_color)); } Ok(Color(in_mem_color)) } @@ -52,21 +53,22 @@ mod tests { let color = Color::try_new(0xff); assert!(color.is_err()) } + #[test] - pub fn test_from_mem_max(){ + pub fn test_from_mem_max() { let color = Color::try_new(Color::COLOR_MAX).unwrap(); - assert_eq!([255u8;3],color.get_rgb()) + assert_eq!([255u8; 3], color.get_rgb()) } #[test] - pub fn from_rgb_zero(){ - let color = Color::from_rgb(0,0,0); - assert_eq!(0,color.get_mem_byte()) - } - #[test] - pub fn from_rgb_max(){ - let color = Color::from_rgb(255,255,255); - assert_eq!(Color::COLOR_MAX,color.get_mem_byte()) + pub fn from_rgb_zero() { + let color = Color::from_rgb(0, 0, 0); + assert_eq!(0, color.get_mem_byte()) } + #[test] + pub fn from_rgb_max() { + let color = Color::from_rgb(255, 255, 255); + assert_eq!(Color::COLOR_MAX, color.get_mem_byte()) + } } \ No newline at end of file diff --git a/src/graphics/graphics_adapter.rs b/src/graphics/graphics_adapter.rs index 71c4991..13b7921 100644 --- a/src/graphics/graphics_adapter.rs +++ b/src/graphics/graphics_adapter.rs @@ -5,6 +5,7 @@ pub trait GraphicsAdapter{ fn draw(&mut self,frame_buf:&[u8;DEVICE_FRAMEBUFFER_SIZE])->EmulatorResult<()>; } +#[derive(Debug, Clone)] pub struct SDLGraphicsAdapter{ graphics_processor: GraphicsProcessor } diff --git a/src/main.rs b/src/main.rs index 4bb5163..4d95aa4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,48 +17,52 @@ mod graphics; fn main() -> EmulatorResult<()> { SimpleLogger::new().env().init().unwrap(); - let program_counter = ProgramCounter::new(); - let mmio = MemoryMappedIO::new(program_counter); - let ram = RamMemory::try_new()?; - let mmu = MappedMemory::new(mmio, ram); - for i in 0..10 { - log::info!("Memory at {} is {}",i,mmu.try_get_byte(i)?); - } + let mut program_counter = ProgramCounter::new(); + let mut mmio = MemoryMappedIO::new(&mut program_counter); + let mut ram = RamMemory::try_new()?; + let mut mmu = MappedMemory::new(&mut mmio,&mut ram); + // for i in 0..10 { + // log::info!("Memory at {} is {}",i,mmu.try_get_byte(i)?); + // } + mmu.try_set_byte(0x2,0x1)?; + let data = program_counter.try_get_byte(0x0)?; + log::info!("Computed data {}",data); - let sdl_context = sdl2::init().unwrap(); - let video_subsystem = sdl_context.video().unwrap(); - let window = video_subsystem.window("rust-sdl2 demo", 512, 512) - .position_centered() - .build() - .unwrap(); - let mut canvas = window.into_canvas().build().unwrap(); - - canvas.set_draw_color(Color::RGB(0, 255, 255)); - canvas.clear(); - canvas.present(); - let mut event_pump = sdl_context.event_pump().unwrap(); - let mut i = 0; - 'running: loop { - i = (i + 1) % 255; - canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); - canvas.clear(); - for event in event_pump.poll_iter() { - match event { - Event::Quit {..} | - Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { - break 'running - }, - event => { - log::info!("Received window event {:?}",event); - } - } - } - // The rest of the game loop goes here... - - canvas.present(); - ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); - } + // let sdl_context = sdl2::init().unwrap(); + // let video_subsystem = sdl_context.video().unwrap(); + // + // let window = video_subsystem.window("rust-sdl2 demo", 512, 512) + // .position_centered() + // .build() + // .unwrap(); + // let mut canvas = window.into_canvas().build().unwrap(); + // + // canvas.set_draw_color(Color::RGB(0, 255, 255)); + // canvas.clear(); + // canvas.present(); + // let mut event_pump = sdl_context.event_pump().unwrap(); + // let mut i = 0; + // 'running: loop { + // i = (i + 1) % 255; + // canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); + // canvas.clear(); + // for event in event_pump.poll_iter() { + // match event { + // Event::Quit {..} | + // Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { + // break 'running + // }, + // event => { + // log::info!("Received window event {:?}",event); + // } + // } + // } + // // The rest of the game loop goes here... + // + // canvas.present(); + // ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); + // } Ok(()) } diff --git a/src/misc/emulator_error.rs b/src/misc/emulator_error.rs index 18ab97f..4a010c3 100644 --- a/src/misc/emulator_error.rs +++ b/src/misc/emulator_error.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub enum DeviceType { RAM, MMU, @@ -11,7 +11,7 @@ pub enum DeviceType { GRAPHICS, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum EmulatorError { AllocationFailure(DeviceType, &'static str), UnreachableMemory(DeviceType, u32),