Microsoft Word - Revised Microcomputer & Interfacing - Repai
Microsoft Word - Revised Microcomputer & Interfacing - Repai
1 a
, s e u e
y s
r e
II
Acknowledgement
I am very much thankful to the Department of Electrical and Computer Engineering for allowing me to write a teaching material on Microcomputers and Interfacing. I am thankful to the reviewers for reviewing the teaching material. I am also very much thankful to my wife for encouraging me and assisting me to write this material. In addition, I would like to thank Mr. Muhammed and Mr. Alem who helped me in writing and editing this document and to Electrical and Computer Engineering Department students for their help in improvement of the material. Furthermore, I am thankful to all the staffs of Electrical and Computer Engineering Department for their help during the completion of this teaching material.
Author
III
Preface
This teaching material is prepared to serve as a valuable teaching material and study guide to the students of Electrical and Computer Engineering Department and to the Computer Engineering stream students in particular to support them in their self-study as well as it could also be used by practicing engineers and technicians. The purpose of this book is to give an in-depth study of both the hardware and software included in microcomputer systems. Although the concepts considered are general in nature, the discussion is based on a particular microprocessor, the Intel 8086, and its associated supporting devices and software. Of the more popular 16-bit single chip processors, the 8086 was chosen because it provides a varied instruction set that includes extensive arithmetic operations and string manipulations and has several advanced architectural features designed to support multiprocessing. It is assumed that the reader is familiar with at least one of high level language and has knowledge of elementary logic. This book consists of: A review of general microcomputer modules or subsystems and evolution of microprocessor. The internal architecture of microprocessor and instruction sets of the 8086 including the arithmetic, logic, and control transfer instructions. Examples are included to enhance the readers understanding of the applications of various instruction set. Pin descriptions of the 8086 microprocessor and bus cycle timing are discussed. The common types of internal memory and interfacing of the input output and memory are described. 8086 peripheral controls: programmable peripheral interface 8255 (PPI); universal synchronous/asynchronous receiver/transmitter 8251a (USART); programmable interrupt controller 8259A (PIC); and programmable interval timer 8254(PIT).
The teaching material microprocessor and interfacing create an environment for selfreliant teaching and learning practices. Besides, it is based on the new curricula and lecture notes for undergraduate course on microcomputers and interfacing that I have been teaching at Mekelle University, Electrical and Computer Engineering for the past four years. Fesseha Manna
IV
Table of Contents
Pages
1.1 INTRODUCTION TO INTEL FAMILY OF MICROPROCESSOR.................................................... 1 1.2 STORED PROGRAM COMPUTERS .......................................................................................... 1 1.3 EVOLUTION OF THE INTEL MICROPROCESSORS ................................................................... 2 1.4 OVERVIEW OF MICROCOMPUTER STRUCTURE AND OPERATION ........................................ 3 1.4.1 MEMORY .................................................................................................................... 3 1.4.2 INPUT/OUTPUT PORTS ............................................................................................... 3 1.4.3 CENTRAL PROCESSING UNIT ...................................................................................... 3 1.4.4 BUSES ......................................................................................................................... 4 1.4.5 HARDWARE, SOFTWARE, AND FIRMWARE ................................................................ 4 Chapter Two ................................................................................................................................ 5 8086 CPU ARCHITECTURE ........................................................................................................... 5 2.1 INTRODUCTION TO 8086 CPU ARCHITECTURE...................................................................... 5 2.2 FETCH AND EXECUTE ............................................................................................................. 7 2.3 PROGRAMMING MODEL ....................................................................................................... 9 2.4 SEGMENTED MEMORY ........................................................................................................14 2.5 MEMORY MAP .....................................................................................................................15 2.6 SEGMENT REGISTERS ..........................................................................................................16 2.7 LOGICAL AND PHYSICAL ADDRESS.......................................................................................18 2.8 ADVANTAGES OF SEGMENTED MEMORY ...........................................................................20 Chapter Three............................................................................................................................21 INSTRUCTION SET OF THE 8086 ................................................................................................21 3.1 DEFINING MEMORY LOCATIONS .........................................................................................21 3.2 ADRRESING MODES .............................................................................................................21 3.2.1 IMMEDIATE ADDRESSING MODE .............................................................................22 3.2.2 REGISTER ADDRESSING MODE .................................................................................22 3.2.3 DIRECT ADDRESSING MODE .....................................................................................22
3.2.4 INDIRECT MEMORY ACCESS ADDRESSING MODES ..................................................22 3.2.5 STRING ADDRESSING MODE ....................................................................................23 3.3 DATA TRANSFER INSTRUCTIONS .........................................................................................25 3.4 SPECIAL DATA TRANSFER INSTRUCTION .............................................................................26 3.5 SEGMENT OVERRIDE ...........................................................................................................29 3.6 STRING INSTRUCTION..........................................................................................................30 3.7 REPEAT PREFIX.....................................................................................................................34 3.8 LOGICAL INSTRUCTIONS ......................................................................................................35 3.8.1 BOOLEAN FUNCTION................................................................................................35 3.9 SHIFT AND ROTATE INSTRUCTIONS.....................................................................................39 3.10 ARITHMETIC INSTRUCTIONS .............................................................................................39 3.10.1 ADDITION AND SUBTRACTION INSTRUCTION........................................................39 3.10.2 MULTIPLICATION AND DIVISION ............................................................................42 3.11 TRANSFER OF CONTROL INSTRUCTION .............................................................................45 3.11.1 Unconditional jump instruction .............................................................................45 3.11.2 Conditional jump ....................................................................................................48 3.12 Loop instructions ...............................................................................................................49 3.13 Push and pop instructions .................................................................................................51 3.14 Call and return instructions ...............................................................................................52 3.15 Software interrupts ...........................................................................................................54 3.16 Processor control instruction ............................................................................................57 Chapter Four..............................................................................................................................60 THE 8086 CUP MODULE ............................................................................................................60 4.1 8086 CPU READ AND WRITE BUS CYCLE .............................................................................60 4.1.1 BUS CYCLE TIMING ...................................................................................................60 4.2 8086 CPU PIN DESIGN/DESCRIPTIONS ................................................................................62 4.3 GENERATING THE 8086 SYSTEM CLOCK & RESET SIGNALS ................................................. 67 4.3.1 COLD STARTING THE 8086 ....................................................................................68 4.4 MICRO COMPUTER BUS-TYPES AND BUFFERING TECHNIQUES ..........................................69 4.5 THE 8086 MINIMUM MODE CPU MODULE .........................................................................70 VI
Chapter Five ..............................................................................................................................71 MAIN MEMORY SYSTEM DESIGN ..............................................................................................71 5.1 TYPES OF MAIN MEMORY ...................................................................................................71 5.2 INTERFACING MEMORY ......................................................................................................73 5.3 ADDRESS DECODING TECHNIQUES .....................................................................................76 5.3.1 THE 3-TO-8 LINE DECODER (74LS138) ......................................................................76 Chapter Six.................................................................................................................................78 BASIC INPUT AND OUT PUT.......................................................................................................78 6.1 PARALLEL I/O .......................................................................................................................78 6.2 Serial I/O ..............................................................................................................................80 6.3 PROGRAMMED INPUT OUTPUT ..........................................................................................83 6.4 INTERRUPT DRIVERS I/O ......................................................................................................86 6.5 DIRECT MEMORY ACCESS (DMA) ........................................................................................87 Chapter Seven ...........................................................................................................................90 PERIPHERAL CONTROLLERS FOR THE 8086 ...............................................................................90 7.1 THE 8255 POGRAMMABLE PERIPHERAL INTERFANCE (PPI)................................................ 90 7.1.1 INTERFACING THE 8255 ...........................................................................................92 7.1.2 SPECIFYING THE CONTROL WORD (Mode Selection)...............................................94 7.1.3 Bit Set/Reset .............................................................................................................96 7.1.4 Mode 0: Simple Input or .Output. ............................................................................97 7.1.5 MODE 1: STROBED I/O .............................................................................................97 7.2 THE 8251A UNIVERSAL SYNCGRINOUS/ASSYCHRNOUS RECEEIVER/TRNASMETER (USART) .................................................................................................................................................105 7.2.1 INTERFACING THE 8251A .......................................................................................106 7.2.2 GENERATING THE ASYNCHRONOUS BAUD RATE CLOCK .......................................109 7.2.3 PROGRAMMING THE 8251A FOR THE ASYNCHRONOUS MODE............................110 7.2.4 PROGRAMMING THE 8251A FOR THE SYNCHRONOUS MODE ..............................112 7.3 PROGRAMMABLE INTERRUPT CONTROLLER (8259A) ....................................................... 113 7.3.1 INTERRUPT SEQUENCE ...........................................................................................117 7.3.2 PROGRAMMING THE 8259A ..................................................................................119
VII
7.3.3 INITIALIZATION COMMAND WORDS (ICWS) ..........................................................119 7.3.4 OPERATION COMMAND WORDS (OCWS)..............................................................121 7.3.5 CASCADE MODE .....................................................................................................121 7.4 PROGRAMMABLE INTERVAL TIMER (8254).......................................................................122 7.4.1 FUNCTIONAL DESCRIPTION ....................................................................................123 7.4.2 BLOCK DIAGRAM ....................................................................................................124 7.4.3 8254 SYSTEM INTERFACE .......................................................................................126 7.4.4 OPERATIONAL DESCRIPTION ..................................................................................126 7.4.5 MODE DEFINITIONS................................................................................................134 7.4.6 OPERATION COMMON TO ALL MODES.................................................................. 138
VIII
List of Figures
Pages
Figure 1.1 block diagram of a simple microcomputer ..................................................................... 3 Figure 2.1 a model of the 8086 CPU................................................................................................. 6 Figure 2.2 fetch and execute ............................................................................................................ 8 Figure 2.3 8086 programming model.............................................................................................10 Figure 2.4 register SI is pointing at memory location 1000H .....................................................10 Figure 2.5 the bit definitions for the 16-bit flag register ...............................................................12 Figure 2.6 an even-addressed bank and an odd-addressed bank of the 8086 ..............................15 Figure 2.7 Memory map for the 8086 microprocessor. .................................................................16 Figure 2.8 Memory segment. .........................................................................................................17 Figure 3.1 LDS transfers double word ............................................................................................29 Figure 3.2 shift and rotate instructions ..........................................................................................37 Figure 3.3 register usage for multiplication and division instructions ...........................................43 Figure 3.4 Stack area after executing an INT type instruction. ......................................................55 Figure 3.5 interrupt jump table ......................................................................................................55 Figure 3.6 interrupt jump table ......................................................................................................56 Figure 4.1 memory, I/O read/write bus cycle timing .....................................................................61 Figure 4.2 8086 CPU pin design ......................................................................................................63 Figure 4.3 the four control bus signals ...........................................................................................64 Figure 4.4 Decoding status signals S3 and S4.................................................................................65 Figure 4.5 tri-state buffers and its truth table ...............................................................................70 Figure 5.1 pin description common to all memory ........................................................................73 Figure 5.2 Interfacing 8k-byte memory to the 8086 CPU. .............................................................74 Figure 5.3 The memory bank write selection input signal: ............................................................75 Figure 5.4 simple address decoding technique. .............................................................................75 Figure 5.5 pin description of 3-to-8 line decoder...........................................................................76 Figure 5.6 the 74LS138 3-to-8 line decoder connected to memory ..............................................77 Figure 6.1 input output port address decoder and 8-bit output port. ..........................................79 Figure 6.2 One-bit input and output port. .....................................................................................81
IX
Figure 6.3 Standard asynchronous serial data format. ..................................................................82 Figure 6.4 parallel printer interface to the 8086 using programmed input output .......................84 Figure 6.5 flow chart for the printer control program ...................................................................85 Figure 6.6 execution process of interrupt service routine. ............................................................87 Figure 6.7 A DMA controller interface ...........................................................................................88 Figure 7.1 Pin definition of the 8255 Chip......................................................................................90 Figure 7.2 (a) block diagram and (b) pin description of 8255 (PPI)................................................91 Figure 7.3 interfacing the 8255 to the 8086 maximum-mode CPU module. .................................93 Figure 7.4 control words of 8255, when bit 7 = 0, a bit set/reset operation is indicated .............95 Figure 7.5 control words of 8255 when bit 7 = 1, any of the modes 0, 1, or 2 can be programmed. ........................................................................................................................................................ 96 Figure 7.6 Interfacing 8255 I/O Ports in Mode 0............................................................................98 Figure 7.7 (a) input configuration; (b) control word; (c) status word of the 8255A in Mode 1 (Strobed input) ...............................................................................................................................99 Figure 7.8 (a) Output Configuration; (b) control word; (c) status word of the 8255A in Mode 1 (Strobed output)...........................................................................................................................100 Figure 7.9 interfacing the 8255A in mode 1 (strobed input/output) ...........................................101 Figure 7.10 8255 mode 2 status word. ........................................................................................103 Figure 7.11 interfacing two computers using mode 2. ................................................................103 Figure 7.12 block diagram for the INTEL 8251A USART ...............................................................105 Figure 7.13 pin descriptions for the INTEL 8251A USART. ...........................................................106 Figure 7.14 interfacing the 8251A to the 8086 microprocessor maximum-mode CPU module. 108 Figure 7.15 (a) Asynchronous mode instruction word format; (b) synchronous mode command word format. ................................................................................................................................110 Figure 7.16 8251A status register ................................................................................................111 Figure 7.17 (a) block diagram (b) pin configuration of 8259........................................................114 Figure 7.18 Interfacing 8259A (PIC) to 8086 microprocessor ......................................................118 Figure 7.19 cascading the 8259. ...................................................................................................121 Figure7.20 block diagram and pin-configuration of 8454 ............................................................122 Figure 7.21 control word register and counter functions; internal block diagram of a counter .125 Figure 7.22 system interface ........................................................................................................127
Figure 7.23 control word format ..................................................................................................127 Figure 7.24 few possible programming sequence .......................................................................129 Figure 7.25 counter latching command format ...........................................................................130 Figure 7.26 Read-Back Command format. ...................................................................................131 Figure 7.27 status byte .................................................................................................................132 Figure 7.28 null count operation ..................................................................................................132 Figure 7.29 read/write operations summery ...............................................................................133 Figure 7.30 Read-back command example. .................................................................................133 Figure 7.31 Gate pin operations summery...................................................................................137 Figure 7.32 minimum and maximum initial counts. ....................................................................138
XI
List of Tables
Table 2.1 8086 flag word Table 2.2 segment register assignments Table 3.1 effective address calculations for the indirect memory access addressing mode Table 3.2 addressing modes of the 8086 microprocessor Table 3.3 MOV instruction Table 3.4 special data transfer instructions Table 3.5 Default register assignments Table 3.6 segment override prefix Table 3.7 string instruction Table 3.8 repeat prefix Table 3.9 logical instructions Table 3.10 shifts and rotate instructions Table 3.11 addition and subtraction instructions Table 3.12 multiplication and division instructions Table 3.13 jump instruction Table 3.14 conditional jump instructions Table 3.15 loop instructions Table 3.16 push and pop instructions Table 3.17 call and return instructions Table 3.18 software interrupt instructions Table 3.19 processor control instructions Table 4.1 S3 S7 status bit definitions Table 4.2 memory access encoding Table 4.3 CPU state following RESET Table 6.1 8086 bus condition during HOLD (minimum mode) Table 7.1 Truth table for the 8255A PPI Table 7.2 8255 port addresses for the interface in figure 7.3 Table 7.3 8251A control logic truth table
Pages
12 19 23 24 26 27 30 31 32 34 36 38 40 44 47 48 50 51 53 57 58 65 66 68 88 92 94 107
XII
Table 7.4 pin description of 8259A Table 7.5 PIC interrupt vectors Table 7.6 pin description of 8454
XIII
Chapter One I TRODUCTIO TO MICROPROCESSORS A D MICROCOMPUTERS 1.1 I TRODUCTIO TO I TEL FAMILY OF MICROPROCESSOR
The three leaders in the 16-bit processor area are the Zilog (Z8000), the Motorola (M6800), and the Intel (8086). This course is concerned primarily with the Intel 8086 and its associated devices. The 8086 microprocessor is actually just one integrated-circuit (IC) chip in a family of 16-bit microprocessors designed by Intel and providing a powerful but general purpose microprocessor supported by specialpurpose co-processors. For example, the iAPX86/21 is a three-chip microcomputer system based on the 8086 microprocessor, the 8087 numerical data processor (NDP), and the 8089 input output processor (IOP) The 8087 and 8089 expands the capabilities of the 8086. Using the 8087, the 8086 microprocessor can perform complex mathematical operations up to 100 times faster than it can alone. Using the 8089, much of the burden of controlling the peripheral devices (printers, disk drives, etc) is lifted from the 8086, allowing it to concentrate on other tasks. The 8088 microprocessor is identical to the 8086 but can read memory only 8-bits at a time Intels 80X86 families of microprocessors is the most widely used architecture in modern microcomputer systems. The family includes both 8-bit microprocessors (8085/88) 16-bit microprocessors (8088, 8086, 80c186, and 80286) and 32-bit microprocessors (80386, 80486, Pentium processor families) The original IBM personal computer (PC) uses the 8088, which is the 8-bit version of the 8086. The 80286 microprocessor is the advanced version of 8086. Note that the numbers 8086, 8088 was probably chosen to avoid copy right violation with Intel.
While ENIAC was under construction, John von Neumann, also of the Moore School of Electrical Engineering, proposed totally new computer architecture. Now called the stored program concept, Von Neumann suggests that just like the data, the computer program be stored in memory. The computer would then be permanently wired to fetch its instruction from memory instead of being rewired for each new program. Interestingly enough, virtually all computers today are still based on the von Neumann architecture and stored program concept.
Beginning in 1970, a second generation of microprocessors was introduced. These devices, the 8008, 8080, and 8085 were 8-bit microprocessors and were designed to processes 8-bit (1-byte-wide) data instead of 4-bitdata. They were characterized by higher performance operation, larger system capabilities, and greater ease of programming. These extended capabilities led to wide spread acceptance of multichip 8-bit microcomputers for special-purpose system designs. Examples are electronic instruments, cash registers, and printers. In the mid-1970s, third-generation 16-bit microprocessor was developed. In 1979 the Intels first 16-bit microprocessor was released. The next year its 8-bit version, the Intel 8088 was followed. This was the birth of Intels 8086 family architecture, such as the 80286, 80186, and 80188. These 16-bit microprocessors provided higher performance and had the ability to satisfy a broad scope of special-purpose and general-purpose microcomputer applications. In 1985, Intel Corporation introduced its first 32bit microprocessor, the 80386DX, which brought true minicomputer-level performance to the microcomputer system. They were followed by a yet higher performance family, the Pentium processors, in 1993. Today, its fourth generation memberthe Pentium IV processor, represents this family.
2
Input device Input output (I/O) ports Output device Control bus
Control bus
Memory
Address bus
1.4.1 MEMORY
The memory section usually consists of RAM and ROM. It may also have magnetic floppy disks, magnetic hard disks or laser optical disks. Memory has two purposes. The first purpose is to store the binary codes for the sequence of instructions you want the computer to carry out. The second purpose of the memory is to store the binary-coded data with which the computer is going to be working.
which are used for temporarily storage of binary data; and circuitry, which generates the control bus signals. The entire CPU with timing and control functions on a single chip is known as Microprocessor. Therefore a Microprocessor or MPU is an integrated circuit that contains many processing capabilities of a large computer.
1.4.4 BUSES
ADDRESS BUSES It consists of 16, 20, 24, or 32 parallel signal lines. On these lines the CPU sends out the address of the memory or I/O port location that is to be written to or read from. The number of memory locations that the CPU can address is determined by the number of address lines. If the CPU has N address lines, then it can directly address 2N memory locations. For Example a CPU with 16 address lines can address 216 or 65,536 memory locations. For N = 20, 220 = 1,048.576 locations are addressed. When the CPU reads data from or writes data to a port, it sends the port address out on the address bus. DATA BUS It consists of 8, 16, or 32 parallel lines. They are bidirectional, this means that the CPU can read data in from memory or from a port on these lines, or it can send data out to memory or to a port on these lines. CO TROL BUS It consists of 4, to 10 parallel lines. CPU sends out signals on the control bus to enable the outputs of addressed memory devices or I/O devices. Typical control bus signal are memory read, memory write, I/O read, and I/O write. For example, to read a byte of data from a memory location, the CPU sends out the memory address of the desired byte on the address bus and then sends out a memory read signal on the control bus. The memory read signal enables the addressed memory device to output a data word on to the data bus. The data word from memory travels along the data bus to the CPU.
Chapter Two 8086 CPU ARCHITECTURE 2.1 I TRODUCTIO TO 8086 CPU ARCHITECTURE
The 8086 was the first 16-bit Microprocessor to be introduced by Intel Corporation. It is designed to be backwardly compatible with the older 8080/8085 series of 8-bit microprocessors. The backward compatibility allows programs written for the 8080/8085 to be easily converted to run on the 8086. The word 16-bit means that its arithmetic logical unit, internal registers, and most of its instructions are designed to work with 16-bit binary words. The 8086 has a 16-bit data bus, so it can read data form or write data to memory and ports either 16-bits or 8- bits at a time. The 8086 has a 20-bit address bus, so it can address any one of 220 or 1,048,576 memory locations. Each of the 1,048,576 memory addresses of the 8086 represents a byte-wide location. Words will be stored in two consecutive memory locations. If the first byte of a word is at an even address, the 8086 can read the entire word in one operation. If the first byte of the word is at an odd address, the 8086 will read the first byte of the word in one operation, and the second byte in another operation. The microprocessor functions as the CPU in the stored program model of the digital computer. Its job is to generate all system timing signals and synchronize the transfer of data between memory, I/O, and itself. It accomplishes this task via the threebus system architecture. The microprocessor has a software function. It must recognize, decode, and execute program instructions fetched from the memory unit. This requires arithmetic logic unit (ALU) within the CPU to perform arithmetic and logical (AND, OR, NOT, compare, etc.) operations. Figure 2.1 is a model of the 8086 CPU. It is divided into two separate functional units called Bus Interface Unit (BIU) and Execution Unit (EU) Bus Interface Unit (BIU) The BIU is made up of the address generation and bus-control unit, the instruction queue, and the instruction pointer. It has the task of making sure that the bus is used to its fullest capacity in order to speed up operations. This function is carried in two ways. First, by fetching the instructions before they are needed by the execution unit and storing them in the instruction queue, the 8086 MPU is able to increase computing speed. Second, by taking care of all bus-control functions, the EU is free to concentrate on processing data and carrying out the instructions. The instruction pointer contains the location or address of the next instruction to be executed.
5
BIU provides hardware functions, including generation of the memory and I/O addresses for the transfer of data between the outside world (outside CPU) and the CPU (E.U)
Instruction Queue: - The instruction queue is used as a temporary memory storage area for data instructions that are to be executed by the MPU. The BIU, through the buscontrol unit, pre-fetches instructions and stores them in the instruction queue. This allows the execution unit to perform its calculations at maximum efficiency. Because the BIU and EU essentially operate independently, the BIU concentrates on loading instructions into the instruction queue. This usually takes more time to do than the calculations performed by the execution unit. In effect, the BIU and the EU work in parallel. The instruction queue is a First- In- First- Out (FIFO) memory. This means that the first instruction loaded into the instruction queue by the bus control unit will be the first instruction to be used the ALU. Execution Unit (EU) The EU is where the actual processing of data takes place inside the 8086 MPU. It is here that the arithmetic and logic unit (ALU) is located, along with the registers used to manipulate data and store immediate results. The EU accepts instructions and data that have been fetched by the BIU and then processes the information. Data processed by the EU can be transmitted to the memory or peripheral devices through the BIU. EU has no direct connection with the outside world and relies solely on the BIU to feed it with instructions and data as indicated in figure 2.1 ALU: - The ALU is the calculator part of the execution unit. It consists of electronic circuitry that performs arithmetic operations or logical operations on the binary represented electrical signals. The control system for the execution unit can also be thought of as part of ALU. It provides a path for the flow of instructions into the ALU, the general registers, and the flag register.
5. While the EU is executing this instruction, the BIU proceeds to fetch anew instruction, and fill the queue with several new instructions. Depending on the execution time of the first instruction, the BIU may fill the queue with several new instructions before the EU is ready to draw its next instruction. Fetch Time Execute Fetch Execute Fetch (a) Execute Fetch
BIU EU
Fetch Wait
Fetch Execute
Fetch Execute
Fetch
Fetch*
Fetch
Execute1 Wait
(b) Figure 2.2 fetch and execute (a) The conventional (non pipelined) microprocessor follows a sequential fetch and executes cycle. (b) The 8086 pipelined architecture allows the execution unit (EU) to execute instruction without the delay associated with instruction fetching. * These bytes are discarded 1 this instruction requires a request data not in the queue 2 jump instructions occurs The BIU is programmed to fetch a new instruction whenever the queue has a room for one (with 8088) or two (with 8086) additional bytes. The advantage of this pipelined architecture is that the EU can execute instructions almost continually instead of having to wait for the BIU to fetch anew instructions. There are three conditions that will cause the EU to enter a wait mode. The first occurs when an instruction requires access to a memory location not in the queue. The BIU must suspend fetching its instructions and output the address of this memory location. After waiting for the memory access, the EU can resume executing instruction codes from the queue and the BIU can resume filling the queue. The second condition occurred when instruction to be executed is a jump instruction. In this case control is transferred to a new (non sequential) address. The queue, however, assumes that instructions will always be executed in sequence and thus will be holding the wrong instruction codes. The EU must wait while the instruction at the jump address is fetched. Note that any bytes presently in the queue must be discarded (they are overwritten).
The other condition that can cause the BIU to suspend fetching instructions occurs during execution of instructions that are slow to execute. For example, the instruction AAM (ASCII adjust for multiplication) requires 83 clock cycles to complete. At four clock cycles per instruction fetch, the queue will be completely filled during the execution of this single instruction. The BIU will thus have to wait for the EU to pull one or two bytes from the queue before resuming the fetch cycle.
AX BX CX DX
AH BH CH DH
AL BL CL DL
SP BP SI DI IP
Stack pointer Base pointer Source index Destination index Instruction pointer Pointer and index group
FlagsH FlagsL
ES CS DS SS
Extra Segment Code Segment Data Segment Stack Segment Segment group
Figure 2.3 8086 programming model The data group consists of the accumulator Register (AX), Base register (BX), counter register (CX), and Data register (DX). Each can be accessed as a byte or a word. Thus BX refers to the 16-bit base register but BH refers only to the high-order 8 bits of this register. The data registers are normally used for storing temporary results that will be acted on by subsequent instructions. The pointer and index group are all 16-bit registers (you cannot access the low or high bytes alone). These registers are used as memory pointers. For example, the instruction MOV AH, [SI] has the word interpretation moves the byte whose address is contained in register SI to registers AH SI thus points at the desired memory locations. The brackets around SI are used to indicate the contents of memory pointed to by SI Not the value of SI itself. Example 2.1 If SI = 1000H, in the figure 2.4 what is the contents of register AH after the instruction MOV AH, [SI] is executed?
BD 4F 17 3A 26 SI
Figure 2.4 register SI is pointing at memory location 1000H Solution from the figure SI is pointing at the byte 26H. Thus AH will store 26H.
10
Register IP (instruction pointer) It is included in the pointer and Index group, but this register has only one function-to point to the next instruction to be fetched to the BIU, It is physically part of the BIU and not under direct control of the programmer as are the other pointer registers. Stack Pointer Register A stack is a section of memory set aside to store addresses and data while a subprogram is being executed. An entire 64 K bytes segment is set aside as stack in 8086 MPU. The upper 16 bits of the starting address for this segment is kept in the stack segment register. The Stack Pointer (SP) register contain the 16-bit offset from the start of the segment to the memory location where a word was most recently stored on the Stack. The memory location where a word was most recently stored is called the top of Stack. Figure 2.4 shows the details. The physical address for a stack read or for a stack write is produced by adding the contents of the stack pointer register to the segment base address in SS. To do this the contents of the Stack segment register are shifted four bit positions left and the contents of SP are added to the shifted result. For example, if SS = 5000H and SP = FFE0H. 5000H in SS is shifted left four bit positions to give 50000H. When FFEOH in the SP is added to this, the resultant physical address for the top of the stack will be 5FFEOH. The physical address can be represented either as a single number 5FFEOH, or it can be represented in SS:SP form as 5000:FFEOH Other pointer and Index Registers In addition to the Stack Pointer register, SP, the EU contains a 16-bit Base Pointer (BP) register. It also contains a 16-bit Source Index (SI) register and a 16-bit Destination Index (DI) register. These three registers can be used for temporary storage of data just as the general purpose registers. However, their main use is to hold the 16-bit offset of a data word in one of the segments. That is, the pointer and index registers are usually used to point to or index to an address in memory. When used in this manner, these registers are address registers that designate a specific location in the memory that may be frequently used by the program. The addresses contained in these registers can be combined with information from the BIU to physically locate the data in the memory. FLAG REGISTER Figure 2.5 shows the bit definitions for the 16-bit flag register. Flag registers in the EU holds the status flags typically after ALU operation. It indicates the condition of the microprocessor and controls its operation.
11
16-bit flag register contains nine active flags, 6 of the flags are status (condition) indictors, reflecting properties of the result of the last arithmetic or logical instruction. These are called conditional or status flags. These are carry flag (CF), parity flag (PF), auxiliary flag (AF), zero flag (ZF), sign flag (SF), overflow flag (OF). FlagsH FlagsL
u u u u OF DF IF TF SF ZF u AF u PF u CF u = undefined. Figure 2.5 The bit definitions for the 16-bit flag register Table 2.1 8086 flag word Bit position Name 0 2 CF PF Function Carry flag: set on high-order bit carry or borrow; Cleared otherwise. Parity flag: set if low-order 8-bits of result contain an even number of 1-bit; cleared otherwise. Auxiliary flag: Set on carry from borrow to the low order 4-bits of AL: cleared otherwise. Zero flag: set if result is zero: cleared otherwise. Sign flag: set equal to high-order bit of result (0 if positive, 1 if negative) Single step flag: once set, a single step interrupt occurs after the next instruction executes; TF is cleared by the single-step interrupt. Interrupt-enable flag: when set, maskable interrupts will the CPU to transfer control to an interrupt vector specified location. Direction flag: causes the string instruction to auto decrement the appropriate index register when set, clearing DF causes to auto increment. Overflow flag: set if the signed result cannot be expressed within the number of bits in the destination operand; cleared otherwise.
4 6 7
AF ZF SF
TF
IF
10
DF
11
OF
12
DF, IF, and TF can be set or reset to control the operation of the processor. The remaining flags are status indicators. Example 2.2 If register AL = 7FH and the instructions ADD Al, 1 (add one to AL) is executed, it gives the following result: AL= 80H CF = 0 PF= 0 Af= 1 Zf= 0 Sf= 1 OF=1 ; 7H+1= 80H ; There is no carrying out of bit 7 ; 80H has an odd number of logic 15 ; There is a carry flag ;The result is not zero ; Bit 7 is set ; the result (+128) exceeds the capacity of (signed) register AL
The 8086 has several instructions that can be used to transfer program control to a new memory location based on the state of the flags. For example, the instruction sequence ADD AL, 1 ; add 1 to register AL JNZ 0100H ; jump to location 0100H if the result is not zero (ZF = 0)
The above result will transfer control to location 0100H if the result of the ADD AL, 1 instruction is not zero. If it is desired to test for the zero condition, the JZ (jump if zero) instruction can be used. Three of the control flags can be set or reset directory by the programmer and are used to control the operation of the processor. These are trap flag (TF), interrupt flag (IF), and directional flag (DF). When TF (trap flag) is set, control is passed to a special address (previously defined by the programmer) after each instruction is executed. Normally, a program to display all the CPU registers and flags is stored there. Setting TF therefore causes the processor to operate in software singlestepping mode, pausing after each instruction is executed. This is very useful for program bugging. When IF (the interrupt flag) is set, external interrupt requests on the 8086/88 INTR in put line will be enabled. When INTR is driven high (i.e., an interrupt occurs), control is transferred to an interrupt service routine (ISR). When this routine has finished, it normally executes an IRET (interrupt return) instruction and control is transferred back to the instructions in the main program that was executing when the interrupt occurred. The DF (direction flag) is used with the block move (also called string) instructions. When DF is set the block memory pointer will automatically decrement; if reset, the pointer will increment.
13
The segment register groups are used by the BIU to determine the memory address output by the CPU when it is reading or writing from the memory unit. To fully understand these registers, we must first study the way the 8086 divides its memory in to segments.
14
4 2 0
5 3 1
16-bit data word to 8086 Figure 2.6 an even-addressed bank and an odd-addressed bank of the 8086
15
16
register. In effect, this multiplies the segment register contents by 16. Then CS register contains B3FFH but is interpreted as pointing to address B3FF0H. The point to note is that the beginning segment address is not arbitrary-it must begin at an address divisible by 16. Another way of saying this is that the low order hex digit must be 0. Also note that the four segments need not be defined separately they can be particularly or completely over lapped. Indeed, it is allowable for all four segments to completely overlap (CS = DS = ES = SS) Example 2.3 Calculate the beginning and ending addresses for the data segment assuming that register DS=E000H. Solution The base address is found by appending four 0s. Base address: E0000H. The ending address is found by adding FFFFH (64K). Ending address: E0000H + FFFFH = EFFFFH.
17
Memory locations not defined to be within one or the current segments cannot be accessed by the 8086/08 without first redefining one of the segment registers to include that location. Thus, at any given instant a maximum of 256K (64K x 4) bytes of memory can be utilized. The contents of the segment registers can only be specified via software. Instructions to load these registers should be among the first given in any 8086 program. The 8086 divides its 1M byte of memory address space into four segments, called the data, code, stack, and extra segments. The four segment registers DS, CS, SS, and ES point to location 0 of the current segment. In this example the stack and extra segments are partially overlapped.
18
Table 2.2 segment register assignments Type of Memory reference Instruction fetch Stack operation General data String source String destination Default Alternate Offset (logical address) IP SP Effective address SI DI Effective address
Segment Segment CS SS DS DS ES None None CS, ES, SS CS, ES, SS None CS, ES, SS
The above table indicates that some memory references can have their segment definitions changed. For example, BP can also be used as a pointer into the code, data, or extra segments. On the other hand, Instruction fetches occur only from the code segment, with IP supplying the offset or logical address. Similarly, register BP used as appointer defaults to the stack segment. The table is programmed in to the BIU Example 2.6 What physical memory location is accessed by the instruction MOV (BP), AL is BP= 2C30H? Assume the segment definition shown in figure 2.8. Solution The table 2.2 indicates that the stack segment will be used. The physical address is 5D270H +2C30H 5FEA0H The table 2.2 segment register assignment indicates that some memory references can have their segment definitions changed. For example, BP can also be used as a pointer in to the code, data, or extra segments on the other hand, instruction codes can only be stored in the code segment with IP used as the offset. Similarly, string instructions
19
always uses the extra segment. The segment override is used to access these alternate segments.
Exercise 2.1 1. If IP = 3456H, where exactly is the next instruction fetch going to come from? 2. Where will the instruction MOV [BP] AL store register AL? Assume BP= 2C30H 3. Calculate the physical address corresponding to logical address. 76 D4H in extra segment. 38A4H in stack segment 4. Calculate the ending address of each segment assume BS= 358B= 35+= 5834, DS 8500?
20
Chapter Three I STRUCTIO SET OF THE 8086 3.1 DEFI I G MEMORY LOCATIO S
Usually, only the logical addresses are important. Indeed the physical address depends on the contents of the segment registers even though the logical address remains constant. When writing assembly language programs it is convenient to assign logical addresses labels or names. For example, the sequence DATA SEGMENT MEMBDS DB ? DATA ENDS Assigns the label MEMBDS to the byte at logical address 0 in the data segment. None of the three statements is a microprocessor instruction. However, they are pseudo instructions recognized by an assembler program. The operator DATA SEGMENT tells the assembler to store the following codes in the data segment. The second line uses the define byte operator (DB) to assign the label MEMBDS (memory byte in data segment) to a byte in the data segment. The ? symbol indicates that the contents of this byte is undefined. The statement DATA ENDS ends the data segment. If it is desired to define a word, the DW operator can be used. For example MEMWDS DW 1234H In this case the label MEMWDS will refer to a memory word location and the contents of that location will be initialized to 1234H (instead of being left undefined). To define a 32-bit double word the DD operator is used. A quad word (8 bytes) DQ. 10 bytes DT. The operators allow the programmer to specify memory locations by a name rather than by their specific address, they make the program clearer and more understandable.
22
Table 3.1 effective address calculations for the indirect memory access addressing mode Effective address Addressing mode Register index Displacement Base register None None Indexed Based + BX or BP + None Index register + None + SI or DI + SI or DI None
+ 127 to -128 + BX or BP +
Based and indexed None Based and indexed +127 to -128 with displacement
+ BX or BP + SI or DI + BX or BP + SI or DI
In general, a displacement can be added to a base register and the result added to an index register. This is called based and indexed with displacement. In between there are the economy modes, which use only a base or index register with or without a displacement. The resulting address is often referred to as the effective address (EA). Note that the displacement is limited to a single byte, allowing the EA to be varied +127 bytes (7FH) or -128 bytes (80H) from the pointer base. Note that BYTE PTR and WORD PTR are assembler pseudo operators needed to help the assembler determine if a byte or word memory access is required. By using these pointers the proper instruction codes will be generated by the assembler. The default memory segments for all the indirect addressing modes are the stack segment when register BP is involved and the data segment when register BX, SI, or DI is involved.
23
Example 3.1 Identify the addressing mode for each of the following instructions: 1. 2. 3. 4. 5. MOV AH,47H MOV AH, [BP+2] MOV AH, [BP+SI] MOV AH, [XRAY] ;XRAY defines a memory location MOV AH, TEMP[BX] ;TEMP defines a memory location
25
26
The 8 low-order flag bits can be stored in or loaded from register AH using the instructions SAHF and LAHF. Table 3.4 special data transfer instructions
27
I /OUT: a byte or a word of data can be input or output but must pass through the accumulator. AL must be used as the source or destination for 8-bit I/O operations. The port address can be specified in two ways: Direct mode: the instruction supplies the address but is limited to ports 0 255 (one byte) Indirect mode: register DX holds the 16-bit port address allowing access to all 65,536 ports Example 3.2 write the program required to output the word in BX to I/O ports 8004H and 8005H. Solution MOV DX, 8004H ;point DX at the port address MOV AX, BX ;data must be in AX OUT DX, AX ;output the data LEA (Load Effective Address) instruction will load pointer and index registers with the effective address of the labels Example LEA BX, MEMBDS will load register BX with effective address of MEMBDS. We would not have to know this address. LDS (load pointer using data segment) and LES (load pointer using extra segment) instructions are intended for loading entirely new address, including the segment register. They load the 16-bit destination register and ES or DS segment register with the contents of the double-word memory operand.
28
Example 3.3 Assume that MEMWWDS defines a double word beginning at address 1000H in the data segment as shown in the figure 3.1. What physical address will BX be pointing to after . the following instruction sequence? LEA SI, MEMWWDS LDS BX, DWORD PTR[SI]
Figure 3.1 LDS transfers double word Solution The instructions loads BX and DS with the contents of the double word pointed at by SI. In this case BX=8010 and DS=E000H. The physical address pointed to by BX BX=E0000H+8010H=E8010H. =E0000H+8010H=E8010H. Note the single instruction LDS BX, MEMWWDS could be used with the same effect. XLAT is useful for extracting data from a table using register AL as the offset into the table.
29
A typical application for the segment override is to allow data to be stored in the code segment. Example 3.4 CODE SEGMENT COUNT DB 0FFH MOV AL, CS:COUNT The variable COUNT is defined within the code segment, requiring the MOV instruction to use the CS: override to access this memory location.
30
31
32
SCAS (scan string byte or word) and CMPS (compare string byte or word) instructions allow the destination byte or word to be compared with the accumulator (SCAS) or the memory source (CMPS). After execution the flags are set to reflect the relationship of the destination to the source element. The conditional jump instructions can then be used to make decision such as jump if AL is greater than the memory byte or jump if the destination memory word equals the source memory word.
33
34
Example 3.5 Write the 8086 program required to fill the 1000D byte of memory in the extra segment beginning at address BLOCK with the data byte 20H. Assumed that the DF is to be 0. Solution ;AL holds the data byte ;DI holds the address of block ;load CX with 1000D ;store AL at ES:DI, increment ;DI, and repeat 1000 times REPE/REPZ (repeat while equal or zero) and REP E/REP Z (repeat while not equal or not zero) forms of the REP prefix are intended for use with the SCAS and CMPS string instructions. They allow the string operation to be repeated while equal (REPE/REPZ) or while not equal (REPNE/REPNZ). MOV AL, 20H LEA DI, BLOCK MOV CX, 03E7H REP STOSB
The flags are affected as follows: CF = 0 ;CF is reset by the AND instruction
35
PF = 0 ;40H has an odd number of logic 1s AF = x ;AF is undefined ZF = 0 ;the result is not zero SF = 0 ;bit is reset OF = 0 ;OF is reset by the AND instruction Table 3.9 logical instructions
36
The applications of A D The destination operand can be forced low by choosing those bits as 0 in the source operand. To consider the source operand a mask for testing selected bits of the destination operand. In the above example all bits are masked except bit 6. If this bit is 0, the result is zero, if it is 1, the result is non zero. AND AL, BH JZ START Using the AND instruction results in a destructive bit test because the contents of the destination operand is altered by the instruction.
TEST: this instruction performs the same function as AND instruction, but doesnt alter the source or destination operand. This is a particular handy instruction to use when several bits must be tested. OR instruction can be used to force selected bits high. Example OR AL, 80H will force bit 7 of AL high without changing any of the other bits in this register. Exclusive-OR can be used to complement selected bits. Example XOR AL, 80H will complement bit 7 of register AL without changing any of the other bits. Table 3.10 shifts and rotate instructions
38
;CX ;DX
CX + AX DX + BX + CF
The first instruction does not include the carry, as there is no carry in at this point. If the addition of AX and CX sets CF, the second addition will add this to the sum of the DX and BX. The SUB (subtract) and SBB (subtract with borrow) instructions work similarly, with CF representing the borrow condition. Table 3.11 addition and subtraction instructions
40
When adding or subtracting one from a memory pointer or counter variable, the I C (increment) and DEC (decrement) instructions should be used. The EG (negate) instruction forms the 2s complement of the destination operand, effectively reversing its sign. This is done by subtracting the destination from 0. Example 3.8 Determine the value of AL and the value of the flags following the instruction sequence MOV AL, 5 NEG AL Solution AL = 00000000 - 00000101 = 11111011 = FBH = -5.
41
The flags are affected as follows: CF = 1 ;NEG sets CF except when the operand is 0 PF = 0 ;FBH has an odd number of logic 1s AF = 1 ;there is a borrow out of bit 4 ZF = 0 ; the result is not 0 SF = 1 ;bit 7 is set OF = 0 ;there is no overflow condition
The CMP (compare) instruction is useful for determining the relative size of two operands. Normally it is followed by a conditional jump instruction such as jump if equal or jump if greater than or equal.
42
Solution MOV DX, 8000H MOV BX, 01F4H IN AX, DX DIV BX The result is AX = INT (56,723/500) = 113=71H and DX = MOD (56,723/500) = 223 = 00DFH A special problem can occur when the divisor is so small as to cause the quotient to overflow the register dedicated to the result. For example, dividing 65,000 by 2 leaves a result too large to fit in register AL alone. When this occurs a special divide by zero software interrupt is automatically generated by the 8086. This causes control to be transferred to the address stored in locations 00000-00003H. The integer multiplication (IMUL) and division (IDIV) instructions are similar to the unsigned forms except that the most significant bit represents the sign of the number. For integer byte division the quotient is limited to the range +127 to -128 and to the range +32767 to -32768 for word division. The register usage is shown
Figure 3.3 register usage for multiplication and division instructions The multiplication and division instructions can be performed on 8-bit or 16-bit register or memory source operands. Specific registers are dedicated to these functions as shown.
43
44
your program, the assembler will automatically generate a short jump if it can determine that the target address is located within -128 to +127 bytes. The following group of instructions shows assembly language program demonstrating the near jump instruction ADDR HEX CODES LABELS OP-CODE OPERANDS 0000 B3 04 MOV BL, 04H 0002 E5 06 REPEAT: IN AX, 06H 0004 F6 F3 DIV BL 0006 E7 9A OUT 9AH, AX 0008 E9 F7 FF JMP REPEAT 000B And the following program is rewritten using the JMP SHORT instruction; one byte of code is saved. ADDR HEX CODES LABELS OP-CODE OPERANDS 0000 B3 04 MOV BL, 04H 0002 E5 06 REPEAT: IN AX, 06H 0004 F6 F3 DIV BL 0006 E7 9A OUT 9AH, AX 0008 E9 F8 JMP SHORT REPEAT 000A The memory indirect and register indirect forms of the jump instruction specify the actual 16-bit target address. These two forms are not thus relative. Example JMP SHORT REPEAT instruction can be replaced with two instructions. MOV BX, 00002 JMP BX Note that using indirect jump, any address within the code segment can be specified. However, an extra instruction is required to set the target address. The main advantage of using relative jump is that the resulting program is relocatable anywhere within the code segment. When control is transferred to a target address in a new code segment, it is called a far jump. Again direct and indirect forms are possible but neither form is relative. The direct form requires the assembler operator FAR PTR to identify the label as being in a new code segment. The indirect forms must specify a double word for the new CS and IP values.
46
47
48
Example 3.11 Explain the operation of the following program. MOV BL, 47H IN AL, 36H CMP AL, BL JE MATCH JA BIG JMP SMALL Note that MATCH, BIG, and SMALL must be located within -128 to +127bytes of the corresponding conditional jump instruction Solution The program inputs a data byte from input port 36H and then compares it with 47H. If a match occurs, control is transferred to the program beginning at address MATCH. If the input byte is >47, control is transferred to the program beginning at address BIG. If none of these conditions are met, control is passed to the program beginning at address SMALL. In summary, the conditional jump instructions are among the most important in the processors instruction set, because they allow the processor to make decisions based on program condition.
This example shows the string and loop instructions actually perform two operations each. LODSB (load string byte) is equivalent to MOV AL, [SI] INC SI And LOOP AGAIN replaces the equivalent instructions DEC CX JNZ AGAIN The LOOPE or LOOPZ (loop if equal or zero) and LOOPNE or LOOPNZ (loop if not equal or not zero) instructions test CX and ZF. For example, LOOPE loops while equal this means that if CX0 and ZF=1, the loop will be repeated. These two forms are useful when comparing two strings or looking for a match between a CPU register and a byte or word in a data table. Note all forms the loop instruction repeat until CX=0, this means that the loop will be repeated 65,536 times if CX=0 initially.
50
51
Registers should be popped off the stack in the reverse order in which they were pushed on. Before using the stack it is important that register SP be initialized to a high memory location allowing room for the stack to grow. Finally, PUSHF and POPF instructions allow the processor flags to be stored on the stack. This can be useful with interrupts and subroutines as the entire state of the machine ul (all CPU registers and flags) can be saved and then restored later.
52
53
54
Figure 3.4 Stack area after executing an I T type instruction. SP is the new value of register SP
Figure 3.5 interrupt jump table If an INT 23H is executed, the processor will multiply 23H by 4 (rotate left twice), resulting in the jump table address 0008CH. CS and IP will then be loaded with the double word stored in 0008CH through 0008FH (CS in the higher order word, IP in th the
55
lower order word). When ISR has finished, the IRET instruction should be executed to pop the flag register from stack (in addition to CS and IP).
400H
Type 255
14H 10H CH 8H 4H 0H
Type 5 Type 4 Type 3 Type 2 Type 1 Type 0 Over flow Break point NMI non interrupt Trap Division error maskable
56
57
There is no instruction for setting or resetting TF (trap flag), but the following sequence of instructions can be used to set TF. PUSHF MOV BP, SP OR BYTE PTR[BP+1], 01H POPF The HALT instruction will stop the processor and cause it to enter an idle loop. However, once halted it can be restarted only via a hardware interrupt or system reset. Table 3.19 processor control instructions
58
59
Chapter Four THE 8086 CUP MODULE 4.1 8086 CPU READ A D WRITE BUS CYCLE
A microprocessor endlessly follows the sequence: 1. Fetch the next instruction in sequence from memory 2. Execute the instruction 3. Go to step-1 The fetch cycle is actually a memory read operation in which the byte or word pointed to by the program counter (or instruction pointer) is transferred from memory to the instruction register in the CPU. Execution of the instruction may require additional memory or I/O read/write operation, or an internal CPU activity. In all; there are five unique operations or bus cycles possible: 1. 2. 3. 4. 5. Memory read Memory write I/O read I/O write Bus idle (internal operation not requiring access to memory or I/O)
Note that three sets of wires (or buses) are dedicated to this transferring of data between the CPU and the memory and I/O units.
Figure 4.1 memory, I/O read/write bus cycle timing During an active bus cycle the microprocessor may perform a memory read, memory write, I/O read, or I/O write operation. The control and address buses are used to specify the memory or I/O address and the direction of data flow on the data bus lines. One bus cycle contains four CPU clock periods called T-states. To see how the three buses shown work together. Consider the sequence of events that occur during a memory read bus cycle. T1. The processor out puts the 20-bit memory addresses. The data lines are open circuited and all control lines disabled. T2. The MEMR control line is driven low. The memory unit recognizes this bus cycle as a memory read and prepares to place the addressed byte or word on to the data lines.
61
T3. The microprocessor configures its data bus lines for input but takes no further action. This state is provided primarily to give the memory time to work up the data byte o r word. T4. The microprocessor now expects the data to be on the data bus lines. It latches the contents of these lines and releases the memory read control signal. This marks the end of the bus cycle. The microprocessor controls all the bus timing. The memory must be able to supply the selected data byte or word by the time MEMR goes high during the T4-state. If it cannot do so, the CPU will read random information on the data bus lines. This will lead to unpredictable results. Note that only one control signal can be active at a given time. The processor cannot read from its memory at the same time it is outputting to an input output device. Example 4.1 Describe the contents of the address, data, and control bus lines when the instruction MOV [1000H], BX is executed. Assume DS = 09D3H and BX = 1234H Solution The instruction requires a memory write to location 1000H of the data segment. During T1 the 20-bit address bus will hold 09D30H + 1000H = 0AD30H. During T2 the control bus signal MEMW will go low and the data bus will contain 1234H (BX). During T4 the memory latches this data by the time MEMW returns high.
Figure 4.2 8086 CPU pin design Using time multiplexing the pin breakdown becomes 16 data and address pins 4 address (and status) pins 3 power pins 17 control and timing pins.
63
Data bus (AD0 AD15) these 16 pins form the CPUs bidirectional data bus. These lines are valid only during the T2 through T4 clock states. During T1 they hold the 16bits of the memory or I/O address. Address bus (AD0 AD15 and A16/S3 A19/S6) these 20-pins correspond to the CPUs 20-bit address, data and status lines on AD0 AD15 bus and allow the processor to access 1,048,576 unique memory locations. They are active during T1 state, switching to become the data and status lines during the T2-T4 clock states. Address latch enable (ALE) the signal output on this pin can be used to de-multiplex the address, data, and status lines on AD0 AD15, A16/S3 A19/S6, BHE/S7. Every cycle begins with the output of ALE pulse during the T1 clock state. The 20-bit address is guaranteed to be valid when ALE switches from high to low near the end of T1. This signal can be used to strobe the address into latch. Memory input output (M/) memory and I/O signal. The 8086 does not output separate memory and I/O read and write signals. It is output early in the T1 state and identifies the current bus cycle as a memory (M/IO= 1) or I/O (M/IO= 0) operation. Read () this active low output signal indicates that the direction of data flow on the bus is from memory or I/O in to the processor. It can be combined with M/IOto form MEMR and control signals. It is output during the T2 state and removed during the IOR T4 state. The memory or I/O device is assumed to have placed the addressed byte or word on to the data lines by the time RD returns high. Write () indicates that data is to flow from the CPU to memory or to an I/O device during T2 state. This gives the memory or I/O plenty of time to latch the data byte or word before WR is removed during T4. Figure below shows how , , and M/IO can RD WR be combined to generate a conventional four-line control bus. Figure 4.3 indicates generating the four memory and I/O control bus signals from the 8086 , , and RD WR M/IO outputs.
Clock (CLK) All events in the microprocessor are synchronized to the system clock applied to the CLK pin. The maximum frequency for the 8086 is 10MHz. the 8284A is used as a clock generator. Status (A16/S3 A19/S6 and /S7) these five status signals are output during states T2 T4. They are intended primarily for diagnostic testing purposes, as their definitions in the table below indicate. It is possible to decode S3 and S4 to provide four separate 1MB address spaces for the extra, data, code, and stack memory segments. This is shown in figure 4.4. All memory read or write operations are intercepted by this circuit, causing the appropriate physical memory block to be accessed. Table 4.1 S3 S7 status bit definitions S4 0 0 1 1 S3 0 1 0 1 Bus cycle access is to Extra segment Stack segment Code segment (or none) Data segment
S5, IF (interrupts enabled flag) S6, 0 (indicates 8086 is on the bus) S7, spare status (not used).
Extra Stack Code Data
0 1 2 3
E
E
E
E
Decoding status signals S3 and S4 allows the extra, stack, code, and data segments to be located in physically separate 1MByte address space. Bus high enable (/S7) this signal is multiplexed with the S7 status indicator. It is output during the T1 state. When BHE is low, it indicates that AD8 AD15 are involved in the data transfer. This can occur for memory or I/O word accesses or when accessing a data byte from an odd address. BHE and A0 are typically used to select even or odd memory banks or I/O ports. Table 4.2 memory access encoding BHE 0 0 1 1 A0 0 1 0 1 Action Access 16 bit word Access odd byte to D8- D15 Access even byte to D0- D7 No action
Data transmit/receive (DT/ ) this signal is intended to control the direction of data flow through the buffers (if any) connected to the system data bus. When low it indicates a read operation, and when high, a write operation. Data enable (DE ) this signal intended to be used with DT/R to enable a set of bidirectional buffers connected to the system data bus. it prevents bus contention (two circuits attempting to drive thw same bus line) by disabling the data bus buffer s until the T2 state, when the address/data lines no longer hold the memory or I/O address. Minimum/maximum mode (M /) the 8086 can be operated in one of two modes called the minimum and maximum modes. The function of pins 24 through 32 changes depending on the logic level applied to this pin. For minimum mode operation MN/MX=1 RESET: when pulsed high this input causes the 8086 to terminate its present activity and perform a reset sequence. The status of the old job is lost. Reset is normally used when first starting the system or after a system crash. This input is used together with the WAIT instruction. If the TEST input is high when the WAIT instruction is encountered, execution of the program is suspended and the CPU enters an idle mode. Only when TEST returns low will execution resume. Normally, this input is driven by the 8087. This prevents the CPU from accessing a memory result before the other microprocessor has finished its calculations.
66
READY the READY input is sampled on the rising edge of the T2 clock pulse. If this line is found low (not ready) an extra T3 state is inserted by the processor. The cycle repeats until the READY input is found high. The READY input is usually driven by a slowmemory device that cannot supply data as fast as required with normal CPU timing. Interrupts (I TR, MI, and ) INTR and NMI are hardware-initiated interrupt requests that function exactly as software interrupts. NMI is rising-edge triggered and INTR is an active high-level trigger. The INTR input can be masked by resetting the IF processor status bit (CLI). NMI is a non maskable interrupt that will always be serviced. As such it should be reserved for catastrophic events such as power failure or memory errors. When NMI is active, control automatically transfers to the address stored in locations 000080000BH. When INTR is active an interrupt-acknowledge cycle is performed. The interrupting device is expected to place an 8-bit type number on to the low-order data bus. Control then transfers to the address stored in locations type x 4 through type x 4 + 3. Hold and hold acknowledge (HOLD and HLDA) HOLD is an active high CPU input that causes the processor to open circuit all of its bus lines. This effectively disconnecting the CPU from its memory and I/O, allowing a second processor to access these devices. This is referred to as direct-memory-access (DMA). HLDA acknowledges the DMA request to the DMA controller. Power and ground (Vcc and G D) the 8086 requires a single +5v power source and has two ground pins.
4.3 GE ERATI G THE 8086 SYSTEM CLOCK & RESET SIG ALS
The 8086 requires a clock signal with fast rise& fall time (< 10ns), logic 0 and 1 levels of 0.5 to 0.6v and 3.9 to 5.0v, respectively, and a duty cycle of 33%. The processors RESET signal must be synchronized to the system clock and persist for at least 4T states. The Intel 8284A clock generator meets all the requirements for the clock and RESET signals. The 8086 clock signal All the activities of the 8086 are sequential and synchronized to a system clock signal. During T1 the memory or I/O address is output T2 Control signals are activated. T3 the memory and I/O are given time to respond
67
T4 the data is in put or output. Importance of clock signal Without the clock signal to synchronize these events we would have disorder. Internally, the microprocessor is designed with dynamic logic gates that require periodic refreshing or they will lose their data. The clock signal provides this refresh. For this reason the clock must never be stopped.
Because, the CS register contains FFFFH, the CPU will fetch its first instruction from physical address FFFFOH + 0000H = FFFF0H. RESET thus solves the random start address problem associated with Cold starting the processor. We must ensure that a useful program resides in memory beginning at the reset start address FFFF0H. Note that RAM cannot be used at this address. ROM is mapped in to this address space, because the content of ROM is not lost when power is
68
removed. We can be sure that the 8086 will have a useful program to execute when it is first started. There are only 16 bytes between addresses FFFFOH and FFFFFH (the end of memory). So the instruction at FFFF0H is normally a jump to some other location where a longer program resides (again in ROM). This program could be a boot strap Loader program that initializes the computer and then loads a more complex program from a disk drive (usually the operating system) in to RAM.
69
Input E 0 Input E 1
E 0 0 1
I 0 1 X
Figure 4.5 tri-state buffers and its truth table Tri-state buffers (74Ls240/241/244) These contain eight buffers in one package and are the most common types. In addition to the two logic states, these buffers out put a third state called the tri-state, which is actually a high-impedance or open circuit. Note that, as a general rule, a bus buffer should be used when even the bus loading exceeds the drive capabilities of the microprocessor or when it is necessary to drive receivers off the main CPU card (due to the capacitive loading associated with edge connectors and backplane wiring in a multi card system . It is not only advantageous to use a buffer for the transmitter, but by also buffering each receiver input, the number of receivers that can be safely driven is increased. Two triWRITE state gates are required for each bi-directional bus line with separate and READ enables.
70
71
During programming the select gate is given a positive bias which helps attract these electrons to the floating-gate electrode. Because the floating gate is surrounded by silicon dioxide (an excellent insulator), the injected charge is effectively trapped. Cells with trapped charge cause the transistor to be biased ON, where as those cells without trapped charge are biased OFF. Blank EPROMs have no charge and each cell stores a logic 1. The EPROM can be erased by subjecting each to ultraviolet (UV) light, which has a wave length of 2537 angstroms. The electrons on the floating gate absorb photons from the UV-light source and acquire enough energy to reverse the programming process and return to the substrate. EPROMs are packaged in special ceramic packages with quartz windows to allow erasure. In operation the EPROM window should be covered with an opaque label because normal room fluorescent lighting could erase the device. Electrically erasable PROMs (EEPROMs) There are several disadvantages to the UV EPROM. These are: 1. The device must be removed from the circuit board to be erased. 2. Byte erasure is not possible; all cells are erased when exposed to UV light. 3. The quartz window package is expensive. Because of these problems the EEPROM is developed. This device can be programmed and erased without removing the chip from its socket. In addition, both byte and bulk erasure modes are possible. Some applications of EEPROM are , although it will not replace conventional RAM, it can be used to hold programs and data that are that are subject to frequent changes: for example, inventory records, set points for NC (numerical controlled) machine tools, and motion paths for industrial robots. A particular interesting application of the EEPROM is programming via a remote data link. Using a telephone interface called a modem; the central factory can call up the EEPROM field system and transfer new data or modify the system software. Static and dynamic RAMs Two types of semiconductor RAM are popular. In static RAM four to six transistors are connected to form a simple RS flip-flop. In a static RAM, data, once entered, remain valid as long as the power supply is not turned off. On the other hand, to retain data in a DRAM, in addition to power supply, a periodic refreshing with in specific interval of time is required, because the elements are made of capacitive. Because of the refresh and address multiplexing requirements, interfacing a
72
DRAM to a microprocessor is more complex than SARM interface. However, DRAM controller chips are available to simplify this task.
Figure 5.1 pin description common to all memory Address connections All memory devices have address inputs that select a memory location within the memory device. A 2K memory device has 11 address pins (A0-A10); therefore, 11 address inputs are required to select any of its 2048 (2K) internal memory locations. The remaining nine address pins (A19A11) are connected to the inputs of a decoder. The decoder selects the memory from the many 2KB sections of the entire 1MB address range of the microprocessor. Data connections Data connections are the points at which data are entered for storage or extracted for reading. Most devices are 8-bits (a byte) wide memory.
73
Chip select () Chip select Selects or enables the memory device. If CS input is active (a logic 0), the memory device performs a read or write operation; if it is inactive (a logic-1) the memory device cannot perform a read or write operation because it is turned off or disabled.
Figure 5.2 Interfacing 8k-byte memory to the 8086 CPU. Control connection A ROM usually has only one control input, output enabled (OE) connection, which allows data to flow out of the output data pins of the ROM. A RAM memory device has either one or two control inputs: i) ii) R/W or and (write and output enabled) WE OE
If a memory write or read cycle is to occur, the chip select () and write enabled (WE, R/W = 0) or output enabled (OE or R/W = 1) are active. Figure 5.2 shows that two ships are required. One stores the even bytes and connects to DB0DB7. The other stores the odd bytes and connects to DB8DB15. A13-address lines are required to access all of the 8K-bytes. AB1AB13 address lines are connected to the memory chip address lines A0A12. You must remember that AB0 and
74
BHE are required to select the appropriate bank. Thus the interface provides a total of 16Kbytes of memory. The memory idles with all data pins open-circuited until the input is taken low. When CS is low, a read cycle will occur provided that is high (disabled) and is low (then WE OE CS used to enable the output buffers). For even byte transfer only AB0 will be low and memory-A is selected. For odd byte transfer memory-b will be selected and only will be low. Note that and are BHEB WE OE WR and which are generated from BHE, , and A0 pins of the MEMR driven by MEMW microprocessor as shown in the figure below.
Figure 5.3 The memory bank write selection input signal: HWR (MEMW) high bank write (MEMW) low bank write LWR The address decoder must decode the unused address lines AB0AB19. Figure below shows one possibility. This circuit forces the unused address lines to be 111111B and maps the circuit to the range FC000 FFFFFH
AB14 AB15 AB16 AB17 AB18 AB19
Figure 5.4 simple address decoding technique. This address decoder causes the memory interface to reside in the 16k-byte range FC000 FFFFFH.
75
Figure 5.5 pin description of 3-to-8 line decoder. For any of the decoders output to go low, the three enable inputs (G2A, G2B, and G1) must all be active. To be active the G2A and G2B inputs must both be low (logic 0) and G1 must be high (logic 1). Once the decoder is enabled the address inputs (A, B, and C) select which output pin should goes low. Only one of the eight outputs ever goes low at any time. Eight memory devices inputs can be connected to the eight outputs of the decoder at the same time. That is why it is called a very powerful device because it selects eight different memory devices at the same time.
76
Figure 5.6 the 74LS138 3-to-8 line decoder connected to memory The 74LS138 3-to-8 line decoder can be used to provide eight consecutive memory-block select signals. A 256 KB memory array is shown in the figure 5.6. It consists of four 64Kbyte blocks of memory and a 74LS138, 3 to 8 line decoder. The low order address lines AB1AB16 select one of 64KB in each memory bank. The decoder allows only one bank to be enabled at a time. Example output 0 will be active when the enable inputs are active and A17A19 are 000. This corresponds to the address range 00000H 1FFFFH.
77
The job of the input port is to detect an input output read bus cycle (M/IO and RD = 0) decode the port address and gate the input data on to the appropriate data bus lines through tri-state gates and the output port is designed to detect an I/O bus cycle and decode the port address, but this time the data on the bus must be latched. Figure below indicates how eight-even addressed and eight odd-addressed port select signals are selected.
Figure 6.1 input output port address decoder and 8-bit output port. Example 6.1 Draw the output port interface circuit (decoding scheme) for the instruction OUT 9, AL or OUT DX, AL when DX = xx09.
79
Solution The hardware required for an output port is similar to that of an input port except that the DSP (device select pulse) signal is used to strobe a latch instead of the tri state buffers. Figure 6.1 shows a circuit that provides 16-separate select signals. BHEB and AB0 are used to select even addresses (IC3) and odd addresses (IC4). Note that for a word select (BHEB and AB0A both low), both decoders are disabled. This might be desirable for byte-oriented peripherals that you do not want selected accidentally by an inadvertent 16-bit I/O cycle. SEL9 is combined with IOWB in IC5 to generate the OUT9 DSP (Device Select Signal) signal which causes IC6 latch to store the data on DB8 DB15. MemoryMapped I/O The address space of the 8086 is divided into 1,048,576 bytes of memory space and 65,536 bytes of I/O space. These two regions do not overlap because memory addresses are selected with the memory commands, while the I/O addresses are selected with the I/O commands. But consider designing a one byte (or word) read/write memory. It would use latches to store the data written during a memory write cycle, and tri-state gates to drive the bus during a memory read cycle exactly the same hardware that would use for an output or input. This is the essence of memory-mapped I/O. in hardware it appears to be a conventional I/O port. But because it is mapped to a memory address, it is accessible in software using any of the memory read or write instructions. For example, the instruction MOV BH, MEMBDS becomes an input instruction (input the data at port MEMBDS to register BH). Indirect I/O is also possible. The advantage of memory-mapped I/O is the large number of instructions and addressing modes available for referencing memory when compared to the single input and output commands available with an I/O-mapped port.
80
There are two basic methods used for serial data transmission and reception. These are asynchronous and synchronous serial communication. With appropriate software the circuit shown below can function as a serial I/O channel.
Figure 6.2 One-bit input and output port. Synchronous serial communication The synchronous data transfer scheme is the simplest of all data transfer schemes. In this scheme the processor does not check the readiness of the device. The I/O device or peripheral should have matched timing parameters. Whenever data is to be obtained from the device or transferred to the device, the user program can issue a suitable instruction for the device. At the end of the execution of this instruction, the transfer would have been completed. The synchronous data transfer scheme can also be implemented with small delay (if the delay is tolerable) after the request has been made. The mode-O input or output in 8155 or 8255 is an example of synchronous data transfer. Bisync protocol because there is no start bit, a special sync character is required in all synchronous serial formats. This character tells the receiver that data is about to follow. The USART, accordingly, must have a special hunt or search mode so that the sync character can be found.
81
Because there is no stop bit, a clock signal usually accompanies the synchronous data to maintain synchronization Asynchronous serial communication The asynchronous data transfer scheme is employed when the speed of processor and I/O device does not match. In this scheme the processor sends a request to the device for read/write operation. Then the processor keeps on polling the status of the device. Once the device is ready, the processor executes a data transfer instruction to complete the process. To implement this scheme, the device should provide a signal which may be tested by the processor to ascertain whether it is ready or not. The handshake data transfer without interrupt (mode-l and mode-2) of 8155 or 8255 is an example of synchronous data transfer. One of the most common applications for a serial I/O port is to interface the keyboard or a video display terminal (VDT). Because of the change of the data rate (number of data or character transmitted per second) between the processor and the peripheral, difficult to tell when the starts or stops, how long should each bit persist, and how are the microprocessor and peripherals synchronized an asynchronous communications protocol (set of universally accepted rules) must be established. The accepted technique for asynchronous serial communications is to hold the serial output line at logic 1 level until data is to be transmitted. Each character is required to begin with logic 0 for one bit time. This first is called the start bit and is used to synchronize the transmitter and receiver. For Example, figure below shows the standard asynchronous serial data format for 7BH 1 0
Start 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 2 stop bit | bit | one byte | | One character |
Figure 6.3 Standard asynchronous serial data format. The data byte (7BH) is framed between the start bit and 2 stop bits. The above figure illustrates how the data byte 7BF would look when transmitted in the asynchronous serial format. The data is sent least significant bit first and framed between a start bit (always a 0) and one or two stop bits (always a 1). The start and stop bits carry no information but are required because of the asynchronous nature of the data.
82
All microprocessors are capable of generating serial data without special hardware. A program can be used to output the data byte by rotating each bit to be transmitted to the bit 0 position of the accumulator and output. Recovering the serial data requires a more complex program but again no special hardware is required. The program begins by waiting for the one-to-zero transition of the start bit, once found, the middle of the bit is located by waiting for delay/2 seconds. If the input is still zero, a valid start bit is assumed and the program then waits for one additional bit time (thus sampling in the middle of all following bits). As each bit is read, it is rotated right-through the carry- and after eight reads the entire byte has been recovered. The ninth read should return the first stop bit, but if this bit is low a framing error is indicated, i.e., the program is out of synchronous with the data. If high the data byte can be saved and the program begins searching for the next start bit. Note that the receiver and transmitter data rates must be exactly matched. When setting up a serial port several parameters must be specified. The most common are: 1. Data bits /character, usually 5 to 8 2. Stop bits, one or two 3. Parity bit, used to detect single bit errors, may be specified as odd or even or no parity 4. Baud rate (data rate) One of the considerations to be made when designing or using a serial port is to select a compatible set of communications parameter for the computer and peripheral. Regardless of the type of I/O port, serial or parallel, a strategy must be developed to control or synchronize the flow of data through that port. The techniques are: Programmed input output Interrupt driven input output Direct memory access (DMA)
The most common method of servicing such devices is the programmed I/O approach. This is where the processor must test each device in sequence and in effect ask each one if it needs servicing. A large portion of the main program is looping through this continuous polling cycle and that such a method would have a serious detrimental effect on system throughput, thus limiting the tasks that could be assumed by the microcomputer and reducing the cost effectiveness of using such devices.
Figure 6.4 parallel printer interfacing to the 8086 using programmed input output
84
85
For example, assume that a 100-character/s (cps) printer is interfaced to a parallel output port. It is capable of printing a new character every 10 ms (1/100 cps). The following output routine can be used to supply data to the printer. AGAIN: LODSB OUT DPORT, AL LOOP AGAIN ; Fetch byte to AL ; send to printer ; Do CX times.
Using this routine, the 8086 can output data at 128,205 cps. There is an obvious mismatch in the data rates of the printer and microprocessor. The printer will latch the data on the data pins DATA1- DATA8 when its input STROBE , BUSY, and signals form a set of handshaking signals is low. The STROBE ACKNLG exchanged between the CPU and the printer. The CPU extends its hand with the pulse, saying hare is the data the printer acknowledges via the ACKNLG STROBE pulse, saying I have got it. You can send me some more. BUSY indicates that the printer is busy printing a character, has some error condition, or is in an OFF-LINE state. The most important point to note about this interface is the use of the and STROBE BUSY signals to synchronize the microprocessor and peripheral. Because the transfer of data to the printer is done under program control, the technique is referred to as programmed I/O. Polling loop is a form of loop by which the CPU continually tests the printers busy flag (external device, if they are ready). Note that, the instruction in the polling loop will be executed several times (1,666) between each printed character. It is wrong to have the microprocessor simply spinning its wheels waiting for slow printer to be done, without doing some other jobs.
86
Figure 6.6 execution process of interrupt service routine. When an interrupt occurs, normal processing is suspended while a special interrupt service routine (SR) is executed. Normal processing resumes when this routine is completed. During time 1 the processor is assumed to be executing its main task. At time 2the peripherals READY flag causes an interrupt to occur. After finishing the current instruction at time 3, the CS, IP, and flag registers are pushed on to the stack at time 4. Control then transfers to the ISR at time 5. During time 6 the ISR is executed, terminating with the instruction IRET (interrupt return). The CS, IP, and flag registers are recovered from the stack during time 7 and the original task is resumed at time 8. Note that the 8086 has two interrupt pins, labeled INTR and NMI.
87
Figure 6.7 A DMA controller interface The DMA controller allows the peripherals to interface directly with memory without CPU intervention. This allows the data transfer rate to approach the access time of the memory. Table 6.1 8086 bus condition during HOLD (minimum mode) Signal AD0- Ad15 A16/S3- A19/S6 RD M/IO WR INTA DT/R DEN ALE HLDA condition
Open circuit
Low High
The cycle begins with the peripheral requesting service via the DAMRQ (DMA request) input of the DMAC. The DMAC, in turn, drives the 8086s HOLD input high, requesting that the CPU enter a HOLD state. The processor responds by finishing the current bus cycle (if any) and then open circuits (Tri-states) its address, data, and (most) control signals as shown.
88
HLDA is output high by the 8086, acknowledging the hold request. In a system with address, data, and control bus buffers, HLDA is used to disable these buffers so that the CPU is completely disconnected from the memory and I/O. Upon receiving HLDA, the DMAC applies DMACK (DMA acknowledge) to the peripheral requesting service normally via the chip select input of the peripheral. The DMAC is now in control of the system outputting all the control and address bus signals just as if it were the system processor. The DMAC is normally programmed by the 8086 prior to the DMA operation for particular type of transfer. For example, it might be programmed to transfer 100 bytes from memory beginning at address 1000:0000H to I/0 port A3H. when the DMAC takes over the buses, it chip selects the output port at A3H, drives the and MEMR control IOW signals low, and then sequentially out puts the address for each of the 100 bytes to be transferred. Note that the data moves directly from memory to the I/O port not through the DMAC. This allows a very high transfer rate.
89
Figure 7.1 Pin definition of the 8255 Chip Programming of the ports is restricted to bytes or nibbles (4 bits). Each of the ports A or B can be programmed as 8 bit input or output port. Port C can be divided in half with the top most or bottom-most 4 bits programmed as inputs or out puts. Individual bits of a particular port cannot be programmed. In the figure below twenty-four I/O pins are provided, grouped as three 8-bit I/O ports. There is one-bit control port.
90
Figure 7.2 (a) block diagram and (b) pin description of 8255 (PPI). The 8255 is a very versatile device. It can be programmed to look like three simple I/O ports (called mode 0), two handshaking I/O ports (called mode 1), or a bidirectional I/O port with five handshaking signals (called mode 2). The modes can also be intermixed. For example, port A can be programmed to operate in mode 2 while port B operates in mode 0. There is also a bit set/reset mode that allows individual bits of port C to be set or reset for control purpose. Table 7.1 shows how the four internal registers (or ports) are accessed for read and write operations by the CPU. Two address input lines, labeled A0 and A1 determine which register is to be selected. For example, the control port is accessed when A1A0 = 11. WR The and input determine the direction of data flow over the chips 8-bit RD bidirectional data bus. Note that each of the data ports can be read from or written to. The control port, however, can only be written to. The byte written to this port determines the mode of operation of the three data ports.
91
Table 7.1 Truth table for the 8255A PPI A1 0 0 1 0 0 1 1 x 1 x A0 0 1 0 0 1 0 1 x 1 x RD 0 0 0 1 1 1 1 x 0 1 WR 1 1 1 0 0 0 0 x 1 1 CS 0 0 0 0 0 0 0 1 0 0 Input operation (READ) Port A data bus Port B data bus Port C data bus Output operation (WRITE) Data bus port A Data bus port B Data bus port C Data bus control Disable function Data bus tri-state Illegal condition Data bus tri-state
Similar to the memory device, the 8255 can be accessed only when its (chip select) CS input is low. Thus this input is normally driven by an address decoder to assign the device to a specific range of I/O addresses.
92
Figure 7.3 interfacing the 8255 to the 8086 maximum-mode CPU module. Two 8255s provide three consecutively addressed 16-bit I/O ports. IC3 combines the PORT SEL signal with AB0 and BHE to produce EVEN PORT SEL and ODD PORT SEL. There will be four combinations of each of these signals because they do not include AB1 and AB2. For example, EVEN PORT SEL will be active if the direct port address is 0, 2, 4, or 6. Similarly, ODD PORT SEL will be active if the direct port address is 1, 3, 5, or 7. Because AB1 and AB2 are used to select one of the four ports within the 8255, the four combinations of the EVEN PORT SEL and ODD PORT SEL signals correspond to port A, B, C and the control port of the 8255. This is shown more clearly in table 7.2. When indirect I/O instructions are given, register DX is used to hold a 16-bit port address. Because AB8-AB15 are not decoded, the circuit is partially decoded for these instructions. The port address range is 0000H 0007, 0100H - 0107H, 0200H - 0207H, ..FF00H - FF07H.
93
Table 7.2 8255 port addresses for the interface in figure 7.3 AB7 AB3 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 AB2 0 0 1 1 0 0 1 1 0 0 1 1 AB1 0 1 0 1 0 1 0 1 0 1 0 1 AB0 0 0 0 0 1 1 1 1 0 0 0 0 BHE 1 1 1 1 0 0 0 0 0 0 0 0 Port 0 2 4 6 1 3 5 7 0 and 1 2 and 3 4 and 5 6 and 7 Description Port A Port B Port C Control port Port A Port B Port C Control port Port A Port B Port C Control port Comment Even port on DB0 - DB7
Be careful not to be misled by the interface in figure 7.3. It is not necessary to interface pairs of 8255s to the 8086. If an 8-bit I/O port is sufficient, the even or odd-addressed PPI chip (and corresponding OR gate-IC3) can be deleted. The resulting interface will still span eight consecutive port locations, but every other address will be empty. Finally, the 8255 circuit provides three 16-bit I/O ports and requires only five chips. In addition, each of the six 8-bit ports can be individually programmed as an input or an output port.
94
Figure 7.4 control words of 8255, when bit 7 = 0, a bit set/reset operation is indicated When bit 7 of the control byte is a 0, one of three operating modes can be specified the three data ports are separated into two groups, labeled group A and B. The ports in group A can be programmed for any of modes 0, 1, or 2. The ports in group B can be programmed for mode 0 or 1 only. Example 7.2 Write the 8086 initialization routine required to program both 8255s in figure 7.4 for mode 0 with, port A an output and port Band C inputs. Solution in figure 7.4 the control word is formed as 10001011=8BH The direct or indirect I/O instructions can be used by duplicating the code in both halves of register AX, both PPI chips can be programmed simultaneously. The program is as follows MOV DX, 0006H ; DX points at ports 6and 7 MOV AX, 8B8BH ; Control word duplicated in AL and AH OUT DX, AX ; Write AL to even PPI and AH to odd PPI. One of the most powerful features of the 8255 is that only one control byte is required to program the mode selection-this is matter how complex the configuration may be.
95
Figure 7.5 control words of 8255 when bit 7 = 1, any of the modes 0, 1, or 2 can be programmed.
96
OUT CTRL_PORT, AL ; Reset PC4 The bit set/reset function is used in modes 1 and 2 to enable interrupt outputs available in these modes. This will be covered later in this section. To communicate with periphera1s through 8255A, three steps are necessary. 1. Determine the addresses of ports and control register from the chip select logic. 2. Write a control word into the contro1.register. 3. Write instructions to transfer data to the peripherals through ports A, B, and C.
97
Figure 7.6 Interfacing 8255 I/O Ports in Mode 0 If the INTE (interrupts enabled) bit of the input port has been set, IBF will also cause the INTR output of that port to go high. The processor now has the choice of polling the IBF line- by reading the mode 1 status word at port C or letting INTR generate an interrupt, thereby alerting the processor that input buffer is full. In either case the processor should branch to a routine that reads the data port. The falling edge of RD causes the 8255 INTR output to be reset and the rising edge of RD resets IBF. The data transfer in now complete and the peripheral (detecting IBF low) can strobe in the next byte of data.
98
(a)
(b)
(c) Figure 7.7 (a) input configuration; (b) control word; (c) status word of the 8255A in Mode 1 (Strobed input)
99
Output port timing:- when ports A or B are programmed as model 1 output ports, three ACK lines of Port C are dedicated to supporting this function: OBF, , and INTR. Their word descriptions are also provided in figure 7.8 illustrates output port timing.
(a)
(c) Figure 7.8 (a) Output Configuration; (b) control word; (c) status word of the 8255A in Mode 1 (Strobed output).
100
Assuming that data has been previously written to one of the data ports, the peripheral monitors OBF (output buffer full). When this line is low, data is available to be read by the peripheral. Using OBF as a STROBE input, the peripheral latches the data byte and OBF responds with an (acknowledge) pulse. The falling edge of this pulse sets high, ACK and if INTE is high, the rising edge causes INTR to also go high. Again the CPU has the choice of polling or interrupts. OBF can be polled by reading the mode 1 status word at port C. INTR can be used to request an interrupt alerting the CPU that OBF is high; that is, the peripheral is ready for more data. In either case, the processor should write a new byte of data to the output port. The falling edge of WR will reset INTR, and there rising edge of will force low. The periphery monitoring WR WR , can latch the new data byte and the cycle repeats. OBF
Figure 7.9 interfacing the 8255A in mode 1 (strobed input/output) Model 2 Strobed Bidirectional I/O When operated in mode 2, port A of the 8255 becomes a bidirectional data port supported by five handshaking signals. The handshaking signals are identical to those provided in mode 1 except that they now refer only to port A. This particular mode of operation is useful when transferring data between two computers. When port A is programmed to operate in mode 2, port B can operate in mode 0 or mode 1. If programmed for mode 0, PC0PC2 can be programmed as mode 0 or mode inputs or out puts. If port B is programmed for mode1, PC0-PC2 becomes handshake signals for this port.
101
Considering all the possible combinations, there are four configurations of the 8255 in mode 2. As an example, if we choose to program port A for mode 2, port B as a mode 0 input port, and PC0-PC2 as mode 0 output pins, the control word is 11XXX010. Input port timing. The sequence of events as a data byte is first transferred to the 8255 by the peripheral and then back to the peripheral by the 8255. The peripheral begins outputting a byte to the 8255. 1. Data is output by the peripheral. 2. The peripheral applies a STB pulse to the 8255. 3. When the data is latched, IBF goes high. 4. After STB returns high with IBF still set, INTR goes high, requesting an interrupt if this feature is used. 5. Polling of interrupts can now be used to service the peripheral. The 8255 buffer is read when RD resets INTR. 6. The falling edge of RD resets INTR. 7. The rising edge of resets IBF. RD Output port timing:- The following sequence occurs as the processor outputs a byte of data to the peripheral through the 8255. 1. Data is output by the processor and latched by the 8255 (note that the peripheral bus is in a high- impedance state at this time.) 2. The rising edge of WR caused OBF to switch low (the output buffer is full). ACK 3. The peripheral acknowledges by causing to go low. OBF the 8255 release its data onto the bus. 4. On the falling edge of ACK 5. returns high the output buffer is empty. OBF 6. The rising edge of sets INTR, requesting an interrupt it this feature is used. ACK 7. Polling or interrupts can now be used to write the next data byte to the 8255. A Subtle point concerning mode 2 operation is that only a single INTR output is available. This raises the question of how the processor can determine if the interrupt requires data to be read or written, that is, who requested the interrupt? Two solutions are possible. One is to use software to poll the mode 2 status word as shown in figure 7.10. In this way IBFA and can be tested to determine which one requested the interrupt. Control is OBFA then transferred to the proper routine.
102
Figure 7.10 8255 mode 2 status words. A hardware solution is also possible. The interrupt request can be combined with OBF of IBF to generate two separate interrupts. These could be connected to the interrupt Inputs of the 8259A PIC (programmable interrupt controller). The PIC would then vector each to its own service routine. The 8259A is discussed later in this chapter. Note that it is possible to mask interrupts generated by BIF or . This is done by OBF resetting INTE 1 or INTE 2 with a bit reset operation. The same bits must be set if interrupts are to be enabled. Using mode 2 to interface two microcomputers Most microcomputer peripherals are not bidirectional and thus are interfaced to the 8255 using modes 0 or 1. However, mode 2 can be useful when it is desired to interface two microcomputer over a common (bidirectional) data bus. Figure 7.11 illustrate such a circuit
When operated in mode 2 the 8255 can be used to interface two separate microcomputer systems. To the slave CPU, the master CPU appears to be an 8-bit input and output port. The 8255 is connected to the master CPU via the CS and RD and WR inputs. It is then programmed for mode 2 operation. The bidirectional port A bus is connected directly to the slave CPUs 8-bit data bus. In this example the IBF and OBF status bits are connected to the slave as bits D0 and D1 of input port Y. Thus the instruction IN AL, PORT Y. can be used by the slave to poll the master CPU and determine if data should be input or output. When the master has data to be out put to the slave, it first checks (via the mode 2 OBF status word at port C) and if high, outputs the byte to port A. The slave, polling via OBF input y, detects OBF = 0 and performs an input instruction from port z (IN AL, PORT-Z) Note that the IN Z DSP causes the 8255 to place its data on to the port A data lines (via ) and also causes OBF to return high. In this way full handshaking is achieved ACK between the master and slave CPUs. When the slave has data to be sent to the master, it first polls input port Y. If IBF is low, the 8255s input buffer is empty and ready to receive data. The slave then executes an output instruction to port X (OUT PORT-X, AL). The OUT X DSP strobes the 8255, causing it to accept the data and force IBF high. The master CPU can then poll its mode 2 status port and input the data from port A when IBF is high. This read cycle will then reset IBF and another byte can be transferred. Some applications for this interface are: The slave CPU can be thought of as an I/O processor. For Example, it might be programmed to take temperature samples once every 5 minutes for 24 hours. At the end of this time it will have stored 288 temperature samples in its memory. The master, busy with other tasks, can now read all 288 samples from the slave and process this information. In this way the master need not be tied up with the nitty gritty details of the temperature interface. In fact, the slave could be programmed to preprocess the data and supply only the mean, or the extremes, if desired. In another application the salve CPU could be thought of as a print buffer. Each time the master is called on for a print job it can transfer the data to be printed to its slave CPU (at high speed via the port A parallel interface between the two CPUs). The slave can then print the data at the rate required by the printer, while the master busies itself with some other task.
104
7.2 THE 8251A U IVERSAL SY CGRI OUS/ASSYCHR OUS RECEEIVER/TR ASMETER (USART)
Peripheral interfacing may involve a parallel or serial data path. As we have seen, the 8255 PPI simplifies the task of designing a parallel interface and is directly compatible with a polled or interrupt-driven control scheme. The 8251A USART provides a similar function when a serial interface is required. It provides two basic functions. In the transmit mode it serializes the parallel data output by the processor, automatically inserting start stop, and parity bits (synchronous mode), or sync characters (synchronous mode). In the receive mode it converts the incoming serial data to parallel and checks for parity, framing, and overrun errors. The 8251A has an 8bit bidirectional data bus, allowing the microprocessor to program its function via one to three control bytes
105
internal data port is selected. When high, the control port is chosen. Because of the multiplexer, AB0 can be used to select these ports. Full decoding is provided for all 16 I/O address lines such that the USART is mapped to consecutive ports 0070H and 0071H (selected by AB0). Tables 7.3 summarize the read and write functions of each of these ports. Table 7.3 8251A control logic truth table C/D RD WR Port address Function 0 0 1 1 0 1 0 1 1 0 1 0 70H 70H 71H 71H Read data byte Write data byte Read status byte Write control byte
Without the multiplexer, D0-D7 of the 8251A would have to be connected to one side of the 8086s data bus or the other. The resulting circuit would then be mapped to two consecutive even addresses (say, 70H and 72H) or two consecutive odd addresses (say, 71H and 73H). CTS Figure 7.12 also shows the four modern control signals: , . , and . Except DSR DTR RTS , these signals are general purpose inputs and not affect the operation of the for CTS 8251A in any way. They can be controlled via the internal status port (discussed later in this section). , however, must be low if the 8251A internal transmitter is to be CTS enabled. There is no similar receiver enable. The CLK input shown in figure 7.12 is required by the 8251A for internal timing, but no external inputs or outputs are related to this signal. It must have a frequency of 30 times that of the asynchronous receiver or transmitter clock and at least 4.5 times the asynchronous receiver or transmitter clock frequency. The maximum clock frequency is 3.125MHZ. In this example the 8284AS PCLK (peripheral clock) is used. This signal has a frequency of onehalf the system clock frequency and is 2.5 MHz for an 8086 processor. The 8251A has tRD of 200 ns maximum write pulse width of 250 ns. This means that one wait state will be required with the buffered 8086 minimum mode CPU configuration. No wait states are required in the other modes or configurations. As with the 8255, the advanced write command must be used for maximum mode interfaces.
107
Figure 7.14 interfacing the 8251A to the 8086 microprocessor maximum-mode CPU module. The data bus multiplexer allows the chip to occupy two consecutive I/O port locations. Most peripheral controller chips are interfaced without the data bus multiplexer. The circuit in figure 7.14 is intended as an example when bus multiplexing is desired.
108
translated to EIA (electronics industries associates) RS-232C specifications. This is (typically) -12V for a logic 1 and +12V for a logic 0. Special drivers and receivers are available for this purpose. May computer peripherals use the asynchronous serial RS-232C format, including printers, plotters, and modems. By following this standard, equipment from several different manufactures can readily be interfaced. Another application for asynchronous serial that should not be overlooked is remote control, after translating the USARTs TTL levels to RS-232C levels, the serial data can be transmitted for several hundred, even thousands of feet, to a remote location. Using an 8-bit data byte, 256 devices could potentially be controlled using only a two-conductor (signal and ground) cable.
7.2.3 PROGRAMMI G THE 8251A FOR THE ASY CHRO OUS MODE
When programming the 8251A, the following sequence must be followed: 1. Reset the chip (via external input or internal control byte). 2. Write the mode instruction to the control port. 3. Write the command instruction to the control port.
Figure 7.15 (a) Asynchronous mode instruction word format; (b) synchronous mode command word format.
110
A reset command must be used to start the initialization sequence. The following command will be interpreted as a mode instruction. After this byte has been written, all further writes to the control register will be interpreted as command instructions. The only way to return to the mode instruction is to apply a reset pulse or write a command byte with bit 6 high.
Figure 7.16 8251A status register Example 7.4 Write the initialization routine required to program the 8251A USART for asynchronous transmission with 7 data bits, 2 stop bits, and odd parity. Select a 16 x clock and program DTR and RTS to be low. Solution the program is as follows: MOV AL, 01000000B OUT 71H, AL MOV AL, 11011010B ;reset command ;control port ;mode instruction: ;7 data, 2 stop, odd parity, 16 x
111
;control port ; command instruction ;RTS and DTR low, error reset, enable OUT 71H, AL ;command port IN AL, 70H ;dummy read to clear receiver The receiver enable (bit 2 of the command instruction) only inhibits the RxRDY flag not the receiver itself. Thus it is possible for the receiver to have captured a character before or during the initialization routine. The dummy read ensures that the receiver holding register is clear. Once initialized, the USART will automatically begin looking for the start bit of any incoming serial data. Similarly, the transmitter will serialize any data written to the transmitter buffer (output port 70H). However, you must not forget the need to synchronize the processor to the data rate of the USART. For example, even at 19,200 baud, more than 0.5 ms is required to transmit or receive one character (assuming 8 data bits, 1 start, and 1 stop bit). This is a very long time to the processor. Figure 7.16 shows the 8251A status bit definitions obtained when the control port is read. Pay particular attention to TxRDY and RxRDY. Data should not be output unless TxRDY is high, nor should received data be input until RxRDY is high. By polling these pins the processor can be synchronized to the data rate of the peripheral.
112
As with the synchronous mode, the mode instruction can be written immediately after a reset. Following this instruction, the USART expects one or two sync characters to be specified. The next (and all following) writes will be interpreted as command instructions. The format for the mode instruction format does not change from that shown in figure 7.15. After programming the USART for the synchronous mode, the serial output line will be high (marking) until CTS goes low. At this time the contents of the transmitter buffer will be serialized and transmitted. Normally, this will be one or two sync characters. Polling TxRDY, the processor begins outputting the data to be transmitted. If at any time the transmitters buffer becomes empty, the SYNC characters will automatically be inserted by the 8251A. This is done to maintain synchronization because of the 1 x clock, one data bit is transmitted with each clock pulse. In the receive mode the final command instruction should set bit 7, requesting the 8251A to enter the hunt mode. As each new bit is shifted into the receiver buffer, the resulting character is compared against the sync character(s), if no match is found, a new bit is input. SYNDET/BD goes high to indicate synchronization when the characters match (two contiguous characters if two sync bytes have been specified). Data can now be input by the CPU polling RxRDY, as in the synchronous mode.
113
(a)
(b) Figure 7.17 (a) block diagram (b) pin configuration of 8259
114
Table 7.4 pin description of 8259A Symbol Vcc GND CS Pin No. 28 14 1 Type Name and function I I I Supply: +5v supply. DROUND CHIP SELECT: a low on this pin enables RD and WR communication between the CPU and the 8259A. INTA functions are independent of CS. WRITE: a low on this pin when CS is low enables the 8259A to accept command words from the CPU. READ: a low on this pin when CS is low enables the 8259A to release status onto the data bus for the CPU. BIRECTIONAL DATA BUS: control, status, and interrupt-vector information is transferred via this bus. CASCADE LINES: the CAS lines form a private 8259A bus to control a multiple 8259A structure. These pins are outputs for a master 8259A and inputs for a slave 8259A. SLAVE PROGRAM/ENABLE BUFFER: this is a dual function pin. When in the buffered mode it can be used as an output to control buffer transceivers (EN). When not in the buffered mode it is used as an input to designate a master (SP=1) or slave (SP=0). INTERRUPT: this pin goes high whenever a valid interrupt request is asserted. It is used to interrupt the CPU, thus it is connected to the CPUs interrupt pin. INTERRUPT REQUEST: asynchronous inputs. An interrupt input is executed by rising an IR input (low to high), and holding it high until it is acknowledged (edge triggered mode), or just by a high level on an IR input (level triggered mode). INTERRUPT ACKNOWLEDGE: this pin is used to enable 8259A interrupt vector data onto the data bus by a sequence of interrupt acknowledge pulses issued by the CPU. A0 ADDRESS LINES: this pin acts in conjunction with the CS, WR, and RD pins. It is used by the 8259A to decipher various command words the CPU writes and status the CPU wishes to read. It is typically connected to the CPU A0 address line (A1 for 8086, 8088).
115
2 3 4-11 12,13,15
I I I/O I/O
16
I/O
INT
17
IR0IR7
18-25
INTA
26
A0
27
The 8259A is a device specifically designed for use in real time, interrupt driven microcomputer systems. It managed eight levels or requests and has built in features for expandability to other 8259As (up to 64 levels). It is programmed by the systems software as an I/O peripheral. A selection of priority modes is available to the programmer so that the manner in which the requests are processed by the 8259A can be configured to match his system requirements. The priority modes can be changed or configured dynamically at any time during the main program. This means that the complete interrupt structure can be defined as required, based on the total system environment. I TERRUPT REQUEST REGISTER (IIR) A D I -SERVICE REGISTER (ISR) The interrupts at the IR input lines are handled by two registers in cascade, the interrupt request register (IRR) and the in-service (ISR). The IRR is used to store all the interrupt levels which are requesting service; and the ISR is used to store all the interrupt levels which are being serviced. PRIORITY RESOLVER This logic block determines the priorities of the bits set in the IRR. The highest priority is selected and strobed into the corresponding bit of the ISR during pulse. INTA I TERRUPT MASK REGISTER (IMR) The IMR stores the bits which mask the interrupt lines to be masked. The IMR operates on the IRR masking of a higher priority input will not affect the interrupt request lines of lower quality. I T (I TERRUPT) This output goes directly to the CPU interrupt input. The Voh level on this line is designed to be fully compatible with the 8080A and 8086 input levels. (I TERRUPT ACK OWLEDGE) Pulses will cause the 8259A to release vectoring information onto the data bus. The INTA format of this data depends on the system mode of the 8259A. DATA BUS BUFFER This 3-state, bidirectional 8-bit buffer is used to interface the 8259A to the system data bus. Control words and status information are transferred through the data bus buffer. READ/ WRITE CO TROL LOGIC The function of this block is to accept output commands from the CPU. It contains the initialization command word (ICW) registers and operation command word (OCW) registers which store the various control formats for device operation. This function block also allows the status of the 8259A to be transferred onto the data bus.
116
(CHIP SELECT) A LOW on this input enables the 8259A. No reading or writing of the chip will occur unless the device is selected. (WRITE) A LOW on this input enables the CPU to write control words (ICWs and OCWs) to the 8259A. (READ) A LOW on this input enables the 8259A to send the status of the interrupt request register (IRR), in service register (ISR), the interrupt mask register (IMR), or the interrupt level onto the data bus. Ao This input signals is used in conjunction with and signals to write commands into WR RD the various command registers, as well as reading the various status registers of the chip. This line can be tied directly to one of the address lines. THE CASCADE BUFFER/COMPARATOR This function block stores and compares the IDs of all 8259As used in the system. The associated three I/O pins (CASO-2) are outputs when the 8259A is used as a master and are inputs when the 8259A is used as a slave. As a master, the 8259A sends the ID of the interrupting slave device onto the CASO-2 lines. The slave thus selected will send its preprogrammed subroutine address onto the data bus during the next one or two consecutive pulses. INTA
5. The 8086 will initiate a second INTA pulse. During this pulse, the 8259A releases an 8-bit pointer onto the data bus where it is read by the CPU. 6. This completes the interrupt cycle. In the AEOI mode the ISR bit is reset at the end of the second pulse. Otherwise, the ISR bit remains set until an INTA appropriate EOI command is issued at the end of the interrupt subroutine. When the 8259A PIC receives an interrupt, INT becomes active and an interrupt acknowledge cycle is started. If a higher priority interrupt occurs between the two INTA pulses, the INT line goes inactive immediately after the second INTA pulse. After an unspecified amount of time the INT line is activated again to signify the higher priority interrupt waiting for service. In 8086 mode only two interrupt acknowledge cycles are issued by the processor and no CALL op-code is sent to the processor. The first interrupt acknowledge cycle is similar to that of MCS-80, 85 systems in that the 8259A uses it to internally freeze the state of the interrupt for priority resolution and as a master it issues the interrupt code on the cascade lines at the end of pulses. On this first cycle it does not issue any data to the INTA processor and leaves its data bus buffers disabled. On the second interrupt acknowledge cycle in 8086 mode the master (or slave if so programmed) will send a byte of data to the processor with the acknowledged interrupt code.
118
f. If IC=4, then all functions selected in ICW4 are set to zero. ICW2: it is written to the next port address 7CE (A0=0), specifies the five high order bits of the interrupt type number to be output by the PIC during the INTA bus cycle. The PIC assigns the three low order bits 000-111 to correspond to the active IR input, IR0-IR7. Thus the base type number of the PIC must end with 000B. Example 7.6 Determine the value for ICW2 if the PIC is to be programmed for base address of C8H. What type numbers will be output by the PIC for each IR input, and from which memory locations will the CPU fetch the interrupt vectors? Solution ICW2 is equal to the base address and thus should be programmed for 11001000B. Table below summarizes the type numbers output for each IR input and the corresponding interrupt vector locations in memory. Table 7.5 PIC interrupt vectors Type number output Input IR0 IR1 IR2 IR3 IR4 IR5 IR6 IR7 By PIC C8H C9H CAH CBH CCH CDH CEH CFH Interrupt vector location 0000:0320-0323H 0000:0324-0327H 0000:0328-032BH 0000:032C-032FH 0000:0330-0333H 0000:0334-0337H 0000:0338-033BH 0000:033C-033FH
ICW3: this byte is also output to port 7EH (A0=1) but is required only if the PIC is to be used in a cascaded system. For a master it specifies the IR input to which a slave is connected. Thus, setting ICW3 to 00000011 indicates that slave PICs are connected to the masters IR1 and IR0 inputs. For a slave PIC, ICW3 indicates the cascade address (master IR input) to which that slave is connected. For example, if the slave is connected to IR6 of the master, ICW3=00000
120
110. During the INTA bus cycle the master will output this address on CAS0-CAS2. If the address matches, the slave will output the type number corresponding to its highestpriority active IR input. ICW4: this byte is also output to port 7EH (A0=1) is required to specify the 8086 mode (bit D0=1). Bit D1 activates the automatic EOI instruction used in the fully nested and automatic rotating priority modes. Bits D2 and D3 specify if the PIC is the master or slave in a buffered CPU environment, and thus control the /EN output. Bit D4 selects SP the fully nested operating mode.
121
acknowledged, the master will enable the corresponding slave to release the device routine address during bytes 2 of only for 8086/8088. INTA The cascade bus lines are normally low and will contain the slave address code from the trailing edge of the first pulse to the trailing edge of the third pulse. Each 8259A in INTA the system must follow a separate initialization sequence and can be programmed to work in a different mode. An EOI command must be issued twice: once for the master and once for the corresponding slave. An address decoder is required to activate the chip select (CS) input of each 8259A. The cascade lines of the master 8259A are activated only for slave input, non-slave inputs leave the cascade line inactive (low).
Digital one-shot Programmable rate generator Square wave generator Binary rate multiplier Complex waveform generator Complex motor controller
124
Figure 7.21 control word register and counter functions; internal block diagram of a counter COU TER 0, COU TER 1, COU TER 2 These three functional blocks are identical in operation, so only a single Counter will be described. The internal block diagram of a single counter is shown in Figure 7.21 The Counters are fully independent. Each Counter may operate in a different Mode.
125
The Control Word Register is shown in the figure 7.21; it is not part of the Counter itself, but its contents determine how the Counter operates. The status register when latched, contains the current contents of the Control Word Register and status of the output and null count flag. The actual counter is labeled CE (for Counting Element). It is a 16-bit presettable synchronous down counter. OLM and OLL are two 8-bit latches. OL stands for Output Latch, the subscripts M and L stand for Most significant byte and Least significant byte respectively. Both are normally referred to as one unit and called just OL. These latches normally follow the CE, but if a suitable Counter Latch Command is sent to the 8254, the latches latch the present count until read by the CPU and then return to following the CE. One latch at a time is enabled by the counters Control Logic to drive the internal bus. This is how the 16-bit Counter communicates over the 8-bit internal bus. Note that the CE itself cannot be read, whenever you read the count, it is the OL that is being read. Similarly, there are two 8-bit registers called CRM and CRL (for Count Register). Both are normally referred to as one unit and called just CR. When a new count is written to the Counter, the count is stored in the CR and later transferred to the CE. The Control Logic allows one register at a time to be loaded from the internal bus. Both bytes are transferred to the CE simultaneously. CRM and CRL are cleared when the Counter is programmed. In this way, if the Counter has been programmed for one byte counts (either most significant byte only or least significant byte only) the other byte will be zero. Note that the CE cannot be written into, whenever a count is written; it is written into the CR. The Control Logic is also shown in the diagram, CLK n, GATE n, and OUT n are all connected to the outside world through the Control Logic.
126
How each Counter operates is determined when it is programmed. Each Counter must be programmed before it can be used. Unused counters need not be programmed.
Programming the 8254 Counters are programmed by writing a Control Word and then an initial count. The Control Words are written into the Control Word Register, which is selected when A1, A0 = 11. The Control Word itself specifies which Counter is being programmed. By contrast; initial counts are written into the Counters, not the Control Word Register. The A1, A0 inputs are used to select the Counter to be written into. The format of the initial count is determined by the Control Word used. Write Operations The programming procedure for the 8254 is very flexible. Only two conventions need to be remembered. 1) For each Counter, the Control Word must be written before the initial count is written. 2) The initial count must follow the count format specified in the Control Word (least significant byte only, most significant byte only, or least significant byte and then most significant byte). Since the Control Word Register and the three Counters have separate addresses (selected by the A1, A0 inputs), and each Control Word specifies the Counter it applies to (SC0, SC1 bits). No special instruction sequence is required. Any programming sequence that follows the conventions in Figure 7.23 is acceptable. A new initial count may be written to a Counter at any time without affecting the Counters programmed Mode in any way. Counting will be affected as described in the Mode definitions. The new count must follow the programmed count format. If a Counter is programmed to read/write two-byte counts, the following precaution applies. A program must not transfer control between writing the first and second byte to another routine which also writes into that same Counter. Otherwise; the Counter will be loaded with an incorrect count.
128
Figure 7.24 few possible programming sequence Read Operations It is often desirable to read the value of a Counter without disturbing the count in progress. This is easily done in the 8254. There are three possible methods for reading the counters, a simple read operation, the Counter Latch Command, and the Read-Back Command. Each is explained below. The first method is to perform a simple read operation. To read the Counter, which is selected with the A1, A0 inputs, the CLK input of the selected Counter must be inhibited by using either the GATE input or external logic. Other-wise, the count may be in the process of changing when it is read, giving an undefined result. COU TER LATCH COMMA D The second method uses the Counter Latch Command. Like a Control Word, this command is written to the Control Word Register, which is selected when A1, A0 =11. Also like a Control Word, the SC0, SC1 bits select one of the three Counters, but two other bits, D5 and D4, distinguish this command from a Control Word.
129
Figure 7.25 counter latching command format The selected Counters output latch (OL) latches the count at the time the Counter Latch Command is received. This count is held in the latch until it is read by the CPU (or until the Counter is reprogrammed). The count is then unlatched automatically and the OL returns to following the counting element (CE). This allows reading the contents of the Counters on the fly without affecting counting in progress. Multiple Counter Latch Commands may be used to latch more than one Counter. Each latched Counters OL holds its count until it is read. Counter Latch Commands do not affect the programmed Mode of the Counter in any way. If a Counter is latched and then, some time later, latched again before the count is read, the second counter Latch Command is ignored. The count read will be the count at the time the first Counter Latch Command was issued. With either method, the count must be read according to the programmed format, specifically, if the Counter is programmed for two byte counts, two bytes must be read. The two bytes do not have to be read one right after the other read or write or programming operations of other Counters may be inserted between them. Another feature of the 8254 is that reads and writes of the same Counter may be interleaved. For example, if the Counter is programmed for two byte counts, the following sequence is valid. 1) Read least significant byte. 2) Write new least significant byte.
130
3) Read most significant byte. 4) Write new most significant byte. If a Counter is programmed to read/write two-byte counts, the following precaution applies. A program must not transfer control between reading the first and second byte to another routine which also reads from that same Counter. Otherwise, an incorrect count will be read. READ-BACK COMMA D The third method uses the Read-Back Command. This command allows the user to check the count value, programmed Mode, and current states of the OUT pin and Null Count flag of the selected counter(s). The command is written into the Control Word Register and has the format shown in Figure 7.26. The command applies to the counters selected by setting their corresponding bits D3, D2, D1 = 1.
Figure 7.26 Read-Back Command format. The read-back command may be used to latch multiple counter output latches (OL) by setting the COUNT bit D5 = 0 and selecting the desired counter(s). This single command is functionally equivalent to several counter latch commands, one for each counter latched. Each counters latched count is held until it is read (or the counter is reprogrammed). The counter is automatically unlatched when read, but other counters remain latched until they are read. If multiple count read-back commands are issued to the same counter without reading the count, all but the first are ignored. I.e.; the count which will be read is the count at the time the first read-back command was issued. The read-back command may also be used to latch status information of selected counter(s) by setting STATUS bit D4 = 0. Status must be latched to be read; status of a counter is accessed by a read from that counter.
131
The counter status format is shown in Figure 7.27, Bits D5 through D0 contain the counters programmed Mode exactly as written in the last Mode Control Word, OUTPUT bit D7 contains the current state of the OUT pin. This allows the user to monitor the counters output via software, possibly eliminating some hardware from a system.
Figure 7.27 status byte ULL COU T bit D6 indicates when the last count written to the counter register (CR) has been loaded into the counting element (CE). The exact time this happens depends on the Mode of the counter and is described in the Mode Definitions, but until the count is loaded into the counting element (CE), it cant be read from the counter. If the count is latched or read before this time, the count value will not reflect the new count just written. The operation of Null Count is shown in Figure 7.28.
Figure 7.28 null count operation If multiple status latch operations of the counter(s) are performed without reading the status, all but the first are ignored. I.e.; the status that will be read is the status of the counter at the time the first status read-back command was issued.
132
Both count and status of the selected counter(s) may be latched simultaneously by setting both COUNT and STATUS bits D5, D4 = 0. This is functionally the same as issuing two separate read-back commands at once, and the above discussions apply here also. Specifically, if multiple count and/or status read-back commands are issued to the same counter(s) without any intervening reads, all but the first are ignored. This is illustrated in Figure 7.29. If both count and status of a counter are latched, the first read operation of that counter will return latched status, regardless of which was latched first. The next one or two reads (depending on whether the counter is programmed for one or two type counts) return latched count. Subsequent reads return un- latched count.
trigger. The one-shot pulse can be repeated without rewriting the same count into the counter. GATE has no effect on OUT. If a new count is written to the Counter during a one- shot pulse, the current one-shot is not affected unless the counter is retriggered. In that case, the Counter is loaded with the new count and the one- shot pulse continues until the new count expires. MODE 2: RATE GE ERATOR This Mode functions like a divide-by-N counter. It is typically used to generate a Real Time Clock interrupt, OUT will initially be high. When the initial count has decremented to 1, OUT goes low for one CLK pulse. OUT then goes high again, the Counter re- loads the initial count and the process is repeated. Mode 2 is periodic; the same sequence is repeated indefinitely. For an initial count of N. the sequence repeats every N CLK cycles. GATE = 1 enables counting. GATE = 0 disables counting. If GATE goes low during an output pulse, OUT is set high immediately. A trigger reloads the Counter with the initial count on the next CLK pulse. OUT goes low N CLK pulses after the trigger. Thus the GATE input can be used to synchronize the Counter. After writing a Control Word and initial count, the Counter will be loaded on the next CLK pulse. OUT goes low N CLK Pulses after the initial count is written. This allows the Counter to be synchronized by software also. Writing a new count while counting does not affect the current counting sequence. If a trigger is received after writing a new count but before the end of the current period, the Counter will be loaded with the new count on the next CLK pulse and counting will continue from the new count. Otherwise; the new count will be loaded at the end of the current counting cycle. In mode 2, a COUNT of 1 is illegal. MODE 3: SQUARE WAVE MODE Mode 3 is typically used for Baud rate generation. Mode 3 is similar to Mode 2 except for the duty cycle of OUT. OUT will initially be high. When half the initial count has expired, OUT goes low for the remainder of the count. Mode 3 is periodic; the sequence above is repeated indefinitely. An initial count of N results in a square wave with a period of N CLK cycles. GATE = 1 enables counting. GATE = 0 disables counting. If GATE goes low while OUT is low, OUT is set high immediately, no CLK pulse is required. A trigger reloads the Counter with the initial count on the next CLK pulse. Thus the GATE input can be used to synchronize the Counter.
135
After writing a Control Word and initial count, the Counter will be loaded on the next CLK pulse. This allows the Counter to be synchronized by software also. Writing a new count while counting does not affect the current counting sequence. If a trigger is received after writing a new count but before the end of the current half-cycle of the square wave, the Counter will be loaded with the new count on the next CLK pulse and counting will continue from the new count. Otherwise; the new count will be loaded at the end of the current half-cycle. Mode 3 is implemented as follows: Even counts: OUT is initially high. The initial count is loaded on one CLK pulse and then is decremented by two on succeeding CLK pulses. When the count expires OUT changes value and the Counter is re- loaded with the initial count. The above process is repeated indefinitely. Odd counts: OUT is initially high. The initial count minus one (an even number) is loaded on one CLK pulse and then is decremented by two on succeeding CLK pulses. One CLK pulse after the count expires. OUT goes low and the Counter is reloaded with the initial count minus one. Succeeding CLK pulses decrement the count by two. When the count expires, OUT goes high again and the Counter is reloaded with the initial count minus one. The above process is repeated indefinitely. So for odd counts, OUT will be high for (N+1)/2 counts and low for (N-1)/2 counts. MODE 4: SOFTWARE TRIGGERED STROBE OUT will be initially high. When the initial count expires, OUT will go low for one CLK pulse and then go high again. The counting sequence is triggered by writing the initial count. GATE = 1 enables counting, GATE = 0 disables Counting. GATE has no effect on OUT. After writing a Control Word and initial count, the Counter will be loaded on the next CLK pulse. This CLK pulse does not decrement the count, so for an initial count of N, OUT does not strobe low until N+1 CLK pulses after the initial count is written. If a new count is written during counting, it will be loaded on the next CLK pulse and counting will continue from the new count. If a two-byte count is written, the following happens: 1. Writing the first byte has no effect on counting. 2. Writing the second byte allows the new count to be loaded on the next CLK pulse. This allows the sequence to be retriggered by software, OUT strobes low N+1 CLK pulses after the new count of N is written.
136
MODE 5: HARDWARE TRIGGERED STROBE (RETRIGGERABLE) OUT will initially be high. Counting is triggered by a rising edge of GATE. When the initial count has expired, OUT will go low for one CLK pulse and then go high again. After writing the Control Word and initial count, the counter will not be loaded until the CLK pulse after a trigger. This CLK pulse does not decrement the count, so for an initial count of N. OUT does not strobe low until N+1 CLK pulses after a trigger. A trigger results in the Counter being loaded with the initial count on the next CLK pulse. The counting sequence is re-triggerable. OUT will not strobe low for N+1 CLK pulses after any trigger. GATE has no effect on OUT. If a new count is written during counting, the current counting sequence will not be affected. If a trigger occurs after the new count is written but before the current count expires, the Counter will be loaded with the new count on the next CLK pulse and counting will continue from there.
137
138
Bibliography
1. JOHN UFFENBECK, the 8086/8088 FAMILY: DESIGN, PROGRAMMING, AND INTERFACING, Prentice-Hall of India private limited, New Delhi-110 001, 1997. 2. Yu-cheng Liu Glenn A. Gibson, microcomputer systems: the 8086/88 Family Architecture, programming and design Prentice-Hall of India private limited, 2nd-edition, New Delhi-110 001, 2001. 3. Douglas V Hall, Microprocessors and Interfacing: Programming and Hardware, McGraw Hill Inc. New Delhi, 2nd edition, 1992 4. Barry B.Brey, The Intel Microprocessors 8086/8088, 80186, 80286, 80386 and 80486 Architecture, programming and interfacing, Prentice Hall of India, 2001 5. Naod Duga, dept. of Electrical and Computer Engineering Addis Ababa University. 6. Richard C.Detmer, introduction to 80x86 Assembly Language and Computer Architecture, 2001 WWW Resources and software tools: 1. Emu8086, the 8086 Emulator [http://www.emu8086.com], January 2011 2. Intel IA32 Architecture Reference [http://www.developer.intel.com], January 2011 3. http://www.scribd.com/, January 2011 4. http://www.scribd.com/doc/22765799/Intel-8259a-Pic, January 2011 5. http://www.scribd.com/8254-Programmable-Interval-Timer/d/21807707, January 2011
139