changeset 107:ad96e9464d6a

working on the start of the bootstrap program; expanded register array to 29 elements
author Robert McIntyre <rlm@mit.edu>
date Thu, 15 Mar 2012 03:24:05 -0500
parents 3a60bb14a64a
children d3c0b990e163
files clojure/com/aurellem/assembly.clj clojure/com/aurellem/gb_driver.clj clojure/com/aurellem/items.clj java/src/com/aurellem/gb/Gb.java
diffstat 4 files changed, 95 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/assembly.clj	Wed Mar 14 21:37:37 2012 -0500
     1.2 +++ b/clojure/com/aurellem/assembly.clj	Thu Mar 15 03:24:05 2012 -0500
     1.3 @@ -6,7 +6,7 @@
     1.4    (read-state "mid-game"))
     1.5  
     1.6  (defn inject-assembly
     1.7 -  [^SaveState state
     1.8 +  ([^SaveState state
     1.9     program-counter registers
    1.10     assembly-code]
    1.11    (let [scratch-memory (memory state)]
    1.12 @@ -20,7 +20,14 @@
    1.13          (write-memory! scratch-memory)
    1.14          (write-registers! registers)
    1.15          (PC! program-counter))))
    1.16 -        
    1.17 +  ;;([program-counter]
    1.18 +  ;;   (fn [^SaveState state registers assembly-code]
    1.19 +  ;;     (inject-assembly state program-counter registers assembly-code)))
    1.20 +  )
    1.21 +
    1.22 +
    1.23 +;;(def inject-assembly-item
    1.24 +;;  (inject-assembly (inc item-list-start)))
    1.25  
    1.26  (defn inject-item-assembly
    1.27    ([^SaveState state assembly-code]
    1.28 @@ -32,20 +39,99 @@
    1.29  
    1.30  (defn info
    1.31    ([^SaveState state]
    1.32 -     (println "PC: " (PC state))
    1.33 +     (println (format "PC: 0x%04X" (PC state)))
    1.34       (println "Instruction:"
    1.35                (format "0x%02X" (aget (memory state) (PC state))))
    1.36       state))    
    1.37  
    1.38 +(defn print-interrupt
    1.39 +  [^SaveState state]
    1.40 +  (println (format "IE: %d" (IE state)))
    1.41 +  state)
    1.42 +
    1.43  (defn run-assembly
    1.44    ([info-fn assembly n]
    1.45       (let [final-state
    1.46             (reduce (fn [state _]
    1.47                       (tick (info-fn state)))
    1.48 -                   (inject-item-assembly (tick (tick (tick (mid-game)))) assembly)
    1.49 +                   (inject-item-assembly
    1.50 +                     (mid-game) assembly)
    1.51                     (range n))]
    1.52 -       (shutdown!)
    1.53         final-state))
    1.54    ([assembly n]
    1.55       (run-assembly info assembly n)))
    1.56 -             
    1.57 \ No newline at end of file
    1.58 +
    1.59 +
    1.60 +(def buttons-port 0xFF00)
    1.61 +
    1.62 +(defn view-register [state name reg-fn]
    1.63 +  (println (format "%s : 0x%02X" name (reg-fn state)))
    1.64 +  state)
    1.65 +
    1.66 +(defn A [state]
    1.67 +  (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
    1.68 +
    1.69 +(defn view-memory [state mem]
    1.70 +  (println (format "mem 0x%04X = 0x%02X" mem (aget (memory state) mem)))
    1.71 +  state)
    1.72 +
    1.73 +(defn read-buttons []
    1.74 +  (let [button-pressed (tick (step (mid-game) [:d]))]
    1.75 +    (-> button-pressed
    1.76 +        (IE! 0)
    1.77 +        (inject-item-assembly
    1.78 +         (concat
    1.79 +          ;; write 00010000 to 0xFF00 to select joypad
    1.80 +          [0x18   ;D31D                 ; jump over          
    1.81 +           0x01   ;D31E                 ; the next 8 bits
    1.82 +           (Integer/parseInt "00010000" 2) ;D31F data section 
    1.83 +           0x00   ;D320                 ; take a break
    1.84 +           0xFA   ;D321                 ; put DC15 into A
    1.85 +           0x1F   ;D322      --]       
    1.86 +           0xD3   ;D323      --]  DC1F  ; data section
    1.87 +           0x01   ;D324                 ; load 0xFF00 into BC
    1.88 +           0x00   ;D325
    1.89 +           0xFF   ;D326
    1.90 +           0x02   ;D327                 ; load (DC1F) into FF00
    1.91 +                                        ; to select directional
    1.92 +                                        ; buttons
    1.93 +
    1.94 +           ]
    1.95 +
    1.96 +          []))
    1.97 +        (info)
    1.98 +        (tick)  ;; skip over data section
    1.99 +        (info)  
   1.100 +        (tick)  ;; no-op
   1.101 +        (info)
   1.102 +        (view-register "Register A" A)
   1.103 +        (tick)  ;; load-data into A
   1.104 +        (view-register "Register A" A)
   1.105 +        (info)
   1.106 +        (view-register "Register BC" BC)
   1.107 +        (tick) ;; load A into 0xFF00
   1.108 +        (view-register "Register BC" BC)
   1.109 +        )))
   1.110 +          
   1.111 +         
   1.112 +(defn trace [state]
   1.113 +  (loop [program-counters []
   1.114 +         opcodes []]
   1.115 +    (let [frame-boundary?
   1.116 +          (com.aurellem.gb.Gb/tick)]
   1.117 +      (println (count opcodes))
   1.118 +      (if frame-boundary?
   1.119 +        [program-counters opcodes]
   1.120 +        (recur
   1.121 +         (conj program-counters
   1.122 +               (first (registers @current-state)))
   1.123 +         (conj opcodes
   1.124 +               (aget (memory @current-state)
   1.125 +                     (PC @current-state))))))))
   1.126 +        
   1.127 +  
   1.128 +(defn good-trace []
   1.129 +  (-> (mid-game) (tick) (IE! 0)
   1.130 +      (set-inv-mem [0x00 0x00 0X00 0x00])
   1.131 +      (PC! item-list-start)(print-interrupt)
   1.132 +      (info) (tick) (info) (tick) (info)))
   1.133 \ No newline at end of file
     2.1 --- a/clojure/com/aurellem/gb_driver.clj	Wed Mar 14 21:37:37 2012 -0500
     2.2 +++ b/clojure/com/aurellem/gb_driver.clj	Thu Mar 15 03:24:05 2012 -0500
     2.3 @@ -116,7 +116,7 @@
     2.4  
     2.5  (defn tick
     2.6    ([] (tick @current-state))
     2.7 -  ([state]
     2.8 +  ([^SaveState state]
     2.9       (set-state! state)
    2.10       (Gb/tick)
    2.11       (update-state)))
     3.1 --- a/clojure/com/aurellem/items.clj	Wed Mar 14 21:37:37 2012 -0500
     3.2 +++ b/clojure/com/aurellem/items.clj	Thu Mar 15 03:24:05 2012 -0500
     3.3 @@ -14,8 +14,6 @@
     3.4     item-list-start
     3.5     (+ item-list-start 150)))
     3.6  
     3.7 -(def item-hack-3 (read-state 77557))
     3.8 -
     3.9  (def item-code->item-name
    3.10    (hash-map
    3.11     0x01 :master-ball       
    3.12 @@ -158,8 +156,7 @@
    3.13     0xFC :TM52     ;; "fly"	   
    3.14     0xFD :TM53     ;; "surf"	   
    3.15     0xFE :TM54     ;; "strength"      
    3.16 -   0xFF :end-of-list-sentinel
    3.17 -   ))
    3.18 +   0xFF :end-of-list-sentinel))
    3.19  
    3.20  (def item-name->item-code
    3.21    (zipmap (vals item-code->item-name)
     4.1 --- a/java/src/com/aurellem/gb/Gb.java	Wed Mar 14 21:37:37 2012 -0500
     4.2 +++ b/java/src/com/aurellem/gb/Gb.java	Thu Mar 15 03:24:05 2012 -0500
     4.3 @@ -97,7 +97,7 @@
     4.4      
     4.5      public static final int ROM_SIZE = 0x100000;
     4.6  
     4.7 -    public static final int NUM_REGISTERS = 28;
     4.8 +    public static final int NUM_REGISTERS = 29;
     4.9  
    4.10      public static final int GB_MEMORY = 0x10000;
    4.11