← Back to Index
Research & Engineering Archive

CO 6. Low-levelGraphicsinSoftware

By Jingnan Huang · March 09, 2026 · 1089 Words

Last Edit: 3/9/26

Intro to Low-Level Graphics
#

image.png

Starting from the structure of the De1-SoC, different parts have own role on Graphics

Coordinate System
#

The screen has been divided into boxes with 320 x 240 pixels

And the origin is at the left top, this means that the right down corner is then $(319,239)$

Pixel Encoding - RGB565
#

On the screen, each box is representing one pixel, while each pixel is encoded using 16 bits

This is because human are more sensitive to green

Address Mapping
#

As memory is 1 Dimension but Screen is 2D, so we’ve design a way to representing 2D

So as the De1-SoC is a 32 bit system, we will divided this 32 bits into

Thus overall we can conclude the formula for calculating the Address’s corresponding pixel

$$ \text{address} = \text{base} + (y \ll 10) + (x \ll 1) $$

Ex. Calculating the Pixel Address
#

Code Example
#

/* program to draw four green pixels into the middle of the screen */

void plot_pixel(int x, int y, short int line_color); // plots one pixel
void clear_screen();                                // clears whole screen

int pixel_buffer_start; // global variable location of frame buffer

int main(void)
{
    volatile int * pixel_ctrl_ptr = (int *)0xFF203020;  // pointer to the base register of controller

    /* The default base of the frame buffer is int VGA controller */
    /* That default address is stored in the base register of the VGA controller port/interface */
    /* so the next statement gets it, leaving pixel_buffer_start = 0x08000000 */

    pixel_buffer_start = *pixel_ctrl_ptr;

    clear_screen();

    plot_pixel(160, 120, 0x07E0);
    plot_pixel(161, 120, 0x07E0);
    plot_pixel(162, 120, 0x07E0);
    plot_pixel(163, 120, 0x07E0);
}

// code not shown for clear_screen() and draw_line() subroutines

void plot_pixel(int x, int y, short int line_color)
{
    volatile short int *one_pixel_address;

    one_pixel_address = pixel_buffer_start + (y << 10) + (x << 1);

    *one_pixel_address = line_color;
}

void clear_screen()
{
    int y, x;

    for (x = 0; x < 320; x++)
        for (y = 0; y < 240; y++)
            plot_pixel(x, y, 0);
}

Drawing Lines on Screen
#

Known that the boxes one screen are discrete, thus drawing a straight lines are hard

image.png

Things like this case will occur which is called aliasing, but that’s already the best approximation

The way we calculate it is by the slope $slope = \frac{y_2 - y_1}{x_2 - x_1}$

Animation
#

The animation is just 1. Draw lines, 2. Erase Lines, 3. Repeat, here’s more detail of approach

Buffer Register
#

Front Buffer stores the starting address of the current image

Flickering
#

If there’s no new image after the old one has been removed, then flickering will occur that there’s a blank or broken image

Vertical Synch
#

The VGA controller will scan the screen from left to right, up to down

Double Buffering
#

The ultimate solution for animation, thus set two buffer regions, Front Buffer and Back Buffer , and the update steps will be

  1. Remove and draw the new shape inside Back Buffer
  2. Wait for the Vertical Synch Signal
  3. Swap

VGA Controller Memory Mapped Registers
#

image.png


Process of swapping to avoid Flickering
#

Initiate
#

At the beginning, the processor will send a 1 into the Front Buffer Reg that tells VGA controller to swap when ready

While in this case, the S bit in Status Reg will swap to 1, meaning the swapping is in progress

Wait for V-Sync
#

The swap won’t happen immediately, that’s because if the screen is at its middle of scanning, the upper and lower part of the screen will occur flickering

Thus only run swap when V-Sync is ready

C Language Code for wait_for_vsync()
#

void wait_for_vsync() {
    // Base Address for VGA Controller
    volatile int * pixel_ctrl_ptr = (int *) 0xff203020; 
    int status;

    // Wrtite a 1 into the Front Reg
    *pixel_ctrl_ptr = 1;

    // Read the status Reg
    status = *(pixel_ctrl_ptr + 3);

    // Polling until the last bit becomes 1
    while ((status & 0x01) != 0) {
        status = *(pixel_ctrl_ptr + 3);
    }
    // If it goes to 0, auto break
}

Poll
#

The processor will then use polling to check the bit S to see when the swapping is finished, so it can start drawing the next frame

Frame Per Second
#

So 60 FPS means the processor can render one frame less than $1/60$ s, if something went wrong that it requires more than $1/60$ s to render a frame, then we want to keep the frame frequency lower but keep the overall animation contingence

Bouncing Boxes
#