/
stringy.asm
1432 lines (1075 loc) · 27.9 KB
/
stringy.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;***************************************************************************
; FILE: stringy.asm *
; CONTENTS: Guitar synthesiser using Karplus-Strong algorithm *
; COPYRIGHT: MadLab Ltd. 2005, 2017 *
; AUTHOR: James Hutchby *
; UPDATED: 19/04/17 *
;***************************************************************************
processor 12F1840
include "p12f1840.inc"
__config _CONFIG1, _FOSC_INTOSC&_WDTE_ON&_PWRTE_ON&_MCLRE_OFF&_CP_ON&_CPD_ON&_BOREN_ON&_CLKOUTEN_OFF&_IESO_OFF&_FCMEN_ON&h'3fff'
__config _CONFIG2, _WRT_ALL&_PLLEN_ON&_STVREN_ON&_BORV_19&_LVP_OFF&h'3fff'
errorlevel -207,-302,-305
__idlocs h'EE21'
;***************************************************************************
; *
; Specification *
; *
;***************************************************************************
; power-up self-test - LED flashes and beeps twice
; plays notes over 5 octaves, a single octave at a time
; pushbuttons shift octave up and down
; both pushbuttons pressed and released changes voice
; both pushbuttons held down for >1 second plays demo tune
; demo tune played in current voice
; sleeps after ~10 minutes of inactivity
; pushbutton or key to awaken
;***************************************************************************
; *
; Port assignments *
; *
;***************************************************************************
PORTA_IO equ b'011011' ; PORTA status
LED equ 5 ; LED
SPEAKER equ 2 ; speaker output (P1A)
KEYBOARD equ 1 ; resistor ladder (AN1)
BUTTON1 equ 4 ; pushbutton #1
BUTTON2 equ 3 ; pushbutton #2
; *** RA0 (pin 7) is currently unused and is available for expansion ***
LED_on macro
movlb 2
bsf LATA,LED
movlb 0
endm
LED_off macro
movlb 2
bcf LATA,LED
movlb 0
endm
;***************************************************************************
; *
; Constants and timings *
; *
;***************************************************************************
CLOCK equ d'8000000'*4 ; processor clock frequency in Hz
TEST equ 0 ; true if test
POLL_PERIOD equ d'10' ; keyboard poll period in ms
LINE equ d'128' ; maximum delay line length
FACTOR equ d'128' ; filter factor *** adjust to change sound ***
DAMPING equ d'250' ; damping factor *** adjust to shorten or lengthen decay times ***
OCTAVES equ d'5' ; number of octaves
VOICES equ d'2' ; number of voices
DEMO_DELAY equ d'1250' ; demo delay in ms
DURATION equ d'150' ; note duration
REST equ d'150' ; rest duration
;***************************************************************************
; *
; File register usage *
; *
;***************************************************************************
cblock h'20' ; bank 0
keys ; keys pressed
tune_note ; tune note
tune_pnt ; tune pointer
tune_ret ; tune return
cur_voice ; current voice, 0 to VOICES-1
cur_octave ; current octave, 0 to (OCTAVES-1)*12
repeat ; note repeat
length ; delay line length
samples ; samples count
acc:2 ; accumulator
arg:2 ; argument
timer1:2 ; timer1 period
timer ; button timer
timeout ; sleep timer
rand:2 ; random number
line_:0 ; delay line (split across banks)
endc
; address in linear memory
line equ h'2000'+(line_-h'20')
cblock h'70' ; common RAM
flags ; various flags
loop, count ; loop counters
temp ; work register
work1, work2 ; work registers
endc
; flags
BLEND equ 0 ; set if waveform blended
RELEASE1 equ 1 ; set if waiting for key release
RELEASE2 equ 2 ; set if waiting for button release
BUTTONS equ 3 ; set if both buttons pressed
;***************************************************************************
; *
; Macros *
; *
;***************************************************************************
routine macro label ; routine
label
endm
table macro label ; lookup table
label brw
endm
entry macro value ; table entry
retlw value
endm
index macro label ; index table
call label
endm
jump macro label ; jump through table
goto label
endm
tstw macro ; test w
iorlw 0
endm
movff macro f1,f2 ; move file to file
movfw f1
movwf f2
endm
movlf macro n,f ; move literal to file
movlw n
movwf f
endm
negw macro ; negate w
sublw 0
endm
;---------------------------------------------------------------------------
; reset and interrupt vectors
;---------------------------------------------------------------------------
org 0
goto main_entry
org 4
goto isr
;***************************************************************************
; *
; Lookup tables *
; *
;***************************************************************************
table notes
note macro len,tmr
entry len
entry high ((-tmr)&h'ffff')
entry low ((-tmr)&h'ffff')
endm
note d'128',d'7645' ; C1
note d'128',d'7215' ; C#1
note d'128',d'6810' ; D1
note d'128',d'6428' ; D#1
note d'128',d'6067' ; E1
note d'128',d'5727' ; F1
note d'128',d'5405' ; F#1
note d'128',d'5102' ; G1
note d'128',d'4816' ; G#1
note d'128',d'4545' ; A1
note d'128',d'4290' ; A#1
note d'128',d'4050' ; B1
note d'128',d'3822' ; C2
note d'128',d'3608' ; C#2
note d'128',d'3405' ; D2
note d'128',d'3214' ; D#2
note d'128',d'3034' ; E2
note d'128',d'2863' ; F2
note d'128',d'2703' ; F#2
note d'128',d'2551' ; G2
note d'128',d'2408' ; G#2
note d'128',d'2273' ; A2
note d'128',d'2145' ; A#2
note d'128',d'2025' ; B2
note d'128',d'1911' ; C3
note d'128',d'1804' ; C#3
note d'128',d'1703' ; D3
note d'128',d'1607' ; D#3
note d'128',d'1517' ; E3
note d'128',d'1432' ; F3
note d'128',d'1351' ; F#3
note d'128',d'1276' ; G3
note d'128',d'1204' ; G#3
note d'128',d'1136' ; A3
note d'128',d'1073' ; A#3
note d'126',d'1028' ; B3
note d'119',d'1028' ; C4
note d'112',d'1031' ; C#4
note d'106',d'1028' ; D4
note d'100',d'1029' ; D#4
note d'94',d'1033' ; E4
note d'89',d'1030' ; F4
note d'84',d'1030' ; F#4
note d'79',d'1033' ; G4
note d'75',d'1027' ; G#4
note d'71',d'1024' ; A4
note d'67',d'1025' ; A#4
note d'63',d'1028' ; B4
note d'59',d'1037' ; C5
note d'56',d'1031' ; C#5
note d'53',d'1028' ; D5
note d'50',d'1029' ; D#5
note d'47',d'1033' ; E5
note d'44',d'1041' ; F5
note d'42',d'1030' ; F#5
note d'39',d'1047' ; G5
note d'37',d'1041' ; G#5
note d'35',d'1039' ; A5
note d'33',d'1040' ; A#5
note d'31',d'1045' ; B5
if TEST
table sine
entry d'0'
entry d'0'
entry d'1'
entry d'1'
entry d'2'
entry d'4'
entry d'5'
entry d'7'
entry d'10'
entry d'12'
entry d'15'
entry d'18'
entry d'21'
entry d'25'
entry d'29'
entry d'33'
entry d'37'
entry d'42'
entry d'46'
entry d'51'
entry d'56'
entry d'62'
entry d'67'
entry d'73'
entry d'78'
entry d'84'
entry d'90'
entry d'96'
entry d'102'
entry d'108'
entry d'115'
entry d'121'
entry d'127'
entry d'133'
entry d'139'
entry d'146'
entry d'152'
entry d'158'
entry d'164'
entry d'170'
entry d'176'
entry d'181'
entry d'187'
entry d'192'
entry d'198'
entry d'203'
entry d'208'
entry d'212'
entry d'217'
entry d'221'
entry d'225'
entry d'229'
entry d'233'
entry d'236'
entry d'239'
entry d'242'
entry d'244'
entry d'247'
entry d'249'
entry d'250'
entry d'252'
entry d'253'
entry d'253'
entry d'254'
entry d'254'
entry d'254'
entry d'253'
entry d'253'
entry d'252'
entry d'250'
entry d'249'
entry d'247'
entry d'244'
entry d'242'
entry d'239'
entry d'236'
entry d'233'
entry d'229'
entry d'225'
entry d'221'
entry d'217'
entry d'212'
entry d'208'
entry d'203'
entry d'198'
entry d'192'
entry d'187'
entry d'181'
entry d'176'
entry d'170'
entry d'164'
entry d'158'
entry d'152'
entry d'146'
entry d'139'
entry d'133'
entry d'127'
entry d'121'
entry d'115'
entry d'108'
entry d'102'
entry d'96'
entry d'90'
entry d'84'
entry d'78'
entry d'73'
entry d'67'
entry d'62'
entry d'56'
entry d'51'
entry d'46'
entry d'42'
entry d'37'
entry d'33'
entry d'29'
entry d'25'
entry d'21'
entry d'18'
entry d'15'
entry d'12'
entry d'10'
entry d'7'
entry d'5'
entry d'4'
entry d'2'
entry d'1'
entry d'1'
entry d'0'
endif
table thresholds
entry d'245'
entry d'224'
entry d'203'
entry d'181'
entry d'160'
entry d'139'
entry d'117'
entry d'96'
entry d'75'
entry d'53'
entry d'32'
entry d'11'
entry 0
table voices
entry 0 ; acoustic guitar
entry 1 ; electric guitar
table tune_esc
goto tesc00 ; end of tune or sub-tune
goto tesc01 ; play sub-tune
goto tesc10 ; long rest
goto tesc11 ; octave shift
;---------------------------------------------------------------------------
; Tune definitions
;---------------------------------------------------------------------------
; rrttnnnn = note:
; rr = rest duration (00 = none ... 11 = max)
; tt = tone duration (00 = min ... 11 = max)
; nnnn = tone value (0001 = C ... 1111 = D')
; xxxx0000 = escape:
; xx000000 = end of tune or sub-tune
; xx010000 = play sub-tune (followed by address)
; rr100000 = long rest (00 = min ... 11 = max)
; xo110000 = octave shift (0 = -1, 1 = +1)
cblock 1
C1, C1S
D1, D1S
E1
F1, F1S
G1, G1S
A1, A1S
B1
C2, C2S
D2
endc
tstart macro label ; start of tune or sub-tune
label equ $&h'ff'
endm
tnote macro n,t,r ; note
dw (r<<6)+((t-1)<<4)+n
endm
tstop macro ; end of tune or sub-tune
dw b'000000'
endm
ttune macro address ; play sub-tune
dw b'010000'
dw address&h'ff'
endm
trest macro r ; long rest
dw ((r-1)<<6)+b'100000'
endm
toctave macro o ; octave shift
if o==-1
dw (0<<6)+b'110000'
endif
if o==+1
dw (1<<6)+b'110000'
endif
endm
;***************************************************************************
; *
; Procedures *
; *
;***************************************************************************
;---------------------------------------------------------------------------
; delay
;---------------------------------------------------------------------------
delay64 goto $+1 ; [8]
goto $+1 ; [8]
goto $+1 ; [8]
goto $+1 ; [8]
goto $+1 ; [8]
goto $+1 ; [8]
delay16 return ; [8]
delay macro cycles ; delay for a number of cycles
if (cycles) < 0
error "Delay cycles negative"
endif
variable i = cycles
while i > d'64'
call delay64 ; [64]
i -= d'64'
endw
if i >= d'16'
variable n = (i-d'16')/d'8'
call delay16-n ; [8+8n+8]
i -= (n*d'8')+d'16'
endif
while i >= d'4'
nop ; [4]
i -= d'4'
endw
if i != 0
error "Delay cycles not multiple of 4"
endif
endm
;---------------------------------------------------------------------------
; multiply w by 8-bit constant
;---------------------------------------------------------------------------
mult macro c
clrf arg+0
movwf arg+1
variable n = 0
while n <= 7
if ((c)&(1<<n)) != 0
movfw arg+1
addwf acc+1
movfw arg+0
addwfc acc+0
endif
if n != 7
lslf arg+1
rlf arg+0
endif
n += 1
endw
endm
;---------------------------------------------------------------------------
; interrupt service routine
;---------------------------------------------------------------------------
routine isr
movlb 0
movff timer1+0,TMR1H ; reload timer
movff timer1+1,TMR1L
movff FSR0H,FSR1H
movff FSR0L,FSR1L
moviw FSR0++ ; next sample
movlb 5 ; duty cycle
bcf CCP1CON,DC1B1
bcf CCP1CON,DC1B0
; lsrf WREG ; *** uncomment these lines to ***
; skpnc ; *** increase output volume ***
; bsf CCP1CON,DC1B1
movwf CCPR1L
movlb 0
decf samples ; end of delay line ?
bnz isr1 ; branch if not
movlf high line,FSR0H ; wrap pointer
movlf low line,FSR0L
movff length,samples
isr1
if !TEST
clrf acc+0 ; low-pass filter + damping
clrf acc+1
movfw INDF0
mult (FACTOR*DAMPING)/d'256'
movfw INDF1
mult ((d'256'-FACTOR)*DAMPING)/d'256'
btfss flags,BLEND
bra isr2
call get_random
andlw b'11' ; *** b'1' can also be used ***
bnz isr3
isr2 movff acc+0,INDF1
isr3
endif
movlb d'31'
movff FSR0H,FSR0H_SHAD
movff FSR0L,FSR0L_SHAD
movlb 0
bcf PIR1,TMR1IF
retfie
;---------------------------------------------------------------------------
; waits, fed with the wait in ms in wreg
;---------------------------------------------------------------------------
routine long_wait
clrw
routine wait_ms
movwf work1
WAIT equ ((CLOCK/4)/d'256')/d'1000'
wait1 movlf WAIT,work2
wait2 bcf PIR1,TMR2IF
wait3 clrwdt
btfss PIR1,TMR2IF
bra wait3
decfsz work2
bra wait2
decfsz work1
bra wait1
return
;---------------------------------------------------------------------------
; polls the pushbuttons, returns the buttons state in w with the NZ flag
; set if either pushbutton pressed
;---------------------------------------------------------------------------
routine poll_buttons
clrw
btfss PORTA,BUTTON1
iorlw 1<<0
btfss PORTA,BUTTON2
iorlw 1<<1
return
;---------------------------------------------------------------------------
; beeps
;---------------------------------------------------------------------------
BEEP_PITCH equ d'250' ; beep pitch
BEEP_PERIOD equ d'250' ; beep period
routine beep
movlf BEEP_PERIOD,work1
beep1 movlb 2 ; toggle speaker output
bsf LATA,SPEAKER
movlb 0
movlf BEEP_PITCH,work2 ; half-cycle delay
beep2 clrwdt
nop
nop
nop
nop
nop
nop
decfsz work2
bra beep2
movlb 2 ; toggle speaker output
bcf LATA,SPEAKER
movlb 0
movlf BEEP_PITCH,work2 ; half-cycle delay
beep3 clrwdt
nop
nop
nop
nop
nop
nop
decfsz work2
bra beep3
decfsz work1
bra beep1
return
;---------------------------------------------------------------------------
; reads from program memory, fed with the address offset in w, returns
; the byte in w
;---------------------------------------------------------------------------
routine read_mem
movlb 3
movwf EEADRL ; address
movlf high demo,EEADRH
bcf EECON1,CFGS ; program memory
bsf EECON1,EEPGD
bsf EECON1,RD ; read byte
nop
nop
movfw EEDATL
movlb 0
return
;---------------------------------------------------------------------------
; returns a pseudo random number in w
;---------------------------------------------------------------------------
routine get_random
movfw rand+0
iorwf rand+1,w
bnz rand1
movfw TMR0 ; seed generator
movwf rand+0
xorlw h'ff'
movwf rand+1
rand1 rlf rand+0,w ; calculate next in sequence
xorwf rand+0,w
movwf temp ; msb <= Q15 ^ Q14
swapf rand+1,w
btfsc rand+0,4
xorlw h'80' ; msb <= Q12 ^ Q3
xorwf temp
rlf temp
rlf rand+1
rlf rand+0 ; << 1 + (Q15 ^ Q14 ^ Q12 ^ Q3)
movfw rand+1 ; random number
return
;---------------------------------------------------------------------------
; returns the key pressed in w (1 to 12) or 0 if none, with the NZ flag
; set if any key pressed
;---------------------------------------------------------------------------
routine poll_keyboard
movlb 1
pollk1 clrwdt
btfsc ADCON0,GO
bra pollk1
movff ADRESH,work1
bsf ADCON0,GO
movlb 0
clrf work2
pollk2 movfw work2
index thresholds
subwf work1,w
bc pollk3
incf work2
bra pollk2
pollk3 incf work2
movlw d'13'
subwf work2,w
skpnz
clrf work2
movfw work2
return
;---------------------------------------------------------------------------
; initialises the delay line with random samples
;---------------------------------------------------------------------------
routine init_line
movlf high line,FSR0H
movlf low line,FSR0L
movff length,samples
if TEST
movlf LINE,samples
initl1 movfw samples
sublw LINE
index sine
movwi FSR0++
decfsz samples
bra initl1
else
initl2 call get_random ; random waveform
movwi FSR0++
decfsz samples
bra initl2
endif
return
;---------------------------------------------------------------------------
; plays a note in the current octave, fed with the note in w (0 - 11)
;---------------------------------------------------------------------------
routine play_note
call stop_note
LED_off
addwf cur_octave,w ; add current octave
movwf work1
addwf work1
addwf work1 ; * 3
movfw work1
index notes
movwf length
incf work1
movfw work1
index notes
movwf timer1+0
incf work1,w
index notes
movwf timer1+1
movfw cur_voice ; select voice
index voices
tstw
bcf flags,BLEND
skpz
bsf flags,BLEND
call init_line
LED_on
movlf high line,FSR0H
movlf low line,FSR0L
movff length,samples
movff timer1+0,TMR1H
movff timer1+1,TMR1L
movlb 5 ; PWM output
clrf CCPR1L
clrf CCP1AS
movlf b'00001100',CCP1CON
movlb 0
bcf PIR1,TMR1IF
movlb 1
bsf PIE1,TMR1IE
movlb 0
bsf INTCON,PEIE ; enable interrupts
bsf INTCON,GIE
clrf TMR2
bsf T1CON,TMR1ON ; Timer1 on
return
;---------------------------------------------------------------------------
; stops the note playing
;---------------------------------------------------------------------------
routine stop_note
bcf INTCON,GIE ; disable interrupts
movlb 5 ; PWM off
clrf CCP1CON
movlb 0
bcf T1CON,TMR1ON ; Timer1 off
return
;---------------------------------------------------------------------------
; plays the demo tune
;---------------------------------------------------------------------------
routine play_tune
movlf d'1'*d'12',cur_octave ; initial octave
movlw 0
playt1 movwf tune_pnt
clrf tune_ret
playt2 call poll_buttons ; exit if pushbutton pressed
btfss flags,RELEASE2
bra playt3
skpnz
bcf flags,RELEASE2
clrw
playt3 bnz texit
movfw tune_pnt ; get the next note in the tune
call read_mem
movwf tune_note
incf tune_pnt
andlw b'1111' ; escape ?
bz playt8 ; branch if yes
clrf repeat
playt4 swapf tune_note,w
andlw b'11'
addwf repeat
incf repeat
movfw tune_note ; extended note ?
andlw b'11110000'
xorlw b'00110000'
bnz playt5
movfw tune_pnt
call read_mem
movwf work1
xorwf tune_note,w
andlw b'1111'
bnz playt5 ; branch if not
movff work1,tune_note
incf tune_pnt
bra playt4
playt5 decf tune_note,w ; play the tune note
andlw b'1111'
call play_note
playt6 movlw DURATION
call wait_ms