@@ -29,6 +29,7 @@ module Protocols.PacketStream.Base (
2929 zeroOutInvalidBytesC ,
3030 stripTrailingEmptyC ,
3131 unsafeAbortOnBackpressureC ,
32+ truncateAbortedPackets ,
3233
3334 -- * Components imported from DfConv
3435 void ,
@@ -439,6 +440,41 @@ zeroOutInvalidBytesC = Circuit $ \(fwdIn, bwdIn) -> (bwdIn, fmap (go <$>) fwdIn)
439440 (\ (j :: Index dataWidth ) byte -> if resize j < i then byte else 0x00 )
440441 (_data transferIn)
441442
443+ data TruncateState = Forwarding | Truncating
444+ deriving (Show , ShowX , Eq , Generic , NFDataX )
445+
446+ {- |
447+ When a packet is aborted, this circuit will truncate the current packet by setting the
448+ '_last' field of the transaction to @Just 0@ and the '_abort' field to @True@.
449+ All subsequent transactions will be consumed without being forwarded.
450+ -}
451+ truncateAbortedPackets ::
452+ forall (dom :: Domain ) (dataWidth :: Nat ) (meta :: Type ).
453+ (HiddenClockResetEnable dom , KnownNat dataWidth , ShowX meta ) =>
454+ (1 <= dataWidth ) =>
455+ Circuit
456+ (PacketStream dom dataWidth meta )
457+ (PacketStream dom dataWidth meta )
458+ truncateAbortedPackets = forceResetSanity |> Circuit (unbundle . mealy go Forwarding . bundle)
459+ where
460+ go state (Nothing , _) = (state, (deepErrorX " truncateAbortedPackets: undefined ack" , Nothing ))
461+ go Truncating (Just m2s, _) = (nextState, (PacketStreamS2M True , Nothing ))
462+ where
463+ nextState
464+ | Maybe. isJust m2s. _last = Forwarding
465+ | otherwise = Truncating
466+ go Forwarding (Just m2sLeft, PacketStreamS2M ack) = (nextState, (PacketStreamS2M ack, Just m2sRight))
467+ where
468+ m2sRight
469+ | m2sLeft. _abort = m2sLeft{_last = Just 0 }
470+ | otherwise = m2sLeft
471+
472+ nextState
473+ -- Note that there is no need to move to 'Truncating' if the transfer we
474+ -- see here is the last transfer of a packet (i.e., '_last' is @Just _@).
475+ | Maybe. isNothing m2sLeft. _last && m2sLeft. _abort && ack = Truncating
476+ | otherwise = Forwarding
477+
442478{- |
443479Copy data of a single `PacketStream` to multiple. LHS will only receive
444480an acknowledgement when all RHS receivers have acknowledged data.
0 commit comments