annotate clojure/com/aurellem/assembly.clj @ 113:0831da75d2c5

completed frame-counting machine language program with dylan's help
author Robert McIntyre <rlm@mit.edu>
date Fri, 16 Mar 2012 00:43:28 -0500
parents 6fe33bb5ea22
children a454730d92dd
rev   line source
rlm@105 1 (ns com.aurellem.assembly
rlm@105 2 (:use (com.aurellem gb-driver vbm title items))
rlm@105 3 (:import [com.aurellem.gb_driver SaveState]))
rlm@105 4
rlm@106 5 (defn mid-game []
rlm@106 6 (read-state "mid-game"))
rlm@105 7
rlm@105 8 (defn inject-assembly
rlm@107 9 ([^SaveState state
rlm@105 10 program-counter registers
rlm@105 11 assembly-code]
rlm@105 12 (let [scratch-memory (memory state)]
rlm@105 13 ;; inject assembly code
rlm@105 14 (dorun (map (fn [index val]
rlm@105 15 (aset scratch-memory index val))
rlm@105 16 (range program-counter
rlm@105 17 (+ program-counter (count assembly-code)))
rlm@105 18 assembly-code))
rlm@106 19 (-> state
rlm@106 20 (write-memory! scratch-memory)
rlm@106 21 (write-registers! registers)
rlm@112 22 (PC! program-counter)))))
rlm@105 23
rlm@105 24 (defn inject-item-assembly
rlm@105 25 ([^SaveState state assembly-code]
rlm@105 26 (inject-assembly state (inc item-list-start)
rlm@105 27 (registers state)
rlm@105 28 assembly-code))
rlm@105 29 ([assembly-code]
rlm@105 30 (inject-item-assembly @current-state assembly-code)))
rlm@105 31
rlm@105 32 (defn info
rlm@105 33 ([^SaveState state]
rlm@107 34 (println (format "PC: 0x%04X" (PC state)))
rlm@106 35 (println "Instruction:"
rlm@106 36 (format "0x%02X" (aget (memory state) (PC state))))
rlm@105 37 state))
rlm@105 38
rlm@107 39 (defn print-interrupt
rlm@107 40 [^SaveState state]
rlm@107 41 (println (format "IE: %d" (IE state)))
rlm@107 42 state)
rlm@107 43
rlm@105 44 (defn run-assembly
rlm@105 45 ([info-fn assembly n]
rlm@105 46 (let [final-state
rlm@105 47 (reduce (fn [state _]
rlm@105 48 (tick (info-fn state)))
rlm@107 49 (inject-item-assembly
rlm@107 50 (mid-game) assembly)
rlm@105 51 (range n))]
rlm@105 52 final-state))
rlm@105 53 ([assembly n]
rlm@105 54 (run-assembly info assembly n)))
rlm@107 55
rlm@107 56 (def buttons-port 0xFF00)
rlm@107 57
rlm@107 58 (defn A [state]
rlm@107 59 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
rlm@107 60
rlm@110 61 (defn binary-str [num]
rlm@110 62 (format "%08d"
rlm@110 63 (Integer/parseInt
rlm@110 64 (Integer/toBinaryString num) 10)))
rlm@110 65
rlm@110 66 (defn view-register [state name reg-fn]
rlm@110 67 (println (format "%s: %s" name
rlm@110 68 (binary-str (reg-fn state))))
rlm@110 69 state)
rlm@110 70
rlm@110 71
rlm@107 72 (defn view-memory [state mem]
rlm@110 73 (println (format "mem 0x%04X = %s" mem
rlm@110 74 (binary-str (aget (memory state) mem))))
rlm@107 75 state)
rlm@107 76
rlm@113 77 (defn read-down-button []
rlm@110 78 (-> (tick (mid-game))
rlm@110 79 (IE! 0) ; disable interrupts
rlm@110 80 (inject-item-assembly
rlm@110 81 (concat
rlm@110 82 ;; write 00010000 to 0xFF00 to select joypad
rlm@110 83 [0x18 ;D31D ; jump over
rlm@110 84 0x01 ;D31E ; the next 8 bits
rlm@113 85 ;D31F
rlm@113 86 (Integer/parseInt "00100000" 2) ; data section
rlm@111 87
rlm@111 88 0xFA ;D320 ; load (D31F) into A
rlm@111 89 0x1F ;D321 -->
rlm@111 90 0xD3 ;D322 --> D31F
rlm@107 91
rlm@111 92 0xEA ;D323 ; load (A), which is
rlm@111 93 0x00 ;D324 --> ; 00010000, into FF00
rlm@111 94 0xFF ;D325 --> FF00
rlm@111 95
rlm@111 96 0x18 ;D326 ; this is the place where
rlm@111 97 0x01 ;D327 ; we will store whether
rlm@111 98 0x00 ;D328 ; "down" is pressed.
rlm@107 99
rlm@111 100 0xFA ;D329 ; (FF00) -> A
rlm@111 101 0x00 ;D32A
rlm@111 102 0xFF ;D32B
rlm@110 103
rlm@111 104 0xCB ;D32C ; Test whether "down"
rlm@111 105 0x5F ;D32D ; is pressed.
rlm@111 106
rlm@111 107 0x28 ;D32E ; if down is pressed,
rlm@111 108 0x03 ;D32F ; skip the next section
rlm@111 109 ; of code.
rlm@111 110 ;; down-is-not-pressed
rlm@111 111 0xC3 ;D330
rlm@111 112 0x1D ;D331 ; return to beginning
rlm@111 113 0xD3 ;D332
rlm@111 114
rlm@111 115 ;; down-is-pressed
rlm@111 116 0xEA ;D334 ; write A to D328 if
rlm@111 117 0x28 ;D335 ; "down" was pressed
rlm@111 118 0xD3 ;D336
rlm@111 119
rlm@111 120 0xC3 ;D330
rlm@111 121 0x1D ;D331 ; return to beginning
rlm@111 122 0xD3 ;D332
rlm@110 123 ]
rlm@110 124
rlm@111 125 []))))
rlm@111 126
rlm@113 127
rlm@113 128 ;; specs for main bootstrap program
rlm@113 129 ;; starts in "mode-select" mode
rlm@113 130 ;; Each button press takes place in a single frame.
rlm@113 131 ;; mode-select-mode takes one of the main buttons
rlm@113 132 ;; which selects one of up to eight modes
rlm@113 133 ;; mode 1 activated by the "A" button
rlm@113 134 ;; the next two button presses indicates the start
rlm@113 135 ;; memory location which to which the bootstrap
rlm@113 136 ;; program will write.
rlm@113 137 ;; This is done by using each of the eight buttons to
rlm@113 138 ;; spell out an 8 bit number. The order of buttons is
rlm@113 139 ;; ["A" "B" "start" "select" "up" "right" "down" "left"],
rlm@113 140 ;; [:a :start :l] --> 10100001
rlm@113 141
rlm@113 142 ;; the next button press determines how many bytes are to be
rlm@113 143 ;; written, starting at the start position.
rlm@113 144
rlm@113 145 ;; then, the actual bytes are entered and are written to the
rlm@113 146 ;; start address in sequence.
rlm@113 147
rlm@113 148
rlm@113 149 (defn count-frames []
rlm@113 150 (-> (tick (mid-game))
rlm@113 151 (IE! 0) ; disable interrupts
rlm@113 152 (inject-item-assembly
rlm@113 153 ;; write 00010000 to 0xFF00 to select joypad
rlm@113 154 [0x18 ;D31D ; jump over
rlm@113 155 0x02 ;D31E ; the next 2 bytes
rlm@113 156 0x00 ;D31F ; frame-count
rlm@113 157 0x00 ;D320 ; v-blank-prev
rlm@113 158
rlm@113 159 0xFA ;D321
rlm@113 160 0x41 ;D322 ; load (FF41) into A
rlm@113 161 0xFF ;D323 ; this contains mode flags
rlm@113 162
rlm@113 163
rlm@113 164
rlm@113 165 ;; if we're in v-blank, the bit-1 is 0
rlm@113 166 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@113 167 0xCB ;D324 ; test bit-1 of A
rlm@113 168 0x4F ;D325
rlm@113 169
rlm@113 170 0xC2 ;D326 ; if bit-1 is not 0
rlm@113 171 0x43 ;D327 ; GOTO not-v-blank
rlm@113 172 0xD3 ;D328
rlm@113 173
rlm@113 174 0xCB ;D329 ; test bit-0 of A
rlm@113 175 0x47 ;D32A
rlm@113 176
rlm@113 177 0xCA ;D32B ; if bit-0 is not 1
rlm@113 178 0x43 ;D32C ; GOTO not-v-blank
rlm@113 179 0xD3 ;D32D
rlm@113 180
rlm@113 181 ;; in v-blank mode
rlm@113 182
rlm@113 183 ;; if v-blank-prev was 0,
rlm@113 184 ;; increment frame-count
rlm@113 185
rlm@113 186 0xFA ;D32E ; load v-blank-prev to A
rlm@113 187 0x20 ;D32F
rlm@113 188 0xD3 ;D330
rlm@113 189
rlm@113 190 0xCB ;D331
rlm@113 191 0x47 ;D332 ; test bit-0 of A
rlm@113 192
rlm@113 193 0x20 ;D333 ; skip next section
rlm@113 194 0x07 ;D334 ; if v-blank-prev was not zero
rlm@113 195
rlm@113 196 ;; v-blank was 0, increment frame-count
rlm@113 197 0xFA ;D335 ; load frame-count into A
rlm@113 198 0x1F ;D336
rlm@113 199 0xD3 ;D337
rlm@113 200
rlm@113 201 0x3C ;D338 ; inc A
rlm@113 202
rlm@113 203 0xEA ;D339 ; load A into frame-count
rlm@113 204 0x1F ;D33A
rlm@113 205 0xD3 ;D33B
rlm@113 206
rlm@113 207 ;; set v-blank? to 1
rlm@113 208 0x3E ;D33C ; load 1 into A
rlm@113 209 0x01 ;D33D
rlm@113 210
rlm@113 211 0xEA ;D33E ; load A into v-blank-prev
rlm@113 212 0x20 ;D33F
rlm@113 213 0xD3 ;D340
rlm@113 214
rlm@113 215 0x18 ;D341 ; skip not-in-v-blank section
rlm@113 216 0x05 ;D342
rlm@113 217
rlm@113 218 ;; not in v-blank mode
rlm@113 219 ;; set b-blank? to 0
rlm@113 220 0x3E ;D343 ; load 0 into A
rlm@113 221 0x00 ;D344
rlm@113 222
rlm@113 223 0xEA ;D345 ; load A into v-blank-prev
rlm@113 224 0x20 ;D346
rlm@113 225 0xD3 ;D347
rlm@113 226
rlm@113 227
rlm@113 228 0xC3 ;D348 ; return to beginning
rlm@113 229 0x1D ;D349
rlm@113 230 0xD3 ;D34A
rlm@113 231
rlm@113 232
rlm@113 233
rlm@113 234 ])))
rlm@113 235
rlm@113 236
rlm@113 237
rlm@113 238
rlm@111 239 (defn run-a-bit []
rlm@113 240 (-> (read-down-button)
rlm@110 241 (info)
rlm@110 242 (tick) ;; skip over data section
rlm@110 243 (info)
rlm@110 244 (view-register "Register A" A)
rlm@110 245 (tick) ;; load-data into A
rlm@110 246 (view-register "Register A" A)
rlm@110 247 (info)
rlm@110 248 (view-memory 0xFF00)
rlm@110 249 (tick) ;; load A into 0xFF00
rlm@110 250 (view-memory 0xFF00)
rlm@111 251 (info)
rlm@111 252 (tick)
rlm@111 253 (info)
rlm@111 254 (tick)
rlm@111 255 (info)
rlm@111 256 (tick)
rlm@111 257 (info)
rlm@111 258 (tick)
rlm@111 259 (info)
rlm@111 260 (tick)
rlm@111 261 (info)
rlm@111 262 (tick)
rlm@111 263 (print-inventory)))
rlm@111 264
rlm@112 265 (defn test-read-pad []
rlm@112 266 (= (view-memory (step (step (read-buttons) [:d])) 0xD328)
rlm@112 267 (view-memory (step (step (read-buttons))) 0xD328)))
rlm@111 268
rlm@107 269 (defn trace [state]
rlm@107 270 (loop [program-counters []
rlm@107 271 opcodes []]
rlm@107 272 (let [frame-boundary?
rlm@107 273 (com.aurellem.gb.Gb/tick)]
rlm@107 274 (println (count opcodes))
rlm@107 275 (if frame-boundary?
rlm@107 276 [program-counters opcodes]
rlm@107 277 (recur
rlm@107 278 (conj program-counters
rlm@107 279 (first (registers @current-state)))
rlm@107 280 (conj opcodes
rlm@107 281 (aget (memory @current-state)
rlm@107 282 (PC @current-state))))))))
rlm@107 283
rlm@107 284 (defn good-trace []
rlm@107 285 (-> (mid-game) (tick) (IE! 0)
rlm@107 286 (set-inv-mem [0x00 0x00 0X00 0x00])
rlm@107 287 (PC! item-list-start)(print-interrupt)
rlm@112 288 (info) (tick) (info) (tick) (info)))