Adds 'name' and 'operand' to Chip8Instruction

This commit is contained in:
Trevor Merritt 2024-10-18 15:34:49 -04:00
parent 1eeb746ccf
commit be6e652982
3 changed files with 243 additions and 99 deletions

View File

@ -1,6 +1,6 @@
use std::arch::x86_64::_mm_xor_pd; use std::arch::x86_64::_mm_xor_pd;
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Debug, Display, Formatter};
use std::ops::{BitAnd, Shr}; use std::ops::{BitAnd, Deref, Shr};
use std::time::Instant; use std::time::Instant;
use log::debug; use log::debug;
use rand::{random, Rng}; use rand::{random, Rng};
@ -21,7 +21,7 @@ kk or byte - An 8-bit value, the lowest 8 bits of the instruction
pub enum Chip8CpuInstructions { pub enum Chip8CpuInstructions {
/// 0nnn /// 0nnn
/// Exit to System Call at nnn /// Exit to System Call at nnn
SysAddr(i16), SysAddr(u16),
/// 00E0 /// 00E0
/// Clear the display. /// Clear the display.
CLS, CLS,
@ -35,13 +35,13 @@ pub enum Chip8CpuInstructions {
/// Jump to location nnn. /// Jump to location nnn.
/// ///
/// The interpreter sets the program counter to nnn. /// The interpreter sets the program counter to nnn.
JpAddr(i16), JpAddr(u16),
/// 2nnn /// 2nnn
/// Call subroutine at nnn. /// Call subroutine at nnn.
/// ///
/// The interpreter increments the stack pointer, then puts the current PC on the top /// The interpreter increments the stack pointer, then puts the current PC on the top
/// of the stack. The PC is then set to nnn. /// of the stack. The PC is then set to nnn.
CallAddr(i16), CallAddr(u16),
/// 0x3xkk /// 0x3xkk
/// Skip next instruction if Vx = kk. /// Skip next instruction if Vx = kk.
/// ///
@ -215,118 +215,175 @@ pub enum Chip8CpuInstructions {
XXXXERRORINSTRUCTION, XXXXERRORINSTRUCTION,
} }
impl Display for Chip8CpuInstructions { impl Chip8CpuInstructions {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { pub fn name(&self) -> &str {
write!(f, "{}", format!("{}", match self { match self {
Chip8CpuInstructions::SysAddr(x) => { Chip8CpuInstructions::SysAddr(_) => {
format!("SYS 0x{:04x}", x) "SYS"
} }
Chip8CpuInstructions::CLS => { Chip8CpuInstructions::CLS => {
String::from("CLS") "CLS"
} }
Chip8CpuInstructions::RET => { Chip8CpuInstructions::RET => {
String::from("RET") "RET"
} }
Chip8CpuInstructions::JpAddr(x) => { Chip8CpuInstructions::JpAddr(_) => {
format!("JP 0x{:04x}", x) "JPA"
} }
Chip8CpuInstructions::CallAddr(x) => { Chip8CpuInstructions::CallAddr(_) => {
format!("SUB 0x{:04x}", x) "CALL"
} }
Chip8CpuInstructions::SeVxByte(x, byte) => { Chip8CpuInstructions::SeVxByte(_, _) => {
format!("SeVxByte 0x{:02x}, 0x{:02x}", x, byte) "SeVxByte"
} }
Chip8CpuInstructions::SneVxByte(x, byte) => { Chip8CpuInstructions::SneVxByte(_, _) => {
format!("SneVxByte 0x{:02x}, 0x{:02x}", x, byte) "SneVxByte"
} }
Chip8CpuInstructions::SeVxVy(x, y) => { Chip8CpuInstructions::SeVxVy(_, _) => {
format!("SeVxVy 0x{:02x}, 0x{:02x}", x, y) "SeVeVy"
} }
Chip8CpuInstructions::LdVxByte(x, byte) => { Chip8CpuInstructions::LdVxByte(_, _) => {
format!("LdVxByte 0x{:02x}, 0x{:02x}", x, byte) "LdVxByte"
} }
Chip8CpuInstructions::AddVxByte(x, byte) => { Chip8CpuInstructions::AddVxByte(_, _) => {
format!("AddVxByte 0x{:02x}, 0x{:02x}", x, byte) "AddVxByte"
} }
Chip8CpuInstructions::LdVxVy(x, y) => { Chip8CpuInstructions::LdVxVy(_, _) => {
format!("LdVxVy 0x{:02x}, 0x{:02x}", x, y) "LdVxVy"
} }
Chip8CpuInstructions::OrVxVy(x, y) => { Chip8CpuInstructions::OrVxVy(_, _) => {
format!("OrVxVy 0x{:02x}, 0x{:02x}", x, y) "OrVxVy"
} }
Chip8CpuInstructions::AndVxVy(x, y) => { Chip8CpuInstructions::AndVxVy(_, _) => {
format!("AndVxVy 0x{:02x}, 0x{:02x}", x, y) "AndVxVy"
} }
Chip8CpuInstructions::XorVxVy(x, y) => { Chip8CpuInstructions::XorVxVy(_, _) => {
format!("XorVxVy 0x{:02x}, 0x{:02x}", x, y) "XorVxVy"
} }
Chip8CpuInstructions::AddVxVy(x, y) => { Chip8CpuInstructions::AddVxVy(_, _) => {
format!("AddVxVy 0x{:02x}, 0x{:02x}", x, y) "AddVxVy"
} }
Chip8CpuInstructions::SubVxVy(x, y) => { Chip8CpuInstructions::SubVxVy(_, _) => {
format!("SubVxVy 0x{:02x}, 0x{:02x}", x, y) "SubVxVy"
} }
Chip8CpuInstructions::ShrVxVy(x, y) => { Chip8CpuInstructions::ShrVxVy(_, _) => {
format!("ShrVxVy 0x{:02x}, 0x{:02x}", x, y) "ShrVxVy"
} }
Chip8CpuInstructions::SubnVxVy(x, y) => { Chip8CpuInstructions::SubnVxVy(_, _) => {
format!("SubnVxVy 0x{:02x}, 0x{:02x}", x, y) "SubnVxVy"
} }
Chip8CpuInstructions::ShlVxVy(x, y) => { Chip8CpuInstructions::ShlVxVy(_, _) => {
format!("ShlVxVy 0x{:02x}, 0x{:02x}", x, y) "ShlVxVy"
} }
Chip8CpuInstructions::SneVxVy(x, y) => { Chip8CpuInstructions::SneVxVy(_, _) => {
format!("SneVxVy 0x{:02x}, 0x{:02x}", x, y) "SneVxVy"
} }
Chip8CpuInstructions::LdIAddr(addr) => { Chip8CpuInstructions::LdIAddr(_) => {
format!("LdIAddr 0x{:04x}", addr) "LdIAddr"
} }
Chip8CpuInstructions::JpV0Addr(addr) => { Chip8CpuInstructions::JpV0Addr(_) => {
format!("JpV0Addr 0x{:04x}", addr) "JpV0Addr"
} }
Chip8CpuInstructions::RndVxByte(x, byte) => { Chip8CpuInstructions::RndVxByte(_, _) => {
format!("RndVxByte 0x:{:02x}, 0x{:02x}", x, byte) "RndVxByte"
} }
Chip8CpuInstructions::DrawVxVyNibble(x, y, nibble) => { Chip8CpuInstructions::DrawVxVyNibble(_, _, _) => {
format!("DrawVxVyNibble 0x:{:02x}, 0x{:02x}, 0x{:02x}", x, y, nibble) "DrawVxVyNibble"
} }
Chip8CpuInstructions::SkpVx(x) => { Chip8CpuInstructions::SkpVx(_) => {
format!("SkpVx 0x{:02x}", x) "SkpVx"
} }
Chip8CpuInstructions::SnkpVx(x) => { Chip8CpuInstructions::SnkpVx(_) => {
format!("SnkpVx 0x{:02x}", x) "SnkpVx"
} }
Chip8CpuInstructions::LdVxDt(x) => { Chip8CpuInstructions::LdVxDt(_) => {
format!("LdVxDt 0x{:02x}", x) "LdVxDt"
} }
Chip8CpuInstructions::LdVxK(x) => { Chip8CpuInstructions::LdVxK(_) => {
format!("LdVxK 0x{:02x}", x) "LdVxK"
} }
Chip8CpuInstructions::LdDtVx(x) => { Chip8CpuInstructions::LdDtVx(_) => {
format!("LdDtVx 0x{:02x}", x) "LdDtVx"
} }
Chip8CpuInstructions::LdStVx(x) => { Chip8CpuInstructions::LdStVx(_) => {
format!("LdStVx 0x{:02x}", x) "LdStVx"
} }
Chip8CpuInstructions::AddIVx(x) => { Chip8CpuInstructions::AddIVx(_) => {
format!("AddIVx 0x{:02x}", x) "AddIVx"
} }
Chip8CpuInstructions::LdFVx(x) => { Chip8CpuInstructions::LdFVx(_) => {
format!("LdFVx 0x{:02x}", x) "LdFVx"
} }
Chip8CpuInstructions::LdBVx(x) => { Chip8CpuInstructions::LdBVx(_) => {
format!("LdBVx 0x{:02x}", x) "LdBVx"
} }
Chip8CpuInstructions::LdIVx(x) => { Chip8CpuInstructions::LdIVx(_) => {
format!("LdIVx 0x{:02x}", x) "LdIVx"
} }
Chip8CpuInstructions::LdVxI(i) => { Chip8CpuInstructions::LdVxI(_) => {
format!("LdVxI 0x{:02x}", i) "LdVxI"
} }
XXXXERRORINSTRUCTION => { XXXXERRORINSTRUCTION => {
String::from("INVALID INSTRUCTION") "XX ERROR XX"
} }
})) }
}
pub fn operands(&self) -> String {
match self {
Chip8CpuInstructions::SysAddr(addr) |
Chip8CpuInstructions::JpV0Addr(addr) |
Chip8CpuInstructions::JpAddr(addr) |
Chip8CpuInstructions::LdIAddr(addr) |
Chip8CpuInstructions::CallAddr(addr) => {
format!("{addr:04x}")
}
Chip8CpuInstructions::SeVxByte(x, byte) |
Chip8CpuInstructions::SneVxByte(x, byte) |
Chip8CpuInstructions::LdVxByte(x, byte) |
Chip8CpuInstructions::RndVxByte(x, byte) |
Chip8CpuInstructions::AddVxByte(x, byte) => {
format!("{x:02x}, {byte:04x}")
}
Chip8CpuInstructions::SeVxVy(x,y) |
Chip8CpuInstructions::LdVxVy(x,y) |
Chip8CpuInstructions::OrVxVy(x,y) |
Chip8CpuInstructions::AndVxVy(x,y) |
Chip8CpuInstructions::XorVxVy(x,y) |
Chip8CpuInstructions::AddVxVy(x,y) |
Chip8CpuInstructions::SubVxVy(x,y) |
Chip8CpuInstructions::ShrVxVy(x,y) |
Chip8CpuInstructions::SubnVxVy(x,y) |
Chip8CpuInstructions::ShlVxVy(x,y) |
Chip8CpuInstructions::SneVxVy(x,y) => {
format!("{x:02x}, {y:02x}")
}
Chip8CpuInstructions::DrawVxVyNibble(x,y,nibble) => {
format!("{x:02x}, {y:02x}, {nibble:02x}")
}
Chip8CpuInstructions::LdDtVx(x) |
Chip8CpuInstructions::LdStVx(x) |
Chip8CpuInstructions::AddIVx(x) |
Chip8CpuInstructions::LdFVx(x) |
Chip8CpuInstructions::LdBVx(x) |
Chip8CpuInstructions::LdIVx(x) |
Chip8CpuInstructions::SkpVx(x) |
Chip8CpuInstructions::LdVxDt(x) |
Chip8CpuInstructions::LdVxK(x) |
Chip8CpuInstructions::LdVxI(x) |
Chip8CpuInstructions::SnkpVx(x) => {
format!("{x:02x}")
}
_ => {
"".parse().unwrap()
}
}
}
}
impl Display for Chip8CpuInstructions {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.name(), self.operands())
} }
} }
@ -386,9 +443,9 @@ impl Chip8CpuInstructions {
match input { match input {
0x00E0 => Chip8CpuInstructions::CLS, 0x00E0 => Chip8CpuInstructions::CLS,
0x00EE => Chip8CpuInstructions::RET, 0x00EE => Chip8CpuInstructions::RET,
0x0000..=0x0FFF => Chip8CpuInstructions::SysAddr(addr_param as i16), 0x0000..=0x0FFF => Chip8CpuInstructions::SysAddr(addr_param),
0x1000..=0x1FFF => Chip8CpuInstructions::JpAddr(addr_param as i16), 0x1000..=0x1FFF => Chip8CpuInstructions::JpAddr(addr_param),
0x2000..=0x2FFF => Chip8CpuInstructions::CallAddr(addr_param as i16), 0x2000..=0x2FFF => Chip8CpuInstructions::CallAddr(addr_param),
0x3000..=0x3FFF => Chip8CpuInstructions::SeVxByte(x_param, byte_param), 0x3000..=0x3FFF => Chip8CpuInstructions::SeVxByte(x_param, byte_param),
0x4000..=0x4FFF => Chip8CpuInstructions::SneVxByte(x_param, byte_param), 0x4000..=0x4FFF => Chip8CpuInstructions::SneVxByte(x_param, byte_param),
0x5000..=0x5FF0 if input & 0x01 == 0 => Chip8CpuInstructions::SeVxVy(x_param, y_param), 0x5000..=0x5FF0 if input & 0x01 == 0 => Chip8CpuInstructions::SeVxVy(x_param, y_param),
@ -412,9 +469,9 @@ impl Chip8CpuInstructions {
0xC000..=0xCFFF => Chip8CpuInstructions::RndVxByte(x_param, byte_param), 0xC000..=0xCFFF => Chip8CpuInstructions::RndVxByte(x_param, byte_param),
0xD000..=0xDFFF => Chip8CpuInstructions::DrawVxVyNibble(x_param, y_param, nibble_param), 0xD000..=0xDFFF => Chip8CpuInstructions::DrawVxVyNibble(x_param, y_param, nibble_param),
0xE09E..=0xEFA1 => match last_byte { 0xE09E..=0xEFA1 => match last_byte {
0x9E => Chip8CpuInstructions::SkpVx(ubln), 0x9E => Chip8CpuInstructions::SkpVx(ubln),
0xA1 => Chip8CpuInstructions::SnkpVx(ubln), 0xA1 => Chip8CpuInstructions::SnkpVx(ubln),
_ => XXXXERRORINSTRUCTION _ => XXXXERRORINSTRUCTION
} }
0xF007..=0xFF65 => match last_byte { 0xF007..=0xFF65 => match last_byte {
0x07 => Chip8CpuInstructions::LdVxDt(ubln), 0x07 => Chip8CpuInstructions::LdVxDt(ubln),
@ -432,7 +489,7 @@ impl Chip8CpuInstructions {
} }
} }
pub fn execute(&self, input: &mut Chip8Computer) -> Chip8Computer { pub fn execute(&self, input: &mut Chip8Computer) -> Chip8Computer {
// print!("INSTRUCTION {}", self); // print!("INSTRUCTION {}", self);
let start_time = Instant::now(); let start_time = Instant::now();
let start_pc = input.registers.peek_pc(); let start_pc = input.registers.peek_pc();
input.registers.poke_pc(start_pc + 2); input.registers.poke_pc(start_pc + 2);
@ -624,7 +681,7 @@ impl Chip8CpuInstructions {
// Set I = nnn. // Set I = nnn.
// //
// The value of register I is set to nnn. // The value of register I is set to nnn.
// debug!("LdiAddr [0x{new_index:3x}]"); // debug!("LdiAddr [0x{new_index:3x}]");
input.registers.poke_i(*new_index); input.registers.poke_i(*new_index);
} }
// 0xBnnn Jump to nnn+V0 // 0xBnnn Jump to nnn+V0
@ -680,7 +737,6 @@ impl Chip8CpuInstructions {
}; };
input.registers.poke(0xf, target); input.registers.poke(0xf, target);
} }
Chip8CpuInstructions::SkpVx(x) => { Chip8CpuInstructions::SkpVx(x) => {
// Ex9E - SKP Vx // Ex9E - SKP Vx
@ -811,7 +867,7 @@ impl Chip8CpuInstructions {
Chip8CpuInstructions::XXXXERRORINSTRUCTION => {} Chip8CpuInstructions::XXXXERRORINSTRUCTION => {}
}; };
let cycle_time = Instant::now().duration_since(start_time).as_nanos(); let cycle_time = Instant::now().duration_since(start_time).as_nanos();
// println!("\t\tTook {cycle_time}ms"); // println!("\t\tTook {cycle_time}ms");
input.to_owned() input.to_owned()
} }
} }

