跳轉到

Verification: Reading Simulations

Finding bugs in code

Mux

原題:Mux

Debug 題型的重點是先對照 interface 與波形。本題需要把 output 改成 8 bit,並可用三元運算子簡化 mux。

Verilog
module top_module (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output [7:0] out
);
    assign out = sel ? a : b;
endmodule

NAND

原題:NAND

NAND 可以由 AND 後接 NOT 完成。使用 submodule 時,port mapping 要嘛照順序,要嘛明確用名稱連接。

Verilog
module top_module (
    input a,
    input b,
    input c,
    output out
);
    wire out_inv;

    andgate inst1 (
        .out(out_inv),
        .a(a),
        .b(b),
        .c(c),
        .d(1'b1),
        .e(1'b1)
    );

    assign out = ~out_inv;
endmodule

Mux(2)

原題:Mux(2)

問題在於 wire 沒宣告成 vector、sel 連接錯誤,以及變數重複命名。

Verilog
module top_module (
    input [1:0] sel,
    input [7:0] a,
    input [7:0] b,
    input [7:0] c,
    input [7:0] d,
    output [7:0] out
);
    wire [7:0] mux0;
    wire [7:0] mux1;

    mux2 mux_0 (sel[0], a, b, mux0);
    mux2 mux_1 (sel[0], c, d, mux1);
    mux2 mux_2 (sel[1], mux0, mux1, out);
endmodule

Add/sub

原題:Add/sub

問題在於混淆 logical NOT 與 bitwise NOT,且缺少 else 會推導出 latch。

Verilog
// synthesis verilog_input_version verilog_2001
module top_module (
    input do_sub,
    input [7:0] a,
    input [7:0] b,
    output reg [7:0] out,
    output reg result_is_zero
);
    always @(*) begin
        if (do_sub)
            out = a - b;
        else
            out = a + b;

        result_is_zero = (out == 8'd0);
    end
endmodule

Case statement

原題:Case statement

問題集中在進位與 bit slice 寫錯,修正位元範圍即可。

Verilog
module top_module (
    input [7:0] code,
    output reg [3:0] out,
    output reg valid
);
    always @(*) begin
        valid = 1'b1;

        case (code)
            8'h45: out = 4'd0;
            8'h16: out = 4'd1;
            8'h1e: out = 4'd2;
            8'h26: out = 4'd3;
            8'h25: out = 4'd4;
            8'h2e: out = 4'd5;
            8'h36: out = 4'd6;
            8'h3d: out = 4'd7;
            8'h3e: out = 4'd8;
            8'h46: out = 4'd9;
            default: begin
                out = 4'd0;
                valid = 1'b0;
            end
        endcase
    end
endmodule

Build a circuit from a simulation waveform

Combinational circuit 1

原題:Combinational circuit 1

觀察波形可知,輸出 q 對應到 a & b

Verilog
module top_module (
    input a,
    input b,
    output q
);
    assign q = a & b;
endmodule

Combinational circuit 2

原題:Combinational circuit 2

觀察波形可知:輸入中有偶數個 1 時,輸出為 1,因此可用 XNOR 表示。

Verilog
module top_module (
    input a,
    input b,
    input c,
    input d,
    output q
);
    assign q = ~(a ^ b ^ c ^ d);
endmodule

Combinational circuit 3

原題:Combinational circuit 3

由波形可得:(~c & ~d) | (~a & ~b) 時輸出為 0;等價地,(c | d) & (a | b) 時輸出為 1。

Verilog
module top_module (
    input a,
    input b,
    input c,
    input d,
    output q
);
    assign q = (a | b) & (c | d);
endmodule

Combinational circuit 4

原題:Combinational circuit 4

由波形可得:當 bc 為 1 時,輸出為 1。

Verilog
module top_module (
    input a,
    input b,
    input c,
    input d,
    output q
);
    assign q = b | c;
endmodule

Combinational circuit 5

原題:Combinational circuit 5

c 為 0、1、2、3 時,輸出分別對應到 bead;其他情況輸出 4'hf

Verilog
module top_module (
    input [3:0] a,
    input [3:0] b,
    input [3:0] c,
    input [3:0] d,
    input [3:0] e,
    output reg [3:0] q
);
    always @(*) begin
        case (c)
            4'd0: q = b;
            4'd1: q = e;
            4'd2: q = a;
            4'd3: q = d;
            default: q = 4'hf;
        endcase
    end
endmodule

Combinational circuit 6

原題:Combinational circuit 6

由波形可看出這接近 decoder 題型,但輸出值依題目指定。

Verilog
module top_module (
    input [2:0] a,
    output reg [15:0] q
);
    always @(*) begin
        case (a)
            3'd0: q = 16'h1232;
            3'd1: q = 16'haee0;
            3'd2: q = 16'h27d4;
            3'd3: q = 16'h5a0e;
            3'd4: q = 16'h2066;
            3'd5: q = 16'h64ce;
            3'd6: q = 16'hc526;
            3'd7: q = 16'h2f19;
        endcase
    end
endmodule

Sequential circuit 7

原題:Sequential circuit 7

由波形可看出 qa 取反後延遲一個 clock cycle 的結果。

Verilog
module top_module (
    input clk,
    input a,
    output q
);
    reg state;
    reg next_state;

    always @(*) begin
        next_state = a;
    end

    always @(posedge clk) begin
        state <= next_state;
    end

    assign q = ~state;
endmodule

Sequential circuit 8

原題:Sequential circuit 8

q 是 negative-edge D flip-flop;p 則需要從波形判斷出 latch 結構。

Verilog
module top_module (
    input clock,
    input a,
    output p,
    output reg q
);
    assign p = clock ? a : p;

    always @(negedge clock) begin
        q <= a;
    end
endmodule

Sequential circuit 9

原題:Sequential circuit 9

波形對應到計時器結構。

Verilog
module top_module (
    input clk,
    input a,
    output reg [3:0] q
);
    always @(posedge clk) begin
        if (a)
            q <= 4'd4;
        else if (q == 4'd6)
            q <= 4'd0;
        else
            q <= q + 1'd1;
    end
endmodule

Sequential circuit 10

原題:Sequential circuit 10

可把波形視為 FSM,整理不同 state 與 input 下的輸出。

Verilog
module top_module (
    input clk,
    input a,
    input b,
    output q,
    output reg state
);
    reg next_state;

    always @(*) begin
        case (state)
            1'b0: next_state = a & b;
            1'b1: next_state = a | b;
        endcase
    end

    always @(posedge clk) begin
        state <= next_state;
    end

    assign q = state ^ a ^ b;
endmodule