Adding a Romdisk
The Dreamcast lacks a hard drive, and instead has to load everything from the CD-Rom into memory in order to use it. To make loading and unloading large amounts of data and assets easier, it’s preferable to use Rom Disks. Rom Disks take a folder on your computer, and turns it into an image file. KOS can then mount that image file in the dreamcast, creating a copy of your folder inside. You can load and unload multiple Rom Disks, making it handy for keeping memory organized.
Let’s create a rom disk. Make a folder in our project folder called romdisk. Let’s make some sample images in here, for us to draw with. Create a new png file in gimp, make it’s dimensions 128x128 pixels. At the top menu bar, click on Image → Precision → 16 bit Integer. Draw something on this png and save it into our romdisk folder as a png. Save it as test.png
We need to edit our Makefile in order to build this Romdisk. Open our makefile rules and let’s add some romdisk information. Let’s begin with an ifdef guard:
ifdef KOS_ROMDISK_DIR endif
This combination only includes these recipes in our Makefile if we have a variable called KOS_ROMDISK_DIR defined. That means we can turn off our romdisk creation in our makefile by just commenting out the definition for that variable.
romdisk.img: $(KOS_GENROMFS) -f romdisk.img -d $(KOS_ROMDISK_DIR) -v -x .svn -x .keepme
This creates our romdisk.img file. We use a program called genromfs to turn a filesystem folder into an image. KOS should install this tool, $(KOS_GENROMFS) points to it. It will be named romdisk.img when it outputs, and it will output to KOS_ROMDISK_DIR as we defined it. The -v flag makes it verbose, so we can see what files it is including in the romdisk. We want to exclude certain types of files from being in our romdisk, which we can do with -x. We are excluding .svn and .keepme files.
We can load and use romdisk.img in our program just fine, but it can be benefitial to bundle a romdisk inside the executable elf itself. This has the effect of burying the files within your program, so they’re always there. When you load the binary itself, the files are there. This is good for having some fundamental files you always want present in your program, like perhaps a font. We can do this by turning romdisk.img into a translation unit object file, romdisk.o. We do that like so:
romdisk.o: romdisk.img $(KOS_BASE)/utils/bin2c/bin2c romdisk.img romdisk_tmp.c romdisk $(KOS_CC) -g $(KOS_CFLAGS) -o romdisk_tmp.o -c romdisk_tmp.c $(KOS_CC) -g -o romdisk.o -r romdisk_tmp.o $(KOS_LIB_PATHS) -Wl,--whole-archive -lromdiskbase rm romdisk_tmp.c romdisk_tmp.o
We rely on romdisk.img being present, then feed it into a program called bin2c. This turns your binary data into a c source file called romdisk_tmp.c, with the data defined as a constant inside the c source file. Next, we call our cross compiler gcc and compile romdisk_tmp.c into romdisk_tmp.o. We then link romdisk_tmp.o with a shared object library called romdiskbase, which provides the functions we need to access the file system inside the Dreamcast. Finally, we clean up our temporary files with rm romdisk_tmp.c romdisk_tmp.o.
Place the above code in between our KOS_ROMDISK_DIR guards and save. Our make file now has an additional rule it can call: romdisk.o. Let’s add this to the $(OBJS) in our makefile:
OBJS = Game.o romdisk.o
Now, when we build our elf, our romdisk will automatically be bundled in and mounted We can access files in our game now. Let’s use this to draw a texture to the screen.