2017-06-04 21:38:34 +01:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Intel8080.h"
|
|
|
|
|
2017-09-06 13:22:23 +01:00
|
|
|
EightBit::Intel8080::Intel8080(Bus& bus, InputOutput& ports)
|
|
|
|
: IntelProcessor(bus),
|
2017-06-16 01:58:12 +01:00
|
|
|
m_ports(ports) {
|
2017-06-04 21:38:34 +01:00
|
|
|
}
|
|
|
|
|
2017-11-05 12:47:42 +00:00
|
|
|
EightBit::register16_t& EightBit::Intel8080::AF() {
|
|
|
|
auto& f = af.low;
|
|
|
|
f = (f | Bit1) & ~(Bit5 | Bit3);
|
|
|
|
return af;
|
|
|
|
}
|
|
|
|
|
|
|
|
EightBit::register16_t& EightBit::Intel8080::BC() {
|
|
|
|
return bc;
|
|
|
|
}
|
|
|
|
|
|
|
|
EightBit::register16_t& EightBit::Intel8080::DE() {
|
|
|
|
return de;
|
|
|
|
}
|
|
|
|
|
|
|
|
EightBit::register16_t& EightBit::Intel8080::HL() {
|
|
|
|
return hl;
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:41:49 +00:00
|
|
|
void EightBit::Intel8080::reset() {
|
|
|
|
IntelProcessor::reset();
|
|
|
|
di();
|
|
|
|
}
|
|
|
|
|
2017-07-22 23:52:58 +01:00
|
|
|
void EightBit::Intel8080::di() {
|
2017-12-02 23:50:59 +00:00
|
|
|
m_interruptEnable = false;
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Intel8080::ei() {
|
2017-12-02 23:50:59 +00:00
|
|
|
m_interruptEnable = true;
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Intel8080::increment(uint8_t& f, uint8_t& operand) {
|
|
|
|
adjustSZP<Intel8080>(f, ++operand);
|
|
|
|
clearFlag(f, AC, lowNibble(operand));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Intel8080::decrement(uint8_t& f, uint8_t& operand) {
|
|
|
|
adjustSZP<Intel8080>(f, --operand);
|
|
|
|
setFlag(f, AC, lowNibble(operand) != Mask4);
|
|
|
|
}
|
|
|
|
|
2017-07-24 22:00:49 +01:00
|
|
|
bool EightBit::Intel8080::jumpConditionalFlag(uint8_t& f, int flag) {
|
|
|
|
switch (flag) {
|
|
|
|
case 0: // NZ
|
|
|
|
return jumpConditional(!(f & ZF));
|
|
|
|
case 1: // Z
|
|
|
|
return jumpConditional(f & ZF);
|
|
|
|
case 2: // NC
|
|
|
|
return jumpConditional(!(f & CF));
|
|
|
|
case 3: // C
|
|
|
|
return jumpConditional(f & CF);
|
|
|
|
case 4: // PO
|
|
|
|
return jumpConditional(!(f & PF));
|
|
|
|
case 5: // PE
|
|
|
|
return jumpConditional(f & PF);
|
|
|
|
case 6: // P
|
|
|
|
return jumpConditional(!(f & SF));
|
|
|
|
case 7: // M
|
|
|
|
return jumpConditional(f & SF);
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EightBit::Intel8080::returnConditionalFlag(uint8_t& f, int flag) {
|
|
|
|
switch (flag) {
|
|
|
|
case 0: // NZ
|
|
|
|
return returnConditional(!(f & ZF));
|
|
|
|
case 1: // Z
|
|
|
|
return returnConditional(f & ZF);
|
|
|
|
case 2: // NC
|
|
|
|
return returnConditional(!(f & CF));
|
|
|
|
case 3: // C
|
|
|
|
return returnConditional(f & CF);
|
|
|
|
case 4: // PO
|
|
|
|
return returnConditional(!(f & PF));
|
|
|
|
case 5: // PE
|
|
|
|
return returnConditional(f & PF);
|
|
|
|
case 6: // P
|
|
|
|
return returnConditional(!(f & SF));
|
|
|
|
case 7: // M
|
|
|
|
return returnConditional(f & SF);
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EightBit::Intel8080::callConditionalFlag(uint8_t& f, int flag) {
|
|
|
|
switch (flag) {
|
|
|
|
case 0: // NZ
|
|
|
|
return callConditional(!(f & ZF));
|
|
|
|
case 1: // Z
|
|
|
|
return callConditional(f & ZF);
|
|
|
|
case 2: // NC
|
|
|
|
return callConditional(!(f & CF));
|
|
|
|
case 3: // C
|
|
|
|
return callConditional(f & CF);
|
|
|
|
case 4: // PO
|
|
|
|
return callConditional(!(f & PF));
|
|
|
|
case 5: // PE
|
|
|
|
return callConditional(f & PF);
|
|
|
|
case 6: // P
|
|
|
|
return callConditional(!(f & SF));
|
|
|
|
case 7: // M
|
|
|
|
return callConditional(f & SF);
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:26:21 +01:00
|
|
|
void EightBit::Intel8080::add(uint8_t& f, register16_t& operand, register16_t value) {
|
|
|
|
const auto result = operand.word + value.word;
|
|
|
|
operand.word = result;
|
2017-11-03 22:05:01 +00:00
|
|
|
setFlag(f, CF, result & Bit16);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:26:21 +01:00
|
|
|
void EightBit::Intel8080::add(uint8_t& f, uint8_t& operand, uint8_t value, int carry) {
|
|
|
|
|
|
|
|
register16_t result;
|
|
|
|
result.word = operand + value + carry;
|
|
|
|
|
|
|
|
adjustAuxiliaryCarryAdd(f, operand, value, result.word);
|
|
|
|
|
|
|
|
operand = result.low;
|
|
|
|
|
|
|
|
setFlag(f, CF, result.word & Bit8);
|
|
|
|
adjustSZP<Intel8080>(f, operand);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:26:21 +01:00
|
|
|
void EightBit::Intel8080::adc(uint8_t& f, uint8_t& operand, uint8_t value) {
|
|
|
|
add(f, operand, value, f & CF);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Intel8080::subtract(uint8_t& f, uint8_t& operand, uint8_t value, int carry) {
|
|
|
|
|
|
|
|
register16_t result;
|
|
|
|
result.word = operand - value - carry;
|
|
|
|
|
|
|
|
adjustAuxiliaryCarrySub(f, operand, value, result.word);
|
|
|
|
|
|
|
|
operand = result.low;
|
|
|
|
|
|
|
|
setFlag(f, CF, result.word & Bit8);
|
|
|
|
adjustSZP<Intel8080>(f, operand);
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:26:21 +01:00
|
|
|
void EightBit::Intel8080::sbb(uint8_t& f, uint8_t& operand, uint8_t value) {
|
|
|
|
subtract(f, operand, value, f & CF);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:26:21 +01:00
|
|
|
void EightBit::Intel8080::andr(uint8_t& f, uint8_t& operand, uint8_t value) {
|
|
|
|
setFlag(f, AC, (operand | value) & Bit3);
|
2017-07-22 23:52:58 +01:00
|
|
|
clearFlag(f, CF);
|
2017-07-25 19:26:21 +01:00
|
|
|
adjustSZP<Intel8080>(f, operand &= value);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:26:21 +01:00
|
|
|
void EightBit::Intel8080::xorr(uint8_t& f, uint8_t& operand, uint8_t value) {
|
2017-07-22 23:52:58 +01:00
|
|
|
clearFlag(f, AC | CF);
|
2017-07-25 19:26:21 +01:00
|
|
|
adjustSZP<Intel8080>(f, operand ^= value);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:26:21 +01:00
|
|
|
void EightBit::Intel8080::orr(uint8_t& f, uint8_t& operand, uint8_t value) {
|
2017-07-22 23:52:58 +01:00
|
|
|
clearFlag(f, AC | CF);
|
2017-07-25 19:26:21 +01:00
|
|
|
adjustSZP<Intel8080>(f, operand |= value);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Intel8080::compare(uint8_t& f, uint8_t check, uint8_t value) {
|
|
|
|
subtract(f, check, value);
|
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::rlc(uint8_t& f, uint8_t& operand) {
|
|
|
|
auto carry = operand & Bit7;
|
|
|
|
operand = (operand << 1) | (carry >> 7);
|
|
|
|
setFlag(f, CF, carry);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::rrc(uint8_t& f, uint8_t& operand) {
|
|
|
|
auto carry = operand & Bit0;
|
|
|
|
operand = (operand >> 1) | (carry << 7);
|
|
|
|
setFlag(f, CF, carry);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::rl(uint8_t& f, uint8_t& operand) {
|
2017-07-22 23:52:58 +01:00
|
|
|
const auto carry = f & CF;
|
2017-07-25 21:22:15 +01:00
|
|
|
setFlag(f, CF, operand & Bit7);
|
|
|
|
operand = (operand << 1) | carry;
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::rr(uint8_t& f, uint8_t& operand) {
|
2017-07-22 23:52:58 +01:00
|
|
|
const auto carry = f & CF;
|
2017-07-25 21:22:15 +01:00
|
|
|
setFlag(f, CF, operand & Bit0);
|
|
|
|
operand = (operand >> 1) | (carry << 7);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::daa(uint8_t& a, uint8_t& f) {
|
|
|
|
const auto& before = a;
|
2017-07-22 23:52:58 +01:00
|
|
|
auto carry = f & CF;
|
|
|
|
uint8_t addition = 0;
|
2017-07-25 21:22:15 +01:00
|
|
|
if ((f & AC) || lowNibble(before) > 9) {
|
2017-07-22 23:52:58 +01:00
|
|
|
addition = 0x6;
|
|
|
|
}
|
2017-07-25 21:22:15 +01:00
|
|
|
if ((f & CF) || highNibble(before) > 9 || (highNibble(before) >= 9 && lowNibble(before) > 9)) {
|
2017-07-22 23:52:58 +01:00
|
|
|
addition |= 0x60;
|
|
|
|
carry = true;
|
|
|
|
}
|
2017-07-25 21:22:15 +01:00
|
|
|
add(f, a, addition);
|
2017-07-22 23:52:58 +01:00
|
|
|
setFlag(f, CF, carry);
|
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::cma(uint8_t& a, uint8_t& f) {
|
|
|
|
a = ~a;
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::stc(uint8_t& a, uint8_t& f) {
|
|
|
|
setFlag(f, CF);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:22:15 +01:00
|
|
|
void EightBit::Intel8080::cmc(uint8_t& a, uint8_t& f) {
|
|
|
|
clearFlag(f, CF, f & CF);
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
void EightBit::Intel8080::xhtl(register16_t& operand) {
|
2018-03-10 01:53:57 +00:00
|
|
|
MEMPTR().low = BUS().read(SP());
|
|
|
|
BUS().write(operand.low);
|
2017-11-03 22:05:01 +00:00
|
|
|
operand.low = MEMPTR().low;
|
2018-03-12 01:22:28 +00:00
|
|
|
++BUS().ADDRESS().word;
|
2018-03-10 01:53:57 +00:00
|
|
|
MEMPTR().high = BUS().read();
|
|
|
|
BUS().write(operand.high);
|
2017-11-03 22:05:01 +00:00
|
|
|
operand.high = MEMPTR().high;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Intel8080::writePort(uint8_t port, uint8_t data) {
|
|
|
|
BUS().ADDRESS().low = port;
|
|
|
|
BUS().ADDRESS().high = data;
|
|
|
|
BUS().placeDATA(data);
|
|
|
|
writePort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Intel8080::writePort() {
|
|
|
|
m_ports.write(BUS().ADDRESS().low, BUS().DATA());
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
void EightBit::Intel8080::readPort(uint8_t port, uint8_t& a) {
|
|
|
|
BUS().ADDRESS().low = port;
|
|
|
|
BUS().ADDRESS().high = a;
|
|
|
|
readPort();
|
|
|
|
a = BUS().DATA();
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
void EightBit::Intel8080::readPort() {
|
|
|
|
BUS().placeDATA(m_ports.read(BUS().ADDRESS().low));
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:38:34 +01:00
|
|
|
int EightBit::Intel8080::step() {
|
|
|
|
ExecutingInstruction.fire(*this);
|
2017-11-03 22:05:01 +00:00
|
|
|
resetCycles();
|
2017-12-02 23:50:59 +00:00
|
|
|
if (LIKELY(powered())) {
|
2017-12-10 21:41:48 +00:00
|
|
|
if (UNLIKELY(lowered(INT()))) {
|
|
|
|
raise(HALT());
|
|
|
|
raise(INT());
|
2017-12-04 23:20:55 +00:00
|
|
|
if (m_interruptEnable) {
|
|
|
|
di();
|
|
|
|
return execute(BUS().DATA());
|
|
|
|
}
|
2017-12-02 23:50:59 +00:00
|
|
|
}
|
2017-12-10 21:41:48 +00:00
|
|
|
if (UNLIKELY(lowered(HALT())))
|
|
|
|
return execute(0); // NOP
|
2017-12-04 23:20:55 +00:00
|
|
|
return execute(fetchByte());
|
2017-12-02 23:50:59 +00:00
|
|
|
}
|
2017-12-03 00:57:47 +00:00
|
|
|
return cycles();
|
2017-06-04 21:38:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int EightBit::Intel8080::execute(uint8_t opcode) {
|
2017-07-24 22:00:49 +01:00
|
|
|
|
|
|
|
const auto& decoded = getDecodedOpcode(opcode);
|
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
const auto x = decoded.x;
|
|
|
|
const auto y = decoded.y;
|
|
|
|
const auto z = decoded.z;
|
2017-07-24 22:00:49 +01:00
|
|
|
|
2017-11-03 22:05:01 +00:00
|
|
|
const auto p = decoded.p;
|
|
|
|
const auto q = decoded.q;
|
2017-07-24 22:00:49 +01:00
|
|
|
|
2018-01-10 23:08:14 +00:00
|
|
|
auto& af = AF();
|
|
|
|
auto& a = af.high;
|
|
|
|
auto& f = af.low;
|
|
|
|
|
|
|
|
execute(a, f, x, y, z, p, q);
|
2017-07-24 22:00:49 +01:00
|
|
|
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(cycles() > 0);
|
2017-11-03 22:05:01 +00:00
|
|
|
return cycles();
|
2017-06-04 21:38:34 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 23:08:14 +00:00
|
|
|
void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, int p, int q) {
|
2017-07-24 22:00:49 +01:00
|
|
|
switch (x) {
|
|
|
|
case 0:
|
|
|
|
switch (z) {
|
|
|
|
case 0: // Relative jumps and assorted ops
|
|
|
|
switch (y) {
|
|
|
|
case 0: // NOP
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // 16-bit load immediate/add
|
|
|
|
switch (q) {
|
|
|
|
case 0: // LD rp,nn
|
2018-02-25 19:48:01 +00:00
|
|
|
RP(p) = fetchWord();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1: // ADD HL,rp
|
2017-07-25 19:26:21 +01:00
|
|
|
add(f, HL(), RP(p));
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(11);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
2017-12-10 21:41:48 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // Indirect loading
|
|
|
|
switch (q) {
|
|
|
|
case 0:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // LD (BC),A
|
2018-03-10 01:53:57 +00:00
|
|
|
BUS().write(BC(), a);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1: // LD (DE),A
|
2018-03-10 01:53:57 +00:00
|
|
|
BUS().write(DE(), a);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2: // LD (nn),HL
|
2018-02-25 19:48:01 +00:00
|
|
|
MEMPTR() = fetchWord();
|
2018-01-10 23:08:14 +00:00
|
|
|
setWord(HL());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(16);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3: // LD (nn),A
|
2018-03-10 01:53:57 +00:00
|
|
|
BUS().ADDRESS() = fetchWord();
|
|
|
|
BUS().write(a);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(13);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // LD A,(BC)
|
2018-03-10 01:53:57 +00:00
|
|
|
a = BUS().read(BC());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1: // LD A,(DE)
|
2018-03-10 01:53:57 +00:00
|
|
|
a = BUS().read(DE());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2: // LD HL,(nn)
|
2018-02-25 19:48:01 +00:00
|
|
|
MEMPTR() = fetchWord();
|
|
|
|
HL() = getWord();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(16);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3: // LD A,(nn)
|
2018-03-10 01:53:57 +00:00
|
|
|
BUS().ADDRESS() = fetchWord();
|
|
|
|
a = BUS().read();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(13);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // 16-bit INC/DEC
|
|
|
|
switch (q) {
|
|
|
|
case 0: // INC rp
|
|
|
|
++RP(p).word;
|
|
|
|
break;
|
|
|
|
case 1: // DEC rp
|
|
|
|
--RP(p).word;
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(6);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
2017-08-06 17:06:48 +01:00
|
|
|
case 4: { // 8-bit INC
|
2017-11-03 22:05:01 +00:00
|
|
|
auto operand = R(y, a);
|
2017-08-06 17:06:48 +01:00
|
|
|
increment(f, operand);
|
2017-11-03 22:05:01 +00:00
|
|
|
R(y, a, operand);
|
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
2017-08-06 17:06:48 +01:00
|
|
|
} case 5: { // 8-bit DEC
|
2017-11-03 22:05:01 +00:00
|
|
|
auto operand = R(y, a);
|
2017-08-06 17:06:48 +01:00
|
|
|
decrement(f, operand);
|
2017-11-03 22:05:01 +00:00
|
|
|
R(y, a, operand);
|
|
|
|
addCycles(4);
|
2017-12-05 21:40:23 +00:00
|
|
|
if (UNLIKELY(y == 6))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
2017-08-06 17:06:48 +01:00
|
|
|
} case 6: // 8-bit load immediate
|
2017-11-03 22:05:01 +00:00
|
|
|
R(y, a, fetchByte());
|
|
|
|
addCycles(7);
|
2017-12-05 21:40:23 +00:00
|
|
|
if (UNLIKELY(y == 6))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(3);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 7: // Assorted operations on accumulator/flags
|
|
|
|
switch (y) {
|
|
|
|
case 0:
|
2017-07-25 21:22:15 +01:00
|
|
|
rlc(f, a);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2017-07-25 21:22:15 +01:00
|
|
|
rrc(f, a);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2017-07-25 21:22:15 +01:00
|
|
|
rl(f, a);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2017-07-25 21:22:15 +01:00
|
|
|
rr(f, a);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
2017-07-25 21:22:15 +01:00
|
|
|
daa(a, f);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 5:
|
2017-07-25 21:22:15 +01:00
|
|
|
cma(a, f);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 6:
|
2017-07-25 21:22:15 +01:00
|
|
|
stc(a, f);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 7:
|
2017-07-25 21:22:15 +01:00
|
|
|
cmc(a, f);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // 8-bit loading
|
2017-12-05 21:40:23 +00:00
|
|
|
if (UNLIKELY(z == 6 && y == 6)) { // Exception (replaces LD (HL), (HL))
|
2017-07-24 22:00:49 +01:00
|
|
|
halt();
|
|
|
|
} else {
|
2017-11-03 22:05:01 +00:00
|
|
|
R(y, a, R(z, a));
|
2017-12-05 21:40:23 +00:00
|
|
|
if (UNLIKELY((y == 6) || (z == 6))) // M operations
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(3);
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2: // Operate on accumulator and register/memory location
|
|
|
|
switch (y) {
|
|
|
|
case 0: // ADD A,r
|
2017-11-03 22:05:01 +00:00
|
|
|
add(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1: // ADC A,r
|
2017-11-03 22:05:01 +00:00
|
|
|
adc(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2: // SUB r
|
2017-11-03 22:05:01 +00:00
|
|
|
subtract(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3: // SBC A,r
|
2017-11-03 22:05:01 +00:00
|
|
|
sbb(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 4: // AND r
|
2017-11-03 22:05:01 +00:00
|
|
|
andr(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 5: // XOR r
|
2017-11-03 22:05:01 +00:00
|
|
|
xorr(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 6: // OR r
|
2017-11-03 22:05:01 +00:00
|
|
|
orr(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 7: // CP r
|
2017-11-03 22:05:01 +00:00
|
|
|
compare(f, a, R(z, a));
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-12-05 21:40:23 +00:00
|
|
|
if (UNLIKELY(z == 6))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(3);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
switch (z) {
|
|
|
|
case 0: // Conditional return
|
|
|
|
if (returnConditionalFlag(f, y))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(6);
|
|
|
|
addCycles(5);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1: // POP & various ops
|
|
|
|
switch (q) {
|
|
|
|
case 0: // POP rp2[p]
|
2018-02-25 19:48:01 +00:00
|
|
|
RP2(p) = popWord();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // RET
|
|
|
|
ret();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2: // JP HL
|
|
|
|
PC() = HL();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3: // LD SP,HL
|
|
|
|
SP() = HL();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // Conditional jump
|
|
|
|
jumpConditionalFlag(f, y);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3: // Assorted operations
|
|
|
|
switch (y) {
|
|
|
|
case 0: // JP nn
|
2018-02-25 19:48:01 +00:00
|
|
|
MEMPTR() = fetchWord();
|
2017-07-24 22:00:49 +01:00
|
|
|
jump();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2: // OUT (n),A
|
2017-11-03 22:05:01 +00:00
|
|
|
writePort(fetchByte(), a);
|
|
|
|
addCycles(11);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 3: // IN A,(n)
|
2017-11-03 22:05:01 +00:00
|
|
|
readPort(fetchByte(), a);
|
|
|
|
addCycles(11);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 4: // EX (SP),HL
|
2017-11-03 22:05:01 +00:00
|
|
|
xhtl(HL());
|
|
|
|
addCycles(19);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 5: // EX DE,HL
|
|
|
|
std::swap(DE(), HL());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 6: // DI
|
|
|
|
di();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 7: // EI
|
|
|
|
ei();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4: // Conditional call: CALL cc[y], nn
|
|
|
|
if (callConditionalFlag(f, y))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
|
|
|
addCycles(10);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 5: // PUSH & various ops
|
|
|
|
switch (q) {
|
|
|
|
case 0: // PUSH rp2[p]
|
|
|
|
pushWord(RP2(p));
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(11);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // CALL nn
|
2018-02-25 19:48:01 +00:00
|
|
|
MEMPTR() = fetchWord();
|
2017-07-24 22:00:49 +01:00
|
|
|
call();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(17);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6: // Operate on accumulator and immediate operand: alu[y] n
|
|
|
|
switch (y) {
|
|
|
|
case 0: // ADD A,n
|
2017-07-25 19:26:21 +01:00
|
|
|
add(f, a, fetchByte());
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 1: // ADC A,n
|
2017-07-25 19:26:21 +01:00
|
|
|
adc(f, a, fetchByte());
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 2: // SUB n
|
|
|
|
subtract(f, a, fetchByte());
|
|
|
|
break;
|
|
|
|
case 3: // SBC A,n
|
2017-07-25 19:26:21 +01:00
|
|
|
sbb(f, a, fetchByte());
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 4: // AND n
|
2017-07-25 19:26:21 +01:00
|
|
|
andr(f, a, fetchByte());
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 5: // XOR n
|
2017-07-25 19:26:21 +01:00
|
|
|
xorr(f, a, fetchByte());
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 6: // OR n
|
2017-07-25 19:26:21 +01:00
|
|
|
orr(f, a, fetchByte());
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 7: // CP n
|
|
|
|
compare(f, a, fetchByte());
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
case 7: // Restart: RST y * 8
|
|
|
|
restart(y << 3);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(11);
|
2017-07-24 22:00:49 +01:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-03 21:30:46 +01:00
|
|
|
UNREACHABLE;
|
2017-07-24 22:00:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-07-22 23:52:58 +01:00
|
|
|
}
|