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) {
|
||||
let opcode = self.get_opcode();
|
||||
// println!("{:#x}", self.opcode);
|
||||
self.pc += 2; // Immediately increment the Program Counter
|
||||
|
||||
self.execute_opcode(opcode);
|
||||
|
||||
self.delay.tick();
|
||||
|
@ -82,7 +83,6 @@ impl Chip8 {
|
|||
|
||||
fn get_opcode(&self) -> u16 {
|
||||
let pc = self.pc as usize;
|
||||
|
||||
((self.memory[pc] as u16) << 8) | self.memory[pc + 1] as u16
|
||||
}
|
||||
|
||||
|
@ -105,8 +105,6 @@ impl Chip8 {
|
|||
let y = nib_3;
|
||||
let n = nib_4;
|
||||
|
||||
self.pc += 2;
|
||||
|
||||
match (nib_1, nib_2, nib_3, nib_4) {
|
||||
// CLS
|
||||
(0x0, 0x0, 0xE, 0x0) => self.cls(),
|
||||
|
@ -197,7 +195,7 @@ impl Chip8 {
|
|||
|
||||
fn jmp_addr(&mut self, nnn: u16) {
|
||||
// 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) {
|
||||
|
@ -205,7 +203,7 @@ impl Chip8 {
|
|||
// pc is then set to addr
|
||||
self.sp += 1;
|
||||
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) {
|
||||
|
@ -323,7 +321,7 @@ impl Chip8 {
|
|||
|
||||
fn jmp_addr_with_offset(&mut self, nnn: u16) {
|
||||
// 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) {
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -15,17 +15,15 @@ static WIDTH: u32 = 64;
|
|||
static HEIGHT: u32 = 32;
|
||||
|
||||
fn main() {
|
||||
let mut chip8: Chip8 = Default::default();
|
||||
let mut input = WinitInputHelper::new();
|
||||
let event_loop = EventLoop::new();
|
||||
let window = init_window(&event_loop);
|
||||
let mut hidpi_factor = window.scale_factor();
|
||||
let mut pixels = init_pixels(&window);
|
||||
|
||||
let mut hidpi_factor = window.scale_factor();
|
||||
let mut input = WinitInputHelper::new();
|
||||
|
||||
let mut chip8: Chip8 = Default::default();
|
||||
chip8
|
||||
.load_rom(Path::new("./games/ibm_logo.ch8"))
|
||||
.expect("Unable to load ROM");
|
||||
let rom_path = Path::new("./games/ibm_logo.ch8");
|
||||
chip8.load_rom(rom_path).expect("Unable to load ROM");
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
if let Event::RedrawRequested(_) = event {
|
||||
|
@ -67,11 +65,13 @@ fn init_pixels(window: &Window) -> Pixels {
|
|||
}
|
||||
|
||||
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()
|
||||
.with_title("Chip8 Emulator")
|
||||
.with_inner_size(size)
|
||||
.with_min_inner_size(size)
|
||||
.with_min_inner_size(min_size)
|
||||
.build(event_loop)
|
||||
.unwrap()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue