DOS VGA Tricks

Discussion of development of software for any "obsolete" computer or video game system.
User avatar
Nikku4211
Posts: 401
Joined: Sun Dec 15, 2019 1:28 pm
Location: Bronx, New York
Contact:

DOS VGA Tricks

Post by Nikku4211 » Mon Nov 23, 2020 9:01 pm

I love the tricks that can be used by DOS games to do various interesting things with Video Graphics Array display adapters.

For example, Mike Hawk made a tutorial on how to access this Mode Y tweaked version of Mode 13h (featuring 4 pages for double-buffering action and the ability to scroll between them) using QuickBASIC.

I have used the code from that tutorial and added some of my own to make this QuickBASIC demo that changes the VGA start address, achieving smooth vertical hardware scrolling. The code is in the video's description, by the way.

People have also discovered Mode X, which is like Mode Y, but has less than 4 pages, a 320x240 resolution with square pixels, and runs at 60hz instead of 70. There's also Mode Q, which has a resolution of 256x256, so only a single byte is required to access either X or Y.
Last edited by Nikku4211 on Tue Nov 24, 2020 6:40 am, edited 1 time in total.
I have an ASD, so empathy is not natural for me. If I hurt you, I apologise.

lidnariq
Posts: 10454
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: DOS VGA Tricks

Post by lidnariq » Mon Nov 23, 2020 9:30 pm

Have you played around with Tweak ("tweak16b.zip")?

I enjoyed it, when it was still relevant.

FRACTINT, Scorched Earth, and Tran's tweaked demos (Timeless, Ambiance, Luminati) also use some funky wacky modes. Maaaybe the Moraffware games do too.

Oziphantom
Posts: 1113
Joined: Tue Feb 07, 2017 2:03 am

Re: DOS VGA Tricks

Post by Oziphantom » Mon Nov 23, 2020 10:53 pm

For such things "Michael Abrash's Graphics Programming Black Book" is the book

User avatar
rainwarrior
Posts: 8016
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: DOS VGA Tricks

Post by rainwarrior » Tue Nov 24, 2020 2:42 am

I second Oziphantom's suggestion.

Also the book is online for free: https://www.drdobbs.com/parallel/graphi ... /184404919

I used Mode X for a recent port I made of my NES game. One of the consequences of 320 x 240 is the refresh rate lowers to 60hz instead of 70hz, which is probably a better choice these days anyway. The other thing I wanted was double buffering.

I wouldn't say it has "3 pages"... it has more than that... but really it just has 256k of memory. 320 x 200 unchained fits 4 pages evenly, but I don't know what you'd use 4 actual video pages for? 2 is good for double buffering... but there are some uses for the rest of that memory, e.g. if you store sprites there there are some tricks to copy memory faster from VRAM to VRAM than you can from other memory regions.

User avatar
rainwarrior
Posts: 8016
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: DOS VGA Tricks

Post by rainwarrior » Tue Nov 24, 2020 3:20 am

Also, I really appreciate David Brackeen's excellent VGA programming tutorial which has been online for more than 20 years now:

http://www.brackeen.com/vga/index.html

User avatar
Nikku4211
Posts: 401
Joined: Sun Dec 15, 2019 1:28 pm
Location: Bronx, New York
Contact:

Re: DOS VGA Tricks

Post by Nikku4211 » Tue Nov 24, 2020 6:43 am

rainwarrior wrote:
Tue Nov 24, 2020 2:42 am
I used Mode X for a recent port I made of my NES game. One of the consequences of 320 x 240 is the refresh rate lowers to 60hz instead of 70hz, which is probably a better choice these days anyway. The other thing I wanted was double buffering.
I heard that 60hz 320x240 actually does have quite a bit of flicker, at least on actual DOS machines. 60hz is better for modern monitors, though.
rainwarrior wrote:
Tue Nov 24, 2020 2:42 am
I wouldn't say it has "3 pages"... it has more than that... but really it just has 256k of memory. 320 x 200 unchained fits 4 pages evenly, but I don't know what you'd use 4 actual video pages for? 2 is good for double buffering... but there are some uses for the rest of that memory, e.g. if you store sprites there there are some tricks to copy memory faster from VRAM to VRAM than you can from other memory regions.
4 actual video pages might be used for actual smooth hardware scrolling, if you manage to do both horizontal and vertical. If you don't use the entire screen in Mode Y, or you use something smaller than 320x200 like 256x200, then you might have some space for storing standalone sprites in VRAM to copy over to your screen.
I have an ASD, so empathy is not natural for me. If I hurt you, I apologise.

Oziphantom
Posts: 1113
Joined: Tue Feb 07, 2017 2:03 am

Re: DOS VGA Tricks

Post by Oziphantom » Tue Nov 24, 2020 6:59 am

4 buffers is overkill, you won't have any need for anything over 3. Tripple-Buffering is a smooth as you can get, adding Quad-Buffering is just a waste of memory. Unless you are doing an optimization for rendering as done in Another World, but that is custom edge case for a peculiar rendering engine.

