← Back to Index
Research & Engineering Archive

DS 2. Circuit Module & Verilog

By Jingnan Huang · October 05, 2025 · 1204 Words

Last Edit: 10/5/25

NAND and NOR Logic Networks
#

NAND
#

image.png

NOR
#

image.png

在实际制造中:NAND 和 NOR 门的晶体管布线最简单,这两个也是现实唯二的 Logic Gate

Multiplexer
#

image.png

image.png

Verilog Code
#

A sample Verilog Structure can be the following

module Name (ports);
    specify input/output ports
    specify signal name for internal use
    assign values to outputs & signals
endmodule

Multiplexer in Verilog
#

A Multiplexer can be implement as

module mux2to1 (x, y, s, f);
    input x, y, s;
    output f;
    assign f = (~s & x) | (s & y);
endmodule

Although x,y,s,f are variable names, in FGPA they don’t actually exist, so those are just random variable so far

FGPA
#

FGPA, Fidd-Prog Gate Array

Multiplexer (Two to one bit)
#

To implement FGPA on the FGPA, can use a Verilog Code as

module mux2to1 (SW, LEDR);
		//Specift in/outputs
    input  [9:0] SW;       
    output [9:0] LEDR;     
		//The intermediate Variables
    wire s, x, y, f;       
		//Assign
    assign s = SW[9];     
    assign x = SW[0];     
    assign y = SW[1];    
    assign f = (~s & x) | (s & y);  
    assign LEDR[0] = f;    
endmodule

Multiplexer (Two to two bits)
#

A two to two bits multiplexer basically retrieve two digit input for x and y, then provide two bits output for F

module mux2to1_2bit (X, Y, S, F);
    input S;
    input [1:0] X, Y;
    output [1:0] F;

    assign F[0] = (~S & X[0]) | (S & Y[0]);
    assign F[1] = (~S & X[1]) | (S & Y[1]);
endmodule

Hierarchical Verilog Code
#

Verilog Code can be assigned as modules that look like functions, one module can call others for simplification

Now define a subcircuit call a 7-Segment Decoder, what is does is to get input x,x_0 and represent them as a 2 bit number

    h0
   ----
h5|    |h1
  | h6 |
   ----
h4|    |h2
   ----
    h3

image.png

image.png

h0 = ~x1 & x0
h1 = 0
h2 = x1 & ~x0
h3 = ~x1 & x0
h4 = x0
h5 = x0 | x1
h6 = ~x1

Data Structure
#

In Verilog, sometimes we are cooping with decimal, sometimes binary, sometimes hex, the way to declare which one is using is by three digits

Top Level Module
#

Top Level Module is like a main file that connect everything

In this case, we created two two digit inputs, and use a multiplexer to choose one to display on HEX by 7 Segment Decoder, so its a circuit look like

image.png

module hier (SW, HEX0);
    input  [9:0] SW;
    output [6:0] HEX0;

    wire [1:0] F;

    mux2to1_2bit U1 (SW[1:0], SW[3:2], SW[9], F);
    seg7         U2 (F, HEX0);
endmodule

Half Adder
#

Half Adder is a circuit that can add two 1 bit input

a b Output S₁S₀
0 0 00
0 1 01
1 0 01
1 1 10

Three input XOR
#

Take three inputs and calculate their XOR

$$ f=a⊕b⊕c $$

c a b (a⊕b) f = a⊕b⊕c
0 0 0 0 0
0 0 1 1 1
0 1 0 1 1
0 1 1 0 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 0 1
module FA (a, b, cin, s, cout);
    input a, b, cin;
    output s, cout;

    assign s = a ^ b ^ cin;               // sum
    assign cout = (a & b) | (a & cin) | (b & cin);  // carry out
endmodule
module adder3 (A, B, Cin, S, Cout);
    input  [2:0] A, B;
    input  Cin;
    output [2:0] S;
    output Cout;

    wire C1, C2;

    FA U1 (A[0], B[0], Cin, S[0], C1);
    FA U2 (A[1], B[1], C1, S[1], C2);
    FA U3 (A[2], B[2], C2, S[2], Cout);
endmodule
   Cin  [FA0]  C1  [FA1]  C2  [FA2]  Cout
                                 
            S0         S1         S2