0% found this document useful (0 votes)
26 views68 pages

Ece 222 Risc-V Based Lab Manual-F2024 (v1.1)

Uploaded by

Ankit D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views68 pages

Ece 222 Risc-V Based Lab Manual-F2024 (v1.1)

Uploaded by

Ankit D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

University of Waterloo

Electrical and Computer Engineering Department

Digital Computers
ECE-222 Lab manual

Fall 2024
September 9, 2024

Version 1.0

Lab Instructor: Julius Olajos

Author: Julius Olajos

This manual is for the exclusive use of students registered in the course.

Reproduction or use for any other purpose is prohibited.


Contents
GENERAL INFORMATION ................................................................................................................. 4
Lab Schedule ..................................................................................................................................... 4
Lab Groups ........................................................................................................................................ 4
Lab Marking ....................................................................................................................................... 4
Lab Grading Scheme .......................................................................................................................... 5
Due Dates and On-time Delivery ......................................................................................................... 5
Lab-0: Introduction to the RISC-V platform in the Lab .................................................................... 7
Objective ....................................................................................................................................... 7
What You Do .................................................................................................................................. 7
Pre-Lab .......................................................................................................................................... 7
Introduction to Hardware and Software ........................................................................................... 7
Running assembly language code on the MCU................................................................................. 9
Sample Code Files ....................................................................................................................... 14
In-Lab Procedure .......................................................................................................................... 16
Running assembly language code on the RISC-V Board.................................................................. 16
Building (Compile) and Uploading your project to the RISC-V Board ............................................... 18
Lab Report ................................................................................................................................... 24
Lab_0 Assembly Language Code Template ....................................................................................... 25
Lab-1: Flashing LED........................................................................................................................ 26
Objective ..................................................................................................................................... 26
Background.................................................................................................................................. 26
Pre-lab ......................................................................................................................................... 26
In-lab procedure........................................................................................................................... 27
Coding Goals ............................................................................................................................... 28
Lab report .................................................................................................................................... 28
Lab_1 Assembly Language Code Template ....................................................................................... 30
Lab-1 Submission form................................................................................................................. 32
Lab-2: Subroutines and Parameter Passing................................................................................... 33
Objective ..................................................................................................................................... 33
What you do ................................................................................................................................. 33
Pre-lab ......................................................................................................................................... 33

- 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.

Lab Grading Scheme


The grade for your labs is worth 30% of your overall course grade. The Lab grade is broken down as
follows:

Grade Lab

5% Lab 0 – Intro to the RISC-V Platform


20% Lab 1 – Flashing LED
25% Lab 2 – Subroutines, Parameter Passing. Includes 10% for Quiz
25% Lab 3 – Input/Output Interfacing
25% Lab 4 – Interrupt Handling. Includes 10% for Quiz

Due Dates and On-time Delivery


Lab reports and lab demonstration sessions will carry some marks associated with each project.
They should be treated like examination sessions. Students should not miss them without a
legitimate reason, otherwise they will lose marks.

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.

Electronic lab report submission is done through LEARN (learn.uwaterloo.ca).

- 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:

• Introduction to RISC-V board

• Introduction to the VS Code/PlatformIO software

• How to create or open a project

• How to build, or assemble, a target

• How to download object code into memory on the target board

• How to debug code

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.

Introduction to Hardware and Software

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

Figure 1.1 – The Sparkfun Freedom E310 development board

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.

Running assembly language code on the MCU

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:

Figure 1.3 – Visual Studio Code Launch

• 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:

Figure 1.5 – Visual Studio Code Select Platform IO Screen

• The screen below will open. Click on the ‘Home’ button at the bottom of the screen to get
to the PlatformIO Home screen.

Figure 1.6 – Home Icon for PlatformIO

- 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:

Figure 1.7 – PlatformIO Home Screen

• 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’.

Figure 1.8 – PlatformIO ‘New Project’ Dialog Box

- 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:

.S - assembler file (your application program)

.inc – include file (contains constants, definitions, macros’ etc.)

.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)

Sample Code Files

