Vertical Blank Interrupts
The Atari computers use the Vertical Blank Interrupt for many housekeeping chores. When the Atari is first powered up under normal conditions, the Vertical Blank Interrupt is enabled. A special list of vectors is placed in page two of memory. The operating system places the list of vectors in page two because these are RAM locations and the user can change their contents to point to his own interrupt service routine. System Reset is the only interrupt that cannot be routed through page two.
The operating system's Vertical Blank Interrupt (VBI) service routine is a two-stage process. When an interrupt occurs, the computer is sent to a vector in the operating system, which in turn sends it to a routine to determine what type of interrupt has occurred. It then jumps through the appropriate vector on page two. It is called two-stage because it goes through two vectors on page two. This allows the programmer to use either his own VBI routine or that of the OS, or both.
The OS uses the VBI routine to transfer data from some of the special hardware registers in Atari's custom chips to shadowed locations in RAM and vice versa. For example, it reads the color register information, the location of the display list, and other graphics control information placed in RAM by the user, and writes them to their hardware addresses. It also reads many hardware registers such as game controllers and light pen and places them in RAM for the user. It updates the clock at locations 18-20 ($ 12-$ 14), and updates the timer for the attract mode which cycles the colors if a key has not been pressed for several minutes. It even takes care of the repeat action on the keyboard keys. Because the VBI routine updates many registers that are used by the average program, most programmers don't wish to replace it entirely by their own routine, since in many cases it would mean duplicating at least some of the procedures that are handled automatically by the OS.
The vectors are called Immediate VBlank and Deferred VBlank. Atari engineers split the Vertical Blank period into two separate sections because a VBI can extend for about 20,000 cycles or nearly all the time available before the next VBI. The
Immediate VBlank portion refers to the time critical period when the electron beam is actually offscreen. It is here that shadowing and hardware registers are updated. Once the OS VBI routine has completed its housekeeping chores, it jumps through the Deferred VBlank vector, which is set by the OS to point to a routine that will restore the registers and return the computer to its position before the interrupt. The OS VBI routine will abort if the computer was in the middle of a time critical I/O routine such as sending data to a cassette or disk drive. In such a case, the Deferred VBlank vector is bypassed. Unless you are using disk I/O where your code must be short enough not to delay shadowing updating, you can use Deferred VBlank without being concerned.
A major question asked by many programmers is: How much time do I have in the VBI routine to execute my code? To answer this we must examine the TV process again.
A television image is composed of 525 lines. Because of what is called interlacing, only half are drawn per frame, and some of these are offscreen due to normal vertical overscan. It takes the TV 63.5 microseconds to draw a single line. This includes the time it takes to shut the electron beam off and reposition it back on the left edge one scan line below. The length of Vertical Blank is equivalent to 22 scan lines or roughly 1400 microseconds. With the Atari's 6502B CPU runningat 1.79 MHz, 1400 microseconds is equivalent to approximately 2500 machine cycles (1400 X 1.79 = 2506). So, how much can you do "inside" the Vertical Blank period? Not very much, if you are trying to accomplish things like moving players and scrolling the screen solely inside the Vertical Blank period. The important thing to remember is that one television frame is l/60th of a second, which is equivalent to 16,666 microseconds or just about 30,000 machine cycles. This means that there is a maximum of approximately 30,000 machine cycles BETWEEN Vertical Blanks. ANTIC delays processor time in order to fetch screen data and look up character data during the screen drawing process. A programmer using an extensive Vertical Blank Interrupt routine will want to be sure that the routine is finished before the next VBI occurs; otherwise, his routine will be aborted in the middle unless very special precautions are taken. A programmer will also want to be sure that his VBlank routine is not so long that it causes serious delays to the program code that is running outside VBlank.
Programmers often ask if the 2500 cycle VBlank time constraint seriously limits the amount of time for smooth offscreen animation and updating. The answer is No! True, all of the graphics updating must be performed while the beam is offscreen, but you can also gain some additional time. Remember what a normal display list looks like. It begins with 24 blank lines. That gives 24 by 63.5 microseconds/line or another 2728 cycles that can be considered offscreen. High scores, player scores and messages generally cover the top few lines too. This adds additional time offscreen. Obviously if your Vertical Blank routine is even longer, you should be OK as long as you do all of your graphics updating within the first 5200 cycles of your routine. Figure that you have a maximum of 20,000 cycles (4500 instructions) in Deferred VBlank, but you must finish before the next VBI or your program will crash.
While programmers have written entire games in Deferred VBlank, only certain operations should be put in VBlank. All graphics updating, including scrolling the screen, moving player-missile objects, and changing color registers, should defi nitely be done in VBlank. In addition, collisions should be checked, and joysticks or paddles should be read. This is also the best place to implement time critical sound routines. Everything else, including calculations, should be in your main code outside VBlank.
Setting up a Vertical Blank Interrupt is a simple process since the OS has a routine to set up the vector for you. All it involves is storing the address of your VBI routine in the vector table on page two of RAM. If a VBI has occurred between the time the two bytes that make up the vector were stored in the table, the 6502 would jump through an erroneous address, and the program would become erroneously lost. The OS setup routine automatically assures this never happens.
Setting up the routine is simple. Load the Accumulator with 7 if you are setting a Deferred VBlank routine, and 6 if you are setting an Immediate VBlank routine. The X register is loaded with the high order byte of your routine, the Y register with the low order byte. You then JSR to the OS SETVBV routine at $E45C.
Example:
SETVBV .EQ $E45C
LDA #$07 ;DEFERRED
LDX /VBLANK ;HIGH BYTE OF USER ROUTINE
LDY #VBLANK ;L0W BYTE
JSR SETVBV
There are two possible exit points from a VBlank routine depending on whether Immediate or Deferred VBlank is used. If the programmer uses the Immediate one and still desires to use the OS VBI routine, the vector is $E45F (SYSVBV). If the Deferred VBlank is used or the programmer does not want the OS VBI routine to execute the vector, then it is $E462 (XITVBV). The XITVBV routine pulls the registers off the stack and does a RTI (Return from Interrupt).
Average user rating: 5 stars out of 1 votes
Post a comment