2022-11-20 23:43:34 +00:00
|
|
|
SAVMSC = $58
|
2022-12-08 04:37:52 +00:00
|
|
|
VDSLST = $200
|
|
|
|
VDSLSTL = $200
|
|
|
|
VDSLSTH = $201
|
2022-11-25 19:51:27 +00:00
|
|
|
COLPF0 = $D016
|
|
|
|
COLPF1 = $D017
|
|
|
|
COLPF2 = $D018
|
|
|
|
COLPF3 = $D019
|
|
|
|
COLBK = $D01A
|
|
|
|
|
2022-11-30 07:59:38 +00:00
|
|
|
AUDC1 = $D201
|
2022-11-29 20:59:26 +00:00
|
|
|
DMACTL = $D400
|
2022-11-29 22:54:02 +00:00
|
|
|
DLISTL = $D402
|
|
|
|
DLISTH = $D403
|
2022-11-25 19:51:27 +00:00
|
|
|
WSYNC = $D40A
|
|
|
|
VCOUNT = $D40B
|
|
|
|
NMIEN = $D40E
|
|
|
|
|
2022-11-26 16:48:01 +00:00
|
|
|
temp1l = $80
|
|
|
|
temp1h = $81
|
|
|
|
temp1 = temp1l
|
|
|
|
temp2l = $82
|
|
|
|
temp2h = $83
|
|
|
|
temp2 = temp2l
|
2022-11-30 07:59:38 +00:00
|
|
|
sample_ptrl = $84
|
|
|
|
sample_ptrh = $85
|
|
|
|
sample_ptr = sample_ptrl
|
2022-11-30 11:04:31 +00:00
|
|
|
scanline = $86
|
2022-12-08 00:35:07 +00:00
|
|
|
audiotemp = $87
|
2022-12-08 04:37:52 +00:00
|
|
|
frame_counter = $89
|
2022-11-26 16:48:01 +00:00
|
|
|
|
2022-11-30 16:53:47 +00:00
|
|
|
height = 160
|
2022-11-26 16:48:01 +00:00
|
|
|
bytes_per_line = 40
|
|
|
|
pages_per_frame = 32
|
2022-11-30 07:59:38 +00:00
|
|
|
lines_per_frame = 262
|
2022-11-30 16:53:47 +00:00
|
|
|
;scanline_offset = 31 + (40 - 24) / 2
|
2022-12-10 21:12:52 +00:00
|
|
|
scanline_offset = 46
|
2022-11-30 11:04:31 +00:00
|
|
|
scanline_max = (lines_per_frame - scanline_offset) / 2
|
2022-11-26 16:48:01 +00:00
|
|
|
|
2022-11-25 19:51:27 +00:00
|
|
|
.data
|
|
|
|
|
2022-11-30 07:59:38 +00:00
|
|
|
.import audio_samples
|
|
|
|
.import audio_samples_end
|
2022-11-25 19:51:27 +00:00
|
|
|
|
2022-12-08 02:20:24 +00:00
|
|
|
.import frame1_top
|
|
|
|
.import frame1_bottom
|
2022-12-10 21:01:29 +00:00
|
|
|
.import frame1_palette1_even
|
|
|
|
.import frame1_palette1_odd
|
|
|
|
.import frame1_palette2_even
|
|
|
|
.import frame1_palette2_odd
|
|
|
|
.import frame1_palette3_even
|
|
|
|
.import frame1_palette3_odd
|
2022-12-08 02:20:24 +00:00
|
|
|
.import frame2_top
|
|
|
|
.import frame2_bottom
|
2022-12-10 21:01:29 +00:00
|
|
|
.import frame2_palette1_even
|
|
|
|
.import frame2_palette1_odd
|
|
|
|
.import frame2_palette2_even
|
|
|
|
.import frame2_palette2_odd
|
|
|
|
.import frame2_palette3_even
|
|
|
|
.import frame2_palette3_odd
|
2022-12-08 02:37:32 +00:00
|
|
|
.import displaylist
|
2022-12-08 01:11:22 +00:00
|
|
|
|
2022-12-08 00:35:07 +00:00
|
|
|
audio_high_byte:
|
|
|
|
.scope
|
|
|
|
.macro byteseq val
|
|
|
|
.repeat 16
|
|
|
|
.byte val | $10
|
|
|
|
.endrep
|
|
|
|
.endmacro
|
|
|
|
byteseq $0
|
|
|
|
byteseq $1
|
|
|
|
byteseq $2
|
|
|
|
byteseq $3
|
|
|
|
byteseq $4
|
|
|
|
byteseq $5
|
|
|
|
byteseq $7
|
|
|
|
byteseq $8
|
|
|
|
byteseq $9
|
|
|
|
byteseq $a
|
|
|
|
byteseq $b
|
|
|
|
byteseq $c
|
|
|
|
byteseq $d
|
|
|
|
byteseq $e
|
|
|
|
byteseq $f
|
|
|
|
.endscope
|
2022-11-20 23:43:34 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
|
|
|
|
.export start
|
|
|
|
|
|
|
|
.proc start
|
2022-11-30 07:59:38 +00:00
|
|
|
; Set up the audio sample buffer
|
|
|
|
lda #.lobyte(audio_samples)
|
|
|
|
sta sample_ptrl
|
|
|
|
lda #.hibyte(audio_samples)
|
|
|
|
sta sample_ptrh
|
|
|
|
|
2022-11-25 19:51:27 +00:00
|
|
|
; Disable display DMA
|
|
|
|
lda #$00
|
2022-11-29 20:59:26 +00:00
|
|
|
sta DMACTL
|
2022-11-26 16:48:01 +00:00
|
|
|
|
2022-11-29 22:54:02 +00:00
|
|
|
; Disable VBI and DLI but allow Reset
|
2022-12-08 02:20:24 +00:00
|
|
|
lda #$20
|
2022-11-29 22:54:02 +00:00
|
|
|
sta NMIEN
|
|
|
|
|
2022-11-25 19:51:27 +00:00
|
|
|
; Set up the display list
|
2022-11-26 01:35:10 +00:00
|
|
|
lda #.lobyte(displaylist)
|
2022-11-29 22:54:02 +00:00
|
|
|
sta DLISTL
|
2022-11-26 01:35:10 +00:00
|
|
|
lda #.hibyte(displaylist)
|
2022-11-29 22:54:02 +00:00
|
|
|
sta DLISTH
|
2022-11-25 19:51:27 +00:00
|
|
|
|
2022-12-08 04:37:52 +00:00
|
|
|
; Set up the DLI handler
|
|
|
|
lda #.lobyte(dli_handler)
|
|
|
|
sta VDSLSTL
|
|
|
|
lda #.hibyte(dli_handler)
|
|
|
|
sta VDSLSTH
|
|
|
|
|
|
|
|
; Disable VBI but allow Reset and DLI
|
|
|
|
lda #$a0
|
|
|
|
sta NMIEN
|
|
|
|
|
2022-11-25 19:51:27 +00:00
|
|
|
; Manually wait for first scan line
|
|
|
|
wait_vblank:
|
2022-11-30 11:04:31 +00:00
|
|
|
sta WSYNC
|
2022-11-25 19:51:27 +00:00
|
|
|
lda VCOUNT
|
2022-11-26 01:35:10 +00:00
|
|
|
bne wait_vblank
|
2022-11-25 19:51:27 +00:00
|
|
|
|
|
|
|
; Re-enable display DMA
|
|
|
|
lda #$22
|
2022-11-29 20:59:26 +00:00
|
|
|
sta DMACTL
|
|
|
|
|
2022-11-30 00:32:37 +00:00
|
|
|
wait_start:
|
2022-11-30 11:04:31 +00:00
|
|
|
; Wait for the vblank
|
|
|
|
; Resynchronize the scanline counter
|
2022-11-30 07:59:38 +00:00
|
|
|
wait_loop:
|
2022-12-11 20:13:25 +00:00
|
|
|
ldy VCOUNT ; 4 cycles
|
|
|
|
bne wait_loop ; 2 cycles
|
2022-11-26 03:17:34 +00:00
|
|
|
|
2022-12-08 00:35:07 +00:00
|
|
|
.macro audio_prep
|
2022-12-08 00:43:45 +00:00
|
|
|
; Y is VCOUNT at entry
|
2022-12-08 00:35:07 +00:00
|
|
|
lda (sample_ptr),y ; 5/6 cyc
|
2022-12-11 20:13:25 +00:00
|
|
|
sta audiotemp ; 3 cyc
|
2022-12-08 00:35:07 +00:00
|
|
|
.endmacro
|
|
|
|
|
2022-12-10 21:01:29 +00:00
|
|
|
.macro inner_scanline frame_offset, line_offset
|
|
|
|
; Y should be VCOUNT at entry
|
2022-11-30 12:52:40 +00:00
|
|
|
; it'll fire on unused lines, but harmlessly
|
2022-12-10 21:01:29 +00:00
|
|
|
;ldy scanline ; 3 cyc
|
|
|
|
;inc scanline ; 5 cyc
|
2022-11-30 11:04:31 +00:00
|
|
|
|
2022-12-11 20:13:25 +00:00
|
|
|
; 23-26 cycles before break
|
2022-11-30 07:59:38 +00:00
|
|
|
; Leisurely memory fetches
|
2022-12-11 20:13:25 +00:00
|
|
|
lda frame1_palette1_even + frame_offset + line_offset - scanline_offset / 2,y ; 4/5 @FIXME alternate
|
|
|
|
pha ; 3
|
|
|
|
ldx frame1_palette2_even + frame_offset + line_offset - scanline_offset / 2,y ; 4/5
|
|
|
|
lda frame1_palette3_even + frame_offset + line_offset - scanline_offset / 2,y ; 4/5
|
|
|
|
tay ; 2
|
|
|
|
pla ; 3
|
2022-11-30 07:59:38 +00:00
|
|
|
; Wait for horizontal blank
|
2022-12-11 20:13:25 +00:00
|
|
|
sta WSYNC ; 4
|
2022-11-30 07:59:38 +00:00
|
|
|
|
2022-12-11 20:13:25 +00:00
|
|
|
; 12 cycles after break
|
2022-11-30 07:59:38 +00:00
|
|
|
; Update color registers as fast as possible
|
2022-12-11 20:13:25 +00:00
|
|
|
sta COLPF0 ; 4
|
|
|
|
stx COLPF1 ; 4
|
|
|
|
sty COLPF2 ; 4
|
2022-11-30 07:59:38 +00:00
|
|
|
.endmacro
|
|
|
|
|
2022-12-08 14:33:05 +00:00
|
|
|
.macro audio_play_raw
|
2022-12-10 21:01:29 +00:00
|
|
|
;ldy VCOUNT ; set on entry
|
2022-12-08 14:33:05 +00:00
|
|
|
lda (sample_ptr),y ; 5/6 cyc
|
2022-12-11 20:13:25 +00:00
|
|
|
sta AUDC1 ; 4 cyc
|
2022-12-08 14:33:05 +00:00
|
|
|
.endmacro
|
|
|
|
|
2022-12-08 14:29:50 +00:00
|
|
|
.macro audio_play_lo
|
2022-12-08 00:35:07 +00:00
|
|
|
lda audiotemp ; 3 cyc
|
2022-12-08 02:20:24 +00:00
|
|
|
and #$0f ; 2 cyc
|
|
|
|
ora #$10 ; 2 cyc
|
2022-11-30 12:52:40 +00:00
|
|
|
sta AUDC1 ; 4 cyc
|
|
|
|
.endmacro
|
|
|
|
|
2022-12-08 00:35:07 +00:00
|
|
|
.macro audio_play_hi ; 12 cycles
|
|
|
|
ldy audiotemp ; 3 cyc
|
|
|
|
lda audio_high_byte,y ; 5 cyc
|
|
|
|
sta AUDC1 ; 4 cyc
|
|
|
|
.endmacro
|
2022-11-30 07:59:38 +00:00
|
|
|
|
2022-11-30 11:39:23 +00:00
|
|
|
.macro audio_inc
|
2022-12-12 01:23:41 +00:00
|
|
|
; 22 cycles
|
|
|
|
lda sample_ptrl ; 3 cyc
|
|
|
|
clc ; 2 cyc
|
|
|
|
adc #131 ; 2 cyc
|
|
|
|
sta sample_ptrl ; 3 cyc
|
|
|
|
lda sample_ptrh ; 3 cyc
|
|
|
|
adc #0 ; 2 cyc
|
|
|
|
sta sample_ptrh ; 3 cyc
|
|
|
|
cmp #.hibyte(audio_samples_end) ; 2 cyc
|
|
|
|
bmi audio_cont ; 2 cyc
|
|
|
|
|
2022-12-12 01:46:56 +00:00
|
|
|
sta WSYNC
|
|
|
|
|
2022-12-12 01:23:41 +00:00
|
|
|
; 10 cycles, optional
|
|
|
|
lda #.lobyte(audio_samples) ; 2
|
|
|
|
sta sample_ptrl ; 3
|
2022-12-11 20:13:25 +00:00
|
|
|
lda #.hibyte(audio_samples) ; 2
|
|
|
|
sta sample_ptrh ; 3
|
2022-11-30 12:01:55 +00:00
|
|
|
|
2022-12-12 01:46:56 +00:00
|
|
|
sta WSYNC
|
|
|
|
ldy VCOUNT ; 4 cycles
|
|
|
|
|
2022-11-30 12:01:55 +00:00
|
|
|
audio_cont:
|
2022-11-30 11:39:23 +00:00
|
|
|
.endmacro
|
2022-11-30 11:36:25 +00:00
|
|
|
|
2022-12-10 21:01:29 +00:00
|
|
|
.macro run_frame frame_offset
|
2022-12-08 04:37:52 +00:00
|
|
|
.scope
|
2022-12-12 00:37:47 +00:00
|
|
|
; each scanline is 228 color clocks
|
|
|
|
; that's 114 CPU cycles
|
|
|
|
; minus 41-43 for DMA leaves 71-73 clock cycles per line
|
|
|
|
|
2022-12-08 14:29:50 +00:00
|
|
|
each_scanline_pair:
|
2022-12-11 20:13:25 +00:00
|
|
|
sty scanline ; 3 cycles
|
|
|
|
inner_scanline frame_offset, 0 ; 23-26 cycles before break, 12 cycles after
|
2022-12-10 21:01:29 +00:00
|
|
|
|
2022-12-11 20:13:25 +00:00
|
|
|
ldy scanline ; 3 cycles
|
2022-12-12 01:23:41 +00:00
|
|
|
audio_play_raw ; 8-10 cycles
|
|
|
|
|
2022-12-11 20:13:25 +00:00
|
|
|
inner_scanline frame_offset, 128 ; 23-26 cycles before break, 12 cycles after
|
2022-12-11 21:09:14 +00:00
|
|
|
|
2022-12-12 00:37:47 +00:00
|
|
|
; pair cleanup: 6 cycles
|
2022-12-11 20:13:25 +00:00
|
|
|
ldy VCOUNT ; 4 cycles
|
|
|
|
bne each_scanline_pair ; 2 cycles
|
|
|
|
|
2022-12-12 01:23:41 +00:00
|
|
|
audio_inc ; 22-32 cycles
|
|
|
|
|
2022-12-12 00:37:47 +00:00
|
|
|
; frame cleanup: 11 cycles
|
2022-12-11 20:13:25 +00:00
|
|
|
lda frame_counter ; 3 cycles
|
|
|
|
eor #1 ; 2 cycles
|
|
|
|
sta frame_counter ; 3 cycles
|
2022-12-12 01:46:56 +00:00
|
|
|
;jmp wait_start ; 3 cycles
|
|
|
|
jmp each_frame ; 3 cycles
|
2022-12-08 04:37:52 +00:00
|
|
|
.endscope
|
|
|
|
.endmacro
|
2022-11-26 03:17:34 +00:00
|
|
|
|
2022-12-12 01:46:56 +00:00
|
|
|
each_frame:
|
2022-12-11 20:13:25 +00:00
|
|
|
lda frame_counter ; 3 cycles
|
|
|
|
beq run_frame1 ; 2 cycles
|
|
|
|
jmp run_frame2 ; 3 cycles
|
2022-12-08 14:29:50 +00:00
|
|
|
run_frame1:
|
2022-12-10 21:01:29 +00:00
|
|
|
run_frame 0
|
2022-12-08 13:59:09 +00:00
|
|
|
run_frame2:
|
2022-12-10 21:01:29 +00:00
|
|
|
run_frame 8192
|
2022-11-25 19:51:27 +00:00
|
|
|
|
2022-11-20 23:43:34 +00:00
|
|
|
.endproc
|
2022-11-26 03:17:34 +00:00
|
|
|
|
2022-12-08 04:37:52 +00:00
|
|
|
|
|
|
|
.proc dli_handler
|
2022-12-08 13:59:09 +00:00
|
|
|
lda #0
|
2022-12-08 04:37:52 +00:00
|
|
|
sta frame_counter
|
|
|
|
rti
|
2022-11-26 03:17:34 +00:00
|
|
|
.endproc
|