Root/lm32/logic/sakc/rtl/lm32/lm32_shifter.v

1// =============================================================================
2// COPYRIGHT NOTICE
3// Copyright 2006 (c) Lattice Semiconductor Corporation
4// ALL RIGHTS RESERVED
5// This confidential and proprietary software may be used only as authorised by
6// a licensing agreement from Lattice Semiconductor Corporation.
7// The entire notice above must be reproduced on all authorized copies and
8// copies may only be made to the extent permitted by a licensing agreement from
9// Lattice Semiconductor Corporation.
10//
11// Lattice Semiconductor Corporation TEL : 1-800-Lattice (USA and Canada)
12// 5555 NE Moore Court 408-826-6000 (other locations)
13// Hillsboro, OR 97124 web : http://www.latticesemi.com/
14// U.S.A email: techsupport@latticesemi.com
15// =============================================================================/
16// FILE DETAILS
17// Project : LatticeMico32
18// File : lm32_shifter.v
19// Title : Barrel shifter
20// Dependencies : lm32_include.v
21// Version : 6.1.17
22// =============================================================================
23
24`include "lm32_include.v"
25
26/////////////////////////////////////////////////////
27// Module interface
28/////////////////////////////////////////////////////
29
30module lm32_shifter (
31    // ----- Inputs -------
32    clk_i,
33    rst_i,
34    stall_x,
35    direction_x,
36    sign_extend_x,
37    operand_0_x,
38    operand_1_x,
39    // ----- Outputs -------
40    shifter_result_m
41    );
42
43/////////////////////////////////////////////////////
44// Inputs
45/////////////////////////////////////////////////////
46
47input clk_i; // Clock
48input rst_i; // Reset
49input stall_x; // Stall instruction in X stage
50input direction_x; // Direction to shift
51input sign_extend_x; // Whether shift is arithmetic (1'b1) or logical (1'b0)
52input [`LM32_WORD_RNG] operand_0_x; // Operand to shift
53input [`LM32_WORD_RNG] operand_1_x; // Operand that specifies how many bits to shift by
54
55/////////////////////////////////////////////////////
56// Outputs
57/////////////////////////////////////////////////////
58
59output [`LM32_WORD_RNG] shifter_result_m; // Result of shift
60wire [`LM32_WORD_RNG] shifter_result_m;
61
62/////////////////////////////////////////////////////
63// Internal nets and registers
64/////////////////////////////////////////////////////
65
66reg direction_m;
67reg [`LM32_WORD_RNG] left_shift_result;
68reg [`LM32_WORD_RNG] right_shift_result;
69reg [`LM32_WORD_RNG] left_shift_operand;
70wire [`LM32_WORD_RNG] right_shift_operand;
71wire fill_value;
72wire [`LM32_WORD_RNG] right_shift_in;
73
74integer shift_idx_0;
75integer shift_idx_1;
76
77/////////////////////////////////////////////////////
78// Combinational Logic
79/////////////////////////////////////////////////////
80    
81// Select operands - To perform a left shift, we reverse the bits and perform a right shift
82always @*
83begin
84    for (shift_idx_0 = 0; shift_idx_0 < `LM32_WORD_WIDTH; shift_idx_0 = shift_idx_0 + 1)
85        left_shift_operand[`LM32_WORD_WIDTH-1-shift_idx_0] = operand_0_x[shift_idx_0];
86end
87assign right_shift_operand = direction_x == `LM32_SHIFT_OP_LEFT ? left_shift_operand : operand_0_x;
88
89// Determine fill value for right shift - Sign bit for arithmetic shift, or zero for logical shift
90assign fill_value = (sign_extend_x == `TRUE) && (direction_x == `LM32_SHIFT_OP_RIGHT)
91                      ? operand_0_x[`LM32_WORD_WIDTH-1]
92                      : 1'b0;
93
94// Determine bits to shift in for right shift or rotate
95assign right_shift_in = {`LM32_WORD_WIDTH{fill_value}};
96
97// Reverse bits to get left shift result
98always @*
99begin
100    for (shift_idx_1 = 0; shift_idx_1 < `LM32_WORD_WIDTH; shift_idx_1 = shift_idx_1 + 1)
101        left_shift_result[`LM32_WORD_WIDTH-1-shift_idx_1] = right_shift_result[shift_idx_1];
102end
103
104// Select result
105assign shifter_result_m = direction_m == `LM32_SHIFT_OP_LEFT ? left_shift_result : right_shift_result;
106    
107/////////////////////////////////////////////////////
108// Sequential Logic
109/////////////////////////////////////////////////////
110
111// Perform right shift
112always @(posedge clk_i `CFG_RESET_SENSITIVITY)
113begin
114    if (rst_i == `TRUE)
115    begin
116        right_shift_result <= {`LM32_WORD_WIDTH{1'b0}};
117        direction_m <= `FALSE;
118    end
119    else
120    begin
121        if (stall_x == `FALSE)
122        begin
123            right_shift_result <= {right_shift_in, right_shift_operand} >> operand_1_x[`LM32_SHIFT_RNG];
124            direction_m <= direction_x;
125        end
126    end
127end
128    
129endmodule
130

Archive Download this file

Branches:
master



interactive