Introduction
Almost everyone who owns a computer will ask or be asked, “What sort of practical things can it do?” One of the most obvious practical applications is controlling external devices; however, few microprocessors or CPUs are designed to do this directly.
Most manufacturers of microprocessors offer devices called peripheral controllers which are integrated circuits designed to be compatible with their particular CPU. These controllers greatly simplify the task of interfacing external peripheral devices such as disk drives, terminals, and printers. Fortunately, the engineers at Sinclair Research Limited chose to design their microcomputer around the Z-80 CPU which is well-supported by several excellent peripheral controllers. One of these, the Z-80 PIO can be used in constructing a simple parallel interface for the ZX80/MicroAce computers.
Overview of the Z-80 PIO
The Z-80 PIO is a 40 pin integrated circuit designed to serve as a simple direct, TTL compatible interface between the Z-80 CPU and peripheral devices employing parallel data transfer. (See Figure 1.) Communication between the PIO and the CPU is accomplished by connecting the PIO data lines directly to the CPU data bus. The PIO is a two-port device. This means it can send and/or receive two sets of 8-bit parallel data. Control lines on the PIO select one of the two ports (B/A SEL), enable the PIO (CE), and allow the PIO to differentiate control words from data words (C/D SEL). Three other control lines (M1, IORQ, RD) insure proper timing sequences during CPU I/O operations. The bars over the signal names indicate that they are active low.
Each port has two control lines used to establish handshaking between the PIO and the peripheral device. These two control lines (RDY and STB) are sometimes, though not always, necessary to synchronize data transfer. In other words, one of these control lines, the RDY line, may be activated to tell a device which is sending data to the PIO, “Do not send data now. I am not ready… O.K., now I am ready. Send data.” The device may respond by activating the STB line, “O.K., here is the data. Get it now so I can do something else.” By using the handshake lines, communication is established between the PIO and the peripheral device resulting in an orderly, efficient transfer of data.
The PIO contains a number of internal registers used to control its operation. The most important is the 2-bit mode control register which can be programmed to select one of several operating modes on port A or port B.
The PIO may be operated in one of four modes, designated mode 0 through 3. Mode 0 is the output mode; all eight lines on the designated port are output to a device. In mode 1, the input mode, all lines on the port are input from a device. Mode 2 is the bidirectional mode and is restricted to port A. In this mode the handshake lines of port B along with the port A handshake lines are used to control the flow of data in both directions on port A. Mode 3, the control mode, is a hybrid of the input mode and the output mode; any line of the specified port can be designated as input or output. The control mode differs from the bidirectional mode in that once a line is designated as input or output, it stays in that condition and reprogramming is necessary to alter the direction of data transfer on that line. The handshake lines are not used in mode 3. A more detailed explanation of the Z-80 PIO operating modes can be found in references 2, 6 and 7 at the end of this article. This discussion is restricted to the control mode (mode 3). Other control registers internal to the PIO are used to store interrupt vector addresses, a distinguishing feature of the Z-80 PIO.

