Verilog刷题笔记63

找BUG

1、:Bug mux2

挑错:

c 复制代码
module top_module (
    input sel,
    input [7:0] a,
    input [7:0] b,
    output [7:0]out  );

    
    assign out =sel?a:b;

endmodule

结果正确:

原因:

1、输出out也应为8位

2、逻辑错误,&按位操作,需要将sel扩展到8位才可以。

但是,& 不可替换为&&,否则out的值只会是0或1。

逻辑错误也可通过三目运算符进行判断。

2:Bugs nand3

挑错:

c 复制代码
module top_module (input a, input b, input c, output out);//

    wire outzj;
    andgate inst1 (outzj,a,b,c,1,1);
    
    assign out=~outzj;

endmodule

结果正确:

原因:

用一个5输入的与门实现三输入的与非门。

首先要补全输入,之前空置的输入可用1'b1代替,再对模块的输出结果进行取反。

给定模块的输出在最前面而不是最后一位参数。

3:Bugs mux4

This 4-to-1 multiplexer doesn't work. Fix the bug(s).

You are provided with a bug-free 2-to-1 multiplexer:

module mux2 (

input sel,

input [7:0] a,

input [7:0] b,

output [7:0] out

);

挑错:

c 复制代码
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 mux11 ( sel[0],    c,    d, mux1 );
    mux2 mux22 ( sel[1], mux0, mux1,  out );

endmodule

结果正确:

原因:

使用给定的2选1选择器实现4选1,测试发现2选1的输出为sel=0输出a,sel=1输出b

例化名称不与变量名称相同

中间变量定义位宽不要出错

4:Bugs addsubz

The following adder-subtractor with zero flag doesn't work. Fix the bug(s).

挑错:

c 复制代码
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



        result_is_zero=(out==8'd0)?1:0;
    end


endmodule

结果正确:

原因:

~out 取反也不能说明out是否为全0,可以通过如下方法:

c 复制代码
(~(out&&1'b1))
(out==8'd0)

一个always组合逻辑块内不要既有case又有其他的逻辑,有的允许,有的不允许,尽量不用。

always里面不要用assign进行赋值

5:Bugs case

This combinational circuit is supposed to recognize 8-bit keyboard scancodes for keys 0 through 9. It should indicate whether one of the 10 cases were recognized (valid), and if so, which key was detected. Fix the bug(s).

挑错:

c 复制代码
module top_module (
    input [7:0] code,
    output reg [3:0] out,
    output reg valid=1 );//

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

endmodule

结果正确:

原因:

原代码中out=3时code是8'd26,out=9时code是6'h46,其它都是8位16进制,改成8'h之后成功运行

相关推荐
Include everything1 小时前
Rust学习笔记(三)|所有权机制 Ownership
笔记·学习·rust
livemetee2 小时前
Flink2.0学习笔记:Flink服务器搭建与flink作业提交
大数据·笔记·学习·flink
INS_KF3 小时前
【C++知识杂记2】free和delete区别
c++·笔记·学习
Easocen4 小时前
Mybatis学习笔记(五)
笔记·学习·mybatis
丑小鸭是白天鹅5 小时前
嵌入式C语言学习笔记之枚举、联合体
c语言·笔记·学习
十一10247 小时前
FX10/20 (CYUSB401X)开发笔记5 固件架构
笔记
FakeOccupational7 小时前
【电路笔记 通信】AXI4-Lite协议 FPGA实现 & Valid-Ready Handshake 握手协议
笔记·fpga开发
奶黄小甜包8 小时前
C语言零基础第18讲:自定义类型—结构体
c语言·数据结构·笔记·学习
rannn_11110 小时前
【MySQL学习|黑马笔记|Day7】触发器和锁(全局锁、表级锁、行级锁、)
笔记·后端·学习·mysql
草莓熊Lotso11 小时前
《详解 C++ Date 类的设计与实现:从运算符重载到功能测试》
开发语言·c++·经验分享·笔记·其他