<<

Behavior Research Methods& Instrumentation 1978, 10 (4),527-530 Hardware and Software A floppy disk for the 8080 and Z80

KARL W. SCHOLZ AdvancedInteractive Systems, 8216 Pickering Street, Philadelphia, Pennsylvania 19150

Techniques for the design and implementation of a floppy for the 8080A or ZSO are described. Implementation details are illustrated through reference to 80S, a prototype Small Operating System which runs with a single IBM· floppy drive on an 8080. Techniques for floppy disk communication, disk design, and system resource management are described and illustrated.

For years, journals such as Behavior Research haps compiler(s) for higher level languages such as Methods & Instrumentation have presented count­ BASIC,PASCAL, or . less descriptions of the successes achieved by com­ The list of desirable features can be greatly extended, puterized research laboratories using equipment resulting in the specification of a complex but compre­ costing tens or hundreds of thousands of dollars. hensive system. However, for a small research laboratory However, as both federal and institutional funds such a system can well represent an unnecessary extrav­ become increasingly difficult to obtain, a growing agance. The following discussion will be restricted number of laboratories are forced to consider economic to the features listed above. alternatives to total computerization, such as pur­ The development of the floppy disk is the single chasing used equipment or -based technological achievement which makes possible the systems. Those who choose the microprocessor route creation of a functional but truly inexpensive laboratory quickly learn that, although inexpensive hardware system. In view of this, the following dis­ is readily available, appropriate software is scarce and cussion centers primarily around techniques for orga­ difficult to obtain. In a sense, the microprocessor nizing and communicating with a floppy disk. user of today is in a position similar to that of the minicomputer user of a decade ago, in that both find rns FLOPPY DISK AS A SECONDARY the only practical source of appropriate software is STORAGE MEDIUM internal. The present summarizes techniques central to the design of disk-based system software A floppy disk is a disk of flexible Mylar 5~ or 8 in. in and illustrates these techniques using SOS, a Small I diameter which is coated with a magnetic emulsion sim­ Operating System, for the 8080 or Z80 micropro­ ilar to that on conventional recording tape. Disk drives cessor with at least 8K of memory and one floppy are available for both sizes which provide either a single disk. read-write head or multiple heads for double-sided re­ cording; can be written using either a single- or SYSTEM REQUIREMENTS double-density format. Although double-density and double-sided formats are growing rapidly in popularity, A disk-based operating system should include as the most widely used is the 8-in. disk, single-density a minimum the following features. (1) A bootstrap and single-sided. This type disk will be used in the loader, often ROM resident, to permit fast loading following illustrations. of the system when the machine is powered up. (2) Disk The disk's surface is formatted into a number of I/O primitives (e.g., subroutines) that permit concentric "tracks," and each track is divided into a from and writing to the disk. (3) A disk directory number of "sectors." In order to permit disk transport­ ("table of contents") through which any information ability from system to system, many manufacturers stored on the disk can be located. (4) Utility functions conform to a commonly referred to as the for system and data file maintenance such as DIREC- "IBM 3740" format, which provides 26 sectors of 128 TORY (display the "table of contents"), DELETE for each of 77 tracks. Disks formatted in this way (to remove an unwanted data file), COPY (to copy provide 2,002 sectors, or 256,256 bytes of usable an existing data file), RENAME (to alter the name storage. of an existing data file), GET (to load a data file to Disk drives are interfaced to the computer via a memory), and SAVE (to create or update information "controller" which attends to all the details of for­ on the disk from information in memory). (5) Utility matting, error detection, and parallel-serial conversion. programs such as a text editor, an assembler, and per- Programming, therefore, requires positioning the disk 527 528 SCHOLZ head to the desired track, locating the desired sector, sector and contains the correct track and sector address. and transferring one or more sectors of information On each transfer, the controller attempts to verify to or from the disk. head position by inspecting this header, and sets an error status bit if the attempt fails. THEDISKDRNER Figure 1 illustrates the code in 8080 assembly lan­ guage for a typical disk driver that includes error The read and write routines (frequently referred to recovery procedures. It can be entered at either the as the disk "driver") are central to the operation of READ or WRITE entry points, and expects to find any disk-based operating system. Since all disk I/O the desired sector address (between 1 and 2,002) in operations are mediated by the driver, it should be the BC register, the desired number of sectors to be coded to maximize ease of use and should include transferred in the DE register, and the memory facilities for error detection and recovery. Although address in the HL register. Although additional error the disk is accessed by both track number and sector processing can be included, this driver is entirely ade­ number, the sector/track distinction should be of quate for all but the most critical applications. concern only to the driver, not to the user. A user need only specify a sector relative to the start of the DISK ORGANIZATION disk or the start of a desired data file. The driver should calculate the correct track and sector within In the simplest case, disks serviced by a simple that track. operating system need to be divided into three distinct Dispite the extreme reliability of the floppy disk, sections: the system loader, the directory, and the user errors occasionally occur. The IBM format includes storage area. The term "system loader" refers to a several cyclic redundancy checks (CRCs, similar to simple routine stored in a known location on a disk a check sum) for each sector transfer. The controller (usually Track 0, Sector 1) which on execution loads generates a CRC when a sector is written and checks the monitor (the nucleus of the operating system). it when that sector is read. If the CRC check reveals If the disk contains only the three sections mentioned, an error, a status bit is set by the controller which the system loader is required to search the directory is accessible to the software. Furthermore, the IBM for the name of the me containing the system monitor format includes a sector header that precedes each and then load it. For SOS, the system loader is con-

