-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.asm
executable file
·65 lines (56 loc) · 1.03 KB
/
random.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
; random.asm
;
; Generates random numbers and pieces
.dseg
lfsr: ; Linear feedback shift register
.byte 2 ; Left: low bit, right: high bit
.cseg
; Seed random with initial value
; in a1:a0
rand_seed:
STS2 lfsr,a1,a0
ret
; Return a random number between 0 and 1 using a 16-bit LFSR
; in lfsr
; mod w,a0,a1,lfsr
rand1:
LDS2 a1,a0,lfsr ; lfsr: a0 := low bit | a1 := high bit
ldi w,0x00
EORB w,0,a0,0,a0,2 ; w := b16 ^ b14
EORB w,0, w,0,a0,3 ; w ^= b13
EORB w,0, w,0,a0,5 ; w ^= b11
bst w,0 ; C := T := w(0)
clc
brtc PC+2
sec
ROR2 a1,a0
STS2 lfsr,a1,a0
ret
; Return a 3-bit random number in range 0 thru 7
; in lfsr
; mod w,a0,a1,lfsr
; out b0
rand3:
ldi b0,0x00
rcall rand1
brtc PC+2
ori b0,1<<0
rcall rand1
brtc PC+2
ori b0,1<<1
rcall rand1
brtc PC+2
ori b0,1<<2
ret
; Return a 3-bit random number in range 0 thru a0
; in a0
; mod w,a0,a1,lfsr
; out b0
randi3:
push a0
rcall rand3
pop a0
cp a0,b0
brge PC+2 ; Branch if a0 >= b0
rjmp randi3
ret