What am I missing? - Calling RND from assembly.

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
Spinnetti
Posts: 253
Joined: Sat Sep 12, 2020 11:29 pm

What am I missing? - Calling RND from assembly.

Post by Spinnetti »

So, I want to something very simple. I want a random int from 1-3 on the ZX-81
- In basic it might look like (RND *3)+1
- In Assembler, My understanding is would do this:
- LD HL,$03
- CALL $0BED
- LD A,L
Then A would have my number in it.

I've tried other methods like - or others using R:
LD A,R
LD C,A
LD A,($4034)
XOR C

Regardless of which method I use however, successive calls always give the same results. I need to be able to get a different number each call, even if I call it back to back. How do I do this simply? Doesn't have to be truly random, just seemingly random.

Thanks!
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
stroebeljc
Posts: 63
Joined: Thu Apr 23, 2020 6:02 am

Re: What am I missing? - Calling RND from assembly.

Post by stroebeljc »

In Basic you need to call RAND without an argument before calling RND. This uses the FRAMES counter as the random seed to generate a pseudo-random number. So, if you want to write your own assembly code, I would just use the FRAMES value, AND the result with 3, and test for 0.

BEGIN:
LD A,($4034)
AND $03
JR Z,BEGIN

Note that this will probably tend to produce more "1" results because the check for zero will eventually see the FRAMES value increment by one. There may be other ways to address this bias in the result, but it will be more code.
John
Spinnetti
Posts: 253
Joined: Sat Sep 12, 2020 11:29 pm

Re: What am I missing? - Calling RND from assembly.

Post by Spinnetti »

Thanks... still getting same value if I call it in near succession. If I call further apart in the code it work fine though. Hmm.
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
User avatar
1024MAK
Posts: 5101
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: What am I missing? - Calling RND from assembly.

Post by 1024MAK »

Don’t forget that the FRAMES counter only advances by one each video frame. For a 50Hz video system (Europe), that’s every 20ms. Also FRAMES only advances if the machine is generating video.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
stroebeljc
Posts: 63
Joined: Thu Apr 23, 2020 6:02 am

Re: What am I missing? - Calling RND from assembly.

Post by stroebeljc »

1024MAK wrote: Thu Sep 24, 2020 6:24 am Don’t forget that the FRAMES counter only advances by one each video frame. For a 50Hz video system (Europe), that’s every 20ms. Also FRAMES only advances if the machine is generating video.

Mark
Excellent point! Depending on your use case, there may be any of a thousand methods to generate a pseudo random number. If you are doing back-to-back calls in assembly, then the ZX-81 RND method will not work. You could just pick a memory location in ROM and read memory values, masking off the upper 6 bits. Then increment to the next memory location for the next call. Or you could use something more mathematical, such as a linear congruential generator. It all gets down to what you need it do and how complicated (and more random) you need to make it.
John
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: What am I missing? - Calling RND from assembly.

Post by dr beep »

Take a look in the newgames topic and download a few of my sources.
Many have a randomroutine, some use the ROM as bas, other a formula with FRAMES and a seedvalue.
Spinnetti
Posts: 253
Joined: Sat Sep 12, 2020 11:29 pm

Re: What am I missing? - Calling RND from assembly.

Post by Spinnetti »

Thanks Will do... - Where's the "new games" topic?

Managed to get things working by moving the code around a little and just using the LD A,R and masking bits. Not toooo bad. Will def. check out other examples as I'll need better methods at some point.

The result of all this will be a reasonable interpretation of Donkey Kong that me and a buddy have been puttering on since 1983! (we know its been done before and probably better) Other than a simple shell and a sorta working first round we didn't get much farther, but in the last few weeks we have been doing a lot. We have all 4 levels of the original Japan release game going (with our own variations - not a total clone). Core is done, now mostly just adding animations and fun little details. When polished up we'll post it up for folks to play with :) Next thing I need is a good "Blitter" routine.. will post that question up separately
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
User avatar
1024MAK
Posts: 5101
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: What am I missing? - Calling RND from assembly.

Post by 1024MAK »

Spinnetti wrote: Sat Sep 26, 2020 9:54 pm Thanks Will do... - Where's the "new games" topic?
It’s a sticky topic in this sub-forum.

I look forward to hearing more about your games progress :D

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: What am I missing? - Calling RND from assembly.

Post by dr beep »

This game has an RND using ROM, FRAMES and a RND-seed

B holds value between 1-255 on entry to set value between

download/file.php?id=6288

Routine is on page 8.
Spinnetti
Posts: 253
Joined: Sat Sep 12, 2020 11:29 pm

Re: What am I missing? - Calling RND from assembly.

Post by Spinnetti »

Thanks!

That 1k Hi rez example is great - game premise is good too!

Next thing I'd like to learn, if ya'll are willing to guide or example is how to do an object array. I'm assuming this is the best way to track multiple objects, and move them?

In our Donkey Kong example (Zonkey Kong as we call it). We've got to spawn, move and remove all the barrels and flaming balls depending on level. Right now we just have a brute force method with 5 sets of variables for location and direction. How would we use an array for those so we can use common functions and just pass pointers to each object?

Game screens are attached. All the rounds work, The player can run and jump, Barrels roll out, the "Princess" dances about and cries for help, the monkey waves his arms and "flames" flicker, and you can knock out the girder keystones in level 4, but having a bit of trouble with the player on the elevators in round 3 and still need to finish up some of the animations. Hard stuff is done, just adding all the fun details since we are "splurging" on 16K. Our original was a single screen and ran on our original TS1000's that we initially developed it on (and hand typed in all the hex!).
Attachments
Zonkey Round 4.png
Zonkey Round 3.png
Zonkey Round 2.png
Zonkey Round 1.png
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
Post Reply