Displays and Refresh

Background

You are probably familiar with the components of the video display system. Somewhere in the system there is some RAM that contains the image to be displayed on the screen. For analog displays, there is some hardware that reads that RAM and converts the bytes into analog voltages that get sent to the monitor.

Some deeper knowledge of the system will help when it comes to trying to figure out what the memory map means and what the code is doing.

Analog displays have a refresh rate and timing requirements. At any given moment in time, there is a specific pixel on the display that is being updated. Picture the screen getting filled in dot by dot, starting in the top left corner, advancing across the screen to the right, moving to the first dot on the second line, advancing across the screen, etc. When the last line is drawn on the screen, the video controller can generate a Vertical Blank Interrupt (known as VBI or VBL).

To achieve smooth animation, the image in RAM being processed by the video controller can not change. If the update of the RAM is changed in the middle of a frame, then parts of 2 images will be seen by the viewer. This gives the appearance of "tearing" where the top part of the screen is showing a different frame than the bottom part. If you've ever seen tearing you know what this looks like.

To avoid tearing, the software must do something to avoid passing the location of the screen update. Here's one way to do this.

  1. The VBL is generated when the last line is finished, and typically there is some period of time until the first line is started again. (This is the Vertical Blank time, and it may be like 1 millisecond or so.)

  2. On receipt of the VBL, the program starts drawing the screen from the top.

  3. Each line is drawn ahead of the vertical retrace process.

The CPU always stays ahead of the vertical retrace process and avoids tearing.

vertical retrace

Space Invaders video system

The massively informative page located here tells us that Space Invaders has 2 video interrupts. One is at the end of the frame, but it also generates an interrupt in the middle of the screen. The page describes the system of updating the screen - the game draws the stuff at the top half of the screen after it receives the mid-screen interrupt, and draws the stuff at the bottom half of the screen after it receives the end-of-frame interrupt. This is a pretty clever way to avoid tearing, and a good example of what can be done when you are designing the hardware and software at the same time.

We'll need to make our machine emulation generate these interrupts. If we generate them at a 60Hz rate like Space Invaders does, the game will draw at the proper rate.

We'll cover the mechanics of interrupts in the next section and talk about how to emulate them.

← Prev: full-8080-emulation   Next: rest-of-the-machine →


Post questions or comments on Twitter @realemulator101, or if you find issues in the code, file them on the github repository.