Skip to content

Commit 35f4875

Browse files
committed
Port MicroPython
1 parent 2f32cd5 commit 35f4875

File tree

16 files changed

+397
-19
lines changed

16 files changed

+397
-19
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "riscof/riscv-arch-test"]
55
path = riscof/riscv-arch-test
66
url = https://github.com/riscv-non-isa/riscv-arch-test
7+
[submodule "micropython/micropython"]
8+
path = micropython/micropython
9+
url = https://github.com/micropython/micropython

micropython/compile_flags.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-I./micropython
2+
-I./mlogv32

micropython/micropython

Submodule micropython added at f498a16

micropython/mlogv32/Makefile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Include the core environment definitions; this will set $(TOP).
2+
include ../micropython/py/mkenv.mk
3+
4+
# Include py core make definitions.
5+
include $(TOP)/py/py.mk
6+
include $(TOP)/extmod/extmod.mk
7+
8+
# Define toolchain and other tools.
9+
CROSS_COMPILE ?= riscv32-unknown-elf-
10+
11+
# Set CFLAGS and libraries.
12+
RV32_ARCH ?= rv32ima_zicsr
13+
RV32_ABI = ilp32
14+
15+
AFLAGS += -march=$(RV32_ARCH) -mabi=$(RV32_ABI)
16+
CFLAGS += -march=$(RV32_ARCH) -mabi=$(RV32_ABI) -ffreestanding -I. -I$(BUILD) -I$(TOP)
17+
LDFLAGS += -march=$(RV32_ARCH) -mabi=$(RV32_ABI) -nostartfiles -Tlink.x
18+
19+
LIBS += -lm
20+
21+
ifeq ($(DEBUG), 1)
22+
CFLAGS += -Og
23+
else
24+
CFLAGS += -Os
25+
endif
26+
27+
# Define the required source files.
28+
SRC_C = \
29+
init.c \
30+
main.c \
31+
mphalport.c \
32+
shared/readline/readline.c \
33+
shared/runtime/gchelper_generic.c \
34+
shared/runtime/pyexec.c \
35+
shared/runtime/stdout_helpers.c \
36+
37+
SRC_S = \
38+
entry.s \
39+
40+
# Define source files containing qstrs.
41+
SRC_QSTR += shared/readline/readline.c shared/runtime/pyexec.c
42+
43+
# Define the required object files.
44+
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))
45+
46+
# Define the top-level target, the main firmware.
47+
all: $(BUILD)/firmware.bin $(BUILD)/firmware.elf
48+
49+
# Define how to build the firmware.
50+
$(BUILD)/firmware.elf: $(OBJ)
51+
$(ECHO) "LINK $@"
52+
$(Q)$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
53+
$(Q)$(SIZE) $@
54+
55+
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
56+
$(ECHO) "GEN $@"
57+
$(Q)$(OBJCOPY) --output-target binary $< $@
58+
59+
# Include remaining core make rules.
60+
include $(TOP)/py/mkrules.mk

micropython/mlogv32/entry.s

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.section .text.start
2+
3+
.global _start
4+
_start:
5+
# initialize icache
6+
la t0, __etext
7+
.insn i CUSTOM_0, 0, zero, t0, 0
8+
9+
# set stack pointer and global pointer
10+
la t1, _stack_start
11+
andi sp, t1, -16
12+
add s0, sp, zero
13+
14+
.option push
15+
.option norelax
16+
la gp, __global_pointer$
17+
.option pop
18+
19+
# initialize .data
20+
la t0, __sidata
21+
la t1, __sdata
22+
la t2, __edata
23+
load_data:
24+
bgeu t1, t2, data_done
25+
lb t3, 0(t0)
26+
sb t3, 0(t1)
27+
addi t0, t0, 1
28+
addi t1, t1, 1
29+
j load_data
30+
data_done:
31+
32+
# clear .bss
33+
la t0, __sbss
34+
la t1, __ebss
35+
clear_bss:
36+
bgeu t0, t1, bss_done
37+
sb zero, 0(t0)
38+
addi t0, t0, 1
39+
j clear_bss
40+
bss_done:
41+
42+
# jump to C code
43+
call main
44+
45+
# if main returns, halt the processor
46+
li t0, 0xfffffff0
47+
sw zero, 0(t0)

micropython/mlogv32/init.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "py/mpconfig.h"
2+
#include "py/mphal.h"
3+
#include "uart.h"
4+
5+
void init(void) {
6+
*UART0_FCR = 0b111;
7+
}
8+
9+
void deinit(void) {
10+
mp_hal_stdout_tx_str("Halting.");
11+
}

micropython/mlogv32/init.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void init(void);
2+
void deinit(void);

