# 40d:Mechanical logic

Machine logic uses the also new system of gears, axles, windmills, and waterwheels to solve problems. Power sources (windmills and waterwheels) transfer power to machine parts (axles and gears). If there is not enough power in a system to allow all the parts to move, it will shut down entirely. Individual gears can also be shut down by levers to interrupt or otherwise alter the transfer of power throughout the machine. All sorts of logic gates can be created based on those rules alone. Currently four have been created in a proof-of-concept sort of way in a relatively quick and cheap manner:

In the above picture, the four basic logic operations are shown. Gears connected to outside power are circled in red. Gears that can be deactivated by lever are color-coded to the lever that deactivates them. For the purposes of binary logic, a lever is TRUE if it is pointing LEFT. Levers pointing RIGHT are FALSE.

In AND, two adjacent gears are connected to levers. Power is only transferred to the one-tile output axle if both levers are set to true.

In OR, two power sources are connected to an output gear and axle. If either lever is set to true, the axle will be connected to power and also be true.

In XOR, 40 units of power are transferred to a central gear. To power the output axle, it must be connected to it by switching one of the levers to true. However, if both levers are set to true, then the machine requires 42 units of power to run, and thus shuts down due to lack of power.

in NOT, there is a powered gear connected directly to the output axle. If the lever is set to true, the powered gear connects to 3 other unpowered gears, which causes the machine to shut down due to lack of power.

## Mechanical Logic

Mechanical Logic is a faster, more reliable replacement for Water Logic. It is also somewhat more complex, requiring massive amounts of wood, stone, and mechanical power. Unlike the above examples, these designs require constant power, or they will not function correctly.

The following examples operate on these design rules

• Systems use Binary Logic
• Logic High is defined as an Unpowered axle.
• Logic Low is defined as a Powered Axle.
• Gearboxes, when supplied a Trigger High, thusly begin to transmit a Logic High signal

Note that there are several flaws with Mechanical Logic. As noted, large amounts of wood, stone, and massive amounts of power are required. Secondly, the system operates on the concept of the 'rotation sensor' Obviously, this does not exist in Dwarf Fortress at this time, and there are no plans for such a device to exist - therefore, a surrogate unit is required.

Now, there is one device we can use: a pressure pad. Therefore, we simply need to convert mechanical energy into pressure - for example, using a Screw pump.

```░░░░░░░
░≈%>^_░
░░░░^░░
░░░
```

The pump draws from a constantly-filled tile on the left, and pushes right. The pressure pad there then acts as the 'sensor' for whether there is rotation present. Finally, the pit on the right acts as a drain- when the pump stops, the water drains out and the pad resets (no rotation). The lower pad shown is used as an 'inverse' out.

Note that in all the examples, "M" is used to denote a rotation sensor - just affix a gearbox instead and route that to one of these units, "P" is used just to say where Power is supposed to be applied, and "X", "Y", and "Z" are just to show that in a multi-part logic gate, which 'sensor' links to which gearbox internally. "o" is a gearbox that does not have any trigger connections, and is used for visual clarity.

Note that because of the above logic rules, rotation sensors linked to gear assemblies must be linked while active - either set the pressure plates to be triggered by creatures of any weight and enable civilians (so that the mechanic is standing on the plate and triggering it while completing the link), or use a second lever to pre-toggle the destination gear assemblies.

## Unclocked Logic

### (N)OR Gate

```P→**→M O
AB
```

```   A
P→o*o→M O
o*o
B
```

### (In)equal

When Input A != Input B, O goes LOW. Invert Y and Z to change this

```   A          !A
P→o*o→M Z  P→o*o→M Y
o*o        o*o
B          !B

YZ
P→**M O
```

### AND NOT

When A is HIGH and B is LOW, O goes HIGH

```   A
P→o*oM O
o*o
!B
```

### NOT or BUFFER

```P→*→M O
A
```

### SELECT

[A?B:C] O will be either B is A is HIGH, or C otherwise. In other words, IF-THEN-ELSE

``` B!A
P→*
o→M O
P→*
CA
```

### DISTRIBUTOR

Passes D to the selected output (selected via A) and a logic HIGH to the other. Remove * lines to output a logic LOW instead.

```   A
*P--*!A
P*o*o---→O0
Do--*o-→O1
*P----*A
!A
```

### S/R LATCH

S = Set R = Reset

```   !RS
P→o*o*M O
o*o
O
```

### OSCILLATOR

```P→*M O
!O
```

### (AND-NOT) OR

A & Z are actually inverted, just not shown.

```AB
**
|MZM O
o-*o
P--*C
```

## Clocked Logic

### D-LATCH

When Clk is HIGH, O will output A When Clk is LOW, O will output whatever A was when Clk went LOW

```A !Clk
*-*oM O
o*-*
↑O Clk
P
```

### Select - Hold Choice

This is basically a clocked SELECT gate- it acts like a normal Select, but only while Clk is high. Then Clk is low, it outputs whatever the last selected output was when CLK was last HIGH.

```  A
P→**!Clk
oM Z
P→**Clk
Z

B!Z
P→*
o→M O
P→*
CZ
```

### SELECT-HOLDALL

This is like the above, but it also holds the last values of B and C as well.

```B !Clk
*-*oM X
o*-*
↑X Clk
P

C !Clk
*-*oM Y
o*-*
↑Y Clk
P

PA!Clk
→**
oM Z
→**
PZClk

X!Z
→*
o→M O
→*
YZ
```

### Flip-Flop

Oscillates between high and Low on Clk

```O !Clk
*-*oM X
o*-*
↑X Clk
P

!XClk
*-*oM O
o*-*
↑O !Clk
P
```

## Complex Logic Gates

### MEMORY CELL

#### Type A

R = Read Input W = Write Input High (Off) is R/W D = Data line (I/O)

```ZW
**
↑oM Z
*!W
Do↓P
**
!RZ
```

#### Type B

R = Read W = Write High (Off) is R/W D = Data line (I/O) A = Cell Active (Enable Chip)

```Z
*oo
↑**WA
Poo
oM Z
*!W
*!A
Do
*!A
**←P
!RZ
```

1-bit w/ full carry A, B are bits in. C is Carry in, O is bit out

```   A          !A
P→o*o→M Z  P→o*o→M Y
o*o        o*o
B          !B

YZ
P→**M X

X          !X
P→o*o→M V  P→o*o→M U
o*o        o*o
C          !C

VU          C
P→**M O    P→o*oM W
o*o
Z

P→**→ Carry (Out)
ZW
```