Verilog数字系统设计——移位寄存器实现
一、各种移位寄存器的原理
1.1、自循环移位
这里用例子说明较为清晰:
假如一个二进制数字是 1111100000
自循环左移 --> 1111000001 1110000011 1100000111 …
自循环右移 --> 0111110000 0011111000 0001111100 …
1.2、带进位位的循环移位
这里用例子说明较为清晰:
带进位的循环左移RCL(Rotate Left Through Carry):
用原CF的值填补空出的位,移出的位再进入CF。
假设当前,AL=01010011B,CF=1,则
执行指令 ROL AL,1
后,AL=10100110B,CF=0
执行指令 RCL AL,1
后,AL=10100111B,CF=0
最后一位的1,是先前CF的1
1.3、区别
具体区别可分为下面三种:
1.3.1、方式不同
循环左移:累加器自身循环向左移位。
带进位循环左移:累加器连同进位位一起左移。
- 1
- 2
1.3.2、过程不同
循环左移:移出的位不仅要进入CF,而且还要填补空出的位。
带进位循环左移:用原CF的值填补空出的位,移出的位再进入CF。
- 1
- 2
1.3.3、功能不同
循环左移:把操作数d,的各个二进制位向左移动d位,从d左端移出的每一位再依次移到右端空出的位上,最后移出的位还要送到CF中。
带进位循环左移:把操作数d的各位与CF联合在一起,构成9个或者17个二进制位,向左移动d位,从左端移出的各位再依次移到右端空出的位上。
- 1
- 2
二、代码实现
2.1、shift module
module shiftreg(out,reset,clk,data,select); output reg [7:0] out; //output signal input [1:0] select; //Status selection signal input [7:0] data; input reset,clk; //reset signal reg CF,temp;
always @<span class="token punctuation">(</span>posedge clk or posedge reset<span class="token punctuation">)</span> begin <span class="token keyword">if</span><span class="token punctuation">(</span>reset<span class="token punctuation">)</span> <span class="token comment">//reset signal</span> begin out <span class="token operator"><=</span> data<span class="token punctuation">;</span> CF <span class="token operator"><=</span> <span class="token number">0</span><span class="token punctuation">;</span> end <span class="token keyword">else</span> begin <span class="token keyword">case</span><span class="token punctuation">(</span>select<span class="token punctuation">)</span> <span class="token number">2</span>'b00<span class="token operator">:</span> <span class="token comment">//ROL:Shift left from cycle</span> out <span class="token operator"><=</span> <span class="token punctuation">{<!-- --></span>out<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>out<span class="token punctuation">[</span><span class="token number">7</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">//用原CF的值填补空出的位,移出的位再进入CF</span> <span class="token number">2</span>'b01<span class="token operator">:</span> <span class="token comment">//RCL:Cyclic left shift with carry</span> begin temp <span class="token operator">=</span> out<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> out <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span>CF<span class="token punctuation">,</span>out<span class="token punctuation">[</span><span class="token number">7</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span> CF <span class="token operator">=</span> temp<span class="token punctuation">;</span> end <span class="token number">2</span>'b10<span class="token operator">:</span> <span class="token comment">//ROR:Rotate Right</span> out <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span>out<span class="token punctuation">[</span><span class="token number">6</span><span class="token operator">:</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>out<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token number">2</span>'b11<span class="token operator">:</span> <span class="token comment">//RCR:Rotate Right Through Carry</span> begin temp <span class="token operator">=</span>out<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">;</span> out <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span>out<span class="token punctuation">[</span><span class="token number">6</span><span class="token operator">:</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>CF<span class="token punctuation">}</span><span class="token punctuation">;</span> CF <span class="token operator">=</span> temp<span class="token punctuation">;</span> end <span class="token keyword">default</span><span class="token operator">:</span>out <span class="token operator"><=</span> <span class="token number">8</span>'bx<span class="token punctuation">;</span> endcase end end
endmodule
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
2.2、test module
module test_shiftreg; reg clk_t,reset_t; reg[1:0] select_t; wire[7:0] out_t; reg[7:0] data_t;
shiftreg <span class="token function">myshift</span><span class="token punctuation">(</span> <span class="token punctuation">.</span><span class="token function">out</span><span class="token punctuation">(</span><span class="token class-name">out_t</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token class-name">reset_t</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token function">clk</span><span class="token punctuation">(</span><span class="token class-name">clk_t</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token function">data</span><span class="token punctuation">(</span><span class="token class-name">data_t</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token class-name">select_t</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> initial begin <span class="token class-name">data_t</span> <span class="token operator">=</span> <span class="token number">8</span>'b1011_0101<span class="token punctuation">;</span> <span class="token class-name">reset_t</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token class-name">clk_t</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token class-name">select_t</span> <span class="token operator">=</span> <span class="token number">2</span>'b11<span class="token punctuation">;</span> #<span class="token number">20</span> <span class="token class-name">reset_t</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> end always #<span class="token number">20</span> <span class="token class-name">clk_t</span> <span class="token operator">=</span> <span class="token operator">~</span><span class="token class-name">clk_t</span><span class="token punctuation">;</span> <span class="token comment">//clock signal</span>
endmodule
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25