trap

The trap trap is a stateful with two states.

vacant mode

00/0000/00
loading…

vacant waiter

In vacant mode, the trap supports two operations:

  • If a marble enters the left side, it passes directly through.
  • If a marble enters the right side, the trap holds it in place and enters occupied mode.

occupied mode

00/0000/00
loading…

occupied waiter

In occupied mode, the trap’s behaviour changes:

  • If a marble enters the left side, it dislodges the trapped marble. The trap ejects both marblemarble and returns to vacant mode.
  • If a marble enters the right side, it is deflected by half a cell to the right. The trapped marble remains in place.

application

The trap allows us to delay operations so we can sync them up to an appropriate time.

We’re going to build a zero checker. This is a device that checks whether a number in a stream is zero.

If we’re using (say) 4-bit numbers, then zero is holeholeholehole. If we encounter marble at any point in the cycle, such as marbleholeholehole or holemarblemarblehole, we know the number isn’t zero. But we don’t want to release this marble immediately — we want to wait until the entire number is processed, and then release either a marble (to indicate a non-zero number) or a hole (to indicate zero).

Traps are perfect for this:

00/0000/00
loading…

zero checker

This device receives a stream of 4-bit numbers, and releases a stream indicating whether they are zero or non-zero.

Note the presence of a pause hole p. Even though our words are 4 bit, the device repeats on a cycle of 5, where the 5th bit is always hole. This is to ensure there’s no data marble to interfere with the trap when it releases any captured marble.

Next, we’ll upgrade this into a more powerful checker.

challenge

Modify the checker so that instead of checking if a number is zero, it checks if two numbers are equal.

continue