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
+37-37Lines changed: 37 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -189,6 +189,43 @@ Run CircuitPython:
189
189
```
190
190
and connect to the console using your favorite terminal program, e.g., `screen /dev/ttys015 115200`.
191
191
192
+
### Using the Python API
193
+
194
+
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:
195
+
```python
196
+
from cpu importCPU
197
+
from ram importRAM
198
+
199
+
ram = RAM(1024)
200
+
cpu = CPU(ram)
201
+
202
+
# Store into RAM a simple program that sums integers from 1 to 100 and returns the result in t0
203
+
ram.store_word(0x00000000, 0x00000293) # li t0, 0
204
+
ram.store_word(0x00000004, 0x00100313) # li t1, 1
205
+
ram.store_word(0x00000008, 0x06400393) # li t2, 100
ram.store_word(0x00000014, 0xfe63dce3) # bge t2, t1, c <loop>
209
+
ram.store_word(0x00000018, 0x00100073) # ebreak
210
+
211
+
# Run the program
212
+
cpu.pc =0x00000000# set initial PC
213
+
whileTrue:
214
+
inst = ram.load_word(cpu.pc) # fetch
215
+
cpu.execute(inst) # decode & execute
216
+
cpu.pc = cpu.next_pc # update PC
217
+
218
+
if cpu.pc ==0x00000018: # when we reach this address, the program has finished
219
+
break
220
+
221
+
print (cpu.registers[5]) # Print result stored in t0/x5
222
+
```
223
+
224
+
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.:
225
+
```
226
+
PYTHONPATH=. python tests/test_python1.py
227
+
```
228
+
192
229
## 🧪 Running Unit Tests
193
230
```
194
231
cd riscv-tests
@@ -260,43 +297,6 @@ Test rv32mi-p-ma_fetch : PASS
260
297
Test rv32mi-p-sbreak : PASS
261
298
```
262
299
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
-
300
300
## Design Goals
301
301
- Simplicity over speed (though it is highly optimized for speed and performs near the limit of what is possible in pure Python)
302
302
- Emphasis on correctness and compliance with RISC-V specifications
0 commit comments