Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

cpu.c File Reference


Detailed Description

The core of processor, this file contains instruction decoder and memory structure. Before use, call Reset() to set correct values of registers. Execution of one instruction is done by calling InstrStep().

Known bugs:

Definition in file cpu.c.#include "cpu.h"

Go to the source code of this file.

Flags

#define FN   0x80
#define FV   0x40
#define FB   0x10
#define FD   0x08
#define FI   0x04
#define FZ   0x02
#define FC   0x01

Adresses

#define RESETVKT   0xFFFC
#define IRQVKT   0xFFFE
#define NMIVKT   0xFFFA

Flag handling defines

#define GETFLAG(X)   ((regs.P & (X))!=0)
#define SETFLAG(X)   regs.P=((regs.P) | (X))
#define CLFLAG(X)   (regs.P=regs.P & (~(X)))
#define FLAG(BL, FL)   if (BL) SETFLAG(FL); else CLFLAG(FL)
#define SETNZFLAGS(X)   FLAG((X) & 0x80, FN); FLAG(!(X), FZ)

Other defines

#define ADR(X, Y)   (((Y) << PAGESIZE)+(X))
#define M2PC(X)   regs.PC=((*memgetb)((X) + 1) << PAGESIZE) + (*memgetb)(X)
#define HI(X)   ((unsigned char) (((X) >> PAGESIZE) & 0xFF))
#define LO(X)   ((unsigned char) ((X) & 0xFF))
#define PCSTEP(X)   regs.PC+=(X)
#define HEX2DEC(X)   ((((X)>>4) & 0x0F)*10 + ((X) & 0x0F))
#define DEC2HEX(X)   (((((X)/10) % 10)<<4) + (X) % 10)

Stack handling defines

#define STACKADD(X)   (*memsetb)(regs.S, (X)); regs.S--; if (regs.S<0x100) regs.S=0x1FF
#define STACKREMOVE(X)   regs.S++; if (regs.S>0x1FF) regs.S=0x100; X=(*memgetb)(regs.S)

Cycle consumption defines.

#define CONSUME(X)   cycles += X
#define CONSREL   if ((oldpc & 0xFF00) != (regs.PC & 0xFF00)) CONSUME(1)
#define CONSABSX
#define CONSABSY
#define CONSINDY

Memory reading/writing macros - according to adressing mode

#define IMM   regs.PC+1
#define ZP   (*memgetb)(regs.PC+1)&0xFF
#define ZPX   ((*memgetb)(regs.PC+1)+regs.X)&0xFF
#define ZPY   ((*memgetb)(regs.PC+1)+regs.Y)&0xFF
#define ABS   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))
#define ABSX   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))+regs.X
#define ABSY   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))+regs.Y
#define INDX   ADR((*memgetb)(((*memgetb)(regs.PC+1)+regs.X) & 0xFF), (*memgetb)(((*memgetb)(regs.PC+1)+regs.X+1) & 0xFF))
#define INDY   ADR((*memgetb)((*memgetb)(regs.PC+1)), (*memgetb)(((*memgetb)(regs.PC+1)+1)) & 0xFF) + regs.Y
#define REL   regs.PC+1
#define JMPADR   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))

Instruction defs

#define ADC(MEMREAD, STEP)
#define AND(MEMREAD, STEP)
#define ASL(MEMREAD, STEP)
#define ASLA
#define BCC(MEMREAD, STEP)
#define BCS(MEMREAD, STEP)
#define BEQ(MEMREAD, STEP)
#define BNE(MEMREAD, STEP)
#define BMI(MEMREAD, STEP)
#define BPL(MEMREAD, STEP)
#define BVC(MEMREAD, STEP)
#define BVS(MEMREAD, STEP)
#define BIT(MEMREAD, STEP)
#define BRK
#define CLC
#define CLD
#define CLI
#define CLV
#define CMP(MEMREAD, STEP)
#define CPX(MEMREAD, STEP)
#define CPY(MEMREAD, STEP)
#define DEC(MEMREAD, STEP)
#define DEX
#define DEY
#define EOR(MEMREAD, STEP)
#define INC(MEMREAD, STEP)
#define INX
#define INY
#define JMP(MEMREAD)   regs.PC=MEMREAD
#define JMPIND
#define JSR(MEMREAD)
#define LDA(MEMREAD, STEP)
#define LDX(MEMREAD, STEP)
#define LDY(MEMREAD, STEP)
#define LSR(MEMREAD, STEP)
#define LSRA
#define NOP   PCSTEP(1)
#define ORA(MEMREAD, STEP)
#define PHA
#define PHP
#define PLA
#define PLP
#define ROL(MEMREAD, STEP)
#define ROLA
#define ROR(MEMREAD, STEP)
#define RORA
#define RTI
#define RTS
#define SBC(MEMREAD, STEP)
#define SEC
#define SED
#define SEI
#define STA(MEMREAD, STEP)
#define STX(MEMREAD, STEP)
#define STY(MEMREAD, STEP)
#define TAX
#define TXA
#define TAY
#define TYA
#define TSX
#define TXS

