JOE PRITCHARD winds up his article on how to achieve random access filing on the CPC
OU saw last month how a couple of Dos routines allowed you to write directly to a given sector on a disc. Using the sector as the record, I showed you how this could be the basis of a random access filing system for CPC.
Now I'll expand on the theme and, for easier use, turn the machine code programs into RSX commands.
Before we start, let me remind you that these techniques are for use only on blank data format discs. Files written by these commands won't show up in the catalogue, though you may get some garbage on the screen because you are writing information to the sectors that normally hold catalogue information.
Finally, remember that a freshly formatted sector contains 512 bytes of &E5s - in other words, nothing. These bytes can be used to see if a sector has previously been written to.
Type in, save and run Program I, which installs five RSX commands. They are initialised by a CALL &8000 command; DO NOT call this address more than once or a nasty mess will often result. The new commands and their syntax are:
|RADRV,n: Sets the drive to be used for random access. The parameter n has the value 0 or 1 but this is not verified by the routine, |WRITEBUFFER , @a$,offset: Writes the contents of a string to the 512 byte buffer used by the commands. The parameter offset specifies where in the buffer the string is to be written. Thus|WRITEBUFFER,@s$,256 will write the contents of s$ starting at position 256 within the buffer, |READBUFFER,@a$,offset: Reads
back the contents of the buffer into a$, starting at the position specified by offset. As the number of characters read back depends on the length of the string variable, it's important to set this to the desired length first. For example:
will read back the first 100 bytes. If you initialise a$ to hold only one space, it will read back the first byte only.
|WRITESECTOR , track , sector: Writes the contents of the random access buffer to a sector specified by track and sector.
|READSECTOR , track , sector: Reads the sector specified by track and sector into the random access buffer.
The commands will return directly if the correct number of parameters isn't present in the command line. However, there's no checking on the values passed to the various routines, so take care. Should outrageous values be passed to |READSECTOR or |WRITESECTOR, a "Disc read fail" message will usually result.
So let's see these commands in action. Program II prints to the screen the first 255 bytes of a specified sector on a data format disc - simply enter the track (0-39) and sector number (1-9). Any Ascii characters in the range 32 to 126 are displayed normally, and all others are shown as a full stop.
This simple program allows you to view what's on a sector - if you wanted to view the second 255 bytes, simply change the value of the offset to 256.
Now you can get down to the business of storing data on your discs. The simplest case to consider is when you treat the disc as a series of 512 byte records - a record occupying a whole sector.
You've already seen you can read or write any sector without having to sequentially access other sectors. As you saw last month, this is great for data storage.
To demonstrate writing data to each record, try Program III on a freshly formatted data disc, or one which you don't mind being overwritten. It writes a small message to each sector in turn, but you could easily modify it so you can select which track and sector to write to and enter your own message.
The basic technique is shown in the program; you first write the data to the random access buffer using |WRITEBUFFER and then write it to the disc using |WRITESECTOR. This is where offset comes in useful allowing you to write strings to
wherever you like in the buffer.
So far you've accessed the disc by specifying the sector of interest. It would be much more elegant if you could specify a record number without having to use the track and sector numbers. Program IV demonstrates this for the records written by Program III.
Simply enter the record number and the first 255 bytes will be printed to the screen. The record number is turned into a track and sector number using the DIV (\) and MOD functions, remembering there are nine sectors to a track.
With the 512 byte records you've just been using, you need to read and write the buffer in at least two steps, due to Amstrad Basic's 255 character limitation on string length.
This isn't too big a problem though, as it would be very unlikely for a single field to be longer than 255 characters. And even if it was, you could write it to the buffer in two parts.
You might like to modify this program to allow you to enter a record number and a message, then work out the track and sector number from the record number and write the message to disc with |WRITEBUFFER and |WRITESECTOR.
The advantages of being able to access a record directly in this way are great in terms of time saved and memory used. You only get one record into the computer at once, thus making more space available for programs, using the disc like the bank switched memory of the CPC6128.
But what if you don't need all the space in a 512 byte record, and need more records? This poses no problems. Program V writes two records to each sector using offset to write a separate message into the first and second 255 bytes of the buffer.
The MOD function is used to decide whether a given message is to be written to the first or second 255 bytes of the buffer. Both records are written at once by a single sector write.
Program VI reads back these 255 byte records. Clearly, when using these smaller records more than one will be read into the machine at once as a single sector is read in.
You can do the opposite of this -have a few very long records, each possibly occupying two or more sectors. In this case, there are two approaches to reading and writing. The first is to keep the buffer at 512 bytes, write the first part of a record to it, then write it to disc. Write the second part to the buffer, then write it to the next sector, and so on.
Alternatively, you could make a larger buffer so a whole record is manipulated at once. The first approach is probably better, as it needs less space.
Although you've used strings here, the random access technique deals simply in bytes. You could have the bytes representing numbers, or even computer programs.
This would allow you to have a loader program saved normally on disc, which, on running, loaded in the rest of the program from known tracks and sectors. This could easily offer your program some limited security.
Unfortunately, we haven't space to print the assembler listing, but anyone who sends in a stamped-addressed envelope is welcome to a copy.
|★ AMSTRAD CPC ★ DOWNLOAD ★|
|★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser...|
|QUE DIT LA LOI FRANÇAISE:|
L'alinéa 8 de l'article L122-5 du Code de la propriété intellectuelle explique que « Lorsque l'œuvre a été divulguée, l'auteur ne peut interdire la reproduction d'une œuvre et sa représentation effectuées à des fins de conservation ou destinées à préserver les conditions de sa consultation à des fins de recherche ou détudes privées par des particuliers, dans les locaux de l'établissement et sur des terminaux dédiés par des bibliothèques accessibles au public, par des musées ou par des services d'archives, sous réserve que ceux-ci ne recherchent aucun avantage économique ou commercial ». Pas de problème donc pour nous!
CPCrulez[Content Management System] v8.7-desktop/cache
Page créée en 116 millisecondes et consultée 60 fois
L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.