Chapter 11 -- I/O Port





Introduction

Hi! Nice to meet you again! This time I'd like to explain the I/O port, stands for input-output port. Concepts and implementations are here, linked in a single page. Well, it's actually another low-level programming lesson.
 

What to Learn

We learn about I/O ports, of course, and how to access it through Pascal too. Make sure that you have at least Turbo Pascal 7.0. Otherwise, the excerpts just won't run. Why should we learn this? It's because many of the peripherals can only be accessed through I/O ports. One of the examples is Sound Blaster. So, that sure means a lot.

Before we begin, I must warn you that I/O ports of each hardware behave differently. So, you need to have a firm grip of the hardware you want to program. As a warning, NT users are not allowed to access I/O port directly since it is prohibited by the operating system kernel. That's bad news.

The Beginning

This chapter is quite brief. So, don't feel bad. :-) What is I/O port exactly? You can consider it as a hardware address. If memory has addresses, each peripheral or equipments in your PC have addresses, too. No, I won't get into any detail to access each port. But first, beware that any misprogramming could cause catasthrope to your computer.

Accessing hardwares through ports means that you are dealing with the hardware itself directly. Any mistakes would cause a severe destruction. Why? It is because Pascal doesn't know what equipment you are dealing with. There are no validation checking whether your input is correct or not. Well, a few hardware provide a minimum validation check, however.

I/O ports can only accept numbers, it is either byte (8-bit), or word (16-bit). Recently, some hardware needs 32-bit input, like Sound Blaster AWE32 and 64. The input depends on the hardware you want to deal with.

Accessing I/O ports

Borland Pascal provides a mean to access I/O ports. It is done through the special array port and portw. Suppose you want to access port 378h, you can do either: x:=port[$378]; to get the contents of port 378h, or assign it with values such as port[$378]:=$44; That is the 8-bit access. x is a byte.

Remember: Don't run the examples. These are intended to give you rough ideas. Not every port has read and write access. Some port has read only access, some others write only access. Look at the hardware programming manual to find out the access attribute of each port. The sign r/w denotes read and write access. The sign r or r/o denotes read only access. The symbol w or w/o denotes write only access.

The behavior of the ports are different from hardware to hardware. Some of the ports need to be written with the same value twice. Some others twice write with different value. So, pay attention to the behavior of the ports. More over, don't dare to write to arbitrary ports which you don't know which hardware it address to. Horrible!

Pascal provides 16-bit port access, too. It is done through portw instead of just port. So n:=portw[$3DA]; means that n accepting 16-bit value from port 3DAh. Writing access is allowed in word operations. (Check the hardware first!) It is better for us to use words instead of integers. Accessing the word port is exactly the same as accessing two consequence 8-bit port. This is an example:

portw[$3C0]:=$44C0;

is the same as:

port[$3C0]:=$C0;
port[$3C1]:=$44;

Just like separating a word port into 2 byte ports. That trick is used to get around with the newer 32-bit ports. Pascal don't have portd or so to deal with it. Instead, we must break a double word (32-bit) port into 2 word ports. You can also break the double word port into 4 byte ports. As you wish.

Port Registers

Sometimes, hardware book says that you need to feed register 1 with 0. The register here is the port register. It is completely different with the registers we have discussed this far. It may be very confusing at first. Yes, I did too. You can view the port register is an index to an array of data inside the hardware. However, because of its limited space, the hardware allows you to access only one of its contents.

Such hardware are usually has two port address, some has more. These addresses are usually bound closely in sequence. For example: port 388h and 389h in Adlib sound card or port 70h and port 71h in CMOS real time clock data. The first port is usually the index port. It is either has write only access or read / write access. The second port is the actual data access. It usually has read / write access. To access the hardware, you need to program the index register first to specify which data you want to access. Then you can access the data inside the hardware. So, feeding register 1 with 0 in Adlib card would be done like this:

port[$388]:=1;
port[$389]:=0;

Getting the hour part of CMOS data is done through getting the register 0 contents. So, that is done like this:

port[$70]:=0;
x:=port[$71];

Easy, no?

Notes

That's all folks! Low level programming is not so hard, isn't it? You can now program in low-level. Congratulations! Do seek hardware books of the hardware you like, sound cards, communication ports, and so on. Shall we go to the next lesson?


Where to go?

Back to main page
Back to Pascal Tutorial Lesson 2 contents
Quiz? No quiz. I think you are pretty mature in programming! :-)
To Chapter 12 about ancient ruins of DOS.
My page of programming link
Contact me


By: Roby Joehanes, © 1997, 2000