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-01-16 12:13:53 +11:00
|
|
|
use processor::CPU;
|
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-02-06 12:32:10 +11:00
|
|
|
use crate::processor::Registers;
|
2023-02-01 17:18:08 +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-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-15 21:05:28 +11:00
|
|
|
type Address = u16;
|
2023-01-15 19:53:15 +11:00
|
|
|
type ROM = Vec<u8>;
|
|
|
|
|
2023-01-22 09:07:57 +11:00
|
|
|
#[allow(dead_code)]
|
2023-01-16 12:13:53 +11:00
|
|
|
pub struct Memory {
|
2023-01-18 12:46:15 +11:00
|
|
|
bootrom: ROM,
|
|
|
|
bootrom_enabled: bool,
|
2023-01-15 19:53:15 +11:00
|
|
|
rom: ROM,
|
2023-01-15 20:34:44 +11:00
|
|
|
vram: [u8; 8192],
|
|
|
|
ram: [u8; 8192],
|
2023-01-16 19:28:11 +11:00
|
|
|
switchable_ram: [u8; 8192],
|
|
|
|
cpu_ram: [u8; 128],
|
2023-01-17 09:39:05 +11:00
|
|
|
oam: [u8; 160],
|
2023-01-18 13:58:53 +11:00
|
|
|
interrupts: u8,
|
2023-01-17 09:45:49 +11:00
|
|
|
ime: bool,
|
2023-02-02 10:54:16 +11:00
|
|
|
ime_scheduled: u8,
|
2023-01-18 12:46:15 +11:00
|
|
|
io: [u8; 76],
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Memory {
|
2023-01-18 12:46:15 +11:00
|
|
|
fn init(bootrom: ROM, bootrom_enabled: bool, rom: ROM) -> Self {
|
2023-01-15 20:34:44 +11:00
|
|
|
Self {
|
2023-01-18 12:46:15 +11:00
|
|
|
bootrom,
|
|
|
|
bootrom_enabled,
|
2023-01-15 20:34:44 +11:00
|
|
|
rom,
|
|
|
|
vram: [0x0; 8192],
|
|
|
|
ram: [0x0; 8192],
|
2023-01-16 19:28:11 +11:00
|
|
|
switchable_ram: [0x0; 8192],
|
|
|
|
cpu_ram: [0x0; 128],
|
2023-01-17 09:39:05 +11:00
|
|
|
oam: [0x0; 160],
|
2023-01-18 13:58:53 +11:00
|
|
|
interrupts: 0x0,
|
2023-01-17 09:45:49 +11:00
|
|
|
ime: false,
|
2023-02-02 10:54:16 +11:00
|
|
|
ime_scheduled: 0x0,
|
2023-01-22 09:07:57 +11:00
|
|
|
io: [0xFF; 76],
|
2023-01-15 20:34:44 +11:00
|
|
|
}
|
|
|
|
}
|
2023-01-16 12:10:21 +11:00
|
|
|
|
2023-01-15 21:05:28 +11:00
|
|
|
fn get(&self, address: Address) -> u8 {
|
2023-01-15 19:53:15 +11:00
|
|
|
match address {
|
2023-02-06 12:32:10 +11:00
|
|
|
0x0..0x8000 => {
|
2023-01-15 19:53:15 +11:00
|
|
|
// rom access
|
|
|
|
// todo - switchable rom banks
|
2023-01-18 12:46:15 +11:00
|
|
|
if self.bootrom_enabled && (address as usize) < self.bootrom.len() {
|
|
|
|
return self.bootrom[address as usize];
|
2023-01-20 14:58:43 +11:00
|
|
|
} else {
|
2023-01-22 09:33:18 +11:00
|
|
|
return self.rom[address as usize];
|
|
|
|
}
|
2023-01-20 14:58:43 +11:00
|
|
|
}
|
2023-01-15 19:53:15 +11:00
|
|
|
0x8000..0xA000 => {
|
2023-01-15 20:34:44 +11:00
|
|
|
return self.vram[(address - 0x8000) as usize];
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
2023-01-22 09:33:18 +11:00
|
|
|
0xA000..0xC000 => 0xFF,
|
2023-01-15 19:53:15 +11:00
|
|
|
0xC000..0xE000 => {
|
2023-01-15 20:34:44 +11:00
|
|
|
return self.ram[(address - 0xC000) as usize];
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
0xE000..0xFE00 => {
|
2023-01-16 11:34:36 +11:00
|
|
|
return self.ram[(address - 0xE000) as usize];
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
0xFE00..0xFEA0 => {
|
2023-01-17 09:39:05 +11:00
|
|
|
return self.oam[(address - 0xFE00) as usize];
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
0xFEA0..0xFF00 => {
|
2023-01-18 12:46:15 +11:00
|
|
|
return 0x0;
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
0xFF00..0xFF4C => {
|
2023-01-18 12:46:15 +11:00
|
|
|
return self.io[(address - 0xFF00) as usize];
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
0xFF4C..0xFF80 => {
|
2023-01-18 12:46:15 +11:00
|
|
|
// println!("empty space 2 read");
|
2023-01-22 09:07:57 +11:00
|
|
|
return 0xFF;
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
0xFF80..0xFFFF => {
|
2023-01-16 19:28:11 +11:00
|
|
|
return self.cpu_ram[(address - 0xFF80) as usize];
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
0xFFFF => {
|
2023-01-18 13:58:53 +11:00
|
|
|
return self.interrupts;
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 12:10:21 +11:00
|
|
|
|
|
|
|
fn set(&mut self, address: Address, data: u8) {
|
2023-02-05 22:37:49 +11:00
|
|
|
// verbose_println!("write addr: {:#X}, data: {:#X}", address, data);
|
2023-01-22 12:13:02 +11:00
|
|
|
|
2023-01-16 12:10:21 +11:00
|
|
|
match address {
|
2023-02-06 12:32:10 +11:00
|
|
|
0x0..0x8000 => {
|
2023-01-16 16:01:50 +11:00
|
|
|
// change this with MBC code...
|
2023-01-18 12:46:15 +11:00
|
|
|
// println!("tried to write {:#5X} at {:#X}", data, address);
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
|
|
|
0x8000..0xA000 => {
|
|
|
|
self.vram[(address - 0x8000) as usize] = data;
|
|
|
|
}
|
|
|
|
0xA000..0xC000 => {
|
2023-01-22 09:07:57 +11:00
|
|
|
// panic!("switchable write");
|
|
|
|
// self.switchable_ram[(address - 0xA000) as usize] = data;
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
|
|
|
0xC000..0xE000 => {
|
|
|
|
self.ram[(address - 0xC000) as usize] = data;
|
|
|
|
}
|
|
|
|
0xE000..0xFE00 => {
|
|
|
|
self.ram[(address - 0xE000) as usize] = data;
|
|
|
|
}
|
|
|
|
0xFE00..0xFEA0 => {
|
2023-01-17 09:39:05 +11:00
|
|
|
self.oam[(address - 0xFE00) as usize] = data;
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
|
|
|
0xFEA0..0xFF00 => {
|
2023-01-18 12:46:15 +11:00
|
|
|
// println!("empty space write: {:#X} to addr {:#X}", data, address);
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
|
|
|
0xFF00..0xFF4C => {
|
2023-02-05 22:37:49 +11:00
|
|
|
// verbose_print!("writing to addr {:#X}\r", address);
|
2023-01-18 13:58:53 +11:00
|
|
|
stdout().flush().unwrap();
|
|
|
|
|
2023-01-18 12:46:15 +11:00
|
|
|
if address == 0xFF02 && data == 0x81 {
|
|
|
|
print!("{}", self.get(0xFF01) as char);
|
|
|
|
stdout().flush().unwrap();
|
|
|
|
}
|
|
|
|
self.io[(address - 0xFF00) as usize] = data;
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
|
|
|
0xFF4C..0xFF80 => {
|
2023-01-18 12:46:15 +11:00
|
|
|
// println!("empty space 2 write: {:#X} to addr {:#X}", data, address);
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
|
|
|
0xFF80..0xFFFF => {
|
2023-01-16 19:28:11 +11:00
|
|
|
self.cpu_ram[(address - 0xFF80) as usize] = data;
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
2023-01-18 13:58:53 +11:00
|
|
|
0xFFFF => {
|
2023-01-22 12:13:02 +11:00
|
|
|
verbose_println!("interrupts set to {:#b}", data);
|
|
|
|
verbose_println!(" / {:#X}", data);
|
2023-01-18 13:58:53 +11:00
|
|
|
self.interrupts = data;
|
|
|
|
}
|
2023-01-16 12:10:21 +11:00
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn swap_rom_endian(rom: &ROM) -> ROM {
|
|
|
|
rom.chunks(2)
|
|
|
|
.map(|l| {
|
|
|
|
let mut m = l.to_owned();
|
|
|
|
m.reverse();
|
|
|
|
m
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
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-06 12:17:58 +11:00
|
|
|
let mut window = Window::new(
|
2023-02-05 22:56:18 +11:00
|
|
|
"Gameboy",
|
|
|
|
WIDTH * FACTOR,
|
|
|
|
HEIGHT * FACTOR,
|
|
|
|
WindowOptions::default(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
panic!("{}", e);
|
|
|
|
});
|
2023-02-03 09:15:30 +11:00
|
|
|
|
2023-02-06 12:17:58 +11:00
|
|
|
window.set_position(50, 50);
|
|
|
|
|
2023-02-05 18:46:55 +11:00
|
|
|
window.topmost(true);
|
2023-02-03 09:15:30 +11:00
|
|
|
|
2023-01-15 19:53:15 +11:00
|
|
|
let rom: ROM = fs::read(args.rom).expect("Could not load ROM");
|
2023-01-16 14:43:11 +11:00
|
|
|
let bootrom: ROM = fs::read(args.bootrom).expect("Could not load BootROM");
|
|
|
|
|
2023-02-06 12:32:10 +11:00
|
|
|
let mut cpu = CPU::new(Memory::init(bootrom, args.run_bootrom, rom), 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() {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|