micropython/micropython: MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems via kwout
MicroPythonは一つの題材なのだけれども、いろんなアプリケーションを移植してRISC-Vで動作させれば、少しはアプリケーションのことが分かるようなるかもしれない。
MicroPythonを題材にして、RISC-Vのソフトウェア実行環境とか、そのあたりについて勉強していこう。
MicroPython のアーキテクチャ移植における変更点
アーキテクチャの変更点においては、 py/nlr[ARCH].c
にまとめてある。ここに今回はpy/nlrriscv32.c
を作成した。
また、コンパイル時にターゲットアーキテクチャを決めるためのGCCのdefine文として、__riscv
を使用した。これらのターゲットの定義defineは、以下で調査することが出来る。
riscv32-unknown-elf-cpp -dM /dev/null | sort ... #define __WINT_TYPE__ unsigned int #define __WINT_WIDTH__ 32 #define __has_include(STR) __has_include__(STR) #define __has_include_next(STR) __has_include_next__(STR) #define __riscv 1 #define __riscv_atomic 1 #define __riscv_cmodel_medlow 1 #define __riscv_div 1 #define __riscv_float_abi_soft 1 #define __riscv_mul 1 #define __riscv_muldiv 1 #define __riscv_xlen 32
- py/nlrriscv32.c (抜粋)
#include "py/mpstate.h" #if !MICROPY_NLR_SETJMP && defined(__riscv) #undef nlr_push unsigned int nlr_push(nlr_buf_t *nlr) { __asm volatile ( "sw x2, 8 (x10) \n" // save regs... "sw x8, 12(x10) \n" "sw x9, 16(x10) \n" ...
とりあえず、 nlr_push()
と nlr_jump()
の2つをいろいろ調べて移植してみた。が、まだちゃんと動作していない。
make clean && make spike pk build/firmware.elf z 00000000 ra 00026664 sp 7fbe5d20 gp 000601a8 tp 00000000 t0 000451f8 t1 00040000 t2 00000000 s0 0005dbd0 s1 00000000 a0 00000001 a1 00000000 a2 00000000 a3 00000000 a4 00060648 a5 7fbe5d30 a6 0000000f a7 7fbe5c64 s2 00000000 s3 00000000 s4 00000000 s5 00000000 s6 00000000 s7 00000000 s8 00000000 s9 00000000 sA 00000000 sB 00000000 t3 00000000 t4 00000000 t5 00000000 t6 00000000 pc 00026664 va 00000000 insn 00000000 sr 80046020 An illegal instruction was executed!