Amigorust Burrough
Amigorust Burrough

Reputation: 89

How does the A-20 line overcome the Wrapping problem

When IBM designed the IBM PC AT machines, it used their newer Intel 80286 microprocessor, which was not entirely compatible with previous x86 microprocessors when in real mode. The problem? The older x86 processors did not have address lines A20 through A31. They did not have an address bus that size yet. Any programs that go beyond the first 1 MB would appear to wrap around. While it worked back then, the 80286's address space required 32 address lines. However, if all 32 lines are accessable, we get the wrapping problem again.

The above is an excerpt from the following link on OS development :

http://www.brokenthorn.com/Resources/OSDev9.html

I do have a doubt that I do not exactly understand what exactly is the wrapping problem but I guess it means something like an attempt to access an address greater than 1 MB would reach back to an address within 1 MB since , the higher bits of the address mean nothing owing to the presence of only 20 address lines from A0-A19 .

Is my guess correct ?

My second question is how does the A-20 line solve this problem ? I guess it when enabled somehow assigns a meaning to the bits in the higher address lines but I do not know how to take this guess to a convincing answer .

So how does the A-20 line solve this problem ?

Upvotes: 0

Views: 169

Answers (1)

Margaret Bloom
Margaret Bloom

Reputation: 44046

Consider the logical address 0xffff:0xffff, its linear address is 0x10FFEF.
If take a look at 0x10FFEF in binary you'll see this:

B B    B    B    B    B  B 
i i    i    i    i    i  i
t t    t    t    t    t  t

2 1    1    1    0    0  0 
0 9    5    1    7    3  0
--------------------------
1 0000 1111 1111 1110 1111

As you see, it takes 21 bits (from bit 0 to bit 20) to represents this address. 0xffff:0xffff is a valid logical address so 0x10ffef is a valid linear address.

This shows that 16-bit programs can generate 21-bit addresses.
But the 8086 and 80186 bus width was 20 bits.
What happens now?

What happens when you drive so many miles that your odometer reads 999,999.9?

Rolling odometer

It wraps around.
Mathematically we are just discarding the 7th digit so that 1,000,000.0 turns to 000,000.0.
This is the same with 21st bit - it is discarded and 0x10ffef turns into 0x0ffef.

Another good way to think about this is doing arithmetic on a hand clock: 7 + 7 = 14 = 2.
After the end it comes again the begin.
Another way to visualise this is to imagine the memory end has being glued to the beginning:

Memory wrapped as a cylinder


Now, some programs relied on this wrapping.
Not sure if this was a form of obfuscation of if this was used by the bios that had a segment already set to point around the end of the memory.

Whatever the reason was, when the 80286 expanded the memory bus to 24 bits this wrapping was eliminated.
It's like we'd add four more digits to our odometer, 1,000,000.0 is now representable without any problem.

To emulate the old 20-bit bus address, an external AND gate was added to force the bit20 to be zero.
This is like handwriting a 0 over the 7th digit of the odometer.

Problem solved.
This was an hack, only the bit 20 is set to zero, the higher bits are untouched.
This kinds of manipulations always generated repeating patterns, it's helping to spend a bit of time to draw them - especially to see how troublesome they can get if one forgets about enabling the a20.

Upvotes: 2

Related Questions