12 #define x r26 //==Xlo==Mh
21 #define Ml r24 //mod3 vars
42 .byte 0x84, 0x9d, 0xb0, 0x69, 0x9d, 0x84, 0x69, 0x58
43 .byte 0x75, 0x8c, 0xb0, 0x69, 0x8c, 0x75, 0x69, 0x58
46 .org 0x0000 ; RESET interrupt
48 .org 0x0008 ; TIM0_OVF interrupt
51 mod3: ; mod3(Mh.Ml) -> t
80 ; definitions to mul-tree readable:
83 .macro always _bit ; nop; for when a test() is not necessary (see tree)
85 .macro never _bit ; nop; for when a test() is not necessary (see tree)
87 .macro test _bit,_jmpto
91 .macro i_test _bit,_jmpto ; inverted test (for reordered 0x8_)
99 .macro shift8 ; top three bits don't need to be corrrect, so save cycles by not carrying
102 .macro shift0 ; nop; last shift is common
108 .macro add8 ; ditto with carrying
123 ;TODO: check correctness!
124 LDI Xhi, hi8(notes) ; hi(notes) always zero, but still need to clear the register
126 ADD Xlo, t ; NOTE: can't overflow, since RAMEND == 0x5F
135 /* decision tree multiplication saves cycles and (hopefully) reduces code size
142 _?000 _?100 _?001 _?101
144 _0000 _1000 _0100 _1100 _1001 _0101 _1101
146 ... ... ... ... ... ... ...
148 B0 58 84 8C 69 75 9D */
165 RJMP end_mul ; calc'd 0xb0
167 m_1000: add16 $ shift16
176 RJMP end_mul ; calc'd 0x58
178 m__100: add16 $ shift16
190 RJMP end_mul ; calc'd 0x8c / 0x84
192 m____1: add16 $ shift16
198 m_1001: add16 $ shift16
207 RJMP end_mul ; calc'd 0x69
209 m__101: add16 $ shift16
220 RJMP end_mul ; calc'd 0x75
222 m_1101: add16 $ shift16
234 LSR a1 ;final shift is a common operation for all
236 MOV t, a1 ;;TODO: use a1 in main() directly
240 RET ; TODO: replace CALL/RET with IJMP?
242 main: ; setup routine
248 CLR acc ; we output a dummy sample before the actual first one
253 OUT SPL, x ; init stack ptr
255 OUT PUEB, zero ; disable pullups
257 OUT DDRB, x ; PORTB0:pwm, PORTB2:debug
259 OUT CCP, x ; change protected ioregs
260 OUT CLKPSR, one ; clock prescaler 1/2 (4Mhz)
261 OUT WDTCSR, zero; turn off watchdog ;;TODO: incomplete - see datasheet pg48
262 ; OUT SMCR, 2 ; sleep mode 'power down' ('idle' (default) has faster response time)
264 ;set timer/counter0 to 8bit fastpwm, non-inverting, no prescaler
269 OUT TIMSK0, one ; enable tim0_ovf
270 OUT TIFR0, one ; TODO: why?
276 SLEEP ; wait for interrupt
280 ; potential TODO: softcounter in r25 to only update duty cicle every n iterations
281 ; potential TODO: save/restore status register (SREG=0x3f) (only if something in mainloop)
283 OUT OCR0AL, acc ; start by outputting a sample, because routine has variable runtime
284 SBI PORTB, 2 ; to measure runtime
430 SWAP acc ; acc<<4, to be passed to OCR0AL
437 CBI PORTB, 2 ; end runtime measurement
438 ;TODO: to reduce jitter: clear pending tim0_ovf (TIFR0[TOV0] <- 1) ?
439 RETI ; reenables interrupts