[HDLBits做题]Reading Simulations --- Finding bugs in code

1.Finding bugs in code

1.1 Mux [Bugs mux2]
问题描述

下面代码描述的8位宽2-to-1多路复用器不工作,找出bug并修改。

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

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

endmodule
错误汇总
  1. 输出out位宽1bit,应为8bit
  2. 在进行与运算时,sel位宽1位,a、b位宽8位,运算时只运算a[0]、b[0] 需要扩充至8位。
  3. 功能有误,sel为1时选通a,sel为0时选通b
代码修改
module top_module (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output [7:0] out  );
	 
	 // 1.在原有代码基础上修改
    assign out = ({8{sel}} & a) | ({8{~sel}} & b);
	 // 2.按照功能直接修改(三目运算符)
	 //assign out = sel ? a : b;
	 
endmodule
1.2 NAND [Bugs nand3]
问题描述

这个三输入的与非门不工作,修改其中的错误。
你必须使用提供的5输入与门:
module andgate(output out, input a, input b, input c, input d, input e);

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

    andgate inst1 ( a, b, c, out );

endmodule
错误汇总
  1. 应用的是与门,需对结果取反形成与非门
  2. 例化andgate模块时,如果是顺序端口连接(如原题)则需要按模块定义时的顺序与外部信号进行匹配,但如果是命名端口例化,则需要指出外部信号连接模块定义中的哪个信号。
  3. 对于这个5输入与门的应用,不能悬空不使用的两个输入引脚,而应该置高电平以满足功能需求。
代码修改
module top_module (input a, input b, input c, output out);

    wire andOut;
    //1.命名端口连接
    andgate inst1( .a(a), .b(b), .c(c), .out(andOut), .d(1'b1), .e(1'b1));
	//2.顺序端口连接
	//andgate inst1( andOut, a, b, c, 1'b1, 1'b1);
	assign out = ~andOut;
	
endmodule
1.3 Mux [Bugs mux4]
问题描述

这个4-to-1多路复用器无法正常工作,请修改其中的错误
你可以使用一个没有错误的2-to-1多路复用器

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 );

endmodule
错误汇总
  1. 例化模块命名与其他命名重复
  2. 模块输出均为8位寄存器,mux0、mux1定义有误
  3. 根据功能,sel[0]决定a和b、c和d之间的选通,sel[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  ); //

    reg [7:0] mux0, mux1;
    mux2 mymux0 ( sel[0],    a,    b, mux0 );
    mux2 mymux1 ( sel[0],    c,    d, mux1 );
    mux2 mymux2 ( sel[1], mux0, mux1,  out );

endmodule
1.4 Add/sub [Bugs addsubz]
问题描述

下面是无法工作的有0标志位的加法器,请修复bug

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
错误汇总
  1. result_is_zero信号的判断条件不全。
代码修改
// 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 == 8'd0)
            result_is_zero = 1;
			else
				result_is_zero = 0;
    end

endmodule
1.5 Case statement [Bugs case]
问题描述

这个组合电路应该识别键0到9的8位键盘扫描码。它应该指出10个按键中是否有一个有效,如果有效则检测是哪个按键。修复其中的bug。

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
错误汇总
  1. valid修改条件不足,置0后不会再发生变化,且参数定义的时候不应赋值
  2. always没有begin end
  3. out 的输出条件不全
  4. case中间条件存在进制错误和位宽错误
代码修改
module top_module (
   input [7:0] code,
   output reg [3:0] out,
   output reg valid );//

    always @(*) begin
   	 out = 4'd0;
        valid = 1'd1;
       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

这个系列主要是记录一下自己的学习过程和简单的思考过程,参考了许多他人的思路。题目均为HDLBits上的题目,借助翻译器与自己的理解组织了题目描述,如果有问题欢迎批评指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值