Skip to main content

Simulator for an 8-bit CPU with registers, memory, and a stack.

Project description

Tiny8 documentation

PyPI version License Python versions CI

Tiny8 is a minimal, easy-to-use library for working with compact data structures and simple in-memory storage patterns. It is designed for learning, experimentation, and small-scale projects where a lightweight dependency footprint is desirable. This documentation covers installation, examples, and the API to help you get started quickly.

Installation

Tiny8 supports Python 3.11 and newer. It has no heavy external dependencies and is suitable for inclusion in virtual environments. Follow the steps below to prepare your environment and install from source or PyPI.

Prerequisites

  • Python 3.11+
  • Git (for installing from the repository)
  • Recommended: create and use a virtual environment

From source (development)

git clone https://github.com/sql-hkr/tiny8.git
cd tiny8
uv venv
source .venv/bin/activate
uv sync

[!TIP] uv is an extremely fast Python package and project manager, written in Rust. To install it, run:

# On macOS and Linux.
curl -LsSf https://astral.sh/uv/install.sh | sh

# On Windows.
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

This flow sets up a development virtual environment, installs development requirements, and prepares the project for local editing and testing.

From PyPI (stable)

uv add tiny8

Examples

Bubble sort

This example demonstrates a simple bubble sort algorithm implemented in assembly language for the Tiny8 CPU. The program first fills a section of RAM with pseudo-random bytes, then sorts those bytes using the bubble sort algorithm. Finally, a Python script runs the assembly program and visualizes the sorting process.

bubblesort.asm:

; Bubble sort using RAM (addresses 100..131) - 32 elements
; Purpose: fill RAM[100..131] with pseudo-random bytes and sort them
; Registers:
;   R0 - base address (start = 100)
;   R1 - index / loop counter for initialization
;   R2 - PRNG state (seed)
;   R3..R8 - temporary registers used in loops and swaps
;   R9 - PRNG multiplier (kept aside to avoid clobber in MUL)
;
; The code below is split into two phases:
; 1) init_loop: generate and store 32 pseudo-random bytes at RAM[100..131]
; 2) outer/inner loops: perform a simple bubble sort over those 32 bytes

    ; initialize pointers and PRNG
    ldi r0, 100    ; base address
    ldi r1, 0      ; index = 0
    ldi r2, 123    ; PRNG seed
    ldi r9, 75     ; PRNG multiplier (kept in r9 so mul doesn't clobber it)

init_loop:
    ; PRNG step: r2 := lowbyte(r2 * 75), then tweak
    mul r2, r9     ; r2 = low byte of (r2 * 75)
    inc r2         ; small increment to avoid repeating patterns
    ; store generated byte into memory at base + index
    st r0, r2      ; RAM[base] = r2
    inc r0         ; advance base pointer
    inc r1         ; increment index
    ldi r7, 32
    cp r1, r7
    brne init_loop

; Bubble sort for 32 elements (perform passes until i == 31)
    ldi r2, 0      ; i = 0 (outer loop counter)
outer_loop:
    ldi r3, 0      ; j = 0 (inner loop counter)
inner_loop:
    ; compute address of element A = base + j
    ldi r4, 100
    add r4, r3
    ld r5, r4      ; r5 = A
    ; compute address of element B = base + j + 1
    ldi r6, 100
    add r6, r3
    ldi r7, 1
    add r6, r7
    ld r8, r6      ; r8 = B
    ; compare A and B (we'll swap if A > B)
    cp r8, r5      ; sets carry if r8 < r5 (unsigned)
    brcs no_swap   ; branch if carry set => r8 < r5 => A > B? (keep original order)
    ; swap A and B: store B into A's address, A into B's address
    st r4, r8
    st r6, r5
no_swap:
    inc r3
    ldi r7, 31
    cp r3, r7
    breq end_inner
    jmp inner_loop
end_inner:
    inc r2
    ldi r7, 31
    cp r2, r7
    breq done
    jmp outer_loop

done:
    jmp done

Python Code:

from tiny8 import CPU, Visualizer, assemble_file

prog, labels = assemble_file("examples/bubblesort.asm")
cpu = CPU()
cpu.load_program(prog, labels)
cpu.run(max_cycles=15000)

print([cpu.read_ram(i) for i in range(100, 132)])

viz = Visualizer(cpu)
base = 100
viz.animate_combined(
    interval=1,
    mem_addr_start=base,
    mem_addr_end=base + 31,
    plot_every=100,
    # filename="bubblesort.gif",
    # fps=60,
)

Example Output:

[6, 10, 15, 23, 26, 34, 50, 54, 94, 102, 106, 127, 130, 135, 139, 142, 150, 155, 159, 167, 171, 186, 187, 190, 195, 210, 211, 227, 238, 239, 243, 247]

API Reference

The API section documents the public modules, classes, functions, and configuration options. See:

License

Tiny8 is licensed under the MIT License. See LICENSE for details.

Contributions, bug reports, and pull requests are welcome; please follow the repository's CONTRIBUTING guidelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tiny8-0.1.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tiny8-0.1.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file tiny8-0.1.0.tar.gz.

File metadata

  • Download URL: tiny8-0.1.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tiny8-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4b1bfe38945739423672187c46d2405b83e26d08ac0d4e8a0bff4925bdde48c9
MD5 e8871b3dd29739e26039dc5ee9cb115a
BLAKE2b-256 93f86ccfe04a8a0d9d8424f96154e295ba2a550e0a50c7b5548fcdf7a8a4718c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiny8-0.1.0.tar.gz:

Publisher: python-publish.yml on sql-hkr/tiny8

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tiny8-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tiny8-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tiny8-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57bec2df3af2025f4f3b26a090c415308de1b6064f703dbd4b20e9268627ea38
MD5 63080d14adfc6688d06f2e5859bbc0d7
BLAKE2b-256 1e74f08513c3f923a452a77ad55e4b01df2eedcdfe7bfd64be840aebd96f824f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiny8-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on sql-hkr/tiny8

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page