Written by Richard Storer <rstorer@cyberspace.org>  28-10-1999

Standard CBM encoding
----------------------

The Datassette stores data on tape as a series of square pulses of varying
wavelength.
All CBM tape data used by different Commodore machines are 100% compatible
with the exception of the C16/+4 which has its own slightly modified version
of the original format not discussed in this documentation.

CBM encoding consists of three different pulses: Short, Medium and Long.

C64

S = 387.2s or 2.583kHz
M = 536.4s or 1.864kHz
L = 707.4s or 1.414kHz

VIC 20

S = 313.4s or 3.190kHz
M = 449.2s or 2.226kHz
L = 593.4s or 1.685kHz

PET

S = 358.7s or 2.788kHz
M = 509.6s or 1.962kHz
L = 677.2s or 1.477kHz

C16/+4

S = 472.7s or 2.116kHz
M = 955.4s or 1.047kHz
L = 1852.2s or 0.540kHz

(All timing data were collected from samples of different PAL tapes 
and therefore maybe slightly inaccurate)

The three pulses when grouped together can be interpreted as,

L,M = new data
L,S = end of data
M,S = bit 1
S,M = bit 0    

The tape encoding begins by a tape leader of several S pulses.
After the tape leader a new data marker L,M is written followed by the byte, 
the check bit, then another L,M.
The byte is written to tape as, bit0 .. bit7
The check bit is calculated by, 1 xor bit0 xor bit1 .... xor bit7

Example,

LM MS MS SM MS MS SM SM MS SM LM MS SM MS
   1  1  0  1  1  0  0  1  0     .  .  .

10011011 = 155
Check bit = 0

When data has finished L,S + 77 S pulses are written followed by a repeat 
of the same data + L,S and 78 S pulses.

Data stored on tape is as follows,

10 second tape leader
ID + header block + check byte
ID + header block + check byte - repeated

Small gap

2 second tape leader
ID + program block + check byte
ID + program block + check byte - repeated

The ID consists of nine bytes and determines whether the tape data
contains the first block or repeated block.

First block ID    = $89 $88 $87 $86 $86 $84 $83 $82 $81
Repeated block ID = $09 $08 $07 $06 $05 $04 $03 $02 $01

The check byte is calculated by, 0 xor all block data written to tape.


The header saved to tape consists of 192 bytes.

Header information:

 offset|bytes used| information
-------+----------+---------------
    0  |     1    | file type
    1  |     2    | load address 
    3  |     2    | file size   
    5  |    16    | file name
   21  |   171    | unused

File type:

1 - Basic file
2 - Data block (for a seq file)
3 - Fixed address file
4 - Seq file
5 - End of tape maker



Turbo Tape encoding
--------------------

Turbo Tape is a special type of signal encoding used by most commercial games
to load and save data much faster than the standard method. To be able to read
and write Turbo Tape data a custom written machine code program is needed.
Turbo Tape encoding consists of two types of pulse data of varying wavelength,
Hi pulse (H) and Low pulse (L). The actual wavelengths used for H and L would
vary from Turbo Tape to Turbo Tape and the encoding method could vary too.
The following Turbo Tape encoding described below is from a program called
TURBOTAPE 64 which appeared in the COMPUTE! Gazette July 85.

Hi pulse  (H) = 207.8s  or  4.811kHz
Low pulse (L) = 338.6s  or  2.953kHz

H pulses = bit 0 
L pulses = bit 1

The bytes are written to tape as, bit0 .. bit7 and unlike CBM standard
encoding TURBOTAPE 64 doesn't include a check bit for each byte or does
it indicate a new byte.

Example,

HLLHLLLH -> 01110110 = 118

The tape encoding begins by a tape leader with the byte value $02 written 256
times (HHHHHHLH).  
After the tape leader the following bytes are written,

$09, $08, $07, $06, $05, $04, $03, $02, $01,
Load address 
End address 
Program data
Check byte
HLLHHH pulses

The check byte is calculated by adding all bytes written for program data.

