comparison clojure/com/aurellem/run/music.clj @ 433:985c90ffa1fe

paramaterized do-message
author Robert McIntyre <rlm@mit.edu>
date Wed, 25 Apr 2012 01:56:39 -0500
parents 8e88366a81b9
children 988f0fda6c93
comparison
equal deleted inserted replaced
432:8e88366a81b9 433:985c90ffa1fe
112 112
113 (defrecord Bit-Note [frequency volume duration duty]) 113 (defrecord Bit-Note [frequency volume duration duty])
114 114
115 (defn clear-music-registers [] 115 (defn clear-music-registers []
116 (flatten 116 (flatten
117 [(store (Integer/parseInt "00000000" 2) 0xFF10) 117 [(store (Integer/parseInt "00000000" 2) 0xFF10) ;; sweep
118 (store (Integer/parseInt "00000000" 2) 0xFF11) 118 (store (Integer/parseInt "00000000" 2) 0xFF11) ;; pattern duty
119 (store (Integer/parseInt "00000000" 2) 0xFF12) 119 (store (Integer/parseInt "00000000" 2) 0xFF12) ;; volume
120 (store (Integer/parseInt "00000000" 2) 0xFF13) 120 (store (Integer/parseInt "00000000" 2) 0xFF13) ;; frequency-low
121 (store (Integer/parseInt "00000000" 2) 0xFF14) 121 (store (Integer/parseInt "00000000" 2) 0xFF14) ;; frequency-high
122 122
123 (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000 123 (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000
124 (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000 124 (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000
125 (store (Integer/parseInt "00000000" 2) 0xFF18) ;; frequency-low 125 (store (Integer/parseInt "00000000" 2) 0xFF18) ;; frequency-low
126 (store (Integer/parseInt "00000000" 2) 0xFF19) ;; 00000 frequency-high 126 (store (Integer/parseInt "00000000" 2) 0xFF19) ;; 00000 frequency-high
129 (store (Integer/parseInt "00000000" 2) 0xFF1B) 129 (store (Integer/parseInt "00000000" 2) 0xFF1B)
130 (store (Integer/parseInt "00000000" 2) 0xFF1C) 130 (store (Integer/parseInt "00000000" 2) 0xFF1C)
131 (store (Integer/parseInt "00000000" 2) 0xFF1D) 131 (store (Integer/parseInt "00000000" 2) 0xFF1D)
132 (store (Integer/parseInt "00000000" 2) 0xFF1E) 132 (store (Integer/parseInt "00000000" 2) 0xFF1E)
133 133
134 (store (Integer/parseInt "00000000" 2) 0xFF20) 134 (store (Integer/parseInt "00000000" 2) 0xFF20) ;; length
135 (store (Integer/parseInt "00000000" 2) 0xFF21) 135 (store (Integer/parseInt "00000000" 2) 0xFF21) ;; volume
136 (store (Integer/parseInt "00000000" 2) 0xFF22) 136 (store (Integer/parseInt "00000000" 2) 0xFF22) ;; noise-frequency
137 (store (Integer/parseInt "00000000" 2) 0xFF23)])) 137 (store (Integer/parseInt "00000000" 2) 0xFF23) ;; control
138 ]))
138 139
139 140
140 ;; mini-midi syntax 141 ;; mini-midi syntax
141 142
142 ;; codes 143 ;; codes
167 168
168 (defn do-message 169 (defn do-message
169 "Read the message which starts at the current value of HL and do 170 "Read the message which starts at the current value of HL and do
170 what it says. Duration is left in A, and HL is advanced 171 what it says. Duration is left in A, and HL is advanced
171 appropraitely." 172 appropraitely."
172 [] 173 ([] (do-message 0x16))
173 (let [switch 174 ([sound-base-address]
174 [0x2A ;; load message code into A, increment HL 175 (let [switch
175 176 [0x2A ;; load message code into A, increment HL
176 ;; switch on message 177
177 0xFE 178 ;; switch on message
178 note-code 179 0xFE
179 180 note-code
180 0x20 181
181 :note-length] 182 0x20
182 183 :note-length]
183 play-note 184
184 [0x2A ;; load volume/frequency-high info 185 play-note
185 0xF5 ;; push A 186 [0x2A ;; load volume/frequency-high info
186 0xE6 187 0xF5 ;; push A
187 (Integer/parseInt "11110000" 2) ;; volume mask 188 0xE6
188 0xE0 189 (Integer/parseInt "11110000" 2) ;; volume mask
189 0x17 ;; set volume 190 0xE0
190 0xF1 ;; pop A 191 (inc sound-base-address) ;;0x17 ;; set volume
191 0xE6 192 0xF1 ;; pop A
192 (Integer/parseInt "00000111" 2) ;; frequency-high mask 193 0xE6
193 0xE0 194 (Integer/parseInt "00000111" 2) ;; frequency-high mask
194 0x19 ;; set frequency-high 195 0xE0
195 196 (+ 3 sound-base-address) ;;0x19 ;; set frequency-high
196 0x2A ;; load frequency low-bits 197
197 0xE0 198 0x2A ;; load frequency low-bits
198 0x18 ;; set frequency-low-bits 199 0xE0
199 200 (+ 2 sound-base-address) ;;0x18 ;; set frequency-low-bits
200 0x2A]] ;; load duration 201 0x2A]] ;; load duration
201 (replace 202 (replace
202 {:note-length (count play-note)} 203 {:note-length (count play-note)}
203 (concat switch play-note)))) 204 (concat switch play-note)))))
204 205
205 (defn play-note 206 ;; (defn play-note
206 "Play the note referenced by HL in the appropiate channel. 207 ;; "Play the note referenced by HL in the appropiate channel.
207 Leaves desired-duration in A." 208 ;; Leaves desired-duration in A."
208 [] 209
209 [0x2A ;; load volume/frequency-high info 210 ;; [0x2A ;; load volume/frequency-high info
210 0xF5 ;; push A 211 ;; 0xF5 ;; push A
211 0xE6 212 ;; 0xE6
212 (Integer/parseInt "11110000" 2) ;; volume mask 213 ;; (Integer/parseInt "11110000" 2) ;; volume mask
213 0xE0 214 ;; 0xE0
214 0x17 ;; set volume 215 ;; 0x17 ;; set volume
215 0xF1 ;; pop A 216 ;; 0xF1 ;; pop A
216 0xE6 217 ;; 0xE6
217 (Integer/parseInt "00000111" 2) ;; frequency-high mask 218 ;; (Integer/parseInt "00000111" 2) ;; frequency-high mask
218 0xE0 219 ;; 0xE0
219 0x19 ;; set frequency-high 220 ;; 0x19 ;; set frequency-high
220 221
221 0x2A ;; load frequency low-bits 222 ;; 0x2A ;; load frequency low-bits
222 0xE0 223 ;; 0xE0
223 0x18 ;; set frequency-low-bits 224 ;; 0x18 ;; set frequency-low-bits
224 225
225 0x2A ;; load duration 226 ;; 0x2A ;; load duration
226 ]) 227 ;; ])
227 228
228 (defn music-step [] 229 (defn music-step []
229 ;; C == current-ticks 230 ;; C == current-ticks
230 ;; A == desired-ticks 231 ;; A == desired-ticks
231 232
232 (flatten 233 (flatten
233 [ 234 [0xF5 ;; push A
234 0xF5 ;; push A
235 0xF0 235 0xF0
236 0x05 ;; load current ticks from 0xF005 236 0x05 ;; load current ticks from 0xF005
237 0xB8 ;; 237 0xB8 ;;
238 0x30 ;; increment C only if last result caused carry 238 0x30 ;; increment C only if last result caused carry
239 0x01 239 0x01
277 0xD0 ;; set HL to 0xD000 == music-start 277 0xD0 ;; set HL to 0xD000 == music-start
278 0x0E 278 0x0E
279 0x00 ;; 0->C 279 0x00 ;; 0->C
280 0x06 280 0x06
281 0x00 ;; 0->B 281 0x00 ;; 0->B
282 282 0xAF ;; 0->A
283 283
284 284
285 0xAF ;; initialiaze A to zero 285
286
287 286
288 (music-step) 287 (music-step)
289 0x18 288 0x18
290 (->signed-8-bit (+ (- (count (music-step))) 289 (->signed-8-bit (+ (- (count (music-step)))
291 -2))])) 290 -2))]))