[brw] Remove clone and satisfy the borrow checker

This commit is contained in:
2024-02-18 19:45:34 +05:30
parent ba1c1a1412
commit f6eca76b0c
9 changed files with 87 additions and 76 deletions

View File

@@ -8,12 +8,12 @@ pub enum CpuState{
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Cpu{ pub struct Cpu<'a>{
mapped_memory: MappedMemory, mapped_memory:&'a mut MappedMemory<'a>,
} }
impl Cpu{ impl <'a> Cpu<'a>{
pub fn new(mapped_memory: MappedMemory)->Cpu{ pub fn new(mapped_memory: &'a mut MappedMemory<'a>)->Cpu<'a>{
Cpu{ Cpu{
mapped_memory mapped_memory
} }

View File

@@ -4,6 +4,7 @@ use crate::misc::result::EmulatorResult;
pub const DEVICE_FRAMEBUFFER_SIZE: usize = 256 * 256; pub const DEVICE_FRAMEBUFFER_SIZE: usize = 256 * 256;
#[derive(Debug, Clone)]
pub struct GraphicsProcessor { pub struct GraphicsProcessor {
framebuffer: Box<[u8; DEVICE_FRAMEBUFFER_SIZE]>, framebuffer: Box<[u8; DEVICE_FRAMEBUFFER_SIZE]>,
} }
@@ -20,10 +21,10 @@ impl GraphicsProcessor {
framebuffer 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); 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 &self.framebuffer
} }
} }

View File

