错题集:HDLBits Dff16e 带2位使能端的16位D触发器

Create 16 D flip-flops. It's sometimes useful to only modify parts of a group of flip-flops. The byte-enable inputs control whether each byte of the 16 registers should be written to on that cycle. byteena[1] controls the upper byte d[15:8], while byteena[0] controls the lower byte d[7:0].

resetn is a synchronous, active-low reset.

All DFFs should be triggered by the positive edge of clk.

错误原因:关于对if语句的理解问题;以及信号保持问题。

module top_module (
    input clk,
    input resetn,
    input [1:0] byteena,
    input [15:0] d,
    output [15:0] q
);
//    ***错误一***
//    always@(posedge clk)//同步复位
//        begin
//            if(resetn == 1'b0)//低电平有效
//                q <= 0;       //if-else语句只执行其中的一条语句,所以这种写法是错的
//            else if(byteena[0]==1'b1)
//                q[7:0] <= d[7:0];
//            else if(byteena[1]==1'b1)
//                q[15:8]<= d[15:8];
//            else
//                q <= 16'hffff;
//        end

   always@(posedge clk)//同步复位
       begin
           if(resetn == 1'b0)//低电平有效
               q <= 0;
           else if(byteena==2'b11)
               q <= d;
           else if(byteena==2'b01)
               q[7:0]<=d[7:0];
           else if(byteena==2'b10)
               q[15:8]<=d[15:8];
           else
               q <= q;//***错误二***:之前我写的是q<=16'hffff;
           //但题目的意思应该是,如果没有使能,则相应位保持前一个状态。
       end
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值