changeset 105:2f8089eacab9

working on some actual assembly programs
author Robert McIntyre <rlm@mit.edu>
date Tue, 13 Mar 2012 14:40:01 -0500
parents d2e00ea31508
children 3a60bb14a64a
files clojure/com/aurellem/assembly.clj clojure/com/aurellem/gb_driver.clj clojure/com/aurellem/items.clj
diffstat 3 files changed, 58 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clojure/com/aurellem/assembly.clj	Tue Mar 13 14:40:01 2012 -0500
     1.3 @@ -0,0 +1,55 @@
     1.4 +(ns com.aurellem.assembly
     1.5 +  (:use (com.aurellem gb-driver vbm title items))
     1.6 +  (:import [com.aurellem.gb_driver SaveState]))
     1.7 +
     1.8 +
     1.9 +
    1.10 +
    1.11 +(defn inject-assembly
    1.12 +  [^SaveState state
    1.13 +   program-counter registers
    1.14 +   assembly-code]
    1.15 +  (set-state! state)
    1.16 +  (let [scratch-memory (memory state)]
    1.17 +    ;; inject assembly code
    1.18 +    (dorun (map (fn [index val]
    1.19 +                  (aset scratch-memory index val))
    1.20 +                (range program-counter
    1.21 +                       (+ program-counter (count assembly-code)))
    1.22 +                assembly-code))
    1.23 +    (write-memory! scratch-memory)
    1.24 +    (set-state! (update-state))
    1.25 +    (write-registers! registers)
    1.26 +    (set-state! (update-state))
    1.27 +    (PC! program-counter)
    1.28 +    (update-state)))
    1.29 +
    1.30 +(defn inject-item-assembly
    1.31 +  ([^SaveState state assembly-code]
    1.32 +     (inject-assembly state (inc item-list-start)
    1.33 +                      (registers state)
    1.34 +                      assembly-code))
    1.35 +  ([assembly-code]
    1.36 +     (inject-item-assembly @current-state assembly-code)))
    1.37 +
    1.38 +(defn info
    1.39 +  ([^SaveState state]
    1.40 +     (set-state! state)
    1.41 +     (println "PC: " (PC))
    1.42 +     (println "Instruction:" (format "0x%02X" (aget (memory state) (PC))))
    1.43 +     state))    
    1.44 +
    1.45 +(defn irony [] (read-state 578544))
    1.46 +
    1.47 +(defn run-assembly
    1.48 +  ([info-fn assembly n]
    1.49 +     (let [final-state
    1.50 +           (reduce (fn [state _]
    1.51 +                     (tick (info-fn state)))
    1.52 +                   (inject-item-assembly (tick (tick (tick (irony)))) assembly)
    1.53 +                   (range n))]
    1.54 +       (shutdown!)
    1.55 +       final-state))
    1.56 +  ([assembly n]
    1.57 +     (run-assembly info assembly n)))
    1.58 +             
    1.59 \ No newline at end of file
     2.1 --- a/clojure/com/aurellem/gb_driver.clj	Tue Mar 13 00:03:02 2012 -0500
     2.2 +++ b/clojure/com/aurellem/gb_driver.clj	Tue Mar 13 14:40:01 2012 -0500
     2.3 @@ -93,8 +93,7 @@
     2.4  (defn set-state! [^SaveState state]
     2.5    (assert (:data state) "Not a valid state!")
     2.6    (if (not @on?) (restart!))
     2.7 -  (if (not= @current-state state)
     2.8 -    (Gb/loadState (:data state)))
     2.9 +  (Gb/loadState (:data state))
    2.10    (reset! current-state state))
    2.11  
    2.12  (defrecord Move [keys state])
     3.1 --- a/clojure/com/aurellem/items.clj	Tue Mar 13 00:03:02 2012 -0500
     3.2 +++ b/clojure/com/aurellem/items.clj	Tue Mar 13 14:40:01 2012 -0500
     3.3 @@ -196,7 +196,8 @@
     3.4                        (apply str (rest (str item-name)))
     3.5                        quantity)))
     3.6             (range 0 (count inv)) inv)
     3.7 -          ["+--+----------------+----------+\n"]))))))
     3.8 +          ["+--+----------------+----------+\n"]))))
     3.9 +     state))
    3.10  
    3.11  (defn inventory-codes [inventory]
    3.12    (flatten