2017-06-04 20:38:34 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Z80.h"
|
|
|
|
|
|
|
|
// based on http://www.z80.info/decoding.htm
|
2017-08-30 22:17:34 +00:00
|
|
|
|
2017-09-07 00:04:09 +00:00
|
|
|
EightBit::Z80::Z80(Bus& bus, InputOutput& ports)
|
|
|
|
: IntelProcessor(bus),
|
2017-11-10 22:41:50 +00:00
|
|
|
m_ports(ports) {
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 14:48:15 +00:00
|
|
|
EightBit::register16_t& EightBit::Z80::AF() {
|
|
|
|
return m_accumulatorFlags[m_accumulatorFlagsSet];
|
|
|
|
}
|
|
|
|
|
|
|
|
EightBit::register16_t& EightBit::Z80::BC() {
|
|
|
|
return m_registers[m_registerSet][BC_IDX];
|
|
|
|
}
|
|
|
|
|
|
|
|
EightBit::register16_t& EightBit::Z80::DE() {
|
|
|
|
return m_registers[m_registerSet][DE_IDX];
|
|
|
|
}
|
|
|
|
|
|
|
|
EightBit::register16_t& EightBit::Z80::HL() {
|
|
|
|
return m_registers[m_registerSet][HL_IDX];
|
|
|
|
}
|
|
|
|
|
2018-08-25 00:34:30 +00:00
|
|
|
void EightBit::Z80::powerOn() {
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-08-25 00:34:30 +00:00
|
|
|
IntelProcessor::powerOn();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-08-25 21:50:18 +00:00
|
|
|
raise(NMI());
|
2017-12-10 21:41:48 +00:00
|
|
|
raise(M1());
|
|
|
|
|
2017-09-01 15:01:40 +00:00
|
|
|
di();
|
2017-06-04 20:38:34 +00:00
|
|
|
IM() = 0;
|
|
|
|
|
2017-09-01 15:01:40 +00:00
|
|
|
REFRESH() = 0;
|
2018-08-25 00:34:30 +00:00
|
|
|
IV() = Mask8;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
|
|
exxAF();
|
2018-10-27 17:41:55 +00:00
|
|
|
AF() = Mask16;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-10-27 17:41:55 +00:00
|
|
|
exx();
|
|
|
|
IX() = IY() = BC() = DE() = HL() = Mask16;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-09-01 15:01:40 +00:00
|
|
|
m_prefixCB = m_prefixDD = m_prefixED = m_prefixFD = false;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 00:34:30 +00:00
|
|
|
void EightBit::Z80::handleRESET() {
|
2018-10-27 17:41:55 +00:00
|
|
|
IntelProcessor::handleRESET();
|
2018-08-25 00:34:30 +00:00
|
|
|
di();
|
|
|
|
addCycles(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Z80::handleNMI() {
|
2018-08-25 11:09:26 +00:00
|
|
|
raise(NMI());
|
2018-08-25 00:34:30 +00:00
|
|
|
raise(HALT());
|
|
|
|
IFF1() = false;
|
|
|
|
restart(0x66);
|
|
|
|
addCycles(13);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Z80::handleINT() {
|
2018-10-27 17:41:55 +00:00
|
|
|
IntelProcessor::handleINT();
|
2018-08-25 00:34:30 +00:00
|
|
|
raise(HALT());
|
|
|
|
if (IFF1()) {
|
|
|
|
di();
|
|
|
|
switch (IM()) {
|
|
|
|
case 0: // i8080 equivalent
|
|
|
|
execute(BUS().DATA());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
restart(7 << 3);
|
|
|
|
addCycles(13);
|
|
|
|
break;
|
|
|
|
case 2:
|
2018-10-27 17:41:55 +00:00
|
|
|
call(MEMPTR() = { BUS().DATA(), IV() });
|
2018-08-25 00:34:30 +00:00
|
|
|
addCycles(19);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 20:08:40 +00:00
|
|
|
void EightBit::Z80::di() {
|
2017-06-04 20:38:34 +00:00
|
|
|
IFF1() = IFF2() = false;
|
|
|
|
}
|
|
|
|
|
2017-06-11 20:08:40 +00:00
|
|
|
void EightBit::Z80::ei() {
|
2017-06-04 20:38:34 +00:00
|
|
|
IFF1() = IFF2() = true;
|
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::increment(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = operand + 1;
|
|
|
|
adjustSZXY<Z80>(F(), result);
|
|
|
|
setFlag(F(), VF, result == Bit7);
|
|
|
|
clearFlag(F(), HC, lowNibble(result));
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::decrement(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), NF);
|
|
|
|
clearFlag(F(), HC, lowNibble(operand));
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = operand - 1;
|
|
|
|
adjustSZXY<Z80>(F(), result);
|
|
|
|
setFlag(F(), VF, result == Mask7);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::jrConditionalFlag(const int flag) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(flag >= 0);
|
|
|
|
ASSUME(flag <= 3);
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (flag) {
|
|
|
|
case 0: // NZ
|
2018-06-10 23:50:46 +00:00
|
|
|
return jrConditional(!(F() & ZF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 1: // Z
|
2018-06-10 23:50:46 +00:00
|
|
|
return jrConditional(F() & ZF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 2: // NC
|
2018-06-10 23:50:46 +00:00
|
|
|
return jrConditional(!(F() & CF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 3: // C
|
2018-06-10 23:50:46 +00:00
|
|
|
return jrConditional(F() & CF);
|
2017-07-02 16:38:19 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::jumpConditionalFlag(const int flag) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(flag >= 0);
|
|
|
|
ASSUME(flag <= 7);
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (flag) {
|
|
|
|
case 0: // NZ
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(!(F() & ZF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 1: // Z
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(F() & ZF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 2: // NC
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(!(F() & CF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 3: // C
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(F() & CF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 4: // PO
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(!(F() & PF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 5: // PE
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(F() & PF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 6: // P
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(!(F() & SF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 7: // M
|
2018-06-10 23:50:46 +00:00
|
|
|
return jumpConditional(F() & SF);
|
2017-07-02 16:38:19 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 21:39:15 +00:00
|
|
|
void EightBit::Z80::retn() {
|
2017-06-04 20:38:34 +00:00
|
|
|
ret();
|
|
|
|
IFF1() = IFF2();
|
|
|
|
}
|
|
|
|
|
2017-06-05 21:39:15 +00:00
|
|
|
void EightBit::Z80::reti() {
|
2017-06-04 20:38:34 +00:00
|
|
|
retn();
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::returnConditionalFlag(const int flag) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(flag >= 0);
|
|
|
|
ASSUME(flag <= 7);
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (flag) {
|
|
|
|
case 0: // NZ
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(!(F() & ZF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 1: // Z
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(F() & ZF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 2: // NC
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(!(F() & CF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 3: // C
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(F() & CF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 4: // PO
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(!(F() & PF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 5: // PE
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(F() & PF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 6: // P
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(!(F() & SF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 7: // M
|
2018-06-10 23:50:46 +00:00
|
|
|
return returnConditional(F() & SF);
|
2017-07-02 16:38:19 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::callConditionalFlag(const int flag) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(flag >= 0);
|
|
|
|
ASSUME(flag <= 7);
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (flag) {
|
|
|
|
case 0: // NZ
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(!(F() & ZF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 1: // Z
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(F() & ZF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 2: // NC
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(!(F() & CF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 3: // C
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(F() & CF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 4: // PO
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(!(F() & PF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 5: // PE
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(F() & PF);
|
2017-06-04 20:38:34 +00:00
|
|
|
case 6: // P
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(!(F() & SF));
|
2017-06-04 20:38:34 +00:00
|
|
|
case 7: // M
|
2018-06-10 23:50:46 +00:00
|
|
|
return callConditional(F() & SF);
|
2017-07-02 16:38:19 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
void EightBit::Z80::sbc(const register16_t value) {
|
2017-06-18 17:14:39 +00:00
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
MEMPTR() = HL2();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-07-03 20:42:18 +00:00
|
|
|
const auto beforeNegative = MEMPTR().high & SF;
|
2017-06-26 22:22:32 +00:00
|
|
|
const auto valueNegative = value.high & SF;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
const auto result = MEMPTR().word - value.word - (F() & CF);
|
2018-08-11 20:19:19 +00:00
|
|
|
HL2() = result;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
const auto afterNegative = HL2().high & SF;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), SF, afterNegative);
|
2018-06-11 22:01:48 +00:00
|
|
|
clearFlag(F(), ZF, HL2().word);
|
|
|
|
adjustHalfCarrySub(F(), MEMPTR().high, value.high, HL2().high);
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustOverflowSub(F(), beforeNegative, valueNegative, afterNegative);
|
|
|
|
setFlag(F(), NF);
|
|
|
|
setFlag(F(), CF, result & Bit16);
|
2018-06-11 22:01:48 +00:00
|
|
|
adjustXY<Z80>(F(), HL2().high);
|
2017-07-03 20:42:18 +00:00
|
|
|
|
2018-08-11 20:19:19 +00:00
|
|
|
++MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
void EightBit::Z80::adc(const register16_t value) {
|
2017-06-18 17:14:39 +00:00
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
MEMPTR() = HL2();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-07-03 20:42:18 +00:00
|
|
|
const auto beforeNegative = MEMPTR().high & SF;
|
2017-06-26 22:22:32 +00:00
|
|
|
const auto valueNegative = value.high & SF;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
const auto result = MEMPTR().word + value.word + (F() & CF);
|
2018-08-11 20:19:19 +00:00
|
|
|
HL2() = result;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
const auto afterNegative = HL2().high & SF;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), SF, afterNegative);
|
2018-06-11 22:01:48 +00:00
|
|
|
clearFlag(F(), ZF, HL2().word);
|
|
|
|
adjustHalfCarryAdd(F(), MEMPTR().high, value.high, HL2().high);
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustOverflowAdd(F(), beforeNegative, valueNegative, afterNegative);
|
|
|
|
clearFlag(F(), NF);
|
|
|
|
setFlag(F(), CF, result & Bit16);
|
2018-06-11 22:01:48 +00:00
|
|
|
adjustXY<Z80>(F(), HL2().high);
|
2017-07-03 20:42:18 +00:00
|
|
|
|
2018-08-11 20:19:19 +00:00
|
|
|
++MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
void EightBit::Z80::add(const register16_t value) {
|
2017-06-18 17:14:39 +00:00
|
|
|
|
2018-06-11 22:01:48 +00:00
|
|
|
MEMPTR() = HL2();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-07-03 20:42:18 +00:00
|
|
|
const auto result = MEMPTR().word + value.word;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-08-11 20:19:19 +00:00
|
|
|
HL2() = result;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF);
|
|
|
|
setFlag(F(), CF, result & Bit16);
|
2018-06-11 22:01:48 +00:00
|
|
|
adjustHalfCarryAdd(F(), MEMPTR().high, value.high, HL2().high);
|
|
|
|
adjustXY<Z80>(F(), HL2().high);
|
2017-07-03 20:42:18 +00:00
|
|
|
|
2018-08-11 20:19:19 +00:00
|
|
|
++MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::add(const uint8_t value, const int carry) {
|
2017-06-18 17:14:39 +00:00
|
|
|
|
2018-06-15 23:55:32 +00:00
|
|
|
const register16_t result = A() + value + carry;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustHalfCarryAdd(F(), A(), value, result.low);
|
|
|
|
adjustOverflowAdd(F(), A(), value, result.low);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF);
|
2018-11-28 21:27:14 +00:00
|
|
|
setFlag(F(), CF, result.high & CF);
|
2018-06-16 09:09:28 +00:00
|
|
|
adjustSZXY<Z80>(F(), A() = result.low);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::adc(const uint8_t value) {
|
|
|
|
add(value, F() & CF);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::subtract(const uint8_t operand, const uint8_t value, const int carry) {
|
2017-06-18 17:14:39 +00:00
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
const register16_t subtraction = operand - value - carry;
|
|
|
|
const auto result = subtraction.low;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
adjustHalfCarrySub(F(), operand, value, result);
|
|
|
|
adjustOverflowSub(F(), operand, value, result);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), NF);
|
2018-11-29 00:09:40 +00:00
|
|
|
setFlag(F(), CF, subtraction.high & CF);
|
|
|
|
adjustSZ<Z80>(F(), result);
|
|
|
|
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::sub(const uint8_t value, const int carry) {
|
2018-11-29 00:09:40 +00:00
|
|
|
A() = subtract(A(), value, carry);
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustXY<Z80>(F(), A());
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::sbc(const uint8_t value) {
|
|
|
|
sub(value, F() & CF);
|
2017-06-26 22:22:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::andr(const uint8_t value) {
|
|
|
|
setFlag(F(), HC);
|
|
|
|
clearFlag(F(), CF | NF);
|
|
|
|
adjustSZPXY<Z80>(F(), A() &= value);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::xorr(const uint8_t value) {
|
|
|
|
clearFlag(F(), HC | CF | NF);
|
|
|
|
adjustSZPXY<Z80>(F(), A() ^= value);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::orr(const uint8_t value) {
|
|
|
|
clearFlag(F(), HC | CF | NF);
|
|
|
|
adjustSZPXY<Z80>(F(), A() |= value);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::compare(const uint8_t value) {
|
2018-11-29 00:09:40 +00:00
|
|
|
subtract(A(), value);
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustXY<Z80>(F(), value);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::rlc(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
2017-10-29 20:15:49 +00:00
|
|
|
const auto carry = operand & Bit7;
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), CF, carry);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = (operand << 1) | (carry >> 7);
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::rrc(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
2017-10-29 20:15:49 +00:00
|
|
|
const auto carry = operand & Bit0;
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), CF, carry);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = (operand >> 1) | (carry << 7);
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::rl(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
const auto carry = F() & CF;
|
|
|
|
setFlag(F(), CF, operand & Bit7);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = (operand << 1) | carry;
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::rr(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
const auto carry = F() & CF;
|
|
|
|
setFlag(F(), CF, operand & Bit0);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = (operand >> 1) | (carry << 7);
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::sla(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
setFlag(F(), CF, operand & Bit7);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = operand << 1;
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::sra(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
setFlag(F(), CF, operand & Bit0);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = (operand >> 1) | (operand & Bit7);
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::sll(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
setFlag(F(), CF, operand & Bit7);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = (operand << 1) | Bit0;
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 00:09:40 +00:00
|
|
|
uint8_t EightBit::Z80::srl(const uint8_t operand) {
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
setFlag(F(), CF, operand & Bit0);
|
2018-11-29 00:09:40 +00:00
|
|
|
const uint8_t result = (operand >> 1) & ~Bit7;
|
|
|
|
adjustXY<Z80>(F(), result);
|
|
|
|
setFlag(F(), ZF, result);
|
|
|
|
return result;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
uint8_t EightBit::Z80::bit(const int n, const uint8_t operand) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(n >= 0);
|
|
|
|
ASSUME(n <= 7);
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), HC);
|
|
|
|
clearFlag(F(), NF);
|
2017-07-02 16:38:19 +00:00
|
|
|
const auto discarded = operand & (1 << n);
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustSZXY<Z80>(F(), discarded);
|
|
|
|
clearFlag(F(), PF, discarded);
|
2017-06-19 12:53:00 +00:00
|
|
|
return operand;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
uint8_t EightBit::Z80::res(const int n, const uint8_t operand) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(n >= 0);
|
|
|
|
ASSUME(n <= 7);
|
2017-08-06 16:06:48 +00:00
|
|
|
return operand & ~(1 << n);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 08:39:06 +00:00
|
|
|
uint8_t EightBit::Z80::set(const int n, const uint8_t operand) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(n >= 0);
|
|
|
|
ASSUME(n <= 7);
|
2017-08-06 16:06:48 +00:00
|
|
|
return operand | (1 << n);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::neg() {
|
2017-06-28 14:39:31 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), PF, A() == Bit7);
|
|
|
|
setFlag(F(), CF, A());
|
|
|
|
setFlag(F(), NF);
|
2017-06-28 14:39:31 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
const auto original = A();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
A() = (~A() + 1); // two's complement
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustHalfCarrySub(F(), 0U, original, A());
|
|
|
|
adjustOverflowSub(F(), 0U, original, A());
|
2017-06-28 14:39:31 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustSZXY<Z80>(F(), A());
|
2017-06-28 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::daa() {
|
2017-06-18 17:14:39 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
auto updated = A();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
const auto lowAdjust = (F() & HC) || (lowNibble(A()) > 9);
|
|
|
|
const auto highAdjust = (F() & CF) || (A() > 0x99);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
if (F() & NF) {
|
2017-06-04 20:38:34 +00:00
|
|
|
if (lowAdjust)
|
2017-06-28 14:39:31 +00:00
|
|
|
updated -= 6;
|
2017-06-04 20:38:34 +00:00
|
|
|
if (highAdjust)
|
2017-06-28 14:39:31 +00:00
|
|
|
updated -= 0x60;
|
2017-06-04 20:38:34 +00:00
|
|
|
} else {
|
|
|
|
if (lowAdjust)
|
2017-06-28 14:39:31 +00:00
|
|
|
updated += 6;
|
2017-06-04 20:38:34 +00:00
|
|
|
if (highAdjust)
|
2017-06-28 14:39:31 +00:00
|
|
|
updated += 0x60;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
F() = (F() & (CF | NF)) | (A() > 0x99 ? CF : 0) | ((A() ^ updated) & HC);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustSZPXY<Z80>(F(), A() = updated);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::cpl() {
|
|
|
|
setFlag(F(), HC | NF);
|
|
|
|
adjustXY<Z80>(F(), A() = ~A());
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::scf() {
|
|
|
|
setFlag(F(), CF);
|
|
|
|
clearFlag(F(), HC | NF);
|
|
|
|
adjustXY<Z80>(F(), A());
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::ccf() {
|
|
|
|
clearFlag(F(), NF);
|
|
|
|
const auto carry = F() & CF;
|
|
|
|
setFlag(F(), HC, carry);
|
|
|
|
clearFlag(F(), CF, carry);
|
|
|
|
adjustXY<Z80>(F(), A());
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 23:55:32 +00:00
|
|
|
void EightBit::Z80::xhtl() {
|
2018-03-10 01:53:57 +00:00
|
|
|
MEMPTR().low = BUS().read(SP());
|
2018-06-15 23:55:32 +00:00
|
|
|
BUS().write(HL2().low);
|
|
|
|
HL2().low = MEMPTR().low;
|
2018-08-11 20:19:19 +00:00
|
|
|
++BUS().ADDRESS();
|
2018-03-10 01:53:57 +00:00
|
|
|
MEMPTR().high = BUS().read();
|
2018-06-15 23:55:32 +00:00
|
|
|
BUS().write(HL2().high);
|
|
|
|
HL2().high = MEMPTR().high;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:16:00 +00:00
|
|
|
void EightBit::Z80::blockCompare(const register16_t source, register16_t& counter) {
|
2017-06-18 17:14:39 +00:00
|
|
|
|
2018-08-11 20:19:19 +00:00
|
|
|
const auto value = BUS().read(source);
|
2018-06-10 23:50:46 +00:00
|
|
|
uint8_t result = A() - value;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-08-11 20:19:19 +00:00
|
|
|
setFlag(F(), PF, --counter.word);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustSZ<Z80>(F(), result);
|
|
|
|
adjustHalfCarrySub(F(), A(), value, result);
|
|
|
|
setFlag(F(), NF);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
result -= ((F() & HC) >> 4);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), YF, result & Bit1);
|
|
|
|
setFlag(F(), XF, result & Bit3);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::cpi() {
|
2018-08-11 20:19:19 +00:00
|
|
|
blockCompare(HL()++, BC());
|
|
|
|
++MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::cpd() {
|
2018-08-11 20:19:19 +00:00
|
|
|
blockCompare(HL()--, BC());
|
|
|
|
--MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::cpir() {
|
|
|
|
cpi();
|
|
|
|
return (F() & PF) && !(F() & ZF); // See CPI
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::cpdr() {
|
|
|
|
cpd();
|
|
|
|
return (F() & PF) && !(F() & ZF); // See CPD
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 20:19:19 +00:00
|
|
|
void EightBit::Z80::blockLoad(const register16_t source, const register16_t destination, register16_t& counter) {
|
2018-03-10 01:53:57 +00:00
|
|
|
const auto value = BUS().read(source);
|
|
|
|
BUS().write(destination, value);
|
2018-06-10 23:50:46 +00:00
|
|
|
const auto xy = A() + value;
|
2018-11-28 21:27:14 +00:00
|
|
|
setFlag(F(), XF, xy & Bit3);
|
|
|
|
setFlag(F(), YF, xy & Bit1);
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
2018-08-11 20:19:19 +00:00
|
|
|
setFlag(F(), PF, --counter.word);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::ldd() {
|
2018-08-11 20:19:19 +00:00
|
|
|
blockLoad(HL()--, DE()--, BC());
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::ldi() {
|
2018-08-11 20:19:19 +00:00
|
|
|
blockLoad(HL()++, DE()++, BC());
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::ldir() {
|
|
|
|
ldi();
|
|
|
|
return !!(F() & PF); // See LDI
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::lddr() {
|
|
|
|
ldd();
|
|
|
|
return !!(F() & PF); // See LDD
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 23:16:00 +00:00
|
|
|
void EightBit::Z80::blockIn(register16_t& source, const register16_t destination) {
|
2018-08-12 15:25:30 +00:00
|
|
|
MEMPTR() = BUS().ADDRESS() = source;
|
2018-06-10 23:50:46 +00:00
|
|
|
const auto value = readPort();
|
2018-08-12 15:25:30 +00:00
|
|
|
BUS().write(destination, value);
|
2018-11-29 00:09:40 +00:00
|
|
|
source.high = decrement(source.high);
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), NF);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 15:25:30 +00:00
|
|
|
void EightBit::Z80::ini() {
|
|
|
|
blockIn(BC(), HL()++);
|
|
|
|
++MEMPTR();
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::ind() {
|
2018-08-12 15:25:30 +00:00
|
|
|
blockIn(BC(), HL()--);
|
2018-08-11 20:19:19 +00:00
|
|
|
--MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::inir() {
|
|
|
|
ini();
|
|
|
|
return !(F() & ZF); // See INI
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::indr() {
|
|
|
|
ind();
|
|
|
|
return !(F() & ZF); // See IND
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 15:25:30 +00:00
|
|
|
void EightBit::Z80::blockOut(const register16_t source, register16_t& destination) {
|
|
|
|
const auto value = BUS().read(source);
|
|
|
|
BUS().ADDRESS() = destination;
|
2017-06-04 20:38:34 +00:00
|
|
|
writePort();
|
2018-11-29 00:09:40 +00:00
|
|
|
destination.high = decrement(destination.high);
|
2018-08-12 15:25:30 +00:00
|
|
|
MEMPTR() = destination;
|
2018-06-10 23:50:46 +00:00
|
|
|
setFlag(F(), NF, value & Bit7);
|
|
|
|
setFlag(F(), HC | CF, (L() + value) > 0xff);
|
2018-11-28 21:27:14 +00:00
|
|
|
adjustParity<Z80>(F(), ((value + L()) & Mask3) ^ B());
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::outi() {
|
2018-08-12 15:25:30 +00:00
|
|
|
blockOut(HL()++, BC());
|
|
|
|
++MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::outd() {
|
2018-08-12 15:25:30 +00:00
|
|
|
blockOut(HL()--, BC());
|
|
|
|
--MEMPTR();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::otir() {
|
|
|
|
outi();
|
|
|
|
return !(F() & ZF); // See OUTI
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
bool EightBit::Z80::otdr() {
|
|
|
|
outd();
|
|
|
|
return !(F() & ZF); // See OUTD
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::rrd() {
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = HL())++;
|
2018-03-10 01:53:57 +00:00
|
|
|
const auto memory = BUS().read();
|
2018-06-10 23:50:46 +00:00
|
|
|
BUS().write(promoteNibble(A()) | highNibble(memory));
|
|
|
|
A() = higherNibble(A()) | lowerNibble(memory);
|
|
|
|
adjustSZPXY<Z80>(F(), A());
|
|
|
|
clearFlag(F(), NF | HC);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::rld() {
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = HL())++;
|
2018-03-10 01:53:57 +00:00
|
|
|
const auto memory = BUS().read();
|
2018-06-10 23:50:46 +00:00
|
|
|
BUS().write(promoteNibble(memory) | lowNibble(A()));
|
|
|
|
A() = higherNibble(A()) | highNibble(memory);
|
|
|
|
adjustSZPXY<Z80>(F(), A());
|
|
|
|
clearFlag(F(), NF | HC);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::writePort(const uint8_t port) {
|
2018-10-27 17:41:55 +00:00
|
|
|
MEMPTR() = BUS().ADDRESS() = { port, A() };
|
2018-06-10 23:50:46 +00:00
|
|
|
BUS().DATA() = A();
|
2017-06-28 14:39:31 +00:00
|
|
|
writePort();
|
2018-03-10 01:53:57 +00:00
|
|
|
++MEMPTR().low;
|
2017-06-28 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::Z80::writePort() {
|
2017-09-07 00:04:09 +00:00
|
|
|
m_ports.write(BUS().ADDRESS().low, BUS().DATA());
|
2017-06-28 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
uint8_t EightBit::Z80::readPort(const uint8_t port) {
|
2018-10-27 17:41:55 +00:00
|
|
|
MEMPTR() = BUS().ADDRESS() = { port, A() };
|
2018-03-10 01:53:57 +00:00
|
|
|
++MEMPTR().low;
|
2018-06-10 23:50:46 +00:00
|
|
|
return readPort();
|
2017-06-28 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
uint8_t EightBit::Z80::readPort() {
|
|
|
|
return BUS().DATA() = m_ports.read(BUS().ADDRESS().low);
|
2017-06-28 14:39:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 21:39:15 +00:00
|
|
|
int EightBit::Z80::step() {
|
2017-11-03 22:05:01 +00:00
|
|
|
resetCycles();
|
2018-10-28 19:29:14 +00:00
|
|
|
ExecutingInstruction.fire(*this);
|
2017-12-03 00:57:47 +00:00
|
|
|
if (LIKELY(powered())) {
|
2018-10-28 19:29:14 +00:00
|
|
|
m_displaced = m_prefixCB = m_prefixDD = m_prefixED = m_prefixFD = false;
|
2017-12-10 21:41:48 +00:00
|
|
|
lower(M1());
|
2018-08-25 00:34:30 +00:00
|
|
|
if (UNLIKELY(lowered(RESET()))) {
|
|
|
|
handleRESET();
|
|
|
|
} else if (UNLIKELY(lowered(NMI()))) {
|
|
|
|
handleNMI();
|
|
|
|
} else if (UNLIKELY(lowered(INT()))) {
|
|
|
|
handleINT();
|
|
|
|
} else if (UNLIKELY(lowered(HALT()))) {
|
|
|
|
execute(0); // NOP
|
|
|
|
} else {
|
|
|
|
execute(fetchByte());
|
2017-12-03 00:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 00:34:30 +00:00
|
|
|
ExecutedInstruction.fire(*this);
|
2017-12-03 00:57:47 +00:00
|
|
|
return cycles();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 14:29:30 +00:00
|
|
|
int EightBit::Z80::execute(const uint8_t opcode) {
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(lowered(M1()));
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-12-02 14:53:35 +00:00
|
|
|
if (LIKELY(!(m_prefixCB && m_displaced))) {
|
2017-06-29 11:19:22 +00:00
|
|
|
++REFRESH();
|
2017-12-10 21:41:48 +00:00
|
|
|
raise(M1());
|
2017-06-29 11:19:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 12:33:17 +00:00
|
|
|
const auto& decoded = getDecodedOpcode(opcode);
|
2017-06-26 22:22:32 +00:00
|
|
|
|
2017-11-18 14:29:30 +00:00
|
|
|
const auto x = decoded.x;
|
|
|
|
const auto y = decoded.y;
|
|
|
|
const auto z = decoded.z;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2017-11-18 14:29:30 +00:00
|
|
|
const auto p = decoded.p;
|
|
|
|
const auto q = decoded.q;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
2018-05-25 21:36:10 +00:00
|
|
|
const auto prefixed = m_prefixCB || m_prefixED;
|
|
|
|
if (LIKELY(!prefixed)) {
|
2018-06-10 23:50:46 +00:00
|
|
|
executeOther(x, y, z, p, q);
|
2018-05-25 21:36:10 +00:00
|
|
|
} else {
|
2017-06-29 20:25:58 +00:00
|
|
|
if (m_prefixCB)
|
2018-06-10 23:50:46 +00:00
|
|
|
executeCB(x, y, z);
|
2017-06-29 20:25:58 +00:00
|
|
|
else if (m_prefixED)
|
2018-06-10 23:50:46 +00:00
|
|
|
executeED(x, y, z, p, q);
|
2017-11-18 14:29:30 +00:00
|
|
|
else
|
|
|
|
UNREACHABLE;
|
2017-06-29 20:25:58 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00: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 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::executeCB(const int x, const int y, const int z) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(x >= 0);
|
|
|
|
ASSUME(x <= 3);
|
|
|
|
ASSUME(y >= 0);
|
|
|
|
ASSUME(y <= 7);
|
|
|
|
ASSUME(z >= 0);
|
|
|
|
ASSUME(z <= 7);
|
2018-11-25 10:38:30 +00:00
|
|
|
const bool memoryY = y == 6;
|
|
|
|
const bool memoryZ = z == 6;
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (x) {
|
2017-08-06 16:06:48 +00:00
|
|
|
case 0: { // rot[y] r[z]
|
2018-06-10 23:50:46 +00:00
|
|
|
auto operand = LIKELY(!m_displaced) ? R(z) : BUS().read(displacedAddress());
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (y) {
|
|
|
|
case 0:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = rlc(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = rrc(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = rl(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = rr(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = sla(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = sra(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = sll(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7:
|
2018-11-29 00:09:40 +00:00
|
|
|
operand = srl(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-08-08 12:38:27 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustSZP<Z80>(F(), operand);
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(!m_displaced)) {
|
|
|
|
R(z, operand);
|
|
|
|
if (UNLIKELY(memoryZ))
|
|
|
|
addCycles(7);
|
|
|
|
} else {
|
2017-12-02 14:53:35 +00:00
|
|
|
if (LIKELY(z != 6))
|
2018-06-10 23:50:46 +00:00
|
|
|
R2(z, operand);
|
2018-03-10 01:53:57 +00:00
|
|
|
BUS().write(operand);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(15);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(8);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-08-06 16:06:48 +00:00
|
|
|
} case 1: // BIT y, r[z]
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(8);
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(!m_displaced)) {
|
2018-06-10 23:50:46 +00:00
|
|
|
const auto operand = bit(y, R(z));
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(z != 6)) {
|
|
|
|
adjustXY<Z80>(F(), operand);
|
|
|
|
} else {
|
2018-06-10 23:50:46 +00:00
|
|
|
adjustXY<Z80>(F(), MEMPTR().high);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2018-11-25 10:38:30 +00:00
|
|
|
} else {
|
|
|
|
bit(y, BUS().read(displacedAddress()));
|
|
|
|
adjustXY<Z80>(F(), MEMPTR().high);
|
|
|
|
addCycles(12);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // RES y, r[z]
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(8);
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(!m_displaced)) {
|
|
|
|
R(z, res(y, R(z)));
|
|
|
|
if (UNLIKELY(memoryZ))
|
|
|
|
addCycles(7);
|
|
|
|
} else {
|
2018-03-10 01:53:57 +00:00
|
|
|
auto operand = BUS().read(displacedAddress());
|
2017-08-06 16:06:48 +00:00
|
|
|
operand = res(y, operand);
|
2018-03-10 01:53:57 +00:00
|
|
|
BUS().write(operand);
|
2018-06-10 23:50:46 +00:00
|
|
|
R2(z, operand);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(15);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // SET y, r[z]
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(8);
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(!m_displaced)) {
|
|
|
|
R(z, set(y, R(z)));
|
|
|
|
if (UNLIKELY(memoryZ))
|
|
|
|
addCycles(7);
|
|
|
|
} else {
|
2018-03-10 01:53:57 +00:00
|
|
|
auto operand = BUS().read(displacedAddress());
|
2017-08-06 16:06:48 +00:00
|
|
|
operand = set(y, operand);
|
2018-03-10 01:53:57 +00:00
|
|
|
BUS().write(operand);
|
2018-06-10 23:50:46 +00:00
|
|
|
R2(z, operand);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(15);
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-06-29 20:25:58 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::executeED(const int x, const int y, const int z, const int p, const int q) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(x >= 0);
|
|
|
|
ASSUME(x <= 3);
|
|
|
|
ASSUME(y >= 0);
|
|
|
|
ASSUME(y <= 7);
|
|
|
|
ASSUME(z >= 0);
|
|
|
|
ASSUME(z <= 7);
|
|
|
|
ASSUME(p >= 0);
|
|
|
|
ASSUME(p <= 3);
|
|
|
|
ASSUME(q >= 0);
|
|
|
|
ASSUME(q <= 1);
|
2018-11-25 10:38:30 +00:00
|
|
|
const bool memoryY = y == 6;
|
|
|
|
const bool memoryZ = z == 6;
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (x) {
|
|
|
|
case 0:
|
|
|
|
case 3: // Invalid instruction, equivalent to NONI followed by NOP
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(8);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (z) {
|
|
|
|
case 0: // Input from port with 16-bit address
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = BC())++;
|
2017-06-04 20:38:34 +00:00
|
|
|
readPort();
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(y != 6)) // IN r[y],(C)
|
|
|
|
R(y, BUS().DATA());
|
|
|
|
adjustSZPXY<Z80>(F(), BUS().DATA());
|
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
addCycles(12);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // Output to port with 16-bit address
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = BC())++;
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(y != 6)) // OUT (C),r[y]
|
2018-06-10 23:50:46 +00:00
|
|
|
BUS().DATA() = R(y);
|
2018-11-25 10:38:30 +00:00
|
|
|
else // OUT (C),0
|
|
|
|
BUS().DATA() = 0;
|
2017-06-04 20:38:34 +00:00
|
|
|
writePort();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(12);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // 16-bit add/subtract with carry
|
|
|
|
switch (q) {
|
|
|
|
case 0: // SBC HL, rp[p]
|
2018-06-11 22:01:48 +00:00
|
|
|
sbc(RP(p));
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // ADC HL, rp[p]
|
2018-06-11 22:01:48 +00:00
|
|
|
adc(RP(p));
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(15);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // Retrieve/store register pair from/to immediate address
|
2018-08-17 12:59:59 +00:00
|
|
|
BUS().ADDRESS() = fetchWord();
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (q) {
|
2018-03-18 22:40:23 +00:00
|
|
|
case 0: // LD (nn), rp[p]
|
2018-01-10 23:08:14 +00:00
|
|
|
setWord(RP(p));
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // LD rp[p], (nn)
|
2018-02-25 19:48:01 +00:00
|
|
|
RP(p) = getWord();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(20);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 4: // Negate accumulator
|
2018-06-10 23:50:46 +00:00
|
|
|
neg();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(8);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // Return from interrupt
|
|
|
|
switch (y) {
|
|
|
|
case 1:
|
|
|
|
reti(); // RETI
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retn(); // RETN
|
|
|
|
break;
|
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(14);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // Set interrupt mode
|
|
|
|
switch (y) {
|
|
|
|
case 0:
|
|
|
|
case 4:
|
|
|
|
IM() = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 6:
|
|
|
|
IM() = 1;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 7:
|
|
|
|
IM() = 2;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 5:
|
|
|
|
IM() = 0;
|
2017-06-29 09:18:07 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(8);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // Assorted ops
|
|
|
|
switch (y) {
|
|
|
|
case 0: // LD I,A
|
2018-06-10 23:50:46 +00:00
|
|
|
IV() = A();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(9);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // LD R,A
|
2018-06-10 23:50:46 +00:00
|
|
|
REFRESH() = A();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(9);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // LD A,I
|
2018-06-16 09:09:28 +00:00
|
|
|
adjustSZXY<Z80>(F(), A() = IV());
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
setFlag(F(), PF, IFF2());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(9);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // LD A,R
|
2018-06-16 09:09:28 +00:00
|
|
|
adjustSZXY<Z80>(F(), A() = REFRESH());
|
2018-06-10 23:50:46 +00:00
|
|
|
clearFlag(F(), NF | HC);
|
|
|
|
setFlag(F(), PF, IFF2());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(9);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 4: // RRD
|
2018-06-10 23:50:46 +00:00
|
|
|
rrd();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(18);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // RLD
|
2018-06-10 23:50:46 +00:00
|
|
|
rld();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(18);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // NOP
|
|
|
|
case 7: // NOP
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
switch (z) {
|
|
|
|
case 0: // LD
|
|
|
|
switch (y) {
|
|
|
|
case 4: // LDI
|
2018-06-10 23:50:46 +00:00
|
|
|
ldi();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // LDD
|
2018-06-10 23:50:46 +00:00
|
|
|
ldd();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // LDIR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(ldir())) {
|
2018-08-11 20:19:19 +00:00
|
|
|
MEMPTR() = --PC();
|
|
|
|
--PC();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // LDDR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(lddr())) {
|
2018-08-17 12:59:59 +00:00
|
|
|
MEMPTR() = --PC();
|
2018-08-11 20:19:19 +00:00
|
|
|
--PC();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // CP
|
|
|
|
switch (y) {
|
|
|
|
case 4: // CPI
|
2018-06-10 23:50:46 +00:00
|
|
|
cpi();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // CPD
|
2018-06-10 23:50:46 +00:00
|
|
|
cpd();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // CPIR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(cpir())) {
|
2018-08-17 12:59:59 +00:00
|
|
|
MEMPTR() = --PC();
|
2018-08-11 20:19:19 +00:00
|
|
|
--PC();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // CPDR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(cpdr())) {
|
2018-08-17 12:59:59 +00:00
|
|
|
MEMPTR() = --PC();
|
2018-08-11 20:19:19 +00:00
|
|
|
--PC();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2018-04-11 22:53:26 +00:00
|
|
|
} else {
|
2018-08-11 20:19:19 +00:00
|
|
|
MEMPTR() = PC() - 2;
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // IN
|
|
|
|
switch (y) {
|
|
|
|
case 4: // INI
|
2018-06-10 23:50:46 +00:00
|
|
|
ini();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // IND
|
2018-06-10 23:50:46 +00:00
|
|
|
ind();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // INIR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(inir())) {
|
2018-08-11 20:19:19 +00:00
|
|
|
PC() -= 2;
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // INDR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(indr())) {
|
2018-08-11 20:19:19 +00:00
|
|
|
PC() -= 2;
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // OUT
|
|
|
|
switch (y) {
|
|
|
|
case 4: // OUTI
|
2018-06-10 23:50:46 +00:00
|
|
|
outi();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // OUTD
|
2018-06-10 23:50:46 +00:00
|
|
|
outd();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // OTIR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(otir())) {
|
2018-08-11 20:19:19 +00:00
|
|
|
PC() -= 2;
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // OTDR
|
2018-06-10 23:50:46 +00:00
|
|
|
if (LIKELY(otdr())) {
|
2018-08-11 20:19:19 +00:00
|
|
|
PC() -= 2;
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
2017-06-14 21:33:02 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(16);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:50:46 +00:00
|
|
|
void EightBit::Z80::executeOther(const int x, const int y, const int z, const int p, const int q) {
|
2018-03-10 01:53:57 +00:00
|
|
|
ASSUME(x >= 0);
|
|
|
|
ASSUME(x <= 3);
|
|
|
|
ASSUME(y >= 0);
|
|
|
|
ASSUME(y <= 7);
|
|
|
|
ASSUME(z >= 0);
|
|
|
|
ASSUME(z <= 7);
|
|
|
|
ASSUME(p >= 0);
|
|
|
|
ASSUME(p <= 3);
|
|
|
|
ASSUME(q >= 0);
|
|
|
|
ASSUME(q <= 1);
|
2018-11-25 10:38:30 +00:00
|
|
|
const bool memoryY = y == 6;
|
|
|
|
const bool memoryZ = z == 6;
|
2017-06-04 20:38:34 +00: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-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // EX AF AF'
|
|
|
|
exxAF();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // DJNZ d
|
2017-12-02 14:53:35 +00:00
|
|
|
if (LIKELY(jrConditional(--B())))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
|
|
|
addCycles(8);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // JR d
|
2017-06-11 20:08:40 +00:00
|
|
|
jr(fetchByte());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(12);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-07-19 12:59:28 +00:00
|
|
|
case 4: // JR cc,d
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
2018-06-10 23:50:46 +00:00
|
|
|
if (UNLIKELY(jrConditionalFlag(y - 4)))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(5);
|
|
|
|
addCycles(5);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-07-19 12:59:28 +00:00
|
|
|
default:
|
2017-10-29 19:48:47 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // 16-bit load immediate/add
|
|
|
|
switch (q) {
|
2017-06-07 21:54:55 +00:00
|
|
|
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-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // ADD HL,rp
|
2018-06-11 22:01:48 +00:00
|
|
|
add(RP(p));
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(11);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // Indirect loading
|
|
|
|
switch (q) {
|
|
|
|
case 0:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // LD (BC),A
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = BC())++;
|
2018-06-10 23:50:46 +00:00
|
|
|
MEMPTR().high = BUS().DATA() = A();
|
2018-06-09 23:40:56 +00:00
|
|
|
BUS().write();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // LD (DE),A
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = DE())++;
|
2018-06-10 23:50:46 +00:00
|
|
|
MEMPTR().high = BUS().DATA() = A();
|
|
|
|
BUS().write();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // LD (nn),HL
|
2018-08-17 12:59:59 +00:00
|
|
|
BUS().ADDRESS() = fetchWord();
|
2018-01-10 23:08:14 +00:00
|
|
|
setWord(HL2());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(16);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // LD (nn),A
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = fetchWord())++;
|
2018-06-10 23:50:46 +00:00
|
|
|
MEMPTR().high = BUS().DATA() = A();
|
|
|
|
BUS().write();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(13);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // LD A,(BC)
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = BC())++;
|
2018-06-10 23:50:46 +00:00
|
|
|
A() = BUS().read();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // LD A,(DE)
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = DE())++;
|
2018-06-10 23:50:46 +00:00
|
|
|
A() = BUS().read();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // LD HL,(nn)
|
2018-08-17 12:59:59 +00:00
|
|
|
BUS().ADDRESS() = fetchWord();
|
2018-02-25 19:48:01 +00:00
|
|
|
HL2() = getWord();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(16);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // LD A,(nn)
|
2018-10-28 13:33:36 +00:00
|
|
|
(MEMPTR() = BUS().ADDRESS() = fetchWord())++;
|
2018-06-10 23:50:46 +00:00
|
|
|
A() = BUS().read();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(13);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // 16-bit INC/DEC
|
|
|
|
switch (q) {
|
|
|
|
case 0: // INC rp
|
2018-08-11 20:19:19 +00:00
|
|
|
++RP(p);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // DEC rp
|
2018-08-11 20:19:19 +00:00
|
|
|
--RP(p);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(6);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2018-11-29 00:09:40 +00:00
|
|
|
case 4: // 8-bit INC
|
2018-11-25 10:38:30 +00:00
|
|
|
if (UNLIKELY(m_displaced && memoryY))
|
2017-08-06 16:06:48 +00:00
|
|
|
fetchDisplacement();
|
2018-11-29 00:09:40 +00:00
|
|
|
R(y, increment(R(y)));
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2018-11-29 00:09:40 +00:00
|
|
|
case 5: // 8-bit DEC
|
2018-11-25 10:38:30 +00:00
|
|
|
if (UNLIKELY(memoryY)) {
|
2018-04-14 08:39:06 +00:00
|
|
|
addCycles(7);
|
|
|
|
if (UNLIKELY(m_displaced))
|
|
|
|
fetchDisplacement();
|
|
|
|
}
|
2018-11-29 00:09:40 +00:00
|
|
|
R(y, decrement(R(y)));
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2018-11-29 00:09:40 +00:00
|
|
|
case 6: // 8-bit load immediate
|
2018-11-25 10:38:30 +00:00
|
|
|
if (UNLIKELY(memoryY)) {
|
2018-04-14 08:39:06 +00:00
|
|
|
addCycles(3);
|
|
|
|
if (UNLIKELY(m_displaced))
|
|
|
|
fetchDisplacement();
|
|
|
|
}
|
2018-06-10 23:50:46 +00:00
|
|
|
R(y, fetchByte()); // LD r,n
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-08-06 16:06:48 +00:00
|
|
|
case 7: // Assorted operations on accumulator/flags
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (y) {
|
|
|
|
case 0:
|
2018-11-29 00:09:40 +00:00
|
|
|
A() = rlc(A());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2018-11-29 00:09:40 +00:00
|
|
|
A() = rrc(A());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-11-29 00:09:40 +00:00
|
|
|
A() = rl(A());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2018-11-29 00:09:40 +00:00
|
|
|
A() = rr(A());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2018-06-10 23:50:46 +00:00
|
|
|
daa();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2018-06-10 23:50:46 +00:00
|
|
|
cpl();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2018-06-10 23:50:46 +00:00
|
|
|
scf();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7:
|
2018-06-10 23:50:46 +00:00
|
|
|
ccf();
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // 8-bit loading
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(!(memoryZ && memoryY))) {
|
2017-06-04 20:38:34 +00:00
|
|
|
bool normal = true;
|
2017-12-02 10:34:37 +00:00
|
|
|
if (UNLIKELY(m_displaced)) {
|
2018-11-25 10:38:30 +00:00
|
|
|
if (LIKELY(memoryZ || memoryY))
|
2017-08-06 16:06:48 +00:00
|
|
|
fetchDisplacement();
|
2018-11-25 10:38:30 +00:00
|
|
|
if (memoryZ) {
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (y) {
|
|
|
|
case 4:
|
2018-06-10 23:50:46 +00:00
|
|
|
H() = R(z);
|
2017-06-04 20:38:34 +00:00
|
|
|
normal = false;
|
|
|
|
break;
|
|
|
|
case 5:
|
2018-06-10 23:50:46 +00:00
|
|
|
L() = R(z);
|
2017-06-04 20:38:34 +00:00
|
|
|
normal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-25 10:38:30 +00:00
|
|
|
if (memoryY) {
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (z) {
|
|
|
|
case 4:
|
2018-06-10 23:50:46 +00:00
|
|
|
R(y, H());
|
2017-06-04 20:38:34 +00:00
|
|
|
normal = false;
|
|
|
|
break;
|
|
|
|
case 5:
|
2018-06-10 23:50:46 +00:00
|
|
|
R(y, L());
|
2017-06-04 20:38:34 +00:00
|
|
|
normal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-02 14:53:35 +00:00
|
|
|
if (LIKELY(normal))
|
2018-06-10 23:50:46 +00:00
|
|
|
R(y, R(z));
|
2018-11-25 10:38:30 +00:00
|
|
|
if (UNLIKELY(memoryY || memoryZ)) // M operations
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(3);
|
2018-11-25 10:38:30 +00:00
|
|
|
} else { // Exception (replaces LD (HL), (HL))
|
|
|
|
halt();
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2018-06-16 09:09:28 +00:00
|
|
|
case 2: { // Operate on accumulator and register/memory location
|
2018-11-25 10:38:30 +00:00
|
|
|
if (UNLIKELY(memoryZ)) {
|
2018-04-14 08:39:06 +00:00
|
|
|
addCycles(3);
|
|
|
|
if (UNLIKELY(m_displaced))
|
|
|
|
fetchDisplacement();
|
|
|
|
}
|
2018-06-16 09:09:28 +00:00
|
|
|
const auto value = R(z);
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (y) {
|
|
|
|
case 0: // ADD A,r
|
2018-06-16 09:09:28 +00:00
|
|
|
add(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // ADC A,r
|
2018-06-16 09:09:28 +00:00
|
|
|
adc(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // SUB r
|
2018-06-16 09:09:28 +00:00
|
|
|
sub(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // SBC A,r
|
2018-06-16 09:09:28 +00:00
|
|
|
sbc(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 4: // AND r
|
2018-06-16 09:09:28 +00:00
|
|
|
andr(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // XOR r
|
2018-06-16 09:09:28 +00:00
|
|
|
xorr(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // OR r
|
2018-06-16 09:09:28 +00:00
|
|
|
orr(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // CP r
|
2018-06-16 09:09:28 +00:00
|
|
|
compare(value);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2018-06-16 09:09:28 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
case 3:
|
|
|
|
switch (z) {
|
|
|
|
case 0: // Conditional return
|
2018-06-10 23:50:46 +00:00
|
|
|
if (UNLIKELY(returnConditionalFlag(y)))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(6);
|
|
|
|
addCycles(5);
|
2017-06-04 20:38:34 +00: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-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // RET
|
|
|
|
ret();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // EXX
|
|
|
|
exx();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // JP HL
|
2018-04-11 22:53:26 +00:00
|
|
|
jump(HL2());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // LD SP,HL
|
2017-06-19 12:53:00 +00:00
|
|
|
SP() = HL2();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-06-29 20:25:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // Conditional jump
|
2018-06-10 23:50:46 +00:00
|
|
|
jumpConditionalFlag(y);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // Assorted operations
|
|
|
|
switch (y) {
|
|
|
|
case 0: // JP nn
|
2018-03-18 22:40:23 +00:00
|
|
|
jump(MEMPTR() = fetchWord());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(10);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // CB prefix
|
|
|
|
m_prefixCB = true;
|
2017-12-02 10:34:37 +00:00
|
|
|
if (UNLIKELY(m_displaced))
|
2017-08-06 16:06:48 +00:00
|
|
|
fetchDisplacement();
|
2017-12-10 21:41:48 +00:00
|
|
|
lower(M1());
|
2017-12-03 00:57:47 +00:00
|
|
|
execute(fetchByte());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // OUT (n),A
|
2018-06-10 23:50:46 +00:00
|
|
|
writePort(fetchByte());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(11);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // IN A,(n)
|
2018-06-10 23:50:46 +00:00
|
|
|
A() = readPort(fetchByte());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(11);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 4: // EX (SP),HL
|
2018-06-15 23:55:32 +00:00
|
|
|
xhtl();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(19);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // EX DE,HL
|
|
|
|
std::swap(DE(), HL());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // DI
|
2017-06-11 20:08:40 +00:00
|
|
|
di();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // EI
|
2017-06-11 20:08:40 +00:00
|
|
|
ei();
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(4);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4: // Conditional call: CALL cc[y], nn
|
2018-06-10 23:50:46 +00:00
|
|
|
if (UNLIKELY(callConditionalFlag(y)))
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
|
|
|
addCycles(10);
|
2017-06-04 20:38:34 +00: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-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (p) {
|
|
|
|
case 0: // CALL nn
|
2018-03-18 22:40:23 +00:00
|
|
|
call(MEMPTR() = fetchWord());
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(17);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // DD prefix
|
2017-06-15 21:21:26 +00:00
|
|
|
m_displaced = m_prefixDD = true;
|
2017-12-10 21:41:48 +00:00
|
|
|
lower(M1());
|
2017-12-03 00:57:47 +00:00
|
|
|
execute(fetchByte());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // ED prefix
|
|
|
|
m_prefixED = true;
|
2017-12-10 21:41:48 +00:00
|
|
|
lower(M1());
|
2017-12-03 00:57:47 +00:00
|
|
|
execute(fetchByte());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // FD prefix
|
2017-06-15 21:21:26 +00:00
|
|
|
m_displaced = m_prefixFD = true;
|
2017-12-10 21:41:48 +00:00
|
|
|
lower(M1());
|
2017-12-03 00:57:47 +00:00
|
|
|
execute(fetchByte());
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-06-29 20:25:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-06-16 09:09:28 +00:00
|
|
|
case 6: { // Operate on accumulator and immediate operand: alu[y] n
|
|
|
|
const auto operand = fetchByte();
|
2017-06-04 20:38:34 +00:00
|
|
|
switch (y) {
|
|
|
|
case 0: // ADD A,n
|
2018-06-16 09:09:28 +00:00
|
|
|
add(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // ADC A,n
|
2018-06-16 09:09:28 +00:00
|
|
|
adc(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 2: // SUB n
|
2018-06-16 09:09:28 +00:00
|
|
|
sub(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 3: // SBC A,n
|
2018-06-16 09:09:28 +00:00
|
|
|
sbc(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 4: // AND n
|
2018-06-16 09:09:28 +00:00
|
|
|
andr(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 5: // XOR n
|
2018-06-16 09:09:28 +00:00
|
|
|
xorr(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 6: // OR n
|
2018-06-16 09:09:28 +00:00
|
|
|
orr(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
case 7: // CP n
|
2018-06-16 09:09:28 +00:00
|
|
|
compare(operand);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(7);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2018-06-16 09:09:28 +00:00
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
case 7: // Restart: RST y * 8
|
|
|
|
restart(y << 3);
|
2017-11-03 22:05:01 +00:00
|
|
|
addCycles(11);
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
2017-06-28 14:39:31 +00:00
|
|
|
default:
|
2017-10-29 18:47:23 +00:00
|
|
|
UNREACHABLE;
|
2017-06-04 20:38:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-08-30 22:17:34 +00:00
|
|
|
}
|