Inside the MZ-80K disc
Source: Peter Sydenham, SHARPSOFT-magazine
August 1984, Volume 14, p. 39 - 42
|
Introduction |
The purpose of these brief notes is to give the reader an understanding
of exactly what is happening when discs are in use on the MZ-80K. lt
should be realised that in general what is said is with reference to
the discs as used within BASIC 6015. When Sharp FDOS, CP/M or any other
DOS is in use there may be considerable differences in how files are
held. Everything in this article has been deduced from a study of the
hex. code used within BASIC 6015 and the Sharp supplied utilities, together
with considerable experimentation. Much of the work was carried out
using the Sharp “Machine language“ tape and latterly with
the disassembler written by R. Tanswell.
|
Tracks and sectors |
A disc is divided up into 70 tracks of 16 sectors each to give a total
capacity of 1120 sectors. One sector can contain 128 bytes of data to
give an overall capacity of 140K, the figure quoted by Sharp. Tracks
are numbered from 0 to 69 with track 0 nearest the outer edge of the
disc. Sectors are numbered from 1 to 16. In the Sharp System all even
numbered tracks are on the top face of the drive and all odd numbered
tracks in the lower face ( when the disc is in the drive ).
Not all the tracks are available for file storage, in fact the system
uses tracks 0, 1, 2 and 3 for its own purposes. Thus a freshly initialised
disc gives 1056 sectors i.e. 132K of user file space. Tracks 1, 2 and
3 are reserved for the directory of what files are held on the disc
and track 0 is used in the following way. Sectors 15 and 16 contain
the bit map giving which sectors of the whole disc are free and which
ones are in use for file storage. The volume number and a count of the
number of the potential 1056 sectors that are in use is also held in
sector 15.
A master disc has the secondary boot code in track 0 sectors 1 - 14.
A non-master does not have this code but the sectors cannot be accessed
for file storage and so represent wasted space. Further space is taken
up on a master by 6015 BASIC in tracks 4 - 13 inclusive, although no
directory entry is present for the BASIC.
|
File storage |
When a file is stored on disc an entry is made in the directory of the
file name and the whereabouts of the first sector of data. At the end
of the first data sector of the file is the address of the next sector
and so on. These links use 2 bytes giving the track number and sector
number, both in hex. Thus only 126 bytes of each sector can be used
for data. The next 2 tables below show this linking. The important of
these links will become more apparent in later paragraphs for although
in many instances a file is stored in consecutive sectors - this will
not always be the case.
Directory entry
( 62 bytes ) |
Track |
Sector |
|
points to
first sector
|
First sector of file
( 126 bytes of data ) |
Track |
Sector |
|
points to
next sector
|
|
Bit map |
lt is important that the system can determine which sectors on the disc
are in use so that when a file is stored it does not overwrite part
of what is already on the disc. This is achieved by keeping a map of
all the sectors that may be used for file storage and indicating in
the map which sectors are in use and which are free. As stated before
there are 1056 sectors for file storage. One bit is used to represent
each sector hence the map is 132 bytes long with 2 bytes i.e. 16 bits
to represent each track. Track 0 sectors 15 and 16 are used for this
bit map with some of the spare bytes holding the disc volume number
and a count of the number of sectors in use. In the map a free sector
is represented by the relevant bit being 0 and a sector that is in use
being represented by the bit being 1. Track 4, the first track available
for file storage, is mapped into bytes 4 and 5 counting the first byte
of the sector as byte 0. Byte 4, bit 0 is sector 1, byte 4 bit 1 is
sector 2 etc. until bit 7 is sector 8. Byte 5 bit 0 is sector 9, byte
5 bit 1 is sector 10 etc. Writing bytes 4 and 5 in binary as below makes
it clear.
Byte 4
|
|
Byte 5
|
0000
|
|
0000
|
|
0000
|
|
0000
|
|
|
|
|
|
|
|
  |
|
|
|
  |
|
|
|
|
|
|
   |
|
|
 |
Sector 8 |
|
Sector 1
|
|
Sector 16 |
|
Sector 9
|
Bytes 6 and 7 represent track 5, bytes 8 and 9 track 6 etc., through
to track 70.
lt should be noticed that tracks 0, 1, 2, 3 do not appear in the bit
map since they are used by the System and hence never available for
file storage. Byte 0 of sector 15 is unused. Byte 1 holds the volume
number in hex. eg. if the volume number was 127, byte 1 would contain
7F. The next 2 bytes hold the number of sectors used for file storage.
When the directory is displayed the number of free sectors is calculated
and the free sector count is displayed. Thus the overall picture is:
Byte |
length |
meaning |
0 |
1 |
unused |
1 |
2 |
volume number ( hex ) |
3 |
2 |
count of sectors ( Z80 format ) |
5 |
2 |
bit map track 4 |
7 |
2 |
bit map track 5 |
9 |
2 |
bit map track 6 |
and so on up to... |
137 |
2 |
last bit map track 70 |
|
Directory |
The purpose of the directory entry is to enable the System to “know“
enough about a file to be able to load it into the RAM correctly. The
directory entry is directly analogous to the header written to a cassette
when using tape storage with the BASIC 5025. The next table below shows
the function of each byte in the directory entry. Note that each entry
is 54 bytes long and hence each sector of the directory can hold 2 entries
giving a total possible of 3 x 16 x 2 = 96 file entries.
Byte |
Length |
Function |
Notes |
0 |
1 |
file type code |
01 = OBJ ( machine code )
02 = BTX ( BASIC program )
03 = BSD ( Basic, sequential data )
04 = BRD ( BASIC, random data ) |
1 |
16 |
file name |
ASCII; $0D at end |
17 |
1 |
lock byte |
00 = unlocked
01 = locked |
18 |
2 |
file length |
Z80 hex |
20 |
2 |
file start address |
all 0 if BASIC program |
22 |
2 |
file execute address |
24 |
39 |
unused |
|
63 |
2 |
link to track / sector |
address of first sector of the file |
|
File erasure |
From the previous sections most of what has to be done when a file is
erased should be apparent; it is
- Find all sectors on the disc that were part of the file and clear
to zero the appropriate bits within the bit map.
- Remove the directory entry.
- Update the ‘sectors in use‘ count.
In the MZ-80K when one entry in the directory is removed all subsequent
entries are moved down one place to close the gap formed. Thus after
a file is removed all later entries are rewritten one place down. This
can result in the final entry in the directory being duplicated. To
prevent ambiguity arising the now redundant entry has a zero as the
file type code digit. When the system is reading the directory a zero
at this place indicates that the end of the directory has been reached.
By following the link in the directory entry of the file to be erased
and following the links in the file all the sectors used for it are
found. The bit map is then updated together with the ‘sectors in
use‘ count. ( lt is this following of links that makes erasure
so slow ). When a new file is saved on disk free sectors are used up
in the order they appear on the disc. After many file erasures and fresh
files being saved, some files may occupy sectors scattered across the
disc, hence the importance of the links at the end of each sector.
|
Start up |
When FD is typed from monitor, the following sequence of events takes
place. The monitor examines the contents of address $F000. If it is
found to be other than 00, the monitor retains control. ( on the disc
controller card ROM is fitted that starts at address $F000 and the contents
of the first byte is 00.) If the controller card is fitted the code
beginning at $F000 is executed. This code is the primary boot which
reads track 0 sectors 1 to 14 inclusive and loads this secondary boot
into RAM starting at address $9800. The first byte of this code is then
examined and if it is $C3 control is passed to $9800. This code then
reads tracks 4 - 13 inclusive into RAM starting at address $1200. BASIC
6015 has now been loaded and control is handed over to it.
|