micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
# test passing arguments
@micropython.asm_thumb
def arg0():
mov(r0, 1)
print(arg0())
@micropython.asm_thumb
def arg1(r0):
add(r0, r0, 1)
print(arg1(1))
@micropython.asm_thumb
def arg2(r0, r1):
add(r0, r0, r1)
print(arg2(1, 2))
@micropython.asm_thumb
def arg3(r0, r1, r2):
add(r0, r0, r1)
add(r0, r0, r2)
print(arg3(1, 2, 3))
@micropython.asm_thumb
def arg4(r0, r1, r2, r3):
add(r0, r0, r1)
add(r0, r0, r2)
add(r0, r0, r3)
print(arg4(1, 2, 3, 4))

View File

@@ -0,0 +1,5 @@
1
2
3
6
10

View File

@@ -0,0 +1,29 @@
# test bcc instructions
# at the moment only tests beq, narrow and wide versions
@micropython.asm_thumb
def f(r0):
mov(r1, r0)
mov(r0, 10)
cmp(r1, 1)
beq(end)
mov(r0, 20)
cmp(r1, 2)
beq_n(end)
mov(r0, 30)
cmp(r1, 3)
beq_w(end)
mov(r0, 0)
label(end)
print(f(0))
print(f(1))
print(f(2))
print(f(3))

View File

@@ -0,0 +1,4 @@
0
10
20
30

View File

@@ -0,0 +1,16 @@
@micropython.asm_thumb
def clz(r0):
clz(r0, r0)
print(clz(0xF0))
print(clz(0x8000))
@micropython.asm_thumb
def rbit(r0):
rbit(r0, r0)
print(hex(rbit(0xF0)))
print(hex(rbit(0x8000)))

View File

@@ -0,0 +1,4 @@
24
16
0xf000000
0x10000

View File

@@ -0,0 +1,23 @@
# test bl and bx instructions
@micropython.asm_thumb
def f(r0):
# jump over the internal functions
b(entry)
label(func1)
add(r0, 2)
bx(lr)
label(func2)
sub(r0, 1)
bx(lr)
label(entry)
bl(func1)
bl(func2)
print(f(0))
print(f(1))

View File

@@ -0,0 +1,2 @@
1
2

View File

@@ -0,0 +1,11 @@
# test constants in assembler
@micropython.asm_thumb
def c1():
movwt(r0, 0xFFFFFFFF)
movwt(r1, 0xF0000000)
sub(r0, r0, r1)
print(hex(c1()))

View File

@@ -0,0 +1 @@
0xfffffff

View File

@@ -0,0 +1,18 @@
@micropython.asm_thumb
def sdiv(r0, r1):
sdiv(r0, r0, r1)
@micropython.asm_thumb
def udiv(r0, r1):
udiv(r0, r0, r1)
print(sdiv(1234, 3))
print(sdiv(-1234, 3))
print(sdiv(1234, -3))
print(sdiv(-1234, -3))
print(udiv(1234, 3))
print(udiv(0xFFFFFFFF, 0x7FFFFFFF))
print(udiv(0xFFFFFFFF, 0xFFFFFFFF))

View File

@@ -0,0 +1,7 @@
411
-411
-411
411
411
2
1

View File

@@ -0,0 +1,15 @@
@micropython.asm_thumb # r0 = r0+r1-r2
def add_sub(r0, r1, r2):
vmov(s0, r0)
vcvt_f32_s32(s0, s0)
vmov(s1, r1)
vcvt_f32_s32(s1, s1)
vmov(s2, r2)
vcvt_f32_s32(s2, s2)
vadd(s0, s0, s1)
vsub(s0, s0, s2)
vcvt_s32_f32(s31, s0)
vmov(r0, s31)
print(add_sub(100, 20, 30))

View File

@@ -0,0 +1 @@
90

View File

@@ -0,0 +1,15 @@
@micropython.asm_thumb # test vcmp, vmrs
def f(r0, r1):
vmov(s0, r0)
vcvt_f32_s32(s0, s0)
vmov(s1, r1)
vcvt_f32_s32(s1, s1)
vcmp(s1, s0)
vmrs(r0, FPSCR)
mov(r1, 28)
lsr(r0, r1)
print(f(0, 1))
print(f(1, 1))
print(f(1, 0))

View File

@@ -0,0 +1,3 @@
2
6
8

View File

@@ -0,0 +1,14 @@
import uarray as array
@micropython.asm_thumb # test vldr, vstr
def arrayadd(r0):
vldr(s0, [r0, 0])
vldr(s1, [r0, 4])
vadd(s2, s0, s1)
vstr(s2, [r0, 8])
z = array.array("f", [2, 4, 10])
arrayadd(z)
print(z[2])

View File

@@ -0,0 +1 @@
6.0

View File

@@ -0,0 +1,15 @@
@micropython.asm_thumb # r0 = (int)(r0*r1/r2)
def muldiv(r0, r1, r2):
vmov(s0, r0)
vcvt_f32_s32(s0, s0)
vmov(s1, r1)
vcvt_f32_s32(s1, s1)
vmov(s2, r2)
vcvt_f32_s32(s2, s2)
vmul(s7, s0, s1)
vdiv(s8, s7, s2)
vcvt_s32_f32(s31, s8)
vmov(r0, s31)
print(muldiv(100, 10, 50))

View File

