view clojure/com/aurellem/gb/gb_driver.clj @ 524:7ef5c73ea8fa

working on recording sound. almost have it, but there is still unexplained popping sounds.
author Robert McIntyre <rlm@mit.edu>
date Sat, 23 Jun 2012 23:10:31 -0500
parents d00096b6bf17
children fa7676dbf6f2
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 (let [store (byte-array Gb/SOUND_SIZE)]
197 (defn sound-data
198 ([](sound-data @current-state))
199 ([state]
200 (set-state! state)
201 (Gb/getFrameSound store)
202 (Gb/setSoundFrameWritten 0)
203 store)))
205 (def memory
206 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
208 (def ram
209 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))
211 (def rom
212 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))
214 (def working-ram
215 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
217 (def video-ram
218 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
220 (def registers
221 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
223 (def pixels
224 (cpu-data (* Gb/DISPLAY_WIDTH Gb/DISPLAY_HEIGHT)
225 #(Gb/getPixels %)))
227 (def write-memory!
228 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))
230 (def write-registers!
231 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))
233 (def write-rom!
234 (write-cpu-data Gb/ROM_SIZE #(Gb/writeROM %)))
236 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;
238 (defmacro gen-get-set-register [name index]
239 (let [name-bang (symbol (str name "!"))]
240 `(do
241 (defn ~name
242 ~(str "Retrieve the " name " register from state, or "
243 "from @current-state if state is absent.")
244 ([state#]
245 (nth (registers state#) ~index))
246 ([]
247 (~name @current-state)))
248 (defn ~name-bang
249 ~(str "Set the " name " register for state, or "
250 "for @current-state if state is absent.")
251 ([state# new-register#]
252 (set-state! state#)
253 (let [registers# (registers state#)]
254 (aset registers# ~index new-register#)
255 (Gb/writeRegisters registers#)
256 (update-state)))
257 ([new-register#]
258 (~name-bang @current-state new-register#))))))
260 ;; 16 bit registers
261 (gen-get-set-register PC 0)
262 (gen-get-set-register SP 1)
263 (gen-get-set-register AF 2)
264 (gen-get-set-register BC 3)
265 (gen-get-set-register DE 4)
266 (gen-get-set-register HL 5)
267 (gen-get-set-register IFF 6)
269 ;; 8 bit registers
270 (gen-get-set-register DIV 7)
271 (gen-get-set-register TIMA 8)
272 (gen-get-set-register TMA 9)
273 (gen-get-set-register IF 11)
274 (gen-get-set-register LCDC 12)
275 (gen-get-set-register STAT 13)
276 (gen-get-set-register SCY 14)
277 (gen-get-set-register SCX 15)
278 (gen-get-set-register LY 16)
279 (gen-get-set-register DMA 18)
280 (gen-get-set-register WY 19)
281 (gen-get-set-register WX 20)
282 (gen-get-set-register VBK 21)
283 (gen-get-set-register HDMA1 22)
284 (gen-get-set-register HDMA2 23)
285 (gen-get-set-register HDMA3 24)
286 (gen-get-set-register HDMA4 25)
287 (gen-get-set-register HDMA5 26)
288 (gen-get-set-register SVBK 27)
289 (gen-get-set-register IE 28)
291 ;;;;;;;;;;;;;;;
293 (defmacro defn-memo
294 [& forms]
295 (let [fun-name (first forms)]
296 `(do
297 (defn ~@forms)
298 (alter-var-root (var ~fun-name) memoize))))
300 (def original-rom (rom (root)))
303 ;; RGB colors in an image are not the same as those in a GameBoy, so I
304 ;; need to convert them. Fortunately, this code is already written
305 ;; for me in this C-code from the public domain hi-color converter by
306 ;; Glen Cook, Jeff Frohwein, and Rob Jones.
307 (defn rgb->gb-rb [[r g b :as color]]
308 (let [store (int-array 3)]
309 (Gb/translateRGB (int-array color) store)
310 (vec store)))