C on the board – line by line in the debugger

One program for every board, F9: compiled, loaded and debugged in its source.

Asmonite compiles C with its own compiler cBelle, a subset of C without floating point. It writes the machine code itself, without an assembler and without a linker, for Windows and for the boards of three architectures: Xtensa (ESP32, ESP32-S3), RISC-V (ESP32-C3, ESP32-S31) and Arm Cortex-M (Raspberry Pi Pico, Pico 2). A program without a line #pragma target runs on each of these boards. F9 starts it there, and it can be debugged over the USB cable: breakpoints, one step per line, the variables under the mouse.

cBelle is in the ZIP: cbelle.exe next to Asmonite.exe, its library in cBelle\lib, the examples in examples. The license of Asmonite applies: free, and programs written with it belong to their authors. Before it came aBelle, a small language of its own with the look of Python.

Two first programs

On the left a program for every board: it takes the functions of the Asmonite console from console.c and blinks the LED. On the right one only for the ESP32: the registers of the chip are an array at their address, and a function of the ROM gets its address with #pragma at.

Every board
#include "console.c"

enum { LED_PIN = 2 };          // the LED of the board

void wait_ms(int n) {
    int t = millis();
    while (millis() - t < n) { }
}

void main() {
    println("blink");
    gpio_output(LED_PIN);
    while (1) {
        gpio_write(LED_PIN, 1);
        wait_ms(500);
        gpio_write(LED_PIN, 0);
        wait_ms(500);
    }
}
ESP32
#pragma target esp32

unsigned *gpio = (unsigned *)0x3FF44000;
unsigned *iomux = (unsigned *)0x3FF49000;
#pragma at 0x40008534
void ets_delay_us(int us);       // in the ROM

void main() {
    iomux[0x40 / 4] = 0x2800;    // GPIO 2 is a GPIO
    gpio[0x538 / 4] = 0x100;
    gpio[0x24 / 4] = 1 << 2;     // an output
    while (1) {
        gpio[0x08 / 4] = 1 << 2; // on
        ets_delay_us(500000);
        gpio[0x0C / 4] = 1 << 2; // off
        ets_delay_us(500000);
    }
}

The files are called .c, and an ordinary C compiler understands them too: it skips the lines #pragma. Without a line #pragma target Asmonite compiles a program for the board of Project > Target, that is the board in the tab Serial; #include "console.c" then takes the file of that board from cBelle\lib.

The language at a glance

cBelle is small on purpose: as much C as a compiler needs to translate itself, and the registers of a board. What is there is ordinary C; some of it is stricter. All of it in detail is in cBelle\README.md (in German).

WhatHow
Typesint, unsigned, char *, int *, unsigned *; void for functions without a value. int and unsigned mix only with a cast
Globalint x = 5;, char text[80];, unsigned u[4];, enum { A = 1, B };, a pointer to registers: unsigned *sio = (unsigned *)0xD0000000;
Statementsblocks, if / else, while, for, break, continue, return, local variables, =, += and the others, ++, --
Expressionsthe operators of C for whole numbers (|| && | ^ & == < << + * / % …), casts, calls, index, numbers decimal and hex, 'c', "text"
Boards#pragma target esp32 (or esp32s3, esp32c3, esp32s31, pico, pico2); #pragma at 0x40008534 above a function without a body puts it at that address
Windowsa function without a body comes from kernel32.dll, with #pragma import msvcrt.dll cdecl from another DLL, also printf with ...
Several files#include "net.c" puts in the text of a file, each file once

One compiler for all targets

cbelle.exe is one program with four back ends: x86, Xtensa, Thumb and RISC-V. F9 calls it and loads what it writes.

WindowsESP32, ESP32-S3Pico, Pico 2ESP32-C3, ESP32-S31
Codex86, a console program (PE)Xtensa LX6 / LX7: code for the IRAM, data for the DRAMThumb for the RAM from 0x20000000RISC-V (RV32IM) for the RAM of the programs
F9compiles and starts in the debuggerloads into the RAM over the console on the board, without a reset; breakpoints over the USB cableloads into the RAM over the console; breakpoints over the USB cableloads over the USB JTAG of the chip, the console starts it; afterwards it is at its prompt again

A program can also stay on the board as a file: Project > Compile and Upload puts it into the flash, the Asmonite console starts it by its name as a command (blink, ping example.com), Autostart in the Board Manager after every reset. When main ends, the console is back.

The library: console, WiFi, the network

What a program includes is in cBelle\lib, written in C itself: it can be read, changed and followed in the debugger. The files of the same name are there for every board; the same functions do the same on every board.

FileWhat it brings
console.cthe functions of the console: text (print, println, print_int …), keys, millis, the files in the flash (file_read, file_write), args, exit, the pins (gpio_output, gpio_write); on Windows the same over the console of Windows
wifi.cthe WiFi of the board: the network the console knows (wlan set), sending and receiving frames
net.cARP, IPv4, ICMP (ping), UDP, DHCP, DNS and mDNS
tcp.cTCP, one connection at a time, as a client or a server
tls.cTLS 1.3 (ChaCha20-Poly1305, X25519, SHA-256) for httpsget
cpuclock.cthe clock of the core: cpu_clock, cpu_clock_khz

When the debugger holds a program at a breakpoint, the WiFi connection stays up, and what comes in waits in a buffer. So a DHCP exchange or a TCP connection can be followed line by line. The network is set once per board with wlan set in the console; F9 writes the WiFi package by itself when a program needs it.

Debugging in the source

Next to the program the compiler writes a map file: which code belongs to which line and where the variables are. With it Asmonite debugs right in the .c file, on Windows as on the board.

A compiler that translates itself

cBelle is written in exactly the C it translates: one file, cbelle.c. It translates itself into the same cbelle.exe, byte for byte.

It works in one pass per function: read, split into tokens and write code at once, without a syntax tree and without an optimizer. An expression leaves its value in a register, so every line stays easy to follow in the debugger. At every test run of Asmonite it translates itself and its test programs; the same programs print the same lines on Windows and on the boards. It grew out of the compiler of aBelle.

Examples

Every boardWith WiFi
hello.c – Hello, world!scan, dhcp, dns, ping, ntp, arpscan, wol, udpecho
blink.c – the LED blinkshttpget, httpsget – fetch a web page, also over TLS 1.3
runs.c – a number that stays in a filewebserver – the board as a web server
args.c – the arguments of a programpingled – the LED flashes at each ping
clockspeed.c, setclock.c – the clock of the core (per board)

The examples are in the folder examples of the ZIP: examples\common for every board, examples\esp32, examples\pico and so on for one. All examples with a description →

What is not (yet) there