Formatting: cargo fmt

This commit is contained in:
Marco Thomas
2021-06-11 20:29:00 +02:00
parent c1f8c33932
commit 8f3bea9098
6 changed files with 566 additions and 580 deletions

View File

@@ -16,8 +16,7 @@ impl Cartridge {
// either read a byte, or noting (0) // either read a byte, or noting (0)
let bytes = if let Ok(bytes) = file.read(&mut buffer) { let bytes = if let Ok(bytes) = file.read(&mut buffer) {
bytes bytes
} } else {
else {
0 0
}; };

View File

@@ -1,5 +1,5 @@
use sdl2; use sdl2;
use sdl2::{render::Canvas, video::Window, pixels, rect::Rect}; use sdl2::{pixels, rect::Rect, render::Canvas, video::Window};
pub struct Display { pub struct Display {
canvas: Canvas<Window>, canvas: Canvas<Window>,
@@ -12,42 +12,36 @@ impl Display {
.window( .window(
"chip8-rs", "chip8-rs",
(crate::SCREEN_WIDTH * crate::SCREEN_SCALE) as u32, (crate::SCREEN_WIDTH * crate::SCREEN_SCALE) as u32,
(crate::SCREEN_HEIGHT * crate::SCREEN_SCALE) as u32) (crate::SCREEN_HEIGHT * crate::SCREEN_SCALE) as u32,
)
.position_centered() .position_centered()
.opengl() .opengl()
.build() .build()
.unwrap(); .unwrap();
let mut canvas = window let mut canvas = window.into_canvas().build().unwrap();
.into_canvas()
.build()
.unwrap();
canvas.set_draw_color(pixels::Color::RGB(0, 0, 0)); canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
canvas.clear(); canvas.clear();
canvas.present(); canvas.present();
Display { Display { canvas }
canvas,
}
} }
pub fn draw(&mut self, pixel: &[[u8; crate::SCREEN_WIDTH]; crate::SCREEN_HEIGHT]) { pub fn draw(&mut self, pixel: &[[u8; crate::SCREEN_WIDTH]; crate::SCREEN_HEIGHT]) {
for (y, &row) in pixel.iter().enumerate() { for (y, &row) in pixel.iter().enumerate() {
for (x, &column) in row.iter().enumerate() { for (x, &column) in row.iter().enumerate() {
self.canvas.set_draw_color( self.canvas.set_draw_color(if column == 1 {
if column == 1 {
pixels::Color::RGB(255, 255, 255) pixels::Color::RGB(255, 255, 255)
} } else {
else {
pixels::Color::RGB(0, 0, 0) pixels::Color::RGB(0, 0, 0)
}); });
let _ = self.canvas let _ = self.canvas.fill_rect(Rect::new(
.fill_rect( (x * crate::SCREEN_SCALE) as i32,
Rect::new((x * crate::SCREEN_SCALE) as i32,
(y * crate::SCREEN_SCALE) as i32, (y * crate::SCREEN_SCALE) as i32,
crate::SCREEN_SCALE as u32, crate::SCREEN_SCALE as u32,
crate::SCREEN_SCALE as u32)); crate::SCREEN_SCALE as u32,
));
} }
} }
self.canvas.present(); self.canvas.present();

View File

@@ -21,7 +21,8 @@ impl Input {
} }
// get all inputs // get all inputs
let input: Vec<Keycode> = self.events let input: Vec<Keycode> = self
.events
.keyboard_state() .keyboard_state()
.pressed_scancodes() .pressed_scancodes()
.filter_map(Keycode::from_scancode) .filter_map(Keycode::from_scancode)

View File

@@ -3,14 +3,14 @@ extern crate sdl2;
use std::env; use std::env;
mod processor;
mod fontset;
mod display;
mod input;
mod cartridge; mod cartridge;
mod display;
mod fontset;
mod input;
mod processor;
use crate::processor::Processor;
use crate::cartridge::Cartridge; use crate::cartridge::Cartridge;
use crate::processor::Processor;
const MEMORY_SIZE: usize = 4096; const MEMORY_SIZE: usize = 4096;
const GAME_ENTRY: usize = 0x200; // most games load into 0x200 const GAME_ENTRY: usize = 0x200; // most games load into 0x200

View File

@@ -1,11 +1,11 @@
extern crate sdl2; extern crate sdl2;
use rand::Rng; use rand::Rng;
use std::time::Duration;
use std::thread; use std::thread;
use std::time::Duration;
use crate::fontset::FONT;
use crate::display::Display; use crate::display::Display;
use crate::fontset::FONT;
use crate::input::Input; use crate::input::Input;
const OPCODE_SIZE: usize = 2; const OPCODE_SIZE: usize = 2;
@@ -92,8 +92,7 @@ impl Processor {
break; break;
} }
} }
} } else {
else {
// decr both timers every cycle // decr both timers every cycle
if self.delay_timer > 0 { if self.delay_timer > 0 {
self.delay_timer -= 1; self.delay_timer -= 1;
@@ -111,10 +110,10 @@ impl Processor {
pub fn load_game(&mut self, game: &[u8]) { pub fn load_game(&mut self, game: &[u8]) {
for (pos, &val) in game.iter().enumerate() { for (pos, &val) in game.iter().enumerate() {
let position = crate::GAME_ENTRY + pos; let position = crate::GAME_ENTRY + pos;
if position < crate::MEMORY_SIZE { // don't go above mem limit if position < crate::MEMORY_SIZE {
// don't go above mem limit
self.memory[position] = val; self.memory[position] = val;
} } else {
else {
break; break;
} }
} }
@@ -132,7 +131,7 @@ impl Processor {
((opcode & 0xF000) >> 12) as usize, ((opcode & 0xF000) >> 12) as usize,
((opcode & 0x0F00) >> 8) as usize, ((opcode & 0x0F00) >> 8) as usize,
((opcode & 0x00F0) >> 4) as usize, ((opcode & 0x00F0) >> 4) as usize,
(opcode & 0x000F) as usize (opcode & 0x000F) as usize,
); );
// nnn or addr - A 12-bit value, the lowest 12 bits of the instruction // nnn or addr - A 12-bit value, the lowest 12 bits of the instruction
@@ -186,7 +185,7 @@ impl Processor {
(0x0f, _, 0x03, 0x03) => self.code_fx33(x), (0x0f, _, 0x03, 0x03) => self.code_fx33(x),
(0x0f, _, 0x05, 0x05) => self.code_fx55(x), (0x0f, _, 0x05, 0x05) => self.code_fx55(x),
(0x0f, _, 0x06, 0x05) => self.code_fx65(x), (0x0f, _, 0x06, 0x05) => self.code_fx65(x),
_ => self.pc += OPCODE_SIZE _ => self.pc += OPCODE_SIZE,
}; };
} }
@@ -219,8 +218,7 @@ impl Processor {
fn code_3xkk(&mut self, x: usize, kk: u8) { fn code_3xkk(&mut self, x: usize, kk: u8) {
if self.register[x] == kk { if self.register[x] == kk {
self.pc += 2 * OPCODE_SIZE; self.pc += 2 * OPCODE_SIZE;
} } else {
else {
self.pc += OPCODE_SIZE; self.pc += OPCODE_SIZE;
} }
} }
@@ -229,8 +227,7 @@ impl Processor {
fn code_4xkk(&mut self, x: usize, kk: u8) { fn code_4xkk(&mut self, x: usize, kk: u8) {
if self.register[x] != kk { if self.register[x] != kk {
self.pc += 2 * OPCODE_SIZE; self.pc += 2 * OPCODE_SIZE;
} } else {
else {
self.pc += OPCODE_SIZE; self.pc += OPCODE_SIZE;
} }
} }
@@ -239,8 +236,7 @@ impl Processor {
fn code_5xy0(&mut self, x: usize, y: usize) { fn code_5xy0(&mut self, x: usize, y: usize) {
if self.register[x] == self.register[y] { if self.register[x] == self.register[y] {
self.pc += 2 * OPCODE_SIZE; self.pc += 2 * OPCODE_SIZE;
} } else {
else {
self.pc += OPCODE_SIZE; self.pc += OPCODE_SIZE;
} }
} }
@@ -326,8 +322,7 @@ impl Processor {
fn code_9xy0(&mut self, x: usize, y: usize) { fn code_9xy0(&mut self, x: usize, y: usize) {
if self.register[x] != self.register[y] { if self.register[x] != self.register[y] {
self.pc += 2 * OPCODE_SIZE; self.pc += 2 * OPCODE_SIZE;
} } else {
else {
self.pc += OPCODE_SIZE; self.pc += OPCODE_SIZE;
} }
} }
@@ -374,8 +369,7 @@ impl Processor {
fn code_ex9e(&mut self, x: usize) { fn code_ex9e(&mut self, x: usize) {
if self.key[self.register[x] as usize] { if self.key[self.register[x] as usize] {
self.pc += 2 * OPCODE_SIZE; self.pc += 2 * OPCODE_SIZE;
} } else {
else {
self.pc += OPCODE_SIZE; self.pc += OPCODE_SIZE;
} }
} }
@@ -384,8 +378,7 @@ impl Processor {
fn code_exa1(&mut self, x: usize) { fn code_exa1(&mut self, x: usize) {
if !(self.key[self.register[x] as usize]) { if !(self.key[self.register[x] as usize]) {
self.pc += 2 * OPCODE_SIZE; self.pc += 2 * OPCODE_SIZE;
} } else {
else {
self.pc += OPCODE_SIZE; self.pc += OPCODE_SIZE;
} }
} }
@@ -624,7 +617,6 @@ mod tests{
assert_eq!(processor.register[1], 1); assert_eq!(processor.register[1], 1);
assert_eq!(processor.register[0x0f], 1); assert_eq!(processor.register[0x0f], 1);
assert_eq!(processor.pc, NEXT); assert_eq!(processor.pc, NEXT);
} }
#[test] #[test]