CLOSE

A20 Line

The A20 address line is the physical representation of the 21st bit (number 20, counting from 0) of any memory access.

Intel 8086 processor does not have the A20 line. The A20 line was introduced with the Intel 80286 processor as a part of the x86 architecture.

  • The A20 line was introduced with the Intel 80286processor, which was released in 1982.
  • Prior to the 80286, the Intel 8086processor could only directly address 1 MB of memory with addressing range from 0x00000 - 0xFFFFF (1,048,576).
  • The A20 line allowed the processor to address up to 1 MB of additional memory, bringing the total addressable memory to 1.64 MB.

The A20 line was introduced to address a limitation in the original IBM PC architecture. In the early days of computing, IBM designed the Intel 8088 processor, which could only directly address 1 MB of memory. However, the architecture reserved the last 384 KB of this memory for system purposes, leaving only 640 KB available for user programs.

To work around this limitation and enable access to more memory, the Intel 80286 processor introduced the A20 line. This additional address line allowed the processor to address up to 1 MB of additional memory, bringing the total addressable memory to 1.64 MB.

The need for the A20 line arose because of the way the 8088 processor handled memory addresses. The 8088 processor only used 20 address lines to address memory, but it could only address up to 1 MB of memory directly. When the 8088 processor accessed memory, it wrapped around back to the beginning of the memory address space after reaching the 1 MB limit.

However, this wrapping behavior caused a problem when accessing memory beyond the 1 MB limit. For example, when trying to access the memory address 0x100000, the processor would wrap around to address 0x00000, effectively accessing the same physical memory location.

To prevent this wrapping behavior and enable access to memory beyond 1 MB, the A20 line was introduced. By enabling the A20 line, the processor could address memory in a linear fashion, allowing access to the full 20-bit memory address space.

Problem: Limited Memory Addressing

  • The Original Intel 8086 processor, released in 1978, could only directly address 1 MB of memory.
  • In real mode, the processor used 20 address lines for memory addressing, but the last 384 KB of this memory were reserved for system purposes, leaving only 640 KB available for user programs.
  • Accessing memory beyond the 1 MB limit without the A20 line enabled resulted in wrapping.
  • Wrapping: When the processor accessed memory beyond the 1 MB limit, the memory addresses wrapped around to the beginning of the memory address space. For example:
    • Accessing memory address 0x100000 would wrap around to address 0x00000, effectively accessing the same physical memory location. Because 2^20 has addressing range from 0x00000 to 0xFFFFF. As soon as we try to address new memory address which is 0x100000 then it get wrapped around to 0x00000.
    • This wrapping behavior caused addressing errors and system instability.

History

1 Intel 8086 (1978):

  • The Intel 8086 was the first 16-bit microprocessor in the x86 family.
  • It had a 20-bit address bus, allowing it to directly address 2^20 (1,048,576) memory locations.
  • However, due to limitations in the IBM PC's memory design, only 1 MB of memory was made directly accessible to the processor.
  • Without the A20 line enabled, the addressing range of the Intel 8086 processor in hexadecimal was from 0x00000 to 0xFFFFF.

2 Intel 8088 (1979):

  • The Intel 8088 was a variant of the 8086 processor with an 8-bit external data bus.
  • Like the 8086, it had a 20-bit address bus and could directly address 2^20 memory locations. These 20 bits could hold a number between 0 and 2^20 = 1,048,576, which is a number of different memory cells we could address. But we never address individual cells - rather we could address blocks of 8 cells (one byte).
  • Without the A20 line enabled, the addressing range of the Intel 8088 processor in hexadecimal was also from 0x00000 to 0xFFFFF.
Memory Addressing (8088 Processor)
---------------------------------------
|  Address (Hex)   |   Memory Content  |
|--------------------------------------|
|     0x00000      |   Byte 0          |
|     0x00001      |   Byte 1          |
|        .         |     .             |
|        .         |     .             |
|     0x9FFFF      |   Byte 640 KB - 1 |
|     0xA0000      |   Byte 640 KB     |
|     0xA0001      |   Byte 640 KB + 1 |
|        .         |     .             |
|        .         |     .             |
|     0xFFFFE      |   Byte 1 MB - 2   |
|     0xFFFFF      |   Byte 1 MB - 1   |

3 Intel 80286:

  • The Intel 80286 processor had a 24-bit address bus, allowing it to directly address up to 2^24 memory locations.
  • It supported both protected mode and real mode. When in real mode the 80286 supposed to behave exactly the same as an 8088 or 8086, for compatibility. 
