Sunday, September 30, 2012

The Dark Age of Video Games Machines

I remember playing the Sega 16bit game console Mortal Kombat, and I was amazed by how different characters animate on the screen and how on earth my joystick controls some pictures which are just some dead bitmaps on the screen. It all happens by "playing' with the TV or the monitor's electron gun on the phosphoric screen.




                             Mortal Kombat on 8 bit devices and Mortal Kombat on an XBox
Now we have a dedicated GPU (Graphics Processing Unit), and they are programmable, you can write some programs called shaders for them, you can perform complex computations, and simulating complex systems. Note that a GPU is simple a video generator : ). We also now have special APIs that access them easily like OpenGL, DirectX,..etc.

How a GPU Generates Signals?! 

Current GPUs are capable of generating advanced video formats like VGA, HDMI,..etc.
In early video game consoles or VHS or whatever any video device, just use a basic concept that the image is painted on the screen line by line. Referring to the next figure, the video devices basically paints the image from top left of the screen and moving horizontally to the right edge, then moves again to the next line which is shown in grey, then again does the same zig-zag motion, until the screen has been scanned. This process is repeated and it is known as Refresh Rate Hz, which the number of times the screen has been scanned in seconds, I'm sure you have seen that term frequently :).  On the other hand, a Frame is constructed from a complete scanning process. There are different scanning techniques like Progressive Scanning and interlaced scanning  You can read more about both here : http://en.wikipedia.org/wiki/Progressive_scan , http://en.wikipedia.org/wiki/Interlaced_video




A lot of video standards have been formalized, but they are all have the same mechanism, NTSC which is commonly used in US, has a 29.79 Frames per second, and the number of lines that are scanned per frame 527.  Asia, Europe, 25 FPS, and 625 Lines. SECAM for France and others, 25 FPS and 625.

Those mentioned standards encode the video signal into something called "composite signal".  A Composite holds  luminance information which holds the black/white image,and  synchronization information. Horizontal and Vertical sync signals.  Referring to the previous figure, You can see a complete description of  composite signal and the timing diagram of an NTSC video standard.  Basically, a horizontal line consists of  
1. A Horizontal Synchronization Pulse, that is used to identify that the line has been scanned horizontally and needs to be reset to the next position for the next line, usually it is called HSync, you can find that term mainly used in Video Generators ICs found in TVs. 
2. Back Porch, that creates a dark frame around the image.
3. Front Porch, that produces the right edge of the image.
4. The black and white information (Luminance) which varies in a voltage range 0 to 1V, where 0.3V represents black, and 1.0V is white, between them is the gray intensities. 
From the figure, one line lasts for 63.5us, at first a 4.7uS sync pulse is sent, so that it tells the TV or the monitor that a new line is going to be drawn, and that is by giving a voltage of 0V. A 52.6uS contains the video data. 

This is only for B/W Video Signal, the color information is transmitted separately, modulated on a higher frequency carrier (  Do you remember Frequency Modulation ?,  Read Openheim or  lathi's book).
A Vertical Syn VSync pluse is also sent to tell that a complete frame or an image has been drawn on the screen and the gun needs to be reset again to line number zero . 

**For Fun
 I remember when I first repaired our home TV old National 1980, and it had  just a horizontal white bar on the screen, and this is for sure means that the vertical deflection circuit in the TV has been broken, and the TV can't try to scan the screen vertically. I changed the Vertical Deflection IC and it worked. Kindly note also the same apperance can happen when the Horizontal Deflection circuits get defected, a Vertical white Bar on the screen appears and that is really bad for a TV, because that always mean that HOT( Horizontal Output Transistor ) and the Line Transformer is defected and this is really an expensive transformer and hardly to be replaced.

How can we generate a composite signal ?

Using any microcontroller, you can use any method that  Digital to Analog Converter D/A to generate a composite signal. Things like PWM, R/2R, a dedicated D/A are enough for that purpose.
All you need is analog voltages that varies from 0 to 1V, but with very precious timing, like you have seen before in the timing diagram, you need to achieve delays in microseconds, which is hard to achieve using C Language, as you can barely know the cycle of the instructions and hence it is better to use assembly language for that purpose. 

A Simple R/2R like the one  shown can be used to generate the required voltages. Note that the input impedance of most TVs is 75ohm, by using voltage divider formulas, you can easily  get the three levels that are needed for generating a composite black/white signals. Make a truth table of 2 bits and check the output. 
This is of course a 2Bit B/W signal, Imagine how a Geforce GTX580 256bit and generative a VGA signal which is more complex than NTSC/PAL Signals..






Sunday, September 9, 2012

Introduction to Embedded Software and Video Game Console Design

SA,

In my childhood, I loved to play games on Atari, Sega,..etc, actually I was addicted. Once My Sega has been destroyed and couldn't work anymore, I tried to open it to see what's inside that box that could make those games. I found bunch of resistors, ICs,..etc. How those silicon chips made those games, I'm wondering.  I was also eager to know how those " dead pictures" can become alive.

A video game console is a product that many sciences involve in, from Electronics Engineering who assemble the circuits, to Game Programmers, and Audio Engineers...etc.

I had always the interest to explain to myself how those black boxes work. I will write few blogs in the next few weeks on how to generate a video/audio signals to plot a pixel, then draw lines, triangles, polygons, which can be used to write a small game!.

There are a lot of constraints while using 8/16bit uCs for a console like Sega, that shows the engineers at that age was Amazing, they knew a lot of tricks and optimization, many of them can be found at Michael Abrash's Book on Zen of Graphics Programming. He has also participate on Microsoft's XBox GPU.

Knowing how video game consoles work, will let you know how your GPU also work, writing a software renderer will let you know  how current APIs like DirectX or OpenGL has been written.

I will stick with PIC24 from microchip, it is a fast uC, and in the next blog, I will explain the basic circuitry that is needed for that project.