view clojure/com/aurellem/gb/gb_driver.clj @ 522:5596cd4a3fc6

added RAM display.
author Robert McIntyre <rlm@mit.edu>
date Sat, 23 Jun 2012 19:12:26 -0500
parents a6d060a64246
children d00096b6bf17
line wrap: on
line source
1 (ns com.aurellem.gb.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
9 (defrecord SaveState [data])
11 (def user-home (File. (System/getProperty "user.home")))
13 (def ^:dynamic *save-state-cache*
14 (File. user-home "proj/vba-clojure/save-states/"))
16 (def current-state (atom nil))
18 (defn state-cache-file [name]
19 (File. *save-state-cache* (str name ".sav")))
21 (defn write-state!
22 ([^SaveState name]
23 (write-state! @current-state name))
24 ([^SaveState save ^String name]
25 (let [buffer (:data save)
26 bytes (byte-array (.limit buffer))
27 dest (state-cache-file name)]
28 (.get buffer bytes)
29 (FileUtils/writeByteArrayToFile dest bytes)
30 (.rewind buffer)
31 dest)))
33 (defn read-state [name]
34 (let [save (state-cache-file name)]
35 (assert (.exists save))
36 (let [buffer (Gb/saveBuffer)
37 bytes (FileUtils/readFileToByteArray save)]
38 (.put buffer bytes)
39 (.flip buffer)
40 (SaveState. buffer))))
41 ;;;;;;;;;;;;;;;;
44 ;; Gameboy management
45 (Gb/loadVBA)
47 (def yellow-rom-image
48 (File. user-home "proj/pokemon-escape/roms/yellow.gbc"))
50 (def yellow-save-file
51 (File. user-home "proj/pokemon-escape/roms/yellow.sav"))
53 (def on? (atom nil))
55 (defn shutdown! [] (Gb/shutdown) (reset! on? false))
57 (defn restart! []
58 (shutdown!)
59 (.delete yellow-save-file)
60 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))
61 (reset! on? true))
63 ;;; The first state!
64 (defn gen-root! []
65 (restart!)
66 (let [state (SaveState. (Gb/saveState))]
67 (write-state! state "root" ) state))
69 (defn root []
70 (if (.exists (state-cache-file "root"))
71 (read-state "root")
72 (gen-root!)))
74 ;;;; Press Buttons
76 (def button-code
77 {;; main buttons
78 :a 0x0001
79 :b 0x0002
81 ;; directional pad
82 :r 0x0010
83 :l 0x0020
84 :u 0x0040
85 :d 0x0080
87 ;; meta buttons
88 :select 0x0004
89 :start 0x0008
91 ;; pseudo-buttons
92 :restart 0x0800 ; hard reset
93 :listen -1 ; listen for user input
94 })
96 (defn button-mask [buttons]
97 (reduce bit-or 0x0000 (map button-code buttons)))
99 (defn set-state! [^SaveState state]
100 (assert (:data state) "Not a valid state!")
101 (if (not @on?) (restart!))
102 (if (not= state @current-state)
103 (do
104 (Gb/loadState (:data state))
105 (reset! current-state state))))
107 (defn update-state []
108 (reset! current-state
109 (SaveState. (Gb/saveState))))
111 (defn step
112 ([^SaveState state buttons]
113 (set-state! state)
114 (Gb/step (button-mask buttons))
115 (reset! current-state
116 (SaveState. (Gb/saveState))))
117 ([^SaveState state]
118 (step state [:listen]))
119 ([] (step (if @current-state @current-state (root)))))
121 (defn tick
122 ([] (tick @current-state))
123 ([^SaveState state]
124 (set-state! state)
125 (Gb/tick)
126 (update-state)))
128 (defn play
129 ([^SaveState state n]
130 (try
131 (set-state! state)
132 (dorun (dotimes [_ n]
133 (Thread/sleep 1)
134 (Gb/step)))
135 (finally
136 (update-state))))
137 ([n]
138 (play @current-state n)))
140 (defn continue!
141 ([state]
142 (play state Integer/MAX_VALUE))
143 ([]
144 (continue! @current-state)))
146 (defn run-moves [state moves]
147 (set-state! state)
148 (dorun (map #(Gb/step (button-mask %))
149 moves))
150 (update-state))
152 (defn play-moves
153 ([moves [prev state]]
154 (set-state! state)
155 (dorun (map (fn [move] (step @current-state move)) moves))
156 [(concat prev moves) @current-state]))
158 (defn accurate-memory
159 ([^SaveState state address]
160 (set-state! state)
161 (Gb/readMemory address))
162 ([address]
163 (accurate-memory @current-state address)))
165 ;;;;;;;;;;;
167 ;; Get Screen Pixels, Save Screenshot
169 (defn write-png!
170 ([^SaveState state ^File target]
171 (set-state! state)
172 (Gb/nwritePNG (.getCanonicalPath target)))
173 ([^File target]
174 (write-png! @current-state target)))
176 ;;;;;;;;;;;;;;; CPU data
178 (defn cpu-data [size arr-fn]
179 (let [store (int-array size)]
180 (fn get-data
181 ([] (get-data @current-state))
182 ([state]
183 (set-state! state) (arr-fn store) store))))
185 (defn write-cpu-data [size store-fn]
186 (fn store-data
187 ([state new-data]
188 (set-state! state)
189 (let [store (int-array new-data)]
190 (assert (= size (count new-data)))
191 (store-fn store)
192 (update-state)))
193 ([new-data]
194 (store-data @current-state new-data))))
196 (def memory
197 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
199 (def ram
200 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))
202 (def rom
203 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))
205 (def working-ram
206 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
208 (def video-ram
209 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
211 (def registers
212 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
214 (def pixels
215 (cpu-data (* Gb/DISPLAY_WIDTH Gb/DISPLAY_HEIGHT)
216 #(Gb/getPixels %)))
218 (def write-memory!
219 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))
221 (def write-registers!
222 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))
224 (def write-rom!
225 (write-cpu-data Gb/ROM_SIZE #(Gb/writeROM %)))
227 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 (defmacro gen-get-set-register [name index]
230 (let [name-bang (symbol (str name "!"))]
231 `(do
232 (defn ~name
233 ~(str "Retrieve the " name " register from state, or "
234 "from @current-state if state is absent.")
235 ([state#]
236 (nth (registers state#) ~index))
237 ([]
238 (~name @current-state)))
239 (defn ~name-bang
240 ~(str "Set the " name " register for state, or "
241 "for @current-state if state is absent.")
242 ([state# new-register#]
243 (set-state! state#)
244 (let [registers# (registers state#)]
245 (aset registers# ~index new-register#)
246 (Gb/writeRegisters registers#)
247 (update-state)))
248 ([new-register#]
249 (~name-bang @current-state new-register#))))))
251 ;; 16 bit registers
252 (gen-get-set-register PC 0)
253 (gen-get-set-register SP 1)
254 (gen-get-set-register AF 2)
255 (gen-get-set-register BC 3)
256 (gen-get-set-register DE 4)
257 (gen-get-set-register HL 5)
258 (gen-get-set-register IFF 6)
260 ;; 8 bit registers
261 (gen-get-set-register DIV 7)
262 (gen-get-set-register TIMA 8)
263 (gen-get-set-register TMA 9)
264 (gen-get-set-register IF 11)
265 (gen-get-set-register LCDC 12)
266 (gen-get-set-register STAT 13)
267 (gen-get-set-register SCY 14)
268 (gen-get-set-register SCX 15)
269 (gen-get-set-register LY 16)
270 (gen-get-set-register DMA 18)
271 (gen-get-set-register WY 19)
272 (gen-get-set-register WX 20)
273 (gen-get-set-register VBK 21)
274 (gen-get-set-register HDMA1 22)
275 (gen-get-set-register HDMA2 23)
276 (gen-get-set-register HDMA3 24)
277 (gen-get-set-register HDMA4 25)
278 (gen-get-set-register HDMA5 26)
279 (gen-get-set-register SVBK 27)
280 (gen-get-set-register IE 28)
282 ;;;;;;;;;;;;;;;
284 (defmacro defn-memo
285 [& forms]
286 (let [fun-name (first forms)]
287 `(do
288 (defn ~@forms)
289 (alter-var-root (var ~fun-name) memoize))))
291 (def original-rom (rom (root)))
294 ;; RGB colors in an image are not the same as those in a GameBoy, so I
295 ;; need to convert them. Fortunately, this code is already written
296 ;; for me in this C-code from the public domain hi-color converter by
297 ;; Glen Cook, Jeff Frohwein, and Rob Jones.
298 (defn rgb->gb-rb [[r g b :as color]]
299 (let [store (int-array 3)]
300 (Gb/translateRGB (int-array color) store)
301 (vec store)))