TechSkills of Future

RTL Design in Verilog: Generate & Parameterized Logic

Advanced RTL Logic & Parametrization

RTL Synthesis & Parameterization

01 Binary ⇌ Gray Conversion Logic

Timing Waveform

binary[3:0] 4’h24’h7 gray[3:0] 4’h3 4’h4

Gray to Binary Logic Circuit

G[3] G[2] G[1] B[3] B[2] B[1]

Gray to Binary (Loop Method)

module gray_to_binary #(parameter N=4) (
    input [N-1:0] gray,
    output reg [N-1:0] binary
);
    integer i;
    always @(*) begin
        binary[N-1] = gray[N-1];
        for (i = N-2; i >= 0; i = i - 1)
            binary[i] = binary[i+1] ^ gray[i];
    end
endmodule

Binary to Gray (Shift Method)

module binary_to_gray #(parameter N=4) (
    input [N-1:0] binary,
    output [N-1:0] gray
);
    assign gray = binary ^ (binary >> 1);
endmodule
DecimalBinaryGray
000000000
200100011
501010111
701110100

02 Binary to Two’s Complement

Timing Waveform

Input A 4’h64’h1 Output Y 4’hA4’hF

Operation: $\text{Result} = \text{NOT}(\text{A}) + 1$. This converts a positive magnitude to its negative equivalent in signed systems.

Structural Logic

Inverter Adder Unit 2’s Output
Truth Table (3-bit)
In: 001 (1)
In: 010 (2)
In: 100 (4)
Out: 111 (-1)
Out: 110 (-2)
Out: 100 (-4)
module twos_complement #(parameter N=4) (
    input [N-1:0] a,
    output [N-1:0] y
);
    // Method 1: Mathematical
    assign y = (~a) + 1'b1;

    /* Method 2: Behavioral 
    always @(*) begin
        y = (~a) + 1;
    end */
endmodule

03 BCD to 7-Segment Decoder

Timing Waveform

BCD 057 Seg[6:0] 7’h7E7’h5B7’h70

Physical Segment Map

a b c d e f g

Simplified SOP

$a = \sum m(0, 2, 3, 5, 6, 7, 8, 9)$
$b = \sum m(0, 1, 2, 3, 4, 7, 8, 9)$
$e = \sum m(0, 2, 6, 8)$

DigitBCDa b c d e f g
000001 1 1 1 1 1 0
300111 1 1 1 0 0 1
701111 1 1 0 0 0 0
module bcd_to_7seg(
    input [3:0] bcd,
    output reg [6:0] seg // {a,b,c,d,e,f,g}
);
    always @(*) begin
        case(bcd)
            4'h0: seg = 7'b1111110;
            4'h1: seg = 7'b0110000;
            4'h2: seg = 7'b1101101;
            4'h3: seg = 7'b1111001;
            4'h4: seg = 7'b0110011;
            4'h5: seg = 7'b1011011;
            4'h6: seg = 7'b1011111;
            4'h7: seg = 7'b1110000;
            4'h8: seg = 7'b1111111;
            4'h9: seg = 7'b1111011;
            default: seg = 7'b0000000;
        endcase
    end
endmodule

Advanced: Parameterized Ripple Carry Adder

Timing Waveform

A[WIDTH-1:0] B[WIDTH-1:0] Sum[WIDTH-1:0]

Logic Schematic (Slice)

FULL ADDER [i] A[i] B[i] C_in[i] C_out[i] Sum[i]

Verilog Hardware Generation

module ripple_carry_adder #(parameter WIDTH = 8) (
    input [WIDTH-1:0] a, b,
    input cin,
    output [WIDTH-1:0] sum,
    output cout
);
    wire [WIDTH:0] carry;
    assign carry[0] = cin;
    assign cout = carry[WIDTH];

    genvar i;
    generate
        for (i=0; i < WIDTH; i=i+1) begin : fa_inst
            full_adder fa (
                .a(a[i]), .b(b[i]), .ci(carry[i]), 
                .s(sum[i]), .co(carry[i+1])
            );
        end
    endgenerate
endmodule

Verification & Timing

  • For N=32, propagation delay involves 32 stages.
  • Best for low-speed, area-constrained designs.
ABCinSumCout
10100110000001
11110001000001

05 BCD to Excess-3 Logic

Timing Waveform

BCD In 4'h04'h4 Excess-3 Out 4'h34'h7

Verilog Design

module bcd_to_excess3(
    input [3:0] bcd,
    output [3:0] excess3
);
    // Continuous assignment for simple arithmetic
    assign excess3 = bcd + 4'b0011;
endmodule

Truth Table

BCD (In) -> XS3 (Out)
0000 -> 0011
0100 -> 0111
1001 -> 1100

Logic Diagram (Gate Level)

BCD[3:0] Adder Unit Fixed: 0011 XS-3

Functional Verification Results

Consolidated Simulation Output

STATUS: PASSED
GRAY CONV

T=10ns | B=0110 -> G=0101
T=20ns | G=1100 -> B=1011
T=30ns | B=1010 -> G=1111
7-SEGMENT

BCD: 0 -> SEG: 1111110
BCD: 5 -> SEG: 1011011
BCD: 9 -> SEG: 1111011
PARAMETER RCA

W=4 | A=F, B=1 -> S=0, C=1
W=8 | A=AA, B=55 -> S=FF, C=0
EXCESS-3

IN: 0000 -> OUT: 0011
IN: 1000 -> OUT: 1011
IN: 0101 -> OUT: 1000

Leave a Comment

Your email address will not be published. Required fields are marked *