RARS 1.6 – HEX Keypad + Bitmap Display Problem
11:19 18 May 2026

RARS 1.6 – HEX Keypad + Bitmap Display Problem

I am using RARS 1.6 with:

  • Digital Lab Sim (HEX Keypad)

  • Bitmap Display

  • Polling-based keypad scanning

Assignment Requirement

Create a 4×4 grid on the Bitmap Display.

  • Each key on the HEX keypad corresponds to one cell in the grid.

  • When a key is pressed, the corresponding cell should turn red.

  • All other cells should remain black.

Example mapping:

0  1  2  3
4  5  6  7
8  9  A  B
C  D  E  F

The bitmap size is 256×256, so each cell is 64×64 pixels.


Current Problems

Problem 1: Program only works at MAX speed

The program only reacts to keypad input when I set Run Speed to MAX in RARS.

If I lower the speed (for example 30 inst/sec or 300 inst/sec):

  • keypad input stops working

  • or becomes extremely inconsistent

At MAX speed, the program sometimes freezes or becomes very laggy.

I already added delays using:

li a7, 32
li a0, 10
ecall

but the behavior is still unstable.


Problem 2: Wrong cells become red [The same cell on the main diagonal becomes red] (https://i.sstatic.net/MBvhRqpB.png)

When I press a key, the correct cell does NOT become red.

Instead:

  • only the cells on the diagonal 0-5-A-F work correctly

  • for other keys, another cell in the SAME ROW becomes red

For example:

  • pressing 1, 2, or 3 changes a wrong cell in row 0

  • pressing 6 or 7 changes another wrong cell in row 1

Only diagonal cells appear correct.

All remaining cells stay black.

This makes me think there may be a problem in:

  • keypad column scanning

  • row/column mapping

  • bitmap coordinate calculation


Environment

  • RARS 1.6

  • Bitmap Display:

    • Base address: 0x10010000

    • Unit width/height: 1

    • Display size: 256x256

  • Digital Lab Sim connected


Code

# =====================================================
# RARS 1.6  –  HEX Keypad → Bitmap 4×4 (Polling Version)
# =====================================================
# Keypad:
#   IN  0xFFFF0012  – write row mask
#   OUT 0xFFFF0014 – read column bits
#
# Bitmap Display:
#   Base address = 0x10010000
#   Width/Height = 256 x 256
#   Unit = 1 pixel
#   4 bytes/pixel
#
# Each keypad key should turn one grid cell red
# =====================================================

.eqv BITMAP_BASE    0x10010000

.eqv RED            0x00FF0000
.eqv BLACK          0x00000000

.eqv KEYPAD_ROW     0xFFFF0012
.eqv KEYPAD_COL     0xFFFF0014

.text
.globl main

# =====================================================
# MAIN
# =====================================================
main:

    # Draw initial grid (all BLACK)
    li   a0, -1
    jal  ra, drawGrid

main_loop:

    # Delay 10ms
    li a7, 32
    li a0, 10
    ecall

    # Read keypad
    jal ra, readKeypad

    li t0, -1
    beq a0, t0, main_loop

    # Draw selected cell
    jal ra, drawGrid

# -----------------------------------------------------
# Wait until key release to avoid repeated input
# -----------------------------------------------------
wait_release:

    # Delay 10ms
    li a7, 32
    li a0, 10
    ecall

    jal ra, readKeypad

    li   t0, -1
    bne  a0, t0, wait_release

    j    main_loop

# =====================================================
# readKeypad
#
# return:
#   a0 = 0..15 if a key is pressed
#   a0 = -1 if no key is pressed
#
# Mapping:
#
#   row0: 0  1  2  3
#   row1: 4  5  6  7
#   row2: 8  9 10 11
#   row3:12 13 14 15
# =====================================================
readKeypad:

    li   t2, 1              # row mask
    li   t3, 0              # row index

    li   t0, KEYPAD_ROW
    li   t1, KEYPAD_COL

rk_row:

    li   t6, 4
    bge  t3, t6, rk_none

    # Activate row
    sb   t2, 0(t0)

    # Small delay
    nop
    nop
    nop

    # Read column bits
    lbu  t4, 0(t1)

    # No key detected
    beqz t4, rk_next

    li   t5, 0              # column index

rk_col:

    li   t6, 4
    bge  t5, t6, rk_next

    andi t6, t4, 1
    bnez t6, rk_found

    srli t4, t4, 1
    addi t5, t5, 1

    j    rk_col

rk_next:

    slli t2, t2, 1
    addi t3, t3, 1

    j    rk_row

rk_none:

    li   a0, -1
    ret

rk_found:

    li   t6, 4
    mul  a0, t3, t6
    add  a0, a0, t5

    ret

# =====================================================
# drawGrid(a0 = selected cell)
#
# selected:
#   -1 → all BLACK
#   0..15 → corresponding RED cell
# =====================================================
drawGrid:

    addi sp, sp, -16

    sw   ra, 12(sp)
    sw   s0,  8(sp)
    sw   s1,  4(sp)

    mv   s1, a0
    li   s0, 0

dg_loop:

    li   t0, 16
    bge  s0, t0, dg_done

    li   t0, 4
    div  a2, s0, t0
    rem  a3, s0, t0

    beq  s0, s1, dg_red

    li   a1, BLACK
    j    dg_draw

dg_red:

    li   a1, RED

dg_draw:

    jal  ra, fillCell

    addi s0, s0, 1
    j    dg_loop

dg_done:

    lw   ra, 12(sp)
    lw   s0,  8(sp)
    lw   s1,  4(sp)

    addi sp, sp, 16

    ret
assembly cpu-architecture riscv memory-mapped-io rars-simulator