Author Topic: Question about CGA graphics  (Read 9578 times)

Offline ben321

  • Full Member
  • **
  • Posts: 184
Question about CGA graphics
« on: June 15, 2016, 04:12:18 AM »
How do video modes 4 and 5 work? I know they start at address 0xB8000, but it seams that instead of one picture, it is divided into 2 fields (first field is lines 0, 2, 4, etc; second field is lines 1, 3, 5, etc). However, the 2 fields don't appear to be completely consecutive. The second field doesn't seem to start exactly one byte after the first field ends, as I had assumed it would. I wrote a CGA graphic program in NASM and compiled it to a raw COM file, based on the previously mentioned assumption, and while the first field displays correctly, the second field doesn't line up with the first one. It appears to be shifted to the left, and a bit up, compared to the first field. Maybe somebody here can tell me the exact memory address that the second field starts on.
« Last Edit: June 15, 2016, 04:21:59 AM by ben321 »

Offline ben321

  • Full Member
  • **
  • Posts: 184
Re: Question about CGA graphics
« Reply #1 on: June 15, 2016, 04:36:11 AM »
Ok, I just figured it out, by trial and error. It turns out that the second image field starts at address 0xBA000. Of course, this leaves 192 bytes of data, apparently unused, between the end of the first image field, and the start of the second image field. Does the CGA graphics card actually use this region of VRAM for anything?

Offline ben321

  • Full Member
  • **
  • Posts: 184
Re: Question about CGA graphics
« Reply #2 on: January 15, 2022, 07:01:06 PM »
Upon further research, this seems to be something that is done intentionally to pad out each video field's data to a power-of-2. The CGA image is 320x200 but unlike VGA mode, the image is split into 2 fields (useful because it was designed to be shown on a TV which uses interlaced scanning). Each field of a CGA image is therefore 320x100, and each field takes 8000 bytes (4 pixels per byte, so 80 bytes per line, and 100 lines per field, equals 8000 bytes per field). This isn't a power-of-2 number of bytes, so it's padded up to the next power-of-2, which is 8192. This is why there are 192 bytes between fields. The padding bytes aren't actually used for storing any data, as far as I know.

I just wished this padding was something better documented. When I got strange results from experimenting before, it took trial and error for me to find that the second field started at 0xBA000. There was literally no documentation I could find on Google with searches like "In CGA graphics, what is the address of the second field". And only once I had found this address was I able to use it in my searches to find more info on why the address for the second field was there. If not for my trial and error, I would likely have never figured out the exact address for the second video field in CGA graphics.

Maybe some of the NASM website admins can include on this official NASM website (nasm.us), a section on DOS programming including something that mentions this important CGA address, and that CGA is actually split into 2 fields (with starting addresses of 0xB8000 and 0xBA000).

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Question about CGA graphics
« Reply #3 on: January 15, 2022, 11:19:56 PM »
Ralf Brown's Interrupt List.

If you consult the online version(s) you  will probably see only the interrupt list itself. If you download and install the entire package, there's the ports list, instructions list, memory list... enormous amount of information! Thanks Ralf!

Best,
Frank