gb-emu/src/main.rs

256 lines
6.7 KiB
Rust
Raw Normal View History

#![feature(exclusive_range_pattern)]
2023-01-16 12:13:53 +11:00
mod processor;
use clap::Parser;
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},
};
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
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
/// Step emulation by...
#[arg(long)]
step_by: Option<usize>,
}
2023-01-15 21:05:28 +11:00
type Address = u16;
type ROM = Vec<u8>;
2023-01-16 11:34:36 +11:00
#[derive(Clone, Copy)]
struct Inner {
left: u8,
2023-01-16 19:28:11 +11:00
right: u8,
2023-01-16 11:34:36 +11:00
}
2023-01-16 14:23:06 +11:00
#[derive(Clone, Copy)]
2023-01-16 11:34:36 +11:00
union Register {
as_u8s: Inner,
as_u16: u16,
}
2023-01-16 12:13:53 +11:00
pub struct Memory {
2023-01-18 12:46:15 +11:00
bootrom: ROM,
bootrom_enabled: bool,
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-01-18 12:46:15 +11:00
io: [u8; 76],
}
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-01-18 12:46:15 +11:00
io: [0x0; 76],
2023-01-15 20:34:44 +11:00
}
}
2023-01-15 21:05:28 +11:00
fn get(&self, address: Address) -> u8 {
match address {
0x0..0x8000 => {
// 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];
}
return self.rom[address as usize];
}
0x8000..0xA000 => {
2023-01-15 20:34:44 +11:00
return self.vram[(address - 0x8000) as usize];
}
0xA000..0xC000 => {
2023-01-16 19:28:11 +11:00
return self.switchable_ram[(address - 0xA000) as usize];
}
0xC000..0xE000 => {
2023-01-15 20:34:44 +11:00
return self.ram[(address - 0xC000) as usize];
}
0xE000..0xFE00 => {
2023-01-16 11:34:36 +11:00
return self.ram[(address - 0xE000) as usize];
}
0xFE00..0xFEA0 => {
2023-01-17 09:39:05 +11:00
return self.oam[(address - 0xFE00) as usize];
}
0xFEA0..0xFF00 => {
2023-01-18 12:46:15 +11:00
return 0x0;
}
0xFF00..0xFF4C => {
2023-01-18 12:46:15 +11:00
return self.io[(address - 0xFF00) as usize];
}
0xFF4C..0xFF80 => {
2023-01-18 12:46:15 +11:00
// println!("empty space 2 read");
return 0x0;
}
0xFF80..0xFFFF => {
2023-01-16 19:28:11 +11:00
return self.cpu_ram[(address - 0xFF80) as usize];
}
0xFFFF => {
2023-01-18 13:58:53 +11:00
return self.interrupts;
}
}
}
fn set(&mut self, address: Address, data: u8) {
match address {
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);
}
0x8000..0xA000 => {
self.vram[(address - 0x8000) as usize] = data;
}
0xA000..0xC000 => {
2023-01-16 19:28:11 +11:00
self.switchable_ram[(address - 0xA000) as usize] = data;
}
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;
}
0xFEA0..0xFF00 => {
2023-01-18 12:46:15 +11:00
// println!("empty space write: {:#X} to addr {:#X}", data, address);
}
0xFF00..0xFF4C => {
2023-01-18 13:58:53 +11:00
print!("writing to addr {:#X}\r", address);
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;
}
0xFF4C..0xFF80 => {
2023-01-18 12:46:15 +11:00
// println!("empty space 2 write: {:#X} to addr {:#X}", data, address);
}
0xFF80..0xFFFF => {
2023-01-16 19:28:11 +11:00
self.cpu_ram[(address - 0xFF80) as usize] = data;
}
2023-01-18 13:58:53 +11:00
0xFFFF => {
println!("interrupts set to {:#b}", data);
println!(" / {:#X}", data);
self.interrupts = data;
}
}
}
}
2023-01-16 12:13:53 +11:00
pub struct State {
af: Register,
bc: Register,
de: Register,
hl: Register,
sp: Register,
pc: Register,
}
impl Default for State {
fn default() -> Self {
2023-01-15 20:34:44 +11:00
// default post-bootrom values
Self {
2023-01-16 11:34:36 +11:00
af: Register { as_u16: 0x01B0 },
bc: Register { as_u16: 0x0013 },
de: Register { as_u16: 0x00D8 },
hl: Register { as_u16: 0x014D },
sp: Register { as_u16: 0xFFFE },
pc: Register { as_u16: 0x0100 },
}
}
}
fn main() {
let args = Args::parse();
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");
let mut state = State::default();
2023-01-18 12:46:15 +11:00
if args.run_bootrom {
2023-01-16 14:43:11 +11:00
state.pc = Register { as_u16: 0x0 };
2023-01-18 12:46:15 +11:00
}
2023-01-16 14:43:11 +11:00
2023-01-15 20:34:44 +11:00
let mut cpu = CPU {
2023-01-18 12:46:15 +11:00
memory: Memory::init(bootrom, args.run_bootrom, rom),
2023-01-16 14:43:11 +11:00
state,
2023-01-18 12:46:15 +11:00
last_instruction: 0x0,
last_instruction_addr: 0x0,
2023-01-15 20:34:44 +11:00
};
2023-01-18 12:46:15 +11:00
#[allow(unused_variables)]
let mut cycle_num = 0;
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-17 08:58:37 +11:00
cpu.exec_next();
2023-01-18 12:46:15 +11:00
println!(
"exec {:#4X} from {:#4X}",
cpu.last_instruction, cpu.last_instruction_addr
);
}
print!(
" ...{} cycles - press enter to continue\r",
cycle_num
);
stdout().flush().unwrap();
2023-01-17 08:58:37 +11:00
pause();
},
None => loop {
2023-01-18 12:46:15 +11:00
cycle_num += 1;
2023-01-18 13:58:53 +11:00
// print_cycles(&cycle_num);
2023-01-18 12:46:15 +11:00
cpu.exec_next();
2023-01-17 08:58:37 +11:00
},
}
}
2023-01-15 21:05:28 +11:00
2023-01-16 11:46:00 +11:00
#[allow(dead_code)]
2023-01-16 11:34:36 +11:00
fn pause() {
io::stdin().read_line(&mut String::new()).unwrap();
2023-01-15 21:05:28 +11:00
}
2023-01-18 12:46:15 +11:00
#[allow(dead_code)]
fn print_cycles(cycles: &i32) {
if *cycles % 456 != 0 {
return;
}
let instructions_per_second = 400000;
print!(
"cycle {} - approx {} seconds on real hardware\r",
cycles,
cycles / instructions_per_second
);
stdout().flush().unwrap();
}