2021-09-18 02:29:29 +00:00
|
|
|
use crate::bus::{Bus, BusIo, BOOT_SIZE};
|
2021-08-01 01:29:13 +00:00
|
|
|
use crate::instruction::Instruction;
|
2021-06-07 01:47:11 +00:00
|
|
|
use crate::interrupt::{InterruptEnable, InterruptFlag};
|
2021-09-12 07:55:14 +00:00
|
|
|
use crate::Cycle;
|
2021-03-16 04:35:20 +00:00
|
|
|
use bitfield::bitfield;
|
2021-03-19 02:06:01 +00:00
|
|
|
use std::fmt::{Display, Formatter, Result as FmtResult};
|
2020-09-03 00:35:48 +00:00
|
|
|
|
2021-11-21 09:27:04 +00:00
|
|
|
#[derive(Debug)]
|
2020-08-29 23:38:27 +00:00
|
|
|
pub struct Cpu {
|
2021-10-20 05:48:44 +00:00
|
|
|
pub(crate) bus: Bus,
|
2020-08-29 23:38:27 +00:00
|
|
|
reg: Registers,
|
|
|
|
flags: Flags,
|
2021-04-05 05:52:12 +00:00
|
|
|
ime: ImeState,
|
2020-08-29 23:38:27 +00:00
|
|
|
state: State,
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:41:19 +00:00
|
|
|
impl Cpu {
|
2021-11-21 09:27:04 +00:00
|
|
|
pub(crate) fn new(rom: [u8; BOOT_SIZE]) -> Self {
|
2021-09-18 02:29:29 +00:00
|
|
|
Self {
|
|
|
|
bus: Bus::with_boot(rom),
|
2021-11-21 09:27:04 +00:00
|
|
|
reg: Default::default(),
|
2021-11-25 07:36:02 +00:00
|
|
|
flags: Flags(0),
|
|
|
|
ime: ImeState::Disabled,
|
|
|
|
state: State::Execute,
|
2021-09-18 02:29:29 +00:00
|
|
|
}
|
2021-01-19 07:36:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 04:26:01 +00:00
|
|
|
pub(crate) fn ime(&self) -> ImeState {
|
|
|
|
self.ime
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) fn set_ime(&mut self, state: ImeState) {
|
2021-04-05 05:52:12 +00:00
|
|
|
self.ime = state;
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2020-12-23 09:25:16 +00:00
|
|
|
|
2021-08-16 04:26:01 +00:00
|
|
|
pub(crate) fn halt_cpu(&mut self, kind: HaltKind) {
|
|
|
|
self.state = State::Halt(kind);
|
2021-03-24 04:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 04:26:01 +00:00
|
|
|
fn resume_execution(&mut self) {
|
|
|
|
self.state = State::Execute;
|
2021-03-24 04:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 04:26:01 +00:00
|
|
|
pub(crate) fn is_halted(&self) -> bool {
|
2021-09-20 07:15:05 +00:00
|
|
|
matches!(self.state, State::Halt(_))
|
2021-08-16 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn halt_kind(&self) -> Option<HaltKind> {
|
|
|
|
match self.state {
|
|
|
|
State::Halt(kind) => Some(kind),
|
|
|
|
_ => None,
|
|
|
|
}
|
2021-03-24 04:05:27 +00:00
|
|
|
}
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 09:27:04 +00:00
|
|
|
impl Default for Cpu {
|
|
|
|
fn default() -> Self {
|
|
|
|
Cpu::new(*include_bytes!("../bin/bootix_dmg.bin"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
impl Cpu {
|
2021-08-14 21:42:38 +00:00
|
|
|
/// Fetch an [Instruction] from the memory bus
|
|
|
|
/// (4 cycles)
|
2021-08-14 05:10:51 +00:00
|
|
|
fn fetch(&mut self) -> u8 {
|
|
|
|
let byte = self.read_byte(self.reg.pc);
|
|
|
|
self.bus.clock();
|
2021-08-01 01:29:13 +00:00
|
|
|
self.reg.pc += 1;
|
|
|
|
byte
|
|
|
|
}
|
|
|
|
|
2021-08-14 21:42:38 +00:00
|
|
|
/// Decode a byte into an [SM83](Cpu) [Instruction]
|
|
|
|
///
|
|
|
|
/// If opcode == 0xCB, then decoding costs 4 cycles.
|
|
|
|
/// Otherwise, decoding is free
|
2021-12-09 09:21:05 +00:00
|
|
|
pub(crate) fn decode(&mut self, opcode: u8) -> Instruction {
|
|
|
|
if opcode == 0xCB {
|
|
|
|
Instruction::decode(self.fetch(), true)
|
2021-08-01 01:29:13 +00:00
|
|
|
} else {
|
|
|
|
Instruction::decode(opcode, false)
|
2021-12-09 09:21:05 +00:00
|
|
|
}
|
2021-06-07 01:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 21:42:38 +00:00
|
|
|
/// Execute an [Instruction].
|
|
|
|
///
|
|
|
|
/// The amount of cycles necessary to execute an instruction range from
|
|
|
|
/// 0 to 20 T-cycles
|
2021-06-07 01:47:11 +00:00
|
|
|
fn execute(&mut self, instruction: Instruction) -> Cycle {
|
2021-01-19 02:47:09 +00:00
|
|
|
Instruction::execute(self, instruction)
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-14 22:51:09 +00:00
|
|
|
/// Perform the [`Cpu::fetch()`] [`Cpu::decode()`] [`Cpu::execute()`]
|
2021-08-14 21:42:38 +00:00
|
|
|
/// routine.
|
|
|
|
///
|
2021-08-14 22:51:09 +00:00
|
|
|
/// Handle HALT and interrupts.
|
2021-10-30 01:28:20 +00:00
|
|
|
pub(crate) fn step(&mut self) -> Cycle {
|
2021-08-14 22:23:45 +00:00
|
|
|
// Log instructions
|
2021-08-14 05:10:51 +00:00
|
|
|
// if self.reg.pc > 0xFF {
|
2021-08-01 01:29:13 +00:00
|
|
|
// let out = std::io::stdout();
|
2021-08-14 22:23:45 +00:00
|
|
|
// let _ = self._print_logs(out.lock());
|
2021-08-01 01:29:13 +00:00
|
|
|
// }
|
|
|
|
|
2021-08-14 22:23:45 +00:00
|
|
|
if let Some(elapsed) = self.handle_interrupt() {
|
|
|
|
return elapsed;
|
|
|
|
}
|
2021-03-27 16:56:47 +00:00
|
|
|
|
2021-08-16 04:26:01 +00:00
|
|
|
if let Some(kind) = self.halt_kind() {
|
2021-08-14 22:23:45 +00:00
|
|
|
use HaltKind::*;
|
|
|
|
|
|
|
|
self.bus.clock();
|
2021-10-20 05:48:44 +00:00
|
|
|
return match kind {
|
2021-09-12 07:55:14 +00:00
|
|
|
ImeEnabled | NonePending => 4,
|
2021-08-14 22:23:45 +00:00
|
|
|
SomePending => todo!("Implement HALT bug"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let opcode = self.fetch();
|
|
|
|
let instr = self.decode(opcode);
|
|
|
|
let elapsed = self.execute(instr);
|
|
|
|
self.handle_ei();
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
// For use in Blargg's Test ROMs
|
2021-10-20 05:48:44 +00:00
|
|
|
// if self.read_byte(0xFF02) == 0x81 {
|
|
|
|
// let c = self.read_byte(0xFF01) as char;
|
|
|
|
// self.write_byte(0xFF02, 0x00);
|
|
|
|
// eprint!("{}", c);
|
|
|
|
// }
|
2021-03-19 02:06:01 +00:00
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
elapsed
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 01:47:11 +00:00
|
|
|
impl BusIo for Cpu {
|
|
|
|
fn read_byte(&self, addr: u16) -> u8 {
|
|
|
|
self.bus.read_byte(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_byte(&mut self, addr: u16, byte: u8) {
|
|
|
|
self.bus.write_byte(addr, byte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
impl Cpu {
|
2021-08-14 21:42:15 +00:00
|
|
|
fn handle_ei(&mut self) {
|
2021-04-05 06:10:03 +00:00
|
|
|
match self.ime {
|
2021-08-14 21:42:15 +00:00
|
|
|
ImeState::EiExecuted => self.ime = ImeState::Pending,
|
|
|
|
ImeState::Pending => self.ime = ImeState::Enabled,
|
|
|
|
ImeState::Disabled | ImeState::Enabled => {}
|
2021-04-05 06:10:03 +00:00
|
|
|
}
|
2021-04-05 05:52:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
pub(crate) fn int_request(&self) -> u8 {
|
|
|
|
self.read_byte(0xFF0F)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn int_enable(&self) -> u8 {
|
|
|
|
self.read_byte(0xFFFF)
|
|
|
|
}
|
|
|
|
|
2021-08-14 22:23:45 +00:00
|
|
|
fn handle_interrupt(&mut self) -> Option<Cycle> {
|
|
|
|
let irq = self.int_request();
|
|
|
|
let enable = self.int_enable();
|
2021-03-19 02:06:01 +00:00
|
|
|
|
2021-08-14 22:23:45 +00:00
|
|
|
// TODO: Ensure that this behaviour is correct
|
2021-08-16 04:26:01 +00:00
|
|
|
if self.is_halted() {
|
2021-03-24 04:05:27 +00:00
|
|
|
// When we're here either a HALT with IME set or
|
|
|
|
// a HALT with IME not set and No pending Interrupts was called
|
|
|
|
|
2021-08-14 22:23:45 +00:00
|
|
|
if irq & enable != 0 {
|
2021-03-24 04:05:27 +00:00
|
|
|
// The if self.ime() below correctly follows the "resuming from HALT" behaviour so
|
|
|
|
// nothing actually needs to be added here. This is just documentation
|
|
|
|
// since it's a bit weird why nothing is being done
|
|
|
|
|
2021-08-16 04:26:01 +00:00
|
|
|
self.resume_execution();
|
2021-03-24 04:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 22:23:45 +00:00
|
|
|
match self.ime() {
|
|
|
|
ImeState::Enabled => {
|
|
|
|
let mut irq: InterruptFlag = irq.into();
|
|
|
|
let enable: InterruptEnable = enable.into();
|
|
|
|
|
|
|
|
let rst_vector = if irq.vblank() && enable.vblank() {
|
|
|
|
// Handle VBlank Interrupt
|
|
|
|
irq.set_vblank(false);
|
|
|
|
|
|
|
|
// INT 40h
|
|
|
|
Some(0x40)
|
|
|
|
} else if irq.lcd_stat() && enable.lcd_stat() {
|
|
|
|
// Handle LCD STAT Interrupt
|
|
|
|
irq.set_lcd_stat(false);
|
|
|
|
|
|
|
|
// INT 48h
|
|
|
|
Some(0x48)
|
|
|
|
} else if irq.timer() && enable.timer() {
|
|
|
|
// Handle Timer Interrupt
|
|
|
|
irq.set_timer(false);
|
|
|
|
|
|
|
|
// INT 50h
|
|
|
|
Some(0x50)
|
|
|
|
} else if irq.serial() && enable.serial() {
|
|
|
|
// Handle Serial Interrupt
|
|
|
|
irq.set_serial(false);
|
|
|
|
|
|
|
|
// INT 58h
|
|
|
|
Some(0x58)
|
|
|
|
} else if irq.joypad() && enable.joypad() {
|
|
|
|
// Handle Joypad Interrupt
|
|
|
|
irq.set_joypad(false);
|
|
|
|
|
|
|
|
// INT 60h
|
|
|
|
Some(0x60)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
match rst_vector {
|
|
|
|
Some(vector) => {
|
|
|
|
// Write the Changes to 0xFF0F and 0xFFFF registers
|
|
|
|
self.write_byte(0xFF0F, irq.into());
|
|
|
|
|
|
|
|
// Disable all future interrupts
|
|
|
|
self.set_ime(ImeState::Disabled);
|
|
|
|
Some(Instruction::reset(self, vector))
|
|
|
|
}
|
|
|
|
None => None,
|
2021-03-19 02:06:01 +00:00
|
|
|
}
|
2021-08-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
_ => None,
|
2021-03-19 02:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 02:47:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 04:24:10 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2020-08-29 23:38:27 +00:00
|
|
|
enum State {
|
|
|
|
Execute,
|
2021-08-16 04:26:01 +00:00
|
|
|
Halt(HaltKind),
|
2020-12-23 09:25:16 +00:00
|
|
|
// Stop,
|
|
|
|
}
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
impl Cpu {
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) 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,
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) 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,
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) 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
|
|
|
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) fn flags(&self) -> &Flags {
|
2021-03-16 04:35:20 +00:00
|
|
|
&self.flags
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn update_flags(&mut self, z: bool, n: bool, h: bool, c: bool) {
|
|
|
|
self.flags.set_z(z);
|
|
|
|
self.flags.set_n(n);
|
|
|
|
self.flags.set_h(h);
|
|
|
|
self.flags.set_c(c);
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) fn set_flags(&mut self, flags: Flags) {
|
2021-03-16 04:35:20 +00:00
|
|
|
self.flags = flags;
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:16:23 +00:00
|
|
|
impl Cpu {
|
2021-08-14 05:10:51 +00:00
|
|
|
fn _print_debug(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
|
|
|
|
write!(w, "A: {:02X} ", self.reg.a)?;
|
|
|
|
write!(w, "F: {:02X} ", u8::from(self.flags))?;
|
|
|
|
write!(w, "B: {:02X} ", self.reg.b)?;
|
|
|
|
write!(w, "C: {:02X} ", self.reg.c)?;
|
|
|
|
write!(w, "D: {:02X} ", self.reg.d)?;
|
|
|
|
write!(w, "E: {:02X} ", self.reg.e)?;
|
|
|
|
write!(w, "H: {:02X} ", self.reg.h)?;
|
|
|
|
write!(w, "L: {:02X} ", self.reg.l)?;
|
|
|
|
write!(w, "SP: {:04X} ", self.reg.sp)?;
|
|
|
|
write!(w, "PC: 00:{:04X} ", self.reg.pc)?;
|
|
|
|
write!(w, "({:02X} ", self.read_byte(self.reg.pc))?;
|
|
|
|
write!(w, "{:02X} ", self.read_byte(self.reg.pc + 1))?;
|
|
|
|
write!(w, "{:02X} ", self.read_byte(self.reg.pc + 2))?;
|
|
|
|
write!(w, "{:02X})", self.read_byte(self.reg.pc + 3))?;
|
|
|
|
writeln!(w, "| {:?}", self._dbg_instr())?;
|
|
|
|
w.flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _print_logs(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
|
2021-07-13 02:08:38 +00:00
|
|
|
write!(w, "A: {:02X} ", self.reg.a)?;
|
2021-08-14 05:10:51 +00:00
|
|
|
write!(w, "F: {:02X} ", u8::from(self.flags))?;
|
2021-07-13 02:08:38 +00:00
|
|
|
write!(w, "B: {:02X} ", self.reg.b)?;
|
|
|
|
write!(w, "C: {:02X} ", self.reg.c)?;
|
|
|
|
write!(w, "D: {:02X} ", self.reg.d)?;
|
|
|
|
write!(w, "E: {:02X} ", self.reg.e)?;
|
|
|
|
write!(w, "H: {:02X} ", self.reg.h)?;
|
|
|
|
write!(w, "L: {:02X} ", self.reg.l)?;
|
|
|
|
write!(w, "SP: {:04X} ", self.reg.sp)?;
|
2021-08-14 05:10:51 +00:00
|
|
|
write!(w, "PC: 00:{:04X} ", self.reg.pc)?;
|
|
|
|
write!(w, "({:02X} ", self.read_byte(self.reg.pc))?;
|
|
|
|
write!(w, "{:02X} ", self.read_byte(self.reg.pc + 1))?;
|
|
|
|
write!(w, "{:02X} ", self.read_byte(self.reg.pc + 2))?;
|
|
|
|
writeln!(w, "{:02X})", self.read_byte(self.reg.pc + 3))?;
|
2021-07-13 02:08:38 +00:00
|
|
|
w.flush()
|
|
|
|
}
|
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
fn _dbg_instr(&self) -> Instruction {
|
2021-12-09 09:21:05 +00:00
|
|
|
let byte = self.read_byte(self.reg.pc);
|
|
|
|
if byte == 0xCB {
|
|
|
|
Instruction::decode(self.read_byte(self.reg.pc + 1), true)
|
2021-08-14 05:10:51 +00:00
|
|
|
} else {
|
|
|
|
Instruction::decode(byte, false)
|
2021-12-09 09:21:05 +00:00
|
|
|
}
|
2021-03-22 02:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 04:24:10 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) enum Register {
|
2020-08-29 23:38:27 +00:00
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
D,
|
|
|
|
E,
|
|
|
|
H,
|
|
|
|
L,
|
|
|
|
}
|
|
|
|
|
2021-07-28 04:24:10 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) enum RegisterPair {
|
2020-08-29 23:38:27 +00:00
|
|
|
AF,
|
|
|
|
BC,
|
|
|
|
DE,
|
|
|
|
HL,
|
|
|
|
SP,
|
|
|
|
PC,
|
|
|
|
}
|
|
|
|
|
2021-07-28 04:24:10 +00:00
|
|
|
#[derive(Debug, 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 {
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-03-27 01:25:30 +00:00
|
|
|
flags.0 & 0xF0
|
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 {
|
2021-03-27 01:25:30 +00:00
|
|
|
Self(byte & 0xF0)
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-24 04:05:27 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-08-14 22:23:45 +00:00
|
|
|
pub(crate) enum HaltKind {
|
2021-04-05 05:53:46 +00:00
|
|
|
ImeEnabled,
|
2021-03-24 04:05:27 +00:00
|
|
|
NonePending,
|
|
|
|
SomePending,
|
|
|
|
}
|
2021-04-05 05:52:12 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) enum ImeState {
|
2021-04-05 05:52:12 +00:00
|
|
|
Disabled,
|
2021-08-14 21:42:15 +00:00
|
|
|
EiExecuted,
|
2021-04-05 06:10:03 +00:00
|
|
|
Pending,
|
2021-04-05 05:52:12 +00:00
|
|
|
Enabled,
|
|
|
|
}
|
2021-11-30 14:23:06 +00:00
|
|
|
|
|
|
|
pub(crate) mod dbg {
|
|
|
|
use super::{Cpu, ImeState, RegisterPair};
|
|
|
|
|
|
|
|
pub(crate) fn flags(cpu: &Cpu) -> u8 {
|
|
|
|
cpu.flags.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn af(cpu: &Cpu) -> u16 {
|
|
|
|
cpu.register_pair(RegisterPair::AF)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn bc(cpu: &Cpu) -> u16 {
|
|
|
|
cpu.register_pair(RegisterPair::BC)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn de(cpu: &Cpu) -> u16 {
|
|
|
|
cpu.register_pair(RegisterPair::DE)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn hl(cpu: &Cpu) -> u16 {
|
|
|
|
cpu.register_pair(RegisterPair::HL)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sp(cpu: &Cpu) -> u16 {
|
|
|
|
cpu.register_pair(RegisterPair::SP)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn pc(cpu: &Cpu) -> u16 {
|
|
|
|
cpu.register_pair(RegisterPair::PC)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn ime(cpu: &Cpu) -> bool {
|
2021-12-06 17:33:22 +00:00
|
|
|
matches!(cpu.ime, ImeState::Enabled)
|
2021-11-30 14:23:06 +00:00
|
|
|
}
|
|
|
|
}
|