The Shift From Thinking in Boolean Algebra to HDL
What HDL chip design taught me about hierarchical thinking and layers

While completing ICs in the curriculums “Building a Modern Computer from first principles” / Nand2Tetris I by Nisan and Schocken, I found progressive difficulty in trying to apply the typical boolean logic workflow, that I learned from a previous curriculum centered on finite state machines (map it as a truth table, derive boolean expressions and map them), to every IC that I had to design. While simple ICs like composite logic gates, multiplexer, demultiplexer, adders… and small multi bit variants were very straightforward to design with boolean expressions, examples that forced me to think differently, simply because of their scales and use a new abstraction altogether for design include, ALU, RAM8-64-512-4k-16k, program counter, memory chip, CPU. It simply is not practical to use boolean algebra here and convolutes the IC as a massive conglomorate of boolean expressions, and the process of deriving the expressions is even more convoluted with large page sized truth tables.
While technically the IC is always described by boolean expressions at a mathematical level, it does not mean that it should be used for design at scale where we begin to have the room for abstraction. Practically it should always be thought of smaller ICs that we already have designed (likely with boolean expressions) and how they can be wired together with combinational logic to affect memory or the output the way we want it to. This is an abstraction where we are no longer thinking in terms of discrete states, inputs and outputs at a time but rather how we can move past that layer, keeping it in mind, to a less litteral “creating from scratch” methodology and instead on focusing on combing the already existing shapes and forms (just as software engineers may be taught to always break a problem into smaller fragments, not just because it usually leads to cleaner code, especially for teams, but for its praticality in scale); this is precisely how hardware description languages are used and is what real designers use, their process involving designing the complex IC hierarchaly with an HDL like VHDL or Verilog - the reason we use HDL is because it’s the most practical way of decribing logic circuits in production, as it’s text based - and testing it against desired inputs and outputs in a test script. Boolean expressions technically always remain as the correct, and truly only, underylying model of description for a logic circuit, but it shouldn’t be used as the design method at scale which is where hierarchal abstractions come in, applied through an HDL.
- Original workflow and its problem (ALU)
- The Shift
- The theoretical floor and conclusion
- Other examples of hierarchal thinking across ICs in HDL
Original workflow and its problem (ALU)
An Arithmetic Logic Unit is a combinational IC that does an arithmetic or logical operation on two input numbers and that operation changes depending on control bits set. In this case the ALU has to be 16 bit, use 2’s compliment and support negation, NOT, AND, OR, addition, subtraction, incrementation and decrementation (some of which are implemented indirectly). It is also supposed to output 2 status bits (flags) of the result, if it is negative or zero which is used for conditions and comparisons outside of the scope of the ALU.
My original work consisted of a large truth table that was going to have all possible inputs and outputs for the Arithmetic Logic Unit: 16 bit input for x number, 16 bit input for y number, 6 inputs for the control bits, 2 outputs for status bits and 16 outputs for f(x, y); same method as how I would design a MUX for instance. The difference is that rather than 3 inputs and 1 output requiring a single boolean expression for a 1 bit MUX, this method would require 18 boolean expressions of 38 inputs each for the ALU. It is mathematically possible to use this method, but it would be incredibly time consuming and real designers don’t use it. Upon noticing the scale, I stopped and looked for the alternative in designing.
The Shift
The shift come from using the previous ICs we have designed and combining them in a way that replicates the behaviour of their truth tables. To achieve this, I found there are 3 fundamental design principles:
- Use small ICs as your “parts” where we only care about the input and output of it; IC parts are proportional to the scale of what you are trying to design. For instance, when designing an ALU, adders, AND gates… are used as “parts” but when designing a CPU, the ALU is a part.
- Precompute all operations regardless of control bits. In hardware there is no such thing as conditional skip or compute something only if the control bit is on, logic can only decide what result is used at this level. This differs from traditional algorithmic methodology used in programming.
- MUXes are logical glue for deciding what results are used.
Hardware Description Languages (HDL) are used for describing these relationships formally. Despite not being programming languages (they are markup languages), chips in HDL can be thought of as functions and MUXes can be thought of as conditionals design wise.
To exemplify my theory, see this design of an ALU in HDL:
CHIP ALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
nx, // negate the x input?
zy, // zero the y input?
ny, // negate the y input?
f, // compute (out = x + y) or (out = x & y)?
no; // negate the out output?
OUT
out[16], // 16-bit output
zr, // if (out == 0) equals 1, else 0
ng; // if (out < 0) equals 1, else 0
PARTS:
//zero x
Mux16(a=x, b=false, sel=zx , out=xz);
//negate x
Not16(in=xz, out=xzxn);
Mux16(a=xz, b=xzxn, sel=nx, out=xn);
//zero y
Mux16(a=y, b=false, sel=zy, out=yz);
//negate y
Not16(in=yz, out=yzyn);
Mux16(a=yz, b=yzyn, sel=ny, out=yn);
//add or logical AND
Add16(a=xn, b=yn, out=sum);
And16(a=xn, b=yn, out=dan);
Mux16(a=dan, b=sum, sel=f, out=comp);
//negate out and ng status
Not16(in=comp, out=compnot);
Mux16(a=comp, b=compnot, sel=no, out=out, out[15]=ng, out[0..7]=l1, out[8..15]=l2);
//zr status
Or8Way(in=l1, out=l1c);
Or8Way(in=l2, out=l2c);
Or(a=l1c, b=l2c, out=is);
Not(in=is, out=zr);
}
Multiplexers are continuously used in a chain for deciding what path to continue on across the logic gates.
The theoretical floor and conclusion
The main takeaway is that abstraction has no ceiling: as scale increases, abstraction increases with it. Logic gates compose into the ALU; the ALU, registers, and program counter compose into the CPU; memory cells compose into RAM units, and those into a memory system/unit; the CPU, memory, and I/O compose into a von Neumann computer; CPU instructions (machine language) stored in RAM and used by the CPU, assembly for writing CPU instructions, and continuing through the abstract domains of software. Downward, however, I believe there is a floor in the mathematics of Boolean logic because there is simply no room left to abstract with chips. Everything is fundamentally built from logical AND, OR, and NOT. It’s worth distinguishing that there is a difference in logical floor of boolean logic (which is AND, OR, NOT, the fundamental gates) and the functional/practical floor of NAND and NOR, the universal gates used for building all other gates including AND, OR, NOT in production since they are cheapest. So it can be argued that the fundamental logic gates are abstractions of NAND and NOR but this is a flawed point because if we only look from a practical perspective, it can be argued the universal gates are abstractions of transistors, which are abstractions of electronics, which are an abstraction of electricity. All that truly matters is the logical floor and that is the mathematical AND, OR and NOT that we use to build up until we have room to abstract and then again.
Other examples of hierarchal thinking across ICs in HDL:
Program Counter:
CHIP PC {
IN in[16], reset, load, inc;
OUT out[16];
PARTS:
Inc16(in=regg , out=icc);
Mux16(a=in, b=icc , sel=inc , out=mux1 );
Mux16(a=mux1, b=in , sel=load , out=mux2 );
Mux16(a=mux2 , b=false , sel=reset , out=mux3);
Register(in=mux3 , load=true , out=regg, out=out );
}
CPU:
CHIP CPU {
IN inM[16], // M value input (M = contents of RAM[A])
instruction[16], // Instruction for execution
reset; // Signals whether to re-start the current
// program (reset==1) or continue executing
// the current program (reset==0).
OUT outM[16], // M value output
writeM, // Write to M?
addressM[15], // Address in data memory (of M)
pc[15]; // address of next instruction
PARTS:
//15 is a or c instruction, 14-13 reserved,
//12 a (type of instruction c alu comp),
//11-6 computation alu control bits,
//5-3 dest: 5 A, 4 D, 3 M regs
//2-0 jump: use 2 status bits to find the exclusive
//possibility, and then use comb. logic to associate
//to conditions that 2-0 encodes that will enable
//load in pc if true. [0] > 0; [1] == 0; [2] < 0
//[control
Mux(a=false , b=true , sel=instruction[3] , out=check );
And(a=check, b=instruction[15], out=writeM);
Not(in=instruction[15], out=isa);
Or(a=isa , b=instruction[5] , out=amuxc);
Not(in=zr, out=notzr);
Not(in=ng, out=notng);
And(a=notzr, b=notng, out=gz);
And(a=instruction[0], b=gz, out=q0);
And(a=instruction[1], b=zr, out=q1);
And(a=instruction[2], b=ng, out=q2);
Or(a=q0 , b=q1 , out=w1 );
Or(a=w1 , b=q2 , out=w2 );
And(a=w2 , b=instruction[15], out=jumpornot);
And(a=instruction[4], b=instruction[15], out=dregc);
//control]
Mux16(a=instruction, b=alucomp, sel=instruction[15] , out=amux);
ARegister(in=amux, load=amuxc, out=areg, out[0..14]=addressM);
Mux16(a=areg , b=inM , sel=instruction[12] , out=mux2 );
PC(in=areg , load=jumpornot , inc=true , reset=reset , out[0..14]=pc);
ALU(x=dreg , y=mux2 , zx=instruction[11] , nx=instruction[10] ,
zy=instruction[9] , ny=instruction[8] , f=instruction[7] ,
no=instruction[6] , out=alucomp, out=outM , zr=zr , ng=ng );
DRegister(in=alucomp , load=dregc , out=dreg );
}
RAM8:
CHIP RAM8 {
IN in[16], load, address[3];
OUT out[16];
PARTS:
DMux8Way(in=load, sel=address , a=a, b=b , c=c , d=d , e=e , f=f , g=g , h=h );
Register(in=in, load=a, out=aa);
Register(in=in, load=b, out=bb);
Register(in=in, load=c, out=cc);
Register(in=in, load=d, out=dd);
Register(in=in, load=e, out=ee);
Register(in=in, load=f, out=ff);
Register(in=in, load=g, out=gg);
Register(in=in, load=h, out=hh);
Mux8Way16(a=aa , b=bb , c=cc , d=dd , e=ee , f=ff , g=gg , h=hh , sel=address, out=out);
}
RAM64:
CHIP RAM64 {
IN in[16], load, address[6];
OUT out[16];
PARTS:
DMux8Way(in=load, sel=address[0..2], a=a, b=b , c=c , d=d , e=e , f=f , g=g , h=h );
RAM8(in=in, load=a, address=address[3..5] , out=aa );
RAM8(in=in, load=b, address=address[3..5] , out=bb );
RAM8(in=in, load=c , address=address[3..5] , out=cc );
RAM8(in=in, load=d , address=address[3..5] , out=dd );
RAM8(in=in, load=e , address=address[3..5] , out=ee );
RAM8(in=in, load=f , address=address[3..5] , out=ff );
RAM8(in=in, load=g , address=address[3..5] , out=gg );
RAM8(in=in, load=h , address=address[3..5] , out=hh );
Mux8Way16(a=aa , b=bb , c=cc , d=dd , e=ee , f=ff , g=gg , h=hh , sel=address[0..2] , out=out );
}
Memory unit:
CHIP Memory {
IN in[16], load, address[15];
OUT out[16];
PARTS:
Not(in=address[14], out=ram);
And(a=ram, b=load, out=loadram);
Not(in=address[13], out=nota);
And(a=address[14], b=nota, out=screen);
And(a=screen, b=load, out=loadscreen);
RAM16K(in=in, load=loadram, address=address[0..13], out=ramo);
Screen(in=in, load=loadscreen, address=address[0..12], out=screeno);
Keyboard(out=keyo);
Mux16(a=ramo, b=screeno, sel=address[14], out=ramscreen);
And(a=address[14], b=address[13], out=keysel);
Mux16(a=ramscreen, b=keyo, sel=keysel, out=out);
}
Computer:
CHIP Computer {
IN reset;
PARTS:
ROM32K(address=pc, out=instruction);
Memory(in=outM, load=writeM, address=addressM, out=inM);
CPU(
inM=inM,
instruction=instruction,
reset=reset,
outM=outM,
writeM=writeM,
addressM=addressM,
pc=pc
);
}