diff clojure/com/aurellem/assembly.clj @ 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
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