From 96c424fba7d2750326e383a09342691fe4f89dd8 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 8 Dec 2022 23:52:02 -0500 Subject: [PATCH] added random number generator --- atlantico/atlantico.asm | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/atlantico/atlantico.asm b/atlantico/atlantico.asm index 610ef11..1db2e67 100755 --- a/atlantico/atlantico.asm +++ b/atlantico/atlantico.asm @@ -44,6 +44,8 @@ PrevOAMCount: .res 1 ; Store the previous number of bytes that were sent ActorsArray: .res MAX_ACTORS * .sizeof(Actor) +Seed: .res 1 ; Used as the RANDOM NUMBER GENERATOR SEED + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PRG-ROM code located at $8000 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -512,6 +514,31 @@ EndRoutine: rts .endproc + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Returns a random 8 bit number inside A (0-255), clobbers Y (0) +;; requires 1-byte value on the Zero Page called Seed +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; this is an 8 bit galois LFSR with polynomial $1D +;; The sequence of numbers it generates will repeat after 255 calls +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +.proc GetRandomNumber + ldy #8 ; Loop counter (generates 8 bits) + lda Seed ; + +Loop8Times: + asl ; Shift the register left + bcc :+ ; + eor #$1D ; Apply XOR feebback when a 1bit is shifted out + : ; + dey ; + bne Loop8Times ; + + sta Seed ; Saves the value in A + cmp #0 ; + rts ; +.endproc + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Reset handler (called when the NES resets or powers on) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;