Arduino UNO/atmega328p 端口上的 mini-rv32ima 上的 Linux。让我们在世界上最差的 Linux PC 中运行 Linux。
Arduino UNO/atmega328p 端口上的 mini-rv32ima 上的 Linux。让我们在世界上最差的 Linux PC 中运行 Linux。
[Tested successfully!] Atmega328p port of mini-rv32ima. Let's run Linux on the world's worst one of the world's worst Linux PC (and beat Dmitry Grinberg)
Note: The code is in pure AVR C. Arduino IDE is just used as serial terminal.
UPDATE: I found a worse Linux PC: C64!
This is a port of mini-rv32ima (a minimum RISC-V emulator, capable of booting Linux) on atmega328p (the core of Arduino UNO, a 8-bit AVR microcontroller). So basically, this code is for booting Linux on Arduino UNO.
Yes you are reading it correctly, Arduino UNO can (theorically, but not practically) boot Linux. And it definitely beats Dmitry Grinberg's (once) world's worst Linux PC.
The idea is really simple: you have an Arduino UNO (or atmega328p) to run the emulator's logic, and emulator's RAM is accessed via swapping with an SD card (which is communicated through SPI interface, see more below). The emulator also has 3 512-bytes cache (1 icache and 2 dcache interchangable) and lazy/delayed cache write system.
The code is written in pure C (and not Arduino) to reduce Arduino overhead (if any). It initializes UART, SPI, SD card, and a digital input-pullup pin for triggering emulator state dump. Finally, it initialize cache, then mini-rv32ima and let the emulator does its works.
About 175Hz - 205Hz 426 - 600Hz most of the time 700 Hz 1500Hz, peak 1500Hz 2000Hz, lowest 32Hz with -O3 code on an Arduino UNO based on atmega328p, clocked at 16MHz, with a class 4 SDHC card connected via 1-bit SPI interface. Complete boot time (from start to shell) is about 15 hours and 44 minutes 8 hours and 18 minutes.
Update 24/9/2023: The speed is double/tripled by implementing icache
Update 26/9/2023: The speed is x1.5 by implementing 3 cache + lazy write system
Update 25/10/2023: SPI speed is now 4MHz, thanks to @kittennbfive and @mjungwirth's suggestions
Why it's that slow? Read Current issues and drawbacks section below.
The pinout is really simple. On Arduino UNO, it should be:
| Arduino UNO pin | Connect to | Description |
|---|---|---|
| 9 | ground-connected button | When connected to GND, emulator state and effective emulated speed will be dump via UART |
| 10 | CS pin on SD card |
SD's chip select/CS/EN/SS pin |
| 11 | MOSI pin on SD card |
SD's DI/CMD pin |
| 12 | MISO pin on SD card |
SD's DO pin |
| 13 | SCLK pin on SD card |
SD's clock/SCK/CLK pin |
[!WARNING] Since Arduino UNO (atmega328p) uses 5V logic level, while SD card use 3.3V (or lower) logic level, you will need a level shifter. You can build your own using MOSFET, but the simplest way is to buy an SD card adapter.
[!NOTE]
You can change theCSand the pin used to dump state to any pins you want. Just modify the code (see below).
[!WARNING] You should backup all your files, since doing this SD card preparation will destroy your SD card's filesystem.
dd if=file.bin of=/dev/sdX conv=notrunc.arv32.bin file. FYI, this file is generated by dumping mini-rv32ima emulator's RAM array to a file when it just finished loading the kernel and stuffs (but has not boot). You can generate your own version for newer updates of Linux kernel or Buildroot.[!IMPORTANT]
ThePreparing the SD cardsection must be repeated every time you boot your emulator. Otherwise it might not boot (if Linux has initialized enough and start cleaning the memory). This might be fixed in the future.
You can just download the latest HEX file in Release. But if you want to build on your own, here are the steps:
git clone https://github.com/raspiduino/arv32-opt. Then cd arv32-opt…main.hexavrdude -v -V -patmega328p -carduino "-PCOM10" -b115200 -D "-Uflash:w:main.hex:i". Replace COM10 with your COM port (on Windows), or /dev/ttyUSB* port on Linux. (Idk about Mac, but it should be the same)[!IMPORTANT]
If you build for another AVR microcontroller, or if you use different clock speed, please change the-mmcu=atmega328pand-DF_CPU=16000000ULoptions to match your situation.
[!NOTE]
Windows users can refer to the scriptbuild.batin this repo. Just change the path to your correct path and it will work.
After completing 2 sections above, you are now ready to boot Linux on Arduino UNO. Just connect the SD card to Arduino UNO, insert the card in, then power the Arduino on. Open a serial connection at baudrate 9600 bps to connect.
You should see the following output (immidiately):
arv32-opt: mini-rv32ima on Arduino UNO
SD card initialized successfully![!IMPORTANT]
ThePreparing the SD cardsection must be repeated every time you boot your emulator. Otherwise it might not boot (if Linux has initialized enough and start cleaning the memory). This might be fixed in the future.
[!IMPORTANT]
If you getError initializaing SD cardwhen you just plugged your board to your computer, just press the reset button. It should work.
You can also dump the state of the emulator while it's running. Just connect a button to GND and then connect pin 9 to the button. When you click the button, state will be dump, and you should see something like this:
…Effective emulated speed is the number of instructions the emulator can execute in 1 second at that time. dtime is the time difference between current time and the last time you dump the status. dcycle is the number of cycle (instructions) excuted from the last time you dump the status until now.
Registers x0 - x31 sections show registers from x0 to x31, listed in order from left to right then from top down.
[!NOTE]
The emulator will resume when you release the button (you should see something likeB1 is set to HIGH, emulator resume). As long as you keep holding the button, the emulator will pause. You can also use a wire connected to GND instead of a button (just like me).
main.cmain.cmain.cspi.hPreparing the SD card section.Hackaday, Hackster, Habr, Maker News, internetua, futuranet.it, zhihu.com, tuxmachines.org, arduino.cc forum, sohu.com
If you can run this, you probably are running one of the world's worst Linux PC. Enjoy!
暂无开放 Issues,或尚未同步最近议题。