Dunce syntax guide
How to write logic in Dunce: expressions, outputs, latches, settings and modules, with examples you can paste into the editor.
Getting started
Dunce has two ways to enter logic, chosen with the buttons at the top left:
- Single line: one expression, such as
A AND NOT B. Good for quick truth tables. - Free form: a whole program of statements: named outputs, latches, settings and modules.
Press Evaluate to build the outputs you have turned on: the Truth table, the Diagram and the Board.
Everything is written in capital letters. The editor turns what you type into capitals, so a and b becomes A AND B.
Names and inputs
A name is made of capital letters, digits and underscores: START, MOTOR_1, Q0.
Any name that your logic reads but never defines is an input. Inputs become columns of the truth table and switches in the diagram and on the board. You don't declare them anywhere.
LAMP := DOOR_OPEN AND NOT DAYLIGHT; // DOOR_OPEN and DAYLIGHT are inputs
Some words are taken by the language and can't be used as names: AND, OR, NOT, R_TRIG, LATCH, MODULE and SETTINGS. Names of the form LT + digits (LT0, LT12) are latches; see Latches.
Operators
Each operator has a word form and a symbol form. They mean the same thing, and you can mix them.
| Operator | Word | Symbol | Is 1 when |
|---|---|---|---|
| Not | NOT A | !A | A is 0 |
| And | A AND B | A * B | both are 1 |
| Or | A OR B | A + B | either is 1 |
| Rising edge | R_TRIG A | A has just changed from 0 to 1 (see R_TRIG) |
Order of operations: NOT and R_TRIG first, then AND, then OR. Use parentheses to change the grouping.
A OR B AND C // means A OR (B AND C)
(A OR B) AND C // parentheses change the grouping
NOT A AND B // means (NOT A) AND B
NOT (A AND B) // NAND
There is no built-in XOR, NAND or NOR. Write them out, or make them into modules:
(A OR B) AND NOT (A AND B) // XOR: one or the other, not both
Single line
In Single line mode you type one expression and nothing else. The truth table gets a column for each input and one for the expression.
(A OR B) AND NOT C
Single line mode doesn't accept :=, ;, braces, commas, or the words MODULE, SETTINGS and LATCH. For any of those, switch to Free form.
Free form
A free-form program is a list of statements, each ending with a semicolon ;. A statement can be spread over several lines, and line breaks and spaces don't matter.
Outputs
Give an expression a name with := to make an output:
SUM := (A OR B) AND NOT (A AND B);
CARRY := A AND B;
Outputs can use other outputs, and the order you write them in doesn't matter:
ALARM := ARMED AND TRIPPED;
TRIPPED := DOOR OR WINDOW; // defined after it is used: fine
Each output can be defined only once. You may also write one expression without a name; it becomes the output OUT. A second unnamed expression is an error.
X := A AND B;
X OR C; // the unnamed output, shown as OUT
Comments
// or # starts a comment that runs to the end of the line. Comments can sit on their own line or after a statement.
// Seal-in circuit for a motor
RUN := (START OR RUN) AND NOT STOP; # this works too
Labels
Put @"text" at the end of an output's expression to give it a readable label in the diagram:
MOTOR := RUN AND NOT FAULT @"Main pump motor";
Latches
A latch remembers a 1 or a 0. Each latch has a name like LT0, LT1, LT25 (LT plus digits), and two statements:
LT0S := ...;is the set condition. When it is 1, the latch becomes 1.LT0R := ...;is the reset condition. When it is 1, the latch becomes 0.
Read the latch's value by its name, LT0.
LT0S := START;
LT0R := STOP;
MOTOR := LT0;
How latches behave:
- Every latch starts at 0.
- Latches change only when you press Step (or Run, which steps over and over). All latches step at the same moment, using the values from just before the step.
- If set and reset are both 1, reset wins and the latch becomes 0.
- If both are 0, the latch keeps its value.
- Every latch needs both a set and a reset statement.
You can also write a latch in one statement, LATCH(name, set, reset):
LATCH(LT3, GO, HALT);
RUNNING := LT3;
In the truth table, a latch has two columns: LT0, the value it has now, and LT0+, the value it will have after the next Step.
Watch the name. Only LT + digits makes a latch (PLT and ALT + digits work too). LTXS := A; is just an ordinary output named LTXS, with no error to warn you.
Outputs that feed back
An output may read its own value. This is how a relay holds itself on (a seal-in):
RUN := (START OR RUN) AND NOT STOP;
Once START turns RUN on, RUN keeps itself on until STOP is pressed. The diagram shows this as a loop. The truth table treats such an output like a latch, with a RUN column for its present value and RUN+ for its next value.
Rising edges: R_TRIG
R_TRIG A is 1 for a single step: the step where A changes from 0 to 1. Holding A at 1 gives just that one pulse. Use it to make a pushbutton act once per press:
PRESS := R_TRIG PB;
LT0S := PRESS AND NOT LT0; // off now? turn on
LT0R := PRESS AND LT0; // on now? turn off
LIGHT := LT0;
The edge depends on what happened at the previous step, which a truth table can't show. In the truth table, R_TRIG A is shown as plain A. To see edges at work, use Step in the diagram or on the board.
SETTINGS
A SETTINGS block gives names fixed values of 0 or 1:
SETTINGS {
MODE := 1;
INVERT := 0;
}
OUT := (A AND MODE) OR (B AND NOT MODE);
A setting is like an input with a value you choose in the text. The truth table uses its value instead of giving it a column, which keeps tables small. The diagram draws a setting as a switch on the pins that read it, and the board puts the settings in a SETTINGS box. Names that form a grid, like a program ROM's P0_OP2 … P15_A0, become rows of small switches, one row per address, with the column names along the top. Flipping one of these switches changes the running value, for trying things out. Reset puts back the value written in the text. A setting's name can't also be used for an output, latch or module.
Modules
A module is a piece of logic you define once and use many times, like a chip you can place again and again. You give it parameters (its input pins), and each call connects those parameters to signals of your choosing.
One-line modules
The simplest module is one expression:
MODULE XOR(X, Y) := (X OR Y) AND NOT (X AND Y);
SUM := XOR(A, B);
ODD := XOR(XOR(A, B), C); // calls can go inside calls
- The module name comes after
MODULE. It starts with a letter. - The parameters are listed in parentheses, separated by commas.
- In a call, each argument can be any expression. The first argument goes to the first parameter, and so on.
- Parameter names belong to the module.
XinsideXORhas nothing to do with any signal calledXoutside it.
A module without parameters is written and used without parentheses:
MODULE ALWAYS_ON := A OR NOT A;
LAMP := ALWAYS_ON AND SWITCH;
Modules with a body
For more than one expression, give the module a body in braces { }. Inside, write statements just as at the top level, each ending with ;. Names you define inside are local: they help build the result and aren't seen outside.
OUT is the module's main output, the value a plain call gives you:
MODULE MAJORITY(A, B, C) {
AB := A AND B;
AC := A AND C;
BC := B AND C;
OUT := AB OR AC OR BC;
}
VOTE := MAJORITY(S1, S2, S3);
Several outputs
Every name defined in a body is an output you can ask for with a dot: MODULE.OUTPUT(arguments). A full adder has two:
MODULE FULL_ADD(A, B, CIN) {
SUM := (A AND NOT B AND NOT CIN) OR (NOT A AND B AND NOT CIN) OR
(NOT A AND NOT B AND CIN) OR (A AND B AND CIN);
COUT := (A AND B) OR (A AND CIN) OR (B AND CIN);
}
S0 := FULL_ADD.SUM(A0, B0, CIN);
C0 := FULL_ADD.COUT(A0, B0, CIN);
S1 := FULL_ADD.SUM(A1, B1, C0);
C1 := FULL_ADD.COUT(A1, B1, C0);
- Calls to the same module with the same arguments are the same copy of the module. Above,
FULL_ADD.SUM(A0, B0, CIN)andFULL_ADD.COUT(A0, B0, CIN)are one adder with two outputs, and the diagram draws it as one box. - Calls with different arguments are separate copies: the bit 0 adder and the bit 1 adder.
OUTis optional. A body withoutOUTmust always be called with an output named, as inFULL_ADD.SUM(...). A plainFULL_ADD(...)then gives the error "Module Has No OUT".
Latches in modules
A body can contain latches, written exactly as at the top level. Each copy of the module gets its own latches, so you can build a memory element once and use it many times.
A T flip-flop flips its output at every Step while T is 1:
MODULE T_FLIP_FLOP(T) {
LT01S := T AND NOT LT01; // T on and the latch off: set it
LT01R := T AND LT01; // T on and the latch on: reset it
Q_OUT := LT01;
QN_OUT := NOT LT01;
}
Q := T_FLIP_FLOP.Q_OUT(T);
QN := T_FLIP_FLOP.QN_OUT(T); // same copy: the same latch
Chain copies together and you have a counter. Each bit flips when every bit below it is 1:
Q0 := T_FLIP_FLOP.Q_OUT(EN);
Q1 := T_FLIP_FLOP.Q_OUT(EN AND Q0);
Q2 := T_FLIP_FLOP.Q_OUT(EN AND Q0 AND Q1);
The truth table names a module's latch after the module: T_FLIP_FLOP.LT01, with T_FLIP_FLOP.LT01+ for its next value. When there are several copies, they are numbered: T_FLIP_FLOP#1.LT01, T_FLIP_FLOP#2.LT01 and so on. A latch in a module inside another module gets both names, as in COUNTER.T_FLIP_FLOP.LT01.
The latch names inside a module (LT01 above) belong to the module. They don't clash with LT01 at the top level or in other modules.
Modules using modules
A module can call other modules, in a one-line module or in a body:
MODULE NAND(X, Y) := NOT (X AND Y);
MODULE INV(X) := NAND(X, X);
MODULE AND2(X, Y) := INV(NAND(X, Y));
MODULE OR2(X, Y) := NAND(INV(X), INV(Y));
Q := OR2(AND2(A, B), C);
- Modules can be defined anywhere in the program, before or after the places that use them.
- A module can't call itself, directly or through other modules. Doing so is an error ("Module Cycle Detected").
- Each module name can be defined only once, and can't be the same as an output, latch or setting.
Good to know
- Pass signals in as parameters. Inside a body, a name that isn't a parameter, a local or a latch refers to the top-level signal with that name. That works, and the diagram shows it as an extra pin in italics, but the truth table then treats that signal as a plain input. Passing it as a parameter is clearer.
- Missing arguments. If a call leaves off arguments at the end, those parameters read the top-level signals with the parameter's name. Giving every argument avoids surprises.
- Statement endings in a body. The semicolon after the last statement in a body is optional, and so is the one after the closing brace.
- In the diagram, a module is a box with its parameters on the left and its outputs on the right. Click its title (or use Open modules) to see inside.
Diagram and board lines
Some comment lines carry instructions for the diagram and the board. They don't change the logic.
Regions
Wrap related statements in region lines, and the diagram draws them inside a labeled box. Regions can hold SUBREGIONs.
// ==== REGION: Program counter ====
PC0 := T_FLIP_FLOP.Q_OUT(STEP_EN);
PC1 := T_FLIP_FLOP.Q_OUT(STEP_EN AND PC0);
// ==== END REGION ====
Board lines
Lines that start with // ==== BOARD: hold the layout of the board: where each switch, lamp and number display sits. The board writes them for you when you press Save to logic. You can leave them alone; deleting them just resets the board to its automatic layout.
Error messages
| Message | What it usually means |
|---|---|
| Unmatched Parenthesis | A ( without its ), or the other way round. |
| Unexpected Input | Something in an expression doesn't fit: two names in a row, an operator missing its operand, or a stray character. |
| Multiple Output Expressions | More than one statement without :=. Name all but one of them. |
| Output Already Defined | The same name is given := twice. |
| Invalid Output Name | The name before := is a reserved word or contains other characters. |
| Latch Missing SET or RESET | A latch has LTnS without LTnR, or the reverse. |
| Latch SET Already Defined | Two LTnS statements for the same latch (or two LTnR). |
| Module Has No OUT | A plain call to a body without OUT. Name an output, as in MODULE.SUM(...). |
| Unknown Module Output Alias | MODULE.NAME(...) asks for a name the module's body doesn't define. |
| Module Cycle Detected | A module calls itself, directly or through other modules. |
| Module Already Defined | Two modules with the same name. |
| Single-line mode … | The text uses something only Free form allows. Switch to Free form. |
Quick reference
| Operators | NOT ! AND * OR + R_TRIG ( ) |
|---|---|
| Order | NOT and R_TRIG, then AND, then OR |
| Output | NAME := expression; |
| Unnamed output | expression; (one only, shown as OUT) |
| Label | NAME := expression @"label"; |
| Comment | // text or # text |
| Latch | LT0S := set; LT0R := reset; read as LT0; or LATCH(LT0, set, reset); |
| Edge | R_TRIG A: 1 on the step where A rises |
| Settings | SETTINGS { NAME := 1; OTHER := 0; } |
| One-line module | MODULE NAME(P1, P2) := expression; |
| Module with body | MODULE NAME(P1, P2) { LOCAL := ...; OUT := ...; } |
| Calls | NAME(arg1, arg2) for OUT; NAME.OUTPUT(arg1, arg2) for another output |
| Region | // ==== REGION: name ==== … // ==== END REGION ==== |