Lab10_RISCV_ICT
Lab10_RISCV_ICT
Lab10_RISCV_ICT
Peripheral devices
Goals
After this laboratory excersice, students should understand the method to control peripheral
devices via simulation tools.
Literature
How does the CPU communicate with input and output devices such as the monitor or
keyboard?
There are several ways. Intel machines have special instructions named in and out that
communicate with I/O ports. These instructions are usually disabled for ordinary users, but
they are used internally for communicating with I/O devices. This is called port-mapped
I/O. However, we are going to look at a different method in which I/O devices have access
to memory. The CPU can place data in memory that can be read by the I/O devices;
likewise, the I/O devices can place data in memory for the CPU. This is called memory-
mapped I/O or MMIO.
.text
main:
li a0, 0x06 # Set value for 7 segments
jal SHOW_7SEG_LEFT # Show the result
li a0, 0x3F # Set value for 7 segments
jal SHOW_7SEG_RIGHT # Show the result
exit:
li a7, 10
ecall
end_main:
# ---------------------------------------------------------------
# Function SHOW_7SEG_LEFT : Turn on/off the 7seg
# param[in] a0 value to shown
# remark t0 changed
# ---------------------------------------------------------------
SHOW_7SEG_LEFT:
li t0, SEVENSEG_LEFT # Assign port's address
sb a0, 0(t0) # Assign new value
jr ra
# ---------------------------------------------------------------
# Function SHOW_7SEG_RIGHT : Turn on/off the 7seg
# param[in] a0 value to shown
# remark t0 changed
# ---------------------------------------------------------------
SHOW_7SEG_RIGHT:
li t0, SEVENSEG_RIGHT # Assign port's address
sb a0, 0(t0) # Assign new value
jr ra
In RARS, in the menu bar, click Tools / Bitmap Display to open the screen simulator.
li t0, RED
sw t0, 0(a0)
li t0, GREEN
sw t0, 4(a0)
li t0, BLUE
sw t0, 8(a0)
li t0, WHITE
sw t0, 12(a0)
li t0, YELLOW
sw t0, 32(a0)
li t0, WHITE
lb t0, 42(a0)
While the tool is connected to the program, each keystroke in the text area causes the
corresponding ASCII code to be placed in the Receiver Data register (low-order byte of
memory word 0xffff0004), and the Ready bit to be set to 1 in the Receiver Control register
(low-order bit of 0xffff0000). The Ready bit is automatically reset to 0 when the program
reads the Receiver Data using an 'lw' instruction.
.text
li a0, KEY_CODE
li a1, KEY_READY
li s0, DISPLAY_CODE
li s1, DISPLAY_READY
loop:
WaitForKey:
lw t1, 0(a1) # t1 = [a1] = KEY_READY
beq t1, zero, WaitForKey # if t1 == 0 then Polling
ReadKey:
lw t0, 0(a0) # t0 = [a0] = KEY_CODE
WaitForDis:
lw t2, 0(s1) # t2 = [s1] = DISPLAY_READY
beq t2, zero, WaitForDis # if t2 == 0 then polling
Encrypt:
addi t0, t0, 1 # change input key
ShowKey:
sw t0, 0(s0) # show key
j loop
Assignment 1
Implement the program in Home Assignment 1, change the values displayed on the LEDs
such as the last two digits of StudentID and the last two digits of the ASCII code of a
character entered from the keyboard.
Assignment 2
Write a program that lets user enter a character from the keyboard and the program will
print the last two digits of the ASCII code of the characters.
Assignment 3
Implement the program in Home Assignment 2, and then update the code so that it can
draw a chess board.
Assignment 4
Implement the program in Home Assignment 3, then update the code so that it can be
executed as follows:
Enter a lowercase character => Display the corresponding uppercase character.
Enter an uppercase character => Display the corresponding lowercase character.
Enter a digit => Display the same digit
Enter another character => Display “*”
The program will be exited if “exit” is entered.
Assignment 5
Write a program that allows the user to enter 2 points with coordinates (x1, y1) and (x2,
y2) (x1 is different from x2 and y1 is different from y2), draw and color a rectangle with 2
corners being the 2 entered points with a red border 1 unit wide and a green background.
For example, with (x1, y1) = (3, 3) and (x2, y2) = (18, 11), or (x1, y1) = (3, 11) and (x2,
y2) = (18, 3), we will have the result as the following figure.