From SMS Power!

Development: Alex Kidd in Miracle World - Code Snippets

+Contents

RST Handlers

_RST_8H_:       ; out(BFh),DE
        ld a, e
        out ($BF), a
        ld a, d
        out ($BF), a
        ret
_RST_10H_:      ; ldHL,(HL+2A)
        add a, a
        ld c, a
        ld b, $00
        add hl, bc
        ld a, (hl)
        inc hl
        ld h, (hl)
        ld l, a
        ret
_RST_20H_:      ; jp(HL+2A)
        add a, a
        ld e, a
        ld d, $00
        add hl, de
        ld a, (hl)
        inc hl
        ld h, (hl)
        ld l, a
        jp (hl)
_RST_30H_:      ; writeBbytesFrom(HL)toVRAMat(DE)
        rst $08 ; out(BFh),DE
        ld c, $BE
-:
        outi
        jr nz, -
        ret

Main Loop

-:
        ld hl, gameState
        ld a, (hl)
        and $0F
        exx
        ld hl, jumpTableGameState
        rst $20 ; jp(HL+2A)
        jp -

Update Entities

updateEntities:
        ld hl, $C706
        ld (pointerToSpriteTerminator), hl      ; Clear SAT in RAM
        ld ix, (pointerToEntityData)
        ld a, (lengthEntityData)
        ld b, a

-:
        ld a, (ix+0)    ; ix+0 = type of entity (0 means no entity)
        and $7F
        jp z, ++
        push bc
        ld hl, jumpTableUpdateEntities - 2
        rst $20 ; jp(HL+2A)
        ld a, (ix+0)    ; ix+0 = type of entity (0 means no entity)
        or a
        jp z, +
        call updateEntityHpos
        call updateEntityVpos
        call updateEntitySprites
+:
        pop bc
++:
        ld de, $0020
        add ix, de
        djnz -

        ld hl, (pointerToSpriteTerminator)
        ld a, l
        cp $40
        jr c, +
        ld l, $3F
        ld (pointerToSpriteTerminator), hl
+:
        ld (hl), $D0
        ret

Update Entity Animation

updateEntityAnimation:
        ld d, (hl)      ; Read the number of frames from the animation descriptor
        inc hl
        ld a, (ix+4)    ; ix+4 = Current animation frame number
        dec (ix+5)      ; ix+5 = Animation Timer
        jr nz, +
        ld e, (ix+6)    ; ix+6 = Animation Timer Reset Value
        ld (ix+5), e    ; ix+5 = Animation Timer
        inc a
        cp d
        jr c, +
        xor a
+:
        ld (ix+4), a    ; ix+4 = Current animation frame number
        add a, a
        ld e, a
        ld d, $00
        add hl, de
        ld e, (hl)      ; Read low byte of pointer to sprite descriptor
        inc hl
        ld h, (hl)      ; Read high byte of pointer to sprite descriptor
        ld (ix+7), e    ; ix+7 = low byte of pointer to sprite descriptor
        ld (ix+8), h    ; ix+8 = high byte of pointer to sprite descriptor
        ret

Update Entity Sprites

updateEntitySprites:
        ld a, (ix+0)    ; ix+0 = type of entity (0 means no entity)
        or a
        ret z
        ld a, (ix+9)    ; ix+9 = flag indicating if entity is horizontaly offscreen
        or (ix+10)      ; ix+10 = flag indicating if entity is verticaly offscreen
        jp nz, updateEntitySpriteVerticalyOffscreen
        ld a, (ix+14)   ; ix+14 = vertical position of entity
        cp $C0
        ret nc
        ld c, a
        ld de, (pointerToSpriteTerminator)
        push de
        ld l, (ix+7)    ; ix+7 = low byte of pointer to sprite descriptor
        ld h, (ix+8)    ; ix+8 = high byte of pointer to sprite descriptor
        ld b, (hl)      ; Read number of sprites from sprite descriptor
        push bc
        inc hl
        ld a, (hl)      ; Read collision data table index from sprite descriptor
        ld (ix+19), a   ; ix+19 = index into collision data table
        inc hl

-:
        ld a, c
        add a, (hl)     ; Read vertical position of sprite from sprite descriptor and add it to entitiy's vertical position
        cp $D0
        jr nz, +
        dec a
+:
        ld (de), a      ; Write the calculated vertical position into the SAT in RAM
        inc e
        inc hl
        djnz -

        ld (pointerToSpriteTerminator), de
        pop bc
        pop de
        sla e
        set 7, e
        ld c, (ix+12)   ; ix+12 = horizontal position of entity

-:
        ld a, c
        add a, (hl)     ; Read horizontal position of sprite from sprite descriptor and add it to entitiy's horizontal position
        bit 7, (hl)
        jp z, +
        ccf
+:
        jp nc, +
        ld a, $E0
        res 7, e
        srl e
        ld (de), a      ; Write the calculated horizontal position into the SAT in RAM
        sla e
        set 7, e
        jp ++
+:
        ld (de), a      ; Write the calculated horizontal position into the SAT in RAM
++:
        inc hl
        inc e
        ldi             ; Copy character code from sprite descriptor into SAT in RAM
        inc bc
        djnz -

        ret

Update SAT

updateSAT:
        ld a, (gameState)
        and $0F
        cp $02
        jr c, copySAT           ; If gamestate is less than 2 the titlescreen is shown
        ld hl, spriteFlickerCounter
        inc (hl)
        bit 0, (hl)
        jr z, handleSpriteFlicker
copySAT:
        ld hl, ramSAT
        ld de, $7F00
        ld bc, $40BE
        rst $08 ; out(BFh),DE

-:
        outi
        jr nz, -

        ld hl, ramSAT+$80
        ld de, $7F80
        ld b, $80
        rst $08 ; out(BFh),DE

-:
        outi
        jr nz, -

        ret

Handle Sprite Flicker

handleSpriteFlicker:
        ld a, (pointerToSpriteTerminator)
        cp $13
        jr c, copySAT
        ld hl, ramSAT
        ld bc, $11BE
        ld de, $7F00
        rst $08 ; out(BFh),DE

-:
        outi
        jr nz, -

        ld hl, (pointerToSpriteTerminator)
        ld a, l
        dec l
        sub $11
        ld b, a

-:
        outd
        jr nz, -

        ld a, $D0
        out ($BE), a
        ld hl, ramSAT+$80
        ld de, $7F80
        ld b, $22
        rst $08 ; out(BFh),DE

-:
        outi
        jr nz, -

        ld hl, (pointerToSpriteTerminator)      ; pointerToSpriteTerminator = $C009
        sla l
        set 7, l
        ld a, l
        sub $A2
        ld b, a

-:
        dec l
        dec l
        outi
        outd
        jp nz, -

        ret

Retrieved from //www.smspower.org/Development/AlexKiddInMiracleWorld-SMS-CodeSnippets
Page last modified on Thu Nov 27, 2014 10:25 pm