Archives
- November 2025
- October 2025
- September 2025
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- February 2025
- January 2025
- December 2024
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- June 2021
- May 2021
- April 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- August 2010
- July 2010
Learn Something Old Every Day, Part X: The VGA Attribute Controller Is Weird
A few days ago I finally swatted a VGA emulation bug that I had known about for several years, but couldn’t identify until recently. The problem affected only Windows 3.1 running in Standard mode. It did not occur in Windows 3.1 running in 386 Enhanced mode, and it did not occur in Windows 3.0 Standard or 386 Enhanced mode.
The failure mode was rather interesting. Start Windows 3.1 in Standard mode, then start a DOS box. No problem there. Use Alt-Tab to switch to the Windows desktop, no problem. Use Alt-Tab to switch back to the DOS box—still no problem. Use Alt-Tab again to go to the Windows desktop (no problem) and then again to the DOS box: Blank screen!
Running ‘MODE CO80’ in the blank DOS box would restore visibility but a few Alt-Tab round-trips would result in a blank screen again.
After a little bit of poking around it was clear that the screen was blank because bit 5 in the VGA attribute controller index register (at I/O port 3C0h) was not set… but why? I had a strong suspicion that the problem had something to do with the attribute controller flip-flop, but couldn’t figure out what exactly.
The VGA Attribute Controller
Like many VGA features, the attribute controller is an extension of the EGA hardware. And like many other EGA registers, the attribute controller was write-only. That is, the EGA hardware registers would be written, but not read. Needless to say, this “feature” of the EGA hardware caused endless headaches for programmers… but that’s a different story.
The relevant bit is that like most other EGA function groups (CRT controller, graphics controller, sequencer), the attribute controller used an index register and a data register. But unlike the other groups, the attribute controller mapped the index and data registers at the same I/O address (3C0h)!
That sounds crazy, but it was not completely crazy. There was an internal flip-flop which controlled whether the next write to I/O port 3C0h would be directed to the index register or to a data register. Each write to port 3C0h toggled the flip-flop.
The flip-flop was reset (i.e. would point at the index register) by reading the EGA miscellaneous status register at port 3DAh (EGA in color mode) or 3BAh (EGA in mono mode).
Now, modifying the attribute controller state without visual glitches was only safe in the retrace interval on the EGA. Each write to the attribute controller registers in the EGA BIOS was therefore preceded by a loop reading the status register and waiting for the retrace.
The flip-flop was thus reset “for free” as a side effect of the necessary status register read.
The attribute controller flip-flop mechanism lent itself to using the REP OUTSB instruction directed to port 3C0h to quickly write a set of index/data pairs.
The VGA by necessity adopted the EGA behavior, but made the attribute controller registers readable. However, reading the attribute controller registers is cumbersome because reads do not toggle the flip-flop. I can only guess that this was done for backwards compatibility with the EGA, where reads from port 3C0h didn’t affect the flip-flop either (although it’s unclear why anyone would be attempting to read from a write-only register, at least intentionally).
Whatever the reason was, on the VGA reading port 3C0h returns the current attribute controller index register, while port 3C1h reads the currently selected data register. To read multiple attribute controller registers, one must reset the flip-flop, write the index register, read the data register, and repeat the cycle starting with the flip-flop reset–because otherwise a data register would be written instead of the index.
Documentation, or the Achilles’ Heel of VGA
The VGA attribute controller behavior is somewhat idiosyncratic, but it is not outrageously complicated. Except… except… everything depends on whose documentation one reads!
The description in the paragraphs above is based on IBM’s VGA documentation. It is to the best of my knowledge accurate. The trouble is that other vendors documented the attribute controller differently.
For example the 1996 Cirrus Logic CL-GD5446 Technical Reference Manual, usually a reliable source of information, documents the attribute controller registers as follows.
Port 3C0h is documented as ‘Attribute Controller Index/Data (Write)’ and port 3C1h as ‘Attribute Controller Index/Data (Read)’. That is at best misleading—port 3C1h only reads the data registers, while the index register is read at port 3C0h. The explanation in text is no better, and does not make it at all clear how attribute controller reads work.
The 1999 3Dfx Voodoo 3 datasheet is even worse. Here is an excerpt: The Attribute Index Register has an internal flip-flop, rather than an input bit, which controls the selection of the Address and Data Registers. Reading the Input Status Register 1 (port = 0x3BA/0x3DA) clears the flip flop and selects the Address Register, which is read through address 0x3C1 and written at address 0x3C0. Once the Address Register has been loaded with an index, the next write operation to 0x3C0 will load the Data Register. The flip-flop toggles between the Address and the Data Registers after every write to address hex 0x3C0, but does not toggle for reads to address 0x3C1.
The text plainly states that the address (index) register is read at 3C1h, but that’s not true–it’s read at 3C0h. The reader is also left to wonder whether reads from 3C0h toggle the flip-flop or not.
I don’t know what documentation the original author(s) of the VGA emulation in question read, but given the two above samples, it is not too surprising that they got it wrong.
Back to Windows 3.1
When running in 386 Enhanced mode, Windows 3.x traps writes to the VGA device, and therefore can keep track of the VGA state. In Standard mode, that is not possible, and the 286 VGA grabber is tasked with saving and restoring the VGA state as best it can.
In Windows 3.0, the grabber saves and restores the attribute controller data registers, but does not restore the state of the attribute index register. That quite possibly caused problems in some uncommon situations, but avoided the problem with broken VGA emulation.
The grabber in Windows 3.1 was improved to save and restore the index register as well, but it does not attempt to restore the flip-flop state, which is significant. The problem with the VGA emulation was that it erroneously applied the flip-flop state to reads from port 3C0h, and Windows 3.1 would save the wrong index register value… but only the second time through, because the flip-flop state was different at that point. That is to say, the Windows 3.1 standard mode grabber read from port 3C0h to query the attribute controller index register state, but the emulation returned the currently selected data register contents instead.
And then, when restoring the attribute controller index register the next time around, the register would be restored to the wrong value which didn’t have bit 5 set, causing the screen to go blank.
The VGA emulation fix was trivial, making reads from 3C0h always return the current index register value, regardless of the flip-flop state. Years to find, seconds to fix.
As an aside, there are semi-undocumented registers in the VGA CRT controller register range which allow reading the current attribute controller flip-flop state. Register CR24 reads the flip-flop state, and register CR26 reads the attribute controller index state. The latter seems at first glance redundant.
Windows 3.1 makes no attempt to use these registers. Without a wider survey it is unclear how widely implemented those registers were. It is also quite unclear why IBM didn’t document the registers, since they filled a real need—together with register CR22, which reads the current value of the graphics controller latches, register CR24 makes it possible to read otherwise hidden internal VGA state, which is required to fully save and restore the state of the VGA. Mysteries of PC hardware.
9 Responses to Learn Something Old Every Day, Part X: The VGA Attribute Controller Is Weird
In what emulator did you make that fix?
I guess the fix would need to be to Windows itself?
Nah, the 286 grabber does what it can. It could be possibly improved by using the undocumented CRTC register to read the current flip-flop state, but either Microsoft didn’t know about it or they found that it wasn’t supported everywhere.
The EGA is a nightmare to virtualise, which is interesting since it was designed about when the PC/AT was supposed to offer next generation multitasking environments. Some minor design decisions would have made it much easier to deal with.
I wonder if the idea was that in a multitasking environment, only the OS would be accessing the EGA hardware. And since the OS driver knows what it wrote to the hardware, it doesn’t need to ask the hardware what its state is. Of course it didn’t work out that way.
But that’s basically what the EGA.SYS driver did. Have one central place that writes the EGA registers, remembers what it wrote, and can be asked what the hardware state is.
I wonder if it is also to save a few transistors in the chips that the original IBM EGA cards used.
A weird thing is that IBM had ASICs on their original EGA card. That means that write-only registers/flags isn’t as reasonable as if it was built using discrete logic chips. Sure, as Yuhong Bao suggest it would save some transistors, but not that many.
As a time scale reference, in the mid 1970’s generic I/O chips (like the 6821, 6522 and whatnot) allowed read back of written information. A year after EGA came to market the Amiga came out using about as many chips for it’s graphics hardware as an EGA card does.
Did IBM on purpose use older technology when designing their ASICs to ensure reliability? Or wasn’t IBM able to have the actual chip manufacturers use the latest capabilities?
Remember we are talking about in 1983 here as well.
(for comparison the original 8088 had 29000 transistors)
This site uses Akismet to reduce spam. Learn how your comment data is processed.