/
main.6502
553 lines (435 loc) · 11.8 KB
/
main.6502
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
;SquatM beeper music engine
;Originally written by Shiru 06'17 for ZX Spectrum 48K
;Ported to Atari 8-bit by Shiru 07'21
;Ported to the BBC Micro by Negative Charge 11'22
\ Constants
OSBYTE = $FFF4
OSWRCH = $FFEE
SHEILABASE = $FE00 ; System peripherals
SYSVIA_DDRA = SHEILABASE + $43 ; Data direction register A
SYSVIA_ORAS = SHEILABASE + $4F ; Same as REGA but with no handshake I/O
SYSVIA_REGB = SHEILABASE + $40 ; Port B I/O
DISPLAY_START = $5800
DEBUG = FALSE
NTSC = FALSE
OP_NOP = $EA
OP_ROL_A = $2A
\ Zero Page
ORG $5f
GUARD $8f
.vars_start
.loop_ptr SKIP 2
.pattern_ptr SKIP 2
.sample_ptr SKIP 2
.sample_mask SKIP 1 ;$00 or $ff
.sample_bit SKIP 1
.sample_out SKIP 1
.row_length SKIP 1
.row_flags SKIP 1
.ch0_add SKIP 2
.ch1_add SKIP 2
.ch2_add SKIP 2
.ch3_add SKIP 2
.ch0_acc SKIP 2
.ch1_acc SKIP 2
.ch2_acc SKIP 2
.ch3_acc SKIP 2
.ch_mixer SKIP 1
.ch_acc SKIP 1
.noise_acc SKIP 2
.noise_add SKIP 2
.noise_cnt SKIP 1
.noise_div SKIP 1
.noise_pitch SKIP 1
.noise_volume SKIP 1
.temp SKIP 1
.vars_end
ORG &1100
GUARD DISPLAY_START
.start
INCLUDE "lib\os.s.6502"
; Write data to sound chip then add processing delay
MACRO sound_write_slow
sta SYSVIA_ORAS ;4 Write reg/data to SN76489
lda #%00000000 ;2
sta SYSVIA_REGB ;4
nop ;2
nop ;2
nop ;2
lda #%00001000 ;2
sta SYSVIA_REGB ;4
ENDMACRO
MACRO RESET_SOUND_CHIP
; Zero volumes on all SN76489 channels, just in case anything already playing
lda #%11111111
sound_write_slow ; Channel 3 (Noise)
lda #%11011111
sound_write_slow ; Channel 2
lda #%10111111
sound_write_slow ; Channel 1
lda #%10011111
sound_write_slow ; Channel 0
ENDMACRO
.init
\ Print Track Title
ldx #1
ldy #27
jsr moveTextCursor
jsr printString
equs "Let's Go",0
\ Print Track Artist
ldx #1
ldy #30
jsr moveTextCursor
jsr printString
equs "@shiru8bit",0
; Set up audio
; System VIA port A to all outputs
lda #%11111111
sta SYSVIA_DDRA
sei
RESET_SOUND_CHIP
; Period to 1 on tone channel 0
lda #%10000001
sound_write_slow ; Channel 0
lda #%00000000
sound_write_slow
; System VIA Port A, place accumulator on wires, no handshake
lda #%00000000
sta SYSVIA_REGB
lda #LO(music_data)
ldx #HI(music_data)
.play
pha
txa
pha
lda #0
tax
.L1
sta vars_start,x
inx
cpx #vars_end-vars_start
bne L1
lda #OP_NOP
sta noise_opcode+0
pla
sta pattern_ptr+1
pla
sta pattern_ptr+0
ldy #0
lda (pattern_ptr),y
sta loop_ptr+0
iny
lda (pattern_ptr),y
sta loop_ptr+1
lda pattern_ptr+0
clc
adc #2
sta pattern_ptr+0
bcc play_loop
inc pattern_ptr+1
.play_loop
ldy #1
lda (pattern_ptr),y ;duration of the row (0=loop), bit 7 percussion
bne no_loop
.return_loop
lda loop_ptr+0
sta pattern_ptr+0
lda loop_ptr+1
sta pattern_ptr+1
jmp play_loop
.no_loop
sta row_length
dey
lda (pattern_ptr),y ;flags DDDN4321 (Drum, Noise, 1-4 channel update)
iny
iny
sta row_flags
lsr row_flags
bcc skip_note_0
lda (pattern_ptr),y
iny
sta ch0_add+0
lda (pattern_ptr),y
iny
sta ch0_add+1
.skip_note_0
lsr row_flags
bcc skip_note_1
lda (pattern_ptr),y
iny
sta ch1_add+0
lda (pattern_ptr),y
iny
sta ch1_add+1
.skip_note_1
lsr row_flags
bcc skip_note_2
lda (pattern_ptr),y
iny
sta ch2_add+0
lda (pattern_ptr),y
iny
sta ch2_add+1
.skip_note_2
lsr row_flags
bcc skip_note_3
lda (pattern_ptr),y
iny
sta ch3_add+0
lda (pattern_ptr),y
iny
sta ch3_add+1
.skip_note_3
lsr row_flags
bcc skip_mode_change
ldx #OP_NOP
lda (pattern_ptr),y
beq L2
ldx #OP_ROL_A
.L2
stx noise_opcode+0
iny
iny
.skip_mode_change
lda row_flags
beq skip_drum
asl a
tax
lda sample_list+0-2,x
sta sample_ptr+0
lda sample_list+1-2,x
sta sample_ptr+1
lda #$80
sta sample_mask
.skip_drum
lda row_length
bpl skip_percussion
and #$7f ;clear percussion flag
sta row_length
lda (pattern_ptr),y ;read noise volume
iny
sta noise_volume
lda (pattern_ptr),y ;read noise pitch
iny
sta noise_div
sta noise_pitch
tya
pha
lda #LO(2174) ;utz's rand seed
sta noise_add+0
sta noise_acc+0
lda #HI(2174)
sta noise_add+1
sta noise_acc+1
ldx #LO(175*64*2/72) ;noise duration, takes as long as inner sound loop
lda #HI(175*64*2/72)
sta noise_cnt
.noise_loop
dec noise_div ;5
beq noise_update ;2 Not Taken / 3 Taken
.noise_skip
FOR n, 0, 13 ;28
nop
NEXT
lda 0 ;3
jmp noise_next ;3
.noise_update
lda noise_acc+0 ;3
adc noise_add+0 ;3
sta noise_acc+0 ;3
lda noise_acc+1 ;3
adc noise_add+1 ;3
cmp #$80 ;2
rol a ;2
sta noise_acc+1 ;3
inc noise_add+1 ;5
lda noise_pitch ;3
sta noise_div ;3
.noise_next
lda noise_acc+1 ;3
IF DEBUG
jsr COLOR4
ENDIF
cmp noise_volume ;3
bcc L4 ;2/3
lda #$00 ;2
jmp L5 ;3
.L4
lda #$0f ;2
nop ;2 dummy
.L5
ora #%10010000 ;2 bit-7 (latch/data), bits-6/5 (channel - 0), bit 4 (type - latch volume)
sta SYSVIA_ORAS ;4 Write reg/data to SN76489
txa ;2
bne L6 ;2/3
lda noise_cnt ;3
beq noise_done ;2/3
dec noise_cnt ;5
.L6
IF NTSC
nop ;2 to make pitch AND speed match between PAL/NTSC
ENDIF
dex ;2
jmp noise_loop ;3=72t
.noise_done
pla
tay
.skip_percussion
tya
clc
adc pattern_ptr+0
sta pattern_ptr+0
bcc L7
inc pattern_ptr+1
.L7
lda sample_mask
sta sample_bit
.sound_loop_0
ldx #64 ;internal loop runs 64 times
ldy #0 ;sample ptr inside the loop
.sound_loop
lda (sample_ptr),y ;5+
and sample_bit ;3
beq L8 ;2/3
lda #$0f ;2
jmp L9 ;3
.L8
nop ;2
nop ;2
.L9
sta sample_out ;3
;18
lsr sample_bit ;5
beq sample_inc ;2/3
lda 0 ;3 dummy
lda 0 ;3 dummy
jmp sample_next ;3
.sample_inc
lda sample_mask ;3
sta sample_bit ;3
iny ;2
.sample_next
lda #0 ;2
sta ch_mixer ;3
;39
lda ch0_acc+0 ;3
clc ;2
adc ch0_add+0 ;3
sta ch0_acc+0 ;3
lda ch0_acc+1 ;3
adc ch0_add+1 ;3
rol ch_mixer ;5
sta ch0_acc+1 ;3
lda ch1_acc+0 ;3
clc ;2
adc ch1_add+0 ;3
sta ch1_acc+0 ;3
lda ch1_acc+1 ;3
adc ch1_add+1 ;3
rol ch_mixer ;5
sta ch1_acc+1 ;3
lda ch2_acc+0 ;3
clc ;2
adc ch2_add+0 ;3
sta ch2_acc+0 ;3
lda ch2_acc+1 ;3
adc ch2_add+1 ;3
rol ch_mixer ;5
sta ch2_acc+1 ;3
lda ch3_acc+0 ;3
clc ;2
adc ch3_add+0 ;3
sta ch3_acc+0 ;3
lda ch3_acc+1 ;3
adc ch3_add+1 ;3
rol ch_mixer ;5
cmp #$80 ;2 needed for rol a, to match Z80's srl h
.noise_opcode
nop ;2 nop or rol a, self-modifying code here!
sta ch3_acc+1 ;3
;141
lda ch_acc ;3
clc ;2
adc ch_mixer ;3
bne L10 ;2/3
dec temp ;5 dummy
jmp L11 ;3
.L10
adc #$ff ;2
sta ch_acc ;3
lda #$0f ;2
.L11
ora sample_out ;3
IF DEBUG
jsr COLOR4
ENDIF
ora #%10010000 ;2 bit-7 (latch/data), bits-6/5 (channel - 0), bit 4 (type - latch volume)
sta SYSVIA_ORAS ;4 Write reg/data to SN76489
IF NTSC
nop ;2 to make pitch and speed match between PAL/NTSC
ENDIF
dex ;2
bne sound_loop ;2/3 = 175t
dey ;last byte of a 64/8 byte sample packet is #80 means it was the last packet
lda (sample_ptr),y
cmp #$80
bne sample_no_stop
lda #0 ;disable sample reading
sta sample_mask
.sample_no_stop
iny
tya
clc
adc sample_ptr+0
sta sample_ptr+0
bcc L14
inc sample_ptr+1
.L14
dec row_length
beq L15
jmp sound_loop_0
.L15
jmp play_loop
.COLOR4
pha
beq L16
lda #$00+(3EOR7)
sta $FE21
jmp L17
.L16
lda #$00+(0EOR7)
sta $FE21
.L17
pla
cli ; Enable interrupts
RESET_SOUND_CHIP
rts
.sample_list
equw sample_1
equw sample_2
equw sample_3
equw sample_4
equw sample_5
equw sample_6
equw sample_7
INCLUDE "tracks\lets_go.track.6502"
.end
SAVE "MAIN",start,end,init
\ ******************************************************************
\ * Memory Info
\ ******************************************************************
PRINT "-----------------------"
PRINT " 1-BIT SQUATM PLAYER "
PRINT "-----------------------"
PRINT "CODE size = ", ~end-start
PRINT "-----------------------"
PRINT "HIGH WATERMARK = ", ~P%
PRINT "FREE = ", ~start+end
PRINT "-----------------------"
\ ******************************************************************
\ * Supporting Files
\ ******************************************************************
PUTBASIC "loader.bas","LOADER"
PUTFILE "screens\title.bin","TITLE",DISPLAY_START
PUTFILE "BOOT","!BOOT",$ffff