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.

135 lines
3.5KB

  1. #lang racket
  2. (require "memory.rkt")
  3. ;---------------------
  4. ; Decode dot ry ky ty
  5. ;---------------------
  6. ; Functions to decode
  7. ; a byte to an assembly
  8. ; instruction.
  9. ;---------------------
  10. (define (inc16b x)
  11. (mod-16bit (add1 x)))
  12. (define (alu-op y)
  13. (case y
  14. [(0) 'RLC]
  15. [(1) 'RRC]
  16. [(2) 'RL]
  17. [(3) 'RR]
  18. [(4) 'SLA]
  19. [(5) 'SRA]
  20. [(6) 'SWAP]
  21. [(7) 'SRL]))
  22. (define (reg-op z)
  23. (case z
  24. [(0) 'reg-B]
  25. [(1) 'reg-C]
  26. [(2) 'reg-D]
  27. [(3) 'reg-E]
  28. [(4) 'reg-H]
  29. [(5) 'reg-L]
  30. [(6) 'reg-ind-HL]
  31. [(7) 'reg-A]))
  32. (define (cc-tab z)
  33. (case z
  34. [(0) 'NZ]
  35. [(1) 'Z]
  36. [(2) 'NC]
  37. [(3) 'C]))
  38. (define (rp-tab z)
  39. (case z
  40. [(0) 'reg-BC]
  41. [(1) 'reg-DE]
  42. [(2) 'reg-HL]
  43. [(3) 'reg-SP]))
  44. (define (read-word-mem memory)
  45. (let ([lsb (read-byte-mem memory)]
  46. [msb (read-byte-mem memory)])
  47. (bitwise-ior lsb
  48. (arithmetic-shift msb 8))))
  49. (define (read-byte-mem memory)
  50. (let ([pc (get-state 'state-PC memory)])
  51. (let ([b (get-byte pc 0 memory)])
  52. (begin
  53. (set-state! 'state-PC memory (inc16b pc))
  54. b))))
  55. (define (read-instruction memory)
  56. (let ([b1 (read-byte-mem memory)])
  57. (if (= #xCB b1)
  58. ; $CB prefix ops
  59. (letrec ([b2 (read-byte-mem memory)]
  60. [x (arithmetic-shift (bitwise-and #b11000000 b2) -6)]
  61. [y (arithmetic-shift (bitwise-and #b00111000 b2) -3)]
  62. [z (bitwise-and #b00000111 b2)]
  63. [p (arithmetic-shift y -1)]
  64. [q (modulo y 2)])
  65. (case x
  66. [(0) `(,(alu-op y) ,(reg-op z))]
  67. [(1) `(BIT ,y ,(reg-op z))]
  68. [(2) `(RES ,y ,(reg-op z))]
  69. [(3) `(SET ,y ,(reg-op z))]
  70. [else (error "bad instruction")]))
  71. ; No prefix ops
  72. (letrec ([x (arithmetic-shift (bitwise-and #b11000000 b1) -6)]
  73. [y (arithmetic-shift (bitwise-and #b00111000 b1) -3)]
  74. [z (bitwise-and #b00000111 b1)]
  75. [p (arithmetic-shift y -1)]
  76. [q (modulo y 2)])
  77. (case x
  78. [(0)
  79. (case z
  80. [(0)
  81. (case y
  82. [(0) '(NOP)]
  83. [(1) `(LD-ind ,(read-word-mem memory) reg-SP)]
  84. [(2) '(STOP)]
  85. [(3) `(JR expl ,(read-byte-mem memory))]
  86. [(4 5 6 7) `(JR ,(cc-tab (- y 4)) ,(read-byte-mem memory))]
  87. [else (error "bad instruction")])]
  88. [(1)
  89. (case q
  90. [(0) `(LD-16b ,(rp-tab p) ,(read-word-memory))]
  91. [(1) `(ADD-16b reg-HL ,(rp-tab p))]
  92. [else (error "bad instruction")])
  93. ]
  94. [(2) (void)]
  95. [(3) (void)]
  96. [(4) (void)]
  97. [(5) (void)]
  98. [(6) (void)]
  99. [(7) (void)])]
  100. [(1) (void)]
  101. [(2) (void)]
  102. [(3) (void)]
  103. [(4 5 6 7) (void)]
  104. [else (error "bad instruction")]
  105. )
  106. )
  107. )))
  108. (define (test1)
  109. (begin
  110. (define m (make-memory))
  111. (set-state! 'state-PC m #x150)
  112. (set-byte! #x150 'nil m #x00)
  113. (read-instruction m)))
  114. (define (test2)
  115. (begin
  116. (define m (make-memory))
  117. (set-state! 'state-PC m #x150)
  118. (set-byte! #x150 'nil m #x08)
  119. (set-byte! #x151 'nil m #x69)
  120. (set-byte! #x152 'nil m #xFF)
  121. (read-instruction m)))
  122. (provide (all-defined-out))