
RiskGate-ML is a pre-trade risk gateway: the hardware check every order must pass before a bank's trading system is allowed to send it to an exchange. Regulators require these controls (in Canada, NI 23-103; in the US, SEC Rule 15c3-5). Knight Capital lost $440M in 45 minutes in 2012 because a malfunctioning algorithm faced no such check.
Each order (side, price, quantity) is checked by two layers; the decision is committed 4 clock cycles (160 ns at 25 MHz) after the SUBMIT write:
1. Hard rules (deterministic, always enforced)
| Check | Rejects when |
|---|---|
| Kill switch | KILL pin high, or CTRL.kill set |
| Fat-finger price band | |price − reference| > BAND |
| Order size | qty = 0 or qty > MAX_QTY |
| Position limit | |net position after the order| > 8 × POS_LIMIT |
| Rate throttle | ≥ RATE_MAX order attempts in the current window of RATE_WIN+1 ticks |
2. ML anomaly detector (decision tree, trained offline)
A depth-3 decision tree looks at six features the chip computes itself:
| # | Feature |
|---|---|
| f0 | |price − reference| |
| f1 | quantity |
| f2 | |position after order| / 8 (saturating) |
| f3 | order attempts in the current window |
| f4 | ticks since the previous order (saturating) |
| f5 | consecutive orders on the same side, including this one (saturating) |
The tree is trained in Python on synthetic order flow that contains realistic failure modes (runaway algorithm, fat-finger near-miss, momentum ignition, quote stuffing). The training finds the globally optimal depth-3 tree with 8-bit integer thresholds: the most anomalies caught under a fixed false-alarm budget, with a proven bound. The chip evaluates the tree along one root-to-leaf path, one level per clock, so it needs a single feature selector and comparator.
The tree's nodes and leaf labels are registers, loaded at start-up, so the model can be retrained after tape-out. The ML layer can only add rejections, never approve an order that broke a hard rule. A shadow mode (ml_en=1, ml_enforce=0) reports what the model would block without blocking it, which is how banks validate a new model before trusting it.
Time base. A tick is 2^TIMEBASE clock cycles. In manual-time mode, ticks are only produced by writing CMD.step. This makes market replays fully deterministic: the chip reproduces the Python golden model decision-for-decision.
Writes: put the register address on uio[3:0] and the data on ui_in, then pulse uio[4] (WR)
high for at least 3 clocks and low for at least 3 clocks. WR and KILL are synchronised on-chip.
Reads: put the address on uio[3:0]; uo_out shows the register (when CTRL.disp = 0).
| Addr | Write | Read |
|---|---|---|
| 0 | CTRL: [0] 7-seg display [1] ml_en [2] ml_enforce [3] kill [4] manual time | STATUS: [0] kill [1] price [2] size [3] position [4] rate [5] ML flag [6] accepted [7] toggles per order |
| 1 | PRICE | position [7:0] |
| 2 | QTY | position [11:8], sign-extended |
| 3 | SUBMIT: [0] side (0 = buy, 1 = sell); evaluates the order | f0 |
| 4 | REF price | f1 |
| 5 | BAND | f2 |
| 6 | MAX_QTY | f3 |
| 7 | POS_LIMIT (units of 8 shares) | f4 |
| 8 | RATE_MAX | leaf index of last order |
| 9 | RATE_WIN | ticks elapsed in window |
| A | TIMEBASE [3:0] | f5 |
| B | NODE_SEL: [6:4] node, [2:0] feature | 0 |
| C | NODE_THR (for the selected node) | 0 |
| D | LEAVES: bit i = leaf i is anomalous | 0 |
| E | SIDE (for the feature preview on reads 3-7 and A) | 0 |
| F | CMD: [0] clear position [1] clear window and streak [2] step tick [3] clear status | ID = 0x52 |
Tree nodes use heap order (children of node n are 2n+1 and 2n+2): go right if feature > threshold. Leaf index = the three branch bits, root first.
STATUS and the LEDs update 4 clocks after the SUBMIT write pulse, which is always before the host can complete its next write. In real-time mode, a tick that falls during the 3-clock tree walk is applied right after it (use TIMEBASE >= 2 so ticks are never closer than that).
The host-written registers are latches (half the area of flip-flops), loaded with their reset values while rst_n is low.
Reset defaults: all limits 255 (permissive), REF 100, TIMEBASE 10, ML off, all nodes "always left", all leaves normal.
demo/tree_config.py (generated by ml/train.py): for each node n, write
NODE_SEL = (n << 4) | feature, then NODE_THR = threshold; finally write LEAVES.demo/riskgate_demo.py does all of this from the demo board's RP2040 and replays normal trading,
a runaway algorithm, a fat-finger order and quote stuffing. Set CTRL bit 0 to show each decision
on the 7-segment display: A accepted, H kill, P price band, 5 size, L position
limit, r rate limit, d ML detector; the decimal point lights when the ML flagged the order.
None required. The demo board's 7-segment display shows decisions; optional LEDs on uio[6] (accept) and uio[7] (reject), and an optional push button on uio[5] as a physical kill switch.
| # | Input | Output | Bidirectional |
|---|---|---|---|
| 0 | DATA0 | RDATA0 / SEG_A | ADDR0 (in) |
| 1 | DATA1 | RDATA1 / SEG_B | ADDR1 (in) |
| 2 | DATA2 | RDATA2 / SEG_C | ADDR2 (in) |
| 3 | DATA3 | RDATA3 / SEG_D | ADDR3 (in) |
| 4 | DATA4 | RDATA4 / SEG_E | WR strobe (in) |
| 5 | DATA5 | RDATA5 / SEG_F | KILL switch (in) |
| 6 | DATA6 | RDATA6 / SEG_G | ACCEPT LED (out) |
| 7 | DATA7 | RDATA7 / SEG_DP (ML flag) | REJECT LED (out) |