The Signal State

The Signal State

View Stats:
Loops
So i played around a little with loops, that is circuits where the input of a module can be affected by its own output and hence its output can change in that step (unlike signal delay).

To understand how such circuits work it is important to know how the game updates the state of the modules during a step.

It is instructive to watch steps on the lowest velocity setting to understand how all of this works.

Disclaimer:
All of this is based on observations of the game, so it may be inaccurate or even completely wrong. More important is: this is "undocumented behavior", so the developers may decide to make changes to the game that break circuits based on this behavior (now you know one of the typical pitfalls of "legacy code": programs relying on undocumented OS-features that break when these features are changed).

First a few definitions:
- step / time step: One time step e.g. what you forward in single step mode.
- state (of a module): the output of a module, depending on its inputs.
- state update: when the inputs of a module change its output has to be calculated anew, that might change the output or not (think of an OR gate, when input A is at 1 its output doesn't change when input B changes, when input A is 0 a change of input B will change the output of the OR gate.

During a step all modules are updated until a stable (overall) state is reached, that is when all modules have outputs according to their inputs. That is the end result of a step.

When there are no loops it doesn't matter in which order the modules are updated, (or at least I can't readily think of an example where it does), but when there are loops that order becomes important.

The order in which modules are updated is top to bottom, left to right. During a step the game performs several update "CYCLES", in each cycle all modules which had changed inputs will be updated. If that changes its output, then the module connected to that has a changed input and is marked for update.

When you make a circuit that never reaches a stable state (e.g. a NOT connected to itself) then the step never resolves and you need to stop and rewire something.

Now if that other module is to the right or in a lower part of the rack, then it is updated in the same cycle, but if it is left/up, then it hasn't been updated by the end of the cycle, which means (a) another cycle is needed, (b) it is updated in the next cycle, not in this one.

This means if you connect 10 splitters, each left to the previous one, a change of the first input will take 10 cycles to reach the last splitter. OTOH if each output goes to the right they all resolve in the same cycle.

This means in loops it becomes important, where the modules are placed on the rack.

Outputs to "previous" modules cause delays, and these may lead to intended or unintended consequences.

Practical applications:
Let's say we put a Clock(2), it oututs to a splitter, that splitter outputs to a NOT, and the splitter and NOT output to a XOR. The clock changes its output at the beginning of each step, let's say from 0->1. Now if all outputs go to the right then in the same cycle
- first the splitter goes 0->1,
- the NOT goes 1->0
- and the XOR stays at 1->1, since 0 XOR 1 = 1 XOR 0 = 0.
But now let's put the NOT left of the Splitter. Then in the first cycle:
- Clock 0->1,
- NOT still has 0 as input from the previous step so its output stays at 1
- Splitter 0-> 1
- XOR has inputs 1,1 so its output changes to 0
Next cycle:
- Clock stays as it is
- NOT now has a new input from the chage of the Splitter so it updates
NOT 1->0
- Splitter has unchanged input, so no update
- XOR now sees 0,1 and outputs 1.

So this circuit briefly gives a "0" at the start of each step, otherwise it stays at "1", which can be useful, e.g. for a latch construction:

Such a latch could be a VCA, outputting to a SUM, out to a Splitter, one of its outputs back to the VCA. By making one of the summands a +1 until some condition is met in that step this could e.g. be a counter that counts upward each cycle the +1 is still in the sum. The output of the above XOR could go to the other input of the VCA, so our counter is reset to 0 at the start of each step, then adds up until the +1 wire is blocked (e.g. by another VCA with one input at 100 until a condition is met that could depend on the counter itself).

A very nice application for such a loop construct is the "Fast Division" workshop example by Monotof1 (there might be a way to do it without loops too, but the space is a bit tight ... but it is possible, hehe).

Edit:
Another very instructive example is "Clock implementation" by xicmiah.