SIIIPLB DIlII: IIrUT/OUTPUT Diua 8PLIT - coa, Set ADDI TO SIC , Til ~LIT: D ;S"I DB (BU: CIIT) DC1 8 ;UloS 0 - 2001 . 1IllU1T&S: LII D.080Dll ;SIT 0.8 , E.13 UGO IQU 0'88 ; COIlIWID REG DIYLP: - 1.8 ;IS E .0T. 81 Ill' RIG2 IIllU OPlB ;SECTOI REG !D8 S ;SU8TRleT E 'ROIl 8 REG3 IQU 0'811 ;D1TA REG JC ROSU8 ;JUMP. C SIT. If IIOT REG_ IQU OPCS ; COITROL RIG Ill' 8,1 ;SU8T DYSR PROM D'ND AD BQU 0888 ; IIAD CCHUID IIOSUB: QIC ;COMPL Ie' (QUOTIEIIT) IR IQU OlBH ; IIITE COIlIWID Ill' I,C ; 'ITCH QUOTIEIIT 1II IQU 016H ;SIIl ClHWID IlL jSHIFT 111 'e' BIT ; lilY C,l ;lID RISTORE. . RIllD 18THt POlIT lilY 1.8 ;'ITCH DIVISOR RIllD: CALL SPLIT ;SIT SEC. , TRI. UL ;SHIn II RElT 8IT C1LL SEll ; POSITIOR BIllD Ill' 8,1 ;lID RESTORE. BLUP1: SIU.J) USTH ;SlYE MEIl ADDR DCI 0 ; DEC l.OOP COUNT lilY 1.8 ;SEC. TO ICC JIZ DIYLP ;LOOP '01 cn 0' 8. OUT REG2 ;IMIT TO CnRLLEI III 8 ;SIT SICT RlIOE 1-26 HYI I.RD ;UAD COIIWID POP D ; RESTORE 8U: CNT OUT REGO ;START 1lEAD. RET ;lID RETURI BLUP2: 11 REG_ ;IUT FOR 8ITE RDt , SCIRC - IRC SEC CIT , TIST OONE ORI I ;Sir ,UOS SCllC: 11 RlGO ; RIllD STATUS RIG JP IlDOIIE ;JUMP If OORE. III OFDB ;1llS1 ERROR BITS II RIG3 ; ELSE Gil 8ITE JZ aOERR ; JUMP IF ALL 01. Ill' H.l ;STORE II BlK IJILI) USTB ;ELSE 81Cl UP tiL III B ; IICR MEIl lDDR RET ; liD RITRt RD/WR. JMP BLUP2 ; LOOP TIL SIC. DOli HOERI: DC1 D ; DIC SEC COURT RDOIIE: CALL SCIIC ; lIC SIC , DIC CIT Ill' I,D ; TIST\ FOR ZIRO JMP BLUPI ;l.OOP TIL CIIT • O. ou I ;8OTH 8ITIS JRZ HORI ; JUMP URLESS OOU , WRITE BIITRI POINT POP D ;DISClRD 1 RITURI WIITE: CALL SPLIT ;SIT SIC •• TRI. RET ; RETURI TO CALLER. CALL SEll ;POSITION BElD IIllRE: IRR 8 ; ON TO lilT SIC ILUP1: SHLD Ll5TB ;snl MEIl lDDR HOY 1.8 ;OET SIC lillY 1,8 ;SIC. TO ICC CPI 27 ;TUCI 00111 OUT RlG2 ;IMIT TO CRTBLLIR RC ;RITURR If lOT HVI A,WI ; WIITI COIII1ND MYI 8,1 ; ELSI IISIT TO 1 OUT RlGO ; START WRITI lIR C; liD lICR TUCI WLUP2: IR RIG_ ; WlIT 'OR 8ITE RDI , Sill - POSITIOR DISK BllD OU I ;SIT FL1GS SEll: HOY I,C ;OIT DlSlllED TRI JP IIDONE ;JUMP IF OOIE. CPI 78 ;resr OYIIlFLOW HO' I,H ; ELSE Gil 8ITI JNC ERROR ;IRROI I' SO OUT RIG3 ; WRITE IT TO DIlII OUT RlG3 ;IMIT TO CRTBLLER 181 B ; I1CR HEll lDDR MYI I,SE ;SBEI COIIWID JMP BLUP2 ;l.OOP TI~ SEC. DOli OUT REGO ;STAIT SEll WOOIE: CALL SCIIC ; IIC SEC , DEC CHT IN REG_ ;WlIT TIL DOli. JMP BLUP1 ;LOOP TIL CRT. O. RET ;00 REruU. Figure 1. A sample floppy disk I/O driver. The driver is entered with the desired in the Be register, the total sector count in the DE register, and the starting memory address in the HL register. FLOPPYDISK OPERATING SYSTEM 529 siderably shortened by reserving a fourth distinct sec­ UTILITY FUNCTIONS tion of the disk for the monitor. The entire first track (26 sectors), minus the one sector assigned to the Any disk operating system includes a collection system loader, is reserved for this purpose. SOS reserves of utility functions that perform necessary directory the next three tracks (78 sectors) for the directory and and file maintenance. Utilities fall into two general uses the remainder of the disk for file storage. On classes, those executed by keyboard command and power-up, a ROM-resident bootstrap loads the system those executed by call from a running program. loader, which in turn loads the monitor and transfers Keyboard-executable utilities presuppose the control to it. existence of a keyboard monitor that accepts and parses The disk directory is used as an index, or table commands entered at the keyboard and directs them of contents, through which files on the disk are located. to the appropriate utility routines. For example, SOS A minimum disk system must retain at least the file accepts commands consisting of a directive followed name and the starting location (a sector address) in by an optional list of arguments. The parser performs each directory entry. More complex systems also retain the following steps, in order. (1) The directive name is creation dates, last-access dates, file version numbers, saved. (2) A pointer to the argument list is stored in file types, and additional relevant statistics for each a location accessible to any program. (3) A short table file. In SOS, each directory entry contains the file of utilities included in the monitor (GET, SAVE, and name, its starting sector address, its size in blocks, its ) is searched; if the directive is found, the memory origin address, and (if the file is an executable appropriate utility is executed. (4) If the directive program) its entry address. Files are named with a is not found in the monitor, the disk directory is one- to six-letter file name and a two-letter file type searched for an executable program of that name; (such as TX for text or DT for data). if found, it is loaded and executed. On execution, such For any disk system, it is essential that directory programs can access the optional argument list by manipulation routines prohibit the accidental duplica­ using the pointer saved in Step 2. tion of a file name and provide a means of managing This technique has the advantage of providing exten­ disk space which becomes fragmented as old flles are sibility. Any time a user requires an additional directive, deleted and new ones are created. Among the most he need only program the service routine for that elegant methods for avoiding fragmentation is the directive and store it on the disk, and it effectively technique of dynamically allocating disk sectors as becomes a new SOS command. In operation, the dis­ needed, keeping track of their use with "bit maps" tinction between monitor-resident and disk-resident or linked lists. A second method involves creating a utilities is transparent to the user. The SOS system "dummy" directory entry for each sector or collection includes a collection of frequently used disk-resident of sectors that has been freed by virtue of a me dele­ utilities coded in this manner (for directory listing, tion. The third and simplest technique (used by SOS) , me copying, file renaming, and disk is to periodically execute a "packing" routine that packing). systematically collects all fragmented space and The second class of utility functions are those arranges it contiguously at the end of the disk. Packing accessible from within an executing program. As a bare need be performed only when required storage ap­ minimum, a small operating system should provide proaches the capacity of a . facilities for program manipulation of disk files, In SOS avoids both me name duplication and unnec­ addition, a small system might include facilities for essary fragmentation by performing two passes over frequently used operations such as programmed timed the directory whenever a new me name is added. On delays, real-time clock support (for reaction time mea­ the first pass, the entire existing directory is searched; surement), ASCII-decimal conversion, and any other if an old copy of the me name is located, it is deleted. operation that is unique to a particular application. On the second pass, the first area of the disk large SOS provides a carefully protected facility for program enough to house the new me is used for its storage. manipulation of disk files through three subroutine The system designer is cautioned to avoid attempt­ calls. The subroutine OPEN associates a file name ing to save time by maintaining a portion of a disk with a "logical unit number" (an integer between directory or sector mapping information in RAM. 1 and 15). The subroutines READ and WRITE are used Floppy controllers typically provide no mechanism to transfer one or more sectors from/to the file associ­ for notifying a program if a disk is changed. Hence, ated with a particular logical unit number. The tech­ it is possible to swap disks and create a situation where nique of associating a file name with a number and directory or sector mapping information for the wrong performaing disk I/O with respect to the number is disk is in RAM, potentially leading to destruction used frequently on larger systems. Among its many of all information on the disk. Some commercially advantages, it permits the coding of a program with­ available operating systems have actually made this out explicit knowledge of those flles on which it will mistake. operate when executed. 530 SCHOLZ

