| rick-roll-md player, Phase 2: stream frames from ROM. | GNU as, Motorola operand syntax, register prefix optional. | | Per video frame (two hardware frames on PAL): | VBlank A : DMA this frame's new tiles into ring slots nothing on screen uses | VBlank B : DMA the patched name-table copy (RAM -> VRAM $C000), write CRAM if new | main loop: between B and the next A, fetch the next record and patch the RAM copy | | ROM: $000000 code | $001000 frame index (u32 each) | $010000 records (rrmd/rom.py) | RAM: $FF0000 name-table copy, 28 rows x 128 B | variables below it at $FFF000 .text .globl _start | -- vector table ------------------------------------------------------------------ .long 0x00FFFE00, _start .rept 24 .long exception .endr .long exception, exception, hblank, exception, vblank, exception .rept 32 .long exception .endr | -- header ($100) ----------------------------------------------------------------- .ascii "SEGA SSF " .ascii "(C)RRMD 2026.OCT" .ascii "RICK ROLL MD " .ascii "RICK ROLL MD " .ascii "GM 00000000-00" .word 0x0000 .ascii "J " .long 0x00000000, rom_end-1 | the actual last byte, for tools that read it .long 0x00FF0000, 0x00FFFFFF .ascii " " .ascii " " .ascii " " .ascii "E " | PAL only: every timing constant here is | derived from 313 lines x 3420 MCLK at 50 Hz, | and "JUE" lets an emulator choose NTSC | -- constants --------------------------------------------------------------------- .equ VDP_DATA, 0xC00000 .equ VDP_CTRL, 0xC00004 .equ VERSION, 0xA10001 .equ TMSS, 0xA14000 .equ BANK1, 0xA130F2 | Extended SSF window 1 page (word write, page in low byte) .equ BANK2, 0xA130F4 | window 2: the tile store's current page (codebook build) .equ WINDOW1, 0x080000 .equ PAGE_SHIFT, 19 .ifndef CODEBOOK .equ CODEBOOK, 0 .endif .ifndef AUDIO .equ AUDIO, 0 .endif .ifndef AUDIO_PAGE .equ AUDIO_PAGE, 0 .endif .equ BANK7, 0xA130FE | window 7 ($380000): the sample stream's current page .equ Z80_RAM, 0xA00000 .equ Z80_BUSREQ, 0xA11100 .equ Z80_RESET, 0xA11200 .equ V_GO, Z80_RAM+0x0F00 | shared with player/pcm.z80 .equ V_WANT_PAGE, Z80_RAM+0x0F01 .equ V_PAGE_ACK, Z80_RAM+0x0F02 .equ V_RUN, Z80_RAM+0x0F03 .equ GO_LINE, 215 | H-interrupt line: stop the Z80's bank reads before the blank's DMA .equ TABLE_RAM, 0xFF0000 | 28 x 128 = 3584 bytes .equ TABLE_BYTES, 3584 .equ VARS, 0xFFF000 .equ v_frame, VARS+0 | u32 next video frame to fetch .equ v_phase, VARS+4 | u16 0 = next VBlank is A, 1 = B .equ v_runs_ptr, VARS+8 | u32 -> this frame's DMA runs (0 = none pending) .equ v_runs_n, VARS+12 | u16 .equ v_cram_ptr, VARS+16 | u32 -> 64 CRAM words (0 = none pending) .equ v_table_ready, VARS+20 | u16 1 = RAM table patched for the pending frame .equ v_consumed, VARS+22 | u16 1 = VBlank B done, main loop may fetch .equ v_frames_total, VARS+24 | u32 .equ v_done, VARS+28 | u16 .equ v_audio_started, VARS+30 | u16 .equ v_page_acked, VARS+32 | u16: the stream page the 68000 has mapped into window 7 .equ v_end_timer, VARS+34 | u16: hardware frames left after the last video frame .equ v_played, VARS+36 | u32: samples played (Z80's 16-bit counter, extended here) .equ END_FRAMES, 150 | wake-ups after the last video frame (1.5 s of the 2 s silent tail), then stop the Z80 .equ v_due, VARS+40 | u32 16.16: sample count at which the pending frame is due .ifndef AUDIO_SPF_FP .equ AUDIO_SPF_FP, 46397928 | samples per video frame, 16.16 (PAL-locked: 707.976) .endif .equ V_PLAYED, Z80_RAM+0x0F04 .equ V_READY, Z80_RAM+0x0F07 .equ V_REBANK, Z80_RAM+0x0F08 | 0 = idle, 1 = page asked for, 2 = mapped, rebank owed .equ VRAM_PLANE_A_DMA, 0x40000083 | $C000, DMA bit set .equ CRAM_0, 0xC0000000 | -- entry ------------------------------------------------------------------------- _start: move.w #0x2700,sr move.b VERSION,d0 andi.b #0x0F,d0 beq.s 1f move.l #0x53454741,TMSS 1: lea VDP_CTRL,a0 lea VDP_DATA,a1 move.w (a0),d0 lea vdp_regs,a2 moveq #(vdp_regs_end-vdp_regs)/2-1,d1 2: move.w (a2)+,(a0) dbra d1,2b | clear VRAM with a DMA fill: tile 0 blank, plane B blank, sprites off screen move.w #0x8F01,(a0) | auto-increment 1 for the fill move.l #0x94FF93FF,(a0) | length $FFFF move.w #0x9780,(a0) | fill mode move.l #0x40000080,(a0) | VRAM $0000, DMA move.w #0,(a1) | fill byte, starts the fill 3: move.w (a0),d0 btst #1,d0 | DMA busy bne.s 3b move.w #0x8F02,(a0) | variables and the RAM name table lea VARS,a3 moveq #0,d0 moveq #(40/4)-1,d1 4: move.l d0,(a3)+ dbra d1,4b lea TABLE_RAM,a3 move.w #TABLE_BYTES/4-1,d1 5: move.l d0,(a3)+ dbra d1,5b move.l #(index_end-index)/4,v_frames_total .if AUDIO move.w #AUDIO_PAGE,BANK7 bsr load_z80 bsr z80_request move.b #1,V_GO | the driver primes its ring before it is told to run move.b #0,V_WANT_PAGE move.b #0,V_PAGE_ACK move.b #0,V_RUN bsr z80_release move.w #0x8A00+GO_LINE,(a0) | H-interrupt counter move.w #0x8014,(a0) | H-interrupt on .endif | Interrupts on before anything is queued: a vertical interrupt that fell during | the fill is pending, and the status reads above do not clear the request -- | only an acknowledge does. Taking it now, with nothing to do, is harmless; | taking it later would start frame 0's DMA mid-frame at the slow display rate | and let the *next* blank's request fire straight after the handler returned. move.w #0x2300,sr bsr fetch_frame | frame 0: patches applied, runs pending move.w #0x8174,(a0) | display on main_loop: stop #0x2300 | idle until an interrupt; the bus is the Z80's meanwhile .if AUDIO tst.w v_done beq.s 0f subq.w #1,v_end_timer | two wake-ups (H and V interrupt) per hardware frame bne.s 0f move.w #0,Z80_RESET | hold the Z80 and the YM2612 in reset: silence, no reads past the stream end_loop: stop #0x2300 bra.s end_loop 0: bsr z80_serve_page .endif tst.w v_consumed beq.s main_loop clr.w v_consumed tst.w v_done bne.s main_loop bsr fetch_frame bra.s main_loop | -- fetch the next record: map it, apply its patches, queue its DMA runs ----------- fetch_frame: move.l v_frame,d0 cmp.l v_frames_total,d0 blo.s 1f move.w #1,v_done move.w #END_FRAMES,v_end_timer rts 1: addq.l #1,v_frame lea index,a2 lsl.l #2,d0 move.l (a2,d0.l),d0 | physical ROM address of the record move.l d0,d1 swap d1 | d1.w = address >> 16 lsr.w #PAGE_SHIFT-16,d1 | = page (address >> 19) beq.s 2f | page 0: visible as is move.w d1,BANK1 andi.l #0x7FFFF,d0 ori.l #WINDOW1,d0 2: movea.l d0,a2 move.w (a2)+,d2 | flags move.w (a2)+,d3 | n_patches move.w (a2)+,d4 | n_runs addq.l #2,a2 | n_tiles (unused by the player) clr.l v_cram_ptr btst #0,d2 beq.s 3f move.l a2,v_cram_ptr lea 128(a2),a2 3: lea TABLE_RAM,a3 btst #1,d2 bne.s packed subq.w #1,d3 bmi.s 5f 4: move.w (a2)+,d0 move.w (a2)+,(a3,d0.w) dbra d3,4b bra.s 5f packed: | 140-byte change mask (bit 7 = first cell), then one word per set bit lea 140(a2),a4 moveq #28-1,d5 6: moveq #5-1,d6 7: move.b (a2)+,d0 moveq #8-1,d7 8: add.b d0,d0 bcc.s 9f move.w (a4)+,(a3) 9: addq.l #2,a3 dbra d7,8b dbra d6,7b lea 48(a3),a3 | the row's unused 24 cells dbra d5,6b movea.l a4,a2 5: move.w d4,v_runs_n | count before pointer: the handler tests the pointer move.l a2,v_runs_ptr move.w #1,v_table_ready rts | -- interrupts -------------------------------------------------------------------- vblank: movem.l d0-d4/a0-a2,-(sp) lea VDP_CTRL,a0 tst.w v_phase bne vblank_b | A: new tiles into the ring move.w #1,v_phase move.l v_runs_ptr,d0 beq vblank_end movea.l d0,a2 move.w v_runs_n,d1 subq.w #1,d1 bmi 1f 0: .if CODEBOOK | 8-byte descriptor: u8 page, u8 tiles, u16 VRAM address, u32 source/2 (rrmd/rom.py) moveq #0,d0 move.b (a2)+,d0 move.w d0,BANK2 | the run's source page into window 2 moveq #0,d4 move.b (a2)+,d4 lsl.w #4,d4 | words = tiles * 16 move.w d4,d0 andi.w #0xFF,d0 ori.w #0x9300,d0 move.w d0,(a0) | reg 19 lsr.w #8,d4 ori.w #0x9400,d4 move.w d4,(a0) | reg 20 move.w (a2)+,d3 | VRAM address move.l (a2)+,d4 | source / 2 move.w d4,d0 andi.w #0xFF,d0 ori.w #0x9500,d0 move.w d0,(a0) | reg 21 move.l d4,d0 lsr.l #8,d0 andi.w #0xFF,d0 ori.w #0x9600,d0 move.w d0,(a0) | reg 22 swap d4 andi.w #0x7F,d4 ori.w #0x9700,d4 move.w d4,(a0) | reg 23: 68000 -> VDP move.w d3,d0 andi.w #0x3FFF,d0 ori.w #0x4000,d0 swap d0 move.w d3,d0 rol.w #2,d0 andi.w #3,d0 ori.w #0x80,d0 move.l d0,(a0) | VRAM write + DMA: go .else move.l (a2)+,(a0) | reg 19, 20: length move.l (a2)+,(a0) | reg 21, 22: source low, mid move.w (a2)+,(a0) | reg 23: source high move.l (a2)+,(a0) | VRAM address + DMA: go (68000 held until done) .endif dbra d1,0b 1: clr.l v_runs_ptr bra vblank_end vblank_b: clr.w v_phase tst.l v_runs_ptr | A has not loaded this frame's tiles yet: wait a cycle bne vblank_end tst.w v_table_ready beq vblank_end .if AUDIO | Audio is the clock: show the pending frame only once its samples are playing. | Half a frame of tolerance keeps PAL's exact 2:1 cadence from ever stuttering | on the rounding; on NTSC it is what paces the 2-3-2-3 pulldown. tst.w v_audio_started bne.s 6f bsr z80_request | frame 0 waits for the driver's ring to be primed, move.b V_READY,d0 | so sound and picture start together bsr z80_release tst.b d0 beq 5f bra.s 3f 6: bsr z80_request moveq #0,d0 move.b V_PLAYED+1,d0 | little-endian u16 in Z80 RAM lsl.w #8,d0 move.b V_PLAYED,d0 bsr z80_release move.l v_played,d1 cmp.w d1,d0 bcc.s 4f addi.l #0x10000,d1 | the 16-bit counter wrapped 4: move.w d0,d1 move.l d1,v_played move.l v_due,d0 subi.l #AUDIO_SPF_FP/2,d0 clr.w d0 swap d0 | integer part of (due - half a frame) cmp.l d0,d1 bcs.s 5f | not yet: stay in B, look again next blank 3: move.l v_due,d0 addi.l #AUDIO_SPF_FP,d0 move.l d0,v_due .endif clr.w v_table_ready move.l #0x93009400+(((TABLE_BYTES/2)&0xFF)<<16)+((TABLE_BYTES/2)>>8),(a0) move.l #0x95009600+(((TABLE_RAM>>1)&0xFF)<<16)+((TABLE_RAM>>9)&0xFF),(a0) move.w #0x9700+((TABLE_RAM>>17)&0x7F),(a0) move.l #VRAM_PLANE_A_DMA,(a0) move.l v_cram_ptr,d0 beq.s 2f movea.l d0,a2 lea VDP_DATA,a1 move.l #CRAM_0,(a0) moveq #64-1,d1 1: move.w (a2)+,(a1) dbra d1,1b 2: move.w #1,v_consumed .if AUDIO tst.w v_audio_started bne.s vblank_end move.w #1,v_audio_started | frame 0 is on screen from the next line: sample 0 now bsr z80_request move.b #1,V_RUN bsr z80_release bra.s vblank_end 5: move.w #1,v_phase | still B next time .endif vblank_end: .if AUDIO bsr z80_go | DMA over: the Z80 may read the cartridge again .endif movem.l (sp)+,d0-d4/a0-a2 rte hblank: .if AUDIO | line GO_LINE: the next blank's DMA is ~9 lines away, longer than the driver's | 4-sample group, so no bank read can still be in flight when it starts ori.w #0x0700,sr | the vertical interrupt must not land while the Z80 is held movem.l d0/a0,-(sp) bsr z80_request move.b #0,V_GO bsr z80_release movem.l (sp)+,d0/a0 .endif rte exception: bra.s exception .if AUDIO | -- Z80 --------------------------------------------------------------------------- z80_request: move.w #0x100,Z80_BUSREQ 1: btst #0,Z80_BUSREQ bne.s 1b rts z80_release: move.w #0,Z80_BUSREQ rts z80_go: | Allow bank reads again: one byte, so the Z80 is paused as briefly as possible. | Not while the driver is waiting for a page, though: opening the gate before | z80_serve_page has mapped it would let the Z80 read the page still under | window 7, which is the one it has just finished. The serve opens the gate | itself, so nothing is lost by leaving it shut here. | Every access here is made with the Z80 halted, so this path is kept to one read | and one write. Reading V_WANT_PAGE and V_PAGE_ACK as well - four accesses - is | measurably too long: it lengthens the hold enough that a group occasionally | overruns its Timer A period and the driver loses a lap, four samples at a time. | V_REBANK alone carries all three states. | Branch straight off each test: MOVEQ sets the condition codes, so loading a | value between the test and its branch loses the test. Writing the immediate | at each leaf keeps this to the one read and one write the hold can afford. bsr z80_request move.b V_REBANK,d0 beq.s 3f | 0 = nothing outstanding: open normally subq.b #1,d0 beq.s 1f | 1 = page asked for, not yet mapped: leave the gate shut move.b #2,V_GO | 2 = mapped, the driver still owes itself a bank write bra.s 1f 3: move.b #1,V_GO 1: bra.s z80_release z80_serve_page: | main loop, each wake-up: map the stream page the driver asked for (one read) move.w sr,-(sp) ori.w #0x0700,sr | no interrupt may land while the Z80 is held bsr z80_request moveq #0,d0 move.b V_WANT_PAGE,d0 cmp.w v_page_acked,d0 beq.s 1f move.w d0,v_page_acked addi.w #AUDIO_PAGE,d0 move.w d0,BANK7 move.b V_WANT_PAGE,V_PAGE_ACK move.b #2,V_REBANK | mapped: the driver owes itself a bank-register write move.b #2,V_GO 1: bsr z80_release move.w (sp)+,sr rts load_z80: move.w #0x100,Z80_BUSREQ move.w #0x100,Z80_RESET 1: btst #0,Z80_BUSREQ bne.s 1b lea z80_driver,a1 lea Z80_RAM,a2 move.w #(z80_driver_end-z80_driver)-1,d1 2: move.b (a1)+,(a2)+ dbra d1,2b move.w #0,Z80_RESET | reset pulse: the Z80 and the YM2612 moveq #0x7F,d1 3: dbra d1,3b move.w #0x100,Z80_RESET rts .align 2 z80_driver: .incbin "pcm.bin" z80_driver_end: .endif | -- VDP registers ----------------------------------------------------------------- .align 2 vdp_regs: .word 0x8004, 0x8134, 0x8230, 0x8334, 0x8407, 0x856C, 0x8600, 0x8700 .word 0x8800, 0x8900, 0x8AFF, 0x8B00, 0x8C89, 0x8D37, 0x8E00, 0x8F02 .word 0x9001, 0x9100, 0x9200 vdp_regs_end: | -- data ---------------------------------------------------------------------------- .org 0x1000 index: .incbin "index.bin" index_end: .org 0x10000 data: .incbin "data.bin" .align 2 .if AUDIO .org AUDIO_PAGE*0x80000 audio: .incbin "audio.u8" audio_end: .align 2 .endif rom_end: | last byte + 1; the header's ROM extent and the checksum both need it