summaryrefslogtreecommitdiff
path: root/src/ram.rs
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2020-03-05 00:33:55 +0300
committerOxore <oxore@protonmail.com>2020-03-05 00:36:29 +0300
commit19e0e4cb5f26df537bf07b428700bea30460fd5b (patch)
tree8f55e2525d107d28899aa3b486c3c8485965aa04 /src/ram.rs
parent7d4dfd8af842796c4eb8a88df0cc5ab30f99232d (diff)
Use bus for all memory types, etc.
Rename Memory to Bus
Diffstat (limited to 'src/ram.rs')
-rw-r--r--src/ram.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/ram.rs b/src/ram.rs
new file mode 100644
index 0000000..7027a0e
--- /dev/null
+++ b/src/ram.rs
@@ -0,0 +1,50 @@
+use super::bus::Bus;
+use std::fmt;
+
+const RESET_VALUE_ACC: u8 = 0x00;
+const RESET_VALUE_SP: u8 = 0x07;
+
+pub const RAM_OFFSET_ACC: u8 = 0xE0;
+pub const RAM_OFFSET_SP: u8 = 0x80;
+
+pub struct Ram {
+ array: [u8; u8::max_value() as usize + 1],
+}
+
+impl fmt::Debug for Ram {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let mut v: Vec<char> = vec![];
+ for (c, x) in self.array.iter().enumerate() {
+ for hex_char in format!("{:02X}", *x).chars() {
+ v.push(hex_char);
+ }
+ v.push(' ');
+ if c % 8 == 7 {
+ v.push('\n');
+ }
+ }
+ write!(f, "{}", v.iter().collect::<String>())
+ }
+}
+
+impl Ram {
+ /// Empty constructor
+ pub fn new() -> Self {
+ let mut array = [0; u8::max_value() as usize + 1];
+ array[RAM_OFFSET_SP as usize] = RESET_VALUE_SP;
+ array[RAM_OFFSET_ACC as usize] = RESET_VALUE_ACC;
+ Self {
+ array,
+ }
+ }
+}
+
+impl Bus<u8> for Ram {
+ fn get(&self, a: u8) -> u8 {
+ self.array[a as usize]
+ }
+
+ fn set(&mut self, a: u8, v: u8) {
+ self.array[a as usize] = v;
+ }
+}