aboutsummaryrefslogtreecommitdiff
path: root/rand.s
diff options
context:
space:
mode:
authorB. Watson <yalhcru@gmail.com>2016-01-22 15:39:14 -0500
committerB. Watson <yalhcru@gmail.com>2016-01-22 15:39:14 -0500
commit06536b454feb4a1db2548fd36c660ee1721a4a4d (patch)
treeead69291ba9288fe33155171f5bb677fdff43438 /rand.s
parent9d032c9a1c0eca663c6877f5421957f50cb1eb79 (diff)
downloadtaipan-06536b454feb4a1db2548fd36c660ee1721a4a4d.tar.gz
use cc65 rand(), see if random events occur closer to how they do in the apple port
Diffstat (limited to 'rand.s')
-rw-r--r--rand.s105
1 files changed, 63 insertions, 42 deletions
diff --git a/rand.s b/rand.s
index af4b693..f2e2449 100644
--- a/rand.s
+++ b/rand.s
@@ -1,17 +1,38 @@
- .export _randb, _randi, _randl
+ .export _randl
+ .import _rand
+; .export _randb, _randi, _randl
; .export _rand1in5
; .export _randbit
.importzp sreg, tmp3
.include "atari.inc"
-; RANDOM is the POKEY LFSR read address. This appears to be the low 8
-; bits of a 17-bit LFSR (Atari calls it a poly counter). Unfortunately,
-; a read from this address will never return 0, since LFSR's can't do
-; that. If Atari had made the RANDOM register read the higher order bits
-; of the LFSR, rather than the bottom 8, this wouldn't be a problem... but
-; they didn't, so there's extra code in here to (try to) compensate.
+; unsigned long __fastcall__ randl(void);
+; this returns the full range of an unsigned long, 0 to 2**32-1
+_randl:
+ jsr _rand
+ sta sreg
+ jsr _rand
+ sta sreg+1
+ jsr _rand
+ sta tmp3
+ jsr _rand
+ ldx tmp3
+ rts
+
+ ;;; rest of file is commented out
+
+; RANDOM is the POKEY LFSR read address. According to the POKEY data
+; sheet, this is the high 8 bits bits of a 17-bit LFSR (Atari calls it
+; a poly counter). Unfortunately, a read from this address never seems
+; to return 0, which confuses me: an LFSR can never return 0, but since
+; we're only reading 8 bits of it, we should be able to get a 0 (some
+; of the other 9 bits would still be 1).
+
+; After some crude statistical analysis, I've decided to go with cc65's
+; rand() implementation. It seems to return more evenly distributed
+; results.
; Might use this at some point:
;_randbit:
@@ -45,46 +66,46 @@
; 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
+;;_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
+;;_randi:
+;; jsr randb
+;; and #$7f
+;; tax
+;; jsr randb
+;; rts
; unsigned long __fastcall__ randl(void);
; this returns the full range of an unsigned long, 0 to 2**32-1
-_randl:
- jsr randb
- sta sreg
- jsr randb
- sta sreg+1
- jsr randb
- tax
- jsr randb
- rts
+;;_randl:
+;; jsr randb
+;; sta sreg
+;; jsr randb
+;; sta sreg+1
+;; jsr randb
+;; tax
+;; jsr randb
+;; rts