用Verilog实现一个桶形移位器

来源:互联网 发布:php不支持mysql 编辑:程序博客网 时间:2024/04/29 03:19

实验六 桶型移位器

实验介绍

本实验使用Verilog语言设计实现一个32位桶型移位器。

实验目标


1. 使用ISE软件设计并进行仿真
2. 学会使用Modelsim
3. 理解桶型移位器原理,使用logicsim软件搭建一个8位的桶型移位器
4. 用verilog实现一个32位桶型移位器

实验原理


桶型移位器是一个多输入、单输出电路。对于输入a[31:0],移位器首先会根据aluc[1:0]值来确定做何种移位,然后根据b[4:0]的值来确定移多少位,最后将结果c[31:0]输出。
下表为aluc的值所对应的运算:

MIPS指令 alu[1] alu[0] 说明 算术右移(sra) 0 0 a向右移动b位,最高位补b位符号位 逻辑右移(srl) 0 1 a向右移动b位,最高位补b位0 算术左移(sll) 1 0 a向左移动b位,最低位补b位0 逻辑左移(sll) 1 1 a向左移动b位,最低位补b位0


下图为桶型移位器的逻辑流程图,可根据其流程编写行为及的桶型移位器
桶型移位器的逻辑流程图

Verilog代码

代码如下:

`timescale 1ns / 1ps//////////////////////////////////////////////////////////////////////////////////// Company: // Engineer: // // Create Date:    14:17:05 11/13/2015 // Design Name: // Module Name:    barrelshifter32 // Project Name: // Target Devices: // Tool versions: // Description: //// Dependencies: //// Revision: // Revision 0.01 - File Created// Additional Comments: ////////////////////////////////////////////////////////////////////////////////////module barrelshifter32(    input [31:0] a,     // 32 位原始输入数据    input [4:0] b,      // 5 位输入数据,控制移位的位数    input [1:0] aluc,   // 2 位输入控制移位的方式    output reg [31:0] c     // 32 位输出,由a 经过b 位通过aluc 指定的移位方式移位而得);    reg [31:0] temp;    always @ (a or b or aluc) begin    case(aluc)        2'b00: begin            temp = b[0] ? {{a[31]}, a[31:1]} : a;            temp = b[1] ? {{2{temp[31]}}, temp[31:2]} : temp;//             b = sext ? {{(32-WIDTH){a[WIDTH-1]}},a} : {32'b0, a};             temp = b[2] ? {{4{temp[31]}}, temp[31:4]} : temp;             temp = b[3] ? {{8{temp[31]}}, temp[31:8]} : temp;             temp = b[4] ? {{16{temp[31]}}, temp[31:16]} : temp;            end        2'b01: begin             temp = b[0] ? {32'b0, a[31:1]} : a;             temp = b[1] ? {32'b0, temp[31:2]} : temp;             temp = b[2] ? {32'b0, temp[31:4]} : temp;             temp = b[3] ? {32'b0, temp[31:8]} : temp;             temp = b[4] ? {32'b0, temp[31:16]} : temp;            end        2'b10, 2'b11: begin             temp = b[0] ? {{a[30:0]}, 1'b0} : a;             temp = b[1] ? {{temp[29:0]}, 2'b0} : temp;             temp = b[2] ? {{temp[27:0]}, 4'b0} : temp;             temp = b[3] ? {{temp[23:0]}, 8'b0} : temp;             temp = b[4] ? {{temp[15:0]}, 16'b0} : temp;            end    endcase     c = temp;    endendmodule
0 0