Defines

#define TRUE   1
#define FALSE   0
#define PAGESIZE   8

Functions

UINT InstrStep ()
 Executes one instruction on adress stored in regs.PC.

RegsGetRegs ()
void SetRegs (Regs r)
void Reset ()
 Reset interrupt.

void Irq ()
 IRQ interrupt.

void Nmi ()
 NMI interrupt.


Variables

Regs regs


Define Documentation

#define ABS   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))
 

Absolute adressing mode

Definition at line 140 of file cpu.c.

#define ABSX   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))+regs.X
 

Absolute, X adressing mode

Definition at line 143 of file cpu.c.

#define ABSY   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))+regs.Y
 

Absolute, Y adressing mode

Definition at line 146 of file cpu.c.

#define ADC MEMREAD,
STEP   
 

Value:

if (regs.P & FD) {      \
                tmp=HEX2DEC(regs.A)+HEX2DEC((*memgetb)(MEMREAD))+GETFLAG(FC); \
                if( tmp > 99 ) {        \
                        tmp=DEC2HEX(tmp);       \
                        SETFLAG(FC);    \
                        if ((tmp & 0x40)==(regs.A & 0x40)) SETFLAG(FV); \
                }       \
                else {  \
                        tmp=DEC2HEX(tmp);       \
                        if (!(tmp & 0x40) && (regs.A & 0x40)) SETFLAG(FV); \
                }       \
                regs.A=tmp; \
        }       \
        else {  \
                tmp=regs.A+(*memgetb)(MEMREAD)+GETFLAG(FC);     \
                if(tmp<regs.A) {        \
                        SETFLAG(FC);    \
                        if ((tmp & 0x40)==(regs.A & 0x40)) SETFLAG(FV); \
                }       \
                else {  \
                        if (!(tmp & 0x40) && (regs.A & 0x40)) SETFLAG(FV); \
                }       \
                regs.A=tmp;     \
        }       \
        SETNZFLAGS(regs.A); \
        PCSTEP(STEP)
ADC: [A] + [M] + [C] -> [A], sets NVZC

Definition at line 169 of file cpu.c.

#define ADR X,
     (((Y) << PAGESIZE)+(X))
 

Converts 2 bytes to 16-bit adress

Definition at line 65 of file cpu.c.

#define AND MEMREAD,
STEP   
 

Value:

regs.A=regs.A & (*memgetb)(MEMREAD); \
        SETNZFLAGS(regs.A); \
        PCSTEP(STEP)
AND: [A] & [M] -> [A], sets NZ

Definition at line 198 of file cpu.c.

#define ASL MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        FLAG(FC, tmp & 0x80);   \
        tmp=(tmp << 1); \
        (*memsetb)(MEMREAD, tmp);       \
        SETNZFLAGS(tmp);        \
        PCSTEP(STEP)
ASL: [C] <- [M] <- 0, left shift, sets NZC

Definition at line 204 of file cpu.c.

#define ASLA
 

Value:

FLAG(FC, regs.A & 0x80);        \
        regs.A=(regs.A << 1);   \
        SETNZFLAGS(regs.A);     \
        PCSTEP(1)
ASL: [C] <- [A] <- 0, left shift, sets NZ

Definition at line 213 of file cpu.c.

#define BCC MEMREAD,
STEP   
 

Value:

