2020-06-25 13:13:09 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Display {
|
2020-07-13 18:08:15 -05:00
|
|
|
pub buf: [u8; Self::WIDTH as usize * Self::HEIGHT as usize],
|
2020-06-25 13:13:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display {
|
2020-07-13 18:08:15 -05:00
|
|
|
const WIDTH: u8 = 64;
|
|
|
|
const HEIGHT: u8 = 32;
|
2020-06-25 13:13:09 -05:00
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
2020-07-13 18:08:15 -05:00
|
|
|
self.buf = [0x0; Self::WIDTH as usize * Self::HEIGHT as usize]
|
2020-07-13 15:28:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw_sprite(&mut self, coords: (u8, u8), data: &[u8]) -> bool {
|
2020-07-13 18:08:15 -05:00
|
|
|
let x = coords.0 % Self::WIDTH;
|
|
|
|
let y = coords.1 % Self::HEIGHT;
|
2020-07-13 15:28:41 -05:00
|
|
|
let mut set_vf = false;
|
2020-07-13 18:08:15 -05:00
|
|
|
|
|
|
|
// Each byte in data is a row, with the column being data.len() tall
|
2020-07-13 23:04:58 -05:00
|
|
|
// This means when writing this sprite, x will change for each bit in a byte and y
|
2020-07-13 18:08:15 -05:00
|
|
|
// should be incremented by 1 for every new byte we draw to the graphics buffer
|
|
|
|
for (y_offset, byte) in data.iter().enumerate() {
|
2020-07-13 15:28:41 -05:00
|
|
|
// Access every bit of the byte
|
2020-07-13 18:08:15 -05:00
|
|
|
for x_bit_offset in 0..8 {
|
|
|
|
// The Coordinates of this bit in the gfx buffer
|
2020-07-14 21:04:07 -05:00
|
|
|
let gfx_x = x + (7 - x_bit_offset);
|
2020-07-13 18:08:15 -05:00
|
|
|
let gfx_y = y + y_offset as u8;
|
|
|
|
|
|
|
|
let bit = (byte >> x_bit_offset) & 0x01;
|
2020-07-13 15:28:41 -05:00
|
|
|
|
2020-07-13 18:08:15 -05:00
|
|
|
// Translate the gfx_x and gfx_y to an index in the 1D buffer
|
|
|
|
let gfx_i = (Self::WIDTH as usize * gfx_y as usize) + gfx_x as usize;
|
2020-07-13 15:28:41 -05:00
|
|
|
|
2020-07-13 18:08:15 -05:00
|
|
|
if gfx_i >= self.buf.len() {
|
|
|
|
break; // Stop Drawing this spite and move on to the next
|
|
|
|
}
|
|
|
|
|
2020-07-13 23:04:58 -05:00
|
|
|
if bit == 0x1 && self.buf[gfx_i] == 0x01 {
|
2020-07-13 15:28:41 -05:00
|
|
|
set_vf = true;
|
|
|
|
}
|
2020-07-13 18:08:15 -05:00
|
|
|
|
|
|
|
self.buf[gfx_i] ^= bit;
|
2020-07-13 15:28:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
set_vf
|
2020-06-25 13:13:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Display {
|
|
|
|
fn default() -> Self {
|
|
|
|
Display {
|
2020-07-13 18:08:15 -05:00
|
|
|
buf: [0x0; Self::WIDTH as usize * Self::HEIGHT as usize],
|
2020-06-25 13:13:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct Timer {
|
|
|
|
remaining: u8,
|
|
|
|
enabled: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Timer {
|
|
|
|
fn default() -> Self {
|
|
|
|
Timer {
|
|
|
|
remaining: 0,
|
|
|
|
enabled: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Timer {
|
|
|
|
pub fn set(&mut self, secs: u8) {
|
|
|
|
self.remaining = secs;
|
|
|
|
self.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self) -> u8 {
|
|
|
|
self.remaining
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) {
|
|
|
|
if self.enabled {
|
|
|
|
self.remaining -= 1;
|
|
|
|
|
|
|
|
if self.remaining == 0 {
|
|
|
|
self.enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct Keypad {
|
|
|
|
keys: [bool; 16],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Keypad {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { keys: [false; 16] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keypad {
|
2020-06-27 21:49:58 -05:00
|
|
|
pub fn get_any_pressed(&self) -> Option<u8> {
|
|
|
|
for (i, key) in self.keys.iter().enumerate() {
|
2020-06-26 15:54:18 -05:00
|
|
|
if *key {
|
2020-06-27 21:49:58 -05:00
|
|
|
return Some(i as u8);
|
2020-06-25 13:13:09 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-27 21:49:58 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.keys = [false; 16];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_overview(&self) -> [bool; 16] {
|
|
|
|
self.keys
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_pressed(&self, key: u8) -> bool {
|
|
|
|
self.keys[key as usize]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_all(&mut self, keys: [bool; 16]) {
|
|
|
|
self.keys = keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_key(&mut self, index: usize) {
|
|
|
|
self.keys[index] = true;
|
|
|
|
}
|
2020-06-25 13:13:09 -05:00
|
|
|
|
2020-06-27 21:49:58 -05:00
|
|
|
pub fn unset_key(&mut self, index: usize) {
|
|
|
|
self.keys[index] = false;
|
2020-06-25 13:13:09 -05:00
|
|
|
}
|
|
|
|
}
|