Port mapped vs. memory mapped I/O

Microprocessors normally use two methods to connect external devices: memory mapped and port mapped I/O.  To understand how to emulate microprocessors (for gaming or other purposes) it is important to understand this subtle difference.  As far as the peripheral is concerned, both methods are really identical.  A device connected to a microprocessor must decode its address from various numbers of address lines and read/write its data from various numbers of data lines.  The difference between the two schemes occurs within the microprocessor.  Intel has, for the most part, used the port mapped scheme for their microprocessors and Motorola has used the memory mapped scheme.  It is certainly possible for a hardware engineer to design a system with memory mapped I/O which uses a CPU supporting port mapped I/O; below you can see why that would not necessarily be a good idea.  Here are the basic differences:

Memory Mapped I/O
I/O devices are mapped into the system memory map along with RAM and ROM.  To access a hardware device, simply read or write to those 'special' addresses using the normal memory access instructions.  The advantage to this method is that every instruction which can access memory can be used to manipulate an I/O device.  The disadvantage to this method is that the entire address bus must be fully decoded for every device.   For example, a machine with a 32-bit address bus would require logic gates to resolve the state of all 32 address lines to properly decode the specific address of any device.  This increases the cost of adding hardware to the machine.

Port Mapped I/O
I/O devices are mapped into a separate address space.  This is usually accomplished by having a different set of signal lines to indicate a memory access versus a port access.  The address lines are usually shared between the two address spaces, but less of them are used for accessing ports.  An example of this is the standard PC which uses 16 bits of port address space, but 32 bits of memory address space.  The advantage to this system is that less logic is needed to decode a discrete address and therefore less cost to add hardware devices to a machine.  On the older PC compatible machines, only 10 bits of address space were decoded for I/O ports and so there were only 1024 unique port locations; modern PC's decode all 16 address lines.  To read or write from a hardware device, special port I/O instructions are used.  From a software perspective, I feel that this is a slight disadvantage because more instructions are required to accomplish the same task.  For instance, if you wanted to test one bit on a memory mapped port, there is a single instruction to test a bit in memory, but for ports you must read the data into a register, then test the bit.

The way this applies to arcade emulation is that the port I/O handling routines of your CPU emulator do not need to have special flags or logic for handling reads and writes to the port address space since every access to a port will be for the purpose of communicating with a hardware device.   Therefore, in the simplest case, the port I/O handler routine will simply be a switch statement on the port address.  Memory mapped I/O is harder to emulate because you must maintain some sort of flag list for each address to know if it is ROM, RAM or a 'special' address referencing a hardware device.  See my article on CPU emulation for a reasonable way to code this into your emulator

Back