2017-06-04 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
2017-07-05 16:46:02 +00:00
|
|
|
|
#include "Memory.h"
|
|
|
|
|
#include "Processor.h"
|
2017-07-02 21:03:33 +00:00
|
|
|
|
#include "Signal.h"
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
namespace EightBit {
|
2017-07-05 16:46:02 +00:00
|
|
|
|
class MOS6502 : public Processor {
|
2017-07-02 21:03:33 +00:00
|
|
|
|
public:
|
2017-07-10 14:51:33 +00:00
|
|
|
|
enum StatusBits {
|
|
|
|
|
NF = 0x80, // Negative
|
|
|
|
|
VF = 0x40, // Overflow
|
|
|
|
|
RF = 0x20, // reserved
|
|
|
|
|
BF = 0x10, // Brk
|
|
|
|
|
DF = 0x08, // D (use BCD for arithmetic)
|
|
|
|
|
IF = 0x04, // I (IRQ disable)
|
|
|
|
|
ZF = 0x02, // Zero
|
|
|
|
|
CF = 0x01, // Carry
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-14 16:22:28 +00:00
|
|
|
|
MOS6502(Memory& memory);
|
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
|
|
|
|
|
2017-07-07 08:24:58 +00:00
|
|
|
|
uint8_t& X() { return x; }
|
|
|
|
|
uint8_t& Y() { return y; }
|
|
|
|
|
uint8_t& A() { return a; }
|
|
|
|
|
uint8_t& S() { return s; }
|
2017-07-10 14:51:33 +00:00
|
|
|
|
uint8_t& P() { return p; }
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2017-07-05 16:46:02 +00:00
|
|
|
|
virtual void initialise();
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2017-07-05 16:46:02 +00:00
|
|
|
|
virtual int step();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
virtual void Reset();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
virtual void TriggerIRQ();
|
|
|
|
|
virtual void TriggerNMI();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-06 20:32:52 +00:00
|
|
|
|
void GetWord(register16_t& output);
|
|
|
|
|
void GetWord(uint16_t offset, register16_t& output);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-06 20:32:52 +00:00
|
|
|
|
uint8_t GetByte() { return m_memory.read(); }
|
2017-07-05 16:46:02 +00:00
|
|
|
|
uint8_t GetByte(uint16_t offset) { return m_memory.read(offset); }
|
2017-07-06 20:32:52 +00:00
|
|
|
|
|
|
|
|
|
void SetByte(uint8_t value) { m_memory.write(value); }
|
2017-07-05 16:46:02 +00:00
|
|
|
|
void SetByte(uint16_t offset, uint8_t value) { m_memory.write(offset, value); }
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
protected:
|
|
|
|
|
virtual void Interrupt(uint16_t vector);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-05 16:46:02 +00:00
|
|
|
|
virtual int Execute(uint8_t cell);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
private:
|
2017-07-16 09:05:49 +00:00
|
|
|
|
void adjustZero(uint8_t datum) { clearFlag(P(), ZF, datum); }
|
|
|
|
|
void adjustNegative(uint8_t datum) { setFlag(P(), NF, datum & NF); }
|
2017-07-06 20:32:52 +00:00
|
|
|
|
|
2017-07-16 09:05:49 +00:00
|
|
|
|
void adjustNZ(uint8_t datum) {
|
|
|
|
|
adjustZero(datum);
|
|
|
|
|
adjustNegative(datum);
|
2017-07-06 20:32:52 +00:00
|
|
|
|
}
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
void PushByte(uint8_t value);
|
|
|
|
|
uint8_t PopByte();
|
2017-07-05 16:46:02 +00:00
|
|
|
|
void PushWord(register16_t value);
|
2017-07-06 20:32:52 +00:00
|
|
|
|
void PopWord(register16_t& output);
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
uint8_t FetchByte();
|
2017-07-06 20:32:52 +00:00
|
|
|
|
void FetchWord(register16_t& output);
|
|
|
|
|
|
2017-07-11 20:34:01 +00:00
|
|
|
|
#pragma region 6502 addressing modes
|
|
|
|
|
|
|
|
|
|
#pragma region Addresses
|
|
|
|
|
|
|
|
|
|
void Address_Absolute() {
|
|
|
|
|
FetchWord(m_memptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_ZeroPage() {
|
|
|
|
|
m_memptr.low = FetchByte();
|
|
|
|
|
m_memptr.high = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_ZeroPageIndirect() {
|
|
|
|
|
Address_ZeroPage();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
GetWord(m_memptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_Indirect() {
|
|
|
|
|
Address_Absolute();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
GetWord(m_memptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_IndirectX() {
|
|
|
|
|
Address_Absolute();
|
|
|
|
|
m_memory.ADDRESS().word = m_memptr.word + X();
|
|
|
|
|
GetWord(m_memptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_ZeroPageX() {
|
|
|
|
|
Address_ZeroPage();
|
|
|
|
|
m_memptr.low += X();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_ZeroPageY() {
|
|
|
|
|
Address_ZeroPage();
|
|
|
|
|
m_memptr.low += Y();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_AbsoluteX() {
|
|
|
|
|
Address_Absolute();
|
|
|
|
|
m_memptr.word += X();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_AbsoluteY() {
|
|
|
|
|
Address_Absolute();
|
|
|
|
|
m_memptr.word += Y();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_IndexedIndirectX() {
|
|
|
|
|
Address_ZeroPageX();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
GetWord(m_memptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Address_IndirectIndexedY() {
|
|
|
|
|
Address_ZeroPageIndirect();
|
|
|
|
|
m_memptr.word += Y();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma endregion Addresses
|
|
|
|
|
|
|
|
|
|
#pragma region References
|
|
|
|
|
|
2017-07-16 09:40:38 +00:00
|
|
|
|
uint8_t& AM_A() {
|
|
|
|
|
m_busRW = false;
|
|
|
|
|
return A();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 20:34:01 +00:00
|
|
|
|
uint8_t& AM_Immediate() {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = false;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
FetchByte();
|
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t& AM_Absolute() {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_Absolute();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t& AM_ZeroPage() {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_ZeroPage();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 22:19:46 +00:00
|
|
|
|
uint8_t& AM_AbsoluteX(bool read = true) {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_AbsoluteX();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
2017-07-15 22:19:46 +00:00
|
|
|
|
if (read && (m_memory.ADDRESS().low == 0xff))
|
|
|
|
|
++cycles;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 22:19:46 +00:00
|
|
|
|
uint8_t& AM_AbsoluteY(bool read = true) {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_AbsoluteY();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
2017-07-15 22:19:46 +00:00
|
|
|
|
if (read && (m_memory.ADDRESS().low == 0xff))
|
|
|
|
|
++cycles;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t& AM_ZeroPageX() {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_ZeroPageX();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t& AM_ZeroPageY() {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_ZeroPageY();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t& AM_IndexedIndirectX() {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_IndexedIndirectX();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 22:19:46 +00:00
|
|
|
|
uint8_t& AM_IndirectIndexedY(bool read = true) {
|
2017-07-16 09:40:38 +00:00
|
|
|
|
m_busRW = true;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
Address_IndirectIndexedY();
|
|
|
|
|
m_memory.ADDRESS() = m_memptr;
|
2017-07-15 22:19:46 +00:00
|
|
|
|
if (read && (m_memory.ADDRESS().low == 0xff))
|
|
|
|
|
++cycles;
|
2017-07-11 20:34:01 +00:00
|
|
|
|
return m_memory.reference();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma endregion References
|
|
|
|
|
|
2017-07-13 11:02:44 +00:00
|
|
|
|
#pragma region 6502 addressing mode switching
|
|
|
|
|
|
2017-07-15 22:19:46 +00:00
|
|
|
|
uint8_t& AM_00(int bbb, bool read = true) {
|
2017-07-13 11:02:44 +00:00
|
|
|
|
switch (bbb) {
|
|
|
|
|
case 0b000:
|
|
|
|
|
return AM_Immediate();
|
|
|
|
|
case 0b001:
|
|
|
|
|
return AM_ZeroPage();
|
|
|
|
|
case 0b011:
|
|
|
|
|
return AM_Absolute();
|
|
|
|
|
case 0b101:
|
|
|
|
|
return AM_ZeroPageX();
|
|
|
|
|
case 0b111:
|
2017-07-15 22:19:46 +00:00
|
|
|
|
return AM_AbsoluteX(read);
|
2017-07-13 11:02:44 +00:00
|
|
|
|
case 0b010:
|
|
|
|
|
case 0b100:
|
|
|
|
|
case 0b110:
|
|
|
|
|
throw std::domain_error("Illegal addressing mode");
|
|
|
|
|
default:
|
|
|
|
|
__assume(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 22:19:46 +00:00
|
|
|
|
uint8_t& AM_01(int bbb, bool read = true) {
|
2017-07-13 11:02:44 +00:00
|
|
|
|
switch (bbb) {
|
|
|
|
|
case 0b000:
|
|
|
|
|
return AM_IndexedIndirectX();
|
|
|
|
|
case 0b001:
|
|
|
|
|
return AM_ZeroPage();
|
|
|
|
|
case 0b010:
|
|
|
|
|
return AM_Immediate();
|
|
|
|
|
case 0b011:
|
|
|
|
|
return AM_Absolute();
|
|
|
|
|
case 0b100:
|
2017-07-15 22:19:46 +00:00
|
|
|
|
return AM_IndirectIndexedY(read);
|
2017-07-13 11:02:44 +00:00
|
|
|
|
case 0b101:
|
|
|
|
|
return AM_ZeroPageX();
|
|
|
|
|
case 0b110:
|
2017-07-15 22:19:46 +00:00
|
|
|
|
return AM_AbsoluteY(read);
|
2017-07-13 11:02:44 +00:00
|
|
|
|
case 0b111:
|
2017-07-15 22:19:46 +00:00
|
|
|
|
return AM_AbsoluteX(read);
|
2017-07-13 11:02:44 +00:00
|
|
|
|
default:
|
|
|
|
|
__assume(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 22:19:46 +00:00
|
|
|
|
uint8_t& AM_10(int bbb, bool read = true) {
|
2017-07-13 11:02:44 +00:00
|
|
|
|
switch (bbb) {
|
|
|
|
|
case 0b000:
|
|
|
|
|
return AM_Immediate();
|
|
|
|
|
case 0b001:
|
|
|
|
|
return AM_ZeroPage();
|
|
|
|
|
case 0b010:
|
2017-07-16 09:40:38 +00:00
|
|
|
|
return AM_A();
|
2017-07-13 11:02:44 +00:00
|
|
|
|
case 0b011:
|
|
|
|
|
return AM_Absolute();
|
|
|
|
|
case 0b101:
|
|
|
|
|
return AM_ZeroPageX();
|
|
|
|
|
case 0b111:
|
2017-07-15 22:19:46 +00:00
|
|
|
|
return AM_AbsoluteX(read);
|
2017-07-13 11:02:44 +00:00
|
|
|
|
case 0b100:
|
|
|
|
|
case 0b110:
|
|
|
|
|
throw std::domain_error("Illegal addressing mode");
|
|
|
|
|
default:
|
|
|
|
|
__assume(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 22:19:46 +00:00
|
|
|
|
uint8_t& AM_10_x(int bbb, bool read = true) {
|
2017-07-13 11:02:44 +00:00
|
|
|
|
switch (bbb) {
|
|
|
|
|
case 0b000:
|
|
|
|
|
return AM_Immediate();
|
|
|
|
|
case 0b001:
|
|
|
|
|
return AM_ZeroPage();
|
|
|
|
|
case 0b010:
|
2017-07-16 09:40:38 +00:00
|
|
|
|
return AM_A();
|
2017-07-13 11:02:44 +00:00
|
|
|
|
case 0b011:
|
|
|
|
|
return AM_Absolute();
|
|
|
|
|
case 0b101:
|
|
|
|
|
return AM_ZeroPageY();
|
|
|
|
|
case 0b111:
|
2017-07-15 22:19:46 +00:00
|
|
|
|
return AM_AbsoluteY(read);
|
2017-07-13 11:02:44 +00:00
|
|
|
|
case 0b100:
|
|
|
|
|
case 0b110:
|
|
|
|
|
throw std::domain_error("Illegal addressing mode");
|
|
|
|
|
default:
|
|
|
|
|
__assume(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma endregion 6502 addressing mode switching
|
|
|
|
|
|
2017-07-11 20:34:01 +00:00
|
|
|
|
#pragma endregion 6502 addressing modes
|
|
|
|
|
|
|
|
|
|
void DEC(uint8_t& output);
|
|
|
|
|
|
|
|
|
|
void ROR(uint8_t& output);
|
|
|
|
|
|
|
|
|
|
void LSR(uint8_t& output);
|
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
void BIT(uint8_t data);
|
|
|
|
|
|
2017-07-11 20:34:01 +00:00
|
|
|
|
void INC(uint8_t& output);
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2017-07-11 20:34:01 +00:00
|
|
|
|
void ROL(uint8_t& output);
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2017-07-11 20:34:01 +00:00
|
|
|
|
void ASL(uint8_t& output);
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
|
|
|
|
void ORA(uint8_t data);
|
|
|
|
|
|
|
|
|
|
void AND(uint8_t data);
|
|
|
|
|
|
|
|
|
|
void SBC(uint8_t data);
|
|
|
|
|
void SBC_b(uint8_t data);
|
|
|
|
|
void SBC_d(uint8_t data);
|
|
|
|
|
|
|
|
|
|
void EOR(uint8_t data);
|
|
|
|
|
|
|
|
|
|
void CMP(uint8_t first, uint8_t second);
|
|
|
|
|
|
|
|
|
|
void LDA(uint8_t data);
|
|
|
|
|
void LDY(uint8_t data);
|
|
|
|
|
void LDX(uint8_t data);
|
|
|
|
|
|
|
|
|
|
void ADC(uint8_t data);
|
|
|
|
|
void ADC_b(uint8_t data);
|
|
|
|
|
void ADC_d(uint8_t data);
|
|
|
|
|
|
|
|
|
|
void Branch(int8_t displacement);
|
2017-07-14 16:22:28 +00:00
|
|
|
|
|
2017-07-02 21:03:33 +00:00
|
|
|
|
void Branch(bool flag);
|
2017-07-14 16:22:28 +00:00
|
|
|
|
|
|
|
|
|
void PHP();
|
|
|
|
|
void PLP();
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
|
|
|
|
void JSR_abs();
|
2017-07-14 16:22:28 +00:00
|
|
|
|
void RTI();
|
|
|
|
|
void RTS();
|
2017-07-02 21:03:33 +00:00
|
|
|
|
void JMP_abs();
|
|
|
|
|
void JMP_ind();
|
|
|
|
|
void JMP_absxind();
|
2017-07-14 16:22:28 +00:00
|
|
|
|
void BRK();
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
|
|
|
|
const uint16_t PageOne = 0x100;
|
|
|
|
|
const uint16_t IRQvector = 0xfffe;
|
|
|
|
|
const uint16_t RSTvector = 0xfffc;
|
|
|
|
|
const uint16_t NMIvector = 0xfffa;
|
|
|
|
|
|
|
|
|
|
uint8_t x; // index register X
|
|
|
|
|
uint8_t y; // index register Y
|
|
|
|
|
uint8_t a; // accumulator
|
|
|
|
|
uint8_t s; // stack pointer
|
2017-07-10 14:51:33 +00:00
|
|
|
|
uint8_t p; // processor status
|
2017-07-02 21:03:33 +00:00
|
|
|
|
|
2017-07-11 20:34:01 +00:00
|
|
|
|
register16_t m_memptr;
|
2017-07-15 22:19:46 +00:00
|
|
|
|
|
|
|
|
|
std::array<int, 0x100> m_timings;
|
2017-07-16 09:40:38 +00:00
|
|
|
|
|
|
|
|
|
bool m_busRW;
|
2017-07-02 21:03:33 +00:00
|
|
|
|
};
|
|
|
|
|
}
|