I have made the source code available on Github.
|Daft Punk from Tron Legacy|
|VGA Controller on a Breadboard|
|The AVR Logo|
The next step was to decide on a colour gamut. I needed something that I could write out to the I/O pins very quickly and also be memory efficient. I decided to implement RGB222. This is a simple and small colour gamut with 2^6 = 64 colours. There are two bits for each Red, Green and Blue. The two remaining two bits are unused.
I implemented the palette in Gimp so that I could index images and apply dithering. You can download a copy of the palette below if you would like to experiment with it. This palette is compatible with both Gimp and Inkscape.
Designing the Framebuffer
I have 16kB of RAM to work with and I must keep the aspect ratio of the frame buffer 4:3. It would also be beneficial if horizontal could divide into 640 and vertical could divide into 480 evenly. I crunched some numbers using one of my favourite tools, WolframAlpha, and decided that 128x96 was my best bet. A frame buffer of this size requires 12288 bytes of RAM. It allows 5 instructions for drawing each pixel and requires that each line be drawn 5 times.
To make this RGB222 color space come to life I had to design a DAC. I decided to use a weighted R/R2 DAC, taking into account the input impedance of the monitor (75 ohms). I end up with a circuit as shown below. There are three of these circuits connected to one of the 8-bit ports on the microcontroller.
|One of the 3 DACs|
|DAC Assembled on a Breadboard|
Implementation in Software
The entire VGA generation and buffer is implemented in assembly under the GNU toolchain. I decided to implement the active video region as an unrolled loop that I include in the main assembly file. It takes 2 instructions to write one pixel to the display and 2 padding nops. The ld takes 2 cycles which comes to a total of 5 cycles.
ld r18, Z+
The H_SYNC and V_SYNC are a series of software delays. The current version has no provision for dynamic graphics. This is something that I will be working on in the future.
Displaying an Image
I have written a crude program in C# to convert an indexed image to the binary format accepted by the microcontroller.
The first thing the CPU does after it boots is copy image data into the frame buffer memory. After this, the standard VGA generation routines continuously draw this frame out on the VGA lines.
My next goal is to implement a command set that is active during the vertical blanking interval. I would like to allow another microcontroller to send commands to this VGA controller such as set pixel. This would allow for dynamic graphics to be generated.