#! an 8-bit value.
3drop ;
-CONSTANT: carry-flag HEX: 01
-CONSTANT: parity-flag HEX: 04
-CONSTANT: half-carry-flag HEX: 10
-CONSTANT: interrupt-flag HEX: 20
-CONSTANT: zero-flag HEX: 40
-CONSTANT: sign-flag HEX: 80
+CONSTANT: carry-flag 0x01
+CONSTANT: parity-flag 0x04
+CONSTANT: half-carry-flag 0x10
+CONSTANT: interrupt-flag 0x20
+CONSTANT: zero-flag 0x40
+CONSTANT: sign-flag 0x80
: >word< ( word -- byte byte )
#! Explode a word into its two 8 bit values.
- dup HEX: FF bitand swap -8 shift HEX: FF bitand swap ;
+ dup 0xFF bitand swap -8 shift 0xFF bitand swap ;
: af>> ( cpu -- word )
#! Return the 16-bit pseudo register AF.
[ a>> 8 shift ] keep f>> bitor ;
-: (>>af) ( value cpu -- )
+: af<< ( value cpu -- )
#! Set the value of the 16-bit pseudo register AF
[ >word< ] dip swap >>f swap >>a drop ;
#! Return the 16-bit pseudo register BC.
[ b>> 8 shift ] keep c>> bitor ;
-: (>>bc) ( value cpu -- )
+: bc<< ( value cpu -- )
#! Set the value of the 16-bit pseudo register BC
[ >word< ] dip swap >>c swap >>b drop ;
#! Return the 16-bit pseudo register DE.
[ d>> 8 shift ] keep e>> bitor ;
-: (>>de) ( value cpu -- )
+: de<< ( value cpu -- )
#! Set the value of the 16-bit pseudo register DE
[ >word< ] dip swap >>e swap >>d drop ;
#! Return the 16-bit pseudo register HL.
[ h>> 8 shift ] keep l>> bitor ;
-: (>>hl) ( value cpu -- )
+: hl<< ( value cpu -- )
#! Set the value of the 16-bit pseudo register HL
[ >word< ] dip swap >>l swap >>h drop ;
#! Read one byte from memory at the specified address.
#! The address is 16-bit, but if a value greater than
#! 0xFFFF is provided then return a default value.
- over HEX: FFFF <= [
+ over 0xFFFF <= [
ram>> nth
] [
- 2drop HEX: FF
+ 2drop 0xFF
] if ;
: read-word ( addr cpu -- word )
[ pc>> ] keep
[ read-byte ] keep
[ pc>> 1 + ] keep
- (>>pc) ;
+ pc<< ;
: next-word ( cpu -- word )
#! Return the value of the word at PC, and increment PC.
[ pc>> ] keep
[ read-word ] keep
[ pc>> 2 + ] keep
- (>>pc) ;
+ pc<< ;
: write-byte ( value addr cpu -- )
#! Write a byte to the specified memory address.
- over dup HEX: 2000 < swap HEX: FFFF > or [
+ over dup 0x2000 < swap 0xFFFF > or [
3drop
] [
3dup ram>> set-nth
: cpu-a-bitand ( quot cpu -- )
#! A &= quot call
- [ a>> swap call bitand ] keep (>>a) ; inline
+ [ a>> swap call bitand ] keep a<< ; inline
: cpu-a-bitor ( quot cpu -- )
#! A |= quot call
- [ a>> swap call bitor ] keep (>>a) ; inline
+ [ a>> swap call bitor ] keep a<< ; inline
: cpu-a-bitxor ( quot cpu -- )
#! A ^= quot call
- [ a>> swap call bitxor ] keep (>>a) ; inline
+ [ a>> swap call bitxor ] keep a<< ; inline
: cpu-a-bitxor= ( value cpu -- )
#! cpu-a ^= value
- [ a>> bitxor ] keep (>>a) ;
+ [ a>> bitxor ] keep a<< ;
: cpu-f-bitand ( quot cpu -- )
#! F &= quot call
- [ f>> swap call bitand ] keep (>>f) ; inline
+ [ f>> swap call bitand ] keep f<< ; inline
: cpu-f-bitor ( quot cpu -- )
#! F |= quot call
- [ f>> swap call bitor ] keep (>>f) ; inline
+ [ f>> swap call bitor ] keep f<< ; inline
: cpu-f-bitxor ( quot cpu -- )
#! F |= quot call
- [ f>> swap call bitxor ] keep (>>f) ; inline
+ [ f>> swap call bitxor ] keep f<< ; inline
: cpu-f-bitor= ( value cpu -- )
#! cpu-f |= value
- [ f>> bitor ] keep (>>f) ;
+ [ f>> bitor ] keep f<< ;
: cpu-f-bitand= ( value cpu -- )
#! cpu-f &= value
- [ f>> bitand ] keep (>>f) ;
+ [ f>> bitand ] keep f<< ;
: cpu-f-bitxor= ( value cpu -- )
#! cpu-f ^= value
- [ f>> bitxor ] keep (>>f) ;
+ [ f>> bitxor ] keep f<< ;
: set-flag ( cpu flag -- )
swap cpu-f-bitor= ;
: clear-flag ( cpu flag -- )
- bitnot HEX: FF bitand swap cpu-f-bitand= ;
+ bitnot 0xFF bitand swap cpu-f-bitand= ;
: update-zero-flag ( result cpu -- )
#! If the result of an instruction has the value 0, this
#! flag is set, otherwise it is reset.
- swap HEX: FF bitand 0 = [ zero-flag set-flag ] [ zero-flag clear-flag ] if ;
+ swap 0xFF bitand 0 = [ zero-flag set-flag ] [ zero-flag clear-flag ] if ;
: update-sign-flag ( result cpu -- )
#! If the most significant bit of the result
#! has the value 1 then the flag is set, otherwise
#! it is reset.
- swap HEX: 80 bitand 0 = [ sign-flag clear-flag ] [ sign-flag set-flag ] if ;
+ swap 0x80 bitand 0 = [ sign-flag clear-flag ] [ sign-flag set-flag ] if ;
: update-parity-flag ( result cpu -- )
#! If the modulo 2 sum of the bits of the result
#! is 0, (ie. if the result has even parity) this flag
#! is set, otherwise it is reset.
- swap HEX: FF bitand 2 mod 0 = [ parity-flag set-flag ] [ parity-flag clear-flag ] if ;
+ swap 0xFF bitand 2 mod 0 = [ parity-flag set-flag ] [ parity-flag clear-flag ] if ;
: update-carry-flag ( result cpu -- )
#! If the instruction resulted in a carry (from addition)
#! or a borrow (from subtraction or a comparison) out of the
#! higher order bit, this flag is set, otherwise it is reset.
- swap dup HEX: 100 >= swap 0 < or [ carry-flag set-flag ] [ carry-flag clear-flag ] if ;
+ swap dup 0x100 >= swap 0 < or [ carry-flag set-flag ] [ carry-flag clear-flag ] if ;
: update-half-carry-flag ( original change-by result cpu -- )
#! If the instruction caused a carry out of bit 3 and into bit 4 of the
#! The 'original' is the original value of the register being changed.
#! 'change-by' is the amount it is being added or decremented by.
#! 'result' is the result of that change.
- [ bitxor bitxor HEX: 10 bitand 0 = not ] dip
+ [ bitxor bitxor 0x10 bitand 0 = not ] dip
swap [ half-carry-flag set-flag ] [ half-carry-flag clear-flag ] if ;
: update-flags ( result cpu -- )
[ 2dup + ] dip
[ update-flags ] 2keep
[ update-half-carry-flag ] 2keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: add-carry ( change-by result cpu -- change-by result )
#! Add the effect of the carry flag to the result
[ add-carry ] keep
[ update-flags ] 2keep
[ update-half-carry-flag ] 2keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: sub-carry ( change-by result cpu -- change-by result )
#! Subtract the effect of the carry flag from the result
[ 2dup - ] dip
[ update-flags ] 2keep
[ update-half-carry-flag ] 2keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: sub-byte-with-carry ( lhs rhs cpu -- result )
#! Subtract rhs from lhs and take carry into account
[ sub-carry ] keep
[ update-flags ] 2keep
[ update-half-carry-flag ] 2keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: inc-byte ( byte cpu -- result )
#! Increment byte by one. Note that carry flag is not affected
[ 1 2dup + ] dip
[ update-flags-no-carry ] 2keep
[ update-half-carry-flag ] 2keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: dec-byte ( byte cpu -- result )
#! Decrement byte by one. Note that carry flag is not affected
[ 1 2dup - ] dip
[ update-flags-no-carry ] 2keep
[ update-half-carry-flag ] 2keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: inc-word ( w cpu -- w )
#! Increment word by one. Note that no flags are modified.
- drop 1 + HEX: FFFF bitand ;
+ drop 1 + 0xFFFF bitand ;
: dec-word ( w cpu -- w )
#! Decrement word by one. Note that no flags are modified.
- drop 1 - HEX: FFFF bitand ;
+ drop 1 - 0xFFFF bitand ;
: add-word ( lhs rhs cpu -- result )
#! Add rhs to lhs. Note that only the carry flag is modified
#! and only if there is a carry out of the double precision add.
- [ + ] dip over HEX: FFFF > [ carry-flag set-flag ] [ drop ] if HEX: FFFF bitand ;
+ [ + ] dip over 0xFFFF > [ carry-flag set-flag ] [ drop ] if 0xFFFF bitand ;
: bit3or ( lhs rhs -- 0|1 )
#! bitor bit 3 of the two numbers on the stack
- BIN: 00001000 bitand -3 shift [
- BIN: 00001000 bitand -3 shift
+ 0b00001000 bitand -3 shift [
+ 0b00001000 bitand -3 shift
] dip
bitor ;
[ bitand ] dip [ update-flags ] 2keep
[ carry-flag clear-flag ] keep
rot 0 = [ half-carry-flag set-flag ] [ half-carry-flag clear-flag ] if
- HEX: FF bitand ;
+ 0xFF bitand ;
: xor-byte ( lhs rhs cpu -- result )
#! Logically xor rhs to lhs. The carry and half-carry flags are cleared.
[ bitxor ] dip [ update-flags ] 2keep
[ half-carry-flag carry-flag bitor clear-flag ] keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: or-byte ( lhs rhs cpu -- result )
#! Logically or rhs to lhs. The carry and half-carry flags are cleared.
[ bitor ] dip [ update-flags ] 2keep
[ half-carry-flag carry-flag bitor clear-flag ] keep
- drop HEX: FF bitand ;
+ drop 0xFF bitand ;
: decrement-sp ( n cpu -- )
#! Decrement the stackpointer by n.
[ sp>> ] keep
- [ swap - ] dip (>>sp) ;
+ [ swap - ] dip sp<< ;
: save-pc ( cpu -- )
#! Save the value of the PC on the stack.
: call-sub ( addr cpu -- )
#! Call the address as a subroutine.
dup push-pc
- [ HEX: FFFF bitand ] dip (>>pc) ;
+ [ 0xFFFF bitand ] dip pc<< ;
: ret-from-sub ( cpu -- )
- [ pop-pc ] keep (>>pc) ;
+ [ pop-pc ] keep pc<< ;
: interrupt ( number cpu -- )
#! Perform a hardware interrupt
-! "***Interrupt: " write over 16 >base print
+! "***Interrupt: " write over >hex print
dup f>> interrupt-flag bitand 0 = not [
dup push-pc
- (>>pc)
+ pc<<
] [
2drop
] if ;
: inc-cycles ( n cpu -- )
#! Increment the number of cpu cycles
- [ cycles>> + ] keep (>>cycles) ;
+ [ cycles>> + ] keep cycles<< ;
: instruction-cycles ( -- vector )
#! Return a 256 element vector containing the cycles for
0 >>a
0 >>f
0 >>pc
- HEX: F000 >>sp
- HEX: FFFF 0 <array> >>ram
+ 0xF000 >>sp
+ 0xFFFF 0 <array> >>ram
f >>halted?
- HEX: 10 >>last-interrupt
+ 0x10 >>last-interrupt
0 >>cycles
drop ;
#! Read the next instruction from the cpu's program
#! counter, and increment the program counter.
[ pc>> ] keep ! pc cpu
- [ over 1 + swap (>>pc) ] keep
+ [ over 1 + swap pc<< ] keep
read-byte ;
: get-cycles ( n -- opcode )
over 16667 < [
2drop
] [
- [ [ 16667 - ] dip (>>cycles) ] keep
- dup last-interrupt>> HEX: 10 = [
- HEX: 08 over (>>last-interrupt) HEX: 08 swap interrupt
+ [ [ 16667 - ] dip cycles<< ] keep
+ dup last-interrupt>> 0x10 = [
+ 0x08 over last-interrupt<< 0x08 swap interrupt
] [
- HEX: 10 over (>>last-interrupt) HEX: 10 swap interrupt
+ 0x10 over last-interrupt<< 0x10 swap interrupt
] if
] if ;
[ pc>> ] keep read-byte instructions nth first ;
: cpu. ( cpu -- )
- [ " PC: " write pc>> 16 >base 4 CHAR: \s pad-head write ] keep
- [ " B: " write b>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " C: " write c>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " D: " write d>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " E: " write e>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " F: " write f>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " H: " write h>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " L: " write l>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " A: " write a>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " SP: " write sp>> 16 >base 4 CHAR: \s pad-head write ] keep
- [ " cycles: " write cycles>> number>string 5 CHAR: \s pad-head write ] keep
+ [ " PC: " write pc>> >hex 4 CHAR: \s pad-head write ] keep
+ [ " B: " write b>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " C: " write c>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " D: " write d>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " E: " write e>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " F: " write f>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " H: " write h>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " L: " write l>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " A: " write a>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " SP: " write sp>> >hex 4 CHAR: \s pad-head write ] keep
+ [ " cycles: " write cycles>> number>string 5 CHAR: \s pad-head write ] keep
[ " " write peek-instruction name>> write " " write ] keep
nl drop ;
: cpu*. ( cpu -- )
- [ " PC: " write pc>> 16 >base 4 CHAR: \s pad-head write ] keep
- [ " B: " write b>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " C: " write c>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " D: " write d>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " E: " write e>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " F: " write f>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " H: " write h>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " L: " write l>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " A: " write a>> 16 >base 2 CHAR: \s pad-head write ] keep
- [ " SP: " write sp>> 16 >base 4 CHAR: \s pad-head write ] keep
- [ " cycles: " write cycles>> number>string 5 CHAR: \s pad-head write ] keep
+ [ " PC: " write pc>> >hex 4 CHAR: \s pad-head write ] keep
+ [ " B: " write b>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " C: " write c>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " D: " write d>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " E: " write e>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " F: " write f>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " H: " write h>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " L: " write l>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " A: " write a>> >hex 2 CHAR: \s pad-head write ] keep
+ [ " SP: " write sp>> >hex 4 CHAR: \s pad-head write ] keep
+ [ " cycles: " write cycles>> number>string 5 CHAR: \s pad-head write ] keep
nl drop ;
: register-lookup ( string -- vector )
#! where the 1st item is the getter and the 2nd is the setter
#! for that register.
H{
- { "A" { a>> (>>a) } }
- { "B" { b>> (>>b) } }
- { "C" { c>> (>>c) } }
- { "D" { d>> (>>d) } }
- { "E" { e>> (>>e) } }
- { "H" { h>> (>>h) } }
- { "L" { l>> (>>l) } }
- { "AF" { af>> (>>af) } }
- { "BC" { bc>> (>>bc) } }
- { "DE" { de>> (>>de) } }
- { "HL" { hl>> (>>hl) } }
- { "SP" { sp>> (>>sp) } }
+ { "A" { a>> a<< } }
+ { "B" { b>> b<< } }
+ { "C" { c>> c<< } }
+ { "D" { d>> d<< } }
+ { "E" { e>> e<< } }
+ { "H" { h>> h<< } }
+ { "L" { l>> l<< } }
+ { "AF" { af>> af<< } }
+ { "BC" { bc>> bc<< } }
+ { "DE" { de>> de<< } }
+ { "HL" { hl>> hl<< } }
+ { "SP" { sp>> sp<< } }
} at ;
#! Given a string containing a flag name, return a vector
#! where the 1st item is a word that tests that flag.
H{
- { "NZ" { flag-nz? } }
- { "NC" { flag-nc? } }
- { "PO" { flag-po? } }
- { "PE" { flag-pe? } }
+ { "NZ" { flag-nz? } }
+ { "NC" { flag-nc? } }
+ { "PO" { flag-po? } }
+ { "PE" { flag-pe? } }
{ "Z" { flag-z? } }
{ "C" { flag-c? } }
{ "P" { flag-p? } }
- { "M" { flag-m? } }
+ { "M" { flag-m? } }
} at ;
SYMBOLS: $1 $2 $3 $4 ;
: (emulate-RST) ( n cpu -- )
#! RST nn
[ sp>> 2 - dup ] keep ! sp sp cpu
- [ (>>sp) ] keep ! sp cpu
+ [ sp<< ] keep ! sp cpu
[ pc>> ] keep ! sp pc cpu
swapd [ write-word ] keep ! cpu
- [ 8 * ] dip (>>pc) ;
+ [ 8 * ] dip pc<< ;
: (emulate-CALL) ( cpu -- )
#! 205 - CALL nn
- [ next-word HEX: FFFF bitand ] keep ! addr cpu
+ [ next-word 0xFFFF bitand ] keep ! addr cpu
[ sp>> 2 - dup ] keep ! addr sp sp cpu
- [ (>>sp) ] keep ! addr sp cpu
+ [ sp<< ] keep ! addr sp cpu
[ pc>> ] keep ! addr sp pc cpu
swapd [ write-word ] keep ! addr cpu
- (>>pc) ;
+ pc<< ;
: (emulate-RLCA) ( cpu -- )
#! The content of the accumulator is rotated left
#! order bit position. Only the carry flag is affected.
[ a>> -7 shift ] keep
over 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
- [ a>> 1 shift HEX: FF bitand ] keep
- [ bitor ] dip (>>a) ;
+ [ a>> 1 shift 0xFF bitand ] keep
+ [ bitor ] dip a<< ;
: (emulate-RRCA) ( cpu -- )
#! The content of the accumulator is rotated right
[ a>> 1 bitand 7 shift ] keep
over 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
[ a>> 254 bitand -1 shift ] keep
- [ bitor ] dip (>>a) ;
+ [ bitor ] dip a<< ;
: (emulate-RLA) ( cpu -- )
#! The content of the accumulator is rotated left
[ carry-flag swap flag-set? [ 1 ] [ 0 ] if ] keep
[ a>> 127 bitand 7 shift ] keep
dup a>> 128 bitand 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
- [ bitor ] dip (>>a) ;
+ [ bitor ] dip a<< ;
: (emulate-RRA) ( cpu -- )
#! The content of the accumulator is rotated right
#! bit is set to the carry flag and the carry flag is
#! set to the value shifd out of the low order bit.
#! Only the carry flag is affected.
- [ carry-flag swap flag-set? [ BIN: 10000000 ] [ 0 ] if ] keep
+ [ carry-flag swap flag-set? [ 0b10000000 ] [ 0 ] if ] keep
[ a>> 254 bitand -1 shift ] keep
dup a>> 1 bitand 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
- [ bitor ] dip (>>a) ;
+ [ bitor ] dip a<< ;
: (emulate-CPL) ( cpu -- )
#! The contents of the accumulator are complemented
#! (zero bits become one, one bits becomes zero).
#! No flags are affected.
- HEX: FF swap cpu-a-bitxor= ;
+ 0xFF swap cpu-a-bitxor= ;
: (emulate-DAA) ( cpu -- )
#! The eight bit number in the accumulator is
#! digits.
[
dup half-carry-flag swap flag-set? swap
- a>> BIN: 1111 bitand 9 > or [ 6 ] [ 0 ] if
+ a>> 0b1111 bitand 9 > or [ 6 ] [ 0 ] if
] keep
[ a>> + ] keep
[ update-flags ] 2keep
- [ swap HEX: FF bitand swap (>>a) ] keep
+ [ swap 0xFF bitand swap a<< ] keep
[
dup carry-flag swap flag-set? swap
- a>> -4 shift BIN: 1111 bitand 9 > or [ 96 ] [ 0 ] if
+ a>> -4 shift 0b1111 bitand 9 > or [ 96 ] [ 0 ] if
] keep
[ a>> + ] keep
[ update-flags ] 2keep
- swap HEX: FF bitand swap (>>a) ;
+ swap 0xFF bitand swap a<< ;
: patterns ( -- hashtable )
#! table of code quotation patterns for each type of instruction.
H{
- { "NOP" [ drop ] }
- { "RET-NN" [ ret-from-sub ] }
- { "RST-0" [ 0 swap (emulate-RST) ] }
- { "RST-8" [ 8 swap (emulate-RST) ] }
- { "RST-10H" [ HEX: 10 swap (emulate-RST) ] }
- { "RST-18H" [ HEX: 18 swap (emulate-RST) ] }
- { "RST-20H" [ HEX: 20 swap (emulate-RST) ] }
- { "RST-28H" [ HEX: 28 swap (emulate-RST) ] }
- { "RST-30H" [ HEX: 30 swap (emulate-RST) ] }
- { "RST-38H" [ HEX: 38 swap (emulate-RST) ] }
- { "RET-F|FF" [ dup $1 [ 6 over inc-cycles ret-from-sub ] [ drop ] if ] }
- { "CP-N" [ [ a>> ] keep [ next-byte ] keep sub-byte drop ] }
- { "CP-R" [ [ a>> ] keep [ $1 ] keep sub-byte drop ] }
- { "CP-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep sub-byte drop ] }
- { "OR-N" [ [ a>> ] keep [ next-byte ] keep [ or-byte ] keep (>>a) ] }
- { "OR-R" [ [ a>> ] keep [ $1 ] keep [ or-byte ] keep (>>a) ] }
- { "OR-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ or-byte ] keep (>>a) ] }
- { "XOR-N" [ [ a>> ] keep [ next-byte ] keep [ xor-byte ] keep (>>a) ] }
- { "XOR-R" [ [ a>> ] keep [ $1 ] keep [ xor-byte ] keep (>>a) ] }
- { "XOR-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ xor-byte ] keep (>>a) ] }
- { "AND-N" [ [ a>> ] keep [ next-byte ] keep [ and-byte ] keep (>>a) ] }
- { "AND-R" [ [ a>> ] keep [ $1 ] keep [ and-byte ] keep (>>a) ] }
- { "AND-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ and-byte ] keep (>>a) ] }
- { "ADC-R,N" [ [ $1 ] keep [ next-byte ] keep [ add-byte-with-carry ] keep $2 ] }
- { "ADC-R,R" [ [ $1 ] keep [ $3 ] keep [ add-byte-with-carry ] keep $2 ] }
- { "ADC-R,(RR)" [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ add-byte-with-carry ] keep $2 ] }
- { "ADD-R,N" [ [ $1 ] keep [ next-byte ] keep [ add-byte ] keep $2 ] }
- { "ADD-R,R" [ [ $1 ] keep [ $3 ] keep [ add-byte ] keep $2 ] }
- { "ADD-RR,RR" [ [ $1 ] keep [ $3 ] keep [ add-word ] keep $2 ] }
- { "ADD-R,(RR)" [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ add-byte ] keep $2 ] }
- { "SBC-R,N" [ [ $1 ] keep [ next-byte ] keep [ sub-byte-with-carry ] keep $2 ] }
- { "SBC-R,R" [ [ $1 ] keep [ $3 ] keep [ sub-byte-with-carry ] keep $2 ] }
- { "SBC-R,(RR)" [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ sub-byte-with-carry ] keep $2 ] }
- { "SUB-R" [ [ a>> ] keep [ $1 ] keep [ sub-byte ] keep (>>a) ] }
- { "SUB-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ sub-byte ] keep (>>a) ] }
- { "SUB-N" [ [ a>> ] keep [ next-byte ] keep [ sub-byte ] keep (>>a) ] }
- { "CPL" [ (emulate-CPL) ] }
- { "DAA" [ (emulate-DAA) ] }
- { "RLA" [ (emulate-RLA) ] }
- { "RRA" [ (emulate-RRA) ] }
- { "CCF" [ carry-flag swap cpu-f-bitxor= ] }
- { "SCF" [ carry-flag swap cpu-f-bitor= ] }
- { "RLCA" [ (emulate-RLCA) ] }
- { "RRCA" [ (emulate-RRCA) ] }
- { "HALT" [ drop ] }
- { "DI" [ [ 255 interrupt-flag - ] swap cpu-f-bitand ] }
- { "EI" [ [ interrupt-flag ] swap cpu-f-bitor ] }
- { "POP-RR" [ [ pop-sp ] keep $2 ] }
- { "PUSH-RR" [ [ $1 ] keep push-sp ] }
- { "INC-R" [ [ $1 ] keep [ inc-byte ] keep $2 ] }
- { "DEC-R" [ [ $1 ] keep [ dec-byte ] keep $2 ] }
- { "INC-RR" [ [ $1 ] keep [ inc-word ] keep $2 ] }
- { "DEC-RR" [ [ $1 ] keep [ dec-word ] keep $2 ] }
- { "DEC-(RR)" [ [ $1 ] keep [ read-byte ] keep [ dec-byte ] keep [ $1 ] keep write-byte ] }
- { "INC-(RR)" [ [ $1 ] keep [ read-byte ] keep [ inc-byte ] keep [ $1 ] keep write-byte ] }
- { "JP-NN" [ [ pc>> ] keep [ read-word ] keep (>>pc) ] }
- { "JP-F|FF,NN" [ [ $1 ] keep swap [ [ next-word ] keep [ (>>pc) ] keep [ cycles>> ] keep swap 5 + swap (>>cycles) ] [ [ pc>> 2 + ] keep (>>pc) ] if ] }
- { "JP-(RR)" [ [ $1 ] keep (>>pc) ] }
- { "CALL-NN" [ (emulate-CALL) ] }
- { "CALL-F|FF,NN" [ [ $1 ] keep swap [ 7 over inc-cycles (emulate-CALL) ] [ [ pc>> 2 + ] keep (>>pc) ] if ] }
- { "LD-RR,NN" [ [ next-word ] keep $2 ] }
- { "LD-RR,RR" [ [ $3 ] keep $2 ] }
- { "LD-R,N" [ [ next-byte ] keep $2 ] }
- { "LD-(RR),N" [ [ next-byte ] keep [ $1 ] keep write-byte ] }
- { "LD-(RR),R" [ [ $3 ] keep [ $1 ] keep write-byte ] }
- { "LD-R,R" [ [ $3 ] keep $2 ] }
- { "LD-R,(RR)" [ [ $3 ] keep [ read-byte ] keep $2 ] }
- { "LD-(NN),RR" [ [ $1 ] keep [ next-word ] keep write-word ] }
- { "LD-(NN),R" [ [ $1 ] keep [ next-word ] keep write-byte ] }
- { "LD-RR,(NN)" [ [ next-word ] keep [ read-word ] keep $2 ] }
- { "LD-R,(NN)" [ [ next-word ] keep [ read-byte ] keep $2 ] }
- { "OUT-(N),R" [ [ $1 ] keep [ next-byte ] keep write-port ] }
- { "IN-R,(N)" [ [ next-byte ] keep [ read-port ] keep (>>a) ] }
- { "EX-(RR),RR" [ [ $1 ] keep [ read-word ] keep [ $3 ] keep [ $1 ] keep [ write-word ] keep $4 ] }
- { "EX-RR,RR" [ [ $1 ] keep [ $3 ] keep [ $2 ] keep $4 ] }
+ { "NOP" [ drop ] }
+ { "RET-NN" [ ret-from-sub ] }
+ { "RST-0" [ 0 swap (emulate-RST) ] }
+ { "RST-8" [ 8 swap (emulate-RST) ] }
+ { "RST-10H" [ 0x10 swap (emulate-RST) ] }
+ { "RST-18H" [ 0x18 swap (emulate-RST) ] }
+ { "RST-20H" [ 0x20 swap (emulate-RST) ] }
+ { "RST-28H" [ 0x28 swap (emulate-RST) ] }
+ { "RST-30H" [ 0x30 swap (emulate-RST) ] }
+ { "RST-38H" [ 0x38 swap (emulate-RST) ] }
+ { "RET-F|FF" [ dup $1 [ 6 over inc-cycles ret-from-sub ] [ drop ] if ] }
+ { "CP-N" [ [ a>> ] keep [ next-byte ] keep sub-byte drop ] }
+ { "CP-R" [ [ a>> ] keep [ $1 ] keep sub-byte drop ] }
+ { "CP-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep sub-byte drop ] }
+ { "OR-N" [ [ a>> ] keep [ next-byte ] keep [ or-byte ] keep a<< ] }
+ { "OR-R" [ [ a>> ] keep [ $1 ] keep [ or-byte ] keep a<< ] }
+ { "OR-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ or-byte ] keep a<< ] }
+ { "XOR-N" [ [ a>> ] keep [ next-byte ] keep [ xor-byte ] keep a<< ] }
+ { "XOR-R" [ [ a>> ] keep [ $1 ] keep [ xor-byte ] keep a<< ] }
+ { "XOR-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ xor-byte ] keep a<< ] }
+ { "AND-N" [ [ a>> ] keep [ next-byte ] keep [ and-byte ] keep a<< ] }
+ { "AND-R" [ [ a>> ] keep [ $1 ] keep [ and-byte ] keep a<< ] }
+ { "AND-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ and-byte ] keep a<< ] }
+ { "ADC-R,N" [ [ $1 ] keep [ next-byte ] keep [ add-byte-with-carry ] keep $2 ] }
+ { "ADC-R,R" [ [ $1 ] keep [ $3 ] keep [ add-byte-with-carry ] keep $2 ] }
+ { "ADC-R,(RR)" [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ add-byte-with-carry ] keep $2 ] }
+ { "ADD-R,N" [ [ $1 ] keep [ next-byte ] keep [ add-byte ] keep $2 ] }
+ { "ADD-R,R" [ [ $1 ] keep [ $3 ] keep [ add-byte ] keep $2 ] }
+ { "ADD-RR,RR" [ [ $1 ] keep [ $3 ] keep [ add-word ] keep $2 ] }
+ { "ADD-R,(RR)" [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ add-byte ] keep $2 ] }
+ { "SBC-R,N" [ [ $1 ] keep [ next-byte ] keep [ sub-byte-with-carry ] keep $2 ] }
+ { "SBC-R,R" [ [ $1 ] keep [ $3 ] keep [ sub-byte-with-carry ] keep $2 ] }
+ { "SBC-R,(RR)" [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ sub-byte-with-carry ] keep $2 ] }
+ { "SUB-R" [ [ a>> ] keep [ $1 ] keep [ sub-byte ] keep a<< ] }
+ { "SUB-(RR)" [ [ a>> ] keep [ $1 ] keep [ read-byte ] keep [ sub-byte ] keep a<< ] }
+ { "SUB-N" [ [ a>> ] keep [ next-byte ] keep [ sub-byte ] keep a<< ] }
+ { "CPL" [ (emulate-CPL) ] }
+ { "DAA" [ (emulate-DAA) ] }
+ { "RLA" [ (emulate-RLA) ] }
+ { "RRA" [ (emulate-RRA) ] }
+ { "CCF" [ carry-flag swap cpu-f-bitxor= ] }
+ { "SCF" [ carry-flag swap cpu-f-bitor= ] }
+ { "RLCA" [ (emulate-RLCA) ] }
+ { "RRCA" [ (emulate-RRCA) ] }
+ { "HALT" [ drop ] }
+ { "DI" [ [ 255 interrupt-flag - ] swap cpu-f-bitand ] }
+ { "EI" [ [ interrupt-flag ] swap cpu-f-bitor ] }
+ { "POP-RR" [ [ pop-sp ] keep $2 ] }
+ { "PUSH-RR" [ [ $1 ] keep push-sp ] }
+ { "INC-R" [ [ $1 ] keep [ inc-byte ] keep $2 ] }
+ { "DEC-R" [ [ $1 ] keep [ dec-byte ] keep $2 ] }
+ { "INC-RR" [ [ $1 ] keep [ inc-word ] keep $2 ] }
+ { "DEC-RR" [ [ $1 ] keep [ dec-word ] keep $2 ] }
+ { "DEC-(RR)" [ [ $1 ] keep [ read-byte ] keep [ dec-byte ] keep [ $1 ] keep write-byte ] }
+ { "INC-(RR)" [ [ $1 ] keep [ read-byte ] keep [ inc-byte ] keep [ $1 ] keep write-byte ] }
+ { "JP-NN" [ [ pc>> ] keep [ read-word ] keep pc<< ] }
+ { "JP-F|FF,NN" [ [ $1 ] keep swap [ [ next-word ] keep [ pc<< ] keep [ cycles>> ] keep swap 5 + swap cycles<< ] [ [ pc>> 2 + ] keep pc<< ] if ] }
+ { "JP-(RR)" [ [ $1 ] keep pc<< ] }
+ { "CALL-NN" [ (emulate-CALL) ] }
+ { "CALL-F|FF,NN" [ [ $1 ] keep swap [ 7 over inc-cycles (emulate-CALL) ] [ [ pc>> 2 + ] keep pc<< ] if ] }
+ { "LD-RR,NN" [ [ next-word ] keep $2 ] }
+ { "LD-RR,RR" [ [ $3 ] keep $2 ] }
+ { "LD-R,N" [ [ next-byte ] keep $2 ] }
+ { "LD-(RR),N" [ [ next-byte ] keep [ $1 ] keep write-byte ] }
+ { "LD-(RR),R" [ [ $3 ] keep [ $1 ] keep write-byte ] }
+ { "LD-R,R" [ [ $3 ] keep $2 ] }
+ { "LD-R,(RR)" [ [ $3 ] keep [ read-byte ] keep $2 ] }
+ { "LD-(NN),RR" [ [ $1 ] keep [ next-word ] keep write-word ] }
+ { "LD-(NN),R" [ [ $1 ] keep [ next-word ] keep write-byte ] }
+ { "LD-RR,(NN)" [ [ next-word ] keep [ read-word ] keep $2 ] }
+ { "LD-R,(NN)" [ [ next-word ] keep [ read-byte ] keep $2 ] }
+ { "OUT-(N),R" [ [ $1 ] keep [ next-byte ] keep write-port ] }
+ { "IN-R,(N)" [ [ next-byte ] keep [ read-port ] keep a<< ] }
+ { "EX-(RR),RR" [ [ $1 ] keep [ read-word ] keep [ $3 ] keep [ $1 ] keep [ write-word ] keep $4 ] }
+ { "EX-RR,RR" [ [ $1 ] keep [ $3 ] keep [ $2 ] keep $4 ] }
} ;
: 8-bit-registers ( -- parser )
#! that would implement that instruction.
dup " " join instruction-quotations
[
- "_" join [ "emulate-" % % ] "" make create-in dup last-instruction global set-at
- ] dip (( cpu -- )) define-declared ;
+ "_" join [ "emulate-" % % ] "" make create-in dup last-instruction set-global
+ ] dip ( cpu -- ) define-declared ;
SYNTAX: INSTRUCTION: ";" parse-tokens parse-instructions ;
-SYNTAX: cycles
- #! Set the number of cycles for the last instruction that was defined.
- scan string>number last-opcode global at instruction-cycles set-nth ;
+SYNTAX: cycles
+ #! Set the number of cycles for the last instruction that was defined.
+ scan-token string>number last-opcode get-global instruction-cycles set-nth ;
-SYNTAX: opcode ( -- )
+SYNTAX: opcode
#! Set the opcode number for the last instruction that was defined.
- last-instruction global at 1quotation scan 16 base>
- dup last-opcode global set-at set-instruction ;
+ last-instruction get-global 1quotation scan-token hex>
+ dup last-opcode set-global set-instruction ;