lr35902ish racket
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
9.2KB

  1. (define (alu-tab y)
  2. (case y
  3. [(0) 'RLC]
  4. [(1) 'RRC]
  5. [(2) 'RL]
  6. [(3) 'RR]
  7. [(4) 'SLA]
  8. [(5) 'SRA]
  9. [(6) 'SWAP]
  10. [(7) 'SRL]
  11. [else (error "unknown alu operation")]))
  12. (define (reg-tab z)
  13. (case z
  14. [(0) 'B]
  15. [(1) 'C]
  16. [(2) 'D]
  17. [(3) 'E]
  18. [(4) 'H]
  19. [(5) 'L]
  20. [(6) 'ind-HL]
  21. [(7) 'A]
  22. [else (error "unknown register index")]))
  23. (define (cc-tab z)
  24. (case z
  25. [(0) 'NZ]
  26. [(1) 'Z]
  27. [(2) 'NC]
  28. [(3) 'C]
  29. [else (error "unknown control index")]))
  30. (define (rp-tab y)
  31. (case
  32. [(0) 'BC]
  33. [(1) 'DE]
  34. [(2) 'HL]
  35. [(3) 'SP]
  36. [else (error "unknown register pair index")]))
  37. (define (rp2-tab y)
  38. (case
  39. [(0) 'BC]
  40. [(1) 'DE]
  41. [(2) 'HL]
  42. [(3) 'AF]
  43. [else (error "unknown register pair index")]))
  44. (define (non-prefix byte)
  45. (let ([x (arithmetic-shift (bitwise-and #b11000000 byte) -6)])
  46. (case x
  47. [(0) (dispatch-zero-x byte)]
  48. [(1) (dispatch-ld-r-r byte)]
  49. [(2) (dispatch-alu-x byte)]
  50. [(3) (dispatch-misc byte)]
  51. [else (error "malformed byte: " byte)])))
  52. (define (dispatch-misc byte)
  53. (let ([z (bitwise-and #b00000111 byte)])
  54. (case z
  55. [(0) (returns-and-misc byte)]
  56. [(1) (pop-ret-misc byte)]
  57. [(2) (cond-jps-and-misc-lds byte)]
  58. [(3) (uncond-jp-and-ints byte)]
  59. [(4) (cond-calls byte)]
  60. [(5) (push-and-call byte)]
  61. [(6) (imm-alu-ops byte)]
  62. [(7) (rst-vecs byte)])))
  63. (define (imm-alu-ops byte)
  64. (letrec ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)])
  65. (lambda x
  66. `(,(case y
  67. [(0) 'ADD]
  68. [(1) 'ADC]
  69. [(2) 'SUB]
  70. [(3) 'SBC]
  71. [(4) 'AND]
  72. [(5) 'XOR]
  73. [(6) 'OR]
  74. [(7) 'CP])
  75. ,x))))
  76. (define (rst-vecs byte)
  77. (letrec ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)])
  78. `(RST ,(* y 8))))
  79. (define (push-and-call byte)
  80. (letrec ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  81. [p (arithmetic-shift y -1)]
  82. [z (bitwise-and #b00000111 byte)])
  83. (case y
  84. [(0 1 2 3) `(PUSH ,(rp2-tab p))]
  85. [(4) (lambda y2 (lambda x `(CALL ,@(make-16b-addr x y2))))]
  86. [(5 6 7) '(REMOVED)])))
  87. (define (cond-calls byte)
  88. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  89. [z (bitwise-and #b00000111 byte)])
  90. (case y
  91. [(0 1 2 3) (lambda y2 (lambda x `(CALL ,(cc-tab y) ,(make-16b-addr x y2))))]
  92. [(4 5 6 7) '(REMOVED)])))
  93. (define (uncond-jp-and-ints byte)
  94. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  95. [z (bitwise-and #b00000111 byte)])
  96. (case y
  97. [(0) (lambda y (lambda x `(JP ,(make-16b-addr x y))))]
  98. [(1) '(CB-PREFIX)]
  99. [(2 3 4 5) '(REMOVED)]
  100. [(6) '(DI)]
  101. [(7) '(EI)])))
  102. (define (cond-jps-and-misc-lds byte)
  103. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  104. [z (bitwise-and #b00000111 byte)])
  105. (case y
  106. [(0 1 2 3) (lambda y2 (lambda x `(JP ,(cc-tab y) ,(make-16b-addr x y2))))]
  107. [(4) '(LD (ADD #xFF00 C) A)]
  108. [(5) (lambda y (lambda x `(LD ,(make-16b-addr x y) A)))]
  109. [(6) '(LD A (ADD #xFF00 C))]
  110. [(7) (lambda y (lambda x `(LD A ,(make-16b-addr x y))))])))
  111. (define (pop-ret-misc byte)
  112. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  113. [z (bitwise-and #b00000111 byte)])
  114. (case y
  115. [(0 1 2 3) `(POP ,(rp2-tab z))]
  116. [(4) '(RET)]
  117. [(5) '(RETI)]
  118. [(6) '(JP HL)]
  119. [(7) '(LD SP HL)])))
  120. (define (returns-and-misc byte)
  121. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  122. [z (bitwise-and #b00000111 byte)])
  123. (if (> y 4)
  124. `(RET ,(cc-tab y))
  125. (make-misc-ldh-and-sp (- y 4)))))
  126. (define (make-misc-ldh-and-sp y)
  127. (case y
  128. [(0) (lambda x `(LD ,(bitwise-ior #xFF00 x) A))]
  129. [(1) (lambda d `(ADD SP ,d))]
  130. [(2) (lambda x `(LD A ,(bitwise-ior #xFF00 x)))]
  131. [(3) (lambda d `(LD HL (ADD SP ,d)))]))
  132. (define (dispatch-alu-x byte)
  133. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  134. [z (bitwise-and #b00000111 byte)])
  135. (case y
  136. [(0) `(ADD ,(reg-tab z))]
  137. [(1) `(ADC ,(reg-tab z))]
  138. [(2) `(SUB ,(reg-tab z))]
  139. [(3) `(SBC ,(reg-tab z))]
  140. [(4) `(AND ,(reg-tab z))]
  141. [(5) `(XOR ,(reg-tab z))]
  142. [(6) `(OR ,(reg-tab z))]
  143. [(7) `(CP ,(reg-tab z))])))
  144. (define (dispatch-zero-x byte)
  145. (let ([z (bitwise-and #b00000111 byte)])
  146. (case z
  147. [(0) (relative-jp-and-misc byte)]
  148. [(1) (16b-ld-and-add byte)]
  149. [(2) (indirect-lds-a byte)]
  150. [(3) (16b-inc-dec byte)]
  151. [(4 5) (8b-inc-dec byte)]
  152. [(6) (8b-ld-imm byte)]
  153. [(7) (rots-and-misc byte)]
  154. [else (error "malformed instruction: " byte)])))
  155. (define (dispatch-ld-r-r byte)
  156. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  157. [z (bitwise-and #b00000111 byte)])
  158. `(LD ,(reg-tab y) ,(reg-tab z))))
  159. (define (rots-and-misc byte)
  160. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)])
  161. (case y
  162. [(0) '(RLCA)]
  163. [(1) '(RRCA)]
  164. [(2) '(RLA)]
  165. [(3) '(RRA)]
  166. [(4) '(DAA)]
  167. [(5) '(CPL)]
  168. [(6) '(SCF)]
  169. [(7) '(CCF)])))
  170. (define (8b-ld-imm byte)
  171. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)])
  172. (lambda x
  173. `(LD (REG ,(reg-tab y)) x))))
  174. (define (8b-inc-dec byte)
  175. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  176. [z (bitwise-and #b00000111 byte)])
  177. `(,(if (= 4 z) 'INC 'DEC) (REG ,(reg-tab y)))))
  178. (define (16b-inc-dec byte)
  179. (letrec ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  180. [q (modulo y 2)]
  181. [p (arithmetic-shift y -1)])
  182. `(,(if (= 0 q) 'INC 'DEC) (REGP ,(rp-tab p)))))
  183. (define (indirect-lds-a byte)
  184. (letrec ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  185. [q (modulo y 2)]
  186. [p (arithmetic-shift y -1)])
  187. (if (= 0 q)
  188. (make-ld-ind-p-a p)
  189. (make-ld-ind-a-p p))))
  190. (define (make-ld-ind-p-a p)
  191. (case p
  192. [(0) '(LD (IND BC) (REG A))]
  193. [(1) '(LD (IND DE) (REG A))]
  194. [(2) '(LDI (IND HL) (REG A))]
  195. [(3) '(LDD (IND HL) (REG A))]))
  196. (define (make-ld-ind-a-p p)
  197. (case p
  198. [(0) '(LD (REG A) (IND BC))]
  199. [(1) '(LD (REG A) (IND DE))]
  200. [(2) '(LDI (REG A) (IND HL))]
  201. [(3) '(LDD (REG A) (IND HL))]))
  202. (define (16b-ld-and-add byte)
  203. (letrec ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  204. [q (modulo y 2)]
  205. [p (arithmetic-shift y -1)])
  206. (if (= 0 q)
  207. (make-16b-ld p)
  208. `(ADD (REGP HL) (REGP ,(rp-tab p))))))
  209. (define (relative-jp-and-misc byte)
  210. (let ([y (arithmetic-shift (bitwise-and #b00111000 byte) -3)])
  211. (case y
  212. [(0) '(NOP)]
  213. [(1) (create-ld-16b-ind 'SP)]
  214. [(2) '(STOP)]
  215. [(3) (make-jr '())]
  216. [(4 5 6 7) (make-jr y)]
  217. [else (error "malformed byte: " byte)])))
  218. (define (decode-cb-prefix byte)
  219. (letrec ([x (arithmetic-shift (bitwise-and #b11000000 byte) -6)]
  220. [y (arithmetic-shift (bitwise-and #b00111000 byte) -3)]
  221. [z (bitwise-and #b00000111 byte)])
  222. (case x
  223. [(0) `(,(alu-tab y) ,(reg-tab z))]
  224. [(1) `(BIT ,y ,(reg-tab z))]
  225. [(2) `(RES ,y ,(reg-tab z))]
  226. [(3) `(SET ,y ,(reg-tab z))])))
  227. (define (test bs)
  228. (car (read-instruction (set-bytes #x0000 bs (make-mem)))))
  229. (define (create-ld-16b-ind src)
  230. (lambda y
  231. (lambda x
  232. `(LD (IND ,(make-16b-addr x y)) ,src))))
  233. (define (make-16b-ld p)
  234. (lambda y
  235. (lambda x
  236. `(LD (REGP ,(rp-tab p)) (IND ,(make-16b-addr x y))))))
  237. (define (make-jr cc)
  238. `(JR ,(if (null? cc) 'no-check (cc-tab (- cc 4)))))
  239. (define (read-instruction m)
  240. (begin
  241. (define (ri-loop f npc)
  242. (if (not (procedure? f))
  243. (cons f (struct-copy mem m [pc npc]))
  244. (ri-loop (f (get-byte npc m)) (mod-16bit (add1 npc)))))
  245. (ri-loop decode-instr (mem-pc m))))
  246. (define (decode-instr byte)
  247. (if (or (>= byte 256) (< byte 0))
  248. (error "invalid opcode byte: " byte)
  249. (if (= byte #xCB)
  250. decode-cb-prefix
  251. (non-prefix byte))))
  252. (define (read-instruction m)
  253. (begin
  254. (define (ri-loop f npc)
  255. (if (not (procedure? f))
  256. (cons f (struct-copy mem m [pc npc]))
  257. (ri-loop (f (get-byte npc m)) (mod-16bit (add1 npc)))))
  258. (ri-loop decode-instr (mem-pc m))))
  259. (define (decode-instr byte)
  260. (if (or (>= byte 256) (< byte 0))
  261. (error "invalid opcode byte: " byte)
  262. (if (= byte #xCB)
  263. decode-cb-prefix
  264. (non-prefix byte))))
  265. (define (read-instruction m)
  266. (begin
  267. (define (ri-loop f npc)
  268. (if (not (procedure? f))
  269. (cons f (struct-copy mem m [pc npc]))
  270. (ri-loop (f (get-byte npc m)) (mod-16bit (add1 npc)))))
  271. (ri-loop decode-instr (mem-pc m))))
  272. (define (decode-instr byte)
  273. (if (or (>= byte 256) (< byte 0))
  274. (error "invalid opcode byte: " byte)
  275. (if (= byte #xCB)
  276. decode-cb-prefix
  277. (non-prefix byte))))