magazine logo
Welcome to the official Attitude website - Europe's #1 C64 scene disk magazine
D64 With Perl
by DJ Gruby/Oxyron

Manipulating D64/D71/D81 contents in Perl

Perl is well known to be a Swiss Army knife of programming languages. Would you be surprised if I told you that manipulating D64/D71/D81 contents in Perl is possible? Probably not. Would you be surprised if I told you that manipulating D64/D71/D81 contents in Perl is easy? Probably not. Would you like to learn how to simplify your coder's life by adding even more manually repeatable steps into your Perl scripts? Read on. If you're up for an automation of your build processes totally, one of the skills you need to learn is how to access and modify the contents of your disk images programmatically.


MagerValp/Genesis Project is one of those Commodore gurus we all admire. Number of projects he had been involved into is really impressive. Despite of having tons of useful utilities (a disk based voting system for demo competitions, terminal program for C64 BBS's, a webserver that generates PETSCII graphics, just to name a few) delivered throughout his entire programming career, he is still actively developing new tools and willingly supporting C64 demoscene community with his expertise. Amongst his projects there is one of our particular interest, namely "diskimage.c" library [1]. "diskimage.c" is an ANSI C library for manipulating Commodore disk images (D64/D71/D81 formats are supported). An API of "diskimage.c" library provides access to the implementation of the following operations: opening a file (including reading a directory), reading, writing, closing, deleting and renaming files, formatting entire disk images, as well as allocating and deallocating individual sectors. Fine, you may say, this is an ANSI C... How the heck is this going to help us with the automation of our release builds in Perl? Worry not! Perl's interface to MagerValp's "diskimage.c" is already available, and it is obtainable from CPAN [2] as the D64::Disk::Image package [3].


D64::Disk::Image comes up bundled with a comprehensive documentation as perldoc pages and plentiful of source code examples, helping you with begin using it in no time required to learn or trying to understand its mechanics. There is even a slick synopsis of all available functionalities listed at the very beginning of the package specification. I don't want this article to be a reprint of all those examples given on the website (it's more meant to be a signal to you that this neat thing is just there, and ready to get raked up), however to at least give you a foretaste of what can be found in the documentation, here is an enlightening example, how to easily extract a PRG program file from within a D64 disk image, using your favourite scripting language of course (comments included):

# Import some semantics into the current package:
use D64::Disk::Image qw(:all);

# Load an image from disk:
my $d64 = D64::Disk::Image->load_image('image.d64');

# Open a file for reading:
my $rawname = $d64->rawname_from_name('filename');
my $prg = $d64->open($rawname, T_PRG, F_READ);

# Read data from file:
my ($counter, $buffer) = $prg->read();

# Close a file:

# Free an image in memory:

Simple, isn't it? With just a few lines of code you are able to integrate all your disk access routines into a clever function with less than 10 lines of code. Want to see more examples? Just go to the D64::Disk::Image package website [3] to get detailed description and full overview of all its powers.


One of Perl's mottos reads "making easy things easy and hard things possible". While getting and writing program file data from/into a disk image seems a trivial task, there's a little more effort required to understand how to read the contents of a disk directory (in general, not in Perl): knowledge of how directory data is structured is essential to correctly access all the required information. Those details include (and are not limited to) size in bytes of each single directory item, offsets within each entry on how to access program name, awareness of how different values represent various file types, way of figuring out how many entries are at all available on disk, etc.

If you look at the example how to list the directory contents, it's no longer such a trivial task as the one discussed in the previous section. However, that's the case only with the D64::Disk::Image package. And there are more helpful modules. D64::Disk::Dir [4] is a simple wrapper around D64::Disk::Image, which provides convenience methods for reading disk image directories. It is as well available on CPAN. It gives you an easy access to all the information that are stored in a directory of a D64 disk image. It is equipped with separate classes for accessing individual file information as well as iterating through the entire directory contents.

The most common operations that you most likely would need to otherwise implement yourself include: traversing the directory tree in a search for a file with the given name, reading file's type, size, data, as well as simply printing out the directory contents. D64::Disk::Dir package already implements and bundles these and more capabilities, hence making them instantly ready for use.


One of the crucial actions amongst your other operational activities, for example when building a trackmo (or a diskmag like "Attitude"), is setting up a look-up table with track and sector data for each file that is loaded from disk during the course of your program. An IRQ loader of your choice is most likely equipped with a loading procedure that expects track and sector information as its input parameters.

Assume we have got a disk image, where all the "Attitude" articles have been written to, and we need to extract track and sector information in order to be able to include this data in our source code:

# Import some semantics into the current package:
use D64::Disk::Dir;
use D64::Disk::Dir::Iterator;

# Read entire D64/D71/D81 disk image directory from file on disk:
my $d64DiskDir = D64::Disk::Dir->new('image.d64');

# Create an iterator with a directory object instance:
my $iter = D64::Disk::Dir::Iterator->new($d64DiskDir);

# Perlish style iterator:
while (my $entry = $iter->getNext()) {

# Get various parameters describing detailed entry properties:
my $track = $entry->get_track();
my $sector = $entry->get_sector();
# something with the data...

This example shows how to retrieve information needed using a human readable abstraction instead of trying to properly interpret raw bytes of data, which is the main advantage of maintaining disk directory access by using a scripting language like Perl. Again, it's easy and straightforward. It does not require you to learn the internals of disk data storage format and delivers all the required functionality you might ever need. You must only edit those very few lines of source code, add them into your build script and voila, you've got everything you need!

It is not only track and sector data that directory object instance is able to retrieve from each file on disk. Every single bit of information that is stored in the disk directory is available via convenience methods exposed by the D64::Disk::Dir API, so there is way more you can do besides accessing track and sector information. Read the entire documentation for a complete list of all available methods.


Manipulating disk image contents in Perl is easy and may turn out to be found very helpful in saving some time you would normally dedicate to routinely realize a standard set of operations which are required to build your new software, time that could just have been used more effectively. There are a few Perl packages that might be found handy whenever you need to effortlessly automate your compile processes, and they have been discussed in less or more details within this article with a few concrete source code examples given as well.

I hope this simple tutorial will add some value to your productivity level by helping you out with saving some time which would have to be otherwise spent on performing manual tasks. By increasing the amount of things that our computers can do for us, we get the possibility to focus on the actual tasks of producing a decent software by concentrating ourselves more on the actual application logic instead of wasting our time doing those long and boring tasks repetitively every time we compile our code.



[1] diskimage.c library official website
[2] Comprehensive Perl Archive Network
[3] D64::Disk::Image CPAN package website
[4] D64::Disk::Dir CPAN package website

   Add/view comments on this article (comments: 0)

Do you believe we are
able to cope with
releasing "Attitude"
on a regular basis?

yes no

 YES: 277 (70.66%)
NO:115 (29.34%) 



all visits:

visits today:

website started:
Official Webpage
of Attitude
Copyright 2004-2017