/* Original one-shot charge-cycle light logger, not a calibrated photometer.
 * GPIO4 reads the real integrating node. GPIO2 commands discharge; GPIO3
 * latches the event after three completed threshold detections. UART 8N1
 * internal loopback reports ASCII E; there is no physical serial cable.
 * RAM words: +0 count, +4 event, +8 latest threshold timestamp (timer ticks),
 * +12 received UART report. Reset clears them and restarts integration. */
    .option norvc
    .option norelax
    .section .text.start,"ax",@progbits
    .globl _start
    .type _start,@function
_start:
    lui x5, 0x40000
    addi x6, zero, 12
    sw x6, 8(x5)
    sw zero, 4(x5)
    lui x7, 0x20000
    sw zero, 0(x7)
    sw zero, 4(x7)
    sw zero, 8(x7)
    sw zero, 12(x7)
    addi x6, zero, 8
    sw x6, 44(x5)
    addi x6, zero, 71
    sw x6, 40(x5)
    addi x11, zero, 0
wait_high:
    lw x10, 0(x5)
    andi x10, x10, 16
    beq x10, zero, wait_high
    addi x11, x11, 1
    sw x11, 0(x7)
    addi x12, zero, 4
    sw x12, 4(x5)
    lw x13, 256(x5)
    sw x13, 8(x7)
    addi x14, zero, 3
    bge x11, x14, report
wait_low:
    lw x10, 0(x5)
    andi x10, x10, 16
    bne x10, zero, wait_low
    sw zero, 4(x5)
    jal zero, wait_high
report:
    addi x6, zero, 1
    sw x6, 4(x7)
    addi x12, zero, 12
    sw x12, 4(x5)
    addi x6, zero, 69
    sw x6, 32(x5)
await_echo:
    lw x10, 36(x5)
    andi x10, x10, 4
    beq x10, zero, await_echo
    lw x10, 32(x5)
    sw x10, 12(x7)
done:
    jal zero, done
    .size _start, .-_start
    .section .note.GNU-stack,"",@progbits
