Wednesday 31 January 2018

Raster splits in BASIC

I have started trying to generate some documentation on the advanced character modes of the MEGA65's VIC-IV video controller, and in the process decided to write some simple example programs, so that people can more easily see what each feature does, and experiment with them themselves.  To make the tests more accessible, I figured that I would write them in BASIC, rather than assembly language. 

The trick was, if I am changing the character mode, and yet want to have some kind of useful information on the display, then I would need a raster split, so that part of the screen could be in the special mode, but the information in ordinary text mode. But hang on, I am programming in BASIC, not assembly, so how can possibly have a raster split?  The answer is with 50MHz! What used to take a second on a C64 can be done in a single frame on the MEGA65, and what used to take a frame, can be done in just a few rasters.  Thus I figured that if I used the rather under rated WAIT command in C64 BASIC, I could probably get a stable enough raster split for this purpose. It wont be perfect, but it will still be pretty good.

Wait is a bit of a strange beast.  It takes three arguments: The address to wait for certain values on, a value to AND with the contents of the location, and a value to XOR before doing the AND.  So:

WAIT 53265,128,0

Will wait while  (PEEK(53265) XOR 0) AND 128) = 0

That is, it will wait until the VIC-II(or III or IV) is near the bottom of the screen, and:

WAIT 53265,64,0

will wait while   (PEEK(53266) XOR 0) AND 64) = 0

That is, until the VIC-II/III/IV has just about finished drawing the top two lines of text.  If I only want to put text in the top line, that's close enough for my little test program.

So, lets put this together into a little loop that changes the border colour based on where we are on the screen:

2000 R = 53248 + 17: R2 = R + 1
2010 REM WAIT FOR BOTTOM OF SCREEN
2020 WAIT R,128,0: POKE 53280,0
2050 REM WAIT UNTIL AFTER A COUPLE OF ROWS OF TEXT
2060 WAIT R,128,128: POKE 53280,2
2070 WAIT R1,64,0: POKE 53280, 1
2090 GET A$: IF A$="" GOTO 2010

Line 2000 works out $D011 and $D012, the two raster indicator registers on the VIC-II in decimal, so that we can easily use them in the routine.

Line 2020 waits until bit 7 of $D011 = 1, i.e., for the bottom part of the screen to start being drawn, and then sets the border colour to black.

Line 2060 waits until we are out of the vertical fly back with $D011=0 again, and then sets the border colour to red.

Line 2070 waits for raster #64, i.e., almost the end of the second row of text, and then makes the border white.

So, if this works, we should see white border for most of the screen, but red at the top, and black at the bottom, and indeed we do:


So now you don't need to learn assembly language to do a raster split any more ;)

No comments:

Post a Comment