view clojure/com/aurellem/gb_driver.clj @ 99:9fad96094950

can now read and set the inventory of a state
author Robert McIntyre <rlm@mit.edu>
date Mon, 12 Mar 2012 09:59:26 -0500
parents cb487c4ce5c0
children 2090bcb78f44
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 move
118 [^Move move buttons]
119 (Move. (step (:state move) buttons) buttons))
121 (defn play
122 ([^SaveState state n]
123 (reduce (fn [s _] (step s)) state (range n)))
124 ([n]
125 (play @current-state n)))
127 (defn play-moves
128 ([moves [prev state]]
129 (set-state! state)
130 (dorun (map (fn [move] (step @current-state move)) moves))
131 [(concat prev moves) @current-state]))
133 ;;;;;;;;;;;
136 ;;;;;;;;;;;;;;; CPU data
138 (defn cpu-data [size arr-fn]
139 (let [store (int-array size)]
140 (fn get-data
141 ([] (get-data @current-state))
142 ([state]
143 (set-state! state) (arr-fn store) store))))
145 (def memory
146 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
148 (def ram
149 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))
151 (def rom
152 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))
154 (def working-ram
155 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
157 (def video-ram
158 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
160 (def registers
161 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
164 (defn write-memory! [new-memory]
165 (let [store (int-array new-memory)]
166 (assert (= Gb/GB_MEMORY) (count new-memory))
167 (Gb/writeMemory new-memory)))
170 ;; TODO add register names
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
174 (defn AF [state]
175 (nth (registers state) 2))
177 (defn BC [state]
178 (nth (registers state) 3))
180 (defn DE [state]
181 (nth (registers state) 4))
183 ;;;;;;;;;;;;;;;
185 (defmacro defn-memo
186 [& forms]
187 (let [fun-name (first forms)]
188 `(do
189 (defn ~@forms)
190 (alter-var-root (var ~fun-name) memoize))))