kiyoshi_akira
kiyoshi_akira

Reputation: 1

How to Run Digital Lab Sim Smoothly on RARS?

I am currently learning how to use RISC-V. My instructor requires us to use RARS and the built-in simulated devices. However, when using the Digital Lab Sim (DLS), my RARS freezes after a few uses, and I have to restart it. This is a major inconvenience.

Below is a sample code I used to read key presses from the Hexadecimal Keyboard:

.eqv IN_ADDRESS_HEXA_KEYBOARD 0xFFFF0012
.eqv OUT_ADDRESS_HEXA_KEYBOARD 0xFFFF0014
.data
    message: .asciz "Key scan code: "
# -----------------------------------------------------------------
# MAIN Procedure
# -----------------------------------------------------------------
.macro checkKeyCode(%registor, %value, %ascii)
    li t0, %value
    bne t0, %registor, skip
    li s6, %ascii
skip:
.end_macro

.text
main:
# Load the interrupt service routine address to the UTVEC register
    la t0, handler
    csrrs zero, utvec, t0
# Set the UEIE (User External Interrupt Enable) bit in UIE register
    li t1, 0x100
    csrrs zero, uie, t1 # uie - ueie bit (bit 8)
# Set the UIE (User Interrupt Enable) bit in USTATUS register
    csrrsi zero, ustatus, 0x1 # ustatus - enable uie (bit 0)
# Enable the interrupt of keypad of Digital Lab Sim
    li t1, IN_ADDRESS_HEXA_KEYBOARD
    li t3, 0x80 # bit 7 = 1 to enable interrupt 
    sb t3, 0(t1)
# ---------------------------------------------------------
# Loop to print a sequence of 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, 5000 # 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
load_all_rows:
    li t1, IN_ADDRESS_HEXA_KEYBOARD
    li t2, OUT_ADDRESS_HEXA_KEYBOARD
    li t3, 0x1 # Start from Row 1
    li t4, 0x8 # Row 4 (maximum row)
scan_rows:
    addi t5, t3, 0x80
    sb t5, 0(t1) # Write Row (Row n) to IN_ADDRESS
    lb a0, 0(t2) # Read key scan code from OUT_ADDRESS
    beqz a0, next_row # If no key pressed, move to the next row
    # Handle key press (print scan code)
checkkeyCode:
    checkKeyCode(a0, 0x11, 0x30)
    checkKeyCode(a0, 0x21, 0x31)
    checkKeyCode(a0, 0x41, 0x32)
    checkKeyCode(a0, 0x81, 0x33)
    
    checkKeyCode(a0, 0x12, 0x34)
    checkKeyCode(a0, 0x22, 0x35)
    checkKeyCode(a0, 0x42, 0x36)
    checkKeyCode(a0, 0x82, 0x37)

    checkKeyCode(a0, 0x14, 0x38)
    checkKeyCode(a0, 0x24, 0x39)
    checkKeyCode(a0, 0x44, 0x61)
    checkKeyCode(a0, 0x84, 0x62)

    checkKeyCode(a0, 0x18, 0x63)
    checkKeyCode(a0, 0x28, 0x64)
    checkKeyCode(a0, 0x48, 0x65)
    checkKeyCode(a0, 0x88, 0x66)

next_row:
    slli t3, t3, 1 # Shift left to move to the next row
    ble t3, t4, scan_rows # Repeat for Row 4
     
prn_key_code:
    mv a0, s6
    li a7, 11
    ecall
# Restores the context
    lw t2, 12(sp)
    lw t1, 8(sp)
    lw a7, 4(sp)
    lw a0, 0(sp)
    addi sp, sp, 16
# Back to the main procedure
    uret

Could you suggest how to solve this issue? Is the problem caused by my code, the simulated device, or RARS itself? Any advice or optimizations to improve performance would be greatly appreciated! Thank for your helps!

I tried increasing the delay time, running the program at a slow speed, but it doesn't seem to work.

Upvotes: 0

Views: 50

Answers (0)

Related Questions