HDLBits刷题合集—21 Finding bugs in code

本文分享了在HDLBits平台上解决FPGA设计中常见错误的方法,包括多路复用器、NAND门、加减法器及案例分析等模块的代码修正,适合初学者参考学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDLBits刷题合集—21 Finding bugs in code

HDLBits-160 Bugs mux2

代码如下:

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

    assign out = sel ? a : b;

endmodule

HDLBits-161 Bugs nand3

代码如下:

module top_module (
	input a,
	input b, 
	input c, 
	output out);//
	
	wire outb;
    
    andgate inst1 ( .a(a), .b(b), .c(c), .d(1'b1), .e(1'b1), .out(outb) );
    assign out = ~ outb;
    
endmodule

HDLBits-162 Bugs mux4

代码如下

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

endmodule

HDLBits-163 Bugs addsubz

代码如下:

// 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;
    default: out = a + b;
        endcase
       if (|out)
   			result_is_zero = 0;
        else
        	result_is_zero = 1;
    end
   
endmodule

HDLBits-164 Bugs case

代码如下:

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

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

endmodule

Note
新手一枚,主要分享博客,记录学习过程,后期参考大佬代码或思想会一一列出。欢迎大家批评指正!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值