← Back to Dunce

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:

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.

OperatorWordSymbolIs 1 when
NotNOT A!AA is 0
AndA AND BA * Bboth are 1
OrA OR BA + Beither is 1
Rising edgeR_TRIG AA 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:

Read the latch's value by its name, LT0.

LT0S := START;
LT0R := STOP;
MOTOR := LT0;

How latches behave:

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

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);

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);

Good to know

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

MessageWhat it usually means
Unmatched ParenthesisA ( without its ), or the other way round.
Unexpected InputSomething in an expression doesn't fit: two names in a row, an operator missing its operand, or a stray character.
Multiple Output ExpressionsMore than one statement without :=. Name all but one of them.
Output Already DefinedThe same name is given := twice.
Invalid Output NameThe name before := is a reserved word or contains other characters.
Latch Missing SET or RESETA latch has LTnS without LTnR, or the reverse.
Latch SET Already DefinedTwo LTnS statements for the same latch (or two LTnR).
Module Has No OUTA plain call to a body without OUT. Name an output, as in MODULE.SUM(...).
Unknown Module Output AliasMODULE.NAME(...) asks for a name the module's body doesn't define.
Module Cycle DetectedA module calls itself, directly or through other modules.
Module Already DefinedTwo modules with the same name.
Single-line mode …The text uses something only Free form allows. Switch to Free form.

Quick reference

OperatorsNOT !   AND *   OR +   R_TRIG   ( )
OrderNOT and R_TRIG, then AND, then OR
OutputNAME := expression;
Unnamed outputexpression; (one only, shown as OUT)
LabelNAME := expression @"label";
Comment// text or # text
LatchLT0S := set; LT0R := reset; read as LT0; or LATCH(LT0, set, reset);
EdgeR_TRIG A: 1 on the step where A rises
SettingsSETTINGS { NAME := 1; OTHER := 0; }
One-line moduleMODULE NAME(P1, P2) := expression;
Module with bodyMODULE NAME(P1, P2) { LOCAL := ...; OUT := ...; }
CallsNAME(arg1, arg2) for OUT; NAME.OUTPUT(arg1, arg2) for another output
Region// ==== REGION: name ==== … // ==== END REGION ====