Understanding V9938/58 R19 and R23 behavior

By DarkSchneider

Paragon (1030)

DarkSchneider's picture

25-05-2023, 18:52

Working with both R19 and R23 I have found some tracks about how (could) works the VDP. Some points:

- For this, we have also scanline, that is the current rendering line. It starts (scanline == 0) at the 1st line drawn, instead the top of the screen, so the top border does not count. scanline is auto-incremented.

- R23: is an offset register, what the VDP draws at each line is the result of scanline + R23.

- R19: is for comparison. If we set R0 IE1, the VDP will set S1 FH flag and trigger an interrupt if R19 == (R23 + scanline).

With this, remember to take into account the scanline factor when want to use line interrupts, or can easily become a mess.

I.e. if we set at vblank R23 = 216 (256 - 40), and R19 = 0, the line interrupt will trigger at scanline 40, because it will be when (scanline + R23) == R19 == 0. But, for the correct adjust of the content at that line, remember that scaline == 40, so we have to subtract from our real R23 value using 0 as origin point. Don't worry if the result is "negative" (underflowing the value to 200 an something), as it will be compensated by the addition of scanline, that is what the VDP draws at that line.

Then, be careful, because if like in this other example, we can get not expected results. In this case we set R23 to 0 at vblank, because we put our top info layout at line 0 in some page. Then set R19 = 40 so it will trigger at the correct line. But, if we also have the gameplay content at line 0 in another page, when we subtract 40 to the real R23 to show the correct content, at later time it will trigger again the line interrupt, because (scanline + R23) == 40 again. In this case we should disable IE1 at line interrupt, and enable again at vblank.

I wonder if the VDP not triggering line interrupts at line near 255 is because those lines are the corresponding to the top border, and the VDP is completely disabled (only filling with the border color) until reach the 1st scanline. Not tested but a test could be to set R23 = 200 at vblank, set R19 to one of those supposed not triggering values, and watch if it triggers the line interrupt at some point in the screen, when (R23 + scanline) == R19.

Login or register to post comments

By PingPong

Enlighted (4156)

PingPong's picture

26-05-2023, 21:55

I must admin that i've not understand how this works. I've also tought that the scanline @ interrupt is simply R23+R19 value.
Am i wrong?

By DarkSchneider

Paragon (1030)

DarkSchneider's picture

27-05-2023, 11:34

Notice that with that if we set R19=0 and R23=216 the interrupt would trigger at line 216, but it triggers at line 40. So the triggering happens when R19 = scanline + R23, or exchanging scanline = R19 - R23, as subtracting 216 from 0 we get 40 by underflow. But is more intuitive using the 1st formula.