if (!GETFLAG(FC)) {     \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BCC: relative jump, if [C]=0

Definition at line 220 of file cpu.c.

#define BCS MEMREAD,
STEP   
 

Value:

if (GETFLAG(FC)) {      \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BCS: relative jump, if [C]=1

Definition at line 237 of file cpu.c.

#define BEQ MEMREAD,
STEP   
 

Value:

if (GETFLAG(FZ)) {      \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BEQ: relative jump, if [Z]=1

Definition at line 254 of file cpu.c.

#define BIT MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        FLAG(FN, tmp & 0x80);   \
        FLAG(FV, tmp & 0x40);   \
        tmp=tmp & regs.A;       \
        FLAG(FZ, ~tmp); \
        PCSTEP(STEP)
BIT: [A] and [M], [M7] -> [N], [M6] -> [V], result sets [Z]

Definition at line 356 of file cpu.c.

#define BMI MEMREAD,
STEP   
 

Value:

if (GETFLAG(FN)) {      \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BMI: relative jump, if [N]=1

Definition at line 288 of file cpu.c.

#define BNE MEMREAD,
STEP   
 

Value:

if (!GETFLAG(FZ)) {     \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BNE: relative jump, if [Z]=0

Definition at line 271 of file cpu.c.

#define BPL MEMREAD,
STEP   
 

Value:

if (!GETFLAG(FN)) {     \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BPL: relative jump, if [N]=0

Definition at line 305 of file cpu.c.

#define BRK
 

Value:

PCSTEP(2);      \
        SETFLAG(FB);    \
        STACKADD(HI(regs.PC));  \
        STACKADD(LO(regs.PC));  \
        STACKADD(regs.P);       \
        SETFLAG(FI);    \
        CLFLAG(FD);     \
        M2PC(IRQVKT)
BRK: Sw interrupt, 1 -> [B], [PC] -> Stack, [P] -> Stack, 1 -> [I] 0 -> [D] (According to Western Design Center W65C02), IRQVKT -> PC

Definition at line 367 of file cpu.c.

#define BVC MEMREAD,
STEP   
 

Value:

if (!GETFLAG(FV)) {     \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BVC: relative jump, if [V]=0

Definition at line 322 of file cpu.c.

#define BVS MEMREAD,
STEP   
 

Value:

if (GETFLAG(FC)) {      \
                CONSUME(3);     \
                tmp=(*memgetb)(MEMREAD);        \
                if (tmp>0x7F) { \
                        PCSTEP(STEP-0x100+tmp); \
                }       \
                else {  \
                        PCSTEP(STEP+tmp);       \
                }       \
        }       \
        else {  \
                PCSTEP(STEP);   \
                CONSUME(2);     \
        }
BVS: relative jump, if [V]=1

Definition at line 339 of file cpu.c.

#define CLC
 

Value:

CLFLAG(FC);     \
        PCSTEP(1)
CLC: 0 -> [C]

Definition at line 378 of file cpu.c.

#define CLD
 

Value:

CLFLAG(FD);     \
        PCSTEP(1)
CLD: 0 -> [D]

Definition at line 383 of file cpu.c.

#define CLFLAG      (regs.P=regs.P & (~(X)))
 

Sets flag X to 0. Affects P register

Definition at line 52 of file cpu.c.

#define CLI
 

Value:

CLFLAG(FI);     \
        PCSTEP(1)
CLI: 0 -> [I]

Definition at line 388 of file cpu.c.

#define CLV
 

Value:

CLFLAG(FV);     \
        PCSTEP(1)
CLV: 0 -> [V]

Definition at line 393 of file cpu.c.

#define CMP MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        if (regs.A<tmp) SETFLAG(FC);    \
        SETNZFLAGS(regs.A-tmp); \
        PCSTEP(STEP)
CMP: [A] - [M], result sets NZC

Definition at line 398 of file cpu.c.

#define CONSABSX
 

Value:

if ((ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2)) & 0xFF00) !=     \
                ((ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2)) + regs.X) & 0xFF00)) \
                CONSUME(1)
If page boundary is crossed, add 1 cycle

Definition at line 109 of file cpu.c.

#define CONSABSY
 

Value:

if ((ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2)) & 0xFF00) !=     \
                ((ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2)) + regs.Y) & 0xFF00)) \
                CONSUME(1)
If page boundary is crossed, add 1 cycle

Definition at line 114 of file cpu.c.

#define CONSINDY
 

Value:

if ((ADR((*memgetb)((*memgetb)(regs.PC+1)), (*memgetb)((*memgetb)(regs.PC+1))+1) & 0xFF00) !=   \
                ((ADR((*memgetb)((*memgetb)(regs.PC+1)), (*memgetb)((*memgetb)(regs.PC+1))+1) + regs.Y) & 0xFF00)) \
                CONSUME(1)
