Typical program has three basic tasks
IO in a typical computer system is not what it normally is in a high level language. In fact, to send data to an output device, the CPU simply moves that data to a special memory location and to read it, it moves data from the devices address into the cpu.
IO operations are like regular memory reads and writes, except there are more wait states.
IO ports split into 5 categories: read only, write only, read write, dual io, bidirectional.
Read only is an input port. CPU can only read the data from the port. The hardware ignores attempts to write to that peripheral port.
Write only port is always an output port. An output port typically uses a latch device to hold data to be sent to the outside world. The latch stores the data and makes it available on an external set of lines.
A read/write port is an output port as far as the outside is concerned, but the cpu can also read it. Reading from the port does not make the value disappear
A dual port is really two ports, a read only and a write only, with a multiplexer line between the two. You read data from an external input device this way, and can write to another port.
Bidirectional allows cpu to read and write to external device on the same port.
Note that you don’t always have to use the data bus to transfer stuff. You can have special circuitry that accesses the special memory addresses directly.
There are three basic io mechanisms that computer systems can use to talk to peripheral devices:
Connected to the CPUs address and data lines exactly like regular memory. So whenever a cpu writes to or reads from the addresses, the data gets transferred. The big advantage is that cpu can use any instructions it wants to interact with this
Disadvantage is they consume addresses in the memory space. Every byte that they use ends up taking a byte from the max RAM you have. The values also cannot be cached
Same as memory mapped but uses its own far away address space and special machine instructions to interact with that address space. Note that the same data bus is used. However, additional control lines dictate whether the data is IO or Memory related.
note that this means you can use IO mapped and memory mapped together on the same board.
DMA works for very high speed IO devices where the cpu may be too slow to process data one byte at a time. These devices access memory directly, without the need to read and copy memory around using the data bus.
High speed devices(which transfer data faster than the CPU is able to process) must use DMA. Low and medium may use whichever they want.
Many computers have more than one bus going to ports. Maximizing performance for a particular kind of bus may require different programming techniques. There is a main address and data bus, and that interacts with a pci bus controller which connects to pci ports etc. That controller might connect to other ports as well with their own controllers.
This stuff is actually similar to routing. All of this stuff is similar to networking. It’s all the same. Physics, caches, and data transfer. The reason networking is tough is because it’s another layer but the reality is that, at the end of the day, you have very similar concerns and things to optimize.
You can also figure out how to get things to play nice by using buffering.
In a protected mode OS, programs without permissions cannot just access pins and ports directly. Instead, they must call into OS routines to do things. However, this is slow. On the other hand, there are OS modules people write that follow a specific interface and work with different device drivers. So, to work with a network card, someone wrote a network card driver for your specific model which you install into your linux OS. Then through uniform calls, you can interface with that hardware. In most modern OS’s, working with peripherals is just like working with files, except you open a special file. The files act as memory buffers, the drivers read the files and push the data to the pins. More on this in the x64 book.
The rest of the chapter details specific peripherals, which relate to IBM PCs. This is probably not worth going over in detail, but rather just skimming. Working with assembly to talk to stuff will be fun though.