port_in (var port);

port_out (var port,var data);

Reads or writes data to an I/O port address.

Parameters:

port Address of the I/O port, 0..4096.
data Byte to be written to the port, 0..255.

Returns:

Data to be read from or written to the port, or -1 if the instruction failed.

Speed:

Medium.

Remarks:

Edition:

 C   P 

Example:

For trying out the port access instructions, we'll write data to the parallel port.

First, a little parallel port theory. The primary purpose of the parallel port is to connect a printer to the computer - therefore it is often called 'printer port'. However it offers 8 general purpose output and 5 input lines that can be used to control any external device - for instance an EPROM programmer. The parallel port connector at the rear panel of the PC is a 25 pin female (DB25) connector shown in the image below.

The lines are divided into three groups: data lines, control lines, and status lines. The black pins are connected with ground. As the name suggests, data is transferred over the data lines, control lines are used to control the peripheral and the peripheral returns status signals through the status lines. These lines are internally connected to the data, control and status registers. By manipulating the registers, one can read or write to the parallel port.

Each register has a unique I/O port address. For a typical PC, the base address of LPT1 is 888 (= 378 hex) and of LPT2 is 632 (= 278 hex). The data register resides at this base address, the status register at baseaddress + 1 and the control register at baseaddress + 2. Details about programming the registers can be found in any PC hardware book.

If the port is set to output mode, everything written to the data register will appear at the corresponding lines as voltages, and can be measured with a multimeter. For instance if we write 1 to the data register, pin 2 of the connector will be driven to a TTL high level of +5V. This way we can turn on and off any of the data lines. After this long introduction, the script that toggles all data lines of the parallel port is really simple:

while(1) {
  port_out(888,255); // set all data lines to +5V
  wait(-1);
  port_out(888,0);   // set all data lines to 0V
  wait(-1);
}

You can measure the voltage level with a multimeter between Pins 25 (Ground) and 2 (Data0). Be aware that on some parallel ports, not all of the pins 18..25 are connected to ground.

► latest version online