micropython/mlogv32/link.x

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
ENTRY(_start);
2+
3+
MEMORY {
4+
ROM (rx) : ORIGIN = 0x00000000, LENGTH = 512K
5+
RAM (rwx) : ORIGIN = 0x80000000, LENGTH = 512K
6+
}
7+
8+
_stack_start = ORIGIN(RAM) + LENGTH(RAM);
9+
10+
SECTIONS {
11+
.text : {
12+
__stext = .;
13+
14+
KEEP(*(.text.start));
15+
*(.text.reset);
16+
*(.text .text.*);
17+
18+
. = ALIGN(4);
19+
__etext = .;
20+
} > ROM
21+
22+
.rodata : ALIGN(4) {
23+
. = ALIGN(4);
24+
__srodata = .;
25+
26+
*(.srodata .srodata.*);
27+
*(.rodata .rodata.*);
28+
29+
. = ALIGN(4);
30+
__erodata = .;
31+
} > ROM
32+
33+
.data : ALIGN(4) {
34+
. = ALIGN(4);
35+
__sdata = .;
36+
37+
__global_pointer$ = . + 0x800;
38+
*(.sdata .sdata.* .sdata2 .sdata2.*);
39+
*(.data .data.*);
40+
41+
. = ALIGN(4);
42+
__edata = .;
43+
} > RAM AT > ROM
44+
45+
__sidata = LOADADDR(.data);
46+
47+
.bss (NOLOAD) : ALIGN(4) {
48+
. = ALIGN(4);
49+
__sbss = .;
50+
51+
*(.sbss .sbss.* .bss .bss.*);
52+
53+
. = ALIGN(4);
54+
__ebss = .;
55+
} > RAM
56+
}

micropython/mlogv32/main.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "py/builtin.h"
2+
#include "py/compile.h"
3+
#include "py/gc.h"
4+
#include "py/mperrno.h"
5+
#include "py/stackctrl.h"
6+
#include "shared/runtime/gchelper.h"
7+
#include "shared/runtime/pyexec.h"
8+
9+
// Allocate memory for the MicroPython GC heap.
10+
static char heap[4096];
11+
12+
void main(void) { // NOLINT
13+
// Initialise the MicroPython runtime.
14+
mp_stack_ctrl_init();
15+
gc_init(heap, heap + sizeof(heap));
16+
mp_init();
17+
18+
// Start a normal REPL; will exit when ctrl-D is entered on a blank line.
19+
pyexec_friendly_repl();
20+
21+
// Deinitialise the runtime.
22+
gc_sweep_all();
23+
mp_deinit();
24+
}
25+
26+
// Handle uncaught exceptions (should never be reached in a correct C implementation).
27+
void nlr_jump_fail(void *val) {
28+
for (;;) {
29+
}
30+
}
31+
32+
// Do a garbage collection cycle.
33+
void gc_collect(void) {
34+
gc_collect_start();
35+
gc_helper_collect_regs_and_stack();
36+
gc_collect_end();
37+
}
38+
39+
// There is no filesystem so stat'ing returns nothing.
40+
mp_import_stat_t mp_import_stat(const char *path) {
41+
return MP_IMPORT_STAT_NO_EXIST;
42+
}
43+
44+
// There is no filesystem so opening a file raises an exception.
45+
mp_lexer_t *mp_lexer_new_from_file(qstr filename) {
46+
mp_raise_OSError(MP_ENOENT);
47+
}

micropython/mlogv32/mpconfigport.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdint.h>
2+
#include "init.h"
3+
4+
// Python internal features.
5+
#define MICROPY_ENABLE_GC (1)
6+
#define MICROPY_HELPER_REPL (1)
7+
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
8+
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
9+
10+
#define MICROPY_EMIT_RV32 (1)
11+
#define MICROPY_EMIT_INLINE_RV32 (1)
12+
13+
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES)
14+
15+
// Fine control over Python builtins, classes, modules, etc.
16+
#define MICROPY_PY_ASYNC_AWAIT (0)
17+
#define MICROPY_PY_IO (0)
18+
19+
#define MICROPY_PY_BUILTINS_HELP (1)
20+
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
21+
#define MICROPY_PY_FSTRINGS (1)
22+
#define MICROPY_PY_MATH_CONSTANTS (1)
23+
#define MICROPY_PY_MATH_FACTORIAL (1)
24+
#define MICROPY_PY_PLATFORM (1)
25+
#define MICROPY_PY_RANDOM (1)
26+
27+
#define MICROPY_PY_ASYNCIO (0)
28+
#define MICROPY_PY_IO_IOBASE (0)
29+
#define MICROPY_PY_JSON (0)
30+
#define MICROPY_PY_OS (0)
31+
#define MICROPY_PY_SELECT (0)
32+
#define MICROPY_PY_SELECT_SELECT (0)
33+
#define MICROPY_PY_SYS_PS1_PS2 (0)
34+
#define MICROPY_PY_SYS_STDFILES (0)
35+
#define MICROPY_PY_SYS_STDIO_BUFFER (0)
36+
#define MICROPY_PY_TIME (0)
37+
38+
// Type definitions for the specific machine.
39+
40+
typedef intptr_t mp_int_t; // must be pointer size
41+
typedef uintptr_t mp_uint_t; // must be pointer size
42+
typedef long mp_off_t;
43+
44+
// We need to provide a declaration/definition of alloca().
45+
#include <alloca.h>
46+
47+
// Define the port's name and hardware.
48+
#define MICROPY_HW_BOARD_NAME "mindustry"
49+
#define MICROPY_HW_MCU_NAME "mlogv32"
50+
51+
#define MP_STATE_PORT MP_STATE_VM
52+
53+
#define MICROPY_PORT_INIT_FUNC init()
54+
#define MICROPY_PORT_DEINIT_FUNC deinit()

0 commit comments

Comments
 (0)