Contents / Reference section / Previous chapter / Next chapter / Index


35 Cassette files

This chapter summarises the facilities available for file handling using a cassette recorder. Refer to section 5 page 34 for an introduction to loading and saving BASIC programs.

A. Cassette motor control

Some cassette recorders have a "remote" or "automatic motor control" socket. This can be used with a switch on the microphone to start and stop the tape. If your recorder is of this type then the computer will be able to start and stop the tape automatically at the start and end of each BASIC program or section of recorded data.

If your cassette recorder does not have motor control then you will have to start and stop the tape manually. A light is provided on the keyboard to tell you when the tape should be running. This light is labelled "cassette motor". When it is on, the tape should be running.

The description which follows assumes that you have "automatic" or "remote" motor control.

B. Recording levels

Many cassette recorders employ "automatic record level". Recorders of this type do not have any "record level" controls. If your recorder does not have "automatic record level" then you will have to set the record level yourself. Set the control so that the recording level indicator is slightly below the "0dB" level or the red mark.

C. Playback volume and tone

It is important that the playback volume is set correctly. A detailed description of how to do this is given on page 12. The tone control should normally be set to "Maximum" or "High".

D. Keeping an Index of Programs

You will be able to record a large number of BASIC programs on a single cassette. However it is vital that the programs do not overlap on the tape. If they do then you will lose one of them. It is strongly suggested that you space the programs out along the tape. Use the tape counter to start recording at positions 000, 100, 200,300 and so on. But beware of recording on the blank leader tape - always wind it on a little first. In this way you will not only be able to find programs easily but you will leave good clear gaps between average sized programs.

If you forget what is on a tape then you can always use the command

*CAT

to obtain a "catalogue" of the tape. When you give the command *CAT (and press the PLAY button on the recorder) the tape will play through, and the computer will print a catalogue of all the programs onto the screen. The catalogue gives the program name, the number of blocks (rather like pages in a book) used to record the program and lastly the length of the program (the number of letters in the book). It also checks that the recording is readable and reports any errors. As the catalogue is building up on the screen you will often see something like this

SKETCH 02

This indicates that the computer has found a file called SKETCH and that it is currently checking block 2 of that file. The block number is given in hexadecimal not decimal numbers. Press ESCAPE at the end of the tape to get back control of the computer.

E. Saving a BASIC program

A program that you have typed into the computer's memory can be saved onto cassette tape in the following way:

1 Insert the cassette into the recorder

2 Type SAVE" PROG"

and press RETURN.

PROG is just an example of a "file-name"; file-names are explained under P of this section.

3 A message RECORD then RETURN will appear. Now use the fast forward and reverse buttons to position the tape at the correct place.

4 Press the RECORD and PLAY buttons on the cassette recorder.

5 Press the RETURN key on the computer to let it know that everything is now ready.

6 The computer will then record your program.

7 The tape will automatically stop when the computer has finished recording your program.

You can always abandon this process by pressing ESCAPE

F. Saving a section of memory

This will not be needed by most people writing BASIC programs. It is most often used to record sections of machine code programs. The process is very similar to that employed to record a BASIC program.

1 Insert the cassette in the recorder.

2 Type *SAVE "PROG" SSSS FFFF EEEE

and press RETURN

3 to 7 as for SAVE shown above.

SSSS represents the start address of the data, in hexadecimal (hex).

FFFF represents the end address of the data plus one, in hex. As an option the format +LLLL can be used in this position. The plus sign is followed by the length of the data, in hex.

EEEE represents the (hex) execution address of the data. If the program is reloaded into the computer using the command

*RUN "PROG"

then once loaded the computer will jump to the specified execution address. The execution address is optional and if it is omitted the execution address will be assumed to be equal to the start address.

Two examples may make the syntax clearer

*SAVE "patch" 6000 6200

*SAVE "match" 4C00 4CE9

G. Loading a BASIC program

A BASIC program saved on cassette tape can be loaded into the computer's memory in the following way:

1 Insert the cassette in the recorder.

2 Type LOAD "PROG" and press RETURN.

3 The message Searching will appear.

4 Now use the fast forward and reverse buttons to position the tape at the correct place.

