HDLBits第十六章练习及答案

1、多路选择器

这个 8 位宽的 2 对 1 多路选择器不起作用。修复错误。

原始代码:

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

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

endmodule

修正后代码:

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

    assign out = sel ? a : b;

endmodule

验证结果:
在这里插入图片描述

2、NAND

这个三输入与非门不起作用。修复错误。

您必须使用提供的 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

修正后代码:

module top_module (input a, input b, input c, output out);//
    
	wire and_out;
    
    andgate inst1 ( and_out, a, b, c, 1'b1 ,1'b1 );
    assign out = ~ and_out;  

endmodule

验证结果:
在这里插入图片描述

3、多路选择器1

这个 4 对 1 多路选择器不起作用。修复错误。

为您提供了一个无错误的 2 对 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

修正后代码:

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

endmodule

验证结果:
在这里插入图片描述

4、加/减法器

以下带有零标志的加减法器不起作用。修复错误。

原始代码:

    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)
          1'b0: out = a + b;
          1'b1: out = a - b;
          default: out = out;
        endcase
        
        if(out == 8'd0)
          result_is_zero = 1'b1;
        else
          result_is_zero = 1'b0;  
    end
    
endmodule

验证结果:
在这里插入图片描述

5、案例说明

这个组合电路应该识别 0 到 9 键的 8 位键盘扫描码。它应该指示是否识别了 10 种情况之一(有效),如果是,则检测到哪个键。修复错误。

原始代码:

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

    always @(*) begin
        case (code)
            8'h45: out = 4'd0;
            8'h16: out = 4'd1;
            8'h1e: out = 4'd2;
            8'h26: out = 4'd3;
            8'h25: out = 4'd4;
            8'h2e: out = 4'd5;
            8'h36: out = 4'd6;
            8'h3d: out = 4'd7;
            8'h3e: out = 4'd8;
            8'h46: out = 4'd9;
            default: out = 4'd0;
           endcase 
        
         if(out == 4'd0 && code!= 8'h45) 
            valid = 1'b0;
        else 
            valid = 1'b1;
    end

endmodule

验证结果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值