Mercurial > vba-clojure
comparison 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 |
comparison
equal
deleted
inserted
replaced
105:2f8089eacab9 | 106:3a60bb14a64a |
---|---|
1 (ns com.aurellem.assembly | 1 (ns com.aurellem.assembly |
2 (:use (com.aurellem gb-driver vbm title items)) | 2 (:use (com.aurellem gb-driver vbm title items)) |
3 (:import [com.aurellem.gb_driver SaveState])) | 3 (:import [com.aurellem.gb_driver SaveState])) |
4 | 4 |
5 | 5 (defn mid-game [] |
6 | 6 (read-state "mid-game")) |
7 | 7 |
8 (defn inject-assembly | 8 (defn inject-assembly |
9 [^SaveState state | 9 [^SaveState state |
10 program-counter registers | 10 program-counter registers |
11 assembly-code] | 11 assembly-code] |
12 (set-state! state) | |
13 (let [scratch-memory (memory state)] | 12 (let [scratch-memory (memory state)] |
14 ;; inject assembly code | 13 ;; inject assembly code |
15 (dorun (map (fn [index val] | 14 (dorun (map (fn [index val] |
16 (aset scratch-memory index val)) | 15 (aset scratch-memory index val)) |
17 (range program-counter | 16 (range program-counter |
18 (+ program-counter (count assembly-code))) | 17 (+ program-counter (count assembly-code))) |
19 assembly-code)) | 18 assembly-code)) |
20 (write-memory! scratch-memory) | 19 (-> state |
21 (set-state! (update-state)) | 20 (write-memory! scratch-memory) |
22 (write-registers! registers) | 21 (write-registers! registers) |
23 (set-state! (update-state)) | 22 (PC! program-counter)))) |
24 (PC! program-counter) | 23 |
25 (update-state))) | |
26 | 24 |
27 (defn inject-item-assembly | 25 (defn inject-item-assembly |
28 ([^SaveState state assembly-code] | 26 ([^SaveState state assembly-code] |
29 (inject-assembly state (inc item-list-start) | 27 (inject-assembly state (inc item-list-start) |
30 (registers state) | 28 (registers state) |
32 ([assembly-code] | 30 ([assembly-code] |
33 (inject-item-assembly @current-state assembly-code))) | 31 (inject-item-assembly @current-state assembly-code))) |
34 | 32 |
35 (defn info | 33 (defn info |
36 ([^SaveState state] | 34 ([^SaveState state] |
37 (set-state! state) | 35 (println "PC: " (PC state)) |
38 (println "PC: " (PC)) | 36 (println "Instruction:" |
39 (println "Instruction:" (format "0x%02X" (aget (memory state) (PC)))) | 37 (format "0x%02X" (aget (memory state) (PC state)))) |
40 state)) | 38 state)) |
41 | |
42 (defn irony [] (read-state 578544)) | |
43 | 39 |
44 (defn run-assembly | 40 (defn run-assembly |
45 ([info-fn assembly n] | 41 ([info-fn assembly n] |
46 (let [final-state | 42 (let [final-state |
47 (reduce (fn [state _] | 43 (reduce (fn [state _] |
48 (tick (info-fn state))) | 44 (tick (info-fn state))) |
49 (inject-item-assembly (tick (tick (tick (irony)))) assembly) | 45 (inject-item-assembly (tick (tick (tick (mid-game)))) assembly) |
50 (range n))] | 46 (range n))] |
51 (shutdown!) | 47 (shutdown!) |
52 final-state)) | 48 final-state)) |
53 ([assembly n] | 49 ([assembly n] |
54 (run-assembly info assembly n))) | 50 (run-assembly info assembly n))) |