Friday, March 15, 2013

AVR VGA Generation

I have always wanted to create a VGA generator. It has been a personal goal of mine for some time. Over the past couple of weeks I toyed with the idea and finally decided to implement one. I decided to implement it as a framebuffer to allow for general purpose display rather than application specific display. I implemented my own small colour gamut to be memory and CPU efficient.

I have made the source code available on Github.

Daft Punk from Tron Legacy
VGA Controller on a Breadboard

 The first thing I did was order some new AVRs. I ordered the ATmega1284p because it has 16kB of RAM and 128kB of program memory. My original idea was to clock the MCU at 20MHz. I successfully created an H_SYNC and V_SYNC generator at this clock rate but found it difficult to implement the frame buffer properly. When the chip is clocked at 20MHz, there are only 508 cycles per line of active video. According to the VGA specification the pixel clock is 25.175MHz. I decided to overclock my microcontroller to 25MHz to get 640 clock cycles per line. This makes the code cleaner and allows for a higher resolution frame buffer.

The AVR Logo

Designing the Colour Space

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.
RGB222 Byte
You might argue that I should assign bits 6 and 7 to either of the 3 channels, but I decided not to do this. I decided that it would make the creation of grey colours more difficult. This colour space allows for 1 byte per pixel. Since the AVR allows for single-cycle writes to the 0th page of RAM, I can also output this colour in a single instruction. This allows me to hit the theoretical 25MHz pixel clock. Woohoo!

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.

RGB222.gpl

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
 In practice, I decided to use 470 and 220 ohm resistors. This isn't ideal, it applies a skew to the colour space. Ideally these resistors would be low tolerance.

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+
out r18
nop
nop

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.

Next Steps

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.

