working on input, still not quite there
This commit is contained in:
parent
b6b07c036f
commit
c4e25f3524
29
src/cpu.rs
29
src/cpu.rs
@ -1,4 +1,4 @@
|
||||
use crate::cpu;
|
||||
// use crate::cpu;
|
||||
use rand::{self, RngExt};
|
||||
|
||||
const FONT: [u8; 80] = [
|
||||
@ -30,6 +30,7 @@ pub struct Cpu {
|
||||
delay_timer: u8,
|
||||
sound_timer: u8,
|
||||
pub keys: [bool; 16],
|
||||
pub prev_keys: [bool; 16],
|
||||
}
|
||||
|
||||
impl Cpu {
|
||||
@ -45,6 +46,7 @@ impl Cpu {
|
||||
delay_timer: 0,
|
||||
sound_timer: 0,
|
||||
keys: [false; 16],
|
||||
prev_keys: [false; 16],
|
||||
};
|
||||
cpu.load_font();
|
||||
cpu.load_rom(rom);
|
||||
@ -139,7 +141,8 @@ impl Cpu {
|
||||
self.reg[x] = result;
|
||||
self.reg[0xF] = not_borrow;
|
||||
}
|
||||
0xE => { //8xyE - SHL Vx {, Vy}
|
||||
0xE => {
|
||||
//8xyE - SHL Vx {, Vy}
|
||||
//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.
|
||||
self.reg[x] = self.reg[y];
|
||||
@ -200,14 +203,32 @@ impl Cpu {
|
||||
},
|
||||
0xF => match nn {
|
||||
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],
|
||||
0x18 => self.sound_timer = self.reg[x],
|
||||
0x1E => self.index = self.index.wrapping_add(self.reg[x] as u16),
|
||||
0x29 => {
|
||||
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
|
||||
let vx = self.reg[x];
|
||||
let hundreds = vx / 100;
|
||||
|
||||
12
src/main.rs
12
src/main.rs
@ -25,6 +25,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
|
||||
|
||||
while window.is_open() {
|
||||
let keys = [
|
||||
window.is_key_down(Key::X), // 0
|
||||
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::V), // F
|
||||
];
|
||||
|
||||
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
|
||||
|
||||
while window.is_open() {
|
||||
chip8.prev_keys = chip8.keys;
|
||||
chip8.keys = keys;
|
||||
for _ in 0..10 {
|
||||
// 10 ticks * 60fps = ~600 cycles/sec
|
||||
chip8.tick();
|
||||
}
|
||||
for (i, pixel) in chip8.display.iter().enumerate() {
|
||||
buffer[i] = if *pixel { 0xFFFFFFFF } else { 0x00000000 };
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user