[ref] Refactor memory

This commit is contained in:
2024-02-18 09:35:58 +05:30
parent 5701808e77
commit 2f0e0b4730
4 changed files with 83 additions and 40 deletions

View File

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

View File

@@ -1,6 +1,6 @@
use crate::emu::mmu::Memory; use crate::emu::mmu::Memory;
use crate::misc::emulator_error::EmulatorError; 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; use crate::misc::result::EmulatorResult;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@@ -15,6 +15,23 @@ pub struct MemoryMappedIO {
} }
impl 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 { pub fn new() -> MemoryMappedIO {
MemoryMappedIO { MemoryMappedIO {
keyboard_bytes: [0, 0], 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 { impl Memory for MemoryMappedIO {
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 {
KEYBOARD_BIT_START..=KEYBOARD_BIT_END => { Self::KEYBOARD_BIT_START..=Self::KEYBOARD_BIT_END => {
let addr_usize = address as usize; let addr_usize = address as usize;
let keyboard_byte = self.keyboard_bytes[addr_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 keyboard_byte
} }
PC_START_ADDR..=PC_END_ADDR => { Self::PC_START_ADDR..=Self::PC_END_ADDR => {
let pc_index = (address - PC_START_ADDR) as usize; let pc_index = (address - Self::PC_START_ADDR) as usize;
let pc_byte = self.program_counter[pc_index]; 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 pc_byte
} }
PIXEL_BASE => { Self::PIXEL_BASE => {
log::trace!("Fetching pixel base reg {}",self.pixel_reg); log::trace!("Fetching pixel base reg {}",self.pixel_reg);
self.pixel_reg self.pixel_reg
} }
AUDIO_SAMPLE_BASE_START => self.audio_sample_address_base[0], Self::AUDIO_SAMPLE_BASE_START => self.audio_sample_address_base[0],
AUDIO_SAMPLE_BASE_END => self.audio_sample_address_base[1], Self::AUDIO_SAMPLE_BASE_END => self.audio_sample_address_base[1],
_ => { panic!("Unreachable code") } _ => { panic!("Unreachable code") }
}; };
Ok(byte) Ok(byte)

View File

@@ -4,6 +4,8 @@ use std::fmt::Debug;
pub enum DeviceType { pub enum DeviceType {
RAM, RAM,
MMU, MMU,
/// Program counter
PC,
KEYBOARD, KEYBOARD,
AUDIO, AUDIO,
GRAPHICS, GRAPHICS,

View File

@@ -1,18 +1,13 @@
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
pub struct MemoryOperations(); pub fn read_big_endian_u24(input: &[u8; 3]) -> u32 {
BigEndian::read_u24(input)
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_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);
}