User avatar
Nikku4211
Posts: 401
Joined: Sun Dec 15, 2019 1:28 pm
Location: Bronx, New York
Contact:

Re: DOS VGA Tricks

Post by Nikku4211 » Tue Nov 24, 2020 7:56 am

Oziphantom wrote:
Tue Nov 24, 2020 6:59 am
4 buffers is overkill, you won't have any need for anything over 3. Tripple-Buffering is a smooth as you can get, adding Quad-Buffering is just a waste of memory. Unless you are doing an optimization for rendering as done in Another World, but that is custom edge case for a peculiar rendering engine.
So you can use 3 pages for smooth VGA hardware scrolling?

I know of this DOS Mode X VGA engine, which is pretty cool.
I have an ASD, so empathy is not natural for me. If I hurt you, I apologise.

Oziphantom
Posts: 1113
Joined: Tue Feb 07, 2017 2:03 am

Re: DOS VGA Tricks

Post by Oziphantom » Tue Nov 24, 2020 8:23 am

Buffers don't get you smooth scrolling. You can smooth scroll with 1 buffer. Its how much time your game logic takes that is the factor.

But the cases are

Single Buffer
you need to "race the beam" and time you on screen updates perfectly to ensure there is no tearing

Double Buffer
Now you have an off screen buffer so you can update any part of it you want then present when ready. This gives you smoothness as long as you don't take more than 1 frame to draw the off screen buffer. If you do then you will drop a frame and go to 30fps etc.
The issue is when you are finished with a frame you must wait until the frame has drawn before you can flip. Thus you "waste cycles"

Triple Buffering
This fixes the wait step.
As you now have
First Buffer <- visible on screen
Second Buffer <- fully drawn ready to display when the screen is ready
Third Buffer <- ready to be draw upon at any time.

So with Triple buffer you don't waste any time waiting for VBlank, thus is you have frames that are mostly under but some times go over by a bit, but not more than you are typically "under" they "even out" and you get a smooth update.

If all your frames are always under time then triple buffer offers you nothing. As you will still end up waiting at some point anyway, it just adds complexity and costs RAM. But if you have the odd spike over then it smooths it out for you.

Adding a 4th Buffer does nothing as now you have
First Buffer <- visible on screen
Second Buffer <- fully drawn ready to display when the screen is ready
Third Buffer <- ready to be draw upon at any time.
Forth Buffer <- if you are drawing here, just draw on the Third instead. If the Third is done and the Second is not displayed then you are to far ahead and need to wait for the frames to catch up anyway.

User avatar
Nikku4211
Posts: 401
Joined: Sun Dec 15, 2019 1:28 pm
Location: Bronx, New York
Contact:

Re: DOS VGA Tricks

Post by Nikku4211 » Tue Nov 24, 2020 9:08 am

Oziphantom wrote:
Tue Nov 24, 2020 8:23 am
Adding a 4th Buffer does nothing as now you have
First Buffer <- visible on screen
Second Buffer <- fully drawn ready to display when the screen is ready
Third Buffer <- ready to be draw upon at any time.
Forth Buffer <- if you are drawing here, just draw on the Third instead. If the Third is done and the Second is not displayed then you are to far ahead and need to wait for the frames to catch up anyway.
That would be a good point, if I didn't say hardware scrolling.

Hardware scrolling alone doesn't require continually drawing to buffers because you can just change the initial screen offset so that you can scroll pretty smoothly vertically without taking much CPU time at all.

This is what my demo does.

Now, if you were able to do horizontal and vertical hardware scrolling like Root42 did, then 4 pages would now be pretty useful.

Both of these demos do nothing more than scroll, though, so there's no software sprites or anything like that.
I have an ASD, so empathy is not natural for me. If I hurt you, I apologise.

lidnariq
Posts: 10454
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: DOS VGA Tricks

Post by lidnariq » Tue Nov 24, 2020 12:25 pm

Nikku4211 wrote:
Tue Nov 24, 2020 6:43 am
I heard that 60hz 320x240 actually does have quite a bit of flicker, at least on actual DOS machines.
Depends on the monitor (smaller is better), how bright the image on the screen is (darker is better), and how bright the room is (darker is better). But yes, I remember games that used the 60Hz modes as being a little flicker-y
Now, if you were able to do horizontal and vertical hardware scrolling [like this video that talks about 3d4 index 13h]
VGA supports arbitrary virtual width (up to 4080 pixels, or 510 columns of text), so yes, you can use it as a scroll-able image without needing double-buffering ... but those don't need to full multiples of the screen!

User avatar
Nikku4211
Posts: 401
Joined: Sun Dec 15, 2019 1:28 pm
Location: Bronx, New York
Contact:

Re: DOS VGA Tricks

Post by Nikku4211 » Tue Nov 24, 2020 1:16 pm

lidnariq wrote:
Tue Nov 24, 2020 12:25 pm
VGA supports arbitrary virtual width (up to 4088 pixels, or 510 columns of text), so yes, you can use it as a scroll-able image without needing double-buffering ... but those don't need to full multiples of the screen!
Oh yeah, that's right. That's pretty interesting.

