From 2f0e0b47300009f2105c797a6a0ad2cfc0d7c53f Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Sun, 18 Feb 2024 09:35:58 +0530 Subject: [PATCH] [ref] Refactor memory --- src/emu/cpu.rs | 45 +++++++++++++++++++++++++++++++++ src/emu/iomem.rs | 51 +++++++++++++++++++------------------- src/misc/emulator_error.rs | 2 ++ src/misc/endian.rs | 25 ++++++++----------- 4 files changed, 83 insertions(+), 40 deletions(-) diff --git a/src/emu/cpu.rs b/src/emu/cpu.rs index e69de29..37555a6 100644 --- a/src/emu/cpu.rs +++ b/src/emu/cpu.rs @@ -0,0 +1,45 @@ +use crate::emu::mmu::{MappedMemory, Memory}; +use crate::misc::result::EmulatorResult; + +#[derive(Debug)] +pub enum CpuState{ + Running, + Paused +} + +#[derive(Debug)] +pub struct Cpu{ + mapped_memory: MappedMemory, +} + +impl Cpu{ + pub fn new(mapped_memory: MappedMemory)->Cpu{ + Cpu{ + mapped_memory + } + } + pub fn cycle()->EmulatorResult<()>{ + todo!(); + Ok(()) + } + fn set_pc(&self,value: &[u8;3])->EmulatorResult<()>{ + todo!() + // for bytete in value { + // self.mapped_memory.try_set_byte() + // }try_set_byte + } +} + + + + + +#[cfg(test)] +mod test{ + + #[test] + pub fn construct(){ + + } + +} \ No newline at end of file diff --git a/src/emu/iomem.rs b/src/emu/iomem.rs index 7d83ef6..04a12e5 100644 --- a/src/emu/iomem.rs +++ b/src/emu/iomem.rs @@ -1,6 +1,6 @@ use crate::emu::mmu::Memory; use crate::misc::emulator_error::EmulatorError; -use crate::misc::endian::MemoryOperations; +use crate::misc::endian::{read_big_endian_u16, read_big_endian_u24}; use crate::misc::result::EmulatorResult; #[derive(Debug, Copy, Clone)] @@ -15,6 +15,23 @@ pub struct MemoryMappedIO { } impl MemoryMappedIO { + // 2 byte keyboard bits + pub const KEYBOARD_BIT_START: u32 = 0; + const KEYBOARD_BIT_END: u32 = 1; + + // 3 bytes PC + const PC_START_ADDR: u32 = 2; + const PC_LEN: u32 = 3; + const PC_END_ADDR: u32 = Self::PC_START_ADDR + Self::PC_LEN - 1; + + // 1 byte pixel base reg + pub const PIXEL_BASE: u32 = 5; + + // 2 byte audio sample base reg + pub const AUDIO_SAMPLE_BASE_START: u32 = 6; + 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() -> MemoryMappedIO { MemoryMappedIO { keyboard_bytes: [0, 0], @@ -24,45 +41,29 @@ impl MemoryMappedIO { } } } -// 2 byte keyboard bits -const KEYBOARD_BIT_START: u32 = 0; -const KEYBOARD_BIT_END: u32 = 1; - -// 3 bytes PC -const PC_START_ADDR: u32 = 2; -const PC_LEN: u32 = 3; -const PC_END_ADDR: u32 = PC_START_ADDR + PC_LEN - 1; - -// 1 byte pixel base reg -const PIXEL_BASE: u32 = 5; - -// 2 byte audio sample base reg -const AUDIO_SAMPLE_BASE_START: u32 = 6; -const AUDIO_SAMPLE_BASE_LEN: u32 = 2; -const AUDIO_SAMPLE_BASE_END: u32 = AUDIO_SAMPLE_BASE_START + AUDIO_SAMPLE_BASE_LEN - 1; impl Memory for MemoryMappedIO { fn try_get_byte(&self, address: u32) -> EmulatorResult { let byte = match address { - KEYBOARD_BIT_START..=KEYBOARD_BIT_END => { + Self::KEYBOARD_BIT_START..=Self::KEYBOARD_BIT_END => { let addr_usize = address as usize; let keyboard_byte = self.keyboard_bytes[addr_usize]; - log::trace!("Fetching keyboard({}) byte segment {} -> {}",MemoryOperations::read_big_endian_u16(&self.keyboard_bytes),address,keyboard_byte); + log::trace!("Fetching keyboard({}) byte segment {} -> {}",read_big_endian_u16(&self.keyboard_bytes),address,keyboard_byte); keyboard_byte } - PC_START_ADDR..=PC_END_ADDR => { - let pc_index = (address - PC_START_ADDR) as usize; + Self::PC_START_ADDR..=Self::PC_END_ADDR => { + let pc_index = (address - Self::PC_START_ADDR) as usize; let pc_byte = self.program_counter[pc_index]; - log::trace!("Fetching PC({}) byte segment {} -> {}",MemoryOperations::read_big_endian_u24(&self.program_counter),pc_index,pc_byte); + log::trace!("Fetching PC({}) byte segment {} -> {}",read_big_endian_u24(&self.program_counter),pc_index,pc_byte); pc_byte } - PIXEL_BASE => { + Self::PIXEL_BASE => { log::trace!("Fetching pixel base reg {}",self.pixel_reg); self.pixel_reg } - AUDIO_SAMPLE_BASE_START => self.audio_sample_address_base[0], - AUDIO_SAMPLE_BASE_END => self.audio_sample_address_base[1], + Self::AUDIO_SAMPLE_BASE_START => self.audio_sample_address_base[0], + Self::AUDIO_SAMPLE_BASE_END => self.audio_sample_address_base[1], _ => { panic!("Unreachable code") } }; Ok(byte) diff --git a/src/misc/emulator_error.rs b/src/misc/emulator_error.rs index b67e0b9..18ab97f 100644 --- a/src/misc/emulator_error.rs +++ b/src/misc/emulator_error.rs @@ -4,6 +4,8 @@ use std::fmt::Debug; pub enum DeviceType { RAM, MMU, + /// Program counter + PC, KEYBOARD, AUDIO, GRAPHICS, diff --git a/src/misc/endian.rs b/src/misc/endian.rs index 72f0bf3..8db0b57 100644 --- a/src/misc/endian.rs +++ b/src/misc/endian.rs @@ -1,18 +1,13 @@ use byteorder::{BigEndian, ByteOrder}; -pub struct MemoryOperations(); - -impl MemoryOperations { - /// read a 24bit endian value, and place it in a u32 num - pub fn read_big_endian_u24(input: &[u8; 3]) -> u32 { - BigEndian::read_u24(input) - } - - pub fn read_big_endian_u16(input: &[u8; 2]) -> u16 { - BigEndian::read_u16(input) - } - /// Write 24-bit endian number into slice - pub fn write_big_endian_u24(input: u32, output_slice: &mut [u8; 3]) { - BigEndian::write_u24(output_slice, input); - } +pub fn read_big_endian_u24(input: &[u8; 3]) -> u32 { + BigEndian::read_u24(input) } + +pub fn read_big_endian_u16(input: &[u8; 2]) -> u16 { + BigEndian::read_u16(input) +} +/// Write 24-bit endian number into slice +pub fn write_big_endian_u24(input: u32, output_slice: &mut [u8; 3]) { + BigEndian::write_u24(output_slice, input); +} \ No newline at end of file