|
Projects: Current Projects: Past Projects: |
PIC Breakout![]() Sometime around October of the year 2000, SigArch shifted its focus from FPGA design to microcontrollers--specifically, Microchip PICs. Upon receiving a programmer and several chips, we took it upon ourselves to learn PIC assembler by finding the hardest programming task we could and jumping right in. After searching the web, we discovered a project that used the PIC to generate monochrome NTSC signals in software to play the classic games Pong and Tetris. I decided to use the same technique to write a Breakout game. Before jumping head-on into PIC assembly, here's a short explanation of how televisions work. Most television sets are built with Cathode Ray Tubes (CRTs), which means that a beam of electrons shoots from the back of the picture tube towards the screen. When electrons hit the phosphor-coated screen, they cause it to light up. Since the electron beam can only aim at one place at a time, the beam actually has to move back and forth and up and down very rapidly to cover the entire screen. The beam sweeps from left to right, moves down a line while moving back right, and repeats. Once it gets to the bottom of the screen, it moves back up and left to the top corner and starts over. Your eyes can't see fast enough to watch this motion, so it looks like the screen has a full moving picture on it all the time.
In the above illustration, the solid lines represent the times when the electron gun is on, drawing to the screen. The dashed lines represent when it is turned off to reposition itself, which is called the retrace. The NTSC standard for black and white television is actually very simple to understand. The signal is divided into 525 horizontal lines, each taking approximately 64μs to display. Instead of displaying all the lines in a frame of video in a single sweep from top to bottom, televisions use a technique called interlacing, which means they display all the odd lines first, then go back and display the even lines. This means that in a single vertical sweep of the gun, only 272.5 lines can be displayed. Below is an illustration of what a typical horizontal line in a black-and-white NTSC signal looks like. The horizontal axis represents time, and the vertical axis represents the intensity of the picture displayed. The signal starts out with the "front porch", several μs at the black level of intensity to calibrate the television's electronics. Then, the signal dips to the sync level, lower than the black level, for several μs, which tells the television where to start a new horizontal line. Then, the "back porch," again at the black level, sets the TV up to display a picture. The actual picture data can range between the top and middle dotted lines, signifying bright white and blackest black, respectively. After the picture data, the next line arrives, and the television retraces back to the beginning of the next line.
The only thing left to understand is the vertical retrace. Each time the electron beam reaches the bottom of the screen, the television signal includes a vertical retrace signal, which lasts for nine scan lines worth of time. The retrace signal looks like the following diagram:
Note that the space between dotted lines is equivalent to one horizontal scanline. The high pulses in this diagram are at the black level, while the low pulses are at the sync level. The Software Now that you've seen the basics of television signals, you can understand exactly how our PIC Breakout game works. Looking at the diagram of a video signal, you can see that only 3 signal levels are required for a signal: sync, black, and white levels. This is easily done with a 2-bit digital to analog converter, with outputs corresponding as follows:
Another thing to be conscious of is the signal timing. Each horizontal line has about 52μs of picture display time. Since PICs divide the external clock by 4, a PIC running at 4MHz gives you 1 cycle per microsecond. This is clearly insufficient for anything close to high resolution. Running the PIC at 12MHz gives 3 cycles/μs, which is adequate for displaying simple graphcis. Note that overclocking 4MHz PICs to 12MHz isn't recommended for real products, however. Since we have to constantly generate a video signal, the task of actually computing game data is left to blank lines at the top of the screen. Since the output stays the same for the entire line, you have about 150 cycles worth of computation per line. This isn't technically cheating since televisions actually aren't supposed to display the first few scanlines anyway. My code uses only two scanlines to compute where the ball is, where the paddle is, and if any blocks have been hit. An ambitious programmer could add sound effects or even music during this period of inactivity, though it would require modifications to our hardware setup. The PIC16F84 we used has two general purpose I/O ports: port A and port B. While it would be simple to build a 2-bit D/A converter using two bits from the same register, there's a good reason not to do this. If you plan on only displaying black and white, forgoing the possibility of grey, then you'll notice that bit 0 of the output only needs to be '0' during a sync pulse. Specifying bit 1 as a pin on the other port allows you to make high resolution sprites by setting Bit 0 to '1', then rotating the bits of your graphic through the other port. This is used in Breakout to display the blocks. The Hardware The entire video game fits on a 1x2" printed circuit board, receives power with a 9V battery, and has an RCA jack to connect to a video-in.
Other Video Projects
Using the same hardware, I wrote a clock which counts down to Engineering Open House. This program is much more readable than the breakout code, and uses a better method for synchronizing the end of each scanline.
Links and Files:
|
|||||||||||||||
|
|
10/26/01 by Joel Jordan |