|
| 1 | + org 32768 |
| 2 | +.core.__START_PROGRAM: |
| 3 | + di |
| 4 | + push ix |
| 5 | + push iy |
| 6 | + exx |
| 7 | + push hl |
| 8 | + exx |
| 9 | + ld (.core.__CALL_BACK__), sp |
| 10 | + ei |
| 11 | + jp .core.__MAIN_PROGRAM__ |
| 12 | +.core.__CALL_BACK__: |
| 13 | + DEFW 0 |
| 14 | +.core.ZXBASIC_USER_DATA: |
| 15 | + ; Defines USER DATA Length in bytes |
| 16 | +.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA |
| 17 | + .core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN |
| 18 | + .core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA |
| 19 | +.core.ZXBASIC_USER_DATA_END: |
| 20 | +.core.__MAIN_PROGRAM__: |
| 21 | + call .core.RND |
| 22 | + ld hl, 0 |
| 23 | + ld b, h |
| 24 | + ld c, l |
| 25 | +.core.__END_PROGRAM: |
| 26 | + di |
| 27 | + ld hl, (.core.__CALL_BACK__) |
| 28 | + ld sp, hl |
| 29 | + exx |
| 30 | + pop hl |
| 31 | + pop iy |
| 32 | + pop ix |
| 33 | + exx |
| 34 | + ei |
| 35 | + ret |
| 36 | + ;; --- end of user code --- |
| 37 | +#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/random.asm" |
| 38 | + ; RANDOM functions |
| 39 | + push namespace core |
| 40 | +RANDOMIZE: |
| 41 | + ; Randomize with 32 bit seed in DE HL |
| 42 | + ; if SEED = 0, calls ROM to take frames as seed |
| 43 | + PROC |
| 44 | + LOCAL TAKE_FRAMES |
| 45 | + LOCAL FRAMES |
| 46 | + ld a, h |
| 47 | + or l |
| 48 | + or d |
| 49 | + or e |
| 50 | + jr z, TAKE_FRAMES |
| 51 | + ld (RANDOM_SEED_LOW), hl |
| 52 | + ld (RANDOM_SEED_HIGH), de |
| 53 | + ret |
| 54 | +TAKE_FRAMES: |
| 55 | + ; Takes the seed from frames |
| 56 | + ld hl, (FRAMES) |
| 57 | + ld (RANDOM_SEED_LOW), hl |
| 58 | + ld hl, (FRAMES + 2) |
| 59 | + ld (RANDOM_SEED_HIGH), hl |
| 60 | + ret |
| 61 | + FRAMES EQU 23672 |
| 62 | + ENDP |
| 63 | + RANDOM_SEED_HIGH EQU RAND+1 ; RANDOM seed, 16 higher bits |
| 64 | + RANDOM_SEED_LOW EQU 23670 ; RANDOM seed, 16 lower bits |
| 65 | +RAND: |
| 66 | + PROC |
| 67 | + ld de,0C0DEh ; yw -> zt |
| 68 | + ld hl,(RANDOM_SEED_LOW) ; xz -> yw |
| 69 | + ld (RANDOM_SEED_LOW),de ; x = y, z = w |
| 70 | + ld a,e ; w = w ^ ( w << 3 ) |
| 71 | + add a,a |
| 72 | + add a,a |
| 73 | + add a,a |
| 74 | + xor e |
| 75 | + ld e,a |
| 76 | + ld a,h ; t = x ^ (x << 1) |
| 77 | + add a,a |
| 78 | + xor h |
| 79 | + ld d,a |
| 80 | + rra ; t = t ^ (t >> 1) ^ w |
| 81 | + xor d |
| 82 | + xor e |
| 83 | + ld d,l ; y = z |
| 84 | + ld e,a ; w = t |
| 85 | + ld (RANDOM_SEED_HIGH),de |
| 86 | + ret |
| 87 | + ENDP |
| 88 | +RND: |
| 89 | + ; Returns a FLOATING point integer |
| 90 | + ; using RAND as a mantissa |
| 91 | + PROC |
| 92 | + LOCAL RND_LOOP |
| 93 | + call RAND |
| 94 | + ; BC = HL since ZX BASIC uses ED CB A registers for FP |
| 95 | + ld b, h |
| 96 | + ld c, l |
| 97 | + ld a, e |
| 98 | + or d |
| 99 | + or c |
| 100 | + or b |
| 101 | + ret z ; Returns 0 if BC=DE=0 |
| 102 | + ; We already have a random 32 bit mantissa in ED CB |
| 103 | + ; From 0001h to FFFFh |
| 104 | + ld l, 81h ; Exponent |
| 105 | + ; At this point we have [0 .. 1) FP number; |
| 106 | + ; Now we must shift mantissa left until highest bit goes into carry |
| 107 | + ld a, e ; Use A register for rotating E faster (using RLA instead of RL E) |
| 108 | +RND_LOOP: |
| 109 | + dec l |
| 110 | + sla b |
| 111 | + rl c |
| 112 | + rl d |
| 113 | + rla |
| 114 | + jp nc, RND_LOOP |
| 115 | + ; Now undo last mantissa left-shift once |
| 116 | + ccf ; Clears carry to insert a 0 bit back into mantissa -> positive FP number |
| 117 | + rra |
| 118 | + rr d |
| 119 | + rr c |
| 120 | + rr b |
| 121 | + ld e, a ; E must have the highest byte |
| 122 | + ld a, l ; exponent in A |
| 123 | + ret |
| 124 | + ENDP |
| 125 | + pop namespace |
| 126 | +#line 18 "arch/zx48k/opt2_rnd_opt.bas" |
| 127 | + END |
0 commit comments