Skip to content

Commit e044950

Browse files
committed
updated README
1 parent cce1c11 commit e044950

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

README.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,43 @@ Run CircuitPython:
189189
```
190190
and connect to the console using your favorite terminal program, e.g., `screen /dev/ttys015 115200`.
191191

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 import CPU
197+
from ram import RAM
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
206+
ram.store_word(0x0000000c, 0x006282b3) # <loop> add t0, t0, t1
207+
ram.store_word(0x00000010, 0x00130313) # addi t1, t1, 1
208+
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+
while True:
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+
192229
## 🧪 Running Unit Tests
193230
```
194231
cd riscv-tests
@@ -260,43 +297,6 @@ Test rv32mi-p-ma_fetch : PASS
260297
Test rv32mi-p-sbreak : PASS
261298
```
262299

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 import CPU
268-
from ram import RAM
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
277-
ram.store_word(0x0000000c, 0x006282b3) # <loop> add t0, t0, t1
278-
ram.store_word(0x00000010, 0x00130313) # addi t1, t1, 1
279-
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-
while True:
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-
300300
## Design Goals
301301
- Simplicity over speed (though it is highly optimized for speed and performs near the limit of what is possible in pure Python)
302302
- Emphasis on correctness and compliance with RISC-V specifications

0 commit comments

Comments
 (0)