I'm working in TASM 16-bit Assembly as an education course, and I'm making a game engine.
I'm using VGA 13h (320x200 256-colors) with DOSBox, and I can sometimes see the screen tearing, meaning my video memory is updating while my screen (which is 60Hrz) is updating.
For this reason, I would like to implement VSync, meaning I wait until the screen retraces and only then recalculate the screen and update video memory (On a 60Hrz screen, that gives me about 1,000 / 60 = 18ms to update the screen).
My question is, how can I, using assembly synchronize with when the screen updates so I won't have situations when I render at the wrong time?
The Input Status 1 VGA register (port
03DAhin color modes) contains a vertical retrace bit that you can read:So a function that returns when a new vertical retrace interval starts might look like:
The first loop is there to handle the case where you call the function while already in the vertical retrace interval, since you probably want to have the entire interval to access video memory, and not just some part of it.
Supposedly it's also possible to set up an interrupt to trigger when a vertical retrace interval starts, to save you from having to poll the register, but that's not something I've ever looked into.