1 |
15 |
stvhawes |
--////////////////////////////////////////////////////////////////////
|
2 |
|
|
--// ////
|
3 |
|
|
--// search_control.vhd ////
|
4 |
|
|
--// ////
|
5 |
|
|
--// This file is part of the open_hitter opencores effort. ////
|
6 |
|
|
--// <http://www.opencores.org/cores/open_hitter/> ////
|
7 |
|
|
--// ////
|
8 |
|
|
--// Module Description: ////
|
9 |
|
|
--// This is the multipelexed search's control item, used to set ////
|
10 |
|
|
--// up, access and control the search. ////
|
11 |
|
|
--// search_*_i - input to search_control ////
|
12 |
|
|
--// order_*_o - output from search_control ////
|
13 |
|
|
--// Buses perform the multiplex and are experienced by each item ////
|
14 |
|
|
--// as b1_* - set by search_control, input to search_item ////
|
15 |
|
|
--// b2_* - output from search_item, read by search_control ////
|
16 |
|
|
--// The state machine in search_control coordinates the search, ////
|
17 |
|
|
--// and the number of transitions is dependant on the incoming ////
|
18 |
|
|
--// instruction (search_*_i). ////
|
19 |
|
|
--// ////
|
20 |
|
|
--// To Do: ////
|
21 |
|
|
--// ////
|
22 |
|
|
--// Author(s): ////
|
23 |
|
|
--// - Stephen Hawes ////
|
24 |
|
|
--// ////
|
25 |
|
|
--////////////////////////////////////////////////////////////////////
|
26 |
|
|
--// ////
|
27 |
|
|
--// Copyright (C) 2015 Stephen Hawes and OPENCORES.ORG ////
|
28 |
|
|
--// ////
|
29 |
|
|
--// This source file may be used and distributed without ////
|
30 |
|
|
--// restriction provided that this copyright statement is not ////
|
31 |
|
|
--// removed from the file and that any derivative work contains ////
|
32 |
|
|
--// the original copyright notice and the associated disclaimer. ////
|
33 |
|
|
--// ////
|
34 |
|
|
--// This source file is free software; you can redistribute it ////
|
35 |
|
|
--// and/or modify it under the terms of the GNU Lesser General ////
|
36 |
|
|
--// Public License as published by the Free Software Foundation; ////
|
37 |
|
|
--// either version 2.1 of the License, or (at your option) any ////
|
38 |
|
|
--// later version. ////
|
39 |
|
|
--// ////
|
40 |
|
|
--// This source is distributed in the hope that it will be ////
|
41 |
|
|
--// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
42 |
|
|
--// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
43 |
|
|
--// PURPOSE. See the GNU Lesser General Public License for more ////
|
44 |
|
|
--// details. ////
|
45 |
|
|
--// ////
|
46 |
|
|
--// You should have received a copy of the GNU Lesser General ////
|
47 |
|
|
--// Public License along with this source; if not, download it ////
|
48 |
|
|
--// from <http://www.opencores.org/lgpl.shtml> ////
|
49 |
|
|
--// ////
|
50 |
|
|
--////////////////////////////////////////////////////////////////////
|
51 |
|
|
--//
|
52 |
|
|
--// \$Id\$ TAKE OUT THE \'s and this comment in order to get this to work
|
53 |
|
|
--//
|
54 |
|
|
--// CVS Revision History
|
55 |
|
|
--//
|
56 |
|
|
--// \$Log\$ TAKE OUT THE \'s and this comment in order to get this to work
|
57 |
|
|
--//
|
58 |
|
|
library ieee;
|
59 |
|
|
use ieee.std_logic_1164.all;
|
60 |
|
|
use ieee.numeric_std.ALL;
|
61 |
|
|
|
62 |
|
|
entity search_control is
|
63 |
|
|
generic ( searchitems : integer );
|
64 |
|
|
port (
|
65 |
|
|
RX_CLK: in std_logic;
|
66 |
|
|
-- control flag(s) on the incoming bus
|
67 |
|
|
search_px_valid_i: in std_logic;
|
68 |
|
|
-- pxdata: in price_packet
|
69 |
|
|
search_px_type_i: in std_logic_vector(4 downto 0);
|
70 |
|
|
search_buy_sell_i: in std_logic_vector(2 downto 0); -- 111 buy, 000 sell
|
71 |
|
|
search_px_i: in std_logic_vector(15 downto 0); -- price
|
72 |
|
|
search_qty_i: in std_logic_vector(15 downto 0); -- quantity
|
73 |
|
|
search_sec_i: in std_logic_vector(55 downto 0); -- 7x 8bits securities identifier
|
74 |
|
|
search_id_i: in std_logic_vector(15 downto 0); -- unique/identifier/counter
|
75 |
|
|
-- pxdata: out price_packet
|
76 |
|
|
order_px_type_o: out std_logic_vector(4 downto 0);
|
77 |
|
|
order_buy_sell_o: out std_logic_vector(2 downto 0); -- 111 buy, 000 sell
|
78 |
|
|
order_px_o: out std_logic_vector(15 downto 0); -- price
|
79 |
|
|
order_qty_o: out std_logic_vector(15 downto 0); -- quantity
|
80 |
|
|
order_sec_o: out std_logic_vector(55 downto 0); -- 7x 8bits securities identifier
|
81 |
|
|
order_id_o: out std_logic_vector(15 downto 0); -- unique/identifier/counter
|
82 |
|
|
-- control flag(s) on the outgoing bus
|
83 |
|
|
order_px_valid_o: out std_logic
|
84 |
|
|
);
|
85 |
|
|
end search_control;
|
86 |
|
|
|
87 |
|
|
architecture search_control_implementation of search_control is
|
88 |
|
|
component search_item
|
89 |
|
|
generic ( item_id: std_logic_vector(15 downto 0) );
|
90 |
|
|
port (
|
91 |
|
|
RX_CLK: in std_logic;
|
92 |
|
|
-- control flag(s) on the incoming bus
|
93 |
|
|
b1_px_valid: in std_logic;
|
94 |
|
|
-- pxdata: in price_packet
|
95 |
|
|
b1_px_type: in std_logic_vector(4 downto 0);
|
96 |
|
|
b1_buy_sell: in std_logic_vector(2 downto 0); -- 111 buy, 000 sell
|
97 |
|
|
b1_px: in std_logic_vector(15 downto 0); -- price
|
98 |
|
|
b1_qty: in std_logic_vector(15 downto 0); -- quantity
|
99 |
|
|
b1_sec: in std_logic_vector(55 downto 0); -- 7x 8bits securities identifier
|
100 |
|
|
b1_id: in std_logic_vector(15 downto 0); -- unique/identifier/counter
|
101 |
|
|
-- pxdata: out price_packet
|
102 |
|
|
b2_px_type: out std_logic_vector(4 downto 0);
|
103 |
|
|
b2_buy_sell: out std_logic_vector(2 downto 0); -- 111 buy, 000 sell
|
104 |
|
|
b2_px: out std_logic_vector(15 downto 0); -- price
|
105 |
|
|
b2_qty: out std_logic_vector(15 downto 0); -- quantity
|
106 |
|
|
b2_sec: out std_logic_vector(55 downto 0); -- 7x 8bits securities identifier
|
107 |
|
|
b2_id: out std_logic_vector(15 downto 0) -- unique/identifier/counter
|
108 |
|
|
);
|
109 |
|
|
end component;
|
110 |
|
|
-- for search_item_0: search_item use entity work.search_item;
|
111 |
|
|
--
|
112 |
|
|
signal state : integer range 0 to 16 := 16;
|
113 |
|
|
-- pxdata: store price_packet
|
114 |
|
|
signal store_px_type: std_logic_vector(4 downto 0) := (others => '0');
|
115 |
|
|
signal store_buy_sell: std_logic_vector(2 downto 0) := (others => '0'); -- 111 buy, 000 sell
|
116 |
|
|
signal store_px: std_logic_vector(15 downto 0) := (others => '0'); -- price
|
117 |
|
|
signal store_qty: std_logic_vector(15 downto 0) := (others => '0'); -- quantity
|
118 |
|
|
signal store_sec: std_logic_vector(55 downto 0) := (others => '0'); -- 7x 8bits securities identifier
|
119 |
|
|
signal store_id: std_logic_vector(15 downto 0) := (others => '0'); -- unique/identifier/counter
|
120 |
|
|
-- control flag(s) on the incoming bus
|
121 |
|
|
signal b1_px_valid: std_logic;
|
122 |
|
|
-- pxdata: b1 price_packet
|
123 |
|
|
signal b1_px_type: std_logic_vector(4 downto 0) := (others => '0');
|
124 |
|
|
signal b1_buy_sell: std_logic_vector(2 downto 0) := (others => '0'); -- 111 buy, 000 sell
|
125 |
|
|
signal b1_px: std_logic_vector(15 downto 0) := (others => '0'); -- price
|
126 |
|
|
signal b1_qty: std_logic_vector(15 downto 0) := (others => '0'); -- quantity
|
127 |
|
|
signal b1_sec: std_logic_vector(55 downto 0) := (others => '0'); -- 7x 8bits securities identifier
|
128 |
|
|
signal b1_id: std_logic_vector(15 downto 0) := (others => '0'); -- unique/identifier/counter
|
129 |
|
|
-- pxdata: b2 price_packet
|
130 |
|
|
signal b2_px_type: std_logic_vector(4 downto 0) := (others => '0');
|
131 |
|
|
signal b2_buy_sell: std_logic_vector(2 downto 0) := (others => '0'); -- 111 buy, 000 sell
|
132 |
|
|
signal b2_px: std_logic_vector(15 downto 0) := (others => '0'); -- price
|
133 |
|
|
signal b2_qty: std_logic_vector(15 downto 0) := (others => '0'); -- quantity
|
134 |
|
|
signal b2_sec: std_logic_vector(55 downto 0) := (others => '0'); -- 7x 8bits securities identifier
|
135 |
|
|
signal b2_id: std_logic_vector(15 downto 0) := (others => '0'); -- unique/identifier/counter
|
136 |
|
|
begin
|
137 |
|
|
items_array : for iter_id in 0 to searchitems - 1 generate
|
138 |
|
|
begin
|
139 |
|
|
cell_item : entity work.search_item
|
140 |
|
|
generic map ( item_id => std_logic_vector(to_unsigned(iter_id,16)) )
|
141 |
|
|
port map (
|
142 |
|
|
RX_CLK => RX_CLK,
|
143 |
|
|
b1_px_valid => b1_px_valid,
|
144 |
|
|
b1_px_type => b1_px_type, b1_buy_sell => b1_buy_sell, b1_px => b1_px, b1_qty => b1_qty, b1_sec => b1_sec, b1_id => b2_id,
|
145 |
|
|
b2_px_type => b2_px_type, b2_buy_sell => b2_buy_sell, b2_px => b2_px, b2_qty => b2_qty, b2_sec => b2_sec, b2_id => b2_id
|
146 |
|
|
);
|
147 |
|
|
end generate items_array;
|
148 |
|
|
|
149 |
|
|
match: process (RX_CLK) is
|
150 |
|
|
begin
|
151 |
|
|
if rising_edge(RX_CLK) then
|
152 |
|
|
if search_px_valid_i = '1' then
|
153 |
|
|
|
154 |
|
|
if search_px_type_i = std_logic_vector'("00000") then
|
155 |
|
|
-- do reset store and outputs
|
156 |
|
|
store_px_type <= (others => '0');
|
157 |
|
|
store_buy_sell <= (others => '0'); -- 111 buy, 000 sell
|
158 |
|
|
store_px <= (others => '0'); -- price
|
159 |
|
|
store_qty <= (others => '0'); -- quantity
|
160 |
|
|
store_sec <= (others => '0'); -- 7x 8bits securities identifier
|
161 |
|
|
-- not reset / generic store_id <= (others => '0'); -- unique/identifier/counter
|
162 |
|
|
--
|
163 |
|
|
b2_px_type <= (others => 'Z');
|
164 |
|
|
b2_buy_sell <= (others => 'Z'); -- 111 buy, 000 sell
|
165 |
|
|
b2_px <= (others => 'Z'); -- price
|
166 |
|
|
b2_qty <= (others => 'Z'); -- quantity
|
167 |
|
|
b2_sec <= (others => 'Z'); -- 7x 8bits securities identifier
|
168 |
|
|
b2_id <= (others => 'Z'); -- unique/identifier/counter
|
169 |
|
|
--
|
170 |
|
|
b2_px_type <= std_logic_vector'(std_logic_vector'("00000"));
|
171 |
|
|
state <= 8;
|
172 |
|
|
|
173 |
|
|
elsif search_px_type_i = std_logic_vector'("00110") then
|
174 |
|
|
-- do set store from incoming price
|
175 |
|
|
store_px_type <= b1_px_type;
|
176 |
|
|
store_buy_sell <= b1_buy_sell;
|
177 |
|
|
store_px <= b1_px;
|
178 |
|
|
store_qty <= b1_qty;
|
179 |
|
|
store_sec <= b1_sec;
|
180 |
|
|
store_id <= b1_id;
|
181 |
|
|
--
|
182 |
|
|
b2_px_type <= std_logic_vector'(std_logic_vector'("00000"));
|
183 |
|
|
state <= 8;
|
184 |
|
|
|
185 |
|
|
elsif search_px_type_i = std_logic_vector'("00101") then
|
186 |
|
|
-- incoming price, register it and start the state machine
|
187 |
|
|
if (store_sec /= b1_sec or store_buy_sell = b1_buy_sell or store_px_type /= std_logic_vector'(std_logic_vector'("0110")) ) then
|
188 |
|
|
-- not this store_item instance no action, also stop anything that might be going on
|
189 |
|
|
state <= 14;
|
190 |
|
|
elsif (to_integer(unsigned(store_qty)) = 0 or to_integer(unsigned(b1_qty)) = 0 or
|
191 |
|
|
(store_buy_sell = std_logic_vector'("111") and store_px < b1_px) or
|
192 |
|
|
(store_buy_sell = std_logic_vector'("000") and store_px > b1_px) ) then
|
193 |
|
|
-- no deal: this is the correct store_item but there's no match
|
194 |
|
|
b2_px_type <= std_logic_vector'(std_logic_vector'("00000"));
|
195 |
|
|
state <= 8;
|
196 |
|
|
else
|
197 |
|
|
-- send a return order
|
198 |
|
|
b2_px_type <= std_logic_vector'("1010");
|
199 |
|
|
b2_buy_sell <= store_buy_sell; -- 111 buy, 000 sell
|
200 |
|
|
b2_px <= b1_px; -- price
|
201 |
|
|
-- b2_qty <=
|
202 |
|
|
if b1_qty < store_qty then
|
203 |
|
|
b2_qty <= b1_qty;
|
204 |
|
|
else
|
205 |
|
|
b2_qty <= store_qty;
|
206 |
|
|
end if; -- quantity
|
207 |
|
|
b2_sec <= store_sec; -- 7x 8bits securities identifier
|
208 |
|
|
b2_id <= store_id; -- unique/identifier/counter
|
209 |
|
|
-- update the store
|
210 |
|
|
-- store_qty
|
211 |
|
|
if (b1_qty < store_qty) then
|
212 |
|
|
store_qty <= std_logic_vector(to_unsigned( to_integer(unsigned(store_qty)) - to_integer(unsigned(b1_qty)) ,16 ));
|
213 |
|
|
else
|
214 |
|
|
store_qty <= (others => '0');
|
215 |
|
|
state <= 1;
|
216 |
|
|
end if;
|
217 |
|
|
end if;
|
218 |
|
|
|
219 |
|
|
else
|
220 |
|
|
-- no action
|
221 |
|
|
null;
|
222 |
|
|
end if; -- search_px_type
|
223 |
|
|
|
224 |
|
|
else -- search_px_valid_i
|
225 |
|
|
-- no incoming search_px_i so check for state machine actions
|
226 |
|
|
case state is
|
227 |
|
|
when 1 =>
|
228 |
|
|
-- sent return order, so clean up
|
229 |
|
|
b2_px_type <= (others => 'Z');
|
230 |
|
|
b2_buy_sell <= (others => 'Z'); -- 111 buy, 000 sell
|
231 |
|
|
b2_px <= (others => 'Z'); -- price
|
232 |
|
|
b2_qty <= (others => 'Z'); -- quantity
|
233 |
|
|
b2_sec <= (others => 'Z'); -- 7x 8bits securities identifier
|
234 |
|
|
b2_id <= (others => 'Z'); -- unique/identifier/counter
|
235 |
|
|
state <= 16;
|
236 |
|
|
|
237 |
|
|
when 8 =>
|
238 |
|
|
-- correct store_item but there was no match
|
239 |
|
|
b2_px_type <= std_logic_vector'("ZZZZZ");
|
240 |
|
|
state <= 16;
|
241 |
|
|
|
242 |
|
|
when others => null;
|
243 |
|
|
end case; -- state
|
244 |
|
|
|
245 |
|
|
if (state < 16) then
|
246 |
|
|
state <= state + 1;
|
247 |
|
|
end if;
|
248 |
|
|
|
249 |
|
|
end if; -- search_px_valid_i
|
250 |
|
|
|
251 |
|
|
end if;
|
252 |
|
|
end process match;
|
253 |
|
|
|
254 |
|
|
end search_control_implementation;
|
255 |
|
|
|