0 Members and 3 Guests are viewing this topic.
.nolist#define TASM#define bcall(xxxx) rst 28h \ .dw xxxx#define equ .equ.listshape=4rand=2update=1 .org 9D93h .db $BB,$6D bcall(4BD0h) ;_GrBufClr bcall(486Ah) ;_GrBufCpy bcall(4570h) ;_RunIndicOff di ld a,$FD out (1),a ld hl,0ChaosGame_SierpinskisGasket: in a,(4) ;test ON press and 8 ret z in a,(1) ;test Clear press and 64 ret z ld (coords),hl call Plot ld hl,(coords) srl h srl l push hl#IF update==256 ld a,(count) inc a ld (count),a jr nz,$+5 bcall(486Ah) ;_GrBufCpy#ENDIF call Random#IF shape==3 call L_mod_3 pop hl jr z,ChaosGame_SierpinskisGasket dec a ld a,32 jr nz,$+7 add a,l ld l,a jp ChaosGame_SierpinskisGasket add a,h ld h,a jp ChaosGame_SierpinskisGasket#ENDIF#IF shape==4 and 3 pop hl jr z,ChaosGame_SierpinskisGasket rrca ld b,a ld a,32 jr nc,$+6 add a,l ld l,a ld a,32 rrc b jp nc,ChaosGame_SierpinskisGasket add a,h ld h,a jp ChaosGame_SierpinskisGasket#ENDIF#IF rand==0; From CaDan, by Iambian and GeekboyRandom:LFSR: ld hl,lfsrseed1 ld a,(hl) rrca ld (hl),a jp nc,+_ xor %00111000 ld (hl),a_: ld l,a ret#ENDIF#IF rand==1; From CaDan, by Iambian and GeekboyRandom:LFSR2Byte: ld hl,(lfsrseed2) ld a,L rlca xor h rlca ; rlca ; xor h ; rlca xor h rlca adc hl,hl ld (lfsrseed2),hl ld a,h ;for combatability ret#ENDIF#IF rand==2; From Zeda's libraryRandom:Rand8:;Output: L is a pseudo-random number, DE=509, B=0, C=seed1;Destroys:A;Size: 141 bytes;Speed: fastest: 672, slowest: 796;Notes:; Requires 2 seeds of 1 byte each, seed1 and seed2;Method; We iterates 2 Linear Congruential Generators. 'x' is seed1, 'y' is seed2; (33x+17) mod 251 -> x; (13y+83) mod 241 -> y; Next, we output the lower 8 bits of x*y mod 509; This all gives a period of 60491 (251*241) which should be overkill for games and such ld hl,(seed1) ld h,0;multiply by 13 ld b,h ld c,l add hl,hl add hl,bc add hl,hl add hl,hl add hl,bc;add 83 ld c,83 add hl,bc;mod_241 ld c,15 ld a,h add a,a add a,a add a,a add a,a sub h add a,l ;139 jr nc,$+6 add a,c jr nc,$+3 add a,c add a,c jr c,$+3 sub c;store back to seed1 ld (seed1),a;seed2 ld hl,(seed2) ld h,b;multiply by 33 ld b,h ld c,l add hl,hl add hl,hl add hl,hl add hl,hl add hl,hl add hl,bc;add 17 ld c,17 add hl,bc;---;make BC=seed1 ld c,a;---;mod_251 ld a,h add a,a add a,a add a,h add a,l jr nc,$+8 add a,5 jr nc,$+4 add a,5 cp 251 jr c,$+4 add a,5;store seed2 ld (seed2),a ld h,a ld l,b;H_Times_C sla h \ jr nc,$+3 \ ld l,c add hl,hl \ jr nc,$+3 \ add hl,bc add hl,hl \ jr nc,$+3 \ add hl,bc add hl,hl \ jr nc,$+3 \ add hl,bc add hl,hl \ jr nc,$+3 \ add hl,bc add hl,hl \ jr nc,$+3 \ add hl,bc add hl,hl \ jr nc,$+3 \ add hl,bc add hl,hl \ jr nc,$+3 \ add hl,bcHL_mod_509:;HL_mod_509 -> HL ld a,h ld d,b srl a rl d add a,h sub d jr nc,$+3 inc d add a,l jr nc,$+4 inc d ccf ld e,a ld hl,509 ex de,hl sbc hl,de jr c,$+6 add hl,de sbc hl,de ret nc add hl,de ret#ENDIF#IF shape==3L_mod_3:;Outputs:; A is the remainder; destroys L,BC; z flag if divisible by 3, else nz ld bc,0306h ld a,l res 4,l rlca rlca rlca rlca and 15 add a,l and 31 ld l,a sra l sra l and b add a,l sub c \ jr nc,$+3 \ add a,c sub b \ ret nc \ add a,b ret;at most 123 cycles, at least 114, 30 bytes#ENDIF#IF rand==3; From Zeda's libraryRandom:Rand24:;Inputs: seed1,seed2;Outputs:; HLA is the pseudo-random number; seed1,seed2 incremented accordingly;Destroys: BC,DE;Notes:; seed1*243+83 mod 65519 -> seed1; seed2*251+43 mod 65521 -> seed2; Output seed1*seed2 ld hl,(seed1) xor a ld b,h \ ld c,l ld de,83 add hl,hl \ rla ;2 add hl,bc \ adc a,d ;3 add hl,hl \ rla ;6 add hl,bc \ adc a,d ;7 add hl,hl \ rla ;14 add hl,bc \ adc a,d ;15 add hl,hl \ rla ;30 add hl,hl \ rla ;60 add hl,hl \ rla ;120 add hl,bc \ adc a,d ;121 add hl,hl \ rla ;242 add hl,bc \ adc a,d ;243 add hl,de \ adc a,d ;243x+83;now AHL mod 65519; Essentially, we can at least subtract A*65519=A(65536-17), so add A*17 to HL ex de,hl ld l,a ld b,h ld c,l add hl,hl add hl,hl add hl,hl add hl,hl add hl,bc add hl,de ld c,17 jr nc,$+3 add hl,bc add hl,bc jr c,$+4 sbc hl,bc ld (seed1),hl;seed1 done, now we need to do seed2 ld hl,(seed2); seed1*243+83 mod 65519 -> seed1; seed2*251+43 mod 65521 -> seed2; Output seed1*seed2 xor a ld b,h \ ld c,l ld de,43 add hl,hl \ rla ;2 add hl,bc \ adc a,d ;3 add hl,hl \ rla ;6 add hl,bc \ adc a,d ;7 add hl,hl \ rla ;14 add hl,bc \ adc a,d ;15 add hl,hl \ rla ;30 add hl,bc \ adc a,d ;31 add hl,hl \ rla ;62 add hl,hl \ rla ;124 add hl,bc \ adc a,d ;125 add hl,hl \ rla ;250 add hl,bc \ adc a,d ;251 add hl,de \ adc a,d ;251x+83;now AHL mod 65521; Essentially, we can at least subtract A*65521=A(65536-15), so add A*15 to HL ex de,hl ld l,a ld b,h ld c,l add hl,hl add hl,hl add hl,hl add hl,hl sbc hl,bc add hl,de ld c,15 jr nc,$+3 add hl,bc add hl,bc jr c,$+4 sbc hl,bc ld (seed2),hl;now seed1 and seed 2 are computed ld bc,(seed1) ex de,hl call BC_Times_DE ex de,hl ld l,b ld h,0 ld b,h ld c,l add hl,hl add hl,hl add hl,bc add hl,hl add hl,hl add hl,bc add hl,hl add hl,bc ld c,d ld d,e ld e,a ld a,c sbc hl,bc sbc a,b ret nc ld c,43 add hl,bc retBC_Times_DE:;BHLA is the result ld a,b or a ld hl,0 ld b,h;1 add a,a jr nc,$+4 ld h,d ld l,e;2 add hl,hl rla jr nc,$+4 add hl,de adc a,b;227+10b-7p add hl,hl rla jr nc,$+4 add hl,de adc a,b add hl,hl rla jr nc,$+4 add hl,de adc a,b add hl,hl rla jr nc,$+4 add hl,de adc a,b add hl,hl rla jr nc,$+4 add hl,de adc a,b add hl,hl rla jr nc,$+4 add hl,de adc a,b add hl,hl rla jr nc,$+4 add hl,de adc a,b;===;AHL is the result of B*DE*256 push hl ld h,b ld l,b ld b,a ld a,c ld c,h;1 add a,a jr nc,$+4 ld h,d ld l,e;2 add hl,hl rla jr nc,$+4 add hl,de adc a,c;227+10b-7p add hl,hl rla jr nc,$+4 add hl,de adc a,c add hl,hl rla jr nc,$+4 add hl,de adc a,c add hl,hl rla jr nc,$+4 add hl,de adc a,c add hl,hl rla jr nc,$+4 add hl,de adc a,c add hl,hl rla jr nc,$+4 add hl,de adc a,c add hl,hl rla jr nc,$+4 add hl,de adc a,c pop de;Now BDE*256+AHL ld c,a ld a,l ld l,h ld h,c add hl,de ret nc inc b;BHLA is the 32-bit result ret#ENDIF#IF rand==4Random:;from ionRandom ld hl,(seed1) ld a,r ld d,a ld e,(hl) add hl,de add a,l xor h ld (seed1),hl ld hl,0 ld e,a ld d,h add hl,de djnz $-1 ld l,h ret#ENDIF#IF rand==5Random: ld hl,(seed1) xor a ld b,h \ ld c,l ld de,83 add hl,hl \ rla ;2 add hl,bc \ adc a,d ;3 add hl,hl \ rla ;6 add hl,bc \ adc a,d ;7 add hl,hl \ rla ;14 add hl,bc \ adc a,d ;15 add hl,hl \ rla ;30 add hl,hl \ rla ;60 add hl,hl \ rla ;120 add hl,bc \ adc a,d ;121 add hl,hl \ rla ;242 add hl,bc \ adc a,d ;243 add hl,de \ adc a,d ;243x+83;now AHL mod 65519; Essentially, we can at least subtract A*65519=A(65536-17), so add A*17 to HL ex de,hl ld l,a ld b,h ld c,l add hl,hl add hl,hl add hl,hl add hl,hl add hl,bc add hl,de ld c,17 jr nc,$+3 add hl,bc add hl,bc jr c,$+4 sbc hl,bc ld (seed1),hl ret#ENDIF#IF rand==6Random: ld hl,(seed2); seed2*251+43 mod 65521 -> seed2 xor a ld b,h \ ld c,l ld de,43 add hl,hl \ rla ;2 add hl,bc \ adc a,d ;3 add hl,hl \ rla ;6 add hl,bc \ adc a,d ;7 add hl,hl \ rla ;14 add hl,bc \ adc a,d ;15 add hl,hl \ rla ;30 add hl,bc \ adc a,d ;31 add hl,hl \ rla ;62 add hl,hl \ rla ;124 add hl,bc \ adc a,d ;125 add hl,hl \ rla ;250 add hl,bc \ adc a,d ;251 add hl,de \ adc a,d ;251x+83;now AHL mod 65521; Essentially, we can at least subtract A*65521=A(65536-15), so add A*15 to HL ex de,hl ld l,a ld b,h ld c,l add hl,hl add hl,hl add hl,hl add hl,hl sbc hl,bc add hl,de ld c,15 jr nc,$+3 add hl,bc add hl,bc jr c,$+4 sbc hl,bc ld (seed2),hl ret#ENDIFPlot:;need to plot the pixel at HL ld a,l cp 64 \ ret nc#IF update==1 add a,80h out (16),a#ENDIF ld a,h cp 96 \ ret nc ld c,l ld h,0 ld b,h add hl,hl add hl,bc add hl,hl add hl,hl ld b,a rrca \ rrca \ rrca and 0Fh#IF update==1 add a,20h out (16),a add a,20h#ELSE or 40h#ENDIF ld c,a ld a,b ld b,93h add hl,bc and 7 ld b,a inc b ld a,1 rrca djnz $-1 or (hl) ld (hl),a#IF update==1 out (17),a#ENDIF retcoords: .db 0,0count: .db 0lfsrseed2:lfsrseed1:seed1: .dw 256seed2: .dw 0
Ohhhh, Nice visualisation. That could end up in a neat screensaver!
; From CaDan, by Iambian and GeekboyLFSR: ld hl,lfsrseed1 ld a,(hl) rrca ld (hl),a ret nc xor %00111000 ld (hl),a ret; From CaDan, by Iambian and GeekboyLFSR2Byte: ld hl,(lfsrseed2) ld a,L rlca xor h rlca ; rlca ; xor h ; rlca xor h rlca adc hl,hl ld (lfsrseed2),hl ld a,h ;for combatability ret