Show HN: I made a little digital circuit simulator that operates on PNGs (github.com)

259 points by lynndotpy ↗ HN
This is a little toy project of mine that lets you simulate digital logic graphs. It was inspired by Minecraft's Redstone and the Piet esolang.

It's got some serious drawbacks-- you write circuits as PNGs and simulate them with a Python interface. It's slow to run and slow to experiment with. And it is certainly difficult to use for people with any kind of color blindness. But despite that, I hope this can still be a fun toy!

53 comments

[ 5.5 ms ] story [ 120 ms ] thread
We’re also working on something like this, but we’re making a game around it

https://charperbonaroo.github.io/bls/#0

Oh wow this is awesome. I got through the first few levels and would like to complete it later...

Also, is this the same as the 2015 'bitmap logic simulator'? I found this while reworking reso awhile back: https://realhet.wordpress.com/2015/09/02/bitmap-logic-simula...

Yes, I also posted the progress there. The 8bit cpu example runs perfectly in my “game”.

I’m rewriting it to Unity and there I can run the 8 bit cpu from that blog with about 14000 ticks per second.

We’re planning to create a game around it with few dozen of levels, including controlling robots, traffic lights, a bowling pin sorter and tanks/units in a tiny top-down shooter (where programs from players play against each other).

Edit: if you download the cpu image from the blog and upload it to the game, you can see it run. It’s visually a bit broken because the game in its current state isn’t really designed to have such a big canvas, but it runs perfectly fine.

Edit 2: the CPU running in unity web build here: https://charperbonaroo.github.io/microengineer-build/

Oh wow yeah, this is really impressive. I love this! Do you plan to submit your game on Steam or anywhere like that?

An idea I had (but never finished) was to let Reso interface with GPIO pins (say, on a Raspberry Pi) so people could interface their virtual circuits with their real circuits. I think that'd be really cool (if that is possible with Unity)

Yes, I want to create an actual game on steam around it. We’re working on it on our spare time in our company, self-funded. Spare time is rare though, so I don’t know when it will be finished.

Interfacing with real hardware is a neat idea, I wonder what people would do with it

Very nice!

FWIW, I can't figure what to do on the "ride the line" level. I don't understand what the motors do exactly (powering both motors simultaneously makes the car go sideways) and what the sensors are sensing.

You just connect the left sensor to the right motor and the right sensor to the left motor. I think the "light sensors" are measuring the brightness of the ground underneath the car or something. Hence if one of the sensors goes over the black track it won't register.
(comment deleted)
That's incredible. It was just "very cool" for the first few levels - if you cleaned up the directions and added a factorio-style blueprint library I could easily see myself sitting down to work out optimal solutions to any level you put out, like I did with Codex of Alchemical Engineering - but the last level put it on a whole new level. This is everything that's good about HS electronics classes where you get to make line-following robots but without any of the real-world annoyances. Massively streamlined. Get enough content in this interface and I bet you could sell a lesson plan to every school district in the United States.
Very cool. One piece of feedback is that it took me an embarrassingly long time to figure out that "edit" was not selected. Since it's highlighted (in red), I tried to edit the circuit on the screen to no avail.
I’m surprised I haven’t heard this feedback earlier. You’re righht.
Brilliant idea!

That logo however is the most repulsive piece of artwork I've seen in a long time.

(I am sorry for being blunt)

The project is super cool but I agree the logo is a terrible example of it in use. I'd love to see a more easy to digest example. The logo circuit is confusing and I'm not sure what to make of it.
Can't account for some taste, I love the look!
Speak for yourself, I love it!
I speak for me, myself and I. That's three votes buddy.
I think it's cool, TBH. But sure, another palette might look better. That might push towards more of a special purpose frontend though, vs. whatever image editor you want (RGB are easy).
That's neat.

There's lots of interesting circuit simulators on the web now, but has anyone ever seen a toy/simple simulator for VLSI devices? Something where you could draw in metal/poly/whatever layers to create and simulate transistors?

