# HG changeset patch # User Robert McIntyre # Date 1331357287 21600 # Node ID 26f002f2868ceb93743caf7ca71b64381ceaeb2f # Parent 95cb2152d7cd229720a69a7505b3eadff34c103b better functional version of earlier code diff -r 95cb2152d7cd -r 26f002f2868c clojure/com/aurellem/gb_funs.clj --- a/clojure/com/aurellem/gb_funs.clj Fri Mar 09 19:18:00 2012 -0600 +++ b/clojure/com/aurellem/gb_funs.clj Fri Mar 09 23:28:07 2012 -0600 @@ -79,7 +79,7 @@ :start 0x0008 ;; pseudo-buttons - :restart 0x0800 ; hard reset -- not really a button + :restart 0x0800 ; hard reset :listen -1 ; listen for user input }) @@ -88,17 +88,71 @@ (def current-state (atom nil)) + +(defn set-state! [^SaveState state] + (if (not @on?) (restart!)) + (if (not= @current-state state) + (Gb/loadState (:data state))) + (reset! current-state state)) + +(defrecord Move [keys state]) + (defn step ([^SaveState state buttons] - (if (not @on?) (restart!)) - (if (not= @current-state state) - (Gb/loadState (:data state))) + (set-state! state) (Gb/step (button-mask buttons)) (reset! current-state - (SaveState. (inc (:frame state))(Gb/saveState))))) - -(defn play + (SaveState. (inc (:frame state))(Gb/saveState)))) ([^SaveState state] (step state [:listen])) ([] (step (if @current-state @current-state (root))))) - + +(defn move + [^Move move buttons] + (Move. (step (:state move) buttons) buttons)) + + +(defn play + ([state n] + (reduce (fn [s _] (step s)) state (range n))) + ([state] + (dorun (iterate step state)))) +;;;;;;;;;;; + + +;;;;;;;;;;;;;;; CPU data + + + +(defn cpu-data [size arr-fn] + (let [store (int-array size)] + (fn [state] (set-state! state) (arr-fn store) store))) + +(def ram + (cpu-data (Gb/getRAMSize) #(Gb/getRAM %))) + +(def rom + (cpu-data (Gb/getROMSize) #(Gb/getROM %))) + +(def working-ram + (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %))) + +(def video-ram + (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %))) + +(def registers + (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %))) + +;; TODO add register names + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn AF [state] + (nth (registers state) 2)) + +(defn BC [state] + (nth (registers state) 3)) + + + + \ No newline at end of file diff -r 95cb2152d7cd -r 26f002f2868c clojure/com/aurellem/title2.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clojure/com/aurellem/title2.clj Fri Mar 09 23:28:07 2012 -0600 @@ -0,0 +1,47 @@ +(ns com.aurellem.title2 + (:use (com.aurellem gb-funs vbm))) + +(defn first-difference [base alt summary root] + (loop [branch-point root + actions []] + (let [base-branch (step branch-point base) + base-val (summary base-branch) + alt-branch (step branch-point alt) + alt-val (summary alt-branch)] + (if (not= base-val alt-val) + [(conj actions alt) alt-branch] + (recur base-branch (conj actions base)))))) + +(defn title [] + (let [[c-1 s-1] (first-difference [] [:a] AF (root)) + [c-2 s-2] (first-difference [] [:start] AF s-1) + [c-3 s-3] (first-difference [] [:a] AF s-2) + [c-4 s-4] (first-difference [] [:start] AF s-3)] + [(concat c-1 c-2 c-3 c-3 c-4) s-4])) + +(def menu-end (second (title))) + +(defn scroll-text [[commands state]] + (let [[c s] (first-difference [:b] [:a :b] AF state)] + [(concat commands c) s])) + +(defn oak [] + (-> [[] menu-end] + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text + scroll-text)) + +(def oak-end (second (oak))) + + +;; looks like it might need a monad here if this pattern continues \ No newline at end of file