CS 1550 – Project 1: Double-Buffered Graphics Library
Due: Sunday, June 10, 2018, by 11:59pm
Your instructor was learning ARM assembly language over a prior summer break and was trying to make some projects for CS/COE 0447 to do. While text-based programs are okay, it’s really the graphical programs that make it a bit more interesting. So your instructor started to write a graphics library so the students could make a simple game.
In doing so, he noticed that a lot of the basic functionality the library provided was accessing the low-level operating system features that we talk about in 1550. The light bulb went off over his head, and project 1 was born.
In this project, you’ll be writing a small graphics library that can set a pixel to a particular color, draw some basic shapes, and read keypresses.
You will provide the following library functions (explained further below):
Library Call |
System Call(s) used |
void init_graphics() |
open, ioctl, mmap |
void exit_graphics() |
ioctl |
char getkey() |
select, read |
void sleep_ms(long ms) |
nanosleep |
void clear_screen(void *img) |
|
void draw_pixel(void *img, int x, int y, color_t color) |
|
void draw_line(void *img, int x1, int y1, int x2, int y2, color_t c) |
|
void *new_offscreen_buffer() |
mmap |
void blit(void *src) |
|
Each library function must be implemented using only the Linux syscalls. You may not use any C standard library functions in your library anywhere.
In this function, you should do any work necessary to initialize the graphics library. This will be at least these five things:
This is your function to undo whatever it is that needs to be cleaned up before the program exits. Many things will be cleaned up automatically if we forget, for instance files will be closed and memory can be unmapped. It’s always a good idea to do it yourself with close() and munmap() though.
What you’ll definitely need to do is to make an ioctl() to reenable key press echoing and buffering as described above.
To make games, we probably want some sort of user input. We will use key press input and we can read a single character using the read() system call. However, read() is blocking and will cause our program to not draw unless the user has typed something. Instead, we want to know if there is a keypress at all, and if so, read it.
This can be done using the Linux non-blocking system call select().
We will use the system call nanosleep() to make our program sleep between frames of graphics being drawn. From its name you can guess that it has nanosecond precision, but we don’t need that level of granularity. We will instead sleep for a specified number of milliseconds and just multiply that by 1,000,000.
We do not need to worry about the call being interrupted and so the second parameter to nanosleep() can be NULL.
If we draw directly to the framebuffer, a complex scene may render slow enough to see each part appear on screen. To get the illusion of more fluid graphics, a technique known as double buffering is often used. We make a duplicate array of pixels in RAM rather than representing any hardware device. We draw to this “offscreen buffer” by making it the target of all our graphical operations. And when it’s time to show the completed scene, we copy the offscreen buffer into the framebuffer in one big memory copy operation, known as a blit (see the next section).
For the new_offscreen_buffer() function, we will allocate a screen-sized region of our address space. Normally, we’d use the C Standard Library function malloc() to do this, but we’re avoiding all library calls. So we will once again ask the OS for memory directly, by using the same mmap() system call we used in init_graphics(). This time, we will pass slightly different parameters. The memory should still be readable and writeable, but rather than MAP_SHARED, we wish to make a private, anonymous allocation. Anonymous means that we do not have a file to map into the region. Use MAP_PRIVATE | MAP_ANONYMOUS and a file descriptor of -1 to use mmap() just like malloc(). The return value (if successful) will be the address of an appropriately-sized (screen-sized) contiguous block of address space. Return that address back to the caller.
A blit is simply a memory copy from our offscreen buffer to the framebuffer. If we were using the C Standard Library, we’d do this using memcpy(). But we must write it ourselves. So, use one or two for loops and copy every byte from the source offscreen buffer onto the frame buffer.
This is the main drawing code, where the work is actually done. We want to set the pixel at coordinate (x, y) to the specified color. You will use the given coordinates to scale the base address of the memory-mapped framebuffer using pointer arithmetic. The image will be stored in row-major order, meaning that the first row starts at offset 0 and then that is followed by the second row of pixels, and so on.
Using draw_pixel, make a line from (x1, y1) to (x2, y2). Use Bresenham’s Algorithm with only integer math. Find your favorite online source to do so (and please cite the source in a comment), but make sure your implementation works for all valid coordinates and slopes.
When we want to blank out our offscreen buffer or our framebuffer, we can just copy over every byte with the value zero. So, much like our blit(), loop over the image buffer parameter and set each byte to zero.
Download the qemu-arm.zip file from the website and extract the files into a folder.
Double-click the start.bat file in the folder to launch QEMU.
Install QEMU through Homebrew. If you don’t have Homebrew, open a terminal and type:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
Go through the install steps. When done, install qemu by typing:
brew install qemu
That will install qemu. Now you can run start.sh from the terminal in the unzipped folder to launch qemu.
Using your appropriate package manager, install qemu-system-arm, likely part of your distro’s qemu package.
Then run start.sh in the zipped folder to launch qemu.
To keep downloads small, the disk image we have provided does not have a full development environment installed. To install gcc, gdb, and ssh/scp, run the script:
./devtools.sh
When this finishes downloading and installing, you should have the ability to use most basic Linux commands, nano for text editing, and gcc, gdb, and ssh/scp/sftp for transferring files. These commands will survive a reboot, so this only needs to be done once.
If you want to install other programs, you may use the Tiny Core Linux distribution’s package installer:
tce
This lets you search for a particular package or program name and install it.
After you’ve finished installing the tools, you may wish to make a backup of the disk image in case something happens and you don’t feel like redoing all of these steps.
Shutdown linux using the command (rebooting has been turned into poweroff by an option to QEMU):
sudo reboot
Then, make a copy of disk.qcow2 someplace safe. If things go wrong, you can always restore back to this point by replacing the disk.qcow2 file in this directory with the one you’ve backed up.
With the dev tools installed you can use scp (secure copy) to transfer files in and out of QEMU.
You can download the test driver program from thoth with the command:
scp USERNAME@thoth.cs.pitt.edu:/u/OSLab/original/hilbert.c .
You can backup a file named library.c to your private folder with:
scp library.c USERNAME@thoth.cs.pitt.edu:private
One of the major contributions the university provides for the AFS filesystem is nightly backups. However, the /u/OSLab/ partition is not part of AFS space. Thus, any files you modify under your personal directory in /u/OSLab/ are not backed up. If there is a catastrophic disk failure, all of your work will be irrecoverably lost. As such, it is my recommendation that you:
Backup all the files you change under /u/OSLab or QEMU to your ~/private/ directory frequently!
Loss of work not backed up is not grounds for an extension. You have been warned.
You need to submit:
Make a tar.gz file named USERNAME-project1.tar.gz
Copy it to ~jrmst106/submit/1550/ by the deadline for credit.