update: change graphics and rom load
ROM load by argument Graphics are manage by main and not machine
This commit is contained in:
parent
d92bd30587
commit
56237065d4
2 changed files with 40 additions and 46 deletions
|
@ -41,13 +41,9 @@ pub const WIDTH: usize = 64;
|
|||
extern crate sdl2;
|
||||
extern crate rand;
|
||||
|
||||
pub use sdl2::pixels::Color;
|
||||
pub use sdl2::event::Event;
|
||||
pub use sdl2::EventPump;
|
||||
pub use sdl2::keyboard::Keycode;
|
||||
pub use sdl2::video::Window;
|
||||
pub use sdl2::rect::Point;
|
||||
pub use sdl2::render::Canvas;
|
||||
|
||||
pub use std::fs::File;
|
||||
pub use std::path::Path;
|
||||
|
@ -91,9 +87,10 @@ impl Machine {
|
|||
}
|
||||
|
||||
/// charge une ROM en mémoire
|
||||
pub fn load_rom(&mut self, p: &Path) -> io::Result<()> {
|
||||
let mut buffer = Vec::new();
|
||||
//pub fn load_rom(&mut self, p: &Path) -> Result<String, io::Error> {
|
||||
pub fn load_rom(&mut self, p: &Path) -> Result<(), io::Error> {
|
||||
let mut f = File::open(p)?;
|
||||
let mut buffer = Vec::new();
|
||||
f.read_to_end(&mut buffer)?;
|
||||
|
||||
for i in 0..buffer.len() {
|
||||
|
@ -101,9 +98,10 @@ impl Machine {
|
|||
}
|
||||
|
||||
Ok(())
|
||||
//Ok(String::from(p.to_str().unwrap()))
|
||||
}
|
||||
|
||||
pub fn cycle(&mut self, screen: &mut Canvas<Window>, events: &mut EventPump) {
|
||||
pub fn cycle(&mut self, events: &mut EventPump) {
|
||||
let opcode = ((self.memory[self.pc] as u16) << 8)
|
||||
+ (self.memory[self.pc + 1] as u16);
|
||||
|
||||
|
@ -126,12 +124,7 @@ impl Machine {
|
|||
|
||||
match (opcode & 0xf000) >> 12 {
|
||||
0 => match opcode {
|
||||
0x00E0 => {
|
||||
self.display_buffer = [[0; 64]; 32];
|
||||
screen.set_draw_color(Color::RGB(0, 0, 0));
|
||||
screen.clear();
|
||||
screen.present();
|
||||
},
|
||||
0x00E0 => self.display_buffer = [[0; 64]; 32],
|
||||
0x00EE => self.pc = self.stack.pop().unwrap() as usize,
|
||||
_ => println!("SYS"),
|
||||
}
|
||||
|
@ -140,10 +133,10 @@ impl Machine {
|
|||
self.stack.push(self.pc as u16);
|
||||
self.pc = (opcode & 0x0fff) as usize
|
||||
},
|
||||
3 => if self.gpio[vx] == k as u8 {
|
||||
3 => if self.gpio[vx] == k {
|
||||
self.pc += 2;
|
||||
},
|
||||
4 => if self.gpio[vx] != k as u8 {
|
||||
4 => if self.gpio[vx] != k {
|
||||
self.pc += 2;
|
||||
},
|
||||
5 => if self.gpio[vx] == self.gpio[vy] {
|
||||
|
@ -158,45 +151,37 @@ impl Machine {
|
|||
2 => self.gpio[vx] &= self.gpio[vy],
|
||||
3 => self.gpio[vx] ^= self.gpio[vy],
|
||||
4 => {
|
||||
/*
|
||||
let tx = self.gpio[vx] as u16;
|
||||
let ty = self.gpio[vy] as u16;
|
||||
let result = tx + ty;
|
||||
self.gpio[vx] = result;
|
||||
self
|
||||
*/
|
||||
self.gpio[vx] = self.gpio[vx].saturating_add(self.gpio[vy]);
|
||||
let (_, overflow) = self.gpio[vx].overflowing_add(self.gpio[vy]);
|
||||
if overflow {
|
||||
self.gpio[0xf] = 1;
|
||||
}
|
||||
self.gpio[0x0f] = if overflow { 1 } else { 0 };
|
||||
},
|
||||
5 => {
|
||||
if self.gpio[vx] > self.gpio[vy] {
|
||||
self.gpio[0xf] = 1;
|
||||
} else {
|
||||
self.gpio[0xf] = 0;
|
||||
}
|
||||
self.gpio[0x0f] = if self.gpio[vx] > self.gpio[vy] { 1 } else { 0 };
|
||||
|
||||
let (t, _) = self.gpio[vx].overflowing_sub(self.gpio[vy]);
|
||||
self.gpio[vx] = t;
|
||||
//let (t, _) = self.gpio[vx].overflowing_sub(self.gpio[vy]);
|
||||
//self.gpio[vx] = t;
|
||||
self.gpio[vx] = self.gpio[vx].wrapping_sub(self.gpio[vy]);
|
||||
},
|
||||
6 => {
|
||||
if self.gpio[vx].trailing_ones() > 0 {
|
||||
self.gpio[0xf] = 1;
|
||||
} else {
|
||||
self.gpio[0xf] = 0;
|
||||
}
|
||||
|
||||
self.gpio[0x0f] = self.gpio[vx] & 0x01;
|
||||
self.gpio[vx] >>= 1;
|
||||
},
|
||||
7 => {
|
||||
if self.gpio[vx] < self.gpio[vy] {
|
||||
self.gpio[0xf] = 1;
|
||||
} else {
|
||||
self.gpio[0xf] = 0;
|
||||
}
|
||||
|
||||
self.gpio[vx] = self.gpio[vy] - self.gpio[vx];
|
||||
self.gpio[0x0f] = if self.gpio[vx] < self.gpio[vy] { 1 } else { 0 };
|
||||
self.gpio[vx] = self.gpio[vy].wrapping_sub(self.gpio[vx]);
|
||||
},
|
||||
0xE => {
|
||||
if self.gpio[vx].leading_ones() > 0 {
|
||||
self.gpio[0xf] = 1;
|
||||
self.gpio[0x0f] = 1;
|
||||
} else {
|
||||
self.gpio[0xf] = 0;
|
||||
self.gpio[0x0f] = 0;
|
||||
}
|
||||
|
||||
self.gpio[vx] <<= 1;
|
||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -28,10 +28,8 @@ use machine::HEIGHT;
|
|||
use machine::WIDTH;
|
||||
use machine::Machine;
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
|
@ -40,11 +38,18 @@ fn main() {
|
|||
//return;
|
||||
let mut t: Machine = Machine::new();
|
||||
t.init();
|
||||
t.load_rom(Path::new(name_file));
|
||||
match t.load_rom(Path::new(name_file)) {
|
||||
Ok(()) => println!("La ROM a correctement été chargée."),
|
||||
Err(e) => match e.kind() {
|
||||
std::io::ErrorKind::NotFound => panic!("Le fichier n'a pas été trouvé."),
|
||||
_ => panic!("Erreur inconnue : {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
let sdl_context = sdl2::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
let window = video_subsystem.window("Chip8 Emulator", WIDTH as u32, HEIGHT as u32)
|
||||
//let window = video_subsystem.window("Chip8 Emulator", 100 + 10 * WIDTH as u32, 10 * HEIGHT as u32)
|
||||
.position_centered()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
@ -57,13 +62,15 @@ fn main() {
|
|||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
|
||||
'running: loop {
|
||||
t.cycle(&mut canvas, &mut event_pump);
|
||||
t.cycle(&mut event_pump);
|
||||
|
||||
canvas.set_draw_color(Color::RGB(255, 255, 255));
|
||||
|
||||
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.set_draw_color(Color::RGB(255, 255, 255));
|
||||
canvas.draw_point(Point::new(j as i32, i as i32)).unwrap();
|
||||
} else {
|
||||
canvas.set_draw_color(Color::RGB(0, 0, 0));
|
||||
canvas.draw_point(Point::new(j as i32, i as i32)).unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -178,6 +185,8 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
#[allow(dead_code, unused_variables)]
|
||||
fn read_file(p: &Path) -> io::Result<()> {
|
||||
let mut buffer = Vec::new();
|
||||
|
|
Loading…
Reference in a new issue