Ece 222 Risc-V Based Lab Manual-F2024 (v1.1)
Ece 222 Risc-V Based Lab Manual-F2024 (v1.1)
Digital Computers
ECE-222 Lab manual
Fall 2024
September 9, 2024
Version 1.0
This manual is for the exclusive use of students registered in the course.
- 1 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
In-lab procedure........................................................................................................................... 33
Lookup Tables .............................................................................................................................. 35
Lab report .................................................................................................................................... 36
The Morse Code ........................................................................................................................... 37
Lab-2 Submission form................................................................................................................. 38
Lab-3: Input / Output Interfacing .................................................................................................. 39
Objective ..................................................................................................................................... 39
What you do ................................................................................................................................. 39
Background.................................................................................................................................. 39
Pre-lab ......................................................................................................................................... 39
In-lab procedure........................................................................................................................... 40
Optional Improvements ................................................................................................................ 41
Lab report .................................................................................................................................... 41
Extra Information .......................................................................................................................... 42
Lab-3 Submission form................................................................................................................. 43
Lab-4: Interrupt Handling.............................................................................................................. 44
Objective ..................................................................................................................................... 44
Pre-lab ......................................................................................................................................... 44
What you do ................................................................................................................................. 44
In-lab procedure........................................................................................................................... 44
Lab report .................................................................................................................................... 45
Lab-4 Submission form................................................................................................................. 46
Appendix A: PlatformIO Main Screen Details ............................................................................... 47
Appendix B: SiFive FE310-G002 MCU Pinout ................................................................................ 49
Appendix C: MCU Board, Daughter Board Schematics .................................................................. 52
Appendix D: Abbreviated Instruction Set Summary ..................................................................... 55
Appendix E: SiFive FE310-G002 User Manual v1p5 ...................................................................... 57
Appendix F: Memory Map ............................................................................................................. 58
Appendix G: Hand Assembly ......................................................................................................... 59
Appendix H: Registers ................................................................................................................... 61
Appendix I: Exceptions and Interrupts ......................................................................................... 62
Appendix J: Installing the PlatformIO Extension .......................................................................... 63
Appendix K: References ................................................................................................................ 66
- 2 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
University Expectations and Policies ............................................................................................. 67
Academic Integrity........................................................................................................................ 67
Grievance .................................................................................................................................... 67
Discipline ..................................................................................................................................... 67
Appeals........................................................................................................................................ 67
Note for Students with Disabilities ................................................................................................ 67
- 3 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
GENERAL INFORMATION
This section contains general information about the ECE-222 lab. All lab contents and resources are
posted on the University of Waterloo on-line learning system called LEARN. It is a password
protected environment and can be accessed here: learn.uwaterloo.ca
This course will be using a Sparkfun RISC-V development board with a University of Waterloo
designed daughter board that has the hardware necessary to complete the projects.
The IDE to be used for this course is VS Code running PlatformIO as an extension. VS Code is a text
editor, and PlatformIO is a cross-platform, cross-architecture, multiple framework for software
developers to write applications.
Lab Schedule
The three hours lab sessions scheduled for this course are listed here: Please see the Lab Schedule
posted on LEARN
Lab Groups
All labs are to be done in groups of two students. Groups of three, or more, students are prohibited.
If a lab session contains an odd number of students, every effort will be made to pairup the single
student with another student from other sessions, if students’ schedules allow.
It is expected that both members will put equal effort into the lab tasks during the term. Unequal
participation or other conflicts in a group should be brought to the lab instructor’s attention at the
earliest possible time.
Lab Marking
The course marking scheme is stated in the Course Outline.
There are three main components related to each lab session. The lab manual for each experiment
will tell you what you will need to submit for that component.
• Prelab. It is designed to get you started with the task. Once you’ve accomplished what is
asked in this section, you will be ready to start coding in assembly language.
- 4 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
• Lab session/Demo. You will present your work to a lab staff to be marked for that section.
Some questions will be posed to students regarding the contents, procedures, debugging,
and techniques used to get the code working correctly.
• Lab report. You will submit a report which often contains your assembly language code, and
a TA will mark your report.
Different labs carry different marks allocated to them. Marking sheets are in the lab manual.
Warning: Failure to complete ALL labs may result in an Incomplete mark for the course. This means
each student is expected to attend all lab sessions.
Grade Lab
If you have an interview scheduled during a lab demonstration session, or if you have to miss a lab
demonstration session because of another legitimate reason, please inform the Lab Instructor to
avoid being recorded as ‘Absent’. They will try to assign you to another session for that particular
lab.
Demos are done during your scheduled lab period. There is a 100% penalty for late demos so be
sure to contact the lab instructor if something prevents you from demoing during the scheduled lab
period.
- 5 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Reports are due within 48 hours of your scheduled lab period and there is a 25% per day (including
weekends) late penalty.
AI tools such as GitHub Copilot and ChatGPT may not be used to complete the labs.
Details about deadlines and penalties are included in the Course Outline.
- 6 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-0: Introduction to the RISC-V platform in the Lab
Objective
We will familiarize ourselves with the basics of the RISC-V board used in the ECE-222 lab. Here is a
short list of what we will do in this session:
What You Do
In this lab you will load, assemble, download, and run a short program. This program loads some
values into registers, performs an addition function, among other tasks. You will confirm the result
by checking the contents of the registers in debug mode.
Pre-Lab
You should familiarize yourself with the Lab 0 chapter in this manual and you should also look at
Chapter D of this manual to familiarize yourself with the typical RISC-V Instruction Set.
In order to get students familiarized with the tools used in the ECE-222 lab, let us take a closer look
at the hardware and software used in the lab. More details can be found in Appendices B and C.
Hardware
Figure 1.1 shows the RISC-V Development board. This board employs a SiFive Freedom E310 micro-
controller unit (MCU).
- 7 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Wake PB Reset PB
The heart of the board is the SiFive Freedom E310 MCU (microcontroller unit), which contains a
CPU, on-chip flash memory, RAM (Random Access Memory) and some peripheral blocks.
The CPU is a SiFive E31 with a 32-bit RV32IMAC architecture. The raw clock speed is 150 MHz.
Figure 1.2 shows the daughter board plugged into the RISC-V board. This daughter board provides
the pushbuttons and LED’s that will be required for the projects in this lab.
Led 7 Led 0
Pb S2
Pb S1
- 8 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.2 – The RISC-V Daughter Board
Software
The software toolchain used to program the RISC-V board is VSCode/PlatformIO.
Note: if you do not have the PlatformIO extension installed, refer to Appendix J on how to do this.
You may have to do this each time you log onto a different station in the lab.
Use the following steps to get yourself familiarized with the IDE software.
• Launch the software by clicking on Start and scrolling down to the Visual Studio Code
software. Click on the Visual Studio Code icon as shown below in Fig. 1.3:
• The program will open and you will see the following screen:
- 9 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.4 – Visual Studio Code Opening Screen
• Start the PlatformIO Extension by clicking on the PlatformIO extension as shown in the
screen below:
• The screen below will open. Click on the ‘Home’ button at the bottom of the screen to get
to the PlatformIO Home screen.
- 10 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
• The home screen for PlatformIO will come up as shown below in Figure 1.7:
• Notice in Figure 1.7 the selections for ‘New Project’ and ‘Open Project’.
• To create a new project, click on “New Project’. The Project Wizard dialog box shown below
in Figure 1.8 will open. Notice that a sample new project name Test was entered and the
Sparkfun RED-V Thing Plus board has been selected. The Freedom E SDK framework is
automatically filled in when you select the board. When you are done, click on ‘Finish’.
- 11 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
• There will be about a minute for the project to initialize.
• Should you get the following dialog box, just click ‘Allow’ 1 – 3 times, however many it asks
you to do.
• Should you get the following dialog box, just click on the ‘Trust the authors of all files……’
box and then click on the ‘Yes, I trust the Authors’ button.
• You should now see a similar PlatformIO project screen below as in Figure 1.9.
- 12 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.9 – PlatformIO Project Screen
• On the left side of the screen, you will see a directory. This is the directory for the just
created project called TEST. This directory is accessed via the ‘Explorer’ Icon in the left-hand
side tool bar and is shown as . Under the directory, there are several
subdirectories. The directories hold various programs and documents.
• There is an INI file called platformio.ini. This file stores the hardware environment. It
describes the hardware and software framework that is to be used for this project. If you
recall, this was specified in the Project Wizard dialog box previously configured when the
new project was created. This INI file is automatically created for you from that Project
Wizard dialog box.
• The ‘src’ directory stands for ‘source’, and this is where your application (project) code(s) is
stored. It will comprise of various files, such as assembler code, C code, and header and
definition files, amongst others as needed. Opening an example project displays the
following files in the ‘src’ directory:
- 13 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
• The file extensions are defined as follows:
.c - C code file (this will be used as the C code wrapper file that calls the assembler files)
.h - header file (this is the file where global constants and declarations are stored)
The C Code Wrapper file contains calls to your various programs. An example called ‘superblink.c’ is listed
below.
- 14 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
This example shows that ‘stdio.h’ and ‘superblink.h’ header files store various definitions and constants. The
setupGPIO.S assembler file is called to set up the boards I/O ports, and the toggleLED.S and delay.S files are
called in a WHILE loop to be executed. This will flash the LED.
Another example is a header, or definition file. This file stores various constants. In the below
example, #define DELAY1 sets value DELAY1 to 500. #define ON sets value ON as 0x01, in effect a
Boolean 1, whereas #define OFF sets value OFF to 0x00, in effect a Boolean 0.
There are also several functions called void. For example, void setupGPIO( ); is a function
declaration in C/C++. It declares a function named setupGPIO that takes no arguments and returns
no value. It tells the complier that somewhere in the code, there is a definition for this function.
- 15 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
In-Lab Procedure
First, we will build and run code on the MCU. Then we will review how to debug the code and learn
some of the basic Assembly language commands.
• You will need to download the files from LEARN in the Lab_0 tab. These will go into the
‘Downloads’ folder. The files are:
i. Lab_0_top.c
ii. Lab_0.S
• Create a new directory on your N: drive called ECE222. Create a new subdirectory called
Lab_0 (ie: N:ECE222\Lab_0)
• Move the files from the ‘Download’ folder into this new folder you created on your N: drive.
• You can now Create a new project by selecting New Project in the PlatformIO Quick Access
Menu. See Figure 1.10 below:
- 16 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
• You will be presented with a dialog box called the Project Wizard to select a name and the
board that you will be using. You will use Lab_0 for the project name and then select
Sparkfun RED-V Thing Plus for the board as shown in Figure 1.11 below:
Make sure
you ‘uncheck’
this box
Select your
directory on
your N: drive
• You can now look at and edit these files in the file directory on the left side of the PlatformIO
screen under the 'src' directory. See Figure 1.12 below:
- 17 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.12 – PlatformIO Project Files Directory
• You will not need to edit any of these files for Lab_0. Your task will be to run the debugger
and single step through each line of the Lab_0.S assembly file to see how the commands are
updating the registers and memory locations.
• To Build (Compile) your project, on the main toolbar, click on Build as shown in Figure. 1.13.
You can find this at the far right top of the screen.
- 18 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
• If the Build (Compile) is successful, you will see the text ‘Success’ at the bottom of the
terminal screen in green.
• Now, you must Upload your project to the RISC-V Development board to be able to run the
debugger. To Upload your project to the RISC-V Development board, click on Upload as
shown in Figure. 1.14. You can find this at the far right top of the screen.
• If the Upload is successful, you will see the text ‘Success’ at the bottom of the terminal
screen in green.
• Now that your code is uploaded to the RISC-V board, you need to start a Debugging session.
This will allow you to single-step through your assembly code. To run a debugging session,
on the main toolbar, click on Run, and then select Start Debugging, as show below in Figure
1.15:
• This will take a minute or two, and if you had previously successfully assembled the code,
you will see the text ‘Success’ at the bottom of the terminal screen in green.
• You will need to put in a break point on the first line of the application code as shown below
in Figure 1.16. You can enter a break point by clicking on the line of code where you want
the break point to be with your mouse. Clicking it again will remove the break point. In
- 19 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.16, a break point has been placed on line 8 of the assembly code. The yellow arrow
at line 9 is where the cursor is currently at and will be the next command executed.
• A ‘Debugging’ menu will have opened near the top of the screen as shown in Figure 1.17.
Figure 1.18 shows more detail of this ‘Debugging’ menu. This menu ONLY appears if there
has been a successful configuring of the Debugger.
- 20 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.17 – PlatformIO Debugging Menu
Step Into
(Single-Step) Step Out
Restart
Step Over
Stop
Continue
• A list of the registers will open on the left side ‘Explorer’ screen as shown in Figure 1.19
NOTE: Do not forget to stop the debugger if you want to make changes to your code. You can
then save your changes, re-build and then download again. See Fig. 1.18a below:
- 21 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.19 – PlatformIO Register List
• Memory locations can also be inspected while in debugging mode. To inspect a memory
location, on the explorer section of the screen is a section called memory. See Figure 1.20.
• To look at what memory location 0x8000000 has stored in it, under Memory, there is a
dialog box that says ‘Enter Address……’. Click on that, and a new dialog box appears near the
centre-top of the screen. Here you will enter the memory address. Enter 0x80000000 as
shown in Figure 1.21 below and then click Enter.
• You will now be prompted to enter how many bytes to read. In this case, 0x16 bytes is to be
read. This can be any range you’d like, but it’s best to keep it relatively small. In this dialog
box, enter 0x16 for 16 bytes. See Figure 1.22 below and then click Enter.
• A memory dialog box will open showing the memory you selected for a total of 16 bytes.
See Figure 1.23 below. You will note that the address range being displayed is from
0x80000000 to 0x80000015. That is a total of 22 bytes (0x16 = 22 decimal)
• You will also notice that the data is displayed as 78 56 34 12. It is the “reverse” of 12345678.
This happens because this particular processor stores data in memory in a little-endian
format. Little-endian stores the least significant byte first.
- 23 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.23 – PlatformIO Requested Memory Address Inspection
Lab Report
Although there is no deliverable assigned to this lab, attendance is mandatory. You must complete
Lab 0 before starting Lab 1. Attendance is taken during the lab session as proof that you completed
Lab 0.
- 24 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab_0 Assembly Language Code Template
// Lab_0.S
// ---------------------------------------
.align 4
.globl _start
_start:
// Store 0x12345678 into memory address 0x80000000 in little-endian format
lui t0, 0x12345 // Load upper half of t0 with 0x12345000
addi t0, t0, 0x678 // Add lower half to t0 to get 0x12345678
li t1, 0x80000000 // Load upper half of t1 with 0x80000000
sw t0, 0(t1) // Store word t0 at address pointed to by t1
LOOP:
j LOOP // Branch back to this line - an infinite loop
- 25 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-1: Flashing LED
Objective
The objective of this lab is to complete, assemble and download a simple assembly language
program. Here is a short list of what you will do in this session:
- Modify the C wrapper code for the Lab_1 to call the Lab_1 assembly code.
- Write some RISC-V assembly language instructions.
- Use different memory addressing modes.
- Test and debug the code on the RISC-V Development Board
Background
The Development Board uses the SiFive FE-310-G002 microprocessor. Conditional instructions
are executed directly. There is no Program Status Register (PSR) as in other common
microprocessors, such as the x86 or ARM processors.
Code can be conditionally executed by using an instruction which tests whether the result of
an executed instruction equals zero or not equals zero and then branching because of this test.
BNEZ and BEQZ are the only branches you will use. BNEZ will branch if the result of the
instruction is not equal 0. BEQZ will branch if the result of the instruction is equal to 0.
To flash an LED, one needs to know how the FE-310-G002 microprocessor is connected to the
LEDs – the pin configuration and interfacing. Much of the hardware interfacing details are
covered in Lab-3. The details to accomplish this in this lab are:
Pre-lab
Before the lab session, look at the RISC-V instruction set in Appendix D.
To see a flashing LED, implement a delay between the LED “on” and “off” states. Think about
implementing a delay in assembly language.
- 26 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Hint: Increment or decrement a register in a loop until it reaches a certain value.
In-lab procedure
Complete the given code that is given at the end of Lab-1 manual. Feel free to change any part
of the code if you wish to do so.
Try to make a connection between the given code and the flowcharts, and then complete the
given code. All you need to do is add about 5 – 8 lines of lines of code that constructs the main
loop that causes the LED to flash.
- Create a new folder (like N:\ECE222\Lab_1) and project as was done in Lab-0
- Start by turning off all eight LEDs.
- Then implement the flashing LED code using an infinite loop which toggles bit 11 (this is
actually bit 1 of the LED Bar) of the address 0x00FC0E3F (GPIO_ALL_LED_PINS)
- Figure 1.1 shows the two different approaches. The shorter flowchart leads to smaller
code size and is more efficient. Your code, when demonstrated for marking, must be
using the short flowchart. But it is strongly recommended that you implement the
longer flowchart as the first step. Once you get the longer flowchart working, changing
your code to implement the short flowchart will be an easy step.
- Don’t forget to insert a 500ms delay in the loop; otherwise, the LED blinks too fast to
see.
- Assemble the code, download it to the board, and debug it if necessary.
- To run the code, select “Run without debugging” from the drop-down menu.
To calculate the number of times you need to run your delay loop to create 500ms delay, you
will need to know the following information:
You will need to think about why you would need to know some commands # of clock cycles to
calculate your delay number.
To get more information about the GPIO ports, refer to chapter 17 in SiFive FE310-G002 Manual
v1p5. (Can find this on LEARN content tab)
- 27 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 1.1 – Flowcharts for flashing LED
Coding Goals
The goal is to get the LED flashing at a frequency close to 1 Hz (on for 500ms and off for
500ms). An accurate period of 1Hz is NOT the primary goal of this lab; time it or count the
number of flashes in 60 seconds to scale the number to get better than 10% accuracy.
All code should be well commented. The number of lines of code added to the given assembly
language program does NOT affect your mark. ALL documentation should be within the
program.
Lab report
Hand-assemble, using Appendix G, the instruction below (if you want a challenge select
another supported instruction) using the tables in Appendix G.
- 28 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
The assembly instruction should appear as comments at the end of your code.
Submit your commented, well-written code (TXT file OR screen capture that shows ALL code),
and a picture (jpg) of the marked Submission form, to the Lab-1 drop-box LEARN. To
understand the deliverables, look at the Lab1 Submission form.
- 29 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab_1 Assembly Language Code Template
.section .text
.globl _start
.align 2
// -------------------------------------------------------------------------------------
// Name: Lab_1.S
// Purpose: This code will flash a single LED at an approximate 1 Hz frequency
// Author: Julius Olajos
// -------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------
// GPIO Control Registers Memory Mapping
.equ GPIO_BASE_CTRL_ADDR, 0x10012000 // Base address for the GPIO control registers
.equ GPIO_OUTPUT_EN, 0x08 // address offset for enabling GPIO outputs
.equ GPIO_OUTPUT_VAL, 0x0C // Address offset for writing to GPIO outputs
.equ GPIO_OUTPUT_XOR, 0x40 // Address offset for GPIO Outputs XOR
// -------------------------------------------------------------------------------------
// 8 LEDS, 7 Segment LED Display Pins Register Address Mapping
// -------------------------------------------------------------------------------------
// Initialize the GPIO control registers and run the delay loop code
// -------------------------------------------------------------------------------------
_start:
- 30 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
lw t3, GPIO_OUTPUT_VAL(t0) // Read GPIO output values into t3
xor t3, t5, t3 // XOR the GPIO output value (all zeroes to the LEDs)
sw t3, GPIO_OUTPUT_VAL(t0) // Store this new value in GPIO output register
loop:
li t4, 0x000FFFFF // Load a starter value for the countdown timer value
// You will need to calculate a correct value for 1 Hz
loop1:
- 31 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-1 Submission form
Member 1 Member 2
Name: Name:
Signature: Signature:
Note: Reports submitted without a signed submission statement will receive a grade of zero (0).
Total 100
TA/LI: _________________
- 32 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-2: Subroutines and Parameter Passing
Objective
In structured programming, big tasks are broken into small routines. A short program is written
for each routine. The main program then calls these short subroutines.
In most cases when a subroutine is called, some information and parameters must be
communicated between the main program and the subroutine. This is called parameter
passing.
In this lab, you will use subroutines and parameter passing by implementing a Morse code
system.
What you do
In this lab you will turn one LED into a Morse code transmitter. You will cause one LED to blink
in Morse code for a five-character word. The LED must be turned on and off with specified time
delays until all characters are communicated.
Pre-lab
Think about implementing Lab-1 code using subroutines. Write a subroutine called LED_OFF
that turns LED bit 11 of address 0x1000200C (LED bar bit 1) off, and another subroutine called
LED_ON that turns the LED on. Write a third subroutine called DELAY that takes one input
parameter (register t0) and waits for t0 * 500ms (roughly) before returning.
In-lab procedure
Use the code you developed in lab-1 as a start point for this lab. A template code for lab-2 is
available on LEARN. Start by initializing the on-board LEDs to off. Then additional
functionalities are added to the code as shown in the flowchart depicted in figure 2.1. This is
described in the following steps:
- Turn all LEDs off (go to lab-1 for memory addresses and contents to write)
- Put the initials of the two lab partners together to create a four-character word (Capital
letters only). Add a fifth character of your choice (capital) which is different from the
four previous ones. Set the five characters in your program at InputLUT label.
- Write a subroutine called LED_OFF that turns LED_7 on the LED Bar to the correct bit of
address 0x1000200C “OFF”.
- Write a subroutine called LED_ON that turns the LED_7 on the LED Bar to the correct bit
- 33 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
of address 0x1000200C “ON”
- Write a subroutine called DELAY that causes an t0 * 500ms delay before returning to
main program. t0 is passed to subroutine from the main program.
- Write a subroutine called CHAR2MORSE that converts an ASCII code into a Morse
pattern. You will use registers for parameter passing between subroutines and the main
program.
NOTE: Do not forget to stop the debugger if you want to make changes to your code. You can
then save your changes, re-build and then download again. See Fig. 1.18a in a previous section.
- 34 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Figure 2.1 – Flowchart for the Morse code transmission using an LED
Lookup Tables
Lookup tables are used to provide data to a program. You should be careful about how you read
the data and index into it. Data can be 8-bit (byte), 16-bit (half-word) or 32-bit (word). Data has
been stored using .half (2 bytes, or 16-bits).
- 35 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
add t1, t1, a0 // add index of character to MorseLUT
// address into register t1
lhu a1, 0(t1) // load data stored at address of MorseLUT
// plus index into register a1
InputLUT:
.asciz "BIRDS" // This is the 5 character word to be sent
// to the LED in Morse code
.align 2
MorseLUT:
.half 0x17, 0x1D5, 0x75D, 0x75 // A, B, C, D
.half 0x1, 0x15D, 0x1DD, 0x55 // E, F, G, H
.half ...
.half ... // Use the template code on
// LEARN for complete list
In order to work with 16-bit data, the LUT for Morse code has to be type .half. You
will need to increment by 2 (bytes) to move through the data and to read the table,
you will need to use LHU.
Example: Suppose the lab partner’s initials plus a fifth letter compose the word BIRDS. Then
the program should extract the letters (B I R D S) and create a Morse code pattern like this:
Please note that all five letters are considered to be one word.
Lab report
Submit your commented, well-written code (TXT file OR screen capture that shows ALL code),
and a picture (jpg) of the marked Submission form, to the Lab-2 drop-box LEARN. Examine the
Lab-2 Submission form to see what you will need to deliver.
- 36 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
The Morse Code
Table 2.1 shows corresponding Morse codes for the English language alphabet.
Note the “Morse code” is left justified and the “Binary Morse code value” is right justified.
- 37 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-2 Submission form
Member 1 Member 2
Name: Name:
Signature: Signature:
Note: Reports submitted without a signed submission statement will receive a grade of zero (0).
Total 100
TA/LI: _________________
- 38 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-3: Input / Output Interfacing
Objective
The objective of this lab is to learn how to use peripherals (LEDs, pushbutton) connected to a
microprocessor. The ARM CPU is connected to the outside world using Ports and in this lab you will
setup and use Input and Output ports.
What you do
In this lab you will measure how fast a user responds (reflex-meter) to an event accurate to a
10th of a millisecond. Initially all LEDs are off and after a random amount of time (between 2 to
10 seconds), one LED turns on (LED_6 on the LED Bar) and then the user presses the
pushbutton (S1).
Between the two events of ‘Turning the LED on’ and ‘Pressing the push button’, a 32-bit counter
is incremented every 10th of a millisecond in a loop. The final value of this 32-bit number will be
sent to the 8 LEDs in separate bytes with a 2 second delay between them.
A delay of 2 to 10 seconds +/-5% is required. To get a 100us delay loop just scale the value
used in Lab 1. A pseudorandom number in register a7 will be between 1 and 65,535 and this is
used to provide the delay value between 20,000 and 100,000. At least two ways to do this are:
1) Keep generating random numbers until one fits the range required
2) Take a certain number of bits (between 8 and 16) of register a7 and scale it to fit
Background
During the code development, you will need to read through some parts of chapters 4 and 17 of
the SiFive FE310-G002 Manual [2]. Part of the information you will need to gather from that
reference is given in Appendix B of this lab manual. Appendix C also shows the full-blown
schematic diagram of the Development Board.
Note that in the previous labs the formula you used to derive the number of cycles your delay
loop needed to run to get a 500ms delay was just an approximation. You will need to fine tune
that number a bit. Once you get the reflex meter to work, you can use the stopwatch on your
phone to measure out a 10 second time before you press the pushbutton. You can then look at
the value of the number of cycles that are being run, and then scale that for 100us.
Pre-lab
There is no deliverable for this part. It is for your practice only.
Determine what to write in which memory-address to turn any of the 8 LEDs on or off. Refer to
Appendix B and to Chapter 17 (GPIO) of the E310-G002 manual for more information. By doing this
- 39 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
step, you should understand the code that was given to you in Lab-1 to turn the LED bar LED 1 on
and off.
Keep in mind that when you write to a pin on the output, you do not want to overwrite the other
pins. This means that you would read the output or input pins, mask out the pin that you want to
write to and then write to only that pin.
In-lab procedure
Please note that you will have to demonstrate two parts in Lab-3:
- A simple counter subroutine that increments from 0x00 to 0xFF, wraps to 0, and
continues counting. This will prove that the bits are displayed in the correct order on the
LEDs.
- The reflex-meter.
LED’s
- 40 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
LED's
P.23
P.22
P.21
P.20
P.19
P.18
P.11
P.10
LED_7
LED_6
LED_5
LED_4
LED_3
LED_2
LED_1
LED_0
MSB LSB
Optional Improvements
Here are some optional ways to improve the program if you have the time and interest:
Lab report
Submit your commented, well-written code for the simple counter AND reflex-meter (TXT file
OR screen capture that shows ALL code), and a picture (jpg OR PDF) of the marked Submission
form, to the Lab-3 drop-box LEARN.
Answer these questions and put them as comments at the end of your program:
1- If a 32-bit register is counting user reaction time in 0.1 milliseconds increments, what is
the maximum amount of time which can be stored in 8 bits, 16-bits, 24-bits and 32-bits?
2- Considering typical human reaction time, which size would be the best for this task (8,
16, 24, or 32 bits)?
3- Prove time delay meets 2 to 10 sec +/- 5% spec.
- 41 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Extra Information
Random numbers with Fibonacci linear feedback shift registers at WikiPedia:
http://en.wikipedia.org/wiki/Linear_feedback_shift_register
- 42 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-3 Submission form
Member 1 Member 2
Name: Name:
Signature: Signature:
Note: Reports submitted without a signed submission statement will receive a grade of zero (0).
Weight Grade Comment
Part-I Pre-lab 0
Simple counter 15
Part-II
Reflex-meter 25
Lab-demo
Questions 40
Code quality 6
Part-III Code comments 6
Lab report 2 – 10 sec. +/- 5% proof 6
Two questions 2
Total 100
TA/LI: _________________
- 43 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-4: Interrupt Handling
Objective
The objective of this lab is to learn about interrupts. You will enable an interrupt source in the
SiFive FE310-G002 microprocessor, and write an interrupt service routine (ISR) that is triggered
when the S1 pushbutton is pressed. The ISR returns to the main program after handling the
interrupt.
Pre-lab
There is no deliverable for this part. It is for your education only.
Read the contents of Appendix I of this lab manual. Review the information presented in the
section ‘9.5.6 – GPIO interrupt registers’ of the LPC17xx User manual [1].
What you do
Reuse your Lab-3 code. The random number generator output will be used to generate a
number which gives a time delay of 5.0 to 25.0 seconds with a resolution of 0.1s. ie. an integer
between 50 and 250.
Once the program is started, R6 is set to 0. Then, all eight LEDs flash on and off at a rate
between 1 and 10 Hz while the random number routine is called continuously. A random
number between 50 and 250 will then be created from the generated random number in R11,
which then stores it in R6. An S1 push button press causes the interrupt service routine (ISR) to
run. The main program then displays this random number (without a decimal so that 5.6
seconds displays as 56 in binary) on the 8 LEDs. The program delays for one second and then
the count in R6 is decremented by the equivalent of 1 second (10) and the new count (time left)
displayed. This continues. When the count goes to zero or less, all decrementing stops and R6
is set to 0 and the program starts flashing all LEDs again and so the process repeats. Do NOT
USE BLO, BHS, BGT, BGE, BLT or BLE as they are for signed number comparisons only and
they will not work as expected!
In-lab procedure
The following steps are suggested for handling the S1 button as a source of interrupt:
- The S1 button or P2.10 pin is, by configured correctly as a GPIO input pin.
- Enable the EINT3 channel (External INTerrupt 3) which is shared with GPIO interrupts
with ISER0 (Interrupt Set Enable 0 – Table 52 of the LPC17xx User manual [1])
- Enable the GPIO interrupt on pin P2.10 for falling edge with IO2IntEnF (IO 2 INTerupt
Enable Falling) using Table 117 of the LPC17xx User manual [1]. P2.10 is high when the
S1 button is not pressed. This is why sensitivity to falling edge (see Figure 4.1) is
necessary. If you configure it so that it is sensitive to the rising edge on P2.10 then the
program will react to the releasing of the push button.
- 44 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
- Complete the External Interrupt 3 (EINT3) interrupt service routine EINT3_IRQHandler.
There are two parts to this routine:
o Generate a new random number from R11 that is between 50 and 250 and store
it in R6
o Clear the cause of interrupt (falling edge on P2.10 pin) using IO2IntClr (IO 2
INTerrupt CLearR) detailed in Table 123 of the LPC17xx User manual [1]. Do NOT
disable the interrupt input. Just clear the latch that has recorded an interrupt on
the falling-edge for the GPIO pin. This is often missed.
Hint: Putting a breakpoint in the ISR for EINT3_IRQHandler will reveal if the ISR is called or not,
and whether it is run only once or more often. This is very helpful when debugging.
- 45 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-4 Submission form
Member 1 Member 2
Name: Name:
Signature: Signature:
Note: Reports submitted without a signed submission statement will receive a grade of zero (0).
Quiz (done 40
during lab)
Part-III 10
Lab report Code quality
10
Code comments
Total 100
TA/LI: ____________
- 46 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix A: PlatformIO Main Screen Details
Build/Upload
Toolbar
Main Toolbar
Explorer Screen
Code Screen Terminal Screen
(shows errors)
- 47 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Debugger Screen
showing Registers
- 48 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix B: SiFive FE310-G002 MCU Pinout
Pushbutt
LED Bar
LED Bar
LED Bar
LED Bar
LED Bar
LED Bar
Led 2
Led 4
Led 3
Led 5
Led 7
Led 6
on S2
Led 2
Pushbutton
S1
7-Seg
segment ‘g’
7-Seg
segment ‘f’
7-Seg
segment ‘e’
7-Seg
segment ‘d’
7-Seg
segment ‘c’
7-Seg
segment ‘b’
7-Seg
segment ‘a’
- 49 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
- 50 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
This information in this appendix was taken from the document SiFive FE310-G002 Datasheet
v1p2, Chapter 2 [1]
- 51 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix C: MCU Board, Daughter Board Schematics
- 52 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Downloaded from Sparkfun.com.:
https://cdn.sparkfun.com/assets/a/c/3/e/4/RedVThingPlus.pdf [3]
- 53 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Daughter Board Schematic
- 54 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix D: Abbreviated Instruction Set Summary
(Downloaded from Reference [4])
- 55 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
- 56 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix E: SiFive FE310-G002 User Manual v1p5
This Manual is located on LEARN in the Contents tab. It can also be downloaded from:
https://sifive.cdn.prismic.io/sifive/034760b5-ac6a-4b1c-911c-f4148bb2c4a5_fe310-g002-
v1p5.pdf
- 57 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix F: Memory Map
This table in this appendix was taken from the document SiFive FE310-G002 Manual, v1p5,
Chapter 4 [2]
- 58 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix G: Hand Assembly
The SiFive FE310-G002 instructions are 32-bits long (in the 32-bit version microprocessor)
The instruction bit pattern for R-type instructions (See Appendix D for a list) is shown below:
The instruction bit pattern for I-type instructions (See Appendix D for a list) is shown below:
Note from the Instruction Set Summary in Appendix D that you can find all the fields that you
need for the opcode. You will also need to look at Appendix H for the register values. Note that
the register values that are put into the machine code are from column ‘Register’. The column
‘ABI Name’ is what we use in the assembly program. For example, register s1 is called x9 in the
Register column. We would then use 1001 as the code for this register in the machine code.
Example:
Type (FMT): R
- 59 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
funct7: 0000000
rs2(t3): 11100
rs1(t5): 11110
funct3: 100
rd(t3): 11100
opcode: 0110011
- 60 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix H: Registers
- 61 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix I: Exceptions and Interrupts
- 62 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix J: Installing the PlatformIO Extension
- 63 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
3. Type in PlatformIO in the search bar shown below:
4. You will see the PlatformIO Install screen to the right as shown below:
- 64 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
5. Click on Install. This should only take a few seconds. When it is done, you should see
the screen below: Click on the PlatformIO icon.
6. When the installation is finished, you will see a message window on the bottom right of
the screen that the installation is finished as shown below. If the ‘Reload Now’ button is
visible, then click that to restart VSCode, or alternatively, you can just close VSCode and
relaunch it.
- 65 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix K: References
[2] SiFive FE310-G002 Manual, v1p5. Sept. 22, 2022. Published by SiFive Inc.
[3] RedVThingPlus Schematic, V10. Under the Creative Commons License. Downloaded
from: https://cdn.sparkfun.com/assets/a/c/3/e/4/RedVThingPlus.pdf on [July 29, 2024]
[5] D.Patterson and A. Waterman, The RISC-V Reader: An Open Architecture Atlas, First Ed.,
November 7, 2017, Published by Strawberry Canyon LLC.
- 66 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
University Expectations and Policies
The following statements represent university expectations and policies with respect to
academic integrity, discipline, grievance, student appeals, and academic accommodations. If
you would like more clarification, please contact your course instructor directly.
Academic Integrity
In order to maintain a culture of academic integrity, members of the University of Waterloo
community are expected to promote honesty, trust, fairness, respect and responsibility. [Check
uwaterloo.ca/academic-integrity for more information.]
Grievance
A student who believes that a decision affecting some aspect of his/her university life has been
unfair or unreasonable may have grounds for initiating a grievance. Read Policy 70, Student
Petitions and Grievances, Section 4, uwaterloo.ca/secretariat/policies-procedures-
guidelines/policy-70. When in doubt please be certain to contact the department’s
administrative assistant who will provide further assistance.
Discipline
A student is expected to know what constitutes academic integrity [check
uwaterloo.ca/academic-integrity] to avoid committing an academic offence, and to take
responsibility for his/her actions. A student who is unsure whether an action constitutes an
offence, or who needs help in learning how to avoid offences (e.g., plagiarism, cheating) or
about “rules” for group work/collaboration should seek guidance from the course instructor,
academic advisor, or the undergraduate Associate Dean. For information on categories of
offences and types of penalties, students should refer to Policy 71, Student Discipline,
uwaterloo.ca/secretariat/policies-procedures-guidelines/policy-71. For typical penalties check
Guidelines for the Assessment of Penalties, uwaterloo.ca/secretariat/policies-procedures-
guidelines/guidelines/guidelines-assessment-penalties.
Appeals
A decision made or penalty imposed under Policy 70 (Student Petitions and Grievances) (other
than a petition) or Policy 71 (Student Discipline) may be appealed if there is a ground. A student
who believes he/she has a ground for an appeal should refer to Policy 72 (Student Appeals)
uwaterloo.ca/secretariat/policies-procedures-guidelines/policy-72.