home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / dis5.seq < prev    next >
Text File  |  1990-12-17  |  9KB  |  349 lines

  1. \ 6805 Disassembler                                     Andrew McKewan
  2.  
  3.  
  4. ONLY FORTH ALSO DEFINITIONS DECIMAL
  5.  
  6. \ : .ID|          ( nfa -- )  \ Print without trailing space
  7. \                DUP 1+ DUP YC@ ROT YC@ 31 AND 0
  8. \                ?DO     DUP 127 AND FEMIT 128 AND
  9. \                        IF   ASCII _ 128 OR   ELSE  1+ DUP YC@  THEN
  10. \                LOOP    2DROP ;
  11.  
  12.  
  13. ASSEMBLER DEFINITIONS
  14. DEFER T@        FORTH ' @       ASSEMBLER IS T@
  15. FORTH DEFINITIONS
  16.  
  17.  
  18. VOCABULARY DIS6805  DIS6805 ALSO DEFINITIONS
  19.  
  20. \ Target Memory Access
  21. DEFER TC@       ASSEMBLER ' TC@      DIS6805 IS TC@
  22. \ DEFER T@        ASSEMBLER ' T@       DIS6805 IS T@
  23.  
  24. : T@  ( tadr -- n )  DUP 1+ TC@  SWAP TC@  JOIN ;
  25.  
  26. variable CP
  27. : nextb  ( -- b )  cp @ tc@  1 cp +! ;
  28. : nextw  ( -- w )  cp @ t@   2 cp +! ;
  29.  
  30.  
  31. \ Display hex object code
  32.  
  33. : ##    save> base hex  0 <# # #     #> type space  restore> base ;
  34. : ####  save> base hex  0 <# # # # # #> type space  restore> base ;
  35.  
  36. : H.    save> base hex  0 <# #S #> TYPE  restore> base ;
  37.  
  38. : bytes  ( tadr n -- tadr )
  39.    2 spaces  over + over do i tc@ ## loop ; \ *** TCOM
  40. \   over #### space  over + over do i tc@ ## loop ;
  41.  
  42. : byte    1 bytes ;
  43. : 2bytes  2 bytes ;
  44. : 3bytes  3 bytes ;
  45.  
  46. : bit#  ( opcode -- bit# )  2/ 7 and ;
  47. : B>W  ( byte -- n )  DUP 128 AND IF 256 - THEN ;
  48.  
  49.  
  50. \ disassembly format:
  51. \ AAAA XX XX XX       OPC     OPR     COMMENT
  52.  
  53. : tab #out @ - spaces ;
  54. : opcode   20 tab ;
  55. : operand  28 tab ;
  56. : comment  36 tab ;
  57.  
  58. \ : op  create last @ ,  does> opcode @ .id|  1+ ;
  59.  
  60. : OP    >IN @ CREATE >IN ! BL WORD C@ 1+ ALLOT
  61.         DOES> OPCODE COUNT TYPE 1+ ;
  62. : opcodes  0 do  op  loop ;
  63.  
  64. 3 opcodes BRSET BSET BCLR
  65. 8 opcodes NEG COM LSR ROR ASR LSL ROL DEC
  66. 8 opcodes INC TST CLR SUB CMP SBC CPX AND
  67. 8 opcodes BIT LDA STA EOR ADC ORA ADD JMP
  68. 3 opcodes JSR LDX STX
  69.  
  70. \ operands                                            
  71. ( tadr -- tadr' )
  72. : dir   operand  dup tc@ h. 1+ ;
  73. : ext   operand  dup t@  h. 2+ ;
  74. : imm   operand  ." #" dir ;
  75. : ix    operand  ." ,X" ;
  76. : ix1   dir ." ,X" ;
  77. : ix2   ext ." ,X" ;
  78. : rel   operand  dup 1+ swap tc@ b>w over + h. ;
  79. : bsc   operand  dup 1- tc@ bit# h. ." ," dir ;
  80. : btb   bsc ." ," rel ;
  81. : inha  ." A" ;
  82. : inhx  ." X" ;
  83.  
  84. \ opcode table                                        
  85. : illegal  byte  opcode  ." ???" 1+ ;
  86.  
  87. create map 512 allot
  88. : >map 2* map + ;
  89. : init  256 0 do  ['] illegal i >map !  loop ;
  90. init forget init
  91.  
  92. : op:  ( n -- )
  93.    >map here swap !
  94.    233 c,  'docol here 2+ - ,
  95.    xhere paragraph + dup xdpseg !
  96.    xseg @ - ,  xdp off
  97.    !csp  ] ;
  98.  
  99. : %inst   ( tadr -- tadr' )
  100.    dup tc@ >map perform ;
  101.  
  102. : inst  cp @ %inst cp ! ;       \ *** TCOM
  103.  
  104.  
  105. \ ****** INSTRUCTIONS ******
  106.  
  107. \ bit instructions
  108.  
  109. $00 op: 3 bytes  brset btb ;
  110. $10 op: 2 bytes  bset  bsc ;
  111. $11 op: 2 bytes  bclr  bsc ;
  112.  
  113. : init 0 >map @  $10 1 do  dup i >map !  loop  drop ;
  114. init forget init
  115.  
  116. : init $10 >map 2@  $20 $12 do  2dup i >map 2!  2 +loop  2drop ;
  117. init forget init
  118.  
  119. \ branches                                            
  120. : branch  >r >r  2 bytes  r> r> opcode type  1+ rel ;
  121.  
  122. $20 op: " BRA"  branch ;        $21 op: " BRN"  branch ;
  123. $22 op: " BHI"  branch ;        $23 op: " BLS"  branch ;
  124. $24 op: " BCC"  branch ;        $25 op: " BCS"  branch ;
  125. $26 op: " BNE"  branch ;        $27 op: " BEQ"  branch ;
  126. $28 op: " BHCC" branch ;        $29 op: " BHCS" branch ;
  127. $2a op: " BPL"  branch ;        $2b op: " BMI"  branch ;
  128. $2c op: " BMC"  branch ;        $2d op: " BMS"  branch ;
  129. $2e op: " BIL"  branch ;        $2f op: " BIH"  branch ;
  130. $ad op: " BSR"  branch ;
  131.  
  132. \ Unary instructions
  133.  
  134. $30 op: 2bytes neg dir ;
  135. $40 op: byte   neg inha ;
  136. $50 op: byte   neg inhx ;
  137. $60 op: 2bytes neg ix1 ;
  138. $70 op: byte   neg ix ;
  139.  
  140. $33 op: 2bytes com dir ;
  141. $43 op: byte   com inha ;
  142. $53 op: byte   com inhx ;
  143. $63 op: 2bytes com ix1 ;
  144. $73 op: byte   com ix ;
  145.  
  146. $34 op: 2bytes lsr dir ;
  147. $44 op: byte   lsr inha ;
  148. $54 op: byte   lsr inhx ;
  149. $64 op: 2bytes lsr ix1 ;
  150. $74 op: byte   lsr ix ;
  151.  
  152. $36 op: 2bytes ror dir ;
  153. $46 op: byte   ror inha ;
  154. $56 op: byte   ror inhx ;
  155. $66 op: 2bytes ror ix1 ;
  156. $76 op: byte   ror ix ;
  157.  
  158. $37 op: 2bytes asr dir ;
  159. $47 op: byte   asr inha ;
  160. $57 op: byte   asr inhx ;
  161. $67 op: 2bytes asr ix1 ;
  162. $77 op: byte   asr ix ;
  163.  
  164. $38 op: 2bytes lsl dir ;
  165. $48 op: byte   lsl inha ;
  166. $58 op: byte   lsl inhx ;
  167. $68 op: 2bytes lsl ix1 ;
  168. $78 op: byte   lsl ix ;
  169.  
  170. $39 op: 2bytes rol dir ;
  171. $49 op: byte   rol inha ;
  172. $59 op: byte   rol inhx ;
  173. $69 op: 2bytes rol ix1 ;
  174. $79 op: byte   rol ix ;
  175.  
  176. $3a op: 2bytes dec dir ;
  177. $4a op: byte   dec inha ;
  178. $5a op: byte   dec inhx ;
  179. $6a op: 2bytes dec ix1 ;
  180. $7a op: byte   dec ix ;
  181.  
  182. $3c op: 2bytes inc dir ;
  183. $4c op: byte   inc inha ;
  184. $5c op: byte   inc inhx ;
  185. $6c op: 2bytes inc ix1 ;
  186. $7c op: byte   inc ix ;
  187.  
  188. $3d op: 2bytes tst dir ;
  189. $4d op: byte   tst inha ;
  190. $5d op: byte   tst inhx ;
  191. $6d op: 2bytes tst ix1 ;
  192. $7d op: byte   tst ix ;
  193.  
  194. $3f op: 2bytes clr dir ;
  195. $4f op: byte   clr inha ;
  196. $5f op: byte   clr inhx ;
  197. $6f op: 2bytes clr ix1 ;
  198. $7f op: byte   clr ix ;
  199.  
  200. \ Inherant instructions
  201.  
  202. : inherant  byte opcode 1+ ;
  203. $80 op: inherant ." RTI" ;
  204. $81 op: inherant ." RTS" ;
  205. $83 op: inherant ." SWI" ;
  206. $8e op: inherant ." STOP" ;
  207. $8f op: inherant ." WAIT" ;
  208.  
  209. $97 op: inherant ." TAX" ;
  210. $98 op: inherant ." CLC" ;
  211. $99 op: inherant ." SEC" ;
  212. $9a op: inherant ." CLI" ;
  213. $9b op: inherant ." SEI" ;
  214. $9c op: inherant ." RSP" ;
  215. $9d op: inherant ." NOP" ;
  216. $9f op: inherant ." TXA" ;
  217.  
  218. \ Binary instructions
  219.  
  220. $a0 op: 2bytes sub imm ;
  221. $b0 op: 2bytes sub dir ;
  222. $c0 op: 3bytes sub ext ;
  223. $d0 op: 3bytes sub ix2 ;
  224. $e0 op: 2bytes sub ix1 ;
  225. $f0 op: byte   sub ix ;
  226.  
  227. $a1 op: 2bytes cmp imm ;
  228. $b1 op: 2bytes cmp dir ;
  229. $c1 op: 3bytes cmp ext ;
  230. $d1 op: 3bytes cmp ix2 ;
  231. $e1 op: 2bytes cmp ix1 ;
  232. $f1 op: byte   cmp ix ;
  233.  
  234. $a2 op: 2bytes sbc imm ;
  235. $b2 op: 2bytes sbc dir ;
  236. $c2 op: 3bytes sbc ext ;
  237. $d2 op: 3bytes sbc ix2 ;
  238. $e2 op: 2bytes sbc ix1 ;
  239. $f2 op: byte   sbc ix ;
  240.  
  241. $a3 op: 2bytes cpx imm ;
  242. $b3 op: 2bytes cpx dir ;
  243. $c3 op: 3bytes cpx ext ;
  244. $d3 op: 3bytes cpx ix2 ;
  245. $e3 op: 2bytes cpx ix1 ;
  246. $f3 op: byte   cpx ix ;
  247.  
  248. $a4 op: 2bytes and imm ;
  249. $b4 op: 2bytes and dir ;
  250. $c4 op: 3bytes and ext ;
  251. $d4 op: 3bytes and ix2 ;
  252. $e4 op: 2bytes and ix1 ;
  253. $f4 op: byte   and ix ;
  254.  
  255. $a5 op: 2bytes bit imm ;
  256. $b5 op: 2bytes bit dir ;
  257. $c5 op: 3bytes bit ext ;
  258. $d5 op: 3bytes bit ix2 ;
  259. $e5 op: 2bytes bit ix1 ;
  260. $f5 op: byte   bit ix ;
  261.  
  262. $a6 op: 2bytes lda imm ;
  263. $b6 op: 2bytes lda dir ;
  264. $c6 op: 3bytes lda ext ;
  265. $d6 op: 3bytes lda ix2 ;
  266. $e6 op: 2bytes lda ix1 ;
  267. $f6 op: byte   lda ix ;
  268.  
  269. \ $a7 op: 2bytes sta imm ;
  270. $b7 op: 2bytes sta dir ;
  271. $c7 op: 3bytes sta ext ;
  272. $d7 op: 3bytes sta ix2 ;
  273. $e7 op: 2bytes sta ix1 ;
  274. $f7 op: byte   sta ix ;
  275.  
  276. $a8 op: 2bytes eor imm ;
  277. $b8 op: 2bytes eor dir ;
  278. $c8 op: 3bytes eor ext ;
  279. $d8 op: 3bytes eor ix2 ;
  280. $e8 op: 2bytes eor ix1 ;
  281. $f8 op: byte   eor ix ;
  282.  
  283. $a9 op: 2bytes adc imm ;
  284. $b9 op: 2bytes adc dir ;
  285. $c9 op: 3bytes adc ext ;
  286. $d9 op: 3bytes adc ix2 ;
  287. $e9 op: 2bytes adc ix1 ;
  288. $f9 op: byte   adc ix ;
  289.  
  290. $aa op: 2bytes ora imm ;
  291. $ba op: 2bytes ora dir ;
  292. $ca op: 3bytes ora ext ;
  293. $da op: 3bytes ora ix2 ;
  294. $ea op: 2bytes ora ix1 ;
  295. $fa op: byte   ora ix ;
  296.  
  297. $ab op: 2bytes add imm ;
  298. $bb op: 2bytes add dir ;
  299. $cb op: 3bytes add ext ;
  300. $db op: 3bytes add ix2 ;
  301. $eb op: 2bytes add ix1 ;
  302. $fb op: byte   add ix ;
  303.  
  304. \ $ac op: 2bytes jmp imm ;
  305. $bc op: 2bytes jmp dir ;
  306. $cc op: 3bytes jmp ext ;
  307. $dc op: 3bytes jmp ix2 ;
  308. $ec op: 2bytes jmp ix1 ;
  309. $fc op: byte   jmp ix ;
  310.  
  311. \ $ad op: 2bytes jsr imm ;
  312. $bd op: 2bytes jsr dir ;
  313. $cd op: 3bytes jsr ext ;
  314. $dd op: 3bytes jsr ix2 ;
  315. $ed op: 2bytes jsr ix1 ;
  316. $fd op: byte   jsr ix ;
  317.  
  318. $ae op: 2bytes ldx imm ;
  319. $be op: 2bytes ldx dir ;
  320. $ce op: 3bytes ldx ext ;
  321. $de op: 3bytes ldx ix2 ;
  322. $ee op: 2bytes ldx ix1 ;
  323. $fe op: byte   ldx ix ;
  324.  
  325. \ $af op: 2bytes stx imm ;
  326. $bf op: 2bytes stx dir ;
  327. $cf op: 3bytes stx ext ;
  328. $df op: 3bytes stx ix2 ;
  329. $ef op: 2bytes stx ix1 ;
  330. $ff op: byte   stx ix ;
  331.  
  332.  
  333. FORTH DEFINITIONS
  334. : wait          key 27 = abort"  OK" ;
  335. : start/stop    key? if wait wait then ;
  336.  
  337. : ndis  ( tadr #inst -- )
  338.         0 do  start/stop  cr %inst  loop drop ;
  339.  
  340. : (dis) ( tadr -- )
  341.         0 swap
  342.         begin   cr 1 0 d+ over 10 mod 0=
  343.                 if ." More..." wait 7 backspaces then
  344.                 dup %inst swap tc@ $81 =
  345.         until   2drop ;
  346.  
  347. ONLY FORTH ALSO DEFINITIONS
  348.  
  349.