From 6b2063c6abd1b42fe51a711c32c891ead15f502a Mon Sep 17 00:00:00 2001 From: Toby Jaffey Date: Thu, 11 Dec 2025 11:53:23 +0000 Subject: [PATCH] Build arduino code (for AVR) and allow test in qemu --- Dockerfile | 14 ++++++++++++-- README.md | 4 ++-- host-arduino/Makefile | 12 +++++++++--- host-arduino/config.h | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 78f15cd..7b0d8d2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,11 @@ ENV LANG C.UTF-8 ENV LC_ALL C.UTF-8 RUN apt-get -y update +# tzdata +ARG DEBIAN_FRONTEND=noninteractive +ENV TZ=Etc/UTC +RUN apt-get install -y tzdata + # for unity test RUN apt-get install -y ruby xxd @@ -13,8 +18,8 @@ RUN apt-get install -y gcc-riscv64-unknown-elf build-essential RUN apt-get install -y curl RUN curl https://raw.githubusercontent.com/tristanisham/zvm/master/install.sh | bash ENV ZVM_INSTALL /root/.zvm/self -ENV PATH="$PATH:/root/.zvm/bin:/root/.zvm/self" -ENV export PATH="$/.zvm/bin" +ENV PATH "$PATH:/root/.zvm/bin:/root/.zvm/self" +ENV PATH "$PATH:/root/.zvm/bin" RUN zvm i 0.15.2 # rust @@ -22,3 +27,8 @@ RUN apt-get install -y rustup libclang1 RUN rustup default stable RUN rustup target add riscv32im-unknown-none-elf RUN apt-get install -y libclang1 + +# arduino +RUN curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh +RUN arduino-cli core install arduino:avr +RUN apt-get install -y qemu-system-misc diff --git a/README.md b/README.md index 1d9b777..a01812a 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ int main(int argc, char *argv[]) { * [host](host) vm host which loads a binary and runs to completion, handling multiple syscall types * [host-mini](host-mini) minimal vm host (shown above), with baked in bytecode * [host-parallel](host-parallel) parallel vm host running multiple vm instances concurrently, with baked in bytecode - * [host-arduino](host-arduino) vm host as Arduino sketch (tested on Arduino Uno ATmega328P, uses 9950 bytes of flash/1254 bytes RAM) + * [host-arduino](host-arduino) vm host as Arduino sketch (`make test` to run AVR code in qemu) * [apps/helloworld](apps/helloworld) C hello world program * [apps/conio](apps/conio) C console IO demo * [apps/lissajous](apps/lissajous) C console lissajous curve (showing softfp, floating point) @@ -112,7 +112,7 @@ Then, from inside the docker shell ## Native build -The example VM hosts should all build with any C compiler. To build all of the examples in `apps`, you will need a RISC-V cross compiler, Zig 0.15.2 and Rust (stable). +The example VM hosts should all build with any C compiler. To build all of the examples in `apps`, you will need a RISC-V cross compiler, Zig 0.15.2 and Rust (stable). To build the example `host-arduino` you will need `arduino-cli`. ## Quickstart API diff --git a/host-arduino/Makefile b/host-arduino/Makefile index ab12a93..58804ad 100644 --- a/host-arduino/Makefile +++ b/host-arduino/Makefile @@ -1,8 +1,10 @@ -# The purpose of this Makefile is to auto-generate the Arduino example +# Auto-generate the Arduino example # Arduino cannot set -DUVM32_MEMORY_SIZE, so add a config file # Arduino expects .cpp files -make: +# make test, runs in qemu + +all: echo '#include "config.h"' > uvm32.cpp cat ../uvm32/uvm32.c >> uvm32.cpp cp ../uvm32/uvm32.h uvm32.h @@ -10,6 +12,10 @@ make: cp ../common/uvm32_common_custom.h uvm32_common_custom.h cp ../common/uvm32_sys.h uvm32_sys.h xxd -i ../precompiled/mandel.bin | sed -e "s/unsigned char/const unsigned char/" > mandel.h + arduino-cli compile -b arduino:avr:uno -e + +test: all + qemu-system-avr -machine uno -bios build/arduino.avr.uno/host-arduino.ino.elf -nographic -serial mon:stdio clean: - rm -f uvm32.cpp uvm32.h mini-rv32ima.h uvm32_common_custom.h uvm32_sys.h mandel.h + rm -rf uvm32.cpp uvm32.h mini-rv32ima.h uvm32_common_custom.h uvm32_sys.h mandel.h build diff --git a/host-arduino/config.h b/host-arduino/config.h index 5952a70..53db8d9 100644 --- a/host-arduino/config.h +++ b/host-arduino/config.h @@ -1,2 +1,2 @@ // Arduino cannot do -DUVM32_MEMORY_SIZE, so set this explicitly -#define UVM32_MEMORY_SIZE 1024 +#define UVM32_MEMORY_SIZE 512