( dco4 )
( daniel j kruszyna )
( http://krue.net/dco4/ )
( 2006.12.02 )

hex

( registers )

1f constant zh
1e constant zl
1e constant zz
1d constant yh
1c constant yl
1c constant yy
1b constant xh
1a constant xl
1a constant xx
19 constant wh
18 constant wl
18 constant w
18 constant as

10 constant zero

0f constant acc3_out
0e constant acc3h
0d constant acc3m
0c constant acc3l
0b constant acc2_out
0a constant acc2h
09 constant acc2m
08 constant acc2l
07 constant acc1_out
06 constant acc1h
05 constant acc1m
04 constant acc1l
03 constant acc0_out
02 constant acc0h
01 constant acc0m
00 constant acc0l

include attiny26.f
include avr.f

( hardware )

: port ;
: ddr swap 1 - swap ;
: pin swap 2 - swap ;

( porta: data bus )

portb 0 2constant mosi
portb 1 2constant miso
portb 2 2constant sck
portb 3 2constant shift_data
( PORTB.4: clock )
portb 5 2constant shift_clock
portb 6 2constant ss
( PORTB.7: reset )


( variables )
4 3 * var increment
4 1 * var waveform

( wavetables )
idp @ 200 idp !

label saw
0001 i, 0203 i, 0405 i, 0607 i, 0809 i, 0a0b i, 0c0d i, 0e0f i,
1011 i, 1213 i, 1415 i, 1617 i, 1819 i, 1a1b i, 1c1d i, 1e1f i,
2021 i, 2223 i, 2425 i, 2627 i, 2829 i, 2a2b i, 2c2d i, 2e2f i,
3031 i, 3233 i, 3435 i, 3637 i, 3839 i, 3a3b i, 3c3d i, 3e3f i,
4041 i, 4243 i, 4445 i, 4647 i, 4849 i, 4a4b i, 4c4d i, 4e4f i,
5051 i, 5253 i, 5455 i, 5657 i, 5859 i, 5a5b i, 5c5d i, 5e5f i,
6061 i, 6263 i, 6465 i, 6667 i, 6869 i, 6a6b i, 6c6d i, 6e6f i,
7071 i, 7273 i, 7475 i, 7677 i, 7879 i, 7a7b i, 7c7d i, 7e7f i,
8081 i, 8283 i, 8485 i, 8687 i, 8889 i, 8a8b i, 8c8d i, 8e8f i,
9091 i, 9293 i, 9495 i, 9697 i, 9899 i, 9a9b i, 9c9d i, 9e9f i,
a0a1 i, a2a3 i, a4a5 i, a6a7 i, a8a9 i, aaab i, acad i, aeaf i,
b0b1 i, b2b3 i, b4b5 i, b6b7 i, b8b9 i, babb i, bcbd i, bebf i,
c0c1 i, c2c3 i, c4c5 i, c6c7 i, c8c9 i, cacb i, cccd i, cecf i,
d0d1 i, d2d3 i, d4d5 i, d6d7 i, d8d9 i, dadb i, dcdd i, dedf i,
e0e1 i, e2e3 i, e4e5 i, e6e7 i, e8e9 i, eaeb i, eced i, eeef i,
f0f1 i, f2f3 i, f4f5 i, f6f7 i, f8f9 i, fafb i, fcfd i, feff i,

label sin
8083 i, 8689 i, 8c8f i, 9295 i, 989c i, 9fa2 i, a5a8 i, abae i,
b0b3 i, b6b9 i, bcbf i, c1c4 i, c7c9 i, ccce i, d1d3 i, d5d8 i,
dadc i, dee0 i, e2e4 i, e6e8 i, eaec i, edef i, f0f2 i, f3f5 i,
f6f7 i, f8f9 i, fafb i, fcfc i, fdfe i, feff i, ffff i, ffff i,
ffff i, ffff i, ffff i, fefe i, fdfc i, fcfb i, faf9 i, f8f7 i,
f6f5 i, f3f2 i, f0ef i, edec i, eae8 i, e6e4 i, e2e0 i, dedc i,
dad8 i, d5d3 i, d1ce i, ccc9 i, c7c4 i, c1bf i, bcb9 i, b6b3 i,
b0ae i, aba8 i, a5a2 i, 9f9c i, 9895 i, 928f i, 8c89 i, 8683 i,
807c i, 7976 i, 7370 i, 6d6a i, 6763 i, 605d i, 5a57 i, 5451 i,
4f4c i, 4946 i, 4340 i, 3e3b i, 3836 i, 3331 i, 2e2c i, 2a27 i,
2523 i, 211f i, 1d1b i, 1917 i, 1513 i, 1210 i, 0f0d i, 0c0a i,
0908 i, 0706 i, 0504 i, 0303 i, 0201 i, 0100 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0101 i, 0203 i, 0304 i, 0506 i, 0708 i,
090a i, 0c0d i, 0f10 i, 1213 i, 1517 i, 191b i, 1d1f i, 2123 i,
2527 i, 2a2c i, 2e31 i, 3336 i, 383b i, 3e40 i, 4346 i, 494c i,
4f51 i, 5457 i, 5a5d i, 6063 i, 676a i, 6d70 i, 7376 i, 797c i,

label squ
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i, 0000 i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,
ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i, ffff i,

label tri
8082 i, 8486 i, 888a i, 8c8e i, 9092 i, 9496 i, 989a i, 9c9e i,
a0a2 i, a4a6 i, a8aa i, acae i, b0b2 i, b4b6 i, b8ba i, bcbe i,
c0c2 i, c4c6 i, c8ca i, ccce i, d0d2 i, d4d6 i, d8da i, dcde i,
e0e2 i, e4e6 i, e8ea i, ecee i, f0f2 i, f4f6 i, f8fa i, fcfe i,
fffd i, fbf9 i, f7f5 i, f3f1 i, efed i, ebe9 i, e7e5 i, e3e1 i,
dfdd i, dbd9 i, d7d5 i, d3d1 i, cfcd i, cbc9 i, c7c5 i, c3c1 i,
bfbd i, bbb9 i, b7b5 i, b3b1 i, afad i, aba9 i, a7a5 i, a3a1 i,
9f9d i, 9b99 i, 9795 i, 9391 i, 8f8d i, 8b89 i, 8785 i, 8381 i,
7f7d i, 7b79 i, 7775 i, 7371 i, 6f6d i, 6b69 i, 6765 i, 6361 i,
5f5d i, 5b59 i, 5755 i, 5351 i, 4f4d i, 4b49 i, 4745 i, 4341 i,
3f3d i, 3b39 i, 3735 i, 3331 i, 2f2d i, 2b29 i, 2725 i, 2321 i,
1f1d i, 1b19 i, 1715 i, 1311 i, 0f0d i, 0b09 i, 0705 i, 0301 i,
0002 i, 0406 i, 080a i, 0c0e i, 1012 i, 1416 i, 181a i, 1c1e i,
2022 i, 2426 i, 282a i, 2c2e i, 3032 i, 3436 i, 383a i, 3c3e i,
4042 i, 4446 i, 484a i, 4c4e i, 5052 i, 5456 i, 585a i, 5c5e i,
6062 i, 6466 i, 686a i, 6c6e i, 7072 i, 7476 i, 787a i, 7c7e i,

idp !

( shift register operations )

: sr_init
  shift_data clear-bit
  shift_clock clear-bit ;

: sr_clk
  shift_clock set-bit
  shift_clock clear-bit ;

: sr_low
  shift_data clear-bit ;

: sr_high
  shift_data set-bit ;

( usi spi driver )

label spi
  usidr wl store
  usisr usioif 2^ load

  begin,
    usisr usioif skip-bit-set
  again,

  wl usidr fetch
  ret,

( set phase increment )
label command_phase
  ( osc number )
  wl 03 andi,

  ( 3 * )
  wh wl mov,
  wh lsl,
  wl wh add,

  ( address of phase increment )
  zl increment 3 + lo load
  zh increment 3 + hi load
  zl wl add,
  zh zero adc,

  spi call
  wl st-z,
  spi call
  wl st-z,
  spi call
  wl st-z,
  ret,

( set waveform )
label command_waveform
  ( osc number )
  wl 03 andi,

  ( address of waveform variable )
  zl waveform lo load
  zh waveform hi load
  zl wl add,
  zh zero adc,

  spi call
  wl stz,
  ret,
  
label command
  ( command byte )
  spi call
  wh wl mov,
  wh f0 andi,

  wh 00 cpi, z if, command_phase jump then,
  wh 10 cpi, z if, command_waveform jump then,

  ret,

reset vector
  ( stack pointer )
  sp ramend load

  ( zero register )
  zero clr,

  ( porta all outputs )
  ddra ff load

  ( portb has two outputs )
  ( for shift register )
  ( and one for usi )
  ddrb 2a load

  portb 45 load

  ( timer0 prescaler set to ck )
  tccr0 cs00 2^ load
  
  ( enable timer0 overflow interrupt )
  timsk toie0 2^ load

  ( set usi to three wire mode 0 )
  usicr usiwm0 2^ usics1 2^ or load

  ( clear shift register )
  sr_init

  sr_clk
  sr_clk
  sr_clk
  sr_clk
  sr_clk
  sr_clk
  sr_clk
  sr_clk

  ( initialize dcos )
  acc0l clr,
  acc0m clr,
  acc0h clr,
  acc1l clr,
  acc1m clr,
  acc1h clr,
  acc2l clr,
  acc2m clr,
  acc2h clr,
  acc3l clr,
  acc3m clr,
  acc3h clr,

  wl 80 load
  acc0_out wl mov,
  acc1_out wl mov,
  acc2_out wl mov,
  acc3_out wl mov,

  zl increment lo load
  zh increment hi load

  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,

  zero stz+,
  zero stz+,
  zero stz+,
  zero stz+,

  ( let's go )
  inton

  begin,
    begin,
      ss pin skip-bit-clear
    again,
    command call
    begin,
      ss pin skip-bit-set
    again,
  again,


timer0_overflow vector
  wl push,
  wl sreg fetch
  wl push,
  zl push,
  zh push,
  
  ( increment accumulators )
  zl increment lo load
  zh increment hi load

  wl ldz+, acc0l wl add,
  wl ldz+, acc0m wl add,
  wl ldz+, acc0h wl adc,
  wl ldz+, acc1l wl add,
  wl ldz+, acc1m wl add,
  wl ldz+, acc1h wl adc,
  wl ldz+, acc2l wl add,
  wl ldz+, acc2m wl add,
  wl ldz+, acc2h wl adc,
  wl ldz+, acc3l wl add,
  wl ldz+, acc3m wl add,
  wl ldz+, acc3h wl adc,

  ( apply wavetables )
  wl waveform 0 + fetch
  wl c0 andi,
  wl swap,
  wl lsr,
  wl lsr,
  zh wl mov,
  zh 04 ori,
  zl acc0h mov,
  acc0_out lpmz,

  wl waveform 1 + fetch
  wl c0 andi,
  wl swap,
  wl lsr,
  wl lsr,
  zh wl mov,
  zh 04 ori,
  zl acc1h mov,
  acc1_out lpmz,

  wl waveform 2 + fetch
  wl c0 andi,
  wl swap,
  wl lsr,
  wl lsr,
  zh wl mov,
  zh 04 ori,
  zl acc2h mov,
  acc2_out lpmz,

  wl waveform 3 + fetch
  wl c0 andi,
  wl swap,
  wl lsr,
  wl lsr,
  zh wl mov,
  zh 04 ori,
  zl acc3h mov,
  acc3_out lpmz,

  ( output to dacs )
  porta acc0_out store
  sr_high
  sr_clk

  porta acc1_out store
  sr_low
  sr_clk

  porta acc2_out store
  sr_clk

  porta acc3_out store
  sr_clk

  zh pop,
  zl pop,
  wl pop,
  sreg wl store
  wl pop,

  reti,

s" flash" write-flash

