-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidN.asm
6948 lines (5648 loc) · 118 KB
/
sidN.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
#target bin
;#code $4000,$1000
; tone duration in jam mode dependent on key press
; endless tone duration in play mode
; gate-array / memory configs:
; $c1 =
; $c2 = 4 mal 2. Bank
; $c3 =
; player constants
SCANLINES_TOTAL equ 312
NOPS_TOTAL equ 64 * SCANLINES_TOTAL
NOPS_PER_SAMPLE equ 72
ITER_TOTAL equ (NOPS_TOTAL / NOPS_PER_SAMPLE) ; = 277
EXTRA_SAMPLES equ 5 ; samples sent outside of waveloop (TODO where is that?)
ITERATIONS equ (ITER_TOTAL - EXTRA_SAMPLES) / 2
META_SIZE equ 1
INSTR_SIZE equ 4 + META_SIZE
; editor constants
WAVETBL_COL equ 2
INSTR_PARAMS equ 5
TRACK_COL_MAX equ 2
TRACK_ROW_MAX equ 32
TRACK_ROWS_DISP equ 9
VOICE_SIZE equ 32 * 3 ; 32 rows a 3 bytes
; jump table constants
RANGE_PAUSE_START equ 1
RANGE_PAUSE_LEN equ 127 ; = 1 to 127
RANGE_NOTES_START equ RANGE_PAUSE_START + RANGE_PAUSE_LEN
RANGE_NOTES_LEN equ 96 ; = 128 to 224
RANGE_EFFECTS_START equ RANGE_NOTES_START + RANGE_NOTES_LEN
RANGE_EFFECTS_LEN equ 31
; misc constants
SEQ_END equ $ff
TRK_END equ $ff
TRK_SLIDE equ $80
; end of the free store
FREE_STORE_END: equ $a600
; wave tables
; TODO use indices (see init_waves) instead of e.g. w_square
w_square equ 0
w_ramp equ 1
w_sinus equ 2
w_triangle equ 3
w_silence equ 4
w_pulse equ 5
; i/o
file_buffer equ $c000
;
; Start
;
org $0300
; jump block
jp jam
jp player_init
;
; Jam mode
;
jam:
; init soundchip
call init_psg
; init engine
call init_engine
;
call init_screen
; disable AY replay
call ay_toggle
; set player mode
call playmode_jam
; jam
jam_loop:
call $bb1b
jr nc,jam_loop
ld b,a
push af
; in track mode?
ld a,(track_mode)
or a
jr z,jam_play
; yes
ld a,(track_col)
or a
jr z,jam_play
; not on note column!
pop af
call func_key
jr jam_loop
jam_play:
; play the pressed key
call play_key
pop af
call func_key
jr jam_loop
init_screen:
; init screen
ld a,2
call $bc0e
; init editor
call init_editor
; show memory stats
call mem_stats
ret
; init editor
init_editor:
; octave
ld a,(octave)
call store_octave
; edit modes
call modes_init
; select inst 0
xor a
call instr_select
call draw_waveform
; enable mem-stats
ld a,1
ld (print_memstats),a
; select 1st track
ld a,1
call track_select
; draw sequence editor
call seq_draw
ret
; calc tone index from octave + tone
; a = octave
; b = tone
; => a = tone index
get_tone_index:
dec a ; starts at octave 1!
; octave * 12
add a
add a ; = a*4
ld c,a
add a ; = a*8
add c ; + a*4
; + tone
add b
ret
; look up tone for a pressed key
; in: B = char
play_key:
ld hl,keytab
; reset last played note
xor a
ld (last_note),a
key_loop:
ld a,(hl) ; compare char
or a
ret z
cp b
jr z,key_found ; match
inc hl
inc hl
inc hl
jr key_loop ; keep lokoing
key_found:
; ascii code, tone, scan row
inc hl ; skip code
ld b,(hl) ; get tone in b
inc hl
ld a,(hl) ; get key-code
; store key-code
ld (key_code+1),a
; add octave
ld a,(octave)
call get_tone_index
; store in jam track
ld (jam_track),a
; store tone
ld a,b
inc a ; add 1!
ld (last_note),a
; reset tone length
ld hl,0
ld (tone_length+1),hl
; restart dummy sequence
xor a
ld (seq_jam),a
; play jam track
call player_start
ret
; last tone in jam mode
last_note:
db 0
; mappings of key to offset and scancode
; 3 bytes per key:
; +0 = ascii code of pressed key
; +1 = half tone offset
; +2 = keyboard row no. for key-release check
keytab:
; upper manual
db $71,12,$48 ; q C
db $32,13,$48 ; 2 C#
db $77,14,$47 ; w D
db $33,15,$47 ; 3 D#
db $65,16,$47 ; e E
db $72,17,$46 ; r F
db $35,18,$46 ; 5 F#
db $74,19,$46 ; t G
db $36,20,$46 ; 6 G#
db $79,21,$45 ; y A
db $37,22,$45 ; 7 A#
db $75,23,$45 ; u B
db $69,24,$44 ; i C
db $39,25,$44 ; 9 C#
db $6f,26,$44 ; o D
db $30,27,$44 ; 0 D#
db $70,28,$43 ; p E
; lower manual
db $7a, 0,$48 ; z C
db $73, 1,$47 ; s C#
db $78, 2,$47 ; x D
db $64, 3,$47 ; d D#
db $63, 4,$47 ; c E
db $76, 5,$46 ; v F
db $67, 6,$46 ; g F#
db $62, 7,$46 ; b G
db $68, 8,$45 ; h G#
db $6e, 9,$45 ; n A
db $6a,10,$45 ; j A#
db $6d,11,$44 ; m B
db $2c,12,$44 ; , C
db $6c,13,$44 ; l C#
db $2e,14,$43 ; . D
db $3a,15,$43 ; : D#
db $2f,16,$43 ; / E
; end of table
db 0
; look up function for a pressed key
; in: A = key
func_key:
ld (char_current),a
ld b,a ; used in func_loop
; look for function in current mode
ld hl,no_match1 ; where to jump to
push hl ; in case of mismatch
ld hl,(mode_current)
func_loop:
; look for function
ld a,(hl)
or a
ret z ; jump to no-match fn
cp b
jr z,func_found ; match
inc hl
inc hl
inc hl
jr func_loop
func_found:
pop de ; discard no-match fn ptr
; fetch target fn ptr
inc hl ; skip char code
ld e,(hl) ; read
inc hl ; address
ld d,(hl) ; into de
push de
ret ; jp (de)
no_match1:
; function not found in current mode
; try global function table
ld hl,no_match2
push hl
ld hl,global_funcs
jr func_loop
no_match2:
; catch_all available?
ld hl,(catch_all)
ld a,h
or l
jp nz,call_hl ; yes, call it
func_print_key:
; no fn for key, print key code (in b)
ld hl,$4e19
call locate
ld a,b
call hex_out
ret
; global function keys
global_funcs:
db 9 ; TAB
dw select_mode
db $05 ; crtl E
dw init_screen
db $0c ; crtl L
dw file_load
db $13 ; ctrl S
dw file_save
db $12 ; ctrl R
dw beep
db $1a ; crtl Y
dw ay_toggle
; db $14 ; ctrl T
; dw new_mem_test
db 32 ; space
dw track_play
db $7f ; del
dw init_psg
db $81 ; f1
dw toggle_A
db $82 ; f2
dw toggle_B
db $83 ; f3
dw toggle_C
db $88 ; f8
dw inc_octave
db $85 ; f5
dw dec_octave
db $87 ; f7
dw instr_next
db $84 ; f4
dw instr_prev
; end of table
db 0
; init edit mode selection
modes_init:
ld hl,mode_list
jr enable_mode
; select next edit mode
select_mode:
; call leave callback
ld hl,(leave_callback)
ld a,h
or l
call nz,call_hl
; get mode list ptr
ld hl,(mode_ptr)
enable_mode:
; get next mode from list
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld a,e
or d
jr nz,tab_store
; restart list
ld hl,mode_list
jr enable_mode
tab_store:
; store new mode
ld (mode_ptr),hl
ex de,hl
; read enter-callback
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ex de,hl
ld (enter_callback),hl
ld (redraw_callback),hl
ex de,hl
push de ; store enter-callback addr
; read leave-callback
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld (leave_callback),de
; read catch-all fn ptr
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld (catch_all),de
; activate new edit-mode
ld (mode_current),hl
; call enter-fn?
pop hl
ld a,h
or l
ret z ; no
push hl ; yes
ret
;
; params for tab-key switch
;
; active edit-mode
mode_current:
dw 0
; entering new edit-mode
enter_callback:
dw 0
; leaving current edit-mode
leave_callback:
dw 0
; re-drawing current mode
; @note: only used in instrument editor!
redraw_callback:
dw 0
; catch-all fn for current mode
catch_all:
dw 0
; pointer into list of edit-modes
mode_ptr:
dw mode_list
; list of edit-modes
mode_list:
dw mode_params
dw mode_instr
dw mode_sequence
dw mode_track
dw 0
; the most recent keys char code
char_current:
db 0
;
; File IO
;
; load
file_load:
call read_filename
cp $fc
ret z
call beep
; TODO
ret
; save
file_save:
call read_filename
cp $fc
ret z
call beep
; TODO!
ret;
; read filename from keyboard
read_filename:
ld hl,$0110
call locate
call print_local
db "filename: ", 0
call cursor_on
ld hl,filename
push hl
ld b,8
call read_string
push af
call cursor_off
pop af
pop hl
ret
; current filename
filename:
ds 8
filetype:
db ".ayb"
db 0
; switch AY skipping on / off
ay_toggle:
; switch
ld a,(skip_flag)
xor 1
ld (skip_flag),a
push af
; copy jump code
add a
ld d,0
ld e,a
ld hl,skip_table
add hl,de
ld e,(hl)
inc hl
ld d,(hl)
ex de,hl
ld de,skip_target
ldi
ldi
ldi
; print
ld hl,$3001
call locate
call print_local
db "AY replay: ",0
pop af
call hex_nib
ret
skip_flag:
db 1
; table of addresses to skip blocks to use
skip_table:
dw skip_code
dw noskip_code
; skip code, will be used to disable skip AY music
skip_code:
jp wave_loop
; no-skip code, will be used to enable AY music
noskip_code:
ds 3
; beep
beep:
ld a,7
jp $bb5a
; toggle AY voices
toggle_A:
ld a,(reg7mask+1)
xor %00000001
ld (reg7mask+1),a
bit 0,a
ld b,0
jr print_voice
toggle_B:
ld a,(reg7mask+1)
xor %00000010
ld (reg7mask+1),a
bit 1,a
ld b,1
jr print_voice
toggle_C:
ld a,(reg7mask+1)
xor %00000100
ld (reg7mask+1),a
bit 2,a
ld b,2
print_voice:
ld d,1 ; on
jr z,print_voice2
dec d ; off
; d = value to print (0,1)
print_voice2:
ld a,b
add a
add a
add a
ld hl,$3c04
add h
ld h,a
call locate
ld a,d
add $30
jp print_char
; increment octave
inc_octave:
ld a,(octave)
cp 7
ret z
inc a
jr store_octave
; decrement octave
dec_octave:
ld a,(octave)
cp 1
ret z
dec a
; store & display current octave
store_octave:
ld (octave),a
push af
ld hl,$0c01
call locate
call print_local
db "octave: ", 0
pop af
call hex_out
ret
; current octave
octave:
db 4
; select next instrument
instr_next:
ld hl,instr_max
ld a,(instr_current)
inc a
cp (hl)
jr c,instr_select
xor a
jr instr_select
; select previous instrument
instr_prev:
ld a,(instr_current)
sub 1
jr nc,instr_select
ld a,(instr_max)
dec a
jr instr_select
; select an instrument
; a = instrument no.
instr_select:
; store instr no
ld (instr_current),a
; also in jam track
ld (jam_track+2),a
push af
call instr_get_addr
ld (instr_current_addr),hl
pop af
; display instr no
call print_instr
ret
; draw current waveform
draw_waveform:
; has it changed?
ld hl,(instr_current_val)
ld a,(last_drawed_waveform)
cp (hl)
ret z ; no, don't draw again
; remove drawing if no jp-command
push hl
cp $80
call nz,undraw
pop hl
; get current waveform
ld a,(hl)
cp $80
ret z
; and its address
ld hl,(waveform_list_ptr)
ld l,a ; add waveform index
ld h,(hl) ; to get base addr
ld l,0 ; of actual waveform
; draw waveform
ld a,1
call draw_now
ret
; remove last drawing
; a = waveform addr hi-byte
undraw:
; draw with pen 0
ld h,a
xor a
ld l,a
jp draw_now
; draw waveform in hl
; hl = waveform to draw
; a = pen
draw_now:
; save to undraw later
ld b,a
ld a,h
ld (last_drawed_waveform),a
ld a,b
; gra set pen
call $bbde
ld b,64 ;
ld de,276 ; x-pos
draw_loop:
push bc
push de
push hl
ld a,(hl)
and 15
push af
ld hl,264 ; y-pos
call $bbc0; gra move absolute
; plot current value
ld h,0
ld d,h
ld e,h
pop af
add a
add a
ld l,a
call $bbed ; gra plot relative
pop hl
ld de,4
add hl,de
pop de
inc de
pop bc
djnz draw_loop
ret
; the waveform that was drawn lastly
last_drawed_waveform:
db $80
; get address of instrument
; a = instr no.
; => hl: address
instr_get_addr:
ld l,a
ld a,(instr_table_base)
ld h,a
; hl = instr table ptr
ld a,(hl)
inc h
ld h,(hl)
ld l,a
; hl = instr addr
ret
; current instrument
instr_current:
db 0
; and its address
instr_current_addr:
dw 0
; highest instr. number
instr_max:
db 0
; base address (i.e. hi-byte) of instrument table
instr_table_base:
db 0
;
; instrument editor
;
; instrument mode function keys
mode_instr:
dw instr_draw_cursor ; enter fn
dw instr_hide_cursor ; leave fn
dw 0 ; catch_all
db $20
dw instr_set_pos
db $f0
dw instr_up
db $f1
dw instr_down
db $f2
dw instr_left
db $f3
dw instr_right
db 13
dw instr_enter
db $f8
dw instr_inc_value
db $f9
dw instr_dec_value
db 0 ; end of table
; max values for instr. edit
INSTR_ROW_MAX: equ 9
INSTR_COL_MAX: equ 3
; get offset in current table
; => a: offset
; => bc: column
; => hl: offset table
get_table_offset:
; get current column
ld a,(inst_col)
ld b,0
ld c,a
; get offset
ld hl,current_inst_offsets
add hl,bc
ld a,(hl) ; = current offset
ret
; print one column of table data
; bc = column
print_one_column:
; print the right table
ld hl,table_pointers
add hl,bc
add hl,bc
ld e,(hl)
inc hl
ld d,(hl)
ex de,hl
jp (hl)
; function ptrs to print an instr. column
table_pointers:
dw print_offset_tbl
dw print_finetune_tbl
dw print_wavetable_tbl
dw print_volume_tbl
; instr. mode - cursor up
instr_up:
push af
ld hl,inst_row
ld a,(hl)
or a
jr z,scroll_up
push hl
call instr_show_cur_val
pop hl
dec (hl)
call instr_done
jr instr_cancel
; scroll up 1 row
scroll_up:
; get column and offset
call get_table_offset
; already at the top?
or a
jr z,instr_cancel
; go one step up
dec (hl)
jr redraw_column
; instr. mode - cursor down
instr_down:
push af
ld hl,inst_row
ld a,(hl)
cp INSTR_ROW_MAX
jr nc,scroll_down
push hl
call instr_show_cur_val
pop hl
inc (hl)
call instr_done
jr instr_cancel
; scroll down 1 row
scroll_down:
; get column and offset
call get_table_offset
; already at the bottom?
cp 245
jr nc,instr_cancel
; go one step down
inc (hl)
redraw_column:
ld a,(hl)
; re-draw
call print_one_column
call instr_done
jr instr_cancel
; instr. mode - cursor left
instr_left:
push af
call instr_show_cur_val