       
       
       
       
       
 .set ch0_gpio, 3
 .set ch0_adc, 2
 .set ch0_led, 10
 .set ch0_out, 2
 .set ch1_gpio, 5
 .set ch1_adc, 4
 .set ch1_led, 6
 .set ch1_out, 1
 .set battery_adc, 8
 .set debounce_max_count, 3
 .set gpio_mask, (1U << 3) | (1U << 5) | (1U << 11) | (1U << 12)
 .set adc_low_thr, (2000 * 8191 / 3300)
 .bss
 .global local
ch0_next_edge:
 .long 0
ch1_next_edge:
 .long 0
ch0_debounce_counter:
 .long 0
ch1_debounce_counter:
 .long 0
 .global ch0_pulse_count
ch0_pulse_count:
 .long 0
 .global ch1_pulse_count
ch1_pulse_count:
 .long 0
 .global ch0_type
ch0_type:
 .long 0
 .global ch1_type
ch1_type:
 .long 0
 .global ch0_adc_value
ch0_adc_value:
 .long 0
 .global ch1_adc_value
ch1_adc_value:
 .long 0
 .global battery_adc_value
battery_adc_value:
 .long 0
 .global use_led
use_led:
 .long 0
 .global use_out
use_out:
 .long 0
 .global input
input:
 .long 0
 .text
 .global entry
entry:
 REG_WR ((((0x3f408800 + 0x0144)) - 0x3f408000) / 4), ((31) + (((((((0x1) >> (0)) & 1) + (((0x1) >> (0 + 1)) & 1)) + ((((0x1) >> (0 + 2)) & 1) + (((0x1) >> (0 + 2 + 1)) & 1))) + (((((0x1) >> (0 + 4)) & 1) + (((0x1) >> (0 + 4 + 1)) & 1)) + ((((0x1) >> (0 + 4 + 2)) & 1) + (((0x1) >> (0 + 4 + 2 + 1)) & 1))))) - 1), (31), ((((1) & 0x1)) & 0xff)
 REG_WR ((((0x3f408400 + 0x4)) - 0x3f408000) / 4), ((10 + 8) + (1) - 1), (10 + 8), ((1) & 0xff)
 adc r1, 0, battery_adc + 1
 move r2, battery_adc_value
 st r1, r2, 0
 REG_RD ((((0x3f408400 + 0x24)) - 0x3f408000) / 4), ((10) + (16) - 1), (10)
 move r2, input
 and r0, r0, gpio_mask
 st r0, r2, 0
 jump ch0_read
 .global ch0_read
ch0_read:
 move r0, ch0_type
 ld r0, r0, 0
 jumpr ch0_end, 0, EQ
 jumpr ch0_analog_read, 2, EQ
ch0_discrete_read:
 move r2, input
 ld r0, r2, 0
 move r3, ch0_gpio
 rsh r0, r0, r3
 and r0, r0, 1
 jump ch0_check
ch0_analog_read:
 adc r1, 0, ch0_adc + 1
 move r2, ch0_adc_value
 st r1, r2, 0
 move r0, 1
 move r3, adc_low_thr
 sub r3, r3, r1
 jump ch0_check, ov
 move r0, 0
ch0_check:
 move r3, ch0_next_edge
 ld r3, r3, 0
 add r3, r0, r3
 and r3, r3, 1
 jump ch0_changed, eq
 move r3, debounce_max_count
 move r2, ch0_debounce_counter
 st r3, r2, 0
 REG_WR ((((0x3f408400 + 0x8)) - 0x3f408000) / 4), ((10 + 2) + (1) - 1), (10 + 2), ((1) & 0xff)
 jump ch0_end
ch0_changed:
 move r3, ch0_debounce_counter
 ld r2, r3, 0
 add r2, r2, 0
 jump ch0_edge_detected, eq
 sub r2, r2, 1
 st r2, r3, 0
 jump ch0_end
ch0_edge_detected:
 move r3, debounce_max_count
 move r2, ch0_debounce_counter
 st r3, r2, 0
 move r3, ch0_next_edge
 ld r2, r3, 0
 move R1, R2
 add r2, r2, 1
 and r2, r2, 1
 st r2, r3, 0
 add r1, r1, 0
 jump ch0_edge_negative, eq
 REG_WR ((((0x3f408400 + 0x8)) - 0x3f408000) / 4), ((10 + 10) + (1) - 1), (10 + 10), ((1) & 0xff)
 REG_WR ((((0x3f408400 + 0x8)) - 0x3f408000) / 4), ((10 + 2) + (1) - 1), (10 + 2), ((1) & 0xff)
 jump ch0_end
