view clojure/com/aurellem/gb_funs.clj @ 84:26f002f2868c

better functional version of earlier code
author Robert McIntyre <rlm@mit.edu>
date Fri, 09 Mar 2012 23:28:07 -0600
parents 95cb2152d7cd
children 3f4fdd270059
line wrap: on
line source
1 (ns com.aurellem.gb-funs
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-save! [^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 save))
25 (defn read-save [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 (write-save! (SaveState. 0 (Gb/saveState))))
59 (defn root []
60 (if (.exists (frame->filename 0))
61 (read-save 0)
62 (gen-root!)))
64 ;;;; Press Buttons
66 (def button-code
67 {;; main buttons
68 :a 0x0001
69 :b 0x0002
71 ;; directional pad
72 :r 0x0010
73 :l 0x0020
74 :u 0x0040
75 :d 0x0080
77 ;; meta buttons
78 :select 0x0004
79 :start 0x0008
81 ;; pseudo-buttons
82 :restart 0x0800 ; hard reset
83 :listen -1 ; listen for user input
84 })
86 (defn button-mask [buttons]
87 (reduce bit-or 0x0000 (map button-code buttons)))
89 (def current-state (atom nil))
92 (defn set-state! [^SaveState state]
93 (if (not @on?) (restart!))
94 (if (not= @current-state state)
95 (Gb/loadState (:data state)))
96 (reset! current-state state))
98 (defrecord Move [keys state])
100 (defn step
101 ([^SaveState state buttons]
102 (set-state! state)
103 (Gb/step (button-mask buttons))
104 (reset! current-state
105 (SaveState. (inc (:frame state))(Gb/saveState))))
106 ([^SaveState state]
107 (step state [:listen]))
108 ([] (step (if @current-state @current-state (root)))))
110 (defn move
111 [^Move move buttons]
112 (Move. (step (:state move) buttons) buttons))
115 (defn play
116 ([state n]
117 (reduce (fn [s _] (step s)) state (range n)))
118 ([state]
119 (dorun (iterate step state))))
120 ;;;;;;;;;;;
123 ;;;;;;;;;;;;;;; CPU data
127 (defn cpu-data [size arr-fn]
128 (let [store (int-array size)]
129 (fn [state] (set-state! state) (arr-fn store) store)))
131 (def ram
132 (cpu-data (Gb/getRAMSize) #(Gb/getRAM %)))
134 (def rom
135 (cpu-data (Gb/getROMSize) #(Gb/getROM %)))
137 (def working-ram
138 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
140 (def video-ram
141 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
143 (def registers
144 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
146 ;; TODO add register names
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 (defn AF [state]
151 (nth (registers state) 2))
153 (defn BC [state]
154 (nth (registers state) 3))