ATP Logo Dark

A Thousand Projects

Coder · Maker · Electronics

MZ80K Retro Cassette Project

April 8, 2024

Project Updates

  • 11 April 2024 - Added tape data format section
  • 10 April 2024 - Added initial page

This is an ongoing project for the Sharp MZ80K computer.
I like Retro and prefer not to use fast loaders/ SD/Compact flash, and instead stick with the original processes.
To that end, I have been coding up a project to decode and encode MZ80K programs to and from tape using a Commodore 64 cassette tape deck.

A program is available to encode MZF files onto tape using the Commodore 64 tape deck via the PC parallel port.
But.... I wanted the fun of coding this myself, decoding the file structures, and building a device to interface with the tape deck.
Of course, in the future it would be nice to add encoding/decoding for ZXC spectrum, C64, and others, but I thought I would start with the MZ80K.

Luckily for me, a site has a lot of info on the MZ80K tape format. Credit for much of the tech detail goes to that site.

Im using a NUCLEO-F411RE dev board with STM32 processor and STM32CubeIDE.

I had some PCB's made up for the Commodore 64 Tape plug so that it was easy to connect it up. This is single sided, and the plug is double sided, however, its just a mirror so only one side needed.

 

Pin Name Description
1-A GND Ground connection
2-B +5V 5V DC
3-C Motor 6V DC to power the motor. This can be used to enable or disable the motor; otherwise, it will run continuously.
4-D Read Data Read - 5V digital
5-E Write Data Write 5V digital pulse
6-F Sense Logical Low to indicate if any of the cassette buttons have been pressed

The Cassette port is connected to the NUCLEO-F411 as follows. The selected IO pins are 5V tolerant, though in the final design, I might add a high speed level shifter.

Nucleo Pin STM32 Pin C64 Port Pin
1-A
2-B
3-C Connected to separate 6V Power
D10 PB-6 4-D Read input to STM32
CN7 - 21 PB-7 5-E Write output from STM32
6-F Not connected Yet

Pulse Read Timer Setup

I initially thought I would use the timer's combined PWM input and the duty cycle to calculate the pulse width, and thus the pulse time. However, the PWM waits until the next pulse rise to trigger, so it captures the full pulse length. This does not work for reading just the pulse length. The PWM is really meant for a continuous waveform. I could probably have used a timeout more, but in the end I opted for "Input Capture direct mode" using Timer 4.

The system clock is running at 84Mhz so the timer set up is as follow

Prescaler - 84 to bring the timer to 1Mhz
Counter Period 1000 - to allow for timeout detection
Input capture Polarity Selection - Rising Edge
Prescaler division ratio - None

NVIC TIM4 global interrupt - Enabled

Pulse Measurement

Using my Saleae Logic Analyser confirms the pulse lengths, which match much of the documentation I have found online. Of course, in the real world, pulse durations aren't perfect, especially with a tape deck. My C64 deck at the time of writing this code is old and probably needs belt replacement and a cleanup, which is on my to-do list.

I set a Long Pulse duration in config to 380us; anything equal or longer is a Long pulse, and anything less than 380 is decoded as a short pulse. I should probably be more specific about that in the pulse measurement, but it seems to work.

The interrupt is configured to trigger on the rising edge initially and, once triggered, is reset to trigger on the falling edge. This lets me measure the pulse length without worrying about the timing of the next incoming pulse.

Data Format (Tape)

The data is stored on the tape in pulses, with a long pulse being a logical 1 and a short pulse a logical 0. The tape also includes synchronising pulses and markers that surround the data blocks.
The tape starts with a synchronising block of pulses, followed by a sequence of markers, headers, and data.

The documentation I found online says this block is 22000 short pulses, but when I analysed the actual pulses from a few MZ80K tapes, the pulse count varied widely, and on one tape (NumberTron), the initial pulse markers were fragmented. There were 6 short pulses, followed by a long pulse of 9ms! Then another block of 198 short pulses and finally a block of 13202 short pulses.

Header File format

Data file format

Sync Block (Long GAP)

The documentation I found online says this block is 22000 short pulses, but when I analysed the actual pulses from a few MZ80K tapes, the pulse count was vastly different, and on one tape (NumberTron), the initial pulse markers were fragmented. There were 6 short pulses, followed by a 9 ms-long pulse! Then another block of 198 short pulses and finally a block of 13202 short pulses.

Astrododge had 7 clean pulses, followed by a 13ms gap, and finally the main sync block of only 4424 short pulses.
Princes and Monsters had a clean single block of 4365 pulses.

This posed a bit of a problem, as these three tapes make it clear that the initial sync pulses are anything but uniform. In the end, I count 1000 pulses regardless of gaps and ignore the rest until the next section (Tape markers). As long as there are 1000 or more short pulses, I can safely assume this is the sync block.

Tape Marks

The next section is the Long Tape Mark, which marks the start of the header. For the header, this is 40 Long Pulses and then 40 Short Pulses. In my reckoning, this is where the stable data starts. I'm not sure why the initial sync block is so long.

The change in pulse lengths in the section (ie the single Long  Pulse after the 40 Short pulse Tape marker is useful to define the end and start of another block. This enables the decoding code to ensure that the pulse counts are what is expected.

Tape header

The header describes the file name, size, load, and execute addresses. It's 128 bytes long. There is also a 'comment' section of 104 bytes, which is just using up the balance of the 128-byte block. I haven't seen much data here, though there do seem to be a few bytes of non-text data at the end. I'm not sure if this is noise or used in some way.
The program Type is a number that defines the type of data being loaded.

The tape header is duplicated after the 256 short pulse marker. I assume this is redundancy. Tapes have issues!

Length Description
1 File Type01: Machine code file
02 : MZ-80 BASIC file
03: MZ-80 data file
04: MZ7-00 data file
05: MZ-700 BASIC Program
17 File Name terminated with 0x0d
2 Size of file
2 Load address
2 Execution address
104 Free text - Comments

 

Header Checksum

The checksum for both the header and file section is a simple bit counter, counting up the number of set bits in each byte. It's not the best checksum and can produce false results. The checksum is a 2-byte unsigned value and will roll over if the value is over 0xFFFF.

Data

The data block is essentially the same sequence as the header, just that the data section is whatever length is specified in the header. It is also repeated along with a checksum. 
The main difference is that the initially sync block is 11000 short pulses and the Tape markers are 20 pulses long each instead of 40. 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest