Skip to content

Commit 920e4c0

Browse files
committed
qemu-arm: Merge RISC-V 32-bit support into qemu-arm port.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 9647f9c commit 920e4c0

31 files changed

+154
-586
lines changed

.github/workflows/ports_qemu-riscv.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

ports/qemu-arm/Makefile

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ QSTR_DEFS = qstrdefsport.h
1414

1515
# MicroPython feature configurations
1616
MICROPY_ROM_TEXT_COMPRESSION ?= 1
17+
18+
ifeq ($(QEMU_ARCH),arm)
1719
FROZEN_MANIFEST ?= "freeze('test-frzmpy')"
20+
endif
21+
ifeq ($(QEMU_ARCH),riscv32)
22+
FROZEN_MANIFEST ?= "freeze('test-frzmpy', ('frozen_const.py', 'frozen_viper.py', 'native_frozen_align.py'))"
23+
endif
1824

1925
# include py core make definitions
2026
include $(TOP)/py/py.mk
2127
include $(TOP)/extmod/extmod.mk
2228

23-
CROSS_COMPILE ?= arm-none-eabi-
24-
2529
QEMU_SYSTEM = qemu-system-$(QEMU_ARCH)
2630
QEMU_ARGS += -machine $(QEMU_MACHINE) -nographic -monitor null -semihosting
2731

@@ -42,6 +46,10 @@ else
4246
COPT += -Os -DNDEBUG
4347
endif
4448

49+
ifeq ($(QEMU_ARCH),arm)
50+
51+
CROSS_COMPILE ?= arm-none-eabi-
52+
4553
## With CoudeSourcery it's actually a little different, you just need `-T generic-m-hosted.ld`.
4654
## Although for some reason `$(LD)` will not find that linker script, it works with `$(CC)`.
4755
## It turns out that this is specific to CoudeSourcery, and ARM version of GCC ships something
@@ -51,16 +59,52 @@ endif
5159
LDFLAGS= -T $(LDSCRIPT) --gc-sections -Map=$(@:.elf=.map)
5260
LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
5361

54-
SRC_C = \
62+
SRC_C += \
63+
mcu/arm/startup.c \
64+
shared/runtime/semihosting_arm.c \
65+
66+
endif
67+
68+
ifeq ($(QEMU_ARCH),riscv32)
69+
70+
#CROSS_COMPILE ?= riscv64-unknown-elf-
71+
CROSS_COMPILE ?= riscv64-elf-
72+
73+
GCC_VERSION = $(word 1, $(subst ., , $(shell $(CC) -dumpversion)))
74+
75+
# If Picolibc is available then select it explicitly. Ubuntu 22.04 ships its
76+
# bare metal RISC-V toolchain with Picolibc rather than Newlib, and the default
77+
# is "nosys" so a value must be provided. To avoid having per-distro
78+
# workarounds, always select Picolibc if available.
79+
PICOLIBC_SPECS = $(shell $(CC) --print-file-name=picolibc.specs)
80+
ifeq ($(PICOLIBC_SPECS),picolibc.specs)
81+
# Picolibc was not found.
82+
SPECS_FRAGMENT =
83+
else
84+
SPECS_FRAGMENT = --specs=$(PICOLIBC_SPECS)
85+
CFLAGS += $(SPECS_FRAGMENT)
86+
endif
87+
88+
LD = $(CC)
89+
90+
LDFLAGS += $(SPECS_FRAGMENT) -Wl,--gc-sections -Wl,-Map=$(@:.elf=.map)
91+
92+
SRC_C += \
93+
mcu/rv32/interrupts.c \
94+
mcu/rv32/startup.c \
95+
96+
QEMU_ARGS += -bios none
97+
98+
endif
99+
100+
SRC_C += \
55101
main.c \
56-
startup.c \
57102
uart.c \
58103
mphalport.c \
59104
shared/libc/string0.c \
60105
shared/readline/readline.c \
61106
shared/runtime/interrupt_char.c \
62107
shared/runtime/pyexec.c \
63-
shared/runtime/semihosting_arm.c \
64108
shared/runtime/stdout_helpers.c \
65109
shared/runtime/sys_stdio_mphal.c \
66110

@@ -86,6 +130,11 @@ repl: $(BUILD)/firmware.elf
86130
run: $(BUILD)/firmware.elf
87131
$(QEMU_SYSTEM) $(QEMU_ARGS) -serial pty -kernel $<
88132

133+
# `make debug` will block QEMU until a debugger is connected to port 1234.
134+
.PHONY: debug
135+
debug: $(BUILD)/firmware.elf
136+
$(QEMU_SYSTEM) $(QEMU_ARGS) -serial mon:stdio -S -s -kernel $<
137+
89138
.PHONY: test
90139
test: $(BUILD)/firmware.elf
91140
$(eval DIRNAME=ports/$(notdir $(CURDIR)))

ports/qemu-arm/README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
MicroPython port to qemu-arm
22
============================
33

4-
This is experimental, community-supported port for Cortex-M emulation as
5-
provided by QEMU (http://qemu.org).
4+
This is experimental, community-supported port for Cortex-M and RISC-V RV32IMC
5+
emulation as provided by QEMU (http://qemu.org).
66

77
The purposes of this port are to enable:
88

@@ -18,6 +18,25 @@ The purposes of this port are to enable:
1818
- no need to use OpenOCD or anything else that might slow down the
1919
process in terms of plugging things together, pressing buttons, etc.
2020

21+
Dependencies
22+
------------
23+
24+
### ARM
25+
26+
For ARM-based boards the build requires a bare-metal ARM toolchain, such as
27+
`arm-none-eabi-gcc`.
28+
29+
### RISC-V
30+
31+
For RISC-V-based boards the build requires a bare metal RISC-V toolchain with GCC 10
32+
or later, either with multilib support or 32 bits specific (M, C, and Zicsr
33+
extensions must be supported, along with ilp32 ABI). Both newlib and picolibc are
34+
supported, with the latter having precedence if found.
35+
36+
Most pre-built toolchains should work out of the box, either coming from your
37+
Linux distribution's package manager, or independently packaged ones like
38+
[xPack](https://xpack.github.io/dev-tools/riscv-none-elf-gcc/).
39+
2140
Build instructions
2241
------------------
2342

ports/qemu-arm/boards/MICROBIT.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CFLAGS += -mthumb -mcpu=cortex-m0 -mfloat-abi=soft
66
CFLAGS += -DQEMU_SOC_NRF51
77
CFLAGS += -DMICROPY_HW_MCU_NAME='"nRF51"'
88

9-
LDSCRIPT = nrf51.ld
9+
LDSCRIPT = mcu/arm/nrf51.ld
1010

1111
SRC_BOARD_O = shared/runtime/gchelper_native.o shared/runtime/gchelper_thumb1.o
1212

ports/qemu-arm/boards/MPS2_AN385.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CFLAGS += -mthumb -mcpu=cortex-m3 -mfloat-abi=soft
55
CFLAGS += -DQEMU_SOC_MPS2
66
CFLAGS += -DMICROPY_HW_MCU_NAME='"Cortex-M3"'
77

8-
LDSCRIPT = mps2.ld
8+
LDSCRIPT = mcu/arm/mps2.ld
99

1010
SRC_BOARD_O = shared/runtime/gchelper_native.o shared/runtime/gchelper_thumb2.o
1111

ports/qemu-arm/boards/NETDUINO2.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CFLAGS += -mthumb -mcpu=cortex-m3 -mfloat-abi=soft
55
CFLAGS += -DQEMU_SOC_STM32
66
CFLAGS += -DMICROPY_HW_MCU_NAME='"STM32"'
77

8-
LDSCRIPT = stm32.ld
8+
LDSCRIPT = mcu/arm/stm32.ld
99

1010
SRC_BOARD_O = shared/runtime/gchelper_native.o shared/runtime/gchelper_thumb2.o
1111

ports/qemu-arm/boards/SABRELITE.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CFLAGS += -DMICROPY_HW_MCU_NAME='"Cortex-A9"'
88
# Cortex-A9 should support unaligned-access, but qemu doesn't seem to.
99
CFLAGS += -mno-unaligned-access
1010

11-
LDSCRIPT = imx6.ld
11+
LDSCRIPT = mcu/arm/imx6.ld
1212

1313
SRC_BOARD_O = shared/runtime/gchelper_generic.o
1414

ports/qemu-arm/boards/VIRT_RV32.mk

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
QEMU_ARCH = riscv32
2+
QEMU_MACHINE = virt
3+
4+
ABI = ilp32
5+
# GCC 10 and lower do not recognise the Zicsr extension in the
6+
# architecture name. "Make" unfortunately does not provide any simple way
7+
# to perform numeric comparisons, so to keep things simple we assume that
8+
# GCC is at least version 10 for the time being.
9+
ifeq ($(GCC_VERSION),10)
10+
ARCH ?= rv32imac
11+
else
12+
# Recent GCC versions explicitly require to declare extensions.
13+
ARCH ?= rv32imac_zicsr
14+
ARCH_LD ?= rv32imac_zicsr
15+
endif
16+
17+
AFLAGS += -mabi=$(ABI) -march=$(ARCH)
18+
CFLAGS += $(AFLAGS)
19+
CFLAGS += -DQEMU_SOC_VIRT
20+
CFLAGS += -DMICROPY_HW_MCU_NAME='"$(ARCH)"'
21+
22+
ARCH_LD ?= $(ARCH)
23+
LDSCRIPT = mcu/rv32/virt.ld
24+
LDFLAGS += -mabi=$(ABI) -march=$(ARCH_LD) -T $(LDSCRIPT) -Wl,-EL
25+
26+
SRC_BOARD_O += shared/runtime/gchelper_native.o shared/runtime/gchelper_rv32i.o
27+
SRC_BOARD_O += rv/entrypoint.o
28+
29+
MPY_CROSS_FLAGS += -march=rv32imc
30+
31+
# These Thumb tests don't run on RV32.
32+
TESTS_EXCLUDE = --exclude 'inlineasm|qemu-arm/asm_test'
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy