Move where CHIP-8 Program Counter is updated
This commit is contained in:
parent
f4a8840b91
commit
acf1b10e66
12
src/emu.rs
12
src/emu.rs
|
@ -59,7 +59,8 @@ impl Chip8 {
|
||||||
|
|
||||||
pub fn execute_cycle(&mut self) {
|
pub fn execute_cycle(&mut self) {
|
||||||
let opcode = self.get_opcode();
|
let opcode = self.get_opcode();
|
||||||
// println!("{:#x}", self.opcode);
|
self.pc += 2; // Immediately increment the Program Counter
|
||||||
|
|
||||||
self.execute_opcode(opcode);
|
self.execute_opcode(opcode);
|
||||||
|
|
||||||
self.delay.tick();
|
self.delay.tick();
|
||||||
|
@ -82,7 +83,6 @@ impl Chip8 {
|
||||||
|
|
||||||
fn get_opcode(&self) -> u16 {
|
fn get_opcode(&self) -> u16 {
|
||||||
let pc = self.pc as usize;
|
let pc = self.pc as usize;
|
||||||
|
|
||||||
((self.memory[pc] as u16) << 8) | self.memory[pc + 1] as u16
|
((self.memory[pc] as u16) << 8) | self.memory[pc + 1] as u16
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,8 +105,6 @@ impl Chip8 {
|
||||||
let y = nib_3;
|
let y = nib_3;
|
||||||
let n = nib_4;
|
let n = nib_4;
|
||||||
|
|
||||||
self.pc += 2;
|
|
||||||
|
|
||||||
match (nib_1, nib_2, nib_3, nib_4) {
|
match (nib_1, nib_2, nib_3, nib_4) {
|
||||||
// CLS
|
// CLS
|
||||||
(0x0, 0x0, 0xE, 0x0) => self.cls(),
|
(0x0, 0x0, 0xE, 0x0) => self.cls(),
|
||||||
|
@ -197,7 +195,7 @@ impl Chip8 {
|
||||||
|
|
||||||
fn jmp_addr(&mut self, nnn: u16) {
|
fn jmp_addr(&mut self, nnn: u16) {
|
||||||
// sets the program counter to addr (nnn)
|
// sets the program counter to addr (nnn)
|
||||||
self.pc = nnn - 2; // We want to execute nnn so decrement pc by 2;
|
self.pc = nnn; // We want to execute nnn so decrement pc by 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call_addr(&mut self, nnn: u16) {
|
fn call_addr(&mut self, nnn: u16) {
|
||||||
|
@ -205,7 +203,7 @@ impl Chip8 {
|
||||||
// pc is then set to addr
|
// pc is then set to addr
|
||||||
self.sp += 1;
|
self.sp += 1;
|
||||||
self.stack[self.sp as usize] = self.pc;
|
self.stack[self.sp as usize] = self.pc;
|
||||||
self.pc = nnn - 2; // see Chip8#jmp_addr()
|
self.pc = nnn; // see Chip8#jmp_addr()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn se_vx_byte(&mut self, x: u8, kk: u8) {
|
fn se_vx_byte(&mut self, x: u8, kk: u8) {
|
||||||
|
@ -323,7 +321,7 @@ impl Chip8 {
|
||||||
|
|
||||||
fn jmp_addr_with_offset(&mut self, nnn: u16) {
|
fn jmp_addr_with_offset(&mut self, nnn: u16) {
|
||||||
// set program counter to addr + V0
|
// set program counter to addr + V0
|
||||||
self.pc = nnn + self.v[0] as u16 - 2; // see Chip8#jmp_addr
|
self.pc = nnn + self.v[0] as u16; // see Chip8#jmp_addr
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rand(&mut self, x: u8, kk: u8) {
|
fn rand(&mut self, x: u8, kk: u8) {
|
||||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -15,17 +15,15 @@ static WIDTH: u32 = 64;
|
||||||
static HEIGHT: u32 = 32;
|
static HEIGHT: u32 = 32;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let mut chip8: Chip8 = Default::default();
|
||||||
|
let mut input = WinitInputHelper::new();
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
let window = init_window(&event_loop);
|
let window = init_window(&event_loop);
|
||||||
|
let mut hidpi_factor = window.scale_factor();
|
||||||
let mut pixels = init_pixels(&window);
|
let mut pixels = init_pixels(&window);
|
||||||
|
|
||||||
let mut hidpi_factor = window.scale_factor();
|
let rom_path = Path::new("./games/ibm_logo.ch8");
|
||||||
let mut input = WinitInputHelper::new();
|
chip8.load_rom(rom_path).expect("Unable to load ROM");
|
||||||
|
|
||||||
let mut chip8: Chip8 = Default::default();
|
|
||||||
chip8
|
|
||||||
.load_rom(Path::new("./games/ibm_logo.ch8"))
|
|
||||||
.expect("Unable to load ROM");
|
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
if let Event::RedrawRequested(_) = event {
|
if let Event::RedrawRequested(_) = event {
|
||||||
|
@ -67,11 +65,13 @@ fn init_pixels(window: &Window) -> Pixels {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_window(event_loop: &EventLoop<()>) -> Window {
|
fn init_window(event_loop: &EventLoop<()>) -> Window {
|
||||||
let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
|
const SCALE: u32 = 10;
|
||||||
|
let min_size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
|
||||||
|
let size = LogicalSize::new((WIDTH * SCALE) as f64, (HEIGHT * SCALE) as f64);
|
||||||
WindowBuilder::new()
|
WindowBuilder::new()
|
||||||
.with_title("Chip8 Emulator")
|
.with_title("Chip8 Emulator")
|
||||||
.with_inner_size(size)
|
.with_inner_size(size)
|
||||||
.with_min_inner_size(size)
|
.with_min_inner_size(min_size)
|
||||||
.build(event_loop)
|
.build(event_loop)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue