Skip to content

Commit

Permalink
Add keyboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanisaacg committed Apr 13, 2023
1 parent dfa7739 commit b9f55cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ impl Chip8 {
}
}

pub fn advance(&mut self) {
pub fn advance(&mut self, keypad: &[bool; 16]) {
let Chip8 {
cpu,
display,
memory,
} = self;
advance(cpu, display, memory);
advance(cpu, display, memory, keypad);
}

pub fn display(&self) -> &Display {
&self.display
}
}

pub fn advance(cpu: &mut CPU, display: &mut Display, memory: &mut Memory) {
pub fn advance(cpu: &mut CPU, display: &mut Display, memory: &mut Memory, keypad: &[bool; 16]) {
let instr = [
memory.get(cpu.program_counter),
memory.get(cpu.program_counter + 1),
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn advance(cpu: &mut CPU, display: &mut Display, memory: &mut Memory) {
} else {
(b as i16) - (a as i16)
};
if dbg!(result) >= 0 {
if result >= 0 {
cpu.set_register(r, result as u8);
cpu.set_register(0xF, 1);
} else {
Expand Down Expand Up @@ -214,7 +214,20 @@ pub fn advance(cpu: &mut CPU, display: &mut Display, memory: &mut Memory) {
}
0xE => {
// skip based on input
// TODO: keyboard input
let op = (instr & 0x0F00) >> 8;
match instr & 0x00FF {
0x9E => {
if keypad[op as usize] {
cpu.program_counter += 2;
}
}
0xA1 => {
if !keypad[op as usize] {
cpu.program_counter += 2;
}
}
_ => panic!("unknown instruction: {}", instr),
}
}
0xF => {
let op = (instr & 0x0F00) >> 8;
Expand All @@ -236,7 +249,11 @@ pub fn advance(cpu: &mut CPU, display: &mut Display, memory: &mut Memory) {
cpu.index_register = result;
}
0x0A => {
// TODO: block and wait for key input
if let Some((key, _)) = keypad.iter().enumerate().filter(|(_, x)| **x).next() {
cpu.set_register(op, key as u8);
} else {
cpu.program_counter -= 2;
}
}
0x29 => {
cpu.index_register = (FONT_START as u16) + (cpu.register(op) as u16) & 0x0F * 5;
Expand Down
24 changes: 21 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crunch::Chip8;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::keyboard::{KeyboardState, Keycode, Scancode};
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use std::time::Duration;
Expand Down Expand Up @@ -53,9 +53,27 @@ pub fn main() -> Result<(), String> {
}
canvas.present();
std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
chip.advance();
let keyboard = KeyboardState::new(&event_pump);
let are_keys_pressed = [
keyboard.is_scancode_pressed(Scancode::X),
keyboard.is_scancode_pressed(Scancode::Num1),
keyboard.is_scancode_pressed(Scancode::Num2),
keyboard.is_scancode_pressed(Scancode::Num3),
keyboard.is_scancode_pressed(Scancode::Q),
keyboard.is_scancode_pressed(Scancode::W),
keyboard.is_scancode_pressed(Scancode::E),
keyboard.is_scancode_pressed(Scancode::A),
keyboard.is_scancode_pressed(Scancode::S),
keyboard.is_scancode_pressed(Scancode::D),
keyboard.is_scancode_pressed(Scancode::Z),
keyboard.is_scancode_pressed(Scancode::C),
keyboard.is_scancode_pressed(Scancode::Num4),
keyboard.is_scancode_pressed(Scancode::R),
keyboard.is_scancode_pressed(Scancode::F),
keyboard.is_scancode_pressed(Scancode::V),
];
chip.advance(&are_keys_pressed);
// TODO: beep if beep > 0
// The rest of the game loop goes here...
}

Ok(())
Expand Down

0 comments on commit b9f55cb

Please sign in to comment.