Skip to content

Instantly share code, notes, and snippets.

@BlockoS
Created November 16, 2010 22:05
Show Gist options
  • Save BlockoS/702610 to your computer and use it in GitHub Desktop.
Save BlockoS/702610 to your computer and use it in GitHub Desktop.
Implementation of a 32-bit KISS generator which uses no multiply instructions (on pcengine [not tested])
; http://www.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
; Implementation of a 32-bit KISS generator which uses no multiply instructions
.zp
_tmp .ds 4
_x .ds 4
_y .ds 4
_z .ds 4
_w .ds 4
_n .ds 4
_c .ds 1
.code
JKISS32_X = 123456789
JKISS32_X0 = 21
JKISS32_X1 = 205
JKISS32_X2 = 91
JKISS32_X3 = 7
JKISS32_Y = 234567891
JKISS32_Y0 = 211
JKISS32_Y1 = 56
JKISS32_Y2 = 251
JKISS32_Y3 = 13
JKISS32_Z = 345678912
JKISS32_Z0 = 64
JKISS32_Z1 = 164
JKISS32_Z2 = 154
JKISS32_Z3 = 20
JKISS32_W = 456789123
JKISS32_W0 = 131
JKISS32_W1 = 12
JKISS32_W2 = 58
JKISS32_W3 = 27
JKISS32_C = 0
JKISS32_M = 2147483647
JKISS32_M0 = 255
JKISS32_M1 = 255
JKISS32_M2 = 255
JKISS32_M3 = 127
; 32 bit JKISS
jkiss32:
; _tmp = _y
lda <_y
sta <_tmp
lda <_y+1
sta <_tmp+1
lda <_y+2
sta <_tmp+2
lda <_y+3
sta <_tmp+3
; _tmp = _tmp << 5
asl <_tmp
rol <_tmp+1
rol <_tmp+2
rol <_tmp+3
asl <_tmp
rol <_tmp+1
rol <_tmp+2
rol <_tmp+3
asl <_tmp
rol <_tmp+1
rol <_tmp+2
rol <_tmp+3
asl <_tmp
rol <_tmp+1
rol <_tmp+2
rol <_tmp+3
asl <_tmp
rol <_tmp+1
rol <_tmp+2
rol <_tmp+3
; _tmp ^= _y
lda <_tmp
eor <_y
sta <_tmp
lda <_tmp+1
eor <_y+1
sta <_tmp+1
lda <_tmp+2
eor <_y+2
sta <_tmp+2
lda <_tmp+3
eor <_y+3
sta <_tmp+3
; _y = _tmp
lda <_tmp
sta <_y
lda <_tmp+1
sta <_y+1
lda <_tmp+2
sta <_y+2
lda <_tmp+3
sta <_y+3
; _tmp = _tmp >> 7
asl <_tmp
rol <_tmp+1
rol <_tmp+2
rol <_tmp+3
stz <_tmp
rol <_tmp
; _y ^= _tmp
lda <_y
eor <_tmp+1
sta <_y
lda <_y+1
eor <_tmp+2
sta <_y+1
lda <_y+2
eor <_tmp+3
sta <_y+2
lda <_y+3
eor <_tmp
sta <_y+3
; _y = _tmp
lda <_y+2
sta <_tmp
lda <_y+3
sta <_tmp+1
; _tmp = _tmp << 22
cla
lsr <_tmp+1
ror <_tmp
ror A
lsr <_tmp+1
ror <_tmp
ror A
; _y ^= _tmp
eor <_y+3
sta <_y+3
lda <_y+2
eor <_tmp+1
sta <_y+2
; _tmp = _z + _w
clc
lda <_z
adc <_w
sta <_tmp
lda <_z+1
adc <_w+1
sta <_tmp+1
lda <_z+2
adc <_w+2
sta <_tmp+2
lda <_z+3
adc <_w+3
sta <_tmp+3
; _tmp = _tmp + _c
clc
lda <_tmp
adc <_c
sta <_tmp
lda <_tmp+1
adc #0
sta <_tmp+1
lda <_tmp+2
adc #0
sta <_tmp+2
lda <_tmp+3
adc #0
sta <_tmp+3
; _c = _tmp < 0
stz <_c
cmp #$80
rol <_c
; _z = _w
lda <_w
sta <_z
lda <_w+1
sta <_z+1
lda <_w+2
sta <_z+2
lda <_w+3
sta <_z+3
; _w = _tmp & 2147483647
lda <_tmp+3
and #JKISS32_M3
sta <_w
lda <_tmp+2
sta <_w+2
lda <_tmp+1
sta <_w+1
lda <_tmp
sta <_w
; _x += 2147483647;
clc
lda <_x
adc #JKISS32_M0
sta <_x
lda <_x+1
adc #JKISS32_M1
sta <_x+1
lda <_x+2
adc #JKISS32_M2
sta <_x+2
lda <_x+3
adc #JKISS32_M3
sta <_x+3
; _n = _x + _y
clc
lda <_x
adc <_y
sta <_n
lda <_x+1
adc <_y+1
sta <_n+1
lda <_x+2
adc <_y+2
sta <_n+2
lda <_x+3
adc <_y+3
sta <_n+3
; _n += _w;
clc
lda <_n
adc <_w
sta <_n
lda <_n+1
adc <_w+1
sta <_n+1
lda <_n+2
adc <_w+2
sta <_n+2
lda <_n+3
adc <_w+3
sta <_n+3
rts
; http://www.6502asm.com/ version :)
lda #2
sta $1
lda #0
sta $0
lda #0
sta $20
sta $21
lda #21
sta $2
lda #205
sta $3
lda #91
sta $4
lda #7
sta $5
lda #211
sta $6
lda #56
sta $7
lda #251
sta $8
lda #13
sta $9
lda #64
sta $a
lda #164
sta $b
lda #154
sta $c
lda #20
sta $d
lda #131
sta $e
lda #12
sta $f
lda #58
sta $10
lda #27
sta $11
lda #0
sta $16
; 32 bit JKISS
jkiss32:
; _tmp = _y
lda $6
sta $12
lda $7
sta $13
lda $8
sta $14
lda $9
sta $9
; _tmp = _tmp << 5
asl $12
rol $13
rol $14
rol $15
asl $12
rol $13
rol $14
rol $15
asl $12
rol $13
rol $14
rol $15
asl $12
rol $13
rol $14
rol $15
asl $12
rol $13
rol $14
rol $15
; _tmp ^= _y
lda $12
eor $6
sta $12
lda $13
eor $7
sta $13
lda $14
eor $8
sta $14
lda $15
eor $9
sta $15
; _y = _tmp
lda $12
sta $6
lda $13
sta $7
lda $14
sta $8
lda $15
sta $9
; _tmp = _tmp >> 7
asl $12
rol $13
rol $14
rol $15
lda #0
sta $12
rol $12
; _y ^= _tmp
lda $6
eor $13
sta $6
lda $7
eor $14
sta $7
lda $8
eor $15
sta $8
lda $9
eor $12
sta $9
; _y = _tmp
lda $8
sta $12
lda $9
sta $13
; _tmp = _tmp << 22
lda #0
lsr $13
ror $12
ror A
lsr $13
ror $12
ror A
; _y ^= _tmp
eor $9
sta $9
lda $8
eor $13
sta $8
; _tmp = _z + _w
clc
lda $a
adc $e
sta $12
lda $b
adc $f
sta $13
lda $c
adc $10
sta $14
lda $d
adc $11
sta $15
; _tmp = _tmp + _c
clc
lda $12
adc $16
sta $12
lda $13
adc #0
sta $13
lda $14
adc #0
sta $14
lda $15
adc #0
sta $15
; _c = _tmp < 0
cmp #$80
lda #0
rol A
sta $16
; _z = _w
lda $e
sta $a
lda $f
sta $b
lda $10
sta $c
lda $11
sta $d
; _w = _tmp & 2147483647
lda $15
and #127
sta $e
lda $14
sta $10
lda $13
sta $f
lda $12
sta $e
; _x += 2147483647;
clc
lda $2
adc #255
sta $2
lda $3
adc #255
sta $3
lda $4
adc #255
sta $4
lda $5
adc #127
sta $5
; _n = _x + _y
clc
lda $2
adc $6
sta $12
lda $3
adc $7
sta $13
lda $4
adc $8
sta $14
lda $5
adc $9
sta $15
; _n += _w;
clc
lda $12
adc $e
sta $12
lda $13
adc $f
sta $13
lda $14
adc $10
sta $14
lda $15
adc $11
sta $15
; display test
lda $12
eor $13
eor $14
eor $15
cmp #128
lda #0
bcs skip
lda #1
skip:
ldy #0
sta ($0),Y
inc $0
bne end
inc $1
lda $1
cmp #$6
bne end
lda #0
sta $0
lda #2
sta $1
end:
jmp jkiss32
rts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment