    .equ BUTTONS, 0xff200050
    .equ LOW_DISPLAYS, 0xff200020
    .equ HIGH_DISPLAYS, 0xff200030
    .global _start
    .text
_start:
    li s0, BUTTONS
    li s1, LOW_DISPLAYS
    li t0, HIGH_DISPLAYS
    sw zero, 0(t0)
    la s2, patterns
    li s3, 0                  # 0..15 for a digit, 16 for a blank
    j render

poll:
    lw t0, 0(s0)
    andi t0, t0, 15
    beqz t0, poll
    li t1, 8
    beq t0, t1, blank
    li t1, 16
    beq s3, t1, clear
    li t1, 1
    beq t0, t1, clear
    li t1, 2
    beq t0, t1, up
    li t1, 4
    bne t0, t1, released     # Use one button per action.
    beqz s3, released
    addi s3, s3, 0           # TODO: reduce the current digit
    j render
up:
    li t1, 15
    beq s3, t1, released
    addi s3, s3, 0           # TODO: advance the current digit
    j render
clear:
    li s3, 0
    j render
blank:
    li s3, 16
render:
    add t0, s2, s3
    lbu t1, 0(t0)
    sw t1, 0(s1)
released:
    lw t0, 0(s0)
    andi t0, t0, 15
    bnez t0, released
    j poll

    .section .rodata
patterns:
    .byte 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07
    .byte 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0
