interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / tensix / oracle.py

137 lines · 7092 bytes Raw Blame History
1#!/usr/bin/env python3
2# oracle.py -- interop oracle, not Omega-native; never generates kernels.
3#
4# Host dependency (oracle only): Python 3 standard library (ctypes, struct). It moves bytes that asm2 made
5# into two outside checkers and moves the result back out; it never writes, changes or chooses an
6# instruction word. Omega replacement: an .om host loader once Omega has a dlopen/FFI word (ttsim path)
7# and a byte-writer for ELF headers (qemu path); both are proposals, not implemented.
8#
9# oracle.py ttsim LIB ARCH IMAGE A B N OUT
10# Tenstorrent ttsim (libttsim_{wh,bh}), the vendor's open simulator, driven through its PCI Express
11# interface only, the way a host driver drives a card: read BAR0 from config space, point one TLB
12# window at Tensix tile L1, hold the tile's five RISC-V cores in soft reset, write IMAGE at L1 0x0
13# (the reset pc of RISCV B), the argument block at 0x1000, A at 0x2000, B at 0x3000, then release
14# RISCV B only and clock until the stub writes the done mark 0x0600d000 at 0x1010. OUT gets the 4N
15# bytes at 0x4000. ARCH is wh (Wormhole B0, n150) or bh (Blackhole A0, p150).
16# oracle.py elf IMAGE A B N OUT
17# Wraps IMAGE in a 32-bit RISC-V ELF executable for qemu-riscv32 (Linux user mode): one load segment
18# at 0x10000, IMAGE at 0x10100 (the entry), argument block 0x11000, A 0x12000, B 0x13000, out 0x14000.
19# Sources (read 2026-10-26): tt-isa-documentation be833101 WormholeB0/PCIExpressTile/TLBs.md,
20# BlackholeA0/PCIExpressTile/HostToDeviceTLBs.md, */TensixTile/SoftReset.md, */NoC/Coordinates.md;
21# ttsim b3620244 src/libttsim.cpp (API), src/pcie.cpp (config space); ELF: System V gABI, RISC-V psABI.
22import ctypes, struct, sys
23
24L1_ARGS, L1_A, L1_B, L1_OUT, DONE = 0x1000, 0x2000, 0x3000, 0x4000, 0x0600D000
25SOFT_RESET_0 = 0xFFB121B0 # RISCV_DEBUG_REG_SOFT_RESET_0 (BH SoftReset.md; WH tile_regs 0xFFB12000+0x1B0)
26ALL_CORES, BRISC = 0x47800, 0x800 # bits 11 (B), 12-14 (T0-T2), 18 (NC)
27TLB_CFG = 0x1FC00000 # TLB configuration array in BAR0 (both architectures)
28
29
30def die(msg):
31 sys.stderr.write("oracle: " + msg + "\n")
32 sys.exit(1)
33
34
35def words(path, n):
36 data = open(path, "rb").read()
37 if len(data) != 4 * n:
38 die("%s has %d bytes, want %d" % (path, len(data), 4 * n))
39 return data
40
41
42def ttsim(lib_path, arch, image_path, a_path, b_path, n, out_path):
43 lib = ctypes.CDLL(lib_path)
44 u32, u64, vp = ctypes.c_uint32, ctypes.c_uint64, ctypes.c_void_p
45 lib.libttsim_pci_config_rd32.restype = u32
46 lib.libttsim_pci_config_rd32.argtypes = [u32, u32]
47 lib.libttsim_pci_mem_wr_bytes.argtypes = [u64, vp, u32]
48 lib.libttsim_pci_mem_rd_bytes.argtypes = [u64, vp, u32]
49 lib.libttsim_clock.argtypes = [u32]
50 lib.libttsim_init()
51 ident = lib.libttsim_pci_config_rd32(0, 0x0)
52 want = {"wh": 0x401E1E52, "bh": 0xB1401E52}[arch]
53 if ident != want:
54 die("config space id 0x%08x, want 0x%08x for %s" % (ident, want, arch))
55 if not lib.libttsim_pci_config_rd32(0, 0x4) & 2:
56 die("BAR memory decode (Command.MSE) is off")
57 bar0 = (lib.libttsim_pci_config_rd32(0, 0x10) & ~0xF) | (lib.libttsim_pci_config_rd32(0, 0x14) << 32)
58
59 def wr(addr, data):
60 buf = ctypes.create_string_buffer(data, len(data))
61 lib.libttsim_pci_mem_wr_bytes(bar0 + addr, buf, len(data))
62
63 def rd(addr, size):
64 buf = ctypes.create_string_buffer(size)
65 lib.libttsim_pci_mem_rd_bytes(bar0 + addr, buf, size)
66 return buf.raw
67
68 if arch == "wh":
69 # 1 MiB windows: local_offset = top 16 bits of the 36-bit tile address, x_end at bit 16, y_end at 22.
70 x, y, shift = 1, 1, 20 # NoC #0 (1,1) is a Tensix tile (WormholeB0/NoC/Coordinates.md)
71
72 def point(window, tile_addr):
73 cfg = (tile_addr >> shift) | (x << 16) | (y << 22)
74 wr(TLB_CFG + 8 * window, struct.pack("<Q", cfg))
75 return window << shift
76 else:
77 # 2 MiB windows, 96-bit configuration: local_offset = tile address >> 21, x_end at bit 43, y_end at 49.
78 x, y, shift = 1, 2, 21 # NoC #0 (1,2) is a Tensix tile (BlackholeA0/NoC/Coordinates.md)
79
80 def point(window, tile_addr):
81 cfg = (tile_addr >> shift) | (x << 43) | (y << 49)
82 for i in range(3): # three 32-bit register writes
83 wr(TLB_CFG + 12 * window + 4 * i, struct.pack("<I", (cfg >> (32 * i)) & 0xFFFFFFFF))
84 return window << shift
85
86 l1 = point(0, 0)
87 regs = point(1, SOFT_RESET_0 & ~((1 << shift) - 1)) + (SOFT_RESET_0 & ((1 << shift) - 1))
88 reset_before = struct.unpack("<I", rd(regs, 4))[0]
89 wr(regs, struct.pack("<I", ALL_CORES)) # hold all five cores
90 image = open(image_path, "rb").read()
91 wr(l1 + 0, image)
92 wr(l1 + L1_ARGS, struct.pack("<IIIII", L1_A, L1_B, L1_OUT, n, 0))
93 wr(l1 + L1_A, words(a_path, n))
94 wr(l1 + L1_B, words(b_path, n))
95 wr(l1 + L1_OUT, b"\xee" * (4 * n)) # poison: an unrun kernel cannot pass
96 wr(regs, struct.pack("<I", ALL_CORES & ~BRISC)) # release RISCV B only
97 clocks, step, limit = 0, 100, 200000
98 while struct.unpack("<I", rd(l1 + L1_ARGS + 16, 4))[0] != DONE:
99 if clocks >= limit:
100 die("no done mark after %d clocks" % clocks)
101 lib.libttsim_clock(step)
102 clocks += step
103 open(out_path, "wb").write(rd(l1 + L1_OUT, 4 * n))
104 wr(regs, struct.pack("<I", ALL_CORES))
105 lib.libttsim_exit()
106 print("ttsim %s: tile (%d,%d) soft_reset_0 at start 0x%x, image %d bytes, done within %d clocks"
107 % (arch, x, y, reset_before, len(image), clocks))
108
109
110def elf(image_path, a_path, b_path, n, out_path):
111 base, code, args, a, b, out = 0x10000, 0x100, 0x1000, 0x2000, 0x3000, 0x4000
112 image = open(image_path, "rb").read()
113 if code + len(image) > args:
114 die("image too large")
115 body = bytearray(out + 4 * n)
116 body[code:code + len(image)] = image
117 body[args:args + 16] = struct.pack("<IIII", base + a, base + b, base + out, n)
118 body[a:a + 4 * n] = words(a_path, n)
119 body[b:b + 4 * n] = words(b_path, n)
120 # ELF32 header: little-endian, ET_EXEC, EM_RISCV (243), version 1, entry, phoff 52, no sections,
121 # e_flags 0 (soft-float ABI, no RVC), ehsize 52, one 32-byte program header.
122 ehdr = b"\x7fELF\x01\x01\x01" + bytes(9) + struct.pack(
123 "<HHIIIIIHHHHHH", 2, 243, 1, base + code, 52, 0, 0, 52, 32, 1, 0, 0, 0)
124 # PT_LOAD, offset 0 -> vaddr 0x10000, read/write/execute, page aligned.
125 phdr = struct.pack("<IIIIIIII", 1, 0, base, base, len(body), len(body), 7, 0x1000)
126 body[0:84] = ehdr + phdr
127 open(out_path, "wb").write(bytes(body))
128
129
130if __name__ == "__main__":
131 a = sys.argv[1:]
132 if len(a) == 8 and a[0] == "ttsim" and a[2] in ("wh", "bh"):
133 ttsim(a[1], a[2], a[3], a[4], a[5], int(a[6]), a[7])
134 elif len(a) == 6 and a[0] == "elf":
135 elf(a[1], a[2], a[3], int(a[4]), a[5])
136 else:
137 die("usage: oracle.py ttsim LIB wh|bh IMAGE A B N OUT | oracle.py elf IMAGE A B N OUT")