2023-01-15 19:53:15 +11:00
|
|
|
#![feature(exclusive_range_pattern)]
|
|
|
|
|
|
|
|
use clap::Parser;
|
2023-01-16 11:35:07 +11:00
|
|
|
use std::{fs, io};
|
2023-01-15 19:53:15 +11:00
|
|
|
|
|
|
|
/// 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-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-16 11:34:36 +11:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Inner {
|
|
|
|
left: u8,
|
|
|
|
right: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
union Register {
|
|
|
|
as_u8s: Inner,
|
|
|
|
as_u16: u16,
|
|
|
|
}
|
|
|
|
|
2023-01-15 19:53:15 +11:00
|
|
|
struct Memory {
|
|
|
|
rom: ROM,
|
2023-01-15 20:34:44 +11:00
|
|
|
vram: [u8; 8192],
|
|
|
|
ram: [u8; 8192],
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Memory {
|
2023-01-15 20:34:44 +11:00
|
|
|
fn init(rom: ROM) -> Self {
|
|
|
|
Self {
|
|
|
|
rom,
|
|
|
|
vram: [0x0; 8192],
|
|
|
|
ram: [0x0; 8192],
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 21:05:28 +11:00
|
|
|
fn get(&self, address: Address) -> u8 {
|
2023-01-15 19:53:15 +11:00
|
|
|
match address {
|
|
|
|
0x0..0x8000 => {
|
|
|
|
// rom access
|
|
|
|
// todo - switchable rom banks
|
|
|
|
return self.rom[address as usize];
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
0xA000..0xC000 => {
|
|
|
|
panic!("switchable ram bank");
|
|
|
|
}
|
|
|
|
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 => {
|
|
|
|
panic!("sprite attrib memory");
|
|
|
|
}
|
|
|
|
0xFEA0..0xFF00 => {
|
|
|
|
panic!("empty")
|
|
|
|
}
|
|
|
|
0xFF00..0xFF4C => {
|
|
|
|
panic!("I/O");
|
|
|
|
}
|
|
|
|
0xFF4C..0xFF80 => {
|
|
|
|
panic!("empty");
|
|
|
|
}
|
|
|
|
0xFF80..0xFFFF => {
|
|
|
|
panic!("internal ram");
|
|
|
|
}
|
|
|
|
0xFFFF => {
|
|
|
|
panic!("interrupt enable register");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-01-15 19:53:15 +11:00
|
|
|
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 },
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 20:34:44 +11:00
|
|
|
struct CPU {
|
|
|
|
memory: Memory,
|
|
|
|
state: State,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CPU {
|
2023-01-16 11:34:36 +11:00
|
|
|
fn exec_next(&mut self) -> u8 {
|
2023-01-15 21:05:28 +11:00
|
|
|
let opcode = self.next_opcode();
|
2023-01-16 11:34:36 +11:00
|
|
|
println!("opcode: {:#X}", opcode);
|
|
|
|
|
2023-01-15 20:34:44 +11:00
|
|
|
match opcode {
|
2023-01-15 21:05:28 +11:00
|
|
|
0x0 => {
|
2023-01-15 20:34:44 +11:00
|
|
|
// noop
|
|
|
|
}
|
2023-01-15 21:05:28 +11:00
|
|
|
0x01 => {
|
2023-01-16 11:34:36 +11:00
|
|
|
self.state.bc.as_u8s.left = self.next_opcode();
|
|
|
|
self.state.bc.as_u8s.right = self.next_opcode();
|
|
|
|
}
|
|
|
|
0x11 => {
|
|
|
|
self.state.de.as_u8s.left = self.next_opcode();
|
|
|
|
self.state.de.as_u8s.right = self.next_opcode();
|
|
|
|
}
|
|
|
|
0x21 => {
|
|
|
|
self.state.de.as_u8s.left = self.next_opcode();
|
|
|
|
self.state.de.as_u8s.right = self.next_opcode();
|
|
|
|
}
|
|
|
|
0x2C => {
|
|
|
|
unsafe {
|
|
|
|
self.state.hl.as_u8s.right += 1;
|
|
|
|
};
|
2023-01-15 21:05:28 +11:00
|
|
|
}
|
|
|
|
0x66 => {
|
2023-01-16 11:34:36 +11:00
|
|
|
unsafe {
|
|
|
|
self.state.hl.as_u8s.left = self.memory.get(self.state.hl.as_u16);
|
|
|
|
};
|
2023-01-15 21:05:28 +11:00
|
|
|
}
|
|
|
|
0xC3 => {
|
2023-01-16 11:34:36 +11:00
|
|
|
let (a, b) = (self.next_opcode(), self.next_opcode());
|
|
|
|
self.state.pc.as_u8s.left = a;
|
|
|
|
self.state.pc.as_u8s.right = b;
|
2023-01-15 21:05:28 +11:00
|
|
|
}
|
2023-01-15 20:34:44 +11:00
|
|
|
_ => {
|
|
|
|
panic!("unimplemented opcode: {:#X}", opcode);
|
|
|
|
}
|
|
|
|
};
|
2023-01-16 11:34:36 +11:00
|
|
|
return opcode;
|
2023-01-15 20:34:44 +11:00
|
|
|
}
|
2023-01-15 21:05:28 +11:00
|
|
|
fn next_opcode(&mut self) -> u8 {
|
2023-01-16 11:34:36 +11:00
|
|
|
unsafe {
|
|
|
|
let opcode = self.memory.get(self.state.pc.as_u16);
|
|
|
|
self.state.pc.as_u16 += 0x1;
|
|
|
|
return opcode;
|
|
|
|
};
|
2023-01-15 21:05:28 +11:00
|
|
|
}
|
2023-01-15 20:34:44 +11:00
|
|
|
}
|
|
|
|
|
2023-01-15 19:53:15 +11:00
|
|
|
fn main() {
|
|
|
|
let args = Args::parse();
|
|
|
|
|
|
|
|
let rom: ROM = fs::read(args.rom).expect("Could not load ROM");
|
2023-01-16 11:35:07 +11:00
|
|
|
let _bootrom: ROM = fs::read(args.bootrom).expect("Could not load BootROM");
|
2023-01-15 20:34:44 +11:00
|
|
|
let mut cpu = CPU {
|
|
|
|
memory: Memory::init(rom),
|
|
|
|
state: State::default(),
|
|
|
|
};
|
2023-01-15 19:53:15 +11:00
|
|
|
loop {
|
2023-01-15 21:05:28 +11:00
|
|
|
cpu.exec_next();
|
2023-01-16 11:34:36 +11:00
|
|
|
pause();
|
2023-01-15 19:53:15 +11:00
|
|
|
}
|
|
|
|
}
|
2023-01-15 21:05:28 +11:00
|
|
|
|
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
|
|
|
}
|