
4.3 16位桶形移位器
本示例设计的是16位桶形移位器,可实现对16位输入信号的循环左移、循环右移、逻辑左移、逻辑右移、算术左移和算术右移这6种操作。移位器的功能选择由模式输入信号决定。移位器将循环左移、循环右移、逻辑左移、逻辑右移、算术左移和算术右移这6种操作,分别定义为“001”模式、“010”模式、“011”模式、“100”模式、“101”模式和“110”模式。对于“000”模式和“111”模式,移位器输出进入高阻态。比如,移位器的16位输入数据为“1011011101001000”,模式输入为“001”,即循环左移,移位量为2,那么,输出信号为“1101110100100010”。
本示例的VHDL代码如下。代码在实体内预定义了移位器的6种状态码。在结构体内使用when赋值语句实现移位模式的选择。代码中的my_rol、my_ror、my_sll、my_srl、my_sla和my_sra是自定义的移位函数,分别可以实现对unsigned数据的循环左移、循环右移、逻辑左移、逻辑右移、算术左移和算术右移。本示例使用模块化的设计,以上移位函数的定义被放置在work库的shifter_pkg包集内,调用时只需在VHDL文件头部声明该包集即可。


包集shifter_pkg的VHDL代码如下。




图4.4是16位桶形移位器的仿真结果。仿真可每10ns分为一个时间段,共10个时间段。16位输入数据为“1011011101001000”。第1个时间段,模式输入为“000”,即无效模式,输出进入高阻态。第2个时间段,模式输入为“001”,即循环左移,移位量为0,输出与输入数据相同。第3个时间段,模式输入为“001”,即循环左移,移位量为5,输出为“1110100100010110”。第4个时间段,模式输入为“001”,即循环左移,移位量为2,输出为“1101110100100010”。第5个时间段,模式输入为“010”,即循环右移,移位量为2,输出为“0010110111010010”。第6个时间段,模式输入为“011”,即逻辑左移,移位量为2,输出为“1101110100100000”。第7个时间段,模式输入为“100”,即逻辑右移,移位量为2,输出为“0010110111010010”。第8个时间段,模式输入为“101”,即算术左移,移位量为2,输出为“1101110100100000”。第9个时间段,模式输入为“110”,即算术右移,移位量为2,输出为“1110110111010010”。第10个时间段,模式输入为“111”,即无效模式,输出进入高阻态。仿真结果验证了设计的正确性。

图4.4 16位桶形移位器的仿真结果