If page boundary is crossed, add 1 cycle

Definition at line 119 of file cpu.c.

#define CONSREL   if ((oldpc & 0xFF00) != (regs.PC & 0xFF00)) CONSUME(1)
 

If branch crosses a page boundary, add 1 cycle

Definition at line 106 of file cpu.c.

#define CONSUME      cycles += X
 

Consume X cycles

Definition at line 103 of file cpu.c.

#define CPX MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        if (regs.X<tmp) SETFLAG(FC);    \
        SETNZFLAGS(regs.X-tmp); \
        PCSTEP(STEP)
CPX: [X] - [M], result sets NZC

Definition at line 405 of file cpu.c.

#define CPY MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        if (regs.Y<tmp) SETFLAG(FC);    \
        SETNZFLAGS(regs.Y-tmp); \
        PCSTEP(STEP)
CPY: [Y] - [M], result sets NZC

Definition at line 412 of file cpu.c.

#define DEC MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        tmp--;  \
        SETNZFLAGS(tmp);        \
        (*memsetb)(MEMREAD, tmp);       \
        PCSTEP(STEP)
DEC: [M] - 1 -> [M], sets NZ

Definition at line 419 of file cpu.c.

#define DEC2HEX      (((((X)/10) % 10)<<4) + (X) % 10)
 

Converts number in BCD code to std. decimal. Example> $10 hex -> 10 dec

Definition at line 83 of file cpu.c.

#define DEX
 

Value:

DEX: [X] -1 -> [X], sets NZ

Definition at line 427 of file cpu.c.

#define DEY
 

Value:

DEY: [Y] - 1 -> [Y], sets NZ

Definition at line 433 of file cpu.c.

#define EOR MEMREAD,
STEP   
 

Value:

regs.A=regs.A ^ (*memgetb)(MEMREAD);    \
        SETNZFLAGS(regs.A);     \
        PCSTEP(STEP)
EOR: [A] xor [M] -> [A], sets NZ

Definition at line 439 of file cpu.c.

#define FB   0x10
 

BRK flag

Definition at line 28 of file cpu.c.

#define FC   0x01
 

Caryy flag

Definition at line 32 of file cpu.c.

#define FD   0x08
 

Decimal flag

Definition at line 29 of file cpu.c.

#define FI   0x04
 

Interrupt flag

Definition at line 30 of file cpu.c.

#define FLAG BL,
FL       if (BL) SETFLAG(FL); else CLFLAG(FL)
 

If condition BL is satisfied, flag FL is set to 1 otherwise 0

Definition at line 55 of file cpu.c.

#define FN   0x80
 

Negative flag

Definition at line 26 of file cpu.c.

#define FV   0x40
 

Overflow flag

Definition at line 27 of file cpu.c.

#define FZ   0x02
 

Zero flag

Definition at line 31 of file cpu.c.

#define GETFLAG      ((regs.P & (X))!=0)
 

Gets value of flag X, for conditions

Definition at line 46 of file cpu.c.

#define HEX2DEC      ((((X)>>4) & 0x0F)*10 + ((X) & 0x0F))
 

Converts number to BCD. Example: 10 dec -> $10 hex

Definition at line 80 of file cpu.c.

#define HI      ((unsigned char) (((X) >> PAGESIZE) & 0xFF))
 

Higher byte of word

Definition at line 71 of file cpu.c.

#define IMM   regs.PC+1
 

Immediate adressing mode

Definition at line 128 of file cpu.c.

#define INC MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        tmp++;  \
        SETNZFLAGS(tmp);        \
        (*memsetb)(MEMREAD, tmp);       \
        PCSTEP(STEP)
INC: [M] + 1 -> [M], sets NZ

Definition at line 445 of file cpu.c.

#define INDX   ADR((*memgetb)(((*memgetb)(regs.PC+1)+regs.X) & 0xFF), (*memgetb)(((*memgetb)(regs.PC+1)+regs.X+1) & 0xFF))
 

Indirect, X adressing mode

Definition at line 149 of file cpu.c.

#define INDY   ADR((*memgetb)((*memgetb)(regs.PC+1)), (*memgetb)(((*memgetb)(regs.PC+1)+1)) & 0xFF) + regs.Y
 

Indirect, Y adressing mode

Definition at line 152 of file cpu.c.