ch0_edge_negative:
 move r0, use_led
 ld r0, r0, 0
 jumpr ch0_edge_increment, 0, EQ
 REG_WR ((((0x3f408400 + 0x4)) - 0x3f408000) / 4), ((10 + 10) + (1) - 1), (10 + 10), ((1) & 0xff)
ch0_edge_out:
 move r0, use_out
 ld r0, r0, 0
 jumpr ch0_edge_increment, 0, EQ
 REG_WR ((((0x3f408400 + 0x4)) - 0x3f408000) / 4), ((10 + 2) + (1) - 1), (10 + 2), ((1) & 0xff)
ch0_edge_increment:
 move r3, ch0_pulse_count
 ld r2, r3, 0
 add r2, r2, 1
 st r2, r3, 0
 move r0, ch0_type
 ld r0, r0, 0
 jumpr ch0_end, 2, EQ
 adc r1, 0, ch0_adc + 1
 move r2, ch0_adc_value
 st r1, r2, 0
ch0_end:
 jump ch1_read
 .global ch1_read
ch1_read:
 move r0, ch1_type
 ld r0, r0, 0
 jumpr ch1_end, 0, EQ
 jumpr ch1_analog_read, 2, EQ
ch1_discrete_read:
 move r2, input
 ld r0, r2, 0
 move r3, ch1_gpio
 rsh r0, r0, r3
 and r0, r0, 1
 jump ch1_check
ch1_analog_read:
 adc r1, 0, ch1_adc + 1
 move r2, ch1_adc_value
 st r1, r2, 0
 move r0, 1
 move r3, adc_low_thr
 sub r3, r3, r1
 jump ch1_check, ov
 move r0, 0
ch1_check:
 move r3, ch1_next_edge
 ld r3, r3, 0
 add r3, r0, r3
 and r3, r3, 1
 jump ch1_changed, eq
 move r3, debounce_max_count
 move r2, ch1_debounce_counter
 st r3, r2, 0
 REG_WR ((((0x3f408400 + 0x8)) - 0x3f408000) / 4), ((10 + 1) + (1) - 1), (10 + 1), ((1) & 0xff)
 jump ch1_end
ch1_changed:
 move r3, ch1_debounce_counter
 ld r2, r3, 0
 add r2, r2, 0
 jump ch1_edge_detected, eq
 sub r2, r2, 1
 st r2, r3, 0
 jump ch1_end
ch1_edge_detected:
 move r3, debounce_max_count
 move r2, ch1_debounce_counter
 st r3, r2, 0
 move r3, ch1_next_edge
 ld r2, r3, 0
 move R1, R2
 add r2, r2, 1
 and r2, r2, 1
 st r2, r3, 0
 add r1, r1, 0
 jump ch1_edge_negative, eq
 REG_WR ((((0x3f408400 + 0x8)) - 0x3f408000) / 4), ((10 + 6) + (1) - 1), (10 + 6), ((1) & 0xff)
 REG_WR ((((0x3f408400 + 0x8)) - 0x3f408000) / 4), ((10 + 1) + (1) - 1), (10 + 1), ((1) & 0xff)
 jump ch1_end
ch1_edge_negative:
 move r0, use_led
 ld r0, r0, 0
 jumpr ch1_edge_out, 0, EQ
 REG_WR ((((0x3f408400 + 0x4)) - 0x3f408000) / 4), ((10 + 6) + (1) - 1), (10 + 6), ((1) & 0xff)
ch1_edge_out:
 move r0, use_out
 ld r0, r0, 0
 jumpr ch1_edge_increment, 0, EQ
 REG_WR ((((0x3f408400 + 0x4)) - 0x3f408000) / 4), ((10 + 1) + (1) - 1), (10 + 1), ((1) & 0xff)
ch1_edge_increment:
 move r3, ch1_pulse_count
 ld r2, r3, 0
 add r2, r2, 1
 st r2, r3, 0
 move r0, ch1_type
 ld r0, r0, 0
 jumpr ch1_end, 2, EQ
 adc r1, 0, ch1_adc + 1
 move r2, ch1_adc_value
 st r1, r2, 0
ch1_end:
 REG_WR ((((0x3f408400 + 0x8)) - 0x3f408000) / 4), ((10 + 8) + (1) - 1), (10 + 8), ((1) & 0xff)
 jump check_wake_up
