8xy4 and 8xy5 pass.
more flag tests passing removes legacy code moves 'gemmaemu' into 'gemma' crate moves 'gemmaimgui' into its own crate update to gemma
This commit is contained in:
@@ -1,19 +1,14 @@
|
||||
use log::{debug, error};
|
||||
use log::{debug};
|
||||
use crate::chip8::delay_timer::DelayTimer;
|
||||
use crate::chip8::instructions::Chip8CpuInstructions::XXXXERRORINSTRUCTION;
|
||||
use crate::chip8::keypad::Keypad;
|
||||
use crate::chip8::registers::Chip8Registers;
|
||||
use crate::chip8::sound_timer::SoundTimer;
|
||||
use crate::chip8::stack::Chip8Stack;
|
||||
use crate::chip8::util::InstructionUtil;
|
||||
use crate::constants::{CHIP8_MEMORY_SIZE, CHIP8_REGISTER_COUNT};
|
||||
|
||||
use super::{
|
||||
cpu_states::Chip8CpuStates, instructions::Chip8CpuInstructions, system_memory::Chip8SystemMemory, video::Chip8Video,
|
||||
};
|
||||
|
||||
const STACK_POINTER_DEFAULT: i16 = 0x100;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Chip8Computer {
|
||||
pub memory: Chip8SystemMemory,
|
||||
@@ -85,7 +80,7 @@ impl Chip8Computer {
|
||||
debug!("Stepping System 1 Step");
|
||||
// read the next instruction
|
||||
|
||||
let mut working_instruction: u16 = 0b0000000000000000;
|
||||
// let mut working_instruction: u16 = 0b0000000000000000;
|
||||
let start_pc = self.registers.peek_pc();
|
||||
let high_byte = (self.memory.clone().peek(start_pc) as u16).rotate_left(8);
|
||||
let low_byte = self.memory.clone().peek(start_pc + 1) as u16;
|
||||
|
||||
+124
-36
@@ -1,12 +1,10 @@
|
||||
use std::ops::{Shl, Shr};
|
||||
use imgui::ColorPicker3;
|
||||
use std::ops::{BitAnd, Shl, Shr};
|
||||
use log::debug;
|
||||
use rand::random;
|
||||
use crate::chip8::computer::{Chip8Computer};
|
||||
use crate::chip8::cpu_states::Chip8CpuStates::WaitingForKey;
|
||||
use crate::chip8::instructions::Chip8CpuInstructions::XXXXERRORINSTRUCTION;
|
||||
use crate::chip8::util::InstructionUtil;
|
||||
use crate::chip8::video::Chip8Video;
|
||||
|
||||
/*
|
||||
nnn or addr - A 12-bit value, the lowest 12 bits of the instruction
|
||||
@@ -137,7 +135,7 @@ impl Chip8CpuInstructions {
|
||||
(0x2000 | (address & 0x0FFF)) as u16
|
||||
}
|
||||
Chip8CpuInstructions::SeVxByte(vx_register, byte) => {
|
||||
(0x3000 | (vx_register << 8 | byte) as u16)
|
||||
0x3000 | ((*vx_register as u16) << 8 | *byte as u16) as u16
|
||||
}
|
||||
Chip8CpuInstructions::SneVxByte(vx_register, byte) => {
|
||||
0x4000u16 | (*vx_register as u16) << 8 | *byte as u16
|
||||
@@ -418,7 +416,7 @@ impl Chip8CpuInstructions {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute(&self, mut input: &mut Chip8Computer) -> Chip8Computer {
|
||||
pub fn execute(&self, input: &mut Chip8Computer) -> Chip8Computer {
|
||||
let start_pc = input.registers.peek_pc();
|
||||
input.registers.poke_pc(start_pc + 2);
|
||||
let _ = match self {
|
||||
@@ -461,10 +459,18 @@ impl Chip8CpuInstructions {
|
||||
}
|
||||
// 0x4xkk Skip next instruction if Vx != kk
|
||||
Chip8CpuInstructions::SneVxByte(x, byte) => {
|
||||
if input.registers.peek(*x as u8) != *byte as u8 {
|
||||
// 4xkk - SNE Vx, byte
|
||||
// Skip next instruction if Vx != kk.
|
||||
//
|
||||
// The interpreter compares register Vx to kk, and if they are not equal,
|
||||
// increments the program counter by 2.
|
||||
let lhs = input.registers.peek(*x);
|
||||
let rhs = *byte;
|
||||
|
||||
if lhs != rhs {
|
||||
input.registers.advance_pc();
|
||||
}
|
||||
debug!("SneVxByte [0x{x:1x}] [0x{byte:2x}");
|
||||
debug!("SneVxByte [0x{x:02x}] [0x{byte:02x}");
|
||||
}
|
||||
// 0x5xy0 Skip next instruction if Vx == Vy
|
||||
Chip8CpuInstructions::SeVxVy(x, y) => {
|
||||
@@ -527,37 +533,54 @@ impl Chip8CpuInstructions {
|
||||
let lhs = input.registers.peek(*x as u8) as i16;
|
||||
let rhs = input.registers.peek(*y as u8) as i16;
|
||||
let working = lhs + rhs;
|
||||
if working > 0xff {
|
||||
input.registers.poke(0xf, 0x01);
|
||||
}
|
||||
input.registers.poke(*x as u8, working as u8);
|
||||
|
||||
if working >= 0x100 {
|
||||
input.registers.poke(0xf, 0x01);
|
||||
} else {
|
||||
input.registers.poke(0x0f, 0x00);
|
||||
}
|
||||
}
|
||||
Chip8CpuInstructions::SubVxVy(x, y) => {
|
||||
// 8xy5 - SUB Vx, Vy
|
||||
// Set Vx = Vx - Vy, set VF = NOT borrow.
|
||||
//
|
||||
// If Vx > Vy, then VF is set to 1, otherwise 0. Then Vy is subtracted from Vx, and the results stored in Vx.
|
||||
let mut x_value: u16 = input.registers.peek(*x as u8) as u16;
|
||||
let y_value = input.registers.peek(*y as u8);
|
||||
// do we borrow?
|
||||
if y_value >= x_value as u8 {
|
||||
x_value += 256;
|
||||
input.registers.poke(0xf, 1);
|
||||
|
||||
let lhs = input.registers.peek(*x);
|
||||
let rhs = input.registers.peek(*y);
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
let borrow_flag: u8 = if rhs > lhs {
|
||||
result = (lhs as u16 + 0x100) - rhs as u16;
|
||||
0
|
||||
} else {
|
||||
input.registers.poke(0xf, 0);
|
||||
}
|
||||
let result = (x_value - y_value as u16) as u8;
|
||||
input.registers.poke(*x as u8, result);
|
||||
result = lhs as u16 - rhs as u16;
|
||||
1
|
||||
};
|
||||
|
||||
input.registers.poke(*x as u8, result as u8);
|
||||
input.registers.poke(0x0f, borrow_flag);
|
||||
}
|
||||
Chip8CpuInstructions::ShrVxVy(x, _) => {
|
||||
// 8xy6 - SHR Vx {, Vy}
|
||||
// Set Vx = Vx SHR 1.
|
||||
//
|
||||
// SHIFT 1 Bit ---->
|
||||
// If the least-significant bit of Vx is 1, then VF is set to 1, otherwise 0. Then Vx is divided by 2.
|
||||
let initial_value = input.registers.peek(*x as u8);
|
||||
if 0xb1 & initial_value == 1 {
|
||||
input.registers.poke(0xf, 1);
|
||||
|
||||
// overflow check
|
||||
|
||||
if initial_value.bitand(0b1) == 1 {
|
||||
input.registers.poke(0x0f, 0x01);
|
||||
} else {
|
||||
input.registers.poke(0x0f, 0x00);
|
||||
}
|
||||
|
||||
let rotated = initial_value >> 1;
|
||||
println!("[{initial_value:80b}] / [{rotated:80b}]");
|
||||
|
||||
input.registers.poke(*x as u8, initial_value.shr(1));
|
||||
}
|
||||
Chip8CpuInstructions::SubnVxVy(x, y) => {
|
||||
@@ -567,14 +590,17 @@ impl Chip8CpuInstructions {
|
||||
// If Vy > Vx, then VF is set to 1, otherwise 0. Then Vx is subtracted from Vy, and the results stored in Vx.
|
||||
let y_register = input.registers.peek(*y as u8);
|
||||
let x_register = input.registers.peek(*x as u8);
|
||||
let new_value = if y_register <= x_register { 1 } else { 0 };
|
||||
let value_to_poke = if y_register <= x_register {
|
||||
((y_register as u16 + 256) - x_register as u16) as u8
|
||||
} else {
|
||||
y_register - x_register
|
||||
let mut value_to_poke = 0;
|
||||
|
||||
let new_value = if y_register <= x_register {
|
||||
value_to_poke = (y_register as u16 + 256) - x_register as u16;
|
||||
1 } else {
|
||||
value_to_poke = (y_register - x_register) as u16;
|
||||
0
|
||||
};
|
||||
|
||||
input.registers.poke(0xf, new_value);
|
||||
input.registers.poke(*x as u8, value_to_poke);
|
||||
input.registers.poke(*x as u8, value_to_poke as u8);
|
||||
}
|
||||
|
||||
Chip8CpuInstructions::ShlVxVy(x, _) => {
|
||||
@@ -583,10 +609,13 @@ impl Chip8CpuInstructions {
|
||||
//
|
||||
// If the most-significant bit of Vx is 1, then VF is set to 1, otherwise to 0. Then Vx is multiplied by 2.
|
||||
let initial_value = input.registers.peek(*x as u8);
|
||||
if 0x80 & initial_value == 0x80 {
|
||||
input.registers.poke(0xf, 1);
|
||||
let rotated = initial_value.shl(1);
|
||||
if 0b10000000 & initial_value == 0b10000000 {
|
||||
input.registers.poke(0x0f, 0x01);
|
||||
} else {
|
||||
input.registers.poke(0x0f, 0x00);
|
||||
}
|
||||
input.registers.poke(*x as u8, initial_value.shl(1));
|
||||
input.registers.poke(*x as u8,rotated);
|
||||
}
|
||||
Chip8CpuInstructions::SneVxVy(vx_register, vy_register) => {
|
||||
// 9xy0 - SNE Vx, Vy
|
||||
@@ -624,7 +653,7 @@ impl Chip8CpuInstructions {
|
||||
// which is then ANDed with the value kk.
|
||||
// The results are stored in Vx.
|
||||
let new_value: u8 = random();
|
||||
input.registers.poke(*x as u8, (new_value & *byte as u8))
|
||||
input.registers.poke(*x as u8, new_value & *byte as u8)
|
||||
}
|
||||
Chip8CpuInstructions::DrawVxVyNibble(y,x, n) => {
|
||||
// Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.
|
||||
@@ -762,8 +791,7 @@ impl Chip8CpuInstructions {
|
||||
let units = to_convert % 10;
|
||||
|
||||
// Convert to BCD
|
||||
let result = ((hundreds as u16) << 8) | units as u16;
|
||||
(tens << 4) | units;
|
||||
let result = ((hundreds as u16) << 8) | units as u16 | ((tens as u16) << 4) | units as u16;
|
||||
// write them to the memory pointed to by I, I+1, and I+2
|
||||
let target_start_offset = input.registers.peek_i();
|
||||
input.memory.poke(target_start_offset, hundreds);
|
||||
@@ -775,7 +803,6 @@ impl Chip8CpuInstructions {
|
||||
//
|
||||
// The interpreter copies the values of registers V0 through Vx into memory,
|
||||
// starting at the address in I.
|
||||
let num_loops = x;
|
||||
let offset = input.registers.peek_i();
|
||||
for i in 0..=*x {
|
||||
input.memory.poke(offset + i as u16, input.registers.peek(i as u8));
|
||||
@@ -1461,4 +1488,65 @@ mod test {
|
||||
Chip8CpuInstructions::LdVxK(0x1).execute(&mut x);
|
||||
assert!(matches!(x.state, WaitingForKey));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn series8xy4_corex_tests() {
|
||||
/// 8xy4
|
||||
/// Set Vx = Vx + Vy
|
||||
/// Set VF=1 if Carry
|
||||
///
|
||||
|
||||
// 1 + 1
|
||||
let mut x = Chip8Computer::new();
|
||||
x.registers.poke(0x01, 0x01);
|
||||
x.registers.poke(0x02, 0x01);
|
||||
Chip8CpuInstructions::AddVxVy(0x01, 0x02).execute(&mut x);
|
||||
assert_eq!(x.registers.peek(0x01), 0x02);
|
||||
assert_eq!(x.registers.peek(0x0f), 0x00);
|
||||
|
||||
// 255+1
|
||||
let mut x = Chip8Computer::new();
|
||||
x.registers.poke(0x01, 0xff);
|
||||
x.registers.poke(0x02, 0x01);
|
||||
Chip8CpuInstructions::AddVxVy(0x01, 0x02).execute(&mut x);
|
||||
assert_eq!(x.registers.peek(0x01), 0x00);
|
||||
assert_eq!(x.registers.peek(0x0f), 0x01);
|
||||
|
||||
// 128+192
|
||||
let mut x = Chip8Computer::new();
|
||||
x.registers.poke(0x01, 128);
|
||||
x.registers.poke(0x02, 192);
|
||||
Chip8CpuInstructions::AddVxVy(0x01, 0x02).execute(&mut x);
|
||||
assert_eq!(x.registers.peek(0x01), 64);
|
||||
assert_eq!(x.registers.peek(0x0f), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn series8xy6_corex_tests() {
|
||||
// 8xy6 - SHR Vx {, Vy}
|
||||
// Set Vx = Vx SHR 1.
|
||||
//
|
||||
// If the least-significant bit of Vx is 1, then VF is set to 1,
|
||||
// otherwise 0. Then Vx is divided by 2.
|
||||
let mut x = Chip8Computer::new();
|
||||
// 0b10000000 -> 0b01000000
|
||||
let start_value = 0b10000000;
|
||||
let end_value = 0b01000000;
|
||||
x.registers.poke(0x01, start_value);
|
||||
Chip8CpuInstructions::ShrVxVy(0x01, 0x00).execute(&mut x);
|
||||
assert_eq!(x.registers.peek(0x01), end_value);
|
||||
assert_eq!(x.registers.peek(0x0f), 0);
|
||||
|
||||
// 0b00000001 -> 0b00000000
|
||||
let start_value = 0b00000001;
|
||||
let end_value = 0b00000000;
|
||||
let mut x = Chip8Computer::new();
|
||||
let start_value = 1;
|
||||
x.registers.poke(0x01, start_value);
|
||||
Chip8CpuInstructions::ShrVxVy(0x01, 0x00).execute(&mut x);
|
||||
assert_eq!(x.registers.peek(0x01), end_value);
|
||||
assert_eq!(x.registers.peek(0x0f), 1);
|
||||
let end_value = start_value / 2;
|
||||
assert_eq!(end_value, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
use imgui::Key;
|
||||
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Keypad {
|
||||
keys: [bool; 0x10],
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use log::debug;
|
||||
|
||||
/// Registers. numbered 1-16 publicly.
|
||||
/// Privately using zero base array so -1 to shift from pub to priv.
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -7,7 +5,6 @@ pub struct Chip8Registers {
|
||||
registers: [u8; 16],
|
||||
i_register: u16,
|
||||
pc: u16,
|
||||
sp: u16,
|
||||
}
|
||||
|
||||
impl Chip8Registers {
|
||||
@@ -22,7 +19,6 @@ impl Default for Chip8Registers {
|
||||
registers: [0x00; 16],
|
||||
i_register: 0x00,
|
||||
pc: 0x200,
|
||||
sp: 0x100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::{thread, time};
|
||||
use beep::beep;
|
||||
use log::trace;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
use glium::RawUniformValue::Vec2;
|
||||
use image::load;
|
||||
use imgui::sys::ImColor;
|
||||
use imgui::{ImColor32, Ui};
|
||||
use log::{debug, trace};
|
||||
use ratatui::{style::Style, widgets::Widget};
|
||||
use log::{trace};
|
||||
|
||||
use crate::constants::{CHIP8_MEMORY_SIZE, CHIP8_VIDEO_HEIGHT, CHIP8_VIDEO_WIDTH};
|
||||
use crate::constants::{CHIP8_MEMORY_SIZE};
|
||||
|
||||
pub const CHIP8_PROGRAM_LOAD_OFFSET: i32 = 0x200;
|
||||
pub const CHIP8FONT_0: [u8; 5] = [0xF0, 0x90, 0x90, 0x90, 0xF0];
|
||||
@@ -41,10 +36,6 @@ impl Default for Chip8SystemMemory {
|
||||
x
|
||||
}
|
||||
}
|
||||
|
||||
const cell_width: i32 = 5i32;
|
||||
const cell_height: i32 = 5i32;
|
||||
|
||||
impl Chip8SystemMemory {
|
||||
|
||||
pub fn new() -> Self {
|
||||
|
||||
@@ -55,7 +55,7 @@ impl InstructionUtil {
|
||||
|
||||
// kk or byte - An 8-bit value, the lowest 8 bits of the instruction
|
||||
pub fn read_byte_from_instruction(instruction_to_read_from: u16) -> u16 {
|
||||
(instruction_to_read_from & 0x00FF)
|
||||
instruction_to_read_from & 0x00FF
|
||||
}
|
||||
|
||||
pub fn read_upper_byte_lower_nibble(to_read_from: u16) -> u16 {
|
||||
|
||||
Reference in New Issue
Block a user