define mapper read (location)
set M to 255
define mapper write (location) (value)
define immediate
mapper read PC+1
define zeropage
mapper read PC+1
mapper read M
define zeropageX
mapper read PC+1
mapper read M + X mod 256
define zeropageY
mapper read PC+1
mapper read M + Y mod 256
define absolute
mapper read PC+1
set tmp to M
mapper read PC+2
mapper read tmp * 256 + M
define absoluteX
mapper read PC+1
set tmp to M
mapper read PC+2
mapper read tmp * 256 + M + X
define absoluteY
mapper read PC+1
set tmp to M
mapper read PC+2
mapper read tmp * 256 + M + Y
define implied
define indirect
mapper read PC+1
set tmp to M
mapper read PC+2
mapper read tmp * 256 + M
set tmp2 to M
mapper read tmp * 256 + M + 1
mapper read tmp2 * 256 + M
define indirectX
mapper read PC+1
mapper read (M + X) mod 256
define indirectY
mapper read PC+1
mapper read M
mapper read M + Y + flagC
define flagZ
set flagZ to
define flagN
set flagN to 7 >
define two2native
set tmp to A - 256 + (256 * )
define push (value)
mapper write 256 + S value
set S to (S - 1) mod 256
define pull
mapper read (257 + S)
set S to (S + 1) mod 256
define interrupt
define ADC
R,N,Z
set tmp to (join "0x" (A)) + (join "0x" (OP)) + flagC
set A to item (tmp mod 256) of hex
set flagC to 255>
set flagV to <<(join "0x" (A)) < 128 and (join "0x" (OP)) < 128 and (tmp mod 256) > 128> or <(join "0x" (A)) > 127 and (join "0x" (OP)) > 127 and (tmp mod 256) < 128>>
set flagV to 127 or tmp < -128>
define SBC
R,N,Z,A
set tmp to (join "0x" (A)) - (join "0x" (OP)) - flagC
set A to item (tmp mod 256) of hex
set flagC to 255>
set flagV to <<(join "0x" (A)) < 128 and (join "0x" (OP)) < 128 and (tmp mod 256) > 128> or <(join "0x" (A)) > 127 and (join "0x" (OP)) > 127 and (tmp mod 256) < 128>>
define AND
R,N,Z,A
set A to join (item (join (letter 1 of A) (letter 1 of OP)) of AND) (item (join (letter 2 of A) (letter 2 of OP)) of AND)
define ASL
RW,N,Z,OP
set flagC to 7 >
set OP to (item ( (join "0x" (OP)) * 2) mod 256 of hex)
define BCS
BRANCH
mapper read PC+1
change PC by flagC * (M + 256 * ( - 1))
define BRK
IMPLIED
set flagI to 1
mapper write (256 + S) (PC + 2)
set S to (S - 1) mod 256
compute SR
mapper write (256 + S) SR
set S to (S - 1) mod 256
interrupt
define CLC
IMPLIED
set flagC to 0
define CMP
R, tmp, N, Z, C
set tmp to (join "0x" (A)) - (join "0x" (OP))
define CPX
R, tmp, N, Z, C
set tmp to X - OP
define CPY
R, tmp, N, Z, C
set tmp to Y - OP
define INC
RW, N, Z
set OP to OP + 1
define INX
IMPLIED
change X by 1
define JMP
RAW
set PC to OP
define JSR
RAW
set tmp to (join "0x" (PC)) + 2
mapper write (256 + S) ( (tmp - (tmp mod 256)) / 256)
set S to (S - 1) mod 256
mapper write (256 + S) (tmp mod 256)
set S to (S - 1) mod 256
set PC to OP
define LDA
R, N, Z, A
set A to OP
define LSR
RW, Z
set flagC to OP mod 2
set OP to floor of (join "0x" (OP)) / 2
define PHA
mapper write 256 + S A
set S to (S - 1) mod 256
define PLA
IMPLIED, N, Z, A
mapper read (257 + S)
set S to (S + 1) mod 256
set A to M
define PLP
IMPLIED
mapper read (257 + S)
set S to (S + 1) mod 256
set flagN to < (item (join (letter 1 of S) "8") of AND) = 8>
set flagV to < (item (join (letter 1 of S) "4") of AND) = 4>
set flagB to < (item (join (letter 1 of S) "1") of AND) = 1>
set flagD to < (item (join (letter 2 of S) "8") of AND) = 8>
set flagI to < (item (join (letter 2 of S) "4") of AND) = 4>
set flagZ to < (item (join (letter 2 of S) "2") of AND) = 2>
set flagC to < (item (join (letter 2 of S) "1") of AND) = 1>
define ROL
RW, N, Z
set tmp to <(join "0x" (letter 1 of OP)) > 7>
set OP to OP * 2 + flagC
set flagC to tmp
define ROR
RW, N, Z
set tmp to <(join "0x" (OP)) mod 2 = 1>
set OP to (floor of OP / 2) + (128 * flagC)
set flagC to tmp
def RTS
IMPLIED
mapper read (257 + S)
set S to (S + 1) mod 256
set tmp to M
mapper read (257 + S)
set S to (S + 1) mod 256
set PC to join tmp M
def STA
W
set OP to A