working on input, still not quite there

This commit is contained in:
Butter 2026-04-08 09:17:52 -04:00
parent b6b07c036f
commit c4e25f3524
2 changed files with 51 additions and 26 deletions

View File

@ -1,4 +1,4 @@
use crate::cpu; // use crate::cpu;
use rand::{self, RngExt}; use rand::{self, RngExt};
const FONT: [u8; 80] = [ const FONT: [u8; 80] = [
@ -30,6 +30,7 @@ pub struct Cpu {
delay_timer: u8, delay_timer: u8,
sound_timer: u8, sound_timer: u8,
pub keys: [bool; 16], pub keys: [bool; 16],
pub prev_keys: [bool; 16],
} }
impl Cpu { impl Cpu {
@ -45,6 +46,7 @@ impl Cpu {
delay_timer: 0, delay_timer: 0,
sound_timer: 0, sound_timer: 0,
keys: [false; 16], keys: [false; 16],
prev_keys: [false; 16],
}; };
cpu.load_font(); cpu.load_font();
cpu.load_rom(rom); cpu.load_rom(rom);
@ -139,7 +141,8 @@ impl Cpu {
self.reg[x] = result; self.reg[x] = result;
self.reg[0xF] = not_borrow; self.reg[0xF] = not_borrow;
} }
0xE => { //8xyE - SHL Vx {, Vy} 0xE => {
//8xyE - SHL Vx {, Vy}
//Set Vx = Vx SHL 1. //Set Vx = Vx SHL 1.
//If the most-significant bit of Vx is 1, then VF is set to 1, otherwise to 0. Then Vx is multiplied by 2. //If the most-significant bit of Vx is 1, then VF is set to 1, otherwise to 0. Then Vx is multiplied by 2.
self.reg[x] = self.reg[y]; self.reg[x] = self.reg[y];
@ -200,14 +203,32 @@ impl Cpu {
}, },
0xF => match nn { 0xF => match nn {
0x07 => self.reg[x] = self.delay_timer, 0x07 => self.reg[x] = self.delay_timer,
0x0A => {} 0x0A => {
// this is supposed to block execution until a key is pressed
let mut released_key = None;
for i in 0..16 {
// If it was pressed in the previous frame but is NOT pressed now
if self.prev_keys[i] && !self.keys[i] {
released_key = Some(i as u8);
break;
}
}
if let Some(key_code) = released_key {
self.reg[x] = key_code;
} else {
// Not released yet? Stay on this instruction.
self.program_counter -= 2;
}
}
0x15 => self.delay_timer = self.reg[x], 0x15 => self.delay_timer = self.reg[x],
0x18 => self.sound_timer = self.reg[x], 0x18 => self.sound_timer = self.reg[x],
0x1E => self.index = self.index.wrapping_add(self.reg[x] as u16), 0x1E => self.index = self.index.wrapping_add(self.reg[x] as u16),
0x29 => { 0x29 => {
self.index = ((self.reg[x] * 5) + 0x050) as u16; self.index = ((self.reg[x] * 5) + 0x050) as u16;
} }
0x33 => { // BCD conversion isn't something I've encountered before 0x33 => {
// BCD conversion isn't something I've encountered before
// Very interesting op // Very interesting op
let vx = self.reg[x]; let vx = self.reg[x];
let hundreds = vx / 100; let hundreds = vx / 100;

View File

@ -25,6 +25,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}, },
) )
.unwrap(); .unwrap();
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
while window.is_open() {
let keys = [ let keys = [
window.is_key_down(Key::X), // 0 window.is_key_down(Key::X), // 0
window.is_key_down(Key::Key1), // 1 window.is_key_down(Key::Key1), // 1
@ -43,12 +47,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
window.is_key_down(Key::F), // E window.is_key_down(Key::F), // E
window.is_key_down(Key::V), // F window.is_key_down(Key::V), // F
]; ];
chip8.prev_keys = chip8.keys;
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
while window.is_open() {
chip8.keys = keys; chip8.keys = keys;
for _ in 0..10 {
// 10 ticks * 60fps = ~600 cycles/sec
chip8.tick(); chip8.tick();
}
for (i, pixel) in chip8.display.iter().enumerate() { for (i, pixel) in chip8.display.iter().enumerate() {
buffer[i] = if *pixel { 0xFFFFFFFF } else { 0x00000000 }; buffer[i] = if *pixel { 0xFFFFFFFF } else { 0x00000000 };
} }