移位寄存器在数字电路中具有广泛的应用,一般情况下,移位寄存器是在时钟的上升沿将数组中每一个bit进行左移或者右移,如下图所示:
利用移位寄存器可以对信号进行延时操作,移位寄存器的位宽决定信号的延时周期数。
移位寄存器也实现数据通路的串并转换功能,单bit信号依次输入到移位寄存器中,当存储的单bit数据达到数据位宽,则直接转换为多bit数据输出。
本篇文章介绍一种动态循环移位寄存器,即在时钟的上升沿可以整体向左或者向右移动多个bit,每次移动的距离是动态可变的,不再恒为1。
以向左的循环移位寄存器为例,代码如下所示:
module shift( input clk, //时钟 input rst, //复位 input [3:0] shift_in, //移位寄存器输入 input [1:0] index, //移位寄存器偏移值 output [3:0] shift_out //移位寄存器输出);reg [3:0] shift_out;always@(clk)begin if(rst) shift_out <= 4'd0; else shift_out <= loop_shift(shift_in,index);end//循环移位寄存器功能函数function [3:0] loop_shift; input [3:0] shift_in; input [1:0] index; reg [3:0] shift_tmp; integer j; begin shift_tmp = 4'd0;//遍历移位寄存器的所有bit for(j=0;j<4;j++)begin if(index != 2'd0)begin if(j<index)//计算低位的bit,例如index值为2//shift_tmp[0] = shift_in[2]//shift_tmp[1] = shift_in[3] shift_tmp[j] = shift_in[4-index+j]; else //计算高位的bit,例如index值为2,//shift_tmp[2] = shift_in[0]//shift_tmp[3] = shift_in[1] shift_tmp[j] = shift_in[j-index]; end else begin//index值0,直接输出移位寄存器值 shift_tmp[j] = shift_in[j]; end end loop_shift = shift_tmp; endendfunctionendmodule
仿真结果:
上述代码构造了位宽为4的动态循环移位寄存器,每次移位距离是动态改变的,根据仿真波形,动态循环移位寄存器的功能符合预期。