diff --git a/STATUS.md b/STATUS.md index 4597818f..4868d425 100644 --- a/STATUS.md +++ b/STATUS.md @@ -237,6 +237,7 @@ Floating point mathematical routines - not official, but well known and broadly | `$BFED` | `exp_FAC1` | NOT DONE | | | `$E043` | `poly1_FAC1` | NOT DONE | | | `$E059` | `poly2_FAC1` | NOT DONE | | +| `$E0BE` | `rnd_generate` | DONE | | | `$E264` | `cos_FAC1` | NOT DONE | | | `$E26B` | `sin_FAC1` | NOT DONE | | | `$E2B4` | `tan_FAC1` | NOT DONE | | diff --git a/c64/aliases/,aliases_ram_lowmem.s b/c64/aliases/,aliases_ram_lowmem.s index fa426b09..f6f9e3ac 100644 --- a/c64/aliases/,aliases_ram_lowmem.s +++ b/c64/aliases/,aliases_ram_lowmem.s @@ -76,7 +76,7 @@ .label FBUFPT = $71 // $71-$72 -- NOT IMPLEMENTED -- .label CHRGET = $73 // $73-$8A -- NOT IMPLEMENTED -- .label TXTPTR = $7A // $7A-$7B current BASIC statement pointer - .label RNDX = $8B // $8B-$8F -- NOT IMPLEMENTED -- + .label RNDX = $8B // $8B-$8F random number seed // // Page 0 - Kernal area ($90-$FF) diff --git a/c64/basic/math/e0be.interop b/c64/basic/math/e0be.interop new file mode 100644 index 00000000..5c452b05 --- /dev/null +++ b/c64/basic/math/e0be.interop @@ -0,0 +1,5 @@ +This is an important floating point math routine, see: + +- https://www.atarimagazines.com/compute/issue72/random_numbers.php + +XXX - try to find real-life example diff --git a/c64/basic/math/e0be.rnd_generate.s b/c64/basic/math/e0be.rnd_generate.s new file mode 100644 index 00000000..2bc257f7 --- /dev/null +++ b/c64/basic/math/e0be.rnd_generate.s @@ -0,0 +1,63 @@ +// #LAYOUT# STD * #TAKE +// #LAYOUT# * BASIC_0 #TAKE +// #LAYOUT# * * #IGNORE + +// +// A fast random generator, idea from: +// - https://codebase64.org/doku.php?id=base:32bit_galois_lfsr +// +// Output: +// - FAC1 +// +// See also: +// - https://www.atarimagazines.com/compute/issue72/random_numbers.php +// + +rnd_generate: + + // Generate new 4-byte random number + + asl RNDX+1 + rol RNDX+2 + rol RNDX+3 + rol RNDX+4 + + bcs rnd_generate_copy + + lda RNDX+1 + eor #$B7 + sta RNDX+1 + lda RNDX+2 + eor #$1D + sta RNDX+2 + lda RNDX+3 + eor #$C1 + sta RNDX+3 + lda RNDX+4 + eor #$04 + sta RNDX+4 + + // FALLTROUGH + +rnd_generate_copy: + + // Copy the number to FAC1, order changed on purpose + + lda RNDX+3 + sta FAC1_mantissa+0 + lda RNDX+1 + sta FAC1_mantissa+1 + lda RNDX+2 + sta FAC1_mantissa+2 + lda RNDX+4 + sta FAC1_mantissa+3 + + lda #$00 + sta FACOV + sta FAC1_sign + + lda #$80 + sta FAC1_exponent + sta RNDX+0 // in case someone tries to use the float directly + + jmp normal_FAC1