2023-01-15 19:53:15 +11:00
|
|
|
#![feature(exclusive_range_pattern)]
|
|
|
|
|
2023-01-16 12:13:53 +11:00
|
|
|
mod processor;
|
|
|
|
|
2023-01-22 12:13:02 +11:00
|
|
|
use clap::{ArgGroup, Parser};
|
2023-02-03 09:15:30 +11:00
|
|
|
use minifb::{Window, WindowOptions};
|
2023-02-06 20:54:26 +11:00
|
|
|
use processor::{
|
2023-02-07 09:19:50 +11:00
|
|
|
memory::{rom::ROM, Memory},
|
2023-02-07 09:12:39 +11:00
|
|
|
Registers, CPU,
|
2023-02-06 20:54:26 +11:00
|
|
|
};
|
2023-01-18 12:46:15 +11:00
|
|
|
use std::{
|
|
|
|
fs,
|
|
|
|
io::{self, stdout, Write},
|
2023-01-22 12:13:02 +11:00
|
|
|
sync::RwLock,
|
2023-01-18 12:46:15 +11:00
|
|
|
};
|
2023-01-15 19:53:15 +11:00
|
|
|
|
2023-01-22 12:13:02 +11:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! verbose_println {
|
|
|
|
($($tts:tt)*) => {
|
|
|
|
if crate::is_verbose() {
|
|
|
|
println!($($tts)*);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! verbose_print {
|
|
|
|
($($tts:tt)*) => {
|
|
|
|
if crate::is_verbose() {
|
|
|
|
print!($($tts)*);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-15 19:53:15 +11:00
|
|
|
/// Simple program to greet a person
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
2023-01-22 12:13:02 +11:00
|
|
|
#[command(group(ArgGroup::new("prints").args(["verbose","cycle_count"])))]
|
2023-01-15 19:53:15 +11:00
|
|
|
struct Args {
|
|
|
|
/// ROM path
|
|
|
|
#[arg(short, long)]
|
|
|
|
rom: String,
|
2023-01-16 11:34:36 +11:00
|
|
|
|
|
|
|
/// BootROM path
|
|
|
|
#[arg(short, long)]
|
|
|
|
bootrom: String,
|
2023-01-16 14:43:11 +11:00
|
|
|
|
|
|
|
/// Just run BootROM
|
|
|
|
#[arg(long)]
|
|
|
|
run_bootrom: bool,
|
2023-01-17 08:58:37 +11:00
|
|
|
|
2023-01-22 12:13:02 +11:00
|
|
|
/// Verbose print
|
|
|
|
#[arg(short, long)]
|
|
|
|
verbose: bool,
|
|
|
|
|
|
|
|
/// Show cycle count
|
|
|
|
#[arg(short, long)]
|
|
|
|
cycle_count: bool,
|
|
|
|
|
2023-02-08 09:40:54 +11:00
|
|
|
/// Show tile window
|
|
|
|
#[arg(short, long)]
|
|
|
|
tile_window: bool,
|
|
|
|
|
2023-01-17 08:58:37 +11:00
|
|
|
/// Step emulation by...
|
|
|
|
#[arg(long)]
|
|
|
|
step_by: Option<usize>,
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
|
2023-01-22 09:33:18 +11:00
|
|
|
fn cpu_ram_init(cpu: &mut CPU) {
|
|
|
|
cpu.memory.set(0xFF10, 0x80);
|
|
|
|
cpu.memory.set(0xFF11, 0xBF);
|
|
|
|
cpu.memory.set(0xFF12, 0xF3);
|
|
|
|
cpu.memory.set(0xFF14, 0xBF);
|
|
|
|
cpu.memory.set(0xFF16, 0x3F);
|
|
|
|
cpu.memory.set(0xFF19, 0xBF);
|
|
|
|
cpu.memory.set(0xFF1A, 0x7F);
|
|
|
|
cpu.memory.set(0xFF1B, 0xFF);
|
|
|
|
cpu.memory.set(0xFF1C, 0x9F);
|
|
|
|
cpu.memory.set(0xFF1E, 0xBF);
|
|
|
|
cpu.memory.set(0xFF20, 0xFF);
|
|
|
|
cpu.memory.set(0xFF23, 0xBF);
|
|
|
|
cpu.memory.set(0xFF24, 0x77);
|
|
|
|
cpu.memory.set(0xFF25, 0xF3);
|
|
|
|
cpu.memory.set(0xFF26, 0xF1);
|
|
|
|
cpu.memory.set(0xFF40, 0x91);
|
|
|
|
cpu.memory.set(0xFF47, 0xFC);
|
|
|
|
cpu.memory.set(0xFF48, 0xFF);
|
|
|
|
cpu.memory.set(0xFF49, 0xFF);
|
|
|
|
}
|
|
|
|
|
2023-01-22 09:07:57 +11:00
|
|
|
static mut PAUSE_ENABLED: bool = false;
|
|
|
|
static mut PAUSE_QUEUED: bool = false;
|
2023-01-22 12:13:02 +11:00
|
|
|
// static mut VERBOSE: bool = false;
|
|
|
|
static VERBOSE: RwLock<bool> = RwLock::new(false);
|
2023-01-22 09:07:57 +11:00
|
|
|
|
2023-02-03 09:15:30 +11:00
|
|
|
const WIDTH: usize = 160;
|
|
|
|
const HEIGHT: usize = 144;
|
2023-02-05 22:56:18 +11:00
|
|
|
const FACTOR: usize = 3;
|
2023-02-03 09:15:30 +11:00
|
|
|
|
2023-01-15 19:53:15 +11:00
|
|
|
fn main() {
|
|
|
|
let args = Args::parse();
|
2023-01-22 12:13:02 +11:00
|
|
|
{
|
|
|
|
let mut v = VERBOSE.write().unwrap();
|
|
|
|
*v = args.verbose;
|
|
|
|
}
|
2023-01-15 19:53:15 +11:00
|
|
|
|
2023-02-07 09:51:11 +11:00
|
|
|
let rom: ROM = ROM::load(fs::read(args.rom).expect("Could not load ROM"));
|
|
|
|
let bootrom: Vec<u8> = fs::read(args.bootrom).expect("Could not load BootROM");
|
|
|
|
|
2023-02-06 12:17:58 +11:00
|
|
|
let mut window = Window::new(
|
2023-02-07 09:51:11 +11:00
|
|
|
format!("{}", rom.get_title()).as_str(),
|
2023-02-05 22:56:18 +11:00
|
|
|
WIDTH * FACTOR,
|
|
|
|
HEIGHT * FACTOR,
|
|
|
|
WindowOptions::default(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
panic!("{}", e);
|
|
|
|
});
|
2023-02-03 09:15:30 +11:00
|
|
|
|
2023-02-06 12:44:58 +11:00
|
|
|
window.set_position(500, 50);
|
2023-02-06 12:17:58 +11:00
|
|
|
|
2023-02-05 18:46:55 +11:00
|
|
|
window.topmost(true);
|
2023-02-03 09:15:30 +11:00
|
|
|
|
2023-02-08 09:40:54 +11:00
|
|
|
let mut cpu = CPU::new(
|
|
|
|
Memory::init(bootrom, args.run_bootrom, rom),
|
|
|
|
window,
|
|
|
|
args.tile_window,
|
|
|
|
);
|
2023-02-06 11:18:18 +11:00
|
|
|
|
2023-02-06 12:32:10 +11:00
|
|
|
if !args.run_bootrom {
|
|
|
|
cpu.reg.pc = 0x0100;
|
2023-02-06 11:18:18 +11:00
|
|
|
cpu_ram_init(&mut cpu);
|
|
|
|
}
|
|
|
|
|
2023-01-18 12:46:15 +11:00
|
|
|
let mut cycle_num = 0;
|
2023-01-22 09:07:57 +11:00
|
|
|
let mut instructions_seen = vec![];
|
2023-02-01 17:18:08 +11:00
|
|
|
let mut last_state = cpu.reg.clone();
|
2023-01-22 12:13:02 +11:00
|
|
|
let mut next_state = last_state;
|
2023-01-27 11:12:38 +11:00
|
|
|
|
|
|
|
verbose_println!("\n\n Begin execution...\n");
|
2023-01-17 08:58:37 +11:00
|
|
|
match args.step_by {
|
|
|
|
Some(step_size) => loop {
|
|
|
|
for _ in 0..step_size {
|
2023-01-18 12:46:15 +11:00
|
|
|
cycle_num += 1;
|
2023-01-22 12:13:02 +11:00
|
|
|
if args.cycle_count {
|
|
|
|
print_cycles(&cycle_num);
|
|
|
|
}
|
|
|
|
run_cycle(
|
|
|
|
&mut cpu,
|
|
|
|
&mut next_state,
|
|
|
|
&mut last_state,
|
|
|
|
&mut instructions_seen,
|
2023-01-18 12:46:15 +11:00
|
|
|
);
|
|
|
|
}
|
|
|
|
print!(
|
|
|
|
" ...{} cycles - press enter to continue\r",
|
|
|
|
cycle_num
|
|
|
|
);
|
|
|
|
stdout().flush().unwrap();
|
2023-01-22 12:13:02 +11:00
|
|
|
pause_once();
|
2023-01-17 08:58:37 +11:00
|
|
|
},
|
|
|
|
None => loop {
|
2023-01-18 12:46:15 +11:00
|
|
|
cycle_num += 1;
|
2023-01-22 12:13:02 +11:00
|
|
|
if args.cycle_count {
|
|
|
|
print_cycles(&cycle_num);
|
2023-01-22 09:07:57 +11:00
|
|
|
}
|
2023-01-22 12:13:02 +11:00
|
|
|
run_cycle(
|
|
|
|
&mut cpu,
|
|
|
|
&mut next_state,
|
|
|
|
&mut last_state,
|
|
|
|
&mut instructions_seen,
|
|
|
|
);
|
2023-01-17 08:58:37 +11:00
|
|
|
},
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
}
|
2023-01-15 21:05:28 +11:00
|
|
|
|
2023-01-22 12:13:02 +11:00
|
|
|
fn run_cycle(
|
|
|
|
cpu: &mut CPU,
|
2023-02-01 17:18:08 +11:00
|
|
|
next_state: &mut Registers,
|
|
|
|
last_state: &mut Registers,
|
2023-01-22 12:13:02 +11:00
|
|
|
instructions_seen: &mut Vec<u8>,
|
|
|
|
) {
|
|
|
|
let will_pause;
|
|
|
|
unsafe {
|
|
|
|
will_pause = PAUSE_QUEUED.clone();
|
|
|
|
}
|
|
|
|
cpu.exec_next();
|
|
|
|
unsafe {
|
2023-02-01 17:18:08 +11:00
|
|
|
*next_state = cpu.reg;
|
2023-01-22 12:13:02 +11:00
|
|
|
if !PAUSE_ENABLED {
|
2023-02-01 17:18:08 +11:00
|
|
|
if next_state.pc >= 0x100 {
|
2023-01-22 12:13:02 +11:00
|
|
|
PAUSE_ENABLED = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*last_state = *next_state;
|
|
|
|
if will_pause {
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match instructions_seen.contains(&cpu.last_instruction) {
|
|
|
|
true => {}
|
|
|
|
false => {
|
|
|
|
// println!("new instruction enountered: {:#X}", cpu.last_instruction);
|
|
|
|
instructions_seen.push(cpu.last_instruction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:34:36 +11:00
|
|
|
fn pause() {
|
2023-01-22 09:07:57 +11:00
|
|
|
unsafe {
|
|
|
|
if PAUSE_ENABLED {
|
|
|
|
let line = &mut String::new();
|
|
|
|
io::stdin().read_line(line).unwrap();
|
|
|
|
PAUSE_QUEUED = !line.contains("continue");
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 21:05:28 +11:00
|
|
|
}
|
2023-01-27 11:26:33 +11:00
|
|
|
|
2023-01-22 12:13:02 +11:00
|
|
|
fn pause_once() {
|
2023-02-06 12:44:58 +11:00
|
|
|
println!("paused...");
|
2023-01-22 12:13:02 +11:00
|
|
|
io::stdin().read_line(&mut String::new()).unwrap();
|
|
|
|
}
|
2023-01-18 12:46:15 +11:00
|
|
|
|
|
|
|
fn print_cycles(cycles: &i32) {
|
2023-02-02 19:01:04 +11:00
|
|
|
if *cycles % 45678 != 0 {
|
2023-01-18 12:46:15 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let instructions_per_second = 400000;
|
|
|
|
print!(
|
|
|
|
"cycle {} - approx {} seconds on real hardware\r",
|
|
|
|
cycles,
|
|
|
|
cycles / instructions_per_second
|
|
|
|
);
|
|
|
|
stdout().flush().unwrap();
|
|
|
|
}
|
2023-01-22 12:13:02 +11:00
|
|
|
|
|
|
|
fn is_verbose() -> bool {
|
|
|
|
match VERBOSE.read() {
|
|
|
|
Ok(v) => *v,
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|