Logic gate diagram
About logic gate diagrams
A logic gate diagram shows how Boolean functions are implemented in hardware — inputs flow left through combinational gates and flip-flops to produce outputs on the right. Digital design engineers use them to document RTL intent, verify gate-level netlists, and teach Boolean algebra. Schematex derives its symbol set from IEEE Std 91-1984 / ANSI Y32.14 (distinctive-shape ANSI symbols, the US default) and IEC 60617-12 (uniform rectangle symbols, the international default), selectable per diagram.
The DSL is functional: you declare signals and describe each gate's inputs by name. Layout and wiring are computed automatically from the dependency graph — no manual coordinates.
1. Your first logic gate diagram
The smallest useful diagram: two inputs, one gate, one output.
Four rules cover 80% of usage:
- Start with the keyword
logic, optionally followed by a quoted title andstyle: ansiorstyle: iec. - Declare ports with
inputandoutputlines — comma-separated signal names. - Each gate is
id = GATE_TYPE(input1, input2, …). Theidbecomes a named signal wire. - An
outputname that matches a gateidis automatically wired; useOUTPUT <- gate_idwhen the names differ.
Comments must start with
#or--on their own line (or after the last token on a gate line).
2. Gate types
2.1 Combinational gates
| DSL keyword | Function | ANSI shape | IEC symbol |
|---|---|---|---|
AND | A · B | D-shaped body | Rectangle + & |
OR | A + B | Curved body | Rectangle + ≥1 |
NOT | Ā | Triangle + bubble | Rectangle + 1 + bubble |
NAND | ¬(A · B) | AND + bubble | Rectangle + & + bubble |
NOR | ¬(A + B) | OR + bubble | Rectangle + ≥1 + bubble |
XOR | A ⊕ B | OR + extra arc | Rectangle + =1 |
XNOR | ¬(A ⊕ B) | XOR + bubble | Rectangle + =1 + bubble |
BUF | A (buffer) | Triangle, no bubble | Rectangle + 1 |
2.2 Special-output buffers
| DSL keyword | Function |
|---|---|
TRISTATE_BUF | Three-state buffer — Z output when enable is low |
TRISTATE_INV | Three-state inverting buffer |
OPEN_DRAIN | Open-drain / open-collector output (external pull-up required) |
SCHMITT | Schmitt trigger — hysteresis symbol inside body |
2.3 Flip-flops and latches
| DSL keyword | Type | Key pins |
|---|---|---|
DFF | D flip-flop (edge-triggered) | D, CLK, Q, Q̄ |
JKFF | JK flip-flop | J, K, CLK, Q, Q̄ |
SRFF | SR flip-flop | S, R, CLK, Q, Q̄ |
TFF | T (toggle) flip-flop | T, CLK, Q, Q̄ |
LATCH_SR | SR latch (level-sensitive, no clock) | S, R, Q, Q̄ |
LATCH_D | D latch (transparent when enable=1) | D, EN, Q, Q̄ |
2.4 Complex combinational
| DSL keyword | Function |
|---|---|
MUX | Multiplexer |
DEMUX | Demultiplexer |
DECODER | Binary decoder |
ENCODER | Priority encoder |
2.5 Sequential complex
| DSL keyword | Function |
|---|---|
COUNTER | Generic binary counter (CTR label, CLK/RESET/Q0–Q3) |
SHIFT_REG | Generic shift register (SRG label, CLK/SER/Q0–Q7) |
3. Inputs and outputs
3.1 Declaring ports
input A, B, Cin # three input ports
output Sum, Cout # two output portsEach name in an input or output list becomes a named signal wire available throughout the diagram.
3.2 Active-low inputs
Prefix a signal name with ~ in the input list to mark it as active-low. The renderer draws a bubble at the port symbol.
input ~nRESET, CLK, DATAActive-low notation also works inside gate input lists:
g1 = AND(~nRESET, CLK)3.3 Wiring outputs to gates
If the output ID matches a gate ID, the connection is implicit:
output Sum # Sum is also a gate id → auto-wired
Sum = XOR(s1, Cin)When the names differ, use the explicit assignment operator:
output F
q1 = NOR(A, B)
F <- q1 # F draws from q1's output4. Symbol style
The style: option on the header line selects the symbol standard. It applies to every gate in the diagram.
| Value | Standard | Use when |
|---|---|---|
ansi (default) | IEEE Std 91 — distinctive curved shapes | US education, hardware docs |
iec | IEC 60617-12 — uniform rectangles + function label | International, European industry |
logic "ALU slice" style: iec5. Module blocks
Use module to group gates into a labeled sub-circuit box. Module blocks are useful for documenting hierarchical designs — each module renders as a named rectangle around its member gates.
logic "Hierarchical adder"
input A, B, Cin
output Sum, Cout
module "Half Adder" {
s1 = XOR(A, B)
c1 = AND(A, B)
}
Sum = XOR(s1, Cin)
Cout = OR(c1, AND(s1, Cin))Module syntax rules:
module "Label" {— opens a module (quoted label or bare identifier). The{must be on the same line.}on its own line closes the most recently opened module.- Modules can be nested.
6. Labels & comments
- Diagram title:
logic "Full Adder"— first line only. - Gate signal names: the
idinid = GATE(…)is both the gate name and the output wire name. - Output labels:
output Sum— the output port label matches the signal name by default. - Active-low marker:
~prefix on a port or gate input. - Comments:
#or--at the start of a line, or after the last meaningful token on a line.
7. Reserved words & escaping
Reserved at line start: logic (header), input, output, module, }.
Reserved operator tokens — avoid these inside signal names: =, (, ), ,, <-, ~.
Signal name rules: must match [a-zA-Z_][a-zA-Z0-9_]*. Lowercase and uppercase are both accepted; gate type keywords (AND, OR, etc.) are case-insensitive in the parser.
8. Common mistakes
| You wrote | Parser says | Fix |
|---|---|---|
f = and(A, B) (lowercase gate) | Accepted — gate types are case-insensitive | Both AND and and work |
output F then F <- q1 but q1 not declared | LogicParseError: Unknown signal "q1" | Declare q1 as a gate before referencing it |
input A B C (spaces, no commas) | Parser takes A only; B and C are ignored | Use commas: input A, B, C |
F = BUFFER(A) | LogicParseError: Unknown gate type: BUFFER | Use BUF |
module FullAdder { (no { brace) | Line does not match module pattern — skipped silently | The opening { is required on the same line as module |
style: IEEE on the header | Unknown style value — silently defaults to ansi | Use style: ansi or style: iec |
9. Grammar (EBNF)
document = header statement*
header = "logic" ( WS quoted-string )? ( WS "style:" WS style )? NEWLINE
style = "ansi" | "iec"
quoted-string = '"' any-char-but-quote* '"'
statement = blank | comment | input-decl | output-decl | gate-def | assign | module-block
comment = ( "#" | "--" ) any NEWLINE
input-decl = "input" WS port-list NEWLINE
output-decl = "output" WS port-list NEWLINE
port-list = port-id ( "," WS? port-id )*
port-id = "~"? id
gate-def = id WS "=" WS gate-type "(" input-list ")" NEWLINE
input-list = ( "~"? id ) ( "," WS? ( "~"? id ) )*
assign = id WS "<-" WS id NEWLINE
module-block = module-open ( statement | module-block )* module-close
module-open = "module" WS ( quoted-string | id ) WS? "{" NEWLINE
module-close = "}" NEWLINE
gate-type = "AND" | "OR" | "NOT" | "NAND" | "NOR" | "XOR" | "XNOR" | "BUF"
| "TRISTATE_BUF" | "TRISTATE_INV" | "OPEN_DRAIN" | "SCHMITT"
| "DFF" | "JKFF" | "SRFF" | "TFF"
| "LATCH_SR" | "LATCH_D"
| "MUX" | "DEMUX" | "DECODER" | "ENCODER"
| "COUNTER" | "SHIFT_REG"
// all case-insensitive
id = [a-zA-Z_] [a-zA-Z0-9_]*Authoritative source: src/diagrams/logic/parser.ts. If this diverges from the parser, the parser wins — please open an issue.
10. Standard compliance
Schematex logic gate diagrams follow IEEE Std 91-1984 / ANSI Y32.14 (distinctive-shape symbols) and IEC 60617-12 (rectangular symbols with function qualifiers).
What is implemented today:
- ✅ All eight combinational gates: AND, OR, NOT, NAND, NOR, XOR, XNOR, BUF
- ✅ Special-output buffers: TRISTATE_BUF, TRISTATE_INV, OPEN_DRAIN, SCHMITT
- ✅ Four edge-triggered flip-flops: DFF, JKFF, SRFF, TFF
- ✅ Two latches: LATCH_SR, LATCH_D
- ✅ Combinational MSI: MUX, DEMUX, DECODER, ENCODER
- ✅ Sequential MSI: COUNTER, SHIFT_REG
- ✅ Active-low (
~) notation on inputs and ports - ✅ ANSI and IEC symbol styles, selectable per diagram
- ✅ Module grouping blocks
- ✅ Automatic DAG layout (topological sort, left-to-right signal flow)
- ⏳ Explicit fan-out wire routing (shared net with junction dot)
- ⏳ Multi-bit bus notation (
/Nslash annotation on wire) - ⏳ Active-low clock inputs on flip-flops
References:
- IEEE Std 91-1984 / ANSI Y32.14: IEEE Standard Graphic Symbols for Logic Functions
- IEEE Std 91a-1991: Supplement to IEEE Std 91
- IEC 60617-12: Graphical symbols for diagrams — binary logic elements
11. Related examples
12. Roadmap
Planned — not yet parseable. Do not use these in generated DSL today; the parser will reject or ignore them.
- Explicit fan-out / junction dot — named wire shared by multiple gate inputs, rendered with a junction dot at the branch point.
- Multi-bit bus notation —
bus Nannotation on a wire to denote an N-bit signal group. - Active-low clock —
~CLKin a flip-flop input list rendering a bubble on the clock triangle. - Feedback arc — explicit wire from a gate output back to an earlier gate input, routed above the main signal path.
- Parameterized gate fan-in —
AND(A, B, C, D)rendering a 4-input AND gate directly.
Track in the GitHub issues if you need any of these sooner.