Inside the Einstein: The Disc, a TatungBytes course: XtalDOS listing the first practice disc image, on a rainbow sunburst
← Back to Modules
module
7

Opening A Real Disc Image

Introduction

Every program so far has had its file name written inside it: FIRST.DSK, four.bin. A tool should work on whatever disc you hand it. This section makes the first version of the tool the rest of the course builds - edisc.py - and all it does yet is open the image you name and show you its first 256 bytes. That is enough to start asking questions.

Using What Python Already Has

Python comes with a large collection of ready-made tools, kept in modules, and a program asks for one with import:

import sys

sys holds things about the running program itself. The one you need is sys.argv: a list of the words typed on the command line to run the program. When you type

python3 edisc.py FIRST.DSK

sys.argv[0] is "edisc.py" and sys.argv[1] is "FIRST.DSK". So the program reads its file name from sys.argv[1], and you can run the same program on any image just by typing a different name.

The code

Make a new file called edisc.py in your edisc folder. This is the tool's first version; every later section adds to it.

Copy show_byte and dump into it exactly as they are in dump.py from S6, then add the import at the top and the five lines at the bottom:

import sys


def show_byte(b):
    if 32 <= b < 127:
        return chr(b)
    return "."


def dump(data, start, length):
    for offset in range(start, start + length, 16):
        row = data[offset:offset + 16]
        hex_part = ""
        text_part = ""
        for b in row:
            hex_part = hex_part + f"{b:02X} "
            text_part = text_part + show_byte(b)
        print(f"{offset:06X}  {hex_part:<48} {text_part}")


filename = sys.argv[1]
with open(filename, "rb") as f:
    disc = f.read()

print(filename, "is", len(disc), "bytes")
dump(disc, 0, 256)

Run it

python3 edisc.py FIRST.DSK

What you should see

FIRST.DSK is 215296 bytes
000000  45 58 54 45 4E 44 45 44 20 43 50 43 20 44 53 4B  EXTENDED CPC DSK
000010  20 46 69 6C 65 0D 0A 44 69 73 6B 2D 49 6E 66 6F   File..Disk-Info
000020  0D 0A 65 64 69 73 63 00 00 00 00 00 00 00 00 00  ..edisc.........
000030  28 01 00 00 15 15 15 15 15 15 15 15 15 15 15 15  (...............
000040  15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15  ................
000050  15 15 15 15 15 15 15 15 15 15 15 15 00 00 00 00  ................
000060  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000070  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000080  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000090  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0000A0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0000B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0000C0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0000D0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0000E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0000F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

What You Are Looking At

This is the very start of a disc image, and it is not the Einstein's disc. It is a header - a page of information at the front of the file, written for the emulator, saying how the disc inside is laid out. (S1 mentioned it: an emulator handed a disc's bytes without this wrapping refuses them.)

Some of it you can read already. The first sixteen bytes are text: EXTENDED CPC DSK. Then File, a carriage return and new line (0D 0A), Disk-Info, another 0D 0A, and the word edisc. Then numbers that are not text at all: 28, 01, and a long run of 15. And then zeros to the end.

You already have the tools to start guessing. 0x28 is 40 - and S1 said an Einstein disc has 40 tracks. Count the 15s. 0x15 is 21, and S4's last "Change one thing" asked you for 0x15 * 256. S8 explains every one of these bytes; for now, it is worth a few minutes trying to explain them yourself.

Change one thing

  • Change dump(disc, 0, 256) to dump(disc, 256, 256) and run it again. The next 256 bytes are the start of something else. What text can you read, and is any of it like the text in the first 256?
  • Count the 15 bytes in the first dump. (Rows of sixteen make it easy.) What number do you get, and where else have you met it?
  • Run python3 edisc.py CHANGED.DSK, the image you made in S5. Is the header any different? Would you expect it to be?

When it goes wrong

SymptomCause
IndexError: list index out of range, on the line filename = sys.argv[1]No file name after edisc.py. sys.argv has only one item, and position 1 does not exist
FileNotFoundError: [Errno 2] No such file or directory: 'FRIST.DSK'The name is mistyped, or the image is not in this folder

Summary

edisc.py exists. It takes the image's name from the command line with sys.argv, reads the whole file, and dumps the first 256 bytes - which turn out to be a header written for the emulator, starting with the text EXTENDED CPC DSK, followed by numbers that look a lot like the disc's own shape.

Next

S8, The File's Front Page - what every byte of that header means, and the tool's first real check: is this an Einstein disc image at all?

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.