From 757d01fc2582cf50ac068ccb7860e91a2d893a62 Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Sat, 17 Feb 2024 08:44:14 +0530 Subject: [PATCH] [dbg] Add endian order operations --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- src/emu/endian.rs | 15 --------------- src/emu/iomem.rs | 24 ++++++++++++++++++------ src/emu/mmu.rs | 2 ++ src/emu/mod.rs | 1 - src/main.rs | 1 + src/misc/endian.rs | 18 ++++++++++++++++++ src/misc/mod.rs | 1 + 9 files changed, 49 insertions(+), 23 deletions(-) delete mode 100644 src/emu/endian.rs create mode 100644 src/misc/endian.rs create mode 100644 src/misc/mod.rs diff --git a/Cargo.lock b/Cargo.lock index b492473..f05831e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -54,11 +54,18 @@ dependencies = [ name = "byte-pusher-emu" version = "0.1.0" dependencies = [ + "byteorder", "clap", "log", "simple_logger", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "clap" version = "4.5.1" diff --git a/Cargo.toml b/Cargo.toml index 415b85b..8e1a55d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] clap = { version = "4.5", features = ["derive"] } log = "0.4.20" -simple_logger = "4.3" \ No newline at end of file +simple_logger = "4.3" +byteorder = "1.5" \ No newline at end of file diff --git a/src/emu/endian.rs b/src/emu/endian.rs deleted file mode 100644 index fea9654..0000000 --- a/src/emu/endian.rs +++ /dev/null @@ -1,15 +0,0 @@ - -/// read a 24bit endian value, and place it in a u32 num -pub fn read_big_endian_tribyte(input:&[u8;3]) ->u32{ - todo!() -} - -pub fn read_big_endian_byte_array(input:&[u8]) ->u32{ - todo!() -} - - -/// Get 24 bit big endian slice -pub fn get_big_endian(input:u32)->[u8;3]{ - todo!() -} \ No newline at end of file diff --git a/src/emu/iomem.rs b/src/emu/iomem.rs index ca9127c..060efd9 100644 --- a/src/emu/iomem.rs +++ b/src/emu/iomem.rs @@ -1,32 +1,42 @@ use crate::emu::mmu::Memory; +use crate::misc::endian::MemoryOperations; #[derive(Debug, Copy, Clone)] pub struct MemoryMappedIO { - keyboard_bits: [u8;2], + //FIXME use a keyboard + keyboard_bytes: [u8;2], program_counter: [u8; 3], + //FIXME use a device pixel_reg: u8, + //FIXME use a device audio_sample_address_base: [u8; 2], } impl MemoryMappedIO { pub fn new() -> MemoryMappedIO { MemoryMappedIO { - keyboard_bits: [0,0], + keyboard_bytes: [0,0], program_counter: [0, 0, 0], pixel_reg: 0, audio_sample_address_base: [0, 0], } } } +// 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; @@ -36,13 +46,15 @@ impl Memory for MemoryMappedIO { match address { KEYBOARD_BIT_START..=KEYBOARD_BIT_END => { let addr_usize = address as usize; - log::trace!("Fetching keyboard bits segment {}",addr_usize); - self.keyboard_bits[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); + keyboard_byte }, PC_START_ADDR..=PC_END_ADDR => { let pc_index = (address - PC_START_ADDR) as usize; - log::trace!("Fetching PC {:?} bit segment {}",self.program_counter,pc_index); - 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); + pc_byte }, PIXEL_BASE => { log::trace!("Fetching pixel base reg {}",self.pixel_reg); diff --git a/src/emu/mmu.rs b/src/emu/mmu.rs index e29485e..1c47db6 100644 --- a/src/emu/mmu.rs +++ b/src/emu/mmu.rs @@ -1,8 +1,10 @@ use crate::emu::iomem::MemoryMappedIO; use crate::emu::mem::RamMemory; +// 8 bytes that are memory mapped i/o pub const MMAPPEDIO_END: u32 = RAM_MEM_START-1; pub const RAM_MEM_START:u32 = 8; +/// Last index of ram pub const RAM_MEM_END: u32 = 2<<24 - 1; /// diff --git a/src/emu/mod.rs b/src/emu/mod.rs index 6dae1b1..0eaa0f9 100644 --- a/src/emu/mod.rs +++ b/src/emu/mod.rs @@ -1,5 +1,4 @@ pub mod cpu; pub mod mmu; pub mod mem; -mod endian; pub mod iomem; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 697edd9..e1d4244 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use crate::emu::mmu::{Memory, MappedMemory}; mod emu; mod args; +mod misc; fn main() { SimpleLogger::new().init().unwrap(); diff --git a/src/misc/endian.rs b/src/misc/endian.rs new file mode 100644 index 0000000..72f0bf3 --- /dev/null +++ b/src/misc/endian.rs @@ -0,0 +1,18 @@ +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); + } +} diff --git a/src/misc/mod.rs b/src/misc/mod.rs new file mode 100644 index 0000000..52b9862 --- /dev/null +++ b/src/misc/mod.rs @@ -0,0 +1 @@ +pub mod endian; \ No newline at end of file