LearningMaterial ICT4 v6 3 Week10
LearningMaterial ICT4 v6 3 Week10
Laboratory Exercise 10
Goals
After this laboratory exercise, students should understand the method to
communicate the CPU to peripherals.
Literature
How does the CPU communicate with I/O devices such as monitor or keyboard?
There are several ways to communicate the CPU to I/O devices. For example,
Intel processors have special instructions named IN and OUT. These instructions
are used internally for communicating with I/O devices and are usually disabled
for ordinary users. This is called port-mapped I/O. However, in this lab session,
we are going to use a different method. In particular, I/O devices access data that
is placed in memory by the CPU. Or the CPU accesses data that is placed in
memory by I/O devices. This is called memory-mapped I/O (MMIO).
7 6 5 4 3 2 1 0
Byte at Address 0xFFFF0010 • g f e d c b a
.eqv SEVENSEG_LEFT 0xFFFF0011 # Dia chi cua den led 7 doan trai.
# Bit 0 = doan a;
# Bit 1 = doan b; ...
# Bit 7 = dau .
.eqv SEVENSEG_RIGHT 0xFFFF0010 # Dia chi cua den led 7 doan phai
.text
main:
li $a0, 0x8 # set value for segments
jal SHOW_7SEG_LEFT # show
nop
li $a0, 0x1F # set value for segments
jal SHOW_7SEG_RIGHT # show
nop
exit: li $v0, 10
syscall
endmain:
#---------------------------------------------------------------
# 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
nop
jr $ra
nop
#---------------------------------------------------------------
# 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
nop
jr $ra
nop
In the menu bar, click Tools / Bitmap Display to open the screen simulator.
.eqv MONITOR_SCREEN 0x10010000 #Dia chi bat dau cua bo nho man hinh
.eqv RED 0x00FF0000 #Cac gia tri mau thuong su dung
.eqv GREEN 0x0000FF00
.eqv BLUE 0x000000FF
.eqv WHITE 0x00FFFFFF
.eqv YELLOW 0x00FFFF00
.text
li $k0, MONITOR_SCREEN #Nap dia chi bat dau cua man hinh
li $t0, RED
sw $t0, 0($k0)
nop
li $t0, GREEN
sw $t0, 4($k0)
nop
li $t0, BLUE
sw $t0, 8($k0)
nop
li $t0, WHITE
sw $t0, 12($ k0)
nop
li $t0, YELLOW
sw $t0, 32($k0)
Ha Noi University of Science and Technology
School of Information and Communication Technology
nop
li $t0, WHITE
lb $t0, 42($k0)
nop
The CPU can place commands in the HEADING, LEAVETRACK, and MOVE
locations. Afterward, the MarsBot can change its direction through HEADING
value, turn on/off the line-drawing through the LEAVETRACK value, and halt
or resume its movement through the MOVING value.
.text
main: jal TRACK # draw track line
nop
addi $a0, $zero, 90 # Marsbot rotates 90* and start
running
jal ROTATE
nop
jal GO
nop
3
http://cs.allegheny.edu/~rroos/cs210f2013
Ha Noi University of Science and Technology
School of Information and Communication Technology
#-----------------------------------------------------------
# GO procedure, to start running
# param[in] none
#-----------------------------------------------------------
GO: li $at, MOVING # change MOVING port
addi $k0, $zero,1 # to logic 1,
sb $k0, 0($at) # to start running
nop
jr $ra
nop
#-----------------------------------------------------------
# STOP procedure, to stop running
# param[in] none
#-----------------------------------------------------------
STOP: li $at, MOVING # change MOVING port to 0
sb $zero, 0($at) # to stop
nop
Ha Noi University of Science and Technology
School of Information and Communication Technology
jr $ra
nop
#-----------------------------------------------------------
# TRACK procedure, to start drawing line
# param[in] none
#-----------------------------------------------------------
TRACK: li $at, LEAVETRACK # change LEAVETRACK port
addi $k0, $zero,1 # to logic 1,
sb $k0, 0($at) # to start tracking
nop
jr $ra
nop
#-----------------------------------------------------------
# UNTRACK procedure, to stop drawing line
# param[in] none
#-----------------------------------------------------------
UNTRACK:li $at, LEAVETRACK # change LEAVETRACK port to 0
sb $zero, 0($at) # to stop drawing tail
nop
jr $ra
nop
#-----------------------------------------------------------
# ROTATE procedure, to rotate the robot
# param[in] $a0, An angle between 0 and 359
# 0 : North (up)
# 90: East (right)
# 180: South (down)
# 270: West (left)
#-----------------------------------------------------------
ROTATE: li $at, HEADING # change HEADING port
sw $a0, 0($at) # to rotate robot
nop
jr $ra
nop
While the tool is connected to MIPS, 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 MIPS program reads the Receiver Data using
an 'lw' instruction.
.text
li $k0, KEY_CODE
li $k1, KEY_READY
li $s0, DISPLAY_CODE
li $s1, DISPLAY_READY
loop: nop
#-----------------------------------------------------
j loop
nop
1. Click Run
2. Click Reset
Assignment 1
Create a new project, type in, and build the program of Sample Code 1.
Show different values on LED
Assignment 2
Create a new project, type in, and build the program of Sample Code 2.
Draw something.
Assignment 3
Create a new project, type in, and build the program of Sample Code 3.
Make the MarsBot run and draw a triangle by tracking
Assignment 4
Create a new project, type in, and build the program of Sample Code 4.
Read key character and terminate the program when receiving “exit” command.