How harris170/fake-microcontroller-emulator works
Project Type and Purpose
• This is a C++-based microcontroller emulator (STM32) with a small C stub CLI (“nob”) that automates building and running your firmware in simulation.
• You write firmware (e.g. blinking_task) against a provided MCU API; the emulator simulates CPU, memory, peripherals (GPIO), and high-level components (LED).
File Structure
• Top-level “nob.c/.h”: CLI stub that invokes g++ to compile the emulator + firmware.
• include/: Public headers defining the emulator’s API
• microcontroller/… (memory, GPIO, MCU lifecycle)
• components/… (LED wrapper)
• source/: Emulator implementation files (main.cpp, microcontroller.cpp, gpio.cpp, component.cpp)
• firmware/: Example firmware (declares and implements blinking_task)
• examples/: Additional firmware snippets for testing (e.g. led_shift.hpp)
• Build output placed under build/ by nob.exe.
Key README Insights
• Build flow:
1. gcc -o nob nob.c → produces nob.exe
2. ./nob.exe → invokes g++ on emulator + firmware, outputs fake_microcontroller_emulator.exe in build/
• Runtime API:
• mcu::flash_program(entry_fn)
• mcu::boot_system()
• mcu::start_execution()
• Firmware instantiates peripherals (e.g. LED status_led) which auto-wire to GPIO pins.
Main Components
a. Build Layer
• “nob.exe” CLI (C)
• Build System (g++) → generates emulator binary + linked firmware
b. Emulator Engine
• MCU Core (lifecycle management)
• Memory Model (flash, registers)
• GPIO Subsystem (CRH, ODR registers)
• Component Layer (LED class)
c. Firmware Module
• firmware.cpp/.hpp (e.g. blinking_task)
• Example code under examples/
d. Console Output (simulated peripheral I/O)
Relationships & Interactions
• Compile-time:
nob.exe → invokes g++ → links emulator engine + firmware → fake_microcontroller_emulator.exe
• Run-time:
1. Emulator starts → calls flash_program(blinking_task) → writes entry into flash model
2. boot_system() sets up registers and initial state
3. start_execution() enters firmware loop
4. Firmware calls GPIOA->ODR ^= …; Component layer intercepts and prints [Status LED] PIN13 -> ON/OFF
5. Sleep emulated via std::this_thread::sleep_for
Architectural Patterns & Principles
• Modular layering: Build stub / core emulator / firmware
• Dependency inversion: Firmware code depends only on abstract headers (include/)
• Registration pattern: Components auto-register with low-level peripheral models
• Clear separation of compile-time vs. simulation-time responsibilities