r/typescript • • 23d ago

Designing hardware in Typescript

Simten is an open source (https://github.com/simtenHQ/simten) HDL where circuits are written in TypeScript. The simulator is Ts as well, so everything runs in-process in Node or the browser.

I built a game around it to showcase the platform, https://play.simten.dev/

So essentially write a circuit in the editor to match the spec, it simulates live on the canvas on the right. You can toggle inputs, step the clock back/forward, drill into parts to view internals etc.

Here is the basic syntax for a half adder (GIF shows a full adder built from two of these):

import { bit, circuit } from '@simten/core'
import { And, Xor } from '@simten/core/std'

const HalfAdder = circuit('HalfAdder', {
  inputs: { a: bit, b: bit },
  outputs: { sum: bit, carry: bit },
  nodes: { xor: Xor, and: And },
  connect: ({ inputs, outputs, nodes: { xor, and } }) => [
    inputs.a.to(xor.a, and.a),
    inputs.b.to(xor.b, and.b),
    xor.out.to(outputs.sum),
    and.out.to(outputs.carry),
  ],
})

You can also run circuits headlessly in node:

import { simulate } from '@simten/core'
const sim = simulate(HalfAdder)
sim.set({ a: 1, b: 1 })
sim.read() // { sum: 0, carry: 1 }

Because it's all in-process, you can use npm packages at the transpile step. For example, this calls figlet to bake ASCII art into a ROM and streams it to a console (press play in the bottom left and change "Simten" text on line 7 ): https://simten.dev/circuit?example=figlet

For when you are sick or typing there's an mcp server, so you can ask your assistant for a circuit and it writes it, tests it and opens it for you to view. Passing it proper hardware specs I have built RISC-V cpus with it among other things. it has been a great learning experience building it. Feedback and/or contributions welcome!

9 Upvotes

4 comments sorted by

2

u/Goodassmf 22d ago

Wow. So cool. So its like a game you need to complete the schematics to learn how to use the library?

1

u/kottbusser_floor 22d ago

Pretty much so far! I'm going to add more levels where you write assembly (as arrays in Typescript) and feed them into a CPU and watch it perform instructions. Then maybe a few other side quests like build hardware snake or pong. Open to any suggestions as well ofc!

1

u/Goodassmf 14d ago

I have so many thoughts. It looks very similar to KittyCad. My friend saw it was so happy because he thuoght he could finally build circuits with UI. He's a backend guy, he will always UI over code. I'm a front end guy, and will always prefer code/cli. The compilation from TypeScript is trivial, correct? Im not familiar with low level architectures.

1

u/kottbusser_floor 6d ago

I actually wasn't aware of KittyCAD but yeah that is a very similar idea

When you say compilation from Ts being trivial do you mean into something that can actually simulate the circuit or into a bitstream that can run on real hardware?

circuit() builds a description of the parts and wires, then simten checks it, flattens it to gates and simulates it, settling the logic after every change and updating all the registers together on a clock edge. Exporting to Verilog for an FPGA is another step once you are happy with how it runs in node.

It's code first on purpose since schematics get messy past a few dozen parts, and it's difficult to handle all the UI states with dragging without adding lots of metadata to each circuit file. Your friend could try Logisim or Turing complete if he prefers UI first, or just describe the circuit to an AI assistant and let the MCP server write the simten code and display it to him?