0% found this document useful (0 votes)
83 views8 pages

LearningMaterial ICT4 v6 3 Week10

This document contains instructions and sample code for Laboratory Exercise 10 at Ha Noi University of Science and Technology. It discusses two main methods for CPU communication with I/O devices - port-mapped I/O using IN and OUT instructions, and memory-mapped I/O where devices access memory placed there by the CPU. Sample assembly code is provided to control a 7-segment LED display using memory addresses, draw to a bitmap display by writing color values to screen memory, and program the movement of a virtual MarsBot robot.

Uploaded by

Tien Nguyen Manh
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)
83 views8 pages

LearningMaterial ICT4 v6 3 Week10

This document contains instructions and sample code for Laboratory Exercise 10 at Ha Noi University of Science and Technology. It discusses two main methods for CPU communication with I/O devices - port-mapped I/O using IN and OUT instructions, and memory-mapped I/O where devices access memory placed there by the CPU. Sample assembly code is provided to control a 7-segment LED display using memory addresses, draw to a bitmap display by writing color values to screen memory, and program the movement of a virtual MarsBot robot.

Uploaded by

Tien Nguyen Manh
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/ 8

Ha Noi University of Science and Technology

School of Information and Communication Technology

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

For more information, see Textbook Computer Organization and Design by


Patterson & Hennessy, p.588 or Appendix A.8, or look it up online!

Sample Code and Assignments

Sample Code 1 – LED PORT


Write an assembly program to show numbers from 0 to F on a 7-segment LED.

To view the 7-segment LED, in the menu


bar, click Tools/Digital Lab Sim

Click Help to understand how to turn on


the 7-segment LED.
Ha Noi University of Science and Technology
School of Information and Communication Technology

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

Sample Code 2 – BITMAP DISPLAY


Bitmap Display like the graphic monitor in which Windows OS draws windows,
Start Button, etc. In order to to that, developer should calculate color values of all
bitmap pixels on the screen and store these color values to the screen memory.
Wherever a value in the screen memory is changed, the color of the respective
pixel on the screen will be changed.
Ha Noi University of Science and Technology
School of Information and Communication Technology

In the menu bar, click Tools / Bitmap Display to open the screen simulator.

0 R G B Each rectangular unit on the


00 FF 00 00 0x10010000 - pixel 0 display represents one memory
00 00 FF 00 0x10010004 - pixel 1 word in a contiguous address
00 00 00 00 0x10010008 - pixel 2 space starting with the specified
00 FF FF FF 0x1001000C - pixel 3 base address. For example, in
above figure, the base address is
0x10010000. The value stored in that word will be interpreted as a 24-bit RGB.

.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

Sample Code 3 – MARSBOT RIDER


The MarsBot is a virtual robot which has a very simple mode of operation.
Specifically, it travels in a 2D space, optionally leaving a trail or track. The
MarsBot uses five following words in memory:3
Name Address Meaning

HEADING 0xffff8010 Integer: An angle between 0 and 359


0xffff8020 Boolean (0 or non-0): whether or not to leave
LEAVETRACK
a track
WHEREX 0xffff8030 Integer: Current x-location of the MarsBot
WHEREY 0xffff8040 Integer: Current y-location of the MarsBot
MOVING 0xffff8050 Boolean: whether or not to move

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.

.eqv HEADING 0xffff8010 # Integer: An angle between 0 and 359


# 0 : North (up)
# 90: East (right)
# 180: South (down)
# 270: West (left)
.eqv MOVING 0xffff8050 # Boolean: whether or not to move
.eqv LEAVETRACK 0xffff8020 # Boolean (0 or non-0):
# whether or not to leave a track
.eqv WHEREX 0xffff8030 # Integer: Current x-location of
MarsBot
.eqv WHEREY 0xffff8040 # Integer: Current y-location of
MarsBot

.text
main: jal TRACK # draw track line
nop
addi $a0, $zero, 90 # Marsbot rotates 90* and start
running
jal ROTATE
nop
jal GO
nop

sleep1: addi $v0,$zero,32 # Keep running by sleeping in 1000 ms


li $a0,1000
syscall

3
http://cs.allegheny.edu/~rroos/cs210f2013
Ha Noi University of Science and Technology
School of Information and Communication Technology

jal UNTRACK # keep old track


nop
jal TRACK # and draw new track line
nop

goDOWN: addi $a0, $zero, 180 # Marsbot rotates 180*


jal ROTATE
nop

sleep2: addi $v0,$zero,32 # Keep running by sleeping in 2000 ms


li $a0,2000
syscall

jal UNTRACK # keep old track


nop
jal TRACK # and draw new track line
nop

goLEFT: addi $a0, $zero, 270 # Marsbot rotates 270*


jal ROTATE
nop

sleep3: addi $v0,$zero,32 # Keep running by sleeping in 1000 ms


li $a0,1000
syscall

jal UNTRACK # keep old track


nop
jal TRACK # and draw new track line
nop

goASKEW:addi $a0, $zero, 120 # Marsbot rotates 120*


jal ROTATE
nop

sleep4: addi $v0,$zero,32 # Keep running by sleeping in 2000 ms


li $a0,2000
syscall

jal UNTRACK # keep old track


nop
jal TRACK # and draw new track line
nop
end_main:

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

Sample Code 4 – KEYBOARD and DISPLAY MMIO


This program is used to simulate Memory-Mapped I/O (MMIO) for a keyboard
input device and character display output device. It may be run either from
MARS’s Tools menu or as a stand-alone application.
Ha Noi University of Science and Technology
School of Information and Communication Technology

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.

.eqv KEY_CODE 0xFFFF0004 # ASCII code from keyboard, 1 byte


.eqv KEY_READY 0xFFFF0000 # =1 if has a new keycode ?
# Auto clear after lw

.eqv DISPLAY_CODE 0xFFFF000C # ASCII code to show, 1 byte


.eqv DISPLAY_READY 0xFFFF0008 # =1 if the display is already to do
# Auto clear after sw

.text
li $k0, KEY_CODE
li $k1, KEY_READY

li $s0, DISPLAY_CODE
li $s1, DISPLAY_READY

loop: nop

WaitForKey: lw $t1, 0($k1) # $t1 = [$k1] = KEY_READY


nop
beq $t1, $zero, WaitForKey # if $t1 == 0 then Polling
nop
#-----------------------------------------------------
ReadKey: lw $t0, 0($k0) # $t0 = [$k0] = KEY_CODE
nop
#-----------------------------------------------------
WaitForDis: lw $t2, 0($s1) # $t2 = [$s1] = DISPLAY_READY
nop
beq $t2, $zero, WaitForDis # if $t2 == 0 then Polling
nop
#-----------------------------------------------------
Encrypt: addi $t0, $t0, 1 # change input key
#-----------------------------------------------------
ShowKey: sw $t0, 0($s0) # show key
nop
Ha Noi University of Science and Technology
School of Information and Communication Technology

#-----------------------------------------------------
j loop
nop

Warning: Must execute as below

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.

You might also like