Skip to content

Instantly share code, notes, and snippets.

@Atari2
Last active June 4, 2022 18:41
Show Gist options
  • Save Atari2/708b74c03a1fef9c2c5ed5ba474c7435 to your computer and use it in GitHub Desktop.
Save Atari2/708b74c03a1fef9c2c5ed5ba474c7435 to your computer and use it in GitHub Desktop.
PCG PoC for the SNES using XSH-RR with 16-bit state and 8-bit output.
!state_const = $5621
!state = $0123 ; some freeram
!multiplier = $12 ; keep this 1 byte only
!increment = $02AB
lorom
org $008000
baseaddr:
lda #$80
sta $2100
clc : xce
rep #$38
lda #$0000
tcd
lda #$01ff
tcs
sep #$30
jsr init_state ; call init_state once at startup
jsr gen_random
jml baseaddr
rotr8:
; A = $00 >> $01;
{
lda $00
ldx $01
dex
.shloop
lsr
dex
bpl .shloop
}
sta $02
; A = $00 << (-$01 & 7)
{
lda $01
eor #$ff : inc
and #$07 : dec
tax
lda $00
.shloop2
asl
dex
bpl .shloop2
}
ora $02
rts
inc_state:
{
; x * multiplier
lda !state
sta $211b
lda !state+1
sta $211b
lda #!multiplier
sta $211c
lda $2135
xba
lda $2134
}
rep #$20
clc : adc #!increment ; + increment
; leave in 16-bit mode, the code after this wants it anyway
rts
init_state:
rep #$20
lda #!state_const
sta !state
sep #$20
rts
gen_random:
rep #$20
lda !state
sta $02 ; x in $02-$03
lsr #11
sep #$20
sta $04 ; count in $04
jsr inc_state
lda $02
lsr #6
eor $02
lsr #3
sta $02
sep #$20
lda $04
sta $01
lda $02
sta $00
jsr rotr8
rts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment