-
Notifications
You must be signed in to change notification settings - Fork 380
/
barrel_shifter.sv
59 lines (46 loc) · 1.43 KB
/
barrel_shifter.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//------------------------------------------------------------------------------
// barrel_shifter.sv
// Konstantin Pavlov, pavlovconst@gmail.com
//------------------------------------------------------------------------------
// INFO -------------------------------------------------------------------------
// Barrel shifter written in System Verilog
//
/* --- INSTANTIATION TEMPLATE BEGIN ---
barrel_shifter #(
.DATA_W( 32 )
) bs_inst (
.clk( clk ),
.nrst( nrst,),
.ena( 1'b1 ),
.l_nr( 1'b1 ),
.dst( ),
.id( id[31:0] ),
.od( od[31:0] )
);
--- INSTANTIATION TEMPLATE END ---*/
module barrel_shifter #( parameter
DATA_W = 32,
DIST_W = $clog2(DATA_W)
)(
input clk, // clock
input nrst, // negative reset
input ena, // enable
input l_nr, // shift left or right
input [DIST_W-1:0] dst, // shift distance in bits
input [DATA_W-1:0] id, // input data vector
output logic [DATA_W-1:0] od = '0 // shifted data vector
);
always_ff @(posedge clk) begin
if( ~nrst ) begin
od[DATA_W-1:0] <= '0;
end else begin
if( ena ) begin
if( l_nr ) begin
od[DATA_W-1:0] <= ({2{id[DATA_W-1:0]}} << dst[DIST_W-1:0]) >> DATA_W;
end else begin
od[DATA_W-1:0] <= {2{id[DATA_W-1:0]}} >> dst[DIST_W-1:0];
end // if l_nr
end // if ena
end // nrst
end
endmodule