C Code Wrapper file

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.

Running assembly language code on the RISC-V Board

• 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.

• Start the VS Code software as described in an earlier section.

• Run the PlatformIO Extension as described in an earlier section.

• You can now Create a new project by selecting New Project in the PlatformIO Quick Access
Menu. See Figure 1.10 below:

Figure 1.10 – PlatformIO ‘New Project’ Dialog Box

- 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

Figure 1.11 – PlatformIO Project Wizard Dialog Box

• 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 can see the two files in the ‘src’ 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.

Building (Compile) and Uploading your project to the RISC-V Board

• 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.

Figure 1.13 – PlatformIO Build Command

- 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.

Figure 1.14 – PlatformIO Upload Command

• 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:

Figure 1.15 – PlatformIO Start Debugger

• 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.

Figure 1.16 – PlatformIO Break Point Placement

• 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

Figure 1.18 – PlatformIO Debugging Menu Detail

• A list of the registers will open on the left side ‘Explorer’ screen as shown in Figure 1.19

• If we step from line 7 to 8, register T0 will update to 0x12345000. If we single-step line 8,


register T0 will update to 0x12345678. Figure 1.19 shows the register T0 and what it’s
contents is after executing Line 7.

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:

Figure 1.18a – PlatformIO Stop Debugging Command

- 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.

Figure 1.20 – PlatformIO Memory Selection


- 22 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
• In the Lab_0 example, Line 11 stores the data in register t0 into memory location
0x80000000. Recall that register t0 will have data 0x12345678. Therefore, if we look at
memory location 0x80000000, we should see 0x12345678.

• 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.

Figure 1.21 – PlatformIO Entering Memory Address

• 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.

Figure 1.22 – PlatformIO Entering Memory Address Bytes to Read

• 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

// Import values to registers - the values could be addresses or data


li t0, 0x123 // Load 0x123 into t0
li t1, 0x456 // Load 0x456 into t1
li t2, 0x789 // Load 0x789 into t2
li t3, 0xabc // Load 0xabc into t3
li t4, 0xdef // Load 0xdef into t4
li t5, 0x0 // Load 0x0 into t5

// Swapping the values in t0 and t1 (t5 is used as temporary buffer)


mv t5, t0 // t5 <- t0 (content of t0 is stored in t5)
mv t0, t1 // t0 <- t1 (content of t1 is stored in t0)
mv t1, t5 // t1 <- t5 (content of t5 is stored in t1)

// Adding five values together t5 <- t0 + t1 + t2 + t3 + t4


add t5, t0, t1 // t5 <- t0 + t1
add t5, t5, t2 // t5 <- t5 + t2
add t5, t5, t3 // t5 <- t5 + t3
add t5, t5, t4 // t5 <- t5 + t4

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

You will flash an LED (Light Emitting Diode) at an approximate 1 Hz frequency.

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:

- Writing 0x00FC0E3F (GPIO_ALL_LED_PINS) into memory address 0x1001200C


(GPIO_OUTPUT_VAL) to turn “off” the 8 LEDs on the LED Bar (pins GPIO 10, 11, 18 - 23)
and to turn “off” the 7 LEDs of the digital display (pins GPIO 0 – 5, 9)
- Toggle bit 11 (bit 1 of the LED bar) of address 0x00FC0E3F (GPIO_ALL_LED_PINS) and
write to memory address 0x1001200C (GPIO_OUTPUT_VAL). You will be toggling bit 11
between a 1 and a 0 with a 1 Hz frequency (500ms ON, 500ms OFF).

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.

There is no deliverable as pre-lab for this lab.

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:

• Clock frequency: 16 MHz


• command ADDI: 1 clock cycle
• command BNEZ: 1 clock cycle

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.

ADDI t4, t3, 0x100

- 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

.equ GPIO_7SEGLED_PINS, 0x0000023F // Seven Segment LED Display Pins (7)


.equ GPIO_LEDBAR_PINS, 0x00FC0C00 // LED Bar Pins (8)
.equ GPIO_ALL_LED_PINS, 0x00FC0E3F // All LED Pins (15)
.equ GPIO_LEDBAR_LED_1, 0x00000800 // LEDBAR LED1

