2014-10-18 11:33:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <Stream.h>
|
|
|
|
|
|
|
|
#include "memory.h"
|
2015-12-02 19:41:15 +00:00
|
|
|
#include "CPU.h"
|
2014-10-18 11:33:48 +00:00
|
|
|
#include "r6502.h"
|
|
|
|
|
2014-11-20 08:28:01 +00:00
|
|
|
void r6502::run(unsigned clocks) {
|
2016-01-23 21:15:14 +00:00
|
|
|
while (clocks--) {
|
|
|
|
byte op = _mem[PC];
|
|
|
|
PC++;
|
|
|
|
(this->*_ops[op])();
|
|
|
|
if (_halted)
|
|
|
|
break;
|
2014-11-20 08:28:01 +00:00
|
|
|
}
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
byte r6502::flags() {
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits.N = ((N & 0x80) != 0);
|
|
|
|
P.bits.V = V;
|
|
|
|
P.bits.Z = !Z;
|
|
|
|
P.bits.C = C;
|
|
|
|
P.bits._ = 1;
|
2016-01-23 21:15:14 +00:00
|
|
|
return P.flags;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 11:55:50 +00:00
|
|
|
char *r6502::status(char *buf, size_t n, bool hdr) {
|
2014-11-19 19:35:16 +00:00
|
|
|
flags();
|
2018-08-10 10:13:45 +00:00
|
|
|
snprintf(buf, n,
|
2016-01-24 11:55:50 +00:00
|
|
|
"%s%02x %02x %02x %02x %d%d%d%d%d%d%d%d %04x",
|
|
|
|
hdr? "aa xx yy sp nv_bdizc _pc_\r\n": "",
|
|
|
|
A, X, Y, S, P.bits.N, P.bits.V, P.bits._, P.bits.B,
|
|
|
|
P.bits.D, P.bits.I, P.bits.Z, P.bits.C, PC);
|
2014-11-19 19:35:16 +00:00
|
|
|
return buf;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void r6502::checkpoint(Stream &s)
|
|
|
|
{
|
2014-11-19 19:35:16 +00:00
|
|
|
s.write(PC / 0xff);
|
|
|
|
s.write(PC % 0xff);
|
2016-01-23 21:15:14 +00:00
|
|
|
s.write(S);
|
|
|
|
s.write(A);
|
|
|
|
s.write(X);
|
|
|
|
s.write(Y);
|
|
|
|
s.write(N);
|
|
|
|
s.write(V);
|
|
|
|
s.write(B);
|
|
|
|
s.write(D);
|
|
|
|
s.write(I);
|
|
|
|
s.write(Z);
|
|
|
|
s.write(C);
|
|
|
|
s.write(P.flags);
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void r6502::restore(Stream &s)
|
|
|
|
{
|
2014-11-19 19:35:16 +00:00
|
|
|
byte hi = s.read(), lo = s.read();
|
|
|
|
PC = hi * 0xff + lo;
|
|
|
|
S = s.read();
|
|
|
|
A = s.read();
|
|
|
|
X = s.read();
|
|
|
|
Y = s.read();
|
|
|
|
N = s.read();
|
|
|
|
V = s.read();
|
|
|
|
B = s.read();
|
|
|
|
D = s.read();
|
|
|
|
I = s.read();
|
|
|
|
Z = s.read();
|
|
|
|
C = s.read();
|
2016-01-23 21:15:14 +00:00
|
|
|
P.flags = s.read();
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::raise(int level) {
|
2014-11-19 19:35:16 +00:00
|
|
|
if (level < 0)
|
2016-01-23 21:15:14 +00:00
|
|
|
nmi();
|
2014-11-19 19:35:16 +00:00
|
|
|
else if (!P.bits.I)
|
2016-01-23 21:15:14 +00:00
|
|
|
irq();
|
2014-11-19 19:35:16 +00:00
|
|
|
else
|
|
|
|
_irq = true;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::irq() {
|
|
|
|
pusha(PC);
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits.B = 0;
|
2016-01-23 21:15:14 +00:00
|
|
|
pushb(flags());
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits.B = 1;
|
|
|
|
P.bits.I = 1;
|
2016-01-23 21:15:14 +00:00
|
|
|
PC = vector(ibvec);
|
2014-11-19 19:35:16 +00:00
|
|
|
_irq = false;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::brk() {
|
2014-11-19 19:35:16 +00:00
|
|
|
if (!P.bits.I) {
|
2016-01-24 15:28:54 +00:00
|
|
|
pusha(PC+1);
|
2016-01-23 21:15:14 +00:00
|
|
|
php();
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits.I = 1;
|
2016-01-23 21:15:14 +00:00
|
|
|
PC = vector(ibvec);
|
2014-11-19 19:35:16 +00:00
|
|
|
}
|
|
|
|
P.bits.B = 1;
|
|
|
|
P.bits._ = 1;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::rti() {
|
|
|
|
plp();
|
|
|
|
PC = popa();
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::cli() {
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits.I = 0;
|
|
|
|
if (_irq)
|
|
|
|
irq();
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::nmi() {
|
|
|
|
pusha(PC);
|
|
|
|
php();
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits.I = 1;
|
2016-01-23 21:15:14 +00:00
|
|
|
PC = vector(nmivec);
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// php and plp are complicated by the representation
|
|
|
|
// of the processor state for efficient normal operation
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::php() {
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits.B = 1;
|
2016-01-23 21:15:14 +00:00
|
|
|
pushb(flags());
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::plp() {
|
|
|
|
P.flags = popb();
|
2014-11-19 19:35:16 +00:00
|
|
|
N = P.bits.N? 0x80: 0;
|
|
|
|
V = P.bits.V;
|
|
|
|
Z = !P.bits.Z;
|
|
|
|
C = P.bits.C;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::rts() {
|
|
|
|
PC = popa()+1;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::jsr() {
|
|
|
|
pusha(PC+1);
|
|
|
|
PC = vector(PC);
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::_adc(byte d) {
|
2014-11-19 19:35:16 +00:00
|
|
|
if (P.bits.D) {
|
|
|
|
int r = _fromBCD[A] + _fromBCD[d] + C;
|
|
|
|
C = (r > 99);
|
|
|
|
if (C) r -= 100;
|
|
|
|
A = _toBCD[r];
|
|
|
|
} else {
|
|
|
|
unsigned short u = (unsigned short)A + (unsigned short)d + (unsigned short)C;
|
|
|
|
short s = (char)A + (char)d + (char)C;
|
|
|
|
C = (u < 0 || u > 255);
|
|
|
|
V = (s > 127 || s < -128);
|
|
|
|
A = (u & 0xff);
|
|
|
|
}
|
|
|
|
N = (A & 0x80);
|
|
|
|
Z = A;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
void r6502::sbcd(byte d) {
|
2014-11-19 19:35:16 +00:00
|
|
|
int r = _fromBCD[A] - _fromBCD[d] - !C;
|
|
|
|
C = (r >= 0);
|
|
|
|
if (r < 0) r += 100;
|
|
|
|
A = _toBCD[r & 0xff];
|
|
|
|
N = (A & 0x80);
|
|
|
|
Z = A;
|
|
|
|
// V not tested for: http://www.6502.org/tutorials/decimal_mode.html
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 12:02:47 +00:00
|
|
|
void r6502::ill() {
|
2016-01-23 21:15:14 +00:00
|
|
|
--PC;
|
|
|
|
_halted = true;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 12:02:47 +00:00
|
|
|
void r6502::reset()
|
2014-10-18 11:33:48 +00:00
|
|
|
{
|
2016-01-24 11:55:50 +00:00
|
|
|
_halted = false;
|
2016-01-23 21:15:14 +00:00
|
|
|
P.flags = 0;
|
2014-11-19 19:35:16 +00:00
|
|
|
P.bits._ = 1;
|
|
|
|
P.bits.B = 1;
|
|
|
|
_irq = false;
|
|
|
|
S = 0xff;
|
|
|
|
PC = vector(resvec);
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 21:15:14 +00:00
|
|
|
r6502::r6502(Memory &m): CPU(m) {
|
2014-10-18 11:33:48 +00:00
|
|
|
|
2014-11-19 19:35:16 +00:00
|
|
|
for (int i=0; i < 256; i++) {
|
|
|
|
_fromBCD[i] = ((i >> 4) & 0x0f)*10 + (i & 0x0f);
|
|
|
|
_toBCD[i] = (((i % 100) / 10) << 4) | (i % 10);
|
|
|
|
}
|
2016-01-23 21:15:14 +00:00
|
|
|
|
2014-11-19 19:35:16 +00:00
|
|
|
OP *p = _ops;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::brk; *p++=&r6502::ora_ix; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::ora_z; *p++=&r6502::asl_z; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::php; *p++=&r6502::ora_; *p++=&r6502::asl; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop3; *p++=&r6502::ora_a; *p++=&r6502::asl_a; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::bpl; *p++=&r6502::ora_iy; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::ora_zx; *p++=&r6502::asl_zx; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::clc; *p++=&r6502::ora_ay; *p++=&r6502::nop; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::ora_ax; *p++=&r6502::asl_ax; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::jsr; *p++=&r6502::and_ix; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::bit_z; *p++=&r6502::and_z; *p++=&r6502::rol_z; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::plp; *p++=&r6502::and_; *p++=&r6502::rol; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::bit_a; *p++=&r6502::and_a; *p++=&r6502::rol_a; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::bmi; *p++=&r6502::and_iy; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::and_zx; *p++=&r6502::rol_zx; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::sec; *p++=&r6502::and_ay; *p++=&r6502::nop; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop3; *p++=&r6502::and_ax; *p++=&r6502::rol_ax; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::rti; *p++=&r6502::eor_ix; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::eor_z; *p++=&r6502::lsr_z; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::pha; *p++=&r6502::eor_; *p++=&r6502::lsr_; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::jmp; *p++=&r6502::eor_a; *p++=&r6502::lsr_a; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::bvc; *p++=&r6502::eor_iy; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::eor_zx; *p++=&r6502::lsr_zx; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::cli; *p++=&r6502::eor_ay; *p++=&r6502::nop; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop3; *p++=&r6502::eor_ax; *p++=&r6502::lsr_ax; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::rts; *p++=&r6502::adc_ix; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::adc_z; *p++=&r6502::ror_z; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::pla; *p++=&r6502::adc_; *p++=&r6502::ror_; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::jmp_i; *p++=&r6502::adc_a; *p++=&r6502::ror_a; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::bvs; *p++=&r6502::adc_iy; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::adc_zx; *p++=&r6502::ror_zx; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::sei; *p++=&r6502::adc_ay; *p++=&r6502::nop; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop3; *p++=&r6502::adc_ax; *p++=&r6502::ror_ax; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::sta_ix; *p++=&r6502::nop2; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::sty_z; *p++=&r6502::sta_z; *p++=&r6502::stx_z; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::dey; *p++=&r6502::nop2; *p++=&r6502::txa; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::sty_a; *p++=&r6502::sta_a; *p++=&r6502::stx_a; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::bcc; *p++=&r6502::sta_iy; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::sty_zx; *p++=&r6502::sta_zx; *p++=&r6502::stx_zy; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::tya; *p++=&r6502::sta_ay; *p++=&r6502::txs; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::ill; *p++=&r6502::sta_ax; *p++=&r6502::ill; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::ldy_; *p++=&r6502::lda_ix; *p++=&r6502::ldx_; *p++=&r6502::lax_ix;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::ldy_z; *p++=&r6502::lda_z; *p++=&r6502::ldx_z; *p++=&r6502::lax_z;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::tay; *p++=&r6502::lda_; *p++=&r6502::tax; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::ldy_a; *p++=&r6502::lda_a; *p++=&r6502::ldx_a; *p++=&r6502::lax_a;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::bcs; *p++=&r6502::lda_iy; *p++=&r6502::ill; *p++=&r6502::lax_iy;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::ldy_zx; *p++=&r6502::lda_zx; *p++=&r6502::ldx_zy; *p++=&r6502::lax_zy;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::clv; *p++=&r6502::lda_ay; *p++=&r6502::tsx; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::ldy_ax; *p++=&r6502::lda_ax; *p++=&r6502::ldx_ay; *p++=&r6502::lax_ay;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::cpy_; *p++=&r6502::cmp_ix; *p++=&r6502::nop2; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::cpy_z; *p++=&r6502::cmp_z; *p++=&r6502::dec_z; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::iny; *p++=&r6502::cmp_; *p++=&r6502::dex; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::cpy_a; *p++=&r6502::cmp_a; *p++=&r6502::dec_a; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::bne; *p++=&r6502::cmp_iy; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::cmp_zx; *p++=&r6502::dec_zx; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::cld; *p++=&r6502::cmp_ay; *p++=&r6502::nop; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop3; *p++=&r6502::cmp_ax; *p++=&r6502::dec_ax; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::cpx_; *p++=&r6502::sbc_ix; *p++=&r6502::nop2; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::cpx_z; *p++=&r6502::sbc_z; *p++=&r6502::inc_z; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::inx; *p++=&r6502::sbc_; *p++=&r6502::nop; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::cpx_a; *p++=&r6502::sbc_a; *p++=&r6502::inc_a; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::beq; *p++=&r6502::sbc_iy; *p++=&r6502::ill; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop2; *p++=&r6502::sbc_zx; *p++=&r6502::inc_zx; *p++=&r6502::ill;
|
2018-08-10 10:13:45 +00:00
|
|
|
*p++=&r6502::sed; *p++=&r6502::sbc_ay; *p++=&r6502::nop; *p++=&r6502::ill;
|
2014-11-19 19:35:16 +00:00
|
|
|
*p++=&r6502::nop3; *p++=&r6502::sbc_ax; *p++=&r6502::inc_ax; *p++=&r6502::ill;
|
2014-10-18 11:33:48 +00:00
|
|
|
}
|
|
|
|
|