arnaucube
arnaucube/chip8-rsPublic Self-hosted repository
u8 operations with wrapping to avoid overflow
arnaucube root@arnaucube.com
Mon, 19 Oct 2020 20:44:08 +0200
commit

0d75e866a9805a2165db531e2d1f0171adff77c5

parent

d68feb1fd98117ecb8c065ae60accb35fe058ffd

M README.md → README.md

@@ -1,6 +1,17 @@

# chip8-rs CHIP-8 emulator written in Rust. -(Done with the Go implementation: https://github.com/arnaucube/go-chip8) + +*(Done with the Go implementation: https://github.com/arnaucube/go-chip8)* https://en.wikipedia.org/wiki/CHIP-8 + +## Usage +``` +./chip8-rs --file roms/invaders.c8 +``` + +![](screenshots/s0.png) +![](screenshots/s1.png) +![](screenshots/s2.png) +![](screenshots/s3.png)
M chip8/src/lib.rs → chip8/src/lib.rs

@@ -15,7 +15,7 @@ pub gfx: [u8; w * h],

delay_timer: u8, sound_timer: u8, stack: [u16; 16], - sp: isize, + sp: usize, pub key: [u8; 16], pub draw_flag: bool, }

@@ -77,7 +77,7 @@ let x: usize = ((self.opcode & 0x0F00) >> 8) as usize;

let y: usize = ((self.opcode & 0x00F0) >> 4) as usize; let nn: u8 = (self.opcode & 0x00FF) as u8; let nnn: u16 = (self.opcode & 0x0FFF) as u16; - // println!("{:?} {:?}", self.opcode, self.pc); + // println!("{:x} {:?}", self.opcode, self.pc); // Decode Opcode // https://en.wikipedia.org/wiki/CHIP-8#Opcode_table

@@ -95,10 +95,10 @@ }

0x000E => { // 00EE Returns from a subroutine self.sp -= 1; - self.pc = self.stack[self.sp as usize]; + self.pc = self.stack[self.sp]; self.pc += 2; } - _ => println!("unk {:x}", self.opcode), + _ => println!("Unknown opcode: {:x}", self.opcode), } } 0x1000 => {

@@ -107,7 +107,7 @@ self.pc = nnn;

} 0x2000 => { // 2NNN Calls subroutine at NNN - self.stack[self.sp as usize] = self.pc; + self.stack[self.sp] = self.pc; self.sp += 1; self.pc = nnn; }

@@ -143,7 +143,7 @@ self.pc += 2;

} 0x7000 => { // 7XNN Adds NN to VX. (Carry flag is not changed) - self.v[x] += nn; + self.v[x] = self.v[x].wrapping_add(nn); self.pc += 2; } 0x8000 => {

@@ -176,7 +176,7 @@ self.v[0xF] = 1;

} else { self.v[0xF] = 0; } - self.v[x] += self.v[y]; + self.v[x] = self.v[x].wrapping_add(self.v[y]); self.pc += 2; } 0x0005 => {

@@ -187,7 +187,7 @@ self.v[0xF] = 1;

} else { self.v[0xF] = 0; } - self.v[x] -= self.v[y]; + self.v[x] = self.v[x].wrapping_sub(self.v[y]); self.pc += 2; } 0x0006 => {

@@ -198,7 +198,7 @@ self.v[0xF] = 1;

} else { self.v[0xF] = 0; } - self.v[x] = self.v[x] >> 1; + self.v[x] = self.v[x].wrapping_shr(1); self.pc += 2; } 0x0007 => {

@@ -209,7 +209,7 @@ self.v[0xF] = 1;

} else { self.v[0xF] = 0; } - self.v[x] = self.v[y] - self.v[x]; + self.v[x] = self.v[y].wrapping_sub(self.v[x]); self.pc += 2; } 0x000E => {

@@ -220,10 +220,10 @@ self.v[0xF] = 1;

} else { self.v[0xF] = 0; } - self.v[x] = self.v[x] << 1; + self.v[x] = self.v[x].wrapping_shl(1); self.pc += 2; } - _ => println!("unk {:x}", self.opcode), + _ => println!("Unknown opcode: {:x}", self.opcode), } } 0x9000 => {

@@ -243,7 +243,7 @@ }

0xB000 => { // BNNN Jumps to the address NNN plus V0 self.pc = nnn + self.v[0] as u16; - self.pc += 2; + // self.pc += 2; } 0xC000 => { // CXNN Sets VX to the result of a bitwise and operation on a

@@ -275,9 +275,8 @@ break;

} if self.gfx[pos] == 1 { self.v[0xF] = 1; - } else { - self.gfx[pos] ^= 1; } + self.gfx[pos] ^= 1; } } }

@@ -305,7 +304,7 @@ self.pc += 2;

} self.pc += 2; } - _ => println!("unk {:x}", self.opcode), + _ => println!("Unknown opcode: {:x}", self.opcode), } } 0xF000 => {

@@ -378,10 +377,10 @@ self.v[i] = self.memory[self.index as usize + i];

} self.pc += 2; } - _ => println!("unk {:x}", self.opcode), + _ => println!("Unknown opcode: {:x}", self.opcode), } } - _ => println!("opc {:x}", self.opcode), + _ => println!("Unknown opcode: {:x}", self.opcode), } if self.delay_timer > 0 { self.delay_timer -= 1;
M src/main.rs → src/main.rs

@@ -30,7 +30,7 @@ let sdl_context = sdl2::init().unwrap();

let video_subsystem = sdl_context.video().unwrap(); let window = video_subsystem - .window("rust-sdl2 demo", (w * zoom) as u32, (h * zoom) as u32) + .window("chip8-rs", (w * zoom) as u32, (h * zoom) as u32) .position_centered() .build() .unwrap();

@@ -45,14 +45,15 @@ vkeys.insert(Keycode::Num4, 0x0c);

vkeys.insert(Keycode::Q, 0x04); vkeys.insert(Keycode::W, 0x05); vkeys.insert(Keycode::E, 0x06); - vkeys.insert(Keycode::R, 0x07); - vkeys.insert(Keycode::A, 0x08); - vkeys.insert(Keycode::S, 0x09); - vkeys.insert(Keycode::D, 0x0E); - vkeys.insert(Keycode::F, 0x0A); - vkeys.insert(Keycode::Z, 0x00); - vkeys.insert(Keycode::X, 0x0B); - vkeys.insert(Keycode::V, 0x0F); + vkeys.insert(Keycode::R, 0x0d); + vkeys.insert(Keycode::A, 0x07); + vkeys.insert(Keycode::S, 0x08); + vkeys.insert(Keycode::D, 0x09); + vkeys.insert(Keycode::F, 0x0e); + vkeys.insert(Keycode::Z, 0x0a); + vkeys.insert(Keycode::X, 0x00); + vkeys.insert(Keycode::C, 0x0b); + vkeys.insert(Keycode::V, 0x0f); SdlEmulator { w,

@@ -152,6 +153,6 @@ if e.chip8.draw_flag {

e.draw_graphics(); } e.set_keys(); - std::thread::sleep(time::Duration::from_millis(1000 / 60)); + std::thread::sleep(time::Duration::from_millis(200 / 60)); } }