Vintage Computing
One of the earliest things we do in the code is to see
the random number generator with this line of code:
notepad random.c
srand( PEEKW(0x00a1));
The “jiffy” clock that is updated by interrupt routines
is stored at locations $A0, $A1, and $A2 in zero page
memory. This is the clock used by the BASIC meta-variable
TIME$. In the line of code above, I use the last two bytes
of the clock as an unsigned int initializer for srand() since
we’re unlikely to have the same values there each time we
run the program.
FIGURE 7. Changing the screen colors.
Random.c contains two function prototypes near the
beginning of the program:
int roll one die(int);
int roll dice();
The colorhello.c program first sets the border and
background colors to black. I used the hexadecimal
constants 0xd020 and 0xd021 because any number over
32767 expressed in decimal is assumed to be a “long” by
the compiler.
This allows functions that appear earlier in the code to
understand the parameter list requirements and return
values of each function.
Note that I used a decimal constant for location 646
which contains the current character color.
When you load and run the colorhello program, you
should see something like the screen in Figure 7.
Our function roll_one_die() accepts an integer
parameter. This parameter is named “sides” in the body of
the function code itself. The variables side’s visibility is
limited to use within the function roll_one_die().
Parameters are local variables.
If you try to LIST the program, you’ll see a line similar
to the following:
704 sys2061
The above is a BASIC stub that’s just long enough to
invoke the compiled machine-language payload that
follows it in memory.
The function roll_one_die() is intended to simulate a
singe die roll. Many role-playing games use dice with
varying numbers of sides. In the code, we call
roll_one_die() with a parameter of 6 when we want to
simulate rolling a six-sided die. We call the same function
with a parameter of 20 when we want to simulate a 20-
sided die.
Fun and Games
The function roll_dice() simulates rolling two six-sided
dice. Note that it calls roll_one_die() twice and then
returns the sum of those values.
Part of the fun of owning a microcomputer in the
1980s was typing in games from listings in books and
magazines. Let’s take a look at a few simple game-related
programs.
Also note that in the body of main(), I issue a “return”
command with the value zero. I did not do this in earlier
examples and received no warnings from the compiler. It’s
good form to supply the return value from main().
Most single-player games require the use of a random
number generator function. CC65 provides the rand()
function which returns a random unsigned integer in the
range of 0 to 32767 inclusive. The randomizer should be
seeded by the srand() function at the beginning of a
program to ensure that the series of random numbers will
be different each time the program is executed.
Let’s compile and run random.c. From a command
prompt, enter the following:
cc random
You should see the verbose compilation messages.
Then, load and run “random” from WinVICE. You
might see output similar to that in Figure 8.
Let’s take a look at random.c. From a command
prompt in the C:\c64\dev directory, enter the following:
If you run it again just by typing the word “run” and
enter, you should see different sets of random dice rolls.
Let’s take a look at guess.c:
notepad guess.c
The random.c code isn’t a game by itself, but it
illustrates a simulation of dice rolls. In addition, it illustrates
a few CC65 concepts that we can study.
This program uses a function named play_the_game()
as the primary function. It is called from main() in a
56 October 2015