Báo cáo project Môn Thực hành Kiến trúc máy tính Mã lớp 122032 Sinh viên Hoàng Trọng Nghiên 20184167 Mai Văn Linh 20184132 Vũ Minh Long 20184144 Nguyễn Việt Tùng 20184225 Phân công công việc Mai Văn L.\ Đề bài: Viết một chương trình sử dụng MIPS để vẽ một quả bóng di chuyển trên màn hình mô phỏng Bitmap của Mars). Nếu đối tượng đập vào cạnh của màn hình thì sẽ di chuyển theo chiều ngược lại. Yêu cầu: Thiết lập màn hình ở kích thước 512x512. Kích thước pixel 1x1. Quả bóng là một đường tròn. Chiều di chuyển phụ thuộc vào phím người dùng bấm, gồm có (di chuyển lên (W), di chuyển xuống (S), Sang trái (A), Sang phải (D) trong bộ giả lập Keyboard and Display MMIO Simulator). Tốc độ bóng di chuyển là không đổi. Vị trí bóng ban đầu ở giữa màn
Trang 1Báo cáo project Môn Thực hành Kiến trúc máy tính
Mã lớp: 122032 Sinh viên:
Hoàng Trọng Nghiên - 20184167 Mai Văn Linh - 20184132
Vũ Minh Long - 20184144 Nguyễn Việt Tùng - 20184225
Phân công công việc
Mai Văn Linh và Nguyễn Việt Tùng: Bài 2
Hoàng Trọng Nghiên và Vũ Minh Long: Bài 10
Trang 2Bài 2:
Đề bài:
Viết một chương trình sử dụng MIPS để vẽ một quả bóng di chuyển trên màn hình
mô phỏng Bitmap của Mars) Nếu đối tượng đập vào cạnh của màn hình thì sẽ di chuyển theo chiều ngược lại
Yêu cầu:
- Thiết lập màn hình ở kích thước 512x512 Kích thước pixel 1x1
- Quả bóng là một đường tròn
Chiều di chuyển phụ thuộc vào phím người dùng bấm, gồm có (di chuyển lên (W), di chuyển xuống (S), Sang trái (A), Sang phải (D) trong bộ giả lập Keyboard and Display MMIO Simulator) Tốc độ bóng di chuyển là không đổi Vị trí bóng ban đầu ở giữa màn hình.
I Phân tích cách làm
II Mã nguồn
1 Biến khởi tạo
.data
frameBuffer: space 0x100000 # 512 wide x 512 high pixels
tail: word 0 # location of rail on bit map display
circleUp: word 0x0000ff00 # green pixel for when snaking moving up circleDown: word 0x0100ff00 # green pixel for when snaking moving down circleLeft: word 0x0200ff00 # green pixel for when snaking moving left circleRight: word 0x0300ff00 # green pixel for when snaking moving right
xConversion: word 512 # x value for converting xPos to bitmap display
yConversion: word 4 # y value for converting (x, y) to bitmap display
2 Vẽ border
.text
main:
# draw border
la $t0, frameBuffer # load frame buffer addres
li $t1, 512 # save 512*512 pixels
li $t2, 0xFF4500 # load light gray color
drawBorderTop:
sw $t2, 0($t0) # color Pixel black
addi $t0, $t0, 4 # go to next pixel
addi $t1, $t1, -1 # decrease pixel count
bnez $t1, drawBorderTop # repeat unitl pixel count == 0
# Bottom wall section
Trang 3la $t0, frameBuffer # load frame buffer addres
addi $t0, $t0, 1046528 # set pixel to be near the bottom left addi $t1, $zero, 512 # t1 = 512 length of row
drawBorderBot:
sw $t2, 0($t0) # color Pixel black
addi $t0, $t0, 4 # go to next pixel
addi $t1, $t1, -1 # decrease pixel count
bnez $t1, drawBorderBot # repeat unitl pixel count == 0
# left wall section
la $t0, frameBuffer
addi $t1, $zero, 511 # t1 = 512 length of col
drawBorderLeft:
sw $t2, 0($t0) # color Pixel black
addi $t0, $t0, 2048 # go to next pixel
addi $t1, $t1, -1 # decrease pixel count
bnez $t1, drawBorderLeft # repeat unitl pixel count == 0
# Right wall section
la $t0, frameBuffer # load frame buffer address
addi $t0, $t0, 2044 # make starting pixel top right
addi $t1, $zero, 511 # t1 = 512 length of col
drawBorderRight:
sw $t2, 0($t0) # color Pixel black
addi $t0, $t0, 2048 # go to next pixel
addi $t1, $t1, -1 # decrease pixel count
bnez $t1, drawBorderRight # repeat unitl pixel count == 0
3 Nhận input từ Keyboard
gameUpdateLoop:
lw $t3, 0xffff0004 # get keypress from keyboard input
### Sleep for 66 ms so frame rate is about 15
addi $v0, $zero, 32 # syscall sleep
addi $a0, $zero, 66 # 66 ms
syscall
beq $t3, 100, moveRight # if key press = 'd' branch to moveright beq $t3, 97, moveLeft # else if key press = 'a' branch to moveLeft beq $t3, 119, moveUp # if key press = 'w' branch to moveUp
beq $t3, 115, moveDown # else if key press = 's' branch to moveDown
beq $t3, 0, moveUp # start game moving up
moveUp:
lw $s3, circleUp # s3 = direction of circle
add $a0, $s3, $zero # a0 = direction of circle
jal updatecircle
# move the circle
jal updateCirclePosition
j exitMoving
moveDown:
lw $s3, circleDown # s3 = direction of circle
add $a0, $s3, $zero # a0 = direction of circle
jal updatecircle
Trang 4# move the circle
jal updateCirclePosition
j exitMoving
moveLeft:
lw $s3, circleLeft # s3 = direction of circle
add $a0, $s3, $zero # a0 = direction of circle
jal updatecircle
# move the circle
jal updateCirclePosition
j exitMoving
moveRight:
lw $s3, circleRight # s3 = direction of circle
add $a0, $s3, $zero # a0 = direction of circle
jal updatecircle
# move the circle
jal updateCirclePosition
j exitMoving
exitMoving:
j gameUpdateLoop # loop back to beginning
4 Vẽ hình tròn và di chuyển hình tròn
# updatecircle: get position center circle -> Velocity -> check -? print
# getNextPosUp: *******************************************************
# getNextPosDown ***************************************************
# getNextPosLeft ***************************************************
# getNextPosRight ***************************************************
#
updatecircle:
addiu $sp, $sp, -24 # allocate 24 bytes for stack
sw $fp, 0($sp) # store caller's frame pointer
sw $ra, 4($sp) # store caller's return address
addiu $fp, $sp, 20 # setup updatecircle frame pointer
### DRAW HEAD
lw $t0, xPos # t0 = xPos of circle
lw $t1, yPos # t1 = yPos of circle
lw $t2, xConversion # t2 = 512
mult $t1, $t2 # yPos * 512
mflo $t3 # t3 = yPos * 64
add $t3, $t3, $t0 # t3 = yPos * 64 + xPos
lw $t2, yConversion # t2 = 4
mult $t3, $t2 # (yPos * 64 + xPos) * 4
mflo $t0 # t0 = (yPos * 64 + xPos) * 4
la $a1, tail
sw $t0,0($a1)
la $t1, frameBuffer # load frame buffer address
add $t0, $t1, $t0 # t0 = (yPos * 64 + xPos) * 4 + frame address
li $t5, 31
li $t6, 0x00FFFF00 # t6 - yellow
beq $a0, 0x0000ff00, getNextPosUp # xem huong di de doi chieu neu la canh tren
beq $a0, 0x0100ff00, getNextPosDown
beq $a0, 0x0200ff00, getNextPosLeft
beq $a0, 0x0300ff00, getNextPosRight
Trang 5addi $t7, $t0, -32768 # o 16 phia tren
lw $t8 ,0($t7) # lay mau
beq $t8, 0xFF4500, swapVelocityUp # dung mau thi doi huong
j loop # in ra binh thuong
getNextPosDown:
addi $t7, $t0, 30720 # o 16 phia duoi
lw $t8 ,0($t7) # lay mau
beq $t8, 0xFF4500, swapVelocityDown # dung mau thi doi huong
j loop # in ra binh thuong
getNextPosLeft:
addi $t7, $t0, -64
lw $t8 ,0($t7)
beq $t8, 0xFF4500, swapVelocityLeft
j loop
getNextPosRight:
addi $t7, $t0, 64
lw $t8 ,0($t7)
beq $t8, 0xFF4500, swapVelocityRight
j loop
#star print circle
loop:
lw $t0, xPos # t0 = xPos of circle
lw $t1, yPos # t1 = yPos of circle
addi $t2,$t0,-15 # ban kinh bang 15
addi $t3,$t1,-15
addi $t4,$t1,15
li $t5,31 # t5 = 15+15+1
circle:
sub $t7,$t2,$t0 # t7 = toa do diem tru toa do tam theo x
mul $t7,$t7,$t7 # binh phuong
sub $t8,$t3,$t1 # giong t7 nhung la theo y
mul $t8,$t8,$t8
add $t9,$t7,$t8 # x^2 + y^2
addi $t9,$t9,-225 # - R^2
bltzal $t9,circle15 # so sanh vo 0 neu nho hon thi chuyen den buoc tiep theo
addi $t2,$t2,1 # tang x va lap lai
addi $t5, $t5, -1
beqz $t5, reset
bnez $t5, circle
reset: # tang y len 1 dua x ve gia tri ban dau
li $t5,31
addi $t2,$t2,-31
addi $t3,$t3,1
beq $t3,$t4,sleep
j circle
circle15: # neu da nam trong vong tron 15 thi can theo dieu kien nam ngoai duong tron ban kinh 14
addi $t9,$t9,29 # 29 = 15^2-14^2
bgezal $t9,print # thoa man thi in ra khong thi quay lai lap
addi $t2,$t2,1
addi $t5, $t5, -1
j circle
print:
lw $t7, xConversion # t7 = 512
mult $t3, $t7 # yPos * 512
mflo $s3 # s3 = yPos * 64
add $s3, $s3, $t2 # s3 = yPos * 64 + xPos
lw $s4, yConversion # s4 = 4
mult $s3, $s4 # (yPos * 64 + xPos) * 4
mflo $s5 # s5 = (yPos * 64 + xPos) * 4
la $s6, frameBuffer # load frame buffer address
add $s5, $s5, $s6 # s5 = (yPos * 64 + xPos) * 4 + frame address
sw $t6,0($s5)
addi $t2,$t2,1
addi $t5, $t5, -1
j circle
sleep:
Trang 6move $a1 , $a0
addi $v0, $zero, 32 # syscall sleep
addi $a0, $zero, 400 # 400 ms
syscall
#end print circle
move $a0 , $a1
### Set Velocity
lw $t2, circleUp # load word circle up = 0x0000ff00
beq $a0, $t2, setVelocityUp # if head direction and color == circle up branch to setVelocityUp
lw $t2, circleDown # load word circle down = 0x0100ff00
beq $a0, $t2, setVelocityDown # if head direction and color == circle down branch to setVelocityUp
lw $t2, circleLeft # load word circle left = 0x0200ff00
beq $a0, $t2, setVelocityLeft # if head direction and color == circle left branch to setVelocityUp
lw $t2, circleRight # load word circle right = 0x0300ff00
beq $a0, $t2, setVelocityRight # if head direction and color == circle right branch to setVelocityUp
setVelocityUp:
addi $t5, $zero, 0 # set x velocity to zero
addi $t6, $zero, -20 # set y velocity to -1
sw $t5, xVel # update xVel in memory
sw $t6, yVel # update yVel in memory
j removeCirle # xoa di de them moi
setVelocityDown:
addi $t5, $zero, 0 # set x velocity to zero
addi $t6, $zero, 20 # set y velocity to 1
sw $t5, xVel # update xVel in memory
sw $t6, yVel # update yVel in memory
j removeCirle
setVelocityLeft:
addi $t5, $zero, -20 # set x velocity to -1
addi $t6, $zero, 0 # set y velocity to zero
sw $t5, xVel # update xVel in memory
sw $t6, yVel # update yVel in memory
j removeCirle
setVelocityRight:
addi $t5, $zero, 20 # set x velocity to 1
addi $t6, $zero, 0 # set y velocity to zero
sw $t5, xVel # update xVel in memory
sw $t6, yVel # update yVel in memory
j removeCirle
swapVelocityUp: # neu gap canh thi doi chieu
li $t3, 0xffff0004 # dia chi cua keyboard
li $t4, 115 # s
sw $t4,0($t3) # luu lai
j moveDown # if head direction and color == circle down branch to
setVelocityUp
swapVelocityDown: # tuong tu
li $t3, 0xffff0004
li $t4, 119
sw $t4,0($t3)
j moveUp # if head direction and color == circle down branch to
setVelocityUp
swapVelocityLeft:
li $t3, 0xffff0004
li $t4, 100
sw $t4,0($t3)
j moveRight # if head direction and color == circle down branch to
Trang 7swapVelocityRight:
li $t3, 0xffff0004
li $t4, 97
sw $t4,0($t3)
j moveLeft # if head direction and color == circle down branch to setVelocityUp
removeCirle: #xoa di de them moi # xoa giong in chi khac la thay bang mau blaclk
li $t5, 31
li $t6, 0x00000000 # t6 - black
lw $t0, xPos # t0 = xPos of circle
lw $t1, yPos # t1 = yPos of circle
addi $t2,$t0,-15
addi $t3,$t1,-15
addi $t4,$t1,15
circle_r:
sub $t7,$t2,$t0
mul $t7,$t7,$t7
sub $t8,$t3,$t1
mul $t8,$t8,$t8
add $t9,$t7,$t8
addi $t9,$t9,-225
bltzal $t9,circle15_r
addi $t2,$t2,1
addi $t5, $t5, -1
beqz $t5, reset_r
bnez $t5, circle_r
reset_r:
li $t5,31
addi $t2,$t2,-31
addi $t3,$t3,1
beq $t3,$t4,sleep_r
j circle_r
circle15_r:
addi $t9,$t9, 29 # 15 binh phuong tru 14 binh phuong
bgezal $t9,print_r
addi $t2,$t2,1
addi $t5, $t5, -1
j circle_r
print_r:
lw $t7, xConversion # t2 = 512
mult $t3, $t7 # yPos * 512
mflo $s3 # t3 = yPos * 64
add $s3, $s3, $t2 # t3 = yPos * 64 + xPos
lw $s4, yConversion # t2 = 4
mult $s3, $s4 # (yPos * 64 + xPos) * 4
mflo $s5 # t0 = (yPos * 64 + xPos) * 4
la $s6, frameBuffer # load frame buffer address
add $s5, $s5, $s6 # t0 = (yPos * 64 + xPos) * 4 + frame address
sw $t6,0($s5)
addi $t2,$t2,1
addi $t5, $t5, -1
j circle_r
sleep_r:
move $a1 , $a0
addi $v0, $zero, 32 # syscall sleep
addi $a0, $zero, 1 # 1000 ms
syscall
move $a0 , $a1
### update new Tail
lw $t5, circleUp # load word circle up = 0x0000ff00
beq $t5, $a0, setNextTailUp # if tail direction and color == circle up branch to setNextTailUp
lw $t5, circleDown # load word circle down = 0x0100ff00 beq $t5, $a0, setNextTailDown # if tail direction and color == circle down
Trang 8branch to setNextTailDown
lw $t5, circleLeft # load word circle left = 0x0200ff00 beq $t5, $a0, setNextTailLeft # if tail direction and color == circle left branch to setNextTailLeft
lw $t5, circleRight # load word circle right = 0x0300ff00 beq $t5, $a0, setNextTailRight # if tail direction and color == circle right branch to setNextTailRight
setNextTailUp:
la $t0,tail
addi $t0, $t0, -2048 # tail = tail - 2048
sw $t0, tail # store tail in memory
j exitUpdatecircle
setNextTailDown:
la $t0,tail
addi $t0, $t0, 2048 # tail = tail + 2048
sw $t0, tail # store tail in memory
j exitUpdatecircle
setNextTailLeft:
la $t0,tail
addi $t0, $t0, -4 # tail = tail - 4
sw $t0, tail # store tail in memory
j exitUpdatecircle
setNextTailRight:
la $t0,tail
addi $t0, $t0, 4 # tail = tail + 4
sw $t0, tail # store tail in memory
j exitUpdatecircle
exitUpdatecircle:
lw $ra, 4($sp) # load caller's return address
lw $fp, 0($sp) # restores caller's frame pointer
addiu $sp, $sp, 24 # restores caller's stack pointer
jr $ra # return to caller's code
updateCirclePosition:
addiu $sp, $sp, -24 # allocate 24 bytes for stack
sw $fp, 0($sp) # store caller's frame pointer
sw $ra, 4($sp) # store caller's return address
addiu $fp, $sp, 20 # setup updatecircle frame pointer
lw $t3, xVel # load xVel from memory
lw $t4, yVel # load yVel from memory
lw $t5, xPos # load xPos from memory
lw $t6, yPos # load yPos from memory
add $t5, $t5, $t3 # update x pos
add $t6, $t6, $t4 # update y pos
sw $t5, xPos # store updated xpos back to memory
sw $t6, yPos # store updated ypos back to memory
lw $ra, 4($sp) # load caller's return address
lw $fp, 0($sp) # restores caller's frame pointer
addiu $sp, $sp, 24 # restores caller's stack pointer
jr $ra # return to caller's code
III Kết quả chạy mô phỏng
Trang 10Bài 10:
Đề bài:
Sử dụng 2 ngoại vi là bàn phím và led 7 thanh để xây dựng một máy tính bỏ túi đơn giản Hỗ trợ các phép toán +, -, *, / Do trên bàn phím không có các phím trên nên sẽ dùng các phím
- Bấm phím a để nhập phép tính +
- Bấm phím b để nhập phép tính –
- Bấm phím c để nhập phép tính *
- Bấm phím d để nhập phép tính /
- Bấm phím f để nhập phép =
Yêu cầu cụ thể như sau:
- Khi nhấn các phím số, hiển thị lên LED, do chỉ có 2 LED nên chỉ hiện thị 2 số cuối cùng Ví dụ khi nhấn phím 1 hiện thị 01 Khi nhấn thêm phím 2 hiển thị 12 Khi nhấn thêm phím 3 hiển thị 23.
- Sau khi nhập số, sẽ nhập phép tính + - * /
- Sau khi nhấn phím f (dấu =) , tính toán và hiển thị kết quả lên LED.
I Phân tích cách làm
II Mã nguồn
1 Biến khởi tạo
.data
Message: asciiz "Loi chia cho 0"
.eqv IN_ADRESS_HEXA_KEYBOARD 0xFFFF0012
.eqv OUT_ADRESS_HEXA_KEYBOARD 0xFFFF0014
.eqv SEVENSEG_RIGHT 0xFFFF0010 # dia chi den LED 7 doan phai
.eqv SEVENSEG_LEFT 0xFFFF0011 # dia chi den LED 7 doan trai
2 Chạy polling
.text
main:
li $t1, IN_ADRESS_HEXA_KEYBOARD
li $t2, OUT_ADRESS_HEXA_KEYBOARD
li $t3, 0x08 # duyet hang c,d,e,f
li $t4, 0x01 # duyet hang 0,1,2,3
li $t5, 0x02 # duyet hang 4,5,6,7
li $t6, 0x04 # duyet hang 8,9,a,b
li $s0, 0x3f # Ma hien thi hang don vi
li $a1, 0x3f # Ma hien thi hang chuc
polling:
lbu $t0, 0($t2) # $t0 = ma phim quet duoc $t0=0 neu ko co phim nao dc an
bnez $t0, switch_button
nop
Trang 11sb $t4, 0($t1 ) # Quet hang 0,1,2,3
lbu $t0, 0($t2)
bnez $t0, switch_button
nop
sb $t5, 0($t1 ) # Quet hang 4,5,6,7
lbu $t0, 0($t2)
bnez $t0, switch_button
nop
sb $t6, 0($t1 ) # Quet hang 8,9,a,b
lbu $t0, 0($t2)
switch_button:
beq $t0, 0x00, free # Ko an phím nào beq $t0, 0x11, case_0 # An phím 0
beq $t0, 0x21, case_1 # An phím 1
beq $t0, 0x41, case_2 # An phím 2
beq $t0, 0x81, case_3 # An phím 3
beq $t0, 0x12, case_4 # An phím 4
beq $t0, 0x22, case_5 # An phím 5
beq $t0, 0x42, case_6 # An phím 6
beq $t0, 0x82, case_7 # An phím 7
beq $t0, 0x14, case_8 # An phím 8
beq $t0, 0x24, case_9 # An phím 9
beq $t0, 0x44, case_a # An phím a
beq $t0, 0x84, case_b # An phím b
beq $t0, 0x18, case_c # An phím c
beq $t0, 0x28, case_d # An phím d
beq $t0, 0x88, case_f # An phím f
nop
case_0:
li $a0, 0x3f # $a0 = ma hien thi so 0
li $s2, 0 # $s2 = gia tri phim dc an
j process_button
case_1:
li $a0, 0x06 # $a0 = ma hien thi so 1
li $s2, 1
j process_button
case_2:
li $a0, 0x5B # $a0 = ma hien thi so 2
li $s2, 2
j process_button
case_3:
li $a0, 0x4f # $a0 = ma hien thi so 3
li $s2, 3
j process_button
case_4:
li $a0, 0x66
li $s2, 4
j process_button
case_5:
li $a0, 0x6D
li $s2, 5
j process_button
case_6:
li $a0, 0x7d
li $s2, 6
j process_button
case_7:
li $a0, 0x07
li $s2, 7
j process_button
case_8:
li $a0, 0x7f
li $s2, 8
j process_button