Mercurial > vba-clojure
annotate clojure/com/aurellem/assembly.clj @ 106:3a60bb14a64a
better functional assembly interface; removed frame numbers from SaveStates
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 14 Mar 2012 21:37:37 -0500 |
parents | 2f8089eacab9 |
children | ad96e9464d6a |
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@105 | 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@106 | 22 (PC! program-counter)))) |
rlm@106 | 23 |
rlm@105 | 24 |
rlm@105 | 25 (defn inject-item-assembly |
rlm@105 | 26 ([^SaveState state assembly-code] |
rlm@105 | 27 (inject-assembly state (inc item-list-start) |
rlm@105 | 28 (registers state) |
rlm@105 | 29 assembly-code)) |
rlm@105 | 30 ([assembly-code] |
rlm@105 | 31 (inject-item-assembly @current-state assembly-code))) |
rlm@105 | 32 |
rlm@105 | 33 (defn info |
rlm@105 | 34 ([^SaveState state] |
rlm@106 | 35 (println "PC: " (PC state)) |
rlm@106 | 36 (println "Instruction:" |
rlm@106 | 37 (format "0x%02X" (aget (memory state) (PC state)))) |
rlm@105 | 38 state)) |
rlm@105 | 39 |
rlm@105 | 40 (defn run-assembly |
rlm@105 | 41 ([info-fn assembly n] |
rlm@105 | 42 (let [final-state |
rlm@105 | 43 (reduce (fn [state _] |
rlm@105 | 44 (tick (info-fn state))) |
rlm@106 | 45 (inject-item-assembly (tick (tick (tick (mid-game)))) assembly) |
rlm@105 | 46 (range n))] |
rlm@105 | 47 (shutdown!) |
rlm@105 | 48 final-state)) |
rlm@105 | 49 ([assembly n] |
rlm@105 | 50 (run-assembly info assembly n))) |
rlm@105 | 51 |