Giter VIP home page Giter VIP logo

patch_cpm3's Introduction

patch_cpm3.py

Introduction

patch_cpm3.py is a Python script to patch NABU PC CP/M 3 disk images to work from simple sector disk images.

The Problem

NABU CP/M 3 disks store the Disk Parameter Block (DPB) out-of-band at the start of the raw track, where it is read using the WD179x FDC READ_TRACK command:

Track View

This information is lost if the disk is converted to a simple sector-based disk image. This has led to many NABU disk images being distributed in HFE format, which work fine with floppy emulators (Gotek/HxC2001), but don't play well with most CP/M tools.

A Solution

There's plenty of spare space at the end of the boot sector so the DPB can be stored there instead, moving it back in-band so it's preserved in all disk images:

Sector View

There are two places that read the DPB: the second stage loader (CPMLDR) and the main CPM3.SYS program. Code in both can be patched to read the DPB from this new location, if needed.

The patch inserts a hook to catch when the raw track reading fails to find a DPB. This happens when it encounters an IDAM (FE byte) after an A1 sync byte instead of the expected 4E filler that leads up to the DPB data. If this happens the hook leads to new code that reads the boot sector to look for a DPB in the last 32 bytes.

If a DPB is not found in either location the behaviour depends on the program. CPMLDR just hangs the system after a failed attempt to display an error message. CPM3.SYS has another try at finding a DPB, matching the format of the first track against a table with 4 entries: 5x1024, 16x128 (FM), 9x512 (DOS), 10x512. If a match is found a DPB stored with the matched entry is used for the disk, allowing some non-NABU disks to be used.

The DPB data starts with a run of 4E bytes, used to indicate the DPB is present. This is followed by a fixed 04 05 06 07 pattern as written by the format.com program, which aren't actually checked by anything. The next byte is a disk type, which is one of 00, 01 or 02, representing 40x1 / 40x2 / 80x2 disk formats. The final 17 bytes are the actual DPB containing CP/M parameters.

The script updates the boot sector to add a DPB block to the end, and attempts to patch CPMLDR and the CPM3.SYS file if they are found. The DPB used depends on the size of the raw disk image, so make sure that is correct. The patched image is written out to a new file with a _patched suffix.

Usage

usage: patch_cpm3.py [-h] [-f] [-d {s4,d8,d16}] file

Patch DPB reading in NABU PC CP/M 3 disk images.

positional arguments:
  file

options:
  -h, --help            show this help message and exit
  -f, --force           Force patching boot sector (default: False)
  -d {s4,d8,d16}, --default {s4,d8,d16}
                        Replace fallback DPB for NABU disks: s4, d8, d16. (default: None)

Example

python patch_cpm3.py disk.img

Output:

Reading: disk.img
Boot sector: added DPB
CPMLDR: code patched
CPM3.SYS: code patched
Written to disk_patched.img

If the end of the boot sector contains unexpected data it will not be overwritten. If you're happy it's safe you can force it to be updated with the --force option.

CP/M 3 allows access to NABU format disks that don't have an embedded DPB, using a built-in default DPB. This is s4 format by default, which is 40 tracks single-sided. Use the -d option to set this to a different format. For example, -d d16 selects 80 tracks double-sided as the default. Note: -d s4 set the same DPB as the format.com program, which is slightly different to the CP/M 3 internal default.

Credits

Special thanks to @alitel, who provided much needed NABU technical information and testing. Thanks also to @brijohn for the NABU MAME core, which made debugging much easier.

License

patch_cpm3.py is released under the MIT license.

Contact

Simon Owen
https://simonowen.com

patch_cpm3's People

Contributors

simonowen avatar

Stargazers

 avatar  avatar intangybles avatar Alexander Jacocks avatar Brian Johnson avatar

Watchers

 avatar  avatar

patch_cpm3's Issues

Support non-bootable disks

The current script expects to find cpm3.sys and cpmldr to patch before it considers writing the DPB to the boot sector. Non-bootable data only disks might not have them but would still benefit from the DPB, which would be picked up if the patched CP/M 3 was booted.

After this is relaxed more care should be taken before writing to the end of the boot sector. Currently 0xE5 filler or a decrementing byte pattern is likely safe, and other cases should be refused unless forced.

Investigate boot sector as track fallback

It would be good to perform the original track0 DPB reading, falling back on reading it from the boot sector if unsuccessful. This would allow mixing of patched and unpatched (but correctly formatted) disks, as long as the patched version of CP/M 3 was booted.

The current patch fits in the area where the track reading was placed, so the sector fallback would need to find another spare location, ideally automatically.

In cpmldr there are at least 128 bytes of decrementing byte pattern at the end of cpmldr, which follows another 64+ bytes of unreferenced code that appears to be from an early assembly attempt. In cpm3.sys there are some C7 filled blocks that appear to be unreferenced, but should be checked.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.