#define INX
 

Value:

INX: [X] + 1 -> [X], sets NZ

Definition at line 453 of file cpu.c.

#define INY
 

Value:

INY: [Y] + 1 -> [Y], sets NZ

Definition at line 459 of file cpu.c.

#define IRQVKT   0xFFFE
 

IRQ jumps here

Definition at line 39 of file cpu.c.

#define JMP MEMREAD       regs.PC=MEMREAD
 

JMP: [M] -> [PC] JMP(..) only for ABSOLUTE adressing! Explanation follows

Definition at line 467 of file cpu.c.

#define JMPADR   ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))
 

Used only for jmp, should be absolute

Definition at line 158 of file cpu.c.

#define JMPIND
 

Value:

regs.PC=ADR(    \
                (*memgetb)(ADR((*memgetb)(regs.PC+1), (*memgetb)(regs.PC+2))),  \
                (*memgetb)(ADR(((*memgetb)(regs.PC+1)+1) & 0xFF, (*memgetb)(regs.PC+2))))
Indirect jump must never use adress beginning on 0xXXFF. if so, jump will be done to adress stored in 0xXXFF and 0xXX00 ! (no page overflow)

Definition at line 473 of file cpu.c.

#define JSR MEMREAD   
 

Value:

tmpjsr=MEMREAD; \
        PCSTEP(2);      \
        STACKADD(HI(regs.PC));  \
        STACKADD(LO(regs.PC));  \
        regs.PC=tmpjsr
JSR: [PC] + 2 - 1 -> Stack, [M] -> PC JSR puts adress of next instruction-1 on the stack (rts adds +1)

Definition at line 481 of file cpu.c.

#define LDA MEMREAD,
STEP   
 

Value:

regs.A=(*memgetb)(MEMREAD);     \
        SETNZFLAGS(regs.A);     \
        PCSTEP(STEP)
LDA: [M] -> [A], sets NZ

Definition at line 489 of file cpu.c.

#define LDX MEMREAD,
STEP   
 

Value:

regs.X=(*memgetb)(MEMREAD);     \
        SETNZFLAGS(regs.X);     \
        PCSTEP(STEP)
LDX: [M] -> [X], sets NZ

Definition at line 495 of file cpu.c.

#define LDY MEMREAD,
STEP   
 

Value:

regs.Y=(*memgetb)(MEMREAD);     \
        SETNZFLAGS(regs.Y);     \
        PCSTEP(STEP)
LDY: [M] -> [Y], sets NZ

Definition at line 501 of file cpu.c.

#define LO      ((unsigned char) ((X) & 0xFF))
 

Lower byte of word

Definition at line 74 of file cpu.c.

#define LSR MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        FLAG(FC, tmp & 0x01);   \
        tmp=(tmp >> 1); \
        (*memsetb)(MEMREAD, tmp);       \
        SETNZFLAGS(tmp);        \
        PCSTEP(STEP)
LSR: 0 -> [M] -> [C], right shift, sets: NZC

Definition at line 507 of file cpu.c.

#define LSRA
 

Value:

FLAG(FC, regs.A & 0x01);        \
        regs.A=(regs.A >> 1);   \
        SETNZFLAGS(regs.A);     \
        PCSTEP(1)
LSR: 0 -> [A] -> [C], right shift, sets: NZC

Definition at line 516 of file cpu.c.

#define M2PC      regs.PC=((*memgetb)((X) + 1) << PAGESIZE) + (*memgetb)(X)
 

Sets PC to 2b adress stored in memory from adress X

Definition at line 68 of file cpu.c.

#define NOP   PCSTEP(1)
 

NOP: Reserved

Definition at line 523 of file cpu.c.

#define ORA MEMREAD,
STEP   
 

Value:

regs.A=regs.A | (*memgetb)(MEMREAD);    \
        SETNZFLAGS(regs.A);     \
        PCSTEP(STEP)
ORA: [A] or [M] -> [A], sets NZ

Definition at line 527 of file cpu.c.

#define PCSTEP      regs.PC+=(X)
 

Moves execution point

Definition at line 77 of file cpu.c.

#define PHA
 

Value:

PHA: A -> Stack

Definition at line 533 of file cpu.c.

#define PHP
 

Value:

PHP: P -> Stack

Definition at line 538 of file cpu.c.

#define PLA
 

Value:

PLA: Stack -> A, sets NZ

