    .equ BUTTONS, 0xff200050
    .equ LOW_DISPLAYS, 0xff200020
    .equ HIGH_DISPLAYS, 0xff200030
    .global _start
    .text
_start:
    li s0, LOW_DISPLAYS
    li s1, BUTTONS
    la s2, patterns
    li s3, 0                  # Initial byte
    li s4, 0                  # Pause flag
    li t0, 15
    sw t0, 12(s1)             # Clear any old captured key edges.
    li t0, HIGH_DISPLAYS
    sw zero, 0(t0)
    li s5, 0xff202100         # mtime followed by mtimecmp
    li s6, 100000000          # Timer ticks per interval
    sw zero, 4(s5)
    sw zero, 0(s5)
    li t0, -1
    sw t0, 8(s5)              # Protect the split compare write.
    sw zero, 12(s5)
    sw s6, 8(s5)
display:
    andi t0, s3, 15
    srli t1, s3, 4
    add t0, s2, t0
    add t1, s2, t1
    lbu t2, 0(t0)
    lbu t3, 0(t1)
    slli t3, t3, 8
    or t2, t2, t3
    sw t2, 0(s0)
wait_interval:
    lw t0, 4(s5)
    bnez t0, expired
    lw t0, 0(s5)
    li t1, 50000000           # TODO: read the configured compare value.
    bltu t0, t1, wait_interval
expired:
    sw zero, 4(s5)
    sw zero, 0(s5)
    lw t0, 12(s1)
    andi t0, t0, 15
    beqz t0, advance
    sw t0, 12(s1)             # Acknowledge only observed edge bits.
    xori s4, s4, 1
advance:
    bnez s4, display
    addi s3, s3, 1
    andi s3, s3, 15           # TODO: retain a full byte.
    j display

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