SURF  1.0
ArbiterPkg.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 -- File : ArbiterPkg.vhd
3 -- Company : SLAC National Accelerator Laboratory
4 -- Created : 2013-05-01
5 -- Last update: 2017-02-23
6 -------------------------------------------------------------------------------
7 -- Description: Arbiter Package File
8 -------------------------------------------------------------------------------
9 -- This file is part of 'SLAC Firmware Standard Library'.
10 -- It is subject to the license terms in the LICENSE.txt file found in the
11 -- top-level directory of this distribution and at:
12 -- https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
13 -- No part of 'SLAC Firmware Standard Library', including this file,
14 -- may be copied, modified, propagated, or distributed except according to
15 -- the terms contained in the LICENSE.txt file.
16 -------------------------------------------------------------------------------
17 
18 library ieee;
19 use ieee.std_logic_1164.all;
20 use ieee.numeric_std.all;
21 use work.StdRtlPkg.all;
22 --use work.TextUtilPkg.all;
23 
24 package ArbiterPkg is
25 --! @file
26  --! @ingroup base_general
27 
28  function priorityEncode (v : slv; p : integer) return slv;
29 
30  procedure arbitrate (
31  req : in slv;
32  lastSelected : in slv;
33  nextSelected : inout slv;
34  valid : inout sl;
35  ack : out slv);
36 
37 end package ArbiterPkg;
38 
39 package body ArbiterPkg is
40 
41  -- Priority encoder with index p having top priority
42  -- followed by p-1, p-2, etc., rolling over to the highest index when 0 reached
43  -- Returns highest priority index with value '1', encoded as unsigned slv.
44  function priorityEncode (v : slv; p : integer) return slv is
45  variable bestReq : integer;
46  variable rotatedV : unsigned(v'range);
47  variable ret : unsigned(bitSize(v'length-1)-1 downto 0) := (others => '0');
48  variable bestReqU : unsigned(ret'length downto 0);
49  begin
50 -- print("priorityEncode(" & str(v) & ", " & str(p) & ")");
51  -- Rotate input by n to give n top priority
52  rotatedV := rotate_right(unsigned(v), p);
53 
54  -- Find lowest index with value of '1'
55  bestReq := 0;
56  for i in v'range loop
57  if (rotatedV(i) = '1') then
58  bestReq := i;
59  end if;
60  end loop;
61 
62  -- Convert integer to unsigned
63  bestReqU := to_unsigned(bestReq, bestReqU'length);
64 
65  -- Add p to rotated select and mod by length to undo the rotation
66  ret := resize((bestReqU+p) mod v'length, ret'length);
67 
68  return slv(ret);
69  end function priorityEncode;
70 
71 
72  procedure arbitrate (
73  req : in slv;
74  lastSelected : in slv;
75  nextSelected : inout slv;
76  valid : inout sl;
77  ack : out slv) is
78  begin
79 -- print("arbitrate(" & str(req'length));-- & ", " & str(lastSelected'length) & ", " str(nextSelected'length) & ", " & ")");
80  valid := uOr(req);
81  if (valid = '1') then
82  nextSelected := priorityEncode(req, to_integer(unsigned(lastSelected)+1) mod req'length);
83  ack := decode(slv(nextSelected))(req'range);
84  else
85  nextSelected := lastSelected;
86  ack(ack'range) := (others => '0');
87  end if;
88  end procedure arbitrate;
89 
90 end package body ArbiterPkg;
slv priorityEncodev,p,
Definition: ArbiterPkg.vhd:28
std_logic sl
Definition: StdRtlPkg.vhd:28
arbitratereq,lastSelected,nextSelected,valid,ack,
Definition: ArbiterPkg.vhd:30
std_logic_vector slv
Definition: StdRtlPkg.vhd:29
_library_ ieeeieee
Definition: Arbiter.vhd:18