Compare commits

...

5 Commits

Author SHA1 Message Date
Adrian Conlon 0a9a1e5d4c Complete all the undocumente M6502 features. Hurrah! 2024-01-07 16:20:58 +00:00
Adrian Conlon 72be3238f2 Undocumented M6502 instruction implemented 2024-01-07 15:52:16 +00:00
Adrian Conlon 86ef340650 Tidy arithmetic overflow handling in M6502 core 2024-01-07 14:35:12 +00:00
Adrian Conlon 4f4bc5355d Simplification of M6502 flag set/reset code 2024-01-07 14:04:41 +00:00
Adrian Conlon 7eca073a6e Make explicit swallow operations. 2024-01-07 12:15:11 +00:00
3 changed files with 156 additions and 104 deletions

View File

@ -137,14 +137,22 @@ namespace EightBit {
// Flag adjustment
constexpr void adjustZero(const uint8_t datum) noexcept { P() = clearBit(P(), ZF, datum); }
constexpr void adjustNegative(const uint8_t datum) noexcept { P() = setBit(P(), NF, negative(datum)); }
constexpr void adjustZero(const uint8_t datum) noexcept { reset_flag(ZF, datum); }
constexpr void adjustNegative(const uint8_t datum) noexcept { set_flag(NF, negative(datum)); }
constexpr void adjustNZ(const uint8_t datum) noexcept {
adjustZero(datum);
adjustNegative(datum);
}
constexpr void adjustOverflow_add(uint8_t operand, uint8_t data, uint8_t intermediate) noexcept {
set_flag(VF, negative(~(operand ^ data) & (operand ^ intermediate)));
}
constexpr void adjustOverflow_subtract(uint8_t operand, uint8_t data, uint8_t intermediate) noexcept {
set_flag(VF, negative((operand ^ data) & (operand ^ intermediate)));
}
// Miscellaneous
void branch(int condition) noexcept;
@ -160,25 +168,66 @@ namespace EightBit {
memoryWrite(data);
}
// Unconditional page fixup cycle required
void fixup(const register16_t address, const uint8_t unfixed_page) noexcept {
getBytePaged(unfixed_page, address.low); // Possible fixup for page boundary crossing
BUS().ADDRESS() = address;
}
void maybe_fixup(const register16_t address, const uint8_t unfixed_page) noexcept {
if (address.high != unfixed_page)
fixup(address, unfixed_page);
}
// Status flag operations
constexpr void set_flag(int which, int condition) noexcept { P() = setBit(P(), which, condition); }
constexpr void set_flag(int which) noexcept { P() = setBit(P(), which); }
constexpr void reset_flag(int which, int condition) noexcept { P() = clearBit(P(), which, condition); }
constexpr void reset_flag(int which) noexcept { P() = clearBit(P(), which); }
// Chew up a cycle
void swallow() noexcept { memoryRead(PC()); }
void swallow_stack() noexcept { getBytePaged(1, S()); }
void swallow_fetch() noexcept { fetchByte(); }
// Instruction implementations
[[nodiscard]] uint8_t andr(uint8_t operand, uint8_t data) noexcept;
[[nodiscard]] uint8_t asl(uint8_t value) noexcept;
void bit(uint8_t operand, uint8_t data) noexcept;
void cmp(uint8_t first, uint8_t second) noexcept;
[[nodiscard]] uint8_t dec(uint8_t value) noexcept;
[[nodiscard]] uint8_t eorr(uint8_t operand, uint8_t data) noexcept;
[[nodiscard]] uint8_t inc(uint8_t value) noexcept;
void jsr() noexcept;
[[nodiscard]] uint8_t lsr(uint8_t value) noexcept;
[[nodiscard]] uint8_t orr(uint8_t operand, uint8_t data) noexcept;
void php() noexcept;
void plp() noexcept;
[[nodiscard]] uint8_t rol(uint8_t operand) noexcept;
[[nodiscard]] uint8_t ror(uint8_t operand) noexcept;
void rti() noexcept;
void rts() noexcept;
[[nodiscard]] constexpr uint8_t asl(uint8_t value) noexcept {
set_flag(CF, value & Bit7);
return through(value << 1);
}
[[nodiscard]] constexpr uint8_t rol(uint8_t operand) noexcept {
const auto carryIn = carry();
return through(asl(operand) | carryIn);
}
[[nodiscard]] constexpr uint8_t lsr(uint8_t value) noexcept {
set_flag(CF, value & Bit0);
return through(value >> 1);
}
[[nodiscard]] constexpr uint8_t ror(uint8_t operand) noexcept {
const auto carryIn = carry();
return through(lsr(operand) | (carryIn << 7));
}
// Undocumented compound instructions
void anc(uint8_t value) noexcept;
@ -192,12 +241,6 @@ namespace EightBit {
void sre(uint8_t value) noexcept;
void jam() noexcept;
// Unconditional page fixup cycle required
void fixup(const register16_t address, const uint8_t unfixed_page) noexcept {
getBytePaged(unfixed_page, address.low); // Possible fixup for page boundary crossing
BUS().ADDRESS() = address;
}
// Complicated addressing mode implementations
void sta_AbsoluteX() noexcept;
@ -209,7 +252,7 @@ namespace EightBit {
memoryWrite(A());
}
// Undocumented complicated mode implementations
// Undocumented complicated mode implementations
// SLO
void slo_AbsoluteX() noexcept;
@ -265,6 +308,16 @@ namespace EightBit {
sre(memoryRead());
}
// SHA
void sha_AbsoluteY() noexcept;
void sha_IndirectIndexedY() noexcept;
// TAS
void tas_AbsoluteY() noexcept;
// LAS
void las_AbsoluteY() noexcept;
// SYA
void sya_AbsoluteX() noexcept;

View File

@ -2,7 +2,7 @@
#include "../inc/mos6502.h"
EightBit::MOS6502::MOS6502(Bus& bus) noexcept
: LittleEndianProcessor(bus) {
: LittleEndianProcessor(bus) {
RaisedPOWER.connect([this](EventArgs) {
X() = Bit7;
Y() = 0;
@ -100,7 +100,7 @@ void EightBit::MOS6502::interrupt() noexcept {
pushWord(PC());
push(P() | (software ? BF : 0));
}
P() = setBit(P(), IF); // Disable IRQ
set_flag(IF); // Disable IRQ
const uint8_t vector = reset ? RSTvector : (nmi ? NMIvector : IRQvector);
jump(getWordPaged(0xff, vector));
m_handlingRESET = m_handlingNMI = m_handlingINT = false;
@ -128,7 +128,7 @@ int EightBit::MOS6502::execute() noexcept {
switch (opcode()) {
case 0x00: fetchByte(); interrupt(); break; // BRK (implied)
case 0x00: swallow_fetch(); interrupt(); break; // BRK (implied)
case 0x01: A() = orr(A(), AM_IndexedIndirectX()); break; // ORA (indexed indirect X)
case 0x02: jam(); break; // *JAM
case 0x03: slo(AM_IndexedIndirectX()); break; // *SLO (indexed indirect X)
@ -136,11 +136,11 @@ int EightBit::MOS6502::execute() noexcept {
case 0x05: A() = orr(A(), AM_ZeroPage()); break; // ORA (zero page)
case 0x06: memoryReadModifyWrite(asl(AM_ZeroPage())); break; // ASL (zero page)
case 0x07: slo(AM_ZeroPage()); break; // *SLO (zero page)
case 0x08: memoryRead(PC()); php(); break; // PHP (implied)
case 0x08: swallow(); php(); break; // PHP (implied)
case 0x09: A() = orr(A(), AM_Immediate()); break; // ORA (immediate)
case 0x0a: memoryRead(PC()); A() = asl(A()); break; // ASL A (implied)
case 0x0a: swallow(); A() = asl(A()); break; // ASL A (implied)
case 0x0b: anc(AM_Immediate()); break; // *ANC (immediate)
case 0x0c: { auto ignored = Address_Absolute(); } break; // *NOP (absolute)
case 0x0c: { auto ignored = Address_Absolute(); } break; // *NOP (absolute)
case 0x0d: A() = orr(A(), AM_Absolute()); break; // ORA (absolute)
case 0x0e: memoryReadModifyWrite(asl(AM_Absolute())); break; // ASL (absolute)
case 0x0f: slo(AM_Absolute()); break; // *SLO (absolute)
@ -153,9 +153,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0x15: A() = orr(A(), AM_ZeroPageX()); break; // ORA (zero page, X)
case 0x16: memoryReadModifyWrite(asl(AM_ZeroPageX())); break; // ASL (zero page, X)
case 0x17: slo(AM_ZeroPageX()); break; // *SLO (zero page, X)
case 0x18: memoryRead(PC()); P() = clearBit(P(), CF); break; // CLC (implied)
case 0x18: swallow(); reset_flag(CF); break; // CLC (implied)
case 0x19: A() = orr(A(), AM_AbsoluteY()); break; // ORA (absolute, Y)
case 0x1a: memoryRead(PC()); break; // *NOP (implied)
case 0x1a: swallow(); break; // *NOP (implied)
case 0x1b: slo_AbsoluteY(); break; // *SLO (absolute, Y)
case 0x1c: nop_AbsoluteX(); break; // *NOP (absolute, X)
case 0x1d: A() = orr(A(), AM_AbsoluteX()); break; // ORA (absolute, X)
@ -170,9 +170,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0x25: A() = andr(A(), AM_ZeroPage()); break; // AND (zero page)
case 0x26: memoryReadModifyWrite(rol(AM_ZeroPage())); break; // ROL (zero page)
case 0x27: rla(AM_ZeroPage()); break; // *RLA (zero page)
case 0x28: memoryRead(PC()); getBytePaged(1, S()); plp(); break; // PLP (implied)
case 0x28: swallow(); plp(); break; // PLP (implied)
case 0x29: A() = andr(A(), AM_Immediate()); break; // AND (immediate)
case 0x2a: memoryRead(PC()); A() = rol(A()); break; // ROL A (implied)
case 0x2a: swallow(); A() = rol(A()); break; // ROL A (implied)
case 0x2b: anc(AM_Immediate()); break; // *ANC (immediate)
case 0x2c: bit(A(), AM_Absolute()); break; // BIT (absolute)
case 0x2d: A() = andr(A(), AM_Absolute()); break; // AND (absolute)
@ -187,16 +187,16 @@ int EightBit::MOS6502::execute() noexcept {
case 0x35: A() = andr(A(), AM_ZeroPageX()); break; // AND (zero page, X)
case 0x36: memoryReadModifyWrite(rol(AM_ZeroPageX())); break; // ROL (zero page, X)
case 0x37: rla(AM_ZeroPageX()); break; // *RLA (zero page, X)
case 0x38: memoryRead(PC()); P() = setBit(P(), CF); break; // SEC (implied)
case 0x38: swallow(); set_flag(CF); break; // SEC (implied)
case 0x39: A() = andr(A(), AM_AbsoluteY()); break; // AND (absolute, Y)
case 0x3a: memoryRead(PC()); break; // *NOP (implied)
case 0x3a: swallow(); break; // *NOP (implied)
case 0x3b: rla_AbsoluteY(); break; // *RLA (absolute, Y)
case 0x3c: nop_AbsoluteX(); break; // *NOP (absolute, X)
case 0x3d: A() = andr(A(), AM_AbsoluteX()); break; // AND (absolute, X)
case 0x3e: memoryReadModifyWrite(rol(AM_AbsoluteX(PageCrossingBehavior::AlwaysReadTwice))); break; // ROL (absolute, X)
case 0x3f: rla_AbsoluteX(); break; // *RLA (absolute, X)
case 0x40: memoryRead(PC()); rti(); break; // RTI (implied)
case 0x40: swallow(); rti(); break; // RTI (implied)
case 0x41: A() = eorr(A(), AM_IndexedIndirectX()); break; // EOR (indexed indirect X)
case 0x42: jam(); break; // *JAM
case 0x43: sre(AM_IndexedIndirectX()); break; // *SRE (indexed indirect X)
@ -204,9 +204,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0x45: A() = eorr(A(), AM_ZeroPage()); break; // EOR (zero page)
case 0x46: memoryReadModifyWrite(lsr(AM_ZeroPage())); break; // LSR (zero page)
case 0x47: sre(AM_ZeroPage()); break; // *SRE (zero page)
case 0x48: memoryRead(PC()); push(A()); break; // PHA (implied)
case 0x48: swallow(); push(A()); break; // PHA (implied)
case 0x49: A() = eorr(A(), AM_Immediate()); break; // EOR (immediate)
case 0x4a: memoryRead(PC()); A() = lsr(A()); break; // LSR A (implied)
case 0x4a: swallow(); A() = lsr(A()); break; // LSR A (implied)
case 0x4b: asr(AM_Immediate()); break; // *ASR (immediate)
case 0x4c: jump(Address_Absolute()); break; // JMP (absolute)
case 0x4d: A() = eorr(A(), AM_Absolute()); break; // EOR (absolute)
@ -221,16 +221,16 @@ int EightBit::MOS6502::execute() noexcept {
case 0x55: A() = eorr(A(), AM_ZeroPageX()); break; // EOR (zero page, X)
case 0x56: memoryReadModifyWrite(lsr(AM_ZeroPageX())); break; // LSR (zero page, X)
case 0x57: sre(AM_ZeroPageX()); break; // *SRE (zero page, X)
case 0x58: memoryRead(PC()); P() = clearBit(P(), IF); break; // CLI (implied)
case 0x58: swallow(); reset_flag(IF); break; // CLI (implied)
case 0x59: A() = eorr(A(), AM_AbsoluteY()); break; // EOR (absolute, Y)
case 0x5a: memoryRead(PC()); break; // *NOP (implied)
case 0x5a: swallow(); break; // *NOP (implied)
case 0x5b: sre_AbsoluteY(); break; // *SRE (absolute, Y)
case 0x5c: nop_AbsoluteX(); break; // *NOP (absolute, X)
case 0x5d: A() = eorr(A(), AM_AbsoluteX()); break; // EOR (absolute, X)
case 0x5e: memoryReadModifyWrite(lsr(AM_AbsoluteX(PageCrossingBehavior::AlwaysReadTwice))); break; // LSR (absolute, X)
case 0x5f: sre_AbsoluteX(); break; // *SRE (absolute, X)
case 0x60: memoryRead(PC()); rts(); break; // RTS (implied)
case 0x60: swallow(); rts(); break; // RTS (implied)
case 0x61: A() = adc(A(), AM_IndexedIndirectX()); break; // ADC (indexed indirect X)
case 0x62: jam(); break; // *JAM
case 0x63: rra(AM_IndexedIndirectX()); break; // *RRA (indexed indirect X)
@ -238,9 +238,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0x65: A() = adc(A(), AM_ZeroPage()); break; // ADC (zero page)
case 0x66: memoryReadModifyWrite(ror(AM_ZeroPage())); break; // ROR (zero page)
case 0x67: rra(AM_ZeroPage()); break; // *RRA (zero page)
case 0x68: memoryRead(PC()); getBytePaged(1, S()); A() = through(pop()); break; // PLA (implied)
case 0x68: swallow(); swallow_stack(); A() = through(pop()); break; // PLA (implied)
case 0x69: A() = adc(A(), AM_Immediate()); break; // ADC (immediate)
case 0x6a: memoryRead(PC()); A() = ror(A()); break; // ROR A (implied)
case 0x6a: swallow(); A() = ror(A()); break; // ROR A (implied)
case 0x6b: arr(AM_Immediate()); break; // *ARR (immediate)
case 0x6c: jump(Address_Indirect()); break; // JMP (indirect)
case 0x6d: A() = adc(A(), AM_Absolute()); break; // ADC (absolute)
@ -255,9 +255,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0x75: A() = adc(A(), AM_ZeroPageX()); break; // ADC (zero page, X)
case 0x76: memoryReadModifyWrite(ror(AM_ZeroPageX())); break; // ROR (zero page, X)
case 0x77: rra(AM_ZeroPageX()); break; // *RRA (zero page, X)
case 0x78: memoryRead(PC()); P() = setBit(P(), IF); break; // SEI (implied)
case 0x78: swallow(); set_flag(IF); break; // SEI (implied)
case 0x79: A() = adc(A(), AM_AbsoluteY()); break; // ADC (absolute, Y)
case 0x7a: memoryRead(PC()); break; // *NOP (implied)
case 0x7a: swallow(); break; // *NOP (implied)
case 0x7b: rra_AbsoluteY(); break; // *RRA (absolute, Y)
case 0x7c: nop_AbsoluteX(); break; // *NOP (absolute, X)
case 0x7d: A() = adc(A(), AM_AbsoluteX()); break; // ADC (absolute, X)
@ -272,9 +272,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0x85: memoryWrite(Address_ZeroPage(), A()); break; // STA (zero page)
case 0x86: memoryWrite(Address_ZeroPage(), X()); break; // STX (zero page)
case 0x87: memoryWrite(Address_ZeroPage(), A() & X()); break; // *SAX (zero page)
case 0x88: memoryRead(PC()); Y() = dec(Y()); break; // DEY (implied)
case 0x88: swallow(); Y() = dec(Y()); break; // DEY (implied)
case 0x89: AM_Immediate(); break; // *NOP (immediate)
case 0x8a: memoryRead(PC()); A() = through(X()); break; // TXA (implied)
case 0x8a: swallow(); A() = through(X()); break; // TXA (implied)
case 0x8b: A() = through((A() | 0xee) & X() & AM_Immediate()); break; // *ANE (immediate)
case 0x8c: memoryWrite(Address_Absolute(), Y()); break; // STY (absolute)
case 0x8d: memoryWrite(Address_Absolute(), A()); break; // STA (absolute)
@ -284,20 +284,19 @@ int EightBit::MOS6502::execute() noexcept {
case 0x90: branch(carry() == 0); break; // BCC (relative)
case 0x91: sta_IndirectIndexedY(); break; // STA (indirect indexed Y)
case 0x92: jam(); break; // *JAM
case 0x93: break;
case 0x93: sha_IndirectIndexedY(); break; // *SHA (indirect indexed, Y)
case 0x94: memoryWrite(Address_ZeroPageX(), Y()); break; // STY (zero page, X)
case 0x95: memoryWrite(Address_ZeroPageX(), A()); break; // STA (zero page, X)
case 0x96: memoryWrite(Address_ZeroPageY(), X()); break; // STX (zero page, Y)
case 0x97: memoryWrite(Address_ZeroPageY(), A() & X()); break; // *SAX (zero page, Y)
case 0x98: memoryRead(PC()); A() = through(Y()); break; // TYA (implied)
case 0x98: swallow(); A() = through(Y()); break; // TYA (implied)
case 0x99: sta_AbsoluteY(); break; // STA (absolute, Y)
case 0x9a: memoryRead(PC()); S() = X(); break; // TXS (implied)
case 0x9b: break;
case 0x9a: swallow(); S() = X(); break; // TXS (implied)
case 0x9b: tas_AbsoluteY(); break; // *TAS (absolute, Y)
case 0x9c: sya_AbsoluteX(); break; // *SYA (absolute, X)
case 0x9d: sta_AbsoluteX(); break; // STA (absolute, X)
case 0x9e: sxa_AbsoluteY(); break; // *SXA (absolute, Y)
case 0x9f: break;
case 0x9f: sha_AbsoluteY(); break; // *SHA (absolute, Y)
case 0xa0: Y() = through(AM_Immediate()); break; // LDY (immediate)
case 0xa1: A() = through(AM_IndexedIndirectX()); break; // LDA (indexed indirect X)
case 0xa2: X() = through(AM_Immediate()); break; // LDX (immediate)
@ -306,9 +305,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0xa5: A() = through(AM_ZeroPage()); break; // LDA (zero page)
case 0xa6: X() = through(AM_ZeroPage()); break; // LDX (zero page)
case 0xa7: A() = X() = through(AM_ZeroPage()); break; // *LAX (zero page)
case 0xa8: memoryRead(PC()); Y() = through(A()); break; // TAY (implied)
case 0xa8: swallow(); Y() = through(A()); break; // TAY (implied)
case 0xa9: A() = through(AM_Immediate()); break; // LDA (immediate)
case 0xaa: memoryRead(PC()); X() = through(A()); break; // TAX (implied)
case 0xaa: swallow(); X() = through(A()); break; // TAX (implied)
case 0xab: A() = X() = through((A() | 0xee) & AM_Immediate()); break; // *ATX (immediate)
case 0xac: Y() = through(AM_Absolute()); break; // LDY (absolute)
case 0xad: A() = through(AM_Absolute()); break; // LDA (absolute)
@ -323,10 +322,10 @@ int EightBit::MOS6502::execute() noexcept {
case 0xb5: A() = through(AM_ZeroPageX()); break; // LDA (zero page, X)
case 0xb6: X() = through(AM_ZeroPageY()); break; // LDX (zero page, Y)
case 0xb7: A() = X() = through(AM_ZeroPageY()); break; // *LAX (zero page, Y)
case 0xb8: memoryRead(PC()); P() = clearBit(P(), VF); break; // CLV (implied)
case 0xb8: swallow(); reset_flag(VF); break; // CLV (implied)
case 0xb9: A() = through(AM_AbsoluteY()); break; // LDA (absolute, Y)
case 0xba: memoryRead(PC()); X() = through(S()); break; // TSX (implied)
case 0xbb: break;
case 0xba: swallow(); X() = through(S()); break; // TSX (implied)
case 0xbb: las_AbsoluteY(); break; // *LAS (absolute, Y)
case 0xbc: Y() = through(AM_AbsoluteX()); break; // LDY (absolute, X)
case 0xbd: A() = through(AM_AbsoluteX()); break; // LDA (absolute, X)
case 0xbe: X() = through(AM_AbsoluteY()); break; // LDX (absolute, Y)
@ -340,9 +339,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0xc5: cmp(A(), AM_ZeroPage()); break; // CMP (zero page)
case 0xc6: memoryReadModifyWrite(dec(AM_ZeroPage())); break; // DEC (zero page)
case 0xc7: dcp(AM_ZeroPage()); break; // *DCP (zero page)
case 0xc8: memoryRead(PC()); Y() = inc(Y()); break; // INY (implied)
case 0xc8: swallow(); Y() = inc(Y()); break; // INY (implied)
case 0xc9: cmp(A(), AM_Immediate()); break; // CMP (immediate)
case 0xca: memoryRead(PC()); X() = dec(X()); break; // DEX (implied)
case 0xca: swallow(); X() = dec(X()); break; // DEX (implied)
case 0xcb: axs(AM_Immediate()); break; // *AXS (immediate)
case 0xcc: cmp(Y(), AM_Absolute()); break; // CPY (absolute)
case 0xcd: cmp(A(), AM_Absolute()); break; // CMP (absolute)
@ -357,9 +356,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0xd5: cmp(A(), AM_ZeroPageX()); break; // CMP (zero page, X)
case 0xd6: memoryReadModifyWrite(dec(AM_ZeroPageX())); break; // DEC (zero page, X)
case 0xd7: dcp(AM_ZeroPageX()); break; // *DCP (zero page, X)
case 0xd8: memoryRead(PC()); P() = clearBit(P(), DF); break; // CLD (implied)
case 0xd8: swallow(); reset_flag(DF); break; // CLD (implied)
case 0xd9: cmp(A(), AM_AbsoluteY()); break; // CMP (absolute, Y)
case 0xda: memoryRead(PC()); break; // *NOP (implied)
case 0xda: swallow(); break; // *NOP (implied)
case 0xdb: dcp_AbsoluteY(); break; // *DCP (absolute, Y)
case 0xdc: nop_AbsoluteX(); break; // *NOP (absolute, X)
case 0xdd: cmp(A(), AM_AbsoluteX()); break; // CMP (absolute, X)
@ -374,9 +373,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0xe5: A() = sbc(A(), AM_ZeroPage()); break; // SBC (zero page)
case 0xe6: memoryReadModifyWrite(inc(AM_ZeroPage())); break; // INC (zero page)
case 0xe7: isb(AM_ZeroPage()); break; // *ISB (zero page)
case 0xe8: memoryRead(PC()); X() = inc(X()); break; // INX (implied)
case 0xe8: swallow(); X() = inc(X()); break; // INX (implied)
case 0xe9: A() = sbc(A(), AM_Immediate()); break; // SBC (immediate)
case 0xea: memoryRead(PC()); break; // NOP (implied)
case 0xea: swallow(); break; // NOP (implied)
case 0xeb: A() = sbc(A(), AM_Immediate()); break; // *SBC (immediate)
case 0xec: cmp(X(), AM_Absolute()); break; // CPX (absolute)
case 0xed: A() = sbc(A(), AM_Absolute()); break; // SBC (absolute)
@ -391,9 +390,9 @@ int EightBit::MOS6502::execute() noexcept {
case 0xf5: A() = sbc(A(), AM_ZeroPageX()); break; // SBC (zero page, X)
case 0xf6: memoryReadModifyWrite(inc(AM_ZeroPageX())); break; // INC (zero page, X)
case 0xf7: isb(AM_ZeroPageX()); break; // *ISB (zero page, X)
case 0xf8: memoryRead(PC()); P() = setBit(P(), DF); break; // SED (implied)
case 0xf8: swallow(); set_flag(DF); break; // SED (implied)
case 0xf9: A() = sbc(A(), AM_AbsoluteY()); break; // SBC (absolute, Y)
case 0xfa: memoryRead(PC()); break; // *NOP (implied)
case 0xfa: swallow(); break; // *NOP (implied)
case 0xfb: isb_AbsoluteY(); break; // *ISB (absolute, Y)
case 0xfc: nop_AbsoluteX(); break; // *NOP (absolute, X)
case 0xfd: A() = sbc(A(), AM_AbsoluteX()); break; // SBC (absolute, X)
@ -533,7 +532,7 @@ uint8_t EightBit::MOS6502::AM_IndirectIndexedY() noexcept {
void EightBit::MOS6502::branch(const int condition) noexcept {
const auto destination = Address_relative_byte();
if (condition) {
memoryRead(PC());
swallow();
const auto page = PC().high;
jump(destination);
if (UNLIKELY(PC().high != page))
@ -549,8 +548,8 @@ uint8_t EightBit::MOS6502::sbc(const uint8_t operand, const uint8_t data) noexce
const auto difference = m_intermediate;
adjustNZ(difference.low);
P() = setBit(P(), VF, (operand ^ data) & (operand ^ difference.low) & NF);
P() = clearBit(P(), CF, difference.high);
adjustOverflow_subtract(operand, data, difference.low);
reset_flag(CF, difference.high);
return returned;
}
@ -591,8 +590,8 @@ uint8_t EightBit::MOS6502::add(uint8_t operand, uint8_t data, int carrying) noex
uint8_t EightBit::MOS6502::add_b(uint8_t operand, uint8_t data, int carrying) noexcept {
m_intermediate.word = operand + data + carrying;
P() = setBit(P(), VF, ~(operand ^ data) & (operand ^ m_intermediate.low) & NF);
P() = setBit(P(), CF, carry(m_intermediate.high));
adjustOverflow_add(operand, data, m_intermediate.low);
set_flag(CF, carry(m_intermediate.high));
adjustNZ(m_intermediate.low);
@ -604,7 +603,7 @@ uint8_t EightBit::MOS6502::add_d(uint8_t operand, uint8_t data, int carry) noexc
register16_t low = lowerNibble(operand) + lowerNibble(data) + carry;
register16_t high = higherNibble(operand) + higherNibble(data);
P() = clearBit(P(), ZF, (low + high).low);
adjustZero((low + high).low);
if (low.word > 0x09) {
high += 0x10;
@ -612,12 +611,12 @@ uint8_t EightBit::MOS6502::add_d(uint8_t operand, uint8_t data, int carry) noexc
}
adjustNegative(high.low);
P() = setBit(P(), VF, ~(operand ^ data) & (operand ^ high.low) & NF);
adjustOverflow_add(operand, data, high.low);
if (high.word > 0x90)
high += 0x60;
P() = setBit(P(), CF, high.high);
set_flag(CF, high.high);
return lowerNibble(low.low) | higherNibble(high.low);
}
@ -626,13 +625,8 @@ uint8_t EightBit::MOS6502::andr(const uint8_t operand, const uint8_t data) noexc
return through(operand & data);
}
uint8_t EightBit::MOS6502::asl(const uint8_t value) noexcept {
P() = setBit(P(), CF, value & Bit7);
return through(value << 1);
}
void EightBit::MOS6502::bit(const uint8_t operand, const uint8_t data) noexcept {
P() = setBit(P(), VF, overflow(data));
set_flag(VF, overflow(data));
adjustZero(operand & data);
adjustNegative(data);
}
@ -640,7 +634,7 @@ void EightBit::MOS6502::bit(const uint8_t operand, const uint8_t data) noexcept
void EightBit::MOS6502::cmp(const uint8_t first, const uint8_t second) noexcept {
const register16_t result = first - second;
adjustNZ(result.low);
P() = clearBit(P(), CF, result.high);
reset_flag(CF, result.high);
}
uint8_t EightBit::MOS6502::dec(const uint8_t value) noexcept {
@ -657,17 +651,12 @@ uint8_t EightBit::MOS6502::inc(const uint8_t value) noexcept {
void EightBit::MOS6502::jsr() noexcept {
const auto low = fetchByte();
getBytePaged(1, S()); // dummy read
swallow_stack();
pushWord(PC());
PC().high = fetchByte();
PC().low = low;
}
uint8_t EightBit::MOS6502::lsr(const uint8_t value) noexcept {
P() = setBit(P(), CF, value & Bit0);
return through(value >> 1);
}
uint8_t EightBit::MOS6502::orr(const uint8_t operand, const uint8_t data) noexcept {
return through(operand | data);
}
@ -677,40 +666,26 @@ void EightBit::MOS6502::php() noexcept {
}
void EightBit::MOS6502::plp() noexcept {
swallow_stack();
P() = (pop() | RF) & ~BF;
}
uint8_t EightBit::MOS6502::rol(const uint8_t operand) noexcept {
const auto carryIn = carry();
P() = setBit(P(), CF, operand & Bit7);
const uint8_t result = (operand << 1) | carryIn;
return through(result);
}
uint8_t EightBit::MOS6502::ror(const uint8_t operand) noexcept {
const auto carryIn = carry();
P() = setBit(P(), CF, operand & Bit0);
const uint8_t result = (operand >> 1) | (carryIn << 7);
return through(result);
}
void EightBit::MOS6502::rti() noexcept {
getBytePaged(1, S()); // dummy read
plp();
ret();
}
void EightBit::MOS6502::rts() noexcept {
getBytePaged(1, S()); // dummy read
swallow_stack();
ret();
fetchByte();
swallow_fetch();
}
// Undocumented compound instructions
void EightBit::MOS6502::anc(const uint8_t value) noexcept {
A() = andr(A(), value);
P() = setBit(P(), CF, A() & Bit7);
set_flag(CF, A() & Bit7);
}
void EightBit::MOS6502::arr(const uint8_t value) noexcept {
@ -725,12 +700,12 @@ void EightBit::MOS6502::arr_d(const uint8_t value) noexcept {
A() &= value;
auto unshiftedA = A();
A() = through((A() >> 1) | (carry() << 7));
P() = setBit(P(), VF, (A() ^ (A() << 1)) & VF);
set_flag(VF, overflow((A() ^ (A() << 1))));
if (lowerNibble(unshiftedA) + (unshiftedA & 0x1) > 5)
A() = lowerNibble(A() + 6) | higherNibble(A());
P() = setBit(P(), CF, higherNibble(unshiftedA) + (unshiftedA & 0x10) > 0x50);
set_flag(CF, higherNibble(unshiftedA) + (unshiftedA & 0x10) > 0x50);
if (carry())
A() += 0x60;
@ -739,8 +714,8 @@ void EightBit::MOS6502::arr_d(const uint8_t value) noexcept {
void EightBit::MOS6502::arr_b(const uint8_t value) noexcept {
A() &= value;
A() = through((A() >> 1) | (carry() << 7));
P() = setBit(P(), CF, A() & Bit6);
P() = setBit(P(), VF, (A() ^ (A() << 1)) & VF);
set_flag(CF, A() & Bit6);
set_flag(VF, overflow((A() ^ (A() << 1))));
}
void EightBit::MOS6502::asr(const uint8_t value) noexcept {
@ -750,7 +725,7 @@ void EightBit::MOS6502::asr(const uint8_t value) noexcept {
void EightBit::MOS6502::axs(const uint8_t value) noexcept {
X() = through(sub_b(A() & X(), value));
P() = clearBit(P(), CF, m_intermediate.high);
reset_flag(CF, m_intermediate.high);
}
void EightBit::MOS6502::dcp(const uint8_t value) noexcept {
@ -784,8 +759,9 @@ void EightBit::MOS6502::sre(const uint8_t value) noexcept {
}
void EightBit::MOS6502::jam() noexcept {
memoryRead(PC());
memoryRead(PC()--);
swallow();
swallow();
--PC();
}
//
@ -897,12 +873,35 @@ void EightBit::MOS6502::sre_IndirectIndexedY() noexcept {
sre_with_fixup(address, page);
}
void EightBit::MOS6502::sha_AbsoluteY() noexcept {
const auto [address, page] = Address_AbsoluteY();
fixup(address, page);
memoryWrite(address, A() & X() & (address.high + 1));
}
void EightBit::MOS6502::sha_IndirectIndexedY() noexcept {
const auto [address, page] = Address_IndirectIndexedY();
fixup(address, page);
memoryWrite(address, A() & X() & (address.high + 1));
}
void EightBit::MOS6502::sya_AbsoluteX() noexcept {
const auto [address, page] = Address_AbsoluteX();
fixup(address, page);
memoryWrite(Y() & (address.high + 1));
}
void EightBit::MOS6502::tas_AbsoluteY() noexcept {
S() = A() & X();
sha_AbsoluteY();
}
void EightBit::MOS6502::las_AbsoluteY() noexcept {
const auto [address, page] = Address_AbsoluteY();
maybe_fixup(address, page);
A() = X() = S() = through(memoryRead(address) & S());
}
void EightBit::MOS6502::sxa_AbsoluteY() noexcept {
const auto [address, page] = Address_AbsoluteY();
fixup(address, page);

View File

@ -13,8 +13,8 @@ EightBit::Processor::Processor(const Processor& rhs)
PC() = rhs.PC();
}
DEFINE_PIN_LEVEL_CHANGERS(RESET, Processor);
DEFINE_PIN_LEVEL_CHANGERS(INT, Processor);
DEFINE_PIN_LEVEL_CHANGERS(RESET, Processor)
DEFINE_PIN_LEVEL_CHANGERS(INT, Processor)
void EightBit::Processor::handleRESET() noexcept {
raiseRESET();