5 Press the PLAY button on the cassette recorder.

6 The computer will give you the message Loading when it finds the correct program. It will then load it into its memory.

7 The tape will automatically stop when the computer has finished loading.

When loading a program the usual catalogue-type display will appear. The message Loading will appear when the correct file is found. If the load should fail for any reason a message will appear. See under S of this section for information on dealing with the problem.

H. Loading a machine code program

This will not be needed by most people using BASIC programs.

It is used to load special purpose programs. The process is identical to that used to load a BASIC program except that the command is

*LOAD "PROG" AAAA

AAAA represents the absolute load address. It is optional but, if included, will force the program to load at the specified address. It therefore over-rides the address given when the program was saved. The program will load but not run; control will return to BASIC.

Two examples may make the syntax clearer:

*LOAD "patch"

100 MODE 7: *LOAD "match" 7E80

I. Loading and running a BASIC program

The statement CHAIN allows a BASIC program to LOAD and RUN another BASIC program. It is particularly useful when there is a sequence of related programs, for example on the WELCOME cassette.

The command is used in exactly the same way as LOAD but with the word CHAIN substituted for the word LOAD.

1 Insert the cassette in the recorder.

2 Type CHAIN "PROG" and press RETURN.

3 The message Searching will appear.

4 Now use the fast forward and reverse buttons to position the tape at the correct place.

5 Press the PLAY button on the cassette recorder.

6 The computer will give you the message Loading when it finds the correct program. It will then load it into its memory.

7 The tape will automatically stop when the computer has finished loading and the computer will automatically run the program.

J. Loading and running a machine code program

A machine code program (not a BASIC program) can be loaded and run by using the statement

*RUN "PROG"

K. Using a cassette file to provide "keyboard" input

It is possible to get the computer to accept input from a cassette file instead of from the keyboard. In this case the cassette file would contain a set of commands, or answers to questions which a BASIC program would need. The command to force the computer to accept input from a file called "edit" would be.

*EXEC "edit"

To create a suitable cassette file you will need to use the BASIC statement BPUT# and not PRINT#, since the latter stores things in internal format. The command *SPOOL also creates suitable files - see page 402 for how to use it to merge programs.

L. Reading cassette data files

Data, as well as programs, may be recorded on cassette tape. This facility enables the user to keep records of names and addresses (for example) on tape for later use. Since the cassette tape can be started and stopped by the computer it also enables it to record results from experiments over many hours.

If the user wishes to read a data file then he must first "open the file for input". In the process of opening a file the computer will allocate a "channel number" to the operation. If we wished to read in a list of names recorded on a data file called NAMES then the following statement would get the channel number into the variable X.

100 X=OPENIN("NAMES")

Once a file has been "opened for input" data can be read in from the tape. This can be done in two ways: a chunk at a time (for example a whole name) or a single letter at a time.

Data is read in a chunk at a time by using the INPUT#X,A$ statement. This will read the first entry into A$.

To read a single character in use the function A=BGET#(X)

M. Testing for end of file

While reading data in it is useful to test to see if the end of the file has been reached. This is done with the function EOF#. For example

100 DIM B$(20)

110 Y=1

120 X=OPENIN("NAMES")

130 REPEAT

140 INPUT#X,A$

150 PRINT A$

160 B$(Y)=A$

165 Y=Y+1

170 UNTIL EOF#X

180 CLOSE#X

190 END

The program above reads up to 20 names off tape, prints them on the screen and then stores them in an array in memory. Of course if there were more than 20 names on file then the program would fail because the array can only hold 20 entries.

Note that EOF# is not implemented on release 0.10.

N. Storing data on tape

The data in the last example was read into an array in the computer. It could be then edited and the corrected version could be re-recorded on cassette.

The process of recording the data, on the cassette, consists of three steps: open the file for output, write out the data and then close the file. The example program records 20 entries back to tape from the array in memory.

200 X=OPENOUT("NEWNAMES")

210 FOR Y=1 TO 20

220 PRINT#X,A$(Y)

230 NEXT Y

240 CLOSE#X

250 END

Note that line 200 will make the computer issue the message

RECORD then RETURN

as it did when saving a BASIC program.

