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

What's Free?

Introduction

To add a file, the tool needs somewhere to put it: blocks nobody is using, and a directory slot nobody is using. An Einstein disc has no list of free space - no "free block map" stored anywhere. Its directory and its files are all there is. So the free space has to be worked out, from the directory, every time. This section does that, and draws the result.

Free Blocks

A block is in use if some live entry - one whose status is not E5 - names it in its block numbers. Every other block from 1 to 94 is free.

That has a consequence worth noticing. A deleted file's entry still lists its blocks (S14), but because its status is E5 it does not count - so deleting a file frees its blocks, without touching them at all. On FILES.DSK, block 14 still holds OLD.TXT's text, and it is free.

Free Slots

A directory slot is free if its first byte is E5 - whether it was never used, or held a file that was deleted.

Lowest First

When there is a choice, the Einstein's DOS takes the lowest free block first, and edisc.py will do the same. free_blocks builds its list in order from 1 upwards, so the first free block is always at the front.

The code

After blank_image:

def used_blocks(disc):
    used = []
    for entry in live_entries(disc):
        for block in entry_blocks(entry):
            used.append(block)
    return used


def free_blocks(disc):
    used = used_blocks(disc)
    free = []
    for block in range(1, 95):
        if block not in used:
            free.append(block)
    return free


def free_slots(disc):
    directory = read_directory(disc)
    free = []
    for slot in range(64):
        if directory[slot * 32] == 0xE5:
            free.append(slot)
    return free

range(1, 95) is 1 to 94: blocks from 1, because block 0 is the directory, to 94, the last. After create, a command that draws the blocks as a line of characters - D for the directory, # for a block in use, . for a free one - and prints it fifty to a line:

def show_free(disc):
    used = used_blocks(disc)
    line = "D"
    for block in range(1, 95):
        if block in used:
            line = line + "#"
        else:
            line = line + "."
    print("Blocks 0-94:  D = directory, # = in use, . = free")
    for start in range(0, 95, 50):
        print(f"{start:3}  {line[start:start + 50]}")
    print(f"Free: {len(free_blocks(disc))} blocks ({len(free_blocks(disc)) * 2}K),"
          f" {len(free_slots(disc))} of 64 directory slots")

Text can be sliced just like bytes: line[0:50] is its first fifty characters. And before the else::

elif command == "free":
    show_free(disc)

(The whole program: s20-edisc.py.)

Run it

python3 edisc.py free FILES.DSK

What you should see

Blocks 0-94:  D = directory, # = in use, . = free
  0  D#############....................................
 50  .............................................
Free: 81 blocks (162K), 60 of 64 directory slots

Block 0 the directory, blocks 1 to 13 in use - README's 1, STORY's 2 to 11, NUMBERS' 12 and 13 - and everything from 14 free, including OLD.TXT's block 14. 81 blocks is 162K: the 162k Free that both list and XtalDOS's DIR showed for this disc. Four slots are used, 60 free - and OLD.TXT's old slot is one of the free ones.

The next file added to FILES.DSK will go into block 14 and slot 4, over the top of what is left of OLD.TXT.

Change one thing

  • free NEW.DSK, your blank disc from S19. How many blocks, and how many slots?
  • In free_blocks, change range(1, 95) to range(0, 95). What does free FILES.DSK claim now, and why would that be a disaster for the next file added?
  • In used_blocks, use every slot instead of live_entries, so deleted entries count too: loop over range(64) and slice each entry out of read_directory(disc), as show_directory does. Which block changes on the map?

Summary

There is no free-space list on an Einstein disc: free space is whatever the live entries do not name. free_blocks lists the unused blocks from 1 to 94, lowest first; free_slots lists the directory slots starting E5. Deleting a file frees its blocks and its slot without touching the data. edisc.py free draws the map - and its figure is DIR's.

Next

S21, Writing An Entry - building a directory entry's 32 bytes, and the tool's first change to an existing disc: rename.

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.