Mercurial > vba-clojure
annotate clojure/com/aurellem/assembly.clj @ 105:2f8089eacab9
working on some actual assembly programs
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 13 Mar 2012 14:40:01 -0500 |
parents | |
children | 3a60bb14a64a |
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@105 | 5 |
rlm@105 | 6 |
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 (set-state! state) |
rlm@105 | 13 (let [scratch-memory (memory state)] |
rlm@105 | 14 ;; inject assembly code |
rlm@105 | 15 (dorun (map (fn [index val] |
rlm@105 | 16 (aset scratch-memory index val)) |
rlm@105 | 17 (range program-counter |
rlm@105 | 18 (+ program-counter (count assembly-code))) |
rlm@105 | 19 assembly-code)) |
rlm@105 | 20 (write-memory! scratch-memory) |
rlm@105 | 21 (set-state! (update-state)) |
rlm@105 | 22 (write-registers! registers) |
rlm@105 | 23 (set-state! (update-state)) |
rlm@105 | 24 (PC! program-counter) |
rlm@105 | 25 (update-state))) |
rlm@105 | 26 |
rlm@105 | 27 (defn inject-item-assembly |
rlm@105 | 28 ([^SaveState state assembly-code] |
rlm@105 | 29 (inject-assembly state (inc item-list-start) |
rlm@105 | 30 (registers state) |
rlm@105 | 31 assembly-code)) |
rlm@105 | 32 ([assembly-code] |
rlm@105 | 33 (inject-item-assembly @current-state assembly-code))) |
rlm@105 | 34 |
rlm@105 | 35 (defn info |
rlm@105 | 36 ([^SaveState state] |
rlm@105 | 37 (set-state! state) |
rlm@105 | 38 (println "PC: " (PC)) |
rlm@105 | 39 (println "Instruction:" (format "0x%02X" (aget (memory state) (PC)))) |
rlm@105 | 40 state)) |
rlm@105 | 41 |
rlm@105 | 42 (defn irony [] (read-state 578544)) |
rlm@105 | 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@105 | 49 (inject-item-assembly (tick (tick (tick (irony)))) assembly) |
rlm@105 | 50 (range n))] |
rlm@105 | 51 (shutdown!) |
rlm@105 | 52 final-state)) |
rlm@105 | 53 ([assembly n] |
rlm@105 | 54 (run-assembly info assembly n))) |
rlm@105 | 55 |