Skip to content

Instantly share code, notes, and snippets.

@pjgpetecodes
Last active October 11, 2026 20:14
Show Gist options
  • Select an option

  • Save pjgpetecodes/90d106c4843961d0194f9801a41738d0 to your computer and use it in GitHub Desktop.

Select an option

Save pjgpetecodes/90d106c4843961d0194f9801a41738d0 to your computer and use it in GitHub Desktop.
SAM Coupe graphics blog4\6-2.asm
;
; -----------------------------------------------------------------------------------
; | Program: SAM Coupe Sprites Tutorial 6 - Part 2: An External Sprite File |
; | Filename: blog4\6-2.asm |
; | Based on: Tutorial 6 part 1 (blog4\6-1.asm) |
; -----------------------------------------------------------------------------------
;
; Part 1 stored both frames inside the program. Here the frames live in the external
; file blog4\dizzy_system_interleaved.asm, which is generated by the Sprite Editor.
; The code is the same as Part 1, apart from the frame labels. We still alternate
; between two frames; the file holds nine, ready for the next part.
; The animation runs forever; reset the machine to stop it.
;
; ***********************************************************************************
; * Program Setup *
; ***********************************************************************************
;
ORG 0x8000
DUMP 1,0
AUTOEXEC
;
LMPR: EQU 250 ; Lower Memory Page Register
VMPR: EQU 252 ; Video Memory Page Register
JCLSBL: EQU 334 ; ROM clear-screen routine
;
Frame_Delay: EQU 5 ; Hold each frame for five 50 Hz interrupts
;
; ***********************************************************************************
; * Application Start *
; ***********************************************************************************
;
Start: DI ; Disable interrupts
LD (System_SP+1),SP; Save BASIC's stack pointer before paging screen RAM
CALL Clear_Screen ; Clear the screen while the ROM is paged in
DI ; Disable interrupts before paging screen RAM
LD SP,0xC000 ; Use a safe stack outside the screen memory
IN A,(VMPR) ; Read the current screen page
AND 0b00011110 ; Keep only the page number
OR 0b00100000 ; Map RAM into the lower memory area
LD (Screen_Page),A ; Save the page value for restoring the screen
;
Animation_Loop: LD A,(Screen_Page)
OUT (LMPR),A ; Page the screen RAM into the lower memory area
;
CALL Clear_Sprite ; Remove the previous frame
LD HL,0 ; Top-left screen byte, as in Tutorial 5
LD DE,(Current_Frame_Data) ; Point to the current interleaved frame
CALL Draw_Sprite ; Draw Dizzy using the mask/pixel data
;
LD A,0x1F ; Page ROM 0 back into the lower memory area
OUT (LMPR),A ; Restore the BASIC ROM mapping
EI ; The ROM interrupt handler is paged in, so allow interrupts
;
LD B,Frame_Delay ; Hold this frame for a few 50 Hz interrupts
Wait_Frame: HALT ; Wait for the next interrupt
DJNZ Wait_Frame ; Keep waiting until the delay expires
;
DI ; Disable interrupts before paging screen RAM again
LD HL,(Current_Frame_Data) ; Address of the frame that was just drawn
LD DE,Dizzy_System_Interleaved_Image1 ; Address of frame 1, to compare against
OR A ; Clear the carry flag so SBC is a plain subtraction
SBC HL,DE ; Is frame 1 on screen?
LD HL,Dizzy_System_Interleaved_Image2 ; Load frame 2's address (LD keeps the flags)
JR Z,Store_Frame ; Yes, so show frame 2 next
LD HL,Dizzy_System_Interleaved_Image1 ; No, so go back to frame 1
Store_Frame: LD (Current_Frame_Data),HL
JR Animation_Loop ; Draw the next frame
;
Exit: DI ; Not reached while the loop runs, kept from 5.asm
System_SP: LD SP,0 ; Restore BASIC's original stack pointer
EI ; Enable interrupts
RET ; Return to BASIC
;
Clear_Screen: XOR A ; Clear the A register to clear the full screen
CALL JCLSBL ; Call the system clear-screen routine
DI ; Disable interrupts before continuing
RET ; Return to the caller
;
; ***********************************************************************************
; * Clear the Sprite's Screen Rectangle *
; ***********************************************************************************
; A masked draw only changes the pixels the sprite owns, so pixels from the previous
; frame would be left behind. On a blank screen we can simply clear the rectangle.
;
Clear_Sprite: LD HL,0 ; Top-left screen byte
LD C,32 ; Clear all 32 sprite rows
;
Clear_Row: LD B,16 ; 16 screen bytes per row
;
Clear_Byte: LD (HL),0 ; Colour zero is the blank background
INC HL ; Advance to the next screen byte
DJNZ Clear_Byte ; Continue across the 16-byte row
LD DE,112 ; Skip the rest of the 128-byte screen row
ADD HL,DE ; Point to the next screen row
DEC C ; Count down the rows
JR NZ,Clear_Row ; Continue until all 32 rows are cleared
RET ; Return after clearing the rectangle
;
; ***********************************************************************************
; * Draw Masked Sprite *
; ***********************************************************************************
;
Draw_Sprite: LD C,32 ; Process all 32 sprite rows
;
Sprite_Row: PUSH BC ; Preserve row count while B counts bytes
LD B,16 ; Process the 16 sprite bytes in this row
;
Sprite_Byte: LD A,(DE) ; Read the mask byte
AND (HL) ; Keep screen bits selected by the mask
LD C,A ; Save the masked background byte
INC DE ; Point to the corresponding pixel byte
LD A,(DE) ; Read the cleaned sprite pixels
OR C ; Combine sprite pixels with the background
LD (HL),A ; Write the combined byte to the screen
INC DE ; Point to the next mask/pixel pair
INC HL ; Advance to the next screen byte
DJNZ Sprite_Byte ; Continue across the 16-byte sprite row
LD BC,112 ; Skip the rest of the 128-byte screen row
ADD HL,BC ; Point to the next screen row
POP BC ; Restore the sprite row counter
DEC C ; Count down the sprite rows
JR NZ,Sprite_Row ; Continue until all 32 rows are drawn
RET ; Return after drawing the sprite
;
Current_Frame_Data: DEFW Dizzy_System_Interleaved_Image1 ; Frame currently on screen
Screen_Page: DEFB 0 ; Screen page used while drawing
;
; ***********************************************************************************
; * Interleaved Sprite Data *
; ***********************************************************************************
; The Sprite Editor output holds nine frames, each with 16 mask/pixel pairs per row.
;
INCLUDE "dizzy_system_interleaved.asm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment