【HDLBits刷题】Shift4.

这篇文章讨论了如何在VerilogHDL中正确实现一个4位移位寄存器,包括异步清零、同步装载和使能控制。原始代码中的问题是,reset和load条件分支在always块中并行处理,导致编译错误。修正后的代码使用if-elseif结构解决了这个问题。
摘要由CSDN通过智能技术生成

Build a 4-bit shift register (right shift), with asynchronous reset, synchronous load, and enable.

areset: Resets shift register to zero.
load: Loads shift register with data[3:0] instead of shifting.
ena: Shift right (q[3] becomes zero, q[0] is shifted out and disappears).
q: The contents of the shift register.
If both the load and ena inputs are asserted (1), the load input has higher priority.

Module Declaration

module top_module(
    input clk,
    input areset,  // async active-high reset to zero
    input load,
    input ena,
    input [3:0] data,
    output reg [3:0] q); 

错误的代码:

module top_module(
    input clk,
    input areset,  // async active-high reset to zero
    input load,
    input ena,
    input [3:0] data,
    output reg [3:0] q); 

    always @(posedge clk or posedge areset)
        begin
            if(areset) begin
                q =0;
            end
            else begin
                q[2:0] = q[3:1];
                q[3] = 0;
            end
            if(load) begin
                q=data;
            end
        end
endmodule

这段代码报综合错误:
Error (10200): Verilog HDL Conditional Statement error at top_module.v(18): cannot match operand(s) in the condition to the corresponding edges in the enclosing event control of the always construct File: /home/h/work/hdlbits.15039432/top_module.v Line: 18

这里的问题是在always中areset和load都对q进行了操作,但是作为两个并行的条件分支,可能同时发生。

正确的代码:

module top_module(
    input clk,
    input areset,  // async active-high reset to zero
    input load,
    input ena,
    input [3:0] data,
    output reg [3:0] q
);

    always @(posedge clk or posedge areset) begin
        if (areset) begin
            q <= 4'b0000;
        end else if (load) begin
            q <= data;
        end else if (ena) begin
            q <= {1'b0,q[3:1],};
        end
    end

endmodule

写成一个if条件的不同分支的形式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三环西北角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值