2021-08-01 01:29:13 +00:00
|
|
|
use self::add::{Source as AddSource, Target as AddTarget};
|
|
|
|
use self::alu::Source as AluSource;
|
|
|
|
use self::cycle::Cycle;
|
|
|
|
use self::jump::{JumpCondition, JumpLocation};
|
|
|
|
use self::load::{Source as LDSource, Target as LDTarget};
|
|
|
|
use self::table::{
|
|
|
|
alu_imm_instr, alu_reg_instr, flag_instr, group1, group2, group3, jump_cond, prefix_alu,
|
|
|
|
register,
|
|
|
|
};
|
|
|
|
use self::table::{Group1RegisterPair, Group2RegisterPair, Group3RegisterPair, Register};
|
2021-08-14 05:10:51 +00:00
|
|
|
use crate::bus::{Bus, BusIo};
|
2021-08-01 01:29:13 +00:00
|
|
|
use crate::cpu::{Cpu, Flags, HaltState, ImeState, Register as CpuRegister, RegisterPair};
|
2020-12-23 07:17:13 +00:00
|
|
|
|
2021-03-16 06:05:13 +00:00
|
|
|
#[allow(clippy::upper_case_acronyms)]
|
2021-08-06 01:04:39 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) enum Instruction {
|
2020-08-30 04:07:53 +00:00
|
|
|
NOP,
|
|
|
|
STOP,
|
2021-08-01 01:29:13 +00:00
|
|
|
JR(JumpCondition),
|
|
|
|
LD(LDTarget, LDSource),
|
|
|
|
ADD(AddTarget, AddSource),
|
|
|
|
LDHL,
|
|
|
|
INC(AllRegisters),
|
|
|
|
DEC(AllRegisters),
|
2020-08-30 04:07:53 +00:00
|
|
|
RLCA,
|
|
|
|
RRCA,
|
|
|
|
RLA,
|
|
|
|
RRA,
|
|
|
|
DAA,
|
|
|
|
CPL,
|
|
|
|
SCF,
|
|
|
|
CCF,
|
|
|
|
HALT,
|
2021-08-01 01:29:13 +00:00
|
|
|
ADC(AluSource),
|
|
|
|
SUB(AluSource),
|
|
|
|
SBC(AluSource),
|
|
|
|
AND(AluSource),
|
|
|
|
XOR(AluSource),
|
|
|
|
OR(AluSource),
|
|
|
|
CP(AluSource),
|
2020-08-30 04:07:53 +00:00
|
|
|
RET(JumpCondition),
|
2021-08-01 01:29:13 +00:00
|
|
|
POP(Group3RegisterPair),
|
2020-08-30 04:07:53 +00:00
|
|
|
RETI,
|
2021-08-01 01:29:13 +00:00
|
|
|
JP(JumpCondition, JumpLocation),
|
2020-08-30 04:07:53 +00:00
|
|
|
DI,
|
|
|
|
EI,
|
2021-08-01 01:29:13 +00:00
|
|
|
CALL(JumpCondition),
|
|
|
|
PUSH(Group3RegisterPair),
|
2020-08-30 04:07:53 +00:00
|
|
|
RST(u8),
|
2021-08-01 01:29:13 +00:00
|
|
|
RLC(Register),
|
|
|
|
RRC(Register),
|
|
|
|
RL(Register),
|
|
|
|
RR(Register),
|
|
|
|
SLA(Register),
|
|
|
|
SRA(Register),
|
|
|
|
SWAP(Register),
|
|
|
|
SRL(Register),
|
|
|
|
BIT(u8, Register),
|
|
|
|
RES(u8, Register),
|
|
|
|
SET(u8, Register),
|
2020-12-23 07:17:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::fmt::Debug for Instruction {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Instruction::*;
|
2020-12-23 07:23:38 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match self {
|
|
|
|
NOP => f.write_str("NOP"),
|
|
|
|
STOP => f.write_str("STOP"),
|
|
|
|
JR(c) => write!(f, "JR {:?} i8", c),
|
|
|
|
LD(t, s) => write!(f, "LD {:?}, {:?}", t, s),
|
|
|
|
ADD(t, s) => write!(f, "ADD {:?}, {:?}", t, s),
|
|
|
|
LDHL => f.write_str("LD HL, SP + i8"),
|
|
|
|
INC(rs) => write!(f, "INC {:?}", rs),
|
|
|
|
DEC(rs) => write!(f, "DEC {:?}", rs),
|
|
|
|
RLCA => f.write_str("RLCA"),
|
|
|
|
RRCA => f.write_str("RRCA"),
|
|
|
|
RLA => f.write_str("RLA"),
|
|
|
|
RRA => f.write_str("RRA"),
|
|
|
|
DAA => f.write_str("DAA"),
|
|
|
|
CPL => f.write_str("CPL"),
|
|
|
|
SCF => f.write_str("SCF"),
|
|
|
|
CCF => f.write_str("CCF"),
|
|
|
|
HALT => f.write_str("HALT"),
|
|
|
|
ADC(s) => write!(f, "ADC {:?}", s),
|
|
|
|
SUB(s) => write!(f, "SUB {:?}", s),
|
|
|
|
SBC(s) => write!(f, "SBC {:?}", s),
|
|
|
|
AND(s) => write!(f, "AND {:?}", s),
|
|
|
|
XOR(s) => write!(f, "XOR {:?}", s),
|
|
|
|
OR(s) => write!(f, "OR {:?}", s),
|
|
|
|
CP(s) => write!(f, "CP {:?}", s),
|
|
|
|
RET(c) => write!(f, "RET {:?}", c),
|
|
|
|
POP(rp) => write!(f, "POP: {:?}", rp),
|
|
|
|
RETI => f.write_str("RETI"),
|
|
|
|
JP(c, l) => write!(f, "JP {:?} {:?}", c, l),
|
|
|
|
DI => f.write_str("DI"),
|
|
|
|
EI => f.write_str("EI"),
|
|
|
|
CALL(c) => write!(f, "CALL {:?}", c),
|
|
|
|
PUSH(rp) => write!(f, "PUSH {:?}", rp),
|
|
|
|
RST(v) => write!(f, "RST {:#04X}", v),
|
|
|
|
RLC(r) => write!(f, "RLC {:?}", r),
|
|
|
|
RRC(r) => write!(f, "RRC {:?}", r),
|
|
|
|
RL(r) => write!(f, "RL {:?}", r),
|
|
|
|
RR(r) => write!(f, "RR {:?}", r),
|
|
|
|
SLA(r) => write!(f, "SLA {:?}", r),
|
|
|
|
SRA(r) => write!(f, "SRA {:?}", r),
|
|
|
|
SWAP(r) => write!(f, "SWAP {:?}", r),
|
|
|
|
SRL(r) => write!(f, "SRL {:?}", r),
|
|
|
|
BIT(b, r) => write!(f, "BIT {}, {:?}", b, r),
|
|
|
|
RES(b, r) => write!(f, "RES {}, {:?}", b, r),
|
|
|
|
SET(b, r) => write!(f, "SET {}, {:?}", b, r),
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 07:17:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 05:16:05 +00:00
|
|
|
impl Instruction {
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) fn execute(cpu: &mut Cpu, instruction: Self) -> Cycle {
|
2020-09-01 05:16:05 +00:00
|
|
|
match instruction {
|
2021-03-27 17:10:18 +00:00
|
|
|
Instruction::NOP => Cycle::new(4),
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::LD(target, src) => match (target, src) {
|
|
|
|
(LDTarget::IndirectImmediateWord, LDSource::SP) => {
|
|
|
|
// LD (u16), SP | Store stack pointer in byte at 16-bit register
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = Self::imm_word(cpu);
|
|
|
|
let sp = cpu.register_pair(RegisterPair::SP);
|
|
|
|
Self::write_word(&mut cpu.bus, addr, sp);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(20)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::Group1(pair), LDSource::ImmediateWord) => {
|
|
|
|
// LD r16, u16 | Store u16 in 16-bit register
|
|
|
|
use Group1RegisterPair::*;
|
2021-08-14 05:10:51 +00:00
|
|
|
let word = Self::imm_word(cpu);
|
2021-03-23 07:11:40 +00:00
|
|
|
|
2020-09-03 00:35:48 +00:00
|
|
|
match pair {
|
2021-08-01 01:29:13 +00:00
|
|
|
BC | DE | HL | SP => cpu.set_register_pair(pair.as_register_pair(), word),
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::IndirectGroup2(pair), LDSource::A) => {
|
|
|
|
// LD (r16), A | Store accumulator in byte at 16-bit register
|
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
|
2020-09-01 05:16:05 +00:00
|
|
|
match pair {
|
2021-08-01 01:29:13 +00:00
|
|
|
Group2RegisterPair::BC | Group2RegisterPair::DE => {
|
|
|
|
let addr = cpu.register_pair(pair.as_register_pair());
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, acc);
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Group2RegisterPair::IncrementHL => {
|
2020-09-01 05:16:05 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, acc);
|
2020-09-01 05:16:05 +00:00
|
|
|
cpu.set_register_pair(RegisterPair::HL, addr + 1);
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Group2RegisterPair::DecrementHL => {
|
2020-09-01 05:16:05 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, acc);
|
2020-09-01 05:16:05 +00:00
|
|
|
cpu.set_register_pair(RegisterPair::HL, addr - 1);
|
|
|
|
}
|
|
|
|
}
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::A, LDSource::IndirectGroup2(pair)) => {
|
|
|
|
// LD A, (r16) | Store byte at 16-bit register in accumulator
|
2020-09-01 05:16:05 +00:00
|
|
|
match pair {
|
2021-08-01 01:29:13 +00:00
|
|
|
Group2RegisterPair::BC | Group2RegisterPair::DE => {
|
|
|
|
let addr = cpu.register_pair(pair.as_register_pair());
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, byte);
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Group2RegisterPair::IncrementHL => {
|
2020-09-01 05:16:05 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, byte);
|
2020-09-01 05:16:05 +00:00
|
|
|
cpu.set_register_pair(RegisterPair::HL, addr + 1);
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Group2RegisterPair::DecrementHL => {
|
2020-09-01 05:16:05 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, byte);
|
2020-09-01 05:16:05 +00:00
|
|
|
cpu.set_register_pair(RegisterPair::HL, addr - 1);
|
|
|
|
}
|
|
|
|
}
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::Register(reg), LDSource::ImmediateByte) => {
|
|
|
|
// LD r8, u8 | Store u8 in 8-bit register
|
|
|
|
use Register::*;
|
2021-08-14 05:10:51 +00:00
|
|
|
let right = Self::imm_byte(cpu);
|
2021-03-23 23:21:59 +00:00
|
|
|
|
2020-09-01 05:16:05 +00:00
|
|
|
match reg {
|
2021-03-23 23:21:59 +00:00
|
|
|
A | B | C | D | E | H | L => {
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(reg.cpu_register(), right);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2021-03-23 23:21:59 +00:00
|
|
|
}
|
|
|
|
IndirectHL => {
|
2020-09-01 05:16:05 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, right);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::IoWithC, LDSource::A) => {
|
|
|
|
// LD (0xFF00 + C), A | Store accumulator in byte at address 0xFF00 + C
|
|
|
|
let addr = 0xFF00 + cpu.register(CpuRegister::C) as u16;
|
2021-08-14 05:10:51 +00:00
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, acc);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::A, LDSource::IoWithC) => {
|
|
|
|
// LD A, (0xFF00 + C) | Store byte at 0xFF00 + C in register A
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = 0xFF00 + cpu.register(CpuRegister::C) as u16;
|
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, byte);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::Register(target), LDSource::Register(source)) => {
|
|
|
|
// LD r8, r8 | Store 8-bit register in 8-bit register
|
|
|
|
use Register::*;
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match source {
|
2021-03-23 07:11:40 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let right = cpu.register(source.cpu_register());
|
2021-03-23 07:11:40 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match target {
|
2021-03-23 07:11:40 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(target.cpu_register(), right);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2021-03-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
IndirectHL => {
|
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, right);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2021-03-23 07:11:40 +00:00
|
|
|
}
|
2021-01-17 23:31:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-23 07:11:40 +00:00
|
|
|
IndirectHL => {
|
2021-01-17 23:31:45 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-03-23 07:11:40 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match target {
|
2021-03-23 07:11:40 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(target.cpu_register(), right);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2021-03-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
IndirectHL => {
|
2021-08-01 01:29:13 +00:00
|
|
|
unreachable!("LD (HL), (HL) is an illegal instruction")
|
2021-03-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-17 23:31:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::IoWithImmediateOffset, LDSource::A) => {
|
|
|
|
// LD (0xFF00 + u8), A | Store accumulator in byte at address 0xFF00 + u8
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = 0xFF00 + Self::imm_byte(cpu) as u16;
|
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, acc);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::A, LDSource::IoWithImmediateOffset) => {
|
|
|
|
// LD A, (0xFF00 + u8) | Store byte at address 0xFF00 + u8 in accumulator
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = 0xFF00 + Self::imm_byte(cpu) as u16;
|
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, byte);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::SP, LDSource::HL) => {
|
|
|
|
// LD SP, HL | Store HL in stack pointer
|
2020-09-04 05:41:19 +00:00
|
|
|
cpu.set_register_pair(RegisterPair::SP, cpu.register_pair(RegisterPair::HL));
|
2021-08-14 05:10:51 +00:00
|
|
|
Cycle::new(8) // performs an internal operation that takes 4 cycles
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::IndirectImmediateWord, LDSource::A) => {
|
|
|
|
// LD (u16), A | Store accumulator in byte at 16-bit register
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = Self::imm_word(cpu);
|
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, acc);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(LDTarget::A, LDSource::IndirectImmediateWord) => {
|
|
|
|
// LD A, (u16) | Store byte at 16-bit register in accumulator
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = Self::imm_word(cpu);
|
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, byte);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
_ => unreachable!("LD {:?}, {:?} is an illegal instruction", target, src),
|
2020-09-01 05:16:05 +00:00
|
|
|
},
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::STOP => todo!("SM83 Instruction STOP executed"),
|
|
|
|
Instruction::JR(cond) => {
|
|
|
|
// JR cond i8 | Add i8 bytes from program counter if condition is true
|
|
|
|
// JR i8 | Add i8 bytes from program counter
|
|
|
|
let flags: Flags = *cpu.flags();
|
2021-03-23 07:11:40 +00:00
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::imm_byte(cpu) as i8; // Note: This modifies the PC we access immediately after
|
2021-08-01 01:29:13 +00:00
|
|
|
let pc = cpu.register_pair(RegisterPair::PC);
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = pc.wrapping_add(byte as u16);
|
2020-09-01 05:16:05 +00:00
|
|
|
|
|
|
|
match cond {
|
|
|
|
JumpCondition::NotZero => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.z() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Zero => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.z() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::NotCarry => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.c() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Carry => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.c() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
JumpCondition::Always => {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
Cycle::new(12)
|
|
|
|
}
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::ADD(target, src) => match (target, src) {
|
|
|
|
(AddTarget::HL, AddSource::Group1(pair)) => {
|
|
|
|
// ADD HL, r16 | Add 16-bit register to HL
|
2021-08-14 05:10:51 +00:00
|
|
|
// FIXME: Memory Timings are not properly emulated for this instruction
|
2021-08-01 01:29:13 +00:00
|
|
|
use Group1RegisterPair::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-01 05:16:05 +00:00
|
|
|
|
|
|
|
match pair {
|
2021-03-24 01:22:11 +00:00
|
|
|
BC | DE | HL | SP => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let left = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = cpu.register_pair(pair.as_register_pair());
|
|
|
|
cpu.set_register_pair(
|
|
|
|
RegisterPair::HL,
|
|
|
|
Self::add_u16(left, right, &mut flags),
|
|
|
|
);
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(AddTarget::A, AddSource::Register(reg)) => {
|
|
|
|
// ADD A, r8 | Add 8-bit register to accumulator
|
|
|
|
use Register::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-08-01 01:29:13 +00:00
|
|
|
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-03-22 02:16:23 +00:00
|
|
|
|
|
|
|
let (cycles, sum) = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let right = cpu.register(reg.cpu_register());
|
|
|
|
(Cycle::new(4), Self::add(left, right, &mut flags))
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-22 02:16:23 +00:00
|
|
|
IndirectHL => {
|
2021-03-23 23:21:59 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(8), Self::add(left, right, &mut flags))
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-22 02:16:23 +00:00
|
|
|
};
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, sum);
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(AddTarget::SP, AddSource::ImmediateSignedByte) => {
|
|
|
|
// ADD SP, i8 | Add i8 to stack pointer
|
2021-08-14 05:10:51 +00:00
|
|
|
// FIXME: Memory Timings are not properly emulated for this instruction
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-04-07 07:27:57 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let left = cpu.register_pair(RegisterPair::SP);
|
2021-08-14 05:10:51 +00:00
|
|
|
let sum = Self::add_u16_i8(left, Self::imm_byte(cpu) as i8, &mut flags);
|
2020-09-04 05:41:19 +00:00
|
|
|
cpu.set_register_pair(RegisterPair::SP, sum);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
(AddTarget::A, AddSource::ImmediateByte) => {
|
|
|
|
// ADD A, u8 | Add u8 to accumulator
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-08 01:50:33 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-08-14 05:10:51 +00:00
|
|
|
let sum = Self::add(left, Self::imm_byte(cpu), &mut flags);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, sum);
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
_ => unreachable!("ADD {:?}, {:?} is an illegal instruction", target, src),
|
2020-09-01 05:16:05 +00:00
|
|
|
},
|
2020-12-23 07:11:03 +00:00
|
|
|
Instruction::INC(registers) => {
|
|
|
|
match registers {
|
2021-08-01 01:29:13 +00:00
|
|
|
AllRegisters::Register(reg) => {
|
|
|
|
// INC r8 | Increment 8-bit register
|
|
|
|
use Register::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-03-23 07:11:40 +00:00
|
|
|
|
|
|
|
let cycles = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
cpu.set_register(reg, Self::inc(cpu.register(reg), &mut flags));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-12-23 07:11:03 +00:00
|
|
|
}
|
2021-03-23 07:11:40 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 07:11:03 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let left = Self::read_byte(&mut cpu.bus, addr);
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, Self::inc(left, &mut flags));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-12-23 07:11:03 +00:00
|
|
|
}
|
2021-03-23 07:11:40 +00:00
|
|
|
};
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2020-12-23 07:11:03 +00:00
|
|
|
cycles
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AllRegisters::Group1(pair) => {
|
|
|
|
// INC r16 | Increment 16-bit register
|
|
|
|
// Note: No flags are set with this version of the INC instruction
|
2021-08-14 05:10:51 +00:00
|
|
|
// FIXME: Memory Timings are not properly emulated for this instruction
|
2021-08-01 01:29:13 +00:00
|
|
|
use Group1RegisterPair::*;
|
2021-03-23 07:11:40 +00:00
|
|
|
|
2020-12-23 07:11:03 +00:00
|
|
|
match pair {
|
2021-03-23 07:11:40 +00:00
|
|
|
BC | DE | HL | SP => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let pair = pair.as_register_pair();
|
|
|
|
let left = cpu.register_pair(pair);
|
|
|
|
cpu.set_register_pair(pair, left.wrapping_add(1));
|
2020-12-23 07:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 07:11:40 +00:00
|
|
|
Instruction::DEC(registers) => {
|
|
|
|
match registers {
|
2021-08-01 01:29:13 +00:00
|
|
|
AllRegisters::Register(reg) => {
|
|
|
|
// DEC r8 | Decrement 8-bit register
|
|
|
|
use Register::*;
|
2021-03-23 07:11:40 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
|
|
|
|
|
|
|
let cycles = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
cpu.set_register(reg, Self::dec(cpu.register(reg), &mut flags));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2021-03-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
IndirectHL => {
|
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let left = Self::read_byte(&mut cpu.bus, addr);
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, Self::dec(left, &mut flags));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2021-03-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
cpu.set_flags(flags);
|
|
|
|
cycles
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AllRegisters::Group1(pair) => {
|
|
|
|
// DEC r16 | Decrement Register Pair
|
2021-08-14 05:10:51 +00:00
|
|
|
// FIXME: Memory Timings are not properly emulated for this instruction
|
2021-08-01 01:29:13 +00:00
|
|
|
use Group1RegisterPair::*;
|
2021-03-23 07:11:40 +00:00
|
|
|
|
|
|
|
match pair {
|
|
|
|
BC | DE | HL | SP => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let pair = pair.as_register_pair();
|
|
|
|
let left = cpu.register_pair(pair);
|
|
|
|
cpu.set_register_pair(pair, left.wrapping_sub(1));
|
2021-03-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
};
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Instruction::RLCA => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RLCA | Rotate accumulator left
|
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
let most_sgfnt = acc >> 7;
|
|
|
|
let acc_rotated = acc.rotate_left(1);
|
|
|
|
cpu.set_register(CpuRegister::A, acc_rotated);
|
|
|
|
cpu.update_flags(false, false, false, most_sgfnt == 0x01);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
Instruction::RRCA => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RRCA | Rotate accumulator right
|
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
let least_sgfnt = acc & 0x01;
|
|
|
|
let acc_rotated = acc.rotate_right(1);
|
|
|
|
cpu.set_register(CpuRegister::A, acc_rotated);
|
|
|
|
cpu.update_flags(false, false, false, least_sgfnt == 0x01);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2020-09-02 22:26:46 +00:00
|
|
|
Instruction::RLA => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RLA | Rotate accumulator left through carry
|
|
|
|
let flags: Flags = *cpu.flags();
|
2020-09-02 22:26:46 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
let (acc_rotated, carry) = Self::rl_thru_carry(acc, flags.c());
|
|
|
|
cpu.set_register(CpuRegister::A, acc_rotated);
|
|
|
|
cpu.update_flags(false, false, false, carry);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-02 22:26:46 +00:00
|
|
|
}
|
|
|
|
Instruction::RRA => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RRA | Rotate accumulator right through carry
|
|
|
|
let flags: Flags = *cpu.flags();
|
2020-09-02 22:26:46 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
let (acc_rotated, carry) = Self::rr_thru_carry(acc, flags.c());
|
|
|
|
cpu.set_register(CpuRegister::A, acc_rotated);
|
|
|
|
cpu.update_flags(false, false, false, carry);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-02 22:26:46 +00:00
|
|
|
}
|
2021-04-04 06:03:44 +00:00
|
|
|
Instruction::DAA => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// DAA | Change accumulator into its BCD representation
|
|
|
|
// resource: https://ehaskins.com/2018-01-30%20Z80%20DAA/
|
|
|
|
// https://github.com/mamedev/mame/blob/master/src/devices/cpu/lr35902/opc_main.hxx#L354
|
2021-04-04 06:03:44 +00:00
|
|
|
let mut flags = *cpu.flags();
|
2021-08-01 01:29:13 +00:00
|
|
|
let mut tmp = cpu.register(CpuRegister::A) as i16;
|
|
|
|
|
|
|
|
if !flags.n() {
|
|
|
|
// Positive
|
|
|
|
if flags.h() || tmp & 0x0F > 0x09 {
|
|
|
|
tmp += 0x06;
|
|
|
|
}
|
2021-04-04 06:03:44 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
if flags.c() || tmp > 0x9F {
|
|
|
|
tmp += 0x60;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Negative
|
|
|
|
if flags.h() {
|
|
|
|
tmp -= 6;
|
|
|
|
|
|
|
|
if !flags.c() {
|
|
|
|
tmp &= 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.c() {
|
|
|
|
tmp -= 0x60;
|
|
|
|
}
|
2021-04-04 06:03:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
if tmp & 0x100 != 0 {
|
2021-04-04 06:03:44 +00:00
|
|
|
flags.set_c(true);
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, tmp as u8);
|
|
|
|
flags.set_z(tmp as u8 == 0);
|
2021-04-04 06:03:44 +00:00
|
|
|
flags.set_h(false);
|
|
|
|
cpu.set_flags(flags);
|
|
|
|
Cycle::new(4)
|
|
|
|
}
|
2020-09-02 22:26:46 +00:00
|
|
|
Instruction::CPL => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// CPL | Compliment accumulator
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-02 22:26:46 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let acc = cpu.register(CpuRegister::A);
|
|
|
|
cpu.set_register(CpuRegister::A, !acc); // Bitwise not is ! instead of ~
|
2021-03-16 04:35:20 +00:00
|
|
|
flags.set_n(true);
|
|
|
|
flags.set_h(true);
|
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-02 22:26:46 +00:00
|
|
|
}
|
|
|
|
Instruction::SCF => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// SCF | Set Carry Flag
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-02 22:26:46 +00:00
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
flags.set_n(false);
|
|
|
|
flags.set_h(false);
|
|
|
|
flags.set_c(true);
|
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-02 22:26:46 +00:00
|
|
|
}
|
|
|
|
Instruction::CCF => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// CCF | Compliment Carry Flag
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-02 22:26:46 +00:00
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
flags.set_n(false);
|
|
|
|
flags.set_h(false);
|
|
|
|
flags.set_c(!flags.c());
|
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-02 22:26:46 +00:00
|
|
|
}
|
2021-03-24 04:05:27 +00:00
|
|
|
Instruction::HALT => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// HALT | Enter CPU low power consumption mode until interrupt occurs
|
2021-03-24 04:05:27 +00:00
|
|
|
use HaltState::*;
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let halt_state = match *cpu.ime() {
|
|
|
|
ImeState::Enabled => ImeEnabled,
|
2021-08-14 05:10:51 +00:00
|
|
|
_ if cpu.int_request() & cpu.int_enable() != 0 => SomePending,
|
2021-08-01 01:29:13 +00:00
|
|
|
_ => NonePending,
|
2021-03-24 04:05:27 +00:00
|
|
|
};
|
|
|
|
cpu.halt(halt_state);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2021-03-24 04:05:27 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::ADC(source) => match source {
|
|
|
|
AluSource::Register(reg) => {
|
|
|
|
// ADC A, r8 | Add 8-bit register to accumulator with carry
|
|
|
|
use Register::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-08-01 01:29:13 +00:00
|
|
|
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-03-24 01:22:11 +00:00
|
|
|
let (cycles, sum) = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let right = cpu.register(reg.cpu_register());
|
2021-05-04 05:50:22 +00:00
|
|
|
let sum = Self::add_with_carry_bit(left, right, flags.c(), &mut flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(4), sum)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-05-04 05:50:22 +00:00
|
|
|
let sum = Self::add_with_carry_bit(left, right, flags.c(), &mut flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(8), sum)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, sum);
|
2021-03-24 01:22:11 +00:00
|
|
|
cpu.set_flags(flags);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::ImmediateByte => {
|
|
|
|
// ADC A, u8 | Add u8 to accumulator with carry
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-08 01:50:33 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-08-14 05:10:51 +00:00
|
|
|
let right = Self::imm_byte(cpu);
|
2021-08-01 01:29:13 +00:00
|
|
|
let sum = Self::add_with_carry_bit(left, right, flags.c(), &mut flags);
|
|
|
|
cpu.set_register(CpuRegister::A, sum);
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
},
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::SUB(source) => match source {
|
|
|
|
AluSource::Register(reg) => {
|
|
|
|
// SUB r8 | Subtract 8-bit register from accumulator
|
|
|
|
use Register::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-08-01 01:29:13 +00:00
|
|
|
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-03-24 01:22:11 +00:00
|
|
|
let (cycles, diff) = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let right = cpu.register(reg.cpu_register());
|
|
|
|
(Cycle::new(4), Self::sub(left, right, &mut flags))
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(8), Self::sub(left, right, &mut flags))
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, diff);
|
2021-03-24 01:22:11 +00:00
|
|
|
cpu.set_flags(flags);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::ImmediateByte => {
|
|
|
|
// SUB u8 | Subtract u8 from accumulator
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-08 01:50:33 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-08-14 05:10:51 +00:00
|
|
|
let right = Self::imm_byte(cpu);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, Self::sub(left, right, &mut flags));
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
},
|
2020-09-08 01:28:24 +00:00
|
|
|
Instruction::SBC(target) => match target {
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::Register(reg) => {
|
|
|
|
// SBC r8 | Subtract 8-bit register from accumulator with carry
|
|
|
|
use Register::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-08-01 01:29:13 +00:00
|
|
|
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-03-24 01:22:11 +00:00
|
|
|
let (cycles, diff) = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let right = cpu.register(reg.cpu_register());
|
2021-04-08 03:37:33 +00:00
|
|
|
let diff = Self::sub_with_carry(left, right, flags.c(), &mut flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(4), diff)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-04-08 03:37:33 +00:00
|
|
|
let diff = Self::sub_with_carry(left, right, flags.c(), &mut flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(8), diff)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, diff);
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::ImmediateByte => {
|
|
|
|
// SBC u8 | Subtract u8 from accumulator with carry
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-08 01:50:33 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-08-14 05:10:51 +00:00
|
|
|
let right = Self::imm_byte(cpu);
|
|
|
|
let diff = Self::sub_with_carry(left, right, flags.c(), &mut flags);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, diff);
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
},
|
|
|
|
Instruction::AND(target) => match target {
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::Register(reg) => {
|
|
|
|
// AND r8 | Perform bitwise AND on accumulator and 8-bit register
|
|
|
|
use Register::*;
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, acc) = match reg {
|
2021-03-23 07:11:40 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(4), left & cpu.register(reg.cpu_register()))
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-23 07:11:40 +00:00
|
|
|
IndirectHL => {
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(8), left & right)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-23 07:11:40 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, acc);
|
|
|
|
cpu.update_flags(acc == 0, false, true, false);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::ImmediateByte => {
|
|
|
|
// AND u8 | Perform bitwise AND on accumulator and u8
|
2021-08-14 05:10:51 +00:00
|
|
|
let acc = cpu.register(CpuRegister::A) & Self::imm_byte(cpu);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, acc);
|
|
|
|
cpu.update_flags(acc == 0, false, true, false);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
},
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::XOR(source) => match source {
|
|
|
|
AluSource::Register(reg) => {
|
|
|
|
// XOR r8 | Perform bitwise XOR on accumulator and 8-bit register
|
|
|
|
use Register::*;
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, acc) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(4), left ^ cpu.register(reg.cpu_register()))
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(8), left ^ right)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, acc);
|
|
|
|
cpu.update_flags(acc == 0, false, false, false);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::ImmediateByte => {
|
|
|
|
// XOR u8 | Perform bitwise XOR on accumulator and u8
|
2021-08-14 05:10:51 +00:00
|
|
|
let acc = cpu.register(CpuRegister::A) ^ Self::imm_byte(cpu);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, acc);
|
|
|
|
cpu.update_flags(acc == 0, false, false, false);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
},
|
|
|
|
Instruction::OR(target) => match target {
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::Register(reg) => {
|
|
|
|
// OR r8 | Perform bitwise OR on accumulator and 8-bit register
|
|
|
|
use Register::*;
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, acc) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(4), left | cpu.register(reg.cpu_register()))
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(8), left | right)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, acc);
|
|
|
|
cpu.update_flags(acc == 0, false, false, false);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::ImmediateByte => {
|
|
|
|
// OR u8 | Perform bitwise OR on accumulator and u8
|
2021-08-14 05:10:51 +00:00
|
|
|
let acc = cpu.register(CpuRegister::A) | Self::imm_byte(cpu);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register(CpuRegister::A, acc);
|
|
|
|
cpu.update_flags(acc == 0, false, false, false);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
},
|
|
|
|
Instruction::CP(target) => match target {
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::Register(reg) => {
|
|
|
|
// CP r8 | Compare accumulator to 8-bit register. Do not store result
|
|
|
|
use Register::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-08-01 01:29:13 +00:00
|
|
|
|
|
|
|
let left = cpu.register(CpuRegister::A);
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-03-22 02:16:23 +00:00
|
|
|
let cycles = match reg {
|
2021-03-23 07:11:40 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let _ = Self::sub(left, cpu.register(reg.cpu_register()), &mut flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-23 07:11:40 +00:00
|
|
|
IndirectHL => {
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
|
|
|
let right = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let _ = Self::sub(left, right, &mut flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
2021-03-22 02:16:23 +00:00
|
|
|
};
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2020-09-03 02:54:58 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
AluSource::ImmediateByte => {
|
|
|
|
// CP u8 | Compare accumulator to u8. Do not store result
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2020-09-08 01:50:33 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let left = cpu.register(CpuRegister::A);
|
2021-08-14 05:10:51 +00:00
|
|
|
let _ = Self::sub(left, Self::imm_byte(cpu), &mut flags);
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-08 01:50:33 +00:00
|
|
|
}
|
2020-09-03 02:54:58 +00:00
|
|
|
},
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::LDHL => {
|
|
|
|
// LD HL, SP + i8 | Store stack pointer + i8 in HL
|
|
|
|
let mut flags: Flags = *cpu.flags();
|
|
|
|
|
|
|
|
let left = cpu.register_pair(RegisterPair::SP);
|
2021-08-14 05:10:51 +00:00
|
|
|
let sum = Self::add_u16_i8(left, Self::imm_byte(cpu) as i8, &mut flags);
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.set_register_pair(RegisterPair::HL, sum);
|
|
|
|
cpu.set_flags(flags);
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // FIXME: Is this in the right place?
|
2021-08-01 01:29:13 +00:00
|
|
|
Cycle::new(12)
|
|
|
|
}
|
2020-09-04 05:41:19 +00:00
|
|
|
Instruction::RET(cond) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RET cond | Return from subroutine if condition is true
|
|
|
|
// RET | Return from subroutine
|
2021-08-14 05:10:51 +00:00
|
|
|
let flags: Flags = *cpu.flags();
|
2020-09-04 05:41:19 +00:00
|
|
|
|
|
|
|
match cond {
|
|
|
|
JumpCondition::NotZero => {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.z() {
|
2020-09-08 02:49:10 +00:00
|
|
|
let addr = Self::pop(cpu);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(20)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Zero => {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.z() {
|
2020-09-08 02:49:10 +00:00
|
|
|
let addr = Self::pop(cpu);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(20)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::NotCarry => {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.c() {
|
2020-09-08 02:49:10 +00:00
|
|
|
let addr = Self::pop(cpu);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(20)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Carry => {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
|
|
|
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.c() {
|
2020-09-08 02:49:10 +00:00
|
|
|
let addr = Self::pop(cpu);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(20)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Always => {
|
2020-09-08 02:49:10 +00:00
|
|
|
let addr = Self::pop(cpu);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Instruction::POP(pair) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// POP r16 | Store word popped from the stack in r16
|
|
|
|
use Group3RegisterPair::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2020-09-04 05:41:19 +00:00
|
|
|
match pair {
|
2021-03-24 01:22:11 +00:00
|
|
|
BC | DE | HL | AF => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let right = Self::pop(cpu);
|
|
|
|
cpu.set_register_pair(pair.as_register_pair(), right);
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
Instruction::RETI => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RETI | Return from subroutine, then enable interrupts
|
2020-09-08 02:49:10 +00:00
|
|
|
let addr = Self::pop(cpu);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-04-05 05:52:12 +00:00
|
|
|
cpu.set_ime(ImeState::Enabled);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::JP(cond, location) => match location {
|
|
|
|
JumpLocation::HL => {
|
|
|
|
// JP HL | Store HL in program counter
|
|
|
|
let right = cpu.register_pair(RegisterPair::HL);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, right);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
JumpLocation::ImmediateWord => {
|
|
|
|
// JP cond u16 | Store u16 in program counter if condition is true
|
|
|
|
// JP u16 | Store u16 in program counter
|
|
|
|
let flags: Flags = *cpu.flags();
|
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = Self::imm_word(cpu);
|
2020-09-08 01:19:10 +00:00
|
|
|
|
|
|
|
match cond {
|
|
|
|
JumpCondition::NotZero => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.z() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Zero => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.z() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::NotCarry => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.c() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Carry => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.c() {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2021-03-23 07:11:40 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Always => {
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::jump(cpu, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 05:41:19 +00:00
|
|
|
},
|
2020-09-08 01:19:10 +00:00
|
|
|
Instruction::DI => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// DI | Disable IME
|
2021-04-05 05:52:12 +00:00
|
|
|
cpu.set_ime(ImeState::Disabled);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
Instruction::EI => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// EI | Enable IME after the next instruction
|
2021-08-14 21:42:15 +00:00
|
|
|
cpu.set_ime(ImeState::EiExecuted);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(4)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::CALL(cond) => {
|
|
|
|
// CALL cond u16 | Push PC on the stack and store u16 in program counter if condition is true
|
|
|
|
// CALL u16 | Push PC on the stack then store u16 in program counter
|
|
|
|
let flags: Flags = *cpu.flags();
|
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
let addr = Self::imm_word(cpu);
|
2021-08-01 01:29:13 +00:00
|
|
|
let return_addr = cpu.register_pair(RegisterPair::PC);
|
2020-09-08 01:19:10 +00:00
|
|
|
|
|
|
|
match cond {
|
|
|
|
JumpCondition::NotZero => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.z() {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
2021-08-01 01:29:13 +00:00
|
|
|
Self::push(cpu, return_addr);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(24)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Zero => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.z() {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
2021-08-01 01:29:13 +00:00
|
|
|
Self::push(cpu, return_addr);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(24)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::NotCarry => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if !flags.c() {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
2021-08-01 01:29:13 +00:00
|
|
|
Self::push(cpu, return_addr);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(24)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Carry => {
|
2021-03-16 04:35:20 +00:00
|
|
|
if flags.c() {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
2021-08-01 01:29:13 +00:00
|
|
|
Self::push(cpu, return_addr);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(24)
|
2021-03-24 01:22:11 +00:00
|
|
|
} else {
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(12)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JumpCondition::Always => {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal branch decision
|
2021-08-01 01:29:13 +00:00
|
|
|
Self::push(cpu, return_addr);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, addr);
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(24)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Instruction::PUSH(pair) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// PUSH r16 | Push r16 onto the stack
|
|
|
|
use Group3RegisterPair::*;
|
2021-03-23 07:11:40 +00:00
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal
|
|
|
|
|
2020-09-08 01:19:10 +00:00
|
|
|
match pair {
|
2021-03-23 07:11:40 +00:00
|
|
|
BC | DE | HL | AF => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let word = cpu.register_pair(pair.as_register_pair());
|
|
|
|
Self::push(cpu, word);
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-09-08 02:49:10 +00:00
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::RST(vector) => {
|
|
|
|
// RST vector | Push current address onto the stack, jump to 0x0000 + n
|
|
|
|
Self::reset(cpu, vector)
|
2020-09-08 01:19:10 +00:00
|
|
|
}
|
2020-09-08 03:34:09 +00:00
|
|
|
Instruction::RLC(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RLC r8 | Rotate r8 left
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, most_sgfnt, rotated) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
let rotated = byte.rotate_left(1);
|
|
|
|
cpu.set_register(reg, rotated);
|
|
|
|
(Cycle::new(8), byte >> 7, rotated)
|
2020-09-08 03:34:09 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-09-08 03:34:09 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let rotated = byte.rotate_left(1);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, rotated);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(16), byte >> 7, rotated)
|
2020-09-08 03:34:09 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(rotated == 0, false, false, most_sgfnt == 0x01);
|
2020-09-08 03:34:09 +00:00
|
|
|
cycles
|
|
|
|
}
|
|
|
|
Instruction::RRC(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RRC r8 | Rotate r8 right
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, least_sgfnt, rotated) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
let rotated = byte.rotate_right(1);
|
|
|
|
cpu.set_register(reg, rotated);
|
|
|
|
(Cycle::new(8), byte & 0x01, rotated)
|
2020-09-08 03:34:09 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-09-08 03:34:09 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let rotated = byte.rotate_right(1);
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, rotated);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(16), byte & 0x01, rotated)
|
2020-09-08 03:34:09 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(rotated == 0, false, false, least_sgfnt == 0x01);
|
2020-12-23 04:23:09 +00:00
|
|
|
cycles
|
|
|
|
}
|
|
|
|
Instruction::RL(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RL r8 | Rotate r8 left through carry
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let flags: Flags = *cpu.flags();
|
2020-12-23 04:23:09 +00:00
|
|
|
|
2021-03-24 01:22:11 +00:00
|
|
|
let (cycles, rotated, carry) = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
let (rotated, carry) = Self::rl_thru_carry(byte, flags.c());
|
|
|
|
cpu.set_register(reg, rotated);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(8), rotated, carry)
|
2020-12-23 04:23:09 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 04:23:09 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let (rotated, carry) = Self::rl_thru_carry(byte, flags.c());
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, rotated);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(16), rotated, carry)
|
2020-12-23 04:23:09 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(rotated == 0, false, false, carry);
|
2020-12-23 04:23:09 +00:00
|
|
|
cycles
|
|
|
|
}
|
|
|
|
Instruction::RR(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// RR r8 | Rotate register r8 right through carry
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let flags: Flags = *cpu.flags();
|
2020-12-23 06:24:29 +00:00
|
|
|
|
2021-03-24 01:22:11 +00:00
|
|
|
let (cycles, rotated, carry) = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
let (rotated, carry) = Self::rr_thru_carry(byte, flags.c());
|
|
|
|
cpu.set_register(reg, rotated);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(8), rotated, carry)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 06:24:29 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let (rotated, carry) = Self::rr_thru_carry(byte, flags.c());
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, rotated);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(16), rotated, carry)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(rotated == 0, false, false, carry);
|
2020-12-23 06:24:29 +00:00
|
|
|
cycles
|
|
|
|
}
|
|
|
|
Instruction::SLA(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// SLA r8 | Shift left arithmetic r8
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, most_sgfnt, shifted) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
let shifted = byte << 1;
|
|
|
|
cpu.set_register(reg, shifted);
|
|
|
|
(Cycle::new(8), (byte >> 7) & 0x01, shifted)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 06:24:29 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let shifted = byte << 1;
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, shifted);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(16), (byte >> 7) & 0x01, shifted)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(shifted == 0, false, false, most_sgfnt == 0x01);
|
2020-12-23 06:24:29 +00:00
|
|
|
cycles
|
|
|
|
}
|
|
|
|
Instruction::SRA(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// SRA r8 | Shift right arithmetic r8
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, least_sgfnt, shifted) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
let shifted = ((byte >> 7) & 0x01) << 7 | byte >> 1;
|
|
|
|
cpu.set_register(reg, shifted);
|
|
|
|
(Cycle::new(8), byte & 0x01, shifted)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 06:24:29 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let shifted = ((byte >> 7) & 0x01) << 7 | byte >> 1;
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, shifted);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(16), byte & 0x01, shifted)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(shifted == 0, false, false, least_sgfnt == 0x01);
|
2020-12-23 06:24:29 +00:00
|
|
|
cycles
|
|
|
|
}
|
|
|
|
Instruction::SWAP(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// SWAP r[z] | Swap the two nybbles in a byte
|
|
|
|
use Register::*;
|
2020-12-23 06:24:29 +00:00
|
|
|
|
2021-03-24 01:22:11 +00:00
|
|
|
let (cycles, swapped) = match reg {
|
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let swapped = Self::swap_bits(cpu.register(reg));
|
|
|
|
cpu.set_register(reg, swapped);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(8), swapped)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 06:24:29 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let swapped = Self::swap_bits(Self::read_byte(&mut cpu.bus, addr));
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, swapped);
|
2021-03-27 17:10:18 +00:00
|
|
|
(Cycle::new(16), swapped)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(swapped == 0, false, false, false);
|
2020-12-23 06:24:29 +00:00
|
|
|
cycles
|
|
|
|
}
|
|
|
|
Instruction::SRL(reg) => {
|
2021-08-01 01:29:13 +00:00
|
|
|
// SRL r[z] | Shift right logic r8
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, least_sgfnt, shift_reg) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
let shifted = byte >> 1;
|
|
|
|
cpu.set_register(reg, shifted);
|
|
|
|
(Cycle::new(8), byte & 0x01, shifted)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 06:24:29 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
let shifted = byte >> 1;
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, addr, shifted);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(16), byte & 0x01, shifted)
|
2020-12-23 06:24:29 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
cpu.update_flags(shift_reg == 0, false, false, least_sgfnt == 0x01);
|
2020-09-08 03:34:09 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::BIT(bit, reg) => {
|
|
|
|
// BIT u8, r8 | Test bit u8 in r8
|
|
|
|
use Register::*;
|
2021-03-16 06:05:13 +00:00
|
|
|
let mut flags: Flags = *cpu.flags();
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
let (cycles, is_set) = match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
(Cycle::new(8), ((byte >> bit) & 0x01) == 0x01)
|
2020-12-23 07:07:30 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 07:07:30 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
2021-08-01 01:29:13 +00:00
|
|
|
(Cycle::new(12), ((byte >> bit) & 0x01) == 0x01)
|
2020-12-23 07:07:30 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
};
|
2021-08-01 01:29:13 +00:00
|
|
|
flags.set_z(!is_set);
|
|
|
|
flags.set_n(false);
|
|
|
|
flags.set_h(true);
|
2021-03-16 04:35:20 +00:00
|
|
|
cpu.set_flags(flags);
|
2020-12-23 07:07:30 +00:00
|
|
|
cycles
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::RES(bit, reg) => {
|
|
|
|
// RES u8, r8 | Reset bit u8 in r8
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2020-12-23 07:07:30 +00:00
|
|
|
match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let register = reg.cpu_register();
|
|
|
|
let byte = cpu.register(register);
|
|
|
|
cpu.set_register(register, byte & !(1 << bit));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-12-23 07:07:30 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 07:07:30 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, byte & !(1 << bit));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-12-23 07:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
Instruction::SET(bit, reg) => {
|
|
|
|
// SET u8, r8 | Set bit u8
|
|
|
|
use Register::*;
|
2021-03-24 01:22:11 +00:00
|
|
|
|
2020-12-23 07:07:30 +00:00
|
|
|
match reg {
|
2021-03-24 01:22:11 +00:00
|
|
|
B | C | D | E | H | L | A => {
|
2021-08-01 01:29:13 +00:00
|
|
|
let reg = reg.cpu_register();
|
|
|
|
let byte = cpu.register(reg);
|
|
|
|
cpu.set_register(reg, byte | (1u8 << bit));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(8)
|
2020-12-23 07:07:30 +00:00
|
|
|
}
|
2021-03-24 01:22:11 +00:00
|
|
|
IndirectHL => {
|
2020-12-23 07:07:30 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::HL);
|
2021-08-14 05:10:51 +00:00
|
|
|
let byte = Self::read_byte(&mut cpu.bus, addr);
|
|
|
|
Self::write_byte(&mut cpu.bus, addr, byte | (1u8 << bit));
|
2021-03-27 17:10:18 +00:00
|
|
|
Cycle::new(16)
|
2020-12-23 07:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 07:31:07 +00:00
|
|
|
/// PUSHes a u16 onto the stack
|
2020-09-04 05:41:19 +00:00
|
|
|
///
|
2021-08-14 05:10:51 +00:00
|
|
|
/// Mutates the stack pointer and the stack (8 cycles)
|
2020-09-08 02:49:10 +00:00
|
|
|
fn push(cpu: &mut Cpu, value: u16) {
|
|
|
|
let mut sp = cpu.register_pair(RegisterPair::SP);
|
|
|
|
|
|
|
|
sp -= 1;
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, sp, (value >> 8) as u8);
|
2020-09-08 02:49:10 +00:00
|
|
|
sp -= 1;
|
2021-08-14 05:10:51 +00:00
|
|
|
Self::write_byte(&mut cpu.bus, sp, value as u8);
|
2020-09-08 02:49:10 +00:00
|
|
|
|
|
|
|
cpu.set_register_pair(RegisterPair::SP, sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// POPs a u16 from the stack
|
|
|
|
///
|
|
|
|
/// Mutates the stack pointer and returns the u16 which was popped from the stack
|
2021-08-14 05:10:51 +00:00
|
|
|
/// (8 cycles)
|
2020-09-08 02:49:10 +00:00
|
|
|
fn pop(cpu: &mut Cpu) -> u16 {
|
|
|
|
let mut sp = cpu.register_pair(RegisterPair::SP);
|
|
|
|
|
2021-08-14 05:10:51 +00:00
|
|
|
let low = Self::read_byte(&mut cpu.bus, sp);
|
2020-09-08 02:49:10 +00:00
|
|
|
sp += 1;
|
2021-08-14 05:10:51 +00:00
|
|
|
let high = Self::read_byte(&mut cpu.bus, sp);
|
2020-09-08 02:49:10 +00:00
|
|
|
sp += 1;
|
|
|
|
|
|
|
|
cpu.set_register_pair(RegisterPair::SP, sp);
|
|
|
|
(high as u16) << 8 | low as u16
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn dec(left: u8, flags: &mut Flags) -> u8 {
|
|
|
|
Self::sub_no_carry(left, 1, flags)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn inc(left: u8, flags: &mut Flags) -> u8 {
|
|
|
|
Self::add_no_carry(left, 1, flags)
|
2020-09-03 02:54:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 05:50:22 +00:00
|
|
|
fn sub_no_carry(left: u8, right: u8, flags: &mut Flags) -> u8 {
|
2020-09-03 02:54:58 +00:00
|
|
|
let diff = left.wrapping_sub(right);
|
2020-09-01 05:16:05 +00:00
|
|
|
|
2021-03-22 02:16:23 +00:00
|
|
|
flags.set_z(diff == 0);
|
|
|
|
flags.set_n(true);
|
2021-04-07 07:27:57 +00:00
|
|
|
flags.set_h(Self::bit_4_borrow(left, right));
|
2021-03-22 02:16:23 +00:00
|
|
|
|
2020-09-03 02:54:58 +00:00
|
|
|
diff
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 05:50:22 +00:00
|
|
|
fn sub(left: u8, right: u8, flags: &mut Flags) -> u8 {
|
2020-09-03 02:54:58 +00:00
|
|
|
let (diff, did_overflow) = left.overflowing_sub(right);
|
|
|
|
|
2020-12-23 04:23:09 +00:00
|
|
|
flags.update(
|
|
|
|
diff == 0,
|
|
|
|
true,
|
2021-04-07 07:27:57 +00:00
|
|
|
Self::bit_4_borrow(left, right),
|
2020-12-23 04:23:09 +00:00
|
|
|
did_overflow,
|
|
|
|
);
|
2020-09-03 02:54:58 +00:00
|
|
|
diff
|
|
|
|
}
|
2020-09-01 05:16:05 +00:00
|
|
|
|
2021-04-08 03:37:33 +00:00
|
|
|
fn sub_with_carry(left: u8, right: u8, carry: bool, flags: &mut Flags) -> u8 {
|
|
|
|
let carry = carry as u8;
|
|
|
|
|
|
|
|
let (diff, did_overflow) = {
|
|
|
|
let (tmp_diff, did) = left.overflowing_sub(right);
|
|
|
|
let (diff, overflow) = tmp_diff.overflowing_sub(carry);
|
|
|
|
|
|
|
|
(diff, did || overflow)
|
|
|
|
};
|
|
|
|
|
|
|
|
flags.update(
|
|
|
|
diff == 0,
|
|
|
|
true,
|
|
|
|
(left & 0x0F).wrapping_sub(right & 0x0F).wrapping_sub(carry) > 0x0F,
|
|
|
|
did_overflow,
|
|
|
|
);
|
|
|
|
|
|
|
|
diff
|
|
|
|
}
|
|
|
|
|
2020-09-04 05:41:19 +00:00
|
|
|
fn add_u16_i8(left: u16, right: i8, flags: &mut Flags) -> u16 {
|
2021-04-07 07:27:57 +00:00
|
|
|
let (_, did_overflow) = (left as u8).overflowing_add(right as u8);
|
2021-05-04 05:50:22 +00:00
|
|
|
let sum = left.wrapping_add(right as u16);
|
2020-09-04 05:41:19 +00:00
|
|
|
|
2021-04-07 07:27:57 +00:00
|
|
|
let half_carry = Self::bit_3_overflow(left as u8, right as u8);
|
2021-03-24 01:22:11 +00:00
|
|
|
flags.update(false, false, half_carry, did_overflow);
|
2020-09-04 05:41:19 +00:00
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
2021-05-04 05:50:22 +00:00
|
|
|
fn add_no_carry(left: u8, right: u8, flags: &mut Flags) -> u8 {
|
2020-09-03 02:54:58 +00:00
|
|
|
let sum = left.wrapping_add(right);
|
|
|
|
|
2021-03-22 02:16:23 +00:00
|
|
|
flags.set_z(sum == 0);
|
|
|
|
flags.set_n(false);
|
2021-04-07 07:27:57 +00:00
|
|
|
flags.set_h(Self::bit_3_overflow(left, right));
|
2020-09-03 02:54:58 +00:00
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
2021-05-04 05:50:22 +00:00
|
|
|
fn add(left: u8, right: u8, flags: &mut Flags) -> u8 {
|
2020-09-03 02:54:58 +00:00
|
|
|
let (sum, did_overflow) = left.overflowing_add(right);
|
|
|
|
|
2020-12-23 04:23:09 +00:00
|
|
|
flags.update(
|
|
|
|
sum == 0,
|
|
|
|
false,
|
2021-04-07 07:27:57 +00:00
|
|
|
Self::bit_3_overflow(left, right),
|
2020-12-23 04:23:09 +00:00
|
|
|
did_overflow,
|
|
|
|
);
|
2021-04-08 03:37:33 +00:00
|
|
|
|
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
2021-05-04 05:50:22 +00:00
|
|
|
fn add_with_carry_bit(left: u8, right: u8, carry: bool, flags: &mut Flags) -> u8 {
|
2021-04-08 03:37:33 +00:00
|
|
|
let carry = carry as u8;
|
|
|
|
|
|
|
|
let (sum, did_overflow) = {
|
|
|
|
let (tmp_sum, did) = left.overflowing_add(right);
|
|
|
|
let (sum, overflow) = tmp_sum.overflowing_add(carry);
|
|
|
|
|
|
|
|
(sum, did || overflow)
|
|
|
|
};
|
|
|
|
|
|
|
|
flags.update(
|
|
|
|
sum == 0,
|
|
|
|
false,
|
|
|
|
(((left & 0x0F) + (right & 0x0F) + carry) & 0x10) == 0x10,
|
|
|
|
did_overflow,
|
|
|
|
);
|
|
|
|
|
2020-09-03 02:54:58 +00:00
|
|
|
sum
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 05:50:22 +00:00
|
|
|
fn add_u16(left: u16, right: u16, flags: &mut Flags) -> u16 {
|
2020-09-01 05:16:05 +00:00
|
|
|
let (sum, did_overflow) = left.overflowing_add(right);
|
2020-09-03 02:54:58 +00:00
|
|
|
|
2021-03-27 01:19:48 +00:00
|
|
|
flags.set_n(false);
|
2021-04-07 07:27:57 +00:00
|
|
|
flags.set_h(Self::bit_11_overflow(left, right));
|
2021-03-27 01:19:48 +00:00
|
|
|
flags.set_c(did_overflow);
|
|
|
|
|
2020-09-01 05:16:05 +00:00
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
2021-04-07 07:27:57 +00:00
|
|
|
fn bit_11_overflow(left: u16, right: u16) -> bool {
|
|
|
|
(((left & 0x0FFF) + (right & 0x0FFF)) & 0x1000) == 0x1000
|
2021-03-22 02:16:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:27:57 +00:00
|
|
|
fn bit_3_overflow(left: u8, right: u8) -> bool {
|
2021-03-22 02:16:23 +00:00
|
|
|
(((left & 0xF) + (right & 0xF)) & 0x10) == 0x10
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:27:57 +00:00
|
|
|
fn bit_4_borrow(left: u8, right: u8) -> bool {
|
|
|
|
(left & 0x0F) < (right & 0x0F)
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
2020-12-23 06:24:29 +00:00
|
|
|
|
2020-12-23 07:07:30 +00:00
|
|
|
fn rl_thru_carry(byte: u8, carry: bool) -> (u8, bool) {
|
2020-12-23 06:24:29 +00:00
|
|
|
let carry_flag = (byte >> 7) & 0x01; // get the MSB of the u8 (which will rotate into the carry bit)
|
|
|
|
let new_byte = (byte << 1) | carry as u8; // shift the bit left, and then OR the carry bit in.
|
|
|
|
|
|
|
|
(new_byte, carry_flag == 0x01)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rr_thru_carry(byte: u8, carry: bool) -> (u8, bool) {
|
|
|
|
let carry_flag = byte & 0x01; // get the LSB of the u8 (which will rotate into the carry bit)
|
2021-03-24 01:22:11 +00:00
|
|
|
let new_byte = ((carry as u8) << 7) | (byte >> 1); // shift the bit right, and then OR the carry bit in.
|
2020-12-23 06:24:29 +00:00
|
|
|
|
|
|
|
(new_byte, carry_flag == 0x01)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn swap_bits(byte: u8) -> u8 {
|
|
|
|
let upper = byte >> 4;
|
|
|
|
let lower = byte & 0x0F;
|
|
|
|
|
|
|
|
(lower << 4) | upper
|
|
|
|
}
|
2021-04-08 04:05:03 +00:00
|
|
|
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) fn reset(cpu: &mut Cpu, vector: u8) -> Cycle {
|
2021-08-14 05:10:51 +00:00
|
|
|
cpu.bus.clock(); // internal
|
2021-04-08 04:05:03 +00:00
|
|
|
let addr = cpu.register_pair(RegisterPair::PC);
|
|
|
|
Self::push(cpu, addr);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, vector as u16);
|
|
|
|
Cycle::new(16)
|
|
|
|
}
|
2021-08-14 05:10:51 +00:00
|
|
|
|
|
|
|
/// Read u8 from memory (4 cycles)
|
|
|
|
fn read_byte(bus: &mut Bus, addr: u16) -> u8 {
|
|
|
|
let byte = bus.read_byte(addr);
|
|
|
|
bus.clock();
|
|
|
|
byte
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write u8 to memory (4 cycles)
|
|
|
|
fn write_byte(bus: &mut Bus, addr: u16, byte: u8) {
|
|
|
|
bus.write_byte(addr, byte);
|
|
|
|
bus.clock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write u16 to memory (8 cycles)
|
|
|
|
fn write_word(bus: &mut Bus, addr: u16, word: u16) {
|
|
|
|
Self::write_byte(bus, addr, word as u8);
|
|
|
|
Self::write_byte(bus, addr + 1, (word >> 8) as u8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read u16 from memory (8 cycles)
|
|
|
|
fn read_word(bus: &mut Bus, addr: u16) -> u16 {
|
|
|
|
// Must preserve the order, can't one-line this.
|
|
|
|
let low = Self::read_byte(bus, addr);
|
|
|
|
let high = Self::read_byte(bus, addr + 1);
|
|
|
|
(high as u16) << 8 | low as u16
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch u16 from memory, increment the program counter by two
|
|
|
|
/// (8 cycles)
|
|
|
|
fn imm_word(cpu: &mut Cpu) -> u16 {
|
|
|
|
let pc = cpu.register_pair(RegisterPair::PC);
|
|
|
|
let word = Self::read_word(&mut cpu.bus, pc);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, pc + 2);
|
|
|
|
|
|
|
|
word
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch u8 from memory, increment the program counter by one
|
|
|
|
/// (4 cycles)
|
|
|
|
fn imm_byte(cpu: &mut Cpu) -> u8 {
|
|
|
|
let pc = cpu.register_pair(RegisterPair::PC);
|
|
|
|
let byte = Self::read_byte(&mut cpu.bus, pc);
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, pc + 1);
|
|
|
|
|
|
|
|
byte
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set program counter to Address.
|
|
|
|
///
|
|
|
|
/// This is explicitly meant to emulate the exact behaviour of JP, JR RET, RETI and CALL
|
|
|
|
/// (4 cycles)
|
|
|
|
fn jump(cpu: &mut Cpu, addr: u16) {
|
|
|
|
cpu.set_register_pair(RegisterPair::PC, addr);
|
|
|
|
cpu.bus.clock();
|
|
|
|
}
|
2020-09-01 05:16:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 23:38:27 +00:00
|
|
|
impl Instruction {
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn decode(byte: u8, prefixed: bool) -> Self {
|
|
|
|
if prefixed {
|
|
|
|
Self::prefixed(byte)
|
2020-08-29 23:38:27 +00:00
|
|
|
} else {
|
2021-08-01 01:29:13 +00:00
|
|
|
Self::unprefixed(byte)
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn unprefixed(byte: u8) -> Self {
|
|
|
|
use Instruction::*;
|
2020-08-29 23:38:27 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match byte {
|
|
|
|
// NOP
|
|
|
|
0o000 => NOP,
|
|
|
|
// LD (u16), SP
|
|
|
|
0o010 => LD(LDTarget::IndirectImmediateWord, LDSource::SP),
|
|
|
|
// STOP
|
|
|
|
0o020 => STOP,
|
|
|
|
// JR i8
|
|
|
|
0o030 => JR(JumpCondition::Always),
|
|
|
|
// JR cond i8
|
|
|
|
0o040 | 0o050 | 0o060 | 0o070 => JR(jump_cond((byte >> 3) & 0x03)),
|
|
|
|
// LD r16, u16
|
|
|
|
0o001 | 0o021 | 0o041 | 0o061 => LD(
|
|
|
|
LDTarget::Group1(group1((byte >> 4) & 0x03)),
|
|
|
|
LDSource::ImmediateWord,
|
2020-08-30 04:07:53 +00:00
|
|
|
),
|
2021-08-01 01:29:13 +00:00
|
|
|
// ADD HL, r16
|
|
|
|
0o011 | 0o031 | 0o051 | 0o071 => {
|
|
|
|
ADD(AddTarget::HL, AddSource::Group1(group1((byte >> 4) & 0x03)))
|
|
|
|
}
|
|
|
|
// LD (r16), A
|
|
|
|
0o002 | 0o022 | 0o042 | 0o062 => LD(
|
|
|
|
LDTarget::IndirectGroup2(group2((byte >> 4) & 0x03)),
|
|
|
|
LDSource::A,
|
2020-08-30 04:07:53 +00:00
|
|
|
),
|
2021-08-01 01:29:13 +00:00
|
|
|
// LD A, (r16)
|
|
|
|
0o012 | 0o032 | 0o052 | 0o072 => LD(
|
|
|
|
LDTarget::A,
|
|
|
|
LDSource::IndirectGroup2(group2((byte >> 4) & 0x03)),
|
2020-08-30 04:07:53 +00:00
|
|
|
),
|
2021-08-01 01:29:13 +00:00
|
|
|
// INC r16
|
|
|
|
0o003 | 0o023 | 0o043 | 0o063 => INC(AllRegisters::Group1(group1((byte >> 4) & 0x03))),
|
|
|
|
// DEC r16
|
|
|
|
0o013 | 0o033 | 0o053 | 0o073 => DEC(AllRegisters::Group1(group1((byte >> 4) & 0x03))),
|
|
|
|
// INC r8
|
|
|
|
0o004 | 0o014 | 0o024 | 0o034 | 0o044 | 0o054 | 0o064 | 0o074 => {
|
|
|
|
INC(AllRegisters::Register(register((byte >> 3) & 0x07)))
|
|
|
|
}
|
|
|
|
// DEC r8
|
|
|
|
0o005 | 0o015 | 0o025 | 0o035 | 0o045 | 0o055 | 0o065 | 0o075 => {
|
|
|
|
DEC(AllRegisters::Register(register((byte >> 3) & 0x07)))
|
|
|
|
}
|
|
|
|
// LD r8, u8
|
|
|
|
0o006 | 0o016 | 0o026 | 0o036 | 0o046 | 0o056 | 0o066 | 0o076 => LD(
|
|
|
|
LDTarget::Register(register((byte >> 3) & 0x07)),
|
|
|
|
LDSource::ImmediateByte,
|
2020-08-30 04:07:53 +00:00
|
|
|
),
|
2021-08-01 01:29:13 +00:00
|
|
|
// RLCA, RRCA, RLA, RRA, DAA, CPL, SCF, and CCF
|
|
|
|
0o007 | 0o017 | 0o027 | 0o037 | 0o047 | 0o057 | 0o067 | 0o077 => {
|
|
|
|
flag_instr((byte >> 3) & 0x07)
|
|
|
|
}
|
|
|
|
// HALT
|
|
|
|
0o166 => HALT,
|
|
|
|
// LD r8, r8
|
|
|
|
0o100..=0o177 => LD(
|
|
|
|
LDTarget::Register(register((byte >> 3) & 0x07)),
|
|
|
|
LDSource::Register(register(byte & 0x07)),
|
2020-08-30 04:07:53 +00:00
|
|
|
),
|
2021-08-01 01:29:13 +00:00
|
|
|
// ADD, ADC, SUB, SBC, AND, XOR, OR, and CP
|
|
|
|
0o200..=0o277 => alu_reg_instr((byte >> 3) & 0x07, byte & 0x07),
|
|
|
|
// RET cond
|
|
|
|
0o300 | 0o310 | 0o320 | 0o330 => RET(jump_cond((byte >> 3) & 0x03)),
|
|
|
|
// LD (0xFF00 + u8), A
|
|
|
|
0o340 => LD(LDTarget::IoWithImmediateOffset, LDSource::A),
|
|
|
|
// ADD SP, i8
|
|
|
|
0o350 => ADD(AddTarget::SP, AddSource::ImmediateSignedByte),
|
|
|
|
// LD A, (0xFF00 + u8)
|
|
|
|
0o360 => LD(LDTarget::A, LDSource::IoWithImmediateOffset),
|
|
|
|
// LD HL, SP + i8
|
|
|
|
0o370 => LDHL,
|
|
|
|
// POP r16
|
|
|
|
0o301 | 0o321 | 0o341 | 0o361 => POP(group3((byte >> 4) & 0x03)),
|
|
|
|
// RET
|
|
|
|
0o311 => RET(JumpCondition::Always),
|
|
|
|
// RETI
|
|
|
|
0o331 => RETI,
|
|
|
|
// JP HL
|
|
|
|
0o351 => JP(JumpCondition::Always, JumpLocation::HL),
|
|
|
|
// LD SP, HL
|
|
|
|
0o371 => LD(LDTarget::SP, LDSource::HL),
|
|
|
|
// JP cond u16
|
|
|
|
0o302 | 0o312 | 0o322 | 0o332 => {
|
|
|
|
JP(jump_cond((byte >> 3) & 0x03), JumpLocation::ImmediateWord)
|
|
|
|
}
|
|
|
|
// LD (0xFF00 + C), A
|
|
|
|
0o342 => LD(LDTarget::IoWithC, LDSource::A),
|
|
|
|
// LD (u16), A
|
|
|
|
0o352 => LD(LDTarget::IndirectImmediateWord, LDSource::A),
|
|
|
|
// LD A, (0xFF00 + C)
|
|
|
|
0o362 => LD(LDTarget::A, LDSource::IoWithC),
|
|
|
|
// LD A, (u16)
|
|
|
|
0o372 => LD(LDTarget::A, LDSource::IndirectImmediateWord),
|
|
|
|
// JP u16
|
|
|
|
0o303 => JP(JumpCondition::Always, JumpLocation::ImmediateWord),
|
|
|
|
// 0xCB Prefix
|
|
|
|
0o313 => unreachable!("{:#04X} should be handled by the prefixed decoder", byte),
|
|
|
|
// DI
|
|
|
|
0o363 => DI,
|
|
|
|
// EI
|
|
|
|
0o373 => EI,
|
|
|
|
// CALL cond u16
|
|
|
|
0o304 | 0o314 | 0o324 | 0o334 => CALL(jump_cond((byte >> 3) & 0x03)),
|
|
|
|
// PUSH r16
|
|
|
|
0o305 | 0o325 | 0o345 | 0o365 => PUSH(group3((byte >> 4) & 0x03)),
|
|
|
|
0o315 => CALL(JumpCondition::Always),
|
|
|
|
0o306 | 0o316 | 0o326 | 0o336 | 0o346 | 0o356 | 0o366 | 0o376 => {
|
|
|
|
alu_imm_instr((byte >> 3) & 0x07)
|
|
|
|
}
|
|
|
|
0o307 | 0o317 | 0o327 | 0o337 | 0o347 | 0o357 | 0o367 | 0o377 => RST(byte & 0b00111000),
|
|
|
|
_ => panic!("{:#04X} is an illegal opcode", byte),
|
2020-08-30 04:07:53 +00:00
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn prefixed(byte: u8) -> Self {
|
|
|
|
use Instruction::*;
|
2021-04-04 06:19:39 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match byte {
|
|
|
|
// RLC, RRC, RL, RR, SLA, SRA, SWAP and SRL
|
|
|
|
0o000..=0o077 => prefix_alu((byte >> 3) & 0x07, byte & 0x07),
|
|
|
|
// BIT bit, r8
|
|
|
|
0o100..=0o177 => BIT((byte >> 3) & 0x07, register(byte & 0x07)),
|
|
|
|
// RES bit, r8
|
|
|
|
0o200..=0o277 => RES((byte >> 3) & 0x07, register(byte & 0x07)),
|
|
|
|
// SET bit, r8
|
|
|
|
0o300..=0o377 => SET((byte >> 3) & 0x07, register(byte & 0x07)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 02:18:53 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
mod jump {
|
2020-09-08 02:18:53 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum JumpCondition {
|
|
|
|
Always,
|
|
|
|
NotZero,
|
|
|
|
Zero,
|
|
|
|
NotCarry,
|
|
|
|
Carry,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for JumpCondition {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use JumpCondition::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Always => f.write_str(""),
|
|
|
|
NotZero => f.write_str("NZ"),
|
|
|
|
Zero => f.write_str("Z"),
|
|
|
|
NotCarry => f.write_str("NC"),
|
|
|
|
Carry => f.write_str("C"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum JumpLocation {
|
|
|
|
HL,
|
|
|
|
ImmediateWord,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for JumpLocation {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use JumpLocation::*;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
HL => f.write_str("HL"),
|
|
|
|
ImmediateWord => f.write_str("u16"),
|
|
|
|
}
|
2020-09-08 02:18:53 +00:00
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-03 00:35:48 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum AllRegisters {
|
|
|
|
Group1(Group1RegisterPair),
|
|
|
|
Register(Register),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for AllRegisters {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use AllRegisters::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Group1(rp) => write!(f, "{:?}", rp),
|
|
|
|
Register(r) => write!(f, "{:?}", r),
|
2020-09-03 00:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
mod alu {
|
|
|
|
use super::table::Register;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Source {
|
|
|
|
Register(Register),
|
|
|
|
ImmediateByte,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Source {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Source::*;
|
|
|
|
match self {
|
|
|
|
Register(r) => write!(f, "{:?}", r),
|
|
|
|
ImmediateByte => f.write_str("u8"),
|
2020-09-03 00:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
mod add {
|
|
|
|
use super::table::{Group1RegisterPair, Register};
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Target {
|
|
|
|
HL,
|
|
|
|
A,
|
|
|
|
SP,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Target {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Target::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
HL => f.write_str("HL"),
|
|
|
|
A => f.write_str("A"),
|
|
|
|
SP => f.write_str("SP"),
|
|
|
|
}
|
2020-09-03 00:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Source {
|
|
|
|
Group1(Group1RegisterPair),
|
|
|
|
Register(Register),
|
|
|
|
ImmediateSignedByte,
|
|
|
|
ImmediateByte,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Source {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Source::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Group1(rp) => write!(f, "{:?}", rp),
|
|
|
|
Register(r) => write!(f, "{:?}", r),
|
|
|
|
ImmediateSignedByte => f.write_str("i8"),
|
|
|
|
ImmediateByte => f.write_str("u8"),
|
2020-09-03 00:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
mod load {
|
|
|
|
use super::table::{Group1RegisterPair, Group2RegisterPair, Register};
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Target {
|
|
|
|
IndirectImmediateWord, // (u16)
|
|
|
|
Group1(Group1RegisterPair),
|
|
|
|
IndirectGroup2(Group2RegisterPair),
|
|
|
|
A,
|
|
|
|
Register(Register),
|
|
|
|
IoWithImmediateOffset, // 0xFF00 + offset
|
|
|
|
SP,
|
|
|
|
IoWithC, // 0xFF00 + C
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Target {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Target::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
IndirectImmediateWord => f.write_str("(u16)"),
|
|
|
|
Group1(rp) => write!(f, "{:?}", rp),
|
|
|
|
IndirectGroup2(rp) => write!(f, "({:?})", rp),
|
|
|
|
A => f.write_str("A"),
|
|
|
|
Register(r) => write!(f, "{:?}", r),
|
|
|
|
IoWithImmediateOffset => f.write_str("(0xFF00 + u8)"),
|
|
|
|
SP => f.write_str("SP"),
|
|
|
|
IoWithC => f.write_str("(0xFF00 + C)"),
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Source {
|
|
|
|
SP,
|
|
|
|
ImmediateWord, // u16
|
|
|
|
ImmediateByte, // u8
|
|
|
|
A,
|
|
|
|
IndirectGroup2(Group2RegisterPair),
|
|
|
|
Register(Register),
|
|
|
|
IoWithImmediateOffset, // 0xFF00 + offset
|
|
|
|
HL,
|
|
|
|
IoWithC,
|
|
|
|
IndirectImmediateWord, // (u16)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Source {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Source::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
SP => f.write_str("SP"),
|
|
|
|
ImmediateWord => f.write_str("u16"),
|
|
|
|
ImmediateByte => f.write_str("u8"),
|
|
|
|
A => f.write_str("A"),
|
|
|
|
IndirectGroup2(rp) => write!(f, "({:?})", rp),
|
|
|
|
Register(r) => write!(f, "{:?}", r),
|
|
|
|
IoWithImmediateOffset => f.write_str("(0xFF00 + u8)"),
|
|
|
|
HL => f.write_str("HL"),
|
|
|
|
IoWithC => f.write_str("(0xFF00 + C)"),
|
|
|
|
IndirectImmediateWord => f.write_str("(u16)"),
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-01 01:29:13 +00:00
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
mod table {
|
|
|
|
use super::add::{Source as AddSource, Target as AddTarget};
|
|
|
|
use super::alu::Source as AluSource;
|
|
|
|
use super::{Instruction, JumpCondition};
|
|
|
|
use crate::cpu::{Register as CpuRegister, RegisterPair};
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Group1RegisterPair {
|
|
|
|
BC,
|
|
|
|
DE,
|
|
|
|
HL,
|
|
|
|
SP,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Group1RegisterPair {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Group1RegisterPair::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BC => f.write_str("BC"),
|
|
|
|
DE => f.write_str("DE"),
|
|
|
|
HL => f.write_str("HL"),
|
|
|
|
SP => f.write_str("SP"),
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl Group1RegisterPair {
|
|
|
|
pub fn as_register_pair(&self) -> RegisterPair {
|
|
|
|
use Group1RegisterPair::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BC => RegisterPair::BC,
|
|
|
|
DE => RegisterPair::DE,
|
|
|
|
HL => RegisterPair::HL,
|
|
|
|
SP => RegisterPair::SP,
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Group2RegisterPair {
|
|
|
|
BC,
|
|
|
|
DE,
|
|
|
|
IncrementHL,
|
|
|
|
DecrementHL,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Group2RegisterPair {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Group2RegisterPair::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BC => f.write_str("BC"),
|
|
|
|
DE => f.write_str("DE"),
|
|
|
|
IncrementHL => f.write_str("HL+"),
|
|
|
|
DecrementHL => f.write_str("HL-"),
|
|
|
|
}
|
2020-08-30 04:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl Group2RegisterPair {
|
|
|
|
pub fn as_register_pair(&self) -> RegisterPair {
|
|
|
|
use Group2RegisterPair::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BC => RegisterPair::BC,
|
|
|
|
DE => RegisterPair::DE,
|
|
|
|
IncrementHL => RegisterPair::HL,
|
|
|
|
DecrementHL => RegisterPair::HL,
|
|
|
|
}
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
2020-09-08 02:18:53 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Group3RegisterPair {
|
|
|
|
BC,
|
|
|
|
DE,
|
|
|
|
HL,
|
|
|
|
AF,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Group3RegisterPair {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Group3RegisterPair::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BC => f.write_str("BC"),
|
|
|
|
DE => f.write_str("DE"),
|
|
|
|
HL => f.write_str("HL"),
|
|
|
|
AF => f.write_str("AF"),
|
|
|
|
}
|
2020-09-08 02:18:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl Group3RegisterPair {
|
|
|
|
pub fn as_register_pair(&self) -> RegisterPair {
|
|
|
|
use Group3RegisterPair::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BC => RegisterPair::BC,
|
|
|
|
DE => RegisterPair::DE,
|
|
|
|
HL => RegisterPair::HL,
|
|
|
|
AF => RegisterPair::AF,
|
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) enum Register {
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
D,
|
|
|
|
E,
|
|
|
|
H,
|
|
|
|
L,
|
|
|
|
IndirectHL,
|
|
|
|
A,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Register {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Register::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
B => f.write_str("B"),
|
|
|
|
C => f.write_str("C"),
|
|
|
|
D => f.write_str("D"),
|
|
|
|
E => f.write_str("E"),
|
|
|
|
H => f.write_str("H"),
|
|
|
|
L => f.write_str("L"),
|
|
|
|
IndirectHL => f.write_str("(HL)"),
|
|
|
|
A => f.write_str("A"),
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 08:22:45 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl Register {
|
|
|
|
pub fn cpu_register(&self) -> CpuRegister {
|
|
|
|
use Register::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
B => CpuRegister::B,
|
|
|
|
C => CpuRegister::C,
|
|
|
|
D => CpuRegister::D,
|
|
|
|
E => CpuRegister::E,
|
|
|
|
H => CpuRegister::H,
|
|
|
|
L => CpuRegister::L,
|
|
|
|
A => CpuRegister::A,
|
|
|
|
IndirectHL => panic!("Register::HL doesn't map onto CpuRegister"),
|
|
|
|
}
|
2021-01-18 08:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 08:29:35 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn group1(code: u8) -> Group1RegisterPair {
|
|
|
|
use Group1RegisterPair::*;
|
|
|
|
|
|
|
|
match code {
|
|
|
|
0b00 => BC,
|
|
|
|
0b01 => DE,
|
|
|
|
0b10 => HL,
|
|
|
|
0b11 => SP,
|
|
|
|
_ => unreachable!("{:#04X} is not a valid Group 1 Register Pair", code),
|
2021-01-18 08:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 08:47:41 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn group2(code: u8) -> Group2RegisterPair {
|
|
|
|
use Group2RegisterPair::*;
|
|
|
|
|
|
|
|
match code {
|
|
|
|
0b00 => BC,
|
|
|
|
0b01 => DE,
|
|
|
|
0b10 => IncrementHL,
|
|
|
|
0b11 => DecrementHL,
|
|
|
|
_ => unreachable!("{:#04X} is not a valid Group 2 Register Pair", code),
|
2021-01-18 08:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn group3(code: u8) -> Group3RegisterPair {
|
|
|
|
use Group3RegisterPair::*;
|
2021-06-02 06:50:16 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match code {
|
|
|
|
0b00 => BC,
|
|
|
|
0b01 => DE,
|
|
|
|
0b10 => HL,
|
|
|
|
0b11 => AF,
|
|
|
|
_ => unreachable!("{:#04X} is not a valid Group 3 Register Pair", code),
|
2021-06-02 06:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn register(code: u8) -> Register {
|
|
|
|
use Register::*;
|
|
|
|
|
|
|
|
match code {
|
|
|
|
0b000 => B,
|
|
|
|
0b001 => C,
|
|
|
|
0b010 => D,
|
|
|
|
0b011 => E,
|
|
|
|
0b100 => H,
|
|
|
|
0b101 => L,
|
|
|
|
0b110 => IndirectHL,
|
|
|
|
0b111 => A,
|
|
|
|
_ => unreachable!("{:#04X} is not a valid Register"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn jump_cond(code: u8) -> JumpCondition {
|
2021-06-02 06:50:16 +00:00
|
|
|
use JumpCondition::*;
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match code {
|
|
|
|
0b00 => NotZero,
|
|
|
|
0b01 => Zero,
|
|
|
|
0b10 => NotCarry,
|
|
|
|
0b11 => Carry,
|
|
|
|
_ => unreachable!("{:#04X} is not a valid JumpCondition", code),
|
2021-06-02 06:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn flag_instr(code: u8) -> Instruction {
|
|
|
|
use Instruction::*;
|
|
|
|
|
|
|
|
match code {
|
|
|
|
0b000 => RLCA,
|
|
|
|
0b001 => RRCA,
|
|
|
|
0b010 => RLA,
|
|
|
|
0b011 => RRA,
|
|
|
|
0b100 => DAA,
|
|
|
|
0b101 => CPL,
|
|
|
|
0b110 => SCF,
|
|
|
|
0b111 => CCF,
|
|
|
|
_ => unreachable!("{:#04X} is not a valid flag opcode code", code),
|
2021-06-02 06:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn alu_reg_instr(alu_code: u8, reg_code: u8) -> Instruction {
|
|
|
|
use Instruction::*;
|
|
|
|
|
|
|
|
match alu_code {
|
|
|
|
0b000 => ADD(AddTarget::A, AddSource::Register(register(reg_code))),
|
|
|
|
0b001 => ADC(AluSource::Register(register(reg_code))),
|
|
|
|
0b010 => SUB(AluSource::Register(register(reg_code))),
|
|
|
|
0b011 => SBC(AluSource::Register(register(reg_code))),
|
|
|
|
0b100 => AND(AluSource::Register(register(reg_code))),
|
|
|
|
0b101 => XOR(AluSource::Register(register(reg_code))),
|
|
|
|
0b110 => OR(AluSource::Register(register(reg_code))),
|
|
|
|
0b111 => CP(AluSource::Register(register(reg_code))),
|
|
|
|
_ => unreachable!("{:#04X} is not a valid alu reg instruction code", alu_code),
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn alu_imm_instr(code: u8) -> Instruction {
|
|
|
|
use Instruction::*;
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match code {
|
|
|
|
0b000 => ADD(AddTarget::A, AddSource::ImmediateByte),
|
|
|
|
0b001 => ADC(AluSource::ImmediateByte),
|
|
|
|
0b010 => SUB(AluSource::ImmediateByte),
|
|
|
|
0b011 => SBC(AluSource::ImmediateByte),
|
|
|
|
0b100 => AND(AluSource::ImmediateByte),
|
|
|
|
0b101 => XOR(AluSource::ImmediateByte),
|
|
|
|
0b110 => OR(AluSource::ImmediateByte),
|
|
|
|
0b111 => CP(AluSource::ImmediateByte),
|
|
|
|
_ => unreachable!("{:#04X} is not a valid alu imm instruction code", code),
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) fn prefix_alu(alu_code: u8, reg_code: u8) -> Instruction {
|
|
|
|
use Instruction::*;
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
match alu_code {
|
|
|
|
0b000 => RLC(register(reg_code)),
|
|
|
|
0b001 => RRC(register(reg_code)),
|
|
|
|
0b010 => RL(register(reg_code)),
|
|
|
|
0b011 => RR(register(reg_code)),
|
|
|
|
0b100 => SLA(register(reg_code)),
|
|
|
|
0b101 => SRA(register(reg_code)),
|
|
|
|
0b110 => SWAP(register(reg_code)),
|
|
|
|
0b111 => SRL(register(reg_code)),
|
|
|
|
_ => unreachable!("{:#04X} is not a valid pfx alu instruction code", alu_code),
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
pub(crate) mod cycle {
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct Cycle(u32);
|
|
|
|
|
|
|
|
impl Cycle {
|
|
|
|
pub const fn new(num: u32) -> Self {
|
|
|
|
Self(num)
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Add for Cycle {
|
|
|
|
type Output = Self;
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn add(self, rhs: Self) -> Self::Output {
|
|
|
|
Self(self.0 + rhs.0)
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Add<u32> for Cycle {
|
|
|
|
type Output = Self;
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn add(self, rhs: u32) -> Self::Output {
|
|
|
|
Self(self.0 + rhs)
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::AddAssign for Cycle {
|
|
|
|
fn add_assign(&mut self, rhs: Self) {
|
|
|
|
*self = Self(self.0 + rhs.0);
|
|
|
|
}
|
2021-01-19 06:29:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::AddAssign<u32> for Cycle {
|
|
|
|
fn add_assign(&mut self, rhs: u32) {
|
|
|
|
*self = Self(self.0 + rhs);
|
|
|
|
}
|
2021-01-19 06:29:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Rem for Cycle {
|
|
|
|
type Output = Self;
|
2021-03-21 08:03:03 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn rem(self, rhs: Self) -> Self::Output {
|
|
|
|
Self(self.0 % rhs.0)
|
|
|
|
}
|
2021-03-21 08:03:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Rem<u32> for Cycle {
|
|
|
|
type Output = Self;
|
2021-03-21 08:03:03 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn rem(self, rhs: u32) -> Self::Output {
|
|
|
|
Self(self.0 % rhs)
|
|
|
|
}
|
2021-03-21 08:03:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::RemAssign for Cycle {
|
|
|
|
fn rem_assign(&mut self, rhs: Self) {
|
|
|
|
*self = Self(self.0 % rhs.0);
|
|
|
|
}
|
2021-03-21 08:03:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::RemAssign<u32> for Cycle {
|
|
|
|
fn rem_assign(&mut self, rhs: u32) {
|
|
|
|
*self = Self(self.0 % rhs);
|
|
|
|
}
|
2021-03-21 08:03:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Sub for Cycle {
|
|
|
|
type Output = Cycle;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
|
|
|
Self(self.0 - rhs.0)
|
|
|
|
}
|
2021-06-04 18:47:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Sub<u32> for Cycle {
|
|
|
|
type Output = Cycle;
|
2021-06-04 18:47:06 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
fn sub(self, rhs: u32) -> Self::Output {
|
|
|
|
Self(self.0 - rhs)
|
|
|
|
}
|
2021-06-04 18:47:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::SubAssign for Cycle {
|
|
|
|
fn sub_assign(&mut self, rhs: Self) {
|
|
|
|
*self = Self(self.0 - rhs.0);
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 18:47:06 +00:00
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::SubAssign<u32> for Cycle {
|
|
|
|
fn sub_assign(&mut self, rhs: u32) {
|
|
|
|
*self = Self(self.0 - rhs);
|
|
|
|
}
|
2021-06-04 18:47:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl PartialEq<u32> for Cycle {
|
|
|
|
fn eq(&self, other: &u32) -> bool {
|
|
|
|
self.0 == *other
|
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Div for Cycle {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn div(self, rhs: Self) -> Self::Output {
|
|
|
|
Self::new(self.0 / rhs.0)
|
|
|
|
}
|
2021-01-19 06:29:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl std::ops::Div<u32> for Cycle {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn div(self, rhs: u32) -> Self::Output {
|
|
|
|
Self::new(self.0 / rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u32> for Cycle {
|
|
|
|
fn from(num: u32) -> Self {
|
|
|
|
Self(num)
|
|
|
|
}
|
2021-03-23 02:48:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 01:29:13 +00:00
|
|
|
impl From<Cycle> for u32 {
|
|
|
|
fn from(cycles: Cycle) -> Self {
|
|
|
|
cycles.0
|
|
|
|
}
|
2021-03-23 02:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:54:38 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-03-27 17:10:18 +00:00
|
|
|
use super::Cycle;
|
2021-01-19 04:54:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cycle_add_works() {
|
2021-03-27 17:10:18 +00:00
|
|
|
let lhs: Cycle = Cycle::new(5);
|
|
|
|
let rhs: Cycle = Cycle::new(4);
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-03-27 17:10:18 +00:00
|
|
|
assert_eq!(Cycle::new(9), rhs + lhs);
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cycle_add_assign_works() {
|
2021-03-27 17:10:18 +00:00
|
|
|
let mut cycles: Cycle = Cycle::new(5);
|
2021-01-19 04:54:38 +00:00
|
|
|
cycles += 5;
|
|
|
|
|
2021-03-27 17:10:18 +00:00
|
|
|
assert_eq!(Cycle::new(10), cycles);
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
|
|
|
}
|