mode7: have some music playing...

This commit is contained in:
Vince Weaver 2018-03-13 13:19:13 -04:00
parent a15ab2a7af
commit 7ecbf2ef7a
4 changed files with 328 additions and 6 deletions

View File

@ -44,6 +44,7 @@ mode7_demo.o: mode7_demo.s mode7_demo_backgrounds.inc sprites.inc \
../asm_routines/gr_scroll.s \
../asm_routines/mockingboard_a.s \
credits.s mode7.s rasterbars.s starfield_demo.s \
interrupt_handler.s \
zp.inc
ca65 -o mode7_demo.o mode7_demo.s -l mode7_demo.lst

View File

@ -0,0 +1,219 @@
;================================
;================================
; mockingboard interrupt handler
;================================
;================================
; On Apple II/6502 the interrupt handler jumps to address in 0xfffe
; This is in the ROM, which saves the registers
; on older IIe it saved A to $45 (which could mess with DISK II)
; newer IIe doesn't do that.
; It then calculates if it is a BRK or not (which trashes A)
; Then it sets up the stack like an interrupt and calls 0x3fe
TIME_OFFSET EQU 13
interrupt_handler:
pha ; save A ; 3
; Should we save X and Y too?
; inc $0404 ; debug (flashes char onscreen)
bit $C404 ; clear 6522 interrupt by reading T1C-L ; 4
lda DONE_PLAYING ; 3
beq mb_play_music ; if song done, don't play music ; 3/2nt
jmp done_interrupt ; 3
;============
; 13
mb_play_music:
;======================================
; Write frames to Mockingboard
;======================================
; actually plays frame loaded at end of
; last interrupt, so 20ms behind?
mb_write_frame:
ldx #0 ; set up reg count ; 2
;============
; 2
;==================================
; loop through the 14 registers
; reading the value, then write out
;==================================
; inlined "write_ay_both" to save up to 156 (12*13) cycles
; unrolled
mb_write_loop:
lda REGISTER_DUMP,X ; load register value ; 4
cmp REGISTER_OLD,X ; compare with old values ; 4
beq mb_no_write ; 3/2nt
;=============
; typ 11
; special case R13. If it is 0xff, then don't update
; otherwise might spuriously reset the envelope settings
cpx #13 ; 2
bne mb_not_13 ; 3/2nt
cmp #$ff ; 2
beq mb_skip_13 ; 3/2nt
;============
; typ 5
mb_not_13:
; address
stx MOCK_6522_ORA1 ; put address on PA1 ; 4
stx MOCK_6522_ORA2 ; put address on PA2 ; 4
lda #MOCK_AY_LATCH_ADDR ; latch_address for PB1 ; 2
sta MOCK_6522_ORB1 ; latch_address on PB1 ; 4
sta MOCK_6522_ORB2 ; latch_address on PB2 ; 4
lda #MOCK_AY_INACTIVE ; go inactive ; 2
sta MOCK_6522_ORB1 ; 4
sta MOCK_6522_ORB2 ; 4
; value
lda REGISTER_DUMP,X ; load register value ; 4
sta MOCK_6522_ORA1 ; put value on PA1 ; 4
sta MOCK_6522_ORA2 ; put value on PA2 ; 4
lda #MOCK_AY_WRITE ; ; 2
sta MOCK_6522_ORB1 ; write on PB1 ; 4
sta MOCK_6522_ORB2 ; write on PB2 ; 4
lda #MOCK_AY_INACTIVE ; go inactive ; 2
sta MOCK_6522_ORB1 ; 4
sta MOCK_6522_ORB2 ; 4
;===========
; 62
mb_no_write:
inx ; point to next register ; 2
cpx #14 ; if 14 we're done ; 2
bmi mb_write_loop ; otherwise, loop ; 3/2nt
;============
; 7
mb_skip_13:
;=====================================
; Copy registers to old
;=====================================
ldx #13 ; 2
mb_reg_copy:
lda REGISTER_DUMP,X ; load register value ; 4
sta REGISTER_OLD,X ; compare with old values ; 4
dex ; 2
bpl mb_reg_copy ; 2nt/3
;=============
; 171
;===================================
; Load all 14 registers in advance
;===================================
; note, assuming not cross page boundary, not any slower
; then loading from zero page?
mb_load_values:
ldx #0 ; set up reg count ; 2
ldy MB_CHUNK_OFFSET ; get chunk offset ; 3
;=============
; 5
mb_load_loop:
lda (MB_ADDRL),y ; load register value ; 5
sta REGISTER_DUMP,X ; 4
;============
; 9
;====================
; point to next page
;====================
clc ; point to next interleaved ; 2
lda MB_ADDRH ; page by adding CHUNKSIZE ; 3
adc #CHUNKSIZE ; 3
sta MB_ADDRH ; 3
inx ; point to next register ; 2
cpx #14 ; if 14 we're done ; 2
bmi mb_load_loop ; otherwise, loop ; 3/2nt
;============
; 18
;=========================================
; if A_COARSE_TONE is $ff then we are done
lda A_COARSE_TONE ; 3
bpl mb_not_done ; 3/2nt
lda #1 ; set done playing ; 2
jmp quiet_exit ; 3
;===========
; typ 6
mb_not_done:
;==============================================
; incremement offset. If 0 move to next chunk
;==============================================
increment_offset:
inc MB_CHUNK_OFFSET ; increment offset ; 5
bne increment_done ; if not zero, done ; 3/2nt
inc WHICH_CHUNK
lda WHICH_CHUNK
cmp #CHUNKSIZE
bne increment_done
lda #0
sta WHICH_CHUNK
increment_done:
lda #>music_start
clc
adc WHICH_CHUNK
sta MB_ADDRH
;=================================
; Finally done with this interrupt
;=================================
done_interrupt:
jmp exit_interrupt
quiet_exit:
sta DONE_PLAYING
jsr clear_ay_both
;=====================================
; clear register area
;=====================================
ldx #13 ; 2
lda #0 ; 2
mb_clear_reg:
sta REGISTER_DUMP,X ; clear register value ; 4
sta REGISTER_OLD,X ; clear old values ; 4
dex ; 2
bpl mb_clear_reg ; 2nt/3
exit_interrupt:
pla ; restore a ; 4
rti ; return from interrupt ; 6
;============
; typical
; ???? cycles
REGISTER_OLD:
.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0

