; rick-roll-md Z80 PCM driver (z80asm syntax). ; ; Unsigned 8-bit samples from the cartridge, through the YM2612 DAC, one per 3 FM ; sample periods: 17,594 Hz on PAL, 17,758 Hz on NTSC. Z80 clock and FM clock are ; both fixed divisions of the master clock, so one output period is 201.6 Z80 cycles ; in either region and this loop needs no region switch. ; ; Pacing: YM2612 Timer A is armed for 12 FM samples = 4 of ours and free-runs; the loop ; is unrolled four ways; iterations 1-3 are cycle-padded to just under the period and ; iteration 4 spins on the timer flag, which absorbs the fraction, the padding and any ; stall on the 68000 bus (a bank-window read that collides with a DMA). A group that ; runs late is followed by groups that catch up at the padding slack - but NO path may ; run late for many groups in a row: once a flag reset lands before the overflow it ; should follow, every later spin passes on a stale flag and the loop free-runs until ; it has lapped the timer, losing 4 samples. So every per-iteration check costs the ; same on its common path in every row, and the slow paths do less, not more. ; Iterations 1-3 also copy 2+3+3 bytes from the bank window into the ring: 8 per group ; of 4 samples, so the ring refills at twice the drain rate whenever the 68000 allows ; it (V_GO) - it says no around its DMA windows, ~30 % of the time. ; ; Memory: $0000-$0EFF code, $0F00-$0F7F variables shared with the 68000, ; $0F80-$0FFF stack, $1000-$1FFF ring buffer (4 KB = 230 ms). ; Registers in the loop: BC = ring read pointer, DE = ring write pointer, ; HL = source pointer inside the bank window ($8000-$FFFF); ; HL' = samples played (16-bit, stored to V_PLAYED each group), DE' = 4. ; LDI copies (HL)->(DE) and increments both but also decrements BC, so every LDI is ; followed by INC BC. ; The stream lives in 68000 space at window 7 ($380000-$3FFFFF); the 68000 maps the ; stream's successive 512 KB pages there when the Z80 asks (V_WANT_PAGE/V_PAGE_ACK) ; and gates bank reads with V_GO. RING_LO: equ $10 ; ring is $1000-$1FFF RING_END: equ $20 STACK_TOP: equ $1000 YM_A0: equ $4000 ; part I address / status YM_D0: equ $4001 ; part I data YM_A1: equ $4002 ; part II address YM_D1: equ $4003 ; part II data BANK_REG: equ $6000 ; -- variables shared with the 68000 (it accesses them with the bus requested) -------- V_GO: equ $0F00 ; byte: 1 = the Z80 may read the bank window now V_WANT_PAGE: equ $0F01 ; byte: 512 KB page of the stream wanted in window 7 V_PAGE_ACK: equ $0F02 ; byte: page the 68000 has mapped there (informational) V_RUN: equ $0F03 ; byte: 68000 sets 1 to start playing V_PLAYED: equ $0F04 ; u16 LE: samples output (wraps; the 68000 extends it) V_BANK: equ $0F06 ; byte: 32 KB bank within the page, 0..15 V_READY: equ $0F07 ; byte: 1 once the ring is primed (68000 waits for it) V_REBANK: equ $0F08 ; byte: 1 = the window register must be rewritten before ; the next read, because the page under it changed org $0000 start: di im 1 ld sp,STACK_TOP xor a ; only our own variables: the 68000 owns the rest ld (V_BANK),a ld (V_READY),a ld (V_PLAYED),a ld (V_PLAYED+1),a ld (V_REBANK),a ; Z80 RAM powers up with garbage on hardware call build_bank_table ; DAC on; Timer A = 12 FM samples (NA = 1012 = $3F4), load + enable + reset flag ld a,$B6 ; channel 6 pan: L + R (part II, register $B6) - reset leaves it muted ld (YM_A1),a call ym_wait ld a,$C0 ld (YM_D1),a call ym_wait ld a,$2B call ym_addr ld a,$80 call ym_data ld a,$24 call ym_addr ld a,$FD call ym_data ld a,$25 call ym_addr ld a,$00 call ym_data ld a,$27 call ym_addr ld a,$15 call ym_data call set_bank ; window 7, bank 0 of page 0 exx ld hl,0 ; HL' = played ld de,4 ; DE' = 4 exx ld bc,$1000 ; read pointer ld de,$1000 ; write pointer ld hl,$8000 ; source: start of the bank window call prime ; the ring is full before we are told to start ld a,1 ld (V_READY),a wait_run: ld a,(V_RUN) or a jr z,wait_run ; Lock to the timer before the first group: clear the stale flag, wait for ; a fresh overflow, clear that. Otherwise the first reset may land just ; before an overflow, after which every spin sees the previous overflow's ; flag and the loop free-runs (0.96 periods a group) for ~23 groups until ; it has lapped the timer - audible as a chirp, and the same mechanism by ; which any group that overruns by nearly a whole period loses 4 samples. ; The reset takes effect a few chip cycles after the write. Do NOT wait to ; see the flag clear: the reset is a one-cycle pulse and an overflow is ; too, so when the overflow falls in the cycle after the reset the flag is ; clear for one FM cycle (42 MCLK, a seventh of a Z80 status read) and set ; again - a fresh overflow, so the chip is right - and a "see it clear" ; loop spins for ever, the 68000 waiting on V_PLAYED behind it (Nuked ; ym3438.c OPN2_DoTimerA: timer_a_reset clears the flag in its cycle, the ; else branch ORs the overflow in the next). Random power-on phase puts an ; overflow in the ~8 cycles between the write and the first read about ; once in 36 boots. BUSY instead: it holds for 32 cycles after a data ; write, by when the reset has long landed, and an overflow that raced in ; is at most 32 cycles old, a ninth of a period, which the first group's ; spin absorbs. ld a,$27 call ym_addr ld a,$15 call ym_data call ym_wait ; BUSY clear: the reset has taken effect sync: ld a,(YM_A0) rra jr nc,sync ld a,$27 call ym_addr ld a,$15 call ym_data ld a,$2A ; latch the DAC data register call ym_addr ; -- the loop ------------------------------------------------------------------------------ ; Cycle counts are the common path; see the notes at the end for the budget. loop: ; iteration 1: sample, group checks, 2 bytes ~189 ld a,(bc) ; 7 ld (YM_D0),a ; 13 inc c ; 4 call z,rd_row ; 10 ld a,(V_GO) ; 13 0 = shut, 1 = open, 2 = open and the page under sub 1 ; 7 the window has just changed: SUB sets carry on jr c,g_nofill ; 7 0 and zero on 1, so one extra 7-cycle branch jr nz,g_rebank ; 7 buys the third state (was OR A + JR Z = 11) ld a,b ; 4 room: the write row must not be the row below the read row sub d ; 4 and $0F ; 7 cp 1 ; 7 jr z,g_nofill ; 7 ld a,e ; 4 near the ring's end? (low byte first: the row cp $FD ; 7 compare then only runs in a row's last 3 bytes, so jr c,i1_ok_r ; 12 the common path costs the same in every row) ld a,d cp RING_END-1 jr z,slow1 i1_ok_r: ld a,l ; 4 near the bank's end? cp $FD ; 7 jr c,i1_ok_s ; 12 ld a,h cp $FF jr z,slow1 i1_ok_s: ldi ; 16 inc bc ; 6 ldi ; 16 inc bc ; 6 i1_done: jp i2f ; 10 the third gate state costs 10 over OR A + JR Z, ; which the three pad nops that used to sit here ; paid for: 188 + 10 = 198, just under the period ; as this row was before at 188 + 12 = 200 slow1: ; The gate is decided once a group, in iteration 1, and iterations 2 and 3 ; then fill without asking again. A page crossing is detected in here, in ; slow_copy's call to next_bank, which shuts the gate - too late for the ; rest of this group, which would copy 3 + 3 more bytes through a window ; whose page is about to be replaced. (Measured: exactly six bytes of the ; old page spliced into the stream at every crossing, leaving every later ; sample six late.) So re-read the gate after any slow copy and, if it has ; shut, take the rest of the group down the rows that do not fill. Costs ; nothing on the fast path, which cannot cross a window boundary. call slow_copy ld a,(V_GO) or a jp z,i2n jp i1_done g_rebank: ; V_GO said 2: the 68000 has mapped the page this driver asked for. Write the ; bank register now - a write by *this* CPU is the only one that re-resolves ; the window - and fill nothing this group: set_bank is ~184 cycles, so the ; group runs a little over its period once per page crossing, seven times in ; the song, and the following groups take it back at the padding slack. ; Falls into the no-fill rows, which are the cheap ones. ; Clearing V_REBANK is what tells the 68000 to go back to writing V_GO = 1. xor a ld (V_REBANK),a ld a,1 ; back to a plain open gate: without this every later ld (V_GO),a ; group reads 2 and rebanks again, filling nothing push bc push de call set_bank pop de pop bc ld hl,$8000 jp i2n g_nofill: ld a,5 ; pad: 7 + 4*16 + 11 = 82 g_pad: dec a jr nz,g_pad nop ; was 191 + 2 nops = 199; SUB 1 costs 3 more than jp i2n ; OR A, so one nop fewer brings it back to 198 ; iterations 2 and 3 with fills: sample, 3 bytes ~202 i2f: ld a,(bc) ; 7 ld (YM_D0),a ; 13 inc c ; 4 call z,rd_row ; 10 ld a,e cp $FD jr c,i2_ok_r ld a,d cp RING_END-1 jr z,slow2 i2_ok_r: ld a,l cp $FD jr c,i2_ok_s ld a,h cp $FF jr z,slow2 i2_ok_s: ldi ; 16 inc bc ; 6 ldi ; 16 inc bc ; 6 ldi ; 16 inc bc ; 6 i2_done: ld a,2 ; pad: 7 + 16 + 11 = 34 (measured 204 with 3: now ~200) i2_pad: dec a jr nz,i2_pad nop nop nop i3f: ld a,(bc) ; --- iteration 3 --- ld (YM_D0),a inc c call z,rd_row ld a,e cp $FD jr c,i3_ok_r ld a,d cp RING_END-1 jr z,slow3 i3_ok_r: ld a,l cp $FD jr c,i3_ok_s ld a,h cp $FF jr z,slow3 i3_ok_s: ldi inc bc ldi inc bc ldi inc bc i3_done: ld a,2 ; measured 215 with 3: now ~199 i3_pad: dec a jr nz,i3_pad jp i4 slow2: call slow_copy ; the slow paths copy one byte and skip the padding, so ld a,(V_GO) ; a slow iteration still fits the period (see slow_copy) or a ; gate shut by a crossing in there? then stop filling jp z,i3n jp i3f slow3: call slow_copy jp i4 ; iterations 2 and 3 without fills ~196 i2n: ld a,(bc) ld (YM_D0),a inc c call z,rd_row ld a,10 ; pad: 7 + 9*16 + 11 = 162 (measured 179 with 9: now ~199) i2n_pad: dec a jr nz,i2n_pad nop i3n: ld a,(bc) ; --- iteration 3 --- ld (YM_D0),a inc c call z,rd_row ld a,10 i3n_pad: dec a jr nz,i3n_pad nop ; iteration 4: sample, resync on Timer A, reset it, count ~163 + spin i4: ld a,(bc) ; 7 ld (YM_D0),a ; 13 inc c ; 4 call z,rd_row ; 10 ; nearly empty (read and write pointers in the same row): repeat this group ; rather than run a lap into stale data - the picture waits with the sound ld a,b ; 4 cp d ; 4 jr nz,i4_ok ; 12 ld a,c sub 4 ld c,a jr nc,i4_ok dec b ld a,b cp RING_LO-1 jr nz,i4_ok ld b,RING_END-1 i4_ok: exx ; 4 add hl,de ; 11 played += 4 ld (V_PLAYED),hl ; 16 exx ; 4 spin: ld a,(YM_A0) ; 13 rra ; 4 bit 0 (Timer A overflow) -> carry jr nc,spin ; 12/7 ld a,$27 ; 7 ld (YM_A0),a ; 13 ld a,$15 ; 7 ld (YM_D0),a ; 13 reset the flag; BUSY ~90 cycles, next DAC write >150 away ld a,$2A ; 7 ld (YM_A0),a ; 13 re-latch the DAC register jp loop ; 10 ; -- slow paths ---------------------------------------------------------------------------- rd_row: ; the read pointer crossed into the next 256-byte row of the ring inc b ld a,b cp RING_END ret nz ld b,RING_LO ret slow_copy: ; copy one byte (HL)->(DE) with every wrap check; leaves BC/DE/HL correct. ; Taken in the last 3 bytes of a ring row or of the bank window, where the ; fast LDI run could step past the end. One byte instead of 2-3 costs the ; fill a little (it runs at twice the drain rate, so nothing is lost) and ; keeps the iteration ~60 cycles over its fast cost, inside the padding ; slack - a group that overruns the Timer A period by nearly a whole ; period gets its flag reset after the next overflow and loses 4 samples. ld a,(hl) ld (de),a inc hl ld a,h or a call z,next_bank inc e ret nz inc d ld a,d cp RING_END ret nz ld d,RING_LO ret next_bank: ; the source ran off the end of the 32 KB window ld a,(V_BANK) inc a cp 16 jr nz,nb_set ; Page crossing. Do NOT point the window at bank 0 here: the page under it ; is about to be replaced, and the bank register must be written *after* ; the 68000 has mapped the new page, not before. Establishing a bank ; against a page that is about to change is the wrong order on any ; machine, and BlastEm makes it visible: it resolves this window to a host ; pointer when the Z80 writes the register (genesis.c ; gen_update_z80_bank_pointer) and never re-resolves it when the mapper ; moves the page, so the window keeps showing the old page until the Z80 ; writes the register again - one whole bank of audio from 512 KB back. ; So just ask, close the gate, and let the loop rebank once it reopens. xor a ld (V_BANK),a ld a,(V_WANT_PAGE) inc a ld (V_WANT_PAGE),a ; ask the 68000 for the next page xor a ld (V_GO),a ; and stop reading until it says so ld a,1 ld (V_REBANK),a ld hl,$8000 ret nb_set: ld (V_BANK),a push bc push de call set_bank pop de pop bc ld hl,$8000 ret prime: ; fill the ring to one row short of full, pausing while V_GO is clear ld a,(V_GO) or a jr z,prime ld a,b sub d and $0F cp 1 ret z call slow_copy jr prime set_bank: ; bank register: A15..A23 of the 68000 address, LSB first, one write each, ; from the 9-byte table for this bank (built at start). 9 x 16 + ~40 cycles. ld a,(V_BANK) and $0F ld l,a ld h,0 add hl,hl ; x 8 add hl,hl add hl,hl ld e,a ld d,0 add hl,de ; x 9 ld de,bank_table add hl,de ld de,BANK_REG ldi ldi ldi ldi ldi ldi ldi ldi ldi ret build_bank_table: ; Window 7 = $380000: A15-A18 = bank (LSB first), A19-A23 = 1,1,1,0,0 ld hl,bank_table ld b,0 ; bank bt_bank: ld c,b ld a,c ld (hl),a inc hl srl c ld a,c ld (hl),a inc hl srl c ld a,c ld (hl),a inc hl srl c ld a,c ld (hl),a inc hl ld a,1 ld (hl),a inc hl ld (hl),a inc hl ld (hl),a inc hl xor a ld (hl),a inc hl ld (hl),a inc hl inc b ld a,b cp 16 jr nz,bt_bank ret ym_addr: push af ya_w: ld a,(YM_A0) rlca jr c,ya_w pop af ld (YM_A0),a ret ym_wait: ld a,(YM_A0) rlca jr c,ym_wait ret ym_data: push af yd_w: ld a,(YM_A0) rlca jr c,yd_w pop af ld (YM_D0),a ret bank_table: ds 16*9 end_of_code: ; Budget notes (Z80 cycles, nominal period 201.6): ; iteration 1 (fill) : 34 + 24 + 29 + 36 + 44 + 10 = 177 (early by ~25; the spin absorbs) ; iteration 1 (no fill): 34 + 24 + (room check skipped or 29) + 146 + 10 ~ 190-214 ; iterations 2/3 (fill): 34 + 36 + 66 + 66 = 202 ; iterations 2/3 (none): 34 + 162 = 196 ; iteration 4 : 34 + 35 + spin(>=24) + 40 + 20 + 10 = 163 + spin ; A group of four is ~744 + spin against the timer's 806.4: the spin waits ~60 cycles ; (3 polls); a bank-read stall of up to ~60 cycles per group is absorbed silently.