Skip to content

Instantly share code, notes, and snippets.

@spacekitcat
Created September 1, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacekitcat/fb28607d7c4cee3dd10725fc0f08a0ec to your computer and use it in GitHub Desktop.
Save spacekitcat/fb28607d7c4cee3dd10725fc0f08a0ec to your computer and use it in GitHub Desktop.
6502 machine code snippet for stiching and rendering a 4 part sprite on the 8-bit Nintendo (NES)
.define OAM_TABLE_START $0200
; .zeropage
; param_1: .res 1
; param_2: .res 1
; param_3: .res 1
; temp_var_1: .res 1
; temp_var_2: .res 1
; temp_var_3: .res 1
; Controls the sprite y/x offsets required to stitch
; and render the four sub sprites
sub_sprite_offsets:
.byte $00, $00, $00, $08, $08, $00, $08, $08
; param_1: Sprite OAM position offset. Subroutine expects
; exclusive use of the subsequent 4 OAM slots
; (16 bytes)
;
; param_2: Sprite Y position
; param_3: Sprite X position
.proc DrawMagpie
ldx param_1
; Sprite sheet offset
lda #$08 ; The Magpie sprite starts at sprite sheet 1
sta temp_var_1
; X iterates the OAM
ldx #$00
; Y iterates the sub sprite offset table (sub_sprite_offsets)
ldy #$00
; byte 0,1,2,3 = [y, pattern number, attributes, x]
LOAD_SUB_SPRITES:
; Y
lda sub_sprite_offsets, Y
adc param_2
sta OAM_TABLE_START, X
iny
inx
; Pattern
lda temp_var_1
sta OAM_TABLE_START, X
inc temp_var_1
inx
; Attr
lda #%00010000
sta OAM_TABLE_START, X
inx
; X
lda sub_sprite_offsets, Y
adc param_3
sta OAM_TABLE_START, X
iny
inx
tya
cmp #$08
bcc LOAD_SUB_SPRITES
rts
.endproc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment