Skip to content

Commit 815947e

Browse files
authored
Merge pull request #19 from sql-hkr/docs/cli
Add CLI Visualizer documentation and usage examples (#17)
2 parents e513422 + 96efab3 commit 815947e

2 files changed

Lines changed: 100 additions & 36 deletions

File tree

README.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ Key points:
8484
- For higher-fidelity animations (GIFs) and interactive matplotlib views, use the `Visualizer` class which requires `matplotlib`.
8585

8686
Interactive controls:
87-
```
87+
88+
```text
8889
Space - toggle play/pause
8990
l or > - next step
9091
h or < - previous step
@@ -101,7 +102,7 @@ You can invoke the terminal visualizer directly from Python after running the CP
101102

102103
```python
103104
from tiny8 import CPU, assemble_file
104-
from tiny8.cli_visualizer import run_cli
105+
from tiny8 import run_cli
105106

106107
prog, labels = assemble_file("examples/bubblesort.asm")
107108
cpu = CPU()
@@ -116,10 +117,10 @@ Tiny8 provides a `tiny8` console script (see `pyproject.toml`). You can run the
116117

117118
```bash
118119
# Run the curses-based CLI visualizer for an assembly file
119-
tiny8 examples/bubblesort.asm # --mode cli --mem-start 100 --mem-end 131
120+
uv run tiny8 examples/bubblesort.asm # --mode cli --mem-start 100 --mem-end 131
120121

121122
# Produce an animated GIF using matplotlib (requires matplotlib)
122-
tiny8 examples/bubblesort.asm --mode ani -o bubblesort.gif --mem-start 100 --mem-end 131 --plot-every 100 --fps 60
123+
uv run tiny8 examples/bubblesort.asm --mode ani -o bubblesort.gif --mem-start 100 --mem-end 131 --plot-every 100 --fps 60
123124
```
124125

125126
> [!IMPORTANT]
@@ -238,48 +239,48 @@ Example Output:
238239
Below is a concise, categorized summary of the Tiny8 instruction set (mnemonics are case-insensitive). This is a quick reference — for implementation details see `src/tiny8/cpu.py`.
239240

240241
- Data transfer
241-
- LDI Rd, K — load immediate into register
242-
- MOV Rd, Rr — copy register
243-
- LD Rd, Rr_addr — load from RAM at address in register
244-
- ST Rr_addr, Rr — store register into RAM at address in register
245-
- IN Rd, port — read byte from RAM/IO into register
246-
- OUT port, Rr — write register to RAM/IO
247-
- PUSH Rr / POP Rd — stack push/pop
242+
- LDI Rd, K — load immediate into register
243+
- MOV Rd, Rr — copy register
244+
- LD Rd, Rr_addr — load from RAM at address in register
245+
- ST Rr_addr, Rr — store register into RAM at address in register
246+
- IN Rd, port — read byte from RAM/IO into register
247+
- OUT port, Rr — write register to RAM/IO
248+
- PUSH Rr / POP Rd — stack push/pop
248249

249250
- Arithmetic
250-
- ADD Rd, Rr — add registers
251-
- ADC Rd, Rr — add with carry
252-
- SUB Rd, Rr / SUBI Rd, K — subtraction
253-
- SBC Rd, Rr / SBCI Rd, K — subtract with carry/borrow
254-
- INC Rd / DEC Rd — increment / decrement
255-
- MUL Rd, Rr — 8x8 -> 16 multiply (low/high in Rd/Rd+1)
256-
- DIV Rd, Rr — unsigned divide (quotient->Rd, remainder->Rd+1)
257-
- NEG Rd — two's complement negate
258-
- CLR Rd / SER Rd — clear or set register to all ones
251+
- ADD Rd, Rr — add registers
252+
- ADC Rd, Rr — add with carry
253+
- SUB Rd, Rr / SUBI Rd, K — subtraction
254+
- SBC Rd, Rr / SBCI Rd, K — subtract with carry/borrow
255+
- INC Rd / DEC Rd — increment / decrement
256+
- MUL Rd, Rr — 8x8 -> 16 multiply (low/high in Rd/Rd+1)
257+
- DIV Rd, Rr — unsigned divide (quotient->Rd, remainder->Rd+1)
258+
- NEG Rd — two's complement negate
259+
- CLR Rd / SER Rd — clear or set register to all ones
259260

260261
- Logical and bit ops
261-
- AND Rd, Rr / ANDI Rd, K — bitwise AND
262-
- OR Rd, Rr / ORI Rd, K — bitwise OR
263-
- EOR Rd, Rr / EORI Rd, K — exclusive OR
264-
- COM Rd — one's complement
265-
- SWAP Rd — swap nibbles
266-
- TST Rd — test for zero or minus
267-
- SBI/CBI / SBIS/SBIC / SBRS/SBRC — set/clear/test single bits and conditional skips
262+
- AND Rd, Rr / ANDI Rd, K — bitwise AND
263+
- OR Rd, Rr / ORI Rd, K — bitwise OR
264+
- EOR Rd, Rr / EORI Rd, K — exclusive OR
265+
- COM Rd — one's complement
266+
- SWAP Rd — swap nibbles
267+
- TST Rd — test for zero or minus
268+
- SBI/CBI / SBIS/SBIC / SBRS/SBRC — set/clear/test single bits and conditional skips
268269

269270
- Shifts & rotates
270-
- LSL Rd / LSR Rd — logical shift left/right
271-
- ROL Rd / ROR Rd — rotate through carry
271+
- LSL Rd / LSR Rd — logical shift left/right
272+
- ROL Rd / ROR Rd — rotate through carry
272273

273274
- Word (16-bit) ops
274-
- SBIW / ADIW — simplified word add/subtract helpers for register pairs
275+
- SBIW / ADIW — simplified word add/subtract helpers for register pairs
275276

276277
- Control flow
277-
- JMP label / RJMP offset — unconditional jump
278-
- CALL label / RCALL offset — call subroutine (push return address)
279-
- RET / RETI — return from subroutine / return from interrupt (sets I)
280-
- BRNE / BREQ / BRCS / BRCC / BRGE / BRLT — conditional branches based on flags
281-
- CP Rd, Rr / CPI Rd, K — compare (sets flags)
282-
- CPSE Rd, Rr — compare and skip if equal
278+
- JMP label / RJMP offset — unconditional jump
279+
- CALL label / RCALL offset — call subroutine (push return address)
280+
- RET / RETI — return from subroutine / return from interrupt (sets I)
281+
- BRNE / BREQ / BRCS / BRCC / BRGE / BRLT — conditional branches based on flags
282+
- CP Rd, Rr / CPI Rd, K — compare (sets flags)
283+
- CPSE Rd, Rr — compare and skip if equal
283284

284285
Use the assembler in `src/tiny8/assembler.py` (or `parse_asm`) to write programs — register operands are specified as R0..R31 and immediates accept decimal, $hex, 0x, or 0b binary notation.
285286

docs/index.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ Designed for rapid experimentation, Tiny8 embraces minimalism with zero unnecess
1919
.. image:: _static/examples/bubblesort.gif
2020
:alt: Bubble sort
2121

22+
⭐️ NEW FEATURE!
23+
24+
.. image:: https://github.com/user-attachments/assets/cd5a0ae0-8aff-41af-81e0-4ff9c426f617
25+
:alt: CLI visualizer
26+
:width: 600px
27+
2228
Installation
2329
------------
2430

@@ -61,6 +67,63 @@ From PyPI (stable)
6167
6268
uv add tiny8
6369
70+
CLI Visualizer
71+
--------------
72+
73+
Tiny8 includes a lightweight terminal-based visualizer that lets you step through a program's execution trace in your terminal. It shows the status register (SREG), the 32 general-purpose registers, and a compact view of a configurable memory range for each step.
74+
75+
Key points
76+
~~~~~~~~~~
77+
78+
- The CLI visualizer expects the CPU to have a populated ``step_trace`` (run the CPU first with ``cpu.run(...)``).
79+
- Controls are keyboard-driven (play/pause, step forward/back, jump, quit) and work in most POSIX terminals that support curses.
80+
- For higher-fidelity animations (GIFs) and interactive matplotlib views, use the ``Visualizer`` class which requires ``matplotlib``.
81+
82+
Interactive controls
83+
~~~~~~~~~~~~~~~~~~~~
84+
85+
.. code-block:: text
86+
87+
Space - toggle play/pause
88+
l or > - next step
89+
h or < - previous step
90+
w - jump forward 10 steps
91+
b - jump back 10 steps
92+
0 - jump to first step
93+
$ - jump to last step
94+
q or ESC - quit
95+
96+
Programmatic usage
97+
------------------
98+
99+
You can invoke the terminal visualizer directly from Python after running the CPU:
100+
101+
.. code-block:: python
102+
103+
from tiny8 import CPU, assemble_file
104+
from tiny8 import run_cli
105+
106+
prog, labels = assemble_file("examples/bubblesort.asm")
107+
cpu = CPU()
108+
cpu.load_program(prog, labels)
109+
cpu.run(max_cycles=15000)
110+
111+
# Run the curses-based CLI visualizer
112+
run_cli(cpu, mem_addr_start=100, mem_addr_end=131)
113+
114+
Tiny8 provides a ``tiny8`` console script (see ``pyproject.toml``). You can run the CLI or the animation mode directly:
115+
116+
.. code-block:: bash
117+
118+
# Run the curses-based CLI visualizer for an assembly file
119+
uv run tiny8 examples/bubblesort.asm # --mode cli --mem-start 100 --mem-end 131
120+
121+
# Produce an animated GIF using matplotlib (requires matplotlib)
122+
uv run tiny8 examples/bubblesort.asm --mode ani -o bubblesort.gif --mem-start 100 --mem-end 131 --plot-every 100 --fps 60
123+
124+
.. important::
125+
126+
Tiny8 uses Python's built-in curses module (Unix-like systems). On Windows, use an appropriate terminal that supports curses or run via WSL.
64127

65128
Examples
66129
--------

0 commit comments

Comments
 (0)