; ARM assembly tutorial for Asmonite, example 05 for the Raspberry Pi Pico (RP2040, Arm Cortex-M0+).
; Adapted from the ARM-ASM-Tutorial by Niklas Guertler (example BlinkDelay); see ..\LICENSE.txt.
;
; The same program as ..\05_blink_delay.asm. What is different on the Pico:
;   - other addresses: RESETS at 0x4000c000, IO_BANK0 at 0x40014000, other bits and SIO offsets, and the
;     pads have no isolation to remove
;   - the Cortex-M0+ knows only a part of the Thumb instructions (ARMv6-M): most of them work on r0 .. r7
;     only and are 16 bit long; "and" has two operands ("ands r2, r0": r2 = r2 and r0, flags set)
.target pico
.arch thumb
.imagebase 0x20000000

_start:
    ldr  r1, resets_clr
    ldr  r0, reset_bits
    str  r0, [r1]               ; IO_BANK0 and PADS_BANK0 out of reset
    ldr  r1, resets_done
wait_reset:
    ldr  r2, [r1]
    ands r2, r0
    cmp  r2, r0
    bne  wait_reset
    ldr  r1, gpio25_ctrl
    movs r0, #5
    str  r0, [r1]               ; GPIO 25: SIO
    ldr  r1, sio_oe_set
    ldr  r0, led_bit
    str  r0, [r1]               ; GPIO 25: output

blink_loop:
    ldr  r1, sio_out_set
    ldr  r0, led_bit
    str  r0, [r1]               ; LED on
    ldr  r2, delay_count
delay1:
    subs r2, r2, #1
    bne  delay1
    ldr  r1, sio_out_clr
    ldr  r0, led_bit
    str  r0, [r1]               ; LED off
    ldr  r2, delay_count
delay2:
    subs r2, r2, #1
    bne  delay2
    b    blink_loop

    align 4
resets_clr:  dd 0x4000f000      ; RESETS.RESET (0x4000c000), +0x3000: clear bits
reset_bits:  dd 0x00000120      ; bit 5 IO_BANK0, bit 8 PADS_BANK0
resets_done: dd 0x4000c008      ; RESETS.RESET_DONE
gpio25_ctrl: dd 0x400140cc      ; IO_BANK0.GPIO25_CTRL
sio_oe_set:  dd 0xd0000024      ; SIO.GPIO_OE_SET
sio_out_set: dd 0xd0000014      ; SIO.GPIO_OUT_SET
sio_out_clr: dd 0xd0000018      ; SIO.GPIO_OUT_CLR
led_bit:     dd 0x02000000      ; bit 25
delay_count: dd 1000000
