HDLBits 系列(20)移位寄存器(逻辑移位、算术移位、循环移位)

举报
李锐博恩 发表于 2021/07/15 00:16:32 2021/07/15
【摘要】 目录 4 bit的右移寄存器设计 100 bit循环移位寄存器 算术移位寄存器 4 bit的右移寄存器设计 先给出一个4位右移寄存器的设计题: Build a 4-bit shift register (right shift), with asynchronous reset, synchronous load, and enable. areset: Re...

目录

4 bit的右移寄存器设计

100 bit循环移位寄存器

算术移位寄存器


4 bit的右移寄存器设计

先给出一个4位右移寄存器的设计题:

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.

题目过于简单,直接给出我的设计:


  
  1. module top_module(
  2. input clk,
  3. input areset, // async active-high reset to zero
  4. input load,
  5. input ena,
  6. input [3:0] data,
  7. output reg [3:0] q);
  8. always@(posedge clk or posedge areset) begin
  9. if(areset) q <= 0;
  10. else if(load) q <= data;
  11. else if(ena) begin
  12. q <= {1'b0, q[3:1]};
  13. end
  14. end
  15. endmodule

100 bit循环移位寄存器

Build a 100-bit left/right rotator, with synchronous load and left/right enable. A rotator shifts-in the shifted-out bit from the other end of the register, unlike a shifter that discards the shifted-out bit and shifts in a zero. If enabled, a rotator rotates the bits around and does not modify/discard them.

  • load: Loads shift register with data[99:0] instead of rotating.
  • ena[1:0]: Chooses whether and which direction to rotate.
    • 2'b01 rotates right by one bit
    • 2'b10 rotates left by one bit
    • 2'b00 and 2'b11 do not rotate.
  • q: The contents of the rotator.

Module Declaration


  
  1. module top_module(
  2. input clk,
  3. input load,
  4. input [1:0] ena,
  5. input [99:0] data,
  6. output reg [99:0] q);

也很简单,直接给出我的设计:


  
  1. module top_module(
  2. input clk,
  3. input load,
  4. input [1:0] ena,
  5. input [99:0] data,
  6. output reg [99:0] q);
  7. always@(posedge clk) begin
  8. if(load) q <= data;
  9. else begin
  10. if(ena == 2'b01) q <= {q[0], q[99:1]};
  11. else if(ena == 2'b10) q <= {q[98:0], q[99]};
  12. else ;
  13. end
  14. end
  15. endmodule

算术移位寄存器

 

Build a 64-bit arithmetic shift register, with synchronous load. The shifter can shift both left and right, and by 1 or 8 bit positions, selected by amount.

An arithmetic right shift shifts in the sign bit of the number in the shift register (q[63] in this case) instead of zero as done by a logical right shift. Another way of thinking about an arithmetic right shift is that it assumes the number being shifted is signed and preserves the sign, so that arithmetic right shift divides a signed number by a power of two.

There is no difference between logical and arithmetic left shifts.

  • load: Loads shift register with data[63:0] instead of shifting.
  • ena: Chooses whether to shift.
  • amount: Chooses which direction and how much to shift.
    • 2'b00: shift left by 1 bit.
    • 2'b01: shift left by 8 bits.
    • 2'b10: shift right by 1 bit.
    • 2'b11: shift right by 8 bits.
  • q: The contents of the shifter.

Module Declaration


  
  1. module top_module(
  2. input clk,
  3. input load,
  4. input ena,
  5. input [1:0] amount,
  6. input [63:0] data,
  7. output reg [63:0] q);

不用看题目,都知道要做什么:

提示:

A 5-bit number 11000 arithmetic right-shifted by 1 is 11100, while a logical right shift would produce 01100.

Similarly, a 5-bit number 01000 arithmetic right-shifted by 1 is 00100, and a logical right shift would produce the same result, because the original number was non-negative.


  
  1. module top_module(
  2. input clk,
  3. input load,
  4. input ena,
  5. input [1:0] amount,
  6. input [63:0] data,
  7. output reg [63:0] q);
  8. always@(posedge clk) begin
  9. if(load) q <= data;
  10. else if(ena) begin
  11. if(amount == 2'b00) q <= {q[62:0],1'b0};
  12. else if(amount == 2'b01) q <= {q[55:0],{8{1'b0}}};
  13. else if(amount == 2'b10) q <= {q[63],q[63:1]};
  14. else if(amount == 2'b11) q <= {{8{q[63]}},q[63:8]};
  15. end
  16. else ;
  17. end
  18. endmodule

 

 

 

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/103265924

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。