1 |
17 |
alirezamon |
// =============================================================================
|
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 |
|
|
|
30 |
|
|
module 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 |
|
|
|
47 |
|
|
input clk_i; // Clock
|
48 |
|
|
input rst_i; // Reset
|
49 |
|
|
input stall_x; // Stall instruction in X stage
|
50 |
|
|
input direction_x; // Direction to shift
|
51 |
|
|
input sign_extend_x; // Whether shift is arithmetic (1'b1) or logical (1'b0)
|
52 |
|
|
input [`LM32_WORD_RNG] operand_0_x; // Operand to shift
|
53 |
|
|
input [`LM32_WORD_RNG] operand_1_x; // Operand that specifies how many bits to shift by
|
54 |
|
|
|
55 |
|
|
/////////////////////////////////////////////////////
|
56 |
|
|
// Outputs
|
57 |
|
|
/////////////////////////////////////////////////////
|
58 |
|
|
|
59 |
|
|
output [`LM32_WORD_RNG] shifter_result_m; // Result of shift
|
60 |
|
|
wire [`LM32_WORD_RNG] shifter_result_m;
|
61 |
|
|
|
62 |
|
|
/////////////////////////////////////////////////////
|
63 |
|
|
// Internal nets and registers
|
64 |
|
|
/////////////////////////////////////////////////////
|
65 |
|
|
|
66 |
|
|
reg direction_m;
|
67 |
|
|
reg [`LM32_WORD_RNG] left_shift_result;
|
68 |
|
|
reg [`LM32_WORD_RNG] right_shift_result;
|
69 |
|
|
reg [`LM32_WORD_RNG] left_shift_operand;
|
70 |
|
|
wire [`LM32_WORD_RNG] right_shift_operand;
|
71 |
|
|
wire fill_value;
|
72 |
|
|
wire [`LM32_WORD_RNG] right_shift_in;
|
73 |
|
|
|
74 |
|
|
integer shift_idx_0;
|
75 |
|
|
integer 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
|
82 |
|
|
always @*
|
83 |
|
|
begin
|
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];
|
86 |
|
|
end
|
87 |
|
|
assign 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
|
90 |
|
|
assign 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
|
95 |
|
|
assign right_shift_in = {`LM32_WORD_WIDTH{fill_value}};
|
96 |
|
|
|
97 |
|
|
// Reverse bits to get left shift result
|
98 |
|
|
always @*
|
99 |
|
|
begin
|
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];
|
102 |
|
|
end
|
103 |
|
|
|
104 |
|
|
// Select result
|
105 |
|
|
assign 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
|
112 |
|
|
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
|
113 |
|
|
begin
|
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
|
127 |
|
|
end
|
128 |
|
|
|
129 |
|
|
endmodule
|