Memory Address Space (80286 Processor)
---------------------------------------
|             System Memory           |
|-------------------------------------|
|             User Memory             |
|-------------------------------------|
|          16 MB Address Limit        |
|-------------------------------------|
|         15.75 MB User Memory        |
|-------------------------------------|
|           256 KB System Memory      |
---------------------------------------
Memory Addressing (80286 Processor)
---------------------------------------
|  Address (Hex)   |   Memory Content  |
|--------------------------------------|
|     0x000000     |   Byte 0          |
|     0x000001     |   Byte 1          |
|        .         |     .             |
|        .         |     .             |
|   0xFFFFFFFE     |   Byte 16,777,214 |
|   0xFFFFFFFF     |   Byte 16,777,215 |
---------------------------------------

Introduction of the A20 line:

  • The A20 line was introduced with the Intel 80286 processor, released in 1982.
  • The A20 line allowed the processor to address up to 2^20 memory locations directly.
  • Enabling the A20 line removed the limitation of accessing only 1 MB of memory directly.
  • With the A20 line enabled, the addressing range of the Intel 80286 processor in hexadecimal remained from 0x00000 to 0xFFFFF, but it allowed access to the additional memory beyond the 1 MB limit.

4 Intel 80386:

  • The Intel 80386, also known as the i386, was a 32-bit microprocessor introduced by Intel in 1985.
  • It was a significant advancement over its predecessors, the 8086 and 80286, due to its 32-bit architecture.
  • The 80386 had a 32-bit address bus, allowing it to directly address up to 2^32 (4,294,967,296) memory locations.
  • With its 32-bit architecture, the 80386 could address a theoretical maximum of 4 GB of memory.
  • Similar to its predecessors, the Intel 80386 processor also relied on the A20 line for memory addressing.
  • Enabling the A20 line allowed the processor to access memory beyond the 1 MB limit, providing access to the full 32-bit address space.
  • With the A20 line enabled, the addressing range of the Intel 80386 processor in hexadecimal was from 0x00000000 to 0xFFFFFFFF.

It had 32 address lines, that's equivalent to 4GB of memory.

Legacy Pain: It has 32 register, which is wide enough to address the entire 4GB space in one go without any of the segmentation.

But – the CPU won't let us because of technical debt. when CPU firstly boots up, it assumes that there are only 20 address lines. We cant' use the rest, so we limited to accessing only 1MB of memory.

In the 8088, you could access 1MB of memory using the segmented real-mode mode. But consider the following memory address: FFFF:0010, it's a valid real-mode address. But as it turns out, it points to a place beyond one megabyte.

  • The segment 0xFFFF, times 16 bytes, points to linear address 0xFFFF0. That's byte number 1,048,560.
  • The offset Ox0010 adds 16 bytes to that. That gives us a final position of 1,048,576.
  • But One megabyte is a range of 0 through 1,048,576. We are actually pointing to a place one byte past the one megabyte mark.

What would happen in this case that (on the Intel 8088 processor), the memory would “wrap around”. The address 0xFFFF:0010 would actually point to address 0000:0000. Similarly, address 0xFFFF:00011 would point to 0000:0001 and so on.

  • Compatibility Issue: Code written to rely on memory wraparound behavior at the 1 MB mark would cease to function.
  • Solution: To maintain compatibility with such code, the A20 line was disabled by default on the Intel 80286 processor.

Intel 80286 processor introduced more address lines, code that depended on the memory wrapping at the one megabyte mark would cease to function. Apparently there was enough of this code around to make Intel engineers decide to disable the additional address lines, and it became necessary to manually turn them on to make use of the higher memory. More precisely, one has to turn on the 21st address line in order to make all higher address lines available. This address line is known as A20 line (Since counting start from zero).

When IBM PC AT System was introduced, the new Intel 286 processor was not compatible with the old x86 processor. The older x86 micro-processors(Intel 8086) had address bus of 20 bits which would total and give access up to 1 megabyte of memory. The Intel 386 and above had address bus up to 32 bits allowing 4 Gigabytes of memory. But the old 8086 processors did not have such a big address bus. To keep in compatible with the older processors and solve the problem the Intel introduced a logical OR gate at the 20 bit of the address bus which could be enabled a or disabled. So, to keep in compatible with the older processors and programs the A20 is disabled at the startup.

NOTE: BIOS actually enables the A20 for counting and testing the available memory and then disables it before booting again to keep in compatible with older processors.

The A20 gate is an electronic OR gate which can be disabled and enabled and placed at the 20th bit of the address bus. It is connected through a P21 line of the keyboard controller which made it possible for the keyboard controller to enable or disable the A20 Gate.

In the current generations, there is a need to have memory lot more than just 1MB.The applications, games, etc. need a lot of memory. Even a operating system kernel might eat up the whole 1MB.So it is something like impossible to run modern programs in 1MB of memory. Looks like the A20 is an important feature for good functional of the operating system.

To enable the A20 gate there are 3 methods or you can skip this step by using the high memory managers such as HIMEM.sys or using bootloaders such as GRUB(GRUB will set up you up with protected mode with A20 enabled)

The 3 methods for enabling the A20 Gate are
1. Keyboard Controller
2. BIOS Function
3. System Port

Turning on the A20 Line

Turning on the 21st address line can be a bit complex. Here are the steps to do it:

  1. Keyboard Controller Method:
    1. Sending a command to the keyboard controller to enable A20.
  2. BIOS Method:
    1. Calling a BIOS function to enable A20.
  3. Direct Hardware Method:
    1. Toggling the A20 gate directly through hardware.

1 Keyboard Controller:

This is the most common method of enabling A20 Gate. The keyboard micro-controller provides functions for disabling and enabling A20.Before enabling A20 we need to disable interrupts to prevent our kernel from getting messed up. The port 0x64 is used to send the command byte.

  • The keyboard controller method involves sending a specific command sequence to the keyboard controller.
  • This method is based on the fact that the keyboard controller is one of the few components of the PC that is guaranteed to be present and accessible from real mode.
  • The sequence consists of :
    • Sending the Write Output Port command to the keyboard controller.
    • Setting the appropriate bit in the Output Port to enable the A20 line.

Command Bytes and ports
0xDD Enable A20 Address Line
0xDF Disable A20 Address Line

0x64  Port of the 8042 micro-controller for sending commands

Using  the keyboard to enable A20:

EnableA20_KB:
cli                ;Disables interrupts
push    ax         ;Saves AX
mov al, 0xdd  ;Look at the command list 
out 0x64, al   ;Command Register 
pop ax          ;Restore's AX
sti                ;Enables interrupts
ret 

2 BIOS Method:

  • The BIOS method involves calling a specific function provided by the system BIOS to enable the A20 line.
  • This method is more abstract and relies on the BIOS to perform the necessary low-level hardware operations.
  • The function is typically called using software interrupt 0x15, function 0x2401.

The INT 15 2400,2401,2402 are used to disable, enable, return status of the A20 Gate respectively.

Return status of the commands 2400 and 2401(Disabling, Enabling)
CF = clear if success
AH = 0
CF = set on error
AH = status (01=keyboard controller is in secure mode, 0x86=function not supported)

Return Status of the command 2402
CF = clear if success
AH = status (01: keyboard controller is in secure mode; 0x86: function not supported)
AL = current state (00: disabled, 01: enabled)
CX = set to 0xffff is keyboard controller is no ready in 0xc000 read attempts
CF = set on error

; Disabling the A20

push ax
mov ax, 0x2400 
int 0x15 
pop ax

; Enabling the A20 
push ax
mov ax, 0x2401 
int 0x15 
pop ax

; Checking A20
push ax
push cx
mov ax, 0x2402 
int 0x15 
pop cx
pop ax

3 Direct Hardware Method:

  • The direct hardware method involves directly toggling the A20 gate through hardware.
  • This method requires detailed knowledge of the hardware architecture and may vary depending on the specific hardware configuration.
  • It typically involves accessing and toggling the appropriate control signals on the system chipset.
  • While this method provides the most direct control, it is also the most complex and hardware-dependent.

This method is quite dangerous because it may cause conflicts with some hardware devices forcing the system to halt.

Port 0x92 Bits

  • Bit 0 - Setting to 1 causes a fast reset 
  • Bit 1 - 0: disable A20, 1: enable A20
  • Bit 2 - Manufacturer defined
  • Bit 3 - power on password bytes. 0: accessible, 1: inaccessible
  • Bits 4-5 - Manufacturer defined
  • Bits 6-7 - 00: HDD activity LED off, 01 or any value is “on”

Code to enable A20 through port 0x92

push ax
mov al, 2 out   0x92, al pop ax