1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-06-14 17:29:42 +00:00
r65emu/z80.h

2175 lines
62 KiB
C
Raw Permalink Normal View History

2015-12-01 19:51:45 +00:00
#ifndef __Z80_H__
#define __Z80_H__
#undef sbi
#undef inch
2018-11-27 07:58:22 +00:00
#undef SP
2015-12-01 19:51:45 +00:00
class z80: public CPU {
public:
2019-02-25 14:55:27 +00:00
z80(Memory &m, PortDevice<z80> &ports): CPU(m), _ports(&ports) {}
2015-12-01 19:51:45 +00:00
void run(unsigned);
void reset();
2019-03-03 17:07:23 +00:00
void raise(uint8_t level) { _irq_pending = level; }
2018-09-13 10:39:09 +00:00
char *status(char *buf, size_t n, bool hdr=false);
2015-12-01 19:51:45 +00:00
void checkpoint(Stream &);
void restore(Stream &);
2018-08-13 13:41:23 +00:00
inline uint8_t a() { return A; }
inline uint8_t b() { return B; }
inline uint8_t c() { return C; }
inline uint8_t d() { return D; }
inline uint8_t e() { return E; }
inline uint8_t h() { return H; }
inline uint8_t l() { return L; }
inline uint16_t af() { return AF; }
inline uint16_t bc() { return BC; }
inline uint16_t de() { return DE; }
inline uint16_t hl() { return HL; }
inline uint8_t sr() { return F; }
inline uint8_t i() { return I; }
inline uint8_t r() { return R; }
inline uint16_t af_() { return AF_; }
inline uint16_t bc_() { return BC_; }
inline uint16_t de_() { return DE_; }
inline uint16_t hl_() { return HL_; }
inline uint16_t ix() { return IX; }
inline uint16_t iy() { return IY; }
inline uint16_t sp() { return SP; }
inline uint16_t pc() { return PC; }
2015-12-01 19:51:45 +00:00
inline bool iff1() { return _iff1; }
inline bool iff2() { return _iff2; }
2018-08-13 13:41:23 +00:00
inline uint8_t im() { return _im; }
2019-02-26 16:06:21 +00:00
inline uint16_t memptr() { return _memptr; }
2018-08-13 13:41:23 +00:00
inline void af(uint16_t w) { AF = w; }
inline void bc(uint16_t w) { BC = w; }
inline void de(uint16_t w) { DE = w; }
inline void hl(uint16_t w) { HL = w; }
inline void pc(uint16_t w) { PC = w; }
inline void sp(uint16_t w) { SP = w; }
inline void ix(uint16_t w) { IX = w; }
inline void iy(uint16_t w) { IY = w; }
inline void af_(uint16_t w) { AF_ = w; }
inline void bc_(uint16_t w) { BC_ = w; }
inline void de_(uint16_t w) { DE_ = w; }
inline void hl_(uint16_t w) { HL_ = w; }
inline void i(uint8_t i) { I = i; }
inline void r(uint8_t r) { R = r; }
inline void iff1(uint8_t iff1) { _iff1 = iff1 != 0; }
inline void iff2(uint8_t iff2) { _iff2 = iff2 != 0; }
inline void im(uint8_t im) { _im = im; }
2019-02-26 16:06:21 +00:00
inline void memptr(uint16_t memptr) { _memptr = memptr; }
2015-12-01 19:51:45 +00:00
inline unsigned long ts() { return _ts; }
inline void ts(int t) { _ts += t; }
2015-12-21 12:08:51 +00:00
inline void reset_ts() { _ts = 0; }
2015-12-01 19:51:45 +00:00
private:
void _handle_interrupt();
2019-02-25 13:21:41 +00:00
void op(uint8_t);
2015-12-01 19:51:45 +00:00
2018-08-13 13:41:23 +00:00
uint8_t _fetch_op();
2015-12-01 19:51:45 +00:00
2019-02-25 14:55:27 +00:00
typedef void (z80::*EXT_OP)(uint8_t, uint8_t);
void ddcb(uint8_t op, uint8_t a);
void fdcb(uint8_t op, uint8_t a);
2015-12-01 19:51:45 +00:00
2019-02-25 14:55:27 +00:00
void _step_idx(EXT_OP op);
void _ddfd(uint16_t &ix, uint8_t &ixL, uint8_t &ixH, EXT_OP op);
2015-12-01 19:51:45 +00:00
union {
struct {
union {
struct __attribute__((packed)) {
unsigned C:1;
unsigned N:1;
unsigned P:1;
unsigned _3:1;
unsigned H:1;
unsigned _5:1;
unsigned Z:1;
unsigned S:1;
} flags;
2018-08-13 13:41:23 +00:00
uint8_t F;
2015-12-01 19:51:45 +00:00
};
2018-08-13 13:41:23 +00:00
uint8_t A;
2015-12-01 19:51:45 +00:00
};
2018-08-13 13:41:23 +00:00
uint16_t AF;
2015-12-01 19:51:45 +00:00
};
union {
2018-08-13 13:41:23 +00:00
struct { uint8_t C, B; };
uint16_t BC;
2015-12-01 19:51:45 +00:00
};
union {
2018-08-13 13:41:23 +00:00
struct { uint8_t E, D; };
uint16_t DE;
2015-12-01 19:51:45 +00:00
};
union {
2018-08-13 13:41:23 +00:00
struct { uint8_t L, H; };
uint16_t HL;
2015-12-01 19:51:45 +00:00
};
Memory::address SP;
2018-08-13 13:41:23 +00:00
uint16_t AF_, BC_, DE_, HL_;
2015-12-01 19:51:45 +00:00
union {
2018-08-13 13:41:23 +00:00
struct { uint8_t IXL, IXH; };
uint16_t IX;
2015-12-01 19:51:45 +00:00
};
union {
2018-08-13 13:41:23 +00:00
struct { uint8_t IYL, IYH; };
uint16_t IY;
2015-12-01 19:51:45 +00:00
};
union {
2018-08-13 13:41:23 +00:00
struct { uint8_t R, I; };
uint16_t IR;
2015-12-01 19:51:45 +00:00
};
2018-08-13 13:41:23 +00:00
uint8_t _im;
2015-12-01 19:51:45 +00:00
bool _iff1, _iff2;
2019-02-26 16:06:21 +00:00
union {
struct { uint8_t MPL, MPH; };
uint16_t _memptr;
};
2015-12-01 19:51:45 +00:00
unsigned long _ts;
2019-03-03 17:07:23 +00:00
uint8_t _irq_pending;
2015-12-01 19:51:45 +00:00
PortDevice<z80> *_ports;
uint8_t parity(uint8_t);
2015-12-01 19:51:45 +00:00
2018-08-13 13:41:23 +00:00
inline uint8_t _rb(Memory::address a) {
2015-12-01 19:51:45 +00:00
#if defined(CPU_DEBUG)
2018-08-09 13:23:54 +00:00
printf("%5ld MC %04x\n", _ts, a);
2015-12-01 19:51:45 +00:00
#endif
ts(3);
#if defined(CPU_DEBUG)
2018-08-13 13:41:23 +00:00
printf("%5ld MR %04x %02x\n", _ts, a, (uint8_t)_mem[a]);
2015-12-01 19:51:45 +00:00
#endif
return _mem[a];
}
2018-08-13 13:41:23 +00:00
inline void _sb(Memory::address a, uint8_t b) {
2015-12-01 19:51:45 +00:00
#if defined(CPU_DEBUG)
2018-08-09 13:23:54 +00:00
printf("%5ld MC %04x\n", _ts, a);
2015-12-01 19:51:45 +00:00
#endif
ts(3);
#if defined(CPU_DEBUG)
2018-08-09 13:23:54 +00:00
printf("%5ld MW %04x %02x\n", _ts, a, b);
2015-12-01 19:51:45 +00:00
#endif
_mem[a] = b;
}
inline void _mc(Memory::address a, int i) {
#if defined(CPU_DEBUG)
2018-08-09 13:23:54 +00:00
printf("%5ld MC %04x\n", _ts, a);
2015-12-01 19:51:45 +00:00
#endif
ts(i);
}
2018-08-13 13:41:23 +00:00
inline uint16_t _rw(Memory::address a) {
2018-08-10 10:13:45 +00:00
return _rb(a) + (_rb(a+1) << 8);
2015-12-01 19:51:45 +00:00
}
2018-08-13 13:41:23 +00:00
inline void _sw(Memory::address a, uint16_t w) {
2015-12-01 19:51:45 +00:00
_sb(a+1, w >> 8);
_sb(a, w & 0xff);
}
2018-08-13 13:41:23 +00:00
inline uint16_t _rwpc() {
uint16_t w = _rw(PC);
2015-12-01 19:51:45 +00:00
PC += 2;
return w;
}
2019-02-26 16:06:21 +00:00
inline void _swPC(uint16_t w) {
uint16_t m = _rw(PC);
_sw(m, w);
PC += 2;
_memptr = m+1;
}
2015-12-01 19:51:45 +00:00
2018-08-13 13:41:23 +00:00
inline uint16_t _rwPC() {
2019-02-26 16:06:21 +00:00
uint16_t a = _rw(PC);
uint16_t w = _rw(a);
2015-12-01 19:51:45 +00:00
PC += 2;
2019-02-26 16:06:21 +00:00
_memptr = a+1;
2015-12-01 19:51:45 +00:00
return w;
}
2018-08-13 13:41:23 +00:00
inline void _35(uint8_t r) {
2015-12-01 19:51:45 +00:00
flags._3 = ((r & 0x08) != 0);
flags._5 = ((r & 0x20) != 0);
}
2018-08-13 13:41:23 +00:00
inline void _sz35(uint8_t r) {
2015-12-01 19:51:45 +00:00
flags.S = ((r & 0x80) != 0);
flags.Z = (r == 0);
_35(r);
}
2018-08-13 13:41:23 +00:00
inline void _szp35(uint8_t r) {
2015-12-01 19:51:45 +00:00
_sz35(r);
flags.P = parity(r);
2015-12-01 19:51:45 +00:00
}
2018-08-13 13:41:23 +00:00
inline void _inc(uint8_t &b) {
uint16_t w = b + 1;
uint8_t r = w & 0xff;
2015-12-01 19:51:45 +00:00
_sz35(r);
flags.P = r == 0x80;
flags.N = 0;
flags.H = !(r & 0x0f);
b = r;
}
2018-08-13 13:41:23 +00:00
inline void _dec(uint8_t &b) {
uint16_t w = b - 1;
uint8_t r = w & 0xff;
2015-12-01 19:51:45 +00:00
_sz35(r);
flags.P = r == 0x7f;
flags.N = 1;
flags.H = !(b & 0x0f);
b = r;
}
2018-08-13 13:41:23 +00:00
inline void _add(uint8_t x) {
uint16_t w = A + x;
uint8_t b = A;
2015-12-01 19:51:45 +00:00
A = w & 0xff;
_sz35(A);
flags.C = w > 0xff;
flags.N = 0;
2018-08-13 13:41:23 +00:00
uint8_t v = b ^ A ^ x;
2015-12-01 19:51:45 +00:00
flags.P = (v >> 7) ^ flags.C;
flags.H = (v >> 4) & 1;
}
2018-08-13 13:41:23 +00:00
inline void _adc(uint8_t x) {
uint16_t w = A + x + flags.C;
uint8_t b = A;
2015-12-01 19:51:45 +00:00
A = w & 0xff;
_sz35(A);
flags.C = w > 0xff;
flags.N = 0;
2018-08-13 13:41:23 +00:00
uint8_t v = b ^ A ^ x;
2015-12-01 19:51:45 +00:00
flags.P = (v >> 7) ^ flags.C;
flags.H = (v >> 4) & 1;
}
2018-08-13 13:41:23 +00:00
inline void _adc16(uint16_t w) {
2019-02-27 19:12:45 +00:00
_memptr = HL+1;
2015-12-01 19:51:45 +00:00
_mc(IR, 1); _mc(IR, 1); _mc(IR, 1);
_mc(IR, 1); _mc(IR, 1); _mc(IR, 1); _mc(IR, 1);
unsigned long r = HL + w + flags.C;
2018-08-13 13:41:23 +00:00
uint8_t h = H;
2015-12-01 19:51:45 +00:00
HL = (r & 0xffff);
_sz35(H);
flags.Z = (HL == 0);
flags.C = (r > 0xffff);
flags.N = 0;
2018-08-13 13:41:23 +00:00
uint8_t v = h ^ H ^ (w >> 8);
2015-12-01 19:51:45 +00:00
flags.P = (v >> 7) ^ flags.C;
flags.H = (v >> 4) & 1;
}
2018-08-13 13:41:23 +00:00
inline void _add16(uint16_t &reg, uint16_t w) {
2015-12-01 19:51:45 +00:00
_mc(IR, 1); _mc(IR, 1); _mc(IR, 1);
_mc(IR, 1); _mc(IR, 1); _mc(IR, 1); _mc(IR, 1);
2019-02-26 16:06:21 +00:00
_memptr = reg+1;
2015-12-01 19:51:45 +00:00
unsigned long r = reg + w;
2018-08-13 13:41:23 +00:00
uint8_t o = reg >> 8;
2015-12-01 19:51:45 +00:00
reg = (r & 0xffff);
2018-08-13 13:41:23 +00:00
uint8_t h = reg >> 8;
2015-12-01 19:51:45 +00:00
_35(h);
flags.C = (r > 0xffff);
flags.N = 0;
2018-08-13 13:41:23 +00:00
uint8_t v = o ^ h ^ (w >> 8);
2015-12-01 19:51:45 +00:00
flags.H = (v >> 4) & 1;
}
2018-08-13 13:41:23 +00:00
inline void _sub(uint8_t x) {
2015-12-01 19:51:45 +00:00
flags.C = 1;
_adc(~x);
flags.C = !flags.C;
flags.H = !flags.H;
flags.N = 1;
}
2018-08-13 13:41:23 +00:00
inline void _sbc(uint8_t x) {
2015-12-01 19:51:45 +00:00
flags.C = !flags.C;
_adc(~x);
flags.C = !flags.C;
flags.H = !flags.H;
flags.N = 1;
}
2018-08-13 13:41:23 +00:00
inline void _sbc16(uint16_t w) {
2015-12-01 19:51:45 +00:00
flags.C = !flags.C;
_adc16(~w);
flags.C = !flags.C;
flags.H = !flags.H;
flags.N = 1;
}
2018-08-13 13:41:23 +00:00
inline uint16_t _ads(uint16_t a, uint8_t b) {
uint16_t w = a + b;
2016-01-21 06:32:44 +00:00
if (b > 127) w -= 0x100;
return w;
}
2015-12-01 19:51:45 +00:00
inline void _incO(Memory::address a) {
2018-08-13 13:41:23 +00:00
uint8_t o = _rb(PC);
2015-12-01 19:51:45 +00:00
_mc(PC, 1); _mc(PC, 1); _mc(PC, 1);
_mc(PC, 1); _mc(PC, 1);
PC++;
2018-08-13 13:41:23 +00:00
uint16_t w = _ads(a, o);
uint8_t b = _rb(w);
2015-12-01 19:51:45 +00:00
_mc(w, 1);
_inc(b);
_sb(w, b);
2019-02-26 16:06:21 +00:00
_memptr = w;
2015-12-01 19:51:45 +00:00
}
inline void _decO(Memory::address a) {
2018-08-13 13:41:23 +00:00
uint8_t o = _rb(PC);
2015-12-01 19:51:45 +00:00
_mc(PC, 1); _mc(PC, 1); _mc(PC, 1);
_mc(PC, 1); _mc(PC, 1);
PC++;
2018-08-13 13:41:23 +00:00
uint16_t w = _ads(a, o);
uint8_t b = _rb(w);
2015-12-01 19:51:45 +00:00
_mc(w, 1);
_dec(b);
_sb(w, b);
2019-02-26 16:06:21 +00:00
_memptr = w;
2015-12-01 19:51:45 +00:00
}
inline void _sbO(Memory::address a) {
2018-08-13 13:41:23 +00:00
uint8_t o = _rb(PC++);
uint8_t b = _rb(PC);
2015-12-01 19:51:45 +00:00
_mc(PC, 1); _mc(PC, 1);
PC++;
2019-02-27 19:12:45 +00:00
_memptr = _ads(a, o);
_sb(_memptr, b);
2015-12-01 19:51:45 +00:00
}
2018-08-13 13:41:23 +00:00
inline uint8_t _rbO(Memory::address a) {
uint8_t o = _rb(PC);
2015-12-01 19:51:45 +00:00
_mc(PC, 1); _mc(PC, 1); _mc(PC, 1);
_mc(PC, 1); _mc(PC, 1);
PC++;
2019-02-27 19:12:45 +00:00
_memptr = _ads(a, o);
return _rb(_memptr);
2015-12-01 19:51:45 +00:00
}
2018-08-13 13:41:23 +00:00
inline void _sbO(Memory::address a, uint8_t b) {
uint8_t o = _rb(PC);
2015-12-01 19:51:45 +00:00
_mc(PC, 1); _mc(PC, 1); _mc(PC, 1);
_mc(PC, 1); _mc(PC, 1);
PC++;
2019-02-27 19:12:45 +00:00
_memptr = _ads(a, o);
_sb(_memptr, b);
2015-12-01 19:51:45 +00:00
}
2018-08-13 13:41:23 +00:00
inline void _exSP(uint16_t &reg) {
2019-02-27 19:12:45 +00:00
_memptr = _pop();
2019-02-26 16:06:21 +00:00
_mc(SP-1, 1);
_push(reg);
2019-02-27 19:12:45 +00:00
reg = _memptr;
2015-12-01 19:51:45 +00:00
_mc(SP, 1); _mc(SP, 1);
}
2019-02-26 16:06:21 +00:00
inline void _exch(uint16_t &a, uint16_t &b) {
uint16_t t = b;
b = a;
a = t;
2019-02-25 13:21:41 +00:00
}
2015-12-01 19:51:45 +00:00
2018-08-13 13:41:23 +00:00
inline uint16_t _pop() { uint16_t w = _rw(SP); SP += 2; return w; }
inline void _push(uint16_t w) { SP -= 2; _sw(SP, w); }
2015-12-01 19:51:45 +00:00
2019-02-26 16:06:21 +00:00
inline void _jmp(uint8_t c) { _memptr = _rw(PC); if (c) PC = _memptr; else PC += 2; }
2018-08-13 13:41:23 +00:00
inline void _ret(uint8_t c) { _mc(IR, 1); if (c) ret(); }
2019-02-26 16:06:21 +00:00
inline void _call(uint8_t c) { _memptr = _rw(PC); if (c) { _mc(PC+1, 1); _push(PC+2); PC = _memptr; } else PC += 2; }
2018-08-13 13:41:23 +00:00
inline void _jr(uint8_t c) { if (c) jr(); else { _mc(PC, 3); PC++; } }
2015-12-01 19:51:45 +00:00
// 0x00
2019-02-25 13:21:41 +00:00
inline void nop() {}
inline void ldbcpc() { BC = _rwpc(); }
2019-02-26 16:06:21 +00:00
inline void ldBCa() { _sb(BC, A); MPH = A; MPL = C+1; }
2019-02-25 13:21:41 +00:00
inline void incbc() { BC++; _mc(IR, 1); _mc(IR, 1); }
inline void incb() { _inc(B); }
inline void decb() { _dec(B); }
inline void ldb() { B = _rb(PC++); }
inline void rlca() { flags.C = ((A & 0x80) >> 7); A = (A << 1) | flags.C; }
2015-12-01 19:51:45 +00:00
// 0x08
2019-02-25 13:21:41 +00:00
inline void exaf() { _exch(AF, AF_); }
2019-02-27 19:12:45 +00:00
inline void addhlbc() { _add16(HL, BC); }
2019-02-26 16:06:21 +00:00
inline void ldaBC() { A = _rb(BC); _memptr = BC+1; }
2019-02-25 13:21:41 +00:00
inline void decbc() { BC--; _mc(IR, 1); _mc(IR, 1); }
inline void incc() { _inc(C); }
inline void decc() { _dec(C); }
inline void ldc() { C = _rb(PC++); }
inline void rrca() {
2015-12-01 19:51:45 +00:00
flags.H = flags.N = 0;
2018-08-10 10:13:45 +00:00
flags.C = (A & 0x01);
A = (A >> 1) | (flags.C << 7);
2015-12-01 19:51:45 +00:00
_35(A);
}
// 0x10
2019-02-25 13:21:41 +00:00
inline void djnz() { _mc(IR, 1); _jr(--B); }
inline void lddepc() { DE = _rwpc(); }
2019-02-26 16:06:21 +00:00
inline void ldDEa() { _sb(DE, A); MPH = A; MPL = E+1; }
2019-02-25 13:21:41 +00:00
inline void incde() { DE++; _mc(IR, 1); _mc(IR, 1); }
inline void incd() { _inc(D); }
inline void decd() { _dec(D); }
inline void ldd() { D = _rb(PC++); }
inline void rla() {
2018-08-13 13:41:23 +00:00
uint8_t b = (A << 1) | flags.C;
2015-12-01 19:51:45 +00:00
flags.C = (A & 0x80) >> 7;
A = b;
}
// 0x18
2019-02-25 13:21:41 +00:00
inline void jr() {
2018-08-13 13:41:23 +00:00
uint8_t b = _rb(PC);
2018-08-10 10:13:45 +00:00
_mc(PC, 1); _mc(PC, 1); _mc(PC, 1); _mc(PC, 1); _mc(PC, 1);
2019-02-26 16:06:21 +00:00
_memptr = PC = _ads(PC, b+1);
2016-01-21 06:32:44 +00:00
}
2019-02-27 19:12:45 +00:00
inline void addhlde() { _add16(HL, DE); }
2019-02-26 16:06:21 +00:00
inline void ldaDE() { A = _rb(DE); _memptr = DE+1; }
2019-02-25 13:21:41 +00:00
inline void decde() { DE--; _mc(IR, 1); _mc(IR, 1); }
inline void ince() { _inc(E); }
inline void dece() { _dec(E); }
inline void lde() { E = _rb(PC++); }
inline void rra() {
2018-08-13 13:41:23 +00:00
uint8_t b = (A >> 1) | (flags.C << 7);
2015-12-01 19:51:45 +00:00
flags.C = (A & 1);
A = b;
}
// 0x20
2019-02-25 13:21:41 +00:00
inline void jrnz() { _jr(!flags.Z); }
inline void ldhlpc() { HL = _rwpc(); }
inline void ldPChl() { _swPC(HL); }
inline void inchl() { HL++; _mc(IR, 1); _mc(IR, 1); }
inline void inch() { _inc(H); }
inline void dech() { _dec(H); }
inline void ldh() { H = _rb(PC++); }
inline void daa();
2015-12-01 19:51:45 +00:00
// 0x28
2019-02-25 13:21:41 +00:00
inline void jrz() { _jr(flags.Z); }
inline void addhlhl() { _add16(HL, HL); }
inline void ldhlPC() { HL = _rwPC(); }
inline void dechl() { HL--; _mc(IR, 1); _mc(IR, 1); }
inline void incl() { _inc(L); }
inline void decl() { _dec(L); }
inline void ldl() { L = _rb(PC++); }
inline void cpl() { A = ~A; flags.H = flags.N = 1; _35(A); }
2015-12-01 19:51:45 +00:00
// 0x30
2019-02-25 13:21:41 +00:00
inline void jrnc() { _jr(!flags.C); }
inline void ldsppc() { SP = _rwpc(); }
2019-02-26 16:06:21 +00:00
inline void ldPCa() { uint16_t a = _rw(PC); _sb(a, A); PC += 2; MPH=A; MPL=a+1; }
2019-02-25 13:21:41 +00:00
inline void incsp() { SP++; _mc(IR, 1); _mc(IR, 1); }
inline void incHL() { uint8_t b = _rb(HL); _mc(HL, 1); _inc(b); _sb(HL, b); }
inline void decHL() { uint8_t b = _rb(HL); _mc(HL, 1); _dec(b); _sb(HL, b); }
inline void ldHL() { _sb(HL, _rb(PC++)); }
inline void scf() { flags.C = 1; flags.N = flags.H = 0; _35(A); }
2015-12-01 19:51:45 +00:00
// 0x38
2019-02-25 13:21:41 +00:00
inline void jrc() { _jr(flags.C); }
inline void addhlsp() { _add16(HL, SP); }
2019-02-26 16:06:21 +00:00
inline void ldaPC() { uint16_t a = _rw(PC); A = _rb(a); PC += 2; _memptr = a+1; }
2019-02-25 13:21:41 +00:00
inline void decsp() { SP--; _mc(IR, 1); _mc(IR, 1); }
inline void inca() { _inc(A); }
inline void deca() { _dec(A); }
inline void lda() { A = _rb(PC++); }
inline void ccf() { flags.H = flags.C; flags.C = flags.N = 0; _35(A); }
2015-12-01 19:51:45 +00:00
// 0x40
2019-02-25 13:21:41 +00:00
inline void ldbb() {}
inline void ldbc() { B = C; }
inline void ldbd() { B = D; }
inline void ldbe() { B = E; }
inline void ldbh() { B = H; }
inline void ldbl() { B = L; }
inline void ldbHL() { B = _rb(HL); }
inline void ldba() { B = A; }
2015-12-01 19:51:45 +00:00
// 0x48
2019-02-25 13:21:41 +00:00
inline void ldcb() { C = B; }
inline void ldcc() {}
inline void ldcd() { C = D; }
inline void ldce() { C = E; }
inline void ldch() { C = H; }
inline void ldcl() { C = L; }
inline void ldcHL() { C = _rb(HL); }
inline void ldca() { C = A; }
2015-12-01 19:51:45 +00:00
// 0x50
2019-02-25 13:21:41 +00:00
inline void lddb() { D = B; }
inline void lddc() { D = C; }
inline void lddd() {}
inline void ldde() { D = E; }
inline void lddh() { D = H; }
inline void lddl() { D = L; }
inline void lddHL() { D = _rb(HL); }
inline void ldda() { D = A; }
2015-12-01 19:51:45 +00:00
// 0x58
2019-02-25 13:21:41 +00:00
inline void ldeb() { E = B; }
inline void ldec() { E = C; }
inline void lded() { E = D; }
inline void ldee() {}
inline void ldeh() { E = H; }
inline void ldel() { E = L; }
inline void ldeHL() { E = _rb(HL); }
inline void ldea() { E = A; }
2015-12-01 19:51:45 +00:00
// 0x60
2019-02-25 13:21:41 +00:00
inline void ldhb() { H = B; }
inline void ldhc() { H = C; }
inline void ldhd() { H = D; }
inline void ldhe() { H = E; }
inline void ldhh() {}
inline void ldhl() { H = L; }
inline void ldhHL() { H = _rb(HL); }
inline void ldha() { H = A; }
2015-12-01 19:51:45 +00:00
// 0x68
2019-02-25 13:21:41 +00:00
inline void ldlb() { L = B; }
inline void ldlc() { L = C; }
inline void ldld() { L = D; }
inline void ldle() { L = E; }
inline void ldlh() { L = H; }
inline void ldll() {}
inline void ldlHL() { L = _rb(HL); }
inline void ldla() { L = A; }
2015-12-01 19:51:45 +00:00
// 0x70
2019-02-25 13:21:41 +00:00
inline void ldHLb() { _sb(HL, B); }
inline void ldHLc() { _sb(HL, C); }
inline void ldHLd() { _sb(HL, D); }
inline void ldHLe() { _sb(HL, E); }
inline void ldHLh() { _sb(HL, H); }
inline void ldHLl() { _sb(HL, L); }
inline void halt() { _halted = true; PC--; }
inline void ldHLa() { _sb(HL, A); }
2015-12-01 19:51:45 +00:00
// 0x78
2019-02-25 13:21:41 +00:00
inline void ldab() { A = B; }
inline void ldac() { A = C; }
inline void ldad() { A = D; }
inline void ldae() { A = E; }
inline void ldah() { A = H; }
inline void ldal() { A = L; }
inline void ldaHL() { A = _rb(HL); }
inline void ldaa() {}
2015-12-01 19:51:45 +00:00
// 0x80
2019-02-25 13:21:41 +00:00
inline void addab() { _add(B); }
inline void addac() { _add(C); }
inline void addad() { _add(D); }
inline void addae() { _add(E); }
inline void addah() { _add(H); }
inline void addal() { _add(L); }
inline void addaHL() { _add(_rb(HL)); }
inline void addaa() { _add(A); }
2015-12-01 19:51:45 +00:00
// 0x88
2019-02-25 13:21:41 +00:00
inline void adcab() { _adc(B); }
inline void adcac() { _adc(C); }
inline void adcad() { _adc(D); }
inline void adcae() { _adc(E); }
inline void adcah() { _adc(H); }
inline void adcal() { _adc(L); }
inline void adcaHL() { _adc(_rb(HL)); }
inline void adcaa() { _adc(A); }
2015-12-01 19:51:45 +00:00
// 0x90
2019-02-25 13:21:41 +00:00
inline void subab() { _sub(B); }
inline void subac() { _sub(C); }
inline void subad() { _sub(D); }
inline void subae() { _sub(E); }
inline void subah() { _sub(H); }
inline void subal() { _sub(L); }
inline void subaHL() { _sub(_rb(HL)); }
inline void subaa() { _sub(A); }
2015-12-01 19:51:45 +00:00
// 0x98
2019-02-25 13:21:41 +00:00
inline void sbcab() { _sbc(B); }
inline void sbcac() { _sbc(C); }
inline void sbcad() { _sbc(D); }
inline void sbcae() { _sbc(E); }
inline void sbcah() { _sbc(H); }
inline void sbcal() { _sbc(L); }
inline void sbcaHL() { _sbc(_rb(HL)); }
inline void sbcaa() { _sbc(A); }
2015-12-01 19:51:45 +00:00
// 0xa0
2018-08-13 13:41:23 +00:00
inline void _and(uint8_t b) {
2015-12-01 19:51:45 +00:00
A &= b;
_szp35(A);
flags.C = flags.N = 0;
flags.H = 1;
}
2019-02-25 13:21:41 +00:00
inline void andb() { _and(B); }
inline void andc() { _and(C); }
inline void andd() { _and(D); }
inline void ande() { _and(E); }
inline void andh() { _and(H); }
inline void andl() { _and(L); }
inline void andHL() { _and(_rb(HL)); }
inline void anda() { _and(A); }
2015-12-01 19:51:45 +00:00
// 0xa8
2018-08-13 13:41:23 +00:00
inline void _xor(uint8_t b) {
2015-12-01 19:51:45 +00:00
A ^= b;
_szp35(A);
flags.C = flags.N = flags.H = 0;
}
2019-02-25 13:21:41 +00:00
inline void xorb() { _xor(B); }
inline void xorc() { _xor(C); }
inline void xord() { _xor(D); }
inline void xore() { _xor(E); }
inline void xorh() { _xor(H); }
inline void xorl() { _xor(L); }
inline void xorHL() { _xor(_rb(HL)); }
inline void xora() { _xor(A); }
2015-12-01 19:51:45 +00:00
// 0xb0
2018-08-13 13:41:23 +00:00
inline void _or(uint8_t b) {
2015-12-01 19:51:45 +00:00
A |= b;
_szp35(A);
flags.C = flags.N = flags.H = 0;
}
2019-02-25 13:21:41 +00:00
inline void orb() { _or(B); }
inline void orc() { _or(C); }
inline void ord() { _or(D); }
inline void ore() { _or(E); }
inline void orh() { _or(H); }
inline void orl() { _or(L); }
inline void orHL() { _or(_rb(HL)); }
inline void ora() { _or(A); }
2015-12-01 19:51:45 +00:00
// 0xb8
2018-08-13 13:41:23 +00:00
inline void _cmp(uint8_t b) {
uint8_t a = A;
2015-12-01 19:51:45 +00:00
_sub(b);
_35(b);
A = a;
}
2019-02-25 13:21:41 +00:00
inline void cpb() { _cmp(B); }
inline void cpc() { _cmp(C); }
inline void cpd() { _cmp(D); }
inline void cpe() { _cmp(E); }
inline void cph() { _cmp(H); }
inline void cpL() { _cmp(L); }
inline void cpHL() { _cmp(_rb(HL)); }
inline void cpa() { _cmp(A); }
2015-12-01 19:51:45 +00:00
// 0xc0
2019-02-25 13:21:41 +00:00
inline void retnz() { _ret(!flags.Z); }
inline void popbc() { BC = _pop(); }
inline void jpnz() { _jmp(!flags.Z); }
2019-02-26 16:06:21 +00:00
inline void jp() { _memptr = PC = _rw(PC); }
2019-02-25 13:21:41 +00:00
inline void callnz() { _call(!flags.Z); }
inline void pushbc() { _mc(IR, 1); _push(BC); }
inline void adda() { _add(_rb(PC++)); }
2019-02-26 16:06:21 +00:00
inline void rst00() { _mc(IR, 1); _push(PC); _memptr = PC = 0x00; }
2015-12-01 19:51:45 +00:00
// 0xc8
2019-02-25 13:21:41 +00:00
inline void retz() { _ret(flags.Z); }
2019-02-26 16:06:21 +00:00
inline void ret() { _memptr = PC = _pop(); }
2019-02-25 13:21:41 +00:00
inline void jpz() { _jmp(flags.Z); }
inline void cb();
inline void callz() { _call(flags.Z); }
2019-02-26 16:06:21 +00:00
inline void call() { uint16_t pc = _rw(PC); _mc(PC+1, 1); _push(PC+2); _memptr = PC = pc; }
2019-02-25 13:21:41 +00:00
inline void adca() { _adc(_rb(PC++)); }
2019-02-26 16:06:21 +00:00
inline void rst08() { _mc(IR, 1); _push(PC); _memptr = PC = 0x08; }
2015-12-01 19:51:45 +00:00
// 0xd0
2019-02-25 13:21:41 +00:00
inline void retnc() { _ret(!flags.C); }
inline void popde() { DE = _pop(); }
inline void jpnc() { _jmp(!flags.C); }
inline void outa() {
2019-02-26 16:06:21 +00:00
uint8_t b = _rb(PC++);
uint16_t p = b + (A << 8);
MPH = A; MPL = b+1;
2015-12-01 19:51:45 +00:00
_ports->out(p, A, this);
}
2019-02-25 13:21:41 +00:00
inline void callnc() { _call(!flags.C); }
inline void pushde() { _mc(IR, 1); _push(DE); }
inline void suba() { _sub(_rb(PC++)); }
2019-02-26 16:06:21 +00:00
inline void rst10() { _mc(IR, 1); _push(PC); _memptr = PC = 0x10; }
2015-12-01 19:51:45 +00:00
// 0xd8
2019-02-25 13:21:41 +00:00
inline void retc() { _ret(flags.C); }
inline void exx() { _exch(BC, BC_); _exch(DE, DE_); _exch(HL, HL_); }
inline void jpc() { _jmp(flags.C); }
inline void ina() {
2019-02-26 16:06:21 +00:00
uint8_t b = _rb(PC++);
uint16_t p = b + (A << 8);
2015-12-01 19:51:45 +00:00
A = _ports->in(p, this);
2019-02-26 16:06:21 +00:00
MPH = A; MPL = b+1;
2015-12-01 19:51:45 +00:00
}
2019-02-25 13:21:41 +00:00
inline void callc() { _call(flags.C); }
2019-02-25 14:55:27 +00:00
inline void dd() { _ddfd(IX, IXL, IXH, &z80::ddcb); }
2019-02-25 13:21:41 +00:00
inline void sbca() { _sbc(_rb(PC++)); }
2019-02-26 16:06:21 +00:00
inline void rst18() { _mc(IR, 1); _push(PC); _memptr = PC = 0x18; }
2015-12-01 19:51:45 +00:00
// 0xe0
2019-02-25 13:21:41 +00:00
inline void retpo() { _ret(!flags.P); }
inline void pophl() { HL = _pop(); }
inline void jppo() { _jmp(!flags.P); }
inline void exSPhl() { _exSP(HL); }
inline void callpo() { _call(!flags.P); }
inline void pushhl() { _mc(IR, 1); _push(HL); }
2019-02-26 16:06:21 +00:00
inline void and_() { uint16_t a = _rb(PC++); _and(a); }
inline void rst20() { _mc(IR, 1); _push(PC); _memptr = PC = 0x20; }
2015-12-01 19:51:45 +00:00
// 0xe8
2019-03-25 16:12:08 +00:00
inline uint8_t _inr() {
_memptr = BC+1;
uint8_t b = _ports->in(BC, this);
2015-12-01 19:51:45 +00:00
_szp35(b);
flags.N = flags.H = 0;
return b;
}
2019-03-25 16:12:08 +00:00
inline void _outr(uint8_t b) {
_memptr = BC+1;
_ports->out(BC, b, this);
2015-12-01 19:51:45 +00:00
}
2019-02-25 13:21:41 +00:00
inline void retpe() { _ret(flags.P); }
inline void jphl() { PC = HL; }
inline void jppe() { _jmp(flags.P); }
inline void exdehl() { _exch(DE, HL); }
inline void callpe() { _call(flags.P); }
inline void ed();
inline void xor_() { _xor(_rb(PC++)); }
2019-02-26 16:06:21 +00:00
inline void rst28() { _mc(IR, 1); _push(PC); _memptr = PC = 0x28; }
2015-12-01 19:51:45 +00:00
// 0xf0
2019-02-25 13:21:41 +00:00