1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/lm32_shifter.v Sun Apr 04 20:40:03 2010 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +// ============================================================================= 1.5 +// COPYRIGHT NOTICE 1.6 +// Copyright 2006 (c) Lattice Semiconductor Corporation 1.7 +// ALL RIGHTS RESERVED 1.8 +// This confidential and proprietary software may be used only as authorised by 1.9 +// a licensing agreement from Lattice Semiconductor Corporation. 1.10 +// The entire notice above must be reproduced on all authorized copies and 1.11 +// copies may only be made to the extent permitted by a licensing agreement from 1.12 +// Lattice Semiconductor Corporation. 1.13 +// 1.14 +// Lattice Semiconductor Corporation TEL : 1-800-Lattice (USA and Canada) 1.15 +// 5555 NE Moore Court 408-826-6000 (other locations) 1.16 +// Hillsboro, OR 97124 web : http://www.latticesemi.com/ 1.17 +// U.S.A email: techsupport@latticesemi.com 1.18 +// =============================================================================/ 1.19 +// FILE DETAILS 1.20 +// Project : LatticeMico32 1.21 +// File : lm32_shifter.v 1.22 +// Title : Barrel shifter 1.23 +// Dependencies : lm32_include.v 1.24 +// Version : 6.1.17 1.25 +// : Initial Release 1.26 +// Version : 7.0SP2, 3.0 1.27 +// : No Change 1.28 +// Version : 3.1 1.29 +// : No Change 1.30 +// ============================================================================= 1.31 + 1.32 +`include "lm32_include.v" 1.33 + 1.34 +///////////////////////////////////////////////////// 1.35 +// Module interface 1.36 +///////////////////////////////////////////////////// 1.37 + 1.38 +module lm32_shifter ( 1.39 + // ----- Inputs ------- 1.40 + clk_i, 1.41 + rst_i, 1.42 + stall_x, 1.43 + direction_x, 1.44 + sign_extend_x, 1.45 + operand_0_x, 1.46 + operand_1_x, 1.47 + // ----- Outputs ------- 1.48 + shifter_result_m 1.49 + ); 1.50 + 1.51 +///////////////////////////////////////////////////// 1.52 +// Inputs 1.53 +///////////////////////////////////////////////////// 1.54 + 1.55 +input clk_i; // Clock 1.56 +input rst_i; // Reset 1.57 +input stall_x; // Stall instruction in X stage 1.58 +input direction_x; // Direction to shift 1.59 +input sign_extend_x; // Whether shift is arithmetic (1'b1) or logical (1'b0) 1.60 +input [`LM32_WORD_RNG] operand_0_x; // Operand to shift 1.61 +input [`LM32_WORD_RNG] operand_1_x; // Operand that specifies how many bits to shift by 1.62 + 1.63 +///////////////////////////////////////////////////// 1.64 +// Outputs 1.65 +///////////////////////////////////////////////////// 1.66 + 1.67 +output [`LM32_WORD_RNG] shifter_result_m; // Result of shift 1.68 +wire [`LM32_WORD_RNG] shifter_result_m; 1.69 + 1.70 +///////////////////////////////////////////////////// 1.71 +// Internal nets and registers 1.72 +///////////////////////////////////////////////////// 1.73 + 1.74 +reg direction_m; 1.75 +reg [`LM32_WORD_RNG] left_shift_result; 1.76 +reg [`LM32_WORD_RNG] right_shift_result; 1.77 +reg [`LM32_WORD_RNG] left_shift_operand; 1.78 +wire [`LM32_WORD_RNG] right_shift_operand; 1.79 +wire fill_value; 1.80 +wire [`LM32_WORD_RNG] right_shift_in; 1.81 + 1.82 +integer shift_idx_0; 1.83 +integer shift_idx_1; 1.84 + 1.85 +///////////////////////////////////////////////////// 1.86 +// Combinational Logic 1.87 +///////////////////////////////////////////////////// 1.88 + 1.89 +// Select operands - To perform a left shift, we reverse the bits and perform a right shift 1.90 +always @(*) 1.91 +begin 1.92 + for (shift_idx_0 = 0; shift_idx_0 < `LM32_WORD_WIDTH; shift_idx_0 = shift_idx_0 + 1) 1.93 + left_shift_operand[`LM32_WORD_WIDTH-1-shift_idx_0] = operand_0_x[shift_idx_0]; 1.94 +end 1.95 +assign right_shift_operand = direction_x == `LM32_SHIFT_OP_LEFT ? left_shift_operand : operand_0_x; 1.96 + 1.97 +// Determine fill value for right shift - Sign bit for arithmetic shift, or zero for logical shift 1.98 +assign fill_value = (sign_extend_x == `TRUE) && (direction_x == `LM32_SHIFT_OP_RIGHT) 1.99 + ? operand_0_x[`LM32_WORD_WIDTH-1] 1.100 + : 1'b0; 1.101 + 1.102 +// Determine bits to shift in for right shift or rotate 1.103 +assign right_shift_in = {`LM32_WORD_WIDTH{fill_value}}; 1.104 + 1.105 +// Reverse bits to get left shift result 1.106 +always @(*) 1.107 +begin 1.108 + for (shift_idx_1 = 0; shift_idx_1 < `LM32_WORD_WIDTH; shift_idx_1 = shift_idx_1 + 1) 1.109 + left_shift_result[`LM32_WORD_WIDTH-1-shift_idx_1] = right_shift_result[shift_idx_1]; 1.110 +end 1.111 + 1.112 +// Select result 1.113 +assign shifter_result_m = direction_m == `LM32_SHIFT_OP_LEFT ? left_shift_result : right_shift_result; 1.114 + 1.115 +///////////////////////////////////////////////////// 1.116 +// Sequential Logic 1.117 +///////////////////////////////////////////////////// 1.118 + 1.119 +// Perform right shift 1.120 +always @(posedge clk_i `CFG_RESET_SENSITIVITY) 1.121 +begin 1.122 + if (rst_i == `TRUE) 1.123 + begin 1.124 + right_shift_result <= {`LM32_WORD_WIDTH{1'b0}}; 1.125 + direction_m <= `FALSE; 1.126 + end 1.127 + else 1.128 + begin 1.129 + if (stall_x == `FALSE) 1.130 + begin 1.131 + right_shift_result <= {right_shift_in, right_shift_operand} >> operand_1_x[`LM32_SHIFT_RNG]; 1.132 + direction_m <= direction_x; 1.133 + end 1.134 + end 1.135 +end 1.136 + 1.137 +endmodule