refactor: remove useless code

This commit is contained in:
rick 2022-02-09 14:53:04 +01:00
parent 979a39405e
commit 8ae985446d
Signed by: Rick
GPG key ID: 2B593F087240EE99

View file

@ -20,10 +20,8 @@ extern crate sdl2;
extern crate rand;
use sdl2::pixels::Color;
#[allow(unused_imports)]
use sdl2::event::Event;
use sdl2::EventPump;
#[allow(unused_imports)]
use sdl2::keyboard::Keycode;
use sdl2::video::Window;
use sdl2::rect::Point;
@ -57,19 +55,16 @@ const FONT: [u8; 80] = [
const HEIGHT: usize = 32;
const WIDTH: usize = 64;
#[allow(dead_code)]
struct Machine {
memory: [u8; 4096],
key_inputs: [u8; 16],
display_buffer: [[i32; WIDTH]; HEIGHT],
gpio: [u8; 16],
index: u16,
//opcode: u16,
pc: usize,
stack: Vec<u16>
}
#[allow(non_snake_case, unused_variables)]
impl Machine {
/// Initialise une machine en mettant tout à 0 (RAM, PC...)
pub fn new() -> Self {
@ -79,7 +74,6 @@ impl Machine {
display_buffer: [[0; WIDTH]; HEIGHT],
gpio: [0; 16],
index: 0,
//opcode: 0,
pc: 0,
stack: Vec::new(),
}
@ -94,7 +88,7 @@ impl Machine {
}
/// charge une ROM en mémoire
pub fn loadRom(&mut self, p: &Path) -> io::Result<()> {
pub fn load_rom(&mut self, p: &Path) -> io::Result<()> {
let mut buffer = Vec::new();
let mut f = File::open(p)?;
f.read_to_end(&mut buffer)?;
@ -144,8 +138,6 @@ impl Machine {
6 => self.gpio[vx] = k,
7 => self.gpio[vx] = self.gpio[vx].saturating_add(k),
8 => {
//let x = (opcode & 0x0f00) >> 8;
//let y = (opcode & 0x00f0) >> 4;
match opcode & 0x000f {
0 => self.gpio[vx] = self.gpio[vy],
1 => self.gpio[vx] |= self.gpio[vy],
@ -165,9 +157,6 @@ impl Machine {
self.gpio[0xf] = 0;
}
//let tmp: i32 = (self.gpio[vx] - self.gpio[vy]) as i32;
//self.gpio[vx] = (tmp & 0x00ff) as u8;
//self.gpio[vx] = self.gpio[vy];
let (t, _) = self.gpio[vx].overflowing_sub(self.gpio[vy]);
self.gpio[vx] = t;
},
@ -232,7 +221,6 @@ impl Machine {
}
},
0xE => {
//let x = (opcode & 0x0f00) >> 8;
match opcode & 0x00ff {
0x9E => {
if self.key_inputs[self.gpio[vx] as usize] == 1 {
@ -248,14 +236,13 @@ impl Machine {
}
}
0xF => {
let x = (opcode & 0x0f00) >> 8;
match opcode & 0x00ff {
0x07 => println!("LD F 7"),
0x0A => self.check_touch(vx, events),
0x15 => println!("LD F 15"),
0x18 => println!("LD F 18"),
0x1E => self.index += self.gpio[vx] as u16,
0x29 => self.index = (self.gpio[vx] as u16) * 5, // self.index = self.gpio[x] * 5) & 0x0fff
0x29 => self.index = (self.gpio[vx] as u16) * 5,
0x33 => {
self.memory[self.index as usize] = self.gpio[vx] / 100 as u8;
self.memory[(self.index + 1) as usize] = (self.gpio[vx] % 100) /10 as u8;
@ -263,17 +250,13 @@ impl Machine {
},
0x55 =>
{
//let mut j: usize = self.index as usize;
for i in 0..(vx + 1) {
self.memory[(self.index as usize) + i] = self.gpio[i];
//j += 1;
}
},
0x65 => {
//let mut j: usize = self.index as usize;
for i in 0..(vx + 1) {
self.gpio[i] = self.memory[(self.index as usize) + i];
//j += 1;
}
},
_ => println!("error F")
@ -339,7 +322,7 @@ impl Machine {
}
}
#[allow(dead_code, unused_variables)]
fn read_file(p: &Path) -> io::Result<()> {
let mut buffer = Vec::new();
let mut f = File::open(p)?;
@ -377,8 +360,6 @@ fn read_file(p: &Path) -> io::Result<()> {
6 => println!("6LD {} {}", vx, k),
7 => println!("7ADD {} {}", vx, k),
8 => {
//let x = (opcode & 0x0f00) >> 8;
//let y = (opcode & 0x00f0) >> 4;
match opcode & 0x000f {
0 => println!("8LD {} {}", vx, vy),
1 => println!("8OR {} {}", vx, vy),
@ -427,19 +408,15 @@ fn read_file(p: &Path) -> io::Result<()> {
Ok(())
}
#[allow(unused_mut, unused_must_use, unused_labels, unused_variables)]
fn main() {
//read_file(Path::new("15PUZZLE"));
//return;
let mut t : Machine = Machine::new();
//println!("{:?}", t.memory);
t.init();
//t.loadRom(Path::new("15PUZZLE"));
t.loadRom(Path::new("BLINKY"));
//t.loadRom(Path::new("GUESS"));
//t.loadRom(Path::new("BLITZ"));
//println!("\n\n{:?}", t.memory);
//t.load_rom(Path::new("15PUZZLE"));
t.load_rom(Path::new("BLINKY")).unwrap();
//t.load_rom(Path::new("GUESS"));
//t.load_rom(Path::new("BLITZ"));
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
@ -454,37 +431,20 @@ fn main() {
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut i = 0;
'running: loop {
/*
i = (i + 1) % 255;
canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
canvas.clear();
*/
t.cycle(&mut canvas, &mut event_pump);
//canvas.set_draw_color(Color::RGB(0, 0, 0));
//canvas.clear();
//println!("{:?}", t.memory);
canvas.set_draw_color(Color::RGB(255, 255, 255));
//println!("{:?}", t.display_buffer);
//println!("\n===\n");
for i in 0..t.display_buffer.len() {
for j in 0..t.display_buffer[i].len() {
if t.display_buffer[i][j] == 1 {
canvas.draw_point(Point::new(j as i32, i as i32));
canvas.draw_point(Point::new(j as i32, i as i32)).unwrap();
}
}
}
//canvas.draw_line(Point::new(10, 10), Point::new(20,20));
//canvas.draw_point(Point::new(10, 10));
for event in event_pump.poll_iter() {
match event {
Event::KeyDown { keycode: Some(Keycode::Escape), ..} => {