As a complete layman I wonder if there’s a future where you could use a picture/X-ray of an old CPU architecture and simulate it blindly with an approach similar to yours?
I think it'd have to be a very different approach (unless it's an electron scan?) I'm not an electrical engineer though, so I'm in the layman camp as well!

If someone made that, I assume it'd use some computer vision to identify the individual components and the connections between them. After that, each component would need to have their individual simulator. (Unless x-rays can see individual semiconductors?)

"Blindly" is a stretch but people have built transistor level simulations of old chips from photographs. Check out the delightful Visual 6502 if you've never seen it before:

In the summer of 2009, working from a single 6502, we exposed the silicon die, photographed its surface at high resolution and also photographed its substrate. Using these two highly detailed aligned photographs, we created vector polygon models of each of the chip's physical components - about 20,000 of them in total for the 6502. These components form circuits in a few simple ways according to how they contact each other, so by intersecting our polygons, we were able to create a complete digital model and transistor-level simulation of the chip.

This model is very accurate and can run classic 6502 programs, including Atari games. By rendering our polygons with colors corresponding to their 'high' or 'low' logic state, we can show, visually, exactly how the chip operates: how it reads data and instructions from memory, how its registers and internal busses operate, and how toggling a single input pin (the 'clock') on and off drives the entire chip to step through a program and get things done.

http://www.visual6502.org/

I appreciate any time folks take steps towards what "could be" rather than merely replicating what "is".

Thanks for building this, contributing to the "marketplace of ideas", and inspiring others.

That's an awesome idea, I wish I had something like that when I was doing college hardware design courses.
> It was inspired by Minecraft's Redstone and the Piet esolang.

Isn't this the crux of the NSO Group exploit on Iphones?

That's awesome, thanks for sharing! Will play with it in the weekend.
I'm annoyed I didn't think of this before. This is genius.
This reminds me of the way Powder Toy does circuit simulation with cellular automata
This reminds me very strongly of the WireWorld cellular automaton: https://quinapalus.com/wires0.html

(Also, how did you get a post with both text and a link? I didn’t know it was possible.)

Oh yes, that was an inspiration!

Re submission: The submit box says "title and link or text". Must be an inclusive OR, because it gave me no problems.

(I also have JavaScript disabled, so if they block it client side, I haven't noticed.)

This is great. Well done. Thanks for posting it.
Have you tried http://Circuitverse.org?

It's by a friend who initiatally built a digital circuit simulator and then turned it into a GitHub like collaborative website.

I have not, but I would have loved this when I took Digital Logic courses. Very cool stuff
Similar idea to Wired-Logic https://github.com/martinkirsche/wired-logic

Wired-Logic uses a GIF as output.

Interesting take on making AND and XOR gates, makes it a bit more colorful.

Oh wow I love this, I missed it while looking for similar projects. The example is very impressive to boot.
Reminds me of Pico-8, which also encodes projects (in that case games) into a PNG, though I think it uses some metadata to do that, and the visible image itself is a kind of screenshot. Either way, I love it when projects do this
Super-cool.

I haven't looked at the source, but I bet it's massively parallel, at the pixel level? (I do see you say it's "not a cellular automaton", though...)

If so, I wonder how difficult it would be to port this to GPU, either writing CUDA yourself, or maybe even using PyTorch.

If PyTorch, you might be able to make it differentiable (by changing ANDs to MULs and such). What would be achieved by making the simulation differentiable I don't know. But surely you'd come up with something interesting. Given how much effort has gone into developing architectures whose gradients don't explode, my guess is you'd run into difficulties, but maybe you could even learn programs that model various sequences.

The main point would just be to run it on the GPU, though.

Thank you! And not parallel, alas. The image compiles to a graph, where contiguous regions are nodes and adjacent regions form edges.

Regarding differentiation, the logic gates could look like a differential equation. Between iterations, input nodes amass a binary vector from adjacent wires, and pass them onto adjacent output nodes. So, mapping {0,1}^n --> {0, 1}, you could write them all in terms of some periodic function. (E.g. xor implemented with sin, or implemented with a sigmoid made from a quarter of a sin, etc.)

Either way, yeah! There is plenty of room to speed this up