-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderentry.cpp
1829 lines (1652 loc) · 63.5 KB
/
orderentry.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 Xilinx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include "orderentry.hpp"
void OrderEntry::byterev16(ap_uint<16>&variable)
{
ap_uint<16>num=(variable);
variable=0;
variable.range(7,0)=num.range(15,8);
variable.range(15,8)=num.range(7,0);
}
void OrderEntry::byterev32(ap_uint<32> &variable)
{
ap_uint<32> num = (variable);
variable = 0;
variable.range(7, 0) = num.range(31, 24);
variable.range(15, 8) = num.range(23, 16);
variable.range(23, 16) = num.range(15, 8);
variable.range(31, 24) = num.range(7, 0);
}
#define A 0x67452301
#define B 0xefcdab89
#define C 0x98badcfe
#define D 0x10325476
static uint32_t S[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
static uint32_t K[] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
/*
* Padding used to make the size (in bits) of the input congruent to 448 mod 512
*/
static uint8_t PADDING[] = {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/*
* Bit-manipulation functions defined by the MD5 algorithm
*/
#define F(X, Y, Z) ((X & Y) | (~X & Z))
#define G(X, Y, Z) ((X & Z) | (Y & ~Z))
#define H(X, Y, Z) (X ^ Y ^ Z)
#define I(X, Y, Z) (Y ^ (X | ~Z))
uint32_t rotateLeft(uint32_t x, uint32_t n)
{
return (x << n) | (x >> (32 - n));
}
void md5Init(MD5Context *ctx)
{
#pragma HLS inline off
ctx->size = (uint64_t)0;
ctx->buffer[0] = (uint32_t)A;
ctx->buffer[1] = (uint32_t)B;
ctx->buffer[2] = (uint32_t)C;
ctx->buffer[3] = (uint32_t)D;
}
void md5Update(MD5Context *ctx, uint8_t *input_buffer, size_t input_len)
{
#pragma HLS inline off
uint32_t input[16];
unsigned int offset = ctx->size % 64;
ctx->size += (uint64_t)input_len;
// Copy each byte in input_buffer into the next space in our context input
for (unsigned int i = 0; i < input_len; ++i)
{
ctx->input[offset++] = (uint8_t) * (input_buffer + i);
// If we've filled our context input, copy it into our local array input
// then reset the offset to 0 and fill in a new buffer.
// Every time we fill out a chunk, we run it through the algorithm
// to enable some back and forth between cpu and i/o
if (offset % 64 == 0)
{
for (unsigned int j = 0; j < 16; ++j)
{
// Convert to little-endian
// The local variable `input` our 512-bit chunk separated into 32-bit words
// we can use in calculations
input[j] = (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
(uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
(uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
(uint32_t)(ctx->input[(j * 4)]);
}
md5Step(ctx->buffer, input);
offset = 0;
}
}
}
void md5Finalize(MD5Context *ctx)
{
#pragma HLS inline off
uint32_t input[16];
unsigned int offset = ctx->size % 64;
unsigned int padding_length = offset < 56 ? 56 - offset : (56 + 64) - offset;
// Fill in the padding andndo the changes to size that resulted from the update
md5Update(ctx, PADDING, padding_length);
ctx->size -= (uint64_t)padding_length;
// Do a final update (internal to this function)
// Last two 32-bit words are the two halves of the size (converted from bytes to bits)
for (unsigned int j = 0; j < 14; ++j)
{
input[j] = (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
(uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
(uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
(uint32_t)(ctx->input[(j * 4)]);
}
input[14] = (uint32_t)(ctx->size * 8);
input[15] = (uint32_t)((ctx->size * 8) >> 32);
md5Step(ctx->buffer, input);
// Move the result into digest (convert from little-endian)
for (unsigned int i = 0; i < 4; ++i)
{
ctx->digest[(i * 4) + 0] = (uint8_t)((ctx->buffer[i] & 0x000000FF));
ctx->digest[(i * 4) + 1] = (uint8_t)((ctx->buffer[i] & 0x0000FF00) >> 8);
ctx->digest[(i * 4) + 2] = (uint8_t)((ctx->buffer[i] & 0x00FF0000) >> 16);
ctx->digest[(i * 4) + 3] = (uint8_t)((ctx->buffer[i] & 0xFF000000) >> 24);
}
}
void md5Step(uint32_t *buffer, uint32_t *input)
{
#pragma HLS inline off
uint32_t AA = buffer[0];
uint32_t BB = buffer[1];
uint32_t CC = buffer[2];
uint32_t DD = buffer[3];
uint32_t E;
unsigned int j;
for (unsigned int i = 0; i < 64; ++i)
{
switch (i / 16)
{
case 0:
E = F(BB, CC, DD);
j = i;
break;
case 1:
E = G(BB, CC, DD);
j = ((i * 5) + 1) % 16;
break;
case 2:
E = H(BB, CC, DD);
j = ((i * 3) + 5) % 16;
break;
default:
E = I(BB, CC, DD);
j = (i * 7) % 16;
break;
}
uint32_t temp = DD;
DD = CC;
CC = BB;
BB = BB + rotateLeft(AA + E + K[i] + input[j], S[i]);
AA = temp;
}
buffer[0] += AA;
buffer[1] += BB;
buffer[2] += CC;
buffer[3] += DD;
}
void md5String(unsigned char *input, unsigned char *output, int length)
{
#pragma HLS inline off
MD5Context ctx;
md5Init(&ctx);
md5Update(&ctx, input, length);
md5Finalize(&ctx);
// uint8_t *result = malloc(16);
// memcpy(result, ctx.digest, 16);
for (int i = 0; i < 16; i++)
output[i] = ctx.digest[i];
// return result;
}
/**
* OrderEntry Core
*/
void OrderEntry:: operationPull(ap_uint<32> ®RxOperation,
hls::stream<orderEntryOperationPack_t> &operationStreamPack,
hls::stream<orderEntryOperationPack_t> &operationHostStreamPack,
hls::stream<orderEntryOperation_t> &operationStream
)
{
#pragma HLS PIPELINE II = 1 style = flp
// char *str1 = "vaibhav";
// md5String(str1,7);
mmInterface intf;
orderEntryOperationPack_t operationPack;
orderEntryOperation_t operation ;
static ap_uint<32> countRxOperation = 0;
// priority to direct path from PricingEngine then host offload path
// TODO: add register control to enable/disable these different paths?
if (operationStreamPack.empty())
{
operationPack = operationStreamPack.read();
++countRxOperation;
intf.orderEntryOperationUnpack(&operationPack, &operation);
operationStream.write(operation);
}
else if (!operationHostStreamPack.empty())
{
operationPack = operationHostStreamPack.read();
++countRxOperation;
intf.orderEntryOperationUnpack(&operationPack, &operation);
operationStream.write(operation);
}
regRxOperation = countRxOperation;
return;
}
void OrderEntry::operationEncode(hls::stream<orderEntryOperation_t> &operationStream,
hls::stream<orderEntryOperationEncode_t> &operationEncodeStream)
{
#pragma HLS PIPELINE II = 1 style = flp
orderEntryOperation_t operation;
orderEntryOperationEncode_t operationEncode;
ap_uint<80> orderIdEncode, quantityEncode, priceEncode;
if (!operationStream.empty())
{
operation = operationStream.read();
orderIdEncode = uint32ToAscii(operation.orderId);
quantityEncode = uint32ToAscii(operation.quantity);
priceEncode = uint32ToAscii(operation.price);
operationEncode.timestamp = operation.timestamp;
operationEncode.opCode = operation.opCode;
operationEncode.symbolIndex = operation.symbolIndex;
operationEncode.orderId = orderIdEncode;
operationEncode.quantity = quantityEncode;
operationEncode.price = priceEncode;
operationEncode.direction = operation.direction;
operationEncodeStream.write(operationEncode);
}
return;
}
void OrderEntry::openListenPortTcp(hls::stream<ipTcpListenPortPack_t> &listenPortStream,
hls::stream<ipTcpListenStatusPack_t> &listenStatusStream)
{
#pragma HLS PIPELINE II = 1 style = flp
ipTcpListenPortPack_t listenPortPack;
ipTcpListenStatusPack_t listenStatusPack;
bool listenDone = false;
static ap_uint<2> state = 0;
#pragma HLS RESET variable = state
switch (state)
{
case 0:
{
// TODO: remove hard coded listen port
listenPortPack.data = 7;
listenPortPack.keep = 0x3;
listenPortPack.last = 1;
listenPortStream.write(listenPortPack);
state = 1;
break;
}
case 1:
{
if (!listenStatusStream.empty())
{
listenStatusPack = listenStatusStream.read();
if (1 == listenStatusPack.data)
{
state = 2;
}
else
{
state = 0;
}
}
break;
}
case 2:
{
// IDLE
break;
}
} // switch
}
void OrderEntry::openActivePortTcp(ap_uint<32> ®Control,
ap_uint<32> ®DestAddress,
ap_uint<32> ®DestPort,
ap_uint<32> ®Debug,
hls::stream<ipTuplePack_t> &openConnectionStream,
hls::stream<ipTcpConnectionStatusPack_t> &connectionStatusStream,
hls::stream<ipTcpCloseConnectionPack_t> &closeConnectionStream,
hls::stream<ipTcpTxStatusPack_t> &txStatusStream,
hls::stream<ap_uint<64>> &boxIPPortFIFO,
hls::stream<bool> &sendBoxFIFO,
ap_uint<64> &GRResponseCapture)
{
#pragma HLS PIPELINE II = 1 style = flp
mmInterface intf;
ipTuple_t tuple;
ipTuplePack_t tuplePack;
ipTcpTxStatus_t txStatus;
ipTcpTxStatusPack_t txStatusPack;
ipTcpCloseConnectionPack_t closeConnectionPack;
ipTcpConnectionStatusPack_t connectionStatusPack;
enum stateType
{
IDLE,
INIT_CON,
WAIT_CON,
ACTIVE_CON
};
static stateType state = IDLE;
static ipTcpConnectionStatus_t connectionStatus;
static ap_uint<1> statusConnected = 0;
static ap_uint<16> statusLength = 0;
static ap_uint<30> statusSpace = 0;
static ap_uint<2> statusError = 0;
static ap_uint<16> statusSessionID = 0;
static ap_uint<32> countDebug = 0;
static ap_uint<32> boxIP,boxPort;
static bool grDisconnect=false;
if (!txStatusStream.empty())
{
txStatusPack = txStatusStream.read();
intf.ipTcpTxStatusStreamUnpack(&txStatusPack, &txStatus);
statusLength = txStatus.length;
statusSpace = txStatus.space;
statusError = txStatus.error;
}
switch (state)
{
case IDLE:
{
if (OE_TCP_CONNECT & regControl)
{
countDebug = (countDebug | 0x00000001);
KDEBUG("In IDLE State");
state = INIT_CON;
}
break;
}
case INIT_CON:
{
countDebug = (countDebug | 0x00000020);
tuple.address = regDestAddress;
tuple.port = regDestPort;
intf.ipTuplePack(&tuple, &tuplePack);
tuplePack.last = 1;
tuplePack.keep = 0x3F; // to see which bytes to keep in the tuplePack sent ahead
openConnectionStream.write(tuplePack);
state = WAIT_CON;
break;
}
case WAIT_CON:
{
countDebug = (countDebug | 0x00000300);
if (!connectionStatusStream.empty())
{
countDebug = (countDebug | 0x00004000);
connectionStatusPack = connectionStatusStream.read();
intf.ipTcpConnectionStatusUnpack(&connectionStatusPack, &connectionStatus);
if (connectionStatus.success)
{
countDebug = (countDebug | 0x00050000);
state = ACTIVE_CON;
statusConnected = 0x1;
statusLength = 0x0;
statusSpace = 0xffff;
statusError = TXSTATUS_SUCCESS;
statusSessionID = connectionStatus.sessionID;
if(grDisconnect)//boxConnect
{
KDEBUG("set sendbox to true");
sendBoxFIFO.write(true);
}
}
}
// This code added to allow reconnect or disconnect to get out of WAIT_CON state
// Note 0x007 instead of 0x006 to show this path was taken.
if (0 == (OE_TCP_CONNECT & regControl))
{
countDebug = (countDebug | 0x00700000);
state = IDLE;
statusConnected = 0x0;
statusLength = 0x0;
statusSpace = 0x0;
statusError = TXSTATUS_CLOSED;
}
break;
}
case ACTIVE_CON:
{
countDebug = (countDebug | 0x00600000);
if (0 == (OE_TCP_CONNECT & regControl))
{
KDEBUG("closed connection successfully");
countDebug = (countDebug | 0x07000000);
closeConnectionPack.data = connectionStatus.sessionID;
closeConnectionPack.keep = 0x3;
closeConnectionPack.last = 1;
closeConnectionStream.write(closeConnectionPack);
state = IDLE;
statusConnected = 0x0;
statusLength = 0x0;
statusSpace = 0x0;
statusError = TXSTATUS_CLOSED;
}
if(!boxIPPortFIFO.empty())
{
ap_uint<64> box = boxIPPortFIFO.read();
boxIP = box.range(31,0);
boxPort = box.range(63,32);
GRResponseCapture.range(31,0)=boxIP;
GRResponseCapture.range(63,32)=boxPort;
grDisconnect=true;
KDEBUG("box port received: "<<box);
// regDestAddress=boxIP;
// regDestPort=boxPort;
// regDebug = boxIP;
// state=BOX_CON_OPEN;
}
break;
}
default:
{
countDebug = (countDebug | 0x80000000);
state = IDLE;
break;
}
}
// TODO: transfer via stream interface rather than use private struct member
mConnectionStatus.connected = statusConnected;
mConnectionStatus.length = statusLength;
mConnectionStatus.space = statusSpace;
mConnectionStatus.error = statusError;
mConnectionStatus.sessionID = statusSessionID;
// regDebug = countDebug;
}
void OrderEntry::notificationHandlerTcp(ap_uint<32> ®Notification,
ap_uint<32> ®ReadRequest,
hls::stream<ipTcpNotificationPack_t> ¬ificationStream,
hls::stream<ipTcpReadRequestPack_t> &readRequestStream)
{
#pragma HLS PIPELINE II = 1 style = flp
mmInterface intf;
ipTcpNotification_t notification;
ipTcpNotificationPack_t notificationPack;
ipTcpReadRequest_t readRequest;
ipTcpReadRequestPack_t readRequestPack;
static ap_uint<32> countNotification = 0;
static ap_uint<32> countReadRequest = 0;
if (!notificationStream.empty())
{
notificationPack = notificationStream.read();
++countNotification;
intf.ipTcpNotificationUnpack(¬ificationPack, ¬ification);
if (notification.length != 0)
{
readRequest.sessionID = notification.sessionID;
readRequest.length = notification.length;
intf.ipTcpReadRequestPack(&readRequest, &readRequestPack);
readRequestPack.last = 1;
readRequestPack.keep = 0x3F;
readRequestStream.write(readRequestPack);
++countReadRequest;
}
}
regNotification = countNotification;
regReadRequest = countReadRequest;
}
void OrderEntry::serverProcessTcp(ap_uint<32> ®RxData,
ap_uint<32> ®RxMeta,
ap_uint<64> &GRResponse,
ap_uint<64> &serverProcessState,
hls::stream<ipTcpRxMetaPack_t> &rxMetaStream,
hls::stream<ipTcpRxDataPack_t> &rxDataStream,
hls::stream<ap_uint<64>> &sessKeyFIFO,
hls::stream<ap_uint<64>> &boxIPPortFIFO)
// hls::stream<bool> &sendMSFIFO)
{
#pragma HLS PIPELINE II = 1 style = flp
ipTcpRxMetaPack_t rxMetaPack;
ap_uint<16> sessionID;
ap_axiu<64, 0, 0, 0> currWord;
enum stateType
{
READ_META_GR,
READ_DATA_GR,
PROCESS_GR_IP,
PROCESS_DATA_GR,
READ_META_BOX,
READ_DATA_BOX,
PROCESS_DATA_BOX,
READ_META_MS_SO,
READ_DATA_MS_SO,
PROCESS_DATA_MS_SO
};
static stateType state = READ_META_GR;
static ap_uint<32> countRxData = 0;
static ap_uint<32> countRxMeta = 0;
static ap_uint<608> MS_GR_raw = 0;
static int framecount = 0;
static int start = 0;
static ap_uint<16> tCode;
static ap_uint<832> gr_resp; // 98 = data(76) + 22 bytes of data (length, seqNo, checksum)
static ap_uint<640> box_resp;
static ap_uint<2432> ms_so_resp;
static ap_uint<32> ip[4];
// static ap_uint<832> signon_resp;
static ap_uint<8> dot = '.', current;
serverProcessState = state;
switch (state)
{
case READ_META_GR:
{
// MS_GR header
if (!rxMetaStream.empty())
{
KDEBUG("Read META GR");
// KDEBUG("rx meta not empty: " << rxMetaStream.empty());
rxMetaPack = rxMetaStream.read();
++countRxMeta;
sessionID = rxMetaPack.data;
state = READ_DATA_GR;
}
break;
}
case READ_DATA_GR:
{
// MS_GR data
/*
value = 0;
value |= ipAddrA << 24; ap_uint<8> each
value |= ipAddrB << 16;
value |= ipAddrC << 8;
value |= ipAddrD << 0;
ip_address to send is ap_uint<32>, port as well
*/
if (!rxDataStream.empty())
{
KDEBUG("Read DATA GR");
currWord = rxDataStream.read();
KDEBUG("debug1: "<<(char)currWord.data.range(7,0));
// KDEBUG("serverprocesstcp1: " << currWord.data);
KDEBUG("start: "<<start);
gr_resp.range(start + 63, start) = currWord.data; // var.range(x+2,x) sid
// GRResponse.range(start + 63, start) = currWord.data; // var.range(x+2,x) sid
start += 64;
++countRxData;
if (currWord.last)
{
state = PROCESS_GR_IP;
start = 70;
ip[0] = ip[1] = ip[2] = ip[3] = 0;
framecount =0;
}
// KDEBUG("currword.last: "<<(bool)(currWord.last))
// KDEBUG("current state: "<<state);
}
break;
}
case PROCESS_GR_IP:
{
if(start>=86)
{
state=PROCESS_DATA_GR;
}
current = gr_resp.range(start * 8 + 7, start * 8);
if (current != dot && current!=0)
{
ip[framecount] = ip[framecount] * 10 + (current - 48);
}
else
{
framecount++;
}
start++;
break;
}
case PROCESS_DATA_GR:
{
KDEBUG("Process DATA GR");
tCode = gr_resp.range(23*8+15,23*8);
byterev16(tCode);
// regRxMeta=tCode;
//status code checked
// ap_uint<32> tempIP;
// tempIP = 0;
char sessionKey[8];
ap_uint<32> port, ipaddr;
ap_uint<64> portIP = 0;
ipaddr = (ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];
KDEBUG("ip address:");
KDEBUG(ip[0]);
KDEBUG(ip[1]);
KDEBUG(ip[2]);
KDEBUG(ip[3]);
// KDEBUG(ipaddr);
port = gr_resp.range(89 * 8 + 7, 86 * 8);
byterev32(port);
// KDEBUG(port);
ap_uint<64>sessKey;
for (int ix = 0; ix < 8; ix++){
sessionKey[ix] = (char)gr_resp.range((90 + ix) * 8 + 7, ((90 + ix) * 8));
sessKey.range((ix+1)*8-1,ix*8)=sessionKey[ix];
}
portIP.range(31,0)=ipaddr;
portIP.range(63,32)=port;
KDEBUG(ipaddr << "\t"<< port << "\t"<< portIP);
boxIPPortFIFO.write(portIP);
sessKeyFIFO.write(sessKey);
GRResponse.range(63,32)=port;
GRResponse.range(31,0)=ipaddr;
// if(tCode == MS_GR_SUCCESS)
// | Rx Data Frames | 3232240940 |
// | Rx Meta Frames | 257884160
// regRxData=ipaddr;
// regRxMeta=port;
// 80-83 port
// 84-91 sessionkey
state = READ_META_BOX;
}
case READ_META_BOX:
{
// MS_GR header
if (!rxMetaStream.empty())
{
KDEBUG("Read META BOX");
KDEBUG("rx meta not empty: " << rxMetaStream.empty());
rxMetaPack = rxMetaStream.read();
++countRxMeta;
sessionID = rxMetaPack.data;
state = READ_DATA_BOX;
start=0;
}
break;
}
case READ_DATA_BOX:
{
// BOX data
if (!rxDataStream.empty())
{
KDEBUG("Read DATA BOX"<< rxDataStream.size());
currWord = rxDataStream.read();
KDEBUG("BOX debug: "<<currWord.data<<"\t"<<currWord.last);
KDEBUG("start: "<<start);
box_resp.range(start + 63, start) = currWord.data; // var.range(x+2,x) sid
// GRResponse.range(start + 63, start) = currWord.data; // var.range(x+2,x) sid
start += 64;
++countRxData;
if (currWord.last)
{
state = PROCESS_DATA_BOX;
}
// KDEBUG("currword.last: "<<(bool)(currWord.last))
// KDEBUG("current state: "<<state);
}
break;
}
case PROCESS_DATA_BOX:
{
//23 and 24 are transaction code, 23001 for successful box
KDEBUG("Process DATA BOX");
// 0-21 bytes are pre header
// 0 th byte is (0+1)*8-1,0*8
//22nd byte
tCode = box_resp.range(24*8-1,22*8);
byterev16(tCode);
// regRxMeta=tCode;
KDEBUG("box tCode"<< tCode);
if(tCode == BOX_SUCCESS)
{
// to implement ahead
}
sessKeyFIFO.write(1);// using this to signal that the box message has been processed and MS signon should be sent.
// sendMSFIFO.write(true);
state=READ_META_MS_SO;
break;
}
case READ_META_MS_SO:
{
// MS_GR header
if (!rxMetaStream.empty())
{
KDEBUG("rx meta not empty: " << rxMetaStream.empty());
rxMetaPack = rxMetaStream.read();
++countRxMeta;
sessionID = rxMetaPack.data;
state = READ_DATA_MS_SO;
start=0;
}
break;
}
case READ_DATA_MS_SO:
{
// MS_GR data
if (!rxDataStream.empty())
{
currWord = rxDataStream.read();
KDEBUG("debug1: "<<(char)currWord.data.range(7,0));
KDEBUG("start: "<<start);
ms_so_resp.range(start + 63, start) = currWord.data; // var.range(x+2,x) sid
start += 64;
++countRxData;
if (currWord.last)
{
state = PROCESS_DATA_BOX;
}
// KDEBUG("currword.last: "<<(bool)(currWord.last))
// KDEBUG("current state: "<<state);
}
break;
}
case PROCESS_DATA_MS_SO:
{
//23 and 24 are transaction code, 23001 for successful box
tCode = box_resp.range(23*8+15,23*8);
byterev16(tCode);
// regRxMeta=tCode;
if(tCode ==2301)
{
// to implement ahead
}
// sendMSFIFO.write(true);
break;
}
default:
{
break;
}
}
regRxData = countRxData;
regRxMeta = countRxMeta;
}
void OrderEntry::checksumGenerate(ap_uint<32> ®Control,
hls::stream<orderEntryOperationEncode_t> &operationEncodeStream,
hls::stream<orderEntryOperationEncode_t> &operationEncodeStreamRelay,
hls::stream<sumOperation_t> &sumOperationStream)
{
#pragma HLS PIPELINE II = 1 style = flp
orderEntryOperationEncode_t operationEncode;
sumOperation_t sumOperation;
ap_uint<24> orderIdSum, timestampSum, quantitySum, priceSum, messageSum;
ap_uint<1> validSum = 0;
if (!operationEncodeStream.empty())
{
operationEncode = operationEncodeStream.read();
// if checksum generation is enabled we calculate the partial sum for the payload here to be indicated on the
// TCP kernel via metadata interface, this reduces latency as TCP can begin sending in cut-through mode rather
// than buffer the full packet in store and forward mode
if (OE_TCP_GEN_SUM & regControl)
{
timestampSum = operationEncode.timestamp.range(15, 0);
timestampSum += operationEncode.timestamp.range(31, 16);
timestampSum = (timestampSum + (timestampSum >> 16)) & 0xFFFF;
timestampSum += operationEncode.timestamp.range(47, 32);
timestampSum = (timestampSum + (timestampSum >> 16)) & 0xFFFF;
timestampSum += operationEncode.timestamp.range(63, 48);
timestampSum = (timestampSum + (timestampSum >> 16)) & 0xFFFF;
orderIdSum = operationEncode.orderId.range(15, 0);
orderIdSum += operationEncode.orderId.range(31, 16);
orderIdSum = (orderIdSum + (orderIdSum >> 16)) & 0xFFFF;
orderIdSum += operationEncode.orderId.range(47, 32);
orderIdSum = (orderIdSum + (orderIdSum >> 16)) & 0xFFFF;
orderIdSum += operationEncode.orderId.range(63, 48);
orderIdSum = (orderIdSum + (orderIdSum >> 16)) & 0xFFFF;
orderIdSum += operationEncode.orderId.range(79, 64);
orderIdSum = (orderIdSum + (orderIdSum >> 16)) & 0xFFFF;
quantitySum = operationEncode.quantity.range(15, 0);
quantitySum += operationEncode.quantity.range(31, 16);
quantitySum = (quantitySum + (quantitySum >> 16)) & 0xFFFF;
quantitySum += operationEncode.quantity.range(47, 32);
quantitySum = (quantitySum + (quantitySum >> 16)) & 0xFFFF;
quantitySum += operationEncode.quantity.range(63, 48);
quantitySum = (quantitySum + (quantitySum >> 16)) & 0xFFFF;
quantitySum += operationEncode.quantity.range(79, 64);
quantitySum = (quantitySum + (quantitySum >> 16)) & 0xFFFF;
// the price field is not 16b aligned, need to shift by one byte
priceSum = (operationEncode.price.range(7, 0) << 8);
priceSum += operationEncode.price.range(23, 8);
priceSum = (priceSum + (priceSum >> 16)) & 0xFFFF;
priceSum += operationEncode.price.range(39, 24);
priceSum = (priceSum + (priceSum >> 16)) & 0xFFFF;
priceSum += operationEncode.price.range(55, 40);
priceSum = (priceSum + (priceSum >> 16)) & 0xFFFF;
priceSum += operationEncode.price.range(71, 56);
priceSum = (priceSum + (priceSum >> 16)) & 0xFFFF;
priceSum += operationEncode.price.range(79, 72);
priceSum = (priceSum + (priceSum >> 16)) & 0xFFFF;
// merge template message partial sum with dynamic field updates
messageSum = messageTemplateSum;
messageSum += orderIdSum;
messageSum = (messageSum + (messageSum >> 16)) & 0xFFFF;
messageSum += timestampSum;
messageSum = (messageSum + (messageSum >> 16)) & 0xFFFF;
messageSum += orderIdSum;
messageSum = (messageSum + (messageSum >> 16)) & 0xFFFF;
messageSum += quantitySum;
messageSum = (messageSum + (messageSum >> 16)) & 0xFFFF;
messageSum += priceSum;
messageSum = (messageSum + (messageSum >> 16)) & 0xFFFF;
validSum = 1;
}
else
{
messageSum = 0;
validSum = 0;
}
sumOperation.subSum = messageSum;
sumOperation.validSum = validSum;
sumOperationStream.write(sumOperation);
operationEncodeStreamRelay.write(operationEncode);
}
}
void OrderEntry::operationProcessTcp(ap_uint<32> ®CaptureControl,
ap_uint<32> ®ProcessOperation,
ap_uint<32> ®TxOrder,
ap_uint<32> ®TxData,
ap_uint<32> ®TxMeta,
ap_uint<32> ®TxStatus,
ap_uint<32> ®TxDrop,
ap_uint<1024> ®CaptureBuffer,
ap_uint<32>®TraderId,
ap_uint<32>®BranchID,
ap_uint<32>®BoxID,
ap_uint<32>®Pass1,
ap_uint<32>®Pass2,
ap_uint<64> &operationProcessState,
hls::stream<orderEntryOperationEncode_t> &operationEncodeStream,
hls::stream<sumOperation_t> &sumOperationStream,
hls::stream<ipTcpTxMetaPack_t> &txMetaStream,
hls::stream<ipTcpTxDataPack_t> &txDataStream,
hls::stream<ap_uint<64>> &sessKeyFIFO,
hls::stream<bool> &sendBoxFIFO)
// hls::stream<bool> &sendMSFIFO)
{
#pragma HLS PIPELINE II = 1 style = flp
mmInterface intf;
orderEntryOperationEncode_t operationEncode;
sumOperation_t sumOperation;
ipTcpTxMeta_t txMeta;
ipTcpTxMetaPack_t txMetaPack;
ipTcpTxDataPack_t txDataPack;
ap_uint<16> length;
ap_uint<64> frameData;
ap_uint<32> rangeIndexHigh, rangeIndexLow;
static unsigned char md5Checksum[16];
enum stateType
{
PRE_IDLE,
IDLE,
SEND_MS_GR_META,
SEND_MS_GR_DATA,
WAIT_MS_GR_DATA,
SEND_MS_BOX_META,
SEND_MS_BOX_DATA,
WAIT_MS_BOX_DATA,
SEND_MS_SO_META,
SEND_MS_SO_DATA,
WAIT_MS_SO_DATA,
};
static stateType state = PRE_IDLE;
static ap_uint<32> frameCount = 0;
static orderEntryMessagePack_t messagePack = {0};
static ap_uint<32> countProcessOperation = 0;
static ap_uint<32> countTxOrder = 0;
static ap_uint<32> countTxData = 0;
static ap_uint<32> countTxMeta = 0;
static ap_uint<32> countTxDrop = 0;
static ap_uint<32> countDebug = 0;
static PRE_PACKET gr_req;
static PRE_PACKET box_req;
static PRE_PACKET signon_req;
static MS_GR_REQUEST req1;
static MESSAGE_HEADER header;
static SIGNON signon;
static ap_uint<16> boxID, branchID;
static ap_uint<32> traderID;
static char pass[8];
static ap_uint<32> seqNo = 1;
static int metaoffset = 22;
// static unsigned char packet_bytes_send_gr[metaoffset+48];