/* Original programmable deadline divider. No interrupts or hardware toggle.
 * RAM[0] is a signed requested half-period in virtual instruction ticks.
 * RAM[4] reports the clamped effective half-period, 32 through 4096.
 * Compare (now-deadline) as signed; deadlines accumulate instead of restarting
 * at each delayed poll. Valid only while lateness stays below 2^31 ticks.
 * x11 holds the next deadline; x12 holds the GPIO0 output intention. */
    .option norvc
    .option norelax
    .section .text.start,"ax",@progbits
    .globl _start
    .type _start,@function
_start:
    lui x5, 0x40000
    addi x6, zero, 1
    sw x6, 8(x5)
    lui x7, 0x20000
    addi x8, zero, 100
    sw x8, 0(x7)
    lw x10, 256(x5)
    add x11, x10, x8
    addi x12, zero, 0
poll:
    lw x10, 256(x5)
    sub x13, x10, x11
    blt x13, zero, poll
    xori x12, x12, 1
    sw x12, 4(x5)
read_command:
    lw x8, 0(x7)
    addi x14, zero, 32
    bge x8, x14, above_min
    addi x8, zero, 32
above_min:
    lui x14, 1
    blt x8, x14, below_max
    addi x8, x14, 0
below_max:
    sw x8, 4(x7)
    add x11, x11, x8
    jal zero, poll
    .size _start, .-_start
    .section .note.GNU-stack,"",@progbits
