Skip to content

Instantly share code, notes, and snippets.

@tom-seddon
Created January 12, 2017 13:26
Show Gist options
  • Save tom-seddon/d1ccdb18e3c3d4a2c288a12c1993e4b2 to your computer and use it in GitHub Desktop.
Save tom-seddon/d1ccdb18e3c3d4a2c288a12c1993e4b2 to your computer and use it in GitHub Desktop.
State-based 6502 emulator snippet
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// Read/Absolute,X
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
static void T1_R_ABX(M6502 *);
static void T2_R_ABX(M6502 *);
static void T3_R_ABX(M6502 *);
static void T4_R_ABX(M6502 *);
static void T0_R_ABX(M6502 *s) {
/* T0 phase 2 */
/* (decode - already done) */
/* T1 phase 1 */
s->abus.w=s->pc.w++;
s->read=1;
s->tfn=&T1_R_ABX;
}
static void T1_R_ABX(M6502 *s) {
/* T1 phase 2 */
s->ad.b.l=s->dbus;
/* T2 phase 1 */
s->abus.w=s->pc.w++;
s->read=1;
s->tfn=&T2_R_ABX;
}
static void T2_R_ABX(M6502 *s) {
/* T2 phase 2 */
s->ad.b.h=s->dbus;
/* T3 phase 1 */
s->abus.w=s->ad.b.l+s->x;
s->acarry=s->abus.b.h;
s->abus.b.h=s->ad.b.h;
s->read=1;
if(s->acarry) {
CheckForInterrupts(s);
}
s->tfn=&T3_R_ABX;
}
static void T3_R_ABX(M6502 *s) {
/* T3 phase 2 */
s->data=s->dbus;
if(!s->acarry) {
/* No carry - bail out early. */
(*s->ifn)(s);
#ifdef _DEBUG
s->ifn=NULL;
#endif
/* T0 phase 1 */
M6502_NextInstruction(s);
return;
}
/* T4 phase 1 */
s->abus.w=s->ad.w+s->x;
s->read=1;
s->tfn=&T4_R_ABX;
CheckForInterrupts(s);
}
static void T4_R_ABX(M6502 *s) {
/* T4 phase 2 */
s->data=s->dbus;
(*s->ifn)(s);
#ifdef _DEBUG
s->ifn=NULL;
#endif
/* T0 phase 1 */
M6502_NextInstruction(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment