I had this problem a while back and asked here for how I would go about solving it. After some testing I came up with a solution.. it seems to work on all the hardware I have (though I don't have any very ancient computers to test this on).
I'm not going to post the source unless you really want it as it has always seemed more fun and fulfilling to me to figure out something on my own
However, I will give you the general idea of what my code does.
My code has a few functions, which are:
- ToggleA20BIOS - Toggles the A20 line from whatever it was using the BIOS
- ToggleA20Fast - Toggles the A20 line from whatever it was using the Fast A20 Line
- ToggleA20Keyboard - Toggles the A20 line from whatever it was using the Keyboard Controller
- CheckA20Wrap - Checks whether the A20 line is enabled by seeing if memory wraps around
- A20SupportBIOS - Checks for various methods of changing the A20 line through the BIOS (BIOS, Fast A20, Keyboard Controller, etc..)
- ToggleA20 - Toggles the A20 line using the above functions
- EnableA20 - Enables the A20 line using the above functions
- DisableA20 - Disables the A20 line using the above functions
Basically for the last two methods the procedure is roughly the same.
First, we check if the A20 line is enabled / disabled. Then, we check for BIOS support for the A20 line. If the BIOS support function works, it will let us know whether we can use the other BIOS functions, the Fast A20 function, or the keyboard controller to enable / disable the A20 line. If it doesn't work, we will assume that the BIOS functions are not available and will proceed to first use the keyboard controller, then the Fast A20 if that does not work. In any case, the general procedure is to first do the BIOS function, then the Keyboard Controller, then then Fast A20. If none of these things work we simply return an error code.
On a final note, you may notice that I created Toggle functions instead of Enable / Disable functions for each different way to edit the A20 line. This allows us to write enable / disable code around the toggle code, thus re-using code that we normally would have to rewrite with a few small differences.
I hope this helps!
- Adrian