Skip to content

Instantly share code, notes, and snippets.

@markusbkk
Forked from mkooi/misc_math.asm
Created July 3, 2024 12:28
Show Gist options
  • Select an option

  • Save markusbkk/8234e3d12ec01b723467e945f0b003f7 to your computer and use it in GitHub Desktop.

Select an option

Save markusbkk/8234e3d12ec01b723467e945f0b003f7 to your computer and use it in GitHub Desktop.
Math macros for the SNES (Ricoh 5A22) in 65C816 assembly.
;===============================================================================
; Multiplication
;===============================================================================
; Although the SNES has a hardware multiplier, multiplication by small
; constants can be implemented faster using the shift-and-add method.
;-------------------------------------------------------------------------------
;===============================================================================
; MUL3 2Val+Val
;===============================================================================
; 6 bytes, 10/12 cycles
;-------------------------------------------------------------------------------
.MACRO MUL3 ARGS Temp
sta Temp
asl
clc
adc Temp
.ENDM
;===============================================================================
; MUL5 4Val+Val
;===============================================================================
; 7 bytes, 12/14 cycles
;-------------------------------------------------------------------------------
.MACRO MUL5 ARGS Temp
sta Temp
asl
asl
clc
adc Temp
.ENDM
;===============================================================================
; MUL7 8VAL-Val
;===============================================================================
; 8 bytes, 14/16 cycles
;-------------------------------------------------------------------------------
.MACRO MUL7 ARGS Temp
sta Temp
asl
asl
asl
sec
sbc Temp
.ENDM
;===============================================================================
; MUL9 8VAL+Val
;===============================================================================
; 8 bytes, 14/16 cycles
;-------------------------------------------------------------------------------
.MACRO MUL9 ARGS Temp
sta Temp
asl
asl
asl
clc
adc Temp
.ENDM
;===============================================================================
; MUL10 2(4Val+Val)
;===============================================================================
; 8 bytes, 14/16 cycles
;-------------------------------------------------------------------------------
.MACRO MUL10 ARGS Temp
sta Temp
asl
asl
clc
adc Temp
asl
.ENDM
;===============================================================================
; 8-bit Division
;===============================================================================
; The hardware divider on the SNES divides a 16-bit unsigned integer by an
; 8-bit unsigned integer, at the cost of 16 extra cycles. It is faster to use
; reciprocal multiplication with the Mode 7 Matrix registers for dividing
; 8-bit by 8-bit unsigned integers.
;-------------------------------------------------------------------------------
; May only be called in Modes 0-6, or Mode 7 during VBlank.
; 12 bytes, 16 cycles each
;-------------------------------------------------------------------------------
.MACRO DIV3_b
ldx #$5556.w
stx M7A
sta M7B
lda MPYH
.ENDM
.MACRO DIV5_b
ldx #$3334.w
stx M7A
sta M7B
lda MPYH
.ENDM
.MACRO DIV7_b
ldx #$2493.w
stx M7A
sta M7B
lda MPYH
.ENDM
.MACRO DIV10_b
ldx #$199A.w
stx M7A
sta M7B
lda MPYH
.ENDM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment