Classic Computer Magazine Archive COMPUTE! ISSUE 88 / SEPTEMBER 1987 / PAGE 93

Amiga And 64 Ramdisk Files

Jim Butterfield, Associate Editor

Learn about the powerful technique of using ramdisk files on both the Amiga and the 64. Sample programs are included for both machines. The 1764 RAM Expansion Module and RAMDOS software are required for the 64.

When most people think of data files, they think of collections of information stored on magnetic media—disk or tape. But files may also be stored, at least temporarily, in the computer's RAM (Random Access Memory). Special software can allow the computer to simulate the actions of a physical disk drive using a portion of memory for storage. Because the memory storage behaves just like a physical disk, it's frequently referred to as a ramdisk. This article explains simple sequential ramdisk file techniques for both the Amiga and Commodore 64.

The Amiga's operating system has built-in support for ramdisks. If you have a Commodore 64, you must also have a 1764 RAM Expansion Module to use the ramdisk techniques discussed here. The module provides the extra RAM to hold the files. Additionally, the RAMDOS program supplied with 1764 must be installed in the computer's memory. See your 1764 User's Guide for information on connecting and using the RAM Expansion Module.

Temporary Files

Many programs create temporary files. Such a file often contains partially processed data—the program has worked through the data concerned, but cannot finish the job until it has completed another pass through the data.

As an example, consider a program which collects a series of examination scores for a class of students. Among other things, its job is to print each student's performance compared with the class average. The program can't print the comparison at the time it receives each student's score—it can't know the average score until all the grades have been entered. One solution is to use a temporary file to store the scores as they come in.

Another classic data processing task that benefits from temporary files is assembling a machine language program. The assembler program can do part of its work as it reads the source code, but it can't finish the job until all the source code has been read. The assembler can create a partially processed temporary file. Then, on the next pass through the data, it can read back this file and fill in any missing information.

In cases like these, the use of a ramdisk can dramatically improve program performance. Files held in a ramdisk can be retrieved much more quickly than files on a disk in a mechanical drive—even a highspeed hard disk drive. For a ramdisk, the computer need only read data from memory—something it can do with blinding speed. Reading data from disk, on the other hand, involves a number of mechanical tasks in addition to the electronic communications. Before the first byte can be read, the drive must start the disk spinning, determine the position of the file on the disk surface, move the read/write head to the proper track, and wait for the first sector of the file to spin by.

Since the ramdisk is so much faster, you may wonder why it isn't used exclusively for program storage. The answer is that ramdisks have one very significant shortcoming: Since all the information is stored in memory, everything in a ramdisk is lost whenever the computer is turned off, even if the power interruption is accidental or only momentary. For this reason, ramdisks are generally used only for temporary files containing information that needs to be retrieved quickly, but could be reconstructed easily, if lost. Physical drives are still the best choice for permanent file storage.

The sample programs at the end of this article illustrate the use of ramdisk files in a routine to calculate prime numbers. Type in the version for your computer and save it to disk. Program 1 is for the Commodore 64, and Program 2 is for the Amiga.

Prime Numbers

One definition of a prime number is one which is not evenly divisible by any lower prime number, assuming that the first prime number is 2. This kind of definition is known as recursive. The series of prime numbers begins like this: 2, 3, 5, 7, 11, 13, 17, and so on. A more technical definition of a prime number is a number that is divisible only by 1 and itself. The number 1 is included in the prime series when we use this definition.

The example programs use the recursive definition to generate prime numbers. We begin with 2. For all following integers, the program tries dividing all previous primes it has found into the current value. If none of them divide evenly, the new value is added to the list of primes. To speed up the routine, the range of trial divisors is restricted to those less than or equal to the square root of the number being tested.

As each prime number is found, it is placed in a temporary file called PRIMES. For the first prime, 2, the file must be opened for writing. Subsequent numbers are tacked onto the end of the file by opening it for appending.

The performance of the routine could be further enhanced by examining only odd numbers. All even numbers other than 2 are divisible by 2, and hence cannot be prime. (In the 64 version, add STEP 2 to the FOR-NEXT statement in line 200. In the Amiga version, change the statement p = p + 1 to p = p + 2, and insert p = 3 just before the WHILE statement.)

Getting Started

With the Commodore 64 and the 1764 RAM Expansion Module, you must install a program to support the use of the module's memory as a ramdisk. The required program, RAMDOS, is supplied on a disk that comes with the module. As part of the RAMDOS installation process, you'll be asked which device number to assign to the ramdisk. The primary floppy disk drive is always drive 8. Unless you already have a second floppy disk drive, device number 9 is a good choice for the ramdisk. Program 1 assumes the ramdisk has this device number. If you assign some other number, you'll need to change the 9 in line 120 to match the value you specified.

The Amiga includes built-in support for ramdisks; however, to conserve memory, the operating system does not normally set up a ramdisk when the system is booted. Instead, it waits until you first request access to a ramdisk, then allocates the memory at that time. In Amiga Basic, you can establish a ramdisk file simply by specifying the device name ram: along with the filename. Program 2, for example, uses the name ram:primes for the data file it creates.

Saving Your Work

The Commodore 64 has no built-in provision for copying files from ramdisk to a physical disk. If you want to transfer the data file created by the example program, PRIMES, to permanent disk storage, you must write a program to do the job. Here's one way to make the copy (as in Program 1, this example assumes that the ramdisk is set up as device 9):

100 Open 9, 9, 9, "PRIMES, S, R"
110 Open 8, 8, 8, "PRIMES, S, W"
120 GET#9, X$
130 S = ST
140 PRINT#8, X$;
150 IF S = 0 GOTO 120
160 CLOSE 8
170 CLOSE 9

If you start BASIC from the Amiga Workbench, you'll find a ramdisk icon on the screen when you return there after running Program 2. Double click, and you'll see an icon for a file named primes. This is the temporary file containing the prime number values. If you wish to keep this file, you must copy the program to a physical disk. Drag the primes icon onto the icon of the disk or drawer where you want it to go.

If you're using the Amiga's CLI (Command Line Interpreter) instead of the Workbench, typing INFO will show you there's a ramdisk mounted. Use DIR RAM: or LIST RAM: to see its contents. To move the primes file to a more permanent place, use the COPY command. If you want the data file's icon to be visible on the Workbench, you must also copy the associated file.

On the Amiga, if you write a file with the same name as an existing file, the old file is scrapped and replaced by the new one. In the same situation, the 64 would refuse your request—you must scratch the old file before you can write a new one with the same name. Attempting to use an existing name will result in a FILE EXISTS error message (badly spelled in the current version of RAMDOS).

The syntax for scratching Commodore 64 ramdisk files is exactly the same as that used for physical disk files. To remove the PRIMES data file, type the following lines in direct mode:

OPEN 15, 9, 15

Without RAM

Programs 1 and 2 also work with a regular disk drive. For the sake of comparison, you might want to run the programs in this manner. For the 64 version (Program 1), change the 9 in the OPEN statement in line 120 to an 8. For the Amiga version (Program 2), change the device name in the OPEN statement from ram: to df0:. You'll see the brakes go on as your program slows to the speed of the mechanical disk unit.

That's why using a ramdisk is so easy. No new or special programming techniques are required—and it sure speeds things up.

For instructions on entering these programs, please refer to "COMPUTE!'s Guide to Typing in Programs" elsewhere in this issue.

Program 1: Ramdisk Example—64 Version

FM 100 P = 2 : OPEN 15, 9, 15
XX 120 OPEN 1, 9, 2, "PRIMES, S, W" : GOSUB 2000
QK 130 GOSUB 1000
BD 140 CLOSE 1
GA 200 FOR P = 3 TO V
MF 210 F = 0
MG 220 Q = SQR(P)
BR 230 OPEN 1, 9, 2, "PRIMES, S, R"
DQ 240 INPUT#1, D
QR 250 F = (P - D * INT(P/D) = 0)
MC 260 IF D < Q AND NOT F AND ST = 0 GOTO 240
MQ 270 CLOSE 1
PQ 280 IF F GOTO 320
AA 290 OPEN 1, 9, 2 "PRIMES,S,A"
HC 300 GOSUB 1000
XS 310 CLOSE 1
CA 330 CLOSE 15
HP 340 END
PF 1000 PRINT#1,P
JS 1010 L = L + 1
FR 1020 PRINT RIGHT$("{2 SPACES}" + STR$(P), 4);
GG 1030 IF L > 8 THEN L = 0 : PRINT
FF 2000 INPUT#15, E, E$, E1, E2

Program 2: Ramdisk Example—Amiga Version

' Copyright 1987 COMPUTE! Publications, Inc.4
' All Rights Reserved.4
PRINT "Copyright 1987" : PRINT "COMPUTE! Publications, Inc." 4
PRINT "All Rights Reserved." : FOR t = 1 TO 10000 : NEXT 4
p = 2 4
INPUT "Primes to what value" ; v 4
OPEN "ram : primes" FOR OUTPUT AS 1 4
GOSUB putprime 4
CLOSE #1 4
WHILE p < V 4
p = p + 1 4
flag = 0 4
d = 0 4
q = SQR(p) 4
OPEN "ram : primes" FOR INPUT AS 1 4
flag 4
INPUT #1, d 4
flag = (p MOD d = 0) 4
CLOSE #1 4
IF NOT flag THEN 4
OPEN "ram : primes" FOR APPEND AS 1 4
GOSUB putprime 4
CLOSE #1 4
putprime : 4
PRINT #1, p 4
LIN = LIN + 1 4
PRINT USING "#####" : p ; 4