View File

@ -8,24 +8,50 @@ use gemma::chip8::computer_manager::Chip8ComputerManager;
mod support; mod support;
#[derive(Default)]
struct DisplayOptions {
pub video: bool,
pub registers: bool,
pub memory: bool
}
pub struct GemmaViewerState {
pub selected_file_index: i32,
pub selected_filename: String,
pub display_options: DisplayOptions
}
impl Default for GemmaViewerState {
fn default() -> Self {
GemmaViewerState {
selected_file_index: -1,
selected_filename: String::new(),
display_options: Default::default()
}
}
}
impl GemmaViewerState {
pub fn new() -> GemmaViewerState {
GemmaViewerState {
..Default::default()
}
}
}
fn main() -> eframe::Result { fn main() -> eframe::Result {
println!("Taxation is Theft"); println!("Taxation is Theft");
let mut computer = Chip8ComputerManager::new();
let mut state = GemmaViewerState::new();
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]), viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
..Default::default() ..Default::default()
}; };
// let mut state = GemmaEGuiState::default(); eframe::run_simple_native("EGUI Gemma", options, move |ctx, _frame| {
let mut computer = Chip8ComputerManager::new();
let mut cps_counter = 0;
let mut last_counter_update = Instant::now();
let cps_refresh = 5;
let mut last_frame_start = Instant::now();
eframe::run_simple_native("EGUI Emma", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
computer.tick();
let local_computer = computer.state(); let local_computer = computer.state();
//if state.display_video { //if state.display_video {
GemmaEguiSupport::video_view(local_computer, ui); GemmaEguiSupport::video_view(local_computer, ui);
@ -40,7 +66,6 @@ fn main() -> eframe::Result {
// if state.display_registers { // if state.display_registers {
GemmaEguiSupport::registers_view(&local_computer, ui); GemmaEguiSupport::registers_view(&local_computer, ui);
// } // }
computer.tick();
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| { ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
if ui.button("Start").clicked() { if ui.button("Start").clicked() {
computer.core_should_run = true; computer.core_should_run = true;
@ -58,8 +83,10 @@ fn main() -> eframe::Result {
// state.is_running = false; // state.is_running = false;
} }
}); });
//
// if ui.button(format!("Load {}", state.selected_rom_filename)).clicked() { if ui.button(format!("Load {}", state.selected_filename)).clicked() {
}
// // load the bin... // // load the bin...
// let read_bin = std::fs::read(PathBuf::from(format!("resources/roms/{}", state.selected_rom_filename))).unwrap(); // let read_bin = std::fs::read(PathBuf::from(format!("resources/roms/{}", state.selected_rom_filename))).unwrap();
// // ...then feed the system. // // ...then feed the system.

