Mercurial > vba-clojure
view clojure/com/aurellem/assembly.clj @ 116:e45031af5327
functional number-input bootstrap code complete
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 16 Mar 2012 17:03:05 -0500 |
parents | 39fb0cbab25e |
children | bcb5c41626b4 |
line wrap: on
line source
1 (ns com.aurellem.assembly2 (:use (com.aurellem gb-driver vbm title items))3 (:import [com.aurellem.gb_driver SaveState]))5 (defn mid-game []6 (read-state "mid-game"))8 (defn inject-assembly9 ([^SaveState state10 program-counter registers11 assembly-code]12 (let [scratch-memory (memory state)]13 ;; inject assembly code14 (dorun (map (fn [index val]15 (aset scratch-memory index val))16 (range program-counter17 (+ program-counter (count assembly-code)))18 assembly-code))19 (-> state20 (write-memory! scratch-memory)21 (write-registers! registers)22 (PC! program-counter)))))24 (defn inject-item-assembly25 ([^SaveState state assembly-code]26 (inject-assembly state (inc item-list-start)27 (registers state)28 assembly-code))29 ([assembly-code]30 (inject-item-assembly @current-state assembly-code)))32 (defn info33 ([^SaveState state]34 (println (format "PC: 0x%04X" (PC state)))35 (println "Instruction:"36 (format "0x%02X" (aget (memory state) (PC state))))37 state))39 (defn print-interrupt40 [^SaveState state]41 (println (format "IE: %d" (IE state)))42 state)44 (defn run-assembly45 ([info-fn assembly n]46 (let [final-state47 (reduce (fn [state _]48 (tick (info-fn state)))49 (inject-item-assembly50 (mid-game) assembly)51 (range n))]52 final-state))53 ([assembly n]54 (run-assembly info assembly n)))56 (def buttons-port 0xFF00)58 (defn A [state]59 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))61 (defn binary-str [num]62 (format "%08d"63 (Integer/parseInt64 (Integer/toBinaryString num) 10)))66 (defn view-register [state name reg-fn]67 (println (format "%s: %s" name68 (binary-str (reg-fn state))))69 state)72 (defn view-memory [state mem]73 (println (format "mem 0x%04X = %s" mem74 (binary-str (aget (memory state) mem))))75 state)77 (defn read-down-button []78 (-> (tick (mid-game))79 (IE! 0) ; disable interrupts80 (inject-item-assembly81 (concat82 ;; write 00010000 to 0xFF00 to select joypad83 [0x18 ;D31D ; jump over84 0x01 ;D31E ; the next 8 bits85 ;D31F86 (Integer/parseInt "00100000" 2) ; data section88 0xFA ;D320 ; load (D31F) into A89 0x1F ;D321 -->90 0xD3 ;D322 --> D31F92 0xEA ;D323 ; load (A), which is93 0x00 ;D324 --> ; 00010000, into FF0094 0xFF ;D325 --> FF0096 0x18 ;D326 ; this is the place where97 0x01 ;D327 ; we will store whether98 0x00 ;D328 ; "down" is pressed.100 0xFA ;D329 ; (FF00) -> A101 0x00 ;D32A102 0xFF ;D32B104 0xCB ;D32C ; Test whether "down"105 0x5F ;D32D ; is pressed.107 0x28 ;D32E ; if down is pressed,108 0x03 ;D32F ; skip the next section109 ; of code.110 ;; down-is-not-pressed111 0xC3 ;D330112 0x1D ;D331 ; return to beginning113 0xD3 ;D332115 ;; down-is-pressed116 0xEA ;D334 ; write A to D328 if117 0x28 ;D335 ; "down" was pressed118 0xD3 ;D336120 0xC3 ;D330121 0x1D ;D331 ; return to beginning122 0xD3 ;D332123 ]125 []))))130 (defn count-frames []131 (-> (tick (mid-game))132 (IE! 0) ; disable interrupts133 (inject-item-assembly134 [0x18 ;D31D ; jump over135 0x02 ;D31E ; the next 2 bytes136 0x00 ;D31F ; frame-count137 0x00 ;D320 ; v-blank-prev139 0xFA ;D321140 0x41 ;D322 ; load (FF41) into A141 0xFF ;D323 ; this contains mode flags143 ;; if we're in v-blank, the bit-1 is 0144 ;; and bit-2 is 1 Otherwise, it is not v-blank.145 0xCB ;D324 ; test bit-1 of A146 0x4F ;D325148 0xC2 ;D326 ; if bit-1 is not 0149 0x44 ;D327 ; GOTO not-v-blank150 0xD3 ;D328152 0xCB ;D329 ; test bit-0 of A153 0x47 ;D32A155 0xCA ;D32B ; if bit-0 is not 1156 0x44 ;D32C ; GOTO not-v-blank157 0xD3 ;D32D159 ;;; in v-blank mode161 ;; if v-blank-prev was 0,162 ;; increment frame-count164 0xFA ;D32E ; load v-blank-prev to A165 0x20 ;D32F166 0xD3 ;D330168 0xCB ;D331169 0x47 ;D332 ; test bit-0 of A171 0x20 ;D333 ; skip next section172 0x07 ;D334 ; if v-blank-prev was not zero174 ;; v-blank was 0, increment frame-count175 0xFA ;D335 ; load frame-count into A176 0x1F ;D336177 0xD3 ;D337179 0x3C ;D338 ; inc A181 0xEA ;D339 ; load A into frame-count182 0x1F ;D33A183 0xD3 ;D33B185 ;; set v-blank-prev to 1186 0x3E ;D33C ; load 1 into A187 0x01 ;D33D189 0xEA ;D33E ; load A into v-blank-prev190 0x20 ;D33F191 0xD3 ;D340193 0xC3 ;D341 ; return to beginning194 0x1D ;D342195 0xD3 ;D343197 ;;; not in v-blank mode198 ;; set v-blank-prev to 0199 0x3E ;D344 ; load 0 into A200 0x00 ;D345202 0xEA ;D346 ; load A into v-blank-prev203 0x20 ;D347204 0xD3 ;D348206 0xC3 ;D349 ; return to beginning207 0x1D ;D34A208 0xD3 ;D34B210 ])))212 (defn step-count-frames []213 (-> (read-down-button)214 (info)215 (tick) ;; skip over data section216 (info)217 (view-register "Register A" A)218 (tick) ;; load-data into A219 (view-register "Register A" A)220 (info)221 (view-memory 0xFF00)222 (tick) ;; load A into 0xFF00223 (view-memory 0xFF00)224 (info)225 (tick)226 (info)227 (tick)228 (info)229 (tick)230 (info)231 (tick)232 (info)233 (tick)234 (info)235 (tick)236 (print-inventory)))238 ;;(defn test-read-down []239 ;; (= (view-memory (step (step (read-buttons) [:d])) 0xD328)240 ;; (view-memory (step (step (read-buttons))) 0xD328)))242 (defn test-count-frames []243 (= 255 (aget (memory ((apply comp (repeat 255 step))244 (count-frames)))245 0xD31F)))248 (defn trace [state]249 (loop [program-counters []250 opcodes []]251 (let [frame-boundary?252 (com.aurellem.gb.Gb/tick)]253 (println (count opcodes))254 (if frame-boundary?255 [program-counters opcodes]256 (recur257 (conj program-counters258 (first (registers @current-state)))259 (conj opcodes260 (aget (memory @current-state)261 (PC @current-state))))))))263 (defn good-trace []264 (-> (mid-game) (tick) (IE! 0)265 (set-inv-mem [0x00 0x00 0X00 0x00])266 (PC! item-list-start)(print-interrupt)267 (info) (tick) (info) (tick) (info)))269 ;; specs for main bootstrap program270 ;; starts in "mode-select" mode271 ;; Each button press takes place in a single frame.272 ;; mode-select-mode takes one of the main buttons273 ;; which selects one of up to eight modes274 ;; mode 1 activated by the "A" button275 ;; the next two button presses indicates the start276 ;; memory location which to which the bootstrap277 ;; program will write.278 ;; This is done by using each of the eight buttons to279 ;; spell out an 8 bit number. The order of buttons is280 ;; [:d :u :l :r :start :select :b :a]281 ;; [:a :start :l] --> 00101001283 ;; the next button press determines how many bytes are to be284 ;; written, starting at the start position.286 ;; then, the actual bytes are entered and are written to the287 ;; start address in sequence.291 (defn input-number []292 (-> (tick (mid-game))293 (IE! 0) ; disable interrupts294 (inject-item-assembly295 [0x18 ;D31D ; jump over296 0x02 ;D31E ; the next 2 bytes297 0x00 ;D31F ; frame-count298 0x00 ;D320 ; v-blank-prev300 0xFA ;D321301 0x41 ;D322 ; load (FF41) into A302 0xFF ;D323 ; this contains mode flags304 ;; if we're in v-blank, the bit-1 is 0305 ;; and bit-2 is 1 Otherwise, it is not v-blank.306 0xCB ;D324 ; test bit-1 of A307 0x4F ;D325309 0xC2 ;D326 ; if bit-1 is not 0310 0x44 ;D327 ; GOTO not-v-blank311 0xD3 ;D328313 0xCB ;D329 ; test bit-0 of A314 0x47 ;D32A316 0xCA ;D32B ; if bit-0 is not 1317 0x44 ;D32C ; GOTO not-v-blank318 0xD3 ;D32D320 ;;; in v-blank mode322 ;; if v-blank-prev was 0,323 ;; increment frame-count325 0xFA ;D32E ; load v-blank-prev to A326 0x20 ;D32F327 0xD3 ;D330329 0xCB ;D331330 0x47 ;D332 ; test bit-0 of A332 0x20 ;D333 ; skip next section333 0x07 ;D334 ; if v-blank-prev was not zero335 ;; v-blank was 0, increment frame-count336 0xFA ;D335 ; load frame-count into A337 0x1F ;D336338 0xD3 ;D337340 0x3C ;D338 ; inc A342 0xEA ;D339 ; load A into frame-count343 0x1F ;D33A344 0xD3 ;D33B346 ;; set v-blank-prev to 1347 0x3E ;D33C ; load 1 into A348 0x01 ;D33D350 0xEA ;D33E ; load A into v-blank-prev351 0x20 ;D33F352 0xD3 ;D340354 0xC3 ;D341 ; GOTO input handling code355 0x4E ;D342356 0xD3 ;D343358 ;;; not in v-blank mode359 ;; set v-blank-prev to 0360 0x3E ;D344 ; load 0 into A361 0x00 ;D345363 0xEA ;D346 ; load A into v-blank-prev364 0x20 ;D347365 0xD3 ;D348367 0xC3 ;D349 ; return to beginning368 0x1D ;D34A369 0xD3 ;D34B371 0x00 ;D34C ; these are here372 0x00 ;D34D ; for glue375 ;;; calculate input number based on button presses376 0x18 ;D34E ; skip next 3 bytes377 0x03 ;D34F378 ;D350379 (Integer/parseInt "00100000" 2) ; select directional pad380 ;D351381 (Integer/parseInt "00010000" 2) ; select buttons382 0x00 ;D352 ; input-number384 ;; select directional pad, store low bits in B386 0xFA ;D353 ; load (D350) into A387 0x50 ;D354 -->388 0xD3 ;D355 --> D31F390 0xEA ;D356 ; load (A), which is391 0x00 ;D357 --> ; 00010000, into FF00392 0xFF ;D358 --> FF00394 0x06 ;D359395 ;D35A396 (Integer/parseInt "11110000" 2) ; "11110000" -> B397 0xFA ;D35B ; (FF00) -> A398 0x00 ;D35C399 0xFF ;D35D401 0xCB ;D35E ; swap nybbles on A402 0x37 ;D35F403 0xA0 ;D360 ; (AND A B) -> A404 0x47 ;D361 ; A -> B406 ;; select buttons store bottom bits in C408 0xFA ; ; load (D351) into A409 0x51 ; -->410 0xD3 ; --> D31F412 0xEA ; ; load (A), which is413 0x00 ; --> ; 00001000, into FF00414 0xFF ; --> FF00416 0x0E ;417 (Integer/parseInt "00001111" 2) ; "00001111" -> C419 0xFA ; ; (FF00) -> A420 0x00 ;421 0xFF ;423 0xA1 ; ; (AND A C) -> A424 0x4F ; ; A -> C427 ;; combine the B and C registers into the input number428 0x79 ; ; C -> A429 0xB0 ; ; (OR A B) -> A430 0x2F ; ; negate A432 0xEA ; ; store A into input-number433 0x52 ;434 0xD3 ;436 0xC3 ; ; return to beginning437 0x1D ;438 0xD3 ;439 ])))444 (defn print-listing [state begin end]445 (dorun (map446 (fn [opcode line]447 (println (format "0x%04X: 0x%02X" line opcode)))448 (subvec (vec (memory state)) begin end)449 (range begin end))))