4 In SOS, flle communication is mediated by a table i PILICOITIOL IILOCl PIOCUSIIG , referred to as a "file control ." The FCB is modi­ SPLIT' IlC1 B ;IISIT IAIGI 0-2001 PUSH a ;SlYl IIIIIORI lDDR fied by the OPEN routine and utilized by the READ PUSII 0 ;SAIE SECTOI coun Ill' I,B ;fllT PILI lOOER and WRITE routines. In both cases, the logical unit PUSH PSlI ;&10 snl IT number is used to index the FCB. When OPEN is &111 7 ;_ OUT FILE IOOER Ill' B,l ;BC 110I IlQUlLS IEL1TIVE SEC IUM executed, it performs a directory search to locate POP PSII ;PILE lUMBER BACE TO ICC &111 OPOB ;LIFT I1B8LI IS FILE IUMBEI the appropriate file. If so specified (by appropriate IWI ; ACC IfOlj IlQUlLS FILE NUM TIM&S 8 LXI a,FCB ,POIiT IlL TO PILE. COITROL BLOCI START arguments to OPEN), a new empty file is created. ADD L ,ADD FILl I_R TIM&S 8 (SlMCI lACS lilY L,l , FCB IITRI IS 8 BITES LOIG) In either case, the file's starting and ending sector ~IC lOCI ,SlIP UILISS C1RRY addresses are inserted into the correct FCB entry for III a ;PIOPIOUE CUll lOCI. IIOY E,M ;GIT FILl STARTIIG SEC AD FROM rcB TO DI use by the READ and WRITE routines. FCB entry 0 111 a HOY D," is always open to the entire disk (for use by the system 111 a ICIIG ;IlOYE TO BL utility routines), thus leaving 15 logical unit numbers DAD B ; ADD RELATIVE SEC AD TO STARTIIG AD IIOY B,a ; ARD RISTORE II Be. for use by user programs. Figure 2 illustrates the code 110' C,L used by the central disk driver to process all file refer­ ICIIG . ,FCB IITRI START PLUS 2 blCE TO IlL 110' E,M ,Gn 2'S COMPLEIlEIIT EIDIIG ADDI TO DE ences through the FCB. On entry, it assumes that the In a 110' D,M logical unit number is in the left 4 bits of the BC regis­ III B 110' M,C ;8lVE ADDR FOR THIS RElD OR WRITI 11 r .C.B. ter, and the desired sector relative to the start ofthe file 111 B lilY M,B is in the remaining 12 bits. This routine is integrated POP a ,BLOCE coun B1CI TO IlL PUSII B ;LIlYE 01 STICE into the SOS disk driver illustrated in Figure 1, DAD B ;HL _ IlllUlLS LAST SEC TO BE Rl&D 01 WlITTII DlD 0 ;SU8TUCT lIDIIG ADDBISS beginning at the SPLIT. POP 0 ; USET DE TO SIC COUIT The complete SOS system includes three additional POP B ; IISTOIE 11III011 lIlDRISS TO IlL JIC DIfD8 ;JUIlP UILISS IElJ>-PlST-IlOF nlOR utilities: a text editor, an assembler, and a BASIC POP D ; DISClID 1 IETOU FROM STACE lit ; liD IETOU WITI CI SIT TO IIDICUI 1IIIlO1. interpreter. These utilities have been coded to utilize DIVDI' the disk structure described above and to provide the Figure 2. File control block processing. This routine, which tools necessary for most applications. All three utilities is an optional component of the disk driver, processes a file number in the left 4 bits of the BC register. It expects to find have been carefully optimized to use as little RAM as the file control block beginning at symbolic location FCB. possible (the editor and assembler are each small enough to execute from a single lk- ROM). A been explored by implementing SOS, a small operating detailed description of these three utilities is beyond system for an 8080 or Z80 with at least 8K of RAM the scope of this paper. and a single IBM-format floppy disk. Copies of the system have been evaluated on both 8080 and Z80 SUMMARY processors using the Tarbell , and, from all reports, it has served as an entirely adequate develop­ We have attempted to summarize design considera­ mental vehicle despite its extreme simplicity. tions and implementation techniques for a small general­ purpose disk operating system. The techniques have (Accepted for publication June 13, 1978.)