Trevor Merritt 67ca71ccb7 my first schip rom works in my schip emulator.
BUGFIX: Corrects runaway after drawing in my first schip rom
scroll down, left, right all test with test rom
assembler now assembles to the expected output it seems
fixes incorrect loading of schip font to memory
replaces schip font from new chatgpt feedback
2024-11-06 10:40:52 -05:00

58 lines
1.5 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::constants::CHIP8_KEYBOARD;
#[derive(Clone, Copy, Default, Serialize, Deserialize, Debug)]
pub struct Keypad {
keys: [bool; 0x10],
}
impl Keypad {
pub fn format_as_string(&self) -> String {
let mut return_value = String::new();
// draw a 4x4 grid showing the keys with * filling the cells that are depressed
for row in CHIP8_KEYBOARD.iter() {
for (index, key) in row.iter().enumerate() {
let is_lit = if self.keys[*key as usize] {
"*".to_string()
} else {
char::from_digit(*key as u32, 16).unwrap_or(' ').to_string()
};
if index == 3 {
return_value += format!("|{}|\n", is_lit).as_str();
} else {
return_value += format!("|{}", is_lit).as_str();
}
}
}
return_value
}
}
impl Keypad {
pub fn push_key(&mut self, key_index: u8) {
self.keys[key_index as usize] = true;
}
pub fn release_key(&mut self, key_index: u8) {
self.keys[key_index as usize] = false;
}
pub fn key_state(&self, key_index: u8) -> bool {
self.keys[key_index as usize]
}
pub fn new() -> Keypad {
Keypad::default()
}
pub fn pressed(&self, key: u8) -> bool {
self.key_state(key)
}
pub fn released(&self, key: u8) -> bool {
!self.key_state(key)
}
}