Skip to content

Commit 50ea989

Browse files
committed
setup orderbook, a bit on decode
1 parent e8c3a8d commit 50ea989

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

pipebomb/rtl/msg_decode.sv

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import orderbook_pkg::*;
2+
3+
module msg_decode (
4+
input logic clk,
5+
input logic rst_n,
6+
7+
input [296:0] parser_data,
8+
input logic parser_valid,
9+
10+
output inst_t inst
11+
);
12+
13+
logic [7:0] msg_type;
14+
logic [63:0] order_id;
15+
logic [63:0] old_order_id; // replace only
16+
logic [15:0] locate;
17+
logic buy_side;
18+
logic [31:0] price;
19+
logic [31:0] num_shares;
20+
logic [31:0] seqnum32;
21+
logic [47:0] timestamp;
22+
23+
always_comb begin
24+
{
25+
msg_type,
26+
order_id,
27+
old_order_id,
28+
locate,
29+
buy_size,
30+
price,
31+
num_shares,
32+
seqnum32,
33+
timestamp
34+
} = parser_data;
35+
end
36+
37+
endmodule

pipebomb/rtl/orderbook_pkg.sv

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
package orderbook_pkg;
2+
3+
// First, these defs are for external ITCH messages. I'm not sure we'll use
4+
// these. See https://github.com/mbattyani/sub-25-ns-nasdaq-itch-fpga-parser/blob/main/nasdaq-itch-parser.lisp.
5+
typedef struct packed {
6+
logic [15:0] locate;
7+
logic [15:0] tracking;
8+
logic [47:0] timestamp;
9+
logic [7:0] event_code;
10+
} raw_sysevent_t;
11+
12+
typedef struct packed {
13+
logic [15:0] locate;
14+
logic [15:0] tracking;
15+
logic [47:0] timestamp;
16+
logic [63:0] symbol;
17+
logic [7:0] trading_state;
18+
} raw_sta_t;
19+
20+
typedef struct packed {
21+
logic [15:0] locate;
22+
logic [15:0] tracking;
23+
logic [47:0] timestamp;
24+
logic [63:0] symbol;
25+
logic [7:0] regsho_action;
26+
} raw_regsho_t;
27+
28+
typedef struct packed {
29+
logic [15:0] locate;
30+
logic [15:0] tracking;
31+
logic [47:0] timestamp;
32+
logic [63:0] order_id;
33+
logic [7:0] buy_sell;
34+
logic [31:0] num_shares;
35+
logic [63:0] symbol;
36+
logic [31:0] price;
37+
} raw_add_t;
38+
39+
typedef struct packed {
40+
logic [15:0] locate;
41+
logic [15:0] tracking;
42+
logic [47:0] timestamp;
43+
logic [63:0] order_id;
44+
logic [7:0] buy_sell;
45+
logic [31:0] num_shares;
46+
logic [63:0] symbol;
47+
logic [31:0] price;
48+
logic [31:0] attribution;
49+
} raw_addwmpid_t;
50+
51+
typedef struct packed {
52+
logic [15:0] locate;
53+
logic [15:0] tracking;
54+
logic [47:0] timestamp;
55+
logic [63:0] old_order_id;
56+
logic [63:0] new_order_id;
57+
logic [31:0] num_shares;
58+
logic [31:0] price;
59+
} raw_replace_t;
60+
61+
typedef struct packed {
62+
logic [15:0] locate;
63+
logic [15:0] tracking;
64+
logic [47:0] timestamp;
65+
logic [63:0] order_id;
66+
logic [31:0] num_shares;
67+
logic [63:0] match_number;
68+
} raw_exec_t;
69+
70+
typedef struct packed {
71+
logic [15:0] locate;
72+
logic [15:0] tracking;
73+
logic [47:0] timestamp;
74+
logic [63:0] order_id;
75+
logic [31:0] num_shares;
76+
logic [63:0] match_number;
77+
logic [7:0] printable;
78+
logic [31:0] price;
79+
} raw_execwp_t;
80+
81+
typedef struct packed {
82+
logic [15:0] locate;
83+
logic [15:0] tracking;
84+
logic [47:0] timestamp;
85+
logic [63:0] order_id;
86+
logic [31:0] num_shares;
87+
} raw_cancel_t;
88+
89+
typedef struct packed {
90+
logic [15:0] locate;
91+
logic [15:0] tracking;
92+
logic [47:0] timestamp;
93+
logic [63:0] order_id;
94+
} raw_delete_t;
95+
96+
typedef struct packed {
97+
logic [15:0] locate;
98+
logic [15:0] tracking;
99+
logic [47:0] timestamp;
100+
logic [63:0] order_id;
101+
logic [7:0] buy_sell;
102+
logic [31:0] num_shares;
103+
logic [63:0] symbol;
104+
logic [31:0] price;
105+
logic [63:0] match_number;
106+
} raw_trade_t;
107+
108+
typedef struct packed {
109+
logic [15:0] locate;
110+
logic [15:0] tracking;
111+
logic [47:0] timestamp;
112+
logic [31:0] num_shares_msb;
113+
logic [31:0] num_shares;
114+
logic [31:0] price;
115+
logic [63:0] match_number;
116+
logic [7:0] cross_type;
117+
} raw_crosstrade_t;
118+
119+
// Params used for internal bit depths thoughout the core.
120+
// Ensure these match the values in other parts of the repo.
121+
parameter int PRICE_BITS = 32;
122+
parameter int QUANTITY_BITS = 32;
123+
parameter int ORDER_ID_BITS = 32;
124+
parameter int RING_SIZE = 1024; // Size of side processor buffers
125+
parameter int CACHE_SIZE = 5; // Size of k-best cache
126+
parameter int MAX_ORDERS = 1024; // Order map size
127+
128+
// The ITCH instructions that have an effect on the book. These types are
129+
// straight from the parser.
130+
typedef enum logic [3:0] {
131+
OP_SYSEVENT = 4'h0,
132+
OP_STA = 4'h1,
133+
OP_REGSHO = 4'h2,
134+
OP_ADD = 4'h3,
135+
OP_ADDWMPID = 4'h4,
136+
OP_REPLACE = 4'h5,
137+
OP_EXEC = 4'h6,
138+
OP_EXECWP = 4'h7,
139+
OP_CANCEL = 4'h8,
140+
OP_DELETE = 4'h9,
141+
OP_TRADE = 4'ha,
142+
OP_CROSSTRADE = 4'hb
143+
} opcode_t;
144+
145+
// These structs are now internal.
146+
// For instance, price bucketing can allow us to use less area and have
147+
// better cache usage. This bucketing isn't a concern of the parser, it's
148+
// a concern of the decoder.
149+
typedef struct packed {
150+
itch_msg_type_t msg_type;
151+
logic [ORDER_ID_BITS-1:0] order_id;
152+
order_side_t side;
153+
logic [PRICE_BITS-1:0] price;
154+
logic [QUANTITY_BITS-1:0] quantity;
155+
} int_add_t;
156+
157+
typedef struct packed {
158+
itch_msg_type_t msg_type;
159+
logic [ORDER_ID_BITS-1:0] order_id;
160+
logic [QUANTITY_BITS-1:0] quantity;
161+
} int_exec_t;
162+
163+
typedef struct packed {
164+
itch_msg_type_t msg_type;
165+
logic [ORDER_ID_BITS-1:0] order_id;
166+
logic [QUANTITY_BITS-1:0] quantity;
167+
logic [PRICE_BITS-1:0] price;
168+
} int_execwp_t;
169+
170+
typedef struct packed {
171+
itch_msg_type_t msg_type;
172+
logic [ORDER_ID_BITS-1:0] order_id;
173+
logic [QUANTITY_BITS-1:0] quantity;
174+
} int_cancel_t;
175+
176+
typedef struct packed {
177+
itch_msg_type_t msg_type;
178+
logic [ORDER_ID_BITS-1:0] order_id;
179+
} int_delete_t;
180+
181+
typedef struct packed {
182+
itch_msg_type_t msg_type;
183+
logic [ORDER_ID_BITS-1:0] old_order_id;
184+
logic [ORDER_ID_BITS-1:0] new_order_id;
185+
logic [PRICE_BITS-1:0] price;
186+
logic [QUANTITY_BITS-1:0] quantity;
187+
} int_replace_t;
188+
189+
// Might need more of these for trade, crosstrade, etc. Idk much about
190+
// those rn.
191+
typedef union packed {
192+
int_add_t add;
193+
int_exec_t exec;
194+
int_execwp_t execwp;
195+
int_cancel_t cancel;
196+
int_delete_t delete;
197+
int_replace_t replace;
198+
} inst_t;
199+
200+
// The effect of an order on the book.
201+
typedef struct packed {
202+
logic valid;
203+
order_side_t side;
204+
logic [PRICE_BITS-1:0] price;
205+
logic [QUANTITY_BITS-1:0] quantity;
206+
} book_effect_t;
207+
208+
// An entry in the k-best.
209+
typedef struct packed {
210+
logic [PRICE_BITS-1:0] price;
211+
logic [QUANTITY_BITS-1:0] total;
212+
} price_level_t;
213+
214+
// The output of the order book that drives strategy.
215+
typedef struct packed {
216+
logic [PRICE_BITS-1:0] best_bid_price;
217+
logic [QUANTITY_BITS-1:0] best_bid_quantity;
218+
logic [PRICE_BITS-1:0] best_ask_price;
219+
logic [QUANTITY_BITS-1:0] best_ask_quantity;
220+
price_level_t bid_levels[CACHE_SIZE-1];
221+
price_level_t ask_levels[CACHE_SIZE-1];
222+
logic [PRICE_BITS-1:0] vwap;
223+
logic [PRICE_BITS-1:0] moving_avg_short;
224+
logic [PRICE_BITS-1:0] moving_avg_long;
225+
logic [PRICE_BITS-1:0] volatility;
226+
logic [QUANTITY_BITS-1:0] order_imbalance;
227+
} accumulators_t;
228+
229+
endpackage

0 commit comments

Comments
 (0)