view clojure/com/aurellem/gb/gb_driver.clj @ 518:bb4434c5a8f3

generated gameboy palette for gimp for Dylan.
author Robert McIntyre <rlm@mit.edu>
date Sat, 23 Jun 2012 14:27:01 -0500
parents a6d060a64246
children 5596cd4a3fc6
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 ;;;;;;;;;;;
160 ;; Get Screen Pixels, Save Screenshot
162 (defn write-png!
163 ([^SaveState state ^File target]
164 (set-state! state)
165 (Gb/nwritePNG (.getCanonicalPath target)))
166 ([^File target]
167 (write-png! @current-state target)))
172 ;;;;;;;;;;;;;;; CPU data
174 (defn cpu-data [size arr-fn]
175 (let [store (int-array size)]
176 (fn get-data
177 ([] (get-data @current-state))
178 ([state]
179 (set-state! state) (arr-fn store) store))))
181 (defn write-cpu-data [size store-fn]
182 (fn store-data
183 ([state new-data]
184 (set-state! state)
185 (let [store (int-array new-data)]
186 (assert (= size (count new-data)))
187 (store-fn store)
188 (update-state)))
189 ([new-data]
190 (store-data @current-state new-data))))
192 (def memory
193 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
195 (def ram
196 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))
198 (def rom
199 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))
201 (def working-ram
202 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
204 (def video-ram
205 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
207 (def registers
208 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
210 (def pixels
211 (cpu-data (* Gb/DISPLAY_WIDTH Gb/DISPLAY_HEIGHT)
212 #(Gb/getPixels %)))
214 (def write-memory!
215 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))
217 (def write-registers!
218 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))
220 (def write-rom!
221 (write-cpu-data Gb/ROM_SIZE #(Gb/writeROM %)))
223 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 (defmacro gen-get-set-register [name index]
226 (let [name-bang (symbol (str name "!"))]
227 `(do
228 (defn ~name
229 ~(str "Retrieve the " name " register from state, or "
230 "from @current-state if state is absent.")
231 ([state#]
232 (nth (registers state#) ~index))
233 ([]
234 (~name @current-state)))
235 (defn ~name-bang
236 ~(str "Set the " name " register for state, or "
237 "for @current-state if state is absent.")
238 ([state# new-register#]
239 (set-state! state#)
240 (let [registers# (registers state#)]
241 (aset registers# ~index new-register#)
242 (Gb/writeRegisters registers#)
243 (update-state)))
244 ([new-register#]
245 (~name-bang @current-state new-register#))))))
247 ;; 16 bit registers
248 (gen-get-set-register PC 0)
249 (gen-get-set-register SP 1)
250 (gen-get-set-register AF 2)
251 (gen-get-set-register BC 3)
252 (gen-get-set-register DE 4)
253 (gen-get-set-register HL 5)
254 (gen-get-set-register IFF 6)
256 ;; 8 bit registers
257 (gen-get-set-register DIV 7)
258 (gen-get-set-register TIMA 8)
259 (gen-get-set-register TMA 9)
260 (gen-get-set-register IF 11)
261 (gen-get-set-register LCDC 12)
262 (gen-get-set-register STAT 13)
263 (gen-get-set-register SCY 14)
264 (gen-get-set-register SCX 15)
265 (gen-get-set-register LY 16)
266 (gen-get-set-register DMA 18)
267 (gen-get-set-register WY 19)
268 (gen-get-set-register WX 20)
269 (gen-get-set-register VBK 21)
270 (gen-get-set-register HDMA1 22)
271 (gen-get-set-register HDMA2 23)
272 (gen-get-set-register HDMA3 24)
273 (gen-get-set-register HDMA4 25)
274 (gen-get-set-register HDMA5 26)
275 (gen-get-set-register SVBK 27)
276 (gen-get-set-register IE 28)
278 ;;;;;;;;;;;;;;;
280 (defmacro defn-memo
281 [& forms]
282 (let [fun-name (first forms)]
283 `(do
284 (defn ~@forms)
285 (alter-var-root (var ~fun-name) memoize))))
287 (def original-rom (rom (root)))
290 ;; RGB colors in an image are not the same as those in a GameBoy, so I
291 ;; need to convert them. Fortunately, this code is already written
292 ;; for me in this C-code from the public domain hi-color converter by
293 ;; Glen Cook, Jeff Frohwein, and Rob Jones.
294 (defn rgb->gb-rb [[r g b :as color]]
295 (let [store (int-array 3)]
296 (Gb/translateRGB (int-array color) store)
297 (vec store)))