This is for 6502, how does this differ from the NES?
Code: Select all
-primary registers-
=register name ; desc=
A ; Accumulator
X ; Variable X
Y ; Variable Y
-flag registers-
=bit ; flag name ; desc=
7 ; N ; negative result
6 ; V ; result overflowed
5 ; - ; (unused)
4 ; B ; BRK instruction used
3 ; D ; decimal mode
2 ; I ; interrupt disabled
1 ; Z ; result zero
0 ; C ; carry occured
-scopes-
=scope ; references list=
Immediate ;
Zero Page ; Current,X,Y
Absolute ; Current,X,Y
Indirect ; Current,X,Y
Accumulator ;
Relative ;
Implied ;
-op codes-
=hex, numeric, bytes used ; syntax ; scope=
69 105 002 ; ADC #char ; Immediate
65 101 002 ; ADC char ; Zero Page
75 117 002 ; ADC char,X ; Zero Page,X
6D 109 003 ; ADC short ; Absolute
7D 125 003 ; ADC short,X ; Absolute,X
79 121 003 ; ADC short,Y ; Absolute,Y
61 097 002 ; ADC (char,X) ; Indirect,X
71 113 002 ; ADC (char),Y ; Indirect,Y
29 041 002 ; AND #char ; Immediate
25 037 002 ; AND char ; Zero Page
35 053 002 ; AND char/X ; Zero Page,X
2D 045 003 ; AND short ; Absolute
3D 061 003 ; AND short ; Absolute,X
39 057 003 ; AND short ; Absolute,Y
21 033 002 ; AND (char,X) ; Indirect,X
31 049 002 ; AND (char),Y ; Indirect,Y
0A 010 001 ; ASL ; Accumulator
06 006 002 ; ASL char ; Zero Page
16 022 002 ; ASL char,X ; Zero Page,X
0E 014 003 ; ASL short ; Absolute
1E 030 003 ; ASL short,X ; Absolute,X
90 144 002 ; BCC addr ; Relative
B0 176 002 ; BCS addr ; Relative
F0 240 002 ; BEQ addr ; Relative
24 036 002 ; BIT char ; Zero Page
2C 044 003 ; BIT short ; Absolute
30 048 002 ; BMI addr ; Relative
D0 208 002 ; BNE addr ; Relative
10 016 002 ; BPL addr ; Relative
00 000 001 ; BRK ; Implied
50 080 002 ; BVC addr ; Relative
70 112 002 ; BVS addr ; Relative
18 024 001 ; CLC ; Implied
D8 216 001 ; CLD ; Implied
58 088 001 ; CLI ; Implied
B8 184 001 ; CLV ; Implied
C9 201 002 ; CMP #char ; Immediate
C5 197 002 ; CMP char ; Zero Page
D5 213 002 ; CMP char,X ; Zero Page,X
CD 205 003 ; CMP short ; Absolute
DD 221 003 ; CMP short,X ; Absolute,X
D9 217 003 ; CMP short,Y ; Absolute,Y
C1 193 002 ; CMP (char,X) ; Indirect,X
D1 209 002 ; CMP (char),Y ; Indirect,Y
E0 224 002 ; CPX #char ; Immediate
E4 228 002 ; CPX char ; Zero Page
EC 236 003 ; CPX short ; Absolute
C0 192 002 ; CPY #char ; Immediate
C4 196 002 ; CPY char ; Zero Page
CC 204 003 ; CPY short ; Absolute
C6 198 002 ; DEC char ; Zero Page
D6 214 002 ; DEC char,X ; Zero Page,X
CE 206 003 ; DEC short ; Absolute
DE 222 003 ; DEC short,X ; Absolute,X
CA 202 001 ; DEX ; Implied
88 136 001 ; DEY ; Implied
49 073 002 ; EOR #char ; Immediate
45 069 002 ; EOR char ; Zero Page
55 085 002 ; EOR char,X ; Zero Page,X
4D 077 003 ; EOR short ; Absolute
5D 093 003 ; EOR short,X ; Absolute,X
59 089 003 ; EOR short,Y ; Absolute,Y
41 065 002 ; EOR (char,X) ; Indirect,X
51 081 002 ; EOR (char),Y ; Indirect,Y
E6 230 002 ; INC char ; Zero Page
F6 246 002 ; INC char,X ; Zero Page,X
EE 238 003 ; INC short ; Absolute
FE 254 003 ; INC short,X ; Absolute,X
E8 232 001 ; INX ; Implied
C8 200 001 ; INY ; Implied
4C 076 003 ; JMP short ; Absolute
6C 108 003 ; JMP (short) ; Indirect
20 032 003 ; JSR short ; Absolute
A9 169 002 ; LDA #char ; Immediate
A5 165 002 ; LDA char ; Zero Page
B5 181 002 ; LDA char,X ; Zero Page,X
AD 173 003 ; LDA short ; Absolute
BD 189 003 ; LDA short,X ; Absolute,X
B9 185 003 ; LDA short,Y ; Absolute,Y
A1 161 002 ; LDA (char,X) ; Indirect,X
B1 177 002 ; LDA (char),Y ; Indirect,Y
A2 162 002 ; LDX #char ; Immediate
A6 166 002 ; LDX char ; Zero Page
B6 182 002 ; LDX char,Y ; Zero Page,Y
AE 174 003 ; LDX short ; Absolute
BE 190 003 ; LDX short,Y ; Absolute,Y
A0 160 002 ; LDY #char ; Immediate
A4 164 002 ; LDY char ; Zero Page
B4 180 002 ; LDY char,X ; Zero Page,X
AC 172 003 ; LDY short ; Absolute
BC 188 003 ; LDY short,X ; Absolute,X
4A 074 002 ; LSR ; Accumulator
46 070 002 ; LSR char ; Zero Page
56 086 002 ; LSR char,X ; Zero Page,X
4E 078 003 ; LSR short ; Absolute
5E 094 003 ; LSR short,X ; Absolute,X
EA 234 001 ; NOP ; Implied
09 009 002 ; ORA #char ; Immediate
05 005 002 ; ORA char ; Zero Page
15 021 002 ; ORA char,X ; Zero Page,X
0D 013 003 ; ORA short ; Absolute
1D 029 003 ; ORA short,X ; Absolute,X
19 025 003 ; ORA short,Y ; Absolute,Y
01 001 002 ; ORA (char,X) ; Indirect,X
11 017 002 ; ORA (char),Y ; Indirect,Y
48 072 001 ; PHA ; Implied
08 008 001 ; PHP ; Implied
68 104 001 ; PLA ; Implied
28 040 001 ; PLP ; Implied
2A 042 001 ; ROL ; Accumulator
26 038 002 ; ROL char ; Zero Page
36 054 002 ; ROL char,X ; Zero Page,X
2E 046 003 ; ROL short ; Absolute
3E 062 003 ; ROL short,X ; Absolute,X
6A 106 001 ; ROR ; Accumulator
66 102 002 ; ROR char ; Zero Page
76 118 002 ; ROR char,X ; Zero Page,X
6E 110 003 ; ROR short ; Absolute
7E 126 003 ; ROR short,X ; Absolute,X
40 064 001 ; RTI ; Implied
60 096 001 ; RTS ; Implied
E9 233 002 ; SBC #char ; Immediate
E5 229 002 ; SBC char ; Zero Page
F5 245 002 ; SBC char,X ; Zero Page,X
ED 237 003 ; SBC short ; Absolute
FD 249 003 ; SBC short,X ; Absolute,X
F9 253 003 ; SBC short,Y ; Absolute,Y
E1 249 002 ; SBC (char,X) ; Indirect,Y
F1 241 002 ; SBC (char),Y ; Indirect,Y
38 056 001 ; SEC ; Implied
F8 248 001 ; SED ; Implied
78 120 001 ; SEI ; Implied
85 133 002 ; STA char ; Zero Page
95 149 002 ; STA char,X ; Zero Page,X
8D 141 003 ; STA short ; Absolute
9D 157 003 ; STA short,X ; Absolute,X
99 153 003 ; STA short,Y ; Absolute,Y
81 129 002 ; STA (15,X) ; Indirect,X
91 145 002 ; STA (15),Y ; Indirect,Y
86 134 002 ; STX char ; Zero Page
96 150 002 ; STX char,Y ; Zero Page,Y
8E 142 003 ; STX short ; Absolute
84 132 002 ; STY char ; Zero Page
94 148 002 ; STY char,X ; Zero Page,X
8C 140 003 ; STY short ; Absolute
AA 170 001 ; TAX ; Implied
A8 168 001 ; TAY ; Implied
BA 186 001 ; TSX ; Implied
8A 138 001 ; TXA ; Implied
9A 154 001 ; TXS ; Implied
98 152 001 ; TYA ; Implied
Code: Select all
<?php
function op(){
/*include state*/
global $ops,$reg,$mem;
/*execute applicable operator which is associated with the bytecode at current location in memory*/
eval('op_'.$ops[ord(substr($mem,$reg[o],1))].'();');
}
function op_adc(){
/*include state*/
global $ops,$reg,$mem;
/*load registers*/
$r=reg_ld();
/*if N is set (negative values enabled flag)*/
if($r[n]&1)
{
/*adjust Accumulator to signed via bit 7 as -0 thru -127*/
$r[a]=(-($r[a]>7)&1)*($r[a]&0x7f);
/*adjust byte at current memory address in same fashion*/
$r[this]=(-($r[this]>7)&1)*($r[this]&0x7f);
}
/*add A (Accumulator) to "This" (byte in memory) to C (carry flag)
$r[a]=$r[a]+$r[this]+$r[c];
/*if result is negative*/
if($r[a]<0)
{
/*set N (enable negative values flag)*/
$r[n]=1;
/*temporarily set A (Accumulator) to invalid range*/
$r[a]=abs($r[a]);
/*** this is where the book lost me ***/
}
/*if result is zero, set Z (result of Accumulator was zero flag), otherwise unset it*/
$r[z]=($r[a]===0)&1;
/*if overflowed into bit 7 now but not before, toggle V (overflow flag), otherwise unset it*/
$r[v]=(($r[a]>>7)&1)&($r[v]===0);
/*set C (Carry Flag) if Accumulator greater than 255, otherwise unset it*/
$r[c]=(($r[a]>>8)!==0)&1;
/*truncate bits in A (Accumulator register) to 1 byte (8bits)*/
$r[a]=$r[a]&0xff;
/*commit*/
$r=reg_sv($r);
}
function reg_ld(){
/*include state*/
global $ops,$reg,$mem;
/*intialize flags and registeres into usable form*/
$r[o]=$reg[o]&0xffff;
$r[a]=$reg[a]&0xff;
$r[x]=$reg[x]&0xff;
$r[y]=$reg[y]&0xff;
$r[n]=($reg[f]>>7)&1;
$r[v]=($reg[f]>>6)&1;
$r[b]=($reg[f]>>4)&1;
$r[d]=($reg[f]>>3)&1;
$r[i]=($reg[f]>>2)&1;
$r[z]=($reg[f]>>1)&1;
$r[c]=($reg[f]>>0)&1;
/*set "This" as value of byte in memory*/
$r[this]=ord(substr($mem,$r[o]+1,1));
/*send off registers*/
return $r;
}
function reg_sv($r){
/*include state*/
global $ops,$reg,$mem;
/*integrate and commit all registers*/
$reg[o]=$r[o]&0xffff;
$reg[a]=$r[a]&0xff;
$reg[x]=$r[x]&0xff;
$reg[y]=$r[y]&0xff;
$reg[f]=$r[n]&1;
$reg[f]=($reg[f]<<1)|($r[v]&1);
$reg[f]=($reg[f]<<2)|($r[b]&1);
$reg[f]=($reg[f]<<1)|($r[d]&1);
$reg[f]=($reg[f]<<1)|($r[i]&1);
$reg[f]=($reg[f]<<1)|($r[z]&1);
$reg[f]=($reg[f]<<1)|($r[c]&1);
}
?>