[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)]
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
}

View File

@@ -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]>,
}

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::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<u8> {
let byte = match address {
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<()>;
}
#[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<u8> {
let byte_at_addr = match address {
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;
#[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(())
}
}

View File

@@ -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;
@@ -19,7 +20,7 @@ impl Color {
}
pub fn try_new(in_mem_color: u8) -> EmulatorResult<Color> {
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,6 +53,7 @@ mod tests {
let color = Color::try_new(0xff);
assert!(color.is_err())
}
#[test]
pub fn test_from_mem_max() {
let color = Color::try_new(Color::COLOR_MAX).unwrap();
@@ -63,10 +65,10 @@ mod tests {
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())
}
}

View File

@@ -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
}

View File

@@ -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(())
}

View File

@@ -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),