Saya pikir saya akan terus maju dan memposting implementasi saya sendiri. Ini SEPENUHNYA ungolfed, tapi ini implementasi penuh.
- 668 baris C. (tidak termasuk garis kosong atau garis dengan hanya komentar)
- Mendukung (saya pikir) semua instruksi tidak berdokumen.
- Mendukung BCD.
- Waktu siklus clock CPU. (termasuk penyesuaian pada batas halaman tertentu)
- Dapat menjalankan instruksi dengan satu langkah atau dengan menentukan jumlah kutu.
- Mendukung mengaitkan fungsi eksternal untuk dipanggil setelah setiap instruksi dikerjakan. Ini karena pada awalnya untuk emulator NES dan saya menggunakan ini untuk waktu audio.
/ * Fake6502 CPU emulator core v1.1 *******************
* (c) 2011-2013 Mike Chambers *
************************************************ *** /
#termasuk <stdio.h>
#termasuk <stdint.h>
// fungsi yang disediakan secara eksternal
extern uint8_t read6502 (alamat uint16_t);
extern void write6502 (alamat uint16_t, nilai uint8_t);
// 6502 mendefinisikan
#define UNDOCUMENTED // ketika ini didefinisikan, opcode tidak berdokumen ditangani.
// kalau tidak, mereka hanya diperlakukan sebagai NOP.
// # define NES_CPU // ketika ini didefinisikan, desimal biner-kode (BCD)
// bendera status tidak dihormati oleh ADC dan SBC. 2A03
// CPU dalam Sistem Hiburan Nintendo tidak
// mendukung operasi BCD.
#define FLAG_CARRY 0x01
#define FLAG_ZERO 0x02
#define FLAG_INTERRUPT 0x04
#define FLAG_DECIMAL 0x08
#define FLAG_BREAK 0x10
#define FLAG_CONSTANT 0x20
#define FLAG_OVERFLOW 0x40
#define FLAG_SIGN 0x80
#define BASE_STACK 0x100
#define saveaccum (n) a = (uint8_t) ((n) & 0x00FF)
// flag pengubah makro
#define setcarry () status | = FLAG_CARRY
#define clearcarry () status & = (~ FLAG_CARRY)
#define setzero () status | = FLAG_ZERO
#define clearzero () status & = (~ FLAG_ZERO)
#define setinterrupt () status | = FLAG_INTERRUPT
#define clearinterrupt () status & = (~ FLAG_INTERRUPT)
#define setdecimal () status | = FLAG_DECIMAL
#define cleardecimal () status & = (~ FLAG_DECIMAL)
#define setoverflow () status | = FLAG_OVERFLOW
#define clearoverflow () status & = (~ FLAG_OVERFLOW)
#define setsign () status | = FLAG_SIGN
#define clearsign () status & = (~ FLAG_SIGN)
// Tandai makro perhitungan
#define zerocalc (n) {\
jika ((n) & 0x00FF) clearzero (); \
lain setzero (); \
}
#define signcalc (n) {\
jika ((n) & 0x0080) setsign (); \
selain itu clearsign (); \
}
#define carrycalc (n) {\
jika ((n) & 0xFF00) setcarry (); \
selain itu clearcarry (); \
}
#define overflowcalc (n, m, o) {/ * n = hasil, m = akumulator, o = memori * / \
jika (((n) ^ (uint16_t) (m)) & ((n) ^ (o)) & 0x0080) setoverflow (); \
selain itu clearoverflow (); \
}
// 6502 register CPU
uint16_t pc;
uint8_t sp, a, x, y, status = FLAG_CONSTANT;
// variabel pembantu
petunjuk uint64_t = 0; // melacak total instruksi yang dijalankan
uint32_t clockticks6502 = 0, clockgoal6502 = 0;
uint16_t oldpc, ea, reladdr, value, result;
opcode uint8_t, oldstatus;
// beberapa fungsi umum yang digunakan oleh berbagai fungsi lainnya
void push16 (uint16_t pushval) {
write6502 (BASE_STACK + sp, (pushval >> 8) & 0xFF);
write6502 (BASE_STACK + ((sp - 1) & 0xFF), pushval & 0xFF);
sp - = 2;
}
void push8 (uint8_t pushval) {
write6502 (BASE_STACK + sp--, pushval);
}
uint16_t pull16 () {
uint16_t temp16;
temp16 = read6502 (BASE_STACK + ((sp + 1) & 0xFF)) | ((uint16_t) read6502 (BASE_STACK + ((sp + 2) & 0xFF)) << 8);
sp + = 2;
kembali (temp16);
}
uint8_t pull8 () {
return (read6502 (BASE_STACK + ++ sp));
}
membatalkan reset6502 () {
pc = (uint16_t) read6502 (0xFFFC) | ((uint16_t) read6502 (0xFFFD) << 8);
a = 0;
x = 0;
y = 0;
sp = 0xFD;
status | = FLAG_CONSTANT;
}
static void (* addrtable [256]) ();
static void (* optable [256]) ();
uint8_t penaltiop, penaltiaddr;
// menangani fungsi mode, menghitung alamat yang efektif
static void imp () {// tersirat
}
static void acc () {// akumulator
}
void imm () {// langsung
ea = pc ++;
}
static void zp () {// nol-halaman
ea = (uint16_t) read6502 ((uint16_t) pc ++);
}
static void zpx () {// nol-halaman, X
ea = ((uint16_t) read6502 ((uint16_t) pc ++) + (uint16_t) x) & 0xFF; // sampul nol halaman
}
static void zpy () {// nol-halaman, Y
ea = ((uint16_t) read6502 ((uint16_t) pc ++) + (uint16_t) y) & 0xFF; // sampul nol halaman
}
static void rel () {// relatif untuk op cabang (nilai langsung 8-bit, diperpanjang tanda)
reladdr = (uint16_t) read6502 (pc ++);
jika (reladdr & 0x80) reladdr | = 0xFF00;
}
void abso statis () {// absolut
ea = (uint16_t) read6502 (pc) | ((uint16_t) read6502 (pc + 1) << 8);
pc + = 2;
}
static void absx () {// absolute, X
halaman awal uint16_t;
ea = ((uint16_t) read6502 (pc) | ((uint16_t) read6502 (pc + 1) << 8));
halaman awal = ea & 0xFF00;
ea + = (uint16_t) x;
if (startpage! = (ea & 0xFF00)) {// satu siklus penlty untuk lintas halaman pada beberapa opcode
penaltiaddr = 1;
}
pc + = 2;
}
static void absy () {// absolute, Y
halaman awal uint16_t;
ea = ((uint16_t) read6502 (pc) | ((uint16_t) read6502 (pc + 1) << 8));
halaman awal = ea & 0xFF00;
ea + = (uint16_t) y;
if (startpage! = (ea & 0xFF00)) {// satu siklus penlty untuk lintas halaman pada beberapa opcode
penaltiaddr = 1;
}
pc + = 2;
}
static void ind () {// tidak langsung
uint16_t eahelp, eahelp2;
eahelp = (uint16_t) read6502 (pc) | (uint16_t) ((uint16_t) read6502 (pc + 1) << 8);
eahelp2 = (eahelp & 0xFF00) | ((eahelp + 1) & 0x00FF); // replikasi 6502 batas sampul halaman bug
ea = (uint16_t) read6502 (eahelp) | ((uint16_t) read6502 (eahelp2) << 8);
pc + = 2;
}
static void indx () {// (tidak langsung, X)
uint16_t eahelp;
eahelp = (uint16_t) (((uint16_t) read6502 (pc ++) + (uint16_t) x) & 0xFF); // sampul nol halaman untuk penunjuk tabel
ea = (uint16_t) read6502 (eahelp & 0x00FF) | ((uint16_t) read6502 ((eahelp + 1) & 0x00FF) << 8);
}
static void indy () {// (indirect), Y
uint16_t eahelp, eahelp2, startpage;
eahelp = (uint16_t) read6502 (pc ++);
eahelp2 = (eahelp & 0xFF00) | ((eahelp + 1) & 0x00FF); // sampul nol halaman
ea = (uint16_t) read6502 (eahelp) | ((uint16_t) read6502 (eahelp2) << 8);
halaman awal = ea & 0xFF00;
ea + = (uint16_t) y;
if (startpage! = (ea & 0xFF00)) {// satu siklus penlty untuk lintas halaman pada beberapa opcode
penaltiaddr = 1;
}
}
static uint16_t getvalue () {
if (addrtable [opcode] == acc) return ((uint16_t) a);
lain kembali ((uint16_t) read6502 (ea));
}
static void putvalue (uint16_t saveval) {
if (addrtable [opcode] == acc) a = (uint8_t) (saveval & 0x00FF);
lain write6502 (ea, (saveval & 0x00FF));
}
// fungsi pengatur instruksi
static void adc () {
penaltiop = 1;
value = getvalue ();
hasil = (uint16_t) a + value + (uint16_t) (status & FLAG_CARRY);
carrycalc (hasil);
zerocalc (hasil);
overflowcalc (hasil, a, nilai);
signcalc (hasil);
#ifndef NES_CPU
if (status & FLAG_DECIMAL) {
clearcarry ();
if ((a & 0x0F)> 0x09) {
a + = 0x06;
}
if ((a & 0xF0)> 0x90) {
a + = 0x60;
setcarry ();
}
clockticks6502 ++;
}
#berakhir jika
saveaccum (hasil);
}
void statis dan () {
penaltiop = 1;
value = getvalue ();
hasil = (uint16_t) a & nilai;
zerocalc (hasil);
signcalc (hasil);
saveaccum (hasil);
}
static void asl () {
value = getvalue ();
hasil = nilai << 1;
carrycalc (hasil);
zerocalc (hasil);
signcalc (hasil);
putvalue (hasil);
}
static batal bcc () {
if ((status & FLAG_CARRY) == 0) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
static batal bcs () {
if ((status & FLAG_CARRY) == FLAG_CARRY) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
static void beq () {
if ((status & FLAG_ZERO) == FLAG_ZERO) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
static void bit () {
value = getvalue ();
hasil = (uint16_t) a & nilai;
zerocalc (hasil);
status = (status & 0x3F) | (uint8_t) (nilai & 0xC0);
}
static batal bmi () {
if ((status & FLAG_SIGN) == FLAG_SIGN) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
static batal bne () {
if ((status & FLAG_ZERO) == 0) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
static batal bpl () {
if ((status & FLAG_SIGN) == 0) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
static void brk () {
pc ++;
push16 (pc); // dorong alamat instruksi berikutnya ke stack
push8 (status | FLAG_BREAK); // dorong status CPU ke tumpukan
setinterrupt (); // atur flag interrupt
pc = (uint16_t) read6502 (0xFFFE) | ((uint16_t) read6502 (0xFFFF) << 8);
}
static batal bvc () {
if ((status & FLAG_OVERFLOW) == 0) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
bvs static batal () {
if ((status & FLAG_OVERFLOW) == FLAG_OVERFLOW) {
oldpc = pc;
pc + = reladdr;
if ((oldpc & 0xFF00)! = (pc & 0xFF00)) clockticks6502 + = 2; // periksa apakah lompatan melewati batas halaman
lain clockticks6502 ++;
}
}
static void clc () {
clearcarry ();
}
static void cld () {
cleardecimal ();
}
cli static void () {
clearinterrupt ();
}
static void clv () {
clearoverflow ();
}
void cmp statis () {
penaltiop = 1;
value = getvalue ();
result = (uint16_t) a - value;
if (a> = (uint8_t) (value & 0x00FF)) setcarry ();
selain clearcarry ();
if (a == (uint8_t) (value & 0x00FF)) setzero ();
lain clearzero ();
signcalc (hasil);
}
static void cpx () {
value = getvalue ();
result = (uint16_t) x - nilai;
if (x> = (uint8_t) (value & 0x00FF)) setcarry ();
selain clearcarry ();
if (x == (uint8_t) (value & 0x00FF)) setzero ();
lain clearzero ();
signcalc (hasil);
}
static void cpy () {
value = getvalue ();
result = (uint16_t) y - value;
if (y> = (uint8_t) (value & 0x00FF)) setcarry ();
selain clearcarry ();
if (y == (uint8_t) (value & 0x00FF)) setzero ();
lain clearzero ();
signcalc (hasil);
}
static decoid dec () {
value = getvalue ();
hasil = nilai - 1;
zerocalc (hasil);
signcalc (hasil);
putvalue (hasil);
}
static void dex () {
x--;
zerocalc (x);
signcalc (x);
}
static void dey () {
y--;
zerocalc (y);
signcalc (y);
}
static void eor () {
penaltiop = 1;
value = getvalue ();
hasil = (uint16_t) nilai ^;
zerocalc (hasil);
signcalc (hasil);
saveaccum (hasil);
}
static void inc () {
value = getvalue ();
hasil = nilai + 1;
zerocalc (hasil);
signcalc (hasil);
putvalue (hasil);
}
void inx () {
x ++;
zerocalc (x);
signcalc (x);
}
void iny statis () {
y ++;
zerocalc (y);
signcalc (y);
}
static void jmp () {
pc = ea;
}
static void jsr () {
push16 (pc - 1);
pc = ea;
}
static void lda () {
penaltiop = 1;
value = getvalue ();
a = (uint8_t) (nilai & 0x00FF);
zerocalc (a);
signcalc (a);
}
static void ldx () {
penaltiop = 1;
value = getvalue ();
x = (uint8_t) (nilai & 0x00FF);
zerocalc (x);
signcalc (x);
}
static void ldy () {
penaltiop = 1;
value = getvalue ();
y = (uint8_t) (nilai & 0x00FF);
zerocalc (y);
signcalc (y);
}
static lsr () {)
value = getvalue ();
hasil = nilai >> 1;
jika (nilai & 1) setcarry ();
selain clearcarry ();
zerocalc (hasil);
signcalc (hasil);
putvalue (hasil);
}
static void nop () {
beralih (opcode) {
case 0x1C:
case 0x3C:
case 0x5C:
case 0x7C:
kasus 0xDC:
case 0xFC:
penaltiop = 1;
istirahat;
}
}
void ora statis () {
penaltiop = 1;
value = getvalue ();
hasil = (uint16_t) a | nilai;
zerocalc (hasil);
signcalc (hasil);
saveaccum (hasil);
}
static void pha () {
push8 (a);
}
static void php () {
push8 (status | FLAG_BREAK);
}
static void pla () {
a = pull8 ();
zerocalc (a);
signcalc (a);
}
static void plp () {
status = pull8 () | FLAG_CONSTANT;
}
static void rol () {
value = getvalue ();
hasil = (nilai << 1) | (status & FLAG_CARRY);
carrycalc (hasil);
zerocalc (hasil);
signcalc (hasil);
putvalue (hasil);
}
static void ror () {
value = getvalue ();
hasil = (nilai >> 1) | ((status & FLAG_CARRY) << 7);
jika (nilai & 1) setcarry ();
selain clearcarry ();
zerocalc (hasil);
signcalc (hasil);
putvalue (hasil);
}
static void rti () {
status = pull8 ();
value = pull16 ();
pc = nilai;
}
static void rts () {
value = pull16 ();
pc = nilai + 1;
}
static void sbc () {
penaltiop = 1;
value = getvalue () ^ 0x00FF;
hasil = (uint16_t) a + value + (uint16_t) (status & FLAG_CARRY);
carrycalc (hasil);
zerocalc (hasil);
overflowcalc (hasil, a, nilai);
signcalc (hasil);
#ifndef NES_CPU
if (status & FLAG_DECIMAL) {
clearcarry ();
a - = 0x66;
if ((a & 0x0F)> 0x09) {
a + = 0x06;
}
if ((a & 0xF0)> 0x90) {
a + = 0x60;
setcarry ();
}
clockticks6502 ++;
}
#berakhir jika
saveaccum (hasil);
}
static void sec () {
setcarry ();
}
static void sed () {
setdecimal ();
}
static void sei () {
setinterrupt ();
}
static void sta () {
putvalue (a);
}
static void stx () {
putvalue (x);
}
static void sty () {
putvalue(y);
}
static void tax() {
x = a;
zerocalc(x);
signcalc(x);
}
static void tay() {
y = a;
zerocalc(y);
signcalc(y);
}
static void tsx() {
x = sp;
zerocalc(x);
signcalc(x);
}
static void txa() {
a = x;
zerocalc(a);
signcalc(a);
}
static void txs() {
sp = x;
}
static void tya() {
a = y;
zerocalc(a);
signcalc(a);
}
//undocumented instructions
#ifdef UNDOCUMENTED
static void lax() {
lda();
ldx();
}
static void sax() {
sta();
stx();
putvalue(a & x);
if (penaltyop && penaltyaddr) clockticks6502--;
}
static void dcp() {
dec();
cmp();
if (penaltyop && penaltyaddr) clockticks6502--;
}
static void isb() {
inc();
sbc();
if (penaltyop && penaltyaddr) clockticks6502--;
}
static void slo() {
asl();
ora();
if (penaltyop && penaltyaddr) clockticks6502--;
}
static void rla() {
rol();
and();
if (penaltyop && penaltyaddr) clockticks6502--;
}
static void sre() {
lsr();
eor();
if (penaltyop && penaltyaddr) clockticks6502--;
}
static void rra() {
ror();
adc();
if (penaltyop && penaltyaddr) clockticks6502--;
}
#else
#define lax nop
#define sax nop
#define dcp nop
#define isb nop
#define slo nop
#define rla nop
#define sre nop
#define rra nop
#endif
static void (*addrtable[256])() = {
/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | */
/* 0 */ imp, indx, imp, indx, zp, zp, zp, zp, imp, imm, acc, imm, abso, abso, abso, abso, /* 0 */
/* 1 */ rel, indy, imp, indy, zpx, zpx, zpx, zpx, imp, absy, imp, absy, absx, absx, absx, absx, /* 1 */
/* 2 */ abso, indx, imp, indx, zp, zp, zp, zp, imp, imm, acc, imm, abso, abso, abso, abso, /* 2 */
/* 3 */ rel, indy, imp, indy, zpx, zpx, zpx, zpx, imp, absy, imp, absy, absx, absx, absx, absx, /* 3 */
/* 4 */ imp, indx, imp, indx, zp, zp, zp, zp, imp, imm, acc, imm, abso, abso, abso, abso, /* 4 */
/* 5 */ rel, indy, imp, indy, zpx, zpx, zpx, zpx, imp, absy, imp, absy, absx, absx, absx, absx, /* 5 */
/* 6 */ imp, indx, imp, indx, zp, zp, zp, zp, imp, imm, acc, imm, ind, abso, abso, abso, /* 6 */
/* 7 */ rel, indy, imp, indy, zpx, zpx, zpx, zpx, imp, absy, imp, absy, absx, absx, absx, absx, /* 7 */
/* 8 */ imm, indx, imm, indx, zp, zp, zp, zp, imp, imm, imp, imm, abso, abso, abso, abso, /* 8 */
/* 9 */ rel, indy, imp, indy, zpx, zpx, zpy, zpy, imp, absy, imp, absy, absx, absx, absy, absy, /* 9 */
/* A */ imm, indx, imm, indx, zp, zp, zp, zp, imp, imm, imp, imm, abso, abso, abso, abso, /* A */
/* B */ rel, indy, imp, indy, zpx, zpx, zpy, zpy, imp, absy, imp, absy, absx, absx, absy, absy, /* B */
/* C */ imm, indx, imm, indx, zp, zp, zp, zp, imp, imm, imp, imm, abso, abso, abso, abso, /* C */
/* D */ rel, indy, imp, indy, zpx, zpx, zpx, zpx, imp, absy, imp, absy, absx, absx, absx, absx, /* D */
/* E */ imm, indx, imm, indx, zp, zp, zp, zp, imp, imm, imp, imm, abso, abso, abso, abso, /* E */
/* F */ rel, indy, imp, indy, zpx, zpx, zpx, zpx, imp, absy, imp, absy, absx, absx, absx, absx /* F */
};
static void (*optable[256])() = {
/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | */
/* 0 */ brk, ora, nop, slo, nop, ora, asl, slo, php, ora, asl, nop, nop, ora, asl, slo, /* 0 */
/* 1 */ bpl, ora, nop, slo, nop, ora, asl, slo, clc, ora, nop, slo, nop, ora, asl, slo, /* 1 */
/* 2 */ jsr, and, nop, rla, bit, and, rol, rla, plp, and, rol, nop, bit, and, rol, rla, /* 2 */
/* 3 */ bmi, and, nop, rla, nop, and, rol, rla, sec, and, nop, rla, nop, and, rol, rla, /* 3 */
/* 4 */ rti, eor, nop, sre, nop, eor, lsr, sre, pha, eor, lsr, nop, jmp, eor, lsr, sre, /* 4 */
/* 5 */ bvc, eor, nop, sre, nop, eor, lsr, sre, cli, eor, nop, sre, nop, eor, lsr, sre, /* 5 */
/* 6 */ rts, adc, nop, rra, nop, adc, ror, rra, pla, adc, ror, nop, jmp, adc, ror, rra, /* 6 */
/* 7 */ bvs, adc, nop, rra, nop, adc, ror, rra, sei, adc, nop, rra, nop, adc, ror, rra, /* 7 */
/* 8 */ nop, sta, nop, sax, sty, sta, stx, sax, dey, nop, txa, nop, sty, sta, stx, sax, /* 8 */
/* 9 */ bcc, sta, nop, nop, sty, sta, stx, sax, tya, sta, txs, nop, nop, sta, nop, nop, /* 9 */
/* A */ ldy, lda, ldx, lax, ldy, lda, ldx, lax, tay, lda, tax, nop, ldy, lda, ldx, lax, /* A */
/* B */ bcs, lda, nop, lax, ldy, lda, ldx, lax, clv, lda, tsx, lax, ldy, lda, ldx, lax, /* B */
/* C */ cpy, cmp, nop, dcp, cpy, cmp, dec, dcp, iny, cmp, dex, nop, cpy, cmp, dec, dcp, /* C */
/* D */ bne, cmp, nop, dcp, nop, cmp, dec, dcp, cld, cmp, nop, dcp, nop, cmp, dec, dcp, /* D */
/* E */ cpx, sbc, nop, isb, cpx, sbc, inc, isb, inx, sbc, nop, sbc, cpx, sbc, inc, isb, /* E */
/* F */ beq, sbc, nop, isb, nop, sbc, inc, isb, sed, sbc, nop, isb, nop, sbc, inc, isb /* F */
};
static const uint32_t ticktable[256] = {
/* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | */
/* 0 */ 7, 6, 2, 8, 3, 3, 5, 5, 3, 2, 2, 2, 4, 4, 6, 6, /* 0 */
/* 1 */ 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7, /* 1 */
/* 2 */ 6, 6, 2, 8, 3, 3, 5, 5, 4, 2, 2, 2, 4, 4, 6, 6, /* 2 */
/* 3 */ 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7, /* 3 */
/* 4 */ 6, 6, 2, 8, 3, 3, 5, 5, 3, 2, 2, 2, 3, 4, 6, 6, /* 4 */
/* 5 */ 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7, /* 5 */
/* 6 */ 6, 6, 2, 8, 3, 3, 5, 5, 4, 2, 2, 2, 5, 4, 6, 6, /* 6 */
/* 7 */ 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7, /* 7 */
/* 8 */ 2, 6, 2, 6, 3, 3, 3, 3, 2, 2, 2, 2, 4, 4, 4, 4, /* 8 */
/* 9 */ 2, 6, 2, 6, 4, 4, 4, 4, 2, 5, 2, 5, 5, 5, 5, 5, /* 9 */
/* A */ 2, 6, 2, 6, 3, 3, 3, 3, 2, 2, 2, 2, 4, 4, 4, 4, /* A */
/* B */ 2, 5, 2, 5, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 4, 4, /* B */
/* C */ 2, 6, 2, 8, 3, 3, 5, 5, 2, 2, 2, 2, 4, 4, 6, 6, /* C */
/* D */ 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7, /* D */
/* E */ 2, 6, 2, 8, 3, 3, 5, 5, 2, 2, 2, 2, 4, 4, 6, 6, /* E */
/* F */ 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7 /* F */
};
void nmi6502() {
push16(pc);
push8(status);
status |= FLAG_INTERRUPT;
pc = (uint16_t)read6502(0xFFFA) | ((uint16_t)read6502(0xFFFB) << 8);
}
void irq6502() {
push16(pc);
push8(status);
status |= FLAG_INTERRUPT;
pc = (uint16_t)read6502(0xFFFE) | ((uint16_t)read6502(0xFFFF) << 8);
}
uint8_t callexternal = 0;
void (*loopexternal)();
void exec6502(uint32_t tickcount) {
clockgoal6502 += tickcount;
while (clockticks6502 < clockgoal6502) {
opcode = read6502(pc++);
penaltyop = 0;
penaltyaddr = 0;
(*addrtable[opcode])();
(*optable[opcode])();
clockticks6502 += ticktable[opcode];
if (penaltyop && penaltyaddr) clockticks6502++;
instructions++;
if (callexternal) (*loopexternal)();
}
}
void step6502() {
opcode = read6502(pc++);
penaltyop = 0;
penaltyaddr = 0;
(*addrtable[opcode])();
(*optable[opcode])();
clockticks6502 += ticktable[opcode];
if (penaltyop && penaltyaddr) clockticks6502++;
clockgoal6502 = clockticks6502;
instructions++;
if (callexternal) (*loopexternal)();
}
void hookexternal(void *funcptr) {
if (funcptr != (void *)NULL) {
loopexternal = funcptr;
callexternal = 1;
} else callexternal = 0;
}