58 October 2015
Our main() function calls the initialization function
and then iterates in an input loop. Once you have headed
South from the “main lobby,” you have walked outside of
the building, ending the simulation.
Here’s a quick walkthrough of the rooms program.
(Refer to Figures 10, 11, and 12.) The data structures
could be fleshed out with other data structure references.
Perhaps each room should contain a pointer to a list of
items to be found there. We would want to create data
structures to model the player, as well as the rooms. A
player data structure might have strength, stamina, and
intelligence attributes, as well as a list of items they are
carrying and a reference to the room they are in.
Note that we created the data structures themselves
from allocated memory blocks that live on the “heap.”
Although we don’t have a whole lot of memory to work
with, the heap is a managed structure that can allow
dynamic allocation and the freeing of memory.
To free up a memory block allocated with malloc() or
one of the related functions, one can call free() passing
the pointer to the block of memory as the argument.
If you need a very large block of memory as a buffer
for a file copy operation or something similar, the only
way I’ve found to allocate memory in a large chunk is to
try for something absurdly large and decrease the
requested amount until you succeed. Refer to Figure 13 to
see the output of the mem.c program.
I would recommend leaving some room so that the
big allocation doesn’t limit our stack usage for automatic
local variables and return addresses.
A C64 program written with CC65 uses memory in
the range $0800 - $CFFF inclusive. The BASIC ROM at
$A000 is disabled so that the 8K of RAM underneath can
be utilized. The C runtime stack begins at location $CFFF
and works backward in memory. The heap begins at the
end of the program code and grows toward the stack
In addition to the char and int types we’ve used so
far, CC65 natively supports signed and unsigned long
integers. The program longs.c demonstrates the usage of a
long variable and a long constant.
Functions and Structures
The header file cbm.h and the corresponding library
provide Commodore-specific functions and data structures
to the CC65 compiler. Please consider the example in
In showdir.c, we open a directory using the CBM-specific cbm_opendir() function. We pass in the address of
a cbm_dirent data structure. If the function returns zero,
we were successful and the data structure is filled.
The while-loop then displays each directory entry
name, the number of 256 byte blocks consumed on the
disk, and the CBM file type. Refer to Figure 14.
FIGURE 10. A text adventure location
FIGURE 11. Part 2 of the location simulator.
FIGURE 12. Part 3 of the location simulator.