]> gitweb.factorcode.org Git - factor.git/blob - extra/cpu/8080/emulator/emulator.factor
Merge git://repo.or.cz/factor/jcg
[factor.git] / extra / cpu / 8080 / emulator / emulator.factor
1 ! Copyright (C) 2006 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 !
4 USING: accessors kernel math sequences words arrays io io.files
5 namespaces math.parser assocs quotations parser lexer
6 parser-combinators tools.time io.encodings.binary sequences.deep
7 symbols combinators ;
8 IN: cpu.8080.emulator
9
10 TUPLE: cpu b c d e f h l a pc sp halted? last-interrupt cycles ram ;
11
12 GENERIC: reset        ( cpu            -- )
13 GENERIC: update-video ( value addr cpu -- )
14 GENERIC: read-port    ( port cpu       -- byte )
15 GENERIC: write-port   ( value port cpu -- )
16
17 M: cpu update-video ( value addr cpu -- )
18   3drop ;
19
20 M: cpu read-port ( port cpu -- byte )
21   #! Read a byte from the hardware port. 'port' should
22   #! be an 8-bit value.
23   2drop 0 ;
24
25 M: cpu write-port ( value port cpu -- )
26   #! Write a byte to the hardware port, where 'port' is
27   #! an 8-bit value.
28   3drop ;
29
30 : carry-flag        HEX: 01 ; inline
31 : parity-flag       HEX: 04 ; inline
32 : half-carry-flag   HEX: 10 ; inline
33 : interrupt-flag    HEX: 20 ; inline
34 : zero-flag         HEX: 40 ; inline
35 : sign-flag         HEX: 80 ; inline
36
37 : >word< ( word -- byte byte )
38   #! Explode a word into its two 8 bit values.
39   dup HEX: FF bitand swap -8 shift HEX: FF bitand swap ;
40
41 : cpu-af ( cpu -- word )
42   #! Return the 16-bit pseudo register AF.
43   [ cpu-a 8 shift ] keep cpu-f bitor ;
44
45 : set-cpu-af ( value cpu -- )
46   #! Set the value of the 16-bit pseudo register AF
47   >r >word< r> tuck set-cpu-f set-cpu-a ;
48
49 : cpu-bc ( cpu -- word )
50   #! Return the 16-bit pseudo register BC.
51   [ cpu-b 8 shift ] keep cpu-c bitor ;
52
53 : set-cpu-bc ( value cpu -- )
54   #! Set the value of the 16-bit pseudo register BC
55   >r >word< r> tuck set-cpu-c set-cpu-b ;
56
57 : cpu-de ( cpu -- word )
58   #! Return the 16-bit pseudo register DE.
59   [ cpu-d 8 shift ] keep cpu-e bitor ;
60
61 : set-cpu-de ( value cpu -- )
62   #! Set the value of the 16-bit pseudo register DE
63   >r >word< r> tuck set-cpu-e set-cpu-d ;
64
65 : cpu-hl ( cpu -- word )
66   #! Return the 16-bit pseudo register HL.
67   [ cpu-h 8 shift ] keep cpu-l bitor ;
68
69 : set-cpu-hl ( value cpu -- )
70   #! Set the value of the 16-bit pseudo register HL
71   >r >word< r> tuck set-cpu-l set-cpu-h ;
72
73 : flag-set? ( flag cpu -- bool )
74   cpu-f bitand 0 = not ;
75
76 : flag-clear? ( flag cpu -- bool )
77   cpu-f bitand 0 = ;
78
79 : flag-nz? ( cpu -- bool )
80   #! Test flag status
81   cpu-f zero-flag bitand 0 = ;
82
83 : flag-z? ( cpu -- bool )
84   #! Test flag status
85   cpu-f zero-flag bitand 0 = not ;
86
87 : flag-nc? ( cpu -- bool )
88   #! Test flag status
89   cpu-f carry-flag bitand 0 = ;
90
91 : flag-c? ( cpu -- bool )
92   #! Test flag status
93   cpu-f carry-flag bitand 0 = not ;
94
95 : flag-po? ( cpu -- bool )
96   #! Test flag status
97   cpu-f parity-flag bitand 0 =  ;
98
99 : flag-pe? ( cpu -- bool )
100   #! Test flag status
101   cpu-f parity-flag bitand 0 = not ;
102
103 : flag-p? ( cpu -- bool )
104   #! Test flag status
105   cpu-f sign-flag bitand 0 = ;
106
107 : flag-m? ( cpu -- bool )
108   #! Test flag status
109   cpu-f sign-flag bitand 0 = not ;
110
111 : read-byte ( addr cpu -- byte )
112   #! Read one byte from memory at the specified address.
113   #! The address is 16-bit, but if a value greater than
114   #! 0xFFFF is provided then return a default value.
115   over HEX: FFFF <= [
116     cpu-ram nth
117   ] [
118     2drop HEX: FF
119   ] if ;
120
121 : read-word ( addr cpu -- word )  
122   #! Read a 16-bit word from memory at the specified address.
123   #! The address is 16-bit, but if a value greater than
124   #! 0xFFFF is provided then return a default value.
125   [ read-byte ] 2keep >r 1 + r> read-byte 8 shift bitor ;
126  
127 : next-byte ( cpu -- byte )
128   #! Return the value of the byte at PC, and increment PC.
129   [ cpu-pc ] keep
130   [ read-byte ] keep 
131   [ cpu-pc 1 + ] keep
132   set-cpu-pc ;
133
134 : next-word ( cpu -- word )
135   #! Return the value of the word at PC, and increment PC.
136   [ cpu-pc ] keep
137   [ read-word ] keep 
138   [ cpu-pc 2 + ] keep
139   set-cpu-pc ;
140
141
142 : write-byte ( value addr cpu -- )
143   #! Write a byte to the specified memory address.
144   over dup HEX: 2000 < swap HEX: FFFF > or [
145     3drop
146   ] [
147     3dup cpu-ram set-nth
148     update-video
149   ] if ;
150
151
152 : write-word ( value addr cpu -- )
153   #! Write a 16-bit word to the specified memory address.
154   >r >r >word< r> r> [ write-byte ] 2keep >r 1 + r> write-byte ;
155
156 : cpu-a-bitand ( quot cpu -- )
157   #! A &= quot call 
158   [ cpu-a swap call bitand ] keep set-cpu-a ; inline
159
160 : cpu-a-bitor ( quot cpu -- )
161   #! A |= quot call 
162   [ cpu-a swap call bitor ] keep set-cpu-a ; inline
163
164 : cpu-a-bitxor ( quot cpu -- )
165   #! A ^= quot call 
166   [ cpu-a swap call bitxor ] keep set-cpu-a ; inline
167
168 : cpu-a-bitxor= ( value cpu -- )
169   #! cpu-a ^= value
170   [ cpu-a bitxor ] keep set-cpu-a ;
171
172 : cpu-f-bitand ( quot cpu -- )
173   #! F &= quot call 
174   [ cpu-f swap call bitand ] keep set-cpu-f ; inline
175
176 : cpu-f-bitor ( quot cpu -- )
177   #! F |= quot call 
178   [ cpu-f swap call bitor ] keep set-cpu-f ; inline
179
180 : cpu-f-bitxor ( quot cpu -- )
181   #! F |= quot call 
182   [ cpu-f swap call bitxor ] keep set-cpu-f ; inline
183
184 : cpu-f-bitor= ( value cpu -- )
185   #! cpu-f |= value
186   [ cpu-f bitor ] keep set-cpu-f ;
187
188 : cpu-f-bitand= ( value cpu -- )
189   #! cpu-f &= value
190   [ cpu-f bitand ] keep set-cpu-f ;
191
192 : cpu-f-bitxor= ( value cpu -- )
193   #! cpu-f ^= value
194   [ cpu-f bitxor ] keep set-cpu-f ;
195
196 : set-flag ( cpu flag -- )
197   swap cpu-f-bitor= ;
198
199 : clear-flag ( cpu flag -- )
200    bitnot HEX: FF bitand swap cpu-f-bitand= ;
201
202 : update-zero-flag ( result cpu -- )
203   #! If the result of an instruction has the value 0, this
204   #! flag is set, otherwise it is reset.
205   swap HEX: FF bitand 0 = [ zero-flag set-flag ] [ zero-flag clear-flag ] if ;
206
207 : update-sign-flag ( result cpu -- )
208   #! If the most significant bit of the result 
209   #! has the value 1 then the flag is set, otherwise
210   #! it is reset.
211   swap HEX: 80 bitand 0 = [ sign-flag clear-flag ] [ sign-flag set-flag ] if ;
212
213 : update-parity-flag ( result cpu -- )
214   #! If the modulo 2 sum of the bits of the result
215   #! is 0, (ie. if the result has even parity) this flag
216   #! is set, otherwise it is reset.
217   swap HEX: FF bitand 2 mod 0 = [ parity-flag set-flag ] [ parity-flag clear-flag ] if ;
218
219 : update-carry-flag ( result cpu -- )
220   #! If the instruction resulted in a carry (from addition) 
221   #! or a borrow (from subtraction or a comparison) out of the
222   #! higher order bit, this flag is set, otherwise it is reset.
223   swap dup HEX: 100 >= swap 0 < or [ carry-flag set-flag ] [ carry-flag clear-flag ] if ;
224
225 : update-half-carry-flag ( original change-by result cpu -- )
226   #! If the instruction caused a carry out of bit 3 and into bit 4 of the
227   #! resulting value, the half carry flag is set, otherwise it is reset.
228   #! The 'original' is the original value of the register being changed.
229   #! 'change-by' is the amount it is being added or decremented by.
230   #! 'result' is the result of that change.
231   >r bitxor bitxor HEX: 10 bitand 0 = not r> 
232   swap [ half-carry-flag set-flag ] [ half-carry-flag clear-flag ] if ;
233
234 : update-flags ( result cpu -- )
235   2dup update-carry-flag
236   2dup update-parity-flag
237   2dup update-sign-flag
238   update-zero-flag ;
239
240 : update-flags-no-carry ( result cpu -- )
241   2dup update-parity-flag
242   2dup update-sign-flag
243   update-zero-flag ;
244
245 : add-byte ( lhs rhs cpu -- result )
246   #! Add rhs to lhs
247   >r 2dup + r> ! lhs rhs result cpu
248   [ update-flags ] 2keep 
249   [ update-half-carry-flag ] 2keep
250   drop HEX: FF bitand ;
251
252 : add-carry ( change-by result cpu -- change-by result )
253   #! Add the effect of the carry flag to the result
254   flag-c? [ 1 + >r 1 + r> ] when ;
255
256 : add-byte-with-carry ( lhs rhs cpu -- result )
257   #! Add rhs to lhs plus carry.
258   >r 2dup + r> ! lhs rhs result cpu
259   [ add-carry ] keep
260   [ update-flags ] 2keep 
261   [ update-half-carry-flag ] 2keep
262   drop HEX: FF bitand ;
263
264 : sub-carry ( change-by result cpu -- change-by result ) 
265   #! Subtract the effect of the carry flag from the result
266   flag-c? [ 1 - >r 1 - r>  ] when ;
267
268 : sub-byte ( lhs rhs cpu -- result )
269   #! Subtract rhs from lhs
270   >r 2dup - r> 
271   [ update-flags ] 2keep 
272   [ update-half-carry-flag ] 2keep
273   drop HEX: FF bitand ;
274
275 : sub-byte-with-carry ( lhs rhs cpu -- result )
276   #! Subtract rhs from lhs and take carry into account
277   >r 2dup - r> 
278   [ sub-carry ] keep 
279   [ update-flags ] 2keep 
280   [ update-half-carry-flag ] 2keep
281   drop HEX: FF bitand ;
282  
283 : inc-byte ( byte cpu -- result )
284   #! Increment byte by one. Note that carry flag is not affected
285   #! by this operation.
286   >r 1 2dup + r> ! lhs rhs result cpu
287   [ update-flags-no-carry ] 2keep 
288   [ update-half-carry-flag ] 2keep
289   drop HEX: FF bitand ;
290
291 : dec-byte ( byte cpu -- result )
292   #! Decrement byte by one. Note that carry flag is not affected
293   #! by this operation.
294   >r 1 2dup - r> ! lhs rhs result cpu
295   [ update-flags-no-carry ] 2keep 
296   [ update-half-carry-flag ] 2keep
297   drop HEX: FF bitand ;
298
299 : inc-word ( w cpu -- w )
300   #! Increment word by one. Note that no flags are modified.
301   drop 1 + HEX: FFFF bitand ;
302
303 : dec-word ( w cpu -- w )
304   #! Decrement word by one. Note that no flags are modified.
305   drop 1 - HEX: FFFF bitand ;
306
307 : add-word ( lhs rhs cpu -- result )
308   #! Add rhs to lhs. Note that only the carry flag is modified
309   #! and only if there is a carry out of the double precision add.
310   >r + r> over HEX: FFFF > [ carry-flag set-flag ] [ drop ] if HEX: FFFF bitand ;
311
312 : bit3or ( lhs rhs -- 0|1 )
313   #! bitor bit 3 of the two numbers on the stack
314   BIN: 00001000 bitand -3 shift >r
315   BIN: 00001000 bitand -3 shift r> 
316   bitor ;
317
318 : and-byte ( lhs rhs cpu -- result )
319   #! Logically and rhs to lhs. The carry flag is cleared and
320   #! the half carry is set to the ORing of bits 3 of the operands.
321   [ drop bit3or ] 3keep ! bit3or lhs rhs cpu
322   >r bitand r> [ update-flags ] 2keep 
323   [ carry-flag clear-flag ] keep
324   rot 0 = [ half-carry-flag set-flag ] [ half-carry-flag clear-flag ] if
325   HEX: FF bitand ;
326
327 : xor-byte ( lhs rhs cpu -- result )
328   #! Logically xor rhs to lhs. The carry and half-carry flags are cleared.
329   >r bitxor r> [ update-flags ] 2keep 
330   [ half-carry-flag carry-flag bitor clear-flag ] keep
331   drop HEX: FF bitand ;
332
333 : or-byte ( lhs rhs cpu -- result )
334   #! Logically or rhs to lhs. The carry and half-carry flags are cleared.
335   >r bitor r> [ update-flags ] 2keep 
336   [ half-carry-flag carry-flag bitor clear-flag ] keep
337   drop HEX: FF bitand ;
338
339 : flags ( seq -- seq )
340   [ 0 [ execute bitor ] reduce ] map ;
341
342 : decrement-sp ( n cpu -- )
343   #! Decrement the stackpointer by n.  
344   [ cpu-sp ] keep 
345   >r swap - r> set-cpu-sp ;
346
347 : save-pc ( cpu -- )
348   #! Save the value of the PC on the stack.
349   [ cpu-pc ] keep ! pc cpu
350   [ cpu-sp ] keep ! pc sp cpu
351   write-word ;
352
353 : push-pc ( cpu -- )
354   #! Push the value of the PC on the stack.
355   2 over decrement-sp
356   save-pc ;
357
358 : pop-pc ( cpu -- pc )
359   #! Pop the value of the PC off the stack.
360   [ cpu-sp ] keep
361   [ read-word ] keep 
362   -2 swap decrement-sp ;
363
364 : push-sp ( value cpu -- )
365   [ 2 swap decrement-sp ] keep
366   [ cpu-sp ] keep
367   write-word ;
368   
369 : pop-sp ( cpu -- value )
370   [ cpu-sp ] keep
371   [ read-word ] keep
372   -2 swap decrement-sp ;
373
374 : call-sub ( addr cpu -- )
375   #! Call the address as a subroutine.
376   dup push-pc 
377   >r HEX: FFFF bitand r> set-cpu-pc ;
378
379 : ret-from-sub ( cpu -- )
380   [ pop-pc ] keep set-cpu-pc ;
381  
382 : interrupt ( number cpu -- )
383   #! Perform a hardware interrupt
384 !  "***Interrupt: " write over 16 >base print 
385   dup cpu-f interrupt-flag bitand 0 = not [
386     dup push-pc
387     set-cpu-pc
388   ] [
389     2drop
390   ] if ;
391
392 : inc-cycles ( n cpu -- )
393   #! Increment the number of cpu cycles
394   [ cpu-cycles + ] keep set-cpu-cycles ;
395   
396 : instruction-cycles ( -- vector )
397   #! Return a 256 element vector containing the cycles for
398   #! each opcode in the 8080 instruction set.
399   << 256 f <array> parsed >> ;
400
401 : not-implemented ( <cpu> -- )
402   drop ;
403
404 : instructions ( -- vector )
405   #! Return a 256 element vector containing the emulation words for
406   #! each opcode in the 8080 instruction set.
407   << 256 [ [ not-implemented ] 2array ] map parsed >> ; inline
408
409 : set-instruction ( quot n -- )
410   tuck >r 2array r> instructions set-nth ;
411
412 M: cpu reset ( cpu -- )
413   #! Reset the CPU to its poweron state
414   [ 0 swap set-cpu-b  ] keep
415   [ 0 swap set-cpu-c  ] keep
416   [ 0 swap set-cpu-d  ] keep
417   [ 0 swap set-cpu-e  ] keep
418   [ 0 swap set-cpu-h  ] keep
419   [ 0 swap set-cpu-l  ] keep
420   [ 0 swap set-cpu-a  ] keep
421   [ 0 swap set-cpu-f  ] keep
422   [ 0 swap set-cpu-pc  ] keep
423   [ HEX: F000 swap set-cpu-sp  ] keep 
424   [ HEX: FFFF 0 <array> swap set-cpu-ram ] keep
425   [ f swap set-cpu-halted? ] keep
426   [ HEX: 10 swap set-cpu-last-interrupt ] keep
427   0 swap set-cpu-cycles ;
428
429 : <cpu> ( -- cpu ) cpu new dup reset ;
430
431 : (load-rom) ( n ram -- )
432   read1 [ ! n ram ch
433     -rot [ set-nth ] 2keep >r 1 + r> (load-rom)
434   ] [
435     2drop
436   ] if* ;
437
438   #! Reads the ROM from stdin and stores it in ROM from
439   #! offset n.
440 : load-rom ( filename cpu -- )
441   #! Load the contents of the file into ROM.
442   #! (address 0x0000-0x1FFF).
443   cpu-ram swap binary [ 
444     0 swap (load-rom)
445   ] with-file-reader ;
446
447 SYMBOL: rom-root
448
449 : rom-dir ( -- string )
450   rom-root get [ home "roms" append-path dup exists? [ drop f ] unless ] unless* ;
451
452 : load-rom* ( seq cpu -- )
453   #! 'seq' is an array of arrays. Each array contains
454   #! an address and filename of a ROM file. The ROM
455   #! file will be loaded at the specified address. This
456   #! file path shoul dbe relative to the '/roms' resource path.
457   rom-dir [
458     cpu-ram [
459       swap first2 rom-dir prepend-path binary [      
460         swap (load-rom)
461       ] with-file-reader
462     ] curry each 
463   ] [
464     ! 
465     ! the ROM files.
466     "Set 'rom-root' to the path containing the root of the 8080 ROM files." throw
467   ] if ;
468
469 : read-instruction ( cpu -- word )
470   #! Read the next instruction from the cpu's program 
471   #! counter, and increment the program counter.
472   [ cpu-pc ] keep ! pc cpu
473   [ over 1 + swap set-cpu-pc ] keep
474   read-byte ;
475
476 : get-cycles ( n -- opcode )
477   #! Returns the cycles for the given instruction value.
478   #! If the opcode is not defined throw an error.
479   dup instruction-cycles nth [ 
480     nip  
481   ] [
482     [ "Undefined 8080 opcode: " % number>string % ] "" make throw
483   ] if* ;
484
485 : process-interrupts ( cpu -- )
486   #! Process any hardware interrupts
487   [ cpu-cycles ] keep 
488   over 16667 < [
489     2drop
490   ] [ 
491     [ >r 16667 - r> set-cpu-cycles ] keep
492     dup cpu-last-interrupt HEX: 10 = [
493       HEX: 08 over set-cpu-last-interrupt HEX: 08 swap interrupt
494     ] [
495       HEX: 10 over set-cpu-last-interrupt HEX: 10 swap interrupt
496     ] if     
497   ] if ;
498
499 : peek-instruction ( cpu -- word )
500   #! Return the next instruction from the cpu's program
501   #! counter, but don't increment the counter.
502   [ cpu-pc ] keep read-byte instructions nth first ;
503
504 : cpu. ( cpu -- )
505   [ " PC: " write cpu-pc 16 >base 4 CHAR: \s pad-left write ] keep 
506   [ " B: " write cpu-b 16 >base 2 CHAR: \s pad-left write ] keep 
507   [ " C: " write cpu-c 16 >base 2 CHAR: \s pad-left write ] keep 
508   [ " D: " write cpu-d 16 >base 2 CHAR: \s pad-left write ] keep 
509   [ " E: " write cpu-e 16 >base 2 CHAR: \s pad-left write ] keep 
510   [ " F: " write cpu-f 16 >base 2 CHAR: \s pad-left write ] keep 
511   [ " H: " write cpu-h 16 >base 2 CHAR: \s pad-left write ] keep 
512   [ " L: " write cpu-l 16 >base 2 CHAR: \s pad-left write ] keep 
513   [ " A: " write cpu-a 16 >base 2 CHAR: \s pad-left write ] keep 
514   [ " SP: " write cpu-sp 16 >base 4 CHAR: \s pad-left write ] keep 
515   [ " cycles: " write cpu-cycles number>string 5 CHAR: \s pad-left write ] keep 
516   [ " " write peek-instruction name>> write " " write ] keep
517   nl drop ;
518
519 : cpu*. ( cpu -- )
520   [ " PC: " write cpu-pc 16 >base 4 CHAR: \s pad-left write ] keep 
521   [ " B: " write cpu-b 16 >base 2 CHAR: \s pad-left write ] keep 
522   [ " C: " write cpu-c 16 >base 2 CHAR: \s pad-left write ] keep 
523   [ " D: " write cpu-d 16 >base 2 CHAR: \s pad-left write ] keep 
524   [ " E: " write cpu-e 16 >base 2 CHAR: \s pad-left write ] keep 
525   [ " F: " write cpu-f 16 >base 2 CHAR: \s pad-left write ] keep 
526   [ " H: " write cpu-h 16 >base 2 CHAR: \s pad-left write ] keep 
527   [ " L: " write cpu-l 16 >base 2 CHAR: \s pad-left write ] keep 
528   [ " A: " write cpu-a 16 >base 2 CHAR: \s pad-left write ] keep 
529   [ " SP: " write cpu-sp 16 >base 4 CHAR: \s pad-left write ] keep 
530   [ " cycles: " write cpu-cycles number>string 5 CHAR: \s pad-left write ] keep 
531   nl drop ;
532
533 : register-lookup ( string -- vector )
534   #! Given a string containing a register name, return a vector
535   #! where the 1st item is the getter and the 2nd is the setter
536   #! for that register.
537   H{
538     { "A"  { cpu-a  set-cpu-a  } }
539     { "B"  { cpu-b  set-cpu-b  } }
540     { "C"  { cpu-c  set-cpu-c  } }
541     { "D"  { cpu-d  set-cpu-d  } }
542     { "E"  { cpu-e  set-cpu-e  } }
543     { "H"  { cpu-h  set-cpu-h  } }
544     { "L"  { cpu-l  set-cpu-l  } }
545     { "AF" { cpu-af set-cpu-af } }
546     { "BC" { cpu-bc set-cpu-bc } }
547     { "DE" { cpu-de set-cpu-de } }
548     { "HL" { cpu-hl set-cpu-hl } }
549     { "SP" { cpu-sp set-cpu-sp } }
550   } at ;
551
552
553 : flag-lookup ( string -- vector )
554   #! Given a string containing a flag name, return a vector
555   #! where the 1st item is a word that tests that flag.
556   H{
557     { "NZ"  { flag-nz?  } }
558     { "NC"  { flag-nc?  } }
559     { "PO"  { flag-po?  } }
560     { "PE"  { flag-pe?  } }
561     { "Z"  { flag-z?  } }
562     { "C"  { flag-c? } }
563     { "P"  { flag-p?  } }
564     { "M" { flag-m?  } }
565   } at ;
566
567 SYMBOLS: $1 $2 $3 $4 ;
568
569 : replace-patterns ( vector tree -- tree )
570   [
571     {
572       { $1 [ first ] }
573       { $2 [ second ] }
574       { $3 [ third ] }
575       { $4 [ fourth ] }
576       [ nip ]
577     } case
578   ] with deep-map ;
579
580 : (emulate-RST) ( n cpu -- )
581   #! RST nn
582   [ cpu-sp 2 - dup ] keep ! sp sp cpu
583   [ set-cpu-sp ] keep ! sp cpu
584   [ cpu-pc ] keep ! sp pc cpu
585   swapd [ write-word ] keep ! cpu
586   >r 8 * r> set-cpu-pc ;
587
588 : (emulate-CALL) ( cpu -- )
589   #! 205 - CALL nn
590   [ next-word HEX: FFFF bitand ] keep ! addr cpu
591   [ cpu-sp 2 - dup ] keep ! addr sp sp cpu
592   [ set-cpu-sp ] keep ! addr sp cpu
593   [ cpu-pc ] keep ! addr sp pc cpu
594   swapd [ write-word ] keep ! addr cpu
595   set-cpu-pc ;
596
597 : (emulate-RLCA) ( cpu -- )
598   #! The content of the accumulator is rotated left
599   #! one position. The low order bit and the carry flag
600   #! are both set to the value shifd out of the high
601   #! order bit position. Only the carry flag is affected.
602   [ cpu-a -7 shift ] keep 
603   over 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
604   [ cpu-a 1 shift HEX: FF bitand ] keep 
605   >r bitor r> set-cpu-a ;
606
607 : (emulate-RRCA) ( cpu -- )
608   #! The content of the accumulator is rotated right
609   #! one position. The high order bit and the carry flag
610   #! are both set to the value shifd out of the low
611   #! order bit position. Only the carry flag is affected.
612   [ cpu-a 1 bitand 7 shift ] keep 
613   over 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
614   [ cpu-a 254 bitand -1 shift ] keep 
615   >r bitor r> set-cpu-a ;
616
617 : (emulate-RLA) ( cpu -- )  
618   #! The content of the accumulator is rotated left
619   #! one position through the carry flag. The low
620   #! order bit is set equal to the carry flag and
621   #! the carry flag is set to the value shifd out 
622   #! of the high order bit. Only the carry flag is
623   #! affected.
624   [ carry-flag swap flag-set? [ 1 ] [ 0 ] if ] keep 
625   [ cpu-a 127 bitand 7 shift ] keep 
626   dup cpu-a 128 bitand 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
627   >r bitor r> set-cpu-a ;
628
629 : (emulate-RRA) ( cpu -- )  
630   #! The content of the accumulator is rotated right
631   #! one position through the carry flag. The high order
632   #! bit is set to the carry flag and the carry flag is
633   #! set to the value shifd out of the low order bit. 
634   #! Only the carry flag is affected.
635   [ carry-flag swap flag-set? [ BIN: 10000000 ] [ 0 ] if ] keep 
636   [ cpu-a 254 bitand -1 shift ] keep 
637   dup cpu-a 1 bitand 0 = [ dup carry-flag clear-flag ] [ dup carry-flag set-flag ] if
638   >r bitor r> set-cpu-a ;
639
640 : (emulate-CPL) ( cpu -- )  
641   #! The contents of the accumulator are complemented
642   #! (zero bits become one, one bits becomes zero).
643   #! No flags are affected.
644   HEX: FF swap cpu-a-bitxor= ;
645
646 : (emulate-DAA) ( cpu -- )  
647   #! The eight bit number in the accumulator is
648   #! adjusted to form two four-bit binary-coded-decimal
649   #! digits.
650   [
651     dup half-carry-flag swap flag-set? swap 
652     cpu-a BIN: 1111 bitand 9 > or [ 6 ] [ 0 ] if 
653   ] keep 
654   [ cpu-a + ] keep
655   [ update-flags ] 2keep  
656   [ swap HEX: FF bitand swap set-cpu-a ] keep 
657   [
658     dup carry-flag swap flag-set? swap 
659     cpu-a -4 shift BIN: 1111 bitand 9 > or [ 96 ] [ 0 ] if 
660   ] keep 
661   [ cpu-a + ] keep
662   [ update-flags ] 2keep  
663   swap HEX: FF bitand swap set-cpu-a ;
664   
665 : patterns ( -- hashtable )
666   #! table of code quotation patterns for each type of instruction.
667   H{
668     { "NOP"          [ drop ]               }
669     { "RET-NN"          [ ret-from-sub  ]               }
670     { "RST-0"      [ 0 swap (emulate-RST) ] }
671     { "RST-8"      [ 8 swap (emulate-RST) ] }
672     { "RST-10H"      [ HEX: 10 swap (emulate-RST) ] }
673     { "RST-18H"      [ HEX: 18 swap (emulate-RST) ] }
674     { "RST-20H"      [ HEX: 20 swap (emulate-RST) ] }
675     { "RST-28H"      [ HEX: 28 swap (emulate-RST) ] }
676     { "RST-30H"      [ HEX: 30 swap (emulate-RST) ] }
677     { "RST-38H"      [ HEX: 38 swap (emulate-RST) ] }
678     { "RET-F|FF"      [ dup $1 [ 6 over inc-cycles ret-from-sub ] [ drop ] if ] }
679     { "CP-N"      [ [ cpu-a ] keep [ next-byte ] keep sub-byte drop ] }
680     { "CP-R"      [ [ cpu-a ] keep [ $1 ] keep sub-byte drop  ] }
681     { "CP-(RR)"      [ [ cpu-a ] keep [ $1 ] keep [ read-byte ] keep sub-byte drop ] }
682     { "OR-N"      [ [ cpu-a ] keep [ next-byte ] keep [ or-byte ] keep set-cpu-a ] }
683     { "OR-R"      [ [ cpu-a ] keep [ $1 ] keep [ or-byte ] keep set-cpu-a ] }
684     { "OR-(RR)"      [ [ cpu-a ] keep [ $1 ] keep [ read-byte ] keep [ or-byte ] keep set-cpu-a  ] }
685     { "XOR-N"      [ [ cpu-a ] keep [ next-byte ] keep [ xor-byte ] keep set-cpu-a ] }
686     { "XOR-R"      [ [ cpu-a ] keep [ $1 ] keep [ xor-byte ] keep set-cpu-a ] }
687     { "XOR-(RR)"   [ [ cpu-a ] keep [ $1 ] keep [ read-byte ] keep [ xor-byte ] keep set-cpu-a  ] }
688     { "AND-N"      [ [ cpu-a ] keep [ next-byte ] keep [ and-byte ] keep set-cpu-a  ] }
689     { "AND-R"      [ [ cpu-a ] keep [ $1 ] keep [ and-byte ] keep set-cpu-a ] }
690     { "AND-(RR)"      [ [ cpu-a ] keep [ $1 ] keep [ read-byte ] keep [ and-byte ] keep set-cpu-a  ] }
691     { "ADC-R,N"      [ [ $1 ] keep [ next-byte ] keep [ add-byte-with-carry ] keep $2 ] }
692     { "ADC-R,R"      [ [ $1 ] keep [ $3 ] keep [ add-byte-with-carry ] keep $2 ] }
693     { "ADC-R,(RR)"      [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ add-byte-with-carry ] keep $2 ] }
694     { "ADD-R,N"      [ [ $1 ] keep [ next-byte ] keep [ add-byte ] keep $2 ] }
695     { "ADD-R,R"      [ [ $1 ] keep [ $3 ] keep [ add-byte ] keep $2 ] }
696     { "ADD-RR,RR"    [ [ $1 ] keep [ $3 ] keep [ add-word ] keep $2 ] }
697     { "ADD-R,(RR)"    [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ add-byte ] keep $2   ]  }
698     { "SBC-R,N"      [ [ $1 ] keep [ next-byte ] keep [ sub-byte-with-carry ] keep $2 ] }
699     { "SBC-R,R"      [ [ $1 ] keep [ $3 ] keep [ sub-byte-with-carry ] keep $2 ] }
700     { "SBC-R,(RR)"      [ [ $1 ] keep [ $3 ] keep [ read-byte ] keep [ sub-byte-with-carry ] keep $2 ] }
701     { "SUB-R"      [ [ cpu-a ] keep [ $1 ] keep [ sub-byte ] keep set-cpu-a ] }
702     { "SUB-(RR)"      [ [ cpu-a ] keep [ $1 ] keep [ read-byte ] keep [ sub-byte ] keep set-cpu-a ] }
703     { "SUB-N"      [ [ cpu-a ] keep [ next-byte ] keep [ sub-byte ] keep set-cpu-a ] }
704     { "CPL"          [ (emulate-CPL) ]               }
705     { "DAA"          [ (emulate-DAA) ]               }
706     { "RLA"          [ (emulate-RLA) ]               }
707     { "RRA"          [ (emulate-RRA) ]               }
708     { "CCF"          [ carry-flag swap cpu-f-bitxor= ]               }
709     { "SCF"          [ carry-flag swap cpu-f-bitor= ]               }
710     { "RLCA"          [ (emulate-RLCA) ]               }
711     { "RRCA"          [ (emulate-RRCA) ]               }
712     { "HALT"          [ drop  ]               }
713     { "DI"          [ [ 255 interrupt-flag - ] swap cpu-f-bitand  ]               }
714     { "EI"          [ [ interrupt-flag ] swap cpu-f-bitor  ]  }  
715     { "POP-RR"     [ [ pop-sp ] keep $2 ] }
716     { "PUSH-RR"     [ [ $1 ] keep push-sp ] }
717     { "INC-R"     [ [ $1 ] keep [ inc-byte ] keep $2 ] }
718     { "DEC-R"     [ [ $1 ] keep [ dec-byte ] keep $2 ] }
719     { "INC-RR"     [ [ $1 ] keep [ inc-word ] keep $2 ] }
720     { "DEC-RR"     [ [ $1 ] keep [ dec-word ] keep $2 ] }
721     { "DEC-(RR)"     [ [ $1 ] keep [ read-byte ] keep [ dec-byte ] keep [ $1 ] keep write-byte ] }
722     { "INC-(RR)" [ [ $1 ] keep [ read-byte ] keep [ inc-byte ] keep  [ $1 ] keep write-byte ] }
723     { "JP-NN"           [ [ cpu-pc ] keep [ read-word ] keep set-cpu-pc ]               }
724     { "JP-F|FF,NN"      [ [ $1 ] keep swap [ [ next-word ] keep [ set-cpu-pc ] keep [ cpu-cycles ] keep swap 5 + swap set-cpu-cycles ] [ [ cpu-pc 2 + ] keep set-cpu-pc ] if ] }
725     { "JP-(RR)"      [ [ $1 ] keep set-cpu-pc ] }
726     { "CALL-NN"         [ (emulate-CALL) ] }
727     { "CALL-F|FF,NN"    [ [ $1 ] keep swap [ 7 over inc-cycles (emulate-CALL) ] [ [ cpu-pc 2 + ] keep set-cpu-pc ] if ]   }
728     { "LD-RR,NN"     [ [ next-word ] keep $2 ] }
729     { "LD-RR,RR"     [ [ $3 ] keep $2 ] }
730     { "LD-R,N"     [ [ next-byte ] keep $2 ] }
731     { "LD-(RR),N"    [ [ next-byte ] keep [ $1 ] keep write-byte ] }
732     { "LD-(RR),R"    [ [ $3 ] keep [ $1 ] keep write-byte ] }
733     { "LD-R,R"    [ [ $3 ] keep $2 ] }
734     { "LD-R,(RR)"    [ [ $3 ] keep [ read-byte ] keep $2  ] }
735     { "LD-(NN),RR"    [ [ $1 ] keep [ next-word ] keep write-word ] }
736     { "LD-(NN),R"    [  [ $1 ] keep [ next-word ] keep write-byte ] }
737     { "LD-RR,(NN)"    [ [ next-word ] keep [ read-word ] keep $2 ]  }
738     { "LD-R,(NN)"    [ [ next-word ] keep [ read-byte ] keep $2 ] }
739     { "OUT-(N),R"    [ [ $1 ] keep [ next-byte ] keep write-port ] }
740     { "IN-R,(N)"    [ [ next-byte ] keep [ read-port ] keep set-cpu-a ] }
741     { "EX-(RR),RR"  [  [ $1 ] keep [ read-word ] keep [ $3 ] keep [ $1 ] keep [ write-word ] keep $4 ] }
742     { "EX-RR,RR"    [ [ $1 ] keep [ $3 ] keep [ $2 ] keep $4 ] }
743   } ;
744
745 : 8-bit-registers ( -- parser )
746   #! A parser for 8-bit registers. On a successfull parse the
747   #! parse tree contains a vector. The first item in the vector
748   #! is the getter word for that register with stack effect
749   #! ( cpu -- value ). The second item is the setter word with
750   #! stack effect ( value cpu -- ).
751   "A" token 
752   "B" token  <|>
753   "C" token  <|>
754   "D" token  <|>
755   "E" token  <|>
756   "H" token  <|>
757   "L" token  <|> [ register-lookup ] <@ ;
758
759 : all-flags ( -- parser )
760   #! A parser for 16-bit flags. 
761   "NZ" token  
762   "NC" token <|>
763   "PO" token <|>
764   "PE" token <|> 
765   "Z" token <|> 
766   "C" token <|> 
767   "P" token <|> 
768   "M" token <|> [ flag-lookup ] <@ ;
769
770 : 16-bit-registers ( -- parser )
771   #! A parser for 16-bit registers. On a successfull parse the
772   #! parse tree contains a vector. The first item in the vector
773   #! is the getter word for that register with stack effect
774   #! ( cpu -- value ). The second item is the setter word with
775   #! stack effect ( value cpu -- ).
776   "AF" token  
777   "BC" token <|>
778   "DE" token <|>
779   "HL" token <|>
780   "SP" token <|> [ register-lookup ] <@ ;
781
782 : all-registers ( -- parser )
783   #! Return a parser that can parse the format
784   #! for 8 bit or 16 bit registers. 
785   8-bit-registers 16-bit-registers <|> ;
786
787 : indirect ( parser -- parser )
788   #! Given a parser, return a parser which parses the original
789   #! wrapped in brackets, representing an indirect reference.
790   #! eg. BC -> (BC). The value of the original parser is left in
791   #! the parse tree.
792   "(" token swap &> ")" token <& ;
793
794 : generate-instruction ( vector string -- quot )
795   #! Generate the quotation for an instruction, given the instruction in 
796   #! the 'string' and a vector containing the arguments for that instruction.
797   patterns at replace-patterns ;
798
799 : simple-instruction ( token -- parser )
800   #! Return a parser for then instruction identified by the token. 
801   #! The parser return parses the token only and expects no additional
802   #! arguments to the instruction.
803   token [ [ { } clone , , \ generate-instruction , ] [ ] make ] <@ ;
804
805 : complex-instruction ( type token -- parser )
806   #! Return a parser for an instruction identified by the token. 
807   #! The instruction is expected to take additional arguments by 
808   #! being combined with other parsers. Then 'type' is used for a lookup
809   #! in a pattern hashtable to return the instruction quotation pattern.
810   token swap [ nip [ , \ generate-instruction , ] [ ] make ] curry <@ ;
811
812 : NOP-instruction ( -- parser )
813   "NOP" simple-instruction ;
814
815 : RET-NN-instruction ( -- parser )  
816   "RET-NN" "RET" complex-instruction  
817   "nn" token sp <&
818   just [ { } clone swap curry  ] <@ ;
819
820 : RST-0-instruction ( -- parser )  
821   "RST-0" "RST" complex-instruction  
822   "0" token sp <&
823   just [ { } clone swap curry  ] <@ ;
824
825 : RST-8-instruction ( -- parser )  
826   "RST-8" "RST" complex-instruction  
827   "8" token sp <&
828   just [ { } clone swap curry  ] <@ ;
829
830 : RST-10H-instruction ( -- parser )  
831   "RST-10H" "RST" complex-instruction  
832   "10H" token sp <&
833   just [ { } clone swap curry  ] <@ ;
834
835 : RST-18H-instruction ( -- parser )  
836   "RST-18H" "RST" complex-instruction  
837   "18H" token sp <&
838   just [ { } clone swap curry  ] <@ ;
839
840 : RST-20H-instruction ( -- parser )  
841   "RST-20H" "RST" complex-instruction  
842   "20H" token sp <&
843   just [ { } clone swap curry  ] <@ ;
844
845 : RST-28H-instruction ( -- parser )  
846   "RST-28H" "RST" complex-instruction  
847   "28H" token sp <&
848   just [ { } clone swap curry  ] <@ ;
849
850 : RST-30H-instruction ( -- parser )  
851   "RST-30H" "RST" complex-instruction  
852   "30H" token sp <&
853   just [ { } clone swap curry  ] <@ ;
854
855 : RST-38H-instruction ( -- parser )  
856   "RST-38H" "RST" complex-instruction  
857   "38H" token sp <&
858   just [ { } clone swap curry  ] <@ ;
859
860 : JP-NN-instruction ( -- parser )  
861   "JP-NN" "JP" complex-instruction  
862   "nn" token sp <&
863   just [ { } clone swap curry  ] <@ ;
864
865 : JP-F|FF,NN-instruction ( -- parser )
866   "JP-F|FF,NN" "JP" complex-instruction  
867   all-flags sp <&> 
868   ",nn" token <&
869   just [ first2 swap curry ] <@ ;
870
871 : JP-(RR)-instruction ( -- parser )
872   "JP-(RR)" "JP" complex-instruction  
873   16-bit-registers indirect sp <&>
874   just [ first2 swap curry ] <@ ;
875
876 : CALL-NN-instruction ( -- parser )  
877   "CALL-NN" "CALL" complex-instruction  
878   "nn" token sp <&
879   just [ { } clone swap curry  ] <@ ;
880
881 : CALL-F|FF,NN-instruction ( -- parser )
882   "CALL-F|FF,NN" "CALL" complex-instruction  
883   all-flags sp <&> 
884   ",nn" token <&
885   just [ first2 swap curry ] <@ ;
886
887 : RLCA-instruction ( -- parser )
888   "RLCA" simple-instruction ;
889
890 : RRCA-instruction ( -- parser )
891   "RRCA" simple-instruction ;
892
893 : HALT-instruction ( -- parser )
894   "HALT" simple-instruction ;
895
896 : DI-instruction ( -- parser )
897   "DI" simple-instruction ;
898
899 : EI-instruction ( -- parser )
900   "EI" simple-instruction ;
901
902 : CPL-instruction ( -- parser )
903   "CPL" simple-instruction ;
904
905 : CCF-instruction ( -- parser )
906   "CCF" simple-instruction ;
907
908 : SCF-instruction ( -- parser )
909   "SCF" simple-instruction ;
910
911 : DAA-instruction ( -- parser )
912   "DAA" simple-instruction ;
913
914 : RLA-instruction ( -- parser )
915   "RLA" simple-instruction ;
916
917 : RRA-instruction ( -- parser )
918   "RRA" simple-instruction ;
919
920 : DEC-R-instruction ( -- parser )
921   "DEC-R" "DEC" complex-instruction  8-bit-registers sp <&> 
922   just [ first2 swap curry ] <@ ;
923
924 : DEC-RR-instruction ( -- parser )
925   "DEC-RR" "DEC" complex-instruction  16-bit-registers sp <&> 
926   just [ first2 swap curry ] <@ ;
927
928 : DEC-(RR)-instruction ( -- parser )
929   "DEC-(RR)" "DEC" complex-instruction  
930   16-bit-registers indirect sp <&>
931   just [ first2 swap curry ] <@ ;
932
933 : POP-RR-instruction ( -- parser )
934   "POP-RR" "POP" complex-instruction  all-registers sp <&> 
935   just [ first2 swap curry ] <@ ;
936
937 : PUSH-RR-instruction ( -- parser )
938   "PUSH-RR" "PUSH" complex-instruction  all-registers sp <&> 
939   just [ first2 swap curry ] <@ ;
940
941 : INC-R-instruction ( -- parser )
942   "INC-R" "INC" complex-instruction  8-bit-registers sp <&> 
943   just [ first2 swap curry ] <@ ;
944
945 : INC-RR-instruction ( -- parser )
946   "INC-RR" "INC" complex-instruction  16-bit-registers sp <&> 
947   just [ first2 swap curry ] <@ ;
948    
949 : INC-(RR)-instruction  ( -- parser )
950   "INC-(RR)" "INC" complex-instruction
951   all-registers indirect sp <&> just [ first2 swap curry ] <@ ;
952
953 : RET-F|FF-instruction ( -- parser )
954   "RET-F|FF" "RET" complex-instruction  all-flags sp <&> 
955   just [ first2 swap curry ] <@ ;
956
957 : AND-N-instruction ( -- parser )
958   "AND-N" "AND" complex-instruction
959   "n" token sp <&
960   just [ { } clone swap curry  ] <@ ;
961
962 : AND-R-instruction  ( -- parser )
963   "AND-R" "AND" complex-instruction
964   8-bit-registers sp <&> just [ first2 swap curry ] <@ ;
965
966 : AND-(RR)-instruction  ( -- parser )
967   "AND-(RR)" "AND" complex-instruction
968   16-bit-registers indirect sp <&> just [ first2 swap curry ] <@ ;
969
970 : XOR-N-instruction ( -- parser )
971   "XOR-N" "XOR" complex-instruction
972   "n" token sp <&
973   just [ { } clone swap curry  ] <@ ;
974
975 : XOR-R-instruction  ( -- parser )
976   "XOR-R" "XOR" complex-instruction
977   8-bit-registers sp <&> just [ first2 swap curry ] <@ ;
978
979 : XOR-(RR)-instruction  ( -- parser )
980   "XOR-(RR)" "XOR" complex-instruction
981   16-bit-registers indirect sp <&> just [ first2 swap curry ] <@ ;
982
983 : OR-N-instruction ( -- parser )
984   "OR-N" "OR" complex-instruction
985   "n" token sp <&
986   just [ { } clone swap curry  ] <@ ;
987
988 : OR-R-instruction  ( -- parser )
989   "OR-R" "OR" complex-instruction
990   8-bit-registers sp <&> just [ first2 swap curry ] <@ ;
991
992 : OR-(RR)-instruction  ( -- parser )
993   "OR-(RR)" "OR" complex-instruction
994   16-bit-registers indirect sp <&> just [ first2 swap curry ] <@ ;
995
996 : CP-N-instruction ( -- parser )
997   "CP-N" "CP" complex-instruction
998   "n" token sp <&
999   just [ { } clone swap curry  ] <@ ;
1000
1001 : CP-R-instruction  ( -- parser )
1002   "CP-R" "CP" complex-instruction
1003   8-bit-registers sp <&> just [ first2 swap curry ] <@ ;
1004
1005 : CP-(RR)-instruction  ( -- parser )
1006   "CP-(RR)" "CP" complex-instruction
1007   16-bit-registers indirect sp <&> just [ first2 swap curry ] <@ ;
1008
1009 : ADC-R,N-instruction ( -- parser )
1010   "ADC-R,N" "ADC" complex-instruction
1011   8-bit-registers sp <&>
1012   ",n" token <& 
1013   just [ first2 swap curry ] <@ ;  
1014
1015 : ADC-R,R-instruction ( -- parser )
1016   "ADC-R,R" "ADC" complex-instruction
1017   8-bit-registers sp <&>
1018   "," token <& 
1019   8-bit-registers <&>
1020   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1021
1022 : ADC-R,(RR)-instruction ( -- parser )
1023   "ADC-R,(RR)" "ADC" complex-instruction
1024   8-bit-registers sp <&>
1025   "," token <& 
1026   16-bit-registers indirect <&>
1027   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1028
1029 : SBC-R,N-instruction ( -- parser )
1030   "SBC-R,N" "SBC" complex-instruction
1031   8-bit-registers sp <&>
1032   ",n" token <& 
1033   just [ first2 swap curry ] <@ ;  
1034
1035 : SBC-R,R-instruction ( -- parser )
1036   "SBC-R,R" "SBC" complex-instruction
1037   8-bit-registers sp <&>
1038   "," token <& 
1039   8-bit-registers <&>
1040   just [ first2 swap first2 swap >r prepend r> curry  ] <@ ;  
1041
1042 : SBC-R,(RR)-instruction ( -- parser )
1043   "SBC-R,(RR)" "SBC" complex-instruction
1044   8-bit-registers sp <&>
1045   "," token <& 
1046   16-bit-registers indirect  <&>
1047   just [ first2 swap first2 swap >r prepend r> curry  ] <@ ;  
1048
1049 : SUB-R-instruction ( -- parser )
1050   "SUB-R" "SUB" complex-instruction
1051   8-bit-registers sp <&>
1052   just [ first2 swap curry ] <@ ;  
1053
1054 : SUB-(RR)-instruction ( -- parser )
1055   "SUB-(RR)" "SUB" complex-instruction
1056   16-bit-registers indirect sp <&>
1057   just [ first2 swap curry ] <@ ;  
1058
1059 : SUB-N-instruction ( -- parser )
1060   "SUB-N" "SUB" complex-instruction
1061   "n" token sp <&
1062   just [ { } clone swap curry  ] <@ ;
1063
1064 : ADD-R,N-instruction ( -- parser )
1065   "ADD-R,N" "ADD" complex-instruction
1066   8-bit-registers sp <&>
1067   ",n" token <& 
1068   just [ first2 swap curry ] <@ ;  
1069
1070 : ADD-R,R-instruction ( -- parser )
1071   "ADD-R,R" "ADD" complex-instruction
1072   8-bit-registers sp <&>
1073   "," token <& 
1074   8-bit-registers <&>
1075   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1076
1077 : ADD-RR,RR-instruction ( -- parser )
1078   "ADD-RR,RR" "ADD" complex-instruction
1079   16-bit-registers sp <&>
1080   "," token <& 
1081   16-bit-registers <&>
1082   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1083
1084 : ADD-R,(RR)-instruction ( -- parser )
1085   "ADD-R,(RR)" "ADD" complex-instruction
1086   8-bit-registers sp <&>
1087   "," token <& 
1088   16-bit-registers indirect <&>
1089   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1090   
1091 : LD-RR,NN-instruction ( -- parser )
1092   #! LD BC,nn
1093   "LD-RR,NN" "LD" complex-instruction
1094   16-bit-registers sp <&>
1095   ",nn" token <& 
1096   just [ first2 swap curry ] <@ ;
1097
1098 : LD-R,N-instruction ( -- parser )
1099   #! LD B,n
1100   "LD-R,N" "LD" complex-instruction
1101   8-bit-registers sp <&>
1102   ",n" token <& 
1103   just [ first2 swap curry ] <@ ;
1104   
1105 : LD-(RR),N-instruction ( -- parser )
1106   "LD-(RR),N" "LD" complex-instruction
1107   16-bit-registers indirect sp <&> 
1108   ",n" token <&
1109   just [ first2 swap curry ] <@ ;
1110
1111 : LD-(RR),R-instruction ( -- parser )
1112   #! LD (BC),A
1113   "LD-(RR),R" "LD" complex-instruction
1114   16-bit-registers indirect sp <&> 
1115   "," token <&
1116   8-bit-registers <&>
1117   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1118
1119 : LD-R,R-instruction ( -- parser )
1120   "LD-R,R" "LD" complex-instruction
1121   8-bit-registers sp <&> 
1122   "," token <&
1123   8-bit-registers <&>
1124   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1125
1126 : LD-RR,RR-instruction ( -- parser )
1127   "LD-RR,RR" "LD" complex-instruction
1128   16-bit-registers sp <&> 
1129   "," token <&
1130   16-bit-registers <&>
1131   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1132
1133 : LD-R,(RR)-instruction ( -- parser )
1134   "LD-R,(RR)" "LD" complex-instruction
1135   8-bit-registers sp <&> 
1136   "," token <&
1137   16-bit-registers indirect <&>
1138   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1139
1140 : LD-(NN),RR-instruction ( -- parser )
1141   "LD-(NN),RR" "LD" complex-instruction
1142   "nn" token indirect sp <&
1143   "," token <&
1144   16-bit-registers <&>
1145   just [ first2 swap curry ] <@ ;
1146
1147 : LD-(NN),R-instruction ( -- parser )
1148   "LD-(NN),R" "LD" complex-instruction
1149   "nn" token indirect sp <&
1150   "," token <&
1151   8-bit-registers <&>
1152   just [ first2 swap curry ] <@ ;
1153
1154 : LD-RR,(NN)-instruction ( -- parser )
1155   "LD-RR,(NN)" "LD" complex-instruction
1156   16-bit-registers sp <&>
1157   "," token <&
1158   "nn" token indirect <&
1159   just [ first2 swap curry ] <@ ;
1160
1161 : LD-R,(NN)-instruction ( -- parser )
1162   "LD-R,(NN)" "LD" complex-instruction
1163   8-bit-registers sp <&>
1164   "," token <&
1165   "nn" token indirect <&
1166   just [ first2 swap curry ] <@ ;
1167
1168 : OUT-(N),R-instruction ( -- parser )
1169   "OUT-(N),R" "OUT" complex-instruction
1170   "n" token indirect sp <&
1171   "," token <&
1172   8-bit-registers <&>
1173   just [ first2 swap curry ] <@ ;
1174
1175 : IN-R,(N)-instruction ( -- parser )
1176   "IN-R,(N)" "IN" complex-instruction
1177   8-bit-registers sp <&>
1178   "," token <&
1179   "n" token indirect <&
1180   just [ first2 swap curry ] <@ ;
1181
1182 : EX-(RR),RR-instruction ( -- parser )
1183   "EX-(RR),RR" "EX" complex-instruction
1184   16-bit-registers indirect sp <&> 
1185   "," token <&
1186   16-bit-registers <&>
1187   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1188
1189 : EX-RR,RR-instruction ( -- parser )
1190   "EX-RR,RR" "EX" complex-instruction
1191   16-bit-registers sp <&> 
1192   "," token <&
1193   16-bit-registers <&>
1194   just [ first2 swap first2 swap >r prepend r> curry ] <@ ;  
1195
1196 : 8080-generator-parser ( -- parser )
1197   NOP-instruction 
1198   RST-0-instruction <|> 
1199   RST-8-instruction <|> 
1200   RST-10H-instruction <|> 
1201   RST-18H-instruction <|> 
1202   RST-20H-instruction <|> 
1203   RST-28H-instruction <|> 
1204   RST-30H-instruction <|> 
1205   RST-38H-instruction <|> 
1206   JP-F|FF,NN-instruction <|> 
1207   JP-NN-instruction <|> 
1208   JP-(RR)-instruction <|> 
1209   CALL-F|FF,NN-instruction <|> 
1210   CALL-NN-instruction <|> 
1211   CPL-instruction <|> 
1212   CCF-instruction <|> 
1213   SCF-instruction <|> 
1214   DAA-instruction <|> 
1215   RLA-instruction <|> 
1216   RRA-instruction <|> 
1217   RLCA-instruction <|> 
1218   RRCA-instruction <|> 
1219   HALT-instruction <|> 
1220   DI-instruction <|> 
1221   EI-instruction <|> 
1222   AND-N-instruction <|> 
1223   AND-R-instruction <|> 
1224   AND-(RR)-instruction <|> 
1225   XOR-N-instruction <|> 
1226   XOR-R-instruction <|> 
1227   XOR-(RR)-instruction <|> 
1228   OR-N-instruction <|> 
1229   OR-R-instruction <|> 
1230   OR-(RR)-instruction <|> 
1231   CP-N-instruction <|> 
1232   CP-R-instruction <|> 
1233   CP-(RR)-instruction <|> 
1234   DEC-RR-instruction <|> 
1235   DEC-R-instruction <|> 
1236   DEC-(RR)-instruction <|> 
1237   POP-RR-instruction <|> 
1238   PUSH-RR-instruction <|> 
1239   INC-RR-instruction <|> 
1240   INC-R-instruction <|> 
1241   INC-(RR)-instruction <|>
1242   LD-RR,NN-instruction <|> 
1243   LD-R,N-instruction <|> 
1244   LD-R,R-instruction <|> 
1245   LD-RR,RR-instruction <|> 
1246   LD-(RR),N-instruction <|> 
1247   LD-(RR),R-instruction <|> 
1248   LD-R,(RR)-instruction <|> 
1249   LD-(NN),RR-instruction <|> 
1250   LD-(NN),R-instruction <|> 
1251   LD-RR,(NN)-instruction <|> 
1252   LD-R,(NN)-instruction <|> 
1253   ADC-R,N-instruction <|> 
1254   ADC-R,R-instruction <|> 
1255   ADC-R,(RR)-instruction <|> 
1256   ADD-R,N-instruction <|> 
1257   ADD-R,R-instruction <|> 
1258   ADD-RR,RR-instruction <|> 
1259   ADD-R,(RR)-instruction <|> 
1260   SBC-R,N-instruction <|> 
1261   SBC-R,R-instruction <|> 
1262   SBC-R,(RR)-instruction <|> 
1263   SUB-R-instruction <|> 
1264   SUB-(RR)-instruction <|> 
1265   SUB-N-instruction <|> 
1266   RET-F|FF-instruction <|> 
1267   RET-NN-instruction <|>
1268   OUT-(N),R-instruction <|>
1269   IN-R,(N)-instruction <|>
1270   EX-(RR),RR-instruction <|>
1271   EX-RR,RR-instruction <|>
1272   just ;
1273
1274 : instruction-quotations ( string -- emulate-quot )
1275   #! Given an instruction string, return the emulation quotation for
1276   #! it. This will later be expanded to produce the disassembly and
1277   #! assembly quotations.
1278   8080-generator-parser some parse call ;
1279
1280 SYMBOL: last-instruction
1281 SYMBOL: last-opcode
1282
1283 : parse-instructions ( list -- emulate-quot )
1284   #! Process the list of strings, which should make
1285   #! up an 8080 instruction, and output a quotation
1286   #! that would implement that instruction.
1287   dup " " join instruction-quotations
1288   >r "_" join [ "emulate-" % % ] "" make create-in dup last-instruction global set-at  
1289   r> (( cpu -- )) define-declared ;
1290
1291 : INSTRUCTION: ";" parse-tokens parse-instructions ; parsing
1292
1293 : cycles ( -- )
1294   #! Set the number of cycles for the last instruction that was defined. 
1295   scan string>number last-opcode global at instruction-cycles set-nth ; parsing
1296
1297 : opcode ( -- )
1298   #! Set the opcode number for the last instruction that was defined.
1299   last-instruction global at 1quotation scan 16 base>
1300   dup last-opcode global set-at set-instruction ; parsing
1301