Verilog HDLBits 第二十二期:4.1 Finding bugs in code(4.1.1-4.1.5)

目录

前言

 4.1.1 Mux(Bugs mux2)

Solution:

 4.1.2 NAND(Bugs nand3)

Solution:

 4.1.3 Mux(Bugs mux4)

Solution:

4.1.4 Add/sub(Bugs addsubz)

Solution:

4.1.5 Case statement(Bugs case)

Solution:


前言

HDLbits网站如下

Problem sets - HDLBits (01xz.net)

从本期开始我们继续HDLbits第四章Circuits的学习,本期的内容是Finding bugs in code(4.1.1-4.1.5)


 4.1.1 Mux(Bugs mux2)

8bit的2选1数据选择器不工作,请解决bug​

Solution:

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

	// 1. 因为使用的是按位运算符,sel只是一个一比特宽的量,这会导致a和b清零。
	// 可以使用复制运算符,但是阅读起来会有些困难
	//   ( {8{~sel}} & a ) | ( {8{sel}} & b )
	
	// 2. 仿真波形表明,当sel=1时,应选择a。

    assign out = sel ? a : b;
	
endmodule

 4.1.2 NAND(Bugs nand3)

这个三输入与非门不工作。请修复错误。 必须使用提供的5输入与门:

module andgate ( output out, input a, input b, input c, input d, input e );

Solution:

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

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

endmodule

中间变量out_1


 4.1.3 Mux(Bugs mux4)

这个4选1数据选择器不工作,请解决bug

你有以下正确的2选1数据选择器可以使用:

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

Solution:

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

endmodule

4.1.4 Add/sub(Bugs addsubz)

以下带零标志的加减器不工作。修复错误。

Solution:

// 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

修改if语句的判断条件,改成逻辑取反。同时增加else


4.1.5 Case statement(Bugs case)

这个组合电路应该能够识别0到9键的8位键盘扫描码。它应该指出10个case项中是否有一个被识别(有效),如果是,则检测到了哪个密钥。修复bug。

Solution:

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

	// A combinational always block.
	always @(*) begin
		out = 0;		// 为避免产生latch,给输出一个默认的赋值
		valid = 1;		//   然后在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

keep!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值