Overview of Input/Output Mechanisms

Similar documents
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

These three counters can be programmed for either binary or BCD count.

Programming Model 2 A. Introduction

PC Interrupt Structure and 8259 DMA Controllers

8086 Interrupts and Interrupt Responses:

CS401 - Computer Architecture and Assembly Language Programming Glossary By

COMP I/O, interrupts, exceptions April 3, 2016

Input / Output. School of Computer Science G51CSA

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

ECE 485/585 Microprocessor System Design

Chapter 5 - Input / Output

Description of the Simulator

THE CPU SPENDS ALMOST ALL of its time fetching instructions from memory

Problem with Scanning an Infix Expression

Memory Addressing, Binary, and Hexadecimal Review

231 Spring Final Exam Name:

For more notes of DAE

Chapter 4. MARIE: An Introduction to a Simple Computer. Chapter 4 Objectives. 4.1 Introduction. 4.2 CPU Basics

Problem with Scanning an Infix Expression

5.b Principles of I/O Hardware CPU-I/O communication

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Reversing. Time to get with the program

EC2304-MICROPROCESSOR AND MICROCONROLLERS 2 marks questions and answers UNIT-I

Today: Computer System Overview (Stallings, chapter ) Next: Operating System Overview (Stallings, chapter ,

Q.1 Explain Computer s Basic Elements

Computer Organization ECE514. Chapter 5 Input/Output (9hrs)

CS 134. Operating Systems. April 8, 2013 Lecture 20. Input/Output. Instructor: Neil Rhodes. Monday, April 7, 14

CS311 Lecture: The Architecture of a Simple Computer

Summer 2003 Lecture 12 06/26/03

INTERRUPTS in microprocessor systems

3. Simple Types, Variables, and Constants

Practical Malware Analysis

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

int $0x32 // call interrupt number 50

The Instruction Set. Chapter 5

Misc. Third Generation Batch Multiprogramming. Fourth Generation Time Sharing. Last Time Evolution of OSs

1. INTRODUCTION TO MICROPROCESSOR AND MICROCOMPUTER ARCHITECTURE:

8051 Microcontroller Interrupts

Summary: Direct Code Generation

Stating the obvious, people and computers do not speak the same language.

ORG ; TWO. Assembly Language Programming

Hardware OS & OS- Application interface

Preprocessor Directives

Introduction to Embedded Systems. Lab Logistics

Week 11 Programmable Interrupt Controller

Module 3. Embedded Systems I/O. Version 2 EE IIT, Kharagpur 1

GUJARAT TECHNOLOGICAL UNIVERSITY MASTER OF COMPUTER APPLICATION SEMESTER: III

MARIE: An Introduction to a Simple Computer

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

Why Study Assembly Language?

Faculty of Science FINAL EXAMINATION

CHAPTER 11 INTERRUPTS PROGRAMMING

Summary of Computer Architecture

Computer System Architecture

EECS 373 Design of Microprocessor-Based Systems

Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

A First Look at Microprocessors

National 5 Computing Science Software Design & Development

Digital System Design Using Verilog. - Processing Unit Design

PESIT Bangalore South Campus

Operating Systems. Part 8. Operating Systems. What is an operating system? Interact with Applications. Vector Tables. The master software

V8-uRISC 8-bit RISC Microprocessor AllianceCORE Facts Core Specifics VAutomation, Inc. Supported Devices/Resources Remaining I/O CLBs

icroprocessor istory of Microprocessor ntel 8086:

Chapter 3 - Top Level View of Computer Function

Input/Output Management

An overview of Interrupts ECE3534

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Machine Architecture. or what s in the box? Lectures 2 & 3. Prof Leslie Smith. ITNP23 - Autumn 2014 Lectures 2&3, Slide 1

CPEG300 Embedded System Design. Lecture 6 Interrupt System

Operating system. Hardware

Class Notes. Dr.C.N.Zhang. Department of Computer Science. University of Regina. Regina, SK, Canada, S4S 0A2

I/O Handling. ECE 650 Systems Programming & Engineering Duke University, Spring Based on Operating Systems Concepts, Silberschatz Chapter 13

BASIC OPERATIONS. Managing System Resources

MICROPROCESSOR MEMORY ORGANIZATION

Overview of Computer Organization. Outline

(Refer Slide Time 00:01:09)

Chapter-6. SUBJECT:- Operating System TOPICS:- I/O Management. Created by : - Sanjay Patel

INPUT/OUTPUT ORGANIZATION

Ch. 11: References & the Copy-Constructor. - continued -

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CS 101, Mock Computer Architecture

A Review on Cache Memory with Multiprocessor System

Newbie s Guide to AVR Interrupts

ECE251: Thursday September 27

Computer Architecture and System Software Lecture 06: Assembly Language Programming

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

CPU: SOFTWARE ARCHITECTURE INSTRUCTION SET (PART

Expressions and Casting

Understand the factors involved in instruction set

Darshan Institute of Engineering & Technology for Diploma Studies Unit - 1

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Battleship. Magnus Hultin, Adam Johansson. Supervisor: Bertil Lindvall

Chapter 4. MARIE: An Introduction to a Simple Computer

INPUT-OUTPUT ORGANIZATION

Northern India Engineering College, Delhi (GGSIP University) PAPER I

CSE 120. Overview. July 27, Day 8 Input/Output. Instructor: Neil Rhodes. Hardware. Hardware. Hardware

Chapters 2, 3: bits and pieces. Chapters 2 & 3. Chapters 2, 3: bits and pieces. Chapters 2, 3: bits and pieces. Using C. A last word about hardware

ECE332, Week 8. Topics. October 15, Exceptions. Hardware Interrupts Software exceptions

Transcription:

Overview of Input/Output Mechanisms Norman Matloff University of California, Davis \copyrigth{2001}, N. Matloff February 5, 2001 Contents 1 Introduction 1 2 Our Mythical Machine Architecture 2 3 I/O Ports and Device Structure 2 4 Program Access to I/O Ports 2 4.1 I/O Address Space Approach.................................. 2 4.2 Memory-Mapped I/O Approach................................ 3 5 I/O Programming 4 5.1 Wait-Loop I/O.......................................... 5 5.2 Interrupt-Driven I/O....................................... 5 5.3 Direct Memory Access (DMA)................................. 7 A Specifications of Our Mythical Machine 7 1 Introduction During the early days of computers, input/output (I/O) was of very little importance. Instead computers, as their name implied, were used to compute. A typical application might involve huge mathematical calculations needed for some problem in physics. The I/O done in such an application would be quite minimal, with the machine grinding away for hours at a time with little or no I/O being done. 1

4 PROGRAM ACCESS TO I/O PORTS Today, we have just the opposite situation. I/O plays a crucial role in most modern computer applications. In fact, applications in which I/O plays an important role can arguably be viewed as the most powerful driving force behind the revolutionary rise to prominence of computers in the last decade or so. Today we make use of computers in numerous aspects of our business and personal lives and in the vast majority of such uses, the programs center around I/O operations. In some of these applications, the use of a computer is fairly obvious: First and foremost, the Internet, but also credit-card billing, airline reservations, word processors, spreadsheets, automatic teller machines, real estate databases, drafting programs for architects, video games, and so on. Possibly less obvious is the existence of computers in automobiles, and in such household items as washing machines and autofocus cameras. However, whether the existence of computers in these applications is obvious or hidden, the common theme behind them is that they all are running programs in which I/O plays an important role, usually the dominant role. 2 Our Mythical Machine Architecture In this tutorial, you will learn about I/O on a mythical machine described in the Appendix below. However, the principles are similar for almost any machine. 3 I/O Ports and Device Structure An I/O device connects to the system bus through ports. I/O ports are similar in structure to CPU registers. However, a port is not in the CPU, being instead located between the I/O device and the system bus. Ports are usually 8 bits in width. They have addresses, just like words in memory do, and these addresses are given meaning through the address bus, just as is the case for words in memory. 4 Program Access to I/O Ports Note that a potential ambiguity arises. Suppose we wish to communicate with port 50, which is connected to some I/O device. The CPU will place the value 50 on the address bus. Since all items which are connected to the bus will see what is on the address bus, how can they distinguish this 50, intended for an I/O port, from a value of 50 meant to address word 50 of memory? In other words, how can the CPU indicate that it wants to address I/O port 50 rather than memory word 50? There are two fundamental systems for handling this problem, described next. 4.1 I/O Address Space Approach Recall that the system bus of a computer consists of an address bus, a data bus, and a control bus. The control bus in many computers, such as those based on Intel CPUs, has a special line or lines to indicate that the current value on the address bus is meant to indicate an I/O port, rather than a word in memory. For Input/Output: 2

4 PROGRAM ACCESS TO I/O PORTS 4.2 Memory-Mapped I/O Approach our mythical computer here we will assume four such lines, MR (memory read), MW (memory write), IOR (I/O read) and IOW (I/O write). To access I/O port 50, the CPU will assert either IOR or IOW (depending on whether it wants to read from or write to that port), while to access word 50 in memory, the CPU will assert either MR or MW. In either case the CPU will place the value 50 on the address bus, but the values it simultaneously places on these lines in the control bus will distinguish between I/O port 50 and word 50 of memory. The programmer himself or herself controls which of these lines will be asserted, by choosing which instruction to use. MOV R2,[50] and IN R5,[50] will both cause the value 0x50 to go out onto the address bus, and in both cases the response will be sent back via the data bus. (Here R2 and R5 are CPU registers.) But the MOV will assert the MR line in the control bus, while the IN instruction will assert the IOR line in that bus. As a result, the MOV will result in copying the memory byte at address 50 to R2, while the IN will copy the byte from I/O port 50 to R5. Since the lines IOR/IOW and MR/MW allow us to distinguish between I/O ports and memory words having identical addresses, we can describe this by saying that the I/O ports have a separate address space from that of memory. Thus we will call this the I/O address space approach. In our machine the keyboard has ports with addresses 60 and 61. For example, the instruction IN R3,[60] would copy a character from the keyboard s data port, which has address 60, to the R3 register. Just as with memory chips, the addresses of I/O ports are determined by combinations of gates whose outputs feed into something like a Chip Select pin in a given port. 1 Denoting the Chip Select pin by CS, we would have in the case of the keyboard port at address 52 4.2 Memory-Mapped I/O Approach Another approach to the duplicate port/memory address problem described above is called memorymapped I/O. 2 1 Or more typically, a set of ports. The keyboard ports in our example here would typically be mounted on one common I/O board, providing a common interface for them to the bus. 2 Unfortunately, many terms in the computer field are overloaded. It has become common on personal computers to refer to the mapping of video monitor pixels to memory addresses also as memory-mapped I/O. However, this is not the original use of the term, and should not be confused with what we are discussing here. Input/Output: 3

5 I/O PROGRAMMING Under this approach, there are no special lines in the control bus to indicate I/O access versus memory access (though there still must be one or two lines to distinguish a read from a write, of course), and thus no special I/O instructions such as IN and OUT. One simply uses ordinary instructions such as MOV, whether one is accessing I/O ports or memory. Note that we can use the memory-mapped approach even if we do have special I/O instructions and control lines, just by ignoring them. In our machine here, for instance, we retain the addresses 60 and 61 for the keyboard ports but connect their read/write pins to the MR and MW lines instead of IOR and IOW. To read a character from the Keyboard Data Port 60 and copy it to a memory location CHARBUFFER: MOV CHARBUFFER,[60] Since this is an ordinary MOV instruction, the CPU is being fooled into thinking that the keyboard data port is physically a memory location, which the MOV is reading. But of course it is not memory. One advantage of the memory-mapped approach to I/O port addressing is that one can easily access the ports from a high-level language such as C directly, instead of using assembly language, by taking advantage of the fact that the * operator in C allows access to specific memory locations. For example, the above operation could be done in a C program by having the declaration char CharBuffer,*KDataPtr; and then using the code KDataPtr = 0x60; CharBuffer = *KDataPtr; We could not do this directly with the I/O address space approach, because we have no way in the C language to make the compiler produce IN or OUT instructions. (The above code would produce a MOV.) However, compilers on machines which use the I/O address space approach usually include library functions which can be called from C to do these operations; the authors of these functions wrote them in assembly language. 5 I/O Programming Consider the case of reading a character from the keyboard. The program which will read the character has no idea when the user will hit a key on the keyboard. How should the program be written to deal with this time problem? (Note that we are for the moment discussing programs which directly access the keyboard, rather than going through the operating system, as is the case with programs you usually write; more on this point later.) There are three basic approaches, covered in the following subsections. Input/Output: 4

5 I/O PROGRAMMING 5.1 Wait-Loop I/O 5.1 Wait-Loop I/O As described in the Appendix to this document, Bit 0 in our machine s Keyboard Status Port (KSP) tells us whether a character is ready to read or not; a value of 1 in this bit means a character is ready, and 0 means the user has not yet typed a character. Wait-loop I/O then consists of writing a loop, in which the program keeps testing Bit 0 of the KSP: LP: IN R4,[61] ; get value of KSP AND R4,1 ; check Bit 0 JZ LP ; if that bit is 0, try again DONE: IN R4,[60] ; finally, get the character During the time before the user hits a key, the value read from the KSP will always have a 0 in Bit 0, so that the AND results in 0, and we jump back to LP. But eventually the user will hit a key, resulting in Bit 0 of the KSP being 1, and we leave the loop. We then pick up the character at DONE. 5.2 Interrupt-Driven I/O Wait-loop I/O is very wasteful. Usually the speed of I/O device is very slow compared to CPU speed. This is particularly true for I/O devices which are mainly of a mechanical rather than a purely electronic nature, such as printers and disk drives, and thus are usually orders of magnitude slower than CPU actions. It is even worse for a keyboard: Not only is human typing extremely slow relative to CPU speeds, but also a lot of the time the user is not typing at all; he/she may be thinking, taking a break, or whatever. Accordingly, if we use wait-loop I/O, the CPU must execute the wait loop many times between successive I/O actions. This is wasted time and wasted CPU cycles. An analogy is the following. Suppose you are expecting an important phone call at the office. Imagine how wasteful it would be if phones didn t have bells -you would have to repeatedly say, Hello, hello, hello, into the phone until the call actually came in! This would be a big waste of time. The bell in the phone frees you from this; you can do other things around the office, without paying attention to the phone, because the bell will notify you when a call arrives. Thus it would be nice to have an analog of a telephone bell in the computer. This does exist, in the form of an interrupt. It takes the form of a signal sent to the CPU by an I/O device. This signal forces the CPU to suspend, i.e. interrupt, the currently-running program, say X, and switch execution to another procedure, which we term the interrupt service routine (ISR) for that I/O device. 3 Here is a simple example (a bit more complex this time, since we are reading several characters, not just one): 3 This is called a hardware interrupt or external interrupt. Those who have done assembly-language programming on PCs should not confuse this with the INT instruction. The latter is almost the same as a procedure CALL instruction, and is used on PCs to implement systems calls, i.e. calls to the operating system. Input/Output: 5

5 I/O PROGRAMMING 5.2 Interrupt-Driven I/O LP: J LP ; do some work in this loop, maybe unrelated to keyboard ; ISR starts here ; the following code must be loaded at address 0x1000000 PUSH R4 ; we will use R4, etc. here, so must save old ones PUSH R5 PUSH R6 IN R4,[60] ; pick up the character from the KSP MOV R5,COUNT ; get current character count, stored in memory MOV R6,a(CHARS) ; where does character array start? ADD R6,R5 ; R6 now points to place for next character MOV [R6],R4 ; place the character there POP R6 ; restore old register values POP R5 POP R4 IRET ; back to the interrupted routine Note that the ISR may or may not have any relation to what is in the loop (starting at LP). But the key point is that at some times (whenever someone hits a key, in this case) the ISR will need to be run, and from the point of view of the interrupted program, that time is unpredictable. In the example here, each time a character is typed, a different instruction within the loop might get interrupted. What kind of code might be in the loop? Well, for example, consider a program to do some printing. For simplicity, let us suppose that the printer is set up so that we give it one character at a time. (Most modern printers would have us give them groups of characters at a time, but the general principle would be similar.) Again, since a printer is a mechanical device, it is slow when compared to CPU speeds. It would be quite a waste to use wait-loop I/O, tying up the CPU between printing of successive characters. So, for example, we could put some sort of game in the LP loop, so that at least the user would have some entertainment while the printing is being done. And even though the game would be interrupted frequently, the user would never notice it, since the execution time for the ISR for each character would be quite small. Interrupt systems can get quite complex. What happens, for example, when two I/O devices both issue interrupts at about the same time (i.e. within the same instruction in the LP loop)? Which one gets priority? And even when there is just one interrupt at a time, how can the program or CPU sense which of several I/O devices is the one which rang the bell in this case? Since each I/O device will have its own ISR, this issue must be resolved. There are many possible resolutions, but we will not discuss them here. Input/Output: 6

A SPECIFICATIONS OF OUR MYTHICAL MACHINE 5.3 Direct Memory Access (DMA) 5.3 Direct Memory Access (DMA) In our keyboard ISR example above, when a user hit a key, the ISR copied the character from the KDP into a register, then copied the register to memory. This is wasteful; it would be more efficient if we could arrange for the character to go directly to memory from the KDP. This is called direct memory access (DMA). DMA is not needed for devices which send or receive characters at a slow rate, such as a keyboard, but it is often needed for something like a disk drive, which sends/receives characters at very high rates. So in many machines a disk drive is connected to a DMA controller, which in turn connects to the system bus. A DMA controller works very much like a CPU, in the sense that it can work as a bus master like a CPU does. Specifically, a DMA controller can read from and write to memory, i.e. assert the MR and MW lines in the bus, etc. So, the device driver program for something like a disk drive will often simply send a command to the DMA controller, and then go dormant. After the DMA controller finishes its assignment, saying writing one disk sector to memory, it causes an interrupt; the ISR here will be the device driver for the disk, though the ISR won t have much work left to do. A Specifications of Our Mythical Machine The instruction set will consist of: MOV x,y ADD x,y SUB x,y AND x,y OR x,y NOT x IN x,y OUT y,x JZ t JN t J t CALL t RET IRET PUSH x POP x copy y to x add y to x subtract y from x AND together x and y, placing the result in x OR together x and y, placing the result in x peform a 1s-complement operation on x do an input operation from the I/O port y, putting the result in x do an output operation to the I/O port y, using x as source jump to t if the Z bit is set jump to t if the N bit is set jump unconditionally to t call the subroutine at t return from subroutine return from interrupt service routine push x onto stack; x must be a register pop stack and place the popped value into x; x must be a register Here x can be either a register or a memory location, and y can be either a register, memory location or a constant (assumed to be in hex notation), but x and y cannot both be memory locations (or a memory Input/Output: 7

A SPECIFICATIONS OF OUR MYTHICAL MACHINE location and a port). Registers are denoted R0, R1, R2 and so on, and also include the stack pointer SP. A memory location can be given either as a label, as with X in MOV R2,X as a hex literal, as in ADD R4,[2500] or with indirect addressing, as in ADD R4,[R6] Note that the brackets are used to distinguish labels from constants, and register addressing from indirect addressing. Thus ADD R4,[2500] adds whatever is in memory location 2500 to R4, while ADD R4,2500 would add the number 2500 to R4, and ADD R4,[R6] adds whatever is in the memory location pointed to by R6 to R4, while ADD R4,R6 adds the contents of R6 itself to R4. One can specify the address of a lable using the notation a(), e.g. a(x) for the address of X. With the control-transfer instructions JZ, JN, J and CALL, the target t is specified as with memory addresses above, i.e. either as a label or as a hex literal surrounded by brackets. The Z (Zero) and N (Negative) bits are stored in a special PS (Program Status) register. They reflect the result of the most recent ALU operation (including MOV, in which the ALU does a copy operation); Z will be 1 or 0, depending on whether the latest ALU operation resulted in a zero value, respectively, and similarly for N. The stack is assumed to grow toward 0. Thus the CALL instruction will perform the following operations: Input/Output: 8

A SPECIFICATIONS OF OUR MYTHICAL MACHINE SP <-- SP-1 copy PC to the memory location pointed to by SP copy t to the PC An interrupt will cause the following to occur as soon as the current instruction finishes: CPU notices the Interrupt Request line in the system bus has been asserted by some I/O device CPU pushes current PC value on stack CPU pushes current PS value on stack CPU does PC <-- 0X1000000 ; save PC of interrupted routine ; save PS of interrupted routine ; jump to ISR The IRET instruction undoes all of this: CPU pops stack and placed popped value ; restore old PS in PS CPU posp stack and placed popped value ; restore old PC in PC We assume the machine has a Keyboard Status Port (KSP) at port 60, and a Keyboard Data Port (KDP) at port 61. The KSP s bit 0 indicates whether a character has arrived (coded 1) or not (coded 0). Before the user hits a key, this bit is 0; when the user finally hits a key, the bit changes to 1; then when the program reads that character by reading the KDP, this bit reverts to 0. We will not define ports for other machines here, since they will not be needed for examples, but a typical machine would have a number of I/O devices and thus have many ports. Input/Output: 9