// -------------------------------------------------------------------------------------
// Initialize the GPIO control registers and run the delay loop code
// -------------------------------------------------------------------------------------
_start:

li t0, GPIO_BASE_CTRL_ADDR // Load Base Address for GPIO Control Registers


li t1, GPIO_ALL_LED_PINS // Load GPIO Register to set GPIO_OUTPUT_EN and
GPIO_OUTPUT_XOR registers for all GPIO LED Pins
sw t1, GPIO_OUTPUT_EN(t0) // Enable outputs on all GPIO LED Pins
sw t1, GPIO_OUTPUT_XOR(t0) // XOR the LED outputs so that they are all active high
sw t1, GPIO_OUTPUT_VAL(t0) // Set all LED pins to zero to turn off all LEDS.

li t5, GPIO_LEDBAR_LED_1 // Load LED_1 Address into t5

- 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:

// Need to add 5 - 10 lines of code here


// Add the instructions to get the LED flashing

j loop // Jump back to loop and start over

- 31 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-1 Submission form

201 □ 202 □ 203 □ 204 □ Group #:


205 □ 206 □ 207 □ 208 □
Submission Statement: We (I) are (am) submitting this report for grading in ECE 222. We (I) certify that this report
(including any code, descriptions, flowcharts, etc., that are part of the submission) were written by us (me) and have
received no prior academic credit at this university or any other institution. The penalty for copying or plagiarism will
be a grade of zero (0).

Member 1 Member 2
Name: Name:

UW-ID (NOT student #) UW-ID (NOT student #)

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 --
Part-II Lab completion 35
Lab-demo
35
Questions

Part-III Hand Assembly 10


Lab report Code quality 10
Code comments 10

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.

There is no deliverable as Pre-lab for this experiment.

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.

To deal with each of the characters in the InputLUT string:


• Fetch a character by reading the string memory (from the first to last). It is in ASCII
format as shown in table 2.1
• Subtract 0x41 from the ASCII value to get the index for the Morse LUT (look up table)
• Read the Morse pattern from the Morse LUT (using the index)
• Blink the LED for the Morse pattern. This step can be broken into the following sub-
steps:
a) Move the Morse code pattern for a character in register a1.
b) Shift left the pattern in register a1 until you reach the MSB (bit 31).
c) If bit 31 is ‘1’, then call the LED_ON subroutine
d) If bit 31 is ‘0’, then call the LED_OFF subroutine
e) Call the DELAY subroutine with a delay of 1
f) If register a1 equals 0, the Morse pattern has been all displayed
g) Otherwise go to step b
• Insert a long delay (equivalent to three dots) before fetching the next character.
• Repeat the above steps for all characters. If the whole string has been processed, then
insert another four DELAY intervals and start from the beginning again as a new word.

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).

Here is some simple code to read a character:

la s1, InputLUT // assign s1 to the address at label LUT


lbu a0, 0(s1) // Read a character to convert to Morse. The character goes into
// register a0. The character comes from the array at address
// InputLUT

Here is some code to read from an array of 16-bit data:

la t1, MorseLUT // load address of lookup table into


// register t1

- 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

Lots more code here

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.

Table 2.1 – The Morse code


