• Stars
    star
    1,410
  • Rank 32,600 (Top 0.7 %)
  • Language
    C
  • License
    MIT License
  • Created over 1 year ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny C header-only risc-v emulator.

riscv_emufun (mini-rv32ima)

Click below for the YouTube video introducing this project:

Writing a Really Tiny RISC-V Emulator But Will It Run Doom?

What

mini-rv32ima is a single-file-header, mini-rv32ima.h, in the STB Style library that:

  • Implements a RISC-V rv32ima/Zifencei†+Zicsr (and partial su), with CLINT and MMIO.
  • Is about 400 lines of actual code.
  • Has no dependencies, not even libc.
  • Is easily extensible. So you can easily add CSRs, instructions, MMIO, etc!
  • Is pretty performant. (~450 coremark on my laptop, about 1/2 the speed of QEMU)
  • Is human-readable and in basic C code.
  • Is "incomplete" in that it didn't implement the tons of the spec that Linux doesn't (and you shouldn't) use.
  • Is trivially embeddable in applications.

It has a demo wrapper that:

  • Implements a CLI, SYSCON, UART, DTB and Kernel image loading.
  • And it only around 250 lines of code, itself.
  • Compiles down to a ~18kB executable and only relies on libc.

†: Zifence+RV32A are stubbed. So, tweaks will need to be made if you want to emulate a multiprocessor system with this emulator.

Just see the mini-rv32ima folder.

It's "fully functional" now in that I can run Linux, apps, etc. Compile flat binaries and drop them in an image.

Why

I'm working on a really really simple C Risc-V emulator. So simple it doesn't even have an MMU (Memory Management Unit). I have a few goals, they include:

  • Furthering RV32-NOMMU work to improve Linux support for RV32-NOMMU. (Imagine if we could run Linux on the $1 ESP32-C3)
  • Learning more about RV32 and writing emulators.
  • Being further inspired by @pimaker's amazing work on Running Linux in a Pixel Shader and having the sneaking suspicion performance could be even better!
  • Hoping to port it to some weird places.
  • Understand the most simplistic system you can run Linux on and trying to push that boundary.
  • Continue to include my education of people about assembly language.

How

Windows instructions (Just playing with the image)

WSL (For full toolchain and image build:

  • You will need to remove all spaces from your path i.e. export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/system32:/snap/bin and continue the instructions. P.S. What in the world was Windows thinking, putting a space between "Program" and "Files"??!?

Linux instructions (both):

  • Clone this repo.
  • Install git build-essential and/or whatever other requirements are in place for buildroot.
  • make testdlimage
  • It automatically downloads the image (~1MB) and runs the emulator.
  • Should be up and running in about 2.5s depending on internet speed.

You can do in-depth work on Linux by:

  • make everything

If you want to play with the bare metal system, see below, or if you have the toolchain installed, just:

  • make testbare

If you just want to play emdoom, and use the prebuilt image:

  • On Windows, run windows\winrundoom.ps1
  • On Linux, cd mini-rv32ima, and type make testdoom

Questions?

  • Why not rv64?
    • Because then I can't run it as easily in a pixel shader if I ever hope to.
  • Can I add an MMU?
    • Yes. It actually probably wouldn't be too difficult.
  • Should I add an MMU?
    • No. It is important to further support for nommu systems to empower minimal Risc-V designs!

Everything else: Contact us on my Discord: https://discord.com/invite/CCeyWyZ

Hopeful goals?

  • Further drive down needed features to run Linux.
    • Remove need for RV32A extension on systems with only one CPU.
    • Support for relocatable ELF executables.
    • Add support for an unreal UART. One that's much simpler than the current 8250 driver.
  • Maybe run this in a pixelshader too!
  • Get opensbi working with this.
  • Be able to "embed" rv32 emulators in random projects.
  • Can I use early console to be a full system console?
  • Can I increase the maximum contiguous memory allocatable?

Special Thanks

More details

If you want to build the kernel yourself:

  • make everything
  • About 20 minutes. (Or 4+ hours if you're on Windows Subsytem for Linux 2)
  • And you should be dropped into a Linux busybox shell with some little tools that were compiled here.

Emdoom notes

  • Emdoom building is in the experiments/emdoom folder
  • You MUST build your kernel with MAX_ORDER set to >12 in buildroot/output/build/linux-5.19/include/linux/mmzone.h if you are building your own image.
  • You CAN use the pre-existing image that is described above.
  • On Windows, it will be very slow. Not sure why.

If you want to use bare metal to build your binaries so you don't need buildroot, you can use the rv64 gcc in 32-bit mode built into Ubuntu 20.04 and up.

sudo apt-get install gcc-multilib gcc-riscv64-unknown-elf make

Links

Attic

General notes:

(These things don't currently work)

Building Tests

(This does not work, now)

cd riscv-tests
export CROSS_COMPILE=riscv64-linux-gnu-
export PLATFORM_RISCV_XLEN=32
CC=riscv64-linux-gnu-gcc ./configure
make XLEN=32 RISCV_PREFIX=riscv64-unknown-elf- RISCV_GCC_OPTS="-g -O1 -march=rv32imaf -mabi=ilp32f -I/usr/include"

Building OpenSBI

(This does not currently work!)

cd opensbi
export CROSS_COMPILE=riscv64-unknown-elf-
export PLATFORM_RISCV_XLEN=32
make

Extra links

Using custom build

Where yminpatch is the patch from the mailing list.

rm -rf buildroot
git clone git://git.buildroot.net/buildroot
cd buildroot
git am < ../yminpatch.txt
make qemu_riscv32_nommu_virt_defconfig
make
# Or use our configs.

Note: For emdoom you will need to modify include/linux/mmzone.h and change MAX_ORDER to 13.

Buildroot Notes

Add this: https://github.com/cnlohr/buildroot/pull/1/commits/bc890f74354e7e2f2b1cf7715f6ef334ff6ed1b2

Use this: https://github.com/cnlohr/buildroot/commit/e97714621bfae535d947817e98956b112eb80a75

More Repositories

1

rawdrawandroid

Build android apps without any java, entirely in C and Make
C
2,674
star
2

espusb

Software-only ESP8266 USB Device
C
1,445
star
3

channel3

ESP8266 Analog Broadcast Television Interface
C
1,277
star
4

ch32v003fun

An open source software development stack for the CH32V003, a 10Β’ 48 MHz RISC-V Microcontroller
C
674
star
5

colorchord

Chromatic Sound to Light Conversion System
C
658
star
6

noeuclid

Non-euclidean GPU Raytraced Game
C
641
star
7

esp8266ws2812i2s

ESP8266-based I2S-output WS2812(B) Driver
C
534
star
8

avrcraft

Minecraft server optimized for 8-bit devices.
C
435
star
9

rv003usb

CH32V003 RISC-V Pure Software USB Controller
C
328
star
10

ws2812esp8266

DEPRECATED. Please use esp8266ws2812i2s. No really, stop forking and starring this.
C
298
star
11

esp82xx

Useful ESP8266 C Environment
C
288
star
12

espthernet

ESP8266 10-Base-T Ethernet Driver
C
246
star
13

nosdk8266

Trying to make ESP8266 projects without a big SDK.
C
235
star
14

esp8266rawpackets

Raw Packet Experiments on the ESP8266
JavaScript
148
star
15

fx3fun

Treat your Cypress FX3 a little like a logic analyzer or software defined bus.
C
147
star
16

epaper_projects

C
145
star
17

voxeltastic

View 3D Volume with Translucency in WebGL
JavaScript
143
star
18

shadertrixx

CNLohr's repo for his Unity assets and other shader notes.
ShaderLab
137
star
19

cnovr

CNLohr's OpenVR Misadventures
C
126
star
20

ethertiny

A firmware-only half-duplex 10Base-T implementation for AVRs
PostScript
121
star
21

cnixxi

My experiment with nixie tubes.
C
112
star
22

wiflier

ESP8266-based Quadcopter Brain
KiCad Layout
93
star
23

embeddedDOOM

"emdoom" - a port of DOOM targeted for memory-strapped systems.
C
90
star
24

espwebc3

An in-browser integrated development platform for the ESP32-C3
HTML
84
star
25

esplocalizer

ESP8266 with IMU, Barometer and Battery
C++
83
star
26

esp8266oddclock

What happens when you rip the ESP8266's stable clock source out from under it's feet?
C
81
star
27

esptracker

Lighthouse-based tracker using the TS4231
C
60
star
28

rawdrawandroidexample

Example app using rawdrawandroid as a submodule.
C
59
star
29

wifirxpower

Linux-based WiFi RX Power Grapher
C
59
star
30

pylotron

Embedded VNC-based FPS.
C
58
star
31

esp32s2-cookbook

Low-level tests with the ESP32-S2
C
48
star
32

androidusbtest

Access external devices from Android apps directly in C.
C
46
star
33

superlongexposure

FFMPEG-based tool for non-linear compositing video into long exposures.
C
41
star
34

tplink-raw-wifi

Test using tp-links to do raw wifi.
C
41
star
35

pi_tpi

Using a Raspberry Pi to program an ATTiny10 9 5 or 4
C
39
star
36

what_im_up_to

Just my dumping ground for ideas.
37
star
37

tsopenxr

Single-file header for simple OpenXR Framework use with C
C
36
star
38

t85loudspeaker

ATTiny85 Sound Output
C
35
star
39

avr_vhf

Channel 3 VHF Broadcasting with an AVR
C++
34
star
40

esp8266_dmx_explorer

C
32
star
41

esp8266duplexi2s

Generic duplex communication on the I2S bus in the ESP8266.
C
31
star
42

lamenet

A way of making a really awful network connection.
C
31
star
43

dumbcraft8266

My first stab at a Minecraft-enabled firmware for the ESP8266
C
30
star
44

swadges2017

MAGFest Swag Badges 2017 Firmware
C
29
star
45

cnhardware

C
28
star
46

esp8266_link_test

Test link between an ESP8266 and a base station or another ESP8266 easily.
KiCad Layout
26
star
47

cnballpit-vrc

C#
26
star
48

brokeredupdateandsync

CN's VRC Tools and Prefabs
C#
26
star
49

tinycc-win64-installer

A quick way to use tinyCC on Windows.
NSIS
25
star
50

wificompositer

Various tools I used to collect and composite data from a wifi device into an image.
C
24
star
51

miniosc

Single-file-header OSC library for C (includes platform-independent sockets)
C
21
star
52

esp32-cnlohr-demo

My always-incomplete ESP32 fun zone
C
20
star
53

spreadgine

An OpenGL ES engine with WebGL Output.
C
19
star
54

slapsplat

GLSL
19
star
55

oledscope

Playing around with a 128x128 OLED module
C
19
star
56

cnlohr_social_media_tools

Various social media interface tools.
C
18
star
57

assembly-notes

My repo for a guide to and notes on assembly lagnuage, inling, etc.
18
star
58

x11framegrab

Small static program for taking screenshots of x11 screens and saving/streaming as a variety of formats.
C
18
star
59

usb2812

ATMega32u2 USB driver for WS2812 LEDs.
C
18
star
60

esp8266lighthouse

DO NOT USE THIS PROJECT - WILL BE SUPERCEDED
C
17
star
61

wasm_integrated

An example of how you can fully integrate wasm into a webpage. No need for fetching or any of that jazz.
HTML
17
star
62

swadge-vrchat-bridge

C
16
star
63

magfestbadges

The codebase, schematics, etc. of the MAGFest Badges.
C
16
star
64

cntools

Incomplete-ish C "libraries"
C
16
star
65

wi07clight

Using the wi07c's ESP8266 to control a desk light.
C
16
star
66

avrjslinux

JSLinux Running on an AVR with I/O Port Remapping
JavaScript
14
star
67

tinyispterm

An SPI terminal for USB Tiny ISP compatible AVR prorgammers.
Python
14
star
68

esp32-c3-cntest

C
13
star
69

h264fun

Fun with h264 (mostly encoding)
C
13
star
70

tensigral_lamp

My PCB Tensegrity structure lamp.
C
12
star
71

addressable_leds

Charles's list of addressable LEDs
12
star
72

kelvindmmwifi

Adding some sweet wifis to a kelvin multimeter.
KiCad Layout
11
star
73

attiny85_tb

ATTiny85 Test Bench Tool with Screw Terminals
Eagle
10
star
74

voxeltastic-vrc

Voxel Tracing Shader for VRChat
ShaderLab
10
star
75

esp32s2_dmx512_usb

C
10
star
76

rawdrawwasm

My stab at rawdraw on wasm.
HTML
10
star
77

swadgeguide

My guide for making swadges.
HTML
9
star
78

terriblewordsearch

A C tool for making terrible word searches.
C
9
star
79

cndvr

Assembly
9
star
80

flexcrt

A header for CRTs to allow for scattered writes.
HLSL
8
star
81

swadge2019

the repo for the 2019 swadge
C
8
star
82

donut

MAGFest /is/ a donut.
C
8
star
83

minimal_stm32f407

STM32F407 Minimal self-contained Makefile-based build environment (Plus printf!)
C
8
star
84

esp8266irxmit

ESP8266 IR Transmistter
C
7
star
85

espbadapple

My shot at bad apple on an '8266.
C
7
star
86

minimal_stm32f303

STM32F303 Minimal self-contained Makefile-based build environment (Plus printf!)
C
7
star
87

netlink_without_libnl

Testing using the kernel's netlink, more specifically nl80211 without libnl or genl
C
7
star
88

wxrchat

Distributed WebXR Chat Application
C
6
star
89

esp32_ethernet_test

My Ethernet on the ESP32 Test (incomplete!)
C
6
star
90

tinyisp-micro

My USBTinyISP (with an ATTiny44)
KiCad Layout
6
star
91

esp_network_rgb

JavaScript
6
star
92

charlesvideotools

Linux Video Creation Notes and Tools
C
5
star
93

esp_nonos_sdk

Patchwork of various esp_nonos_sdk subsystems.
C
5
star
94

in99

Gain a superficial of topics in 99 seconds.
5
star
95

shader_perf_grass_comparison

Fake test for checking perf of different shader types.
ShaderLab
5
star
96

bridgesim

Project abandoned. Library code probably has problems.
C
4
star
97

stm32f042_fun

My fun repo for the STM32f042
C
4
star
98

avr_ledprojector

Using an AVR with an mlx90614 to project temperatures!
KiCad Layout
4
star
99

esp8266_32x32panel

An attempt at driving 32x32 panels with an ESP8266.
KiCad Layout
4
star
100

sk6812rgbyspectraldata

Just an email I got from Andrew Sowa
4
star