Skip to content

Commit 9541121

Browse files
committed
Upgrade edition, fix lints
1 parent 19526b1 commit 9541121

5 files changed

Lines changed: 70 additions & 38 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "chip8"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2024"
55
categories = ["no-std"]
66

77
[dependencies]

Justfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,3 @@ test:
1212
cargo nextest run --all-targets --no-fail-fast
1313

1414
t:test
15-
16-
lox_run:build
17-
cargo run run test.lox

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,39 @@
33
[![codecov](https://codecov.io/gh/sinon/chip8/graph/badge.svg?token=9QYDO9E4RU)](https://codecov.io/gh/sinon/chip8)
44

55
Spec: <http://devernay.free.fr/hacks/chip8/C8TECH10.HTM>
6+
7+
## Run ROMs
8+
9+
### Pong
10+
11+
`cargo run pong`
12+
13+
Controls:
14+
- Player 1 Up: 1
15+
- Player 1 Down: Q
16+
- Player 2 Up: 4
17+
- Player 2 Up: R
18+
- Quit: Esc
19+
20+
21+
### Guess
22+
23+
`cargo run guess`
24+
25+
### Maze
26+
27+
`cargo run maze`
28+
29+
30+
## Development
31+
32+
### Tests
33+
34+
`cargo test`
35+
or
36+
`just t`
37+
38+
### Lint
39+
40+
`just lint`
41+

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! <http://devernay.free.fr/hacks/chip8/C8TECH10.HTM>
33
44
#![no_std]
5+
#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
56

67
const RAM_SIZE: usize = 4096;
78
// The original implementation of the Chip-8 language used a 64x32-pixel monochrome display with this format:
@@ -89,7 +90,7 @@ impl Default for Chip8Emulator {
8990
impl Chip8Emulator {
9091
#[must_use]
9192
pub fn new() -> Self {
92-
let mut emu: Self = Default::default();
93+
let mut emu: Self = Self::default();
9394
emu.memory[..(16 * 5)].copy_from_slice(&FONT_SPRITES);
9495
emu
9596
}
@@ -404,8 +405,8 @@ impl Chip8Emulator {
404405
// See instruction 8xy3 for more information on XOR, and section 2.4, Display, for more information on the Chip-8 screen and sprites.
405406

406407
// Implementation based on: <https://aquova.net/emudev/chip8/5-instr.html>
407-
let x_coord = self.v_registers[x as usize] as u16;
408-
let y_coord = self.v_registers[y as usize] as u16;
408+
let x_coord = u16::from(self.v_registers[x as usize]);
409+
let y_coord = u16::from(self.v_registers[y as usize]);
409410

410411
let num_rows = u16::from(d);
411412
let mut flipped = false;
@@ -520,7 +521,7 @@ impl Chip8Emulator {
520521
// The interpreter takes the decimal value of Vx, and places the hundreds digit in memory at location in I,
521522
// the tens digit at location I+1, and the ones digit at location I+2.
522523
// https://en.wikipedia.org/wiki/Binary-coded_decimal
523-
let vx = self.v_registers[x as usize] as f64;
524+
let vx = f64::from(self.v_registers[x as usize]);
524525

525526
let hundredths = (vx / 100.0).floor() as u8;
526527
let tenths = ((vx / 10.0) % 10.0).floor() as u8;

src/main.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
2+
13
use std::{io, time::Duration};
24

35
use chip8::{Chip8Emulator, SCREEN_WIDTH};
@@ -7,15 +9,15 @@ use itertools::Itertools;
79
use ratatui::crossterm::event::KeyboardEnhancementFlags;
810
use ratatui::crossterm::event::PushKeyboardEnhancementFlags;
911
use ratatui::{
12+
DefaultTerminal, Frame,
1013
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind},
1114
layout::{Constraint, Layout, Position, Rect},
1215
style::Color,
1316
symbols::Marker,
1417
widgets::{
15-
canvas::{Canvas, Points},
1618
Block, Widget,
19+
canvas::{Canvas, Points},
1720
},
18-
DefaultTerminal, Frame,
1921
};
2022

2123
#[derive(Parser, Debug)]
@@ -45,13 +47,14 @@ fn main() -> io::Result<()> {
4547
io::stderr(),
4648
PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::REPORT_EVENT_TYPES)
4749
)?;
48-
let app_result = App::new(command).run(&mut terminal);
50+
let app_result = App::new(&command).run(&mut terminal);
4951
ratatui::restore();
5052
app_result
5153
}
5254

5355
impl App {
54-
pub fn new(command: Commands) -> Self {
56+
#[must_use]
57+
pub fn new(command: &Commands) -> Self {
5558
let pong = include_bytes!("./roms/PONG");
5659
let guess = include_bytes!("./roms/GUESS");
5760
let maze = include_bytes!("./roms/MAZE");
@@ -61,13 +64,15 @@ impl App {
6164
Commands::Guess => emulator.load_data(guess),
6265
Commands::Maze => emulator.load_data(maze),
6366
}
64-
App {
67+
Self {
6568
emulator,
6669
exit: false,
6770
points: vec![],
6871
}
6972
}
70-
73+
/// # Errors
74+
/// - reading events
75+
/// - fails to draw state to terminal
7176
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
7277
while !self.exit {
7378
for _ in 0..10 {
@@ -89,17 +94,10 @@ impl App {
8994

9095
fn handle_events(&mut self) -> io::Result<()> {
9196
if event::poll(Duration::from_millis(16))? {
92-
match event::read()? {
93-
Event::Key(key_event) => {
94-
let pressed = if key_event.kind == KeyEventKind::Press {
95-
true
96-
} else {
97-
false
98-
};
99-
self.handle_key_event(key_event, pressed)
100-
}
101-
_ => {}
102-
};
97+
if let Event::Key(key_event) = event::read()? {
98+
let pressed = key_event.kind == KeyEventKind::Press;
99+
self.handle_key_event(key_event, pressed);
100+
}
103101
}
104102
Ok(())
105103
}
@@ -112,18 +110,18 @@ impl App {
112110
KeyCode::Char('2') => Some(0x2),
113111
KeyCode::Char('3') => Some(0x3),
114112
KeyCode::Char('4') => Some(0xC),
115-
KeyCode::Char('q') | KeyCode::Char('Q') => Some(0x4),
116-
KeyCode::Char('w') | KeyCode::Char('W') => Some(0x5),
117-
KeyCode::Char('e') | KeyCode::Char('E') => Some(0x6),
118-
KeyCode::Char('r') | KeyCode::Char('R') => Some(0xD),
119-
KeyCode::Char('a') | KeyCode::Char('A') => Some(0x7),
120-
KeyCode::Char('s') | KeyCode::Char('S') => Some(0x8),
121-
KeyCode::Char('d') | KeyCode::Char('D') => Some(0x9),
122-
KeyCode::Char('f') | KeyCode::Char('F') => Some(0xE),
123-
KeyCode::Char('z') | KeyCode::Char('Z') => Some(0xA),
124-
KeyCode::Char('x') | KeyCode::Char('X') => Some(0x0),
125-
KeyCode::Char('c') | KeyCode::Char('C') => Some(0xB),
126-
KeyCode::Char('v') | KeyCode::Char('V') => Some(0xB),
113+
KeyCode::Char('q' | 'Q') => Some(0x4),
114+
KeyCode::Char('w' | 'W') => Some(0x5),
115+
KeyCode::Char('e' | 'E') => Some(0x6),
116+
KeyCode::Char('r' | 'R') => Some(0xD),
117+
KeyCode::Char('a' | 'A') => Some(0x7),
118+
KeyCode::Char('s' | 'S') => Some(0x8),
119+
KeyCode::Char('d' | 'D') => Some(0x9),
120+
KeyCode::Char('f' | 'F') => Some(0xE),
121+
KeyCode::Char('z' | 'Z') => Some(0xA),
122+
KeyCode::Char('x' | 'X') => Some(0x0),
123+
KeyCode::Char('c' | 'C') => Some(0xB),
124+
KeyCode::Char('v' | 'V') => Some(0xF),
127125
_ => None,
128126
};
129127

@@ -132,7 +130,7 @@ impl App {
132130
}
133131
}
134132

135-
fn exit(&mut self) {
133+
const fn exit(&mut self) {
136134
self.exit = true;
137135
}
138136

0 commit comments

Comments
 (0)