2020-08-29 23:38:27 +00:00
|
|
|
use super::bus::Bus;
|
2021-01-19 02:47:09 +00:00
|
|
|
use super::instruction::{Cycles, Instruction};
|
2021-03-16 06:05:13 +00:00
|
|
|
use super::ppu::Ppu;
|
2021-03-16 04:35:20 +00:00
|
|
|
use bitfield::bitfield;
|
2021-01-20 07:39:24 +00:00
|
|
|
use std::{
|
|
|
|
fmt::{Display, Formatter, Result as FmtResult},
|
|
|
|
io::Write,
|
|
|
|
};
|
2020-09-03 00:35:48 +00:00
|
|
|
|
2020-12-24 01:39:37 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-08-29 23:38:27 +00:00
|
|
|
pub struct Cpu {
|
2021-01-19 02:47:09 +00:00
|
|
|
pub bus: Bus,
|
2020-08-29 23:38:27 +00:00
|
|
|
reg: Registers,
|
|
|
|
flags: Flags,
|
|
|
|
ime: bool,
|
|
|
|
state: State,
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:41:19 +00:00
|
|
|
impl Cpu {
|
2020-12-23 09:25:16 +00:00
|
|
|
pub fn new() -> Self {
|
2020-12-24 01:39:37 +00:00
|
|
|
Self {
|
|
|
|
reg: Registers {
|
|
|
|
a: 0x01,
|
|
|
|
b: 0x00,
|
|
|
|
c: 0x13,
|
|
|
|
d: 0x00,
|
|
|
|
e: 0xD8,
|
|
|
|
h: 0x01,
|
|
|
|
l: 0x4D,
|
|
|
|
sp: 0xFFFE,
|
|
|
|
pc: 0x0100,
|
|
|
|
},
|
|
|
|
flags: 0xb0.into(),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 04:07:31 +00:00
|
|
|
pub fn boot_new(path: &str) -> Self {
|
2021-01-19 07:36:44 +00:00
|
|
|
Self {
|
2021-01-28 04:07:31 +00:00
|
|
|
bus: Bus::with_boot(path),
|
2021-01-19 07:36:44 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:41:19 +00:00
|
|
|
pub fn ime(&self) -> bool {
|
|
|
|
self.ime
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_ime(&mut self, enabled: bool) {
|
|
|
|
self.ime = enabled;
|
|
|
|
}
|
2020-12-23 09:25:16 +00:00
|
|
|
|
|
|
|
pub fn inc_pc(&mut self) {
|
|
|
|
self.reg.pc += 1;
|
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
|
|
|
|
pub fn load_cartridge(&mut self, path: &str) {
|
|
|
|
self.bus.load_cartridge(path);
|
|
|
|
}
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
impl Cpu {
|
2020-12-23 09:25:16 +00:00
|
|
|
pub fn fetch(&mut self) -> u8 {
|
|
|
|
let opcode = self.bus.read_byte(self.reg.pc);
|
|
|
|
self.inc_pc();
|
|
|
|
|
|
|
|
opcode
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:25:16 +00:00
|
|
|
pub fn decode(&mut self, opcode: u8) -> Instruction {
|
2021-01-19 02:47:09 +00:00
|
|
|
Instruction::from_byte(self, opcode)
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
pub fn execute(&mut self, instruction: Instruction) -> Cycles {
|
|
|
|
Instruction::execute(self, instruction)
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
|
|
|
|
pub fn step(&mut self) -> Cycles {
|
|
|
|
let opcode = self.fetch();
|
|
|
|
let instr = self.decode(opcode);
|
|
|
|
|
2021-01-20 07:39:24 +00:00
|
|
|
// println!(
|
|
|
|
// "Addr: {:#06X} | Opcode: {:#04X} | Instr: {:X?}",
|
|
|
|
// self.reg.pc, opcode, instr
|
|
|
|
// );
|
2021-01-19 08:05:04 +00:00
|
|
|
|
|
|
|
let cycles = self.execute(instr);
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-01-20 07:39:24 +00:00
|
|
|
if self.bus.read_byte(0xFF02) == 0x81 {
|
|
|
|
let c = self.bus.read_byte(0xFF01) as char;
|
|
|
|
self.bus.write_byte(0xFF02, 0x00);
|
|
|
|
|
|
|
|
print!("{}", c);
|
|
|
|
std::io::stdout().flush().unwrap();
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:54:38 +00:00
|
|
|
self.bus.step(cycles);
|
|
|
|
|
|
|
|
cycles
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 03:13:59 +00:00
|
|
|
impl Cpu {
|
2021-01-18 04:12:00 +00:00
|
|
|
pub fn read_imm_byte(&mut self, addr: u16) -> u8 {
|
|
|
|
self.inc_pc();
|
2021-01-18 03:13:59 +00:00
|
|
|
self.bus.read_byte(addr)
|
|
|
|
}
|
|
|
|
|
2021-01-18 04:12:00 +00:00
|
|
|
pub fn read_imm_word(&mut self, addr: u16) -> u16 {
|
|
|
|
self.inc_pc();
|
|
|
|
self.inc_pc();
|
2021-01-18 03:13:59 +00:00
|
|
|
self.bus.read_word(addr)
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:25:16 +00:00
|
|
|
pub fn read_byte(&mut self, addr: u16) -> u8 {
|
2021-01-18 04:12:00 +00:00
|
|
|
self.bus.read_byte(addr)
|
2020-08-30 04:07:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 04:23:09 +00:00
|
|
|
pub fn write_byte(&mut self, addr: u16, byte: u8) {
|
2020-12-24 03:24:27 +00:00
|
|
|
self.bus.write_byte(addr, byte);
|
2020-08-30 04:07:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:25:16 +00:00
|
|
|
pub fn read_word(&mut self, addr: u16) -> u16 {
|
2021-01-18 04:12:00 +00:00
|
|
|
self.bus.read_word(addr)
|
2020-08-30 04:07:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 04:23:09 +00:00
|
|
|
pub fn write_word(&mut self, addr: u16, word: u16) {
|
|
|
|
self.bus.write_word(addr, word)
|
2020-08-30 04:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
impl Cpu {
|
2021-03-16 06:05:13 +00:00
|
|
|
pub fn get_ppu(&mut self) -> &mut Ppu {
|
2021-01-19 02:47:09 +00:00
|
|
|
&mut self.bus.ppu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 00:35:48 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2020-08-29 23:38:27 +00:00
|
|
|
enum State {
|
|
|
|
Execute,
|
2020-12-23 09:25:16 +00:00
|
|
|
// Halt,
|
|
|
|
// Stop,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Execute
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Cpu {
|
|
|
|
pub fn set_register(&mut self, register: Register, value: u8) {
|
2021-03-16 00:19:40 +00:00
|
|
|
use Register::*;
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
match register {
|
2021-03-16 00:19:40 +00:00
|
|
|
A => self.reg.a = value,
|
|
|
|
B => self.reg.b = value,
|
|
|
|
C => self.reg.c = value,
|
|
|
|
D => self.reg.d = value,
|
|
|
|
E => self.reg.e = value,
|
|
|
|
H => self.reg.h = value,
|
|
|
|
L => self.reg.l = value,
|
|
|
|
Flag => self.flags = value.into(),
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register(&self, register: Register) -> u8 {
|
2021-03-16 00:19:40 +00:00
|
|
|
use Register::*;
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
match register {
|
2021-03-16 00:19:40 +00:00
|
|
|
A => self.reg.a,
|
|
|
|
B => self.reg.b,
|
|
|
|
C => self.reg.c,
|
|
|
|
D => self.reg.d,
|
|
|
|
E => self.reg.e,
|
|
|
|
H => self.reg.h,
|
|
|
|
L => self.reg.l,
|
|
|
|
Flag => self.flags.into(),
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_pair(&self, pair: RegisterPair) -> u16 {
|
2021-03-16 00:19:40 +00:00
|
|
|
use RegisterPair::*;
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
match pair {
|
2021-03-16 00:19:40 +00:00
|
|
|
AF => (self.reg.a as u16) << 8 | u8::from(self.flags) as u16,
|
|
|
|
BC => (self.reg.b as u16) << 8 | self.reg.c as u16,
|
|
|
|
DE => (self.reg.d as u16) << 8 | self.reg.e as u16,
|
|
|
|
HL => (self.reg.h as u16) << 8 | self.reg.l as u16,
|
|
|
|
SP => self.reg.sp,
|
|
|
|
PC => self.reg.pc,
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_register_pair(&mut self, pair: RegisterPair, value: u16) {
|
2021-03-16 00:19:40 +00:00
|
|
|
use RegisterPair::*;
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
let high = (value >> 8) as u8;
|
|
|
|
let low = value as u8;
|
2020-09-01 05:16:05 +00:00
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
match pair {
|
2021-03-16 00:19:40 +00:00
|
|
|
AF => {
|
2020-08-29 23:38:27 +00:00
|
|
|
self.reg.a = high;
|
|
|
|
self.flags = low.into();
|
|
|
|
}
|
2021-03-16 00:19:40 +00:00
|
|
|
BC => {
|
2020-08-29 23:38:27 +00:00
|
|
|
self.reg.b = high;
|
|
|
|
self.reg.c = low;
|
|
|
|
}
|
2021-03-16 00:19:40 +00:00
|
|
|
DE => {
|
2020-08-29 23:38:27 +00:00
|
|
|
self.reg.d = high;
|
|
|
|
self.reg.e = low;
|
|
|
|
}
|
2021-03-16 00:19:40 +00:00
|
|
|
HL => {
|
2020-08-29 23:38:27 +00:00
|
|
|
self.reg.h = high;
|
|
|
|
self.reg.l = low;
|
|
|
|
}
|
2021-03-16 00:19:40 +00:00
|
|
|
SP => self.reg.sp = value,
|
|
|
|
PC => self.reg.pc = value,
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 04:35:20 +00:00
|
|
|
|
|
|
|
pub fn flags(&self) -> &Flags {
|
|
|
|
&self.flags
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_flags(&mut self, flags: Flags) {
|
|
|
|
self.flags = flags;
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 00:35:48 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2020-08-29 23:38:27 +00:00
|
|
|
pub enum Register {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
D,
|
|
|
|
E,
|
|
|
|
H,
|
|
|
|
L,
|
2020-09-02 22:26:46 +00:00
|
|
|
Flag,
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 00:35:48 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2020-08-29 23:38:27 +00:00
|
|
|
pub enum RegisterPair {
|
|
|
|
AF,
|
|
|
|
BC,
|
|
|
|
DE,
|
|
|
|
HL,
|
|
|
|
SP,
|
|
|
|
PC,
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:25:16 +00:00
|
|
|
#[derive(Debug, Copy, Clone, Default)]
|
2020-08-29 23:38:27 +00:00
|
|
|
struct Registers {
|
|
|
|
a: u8,
|
|
|
|
b: u8,
|
|
|
|
c: u8,
|
|
|
|
d: u8,
|
|
|
|
e: u8,
|
|
|
|
h: u8,
|
|
|
|
l: u8,
|
|
|
|
sp: u16,
|
|
|
|
pc: u16,
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
bitfield! {
|
|
|
|
pub struct Flags(u8);
|
|
|
|
impl Debug;
|
|
|
|
pub z, set_z: 7; // Zero Flag
|
|
|
|
pub n, set_n: 6; // Subtraction Flag
|
|
|
|
pub h, set_h: 5; // Half Carry Flag
|
|
|
|
pub c, set_c: 4; // Carry Flag
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 04:23:09 +00:00
|
|
|
impl Flags {
|
|
|
|
pub fn update(&mut self, z: bool, n: bool, h: bool, c: bool) {
|
2021-03-16 04:35:20 +00:00
|
|
|
self.set_z(z);
|
|
|
|
self.set_n(n);
|
|
|
|
self.set_h(h);
|
|
|
|
self.set_c(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Copy for Flags {}
|
|
|
|
impl Clone for Flags {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Flags {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(0)
|
2020-12-23 04:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 03:13:59 +00:00
|
|
|
impl Display for Flags {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
2021-03-16 04:35:20 +00:00
|
|
|
if self.z() {
|
2021-01-18 03:13:59 +00:00
|
|
|
f.write_str("Z")?;
|
|
|
|
} else {
|
|
|
|
f.write_str("_")?;
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
if self.n() {
|
2021-01-18 03:13:59 +00:00
|
|
|
f.write_str("N")?;
|
|
|
|
} else {
|
|
|
|
f.write_str("_")?;
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
if self.h() {
|
2021-01-18 03:13:59 +00:00
|
|
|
f.write_str("H")?;
|
|
|
|
} else {
|
|
|
|
f.write_str("_")?;
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
if self.c() {
|
2021-01-18 03:13:59 +00:00
|
|
|
f.write_str("C")
|
|
|
|
} else {
|
|
|
|
f.write_str("_")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
impl From<Flags> for u8 {
|
|
|
|
fn from(flags: Flags) -> Self {
|
|
|
|
flags.0
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
impl From<u8> for Flags {
|
|
|
|
fn from(byte: u8) -> Self {
|
|
|
|
Self(byte)
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|