Debian Codegraph @

hdl/fpga/ip/nuand/arbiter/vhdl/arbiter.vhd

raw
1 -- Copyright (c) 2017 Nuand LLC
2 --
3 -- This program is free software: you can redistribute it and/or modify
4 -- it under the terms of the GNU Affero General Public License as
5 -- published by the Free Software Foundation, either version 3 of the
6 -- License, or (at your option) any later version.
7 --
8 -- This program is distributed in the hope that it will be useful,
9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 -- GNU Affero General Public License for more details.
12 --
13 -- You should have received a copy of the GNU Affero General Public License
14 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 library ieee ;
17 use ieee.std_logic_1164.all ;
18 use ieee.numeric_std.all ;
19
20 entity arbiter is
21 generic (
22 N : integer := 1
23 ) ;
24
25 port (
26 -- Control signals
27 clock : in std_logic ;
28 reset : in std_logic ;
29
30 -- Memory mapped interface
31 addr : in std_logic_vector(4 downto 0) ;
32 din : in std_logic_vector(7 downto 0) ;
33 dout : out std_logic_vector(7 downto 0) ;
34 write : in std_logic ;
35 read : in std_logic ;
36 waitreq : out std_logic ;
37 readack : out std_logic ;
38 intr : out std_logic ;
39
40 -- Arbiter signals
41 request : in std_logic_vector(N-1 downto 0) ;
42 ack : in std_logic_vector(N-1 downto 0) ;
43
44 granted : out std_logic_vector(N-1 downto 0)
45 ) ;
46 end entity ;
47
48 architecture arch of arbiter is
49
50 signal rack : std_logic ;
51
52 signal uaddr : unsigned(addr'range) ;
53
54 signal intr_clear : std_logic ;
55 signal intr_set : std_logic ;
56
57 signal nios_csr : std_logic_vector(1 downto 0) ;
58 signal nios_request : std_logic ;
59 signal nios_ack : std_logic ;
60 signal nios_granted : std_logic ;
61
62 signal arbiter_request : std_logic_vector(N-1 downto 0) ;
63 signal arbiter_ack : std_logic_vector(N-1 downto 0) ;
64 signal arbiter_granted : std_logic_vector(N-1 downto 0) ;
65
66 type fsm_t is (INIT, WAIT_FOR_REQ, GRANT, WAIT_FOR_ACK) ;
67
68 type state_t is record
69 state : fsm_t;
70 grant : std_logic_vector(N-1 downto 0) ;
71 mask : std_logic_vector(N-1 downto 0) ;
72 mask_valid : std_logic ;
73 end record;
74
75 function NULL_STATE return state_t is
76 variable rv : state_t ;
77 begin
78 rv.state := INIT ;
79 rv.grant := ( others => '0' ) ;
80 rv.mask := ( others => '1' ) ;
81 rv.mask_valid := '0' ;
82
83 return rv ;
84 end function;
85
86
87 signal current : state_t ;
88 signal future : state_t ;
89
90 begin
91
92 nios_request <= nios_csr(0) ;
93 nios_ack <= nios_csr(1) ;
94 nios_granted <= current.grant(0) ;
95
96 waitreq <= '0';
97 readack <= rack ;
98
99 uaddr <= unsigned(addr) ;
100
101 generate_ack : process(clock, reset)
102 begin
103 if( reset = '1' ) then
104 rack <= '0' ;
105 elsif( rising_edge(clock) ) then
106 if( read = '1' ) then
107 rack <= '1' ;
108 else
109 rack <= '0' ;
110 end if ;
111 end if ;
112 end process ;
113
114 mm_read : process(clock)
115 begin
116 if( rising_edge(clock) ) then
117 case to_integer(uaddr) is
118 when 0 => dout <= "000000" & nios_csr ;
119 when 1 => dout <= "0000000" & nios_granted ;
120
121 when others => dout <= x"13";
122 end case;
123 end if ;
124 end process;
125
126 mm_write : process(clock, reset)
127 begin
128 if( reset = '1' ) then
129 nios_csr <= ( others => '0' ) ;
130 elsif( rising_edge(clock) ) then
131 intr_set <= '0' ;
132 intr_clear <= '0' ;
133 if( write = '1' ) then
134 case to_integer(uaddr) is
135 when 0 => nios_csr <= din(nios_csr'range) ;
136
137 when 8 =>
138 -- Command coming in!
139 case to_integer(unsigned(din)) is
140 when 0 =>
141 -- Set interrupt
142 intr_set <= '1' ;
143
144 when 1 =>
145 -- Clear interrupt
146 intr_clear <= '1' ;
147
148 when others =>
149 null ;
150
151 end case ;
152
153 when others => null ;
154 end case ;
155 end if ;
156 end if ;
157 end process ;
158
159 arbiter_request <= request(N-1 downto 1) & nios_request ;
160 arbiter_ack <= ack(N-1 downto 1) & nios_ack ;
161 granted <= current.grant ;
162
163 sync_proc : process(clock, reset)
164 begin
165 if( reset = '1' ) then
166 current <= NULL_STATE ;
167 elsif( rising_edge(clock) ) then
168 current <= future;
169 end if;
170 end process;
171
172 comb_proc : process( all )
173 variable arbiter_request_bit : std_logic_vector(N-1 downto 0) ;
174 begin
175
176 -- Next state defaults
177 future <= current;
178
179 case( current.state ) is
180
181 when INIT =>
182 future.state <= WAIT_FOR_REQ ;
183
184 when WAIT_FOR_REQ =>
185 if( arbiter_request /= ( arbiter_request'range => '0' ) ) then
186 arbiter_request_bit := std_logic_vector(unsigned(not(arbiter_request)) + 1 ) ;
187 if( (arbiter_request and current.mask) = ( arbiter_request'range => '0' ) ) then
188 future.grant <= arbiter_request and arbiter_request_bit ;
189 else
190 future.grant <= (arbiter_request and current.mask) and arbiter_request_bit ;
191 end if ;
192 future.state <= GRANT ;
193 end if ;
194
195 when GRANT =>
196 -- it's fine if this goes to 0, the unmasked check gives requester #0 the highest priority
197 future.mask <= std_logic_vector(unsigned(current.grant) - 1 ) ;
198 future.state <= WAIT_FOR_ACK ;
199
200 when WAIT_FOR_ACK =>
201 --if( arbiter_ack /= ( arbiter_ack'range => '0' ) ) then
202 if( (arbiter_ack and current.grant) = current.grant ) then
203 future.grant <= ( others => '0' ) ;
204 future.state <= WAIT_FOR_REQ ;
205 end if ;
206
207 end case ;
208 end process ;
209
210 interrupt : process(clock, reset)
211 type intrfsm_t is (WAIT_FOR_ARM, WAIT_FOR_GRANT, WAIT_FOR_CLEAR) ;
212 variable intrfsm : intrfsm_t := WAIT_FOR_ARM ;
213 begin
214 if( reset = '1' ) then
215 intr <= '0' ;
216 intrfsm := WAIT_FOR_ARM ;
217 elsif( rising_edge(clock) ) then
218 case intrfsm is
219 when WAIT_FOR_ARM =>
220 if( intr_set = '1' ) then
221 intrfsm := WAIT_FOR_GRANT ;
222 end if ;
223 when WAIT_FOR_GRANT =>
224 if( nios_granted = '1' ) then
225 intr <= '1' ;
226 intrfsm := WAIT_FOR_CLEAR ;
227 end if ;
228 when WAIT_FOR_CLEAR =>
229 if( intr_clear = '1' ) then
230 intr <= '0' ;
231 intrfsm := WAIT_FOR_ARM ;
232 end if ;
233 end case ;
234 end if ;
235 end process ;
236 end architecture ;
237