From a7f0280aae210b11884a5320889c74568c82aec4 Mon Sep 17 00:00:00 2001 From: Vince Weaver Date: Sun, 31 Oct 2021 18:46:23 -0400 Subject: [PATCH] peasant: add random number generator --- games/peasant/Makefile | 4 +- games/peasant/keyboard.s | 1 + games/peasant/qload.s | 1 + games/peasant/random16.s | 119 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 games/peasant/random16.s diff --git a/games/peasant/Makefile b/games/peasant/Makefile index 69f9b335..1566bce5 100644 --- a/games/peasant/Makefile +++ b/games/peasant/Makefile @@ -74,7 +74,8 @@ qload.o: qload.s qboot.inc \ clear_bottom.s \ hgr_hgr2.s \ gr_offsets.s \ - qkumba_popwr.s + qkumba_popwr.s \ + random16.s ca65 -o qload.o qload.s -l qload.lst ### @@ -136,6 +137,7 @@ qload.inc: generate_common QLOAD ./generate_common -a 0xb00 -s location_names_l qload.lst >> qload.inc ./generate_common -a 0xb00 -s location_names_h qload.lst >> qload.inc ./generate_common -a 0xb00 -s wait_until_keypress qload.lst >> qload.inc + ./generate_common -a 0xb00 -s random16 qload.lst >> qload.inc echo "hposn_high = \$$BA00" >> qload.inc echo "hposn_low = \$$BB00" >> qload.inc echo "driveoff = \$$A22" >> qload.inc diff --git a/games/peasant/keyboard.s b/games/peasant/keyboard.s index 30804d67..53c22a5c 100644 --- a/games/peasant/keyboard.s +++ b/games/peasant/keyboard.s @@ -21,6 +21,7 @@ check_keyboard: rts key_was_pressed: + inc SEEDL and #$5f ; strip off high bit and make uppercase diff --git a/games/peasant/qload.s b/games/peasant/qload.s index f6497a33..2b032522 100644 --- a/games/peasant/qload.s +++ b/games/peasant/qload.s @@ -257,6 +257,7 @@ length_array: .include "gr_offsets.s" .include "loadsave_menu.s" .include "wait_keypress.s" +.include "random16.s" peasant_text: .byte 25,2,"Peasant's Quest",0 diff --git a/games/peasant/random16.s b/games/peasant/random16.s new file mode 100644 index 00000000..44b45a79 --- /dev/null +++ b/games/peasant/random16.s @@ -0,0 +1,119 @@ +; 16-bit 6502 Random Number Generator (cycle-invariant version) + +; Linear feedback shift register PRNG by White Flame +; http://codebase64.org/doku.php?id=base:small_fast_16-bit_prng + +; The Apple II KEYIN routine increments SEEDL:SEEDH +; while waiting for keypress + +;SEEDL = $4E +;SEEDH = $4F + +XOR_MAGIC = $7657 ; "vW" + + ;============================= + ; random16 + ;============================= + ; takes: + ; not 0, cs = 6(r16)+12(lnz)+5(nop)+ 19(deo) = 42 + ; not 0, cc = 6(r16)+14(lnz)+2(nop)+ 20(neo) = 42 + + ; $0000 = 6(r16)+ 6(loz)+11nops+ 19(deo) = 42 + ; $8000 = 6(r16)+ 6(loz)+ 4(ceo) + 6nops+ 20(neo) = 42 + + ; $XX00 cc = 6(r16)+ 6(loz)+4(ceo)+2(cep) +4nops+ 20(neo) = 42 + ; $XX00 cs = 6(r16)+ 6(loz)+4(ceo)+4(cep) +3nops+ 19(deo) = 42* +random16: + + lda SEEDL ; 3 + beq low_zero ; $0000 and $8000 are special values ; 3 + ;========== + ; 6 +lownz: + ; -1 + asl SEEDL ; Do a normal shift ; 5 + lda SEEDH ; 3 + rol ; 2 + bcs five_cycle_do_eor ; 3 + ;=========== + ; 12 + + bcc two_cycle_no_eor ; 3 + ;========== + ; 12+3-1 = 14 + + +;=================================================================== + +eleven_cycle_do_eor: + nop ; 2 + nop ; 2 + nop ; 2 +five_cycle_do_eor: + nop ; 2 +three_cycle_do_eor: + sta SEEDH ; nop ; 3 + +do_eor: + ; high byte is in A + + eor #>XOR_MAGIC ; 2 + sta SEEDH ; 3 + lda SEEDL ; 3 + eor #