ASCII Morse code value
Letter Morse code
value Binary Decimal Hex
A 0x41 0000,0000,0001,0111 23 0x 17
B 0x42 0000,0001,1101,0101 469 0x 1D5
C 0x43 0000,0111,0101,1101 1885 0x 75D
D 0x44 0000,0000,0111,0101 117 0x 75
E 0x45 0000,0000,0000,0001 1 0x 1
F 0x46 0000,0001,0101,1101 349 0x 15D
G 0x47 0000,0001,1101,1101 477 0x 1DD
H 0x48 0000,0000,0101,0101 85 0x 55
I 0x49 0000,0000,0000,0101 5 0x 5
J 0x4A 0001,0111,0111,0111 6007 0x 1777
K 0x4B 0000,0001,1101,0111 471 0x 1D7
L 0x4C 0000,0001,0111,0101 373 0x 175
M 0x4D 0000,0000,0111,0111 119 0x 77
N 0x4E 0000,0000,0001,1101 29 0x 1D
O 0x4F 0000,0111,0111,0111 1911 0x 777
P 0x50 0000,0101,1101,1101 1501 0x 5DD
Q 0x51 0001,1101,1101,0111 7639 0x 1DD7
R 0x52 0000,0000,0101,1101 93 0x 5D
S 0x53 0000,0000,0001,0101 21 0x 15
T 0x54 0000,0000,0000,0111 7 0x 7
U 0x55 0000,0000,0101,0111 87 0x 57
V 0x56 0000,0001,0101,0111 343 0x 157
W 0x57 0000,0001,0111,0111 375 0x 177
X 0x58 0000,0111,0101,0111 1879 0x 757
Y 0x59 0001,1101,0111,0111 7543 0x 1D77
Z 0x5A 0000,0111,0111,0101 1909 0x 775
Notes:
- A dash is equal to three dots
- The space between parts of the same letter is equal to one dot
- The space between two letters is equal to three dots
- The space between two words is equal to seven dots
- LED on for one dot
- LED off (same length as one dot)

- 37 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-2 Submission form

201 □ 202 □ 203 □ 204 □ Group #


205 □ 206 □ 207 □ 208 □
Submission Statement: We (I) are (am) submitting this report for grading in ECE 222. We (I) certify that this report
(including any code, descriptions, flowcharts, etc., that are part of the submission) were written by us (me) and have
received no prior academic credit at this university or any other institution. The penalty for copying or plagiarism will
be a grade of zero (0).

Member 1 Member 2
Name: Name:

UW-ID (NOT student #) UW-ID (NOT student #)

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
Part-II Lab completion 40
Lab-demo Quiz (done 40
during lab)

Part-III Code quality 10


Lab report Code comments 10

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.

Here are the suggested steps to implement this program:


1. Modify your assembly language code to implement a 0.1 millisecond delay routine.
2. Create a simple counter to generate numbers from 0 to 255 (0xff) and write these to the
8 LEDs to verify the LED decoding functionality. Use a 100ms delay between numbers.
3. To implement the reflex-meter project:
a) Turn off all 8 LEDs
b) Call the provided pseudorandom number subroutine to generate a 16 bit
number, then scale it and add an offset, to result in a 2 to 10 +/- 5% second
delay.
c) Call the delay function for that amount of time (in 0.1 millisecond increments)
d) Turn one LED on (LED Bar LED_6) and start incrementing a register value once
every 0.1 millisecond
e) Monitor the status of the S1 push button using polling.
f) Once the push button is pressed, stop incrementing (exit from the loop)
g) Send the first 8 bits (least significant part) of the register to the LEDs
h) Wait for 2 seconds
i) Send the next 8 bits and wait again.
j) Do the above steps two more times until all 32 bits are shown on the LEDs.
k) Wait 5 seconds and go back to step (g).

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:

1- Go to a website like http://www.humanbenchmark.com/tests/reactiontime/ and


measure your average reaction time for comparison to your RISC-V program.
2- Merge the two programs into one by starting with the simple counter subroutine and
when the button is pressed the program changes to the reflex-meter.
3- To make the pseudo-random generator more random keep calling it every 100uS, while
in the counting subroutine. The variable time delay, while waiting for the user to press
the button to exit the counter, ensures a random delay in the game.
4- To enable replaying the game; if the button is pressed during the 5 second delay - restart
the reflex-meter.
5- If the 8 highest bits of the time delay are 0x0 simply perform a 3 second delay (while
optionally checking for a key press to restart the game) and then redisplay the time
delay again.

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

201 □ 202 □ 203 □ 204 □ Group #:


205 □ 206 □ 207 □ 208 □
Submission Statement: We (I) are (am) submitting this report for grading in ECE 222. We (I) certify that this report
(including any code, descriptions, flowcharts, etc., that are part of the submission) were written by us (me) and have
received no prior academic credit at this university or any other institution. The penalty for copying or plagiarism will
be a grade of zero (0).

Member 1 Member 2
Name: Name:

UW-ID (NOT student #) UW-ID (NOT student #)

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.

Figure 4.1 – Falling/Rising edges when the S1 push-button is pressed/released


The following steps are suggested for implementing the procedure. Add each step and test the
code.
- Revert to the Lab 3 code which used a counter to exercise all 8 LED outputs
- Modify your delay subroutine to have a resolution of 100ms (ie delay R0 * 0.1s)
- Generate a random number, in R6, by using the random number in R11. The 16-bit
output of the random number generator in R11 processed to generate an integer
between 50 and 250 - a delay between 5.0 and 25.0 seconds.
- Count the random number, in R6, down, using 1 second delays, until the count would be
<=0 and hold the display at zero on the LEDs
- Add code to configure a falling edge S1 interrupt and enable it. Your interrupt service
routine (ISR) will be called when the button is pressed.
- Write code so that the ISR generates a new random number from R11 and stores it in R6
Yes there are issues with this procedure. After a new random number is generated it can take
up to one second to display it and the new random number may be decremented by one
second nearly immediately after being generated. Fixing these problems is optional.
Lab report
Submit your commented, well-written code (TXT file OR screen capture that shows ALL code)
for Lab 4, and a picture (jpg) of the marked Submission Form, to the Lab-4 drop-box on LEARN.
Examine the Lab-4 Submission form to see what you will need to deliver.

- 45 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Lab-4 Submission form

201 □ 202 □ 203 □ 204 □ Group #:


205 □ 206 □ 207 □ 208 □
Submission Statement: We (I) are (am) submitting this report for grading in ECE 222. We (I) certify that this
report (including any code, descriptions, flowcharts, etc., that are part of the submission) were written by us (me)
and have received no prior academic credit at this university or any other institution. The penalty for copying or
plagiarism will be a grade of zero (0).

Member 1 Member 2
Name: Name:

UW-ID (NOT student #) UW-ID (NOT student #)

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
Part-II 40
Lab-demo Lab completion

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

LED Bar Led


1

LED Bar Led


0

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:

Bit Position 31 - 25 24 - 20 19 - 15 14 - 12 11 - 7 6-0

Bit Content funct7 rs2 rs1 funct3 rd opcode

The instruction bit pattern for I-type instructions (See Appendix D for a list) is shown below:

Bit Position 31 - 20 19 - 15 14 - 12 11 - 7 6-0

Bit Content imm[11:0] rs1 funct3 rd opcode

funct7 - 7-bit function code


rs2 - Operand Register 2
rs1 - Operand Register 1
funct3 - 3-bit function code
rd - Destination Register
opcode – The actual opcode of the instruction
imm - The immediate value of data (in hex)

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:

XOR t3, t5, t3

Looking at the information in Appendix D, we can see the following:

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

This will give you the following machine code:

0000000 11100 11110 100 11100 0110011

- 60 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX
Appendix H: Registers

Downloaded from reference [4]

- 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

To install the PlatformIO extension, you will need to do the following:

1. Launch VSCode. You will get a screen as below:

2. Click on Extensions. You will get a search bar as shown below:

- 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

[1] SiFive FE310-G002 Datasheet v1p2.

[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]

[4] Simon Fraser University. Downloaded from:


https://www.cs.sfu.ca/~ashriram/Courses/CS295/assets/notebooks/RISCV/RISCV_CA
RD.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.

Note for Students with Disabilities


Access Ability Services, located in Needles Hall, Room 1132, collaborates with all academic
departments to arrange appropriate accommodations for students with disabilities without
compromising the academic integrity of the curriculum. If you require academic
accommodations to lessen the impact of your disability, please register with Access Ability
Services at the beginning of each academic term.
- 67 - ECE_222_RISC-V_BASED_LAB_MANUAL-F2024 (V1.1).DOCX

You might also like