" />

A place to talk about software and stuff.


23 September 2020

CircuitSim2: A logical circuit simulation library

CircuitSim2 is a library I wrote in C# for logical circuit simulation. By ‘logical circuit’, I mean a graph of logical gates assembled in some way. The library is inspired by a few other pieces of software - I had used the venerable Logisim in a course about low-level computing, and I had spent way too much time using Wiremod in the game Garry’s Mod.

Writing CircuitSim2 is probably the most fun I’ve had on a side project, although I wouldn’t say it’s quite complete. At this point, there is a library of components that can be used in any C# project. My ultimate goal is to have, as one part of CircuitSim2, a graphical circuit editor which can be used to build circuit designs, similar to Logisim.

Building a circuit in C#

To start using CircuitSim in the context of a C# program or library, you can just add a reference to CircuitSim and start using the components it includes. It’s designed to be extensible, so that if you need to be able to add components that you design, it’s as easy as possible.

The core type of component in CircuitSim2 is a chip. A chip is a type of object which has inputs and outputs (in addition to other things). The inputs and outputs are generic, so an input or output has a type. While in the real world, a chip might only accept a wire carrying an electrical signal, in CircuitSim2, an input or output can accept any kind of type, like a boolean, an integer, a string, or a list.

Most of the chips in CircuitSim2 will run as expected, but CircuitSim2 also has a rudimentary engine which will support more advanced operation. Without an engine, when a chip’s input is changed, it will trigger a change in each of its outputs, which in turn triggers chips connected to those outputs to process the inputs. Since this is a recursive process, if the circuit is cyclical, it will result in a stack overflow. I’m not going to cover using the CircuitSim2 engine in this post, but it is covered in some of the examples.

Let’s say that we have a C# program, and part of that program is a function that evaluates two boolean conditions to see if they are both true. Of course, we could use the ‘&&’ operator, but let’s just suppose that the keyboard is possessed and reacts badly if we type ‘&’ even once. Yes, it’s a contrived example, but work with me here.

We can define a C# class that will do our boolean evaluation for us, using CircuitSim2 components:

using CircuitSim2.Chips.Digital.Logic;

namespace stupid.boolean.evaluators
    public class AndEvaluator
        private readonly Generators.Constant input_A;
        private readonly Generators.Constant input_B;

        private readonly Logic.AND chip;

        public AndEvaluator()
            chip = new Logic.AND();
            input_A = new Generators.Constant();
            input_B = new Generators.Constant();

        public bool Evaluate(bool A, bool B)
            input_A.Value = A;
            input_B.Value = B;

            return chip.Outputs.Out.Value;

If you’ll excuse my contrived example, I do think this is pretty cool! We can assemble arbitrary circuits to accomplish any kind of task. There’s even a SerialPort chip which can interface with a physical serial port (which has not been tested)!

A graphical circuit editor

I started working on a graphical editor using CircuitSim2 - the header image for this post is an early render. The first version of the editor used WinForms and isn’t complete - I realized too late that I’d rather build something cross-platform. I’m currently looking at Avalonia.

Howver, even the WinForms version did end up pretty cool - it allows you to spawn chips, and connect them in arbitrary combinations so long as the types match. In fact, since we can write CircuitSim2 chips that are composed of other chips, the circuit editor could render a ‘see through’ version of a chip. Here’s a byte adder:

It was really fun to write the rendering routine for this - none of what’s pictured is a static asset, so the grid and the chip are all software-rendered.