rendered paste body;//_ parallel dac dds
;//_ stk500/atmega168p
;//_ 2007.11.6.01
;//_ by rmrubin@darkertechnologies.com
;//_ part file
.include "m164pdef.inc"
;//_ program constants
.equ sample_rate =39 ;500KHz @ 20MHz cpu clk = 39
.equ test_note =28
.equ dac_port =portb
.equ dac_ddr =ddrb
;//_ register definitions
.def lpmin =r0 ;flash read data register
.def phase0 =r1 ;32b phase accumulator
.def phase1 =r2
.def phase2 =r3
.def phase3 =r4
.def time0 =r5 ;32b phase incriment
.def time1 =r6
.def time2 =r7
.def time3 =r8
.def note =r20 ;midi note
.def point0 =r18 ;flash pointer buffer
.def point1 =r19
.def temp =r16 ;working registers
.def temp2 =r17
;//_ interrupt vectors
.org $0
jmp reset ;poweron/reset
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp sample_out ;ocr1a
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
jmp stray_int
stray_int:
reti
;//_ program init
reset:
ldi temp,high(ramend) ;stack setup
ldi temp2,low(ramend)
out sph,temp
out spl,temp2
clr temp ;sample timer setup
sts tccr1a,temp
ldi temp,0b00001001
sts tccr1b,temp
ldi temp,high(sample_rate)
sts ocr1ah,temp
ldi temp,low(sample_rate)
sts ocr1al,temp
ldi temp,(1<<ocie1a)
sts timsk1,temp
ser temp ;dac port setup
out dac_port,temp
out dac_ddr,temp
;///DEBUG///
ldi note,test_note
ldi point1,high(2*sine1024)
ldi point0,low(2*sine1024)
;///DEBUG///
clr phase0
clr phase1
clr phase2
clr phase3
;//_ load new note and restart signal generator
change_note:
ldi zh,high(2*miditab) ;load midi timing table flash pointer
ldi zl,low(2*miditab)
mov temp,note ;multiply midi note value by 4
clr temp2
lsl temp
rol temp2
lsl temp
rol temp2
add zl,temp ;add adjusted midi note to flash pointer
adc zh,temp2 ;and store the 32b time value in the
lpm ;timex registers
mov time3,lpmin
adiw zh:zl,1
lpm
mov time2,lpmin
adiw zh:zl,1
lpm
mov time1,lpmin
adiw zh:zl,1
lpm
mov time0,lpmin
sei ;global interrupt enable
deathloop: rjmp deathloop ;main program wait loop
;//_ timed sample output loop
sample_out:
mov temp,phase2 ;store top word of phase accumulator
mov temp2,phase3
lsr temp2 ;shift stored word right 6b
ror temp
lsr temp2
ror temp
lsr temp2
ror temp
lsr temp2
ror temp
lsr temp2
ror temp
lsr temp2
ror temp
mov zh,point1 ;load wavetable start location
mov zl,point0 ;from buffer into z pointer
add zl,temp ;add shifted phase accumulator
adc zh,temp2 ;word to start location
lpm ;load sample
out dac_port,lpmin ;output sample
add phase0,time0 ;incriment phase accumulator
adc phase1,time1
adc phase2,time2
adc phase3,time3
reti ;exit sample output interrupt
;//_ midi note timing values
miditab:
.db $00,$01,$12,$56
.db $00,$01,$22,$A6
.db $00,$01,$33,$EE
.db $00,$01,$46,$3E
.db $00,$01,$59,$A4
.db $00,$01,$6E,$31
.db $00,$01,$83,$F8
.db $00,$01,$9B,$09
.db $00,$01,$B3,$7B
.db $00,$01,$CD,$60
.db $00,$01,$E8,$CF
.db $00,$02,$05,$E0
.db $00,$02,$24,$AB
.db $00,$02,$45,$4B
.db $00,$02,$67,$DC
.db $00,$02,$8C,$7B
.db $00,$02,$B3,$47
.db $00,$02,$DC,$62
.db $00,$03,$07,$EF
.db $00,$03,$36,$13
.db $00,$03,$66,$F5
.db $00,$03,$9A,$BF
.db $00,$03,$D1,$9E
.db $00,$04,$0B,$C0
.db $00,$04,$49,$56
.db $00,$04,$8A,$97
.db $00,$04,$CF,$B8
.db $00,$05,$18,$F6
.db $00,$05,$66,$8F
.db $00,$05,$B8,$C5
.db $00,$06,$0F,$DE
.db $00,$06,$6C,$26
.db $00,$06,$CD,$EA
.db $00,$07,$35,$7E
.db $00,$07,$A3,$3C
.db $00,$08,$17,$7F
.db $00,$08,$92,$AD
.db $00,$09,$15,$2D
.db $00,$09,$9F,$70
.db $00,$0A,$31,$EC
.db $00,$0A,$CD,$1E
.db $00,$0B,$71,$8A
.db $00,$0C,$1F,$BD
.db $00,$0C,$D8,$4C
.db $00,$0D,$9B,$D4
.db $00,$0E,$6A,$FD
.db $00,$0F,$46,$77
.db $00,$10,$2E,$FE
.db $00,$11,$25,$59
.db $00,$12,$2A,$5A
.db $00,$13,$3E,$E1
.db $00,$14,$63,$D8
.db $00,$15,$9A,$3C
.db $00,$16,$E3,$14
.db $00,$18,$3F,$7A
.db $00,$19,$B0,$98
.db $00,$1B,$37,$A8
.db $00,$1C,$D5,$FA
.db $00,$1E,$8C,$EE
.db $00,$20,$5D,$FD
.db $00,$22,$4A,$B2
.db $00,$24,$54,$B5
.db $00,$26,$7D,$C1
.db $00,$28,$C7,$B0
.db $00,$2B,$34,$77
.db $00,$2D,$C6,$28
.db $00,$30,$7E,$F4
.db $00,$33,$61,$2F
.db $00,$36,$6F,$50
.db $00,$39,$AB,$F3
.db $00,$3D,$19,$DC
.db $00,$40,$BB,$F9
.db $00,$44,$95,$65
.db $00,$48,$A9,$69
.db $00,$4C,$FB,$82
.db $00,$51,$8F,$61
.db $00,$56,$68,$EE
.db $00,$5B,$8C,$4F
.db $00,$60,$FD,$E8
.db $00,$66,$C2,$5E
.db $00,$6C,$DE,$A0
.db $00,$73,$57,$E6
.db $00,$7A,$33,$B8
.db $00,$81,$77,$F3
.db $00,$89,$2A,$CA
.db $00,$91,$52,$D3
.db $00,$99,$F7,$05
.db $00,$A3,$1E,$C2
.db $00,$AC,$D1,$DD
.db $00,$B7,$18,$9F
.db $00,$C1,$FB,$D0
.db $00,$CD,$84,$BD
.db $00,$D9,$BD,$41
.db $00,$E6,$AF,$CD
.db $00,$F4,$67,$71
.db $01,$02,$EF,$E5
.db $01,$12,$55,$94
.db $01,$22,$A5,$A5
.db $01,$33,$EE,$09
.db $01,$46,$3D,$84
.db $01,$59,$A3,$B9
.db $01,$6E,$31,$3D
.db $01,$83,$F7,$9F
.db $01,$9B,$09,$79
.db $01,$B3,$7A,$82
.db $01,$CD,$5F,$9A
.db $01,$E8,$CE,$E2
.db $02,$05,$DF,$CA
.db $02,$24,$AB,$28
.db $02,$45,$4B,$4B
.db $02,$67,$DC,$12
.db $02,$8C,$7B,$07
.db $02,$B3,$47,$72
.db $02,$DC,$62,$7B
.db $03,$07,$EF,$3E
.db $03,$36,$12,$F2
.db $03,$66,$F5,$03
.db $03,$9A,$BF,$34
.db $03,$D1,$9D,$C4
.db $04,$0B,$BF,$94
.db $04,$49,$56,$50
.db $04,$8A,$96,$95
.db $04,$CF,$B8,$25
.db $05,$18,$F6,$0E
.db $05,$66,$8E,$E5
.db $05,$B8,$C4,$F5
.db $06,$0F,$DE,$7C
.db $06,$6C,$25,$E5
.db $06,$CD,$EA,$06
.db $07,$35,$7E,$67
.db $07,$A3,$3B,$87
.db $08,$17,$7F,$28
.db $08,$92,$AC,$A0
.db $09,$15,$2D,$2B
.db $09,$9F,$70,$49
.db $0A,$31,$EC,$1C
.db $0A,$CD,$1D,$CA
.db $0B,$71,$89,$EB
.db $0C,$1F,$BC,$F9
.db $0C,$D8,$4B,$CA
.db $0D,$9B,$D4,$0C
.db $0E,$6A,$FC,$CE
.db $0F,$46,$77,$0E
.db $10,$2E,$FE,$50
.db $11,$25,$59,$3F
;//_ 1024*8b sample wavetables
sine1024:
.db 128,129,130,131,131,132,133,134,135,135,136,137,138,138,139,140,141,142,142,143
.db 144,145,145,146,147,148,149,149,150,151,152,152,153,154,155,155,156,157,158,158
.db 159,160,161,162,162,163,164,165,165,166,167,167,168,169,170,170,171,172,173,173
.db 174,175,176,176,177,178,178,179,180,181,181,182,183,183,184,185,186,186,187,188
.db 188,189,190,190,191,192,192,193,194,194,195,196,196,197,198,198,199,200,200,201
.db 202,202,203,203,204,205,205,206,207,207,208,208,209,210,210,211,211,212,213,213
.db 214,214,215,215,216,217,217,218,218,219,219,220,220,221,221,222,222,223,224,224
.db 225,225,226,226,227,227,228,228,228,229,229,230,230,231,231,232,232,233,233,234
.db 234,234,235,235,236,236,236,237,237,238,238,238,239,239,240,240,240,241,241,241
.db 242,242,242,243,243,243,244,244,244,245,245,245,246,246,246,246,247,247,247,248
.db 248,248,248,249,249,249,249,250,250,250,250,250,251,251,251,251,251,252,252,252
.db 252,252,252,253,253,253,253,253,253,253,254,254,254,254,254,254,254,254,254,254
.db 254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
.db 255,255,255,255,255,255,255,255,255,255,254,254,254,254,254,254,254,254,254,254
.db 254,253,253,253,253,253,253,253,252,252,252,252,252,252,251,251,251,251,251,250
.db 250,250,250,250,249,249,249,249,248,248,248,248,247,247,247,246,246,246,246,245
.db 245,245,244,244,244,243,243,243,242,242,242,241,241,241,240,240,240,239,239,238
.db 238,238,237,237,236,236,236,235,235,234,234,234,233,233,232,232,231,231,230,230
.db 229,229,228,228,228,227,227,226,226,225,225,224,224,223,222,222,221,221,220,220
.db 219,219,218,218,217,217,216,215,215,214,214,213,213,212,211,211,210,210,209,208
.db 208,207,207,206,205,205,204,203,203,202,202,201,200,200,199,198,198,197,196,196
.db 195,194,194,193,192,192,191,190,190,189,188,188,187,186,186,185,184,183,183,182
.db 181,181,180,179,178,178,177,176,176,175,174,173,173,172,171,170,170,169,168,167
.db 167,166,165,165,164,163,162,162,161,160,159,158,158,157,156,155,155,154,153,152
.db 152,151,150,149,149,148,147,146,145,145,144,143,142,142,141,140,139,138,138,137
.db 136,135,135,134,133,132,131,131,130,129,128,128,127,126,125,124,124,123,122,121
.db 120,120,119,118,117,117,116,115,114,113,113,112,111,110,110,109,108,107,106,106
.db 105,104,103,103,102,101,100,100, 99, 98, 97, 97, 96, 95, 94, 93, 93, 92, 91, 90
.db 90, 89, 88, 88, 87, 86, 85, 85, 84, 83, 82, 82, 81, 80, 79, 79, 78, 77, 77, 76
.db 75, 74, 74, 73, 72, 72, 71, 70, 69, 69, 68, 67, 67, 66, 65, 65, 64, 63, 63, 62
.db 61, 61, 60, 59, 59, 58, 57, 57, 56, 55, 55, 54, 53, 53, 52, 52, 51, 50, 50, 49
.db 48, 48, 47, 47, 46, 45, 45, 44, 44, 43, 42, 42, 41, 41, 40, 40, 39, 38, 38, 37
.db 37, 36, 36, 35, 35, 34, 34, 33, 33, 32, 31, 31, 30, 30, 29, 29, 28, 28, 27, 27
.db 27, 26, 26, 25, 25, 24, 24, 23, 23, 22, 22, 21, 21, 21, 20, 20, 19, 19, 19, 18
.db 18, 17, 17, 17, 16, 16, 15, 15, 15, 14, 14, 14, 13, 13, 13, 12, 12, 12, 11, 11
.db 11, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5
.db 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2
.db 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
.db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
.db 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2
.db 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6
.db 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12
.db 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 19, 19, 19, 20
.db 20, 21, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 29
.db 29, 30, 30, 31, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 40
.db 40, 41, 41, 42, 42, 43, 44, 44, 45, 45, 46, 47, 47, 48, 48, 49, 50, 50, 51, 52
.db 52, 53, 53, 54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 61, 61, 62, 63, 63, 64, 65
.db 65, 66, 67, 67, 68, 69, 69, 70, 71, 72, 72, 73, 74, 74, 75, 76, 77, 77, 78, 79
.db 79, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 88, 88, 89, 90, 90, 91, 92, 93, 93
.db 94, 95, 96, 97, 97, 98, 99,100,100,101,102,103,103,104,105,106,106,107,108,109
.db 110,110,111,112,113,113,114,115,116,117,117,118,119,120,120,121,122,123,124,124
.db 125,126,127,128