Lab11 RISCV ICT
Lab11 RISCV ICT
Goals
After this laboratory exercise, you should understand the basic principles of interrupts and
how interrupts can be used for programming. You should also know the difference
between polling and using interrupts and the relative merits of these methods.
Preparation
Polling or Interrupts
A computer can react to external events either by polling or by using interrupts. One
method is simpler, while the other one is more systematic and more efficient. You will
study the similarities and differences of these methods using a simple “toy” example
program.
Each peripheral device connects to the CPU via a few ports. CPU uses address to find out
the respective port, and after that, CPU could read/write the new value to these ports to
get/control the device.
Note: Run the program at the speed of 30 ins/s to avoid RARS stop working.
1
Key matrix animation: http://hackyourmind.org/public/images/keypad12keys_anim.gif
#
# col 0x1 col 0x2 col 0x4 col 0x8
# row 0x1 0 1 2 3
# 0x11 0x21 0x41 0x81
# row 0x2 4 5 6 7
# 0x12 0x22 0x42 0x82
# row 0x4 8 9 a b
# 0x14 0x24 0x44 0x84
# row 0x8 c d e f
# 0x18 0x28 0x48 0x88
#
# Command row number of hexadecimal keyboard (bit 0 to 3)
# Eg. assign 0x1, to get key button
0,1,2,3 # assign 0x2, to get key button
4,5,6,7
# NOTE must reassign value for this address before
reading, # eventhough you only want to scan 1 row
.eqv IN_ADDRESS_HEXA_KEYBOARD 0xFFFF0012
The RISC-V ISA defines three levels of access privileges including User/Application,
Supervisor, and Machine. Access privileges define which resources (registers,
instructions, ...) can be accessed by software. This mechanism will limit the execution of
software and protect the system from software that intentionally performs unauthorized
operations. Machine privileges have the highest level of access, User/Application
privileges have the lowest level of access. RARS simulates the User/Application level.
• mtvec (Machine Trap Vector): Register containing information about the subroutine that
the CPU will execute when an interrupt occurs.
• mie (Machine Interrupt Enable): Register that sets whether to enable or disable specific
interrupt sources.
• mip (Machine Interrupt Pending): Register containing information about interrupts that
are not yet processed by the CPU.
• mepc (Machine Exception Program Counter): Register containing the value of the PC
register when an interrupt occurs.
The example below illustrates the setup and handling of interrupts generated by the Keypad
tool. Read carefully and understand how the program works.
.eqv 0xFFFF001
IN_ADDRESS_HEXA_KEYBOARD 2
.data
message: .asciz "Someone's presed a button.\n"
#
# MAIN
Procedure #
.tex
t
main
:
# Load the interrupt service routine address to the UTVEC
register la t0, handler
csrrs zero, utvec, t0
#
# No-end loop, main program, to demo the effective of
interrupt #
loop:
li a7,
li 32
ecal a0,
l
nop loo
j p
#
# Interrupt service
routine #
handler:
# ebreak # Can pause the execution to observe
registers # Saves the context
addi sp, sp, -8
sw a0, 0(sp)
sw a7, 4(sp)
# Restores the
context lw a7,
4(sp)
lw a0, 0(sp)
addi sp, sp, 8
.eqv 0xFFFF001
IN_ADDRESS_HEXA_KEYBOARD 2
.eqv 0xFFFF001
OUT_ADDRESS_HEXA_KEYBOARD
message: .asciz "Key scan code: "
#
# MAIN
Procedure #
.tex
t
main
:
la t0, handler
csrrs zero, utvec,
t0
#
# Loop to print a sequence
numbers #
xor s0, s0, s0 # count = s0 =
0 loop:
addi s0, s0, 1 # count = count +
1 prn_seq:
addi a7, zero, 1
add a0, s0, zero # Print auto sequence
number ecall
addi a7, zero, 11
li a0, '\n' # Print
EOL ecall
sleep:
addi a7, zero, 32
li a0, 300 # Sleep 300
ms ecall
j loop
end_main:
#
# Interrupt service
routine #
handler:
# Saves the
context addi
sp, sp, -
16
sw a0, 0(sp)
sw a7, 4(sp)
sw t1, 8(sp)
sw t2, 12(sp)
# Handles the
interrupt prn_msg:
addi a7, zero,
4
la a0,
message ecall
li t1,
get_key_code: IN_ADDRESS_HEXA_KEYBOARD
li t2, 0x88 # Check row 4 and re-enable
bit 7
sb t2, 0(t1) # Must reassign expected row
li t1, OUT_ADDRESS_HEXA_KEYBOARD
lb a0, 0(t1)
prn_key_code
: li a7,
ecal 34
l li a7, 11
li a0, '\ # Print
ecal n' EOL
Read this source code carefully and understand how the program works.
.eqv IN_ADDRESS_HEXA_KEYBOARD 0xFFFF0012
.eqv TIMER_NOW 0xFFFF0018
.eqv TIMER_CMP 0xFFFF0020
.eqv MASK_CAUSE_TIMER 4
.eqv MASK_CAUSE_KEYPAD 8
.data
msg_keypad: .asciz "Someone has pressed a
key!\n" msg_timer: .asciz "Time inteval!\n"
#
# MAIN
Procedure #
.tex
t
main
:
la t0,
handler csrrs
zero, 5,
t0
li t1, 0x100
csrrs zero, 4, t1 # uie - ueie bit (bit 8) - external
interrupt csrrsi zero, 4, 0x10 # uie - utie bit (bit 4) -
timer interrupt
#
# Enable interrupts you
expect #
# Enable the interrupt of keypad of Digital
Lab Sim li t1, IN_ADDRESS_HEXA_KEYBOARD
li t2, 0x80 # bit 7 of = 1 to enable
interrupt sb t2, 0(t1)
#
# No-end loop, main program, to demo the effective of
interrupt #
loop:
nop
li a7, 32
li a0, 10
ecall
nop
j loop
end_main:
#
# Interrupt service
routine #
handler:
# Saves the
context addi
sp, sp, -
16
sw a0, 0(sp)
sw a1, 4(sp)
sw a2, 8(sp)
sw a7, 12(sp)
# Handles the
interrupt csrr a1,
ucause
li a2, 0x7FFFFFFF
and a1, a1, a2 # Clear interrupt bit to get the value
li a2, MASK_CAUSE_TIMER
beq a1, a2, timer_isr
li a2, MASK_CAUSE_KEYPAD
beq a1, a2,
keypad_isr j
end_process
timer_isr:
li a7, 4
la a0,
msg_timer ecall
j end_process
keypad_isr:
li a7, 4
la a0,
msg_keypad ecall
j end_process
end_process:
# Restores the context
lw a7,
lw 12(sp)
lw a2, 8(sp)
lw a1, 4(sp)
add a0, 0(sp)
i sp, sp,
.data
message: .asciz "Exception
occurred.\n"
.text
main
: la t0, catch
csrrw zero, 5, t0 # Set utvec (5) to the handlers
csrrs address
zero, 0, # Set interrupt enable bit in ustatus
i 1 (0)
lw zero, # Trigger trap for Load access
0 fault
finally:
li a7, 10 # Exit the program
ecall
catch:
# Show message
li a7, 4
la a0,
message ecall
# Load finally address to
uepc la t0, finally
csrrw zero, 65, t0
uret
Assignment 1
Create a new project, type in, and build the program of Home Assignment 1. Run the
program step by step to understand each line of the source code. Upgrade the source code
so that it could detect all 16 key buttons, from 0 to F.
Assignment 2
Create a new project, type in, and build the program of Home Assignment 2. Run the
program step by step to understand each line of the source code.
Assignment 3
Create a new project, type in, and build the program of Home Assignment 3. Run the
program step by step to understand each line of the source code. Upgrade the source code
so that it could detect all 16 key buttons, from 0 to F.
Assignment 4
Create a new project, type in, and build the program of Home Assignment 4. Run the
program step by step to understand each line of the source code.
Assignment 5
Create a new project, type in, and build the program of Home Assignment 5. Run the
program step by step to understand each line of the source code.
Conclusion
Answer the following questions before ending the lab:
What is Polling?
What is Interrupt?
What is Interrupt Service Routine?
What are the advantages of Polling?
What are the advantages of Interrupt?
Distinguish between Interrupt, Exception and Trap?