Definition at line 543 of file cpu.c.

#define PLP
 

Value:

STACKREMOVE(regs.P);    \
        regs.P=regs.P | 0x20;   \
        PCSTEP(1)
PLP: Stack -> P, sets NVBDIZC

Definition at line 549 of file cpu.c.

#define REL   regs.PC+1
 

Relative adressing mode

Definition at line 155 of file cpu.c.

#define RESETVKT   0xFFFC
 

Reset interrupt jumps here

Definition at line 38 of file cpu.c.

#define ROL MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        tmpc=GETFLAG(FC);       \
        FLAG(FC, tmp & 0x80);   \
        tmp=(tmp << 1) | ((tmpc) ? 0x01 : 0x00);        \
        (*memsetb)(MEMREAD, tmp);       \
        PCSTEP(STEP)
ROL: [C_new] <- [M] <- [C_old], left rotation, sets NZC

Definition at line 555 of file cpu.c.

#define ROLA
 

Value:

tmpc=GETFLAG(FC);       \
        FLAG(FC, regs.A & 0x80);        \
        regs.A=(regs.A << 1) | ((tmpc) ? 0x01 : 0x00);  \
        PCSTEP(1)
ROL: [C_new] <- [A] <- [C_old], left rotation, sets NZC

Definition at line 564 of file cpu.c.

#define ROR MEMREAD,
STEP   
 

Value:

tmp=(*memgetb)(MEMREAD);        \
        tmpc=GETFLAG(FC);       \
        FLAG(FC, tmp & 0x01);   \
        tmp=(tmp >> 1) | ((tmpc) ? 0x80 : 0x00);        \
        (*memsetb)(MEMREAD, tmp);       \
        PCSTEP(STEP)
ROR: [C_old] -> [M] -> [C_new], right rotation, sets NZC

Definition at line 571 of file cpu.c.

#define RORA
 

Value:

tmpc=GETFLAG(FC);       \
        FLAG(FC, regs.A & 0x01);        \
        regs.A=(regs.A >> 1) | ((tmpc) ? 0x80 : 0x00);  \
        PCSTEP(1)
ROR A: [C_old] -> [A] -> [C_new], right rotation, sets NZC

Definition at line 580 of file cpu.c.

#define RTI
 

Value:

STACKREMOVE(regs.P);    \
        STACKREMOVE(tmp);       \
        STACKREMOVE(regs.PC);   \
        regs.PC=(regs.PC << 8) + tmp
RTI: Stack -> [P], Stack -> [PC], return from interrupt, sets NVBDIZC

Definition at line 587 of file cpu.c.

#define RTS
 

Value:

STACKREMOVE(tmp);       \
        STACKREMOVE(regs.PC);   \
        regs.PC=(regs.PC << 8) + tmp + 1
RTS: Stack + 1 -> [PC], return from JSR subroutine (for +1 see JSR)

Definition at line 594 of file cpu.c.

#define SBC MEMREAD,
STEP   
 

Value:

if (regs.P & FD) {      \
                tmp=HEX2DEC(regs.A)-HEX2DEC((*memgetb)(MEMREAD))- (1-GETFLAG(FC)); \
                if(tmp<=99) {   \
                        tmp=DEC2HEX(tmp);       \
                        SETFLAG(FC);    \
                        if ((tmp & 0x40) && !(regs.A & 0x40)) SETFLAG(FV);      \
                }       \
                else {  \
                        tmp=tmp-156;    \
                        tmp=DEC2HEX(tmp);       \
                        if ((tmp & 0x40)==(regs.A & 0x40)) SETFLAG(FV); \
                }       \
                regs.A=tmp; \
        }       \
        else {  \
                tmp=regs.A-(*memgetb)(MEMREAD)-(1-GETFLAG(FC)); \
                if(tmp<=regs.A) {       \
                        SETFLAG(FC);    \
                        if ((tmp & 0x40) && !(regs.A & 0x40)) SETFLAG(FV);      \
                }       \
                else {  \
                        if ((tmp & 0x40)==(regs.A & 0x40)) SETFLAG(FV); \
                }       \
                regs.A=tmp;     \
        }       \
        SETNZFLAGS(regs.A); \
        PCSTEP(STEP)
SBC: [A] - [M] - non[C] -> [A], sets NVZ(non C)

Definition at line 600 of file cpu.c.

#define SEC
 

