0

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?

1 Answers1

2

The Input Status 1 VGA register (port 03DAh in color modes) contains a vertical retrace bit that you can read:

VRetrace -- Vertical Retrace

"When set to 1, this bit indicates that the display is in a vertical retrace interval.This bit can be programmed, through the Vertical Retrace End register, to generate an interrupt at the start of the vertical retrace."

So a function that returns when a new vertical retrace interval starts might look like:

wait_sync:
    mov dx,03DAh
wait_end:
    in al,dx
    test al,8
    jnz wait_end
wait_start:
    in al,dx
    test al,8
    jz wait_start
    ret

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.

Michael
  • 57,169
  • 9
  • 80
  • 125