The VGA interface is common to most modern computer displays and is based on a pixel map, color planes, and horizontal and vertical sync signals. A VGA monitor has three color signals (Red, Green, and Blue) that set one of these colors on or off on the screen. The intensity of each of those colors sets the final color seen on the display.
The VGA interface is common to most modern computer displays and is based on a pixel map, color planes, and horizontal and vertical sync signals. A VGA monitor has three color signals (Red, Green, and Blue) that set one of these colors on or off on the screen. The intensity of each of those colors sets the final color seen on the display. For example, if the Red was fully on, but the Blue and Green off, then the color would be seen as a strong red. Each analog intensity is defined by a 2-bit digital word for each color (e.g., red0 and red1) that are connected to a simple digital-to-analog converter to obtain the correct output signal.
The resolution of the screen can vary from 480 × 320 up to much larger screens, but a standard default size is 640 × 480 pixels. This is 480 lines of 640 pixels in each line, so the aspect ratio is 640/480, leading to the classic landscape layout of a conventional monitor screen.
The VGA image is controlled by two signals—horizontal sync and vertical sync. The horizontal sync marks the start and finish of a line of pixels with a negative pulse in each case. The actual image data is sent in a 25.17 μs window in a 31.77 μs space between the sync pulses. (The time that image data is not sent is where the image is defined as a blank space and the image is dark.) The vertical sync is similar to the horizontal sync except that in this case the negative pulse marks the start and finish of each frame as a whole and the time for the frame (image as a whole) takes place in a 15.25 ms window in the space between pulses, which is 16.784 ms.
There are some constraints about the spacing of the data between pulses which will be considered later in this chapter, but it is clear that the key to a correct VGA output is the accurate definition of timing and data by the VHDL.
If there is a space of 25.17 s to handle all of the required pixels, then some basic calculations need to be carried out to make sure that the FPGA can display the correct data in the time available. For example, if we have a 640 × 480 VGA system, then that means that 640 pixels must be sent to the monitor in 25.17 μs. Doing the simple calculation shows that for each pixel we need 25.17 μs/640 = 39.328 ns per pixel. If our clock frequency is 100 MHz on the FPGA, then that gives a minimum clock period of 10 ns, so this can be achieved with a relatively standard FPGA.
Clearly it is not sensible to use an integrated image system on the FPGA, but rather it makes much more sense to store the image in memory (RAM) and retrieve it frame by frame. Therefore, as well as the basic VGA interface, it makes a lot of sense for the images to be moved around in memory and using the same basic RAM interface as defined previously is sensible. Therefore, as well as the VGA interface pins, our VGA handler should include a RAM interface.
The first stage in defining the VHDL for the VGA driver is to create a VHDL entity that has the global clock and reset, the VGA output pins, and a memory interface. The outline VHDL entity is therefore given as follows:
The architecture contains a number of processes, with internal signals that manage the transfer of pixel data from memory to the screen. As can be seen from the entity, the data comes back from the memory in 8-bit blocks and we require 3 × 2 bits for each pixel and so when the data is returned, each memory byte will contain the data for a single pixel. In this example, as we are using a 640 × 480 pixel image, this will therefore require a memory that is 307,200 bytes in size as a minimum. To put this in perspective, this means that using a raw memory approach we can put three frames per megabyte. In practice, of course, we would use a form of image compression (such as JPEG for photographic images), but this is beyond the scope of this book.
We can therefore use a simple process to obtain the current pixel of data from memory as follows:
This process returns the current value of the pixel data into a signal called pixel_data, which is declared at the architecture level:
This has the red, green, and blue data defined in the lowest 6 bits of the 8-bit data word with the indexes, respectively, of 0-1, 2-3, and 4-5.
The next key process is the timing of the horizontal and vertical sync pulses, and the blanking intervals. The line timing for VGA is 31,770 ns per line with a window for displaying the data of 25,170 ns. If the FPGA is running at 100 MHz (period of 10 ns) then this means that each line requires 3177 clock cycles with 2517 for each line of pixel data, with 660 pulses in total for blanking (330 at either side). This also means that for a 640 pixel wide line, 39.3 ns are required for each pixel. We could round this up to 4 clock cycles per pixel. As you may have noticed, for the pixel retrieval we have a new internal clock signal called pclk, and we can create a process that generates the appropriate pixel clock (pclk) with this timing in place.
With this slightly elongated window, the blanking pulses must therefore reduce to 617 clock cycles and this means 308 before and 309 after the display window.
The horizontal sync pulse, on the other hand, takes place between 26,110 ns and 29,880 ns of the overall interval. This is 189 clock pulses less than the overall line time, and so the horizontal sync pulse goes low after 94 clock cycles and then at the end must return high 95 clock cycles prior to the end of the line. The difference between the outside and inside timings for the horizontal sync pulse is 377 clock cycles and so the sync pulse must return high 94 + 188 clock cycles and then return low 95 + 189 prior to the end of the window.
Thus, the horizontal sync has the following basic behavior:
and this can be implemented using a process with a simple counter:
The horizontal sync process manages the individual pixels in a line, and the vertical sync does the same for the lines as a whole to create the image. The period of a frame (containing all the lines) is defined as 16,784,000 ns. Within this timescale, the lines of the image are displayed (within 15,250,000 ns), then the vertical blanking interval is defined (up to the whole frame period of 16,784,000 ns) and finally the vertical sync pulse is defined as 1 until 15,700,000 ns at which time it goes to zero, returning to 1 at 15,764,000 ns.
Clearly it would not be sensible to define a clock of 10 ns for these calculations, so the largest common divisor is a clock of 2 s, so we can divide down the system clock by 2000 to get a vertical sync clock of 2 s to simplify and make the design as compact as possible.
where the vertical sync clock (vclk) is defined as a std_logic signal in the architecture. This can then be used to control the vsync pulses in a second process that now waits for the vertical sync derived clock:
Using this process, the vertical sync (frame synchronization) pulses are generated.
In addition to the basic horizontal and vertical sync pulse counters, we have to define a horizontal blanking pulse which sets the line data low after 25,170 ns (2517 clock cycles). This can be implemented as a simple counter in exactly the same way as the horizontal sync pulse and similarly for a vertical blanking pulse. The two processes to implement these are given in the following VHDL.
As we have seen previously, the data of each pixel is retrieved from a memory location and this is obtained using the pixel clock (pclk). The pixel clock is simply a divided (by 4) version of the system clock and at each rising edge of this pclk signal, the next pixel data is obtained from the memory data stored in the signal called data and translated into the red, green, and blue line signals. This is handled using the basic process given here:
This is a basic handler process that picks out the correct pixel data, but it does not include the video blanking signal and if this is included, then the simple VHDL changes slightly to this form:
This is the final step and completes the basic VHDL VGA handler.
The first stage in defining the Verilog for the VGA driver is to create a module that has the global clock and reset, the VGA output pins, and a memory interface. The outline Verilog module is therefore given as follows:
The module contains a number of processes, with internal signals that manage the transfer of pixel data from memory to the screen. As before, the data comes back from the memory in 8-bit blocks and we require 3 × 2 bits for each pixel and so when the data is returned, each memory byte will contain the data for a single pixel. In this example, as we are using a 640 × 480 pixel image, this will therefore require a memory that is 307,200 bytes in size as a minimum. To put this in perspective, this means that using a raw memory approach we can put three frames per megabyte. In practice, of course, we would use a form of image compression (such as JPEG for photographic images), but this is beyond the scope of this book.
We can therefore use a simple process to obtain the current pixel of data from memory as follows:
This process returns the current value of the pixel data into a signal called pixel_data which is declared at the module level:
This has the red, green, and blue data defined in the lowest 6 bits of the 8-bit data word with the indexes, respectively, of 0-1, 2-3, and 4-5.
The next key process is the timing of the horizontal and vertical sync pulses, and the blanking intervals. The line timing for VGA is 31,770 ns per line with a window for displaying the data of 25,170 ns. If the FPGA is running at 100 MHz (period of 10 ns) then this means that each line requires 3177 clock cycles with 2517 for each line of pixel data, with 660 pulses in total for blanking (330 at either side). This also means that for a 640 pixel wide line, 39.3 ns are required for each pixel. We could round this up to 4 clock cycles per pixel. As you may have noticed, for the pixel retrieval we have a new internal clock signal called pclk, and we can create a process that generates the appropriate pixel clock (pclk) with this timing in place.
With this slightly elongated window, the blanking pulses must therefore reduce to 617 clock cycles and this means 308 before and 309 after the display window.
The horizontal sync pulse, on the other hand, takes place between 26,110 ns and 29,880 ns of the overall interval. This is 189 clock pulses less than the overall line time, and so the horizontal sync pulse goes low after 94 clock cycles and then at the end must return high 95 clock cycles prior to the end of the line. The difference between the outside and inside timings for the horizontal sync pulse is 377 clock cycles and so the sync pulse must return high 94 + 188 clock cycles and then return low 95 + 189 prior to the end of the window.
Thus, the horizontal sync has the same behavior as described previously in this chapter and this can be implemented using a process with a simple counter:
The horizontal sync process manages the individual pixels in a line, and the vertical sync does the same for the lines as a whole to create the image. The period of a frame (containing all the lines) is defined as 16,784,000 ns. Within this timescale, the lines of the image are displayed (within 15,250,000 ns), then the vertical blanking interval is defined (up to the whole frame period of 16,784,000 ns) and finally the vertical sync pulse is defined as 1 until 15,700,000 ns at which time it goes to zero, returning to 1 at 15,764,000 ns.
Clearly it would not be sensible to define a clock of 10 ns for these calculations, so the largest common divisor is a clock of 2 s, so we can divide down the system clock by 2000 to get a vertical sync clock of 2 s to simplify and make the design as compact as possible.
where the vertical sync clock (vclk) is defined as a std_logic signal in the architecture. This can then be used to control the vsync pulses in a second process that now waits for the vertical sync derived clock:
Using this process, the vertical sync (frame synchronization) pulses are generated.
In addition to the basic horizontal and vertical sync pulse counters, we have to define a horizontal blanking pulse, which sets the line data low after 25,170 ns (2517 clock cycles). This can be implemented as a simple counter in exactly the same way as the horizontal sync pulse and similarly for a vertical blanking pulse. The two processes to implement these are given in the following VHDL.
As we have seen previously, the data of each pixel is retrieved from a memory location and this is obtained using the pixel clock (pclk). The pixel clock is simply a divided (by 4) version of the system clock and at each rising edge of this pclk signal, the next pixel data is obtained from the memory data stored in the signal called data and translated into the red, green and blue line signals. This is handled using a similar process as was used for the VHDL.
This is the final step and completes the basic Verilog VGA handler.
This chapter has introduced the basics of developing a simple VGA handler in VHDL and Verilog. While it is a simplistic view of the process, hopefully it has shown how a simple VGA interface can be developed using not very complex VHDL or Verilog and a building block approach. It is left to the reader to develop their own complete VGA routines for the specific monitor that they have, using the techniques developed in this chapter as a basis.
3.138.138.144