Value:

SETFLAG(FC);    \
        PCSTEP(1)
SEC: 1 -> [C]

Definition at line 630 of file cpu.c.

#define SED
 

Value:

SETFLAG(FD);    \
        PCSTEP(1)
SED: 1 -> [D]

Definition at line 635 of file cpu.c.

#define SEI
 

Value:

SETFLAG(FI);    \
        PCSTEP(1)
SEI: 1 -> [I]

Definition at line 640 of file cpu.c.

#define SETFLAG      regs.P=((regs.P) | (X))
 

Sets flag X to 1. Affects P register

Definition at line 49 of file cpu.c.

#define SETNZFLAGS      FLAG((X) & 0x80, FN); FLAG(!(X), FZ)
 

Sets N and Z flag according to X

Definition at line 58 of file cpu.c.

#define STA MEMREAD,
STEP   
 

Value:

(*memsetb)(MEMREAD, regs.A);    \
        PCSTEP(STEP)
STA: [A] -> [M]

Definition at line 645 of file cpu.c.

#define STACKADD      (*memsetb)(regs.S, (X)); regs.S--; if (regs.S<0x100) regs.S=0x1FF
 

Adds byte X to/from the top of stack

Definition at line 91 of file cpu.c.

#define STACKREMOVE      regs.S++; if (regs.S>0x1FF) regs.S=0x100; X=(*memgetb)(regs.S)
 

Removes byte X to/from the top of stack

Definition at line 94 of file cpu.c.

#define STX MEMREAD,
STEP   
 

Value:

(*memsetb)(MEMREAD, regs.X);    \
        PCSTEP(STEP)
STX: [X] -> [M]

Definition at line 650 of file cpu.c.

#define STY MEMREAD,
STEP   
 

Value:

(*memsetb)(MEMREAD, regs.Y);    \
        PCSTEP(STEP)
STY: [Y] -> [M]

Definition at line 655 of file cpu.c.

#define TAX
 

Value:

TAX: [A] -> [X], sets NZ

Definition at line 660 of file cpu.c.

#define TAY
 

Value:

TAY: [A] -> [Y], sets NZ

Definition at line 672 of file cpu.c.

#define TSX
 

Value:

TSX: [S] -> [X], sets NZ

Definition at line 684 of file cpu.c.

#define TXA
 

Value:

TXA: [X] -> [A], sets NZ

Definition at line 666 of file cpu.c.

#define TXS
 

Value:

TXS: [X] -> [S]

Definition at line 690 of file cpu.c.

#define TYA
 

Value:

TYA: [Y] -> [A], sets NZ

Definition at line 678 of file cpu.c.

#define ZP   (*memgetb)(regs.PC+1)&0xFF
 

Zero page adressing mode

Definition at line 131 of file cpu.c.

#define ZPX   ((*memgetb)(regs.PC+1)+regs.X)&0xFF
 

Zero page, X adressing mode

Definition at line 134 of file cpu.c.

#define ZPY   ((*memgetb)(regs.PC+1)+regs.Y)&0xFF
 

Zero page, Y adressing mode

Definition at line 137 of file cpu.c.


Function Documentation

UINT InstrStep  
 

Executes one instruction on adress stored in regs.PC.

Executes one instruction on adress stored in regs.PC; increments (or changes in case of jump) program counter

Returns :
Number of cycles consumed
Todo:
Add undocumented instructions

Definition at line 706 of file cpu.c.

void Irq  
 

IRQ interrupt.

Adds actual position and the P register (flags) to the top of the stack, sets the I (interrupt) flag to 1 and jumps to the IRQVKT, reset initialization routine.

Definition at line 1379 of file cpu.c.

void Nmi  
 

NMI interrupt.

Adds actual position and the P register (flags) to the top of the stack, sets the I (interrupt) flag to 1 and jumps to the NMIVKT, reset initialization routine.

Definition at line 1398 of file cpu.c.

void Reset  
 

Reset interrupt.

Zeroes A, X, Y registers, resets stack pointer S, initializes P register (zeroes all flags except I, which is set to 1) and jumps to the RESETVKT, reset initialization routine.

Definition at line 1362 of file cpu.c.


Variable Documentation

Regs regs
 

Variable containing all registers

Definition at line 696 of file cpu.c.


Generated on Fri Sep 6 18:32:15 2002 for Emu6502 by doxygen1.2.17