Let us now see how to construct and program a parallel interface for the ZX80/MicroAce computer, using this Z-80 PIO under non-interrupt, non-handshake control.
Construction of the Parallel Interface
Figure 2 shows a schematic diagram of the parallel interface. Port B is used for input to read the states of eight toggle switches (S1-S8) while port A is configured for output to drive eight light emitting diodes (D1-D8). Inverters are used to buffer the output port. The maximum output current capability of the PIO port data lines is about 1.5 milliamperes, not enough to drive an LED but enough to drive one TTL input or about four low power Schottky (LS) TTL inputs. The inverted system clock, Φ, is available at pin #6B on the back of the computer board. This signal is inverted again before being presented to the PIO. The handshake lines, STB and RDY, on each port are not connected. They are not needed because operating mode 3 will be selected. Since this application does not require interrupts, the IEI (Interrupt Enable In) line is tied high and the IEO (Interrupt Enable Out) is not connected.
Signals on the edge contacts of the computer board can be brought out through a cable using a modified 50-pin edge connector with 0.1 inch spacing between contacts (i.e., 3M part #3439-1000). The connector must be modified because it is closed ended and the computer requires an open ended version. The modification can be done with a sharp knife or a small saw.
The parallel interface circuit should be constructed on some sort of plug-in circuit board for easy inspection and modification. A high quality plug board such as Vector’s 4677-2DP works well since it provides an etched power and ground bus. Etched pads for mounting dual-inline-plug (DIP) integrated circuits are also provided. All ICs should be socketed. Interconnections can be made by soldering small wires to the pads or by wire wrapping or a combination of both. I recommend the latter method: solder all power and ground lines to the appropriate pins on the wire wrap IC sockets and wire wrap control, data and address lines. Locate 0.1uF capacitors at each IC package, connected between +5V and GND, to decouple power supply spikes and suppress high frequency oscillations on the supply.
A suitable enclosure for the interface can be purchased from most electronic supply companies. It should be large enough to house a separate power supply which is required for operation of the interface. The circuit and power supply could also be mounted on a flat piece of material, such as aluminium or plexiglass, “open face” style.
There are two basic options for handling the power supply. If you are planning to add more circuitry to your system later, you should buy or build a relatively high current power supply. A schematic for a +5V, 3A power supply is shown in Figure 3. The regulated portion can be used to power the interface. The unregulated portion can be used to run the computer if you want to eliminate the standard calculator-type power supply. However, if you are not planning to add more active circuitry and you are satisfied with the calculator-type supply, you can get by without a separate supply. You will need though, a +5V voltage regulator to regulate the rough +9-11V going to the computer down to +5V for powering the interface which requires a total of about 100mA.


PARTS LIST FOR POWER SUPPLY T1-18VCT 4A Transformer Radio Shack 273-1514 D1,D2- 400 PIV 3A Silicon diodes 276-114 R1- 1 ohm , 10% , 10 watt resistor U10- LM323 +5V regulator C1-4400 mF 25V electrolytic capacitor C2-10 mF 25V tantalum capacitor C3-0.1 mF ceramic disc capacitor



Figures 4, 5 and 6 are photographs of the author’s MicroAce system. The power supply provides unregulated +10V for the computer and 2K of on-board memory, and regulated +5V for the parallel interface plus an additional 6K of memory on the expansion board which is mounted just above the computer. The entire system is housed in a steel enclosure fitted with a hinged lid to which a standard size keyboard is mounted. The parallel port I/O (input/output) lines and handshake lines are brought out through two 16-pin DIP IC sockets. The cassette I/O connections are made available through two isolated phone jacks mounted on the front of the enclosure. Two RCA type phono jacks bring out the video signals: one for driving a standard video monitor and one for the RF modulator. Since the modulator is external to the computer, I use a TV as a video monitor for my other computer (Exidy Sorcerer).
Programming the PIO
Since the ZX80 version of Basic offers no direct means of communication with an I/O device, a driver subroutine coded in Z-80 machine language must be loaded into memory to operate the PIO. Data and control words can be passed from Basic to the driver routine through the POKE instruction. The routine is executed by calling it with a USR instruction. Some knowledge of the Z-80 CPU instruction set is helpful in understanding the driver routine.
Data is transferred from the CPU to the PIO by addressing one of its internal registers and writing to it by using one of the Z-80’s OUT instructions. We need only be concerned with four of the PIO registers in this application: port A control, port B control, port A data, and port B data. Each register is accessed by a unique address. I/O instructions are always associated with one-byte addresses comprised of the lower 8 bits of the address bus. A minimum of 3 address bits is required to operate the PIO. Normally, address line A0 is connected to the port select line (B/A SEL) of the PIO and address line A1 is connected to the control/data select line (C/D SEL). The six remaining bits of the address byte are decoded to select one of a number of I/O devices. Since the PIO is the only I/O device in this system, decoding is not necessary. As shown in the schematic (Figure 2), address line A7 is inverted and connected to the chip enable line (CE) of the PIO. Therefore, any address within the range 10000000B (B stands for binary) 11111111B will enable the PIO. The machine language driver routine (Figure 7) uses the “output immediate From Accumulator” instruction to transfer a byte of data to the PIO. This instruction is represented mnemonically
OUT(n),A
It transfers the contents of the Accumulator of the A register (one of the Z-80 CPU internal registers) to the I/O device addressed by n. The table in Figure 8 gives the addresses of the PIO internal registers and their significance when using the OUT immediate instruction in this configuration.
Address Meaning
Binary Hexadecimal* Decimal* Contents of Accumulator
interpreted as...
1XXXXX00 80 128 data - port A
1XXXXX01 81 129 data - port B
1XXXXX10 82 130 control - port A
1XXXXX11 83 131 control - port B
0XXXXXXX 00 0 PIO is not enabled,
no change
X means "don't care"; this bit can be 1 or 0. * These values assume that X=0.
[Figure: Figure 8.]
Label Location Machine code Mnemonic Comment
(Dec) (Hex)
0 0 00 NOP Do nothing.
1 0 00 NOP Do nothing.
2 62 3E LD A, CFH Load register A with operating
3 207 CF mode control word.
4 211 D3 OUT (82H), A Send control word to port A
5 130 82 control register.
6 62 3E LD A, 00H Load register A with data
7 0 00 direction word. All lines output.
8 211 D3 OUT (82H), A Send data direction word to
9 130 82 port A control register.
10 62 3E LD A, CFH Load register A with operating
11 207 CF mode control word.
12 211 D3 OUT (83H), A Send control word to port B
13 131 83 control register.
14 62 3E LD A, FFH Load register A with data
15 255 FF direction word. All lines input.
16 211 D3 OUT (83H), A Send data direction word to
17 131 83 port B control register.
18 62 3E LD A, 07H Load interrupt control word.
19 07 07
20 211 D3 OUT (82H), A Send interrupt control word to
21 130 82 port A control register.
22 211 D3 OUT (83H), A Send interrupt control word to
23 131 83 port B control register.
24 201 C9 RETN Return to Basic program.
25 62 3E LD A, 00H Load register A with the
26 00 00 contents of this location.
27 211 D3 OUT (80H), A Send contents of register A
28 128 80 to port A data register.
29 201 C9 RETN Return to Basic program.
30 33 21 LD HL,, 0000H Clear the HL register pair.
31 0 00
32 0 00
33 14 0E LD C, 81H Load register with port B
34 129 81 data register address.
35 237 ED IN L, (C) Read port B I/O lines. Load
36 104 68 data into register L.
37 201 C9 RETN Return to Basic program.
[Figure: Figure 7.]
Before data can be sent through a port, certain control words must be loaded into the internal registers of the PIO. This process is called initialization, and the code that does this is called the initialization routine. Several things must be done in the initialization process: the operating mode must be set, the data direction must be established, and the interrupt servicing must be taken care of. In this example the selection of mode 3 simplifies matters since the handshake lines are not used. The operating mode is selected by writing a control word with the four least significant bits set high. The two most significant bits determine the opterating mode and the other two bits are not used as shown in Figure 9.
Operating Mode Control Word
Binary Hexadecimal Decimal
Output 0 00XX1111 0 15
Input 1 01XX1111 4F 79
Bidirectional 2 10XX1111 8F 143
Control 3 11XX1111 CF 207
[Figure: Figure 9.]
When the control mode (mode 3) is selected for a particular port, the next control word sent to that port will define the direction of data transfer on each of the port’s I/O lines. Each line corresponds to a bit position in the control word; the most significant bit of the control word corresponds to the most significant I/O line. A high condition (1) means input and a low condition (0) means output. For example, suppose the control word FOH (H stands for hexadecimal) is used to select data direction on the port B. Lines PBO through PB3 would be set up for output while lines PB4 through PB7 would be set up for input.
Interrupts are handled very conveniently in this application; they are disabled by simply writing 07H (00000111B) to the control registers in both ports.
The PIO machine language driver routine listed in Figure 7 may be located in the unused spare portion of memory. However, in order to save the driver on cassette tape it must be located in the variables area of memory which is located immediately following the user Basic program. (When a program is stored on tape only the program itself, system variables and program variables are saved; not all of memory.) The two memory locations 16392 and 16393 contain the low byte and high byte, respectively, of the starting address of the variables area. This address will be referred to by the symbol ORG which stand for origin. Since the value of ORG depends on the size of the Basic program, all addresses in the driver routine must be relative to ORG.
The driver consists of three machine language subroutines, each ending with a return from subroutine instruction. The first routine initializes the PIO, setting up port A for output and port B for input. (Note: This is opposite to what is shown in the schematic diagram of the parallel interface. This means that the switches should be connected to port B and the inverter-buffer inputs should be connected to port A.) The interrupts are disabled in the last portion of the initialization routine. Another routine sends a selected byte of the port A output routine. It is altered by the execution of a POKE instruction in the Basic program. The third routine, the port B input routine, reads the data present at the port B I/O lines and stores the information in the L register. The HL register pair is cleared, set to 0, at the beginning of the routine. Storing the data in the L register is convenient because, when a USR function is called, the value of the HL register pair is returned. For example, suppose that during execution of a Basic program the statement LET X=USR(Z) is encountered, where Z is equal to the starting address of a machine language routine that merely loads the value 31264 in the HL register pair. The variable X would then be equal to 31264 after the completion of the machine language routine. If the HL register pair was not altered during the routine, X would equal Z.
The Basic program that calls the driver routine must provide a means of entering the machine language code. Getting the code into the variables area is done by setting up an array, i.e., allocating a portion of memory (large enough to hold the driver) with a DIM statement. Getting the proper code into the array can be done in several ways. The simplest is to enter the elements as signed integers. Be aware that the integers are stored in two bytes of memory, with the less significant byte first. This makes it very difficult to decipher the machine code. A more elaborate method involves writing a Basic monitor which would include a hexadecimal-to-decimal routine and a decimal-to-hexadecimal routine for entering and displaying one-byte entries in hexadecimal notation. This would require perhaps more memory than a 1K machine could accommodate, but inspection and modification of the machine code would be much easier. The Basic program in Figure 10 employs the former method for entering the code.
10 DIM M(20)
20 LET V=16392
30 LET ORG=PEEK(V)+PEEK(V+1)*256+2
40 LET AO=ORG+25
50 LET BI=ORG+30
60 LET MLA=AO+1
70 PRINT
80 PRINT
90 PRINT
100 PRINT "MENU"
120 PRINT "1) INPUT CODE"
130 PRINT "2) REVIEW CODE"
140 PRINT "3) PORT -A- OUT"
150 PRINT "4) PORT -B- IN"
160 INPUT A
170 LET A=A*1000
180 CLS
190 GOSUB A
1000 FOR I=0 TO 20
1020 PRINT I
1040 INPUT M(I)
1060 NEXT I
1080 RETURN
2000 FOR I=1 TO 20
2020 PRINT I+ORG,PEEK(I+ORG),1+ORG+21,
PEEK(I+ORG+21)
2040 NEXT I
2060 INPUT Z$
2080 RETURN
3000 PRINT "ENTER BYTE OUT"
3020 INPUT B
3040 POKE MLA,B
3060 LET X=USR(ORG)
3080 LET X=USR(AO)
3100 PRINT "ANOTHER BYTE ?"
3120 INPUT Z$
3140 CLS
3160 IF NOT Z$="" THEN RETURN
3180 GOTO 3000
4000 PRINT "HIT NEW LINE TO READ PORT B"
4020 INPUT Z$
4040 CLS
4060 IF NOT Z$="" THEN RETURN
4080 LET X=USR(ORG)
4100 LET X=USR(BI)
4120 PRINT "DATA AT PORT B...";X
4140 GOTO 4000
%
[Figure: Figure 10.]
This Basic program is menu-driven, giving the user the following options: entering the machine code, reviewing or listing the machine code, entering the byte to be sent out through the port A I/O lines, and reading the data present at the port B I/O lines. The variables used in the program are listed in Figure 11. The Sinclair version of integer Basic allows variables to be used as labels for GOTO and GOSUB statements. This feature is absent from many “expensive” versions.
Variable Meaning ORG Beginning of driver routine. AO Beginning of port A output routine. BI Beginning of port B input routine. MLA Location of byte to be output through port A. V,V+1 Points to the beginning of the variables area.
[Figure: Figure 11.]
Remember, when you get the driver routine loaded into memory, either by hand or by tape, do not press RUN. Instead, press GOTO followed by a number less than or equal to the lowest line number. GOTO 1 is safe.
Operation of the program is straightforward. The user is first shown a menu and is prompted to input a number between 1 and 4. If 1 is entered, the user is prompted to enter the signed integer elements which comprise the machine code. In this program the user should respond with the following integers. The screen is cleared after each entry.
Display Enter Display Enter
0 0 10 -32045
1 -12482 11 -31789
2 -32045 12 16073
3 62 13 -11386
4 -32045 14 -13952
5 -12482 15 33
6 -31789 16 3584
7 -194 17 -4735
8 -31789 18 -13976
9 1854 19 0
20 0
[Figure: Figure 12.]
Item 2 on the menu displays the contents of the array in which the driver routine is buried. Item 3 asks the user to input an integer, between 0 and 255, to be output through the port A I/O lines. For example, if the integer 255 is entered, all eight I/O lines will be set high; if 0 is entered, all lines will be set low. Selection of item 4 will read the port B data present at the port B I/O lines. If lines 0 and line are high and the others connected to ground, the decimal value “129” will be displayed. One way to exit the program is to break (hit the space key) while the screen is blank. Another way is to enter the letter “Z” when the computer is expecting an integer input, indicated by the appearance of the [LS] cursor in inverse video.
Applications
The number of possible applications for the ZX80/MicroAce with a parallel interface is limited only by the user’s imagination. With 16 I/O lines, interfacing devices such as A/D (analog to digital) and D/A (digital to analog) converters to the computer is a possibility perhaps once thought unachievable by many ZX80/MicroAce owners. Control of high voltage-current devices is also possible with relays and relay driver circuits.
With an A/D converter one could realize an inexpensive data acquisition system for monitoring and recording various quantities in the laboratory, in industry, or in the home. For instance, a temperature to voltage or temperature to current converter could be connected to the A/D for recording temperatures over a period of time at specified intervals. The calibration could be done in software to reduce hardware costs. Of course, a simple voltmeter would also be a useful application.
A programmable voltage source or power supply could be constructed by connecting a D/A converter to the parallel interface. Complex waveforms can be generated by cycling through a table of data words to be output by the D/A thus providing the user with a programmable function generator. An A/D converter can even be realized by using a D/A and a voltage comparator in a configuration known as a successive approximation A/D converter.

PARTS LIST FOR HIGH VOLTAGE-CURRENT CONTROL CIRCUIT R1- 220 ohm 1/4watt resistor R2- 2.2K ohm 1/4watt resistor U1- TIL113 optical coupler Q1- 2N3055 transistor D1- 1N4001 diode K1- Relay (Potter-Brumfield KRP11DG)
Figure 13 shows a circuit that enables computer control of high voltage-current devices such as televisions, coffee makers, and lights, (NOTE: Use caution if you decide to build this circuit. All high voltage wires and connections should be isolated and insulated from the user and the computer circuits). In this circuit an output line from one of the inverting buffers is used to drive an optically isolated relay driver circuit. An optical coupler with a darlington transistor output is used to isolate the computer circuits from any high voltages which may appear. The darlington output provides higher current driving capability than a standard, single transistor optical coupler but at the sacrifice of speed which is of no consequence in this application. Any comparable relay with a 12 VDC coil will also work. Be sure to stay within the current ratings of the contacts, however. Relays with other DC (direct current) voltage ratings will also work with appropriate resistor-value substitutions in the circuit.
As I said, the possibilities are limitless. You may decide to just let the computer turn on LEDs in random sequence. At any rate, I hope you will experiment and share your discoveries with others via SYNC.
Selected References
1. Artwick, Bruce. Microcomputer Interfacing. Prentice-Hall, 1980.
2. Barden, William Jr. The Z-80 Microcomputer Handbook. Howard W. Sams and Co., 1979.
3. Engineering Staff on Analog Devices, Inc. Analog-Digital Conversion Notes. Analog Devices, 1976.
4. Nichols, Elizabeth; Nichols, Joseph; Rony, Peter. Z80 Microprocessor Programing and Interfacing. Books 1 and 2. Howard W. Sams and Co., 1979.
5. Salt, Alger. “Build Additional RAM.” Syntax 2, no. 3 (March 1981).
6. Z80 Assembly Language Programming Manual. Zilog, 1977.
7. Z-80-PIO Z80A-PIO Technical Manual. Zilog, 1977.
See SYNC NOTES for a P.S. from the author.