So you can use a virtual resolution of 1280 x 200 for, say, a game that does only horizontal or 320x800 for only vertical scroll, huh?

Though it's probably better to just use 3 'pages' for the scrolling background while the first 'page' contains the HUD at the top for a raster split, while also having all of the software sprites that are going to be latched onto the background using offsets of the scroll position variables that the program already uses to scroll. You would have to find out how to clear all of the sprites before re-latching, though.
I have an ASD, so empathy is not natural for me. If I hurt you, I apologise.

User avatar
rainwarrior
Posts: 8016
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: DOS VGA Tricks

Post by rainwarrior » Tue Nov 24, 2020 2:38 pm

Nikku4211 wrote:
Tue Nov 24, 2020 6:43 am
I heard that 60hz 320x240 actually does have quite a bit of flicker, at least on actual DOS machines. 60hz is better for modern monitors, though.
I don't know why you've heard that? 60hz looks perfectly fine on old monitors. CGA and EGA cards were also 60hz at 320x200. 60hz was never a problem.

If you're making something that people are going to play in emulators (or even just an old computer with a modern display), 60hz helps a lot with smoothness since most people won't be able to actually display at 70hz, and 320x240 has a square pixel aspect ratio like everything uses now. It's kinda win/win/win to me, and like I said, works perfectly on old hardware too.
Nikku4211 wrote:
Tue Nov 24, 2020 6:43 am
4 actual video pages might be used for actual smooth hardware scrolling, if you manage to do both horizontal and vertical.
Hardware scrolling is done by repositioning a single page within the 64k/256k window. It doesn't directly have anything to do with having more than one page, unless you want to restrict your scrolling to within a small pre-drawn field that fits in your total memory... though in that case "pages" isn't particularly meaningful, the virtual field size is just anything that arbitrarily fits in memory, not separate pages.

Making scrolling smooth is mostly about doing the update efficiently enough for the target CPU. For something like Commander Keen, using hardware scrolling meant it only had to update the edges of the visible screen and any moving sprites.

Once your CPU is fast enough to copy the whole screen in vblank, there's less and less point to using hardware scrolling. The optimization of using hardware scrolling to limit your needed updates becomes very unnecessary. Even for Keen, the stuff it was doing only really helped the game play more smoothly on older model computers.

So... using hardware scrolling on VGA only really makes sense to me for a very narrow range of target CPU hardware. It can be done, but for it to be a tangible optimization advantage, it's a rather situational thing?
Nikku4211 wrote:
Tue Nov 24, 2020 1:16 pm
Though it's probably better to just use 3 'pages' for the scrolling background while the first 'page' contains the HUD at the top for a raster split...
Are raster splits actually possible on VGA? I'm not sure if I've ever seen this demonstrated, and I didn't think the VGA registers worked this way.

I've seen talk of mid frame palette splits, but that's it? ...and even that seems like a lot of work for very limited utility, especially with 256 colours to work with already.

lidnariq
Posts: 10454
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: DOS VGA Tricks

Post by lidnariq » Tue Nov 24, 2020 4:03 pm

There's the hardware split screen register (3d4 idx 18h) which resets scrolling to 0 at a configured time.

I'm not clear if there's enough else. There's all the effects in Copper, which tentatively appears to be:
Plasma (no idea)
Kefrens Bars (probably setting 3d4 idx 13h to 0)
bottom branding (3d4 idx 18h)
Parallax bars (I assume 3d8/9 across multiple colors)
"traditional" raster bars (3d8/9 on one color)
Changing display start relative to hsync (3d4 idx 1-5)
Brightness control per scanline (3c0 idx 14h; reportedly only works on some Tseng cards)
Not clear how the "pour" effect works
Y zooming (3d4 idx 9)
water effects (same)
Not clear how the X zooming works

Joe
Posts: 460
Joined: Mon Apr 01, 2013 11:17 pm

Re: DOS VGA Tricks

Post by Joe » Tue Nov 24, 2020 4:19 pm

rainwarrior wrote:
Tue Nov 24, 2020 2:38 pm
Once your CPU is fast enough to copy the whole screen in vblank, there's less and less point to using hardware scrolling. The optimization of using hardware scrolling to limit your needed updates becomes very unnecessary. Even for Keen, the stuff it was doing only really helped the game play more smoothly on older model computers.
Even if your CPU is fast enough, your video card might not be, especially for 256-color graphics. For 60Hz 320x240, you need to write around 4.4MB/s to update the entire screen, and that's assuming you can double-buffer (so you don't need to wait for vblank) and don't need to spend any CPU time doing anything else. Good luck doing that with any ISA video card.
rainwarrior wrote:
Tue Nov 24, 2020 2:38 pm
Are raster splits actually possible on VGA?
Yes, but you can only scroll the top part of the screen. There's an entire chapter about it in the aforementioned Black Book. (And you can abuse it for various effects by triggering the split more than once per frame!)

Post Reply