The TV and our Kernel

It's time to complete our understanding of what constitutes a TV frame - exactly what has to be sent to the TV to make it display a picture correctly. Let's revisit the diagram in TV Timing Diagram, with the timing information and the Pitfall! image inside.

Timing

Your understanding of the numbers across the top should be good, but just to briefly revisit what they mean:

There are 228 TIA colour clocks on each scanline. 160 of those are spent drawing pixels, and 68 of them are the horizontal retrace period for the TV's scanning of the electron beam back to the start of the next line. In the diagram we see the horizontal blank (retrace) at the left side, so our very first colour clock for the TIA's first visible pixel on the screen is cycle #68. We should understand this timing fairly well by now. Those 228 colour clocks correspond to exactly 76 cycles of CPU time - because the CPU clock is derived from the TIA clock by a divide-by-three circuit. So, once again, 228 colour clocks = 76 CPU cycles on each scanline.

What we're going to finalise this session is our understanding of the numbers down the right hand side - which represent the scanlines sent to the TV. The diagram shows a valid NTSC TV frame - and thus it consists of 262 scanlines. A PAL diagram would consist of 312 scanlines - and the inner 'picture' area would increase from 192 lines to 242 lines.

Let's go from the top. The first thing that the TV needs is a 'reset signal' to indicate to it that a new frame is starting. This is the 3-scanline section at the very top of the frame. There are special ways to trigger the TIA to send this signal, but we're not going to have to worry too much about understanding that - just about every game does it exactly the same way - all we need to remember is that the first thing to send is that reset trigger (called VSYNC).

TVs are not all made the same. Some cut off more of the picture than others, some show wider pictures, some show taller pictures, etc. Not to mention modern LCD TVs that attempt to emulate how an older-style TV would handle a signal. To 'standardise' the picture, the diagram shows the recommended spread of valid picture lines, surrounded by blank (or 'overscan') lines. In this case, there are 192 lines of actual picture. We don't have to stick to this - we could steal some of the lines from the vertical blank section, and some from the overscan section, and increase our picture section appropriately. Many modern homebrew games do exactly this.

As long as our total number of scanlines adds up to 262 for NTSC TVs (or 312 for PAL TVs), then the TV will be able to display the frame. But remember, the further we get 'out of specs' with this method, the less likely it is that ALL TVs will show the picture section in its entirety.

OK, let's march through the numbers on the right side of the diagram.

  • 3 Scanlines devoted to the vertical synchronisation
  • 37 scanlines of vertical blank time
  • 192 (NTSC) or 242 (PAL) lines of actual picture
  • 30 scanlines of overscan


Total: 262 scanlines (NTSC) or 312 scanlines (PAL), constituting a valid TV frame. You send the TV this, and it will be a rock-solid display.

One interesting aside: if you send a PAL TV an odd number of scanlines, it will only display in black and white. I don't know the exact reason for this, but it must be to do with where/when the colour signal is encoded in the TV image, and where the TV looks for it. So remember, always send an even number of scanlines to a PAL TV.

You can send frames with different numbers of scanlines. That is, 262 and 312 are not totally immutable values. But if you do vary these numbers, you wander further from standards/specification, and an increasing number of TVs - the further you deviate from these standards - will simply not be able to display your image. So, although you *can*... you shouldn't unless you are confident that you know what you are doing, and the consequences.

Fortunately, most emulators available to us today are able to show us the actual number of scanlines which are being generated on each frame. This must have been quite a challenging task for early '2600 programmers - nowadays its quite easy to make sure we get it right.

Now we have all the knowledge we need about the composition of a TV frame! Once we know how to make the TIA generate its reset signal at the top of the frame, and how to wait the correct amount of time to allow us to correctly generate the right number of scanlines for those other sections, we will be able to design our first 'kernel' - the bit that actually 'draws' the TV's frame.

When we have our kernel working, there's not much more to a '2600 game other than moving sprites around, changing colours, etc.

see Programming for Newbies
see TIA
next My First Kernel
previous TV Timing Diagram