From ec9b7c48257b69b805a7d679eb711d2959ce7a86 Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Thu, 20 Feb 2025 17:08:30 +0000 Subject: [PATCH] add: registers --- src/device/device.rs | 30 ++++-------------------------- src/device/mod.rs | 1 + src/device/registers.rs | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 src/device/registers.rs diff --git a/src/device/device.rs b/src/device/device.rs index 3b23bfb..c49bd71 100644 --- a/src/device/device.rs +++ b/src/device/device.rs @@ -7,6 +7,8 @@ use std::sync::{Arc, Mutex}; use std::thread::sleep; use std::time::Duration; +use super::registers::RegisterFile; + pub struct Device { pub registers: RegisterFile, pub memory: Box<[u8; Self::DEVICE_MEMORY_SIZE]>, @@ -34,7 +36,7 @@ impl Device { .unwrap(); log::trace!("Successfully initiated device memory"); Device { - registers: RegisterFile::new(), + registers: RegisterFile::default(), memory, frame_buffer: fb, stack: Vec::with_capacity(16), @@ -46,10 +48,10 @@ impl Device { } impl Device { + pub const ROM_START: usize = 0x200; const FONT_HEIGHT: u16 = 5; const FONT_DEFAULT_MEM_LOCATION_START: usize = 0x50; const FONT_DEFAULT_MEM_LOCATION_END: usize = 0x9F; - const ROM_START: usize = 0x200; pub fn cycle(&mut self) -> EmulatorResult<()> { let time_start = std::time::Instant::now(); @@ -240,9 +242,6 @@ impl Device { self.registers.i = addn_res; } Instruction::GetKey(x) => { - // if !self.device_keyboard.query_key_down(key_expected) { - // self.registers.pc -= 2; - // } let mut possible_presses = (0..=0xfu8).filter(|x|{self.device_keyboard.query_key_down(*x)}); let pressed = possible_presses.next(); if let Some(pressed_key) = pressed { @@ -252,7 +251,6 @@ impl Device { } else{ self.registers.pc -= 2; } - // let key_expected = self.registers.v[x]; } Instruction::SetIndexToFontCharacter(x) => { let requested_char = self.registers.v[x]; @@ -382,23 +380,3 @@ impl Drop for Device { self.timer.send_stop_signal() } } - -#[derive(Debug)] -pub struct RegisterFile { - pub v: [u8; 0x10], - /// program counter - only u12 technically. - pub pc: u16, - /// stack pointer - pub i: u16, -} - -impl RegisterFile { - pub const DEFAULT_PC_VALUE: u16 = Device::ROM_START as u16; - pub fn new() -> RegisterFile { - RegisterFile { - v: [0; 0x10], - pc: Self::DEFAULT_PC_VALUE, - i: 0, - } - } -} diff --git a/src/device/mod.rs b/src/device/mod.rs index 2bf1397..38d58f7 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -2,5 +2,6 @@ pub mod timer; pub mod keyboard; pub mod instruction; mod device; +mod registers; pub use device::*; diff --git a/src/device/registers.rs b/src/device/registers.rs new file mode 100644 index 0000000..d99a733 --- /dev/null +++ b/src/device/registers.rs @@ -0,0 +1,22 @@ +use super::Device; + + +#[derive(Debug)] +pub struct RegisterFile { + pub v: [u8; 0x10], + /// program counter - only u12 technically. + pub pc: u16, + /// stack pointer + pub i: u16, +} + +impl RegisterFile { + pub const DEFAULT_PC_VALUE: u16 = Device::ROM_START as u16; +} + +impl Default for RegisterFile{ + fn default() -> Self { + Self { v: [0;0x10], pc: Self::DEFAULT_PC_VALUE, i: 0 } + } +} +