The CLOSE# statement will record any remaining data and then stop the recorder automatically.

O. Recording single characters on tape

Single characters (bytes) can be placed on tape using the command BPUT#. The following program stores the alphabet on tape. Note that the letter "A" has an ASCII value of 65 and the letter "Z" has an ASCII value of 90.

100 X=OPENOUT("ALPHABET")

110 FOR D=65 TO 90

120 BPUT#X,D

130 NEXT D

140 CLOSE#X

150 END

P. File-names

File-names on cassettes can be up to 10 characters long and can include any character except space.

Q. Responses to errors

If an error occurs during any of the following operations

SAVE

LOAD

CHAIN

*SAVE

*LOAD

*RUN

an error message and then the message Rewind tape will be printed on the screen. The user must then rewind the tape a short way and play it again. It is not usually necessary to replay the whole program, but only far enough to load the blocks causing errors.

If an error is detected during

BGET#

BPUT#

INPUT#

PRINT#

*EXEC

*SPOOL

the tape will stop and an error will be generated. This error can be trapped by BASIC in the usual way.

The user can escape at any time by pressing the key.

The error numbers generated by the cassette file system are as follows

216 Data Cyclic Redundancy Check (CRC) error

217 Header CRC error

218 An unexpected block number was encountered

219 An unexpected file name was encountered

220 Syntax error - for example an illegal *OPT statement

222 An attempt was made to use a channel which was not opened

223 End of file reached

R. Changing responses to errors

If the user wishes to change the way the computer behaves when it detects an error during a cassette file operation (e.g. LOAD or SAVE) then this can be done with the *OPT statement

*OPT by itself resets error handling and the "message switch" to their default values. Default values are given later.

*OPT 1,X is used to set the "message switch" which controls the detail of the message.

X=0 implies no messages are issued

X=1 gives short messages

X=2 gives detailed information including load and execution addresses.

*OPT 2,X is used to control the action that the computer takes

when it detects an error during a cassette file operation.

X=0 lets the computer ignore all errors though messages may be given.

X=1 the computer prompts the user to re-try by re-winding the cassette.

X=2 will make the computer abort the operation by generating an error.

*OPT 3,X is used to set the interblock gap used when recording data using PRINT#, and BPUT#. The gap on SAVE is fixed. The value of X determines the gap in 1/10 seconds. If X is set to greater than 127 then the gap is set to the default value.

The default values for the *OPT command are:

For LOAD, SAVE, CHAIN, *SAVE, *LOAD, *RUN

*OPT 1,1 (short messages)

*OPT 2,1 (prompt for re-try)

*OPT 3,6 (0.6 second inter block gap)

For BGET#, INPUT#, BPUT#, PRINT#

*OPT 1,0 (No messages)

*OPT 2,2 (Abort on error)

*OPT 3,25 (2.5 second inter block gap)

S. Cassette tape format

The format of each block of data stored on cassette is given here for those who wish to produce tapes on other machines that may be read into the BBC computer.

5 seconds of 2400Hz tone

1 synchronization byte (&2A)

Filename (1 to 10 characters)

1 end of filename marker byte (&00)

Load address of file, 4 bytes, low byte first

Execution address of file, 4 bytes, low byte first

Block number - 2 bytes, low byte first

Data block length - 2 bytes, low byte first

Block flag - 1 byte

Spare - 4 bytes currently &00

CRC on header - 2 bytes

Data - 0 to 256 bytes

CRC on data - 2 bytes

Notes

  1. Each data block has its own header as shown above
  2. Load and execution addresses should have the top two bytes set to &FF in 8 bit machines
  3. Bit 7 of the Block flag is set on the last block of a file
  4. CRC stands for Cyclic Redundancy Check.

The header CRC acts on all bytes from the filename to the spare bytes inclusive. The data CRC acts on the data only. CRCs are stored high byte first and are calculated as follows. In the following C represents the character and H and L represent the high and low bytes of the CRC.

H=C EOR H

FOR X=1 TO 8

T=0

IF (bit 7 of H=1) THEN HL=HL EOR &0810: T=1

HL=(HL*2+T) AND &FFFF

NEXT X

The above algorithm is not a BASIC program!

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001