| Boot credits after the first picture, before defaults/main menu. Renderer
| follows tools/intro.s; own text-only tile card at $2D0000, code $2D8000.
| Wait for Start only, indefinitely. Preserve the old timer/RNG word on exit.
| No save writes; screen always returns with the original SR/stack contract.
        .equ    DATA,     0x2D0000       | palette (32 bytes), map (width, height, 40*28 words), tile count, tiles
        .equ    MAP,      DATA+32
        .equ    TILES,    MAP+4+2*40*28

        .ifndef LEGACY_CREDITS
        .equ LEGACY_CREDITS,0
        .endif
        .text
        .globl  _start
_start: jsr     0x2FE280                | keep the existing first picture
        bra.w   intro                   | then this credits card
intro:  move.l  #0x15E4C,0xFFB03A       | as $FED70
        bclr    #1,0xFFBF78             | H40
        move.w  #5,0xFFB00E
        move.w  #0xA000,0xFFB008
        move.w  #7,0xFFB00A
        move.w  #0xC000,0xFFB004
        move.w  #7,0xFFB006
        move.w  #0xF000,0xFFB00C
        move.w  #0xF800,0xFFB002
        move.w  #0xFC00,0xFFB000
        jsr     0x10F32
        movea.l #0xFFBD28,%a0           | palette buffer: black
        moveq   #31,%d1
1:      clr.l   (%a0)+
        dbra    %d1,1b
        jsr     0x115AA                 | VDP registers, VRAM cleared
        move.w  %sr,-(%sp)
        move.w  #0x2700,%sr
        move.l  #0x40200000,0xC00004    | VRAM write $0020 (tile 1)
        lea     TILES,%a0
        move.w  (%a0)+,%d1
        lsl.w   #3,%d1                  | 8 longs per tile
        subq.w  #1,%d1
2:      move.l  (%a0)+,0xC00000
        dbra    %d1,2b
        lea     DATA,%a0                | palette 0 into the game's palette buffer (the fade copies it to CRAM)
        movea.l #0xFFBD28,%a1
        moveq   #15,%d1
5:      move.w  (%a0)+,(%a1)+
        dbra    %d1,5b
        move.w  #0x2700,%sr             | map: plane A ($C000, 128 cells per row), and plane B ($A000) emptied
        lea     MAP+4,%a0
        moveq   #0,%d4                  | row
6:      move.w  %d4,%d2
        lsl.w   #8,%d2                  | row * 256 bytes
        move.w  %d2,%d3
        addi.w  #0xC000,%d2
        bsr.s   vaddr
        moveq   #39,%d1
7:      move.w  (%a0)+,0xC00000
        dbra    %d1,7b
        addi.w  #0xA000,%d3
        move.w  %d3,%d2
        bsr.s   vaddr
        moveq   #63,%d1
8:      move.w  #0,0xC00000
        dbra    %d1,8b
        addq.w  #1,%d4
        cmpi.w  #28,%d4
        blt.s   6b
        move.w  (%sp)+,%sr
        move.w  #0x18,0xFFBD26          | fade in, then wait as the original
        move.w  #0x2500,%sr
        .if LEGACY_CREDITS
        move.w  #44,0xFFD066
3:      moveq   #4,%d0
        jsr     0x11176
        tst.w   %d1
        bne.s   4f
        subq.w  #1,0xFFD066
        bpl.s   3b
4:      move.w  #0x2700,%sr
        rts
        .else
        move.w  0xFFD066,-(%sp)
        move.w  #0x4352,0xFFD066        | active card, reuses former countdown word
3:      moveq   #4,%d0
        jsr     0x11176
        btst    #7,%d1                 | Genesis Start, all polled pads
        beq.s   3b
4:      move.w  #0x2700,%sr
        move.w  (%sp)+,0xFFD066
        rts
        .endif

| vaddr: d2 = VRAM address -> VDP write address set
vaddr:  move.l  %d2,-(%sp)
        andi.l  #0xFFFF,%d2
        move.l  %d2,%d0
        andi.w  #0x3FFF,%d0
        ori.w   #0x4000,%d0
        swap    %d0
        lsr.w   #8,%d2
        lsr.w   #6,%d2                  | address >> 14
        move.w  %d2,%d0
        move.l  %d0,0xC00004
        move.l  (%sp)+,%d2
        rts

