🡄 Previous

Next 🡆

Contents > Applications > TetrominoScript Editor

NAND Tutorial

This tutorial demonstrates the features of TSE by explaining how to construct a NAND gate.

Select File > New... or press Ctrl-N:

tse-file-new.png

In the New file chooser, navigate to code/ts/examples, enter nandExample.t, and hit Save:

tse-new-file-chooser.png

The window regions reset, and nandExample appears in the title bar:

tse-new-result.png

Turn the scroll wheel to adjust the grid cell size to 32:

tse-zoom-32.png

The goal is to build the following circuit.

NAND Schematic

It will be constructed bottom-up, starting with the input nodes.

Type “in a” into the TS program region:

tse-in-a.png

Click on grid cell −2:0:

tse-cell-minus-two-zero.png

That transfers the cell coordinates to the TS program:

tse-in-a-coordinates.png

Press Enter. Type “in b”. Click on cell 1:0:

tse-in-b.png

Click on the input field. Type “01”:

tse-input-01.png

Press the Run button.

The input nodes appear in the playfield. Their states correspond to the input bits:

tse-nodes.png

Note: TSE assigns the input nodes to the bit values from left to right regardless of order that the TS program defines them. Also, for readability, bit strings with many digits can be partitioned into nibbles or arbitrary group sizes by using spaces as separators.

In the TS program, make sure the cursor is positioned at the end of line 2 or at the start of line 3. Click on the components combo box. Type “notLeft”:

tse-notleft-select.png

Press the Add button. Move the mouse cursor into the playfield:

tse-notleft-drag.png

TSE depicts notLeft as a dark rectangle. An unassigned input node protrudes from its bottom. An output node assigned 1 extends from its top.

Node a will serve as notLeft's input node. Overlap the nodes thusly:

tse-notleft-insert.png

Click the left mouse button.

TSE appends notLeft to the TS program:

tse-notleft-added.png

Note: While positioning a component, the right mouse button or Esc deselects the component. Also, if a component is put in the wrong place, edit its coordinates in the TS program and press the Run button to update the playfield.

In the TS program, make sure the cursor is positioned at the end of line 3 or at the start of line 4. Click on the components combo box. Type “notRight”:

tse-notright-select.png

Press the Add button. Move the mouse cursor into the playfield. Overlap node b and notRight's input node:

tse-notright-overlap.png

Click the left mouse button.

TSE appends notRight to the TS program:

tse-notright-appended.png

Note that the inverters' input and output nodes are in opposite states:

tse-notright-value.png

Set the rendering depth to 2 by adjusting the leftmost spinner on the status bar:

tse-depth-2.png

At that rendering depth, TSE reveals the inner workings of the inverters.

Both input nodes should be three cells wide. Edit them as follows.

tse-three-wide.png

Press the Run button:

tse-three-wide-2.png

A TS program can label its components. Type “not1” immediately right of notLeft. Type “not2” immediately right of notRight:

tse-not1-not2.png

Set the rendering depth back to 1. Press the Run button:

tse-not1-not2-depth-1.png

In the TS program, make sure the cursor is positioned at the end of line 4 or at the start of line 5. Click on the components combo box. Type “or”:

tse-or-select.png

Press the Add button. Move the mouse cursor into the playfield:

tse-or-added.png

TSE depicts or as a dark square. Two unassigned input nodes protrude from its bottom. An unsigned output extends from its top.

The output nodes of the inverters will serve as the input nodes of the OR gate. Overlap the nodes thusly:

tse-or-overlap.png

Click the left mouse button.

TSE appends or to the TS program:

tse-or-appended.png

The OR gate's output node will serve as the output of the NAND gate.

Press Enter. Type “out o -1..0 7”:

tse-output-node.png

Click on the input field. Type “11”. Press the Run button:

tse-input-11.png

Repeat for all possible input values. Verify the output is 0 only when both inputs are 1.

In the JavaScript region, type in the following snippet.

tse-js.png

Select Run > Test Fully or press Shift-F6:

tse-test-fully.png

That causes TSE to compare the NAND gate's output with the value from JavaScript snippet for all possible inputs. They match:

tse-test-passed.png

Click on line 4 of the TS program. Press Ctrl-/ to comment out the line:

tse-comment.png

Press Shift-F6 to test again. With the missing inverter, the test fails:

tse-test-failed.png

Click on line 4 of the TS program. Press Ctrl-/ to uncomment out the line.

Verify the NAND gates works once again.

If a circuit has a large number of inputs, Run > Test Fully may take several minutes to complete. And since TSE parallelizes testing, it consumes 100% CPU during that period.

The alternative, Run > Test Quickly or Ctrl-F6, compares the circuit’s output with the JavaScript for a randomly selected subset of the possible input values. It covers only 10% of the testing space, enabling it to finish 10× faster.

To terminate a long-running test, select Run > Cancel Test or press Alt-F6.

TSE recursively renders a circuit to a specified cutoff depth. At the cutoff, it assigns output nodes by evaluating JavaScript snippets. Meaning, not only does it display less detail, it simulates less detail. Consequentially, the lower the depth, the faster the render.

To make this clear, set the rendering depth to 0:

tse-depth-0.png

TSE assigns the output node by evaluating the JavaScript snippet rather than simulating nandExample's unseen internals.

Set the rendering depth to 1:

tse-depth-1.png

TSE assigns the output nodes of the inverters and the OR gate by evaluating their JavaScript snippets rather than simulating their unseen internals.

Set the rendering depth to 2:

tse-depth-2a.png

At this rendering depth, TSE fully simulates everything. It ignores the JavaScript snippets.

Select File > Save or press Ctrl-S.

TSE saves components as two separate files. In this case, it writes the TS program to code/ts/examples/nandExample.t and the JavaScript snippet to code/ts/examples/nandExample.js.

All files within code/ts, in any subdirectory or otherwise, are part of the components catalog.

That completes the NAND tutorial.

🡄 Previous

Next 🡆