@@ -0,0 +1 @@
20

View File

@@ -0,0 +1,15 @@
# test vsqrt, vneg
@micropython.asm_thumb # r0 = -(int)(sqrt(r0)*r1)
def sqrt_test(r0, r1):
vmov(s1, r0)
vcvt_f32_s32(s1, s1)
vsqrt(s1, s1)
vmov(s2, r1)
vcvt_f32_s32(s2, s2)
vmul(s0, s1, s2)
vneg(s7, s0)
vcvt_s32_f32(s31, s7)
vmov(r0, s31)
print(sqrt_test(256, 10))

View File

@@ -0,0 +1 @@
-160

View File

@@ -0,0 +1,22 @@
# test it instruction
@micropython.asm_thumb
def f(r0, r1):
cmp(r0, r1)
it(eq)
mov(r0, 100)
print(f(0, 0), f(1, 2))
@micropython.asm_thumb
def g(r0, r1):
cmp(r0, r1)
ite(eq)
mov(r0, 100)
mov(r0, 200)
print(g(0, 0), g(0, 1))

View File

@@ -0,0 +1,2 @@
100 1
100 200

View File

@@ -0,0 +1,21 @@
@micropython.asm_thumb
def f(r0, r1, r2):
push({r0})
push({r1, r2})
pop({r0})
pop({r1, r2})
@micropython.asm_thumb
def g():
b(START)
label(SUBROUTINE)
push({lr}) # push return address
mov(r0, 7)
pop({pc}) # return
label(START)
bl(SUBROUTINE)
print(f(0, 1, 2))
print(g())

View File

@@ -0,0 +1,2 @@
1
7

View File

@@ -0,0 +1,33 @@
# test return type of inline asm
@micropython.asm_thumb
def ret_obj(r0) -> object:
pass
ret_obj(print)(1)
@micropython.asm_thumb
def ret_bool(r0) -> bool:
pass
print(ret_bool(0), ret_bool(1))
@micropython.asm_thumb
def ret_int(r0) -> int:
lsl(r0, r0, 29)
print(ret_int(0), hex(ret_int(1)), hex(ret_int(2)), hex(ret_int(4)))
@micropython.asm_thumb
def ret_uint(r0) -> uint:
lsl(r0, r0, 29)
print(ret_uint(0), hex(ret_uint(1)), hex(ret_uint(2)), hex(ret_uint(4)))

View File

@@ -0,0 +1,4 @@
1
False True
0 0x20000000 0x40000000 -0x80000000
0 0x20000000 0x40000000 0x80000000

View File

@@ -0,0 +1,46 @@
@micropython.asm_thumb
def lsl1(r0):
lsl(r0, r0, 1)
print(hex(lsl1(0x123)))
@micropython.asm_thumb
def lsl23(r0):
lsl(r0, r0, 23)
print(hex(lsl23(1)))
@micropython.asm_thumb
def lsr1(r0):
lsr(r0, r0, 1)
print(hex(lsr1(0x123)))
@micropython.asm_thumb
def lsr31(r0):
lsr(r0, r0, 31)
print(hex(lsr31(0x80000000)))
@micropython.asm_thumb
def asr1(r0):
asr(r0, r0, 1)
print(hex(asr1(0x123)))
@micropython.asm_thumb
def asr31(r0):
asr(r0, r0, 31)
print(hex(asr31(0x80000000)))

View File

@@ -0,0 +1,6 @@
0x246
0x800000
0x91
0x1
0x91
-0x1

View File

@@ -0,0 +1,12 @@
@micropython.asm_thumb
def getIPSR():
mrs(r0, IPSR)
@micropython.asm_thumb
def getBASEPRI():
mrs(r0, BASEPRI)
print(getBASEPRI())
print(getIPSR())

View File

@@ -0,0 +1,2 @@
0
0

View File

@@ -0,0 +1,63 @@
@micropython.asm_thumb
def asm_sum_words(r0, r1):
# r0 = len
# r1 = ptr
# r2 = sum
# r3 = dummy
mov(r2, 0)
b(loop_entry)
label(loop1)
ldr(r3, [r1, 0])
add(r2, r2, r3)
add(r1, r1, 4)
sub(r0, r0, 1)
label(loop_entry)
cmp(r0, 0)
bgt(loop1)
mov(r0, r2)
@micropython.asm_thumb
def asm_sum_bytes(r0, r1):
# r0 = len
# r1 = ptr
# r2 = sum
# r3 = dummy
mov(r2, 0)
b(loop_entry)
label(loop1)
ldrb(r3, [r1, 0])
add(r2, r2, r3)
add(r1, r1, 1)
sub(r0, r0, 1)
label(loop_entry)
cmp(r0, 0)
bgt(loop1)
mov(r0, r2)
import uarray as array
b = array.array("l", (100, 200, 300, 400))
n = asm_sum_words(len(b), b)
print(b, n)
b = array.array("b", (10, 20, 30, 40, 50, 60, 70, 80))
n = asm_sum_bytes(len(b), b)
print(b, n)
b = b"\x01\x02\x03\x04"
n = asm_sum_bytes(len(b), b)
print(b, n)

View File

@@ -0,0 +1,3 @@
array('l', [100, 200, 300, 400]) 1000
array('b', [10, 20, 30, 40, 50, 60, 70, 80]) 360
b'\x01\x02\x03\x04' 10