HDLbits刷題(Finding bugs in code)

This 8-bit wide 2-to-1 multiplexer doesn't work. Fix the bug(s).

module top_module (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output[7:0] out  );

    //assign out = (~sel & a) | (sel & b);
    assign out = (sel==1)? a : b;

endmodule
-------------------官網(wǎng)答案----------------------------------------
module top_module (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output reg [7:0] out
);

    // 1. A mux coded as (~sel & a) | (sel & b) does not work for vectors.
    // This is because these are bitwise operators, and sel is only a 1 bit wide quantity,
    // which leaves the upper bits of a and b zeroed. It is possible to code it using
    // the replication operator, but this is somewhat difficult to read:
    //   ( {8{~sel}} & a ) | ( {8{sel}} & b )
    
    // 2. The simulation waveform shows that when sel = 1, a should be selected. This
    // is flipped in the suggested code.

    assign out = sel ? a : b;
    
endmodule


This 4-to-1 multiplexer doesn't work. Fix the bug(s).

You are provided with a bug-free 2-to-1 multiplexer:

module mux2 (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output [7:0] out
);

被給的待改代碼如下:

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 mux0, mux1;
    mux2 mux0 ( sel[0],    a,    b, mux0 );
    mux2 mux1 ( sel[1],    c,    d, mux1 );
    mux2 mux2 ( sel[1], mux0, mux1,  out );
-----------------------------------更改情況-----------------
      //wire mux0, mux1;
    reg[7:0] mux0;
    reg[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

  • Bugs addsubz
    The following adder-subtractor with zero flag doesn't work. Fix the bug(s).
// 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
        case (do_sub)
          0: out = a+b;
          1: out = a-b;
        endcase

        if (~out)
            result_is_zero = 1;
    end

endmodule
----------------------------------------------------------------------------------------------
// 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
        case (do_sub)
          0: out = a+b;
          1: out = a-b;
        endcase

        if (out==0)
            result_is_zero = 1'd1;
        else
            result_is_zero = 1'd0;
    end

endmodule

  • Bugs case
    This combinational circuit is supposed to recognize 8-bit keyboard scancodes for keys 0 through 9. It should indicate whether one of the 10 cases were recognized (valid), and if so, which key was detected. Fix the bug(s).
module top_module (
    input [7:0] code,
    output reg [3:0] out,
    output reg valid=1 );//

     always @(*)
        case (code)
            8'h45: out = 0;
            8'h16: out = 1;
            8'h1e: out = 2;
            8'd26: out = 3;
            8'h25: out = 4;
            8'h2e: out = 5;
            8'h36: out = 6;
            8'h3d: out = 7;
            8'h3e: out = 8;
            6'h46: out = 9;
            default: valid = 0;
        endcase

endmodule
----------------------------------參考答案-----------------------------------------
module top_module (
    input [7:0] code,
    output reg [3:0] out,
    output reg valid
);

    // A combinational always block.
    always @(*) begin
        out = 0;        // To avoid latches, give the outputs a default assignment
        valid = 1;      //   then override them in the case statement. This is less
                        //   code than assigning a value to every variable for every case.
        case (code)
            8'h45: out = 0;
            8'h16: out = 1;
            8'h1e: out = 2;
            8'h26: out = 3;     // 8'd26 is 8'h1a
            8'h25: out = 4;
            8'h2e: out = 5;
            8'h36: out = 6;
            8'h3d: out = 7;
            8'h3e: out = 8;
            8'h46: out = 9;
            default: valid = 0;
        endcase
    end
    
endmodule

不知不覺(jué),已經(jīng)堅(jiān)持了一個(gè)月了,看完了電路,對(duì)Verilog也有了一定的了解,最近剛開(kāi)始學(xué)習(xí)SV,希望可以一直堅(jiān)持下去,希望早日可以找到工作

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容