Formatting: cargo fmt
This commit is contained in:
@@ -16,8 +16,7 @@ impl Cartridge {
|
||||
// either read a byte, or noting (0)
|
||||
let bytes = if let Ok(bytes) = file.read(&mut buffer) {
|
||||
bytes
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use sdl2;
|
||||
use sdl2::{render::Canvas, video::Window, pixels, rect::Rect};
|
||||
use sdl2::{pixels, rect::Rect, render::Canvas, video::Window};
|
||||
|
||||
pub struct Display {
|
||||
canvas: Canvas<Window>,
|
||||
@@ -12,42 +12,36 @@ impl Display {
|
||||
.window(
|
||||
"chip8-rs",
|
||||
(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()
|
||||
.opengl()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mut canvas = window
|
||||
.into_canvas()
|
||||
.build()
|
||||
.unwrap();
|
||||
let mut canvas = window.into_canvas().build().unwrap();
|
||||
|
||||
canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
|
||||
canvas.clear();
|
||||
canvas.present();
|
||||
|
||||
Display {
|
||||
canvas,
|
||||
}
|
||||
Display { canvas }
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, pixel: &[[u8; crate::SCREEN_WIDTH]; crate::SCREEN_HEIGHT]) {
|
||||
for (y, &row) in pixel.iter().enumerate() {
|
||||
for (x, &column) in row.iter().enumerate() {
|
||||
self.canvas.set_draw_color(
|
||||
if column == 1 {
|
||||
self.canvas.set_draw_color(if column == 1 {
|
||||
pixels::Color::RGB(255, 255, 255)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
pixels::Color::RGB(0, 0, 0)
|
||||
});
|
||||
let _ = self.canvas
|
||||
.fill_rect(
|
||||
Rect::new((x * crate::SCREEN_SCALE) as i32,
|
||||
let _ = self.canvas.fill_rect(Rect::new(
|
||||
(x * 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,
|
||||
));
|
||||
}
|
||||
}
|
||||
self.canvas.present();
|
||||
|
||||
@@ -21,7 +21,8 @@ impl Input {
|
||||
}
|
||||
|
||||
// get all inputs
|
||||
let input: Vec<Keycode> = self.events
|
||||
let input: Vec<Keycode> = self
|
||||
.events
|
||||
.keyboard_state()
|
||||
.pressed_scancodes()
|
||||
.filter_map(Keycode::from_scancode)
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -3,14 +3,14 @@ extern crate sdl2;
|
||||
|
||||
use std::env;
|
||||
|
||||
mod processor;
|
||||
mod fontset;
|
||||
mod display;
|
||||
mod input;
|
||||
mod cartridge;
|
||||
mod display;
|
||||
mod fontset;
|
||||
mod input;
|
||||
mod processor;
|
||||
|
||||
use crate::processor::Processor;
|
||||
use crate::cartridge::Cartridge;
|
||||
use crate::processor::Processor;
|
||||
|
||||
const MEMORY_SIZE: usize = 4096;
|
||||
const GAME_ENTRY: usize = 0x200; // most games load into 0x200
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
extern crate sdl2;
|
||||
|
||||
use rand::Rng;
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::fontset::FONT;
|
||||
use crate::display::Display;
|
||||
use crate::fontset::FONT;
|
||||
use crate::input::Input;
|
||||
|
||||
const OPCODE_SIZE: usize = 2;
|
||||
@@ -92,8 +92,7 @@ impl Processor {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// decr both timers every cycle
|
||||
if self.delay_timer > 0 {
|
||||
self.delay_timer -= 1;
|
||||
@@ -111,10 +110,10 @@ impl Processor {
|
||||
pub fn load_game(&mut self, game: &[u8]) {
|
||||
for (pos, &val) in game.iter().enumerate() {
|
||||
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;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -132,7 +131,7 @@ impl Processor {
|
||||
((opcode & 0xF000) >> 12) as usize,
|
||||
((opcode & 0x0F00) >> 8) 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
|
||||
@@ -186,7 +185,7 @@ impl Processor {
|
||||
(0x0f, _, 0x03, 0x03) => self.code_fx33(x),
|
||||
(0x0f, _, 0x05, 0x05) => self.code_fx55(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) {
|
||||
if self.register[x] == kk {
|
||||
self.pc += 2 * OPCODE_SIZE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
self.pc += OPCODE_SIZE;
|
||||
}
|
||||
}
|
||||
@@ -229,8 +227,7 @@ impl Processor {
|
||||
fn code_4xkk(&mut self, x: usize, kk: u8) {
|
||||
if self.register[x] != kk {
|
||||
self.pc += 2 * OPCODE_SIZE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
self.pc += OPCODE_SIZE;
|
||||
}
|
||||
}
|
||||
@@ -239,8 +236,7 @@ impl Processor {
|
||||
fn code_5xy0(&mut self, x: usize, y: usize) {
|
||||
if self.register[x] == self.register[y] {
|
||||
self.pc += 2 * OPCODE_SIZE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
self.pc += OPCODE_SIZE;
|
||||
}
|
||||
}
|
||||
@@ -326,8 +322,7 @@ impl Processor {
|
||||
fn code_9xy0(&mut self, x: usize, y: usize) {
|
||||
if self.register[x] != self.register[y] {
|
||||
self.pc += 2 * OPCODE_SIZE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
self.pc += OPCODE_SIZE;
|
||||
}
|
||||
}
|
||||
@@ -374,8 +369,7 @@ impl Processor {
|
||||
fn code_ex9e(&mut self, x: usize) {
|
||||
if self.key[self.register[x] as usize] {
|
||||
self.pc += 2 * OPCODE_SIZE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
self.pc += OPCODE_SIZE;
|
||||
}
|
||||
}
|
||||
@@ -384,8 +378,7 @@ impl Processor {
|
||||
fn code_exa1(&mut self, x: usize) {
|
||||
if !(self.key[self.register[x] as usize]) {
|
||||
self.pc += 2 * OPCODE_SIZE;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
self.pc += OPCODE_SIZE;
|
||||
}
|
||||
}
|
||||
@@ -624,7 +617,6 @@ mod tests{
|
||||
assert_eq!(processor.register[1], 1);
|
||||
assert_eq!(processor.register[0x0f], 1);
|
||||
assert_eq!(processor.pc, NEXT);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user