View File

@ -0,0 +1,61 @@
use egui::Ui;
use gemma::chip8::instructions::Chip8CpuInstructions;
/// GemmaEGui - Viewer
///
///
/// This program lets a user open a CH8 file and it will be decoded
/// as Chip-8 Instructions.
/// Colour variants for instructions show colours for
/// -> Jumps
/// -> Load
/// -> Store
/// -> Timers
/// -> Math (Add, Sub)
/// -> Logic
/// -> Video
fn display_instruction(offset: i32, to_display: &Chip8CpuInstructions, ui: &mut Ui) {
ui.label(format!("{offset:04x} {}", to_display.name()));
ui.label(format!("{}", to_display.operands()));
ui.end_row();
}
fn display_instructions(base_offset: i32, instructions: Vec<Chip8CpuInstructions>, ui: &mut Ui) {
egui::Grid::new("my_grid")
.min_col_width(200.0)
.show(ui, |ui| {
for (index, instruction) in instructions.iter().enumerate() {
display_instruction(base_offset + index as i32, instruction, ui);
}
});
}
fn main() -> eframe::Result {
let mut edit_space: String = String::new();
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([640.0, 480.0]),
..Default::default()
};
eframe::run_simple_native("Gemma - Chip8 Machine Code Viewer", options, move |ctx, frame| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("Taxation is Theft");
// file picker
ui.vertical_centered(|ui| {
if ui.button("Load File").clicked() {
println!("Clicked load file");
}
});
ui.label("File Picker Herre");
ui.label("display of file here");
ui.vertical(|ui| {
display_instruction(0x010, &Chip8CpuInstructions::AddIVx(0x01), ui);
display_instruction(0x012, &Chip8CpuInstructions::RndVxByte(0x01, 0x02), ui);
});
ui.text_edit_multiline(&mut edit_space);
});
})
}