Phillip Stanley-Marbell
Foundations of
Embedded Systems
Physical Constraints, Sensor Uncertainty, Error Propagation,
Low-Level C on RISC-V, and Open-Source FPGA Tools
Draft Version of Michaelmas 2020
Appendices
E
Coursework Activity #1:
Running Embedded C Programs
on the Sunflower Emulator
[20%]
The intended learning outcomes of this coursework item are for you to get
more insight into what goes on when you compile a C program and run
it on a microcontroller. You will also have the opportunity for self-directed
learning on how to use a number of Unix tools (Makefiles, sed, awk, grep,
cat). After successfully completing this exercise, you should be able to:
Compile any combination of C and assembly language source files to gen-
erate an S-record format binary for execution on the Sunflower emulator
(or for loading onto an embedded hardware platform).
Run an S-record format binary compiled for Sunflower on the emulator (or
load the binary into memory on an actual embedded hardware platform).
Use the Sunflower emulator commands
man and help along with the Sun-
flower emulator manual, to identify additional Sunflower emulator com-
mands relevant to tasks you want to complete using the emulator.
Use information in the map file together with information obtained from
the emulator, to debug a program that is terminating abnormally.
Apply your understanding gained of map files, binary and object file for-
mats, binary and object file sections, and interrupts, to new embedded
programming challenges.
The estimated time for most students to complete this coursework item
is between 1 and 6 hours. This estimate is based on how long it took a
number of volunteers (not enrolled in 4B25) selected by the instructor to
perform a dry-run of the exercise. Self-reported durations from 38 students
who did a similar coursework item in 2018 concur with this estimate.
4 phillip stanley-marbell
E.1 Connecting to the coursework server
The coursework server is cpu1.f-of-e.org. All the tools we will use for
this course are accessible from the command line. You will access them on
the server by connecting to the server using
ssh. You will therefore need to
be familiar with the Unix command line to use the tools for this coursework.
The first 15 minutes of the slides and video at
https://f-of-e.org/coursework-
1 provide a quick introduction to the bare essentials that you will need. Be
aware that only the first 15 minutes of the video linked above are pertinent
to this coursework.
E.2 Deadline
This coursework item is due on Friday 23rd October 2020 at 16:00 UK time.
E.3 What is due at the deadline (what you need to hand in)
1. Upload, to Moodle, a file with the answers / results of the questions listed
in Section
E.5 below placed in a single plain text file named <your CR-
Sid>-coursework-1.txt. Please make sure the file is named as specified
above. Please also make sure that the file is a plain text file, and not a
rich-text format (RTF) or any other format.
E.4 Grading rubric (how this coursework item will be graded)
You will be submitting your answers to all the questions in a single plain text
file named <your CRSid>
-coursework-1.txt which you will upload to Moo-
dle. The instructions below are purposefully not a copy-and-paste recipe.
You will need to understand the question and the action requested in each
step in order to complete it successfully.
Before getting to the questions, you will first need to Fork the
Sun-
flower repository on GitHub
1
; Clone your fork of the repository to your
1
If you are new to Git
and to GitHub, you can
find a brief tutorial on
how to fork a reposi-
tory
here. The first 15
minutes of the slides
and video at https://f-
of-e.org/coursework-1
provide an additional tuto-
rial.
Appendix A provides
a brief overview of Git and
GitHub.
account on cpu1.f-of-e.org; compile the C program in the directory
benchmarks/source/superh/findTheBugExample/ of the Sunflower emulator
source tree by running
make TREEROOT=/data/f-of-e-tools/tools/sunflower
inside the directory
benchmarks/source/superh/findTheBugExample/. Once
you have cross-compiled
findTheBugExample.c, you will have several new
files in the directory, including the map file
findTheBugExample.map. While
still in benchmarks/source/superh/findTheBugExample/, you can launch the
emulator by executing sf. The Sunflower emulator has an interactive com-
mand prompt. You can type help at the Sunflower command prompt to see
foundations of embedded systems 5
a list of commands and you can type
man followed by a command name (e.g.,
man pcbt to see the help or manual page for the command pcbt.) You can find
a detailed manual for the Sunflower emulator in the file sunflowersim-manual-and-cover.pdf
of your clone of the forked repository. You can also find a copy online at
github.com/physical-computation/sunflower-simulator. You can find addi-
tional tutorial material on the Sunflower emulator at https://f-of-e.org/fascicle-
00
.
NOTE: For this exercise, you do not need to follow the usual installation
process of compiling Sunflower since a pre-compiled version is already in-
stalled on cpu1.f-of-e.org.
6 phillip stanley-marbell
E.5 Questions
1. List the programs called by
make and state briefly what they do. You may
want to consult Chapter 4. Please state the exact names of the programs
(e.g., exactly as you would type them at the command line is the binaries
in question were already in your path) rather than any colloquial names
or pseudonyms by which the programs might informally be called.
2. By inspecting the map file (
findTheBugExample.map), determine how many
libraries are linked into the binary for findTheBugExample. State the num-
ber.
3. By inspecting the map file, how many object files within each library are
actually used?
4. Based on the contents of the map file (and not by any other method) and
based on information in Chapter
4 (especially Section 4.11), what are the
sizes (number of bytes) of the .text, .data, and .bss sections of the binary
findTheBugExample. Please state your answer in decimal.
5. By inspecting the map file, what fraction of the .text section in the final
binary is due to code that is not from findTheBugExample.c. Please state
your answer as a percentage to two decimal places.
6. Execute the commands in benchmarks/source/superh/findTheBugExample/run.m
on Sunflower. You can do this using either the load command with the
filename as an argument, or by typing in the contents of
run.m manually
to see their individual effect, or by launching
sf with the file run.m as a
command-line argument. Include a complete transcript of your actions
and the results in your submitted coursework.
After you type the final command, you will see the message Heap and
stack collision interspersed with other messages in the emulator out-
put. The emulator is still running. You can hit <ENTER> to get back to
the emulator command prompt. Describe in fewer than 150 words what
you think has happened to the emulator, to the program you are run-
ning over the emulator, or to both. What program or function printed the
text Heap and stack collision”? You have access to the source for the
findTheBugExample program as well as the source for Sunflower so you
can verify the validity of your answer.
7. Using information from the result of the
pcbt command and information
from the map file (
findTheBugExample.map), describe what you think hap-
pened to the program.
foundations of embedded systems 7
8. Apply the fix for the program
findTheBugExample.c described in
benchmarks/source/superh/findTheBugExample/README.md. By inspect-
ing the map file, how many object files within each library are used in the
.text section on the binary? Rather than counting manually, you can use
tools such as cat, grep, sed, and awk to make the process of counting more
automated. You can find out more about each of these tools from their
corresponding Unix manual pages (referred to colloquially as man pages).
You can see the man page for a tool at the command line by typing, e.g.,
man grep to see the manual page for the Unix utility grep.
9. The program in findTheBugExample.c prints in a loop that looks, from
analyzing the C code, like it should never terminate, yet the program will
terminate. Why? Explain in fewer than 150 words.
10. Submit your <your CRSid>
-coursework-1.txt results via Moodle, by Fri-
day 23rd October 2020 at 16:00 UK time.
8 phillip stanley-marbell
E.6 The Muddiest Point
Think about the following two questions and submit your responses through
this
link.
1. Approximately how many hours did you spend on this coursework?
2. What was least clear to you in this coursework? (You can simply list the
question numbers or write a few words.)
3. What was most clear to you in this coursework? (You can simply list the
question numbers or write a few words.)