1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Copyright
(C
) 2020 Ketmar Dark
// Invisible Vector
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10 defer
-not
-yet peek
( addr
-- byte
)
16 0 value
(ixiy
) \ contains
0, [char
] x or
[char
] y
23 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 0x00 c
, 0x00 c
, 0x00 c
, 0x00 c
,
26 0x00 c
, 0x00 c
, 0x70 c
, 0x00 c
,
27 0x40 c
, 0x40 c
, 0x40 c
, 0x40 c
,
28 0x40 c
, 0x40 c
, 0xBF c
, 0x40 c
,
29 0x40 c
, 0x40 c
, 0x40 c
, 0x40 c
,
30 0x40 c
, 0x40 c
, 0x40 c
, 0x40 c
,
31 0x00 c
, 0x08 c
, 0x00 c
, 0x00 c
,
32 0x00 c
, 0x00 c
, 0x00 c
, 0x00 c
,
36 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 : get
-dis
-str
( addr count
-- ) (dptr
) (dofs
) ; (public
)
44 : put
-str
( addr len
-- )
45 dup
+if over
+ swap
do i c@ put
-char loop
else 2drop
endif
48 : put
-space
( -- ) bl put
-char
;
49 : put
-comma
( -- ) [char
] , put
-char
;
50 : put
-lpar
( -- ) [char
] ( put
-char
; ;; )
51 : put
-rpar
( -- ) [char
] ) put
-char
;
54 : put
-str
-4 ( idx addr count
-- ) drop
+ 4 put
-str
;
55 : put
-str
-2x
( addr
-- ) 2 put
-str
;
56 : put
-str
-4x
( addr
-- ) dup
3 + c@ bl
= if 3 else 4 endif put
-str
;
59 \
////////////////////////////////////////////////////////////////////////// //
63 >r hex
<#u r
> for # endfor
[char
] # hold
70 : put
-n8
( n
-- ) 0xff and
2 put
-n
.r
;
71 : put
-n16
( n
-- ) 0xffff and
4 put
-n
.r
;
74 (disp
) -if [char
] - else [char
] + endif put
-char
76 base @
>r decimal
<#u #s #
>
81 \
////////////////////////////////////////////////////////////////////////// //
85 pc
1+ 0xFFFF and
to pc
90 fetch
-byte fetch
-byte
8 lshift or
93 : byte
->signed
( b
-- n
) dup
0x80 >= if 0x100 - endif ;
96 \
////////////////////////////////////////////////////////////////////////// //
106 7 and
" bcdehl.a" drop
+ c@
109 drop
(ixiy
) if put
-ixy
-mem
else " (hl)" put
-str
endif
111 \ undocumented IX
/IY
8-bit part access
113 dup
[char
] h
= over
[char
] l
= or
if
123 : put
-r16
-hl
-ixy
( -- )
133 : put
-v16
( -- ) fetch
-word put
-n16
;
134 : put
-m16
( -- ) put
-lpar put
-v16 put
-rpar
;
136 : put
-r16
-common ( r16 addr count
-- )
137 drop swap
3 and
2u* +
138 dup c@
[char
] h
= if drop put
-r16
-hl
-ixy
else put
-str
-2x
endif
141 : put
-r16
-sp
( r16
-- )
143 " bcdehlsp" put
-r16
-common
146 : put
-r16
-af
( r16
-- )
148 " bcdehlaf" put
-r16
-common
153 7 and
2u* " nzz ncc popep m " drop
+
155 1+ c@ dup
32 <> if put
-char
else drop
endif
159 \
////////////////////////////////////////////////////////////////////////// //
160 : decode
-cb
-unixy
( -- )
161 \ special undocumented thing
163 \ `bit` doesn
't need undoc ixy
165 (opcode) 7 and 6 <> if
175 (opcode) 4 rshift 0x0c and 4- " bit res set " put-str-4
177 (opcode) 3 rshift 7 and [char] 0 + put-char
180 (opcode) 2u/ 0x1c and
181 " rlc rrc rl rr sla sra sll srl " put-str-4
189 \ ////////////////////////////////////////////////////////////////////////// //
190 : decode-ed-xrep ( -- )
191 \ two instructions with the wrong mnemonic length
192 (opcode) 0xa3 = if " outi" put-str exit endif
193 (opcode) 0xab = if " outd" put-str exit endif
195 (opcode) 3 and 2u* " ldcpinot" drop + put-str-2x
196 (opcode) 0x08 and if [char] d else [char] i endif put-char
197 (opcode) 0x10 and if [char] r put-char endif
200 : decode-ed-spec-im ( -- )
202 (opcode) 0x47 = if " 0/1" put-str exit endif
204 (opcode) 0x08 and if [char] 2 else [char] 1 endif
211 : decode-ed-spec-7 ( -- )
213 0x47 of " ld i,a" endof
214 0x4f of " ld r,a" endof
215 0x57 of " ld a,i" endof
216 0x5f of " ld a,r" endof
219 otherwise drop " nope"
224 : decode-ed-spec ( -- )
226 0x04 of " neg" put-str endof
227 0x05 of " ret" put-str (opcode) 0x08 and if [char] i else [char] n endif put-char endof
228 0x06 of decode-ed-spec-im endof
229 0x07 of decode-ed-spec-7 endof
234 : decode-ed-spec-r16 ( -- )
251 (opcode) 2u/ 4 and " sbc adc " put-str-4
257 : decode-ed-spec-in/out ( -- )
261 \ check for `(hl)`, it is special here
263 drop [char] 0 put-char
270 \ check for `(hl)`, it is special here
283 (opcode) 0xa4 and 0xa0 = if decode-ed-xrep exit endif
284 (opcode) 0xc0 and 0x40 <> if " nope" put-str exit endif
286 0x04 and-of decode-ed-spec endof
287 0x02 and-of decode-ed-spec-r16 endof
288 decode-ed-spec-in/out
293 \ ////////////////////////////////////////////////////////////////////////// //
294 \ ld r8,r8 (and halt)
295 : decode-norm-grp1 ( -- )
296 (opcode) 0x76 = if " halt" put-str exit endif
298 (opcode) 3 rshift put-r8
303 : decode-alu-name ( -- )
304 (opcode) 2u/ 0x1c and " add adc sub sbc and xor or cp " put-str-4
306 \ two special opcodes
307 (opcode) 0x38 and dup 0x08 = over 0x18 = or swap 0x00 = or if " a," put-str endif
311 : decode-norm-grp3-1 ( -- )
313 (opcode) 0x30 and case
314 0x00 of " ret" put-str endof
315 0x10 of " exx" put-str endof
316 0x20 of " jp (" put-str put-r16-hl-ixy put-rpar endof
317 0x30 of " ld sp," put-str put-r16-hl-ixy endof
325 : decode-norm-grp3-3 ( -- )
326 (opcode) 0x38 and case
327 0x00 of " jp " put-str put-v16 endof
329 0x10 of " out (" put-str fetch-byte put-n8 " ),a" put-str endof
330 0x18 of " in a,(" put-str fetch-byte put-n8 put-rpar endof
331 0x20 of " ex (sp)," put-str put-r16-hl-ixy endof
332 0x28 of " ex de,hl" put-str endof
333 0x30 of " di" put-str endof
334 0x38 of " ei" put-str endof
338 : decode-norm-grp3-5 ( -- )
340 \ prefixes already done, so only call is left
349 \ call,ret,push,pop,etc.
350 : decode-norm-grp3 ( -- )
352 0x00 of " ret " put-str (opcode) 3 rshift put-cc endof
353 0x01 of decode-norm-grp3-1 endof
354 0x02 of " jp " put-str (opcode) 3 rshift put-cc put-comma put-v16 endof
355 0x03 of decode-norm-grp3-3 endof
356 0x04 of " call " put-str (opcode) 3 rshift put-cc put-comma put-v16 endof
357 0x05 of decode-norm-grp3-5 endof
358 0x06 of decode-alu-name fetch-byte put-n8 endof
359 0x07 of " rst " put-str (opcode) 0x38 and put-n8 endof
363 : decode-norm-grp0-0-add/ld ( -- )
377 : decode-norm-grp0-0-jr-cc ( -- )
379 (opcode) 3 rshift 3 and put-cc
381 fetch-byte dup 0x80 >= if 0x100 - endif
385 : decode-norm-grp0-0-jr-djnz ( -- )
386 (opcode) 2u/ 4 and " djnzjr " put-str-4
388 fetch-byte dup 0x80 >= if 0x100 - endif
392 : decode-norm-grp0-0 ( -- )
394 decode-norm-grp0-0-add/ld
397 0x20 and-of decode-norm-grp0-0-jr-cc endof
398 0x10 and-of decode-norm-grp0-0-jr-djnz endof
399 (opcode) 0x08 and if " ex af,af'" else " nop
" endif put-str
404 : decode-norm-grp0-2 ( -- )
406 (opcode) 2u/ 4 and " inc dec
" put-str-4
411 (opcode) 0x3c and case
412 0x00 of " (bc
),a
" put-str endof
413 0x08 of " a
,(bc
)" put-str endof
414 0x10 of " (de
),a
" put-str endof
415 0x18 of " a
,(de
)" put-str endof
416 0x20 of put-m16 put-comma put-r16-hl-ixy endof
417 0x28 of put-r16-hl-ixy put-comma put-m16 endof
418 0x30 of put-m16 " ,a
" put-str endof
419 0x38 of " a
," put-str put-m16 endof
424 : decode-norm-grp0-4 ( -- )
425 (opcode) 0x01 and 2 lshift " inc dec
" put-str-4
427 (opcode) 3 rshift put-r8
430 : decode-norm-grp0-6 ( -- )
432 (opcode) 2u/ 0x1c and " rlcarrcarla rra daa cpl scf ccf
" drop + put-str-4x
435 (opcode) 3 rshift put-r8
441 : decode-norm-grp0 ( -- )
442 (opcode) 0x06 and case
443 0x00 of decode-norm-grp0-0 endof
444 0x02 of decode-norm-grp0-2 endof
445 0x04 of decode-norm-grp0-4 endof
446 0x06 of decode-norm-grp0-6 endof
451 (opcode) 0xc0 and case
452 0x00 of decode-norm-grp0 endof
453 0x40 of decode-norm-grp1 endof
454 0x80 of decode-alu-name (opcode) put-r8 endof \ alu a,r8
460 \ ////////////////////////////////////////////////////////////////////////// //
462 ;; set "pc
"; will modify "pc
"
470 \ check if I<X|Y> prefix
471 dup 0xdd = if [char] x to (ixiy) endif
472 dup 0xfd = if [char] y to (ixiy) endif
474 drop fetch-byte dup to (opcode)
475 dup 0xdd = over 0xfd = or if
478 pc 1- 0xFFFF and to pc
481 \ check if we have disp here
482 dup 3 rshift ixydisp-table + c@
483 1 rot 7 and lshift and if
485 fetch-byte byte->signed to (disp)
492 0xcb of fetch-byte to (opcode) decode-cb endof
493 0xed of fetch-byte to (opcode) decode-ed endof