View File

@ -1,5 +1,7 @@
.include "zp.inc"
CHUNKSIZE EQU 4 ; hardcoded, based on krg file
square1_lo EQU $1000
square1_hi EQU $1200
square2_lo EQU $1400
@ -32,7 +34,83 @@ start:
jsr mockingboard_detect_slot4 ; call detection routine
stx MB_DETECTED
beq mockingboard_setup_done
;================================
; Mockingboard start
;================================
mockingboard_setup:
jsr mockingboard_init
jsr reset_ay_both
jsr clear_ay_both
;=========================
; Setup Interrupt Handler
;=========================
; Vector address goes to 0x3fe/0x3ff
; FIXME: should chain any existing handler
lda #<interrupt_handler
sta $03fe
lda #>interrupt_handler
sta $03ff
;============================
; Enable 50Hz clock on 6522
;============================
sei ; disable interrupts just in case
lda #$40 ; Continuous interrupts, don't touch PB7
sta $C40B ; ACR register
lda #$7F ; clear all interrupt flags
sta $C40E ; IER register (interrupt enable)
lda #$C0
sta $C40D ; IFR: 1100, enable interrupt on timer one oflow
sta $C40E ; IER: 1100, enable timer one interrupt
lda #$E7
sta $C404 ; write into low-order latch
lda #$4f
sta $C405 ; write into high-order latch,
; load both values into counter
; clear interrupt and start counting
; 4fe7 / 1e6 = .020s, 50Hz
;============================
; Start Playing
;============================
lda #0
sta DONE_PLAYING
sta WHICH_CHUNK
sta MB_CHUNK_OFFSET
sta MB_ADDRL ; we are aligned, so should be 0
lda #>music_start
sta MB_ADDRH
;=====================================
; clear register area
;=====================================
ldx #13 ; 2
lda #0 ; 2
mb_setup_clear_reg:
sta REGISTER_DUMP,X ; clear register value ; 4
sta REGISTER_OLD,X ; clear old values ; 4
dex ; 2
bpl mb_setup_clear_reg ; 2nt/3
cli ; start interrupts
mockingboard_setup_done:
;================================
; Clear screen and setup graphics
;================================
@ -260,7 +338,9 @@ title_routine:
.include "starfield_demo.s"
.include "rasterbars.s"
.include "credits.s"
.include "interrupt_handler.s"
.align 256
music_start:
.incbin "out.krg"

View File

@ -24,7 +24,28 @@ MASK EQU $2E
COLOR EQU $30
;INVFLG EQU $32
MB_DETECTED EQU $50
REGISTER_DUMP EQU $50
A_FINE_TONE EQU $50
A_COARSE_TONE EQU $51
B_FINE_TONE EQU $52
B_COARSE_TONE EQU $53
C_FINE_TONE EQU $54
C_COARSE_TONE EQU $55
NOISE EQU $56
ENABLE EQU $57
A_VOLUME EQU $58
B_VOLUME EQU $59
C_VOLUME EQU $5A
ENVELOPE_FINE EQU $5B
ENVELOPE_COARSE EQU $5C
ENVELOPE_SHAPE EQU $5D
COPY_OFFSET EQU $5E
DECODER_STATE EQU $5F
;; Flying Routine Only
@ -76,11 +97,12 @@ RANDOM_POINTER EQU $8F
FRAME_COUNT EQU $90
MB_VALUE EQU $91
MB_ADDRL EQU $91
MB_ADDRH EQU $92
DONE_PLAYING EQU $93
MB_CHUNK_OFFSET EQU $94
DONE_PLAYING EQU $92
MB_ADDRL EQU $93
MB_ADDRH EQU $94
MB_CHUNK_OFFSET EQU $95
MB_DETECTED EQU $96
WHICH_CHUNK EQU $97
; More zero-page addresses
; we try not to conflict with anything DOS, MONITOR or BASIC related