2017-06-04 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
2018-10-31 23:29:13 +00:00
|
|
|
|
#include <utility>
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
#include <EightBitCompilerDefinitions.h>
|
2018-08-17 20:53:49 +00:00
|
|
|
|
#include <LittleEndianProcessor.h>
|
2018-10-31 23:29:13 +00:00
|
|
|
|
#include <Register.h>
|
2017-09-06 23:58:56 +00:00
|
|
|
|
#include <Signal.h>
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
namespace EightBit {
|
2018-10-31 23:29:13 +00:00
|
|
|
|
|
|
|
|
|
class Bus;
|
|
|
|
|
|
2018-08-17 20:53:49 +00:00
|
|
|
|
class MOS6502 : public LittleEndianProcessor {
|
2017-07-02 21:03:33 +00:00
|
|
|
|
public:
|
2017-07-10 14:51:33 +00:00
|
|
|
|
enum StatusBits {
|
2017-07-17 12:46:06 +00:00
|
|
|
|
NF = Bit7, // Negative
|
|
|
|
|
VF = Bit6, // Overflow
|
|
|
|
|
RF = Bit5, // reserved
|
|
|
|
|
BF = Bit4, // Brk
|
|
|
|
|
DF = Bit3, // D (use BCD for arithmetic)
|
|
|
|
|
IF = Bit2, // I (IRQ disable)
|
|
|
|
|
ZF = Bit1, // Zero
|
|
|
|
|
CF = Bit0, // Carry
|
2017-07-10 14:51:33 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-09-06 23:58:56 +00:00
|
|
|
|
MOS6502(Bus& bus);
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
|
|
|
|
Signal<MOS6502> ExecutingInstruction;
|
2017-07-05 16:46:02 +00:00
|
|
|
|
Signal<MOS6502> ExecutedInstruction;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2018-12-29 19:40:02 +00:00
|
|
|
|
virtual int execute() final;
|
2017-12-10 21:41:48 +00:00
|
|
|
|
virtual int step() final;
|
2018-08-25 12:35:53 +00:00
|
|
|
|
virtual void powerOn() final;
|
2017-08-30 22:17:34 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
auto& X() { return x; }
|
|
|
|
|
auto& Y() { return y; }
|
|
|
|
|
auto& A() { return a; }
|
|
|
|
|
auto& S() { return s; }
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
auto& P() { return p; }
|
|
|
|
|
const auto& P() const { return p; }
|
|
|
|
|
|
|
|
|
|
auto& NMI() { return m_nmiLine; } // In
|
|
|
|
|
auto& SO() { return m_soLine; } // In
|
2018-01-12 20:13:35 +00:00
|
|
|
|
|
2017-12-10 21:41:48 +00:00
|
|
|
|
protected:
|
2018-08-25 11:09:26 +00:00
|
|
|
|
virtual void handleRESET() final;
|
|
|
|
|
virtual void handleIRQ() final;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2018-12-29 22:18:01 +00:00
|
|
|
|
virtual void busWrite() final;
|
2019-01-05 17:23:50 +00:00
|
|
|
|
virtual uint8_t busRead() final;
|
2018-12-29 22:18:01 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
virtual uint8_t sub(uint8_t operand, uint8_t data, int borrow = 0);
|
|
|
|
|
uint8_t sbc(uint8_t operand, uint8_t data);
|
|
|
|
|
uint8_t sub_b(uint8_t operand, uint8_t data, int borrow);
|
|
|
|
|
uint8_t sub_d(uint8_t operand, uint8_t data, int borrow);
|
2017-12-25 23:34:56 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
virtual uint8_t add(uint8_t operand, uint8_t data, int carry = 0);
|
|
|
|
|
uint8_t adc(uint8_t operand, uint8_t data);
|
|
|
|
|
uint8_t add_b(uint8_t operand, uint8_t data, int carry);
|
|
|
|
|
uint8_t add_d(uint8_t operand, uint8_t data, int carry);
|
2017-12-25 23:34:56 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
private:
|
2018-10-31 23:29:13 +00:00
|
|
|
|
const uint8_t IRQvector = 0xfe; // IRQ vector
|
|
|
|
|
const uint8_t RSTvector = 0xfc; // RST vector
|
|
|
|
|
const uint8_t NMIvector = 0xfa; // NMI vector
|
|
|
|
|
|
2018-08-25 11:09:26 +00:00
|
|
|
|
void handleNMI();
|
|
|
|
|
void handleSO();
|
|
|
|
|
void handleHALT();
|
|
|
|
|
|
2017-12-29 11:50:24 +00:00
|
|
|
|
void interrupt(uint8_t vector);
|
2017-12-10 21:41:48 +00:00
|
|
|
|
|
2017-11-11 15:13:26 +00:00
|
|
|
|
virtual void push(uint8_t value) final;
|
|
|
|
|
virtual uint8_t pop() final;
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
// Addressing modes
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
register16_t Address_Absolute();
|
|
|
|
|
uint8_t Address_ZeroPage();
|
|
|
|
|
register16_t Address_ZeroPageIndirect();
|
|
|
|
|
register16_t Address_Indirect();
|
|
|
|
|
uint8_t Address_ZeroPageX();
|
|
|
|
|
uint8_t Address_ZeroPageY();
|
2019-01-06 12:17:43 +00:00
|
|
|
|
std::pair<register16_t, uint8_t> Address_AbsoluteX();
|
|
|
|
|
std::pair<register16_t, uint8_t> Address_AbsoluteY();
|
2018-10-31 23:29:13 +00:00
|
|
|
|
register16_t Address_IndexedIndirectX();
|
2019-01-06 12:17:43 +00:00
|
|
|
|
std::pair<register16_t, uint8_t> Address_IndirectIndexedY();
|
2018-10-31 23:29:13 +00:00
|
|
|
|
register16_t Address_relative_byte();
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2017-12-30 15:22:27 +00:00
|
|
|
|
// Addressing modes, read
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2019-01-06 12:58:13 +00:00
|
|
|
|
enum PageCrossingBehavior { AlwaysReadTwice, MaybeReadTwice };
|
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
uint8_t AM_Immediate();
|
|
|
|
|
uint8_t AM_Absolute();
|
|
|
|
|
uint8_t AM_ZeroPage();
|
2019-01-06 12:58:13 +00:00
|
|
|
|
uint8_t AM_AbsoluteX(PageCrossingBehavior behaviour = MaybeReadTwice);
|
2018-10-31 23:29:13 +00:00
|
|
|
|
uint8_t AM_AbsoluteY();
|
|
|
|
|
uint8_t AM_ZeroPageX();
|
|
|
|
|
uint8_t AM_ZeroPageY();
|
|
|
|
|
uint8_t AM_IndexedIndirectX();
|
|
|
|
|
uint8_t AM_IndirectIndexedY();
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
// Flag adjustment
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
void adjustZero(const uint8_t datum) { clearFlag(P(), ZF, datum); }
|
|
|
|
|
void adjustNegative(const uint8_t datum) { setFlag(P(), NF, datum & NF); }
|
|
|
|
|
|
|
|
|
|
void adjustNZ(const uint8_t datum) {
|
|
|
|
|
adjustZero(datum);
|
|
|
|
|
adjustNegative(datum);
|
2017-07-11 20:34:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
// Flag checking
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
auto interruptMasked() const { return P() & IF; }
|
|
|
|
|
auto decimal() const { return P() & DF; }
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
auto negative() const { return P() & NF; }
|
|
|
|
|
auto zero() const { return P() & ZF; }
|
|
|
|
|
auto overflow() const { return P() & VF; }
|
|
|
|
|
auto carry() const { return P() & CF; }
|
2017-07-11 20:34:01 +00:00
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
2019-01-05 17:23:50 +00:00
|
|
|
|
void branch(int condition);
|
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
auto through(const uint8_t data) {
|
|
|
|
|
adjustNZ(data);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 01:04:12 +00:00
|
|
|
|
void busReadModifyWrite(const uint8_t data) {
|
|
|
|
|
// The read will have already taken place...
|
|
|
|
|
busWrite();
|
|
|
|
|
Processor::busWrite(data);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 23:29:13 +00:00
|
|
|
|
// Instruction implementations
|
|
|
|
|
|
|
|
|
|
uint8_t andr(uint8_t operand, uint8_t data);
|
|
|
|
|
uint8_t asl(uint8_t value);
|
|
|
|
|
void bit(uint8_t operand, uint8_t data);
|
|
|
|
|
void brk();
|
|
|
|
|
void cmp(uint8_t first, uint8_t second);
|
|
|
|
|
uint8_t dec(uint8_t value);
|
|
|
|
|
uint8_t eorr(uint8_t operand, uint8_t data);
|
|
|
|
|
uint8_t inc(uint8_t value);
|
2019-01-05 23:21:43 +00:00
|
|
|
|
void jsr();
|
2018-10-31 23:29:13 +00:00
|
|
|
|
uint8_t lsr(uint8_t value);
|
|
|
|
|
uint8_t orr(uint8_t operand, uint8_t data);
|
|
|
|
|
void php();
|
|
|
|
|
void plp();
|
|
|
|
|
uint8_t rol(uint8_t operand);
|
|
|
|
|
uint8_t ror(uint8_t operand);
|
|
|
|
|
void rti();
|
|
|
|
|
void rts();
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2018-12-29 22:22:31 +00:00
|
|
|
|
// Undocumented compound instructions
|
|
|
|
|
|
2018-12-30 12:01:23 +00:00
|
|
|
|
void anc(uint8_t value);
|
|
|
|
|
void arr(uint8_t value);
|
|
|
|
|
void asr(uint8_t value);
|
|
|
|
|
void axs(uint8_t value);
|
2018-12-29 22:22:31 +00:00
|
|
|
|
void dcp(uint8_t value);
|
|
|
|
|
void isb(uint8_t value);
|
|
|
|
|
void rla(uint8_t value);
|
|
|
|
|
void rra(uint8_t value);
|
|
|
|
|
void slo(uint8_t value);
|
|
|
|
|
void sre(uint8_t value);
|
|
|
|
|
|
2018-01-18 17:50:15 +00:00
|
|
|
|
uint8_t x = 0; // index register X
|
|
|
|
|
uint8_t y = 0; // index register Y
|
|
|
|
|
uint8_t a = 0; // accumulator
|
|
|
|
|
uint8_t s = 0; // stack pointer
|
|
|
|
|
uint8_t p = 0; // processor status
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2018-12-01 15:24:29 +00:00
|
|
|
|
PinLevel m_nmiLine = PinLevel::Low;
|
|
|
|
|
PinLevel m_soLine = PinLevel::Low;
|
2018-03-18 22:40:23 +00:00
|
|
|
|
|
2018-06-15 23:55:32 +00:00
|
|
|
|
register16_t m_intermediate;
|
2017-07-02 21:03:33 +00:00
|
|
|
|
};
|
|
|
|
|
}
|