48 comments :

  1. Why not put both extra bits into the G channel? This gives you enhanced color depth where the human eye will most notice it but still allows your matched greys.

    ReplyDelete
    Replies
    1. I had considered the fact that the eye is most sensitive to green light, but didn't consider assigning both bits to the green channel.

      Good idea!

      Delete
  2. A nice solution I saw before was instead of using them as colours use them as brightness; so the 4 possible values give 4 levels of brightness for each of the 64 combinations of colours -- however I think it complicates the DAC layout somewhat.

    Interesting project; I think VGA is a lot nicer than composite; You must have seen the uzebox project by now?

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I make an assembler project using avrstudio 5
    and included all the project files
    when I build the Project
    it give me that error
    invalid entry file , value cannot be null

    Any ideas ?
    Thanks

    ReplyDelete
    Replies
    1. Hi Mohammad,

      I am unsure of why you would get this error. You I suspect that the error message you have posted is truncated. Is there any indication of line number that this error occurred on?

      I know that AVR Studio will include its' own header file (for register definitions). You may need to remove this.

      Delete
    2. I have no idea what assembler you are using. I assume something AVR Studio (which is potentially GNU). The code I have posted assembles on my system (Arch Linux).

      Delete
  5. You could use remainding 2 bits left from RGB222 (RRGGBBxx) as intensity bits (RRGGBBII), similar trick to what old ZX Spectrum did. That way you get 256 colors with correct grays as you would expect. Implementation is quite simple, and can be seen for example here: http://www.lucidscience.com/pro-vga%20video%20generator-2.aspx

    ReplyDelete
  6. Hi,
    your project inspred me to do something similar on a beefier hardware (Beaglebone Black). Thanks for sharing the idea!
    http://mujweb.cz/molej/arvid/

    ReplyDelete
  7. Hii can you have all project in atmel studio? because i don't know about linux and it command. if you have project in atmel studio than please send it to my following email address.

    ReplyDelete
  8. plz provide me the pin description and also the the list of components require.
    thank you

    ReplyDelete
  9. Choosing the best video converter software is like choosing the best car, there is no "one size fits all" video converter. Before purchasing one, you need to know what files you have and which device you want them converted to. For example, if you want your video files put on your Apple iPhone 3G s or iPod Touch, you need to convert the video to MP4; if you received video made on Mac, you probably Youtube to Mp3 Converter to choose a converter program that is able to convert MOV to other windows media support, like WMV.

    ReplyDelete
  10. A printed circuit board is an electronic unit that is used to connect electrical elements into various appliances and machinery. PCB reverse engineering You need to also be sure that the concerned company will do what it takes to test the PCB before the actual production begins.

    ReplyDelete
  11. I got what you intend, saved to bookmarks , very decent web site . solidworks 2016 setup download

    ReplyDelete
  12. Universial Studios offers one of the best studio in Hollywood, they also make great films** putlocker

    ReplyDelete
  13. If you are new to your own business or come from a corporate or a therapeutic practice background, figuring out the "new rules of business" can be confusing. You want to be pragmatic and have a heart at the same time. You find yourself trying to give away too much. You watch how other people do their business and decide you don't want to do it the way they do. 2cad.cheapsoftwaredownload.net

    ReplyDelete
  14. Feels amazing when you can code something useful for everyone :). I've been coding this online video converter tool that can be used to download facebook videos for years. It turns out people use it and makes me feel great. bravo for the VGA generator

    ReplyDelete
  15. I guess we know that you can convert youtube to mp4 480p online these days. And how about converting instagram to mp4 online for free online.

    ReplyDelete
  16. i love bloghoping and i really love to comment on your blog. 먹튀

    ReplyDelete
  17. In this way, the PCB prototype saves the inventor of the electronic appliance lots of money, as any mistakes that may be present in the PCB will be pinpointed before the actual commercial manufacture of the PCB. Aluminum PCB In general, there are two types of malfunctions that can cause a faulty PCB: a short or an open.

    ReplyDelete
  18. My friend sent me here and I thought I’d say hi, great blog. 먹튀검증

    ReplyDelete
  19. cheers for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more info? as it is extremely useful for me. 먹튀검증

    ReplyDelete
  20. This web-site can be a walk-through its the data it suited you with this and didn’t know who ought to. Glimpse here, and you’ll undoubtedly discover it. 918kiss xe88 ios

    ReplyDelete
  21. Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Great. I’m also a specialist in this topic so I can understand your hard work. 918kiss kiss918 apk download

    ReplyDelete
  22. Thank you for another magnificent article. Where else could anybody get that kind of info in such an ideal way of writing? I’ve a presentation next week, and I’m on the look for such info. 파워볼사이트

    ReplyDelete
  23. Due to this it is advisable that you have to relevant analysis previous to building. You may upload more practical upload by doing this. mutuelle generation

    ReplyDelete
  24. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts.먹튀검증커뮤니티

    ReplyDelete
  25. This is one very interesting post. I like the way you write and I will bookmark your blog to my favorites.토토커뮤니티

    ReplyDelete
  26. While looking for articles on these topics, I came across this article on the site here. As I read your article, I felt like an expert in this field. I have several articles on these topics posted on my site. Could you please visit my homepage? 온라인홀덤

    ReplyDelete

  27. Hello, thank you for the good information.
    I have good information. I want to take a look.
    토토사이트코드

    ReplyDelete
  28. Nice to meet you. I read the text well. I have good information.
    메이저놀이터

    ReplyDelete
  29. Hi I read the article well
    I also have good information. Take a look
    안전놀이터주소

    ReplyDelete
  30. It's cold
    Take good care of your body. I have good information.
    파워볼게임

    ReplyDelete
  31. I've been searching for 한국야동 hours on this topic 무료야동사이트, and finally I found your post 무료성인야동, and I've read your post 일본야동 and I'm very impressed 무료야동. We prefer your opinion 성인사진 and will visit this site frequently to refer to your opinion 성인야동. When would you like to visit my site? 조개모아

    ReplyDelete
  32. Awesome blog you have here 온라인카지노 but I was wondering if you knew of any forums 스포츠토토 that cover the same topics talked about here 먹튀검증사이트? I’d really like to be a part of group where I can get suggestions 안전놀이터 from other experienced people 추천픽 that share the same interest 먹튀사이트. If you have any recommendations 검증사이트, please let me know 먹중소. Bless you! 먹튀중개소

    ReplyDelete
  33. I've been searching for 토렌트사이트 hours on this topic 야동사이트, and finally I found your post 먹튀검증사이트, and I've read your post 웹툰사이트 and I'm very impressed 성인용품. We prefer your opinion 스포츠중계 and will visit this site frequently to refer to your opinion 드라마다시보기. When would you like to visit my site? 한인사이트 무료야동

    ReplyDelete
  34. I've been searching for 한국야동 hours on this topic 무료야동사이트, and finally I found your post 무료성인야동, and I've read your post 일본야동 and I'm very impressed 무료야동. We prefer your opinion 성인사진 and will visit this site frequently to refer to your opinion 성인야동. When would you like to visit my site? 조개모아

    ReplyDelete
  35. Awesome blog you have here 온라인카지노 but I was wondering if you knew of any forums 스포츠토토 that cover the same topics talked about here 먹튀검증사이트? I’d really like to be a part of group where I can get suggestions 안전놀이터 from other experienced people 추천픽 that share the same interest 먹튀사이트. If you have any recommendations 검증사이트, please let me know 먹중소. Bless you! 먹튀중개소

    ReplyDelete
  36. I've been troubled for several days with this topic. 먹튀검증, But by chance looking at your post solved my problem! I will leave my blog, so when would you like to visit it?

    ReplyDelete
  37. A very awesome blog post. 토토사이트 We are really grateful for your blog post. combat, law enforcement You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.

    ReplyDelete
  38. Thanks for your marvelous posting! I actually enjoyed reading it, you could be
    a great author.I will remember to bookmark your blog and will
    eventually come back from now on. I want to encourage you to continue your great
    writing, have a nice weekend!부산달리기


    ReplyDelete
  39. Thanks for your nice post I really like it and appreciate it. My work is about Packaging Printers
    . If you need perfect quality boxes then you can visit our website.

    ReplyDelete
  40. Your paragraph has very interesting and wonderful information which impressed me to read this.
    Get quality CBD Packaging UK, Bespoke cosmetic boxes, wholesale printed boxes for CBD and more for any product you need.

    ReplyDelete
  41. Your article is great, but I think it will be more complete if you add some topics to this article. I have these types of articles on my blog too. Would you like to visit them?먹튀검증

    ReplyDelete
  42. Simply unadulterated brilliance from you here. I have never expected something not as much as this from you and 먹튀검증 have not confounded me by any reach out of the inventive vitality. I acknowledge you will keep the quality work going on.

    ReplyDelete
  43. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder for 안전놀이터, what about the other side? !!!!!!Thanks

    ReplyDelete