-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
emu: Refactor memory to consist of a bus with traits
- Loading branch information
Showing
7 changed files
with
150 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
mod ram; | ||
|
||
pub use ram::Ram; | ||
|
||
pub trait Writeable { | ||
fn data_mut(&mut self) -> &mut [u8]; | ||
|
||
fn load(&mut self, start_address: u16, data: &[u8]) { | ||
let mut address = start_address; | ||
for byte in data { | ||
self.write_byte(address, *byte); | ||
address += 1; | ||
} | ||
} | ||
fn write_byte(&mut self, address: u16, data: u8) { | ||
self.data_mut()[address as usize] = data; | ||
} | ||
fn write_word(&mut self, address: u16, data: u16) { | ||
self.data_mut()[address as usize] = (data & 0xff) as u8; | ||
self.data_mut()[(address + 1) as usize] = ((data >> 8) & 0xff) as u8; | ||
} | ||
} | ||
|
||
pub trait Readable { | ||
fn data(&self) -> &[u8]; | ||
fn dump(&self, start_address: u16, end_address: u16) { | ||
for address in start_address..end_address { | ||
eprintln!("{:#06x}: {:#04x}", address, self.read_byte(address)); | ||
} | ||
} | ||
|
||
fn read_byte(&self, address: u16) -> u8 { | ||
self.data()[address as usize] | ||
} | ||
|
||
/// Reads a 16-bit word from memory. | ||
/// The 16-bit word is stored in little-endian format. | ||
fn read_word(&self, address: u16) -> u16 { | ||
self.data()[address as usize] as u16 | ((self.data()[(address + 1) as usize] as u16) << 8) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Bus { | ||
ram: Ram, | ||
} | ||
|
||
impl Default for Bus { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Bus { | ||
pub const BUS_SIZE: usize = 64 * 1024; | ||
|
||
#[tracing::instrument] | ||
pub fn new() -> Self { | ||
Self { ram: Ram::new() } | ||
} | ||
|
||
pub fn load(&mut self, start_address: u16, data: &[u8]) { | ||
self.ram.load(start_address, data); | ||
} | ||
} | ||
|
||
impl Readable for Bus { | ||
fn data(&self) -> &[u8] { | ||
self.ram.data() | ||
} | ||
} | ||
|
||
impl Writeable for Bus { | ||
fn data_mut(&mut self) -> &mut [u8] { | ||
self.ram.data_mut() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use super::{Bus, Readable, Writeable}; | ||
|
||
#[derive(Debug)] | ||
pub struct Ram { | ||
data: [u8; Bus::BUS_SIZE], | ||
#[allow(dead_code)] // Not used at the moment, but would be useful for memory-mapped I/O | ||
size: usize, | ||
} | ||
|
||
impl Ram { | ||
pub fn new() -> Self { | ||
Self { | ||
data: [0; Bus::BUS_SIZE], | ||
size: Bus::BUS_SIZE, | ||
} | ||
} | ||
|
||
pub fn _size(&self) -> usize { | ||
self.size | ||
} | ||
} | ||
|
||
impl Default for Ram { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Readable for Ram { | ||
fn data(&self) -> &[u8] { | ||
&self.data | ||
} | ||
} | ||
|
||
impl Writeable for Ram { | ||
fn data_mut(&mut self) -> &mut [u8] { | ||
&mut self.data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.