Dice 2

This short program rolls 2 dice. It demonstrates the use of the Z80's refresh register R as a simple pseudo random number generator for game purposes. Later programs will show you other means of generating pseudo random numbers, but for 2 dice the R register is quite suitable.

Operation

Like most programs on the MPF-1 this program is started from address 01800H. You can download the package belonging to this experiment by clicking on the link on the left. The included MP3 file can be uploaded as filename 0003.
The program simply shows two digits   3   6  , representing the rolls of the 2 separate dice. Each time you press just about any key the program generates 2 fairly random new rolls.

This concludes the complete user interface of the program, but how does it work under the bonnet? The Z80 is equipped with a refresh register R. This register's official purpose is to assist in refreshing dynamic RAM memory. We don't have any of that on our MPF-1, but we can still abuse register R to generate pseudo random numbers. If you read this register whenever a key is pressed you'll get a fairly unpredictable value.
For our purpose it suffices to say that register R is incremented at just about every instruction the Z80 executes. Unfortunately the Z80 only increments 7 of the 8 bits of register R. The most significant bit is not automatically incremented. We don't let that bother is now though, after copying register R to A we simply shift all bits 1 bit to the left. We regard this fairly random byte as a fraction (placed in L). If we multiply this fraction by 6 and ignore the new fraction we end up with an integer (placed in H) ranging from 0 to 5. After one final increment we end up with a random number ranging from 1 to 6.

Each die roll can be represented by a 3 bit number. We want 2 dice, which requires a total of 6 bits. We started off with 7 random bits from register R, so we have enough resolution to handle both dice.
This automatically demonstrates the limitations of the R register as a random number generator. It's no use reading the R register twice in a row to increase its resolution because it is very likely that there is a fixed relation between the two which probably rubs off on the outcome of the rolls.

Room For Improvement

Like any program there is always room for improvement. Maybe you'll feel like doing that yourself. Here are some ideas you might implement:

  • The program generates key beeps, apparently like the system key beeps. Currently the frequency and duration of the beeps are fixed. The monitor allows you to change the frequency and duration of the system beeps by altering addresses FBEEP (01FF1H) and TBEEP (01FF2H and 01FF3H). See if you can change the program to automatically adopt these system beep parameters.
  • Another property of the system beep is that you can switch it off by changing address BEEPSET (01FF0H) to any value other than 055H. You may also change the program to automatically follow the system's setting.
  • You may brush up the cosmetics of this program. Frankly its current appearance is a bit dull. You may for instance add some rolling animation while a key is held down, just like the Dice 6 program does.

Download

Here you can download the file dice2.zip, which contains the source listing, the actual plain hex file, a list file and an mp3 file of the program.