@@ -38,6 +38,11 @@ entity PeekRingBuffer is
3838 DataEndAddress : out std_logic_vector (PeekRamDepth - 1 downto 0 );
3939 PeekAddress : in std_logic_vector (PeekRamDepth - 1 downto 0 );
4040 PopAddress : in std_logic_vector (PeekRamDepth - 1 downto 0 );
41+ LastHeaderEnd : out std_logic_vector (PeekRamDepth - 1 downto 0 );
42+ LastFooterEnd : out std_logic_vector (PeekRamDepth - 1 downto 0 );
43+ PopAddress : in std_logic_vector (PeekRamDepth - 1 downto 0 );
44+ PayloadLen : out std_logic_vector (31 downto 0 );
45+ HeaderFooterPayloadLenMatches : out std_logic ;
4146 ByteIn : in std_logic_vector (7 downto 0 );
4247 ByteOut : out std_logic_vector (7 downto 0 );
4348 WriteReq : in std_logic ;
@@ -68,11 +73,49 @@ architecture PeekRingBufferImplemenatation of PeekRingBuffer is
6873 );
6974 end component ;
7075
76+ component PatternFinder is
77+ generic (
78+ Byte0 : std_logic_vector (7 downto 0 ) := x"00" ;
79+ Byte1 : std_logic_vector (7 downto 0 ) := x"00" ;
80+ Byte2 : std_logic_vector (7 downto 0 ) := x"00" ;
81+ Byte3 : std_logic_vector (7 downto 0 ) := x"00" --;
82+ );
83+ port (
84+ clk : in std_logic ;
85+ rst : in std_logic ;
86+
87+ -- Bus:
88+ ByteIn : in std_logic_vector (7 downto 0 );
89+ WriteReq : in std_logic ;
90+ Found : out std_logic --;
91+ );
92+ end component ;
93+
94+ component FieldLatcher is
95+ port (
96+ clk : in std_logic ;
97+ rst : in std_logic ;
98+
99+ -- Bus:
100+ ByteIn : in std_logic_vector (7 downto 0 );
101+ WriteReq : in std_logic ;
102+ FieldLatched : out std_logic_vector (31 downto 0 )--;
103+ );
104+ end component ;
105+
106+
71107 signal DataStartAddress_i : std_logic_vector (PeekRamDepth - 1 downto 0 );
72108 signal WriteAddress : std_logic_vector (PeekRamDepth - 1 downto 0 );
73109
74110 signal LastPopReq : std_logic ;
75111 signal LastWriteReq : std_logic ;
112+
113+ signal HeaderFound : std_logic ;
114+ signal FooterFound : std_logic ;
115+ signal LastHeaderFound : std_logic ;
116+ signal LastFooterFound : std_logic ;
117+
118+ signal MaybePayloadLen : std_logic_vector (31 downto 0 );
76119
77120 begin
78121
@@ -88,6 +131,50 @@ architecture PeekRingBufferImplemenatation of PeekRingBuffer is
88131 WriteReq => WriteReq
89132 );
90133
134+ HeaderFinder : PatternFinder
135+ generic map
136+ (
137+ Byte0 => x"1B" ,
138+ Byte1 => x"AD" ,
139+ Byte2 => x"BA" ,
140+ Byte3 => x"BE" --,
141+ );
142+ port map
143+ (
144+ clk => clk,
145+ rst => rst,
146+ ByteIn => ByteIn,
147+ WriteReq => WriteReq,
148+ Found => HeaderFound--,
149+ );
150+
151+ FooterFinder : PatternFinder
152+ generic map
153+ (
154+ Byte0 => x"0A" ,
155+ Byte1 => x"0F" ,
156+ Byte2 => x"AD" ,
157+ Byte3 => x"ED" --,
158+ );
159+ port map
160+ (
161+ clk => clk,
162+ rst => rst,
163+ ByteIn => ByteIn,
164+ WriteReq => WriteReq,
165+ Found => FooterFound--,
166+ );
167+
168+ PayloadLenLatcher : FieldLatcher
169+ port map
170+ (
171+ clk => clk,
172+ rst => rst,
173+ ByteIn => ByteIn,
174+ WriteReq => WriteReq,
175+ FieldLatched => MaybePayloadLen--,
176+ );
177+
91178 process (clk, rst)
92179 begin
93180
@@ -114,12 +201,17 @@ architecture PeekRingBufferImplemenatation of PeekRingBuffer is
114201 LastWriteReq <= '0' ;
115202 DataStartAddress_i <= (others => '0' );
116203 WriteAddress <= (others => '0' );
204+ LastHeaderEnd <= (others => '0' );
205+ LastFooterEnd <= (others => '0' );
206+ PayloadLen <= x"00000000" ;
117207
118208 else
119209 if ( (clk'event ) and (clk = '1' ) ) then
120210
121211 LastPopReq <= PopReq;
122212 LastWriteReq <= WriteReq;
213+ LastHeaderFound <= HeaderFound;
214+ LastFooterFound <= FooterFound;
123215
124216 if ( (LastPopReq = '0' ) and (PopReq = '1' ) ) then
125217
@@ -133,21 +225,68 @@ architecture PeekRingBufferImplemenatation of PeekRingBuffer is
133225
134226 if (WriteAddress < ( (2 ** PeekRamDepth) - 1 ) ) then
135227
136- --~ WriteAddress <= WriteAddress + std_logic_vector(to_unsigned(1, PeekRamDepth));
137- WriteAddress <= WriteAddress + "0000000001" ;
228+ WriteAddress <= WriteAddress + std_logic_vector (to_unsigned (1 , PeekRamDepth));
229+ --~ WriteAddress <= WriteAddress + "0000000001";
138230
139231 else --wrap
140232
141233 WriteAddress <= (others => '0' );
142234
143235 end if ;
144-
236+
237+ --If we wrap footer or header, clear!
238+ if (LastHeaderEnd = WriteAddress + "0000000001" ) then LastHeaderEnd <= (others => '0' ); end if ;
239+
240+ if (LastFooterEnd = WriteAddress + "0000000001" ) then LastFooterEnd <= (others => '0' ); end if ;
241+
242+ --Grab the payload len?
243+
244+ if (WriteAddress >= LastHeaderEnd) then
245+
246+ if (WriteAddress = (LastHeaderEnd + 3 ) then PayloadLen <= MaybePayloadLen; end if ;
247+
248+ else
249+
250+ if (WriteAddress = (LastHeaderEnd + 3 - (2 ** PeekRamDepth)) then PayloadLen <= MaybePayloadLen; end if ; --!!!this calc is WRONG!!! Needs to WRAP correctly...
251+
252+ end if ;
253+
145254 else
146255
147256 Dbg1 <= '0' ;
148257
149258 end if ;
150259
260+ --Update on the edge of found; can't put this on the writereq edge, because the flag will toggle on the next clock after, not synchrounously!
261+ if ( (LastHeaderFound = '0' ) and (HeaderFound = '1' ) ) then LastHeaderEnd = WriteAddress - "0000000001" ; end if ;
262+
263+ if ( (LastFooterFound = '0' ) and (FooterFound = '1' ) ) then
264+
265+ LastFooterEnd = WriteAddress - "0000000001" ;
266+
267+ --Found a footer! This should initiate more checks; namely, generating & testing the CRC and checking if headerpos +length <+appropriate offsets> = footerpos
268+ --Really need to test & fix CRCer first...
269+ --~ RS422_Rx0_Crcer : CrcFifo
270+ --~ generic map
271+ --~ (
272+ --~ DEPTH_BITS => 10--,
273+ --~ )
274+ --~ port map
275+ --~ (
276+ --~ clk => MasterClk,
277+ --~ rst => Uart0DoCrc,
278+ --~ FifoStartAddr => Uart0CrcStartAddr,
279+ --~ FifoEndAddr => Uart0CrcEndAddr,
280+ --~ FifoPeekData => Uart0RxFifoPeekPeekData,
281+ --~ FifoPeekAddr => Uart0RxFifoPeekPeekAddrCrcer,
282+ --~ Crc => Uart0Crc,
283+ --~ CrcComplete => Uart0CrcDone--,
284+ --~ );
285+
286+ --~ HeaderFooterPayloadLenMatches <=
287+
288+ end if ;
289+
151290 end if ;
152291 end if ;
153292 end process ;
0 commit comments