aboutsummaryrefslogtreecommitdiff
path: root/rand.s
blob: fa181ccc441eb48f0d3893d9de559a98527d3b78 (plain)
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

 .export _randb, _randi, _randl
 .importzp sreg, tmp3

 .include "atari.inc"

; RANDOM is the POKEY LFSR read address.
; unfortunately, a read from this address will never
; return 0, since LFSR's can't do that. So there's
; extra code in here to (try to) compensate.

; unsigned char __fastcall__ randb(void);
_randb: ; C-callable entry point
 ldx #0
randb:  ; asm-callable (doesn't trash X reg)
 lda RANDOM ; bit 7 of this read ends up as bit 0 of result
 sta tmp3
 nop        ; let the LFSR cook for a bit...
 nop
 lda RTCLOK+2 ; different amount of cooking depending on whether
 and #$01     ; we're on an even or odd numbered TV frame
 bne @1
 nop
 nop
 nop
@1:
 rol tmp3   ; tmp3 bit 7 now in carry
 lda RANDOM
 rol        ; carry now in bit 0 of A
 nop
 nop
 rts

; unsigned int __fastcall__ randi(void);
; NB cc65's rand() returns a positive signed int, meaning
; 0 to 0x7fff.
_randi:
 jsr randb
 and #$7f
 tax
 jsr randb
 rts

; unsigned long __fastcall__ randl(void);
_randl:
 jsr randb
 sta sreg
 jsr randb
 sta sreg+1
 jsr randb
 tax
 jsr randb
 rts