view clojure/com/aurellem/gb_driver.clj @ 100:2090bcb78f44

created functions to get and set register values
author Robert McIntyre <rlm@mit.edu>
date Mon, 12 Mar 2012 12:37:04 -0500
parents 9fad96094950
children 37711ff4a7bc
line wrap: on
line source
1 (ns com.aurellem.gb-driver
2 (:import com.aurellem.gb.Gb)
3 (:import java.io.File)
4 (:import org.apache.commons.io.FileUtils)
5 (:import (java.nio IntBuffer ByteOrder)))
7 ;; Savestates
8 (defrecord SaveState [frame data])
10 (def ^:dynamic *save-state-cache*
11 (File. "/home/r/proj/pokemon-escape/save-states/"))
13 (defn frame->filename [frame]
14 (File. *save-state-cache* (format "%07d.sav" frame)))
16 (defn write-state! [^SaveState save]
17 (let [buf (:data save)
18 bytes (byte-array (.limit buf))
19 dest (frame->filename (:frame save))]
20 (.get buf bytes)
21 (FileUtils/writeByteArrayToFile dest bytes)
22 (.rewind buf)
23 dest))
25 (defn read-state [frame]
26 (let [save (frame->filename frame)]
27 (if (.exists save)
28 (let [buf (Gb/saveBuffer)
29 bytes (FileUtils/readFileToByteArray save)]
30 (.put buf bytes)
31 (.flip buf)
32 (SaveState. frame buf)))))
33 ;;;;;;;;;;;;;;;;
35 ;; Gameboy management
36 (Gb/loadVBA)
38 (def yellow-rom-image
39 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))
41 (def yellow-save-file
42 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))
44 (def on? (atom nil))
46 (defn shutdown! [] (Gb/shutdown) (reset! on? false))
48 (defn restart! []
49 (shutdown!)
50 (.delete yellow-save-file)
51 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))
52 (reset! on? true))
54 ;;; The first state!
55 (defn gen-root! []
56 (restart!)
57 (let [state (SaveState. 0 (Gb/saveState))]
58 (write-state! state)
59 state))
61 (defn root []
62 (if (.exists (frame->filename 0))
63 (read-state 0)
64 (gen-root!)))
66 ;;;; Press Buttons
68 (def button-code
69 {;; main buttons
70 :a 0x0001
71 :b 0x0002
73 ;; directional pad
74 :r 0x0010
75 :l 0x0020
76 :u 0x0040
77 :d 0x0080
79 ;; meta buttons
80 :select 0x0004
81 :start 0x0008
83 ;; pseudo-buttons
84 :restart 0x0800 ; hard reset
85 :listen -1 ; listen for user input
86 })
88 (defn button-mask [buttons]
89 (reduce bit-or 0x0000 (map button-code buttons)))
91 (def current-state (atom nil))
93 (defn set-state! [^SaveState state]
94 (assert (:data state) "Not a valid state!")
95 (if (not @on?) (restart!))
96 (if (not= @current-state state)
97 (Gb/loadState (:data state)))
98 (reset! current-state state))
100 (defrecord Move [keys state])
102 (defn update-state []
103 (reset! current-state
104 (SaveState. (:frame @current-state)
105 (Gb/saveState))))
107 (defn step
108 ([^SaveState state buttons]
109 (set-state! state)
110 (Gb/step (button-mask buttons))
111 (reset! current-state
112 (SaveState. (inc (:frame state))(Gb/saveState))))
113 ([^SaveState state]
114 (step state [:listen]))
115 ([] (step (if @current-state @current-state (root)))))
117 (defn tick
118 ([] (tick @current-state))
119 ([state]
120 (set-state! state)
121 (Gb/tick)
122 (update-state)))
124 (defn move
125 [^Move move buttons]
126 (Move. (step (:state move) buttons) buttons))
128 (defn play
129 ([^SaveState state n]
130 (reduce (fn [s _] (step s)) state (range n)))
131 ([n]
132 (play @current-state n)))
134 (defn play-moves
135 ([moves [prev state]]
136 (set-state! state)
137 (dorun (map (fn [move] (step @current-state move)) moves))
138 [(concat prev moves) @current-state]))
140 ;;;;;;;;;;;
143 ;;;;;;;;;;;;;;; CPU data
145 (defn cpu-data [size arr-fn]
146 (let [store (int-array size)]
147 (fn get-data
148 ([] (get-data @current-state))
149 ([state]
150 (set-state! state) (arr-fn store) store))))
152 (defn write-cpu-data [size store-fn]
153 (fn [new-data]
154 (let [store (int-array new-data)]
155 (assert (= size (count new-data)))
156 (store-fn store))))
158 (def memory
159 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
161 (def ram
162 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))
164 (def rom
165 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))
167 (def working-ram
168 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
170 (def video-ram
171 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
173 (def registers
174 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
176 (def write-memory!
177 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))
179 (def write-registers!
180 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))
182 ;; (defn write-memory! [new-memory]
183 ;; (let [store (int-array new-memory)]
184 ;; (assert (= Gb/GB_MEMORY) (count new-memory))
185 ;; (Gb/writeMemory new-memory)))
188 ;; TODO add register names
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 (defmacro gen-get-set-register [name index]
193 (let [name-bang (symbol (str name "!"))]
194 `(do
195 (defn ~name
196 ~(str "Retrieve the " name " register from state, or "
197 "from @current-state if state is absent.")
198 ([state#]
199 (nth (registers state#) ~index))
200 ([]
201 (~name @current-state)))
202 (defn ~name-bang
203 ~(str "Set the " name " register for state, or "
204 "for @current-state if state is absent.")
205 ([state# new-register#]
206 (set-state! state#)
207 (let [registers# (registers state#)]
208 (aset registers# ~index new-register#)
209 (Gb/writeRegisters registers#)
210 (update-state)))
211 ([new-register#]
212 (~name-bang @current-state new-register#))))))
216 (gen-get-set-register PC 0)
217 (gen-get-set-register SP 1)
218 (gen-get-set-register AF 2)
219 (gen-get-set-register BC 3)
220 (gen-get-set-register DE 4)
221 (gen-get-set-register HL 5)
222 (gen-get-set-register IFF 6)
226 ;;;;;;;;;;;;;;;
228 (defmacro defn-memo
229 [& forms]
230 (let [fun-name (first forms)]
231 `(do
232 (defn ~@forms)
233 (alter-var-root (var ~fun-name) memoize))))