【HDLBits刷题笔记】15 Finding bugs in code
  SpwcTktXWK0M 2023年11月02日 76 0

Bugs mux2

原本代码的逻辑是反的,这不是坑人吗。

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

    assign out = ({8{sel}} & a) | ({8{~sel}} & b);

endmodule

Bugs nand3

五输入的与门现在要实现三输入的与非门,多余的门可以输入1并将输出取反。

module top_module (input a, input b, input c, output out);//

    wire out_n;
    andgate inst1 ( out_n,a, b, c, 1'b1,1'b1 );
    assign out = ~out_n;

endmodule

Bugs mux4

bug1:mux0和mux1的位宽没设置,默认是1;

bug2:选通引脚有问题,应该先通过mux[0]判断是ac还是bd,再通过mux[1]进行判断。

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, mux1;
    mux2 u_mux0 ( sel[0],    a,    b, mux0 );
    mux2 u_mux1 ( sel[0],    c,    d, mux1 );
    mux2 u_mux2 ( sel[1], mux0, mux1,  out );

endmodule

Bugs addsubz

verilog中~是按位取反,!是逻辑取反。

同时需要补充out不为0的情况,否则输出会默认保持,综合出latch。

// 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;
        else
            result_is_zero = 0;
    end

endmodule

Bugs case

这道题比较考验眼力,一个是d要改成h,还有一个是6位改成8位。晕。

还有就是先给两个输出赋默认值,就不会综合出latch了,而且代码也更加简洁。

module top_module (
    input [7:0] code,
    output reg [3:0] out,
    output reg valid );//

     always @(*)
     begin
         out = 0;
         valid = 1'b1;
         case (code)
             8'h45: out = 0;
             8'h16: out = 1;
             8'h1e: out = 2;
             8'h26: out = 3;
             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

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  Vf84xTcexQdo   2024年01月04日   14   0   0 Verilog
  UXm2NDaWG8OP   2024年03月28日   52   0   0 Verilog
  sAAkk3Vxfaa8   2023年11月02日   191   0   0 VerilogHTMLhtmlVerilog
SpwcTktXWK0M