working on input, still not quite there
This commit is contained in:
parent
b6b07c036f
commit
c4e25f3524
35
src/cpu.rs
35
src/cpu.rs
@ -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,9 +141,10 @@ 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 => {
|
||||||
//Set Vx = Vx SHL 1.
|
//8xyE - SHL Vx {, Vy}
|
||||||
//If the most-significant bit of Vx is 1, then VF is set to 1, otherwise to 0. Then Vx is multiplied by 2.
|
//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];
|
self.reg[x] = self.reg[y];
|
||||||
let msb = (self.reg[x] >> 7) & 1;
|
let msb = (self.reg[x] >> 7) & 1;
|
||||||
self.reg[x] <<= 1;
|
self.reg[x] <<= 1;
|
||||||
@ -200,15 +203,33 @@ 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 => {
|
||||||
// Very interesting op
|
// BCD conversion isn't something I've encountered before
|
||||||
|
// Very interesting op
|
||||||
let vx = self.reg[x];
|
let vx = self.reg[x];
|
||||||
let hundreds = vx / 100;
|
let hundreds = vx / 100;
|
||||||
let tens = (vx / 10) % 10;
|
let tens = (vx / 10) % 10;
|
||||||
|
|||||||
42
src/main.rs
42
src/main.rs
@ -25,30 +25,34 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let keys = [
|
|
||||||
window.is_key_down(Key::X), // 0
|
|
||||||
window.is_key_down(Key::Key1), // 1
|
|
||||||
window.is_key_down(Key::Key2), // 2
|
|
||||||
window.is_key_down(Key::Key3), // 3
|
|
||||||
window.is_key_down(Key::Q), // 4
|
|
||||||
window.is_key_down(Key::W), // 5
|
|
||||||
window.is_key_down(Key::E), // 6
|
|
||||||
window.is_key_down(Key::A), // 7
|
|
||||||
window.is_key_down(Key::S), // 8
|
|
||||||
window.is_key_down(Key::D), // 9
|
|
||||||
window.is_key_down(Key::Z), // A
|
|
||||||
window.is_key_down(Key::C), // B
|
|
||||||
window.is_key_down(Key::Key4), // C
|
|
||||||
window.is_key_down(Key::R), // D
|
|
||||||
window.is_key_down(Key::F), // E
|
|
||||||
window.is_key_down(Key::V), // F
|
|
||||||
];
|
|
||||||
|
|
||||||
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
|
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
|
||||||
|
|
||||||
while window.is_open() {
|
while window.is_open() {
|
||||||
|
let keys = [
|
||||||
|
window.is_key_down(Key::X), // 0
|
||||||
|
window.is_key_down(Key::Key1), // 1
|
||||||
|
window.is_key_down(Key::Key2), // 2
|
||||||
|
window.is_key_down(Key::Key3), // 3
|
||||||
|
window.is_key_down(Key::Q), // 4
|
||||||
|
window.is_key_down(Key::W), // 5
|
||||||
|
window.is_key_down(Key::E), // 6
|
||||||
|
window.is_key_down(Key::A), // 7
|
||||||
|
window.is_key_down(Key::S), // 8
|
||||||
|
window.is_key_down(Key::D), // 9
|
||||||
|
window.is_key_down(Key::Z), // A
|
||||||
|
window.is_key_down(Key::C), // B
|
||||||
|
window.is_key_down(Key::Key4), // C
|
||||||
|
window.is_key_down(Key::R), // D
|
||||||
|
window.is_key_down(Key::F), // E
|
||||||
|
window.is_key_down(Key::V), // F
|
||||||
|
];
|
||||||
|
chip8.prev_keys = chip8.keys;
|
||||||
chip8.keys = keys;
|
chip8.keys = keys;
|
||||||
chip8.tick();
|
for _ in 0..10 {
|
||||||
|
// 10 ticks * 60fps = ~600 cycles/sec
|
||||||
|
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 };
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user