| 1 |
161 |
ja_rd |
--------------------------------------------------------------------------------
|
| 2 |
|
|
-- mips_shifter.vhdl -- combinational barrel shifter
|
| 3 |
|
|
--
|
| 4 |
|
|
--------------------------------------------------------------------------------
|
| 5 |
162 |
ja_rd |
-- Copyright (C) 2011 Jose A. Ruiz
|
| 6 |
161 |
ja_rd |
--
|
| 7 |
|
|
-- This source file may be used and distributed without
|
| 8 |
|
|
-- restriction provided that this copyright statement is not
|
| 9 |
|
|
-- removed from the file and that any derivative work contains
|
| 10 |
|
|
-- the original copyright notice and the associated disclaimer.
|
| 11 |
|
|
--
|
| 12 |
|
|
-- This source file is free software; you can redistribute it
|
| 13 |
|
|
-- and/or modify it under the terms of the GNU Lesser General
|
| 14 |
|
|
-- Public License as published by the Free Software Foundation;
|
| 15 |
|
|
-- either version 2.1 of the License, or (at your option) any
|
| 16 |
|
|
-- later version.
|
| 17 |
|
|
--
|
| 18 |
|
|
-- This source is distributed in the hope that it will be
|
| 19 |
|
|
-- useful, but WITHOUT ANY WARRANTY; without even the implied
|
| 20 |
|
|
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
| 21 |
|
|
-- PURPOSE. See the GNU Lesser General Public License for more
|
| 22 |
|
|
-- details.
|
| 23 |
|
|
--
|
| 24 |
|
|
-- You should have received a copy of the GNU Lesser General
|
| 25 |
|
|
-- Public License along with this source; if not, download it
|
| 26 |
|
|
-- from http://www.opencores.org/lgpl.shtml
|
| 27 |
|
|
--------------------------------------------------------------------------------
|
| 28 |
2 |
ja_rd |
|
| 29 |
|
|
library ieee;
|
| 30 |
|
|
use ieee.std_logic_1164.all;
|
| 31 |
|
|
use ieee.std_logic_arith.all;
|
| 32 |
|
|
use ieee.std_logic_signed.all;
|
| 33 |
|
|
|
| 34 |
|
|
entity mips_shifter is
|
| 35 |
|
|
port(
|
| 36 |
|
|
-- data input
|
| 37 |
|
|
d : in std_logic_vector(31 downto 0);
|
| 38 |
|
|
-- shift amount
|
| 39 |
|
|
a : in std_logic_vector(4 downto 0);
|
| 40 |
|
|
-- shift function: {0=sll,1=sla(unused),2=srl,3=sra}
|
| 41 |
|
|
fn : in std_logic_vector(1 downto 0);
|
| 42 |
|
|
-- shift result
|
| 43 |
|
|
r : out std_logic_vector(31 downto 0)
|
| 44 |
|
|
);
|
| 45 |
|
|
end;
|
| 46 |
|
|
|
| 47 |
|
|
architecture small of mips_shifter is
|
| 48 |
|
|
|
| 49 |
|
|
signal i_rev, o_rev : std_logic_vector(31 downto 0);
|
| 50 |
|
|
|
| 51 |
|
|
signal ext : std_logic_vector(31 downto 0);
|
| 52 |
|
|
type t_s is array(0 to 5) of std_logic_vector(31 downto 0);
|
| 53 |
|
|
signal s : t_s;
|
| 54 |
|
|
|
| 55 |
|
|
begin
|
| 56 |
|
|
-- The barrel shifter needs to shift left and right. This would usually
|
| 57 |
|
|
-- require two parallel barrel shifters (left and right) and an output mux
|
| 58 |
|
|
-- stage. Instead, we're gonna use a single left shifter, with two
|
| 59 |
|
|
-- conditional bit-reversal stages at input and output.
|
| 60 |
|
|
-- This will increase the LUT depth (and thus the delay) by 1 LUT row but
|
| 61 |
|
|
-- we'll cut the area by 4/11 more or less (depends on how many dedicated
|
| 62 |
|
|
-- muxes vs. LUTs the synth will use).
|
| 63 |
|
|
-- The barrel shifter can account for as much as 1/4 of the CPU area
|
| 64 |
|
|
-- (excluding mult/div unit) so it makes sense to be cheap here if what we
|
| 65 |
|
|
-- want is a small core.
|
| 66 |
161 |
ja_rd |
-- NOTE: this logic may or may not be in the critical delay path of the
|
| 67 |
|
|
-- core, depending on the cache implementation. See your synthesis report.
|
| 68 |
2 |
ja_rd |
|
| 69 |
|
|
-- Reverse input when shifting right
|
| 70 |
|
|
input_reversed:
|
| 71 |
|
|
for i in 0 to 31 generate
|
| 72 |
|
|
begin
|
| 73 |
|
|
i_rev(i) <= d(31-i);
|
| 74 |
|
|
end generate input_reversed;
|
| 75 |
|
|
s(5) <= i_rev when fn(1)='1' else d;
|
| 76 |
|
|
|
| 77 |
|
|
-- Sign extension / zero extension
|
| 78 |
|
|
ext <= (others => d(31)) when fn(0)='1' else (others => '0');
|
| 79 |
|
|
|
| 80 |
|
|
-- Build left barrel shifter in 5 binary stages as usual
|
| 81 |
|
|
shifter_stages:
|
| 82 |
|
|
for i in 0 to 4 generate
|
| 83 |
|
|
begin
|
| 84 |
|
|
with a(i) select s(i) <=
|
| 85 |
|
|
s(i+1)(31-2**i downto 0) & ext(2**i-1 downto 0) when '1',
|
| 86 |
|
|
s(i+1) when others;
|
| 87 |
|
|
end generate shifter_stages;
|
| 88 |
|
|
|
| 89 |
|
|
-- Reverse output when shifting right
|
| 90 |
|
|
output_reversal:
|
| 91 |
|
|
for i in 0 to 31 generate
|
| 92 |
|
|
begin
|
| 93 |
|
|
o_rev(i) <= s(0)(31-i);
|
| 94 |
|
|
end generate output_reversal;
|
| 95 |
|
|
r <= o_rev when fn(1)='1' else s(0);
|
| 96 |
|
|
|
| 97 |
|
|
end architecture small;
|