I understand the CPU communicates with IO devices through their IO port address (usually 16-bits) but I'm wondering who does the IO port address assignment? BIOS? CPU? OS? Are these addresses preset or dynamic?
2 Answers
It depends on the type of device you are talking about:
If it is a PCI or PCI Express device, the base I/O addresses are set by the BIOS at boot time. And they may get remapped by the Operating System if needed (consider the case of a hot-swappable device).
If it is a traditional ISA slot device (non-PnP), the base address is typically set by jumpers or DIP switches on the board. In that case, the base address cannot be changed dynamically.
Some ISA boards supported ISA PnP, which allowed their base address to be set by the BIOS or OS at boot time.
In a PC, there are also several "ISA" devices built into the motherboard/chipset that live at fixed I/O addresses (e.g. PS/2 keyboard controller). There are hard-coded and do not change.
Some on-motherboard peripherals like serial ports and parallel ports have their base address configured in the BIOS setup. In that case, the BIOS setting behaves like a jumper or DIP switch.
Normally the addresses of addressable I/O are assigned by hardware.
Literally, there is logic circuitry (either internal to the processor or external) which watches the bus for a specific address, and causes a peripheral function register to latch the data which is being written off the bus, or drive the data being read onto it.
Sometimes the address of a particular peripheral is the sum of a base address and an internal address, where the base address may be determined by a DIP switch or jumpers or even (in some historic cases, but not in the IBM PC) the slot an expansion card is plugged into.
In more complicated interfaces such as PCI, it is possible that the I/O base address might(?) be assigned by software after discovery. While a traditional local bus interface can simply be a few logic gates, a PCI interface is quite complicated with a lot of configuration capabilities.