@@ -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::endian::{read_big_endian_u16, read_big_endian_u24};
use crate::misc::result::EmulatorResult; use crate::misc::result::EmulatorResult;
#[derive(Debug, Copy, Clone)] #[derive(Debug)]
pub struct MemoryMappedIO { pub struct MemoryMappedIO<'a> {
//FIXME use a keyboard //FIXME use a keyboard
keyboard_bytes: [u8; 2], keyboard_bytes: [u8; 2],
program_counter: ProgramCounter, program_counter: &'a mut ProgramCounter,
//FIXME use a device //FIXME use a device
pixel_reg: u8, pixel_reg: u8,
//FIXME use a device //FIXME use a device
@@ -17,7 +17,7 @@ pub struct MemoryMappedIO {
} }
/// Represents the memory mapped segment of IO. Aggregates the mapping logic. /// Represents the memory mapped segment of IO. Aggregates the mapping logic.
impl MemoryMappedIO { impl <'a> MemoryMappedIO<'a> {
// 2 byte keyboard bits // 2 byte keyboard bits
pub const KEYBOARD_BIT_START: u32 = 0; pub const KEYBOARD_BIT_START: u32 = 0;
const KEYBOARD_BIT_END: u32 = 1; const KEYBOARD_BIT_END: u32 = 1;
@@ -35,7 +35,7 @@ impl MemoryMappedIO {
pub const AUDIO_SAMPLE_BASE_LEN: u32 = 2; 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; 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 { MemoryMappedIO {
keyboard_bytes: [0, 0], keyboard_bytes: [0, 0],
program_counter, 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<u8> { fn try_get_byte(&self, address: u32) -> EmulatorResult<u8> {
let byte = match address { let byte = match address {
Self::KEYBOARD_BIT_START..=Self::KEYBOARD_BIT_END => { Self::KEYBOARD_BIT_START..=Self::KEYBOARD_BIT_END => {

View File

@@ -20,14 +20,14 @@ pub trait Memory {
fn try_set_byte(&mut self, address: u32, value: u8) -> EmulatorResult<()>; fn try_set_byte(&mut self, address: u32, value: u8) -> EmulatorResult<()>;
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct MappedMemory { pub struct MappedMemory<'a> {
memory_mapped_io: MemoryMappedIO, memory_mapped_io: &'a mut MemoryMappedIO<'a>,
ram_memory: RamMemory, ram_memory: &'a mut RamMemory,
} }
impl MappedMemory { impl <'a> MappedMemory<'a> {
pub fn new(memory_mapped_io: MemoryMappedIO, ram_memory: RamMemory) -> MappedMemory { pub fn new(memory_mapped_io: &'a mut MemoryMappedIO<'a>, ram_memory:&'a mut RamMemory) -> MappedMemory<'a> {
MappedMemory { MappedMemory {
memory_mapped_io, memory_mapped_io,
ram_memory, 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<u8> { fn try_get_byte(&self, address: u32) -> EmulatorResult<u8> {
let byte_at_addr = match address { let byte_at_addr = match address {
0..=MMAPPEDIO_END => { 0..=MMAPPEDIO_END => {

View File

@@ -5,7 +5,7 @@ use crate::misc::endian::{read_big_endian_u24, write_big_endian_u24};
use crate::misc::result::EmulatorResult; use crate::misc::result::EmulatorResult;
#[derive(Debug, Default, Copy, Clone)] #[derive(Debug, Default, Clone)]
pub struct ProgramCounter { pub struct ProgramCounter {
/// 24bit location register /// 24bit location register
program_counter_register: [u8; 3], program_counter_register: [u8; 3],
@@ -42,8 +42,11 @@ impl Memory for ProgramCounter {
} }
/// TODO: Set a byte of PC from the memory. /// TODO: Set a byte of PC from the memory.
fn try_set_byte(&mut self, address: u32, value: u8) -> EmulatorResult<()> { fn try_set_byte(&mut self, address: u32, value: u8) -> EmulatorResult<()> {
log::error!("Unsupported Method!"); match address {
todo!() 0..=2 => { self.program_counter_register[address as usize] = value }
_ => { return Err(EmulatorError::UnreachableMemory(PC, address)); }
}
Ok(())
} }
} }

View File

@@ -1,6 +1,7 @@
use crate::misc::emulator_error::EmulatorError; use crate::misc::emulator_error::EmulatorError;
use crate::misc::result::EmulatorResult; use crate::misc::result::EmulatorResult;
#[derive(Debug, Copy, Clone)]
pub struct Color(u8); pub struct Color(u8);
const RED_MULT: u8 = 36; const RED_MULT: u8 = 36;
@@ -8,7 +9,7 @@ const GREEN_MULT: u8 = 6;
impl Color { impl Color {
/// Only first 216 color indices are used. /// Only first 216 color indices are used.
const COLOR_MAX:u8 = 215; const COLOR_MAX: u8 = 215;
const COLOR_FACTOR_8_BIT: u8 = 0x33; const COLOR_FACTOR_8_BIT: u8 = 0x33;
/// This constructs a valid color from rgb triplet /// This constructs a valid color from rgb triplet
pub fn from_rgb(red: u8, green: u8, blue: u8) -> Color { 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<Color> { pub fn try_new(in_mem_color: u8) -> EmulatorResult<Color> {
if in_mem_color > Self::COLOR_MAX { 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)) Ok(Color(in_mem_color))
} }
@@ -52,21 +53,22 @@ mod tests {
let color = Color::try_new(0xff); let color = Color::try_new(0xff);
assert!(color.is_err()) assert!(color.is_err())
} }
#[test] #[test]
pub fn test_from_mem_max(){ pub fn test_from_mem_max() {
let color = Color::try_new(Color::COLOR_MAX).unwrap(); let color = Color::try_new(Color::COLOR_MAX).unwrap();
assert_eq!([255u8;3],color.get_rgb()) assert_eq!([255u8; 3], color.get_rgb())
} }
#[test] #[test]
pub fn from_rgb_zero(){ pub fn from_rgb_zero() {
let color = Color::from_rgb(0,0,0); let color = Color::from_rgb(0, 0, 0);
assert_eq!(0,color.get_mem_byte()) 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())
} }
#[test]
pub fn from_rgb_max() {
let color = Color::from_rgb(255, 255, 255);
assert_eq!(Color::COLOR_MAX, color.get_mem_byte())
}
} }

View File

@@ -5,6 +5,7 @@ pub trait GraphicsAdapter{
fn draw(&mut self,frame_buf:&[u8;DEVICE_FRAMEBUFFER_SIZE])->EmulatorResult<()>; fn draw(&mut self,frame_buf:&[u8;DEVICE_FRAMEBUFFER_SIZE])->EmulatorResult<()>;
} }
#[derive(Debug, Clone)]
pub struct SDLGraphicsAdapter{ pub struct SDLGraphicsAdapter{
graphics_processor: GraphicsProcessor graphics_processor: GraphicsProcessor
} }

View File

@@ -17,48 +17,52 @@ mod graphics;
fn main() -> EmulatorResult<()> { fn main() -> EmulatorResult<()> {
SimpleLogger::new().env().init().unwrap(); SimpleLogger::new().env().init().unwrap();
let program_counter = ProgramCounter::new(); let mut program_counter = ProgramCounter::new();
let mmio = MemoryMappedIO::new(program_counter); let mut mmio = MemoryMappedIO::new(&mut program_counter);
let ram = RamMemory::try_new()?; let mut ram = RamMemory::try_new()?;
let mmu = MappedMemory::new(mmio, ram); let mut mmu = MappedMemory::new(&mut mmio,&mut ram);
for i in 0..10 { // for i in 0..10 {
log::info!("Memory at {} is {}",i,mmu.try_get_byte(i)?); // 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) // let sdl_context = sdl2::init().unwrap();
.position_centered() // let video_subsystem = sdl_context.video().unwrap();
.build() //
.unwrap(); // let window = video_subsystem.window("rust-sdl2 demo", 512, 512)
let mut canvas = window.into_canvas().build().unwrap(); // .position_centered()
// .build()
canvas.set_draw_color(Color::RGB(0, 255, 255)); // .unwrap();
canvas.clear(); // let mut canvas = window.into_canvas().build().unwrap();
canvas.present(); //
let mut event_pump = sdl_context.event_pump().unwrap(); // canvas.set_draw_color(Color::RGB(0, 255, 255));
let mut i = 0; // canvas.clear();
'running: loop { // canvas.present();
i = (i + 1) % 255; // let mut event_pump = sdl_context.event_pump().unwrap();
canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); // let mut i = 0;
canvas.clear(); // 'running: loop {
for event in event_pump.poll_iter() { // i = (i + 1) % 255;
match event { // canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
Event::Quit {..} | // canvas.clear();
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { // for event in event_pump.poll_iter() {
break 'running // match event {
}, // Event::Quit {..} |
event => { // Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
log::info!("Received window event {:?}",event); // 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)); // // The rest of the game loop goes here...
} //
// canvas.present();
// ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
// }
Ok(()) Ok(())
} }

View File

@@ -1,6 +1,6 @@
use std::fmt::Debug; use std::fmt::Debug;
#[derive(Debug)] #[derive(Debug, Copy, Clone)]
pub enum DeviceType { pub enum DeviceType {
RAM, RAM,
MMU, MMU,
@@ -11,7 +11,7 @@ pub enum DeviceType {
GRAPHICS, GRAPHICS,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum EmulatorError { pub enum EmulatorError {
AllocationFailure(DeviceType, &'static str), AllocationFailure(DeviceType, &'static str),
UnreachableMemory(DeviceType, u32), UnreachableMemory(DeviceType, u32),