2

I am interested in knowing how does the CPU writes data to (and reads data from) an IO port (for example: serial/parallel/USB).

Are there instructions that sends data directly to an IO port, for example:

send the number 3 to pin 0 of serial port 1

Or does the CPU writes to a specified memory location, and once the data is written to that memory location, the data will automatically be sent to the IO port, for example:

send the number 3 to memory location 0x12345

Now once the number 3 is written to memory location 0x12345, it will automatically be sent to pin 0 of serial port 1.

John
  • 1,049
  • 1
  • 14
  • 34

3 Answers3

5

First, an "I/O-port" in CPU terminology is not the same as, for example a serial port. It is an addressable entity within the CPU's address space that can be read and/or written to. I/O ports are typically byte, word, or longword sized (which means you simply don't address specific bits, but rather write at least bytes to the port. Single bit setting must be done by reading the port, setting a bit and write back).

Your serial port example would normally break down into a number of I/O ports (like, for example, a port that holds the last received byte, a port that is written to to send a byte and one or more control ports to control the hardware, like setting baud rates or other behavior).

Generally, there's two types of CPU designs related to input/output:

  1. memory-mapped I/O - Here, the CPU addresses the I/O port just like it would address memory - The I/O port is located within the "normal" memory address space of the CPU, the CPU itself has no specific instructions for I/O, because it will use the very same instructions it uses to access memory. The CPU control bus does normally not have specific lines for I/O port access. Typical examples for CPUs implementing memory-mapped I/O would be Motorola 68000 or MOS 6502.
  2. Separate I/O address space - Here I/O ports are located outside the "normal" memory address space of the CPU. The CPU also has specific instructions to access I/O ports like IN or OUT. Normally, the CPU control bus also has specific control lines that signal to external hardware that the CPU wants to address within the I/O address space, like IOREQ. Typical examples for CPUs implementing this approach are the Zilog Z80 or Intel x86 processors. Note that CPUs supporting a separate I/O address space are perfectly capable of supporting memory-mapped I/O as well, so there can be mixed-mode approaches.
tofro
  • 5,640
  • 14
  • 31
  • So when I want to write to a device, I can simply write to some memory location and not have to worry about actually sending the data to the serial port? And does all devices take the same amount of memory or can a device specify how much memory it needs, for example: can a printer have 1MB of memory while a webcam have 2MB of memory? – John May 29 '17 at 12:56
  • @John In case of memory-mapped I/O, yes. But your imagination of register space in an I/O device is way over the top. Typical I/O devices don't occupy megabytes, but rather from several (<10, for a PS/2 keyboard, for example) to several kBytes (maybe a hard disk or floppy device). Graphic cards are an exception, because they normally expose a lot of video memory (memory-mapped, obviously) to the CPU. – tofro May 29 '17 at 13:03
  • I have read that each device have its own internal registers that they map to RAM, but is the number of registers the same for all devices, or can each device have any number of registers (for example: can a printer have 4 registers while a webcam have 7 registers)? – John May 29 '17 at 13:13
  • @John Sure. Depends not only on the device class, but rather on vendor and make of the I/O chip. Let's have a look at the 8255 PIO, a typical I/O chip used to connect parallel printers: This has 3 8 bit-parallel registers that can be read or written plus 1 control byte that configures the chip for input and output that is write-only. – tofro May 29 '17 at 13:23
  • @tofro I have two questions if you don't mind: 1) did printers had a parallel port only used for printers? 2) was the 8255 PIO chip part of this parallel port for printers, or was it part of the printer itself? – Christopher Jun 04 '17 at 09:45
  • @Christopher The simplest design for a printer connection would have been a 8255 PIO on both ends - one on the computer end, another on the printer end of the cable, although a lot of designs (IBM PC, for example) used simpler chips for cost reasons. And I don't get the (1) part of your question - Obviously, a parallel port (look up "Centronics") on a printer is only used for that printer... – tofro Jun 04 '17 at 10:38
  • @tofro "And I don't get the (1) part of your question" what I mean is: did the computer had a parallel port that only printers could connect to, or was the parallel port generic so that any parallel device can connect to? – Christopher Jun 04 '17 at 10:51
  • @Christopher Even if the (for example) IBM PC's parallel printer port was intended mainly for printers (hence the name), you could use it to connect other 8bit-parallel things as well, like for example the LapLink parallel cable that allowed high-speed connectivity between PCs. – tofro Jun 04 '17 at 12:15
2

Hello I am a student major in electronic engineering. In our class, microprocessor there are some addresses related to serial communication. So if you write data to the address, the hardware automatically transfer the data through the serial.

For example, let us there is an address called TXR(0xffff01) And a CPU do follow instruction : send 143 to 0xffff01 address. Then in 0xffff01 location, there will be 143 value. After few clocks, the serial hardware automatically read the data from TXR(0xffff01) and transmit it.

In this case, TXR is a kind of registers. So if you want to do something, what you have do is just send data to correspond address(register).

Thank you.

박광렬
  • 21
  • 1
-1

Usually the registers of an I/O device are mapped to memory. This will be well defined in documentation of the device.

For example to send data to a device the way it goes is you write data to a buffer, then you set a specific register to a certain value which signals the device that there is data in the buffer it needs to get. The buffer to which you write can be device memory mapped in the same address space as system memory, or can be system memory. The setting of the register is done by writing to the memory location of it's mapping.


By far not an expert on the matter, so if I am wrong please point out and I will delete this.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • register of IO device is not mapped to memory. Its mapped to IO port and IO port can be mapped to memory. Which doesn't require the IO device know is its register mapped to memory or not. The IO device only know that the its register mapped to IO port – Blanket Fox Jun 29 '21 at 03:52