10 #define x r24 //==a1==Mh
19 #define Ml t //mod3 vars
21 #define a1 x //mul_ vars
48 .org 0x0000 ; RESET interrupt
51 .org 0x0004 ; PCINT0 interrupt
52 CBI PCICR, 0 ; disable interrupt
54 .org 0x0008 ; TIM0_OVF interrupt
58 .byte 0x84, 0x9d, 0xb0, 0x69, 0x9d, 0x84, 0x69, 0x58
59 .byte 0x75, 0x8c, 0xb0, 0x69, 0x8c, 0x75, 0x69, 0x58
61 mod3: ; mod3(Mh.Ml) -> t
65 ADC Mh, Mh ; store carry in Mh
104 ADD Xlo, t ; NOTE: notes are positioned so that hi8(notes) never changes
109 ; begin of mulitiplication:
118 ; BRCC skip2 -- this bit is always zero
146 BRCC skip6 ;sbrc t, NNN
161 MOV t, a1 ;;TODO: use a1 in loop: directly
164 main: ; setup routine
165 ; NOTE: clr i0 moved to .ord 0x0
169 CLR acc ; we output a dummy sample before the actual first one
170 LDI Xhi, hi8(FLASHM + notes) ; never changes
171 LDI one, 1 ; mostly for clearing TIM0_OVF bit
175 OUT SPL, x ; init stack ptr
177 OUT PUEB, zero ; disable pullups
179 OUT DDRB, x ; PORTB0:pwm, PORTB2:debug
181 OUT CCP, x ; change protected ioregs
182 OUT CLKPSR, one ; clock prescaler 1/2 (4Mhz)
183 LDI x, 0xa7 ; determined by trial-and-error (->PORTB2)
184 OUT OSCCAL, x ; set oscillator calibration
185 OUT WDTCSR, zero; turn off watchdog
187 ;set timer/counter0 to 8bit fastpwm, non-inverting, no prescaler
192 OUT TIMSK0, one ; enable tim0_ovf
197 CPI i2, 0x78>>5 ; 16m23 -- one loop
205 CLR i2 ; clear halt condition
208 ; disable timer0, set all pins as input+pullup to conserve battery.
209 LDI x, 0x08 ; disable timer clock
211 ;OUT PRR, one ; ยง8.3: only helps in idle and active modes
214 sbi PORTB, 0 ; force pin high, so pcint triggers on connect to ground --- XXX TODO: this does not work reliably for PORTB0
215 sbi PORTB, 2 ; force pin high, so pcint triggers on connect to ground (works reliably for portb2)
224 SBI PCMSK, 2 ; listen for pin change on the audio out pin (i.e. wake up when a audio sink is connected)
226 OUT PCICR, one ; enable PCINT0
228 ;enter power-down-mode
229 LDI x, 0x05 ; sleep mode: power-down, enabled
232 OUT SMCR, one ; sleep mode: idle, enabled
237 SBI PCIFR, 0 ; clear interrupt
238 OUT PUEB, zero ; remove pullups
239 LDI x, 0x05 ; restore output pins
241 ;OUT PRR, zero ; reenable periphials
243 OUT TCCR0B, x ; reenable timer0
248 OUT OCR0AL, acc ; start by outputting a sample, because routine has variable runtime
250 SBI PORTB, 2 ; to measure runtime
397 SWAP acc ; acc<<4, to be passed to OCR0AL
405 CBI PORTB, 2 ; end runtime measurement
407 OUT TIFR0, one ; clear pending interrupt (routine takes two intr.cycles)
408 RETI ; reenables interrupts