You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ This is a simple and readable **RISC-V RV32I emulator** written in pure Python.
14
14
-**Passes all `rv32ui` and `rv32mi` unit tests** provided by [RISC-V International](https://github.com/riscv-software-src/riscv-tests)
15
15
-**Supports logging** of register values, function calls, system calls, traps, invalid memory accesses, and violations of invariants
16
16
- Runs [MicroPython](https://micropython.org/), [CircuitPython](https://circuitpython.org/) with emulated peripherals, and [FreeRTOS](https://www.freertos.org/) with preemptive multitasking
17
-
- Self-contained, modular, extensible codebase
17
+
- Self-contained, modular, extensible codebase. Provides a **Python API** enabling users to control execution, inspect state, and script complex tests directly in Python.
18
18
19
19
## 🔧 Requirements
20
20
@@ -47,6 +47,7 @@ pip install -r requirements.txt
47
47
├── tests/test_bare*.c # Example C programs without Newlib support
48
48
├── tests/test_newlib*.c # Example C programs with Newlib-nano support
49
49
├── tests/test_peripheral*.c # Example C programs using emulated peripherals
50
+
├── tests/test_api*.py # Examples of programmatic control of the emulator in Python
50
51
├── build/ # Executable and binaries
51
52
├── prebuilt/ # Pre-built examples
52
53
├── run_unit_tests.py # Runs RISC-V unit tests (RV32UI and RV32MI)
@@ -259,6 +260,43 @@ Test rv32mi-p-ma_fetch : PASS
259
260
Test rv32mi-p-sbreak : PASS
260
261
```
261
262
263
+
### Using the Python API
264
+
265
+
The emulator provides a Python API that allows users to control execution, set and inspect state, and run complex tests directly from Python programs. Here is an example of how you can load and run a simple program:
266
+
```python
267
+
from cpu importCPU
268
+
from ram importRAM
269
+
270
+
ram = RAM(1024)
271
+
cpu = CPU(ram)
272
+
273
+
# Store into RAM a simple program that sums integers from 1 to 100 and returns the result in t0
274
+
ram.store_word(0x00000000, 0x00000293) # li t0, 0
275
+
ram.store_word(0x00000004, 0x00100313) # li t1, 1
276
+
ram.store_word(0x00000008, 0x06400393) # li t2, 100
ram.store_word(0x00000014, 0xfe63dce3) # bge t2, t1, c <loop>
280
+
ram.store_word(0x00000018, 0x00100073) # ebreak
281
+
282
+
# Run the program
283
+
cpu.pc =0x00000000# set initial PC
284
+
whileTrue:
285
+
inst = ram.load_word(cpu.pc) # fetch
286
+
cpu.execute(inst) # decode & execute
287
+
cpu.pc = cpu.next_pc # update PC
288
+
289
+
if cpu.pc ==0x00000018: # when we reach this address, the program has finished
290
+
break
291
+
292
+
print (cpu.registers[5]) # Print result stored in t0/x5
293
+
```
294
+
295
+
Example Python programs using programmatic access to the emulator are provided in the `tests` directory. Run them from the top-level directory of the emulator, e.g.:
296
+
```
297
+
PYTHONPATH=. python tests/test_python1.py
298
+
```
299
+
262
300
## Design Goals
263
301
- Simplicity over speed (though it is highly optimized for speed and performs near the limit of what is possible in pure Python)
264
302
- Emphasis on correctness and compliance with RISC-V specifications
Copy file name to clipboardExpand all lines: tests/README.md
+6-2Lines changed: 6 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,10 @@
30
30
31
31
-`test_newlib13.c`: Test using `setjump`/`longjump` C exception handling.
32
32
33
-
-`test_peripheral_uart.c`: Test the memory-mapped UART implementation backed by a pseudo-terminal on the host. Run this example with the `--uart` option, and then connect to the indicated PTY using your preferred terminal program, e.g., `screen /dev/ttys015 115200`.
33
+
-`test_peripheral_uart.c`: Tests the memory-mapped UART implementation backed by a pseudo-terminal on the host. Run this example with the `--uart` option, and then connect to the indicated PTY using your preferred terminal program, e.g., `screen /dev/ttys015 115200`.
34
34
35
-
-`test_peripheral_blkdev.c`: Test the memory-mapped block device implementation backed by a file on the host. Run this example with the `--blkdev=image` option, where `image` is the filename you want to use. If the file does not exist, it will be created by the emulator.
35
+
-`test_peripheral_blkdev.c`: Tests the memory-mapped block device implementation backed by a file on the host. Run this example with the `--blkdev=image` option, where `image` is the filename you want to use. If the file does not exist, it will be created by the emulator.
36
+
37
+
-`test_api1.py`: Python API example: loads and executes a simple program.
38
+
39
+
-`test_api2.py`: Python API example: loads a flat binary executable into RAM, runs it, intercepts a trap.
0 commit comments