changeset 418:f211cd655ccb

completed basic music playing kernel
author Robert McIntyre <rlm@mit.edu>
date Tue, 17 Apr 2012 06:36:17 -0500
parents 0b6624c1291c
children b58a356f7cc2
files clojure/com/aurellem/run/music.clj
diffstat 1 files changed, 48 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/music.clj	Mon Apr 16 14:08:56 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/music.clj	Tue Apr 17 06:36:17 2012 -0500
     1.3 @@ -83,9 +83,9 @@
     1.4     0x18   ;; set frequency-low-bits
     1.5  
     1.6     0x7E   ;; load duration
     1.7 -   ;;0x2B   ;; 
     1.8 -   ;;0x2B
     1.9 -   ]) ;; HL-2 -> HL
    1.10 +   0x2B   ;; 
    1.11 +   0x2B   ;; HL-2 -> HL
    1.12 +   ]) 
    1.13  
    1.14  (defn music-step []
    1.15    (flatten
    1.16 @@ -93,14 +93,14 @@
    1.17      0xF5 ;; push A
    1.18      0xF0
    1.19      0x05 ;; load current ticks
    1.20 -    0x90 ;; B holds previous sub-ticks, subtract it from A
    1.21 +    0xB8 ;; B holds previous sub-ticks, subtract it from A
    1.22      ;; if A-B caused a carry, then (B > A) is true, and
    1.23      ;; A = current-sub-tics, B = previous-sub-ticks, so
    1.24      ;; current-sub-ticks < previous-sub-ticks, which means that the
    1.25      ;; timer counter HAS overflowed.
    1.26      0x30 ;; increment C only if last result caused carry
    1.27      0x01
    1.28 -    0x00;;0x0C
    1.29 +    0x0C
    1.30  
    1.31      0x47 ;; update sub-ticks (A->B)
    1.32  
    1.33 @@ -121,9 +121,28 @@
    1.34      0x0E
    1.35      0x00])) ;; 0->C (current-ticks)
    1.36  
    1.37 +(defn test-timer []
    1.38 +  (flatten
    1.39 +   [0x3E
    1.40 +    0x01
    1.41 +    0xE0
    1.42 +    0x06 ;; set TMA to 0
    1.43 +    
    1.44 +    0x3E
    1.45 +    (Integer/parseInt "00000100" 2)
    1.46 +    0xE0
    1.47 +    0x07 ;; set TAC to 16384 Hz and activate timer
    1.48 +    
    1.49 +    (repeat
    1.50 +     500
    1.51 +     [0xF0
    1.52 +      0x05])]))
    1.53 +
    1.54 +
    1.55  (defn music-kernel []
    1.56    (flatten
    1.57     [(clear-music-registers)
    1.58 +
    1.59      0x21
    1.60      0x00
    1.61      0xD0 ;; set HL to 0xD000 == music-start
    1.62 @@ -133,22 +152,40 @@
    1.63      0x00 ;; 0->B
    1.64  
    1.65      0x3E
    1.66 -    0x00
    1.67 +    0x01
    1.68      0xE0
    1.69      0x06 ;; set TMA to 0
    1.70      
    1.71      0x3E
    1.72      (Integer/parseInt "00000111" 2)
    1.73      0xE0
    1.74 -    0x07 ;; set TAC to 16384 Hz
    1.75 +    0x07 ;; set TAC to 16384 Hz and activate timer
    1.76  
    1.77 +    0xF0
    1.78 +    0x07
    1.79 +    
    1.80      (music-step)
    1.81      0x18
    1.82      (->signed-8-bit (+ (- (count (music-step)))
    1.83                         -2))]))
    1.84  
    1.85 +(def one-note
    1.86 +  [0xA0 0x00 0xFF])
    1.87  
    1.88 -(defn play-music [steps music-bytes]
    1.89 +(def many-notes
    1.90 +  (flatten (repeat 10 one-note)))
    1.91 +
    1.92 +(def increasing-notes
    1.93 +  [0xA0 0x00 0x55
    1.94 +   0xA1 0x00 0x55
    1.95 +   0xA2 0x00 0x55
    1.96 +   0xA3 0x00 0x55
    1.97 +   0xA4 0x00 0x55
    1.98 +   0xA5 0x00 0x55
    1.99 +   0xA6 0x00 0x55
   1.100 +   0xA7 0x00 0x55])
   1.101 +   
   1.102 +(defn play-music [music-bytes]
   1.103    (let [program-target 0xC000
   1.104          music-target 0xD000]
   1.105      (-> (set-memory-range (second (music-base))
   1.106 @@ -169,11 +206,12 @@
   1.107  
   1.108  
   1.109  (defn run-program
   1.110 -  ([program] (run-program program 90))
   1.111 -  ([program steps]
   1.112 +  ([program]
   1.113       (let [target 0xC000]
   1.114         (-> (set-memory-range (second (music-base))
   1.115                               target program)
   1.116             (PC! target)))))
   1.117  
   1.118 +(defn trippy []
   1.119 +  (run-moves (play-music many-notes ) (repeat 8000 [])))
   1.120