OpenCores
URL https://opencores.org/ocsvn/rio/rio/trunk

Subversion Repositories rio

[/] [rio/] [branches/] [parallelSymbols/] [rtl/] [vhdl/] [RioSerial.vhd] - Blame information for rev 14

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 magro732
-------------------------------------------------------------------------------
2
-- 
3
-- RapidIO IP Library Core
4
-- 
5
-- This file is part of the RapidIO IP library project
6
-- http://www.opencores.org/cores/rio/
7
-- 
8
-- Description
9
-- Containing the transmission channel independent parts of the LP-Serial
10
-- Physical Layer Specification (RapidIO 2.2, part 6).
11
-- 
12
-- To Do:
13
-- -
14
-- 
15
-- Author(s): 
16
-- - Magnus Rosenius, magro732@opencores.org 
17
-- 
18
-------------------------------------------------------------------------------
19
-- 
20
-- Copyright (C) 2013 Authors and OPENCORES.ORG 
21
-- 
22
-- This source file may be used and distributed without 
23
-- restriction provided that this copyright statement is not 
24
-- removed from the file and that any derivative work contains 
25
-- the original copyright notice and the associated disclaimer. 
26
-- 
27
-- This source file is free software; you can redistribute it 
28
-- and/or modify it under the terms of the GNU Lesser General 
29
-- Public License as published by the Free Software Foundation; 
30
-- either version 2.1 of the License, or (at your option) any 
31
-- later version. 
32
-- 
33
-- This source is distributed in the hope that it will be 
34
-- useful, but WITHOUT ANY WARRANTY; without even the implied 
35
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
36
-- PURPOSE. See the GNU Lesser General Public License for more 
37
-- details. 
38
-- 
39
-- You should have received a copy of the GNU Lesser General 
40
-- Public License along with this source; if not, download it 
41
-- from http://www.opencores.org/lgpl.shtml 
42
-- 
43
-------------------------------------------------------------------------------
44
 
45
 
46
-------------------------------------------------------------------------------
47
-- RioSerial
48 8 magro732
--
49 11 magro732
-- Generics
50
-- --------
51
-- TIMEOUT_WIDTH - The number of bits to be used in the portLinkTimeout signal.
52
-- NUMBER_WORDS - The number of parallell words that the data symbols can
53
-- contain. This sizes the data buses. It can be used to increase the bandwidth
54
-- of the core. Note that it cannot be larger than 4. This is since two
55
-- packets may be completed at the same tick and the interface to the
56
-- packetBuffer cannot handle more than one packets in one tick.
57
--
58
-- Signals
59
-- -------
60
-- System signals.
61 8 magro732
-- clk - System clock.
62
-- areset_n - System reset. Asynchronous, active low. 
63
--
64 11 magro732
-- Configuration signals. These are used to change the runtime behaviour.
65 8 magro732
-- portLinkTimeout_i - The number of ticks to wait for a packet-accepted before
66
--   a timeout occurrs.
67
-- linkInitialized_o - Indicates if a link partner is answering with valid
68
--   status-control-symbols. 
69
-- inputPortEnable_i - Activate the input port for non-maintenance packets. If
70
--   deasserted, only non-maintenance packets are allowed.
71
-- outputPortEnable_i - Activate the output port for non-maintenance packets.
72
--   If deasserted, only non-maintenance packets are allowed.
73
--
74
-- This interface makes it possible to read and write ackId in both outbound
75
-- and inbound directions. All input signals are validated by localAckIdWrite.
76
-- localAckIdWrite_i - Indicate if a localAckId write operation is ongoing.
77
--   Usually this signal is high one tick. 
78
-- clrOutstandingAckId_i - Clear outstanding ackId, i.e. reset the transmission
79
--   window. The signal is only read if localAckIdWrite_i is high.
80
-- inboundAckId_i - The value to set the inbound ackId (the ackId that the
81
--  next inbound packet should have) to. This signal is only read if localAckIdWrite
82
--  is high.
83
-- outstandingAckId_i - The value to set the outstanding ackId (the ackId
84
--   transmitted but not acknowledged) to. This signal is only read if localAckIdWrite
85
--   is high.
86
-- outboundAckId_i - The value to set the outbound ackId (the ackId that the
87
--   next outbound packet will have) to. This signal is only read if localAckIdWrite
88
--   is high.
89
-- inboundAckId_o - The current inbound ackId.
90
-- outstandingAckId_o - The current outstanding ackId.
91
-- outboundAckId_o - The current outbound ackId.
92
--
93
-- This is the interface to the packet buffering sublayer. 
94
-- The window signals are used to send packets without removing them from the
95
-- memory storage. This way, many packet can be sent without awaiting
96
-- packet-accepted symbols and if a packet-accepted gets lost, it is possible
97
-- to revert and resend a packet. This is achived by reading readWindowEmpty
98
-- for new packet and asserting readWindowNext when a packet has been sent.
99
-- When the packet-accepted is received, readFrame should be asserted to remove the
100
-- packet from the storage. If a packet-accepted is missing, readWindowReset is
101
-- asserted to set the current packet to read to the one that has not received
102
-- a packet-accepted.
103
-- readFrameEmpty_i - Indicate if a packet is ready in the outbound direction.
104
--   Once deasserted, it is possible to read the packet content using
105
--   readContent_o to update readContentData and readContentEnd.
106
-- readFrame_o - Assert this signal for one tick to discard the oldest packet.
107
--   It should be used when a packet has been fully read, a linkpartner has
108
--   accepted it and the resources occupied by it should be returned to be
109
--   used for new packets.
110
-- readFrameRestart_o - Assert this signal to restart the reading of the
111
--   current packet. readContentData and readContentEnd will be reset to the
112
--   first content of the packet. 
113
-- readFrameAborted_i - This signal is asserted if the current packet was
114
--   aborted while it was written. It is used when a transmitter starts to send a
115
--   packet before it has been fully received and it is cancelled before it is
116
--   completed. A one tick asserted readFrameRestart signal resets this signal.
117
-- readWindowEmpty_i - Indicate if there are more packets to send.
118
-- readWindowReset_o - Reset the current packet to the oldest stored in the memory.
119
-- readWindowNext_o - Indicate that a new packet should be read. Must only be
120
--   asserted if readWindowEmpty is deasserted. It should be high for one tick.
121
-- readContentEmpty_i - Indicate if there are any packet content to be read.
122
--   This signal is updated directly when packet content is written making it
123
--   possible to read packet content before the full packet has been written to
124
--   the memory storage.
125
-- readContent_o - Update readContentData and readContentEnd.
126
-- readContentEnd_i - Indicate if the end of the current packet has been
127
--   reached. When asserted, readContentData is not valid.
128
-- readContentData_i - The content of the current packet.
129
-- writeFrameFull_i - Indicate if the inbound packet storage is ready to accept
130
--   a new packet.
131
-- writeFrame_o - Indicate that a new complete inbound packet has been written.
132
-- writeFrameAbort_o - Indicate that the current packet is aborted and that all
133
--   data written for this packet should be discarded. 
134
-- writeContent_o - Indicate that writeContentData is valid and should be
135
--   written into the packet content storage. 
136
-- writeContentData_o - The content to write to the packet content storage.
137
--
138
-- This is the interface to the PCS (Physical Control Sublayer). Four types of
139
-- symbols exist, idle, control, data and error.
140
-- Idle symbols are transmitted when nothing else can be transmitted. They are
141
-- mainly intended to enforce a timing on the transmitted symbols. This is
142
-- needed to be able to guarantee that a status-control-symbol is transmitted
143
-- at least once every 256 symbol.
144
-- Control symbols contain control-symbols as described by the standard.
145
-- Data symbols contains a 32-bit fragment of a RapidIO packet.
146
-- Error symbols indicate that a corrupted symbol was received. This could be
147
-- used by a PCS layer to indicate that a transmission error was detected and
148
-- that the above layers should send link-requests to ensure the synchronism
149
-- between the link-partners.
150
-- The signals in this interface are:
151
-- portInitialized_i - An asserted signal on this pin indicates that the PCS
152
--   layer has established synchronization with the link and is ready to accept
153
--   symbols. 
154
-- outboundSymbolEmpty_o - An asserted signal indicates that there are no
155
--   outbound symbols to read. Once deasserted, outboundSymbol_o will be
156
--   already be valid. This signal will be updated one tick after
157
--   outboundSymbolRead_i has been asserted.
158
-- outboundSymbolRead_i - Indicate that outboundSymbol_o has been read and a
159
--   new value could be accepted. It should be active for one tick. 
160 11 magro732
-- REMARK: Update this comment...
161
-- outboundSymbol_o - The outbound symbol. It is divided into two parts,
162
--   symbolType and symbolContent.
163
--   symbolType - The two MSB bits are the type of the symbol according to
164
--   table below:
165
--     00=IDLE, the rest of the bits are not used.
166
--     01=CONTROL, the control symbols payload (24 bits) are placed in the MSB
167
--       part of the symbolContent.
168
--     10=ERROR, the rest of the bits are not used.
169
--     11=DATA, all the remaining bits contain the number of valid words and
170
--     the payload of the symbol.
171
--   symbolContent - The rest of the bits are symbol content. If there are
172
--     multiple words in the symbols they must be set to zero. The first
173
--     received word is placed in the MSB part of this field.
174 8 magro732
-- inboundSymbolFull_o - An asserted signal indicates that no more inbound
175
--   symbols can be accepted.
176
-- inboundSymbolWrite_i - Indicate that inboundSymbol_i contains valid
177 11 magro732
--   information that should be forwarded. Should be active for one tick.
178 8 magro732
-- inboundSymbol_i - The inbound symbol. See outboundSymbol_o for formating.
179 4 magro732
-------------------------------------------------------------------------------
180
library ieee;
181
use ieee.std_logic_1164.all;
182
use ieee.numeric_std.all;
183
use work.rio_common.all;
184
 
185
 
186
-------------------------------------------------------------------------------
187
-- Entity for RioSerial.
188
-------------------------------------------------------------------------------
189
entity RioSerial is
190
  generic(
191 13 magro732
    TIMEOUT_WIDTH : natural := 20;
192 11 magro732
    NUMBER_WORDS : natural range 1 to 4 := 1);
193 4 magro732
  port(
194
    -- System signals.
195
    clk : in std_logic;
196
    areset_n : in std_logic;
197
 
198
    -- Status signals for maintenance operations.
199
    portLinkTimeout_i : in std_logic_vector(TIMEOUT_WIDTH-1 downto 0);
200
    linkInitialized_o : out std_logic;
201
    inputPortEnable_i : in std_logic;
202
    outputPortEnable_i : in std_logic;
203
 
204
    -- Support for portLocalAckIdCSR.
205
    localAckIdWrite_i : in std_logic;
206
    clrOutstandingAckId_i : in std_logic;
207
    inboundAckId_i : in std_logic_vector(4 downto 0);
208
    outstandingAckId_i : in std_logic_vector(4 downto 0);
209
    outboundAckId_i : in std_logic_vector(4 downto 0);
210
    inboundAckId_o : out std_logic_vector(4 downto 0);
211
    outstandingAckId_o : out std_logic_vector(4 downto 0);
212
    outboundAckId_o : out std_logic_vector(4 downto 0);
213
 
214
    -- Outbound frame interface.
215
    readFrameEmpty_i : in std_logic;
216
    readFrame_o : out std_logic;
217
    readFrameRestart_o : out std_logic;
218
    readFrameAborted_i : in std_logic;
219
    readWindowEmpty_i : in std_logic;
220
    readWindowReset_o : out std_logic;
221
    readWindowNext_o : out std_logic;
222
    readContentEmpty_i : in std_logic;
223
    readContent_o : out std_logic;
224
    readContentEnd_i : in std_logic;
225 11 magro732
    readContentData_i : in std_logic_vector(2+(32*NUMBER_WORDS-1) downto 0);
226 4 magro732
 
227
    -- Inbound frame interface.
228
    writeFrameFull_i : in std_logic;
229
    writeFrame_o : out std_logic;
230
    writeFrameAbort_o : out std_logic;
231
    writeContent_o : out std_logic;
232 11 magro732
    writeContentData_o : out std_logic_vector(2+(32*NUMBER_WORDS-1) downto 0);
233 4 magro732
 
234
    -- PCS layer signals.
235
    portInitialized_i : in std_logic;
236
    outboundSymbolEmpty_o : out std_logic;
237
    outboundSymbolRead_i : in std_logic;
238 11 magro732
    outboundSymbol_o : out std_logic_vector(((2+32)*NUMBER_WORDS-1) downto 0);
239 4 magro732
    inboundSymbolFull_o : out std_logic;
240
    inboundSymbolWrite_i : in std_logic;
241 11 magro732
    inboundSymbol_i : in std_logic_vector(((2+32)*NUMBER_WORDS-1) downto 0));
242 4 magro732
end entity;
243
 
244
 
245
-------------------------------------------------------------------------------
246
-- Architecture for RioSerial.
247
-------------------------------------------------------------------------------
248
architecture RioSerialImpl of RioSerial is
249
 
250
  component RioFifo1 is
251
    generic(
252
      WIDTH : natural);
253
    port(
254
      clk : in std_logic;
255
      areset_n : in std_logic;
256
 
257
      empty_o : out std_logic;
258
      read_i : in std_logic;
259
      data_o : out std_logic_vector(WIDTH-1 downto 0);
260
 
261
      full_o : out std_logic;
262
      write_i : in std_logic;
263
      data_i : in std_logic_vector(WIDTH-1 downto 0));
264
  end component;
265
 
266
  component RioTransmitter is
267
    generic(
268 11 magro732
      TIMEOUT_WIDTH : natural;
269
      NUMBER_WORDS : natural range 1 to 4 := 1);
270 4 magro732
    port(
271
      clk : in std_logic;
272
      areset_n : in std_logic;
273
 
274
      portLinkTimeout_i : in std_logic_vector(TIMEOUT_WIDTH-1 downto 0);
275
      portEnable_i : in std_logic;
276
 
277
      localAckIdWrite_i : in std_logic;
278
      clrOutstandingAckId_i : in std_logic;
279
      outstandingAckId_i : in std_logic_vector(4 downto 0);
280
      outboundAckId_i : in std_logic_vector(4 downto 0);
281
      outstandingAckId_o : out std_logic_vector(4 downto 0);
282
      outboundAckId_o : out std_logic_vector(4 downto 0);
283
 
284
      portInitialized_i : in std_logic;
285
      txFull_i : in std_logic;
286
      txWrite_o : out std_logic;
287 11 magro732
      txType_o : out std_logic_vector(1 downto 0);
288 4 magro732
      txData_o : out std_logic_vector(31 downto 0);
289
 
290
      txControlEmpty_i : in std_logic;
291
      txControlSymbol_i : in std_logic_vector(12 downto 0);
292
      txControlUpdate_o : out std_logic;
293
      rxControlEmpty_i : in std_logic;
294
      rxControlSymbol_i : in std_logic_vector(12 downto 0);
295
      rxControlUpdate_o : out std_logic;
296
 
297
      linkInitialized_i : in std_logic;
298
      linkInitialized_o : out std_logic;
299
      ackIdStatus_i : in std_logic_vector(4 downto 0);
300
 
301
      readFrameEmpty_i : in std_logic;
302
      readFrame_o : out std_logic;
303
      readFrameRestart_o : out std_logic;
304
      readFrameAborted_i : in std_logic;
305
      readWindowEmpty_i : in std_logic;
306
      readWindowReset_o : out std_logic;
307
      readWindowNext_o : out std_logic;
308
      readContentEmpty_i : in std_logic;
309
      readContent_o : out std_logic;
310
      readContentEnd_i : in std_logic;
311 11 magro732
      readContentWords_i : in std_logic_vector(1 downto 0);
312
      readContentData_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0));
313 4 magro732
  end component;
314
 
315
  component RioReceiver is
316 11 magro732
    generic(
317
      NUMBER_WORDS : natural range 1 to 4 := 1);
318 4 magro732
    port(
319
      clk : in std_logic;
320
      areset_n : in std_logic;
321
 
322
      portEnable_i : in std_logic;
323
 
324
      localAckIdWrite_i : in std_logic;
325
      inboundAckId_i : in std_logic_vector(4 downto 0);
326
      inboundAckId_o : out std_logic_vector(4 downto 0);
327
 
328
      portInitialized_i : in std_logic;
329
      rxEmpty_i : in std_logic;
330
      rxRead_o : out std_logic;
331 11 magro732
      rxType_i : in std_logic_vector(2*NUMBER_WORDS-1 downto 0);
332
      rxData_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
333 4 magro732
 
334 11 magro732
      txControlWrite_o : out std_logic_vector(NUMBER_WORDS-1 downto 0);
335
      txControlSymbol_o : out std_logic_vector(12*NUMBER_WORDS downto 0);
336
      rxControlWrite_o : out std_logic_vector(NUMBER_WORDS-1 downto 0);
337
      rxControlSymbol_o : out std_logic_vector(12*NUMBER_WORDS downto 0);
338 4 magro732
 
339
      ackIdStatus_o : out std_logic_vector(4 downto 0);
340
      linkInitialized_o : out std_logic;
341
 
342
      writeFrameFull_i : in std_logic;
343
      writeFrame_o : out std_logic;
344
      writeFrameAbort_o : out std_logic;
345
      writeContent_o : out std_logic;
346 11 magro732
      writeContentWords_o : out std_logic_vector(1 downto 0);
347
      writeContentData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0));
348 4 magro732
  end component;
349
 
350
  signal linkInitializedRx : std_logic;
351
  signal linkInitializedTx : std_logic;
352
  signal ackIdStatus : std_logic_vector(4 downto 0);
353
 
354 11 magro732
  signal txControlWrite : std_logic_vector(NUMBER_WORDS-1 downto 0);
355
  signal txControlWriteSymbol : std_logic_vector(12*NUMBER_WORDS downto 0);
356
  signal txControlReadEmpty : std_logic_vector(NUMBER_WORDS-1 downto 0);
357
  signal txControlRead : std_logic_vector(NUMBER_WORDS-1 downto 0);
358
  signal txControlReadSymbol : std_logic_vector(12*NUMBER_WORDS downto 0);
359 4 magro732
 
360 11 magro732
  signal rxControlWrite : std_logic_vector(NUMBER_WORDS-1 downto 0);
361
  signal rxControlWriteSymbol : std_logic_vector(12*NUMBER_WORDS downto 0);
362
  signal rxControlReadEmpty : std_logic_vector(NUMBER_WORDS-1 downto 0);
363
  signal rxControlRead : std_logic_vector(NUMBER_WORDS-1 downto 0);
364
  signal rxControlReadSymbol : std_logic_vector(12*NUMBER_WORDS downto 0);
365 4 magro732
 
366
  signal outboundFull : std_logic;
367
  signal outboundWrite : std_logic;
368 11 magro732
  signal outboundType : std_logic_vector(1 downto 0);
369
  signal outboundData : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
370
  signal outboundSymbol : std_logic_vector(2+(32*NUMBER_WORDS-1) downto 0);
371 4 magro732
 
372
  signal inboundEmpty : std_logic;
373
  signal inboundRead : std_logic;
374 11 magro732
  signal inboundType : std_logic_vector(1 downto 0);
375
  signal inboundData : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
376
  signal inboundSymbol : std_logic_vector(((2+32)*NUMBER_WORDS-1) downto 0);
377 4 magro732
 
378
begin
379
 
380
  linkInitialized_o <=
381
    '1' when ((linkInitializedRx = '1') and (linkInitializedTx = '1')) else '0';
382
 
383
  -----------------------------------------------------------------------------
384
  -- Serial layer modules.
385
  -----------------------------------------------------------------------------
386
 
387
  Transmitter: RioTransmitter
388
    generic map(
389 11 magro732
      TIMEOUT_WIDTH=>TIMEOUT_WIDTH,
390
      NUMBER_WORDS=>NUMBER_WORDS)
391 4 magro732
    port map(
392
      clk=>clk, areset_n=>areset_n,
393
      portLinkTimeout_i=>portLinkTimeout_i,
394
      portEnable_i=>outputPortEnable_i,
395
      localAckIdWrite_i=>localAckIdWrite_i,
396
      clrOutstandingAckId_i=>clrOutstandingAckId_i,
397
      outstandingAckId_i=>outstandingAckId_i,
398
      outboundAckId_i=>outboundAckId_i,
399
      outstandingAckId_o=>outstandingAckId_o,
400
      outboundAckId_o=>outboundAckId_o,
401
      portInitialized_i=>portInitialized_i,
402
      txFull_i=>outboundFull, txWrite_o=>outboundWrite,
403 11 magro732
      txType_o=>outboundType, txData_o=>outboundData,
404
      txControlEmpty_i=>txControlReadEmpty(0), txControlSymbol_i=>txControlReadSymbol,
405
      txControlUpdate_o=>txControlRead(0),
406
      rxControlEmpty_i=>rxControlReadEmpty(0), rxControlSymbol_i=>rxControlReadSymbol,
407
      rxControlUpdate_o=>rxControlRead(0),
408 4 magro732
      linkInitialized_o=>linkInitializedTx,
409
      linkInitialized_i=>linkInitializedRx, ackIdStatus_i=>ackIdStatus,
410
      readFrameEmpty_i=>readFrameEmpty_i, readFrame_o=>readFrame_o,
411
      readFrameRestart_o=>readFrameRestart_o, readFrameAborted_i=>readFrameAborted_i,
412
      readWindowEmpty_i=>readWindowEmpty_i,
413
      readWindowReset_o=>readWindowReset_o, readWindowNext_o=>readWindowNext_o,
414
      readContentEmpty_i=>readContentEmpty_i, readContent_o=>readContent_o,
415 11 magro732
      readContentEnd_i=>readContentEnd_i,
416
      readContentWords_i=>readContentData_i(2+(32*NUMBER_WORDS-1) downto 1+(32*NUMBER_WORDS-1)),
417
      readContentData_i=>readContentData_i(32*NUMBER_WORDS-1 downto 0));
418 4 magro732
 
419 11 magro732
  SymbolFifo: for i in 0 to NUMBER_WORDS-1 generate
420
    TxSymbolFifo: RioFifo1
421
      generic map(WIDTH=>13)
422
      port map(
423
        clk=>clk, areset_n=>areset_n,
424
        empty_o=>txControlReadEmpty(i),
425
        read_i=>txControlRead(i),
426
        data_o=>txControlReadSymbol(12*(i+1) downto 12*i),
427
        full_o=>open,
428
        write_i=>txControlWrite(i),
429
        data_i=>txControlWriteSymbol(12*(i+1) downto 12*i));
430 4 magro732
 
431 11 magro732
    RxSymbolFifo: RioFifo1
432
      generic map(WIDTH=>13)
433
      port map(
434
        clk=>clk, areset_n=>areset_n,
435
        empty_o=>rxControlReadEmpty(i),
436
        read_i=>rxControlRead(i),
437
        data_o=>rxControlReadSymbol(12*(i+1) downto 12*i),
438
        full_o=>open,
439
        write_i=>rxControlWrite(i),
440
        data_i=>rxControlWriteSymbol(12*(i+1) downto 12*i));
441
  end generate;
442
 
443 4 magro732
  Receiver: RioReceiver
444 11 magro732
    generic map(NUMBER_WORDS=>NUMBER_WORDS)
445 4 magro732
    port map(
446
      clk=>clk, areset_n=>areset_n,
447
      portEnable_i=>inputPortEnable_i,
448
      localAckIdWrite_i=>localAckIdWrite_i,
449
      inboundAckId_i=>inboundAckId_i,
450
      inboundAckId_o=>inboundAckId_o,
451
      portInitialized_i=>portInitialized_i,
452
      rxEmpty_i=>inboundEmpty, rxRead_o=>inboundRead,
453 11 magro732
      rxType_i=>inboundType, rxData_i=>inboundData,
454 4 magro732
      txControlWrite_o=>txControlWrite, txControlSymbol_o=>txControlWriteSymbol,
455
      rxControlWrite_o=>rxControlWrite, rxControlSymbol_o=>rxControlWriteSymbol,
456
      ackIdStatus_o=>ackIdStatus,
457
      linkInitialized_o=>linkInitializedRx,
458
      writeFrameFull_i=>writeFrameFull_i,
459
      writeFrame_o=>writeFrame_o, writeFrameAbort_o=>writeFrameAbort_o,
460 11 magro732
      writeContent_o=>writeContent_o,
461
      writeContentWords_o=>writeContentData_o(2+(32*NUMBER_WORDS-1) downto 1+(32*NUMBER_WORDS-1)),
462
      writeContentData_o=>writeContentData_o(32*NUMBER_WORDS-1 downto 0));
463 4 magro732
 
464
  -----------------------------------------------------------------------------
465
  -- PCS layer FIFO interface.
466
  -----------------------------------------------------------------------------
467
 
468 11 magro732
  outboundSymbol <= outboundType & outboundData;
469 4 magro732
  OutboundSymbolFifo: RioFifo1
470 11 magro732
    generic map(WIDTH=>2+32*NUMBER_WORDS)
471 4 magro732
    port map(
472
      clk=>clk, areset_n=>areset_n,
473
      empty_o=>outboundSymbolEmpty_o, read_i=>outboundSymbolRead_i, data_o=>outboundSymbol_o,
474
      full_o=>outboundFull, write_i=>outboundWrite, data_i=>outboundSymbol);
475
 
476 11 magro732
  inboundType <= inboundSymbol(2+(32*NUMBER_WORDS-1) downto 1+(32*NUMBER_WORDS-1));
477
  inboundData <= inboundSymbol(32*NUMBER_WORDS-1 downto 0);
478 4 magro732
  InboundSymbolFifo: RioFifo1
479 11 magro732
    generic map(WIDTH=>2+32*NUMBER_WORDS)
480 4 magro732
    port map(
481
      clk=>clk, areset_n=>areset_n,
482
      empty_o=>inboundEmpty, read_i=>inboundRead, data_o=>inboundSymbol,
483
      full_o=>inboundSymbolFull_o, write_i=>inboundSymbolWrite_i, data_i=>inboundSymbol_i);
484
 
485
end architecture;
486
 
487
 
488
 
489
-------------------------------------------------------------------------------
490
-- RioTransmitter
491
-------------------------------------------------------------------------------
492
library ieee;
493
use ieee.std_logic_1164.all;
494
use ieee.numeric_std.all;
495
use work.rio_common.all;
496
 
497
 
498
-------------------------------------------------------------------------------
499
-- Entity for RioTransmitter.
500
-------------------------------------------------------------------------------
501
entity RioTransmitter is
502
  generic(
503 11 magro732
    TIMEOUT_WIDTH : natural;
504
    NUMBER_WORDS : natural range 1 to 4 := 1);
505 4 magro732
  port(
506
    -- System signals.
507
    clk : in std_logic;
508
    areset_n : in std_logic;
509
 
510
    -- Status signals used for maintenance.
511
    portLinkTimeout_i : in std_logic_vector(TIMEOUT_WIDTH-1 downto 0);
512
    portEnable_i : in std_logic;
513
 
514
    -- Support for localAckIdCSR.
515
    localAckIdWrite_i : in std_logic;
516
    clrOutstandingAckId_i : in std_logic;
517
    outstandingAckId_i : in std_logic_vector(4 downto 0);
518
    outboundAckId_i : in std_logic_vector(4 downto 0);
519
    outstandingAckId_o : out std_logic_vector(4 downto 0);
520
    outboundAckId_o : out std_logic_vector(4 downto 0);
521
 
522
    -- Port output interface.
523
    portInitialized_i : in std_logic;
524
    txFull_i : in std_logic;
525
    txWrite_o : out std_logic;
526 11 magro732
    txType_o : out std_logic_vector(2*NUMBER_WORDS-1 downto 0);
527
    txData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
528 4 magro732
 
529
    -- Control symbols aimed to the transmitter.
530
    txControlEmpty_i : in std_logic;
531
    txControlSymbol_i : in std_logic_vector(12 downto 0);
532
    txControlUpdate_o : out std_logic;
533
 
534
    -- Control symbols from the receiver to send.
535
    rxControlEmpty_i : in std_logic;
536
    rxControlSymbol_i : in std_logic_vector(12 downto 0);
537
    rxControlUpdate_o : out std_logic;
538
 
539
    -- Internal signalling from the receiver part.
540
    linkInitialized_o : out std_logic;
541
    linkInitialized_i : in std_logic;
542
    ackIdStatus_i : in std_logic_vector(4 downto 0);
543
 
544
    -- Frame buffer interface.
545
    readFrameEmpty_i : in std_logic;
546
    readFrame_o : out std_logic;
547
    readFrameRestart_o : out std_logic;
548
    readFrameAborted_i : in std_logic;
549
    readWindowEmpty_i : in std_logic;
550
    readWindowReset_o : out std_logic;
551
    readWindowNext_o : out std_logic;
552
    readContentEmpty_i : in std_logic;
553
    readContent_o : out std_logic;
554
    readContentEnd_i : in std_logic;
555 11 magro732
    readContentWords_i : in std_logic_vector(1 downto 0);
556
    readContentData_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0));
557 4 magro732
end entity;
558
 
559
 
560
-------------------------------------------------------------------------------
561
-- Architecture for RioTransmitter.
562
-------------------------------------------------------------------------------
563
architecture RioTransmitterImpl of RioTransmitter is
564
 
565
  constant NUMBER_STATUS_TRANSMIT : natural := 15;
566
  constant NUMBER_LINK_RESPONSE_RETRIES : natural := 2;
567 11 magro732
 
568
  component RioTransmitterCore is
569
    generic(
570
      NUMBER_WORDS : natural range 1 to 4 := 1);
571
    port(
572
      -- System signals.
573
      clk : in std_logic;
574
      areset_n : in std_logic;
575
 
576
      -- Status signals used for maintenance.
577
      portEnable_i : in std_logic;
578
 
579
      -- Port output interface.
580
      portInitialized_i : in std_logic;
581
      txFull_i : in std_logic;
582
      txWrite_o : out std_logic;
583
      txType_o : out std_logic_vector(2*NUMBER_WORDS-1 downto 0);
584
      txData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
585
 
586
      -- Control symbols aimed to the transmitter.
587
      txControlEmpty_i : in std_logic;
588
      txControlSymbol_i : in std_logic_vector(13*NUMBER_WORDS-1 downto 0);
589
      txControlUpdate_o : out std_logic;
590
 
591
      -- Control symbols from the receiver to send.
592
      rxControlEmpty_i : in std_logic;
593
      rxControlSymbol_i : in std_logic_vector(13*NUMBER_WORDS-1 downto 0);
594
      rxControlUpdate_o : out std_logic;
595
 
596
      -- Internal signalling from the receiver part.
597
      linkInitialized_o : out std_logic;
598
      linkInitialized_i : in std_logic;
599
      ackIdStatus_i : in std_logic_vector(4 downto 0);
600
 
601
      -- Internal core variables for cascading.
602 13 magro732
      timeSentSet_o : out std_logic;
603
      timeSentReset_o : out std_logic;
604
      timeSentExpired_i : in std_logic;
605 11 magro732
      operational_i : in std_logic;
606
      operational_o : out std_logic;
607
      ackId_i : in std_logic_vector(4 downto 0);
608
      ackId_o : out std_logic_vector(4 downto 0);
609
      bufferStatus_i : in std_logic_vector(4 downto 0);
610
      bufferStatus_o : out std_logic_vector(4 downto 0);
611
      statusReceived_i : in std_logic;
612
      statusReceived_o : out std_logic;
613
      numberSentLinkRequests_i : in std_logic_vector(1 downto 0);
614
      numberSentLinkRequests_o : out std_logic_vector(1 downto 0);
615
      outputErrorStopped_i : in std_logic;
616
      outputErrorStopped_o : out std_logic;
617 14 magro732
      fatalError_i : in std_logic;
618
      fatalError_o : out std_logic;
619 11 magro732
      recoverActive_i : in std_logic;
620
      recoverActive_o : out std_logic;
621
      recoverCounter_i : in std_logic_vector(4 downto 0);
622
      recoverCounter_o : out std_logic_vector(4 downto 0);
623
      ackIdWindow_i : in std_logic_vector(4 downto 0);
624
      ackIdWindow_o : out std_logic_vector(4 downto 0);
625 14 magro732
      frameState_i : in std_logic_vector(6 downto 0);
626
      frameState_o : out std_logic_vector(6 downto 0);
627 11 magro732
      frameWordCounter_i : in std_logic_vector(1 downto 0);
628
      frameWordCounter_o : out std_logic_vector(1 downto 0);
629
      frameContent_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
630
      frameContent_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
631
      counter_i : in std_logic_vector(3 downto 0);
632
      counter_o : out std_logic_vector(3 downto 0);
633
      symbolsTransmitted_i : in std_logic_vector(7 downto 0);
634
      symbolsTransmitted_o : out std_logic_vector(7 downto 0);
635
 
636
      -- Frame buffer interface.
637
      readFrameEmpty_i : in std_logic;
638
      readFrame_o : out std_logic;
639
      readFrameRestart_o : out std_logic;
640
      readFrameAborted_i : in std_logic;
641
      readWindowEmpty_i : in std_logic;
642
      readWindowReset_o : out std_logic;
643
      readWindowNext_o : out std_logic;
644
      readContentEmpty_i : in std_logic;
645
      readContent_o : out std_logic;
646
      readContentEnd_i : in std_logic;
647
      readContentWords_i : in std_logic_vector(1 downto 0);
648
      readContentData_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0));
649
  end component;
650 4 magro732
 
651
  component MemorySimpleDualPortAsync is
652
    generic(
653
      ADDRESS_WIDTH : natural := 1;
654
      DATA_WIDTH : natural := 1;
655
      INIT_VALUE : std_logic := 'U');
656
    port(
657
      clkA_i : in std_logic;
658
      enableA_i : in std_logic;
659
      addressA_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
660
      dataA_i : in std_logic_vector(DATA_WIDTH-1 downto 0);
661
 
662
      addressB_i : in std_logic_vector(ADDRESS_WIDTH-1 downto 0);
663
      dataB_o : out std_logic_vector(DATA_WIDTH-1 downto 0));
664
  end component;
665
 
666 11 magro732
  signal timeCurrent : std_logic_vector(TIMEOUT_WIDTH downto 0);
667 13 magro732
  signal timeSentElapsed : unsigned(TIMEOUT_WIDTH downto 0);
668
  signal timeSentDelta : unsigned(TIMEOUT_WIDTH downto 0);
669
  signal timeSentExpired : std_logic;
670
  signal timeSentSet : std_logic;
671
  signal timeSentReset : std_logic;
672
 
673
  signal timeSentEnable : std_logic;
674 11 magro732
  signal timeSentWriteAddress : std_logic_vector(4 downto 0);
675
  signal timeSentReadAddress : std_logic_vector(4 downto 0);
676
  signal timeSentReadData : std_logic_vector(TIMEOUT_WIDTH downto 0);
677 4 magro732
 
678 11 magro732
  signal operationalCurrent, operationalNext : std_logic;
679
  signal ackIdCurrent, ackIdNext : std_logic_vector(4 downto 0);
680 4 magro732
  signal bufferStatusCurrent, bufferStatusNext : std_logic_vector(4 downto 0);
681 11 magro732
  signal statusReceivedCurrent, statusReceivedNext : std_logic;
682
  signal numberSentLinkRequestsCurrent, numberSentLinkRequestsNext : std_logic_vector(1 downto 0);
683
  signal outputErrorStoppedCurrent, outputErrorStoppedNext : std_logic;
684 14 magro732
  signal fatalErrorCurrent, fatalErrorNext : std_logic;
685 11 magro732
  signal recoverActiveCurrent, recoverActiveNext : std_logic;
686
  signal recoverCounterCurrent, recoverCounterNext : std_logic_vector(4 downto 0);
687
  signal ackIdWindowCurrent, ackIdWindowNext : std_logic_vector(4 downto 0);
688 14 magro732
  signal frameStateCurrent, frameStateNext : std_logic_vector(6 downto 0);
689 11 magro732
  signal frameWordCounterCurrent, frameWordCounterNext : std_logic_vector(1 downto 0);
690
  signal frameContentCurrent, frameContentNext : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
691
  signal counterCurrent, counterNext : std_logic_vector(3 downto 0);
692
  signal symbolsTransmittedCurrent, symbolsTransmittedNext : std_logic_vector(7 downto 0);
693 4 magro732
 
694
begin
695
 
696
  process(areset_n, clk)
697
  begin
698
    if (areset_n = '0') then
699 11 magro732
      timeCurrent <= (others=>'0');
700 4 magro732
    elsif (clk'event and clk = '1') then
701 11 magro732
      timeCurrent <= std_logic_vector(unsigned(timeCurrent) + 1);
702 4 magro732
    end if;
703
  end process;
704
 
705 13 magro732
  timeSentElapsed <= unsigned(timeCurrent) - unsigned(timeSentReadData);
706
  timeSentDelta <= unsigned('0' & portLinkTimeout_i) - timeSentElapsed;
707
  timeSentExpired <= timeSentDelta(TIMEOUT_WIDTH);
708
 
709
  timeSentEnable <= (not txFull_i) and (timeSentSet or timeSentReset);
710
  timeSentWriteAddress <= ackIdWindowCurrent when timeSentSet = '1' else
711
                          ackIdCurrent;
712
  timeSentReadAddress <= ackIdCurrent;
713
 
714 4 magro732
  TimeoutMemory: MemorySimpleDualPortAsync
715
    generic map(ADDRESS_WIDTH=>5, DATA_WIDTH=>TIMEOUT_WIDTH+1, INIT_VALUE=>'0')
716
    port map(
717 13 magro732
      clkA_i=>clk, enableA_i=>timeSentEnable,
718
      addressA_i=>timeSentWriteAddress, dataA_i=>timeCurrent,
719 11 magro732
      addressB_i=>timeSentReadAddress, dataB_o=>timeSentReadData);
720 4 magro732
 
721
  process(areset_n, clk)
722
  begin
723
    if (areset_n = '0') then
724 11 magro732
      operationalCurrent <= '0';
725
      ackIdCurrent <= (others=>'0');
726
      bufferStatusCurrent <= (others=>'0');
727 4 magro732
      statusReceivedCurrent <= '0';
728 11 magro732
      numberSentLinkRequestsCurrent <= (others=>'0');
729
      outputErrorStoppedCurrent <= '0';
730 14 magro732
      fatalErrorCurrent <= '0';
731 11 magro732
      recoverActiveCurrent <= '0';
732
      recoverCounterCurrent <= (others=>'0');
733
      ackIdWindowCurrent <= (others=>'0');
734
      frameStateCurrent <= (others=>'0');
735
      frameWordCounterCurrent <= (others=>'0');
736
      frameContentCurrent <= (others=>'0');
737
      counterCurrent <= (others=>'0');
738
      symbolsTransmittedCurrent <= (others=>'0');
739 4 magro732
    elsif (clk'event and clk = '1') then
740 13 magro732
      if (txFull_i = '0') then
741
        operationalCurrent <= operationalNext;
742
        ackIdCurrent <= ackIdNext;
743
        bufferStatusCurrent <= bufferStatusNext;
744
        statusReceivedCurrent <= statusReceivedNext;
745
        numberSentLinkRequestsCurrent <= numberSentLinkRequestsNext;
746
        outputErrorStoppedCurrent <= outputErrorStoppedNext;
747 14 magro732
        fatalErrorCurrent <= fatalErrorNext;
748 13 magro732
        recoverActiveCurrent <= recoverActiveNext;
749
        recoverCounterCurrent <= recoverCounterNext;
750
        ackIdWindowCurrent <= ackIdWindowNext;
751
        frameStateCurrent <= frameStateNext;
752
        frameWordCounterCurrent <= frameWordCounterNext;
753
        frameContentCurrent <= frameContentNext;
754
        counterCurrent <= counterNext;
755
        symbolsTransmittedCurrent <= symbolsTransmittedNext;
756
      end if;
757 4 magro732
    end if;
758
  end process;
759 11 magro732
 
760
  TxCore: RioTransmitterCore
761 13 magro732
    generic map(NUMBER_WORDS=>NUMBER_WORDS)
762 11 magro732
    port map(
763
      clk=>clk, areset_n=>areset_n,
764 13 magro732
      portEnable_i=>portEnable_i,
765 11 magro732
      portInitialized_i=>portInitialized_i,
766
      txFull_i=>txFull_i, txWrite_o=>txWrite_o, txType_o=>txType_o, txData_o=>txData_o,
767
      txControlEmpty_i=>txControlEmpty_i,
768
      txControlSymbol_i=>txControlSymbol_i,
769
      txControlUpdate_o=>txControlUpdate_o,
770
      rxControlEmpty_i=>rxControlEmpty_i,
771
      rxControlSymbol_i=>rxControlSymbol_i,
772
      rxControlUpdate_o=>rxControlUpdate_o,
773
      linkInitialized_o=>linkInitialized_o,
774
      linkInitialized_i=>linkInitialized_i,
775
      ackIdStatus_i=>ackIdStatus_i,
776 4 magro732
 
777 13 magro732
      timeSentSet_o=>timeSentSet,
778
      timeSentReset_o=>timeSentReset,
779
      timeSentExpired_i=>timeSentExpired,
780 11 magro732
 
781
      operational_i=>operationalCurrent, operational_o=>operationalNext,
782
      ackId_i=>ackIdCurrent, ackId_o=>ackIdNext,
783
      bufferStatus_i=>bufferStatusCurrent, bufferStatus_o=>bufferStatusNext,
784
      statusReceived_i=>statusReceivedCurrent, statusReceived_o=>statusReceivedNext,
785
      numberSentLinkRequests_i=>numberSentLinkRequestsCurrent, numberSentLinkRequests_o=>numberSentLinkRequestsNext,
786
      outputErrorStopped_i=>outputErrorStoppedCurrent, outputErrorStopped_o=>outputErrorStoppedNext,
787 14 magro732
      fatalError_i=>fatalErrorCurrent, fatalError_o=>fatalErrorNext,
788 11 magro732
      recoverActive_i=>recoverActiveCurrent, recoverActive_o=>recoverActiveNext,
789
      recoverCounter_i=>recoverCounterCurrent, recoverCounter_o=>recoverCounterNext,
790
      ackIdWindow_i=>ackIdWindowCurrent, ackIdWindow_o=>ackIdWindowNext,
791
      frameState_i=>frameStateCurrent, frameState_o=>frameStateNext,
792
      frameWordCounter_i=>frameWordCounterCurrent, frameWordCounter_o=>frameWordCounterNext,
793
      frameContent_i=>frameContentCurrent, frameContent_o=>frameContentNext,
794
      counter_i=>counterCurrent, counter_o=>counterNext,
795
      symbolsTransmitted_i=>symbolsTransmittedCurrent, symbolsTransmitted_o=>symbolsTransmittedNext,
796 4 magro732
 
797 11 magro732
      readFrameEmpty_i=>readFrameEmpty_i,
798
      readFrame_o=>readFrame_o,
799
      readFrameRestart_o=>readFrameRestart_o,
800
      readFrameAborted_i=>readFrameAborted_i,
801
      readWindowEmpty_i=>readWindowEmpty_i,
802
      readWindowReset_o=>readWindowReset_o,
803
      readWindowNext_o=>readWindowNext_o,
804
      readContentEmpty_i=>readContentEmpty_i,
805
      readContent_o=>readContent_o,
806
      readContentEnd_i=>readContentEnd_i,
807
      readContentWords_i=>readContentWords_i,
808
      readContentData_i=>readContentData_i);
809 4 magro732
 
810 11 magro732
end architecture;
811
 
812
 
813
 
814
-------------------------------------------------------------------------------
815
-- RioTransmitterCore
816
-------------------------------------------------------------------------------
817
library ieee;
818
use ieee.std_logic_1164.all;
819
use ieee.numeric_std.all;
820
use work.rio_common.all;
821
 
822
-------------------------------------------------------------------------------
823
-- Entity for RioTransmitterCore.
824
-------------------------------------------------------------------------------
825
entity RioTransmitterCore is
826
  generic(
827
    NUMBER_WORDS : natural range 1 to 4 := 1);
828
  port(
829
    -- System signals.
830
    clk : in std_logic;
831
    areset_n : in std_logic;
832
 
833
    -- Status signals used for maintenance.
834
    portEnable_i : in std_logic;
835
 
836
    -- Port output interface.
837
    portInitialized_i : in std_logic;
838
    txFull_i : in std_logic;
839
    txWrite_o : out std_logic;
840
    txType_o : out std_logic_vector(2*NUMBER_WORDS-1 downto 0);
841
    txData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
842
 
843
    -- Control symbols aimed to the transmitter.
844
    txControlEmpty_i : in std_logic;
845
    txControlSymbol_i : in std_logic_vector(13*NUMBER_WORDS-1 downto 0);
846
    txControlUpdate_o : out std_logic;
847
 
848
    -- Control symbols from the receiver to send.
849
    rxControlEmpty_i : in std_logic;
850
    rxControlSymbol_i : in std_logic_vector(13*NUMBER_WORDS-1 downto 0);
851
    rxControlUpdate_o : out std_logic;
852
 
853
    -- Internal signalling from the receiver part.
854
    linkInitialized_o : out std_logic;
855
    linkInitialized_i : in std_logic;
856
    ackIdStatus_i : in std_logic_vector(4 downto 0);
857
 
858
    -- Internal core variables for cascading.
859 13 magro732
    timeSentSet_o : out std_logic;
860
    timeSentReset_o : out std_logic;
861
    timeSentExpired_i : in std_logic;
862 11 magro732
 
863
    operational_i : in std_logic;
864
    operational_o : out std_logic;
865
    ackId_i : in std_logic_vector(4 downto 0);
866
    ackId_o : out std_logic_vector(4 downto 0);
867
    bufferStatus_i : in std_logic_vector(4 downto 0);
868
    bufferStatus_o : out std_logic_vector(4 downto 0);
869
    statusReceived_i : in std_logic;
870
    statusReceived_o : out std_logic;
871
    numberSentLinkRequests_i : in std_logic_vector(1 downto 0);
872
    numberSentLinkRequests_o : out std_logic_vector(1 downto 0);
873
    outputErrorStopped_i : in std_logic;
874
    outputErrorStopped_o : out std_logic;
875 14 magro732
    fatalError_i : in std_logic;
876
    fatalError_o : out std_logic;
877 11 magro732
    recoverActive_i : in std_logic;
878
    recoverActive_o : out std_logic;
879
    recoverCounter_i : in std_logic_vector(4 downto 0);
880
    recoverCounter_o : out std_logic_vector(4 downto 0);
881
    ackIdWindow_i : in std_logic_vector(4 downto 0);
882
    ackIdWindow_o : out std_logic_vector(4 downto 0);
883 14 magro732
    frameState_i : in std_logic_vector(6 downto 0);
884
    frameState_o : out std_logic_vector(6 downto 0);
885 11 magro732
    frameWordCounter_i : in std_logic_vector(1 downto 0);
886
    frameWordCounter_o : out std_logic_vector(1 downto 0);
887
    frameContent_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
888
    frameContent_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
889
    counter_i : in std_logic_vector(3 downto 0);
890
    counter_o : out std_logic_vector(3 downto 0);
891
    symbolsTransmitted_i : in std_logic_vector(7 downto 0);
892
    symbolsTransmitted_o : out std_logic_vector(7 downto 0);
893
 
894
    -- Frame buffer interface.
895
    readFrameEmpty_i : in std_logic;
896
    readFrame_o : out std_logic;
897
    readFrameRestart_o : out std_logic;
898
    readFrameAborted_i : in std_logic;
899
    readWindowEmpty_i : in std_logic;
900
    readWindowReset_o : out std_logic;
901
    readWindowNext_o : out std_logic;
902
    readContentEmpty_i : in std_logic;
903
    readContent_o : out std_logic;
904
    readContentEnd_i : in std_logic;
905
    readContentWords_i : in std_logic_vector(1 downto 0);
906
    readContentData_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0));
907
end entity;
908 4 magro732
 
909
 
910 11 magro732
-------------------------------------------------------------------------------
911
-- Architecture for RioTransmitterCore.
912
-------------------------------------------------------------------------------
913 13 magro732
-- REMARK: Check that all _o except channel variables are registered...
914 11 magro732
architecture RioTransmitterCoreImpl of RioTransmitterCore is
915 4 magro732
 
916 11 magro732
  constant NUMBER_STATUS_TRANSMIT : std_logic_vector := "1111";
917
  constant NUMBER_LINK_RESPONSE_RETRIES : std_logic_vector := "10";
918 4 magro732
 
919 11 magro732
  -- REMARK: Binary coding???
920 14 magro732
  constant FRAME_IDLE : std_logic_vector(6 downto 0) := "0000001";
921
  constant FRAME_START : std_logic_vector(6 downto 0) := "0000010";
922
  constant FRAME_FIRST : std_logic_vector(6 downto 0) := "0000100";
923
  constant FRAME_MIDDLE : std_logic_vector(6 downto 0) := "0001000";
924
  constant FRAME_LAST : std_logic_vector(6 downto 0) := "0010000";
925
  constant FRAME_END : std_logic_vector(6 downto 0) := "0100000";
926
  constant FRAME_DISCARD : std_logic_vector(6 downto 0) := "1000000";
927 11 magro732
 
928
  component Crc5ITU is
929
    port(
930
      d_i : in  std_logic_vector(18 downto 0);
931
      crc_o : out std_logic_vector(4 downto 0));
932
  end component;
933 4 magro732
 
934 13 magro732
  signal txControlUpdateOut : std_logic;
935 11 magro732
  signal sendRestartFromRetry, sendRestartFromRetryOut : std_logic;
936
  signal sendLinkRequest, sendLinkRequestOut : std_logic;
937 4 magro732
 
938 13 magro732
  signal readFrameOut : std_logic;
939
  signal readFrameRestartOut : std_logic;
940
  signal readWindowResetOut : std_logic;
941
  signal readWindowNextOut : std_logic;
942
  signal readContentOut : std_logic;
943 11 magro732
  signal symbolControlRestartOut, symbolControlRestart : std_logic;
944
  signal symbolControlLinkRequestOut, symbolControlLinkRequest : std_logic;
945
  signal symbolControlStartOut, symbolControlStart : std_logic;
946
  signal symbolControlEndOut, symbolControlEnd : std_logic;
947
  signal symbolDataOut, symbolData : std_logic;
948
  signal symbolDataContentOut, symbolDataContent : std_logic_vector(31 downto 0);
949 4 magro732
 
950 13 magro732
  signal rxControlUpdateOut : std_logic;
951 11 magro732
  signal symbolControlStype1 : std_logic;
952
  signal controlValidOut, controlValid : std_logic;
953
  signal stype0Out, stype0 : std_logic_vector(2 downto 0);
954
  signal parameter0Out, parameter0 : std_logic_vector(4 downto 0);
955
  signal parameter1Out, parameter1 : std_logic_vector(4 downto 0);
956
  signal stype1 : std_logic_vector(2 downto 0);
957
  signal cmd : std_logic_vector(2 downto 0);
958
  signal dataValid : std_logic;
959
  signal dataContent : std_logic_vector(31 downto 0);
960
 
961
  signal controlContent : std_logic_vector(31 downto 0);
962
  signal crc5 : std_logic_vector(4 downto 0);
963
 
964
  signal txControlStype0 : std_logic_vector(2 downto 0);
965
  signal txControlParameter0 : std_logic_vector(4 downto 0);
966
  signal txControlParameter1 : std_logic_vector(4 downto 0);
967 4 magro732
 
968 11 magro732
  signal rxControlStype0 : std_logic_vector(2 downto 0);
969
  signal rxControlParameter0 : std_logic_vector(4 downto 0);
970
  signal rxControlParameter1 : std_logic_vector(4 downto 0);
971 4 magro732
 
972 11 magro732
begin
973 4 magro732
 
974 11 magro732
  linkInitialized_o <= operational_i;
975
 
976
  -----------------------------------------------------------------------------
977 12 magro732
  -- Assign control symbol from fifo signals.
978
  -----------------------------------------------------------------------------
979
 
980
  txControlStype0 <= txControlSymbol_i(12 downto 10);
981
  txControlParameter0 <= txControlSymbol_i(9 downto 5);
982
  txControlParameter1 <= txControlSymbol_i(4 downto 0);
983
 
984
  rxControlStype0 <= rxControlSymbol_i(12 downto 10);
985
  rxControlParameter0 <= rxControlSymbol_i(9 downto 5);
986
  rxControlParameter1 <= rxControlSymbol_i(4 downto 0);
987
 
988
  -----------------------------------------------------------------------------
989 11 magro732
  -- N-3
990
  -- Receive stuff from link-partner and timeout supervision.
991
  -- Input: ackId, ackIdWindow, timeoutExpired
992
  -- Output: sendLinkRequest, sendRestartFromRetry, ackId,
993
  -----------------------------------------------------------------------------
994 4 magro732
 
995 11 magro732
  process(clk, areset_n)
996
  begin
997
    if (areset_n = '0') then
998 13 magro732
      txControlUpdate_o <= '0';
999
      readFrame_o <= '0';
1000
 
1001 11 magro732
      sendRestartFromRetry <= '0';
1002
      sendLinkRequest <= '0';
1003
    elsif (clk'event and clk = '1') then
1004 13 magro732
      txControlUpdate_o <= '0';
1005
      readFrame_o <= '0';
1006
 
1007
      if (txFull_i = '0') then
1008
        txControlUpdate_o <= txControlUpdateOut;
1009
        readFrame_o <= readFrameOut;
1010
 
1011
        sendRestartFromRetry <= sendRestartFromRetryOut;
1012
        sendLinkRequest <= sendLinkRequestOut;
1013
      end if;
1014 11 magro732
    end if;
1015
  end process;
1016
 
1017 12 magro732
  process(outputErrorStopped_i, recoverActive_i, recoverCounter_i,
1018 13 magro732
          ackId_i, ackIdWindow_i, bufferStatus_i, statusReceived_i,
1019 12 magro732
          numberSentLinkRequests_i,
1020 13 magro732
          operational_i,
1021 12 magro732
          txControlEmpty_i, txControlStype0,
1022 13 magro732
          txControlParameter0, txControlParameter1,
1023 14 magro732
          timeSentExpired_i,
1024
          fatalError_i)
1025 11 magro732
  begin
1026
    outputErrorStopped_o <= outputErrorStopped_i;
1027 14 magro732
    fatalError_o <= fatalError_i;
1028 11 magro732
    recoverActive_o <= recoverActive_i;
1029
    recoverCounter_o <= recoverCounter_i;
1030
    ackId_o <= ackId_i;
1031
    bufferStatus_o <= bufferStatus_i;
1032
    statusReceived_o <= statusReceived_i;
1033
    numberSentLinkRequests_o <= numberSentLinkRequests_i;
1034 4 magro732
 
1035 13 magro732
    timeSentReset_o <= '0';
1036
    txControlUpdateOut <= '0';
1037
    readFrameOut <= '0';
1038 12 magro732
 
1039 11 magro732
    sendRestartFromRetryOut <= '0';
1040
    sendLinkRequestOut <= '0';
1041 4 magro732
 
1042 14 magro732
    if (fatalError_i = '1') then
1043
      outputErrorStopped_o <= '0';
1044
      fatalError_o <= '0';
1045
    elsif (recoverActive_i = '1') then
1046
      if (ackId_i = recoverCounter_i) then
1047
        recoverActive_o <= '0';
1048
        outputErrorStopped_o <= '0';
1049
      else
1050 11 magro732
        ackId_o <= std_logic_vector(unsigned(ackId_i) + 1);
1051 13 magro732
        readFrameOut <= '1';
1052 11 magro732
      end if;
1053
    else
1054 13 magro732
      if (operational_i = '0') then
1055
        if (txControlEmpty_i = '0') then
1056
          if (txControlStype0 = STYPE0_STATUS) then
1057
            -- A status-control symbol has been received.
1058
            ackId_o <= txControlParameter0;
1059
            bufferStatus_o <= txControlParameter1;
1060
            statusReceived_o <= '1';
1061
          else
1062
            -- Discard all other received symbols in this state.
1063
          end if;
1064
          txControlUpdateOut <= '1';
1065
        end if;
1066
      else
1067
        -- Operational mode.
1068
 
1069
        -- Make sure to reset the status received flag.
1070
        statusReceived_o <= '0';
1071
 
1072
        -- Check if the oldest frame timeout has expired.
1073
        -- REMARK: Two link-requests are transmitted when a timeout occurrs...
1074
        if ((ackId_i /= ackIdWindow_i) and
1075
            (timeSentExpired_i = '1')) then
1076
          -- There has been a timeout on a transmitted frame.
1077
 
1078
          -- Reset the timeout to expire when the transmitted link-request has
1079
          -- timed out instead.
1080
          timeSentReset_o <= '1';
1081
 
1082
          if (outputErrorStopped_i = '1') then
1083
            -- Count the number of retransmissions and abort if
1084
            -- no reply has been received for too many times.
1085
            if (unsigned(numberSentLinkRequests_i) /= 0) then
1086
              -- Not sent link-request too many times.
1087
 
1088
              -- Send another link-request.
1089
              sendLinkRequestOut <= '1';
1090
              numberSentLinkRequests_o <= std_logic_vector(unsigned(numberSentLinkRequests_i) - 1);
1091 4 magro732
            else
1092 13 magro732
              -- No response for too many times.
1093 14 magro732
 
1094
              -- Indicate that a fatal error has occurred.
1095
              fatalError_o <= '1';
1096 4 magro732
            end if;
1097 13 magro732
          else
1098
            sendLinkRequestOut <= '1';
1099
            numberSentLinkRequests_o <= NUMBER_LINK_RESPONSE_RETRIES;
1100
            outputErrorStopped_o <= '1';
1101 11 magro732
          end if;
1102
        else
1103 13 magro732
          -- There has been no timeout.
1104
 
1105
          -- Check if any control symbol has been received from the link
1106
          -- partner.
1107
          if (txControlEmpty_i = '0') then
1108
            -- A control symbol has been received.
1109 4 magro732
 
1110 13 magro732
            -- Check the received control symbol.
1111
            case txControlStype0 is
1112
 
1113
              when STYPE0_STATUS =>
1114
                if (outputErrorStopped_i = '0') then
1115
                  -- Save the number of buffers in the link partner.
1116
                  bufferStatus_o <= txControlParameter1;
1117
                end if;
1118 4 magro732
 
1119 13 magro732
              when STYPE0_PACKET_ACCEPTED =>
1120
                -- The link partner is accepting a frame.
1121 4 magro732
 
1122 13 magro732
                if (outputErrorStopped_i = '0') then
1123
                  -- Save the number of buffers in the link partner.
1124
                  bufferStatus_o <= txControlParameter1;
1125 4 magro732
 
1126 13 magro732
                  -- Check if expecting this type of reply and that the ackId is
1127
                  -- expected.
1128
                  if ((ackId_i /= ackIdWindow_i) and
1129
                      (ackId_i = txControlParameter0)) then
1130
                    -- The packet-accepted is expected and the ackId is the expected.
1131
                    -- The frame has been accepted by the link partner.
1132 11 magro732
 
1133 13 magro732
                    -- Update to a new buffer and increment the ackId.
1134
                    readFrameOut <= '1';
1135
                    ackId_o <= std_logic_vector(unsigned(ackId_i) + 1);
1136
                  else
1137
                    -- Unexpected packet-accepted or packet-accepted for
1138
                    -- unexpected ackId.
1139
                    sendLinkRequestOut <= '1';
1140
                    numberSentLinkRequests_o <= NUMBER_LINK_RESPONSE_RETRIES;
1141
                    outputErrorStopped_o <= '1';
1142 4 magro732
                  end if;
1143 13 magro732
                end if;
1144
 
1145
              when STYPE0_PACKET_RETRY =>
1146
                -- The link partner has asked for a frame retransmission.
1147 4 magro732
 
1148 13 magro732
                if (outputErrorStopped_i = '0') then
1149
                  -- Save the number of buffers in the link partner.
1150
                  bufferStatus_o <= txControlParameter1;
1151 4 magro732
 
1152 13 magro732
                  -- Check if the ackId is the one expected.
1153
                  if (ackId_i = txControlParameter0) then
1154
                    -- The ackId to retry is expected.
1155
                    -- Go to the output-retry-stopped state.
1156
                    -- REMARK: The output-retry-stopped state is equivalent
1157
                    -- to sending a restart-from-retry.
1158
                    sendRestartFromRetryOut <= '1';
1159
                  else
1160
                    -- Unexpected ackId to retry.
1161 11 magro732
                    sendLinkRequestOut <= '1';
1162
                    numberSentLinkRequests_o <= NUMBER_LINK_RESPONSE_RETRIES;
1163
                    outputErrorStopped_o <= '1';
1164
                  end if;
1165 13 magro732
                end if;
1166
 
1167
              when STYPE0_PACKET_NOT_ACCEPTED =>
1168
                if (outputErrorStopped_i = '0') then
1169
                  -- Packet was rejected by the link-partner.
1170
                  sendLinkRequestOut <= '1';
1171
                  numberSentLinkRequests_o <= NUMBER_LINK_RESPONSE_RETRIES;
1172
                  outputErrorStopped_o <= '1';
1173
                end if;
1174
 
1175
              when STYPE0_LINK_RESPONSE =>
1176
                if (outputErrorStopped_i = '1') then
1177
                  -- Check if the link partner return value is acceptable.
1178
                  if ((unsigned(txControlParameter0) - unsigned(ackId_i)) <=
1179
                      (unsigned(ackIdWindow_i) - unsigned(ackId_i))) then
1180
                    -- Recoverable error.
1181
                    -- Use the received ackId and recover by removing packets
1182
                    -- that has been received by the link-partner.
1183
                    recoverCounter_o <= txControlParameter0;
1184
                    recoverActive_o <= '1';
1185 11 magro732
                  else
1186 13 magro732
                    -- Totally out of sync.
1187 14 magro732
 
1188
                    -- Indicate that a fatal error has occurred.
1189
                    fatalError_o <= '1';
1190
 
1191 13 magro732
                    -- REMARK: What to do here???
1192
                    --readWindowReset_o <= '1';
1193
                    --stateNext <= STATE_UNINITIALIZED;
1194 11 magro732
                  end if;
1195 13 magro732
                else
1196
                  -- Dont expect or need a link-response in this state.
1197 4 magro732
                  -- Discard it.
1198 13 magro732
                end if;
1199
 
1200
              when STYPE0_VC_STATUS =>
1201
                -- Not supported.
1202
                -- Discard it.
1203
 
1204
              when STYPE0_RESERVED =>
1205
                -- Not supported.
1206
                -- Discard it.
1207 4 magro732
 
1208 13 magro732
              when STYPE0_IMPLEMENTATION_DEFINED =>
1209
                -- Not supported.
1210
                -- Discard it.
1211
 
1212
              when others =>
1213
                null;
1214
            end case;
1215 4 magro732
 
1216 13 magro732
            -- Indicate the control symbol has been processed.
1217
            txControlUpdateOut <= '1';
1218 11 magro732
          end if;
1219
        end if;
1220
      end if;
1221
    end if;
1222
  end process;
1223
 
1224
  -----------------------------------------------------------------------------
1225
  -- N-2 pipeline stage.
1226
  -- Create stype1-part of symbols and data symbols. Save the time when a
1227
  -- packet was fully sent.
1228
  -- Input:  sendRestartFromRetry, sendLinkRequest
1229
  -- Output: ackIdWindow, frameState, timeout(0 to 31), 
1230
  --         symbolControlStart, symbolControlEnd, symbolControlRestart,
1231
  --         symbolControlLinkRequest, symbolData2, symbolData2Content.
1232
  -----------------------------------------------------------------------------
1233 4 magro732
 
1234 11 magro732
  process(clk, areset_n)
1235
  begin
1236
    if (areset_n = '0') then
1237 13 magro732
      readFrameRestart_o <= '0';
1238
      readWindowReset_o <= '0';
1239
      readWindowNext_o <= '0';
1240
      readContent_o <= '0';
1241
 
1242 11 magro732
      symbolControlRestart <= '0';
1243
      symbolControlLinkRequest <= '0';
1244
      symbolControlStart <= '0';
1245
      symbolControlEnd <= '0';
1246
      symbolData <= '0';
1247
      symbolDataContent <= (others => '0');
1248
    elsif (clk'event and clk = '1') then
1249 13 magro732
      readFrameRestart_o <= '0';
1250
      readWindowReset_o <= '0';
1251
      readWindowNext_o <= '0';
1252
      readContent_o <= '0';
1253
 
1254
      if (txFull_i = '0') then
1255
        readFrameRestart_o <= readFrameRestartOut;
1256
        readWindowReset_o <= readWindowResetOut;
1257
        readWindowNext_o <= readWindowNextOut;
1258
        readContent_o <= readContentOut;
1259
 
1260
        symbolControlRestart <= symbolControlRestartOut;
1261
        symbolControlLinkRequest <= symbolControlLinkRequestOut;
1262
        symbolControlStart <= symbolControlStartOut;
1263
        symbolControlEnd <= symbolControlEndOut;
1264
        symbolData <= symbolDataOut;
1265
        symbolDataContent <= symbolDataContentOut;
1266
      end if;
1267 11 magro732
    end if;
1268
  end process;
1269
 
1270
  -- This process decide which stype1-part of a control symbols to send as well
1271
  -- as all data symbols.
1272 13 magro732
  process(readWindowEmpty_i, bufferStatus_i,
1273
          recoverActive_i, recoverCounter_i, ackId_i, operational_i, outputErrorStopped_i, portEnable_i, readContentData_i, readContentWords_i, readContentEnd_i,
1274
          frameState_i, frameWordCounter_i, frameContent_i,
1275
          ackIdWindow_i,
1276 14 magro732
          sendRestartFromRetry, sendLinkRequest,
1277
          fatalError_i)
1278 11 magro732
  begin
1279 13 magro732
    readFrameRestartOut <= '0';
1280
    readWindowResetOut <= '0';
1281
    readWindowNextOut <= '0';
1282
    readContentOut <= '0';
1283 4 magro732
 
1284 11 magro732
    frameState_o <= frameState_i;
1285 12 magro732
    frameWordCounter_o <= frameWordCounter_i;
1286
    frameContent_o <= frameContent_i;
1287 11 magro732
    ackIdWindow_o <= ackIdWindow_i;
1288
 
1289 13 magro732
    timeSentSet_o <= '0';
1290 4 magro732
 
1291 11 magro732
    symbolControlRestartOut <= '0';
1292
    symbolControlLinkRequestOut <= '0';
1293
    symbolControlStartOut <= '0';
1294
    symbolControlEndOut <= '0';
1295
    symbolDataOut <= '0';
1296
    symbolDataContentOut <= (others => '0');
1297 4 magro732
 
1298 14 magro732
    if (fatalError_i = '1') then
1299
      readWindowResetOut <= '1';
1300
    elsif (recoverActive_i = '1') then
1301 11 magro732
      -- REMARK: Make sure idle is generated when this state is active...
1302 14 magro732
      ackIdWindow_o <= ackId_i;
1303
      frameState_o <= FRAME_IDLE;
1304
      readWindowResetOut <= '1';
1305 11 magro732
    else
1306 13 magro732
      if (operational_i = '0') then
1307
        -----------------------------------------------------------------------
1308
        -- This state is entered at startup. A port that is not initialized
1309
        -- should only transmit idle sequences.
1310
        -----------------------------------------------------------------------
1311
 
1312
        -- Initialize framing before entering the operational state.
1313
        -- REMARK: Only do this when the portInitialized becomes asserted???
1314 14 magro732
        frameState_o <= FRAME_IDLE;
1315 13 magro732
        ackIdWindow_o <= ackId_i;
1316
        readWindowResetOut <= '1';
1317
      else
1318
        -------------------------------------------------------------------
1319
        -- This state is the operational state. It relays frames and handle
1320
        -- flow control.
1321
        -------------------------------------------------------------------
1322
 
1323
        if (sendRestartFromRetry = '1') then
1324
          -- Send a restart-from-retry control symbol to acknowledge the restart
1325
          -- of the frame.
1326
          symbolControlRestartOut <= '1';
1327
 
1328
          -- Make sure there wont be any timeout before the frame is
1329
          -- starting to be retransmitted.
1330
          timeSentSet_o <= '1';
1331
 
1332
          -- Restart the frame transmission.
1333
          ackIdWindow_o <= ackId_i;
1334 14 magro732
          frameState_o <= FRAME_IDLE;
1335 13 magro732
          readWindowResetOut <= '1';
1336
        end if;
1337 4 magro732
 
1338 13 magro732
        if (sendLinkRequest = '1') then
1339
          -- Dont restart the packet transmission since we do not yet know which
1340
          -- packets that was successfully received by our link partner.
1341 4 magro732
 
1342 13 magro732
          -- Send a link-request symbol.
1343
          symbolControlLinkRequestOut <= '1';
1344 4 magro732
 
1345 13 magro732
          -- Write the current timer value.
1346
          timeSentSet_o <= '1';
1347
        end if;
1348
 
1349
        if ((sendRestartFromRetry = '0') and (sendLinkRequest = '0')  and
1350
            (outputErrorStopped_i = '0')) then
1351
          -- Check if a frame transfer is in progress.
1352
          -- REMARK: Hold any data symbol if there is a pending symbol from the
1353
          -- receiver side...
1354 4 magro732
 
1355 13 magro732
          case frameState_i is
1356
 
1357 14 magro732
            when FRAME_IDLE =>
1358 13 magro732
              ---------------------------------------------------------------
1359
              -- No frame has been started.
1360
              ---------------------------------------------------------------
1361 4 magro732
 
1362 14 magro732
              -- Wait for a new frame to arrive from the frame buffer.
1363
              if (readWindowEmpty_i = '0') then
1364 13 magro732
                -- Update the output from the frame buffer to contain the
1365
                -- data when it is read later.
1366
                readContentOut <= '1';
1367 14 magro732
 
1368
                -- Indicate that a control symbol has been sent to start the
1369
                -- transmission of the frame.
1370
                frameState_o <= FRAME_START;
1371 13 magro732
              end if;
1372
 
1373 14 magro732
            when FRAME_START =>
1374 13 magro732
              -------------------------------------------------------
1375
              -- Check if we are allowed to transmit this packet.
1376
              -------------------------------------------------------
1377 14 magro732
              -- The packet may be either not allowed, i.e. a non-maintenance
1378
              -- sent when only maintenance is allowed. The link-partner can be
1379
              -- busy, i.e. no having enough buffers to receive the new packet
1380
              -- in or the number of outstanding packets may be too large.
1381
              -- REMARK: Only update readContent_o in the last instance
1382
              -- if cascaded...
1383
 
1384
              -- Check if the packet is allowed.
1385 13 magro732
              if ((portEnable_i = '1') or
1386
                  (readContentData_i(19 downto 16) = FTYPE_MAINTENANCE_CLASS)) then
1387 14 magro732
                -- Packet is allowed.
1388 4 magro732
 
1389 14 magro732
                -- Check if the link is able to accept the new frame.
1390
                if ((bufferStatus_i /= "00000") and
1391
                    ((unsigned(ackIdWindow_i) - unsigned(ackId_i)) /= 31)) then
1392
                  -- New data is available for transmission and there
1393
                  -- is room to receive it at the other side.
1394
                  -- The packet may be transmitted.
1395
 
1396
                  -- Indicate that a control symbol has been sent to start the
1397
                  -- transmission of the frame.
1398
                  frameWordCounter_o <= readContentWords_i;
1399
                  frameContent_o <= readContentData_i;
1400
                  readContentOut <= '1';
1401
 
1402
                  -- Send a control symbol to start the packet and a status to
1403
                  -- complete the symbol.
1404
                  symbolControlStartOut <= '1';
1405 4 magro732
 
1406 14 magro732
                  -- Proceed to send the first packet data symbol containing
1407
                  -- the ackId.
1408
                  frameState_o <= FRAME_FIRST;
1409
                else
1410
                  -- The link cannot accept the packet.
1411
                  -- Wait in this state and dont do anything.
1412 11 magro732
                end if;
1413 14 magro732
              else
1414
                -- The packet is not allowed.
1415
                -- Discard it.
1416
                frameState_o <= FRAME_DISCARD;
1417 13 magro732
              end if;
1418 14 magro732
 
1419
            when FRAME_FIRST =>
1420 13 magro732
              ---------------------------------------------------------------
1421
              -- Send the first packet content containing our current
1422
              -- ackId.
1423
              ---------------------------------------------------------------
1424 11 magro732
 
1425 13 magro732
              -- Write a new data symbol and fill in our ackId on the
1426
              -- packet.
1427
              symbolDataOut <= '1';
1428
              symbolDataContentOut <=
1429
                std_logic_vector(ackIdWindow_i) & "0" &
1430
                frameContent_i((32*NUMBER_WORDS)-7 downto (32*(NUMBER_WORDS-1)));
1431 4 magro732
 
1432 13 magro732
              -- REMARK: Code frameWordCounter as 0=1 or 1=1???
1433
              if (unsigned(frameWordCounter_i) /= 0) then
1434
                frameWordCounter_o <=
1435
                  std_logic_vector(unsigned(frameWordCounter_i) - 1);
1436
                frameContent_o <=
1437
                  frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & x"00000000";
1438
              else
1439
                frameWordCounter_o <= readContentWords_i;
1440
                frameContent_o <= readContentData_i;
1441
                readContentOut <= '1';
1442
              end if;
1443 14 magro732
 
1444 13 magro732
              -- Continue to send the rest of the body of the packet.
1445 14 magro732
              frameState_o <= FRAME_MIDDLE;
1446 13 magro732
 
1447 14 magro732
            when FRAME_MIDDLE =>
1448 13 magro732
              ---------------------------------------------------------------
1449
              -- The frame has not been fully sent.
1450 14 magro732
              -- Send a data symbol until the last part of the packet is
1451
              -- detected.
1452 13 magro732
              ---------------------------------------------------------------
1453
              -- REMARK: Dont send anything if there is a pending symbol in the
1454
              -- rx-control fifo to let it be transmitted in the middle of the
1455
              -- packet...
1456
 
1457
              -- Write a new data symbol.
1458
              symbolDataOut <= '1';
1459
              symbolDataContentOut <=
1460
                frameContent_i((32*NUMBER_WORDS)-1 downto (32*(NUMBER_WORDS-1)));
1461 4 magro732
 
1462 13 magro732
              if (unsigned(frameWordCounter_i) /= 0) then
1463
                frameWordCounter_o <=
1464
                  std_logic_vector(unsigned(frameWordCounter_i) - 1);
1465
                frameContent_o <=
1466
                  frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & x"00000000";
1467
              else
1468 14 magro732
                frameWordCounter_o <= readContentWords_i;
1469
                frameContent_o <= readContentData_i;
1470
 
1471 13 magro732
                -- Check if the frame is ending.
1472 14 magro732
                -- REMARK: Dont forget to update RioPacketBuffer to support this...
1473 13 magro732
                if (readContentEnd_i = '1') then
1474
                  -- The frame is ending.
1475 11 magro732
 
1476 13 magro732
                  -- Update the window to the next frame.
1477
                  -- It takes one tick for the output from the frame
1478
                  -- buffer to get updated.
1479
                  readWindowNextOut <= '1';
1480 11 magro732
 
1481 13 magro732
                  -- Proceed to check if there is another frame to start
1482
                  -- with directly.
1483 14 magro732
                  frameState_o <= FRAME_LAST;
1484 4 magro732
                else
1485 13 magro732
                  readContentOut <= '1';
1486 4 magro732
                end if;
1487 13 magro732
              end if;
1488
 
1489 14 magro732
            when FRAME_LAST =>
1490 13 magro732
              ---------------------------------------------------------------
1491
              -- A frame has ended and the window has been updated.
1492
              -- Check if the next symbol should end the frame or if a
1493
              -- new one should be started.
1494
              ---------------------------------------------------------------
1495 4 magro732
 
1496 14 magro732
              -- Write a new data symbol.
1497
              symbolDataOut <= '1';
1498
              symbolDataContentOut <=
1499
                frameContent_i((32*NUMBER_WORDS)-1 downto (32*(NUMBER_WORDS-1)));
1500 11 magro732
 
1501 14 magro732
              if (unsigned(frameWordCounter_i) /= 0) then
1502
                frameWordCounter_o <=
1503
                  std_logic_vector(unsigned(frameWordCounter_i) - 1);
1504
                frameContent_o <=
1505
                  frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & x"00000000";
1506
              else
1507
                -- Update the window ackId.
1508
                ackIdWindow_o <= std_logic_vector(unsigned(ackIdWindow_i) + 1);
1509 11 magro732
 
1510 14 magro732
                -- Start timeout supervision for transmitted frame.
1511
                timeSentSet_o <= '1';
1512 11 magro732
 
1513 14 magro732
                -- Note that the packet must be ended if the link-partner
1514
                -- cannot receive it.
1515
                if ((readWindowEmpty_i = '0') and
1516
                    (bufferStatus_i /= "00000") and
1517
                    ((unsigned(ackIdWindow_i) - unsigned(ackId_i)) /= 31)) then
1518
                  readContentOut <= '1';
1519
                  frameState_o <= FRAME_START;
1520
                else
1521
                  frameState_o <= FRAME_END;
1522
                end if;
1523
              end if;
1524
 
1525
            when FRAME_END =>
1526
              -----------------------------------------------------------------
1527
              -- 
1528
              -----------------------------------------------------------------
1529
 
1530 13 magro732
              -- Start a new frame the next time.
1531 14 magro732
              frameState_o <= FRAME_IDLE;
1532 13 magro732
 
1533 14 magro732
              -- Send a control symbol to end the packet.
1534
              symbolControlEndOut <= '1';
1535
 
1536
            when FRAME_DISCARD =>
1537
              ---------------------------------------------------------------
1538
              -- 
1539
              ---------------------------------------------------------------
1540
              -- The packet should be discarded.
1541
              -- Send idle-sequence.
1542
 
1543
              -- Check that there are no outstanding packets that
1544
              -- has not been acknowledged.
1545
              if(unsigned(ackIdWindow_i) = unsigned(ackId_i)) then
1546
                -- No unacknowledged packets.
1547
                -- It is now safe to remove the unallowed frame.
1548
                -- REMARK: Discard packet; readFrameOut <= '1';
1549
 
1550
                -- Go back and send a new frame.
1551
                frameState_o <= FRAME_IDLE;
1552
              end if;
1553
 
1554 13 magro732
            when others =>
1555
              ---------------------------------------------------------------
1556
              -- 
1557
              ---------------------------------------------------------------
1558
              null;
1559
 
1560
          end case;
1561 11 magro732
        end if;
1562
      end if;
1563
    end if;
1564
  end process;
1565
 
1566
  -----------------------------------------------------------------------------
1567
  -- N-1
1568
  -- Create the stype0 and stype1 part of a control symbol.
1569
  -- This process makes sure that the buffer status are transmitted at least
1570
  -- every 255 symbol.
1571
  -- At startup it makes sure that at least 16 status symbols are transmitted
1572
  -- before the operational-state is entered.
1573
  -- Input:  symbolControlStart, symbolControlEnd, symbolControlRestart,
1574
  --         symbolControlLinkRequest, symbolData, symbolDataContent
1575
  -- Output: symbolsTransmitted_o, operational_o, 
1576
  --         symbolControl, stype0, parameter0, parameter1, stype1, cmd,
1577
  --         symbolData1, symbolData1Content
1578
  -----------------------------------------------------------------------------
1579
 
1580
  process(clk, areset_n)
1581
  begin
1582
    if (areset_n = '0') then
1583 13 magro732
      rxControlUpdate_o <= '0';
1584
 
1585 11 magro732
      controlValid <= '0';
1586
      stype0 <= (others=>'0');
1587
      parameter0 <= (others=>'0');
1588
      parameter1 <= (others=>'0');
1589
      stype1 <= STYPE1_NOP;
1590
      cmd <= (others=>'0');
1591
      dataValid <= '0';
1592
      dataContent <= (others=>'0');
1593
    elsif (clk'event and clk = '1') then
1594
      if (txFull_i = '0') then
1595 13 magro732
        rxControlUpdate_o <= rxControlUpdateOut;
1596
 
1597 11 magro732
        controlValid <= controlValidOut;
1598
        stype0 <= stype0Out;
1599
        parameter0 <= parameter0Out;
1600
        parameter1 <= parameter1Out;
1601
        stype1 <= STYPE1_NOP;
1602
        cmd <= "000";
1603
        dataValid <= symbolData;
1604
        dataContent <= symbolDataContent;
1605
 
1606
        if (symbolControlStart = '1') then
1607
          stype1 <= STYPE1_START_OF_PACKET;
1608
        end if;
1609
        if (symbolControlEnd = '1') then
1610
          stype1 <= STYPE1_END_OF_PACKET;
1611
        end if;
1612
        if (symbolControlRestart = '1') then
1613
          stype1 <= STYPE1_RESTART_FROM_RETRY;
1614
        end if;
1615
        if (symbolControlLinkRequest = '1') then
1616
          stype1 <= STYPE1_LINK_REQUEST;
1617
          cmd <= LINK_REQUEST_CMD_INPUT_STATUS;
1618
        end if;
1619
      end if;
1620
    end if;
1621
  end process;
1622
 
1623
  symbolControlStype1 <=
1624
    symbolControlRestart or symbolControlLinkRequest or
1625
    symbolControlStart or symbolControlEnd;
1626
 
1627 13 magro732
  process(linkInitialized_i, ackIdStatus_i, portInitialized_i,
1628
          operational_i, counter_i, statusReceived_i, symbolsTransmitted_i,
1629
          rxControlEmpty_i,
1630
          symbolControlStype1, symbolData,
1631 14 magro732
          rxControlStype0, rxControlParameter0, rxControlParameter1,
1632
          fatalError_i)
1633 11 magro732
  begin
1634
    operational_o <= operational_i;
1635
    counter_o <= counter_i;
1636 12 magro732
    symbolsTransmitted_o <= symbolsTransmitted_i;
1637 13 magro732
    rxControlUpdateOut <= '0';
1638 11 magro732
 
1639
    controlValidOut <= '0';
1640
    stype0Out <= STYPE0_STATUS;
1641
    parameter0Out <= ackIdStatus_i;
1642
    parameter1Out <= "11111";
1643
 
1644 14 magro732
    if (fatalError_i = '1') then
1645
      operational_o <= '0';
1646
      counter_o <= NUMBER_STATUS_TRANSMIT;
1647
      symbolsTransmitted_o <= (others=>'0');
1648
    else
1649
      -- Check the operational state.
1650
      if (operational_i = '0') then
1651
        -----------------------------------------------------------------------
1652
        -- This state is entered at startup. A port that is not initialized
1653
        -- should only transmit idle sequences.
1654
        -----------------------------------------------------------------------
1655
 
1656
        -- Check if the port is initialized.
1657
        if (portInitialized_i = '1') then
1658
          ---------------------------------------------------------------------
1659
          -- The specification requires a status control symbol being sent at
1660
          -- least every 1024 code word until an error-free status has been
1661
          -- received. This implies that at most 256 idle sequences should be
1662
          -- sent in between status control symbols. Once an error-free status 
1663
          -- has been received, status symbols may be sent more rapidly. At
1664
          -- least 15 statuses has to be transmitted once an error-free status
1665
          -- has been received.
1666
          ---------------------------------------------------------------------
1667 11 magro732
 
1668 14 magro732
          -- Check if we are ready to change state to operational.
1669
          if ((linkInitialized_i = '1') and
1670
              (unsigned(counter_i) = 0)) then
1671
            -- Receiver has received enough error free status symbols and we
1672
            -- have transmitted enough.
1673
 
1674
            -- Considder ourselfs operational.
1675
            operational_o <= '1';
1676
          else
1677
            -- Not ready to change state to operational.
1678
            -- Dont do anything.
1679
          end if;
1680 13 magro732
 
1681 14 magro732
          -- Check if idle sequence or a status symbol should be transmitted.
1682
          if (((statusReceived_i = '0') and (symbolsTransmitted_i = x"ff")) or
1683
              ((statusReceived_i = '1') and (symbolsTransmitted_i > x"0f"))) then
1684
            -- A status symbol should be transmitted.
1685
 
1686
            -- Send a status control symbol to the link partner.
1687
            controlValidOut <= '1';
1688 11 magro732
 
1689 14 magro732
            -- Reset idle sequence transmission counter.
1690
            symbolsTransmitted_o <= (others=>'0');
1691 11 magro732
 
1692 14 magro732
            -- Check if the number of transmitted statuses should be updated.
1693
            if (statusReceived_i = '1') and (unsigned(counter_i) /= 0) then
1694
              counter_o <= std_logic_vector(unsigned(counter_i) - 1);
1695
            end if;
1696
          else
1697
            -- Increment the idle sequence transmission counter.
1698
            symbolsTransmitted_o <= std_logic_vector(unsigned(symbolsTransmitted_i) + 1);
1699 4 magro732
          end if;
1700 11 magro732
        else
1701 14 magro732
          -- The port is not initialized.
1702
          -- Reset initialization variables.
1703
          counter_o <= NUMBER_STATUS_TRANSMIT;
1704 11 magro732
        end if;
1705
      else
1706 14 magro732
        ---------------------------------------------------------------------
1707
        -- This is the operational state.
1708
        -- It is entered once the link has been considdered up and running.
1709
        ---------------------------------------------------------------------
1710 4 magro732
 
1711 14 magro732
        -- Check if the port is still initialized.
1712
        if (portInitialized_i = '1') then
1713
          -- The port is still initialized.
1714
 
1715
          -- Check if a status must be sent.
1716
          -- A status must be sent when there are not other stype0 value to
1717
          -- send or if too many symbols without buffer-status has been sent.
1718
          -- REMARK: Is there a risk of a race when a generated status-symbol
1719
          -- is sent before another symbol stored in the rx-control fifo???
1720
          if (((symbolControlStype1 = '1') and (rxControlEmpty_i = '1')) or
1721
              ((symbolControlStype1 = '0') and (symbolData = '0') and
1722
               (symbolsTransmitted_i = x"ff"))) then
1723
            -- A control symbol is about to be sent without pending symbol from
1724
            -- receiver or too many idle symbols has been sent.
1725 13 magro732
 
1726 14 magro732
            -- Force the sending of a status containing the bufferStatus.
1727
            controlValidOut <= '1';
1728 11 magro732
            symbolsTransmitted_o <= (others=>'0');
1729 14 magro732
          elsif ((symbolData = '0') and (rxControlEmpty_i = '0')) then
1730
            -- A control symbol is about to be sent and there is a pending
1731
            -- symbol from the receiver.
1732
 
1733
            -- Remove the symbol from the fifo.
1734
            rxControlUpdateOut <= '1';
1735
 
1736
            -- Send the receiver symbol.
1737
            controlValidOut <= '1';
1738
            stype0Out <= rxControlStype0;
1739
            parameter0Out <= rxControlParameter0;
1740
            parameter1Out <= rxControlParameter1;
1741
 
1742
            -- Check if the transmitted symbol contains status about
1743
            -- available buffers.
1744
            if ((rxControlStype0 = STYPE0_PACKET_ACCEPTED) or
1745
                (rxControlStype0 = STYPE0_PACKET_RETRY)) then
1746
              -- A symbol containing the bufferStatus has been sent.
1747
              symbolsTransmitted_o <= (others=>'0');
1748
            else
1749
              -- A symbol not containing the bufferStatus has been sent.
1750
              symbolsTransmitted_o <= std_logic_vector(unsigned(symbolsTransmitted_i) + 1);
1751
            end if;
1752 4 magro732
          else
1753 11 magro732
            -- A symbol not containing the bufferStatus has been sent.
1754 14 magro732
            controlValidOut <= '0';
1755 11 magro732
            symbolsTransmitted_o <= std_logic_vector(unsigned(symbolsTransmitted_i) + 1);
1756 4 magro732
          end if;
1757 11 magro732
        else
1758 14 magro732
          -- The port is not initialized anymore.
1759
          -- Change the operational state.
1760
          operational_o <= '0';
1761
        end if;
1762
      end if;
1763 11 magro732
    end if;
1764
  end process;
1765
 
1766
  -----------------------------------------------------------------------------
1767
  -- N pipeline stage.
1768
  -- Make all symbols ready for transmission, i.e. calculate the CRC5 on
1769
  -- control symbols.
1770
  -- Inputs: controlValid, stype0, parameter0, parameter1, stype1, cmd,
1771
  --         symbolData1, symbolData1Content 
1772
  -----------------------------------------------------------------------------
1773 4 magro732
 
1774 11 magro732
  controlContent(31 downto 29) <= stype0;
1775
  controlContent(28 downto 24) <= parameter0;
1776
  controlContent(23 downto 19) <= parameter1;
1777
  controlContent(18 downto 16) <= stype1;
1778
  controlContent(15 downto 13) <= cmd;
1779
  controlContent(12 downto 8) <= crc5;
1780
  controlContent(7 downto 0) <= x"00";
1781 4 magro732
 
1782 11 magro732
  Crc5Calculator: Crc5ITU
1783
    port map(
1784
      d_i=>controlContent(31 downto 13), crc_o=>crc5);
1785
 
1786
  txWrite_o <= not txFull_i;
1787
  process(clk, areset_n)
1788
  begin
1789
    if (areset_n = '0') then
1790
      txType_o <= SYMBOL_IDLE;
1791
      txData_o <= (others=>'0');
1792
    elsif (clk'event and clk = '1') then
1793
      if (txFull_i = '0') then
1794
        txType_o <= SYMBOL_IDLE;
1795
        if (controlValid = '1') then
1796
          txType_o <= SYMBOL_CONTROL;
1797
          txData_o <= controlContent;
1798
        end if;
1799
        if (dataValid = '1') then
1800
          txType_o <= SYMBOL_DATA;
1801
          txData_o <= dataContent;
1802
        end if;
1803
      end if;
1804 4 magro732
    end if;
1805 11 magro732
  end process;
1806
 
1807 4 magro732
end architecture;
1808
 
1809
 
1810
 
1811
-------------------------------------------------------------------------------
1812
-- 
1813
-------------------------------------------------------------------------------
1814
library ieee;
1815
use ieee.std_logic_1164.all;
1816
use ieee.numeric_std.all;
1817
use work.rio_common.all;
1818
 
1819
 
1820 11 magro732
entity RioReceiver is
1821
  generic(
1822
    NUMBER_WORDS : natural range 1 to 4 := 1);
1823
  port(
1824
    clk : in std_logic;
1825
    areset_n : in std_logic;
1826
 
1827
    portEnable_i : in std_logic;
1828
 
1829
    localAckIdWrite_i : in std_logic;
1830
    inboundAckId_i : in std_logic_vector(4 downto 0);
1831
    inboundAckId_o : out std_logic_vector(4 downto 0);
1832
 
1833
    portInitialized_i : in std_logic;
1834
    rxEmpty_i : in std_logic;
1835
    rxRead_o : out std_logic;
1836
    rxType_i : in std_logic_vector(2*NUMBER_WORDS-1 downto 0);
1837
    rxData_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1838
 
1839
    txControlWrite_o : out std_logic_vector(NUMBER_WORDS-1 downto 0);
1840
    txControlSymbol_o : out std_logic_vector(12*NUMBER_WORDS downto 0);
1841
    rxControlWrite_o : out std_logic_vector(NUMBER_WORDS-1 downto 0);
1842
    rxControlSymbol_o : out std_logic_vector(12*NUMBER_WORDS downto 0);
1843
 
1844
    ackIdStatus_o : out std_logic_vector(4 downto 0);
1845
    linkInitialized_o : out std_logic;
1846
 
1847
    writeFrameFull_i : in std_logic;
1848
    writeFrame_o : out std_logic;
1849
    writeFrameAbort_o : out std_logic;
1850
    writeContent_o : out std_logic;
1851
    writeContentWords_o : out std_logic_vector(1 downto 0);
1852
    writeContentData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0));
1853
end entity;
1854
 
1855 4 magro732
-------------------------------------------------------------------------------
1856
-- 
1857
-------------------------------------------------------------------------------
1858 11 magro732
architecture RioReceiverImpl of RioReceiver is
1859
 
1860
  component RioReceiverCore is
1861
    generic(
1862
      NUMBER_WORDS : natural range 1 to 4 := 1);
1863
    port(
1864
      clk : in std_logic;
1865
      areset_n : in std_logic;
1866
 
1867
      -- Status signals used for maintenance.
1868
      portEnable_i : in std_logic;
1869
 
1870
      -- Support for localAckIdCSR.
1871
      -- REMARK: Add support for this???
1872
      localAckIdWrite_i : in std_logic;
1873
      inboundAckId_i : in std_logic_vector(4 downto 0);
1874
      inboundAckId_o : out std_logic_vector(4 downto 0);
1875
 
1876
      -- Port input interface.
1877
      portInitialized_i : in std_logic;
1878
      rxEmpty_i : in std_logic;
1879
      rxRead_o : out std_logic;
1880
      rxType_i : in std_logic_vector(1 downto 0);
1881
      rxData_i : in std_logic_vector(31 downto 0);
1882
 
1883
      -- Receiver has received a control symbol containing:
1884
      -- packet-accepted, packet-retry, packet-not-accepted, 
1885
      -- status, VC_status, link-response
1886
      txControlWrite_o : out std_logic;
1887
      txControlSymbol_o : out std_logic_vector(12 downto 0);
1888
 
1889
      -- Reciever wants to signal the link partner:
1890
      -- a new frame has been accepted => packet-accepted(rxAckId, bufferStatus)
1891
      -- a frame needs to be retransmitted due to buffering =>
1892
      -- packet-retry(rxAckId, bufferStatus)
1893
      -- a frame is rejected due to errors => packet-not-accepted
1894
      -- a link-request should be answered => link-response
1895
      rxControlWrite_o : out std_logic;
1896
      rxControlSymbol_o : out std_logic_vector(12 downto 0);
1897
 
1898
      -- Status signals used internally.
1899
      ackIdStatus_o : out std_logic_vector(4 downto 0);
1900
      linkInitialized_o : out std_logic;
1901
 
1902
      -- Core->Core cascading signals.
1903
      enable_o : out std_logic;
1904
      operational_i : in std_logic;
1905
      operational_o : out std_logic;
1906
      inputRetryStopped_i : in std_logic;
1907
      inputRetryStopped_o : out std_logic;
1908
      inputErrorStopped_i : in std_logic;
1909
      inputErrorStopped_o : out std_logic;
1910
      ackId_i : in unsigned(4 downto 0);
1911
      ackId_o : out unsigned(4 downto 0);
1912
      frameIndex_i : in std_logic_vector(6 downto 0);
1913
      frameIndex_o : out std_logic_vector(6 downto 0);
1914
      frameWordCounter_i : in std_logic_vector(1 downto 0);
1915
      frameWordCounter_o : out std_logic_vector(1 downto 0);
1916
      frameContent_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1917
      frameContent_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1918
      crc_i : in std_logic_vector(15 downto 0);
1919
      crc_o : out std_logic_vector(15 downto 0);
1920
 
1921
      -- Frame buffering interface.
1922
      writeFrameFull_i : in std_logic;
1923
      writeFrame_o : out std_logic;
1924
      writeFrameAbort_o : out std_logic;
1925
      writeContent_o : out std_logic;
1926
      writeContentWords_o : out std_logic_vector(1 downto 0);
1927
      writeContentData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0));
1928
  end component;
1929
 
1930
  signal enable : std_logic_vector(NUMBER_WORDS-1 downto 0);
1931
  signal operationalCurrent, operationalNext : std_logic_vector(NUMBER_WORDS-1 downto 0);
1932
  signal inputRetryStoppedCurrent, inputRetryStoppedNext : std_logic_vector(NUMBER_WORDS-1 downto 0);
1933
  signal inputErrorStoppedCurrent, inputErrorStoppedNext : std_logic_vector(NUMBER_WORDS-1 downto 0);
1934
  signal ackIdCurrent, ackIdNext : unsigned(5*NUMBER_WORDS-1 downto 0);
1935
  signal frameIndexCurrent, frameIndexNext : std_logic_vector(7*NUMBER_WORDS-1 downto 0);
1936
  signal frameWordCounterCurrent, frameWordCounterNext : std_logic_vector(2*NUMBER_WORDS-1 downto 0);
1937
  signal frameContentCurrent, frameContentNext : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1938
  signal crcCurrent, crcNext : std_logic_vector(16*NUMBER_WORDS-1 downto 0);
1939
 
1940
  signal txControlWrite : std_logic_vector(NUMBER_WORDS-1 downto 0);
1941
  signal rxControlWrite : std_logic_vector(NUMBER_WORDS-1 downto 0);
1942
 
1943
  signal writeFrame : std_logic_vector(NUMBER_WORDS-1 downto 0);
1944
  signal writeFrameAbort : std_logic_vector(NUMBER_WORDS-1 downto 0);
1945
  signal writeContent : std_logic_vector(NUMBER_WORDS-1 downto 0);
1946
  signal writeContentWords : std_logic_vector(2*NUMBER_WORDS-1 downto 0);
1947
  signal writeContentData : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1948
 
1949
begin
1950
 
1951
  -----------------------------------------------------------------------------
1952
  -- Output generation to packet buffer.
1953
  -----------------------------------------------------------------------------
1954
  process(enable, writeFrame, writeFrameAbort,
1955
          writeContent, writeContentWords, writeContentData)
1956
  begin
1957
    writeFrame_o <= '0';
1958
    writeFrameAbort_o <= '0';
1959
    writeContent_o <= '0';
1960
    writeContentWords_o <= (others=>'0');
1961
    writeContentData_o <= (others=>'0');
1962
    for i in 0 to NUMBER_WORDS-1 loop
1963
      if ((writeFrame(i) = '1') and (enable(i) = '1')) then
1964
        writeFrame_o <= '1';
1965
      end if;
1966
 
1967
      if ((writeFrameAbort(i) = '1') and (enable(i) = '1')) then
1968
        writeFrameAbort_o <= '1';
1969
      end if;
1970
 
1971
      if ((writeContent(i) = '1') and (enable(i) = '1')) then
1972
        writeContent_o <= '1';
1973
        writeContentWords_o <= writeContentWords(2*(i+1)-1 downto 2*i);
1974
        writeContentData_o <= writeContentData(32*(i+1)-1 downto 32*i);
1975
      end if;
1976
    end loop;
1977
  end process;
1978
 
1979
  -----------------------------------------------------------------------------
1980
  -- Protocol core and synchronization.
1981
  -----------------------------------------------------------------------------
1982
  process(clk, areset_n)
1983
  begin
1984
    if (areset_n = '0') then
1985
      operationalCurrent <= (others=>'0');
1986
      inputRetryStoppedCurrent <= (others=>'0');
1987
      inputErrorStoppedCurrent <= (others=>'0');
1988
      ackIdCurrent <= (others=>'0');
1989
      frameIndexCurrent <= (others => '0');
1990
      frameWordCounterCurrent <= (others=>'0');
1991
      frameContentCurrent <= (others=>'0');
1992
      crcCurrent <= (others=>'0');
1993
    elsif (clk'event and clk = '1') then
1994
      if (enable(0) = '1') then
1995
        operationalCurrent <= operationalNext;
1996
        inputRetryStoppedCurrent <= inputRetryStoppedNext;
1997
        inputErrorStoppedCurrent <= inputErrorStoppedNext;
1998
        ackIdCurrent <= ackIdNext;
1999
        frameIndexCurrent <= frameIndexNext;
2000
        frameWordCounterCurrent <= frameWordCounterNext;
2001
        frameContentCurrent <= frameContentNext;
2002
        crcCurrent <= crcNext;
2003
      end if;
2004
    end if;
2005
  end process;
2006
 
2007
  CoreGeneration: for i in 0 to NUMBER_WORDS-1 generate
2008
    txControlWrite_o(i) <= txControlWrite(i) and enable(i);
2009
    rxControlWrite_o(i) <= rxControlWrite(i);
2010
 
2011
    ReceiverCore: RioReceiverCore
2012
      generic map(NUMBER_WORDS=>NUMBER_WORDS)
2013
      port map(
2014
        clk=>clk,
2015
        areset_n=>areset_n,
2016
        portEnable_i=>portEnable_i,
2017
        localAckIdWrite_i=>localAckIdWrite_i,
2018
        inboundAckId_i=>inboundAckId_i,
2019
        inboundAckId_o=>inboundAckId_o,
2020
        portInitialized_i=>portInitialized_i,
2021
        rxEmpty_i=>rxEmpty_i,
2022
        rxRead_o=>rxRead_o,
2023
        rxType_i=>rxType_i,
2024
        rxData_i=>rxData_i,
2025
        txControlWrite_o=>txControlWrite(i),
2026
        txControlSymbol_o=>txControlSymbol_o(13*(i+1)-1 downto 13*i),
2027
        rxControlWrite_o=>rxControlWrite(i),
2028
        rxControlSymbol_o=>rxControlSymbol_o(13*(i+1)-1 downto 13*i),
2029
        ackIdStatus_o=>ackIdStatus_o,
2030
        linkInitialized_o=>linkInitialized_o,
2031
        enable_o=>enable(i),
2032
        operational_i=>operationalCurrent(i),
2033
        operational_o=>operationalNext(i),
2034
        inputRetryStopped_i=>inputRetryStoppedCurrent(i),
2035
        inputRetryStopped_o=>inputRetryStoppedNext(i),
2036
        inputErrorStopped_i=>inputErrorStoppedCurrent(i),
2037
        inputErrorStopped_o=>inputErrorStoppedNext(i),
2038
        ackId_i=>ackIdCurrent(5*(i+1)-1 downto 5*i),
2039
        ackId_o=>ackIdNext(5*(i+1)-1 downto 5*i),
2040
        frameIndex_i=>frameIndexCurrent(7*(i+1)-1 downto 7*i),
2041
        frameIndex_o=>frameIndexNext(7*(i+1)-1 downto 7*i),
2042
        frameWordCounter_i=>frameWordCounterCurrent(2*(i+1)-1 downto 2*i),
2043
        frameWordCounter_o=>frameWordCounterNext(2*(i+1)-1 downto 2*i),
2044
        frameContent_i=>frameContentCurrent(32*(i+1)-1 downto 32*i),
2045
        frameContent_o=>frameContentNext(32*(i+1)-1 downto 32*i),
2046
        crc_i=>crcCurrent(16*(i+1)-1 downto 16*i),
2047
        crc_o=>crcNext(16*(i+1)-1 downto 16*i),
2048
        writeFrameFull_i=>writeFrameFull_i,
2049
        writeFrame_o=>writeFrame(i),
2050
        writeFrameAbort_o=>writeFrameAbort(i),
2051
        writeContent_o=>writeContent(i),
2052
        writeContentWords_o=>writeContentWords(2*(i+1)-1 downto 2*i),
2053
        writeContentData_o=>writeContentData(32*(i+1)-1 downto 32*i));
2054
  end generate;
2055
 
2056
end architecture;
2057
 
2058
-------------------------------------------------------------------------------
2059
-- RioReceiverCore
2060
-------------------------------------------------------------------------------
2061
library ieee;
2062
use ieee.std_logic_1164.all;
2063
use ieee.numeric_std.all;
2064
use work.rio_common.all;
2065
 
2066
 
2067
-------------------------------------------------------------------------------
2068
-- 
2069
-------------------------------------------------------------------------------
2070
entity RioReceiverCore is
2071
  generic(
2072
    NUMBER_WORDS : natural range 1 to 4 := 1);
2073 4 magro732
  port(
2074
    clk : in std_logic;
2075
    areset_n : in std_logic;
2076
 
2077
    -- Status signals used for maintenance.
2078
    portEnable_i : in std_logic;
2079
 
2080
    -- Support for localAckIdCSR.
2081 11 magro732
    -- REMARK: Add support for this???
2082 4 magro732
    localAckIdWrite_i : in std_logic;
2083
    inboundAckId_i : in std_logic_vector(4 downto 0);
2084
    inboundAckId_o : out std_logic_vector(4 downto 0);
2085
 
2086
    -- Port input interface.
2087
    portInitialized_i : in std_logic;
2088
    rxEmpty_i : in std_logic;
2089
    rxRead_o : out std_logic;
2090 11 magro732
    rxType_i : in std_logic_vector(1 downto 0);
2091 4 magro732
    rxData_i : in std_logic_vector(31 downto 0);
2092
 
2093
    -- Receiver has received a control symbol containing:
2094
    -- packet-accepted, packet-retry, packet-not-accepted, 
2095
    -- status, VC_status, link-response
2096
    txControlWrite_o : out std_logic;
2097
    txControlSymbol_o : out std_logic_vector(12 downto 0);
2098
 
2099
    -- Reciever wants to signal the link partner:
2100
    -- a new frame has been accepted => packet-accepted(rxAckId, bufferStatus)
2101
    -- a frame needs to be retransmitted due to buffering =>
2102
    -- packet-retry(rxAckId, bufferStatus)
2103
    -- a frame is rejected due to errors => packet-not-accepted
2104
    -- a link-request should be answered => link-response
2105
    rxControlWrite_o : out std_logic;
2106
    rxControlSymbol_o : out std_logic_vector(12 downto 0);
2107
 
2108
    -- Status signals used internally.
2109
    ackIdStatus_o : out std_logic_vector(4 downto 0);
2110
    linkInitialized_o : out std_logic;
2111
 
2112 11 magro732
    -- Core->Core cascading signals.
2113
    enable_o : out std_logic;
2114
    operational_i : in std_logic;
2115
    operational_o : out std_logic;
2116
    inputRetryStopped_i : in std_logic;
2117
    inputRetryStopped_o : out std_logic;
2118
    inputErrorStopped_i : in std_logic;
2119
    inputErrorStopped_o : out std_logic;
2120
    ackId_i : in unsigned(4 downto 0);
2121
    ackId_o : out unsigned(4 downto 0);
2122
    frameIndex_i : in std_logic_vector(6 downto 0);
2123
    frameIndex_o : out std_logic_vector(6 downto 0);
2124
    frameWordCounter_i : in std_logic_vector(1 downto 0);
2125
    frameWordCounter_o : out std_logic_vector(1 downto 0);
2126
    frameContent_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
2127
    frameContent_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
2128
    crc_i : in std_logic_vector(15 downto 0);
2129
    crc_o : out std_logic_vector(15 downto 0);
2130
 
2131 4 magro732
    -- Frame buffering interface.
2132
    writeFrameFull_i : in std_logic;
2133
    writeFrame_o : out std_logic;
2134
    writeFrameAbort_o : out std_logic;
2135
    writeContent_o : out std_logic;
2136 11 magro732
    writeContentWords_o : out std_logic_vector(1 downto 0);
2137
    writeContentData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0));
2138 4 magro732
end entity;
2139
 
2140
 
2141
-------------------------------------------------------------------------------
2142
-- 
2143
-------------------------------------------------------------------------------
2144 11 magro732
architecture RioReceiverCoreImpl of RioReceiverCore is
2145 4 magro732
 
2146
  component Crc5ITU is
2147
    port(
2148
      d_i : in  std_logic_vector(18 downto 0);
2149
      crc_o : out std_logic_vector(4 downto 0));
2150
  end component;
2151
 
2152
  component Crc16CITT is
2153
    port(
2154
      d_i : in  std_logic_vector(15 downto 0);
2155
      crc_i : in  std_logic_vector(15 downto 0);
2156
      crc_o : out std_logic_vector(15 downto 0));
2157
  end component;
2158
 
2159 11 magro732
  signal symbolEnable0, symbolEnable1 : std_logic;
2160
  signal symbolType0 : std_logic_vector(1 downto 0);
2161
  signal symbolContent0, symbolContent1 : std_logic_vector(31 downto 0);
2162
  signal crc5Valid : std_logic;
2163 4 magro732
  signal crc5 : std_logic_vector(4 downto 0);
2164
 
2165 11 magro732
  signal symbolValid : std_logic;
2166
  signal stype0Status : std_logic;
2167
  signal stype1Start : std_logic;
2168
  signal stype1End : std_logic;
2169
  signal stype1Stomp : std_logic;
2170
  signal stype1Restart : std_logic;
2171
  signal stype1LinkRequest : std_logic;
2172
  signal symbolData : std_logic;
2173
 
2174 4 magro732
  signal crc16Data : std_logic_vector(31 downto 0);
2175
  signal crc16Current : std_logic_vector(15 downto 0);
2176
  signal crc16Temp : std_logic_vector(15 downto 0);
2177
  signal crc16Next : std_logic_vector(15 downto 0);
2178 11 magro732
  signal crc16Valid : std_logic;
2179
 
2180
  signal frameContent : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
2181
 
2182
  signal rxControlWrite : std_logic;
2183
  signal rxControlSymbol : std_logic_vector(12 downto 0);
2184 4 magro732
 
2185
begin
2186
 
2187 11 magro732
  linkInitialized_o <= operational_i;
2188
  ackIdStatus_o <= std_logic_vector(ackId_i);
2189
  inboundAckId_o <= std_logic_vector(ackId_i);
2190
 
2191 4 magro732
  -----------------------------------------------------------------------------
2192 11 magro732
  -- First pipeline stage.
2193
  -- Check the validity of the symbol, CRC5 on control symbols, and save the
2194
  -- symbol content for the next stage.
2195 4 magro732
  -----------------------------------------------------------------------------
2196
 
2197 11 magro732
  -- Read the fifo immediatly.
2198
  rxRead_o <= not rxEmpty_i;
2199 4 magro732
 
2200 11 magro732
  Crc5Calculator: Crc5ITU
2201
    port map(
2202
      d_i=>rxData_i(31 downto 13), crc_o=>crc5);
2203
 
2204
  process(clk, areset_n)
2205
  begin
2206
    if (areset_n = '0') then
2207
      crc5Valid <= '0';
2208
      symbolType0 <= (others => '0');
2209
      symbolContent0 <= (others => '0');
2210
    elsif (clk'event and clk = '1') then
2211
      if (rxEmpty_i = '0') then
2212
        if (crc5 = rxData_i(12 downto 8)) then
2213
          crc5Valid <= '1';
2214
        else
2215
          crc5Valid <= '0';
2216
        end if;
2217
        symbolEnable0 <= '1';
2218
        symbolType0 <= rxType_i;
2219
        symbolContent0 <= rxData_i;
2220
      else
2221
        symbolEnable0 <= '0';
2222
      end if;
2223
    end if;
2224
  end process;
2225
 
2226 4 magro732
  -----------------------------------------------------------------------------
2227 11 magro732
  -- Second pipeline stage.
2228
  -- Separate the part of the control symbol that are going to the transmitter
2229
  -- side and check the type of symbol for this side. 
2230 4 magro732
  -----------------------------------------------------------------------------
2231
 
2232 11 magro732
  process(clk, areset_n)
2233
  begin
2234
    if (areset_n = '0') then
2235
      txControlWrite_o <= '0';
2236
      txControlSymbol_o <= (others => '0');
2237 4 magro732
 
2238 11 magro732
      symbolValid <= '0';
2239
      stype0Status <= '0';
2240
      stype1Start <= '0';
2241
      stype1End <= '0';
2242
      stype1Stomp <= '0';
2243
      stype1Restart <= '0';
2244
      stype1LinkRequest <= '0';
2245
      symbolData <= '0';
2246
 
2247
      symbolContent1 <= (others => '0');
2248
    elsif (clk'event and clk = '1') then
2249
      if (symbolEnable0 = '1') then
2250
        symbolEnable1 <= '1';
2251
        symbolContent1 <= symbolContent0;
2252
 
2253
        if (symbolType0 = SYMBOL_CONTROL) then
2254
          if (crc5Valid = '1') then
2255
            -- REMARK: Check if stype0 is nop and dont forward it???
2256
            symbolValid <= '1';
2257
            txControlWrite_o <= '1';
2258
            txControlSymbol_o <= symbolContent0(31 downto 19);
2259
          else
2260
            symbolValid <= '0';
2261
            txControlWrite_o <= '0';
2262
            txControlSymbol_o <= (others => '0');
2263
          end if;
2264
        else
2265
          symbolValid <= '1';
2266
        end if;
2267
 
2268
        if ((symbolType0 = SYMBOL_CONTROL) and
2269
            (symbolContent0(31 downto 29) = STYPE0_STATUS)) then
2270
          stype0Status <= '1';
2271
        else
2272
          stype0Status <= '0';
2273
        end if;
2274
        if ((symbolType0 = SYMBOL_CONTROL) and
2275
            (symbolContent0(18 downto 16) = STYPE1_START_OF_PACKET)) then
2276
          stype1Start <= '1';
2277
        else
2278
          stype1Start <= '0';
2279
        end if;
2280
        if ((symbolType0 = SYMBOL_CONTROL) and
2281
            (symbolContent0(18 downto 16) = STYPE1_END_OF_PACKET)) then
2282
          stype1End <= '1';
2283
        else
2284
          stype1End <= '0';
2285
        end if;
2286
        if ((symbolType0 = SYMBOL_CONTROL) and
2287
            (symbolContent0(18 downto 16) = STYPE1_STOMP)) then
2288
          stype1Stomp <= '1';
2289
        else
2290
          stype1Stomp <= '0';
2291
        end if;
2292
        if ((symbolType0 = SYMBOL_CONTROL) and
2293
            (symbolContent0(18 downto 16) = STYPE1_RESTART_FROM_RETRY)) then
2294
          stype1Restart <= '1';
2295
        else
2296
          stype1Restart <= '0';
2297
        end if;
2298
        if ((symbolType0 = SYMBOL_CONTROL) and
2299
            (symbolContent0(18 downto 16) = STYPE1_LINK_REQUEST)) then
2300
          stype1LinkRequest <= '1';
2301
        else
2302
          stype1LinkRequest <= '0';
2303
        end if;
2304
        if (symbolType0 = SYMBOL_DATA) then
2305
          symbolData <= '1';
2306
        else
2307
          symbolData <= '0';
2308
        end if;
2309
      else
2310
        symbolEnable1 <= '0';
2311
      end if;
2312
    end if;
2313
  end process;
2314
 
2315 4 magro732
  -----------------------------------------------------------------------------
2316 11 magro732
  -- Third pipeline stage.
2317
  -- Update the CRC16 for the packet.
2318
  -- Update the buffered data and write it to the packet buffer if needed.
2319
  -- Update the main receiver state machine.
2320
  -- Generate reply symbols to the link-partner.
2321
  -- Note that this stage cannot contain any registers as it could be cascaded
2322
  -- to other cores.
2323 4 magro732
  -----------------------------------------------------------------------------
2324 11 magro732
 
2325
  -----------------------------------------------------------------------------
2326
  -- CRC-calculation.
2327
  -- Add a data symbol to the calculated CRC for the packet.
2328
  -- controlSymbol->just forward old crc16.
2329
  -- first data-symbol in packet->crc_o is product of 11111 and the
2330
  -- symbolContent1 without ackid.
2331
  -- not first data-symbol->crc_o is product of crc_i and symbolContent1.
2332
  -----------------------------------------------------------------------------
2333 4 magro732
 
2334 11 magro732
  crc16Data(31 downto 26) <= "000000" when (unsigned(frameIndex_i) = 1) else
2335
                             symbolContent1(31 downto 26);
2336
  crc16Data(25 downto 0) <= symbolContent1(25 downto 0);
2337 4 magro732
 
2338 11 magro732
  crc16Current <= crc_i when (unsigned(frameIndex_i) /= 1) else
2339
                  (others => '1');
2340
 
2341
  Crc16Msb: Crc16CITT
2342 4 magro732
    port map(
2343
      d_i=>crc16Data(31 downto 16), crc_i=>crc16Current, crc_o=>crc16Temp);
2344 11 magro732
  Crc16Lsb: Crc16CITT
2345 4 magro732
    port map(
2346
      d_i=>crc16Data(15 downto 0), crc_i=>crc16Temp, crc_o=>crc16Next);
2347
 
2348 11 magro732
  crc_o <= crc_i when (symbolData = '0') else
2349
           crc16Next;
2350
 
2351
  crc16Valid <= '1' when (crc_i = x"0000") else '0';
2352
 
2353 4 magro732
  -----------------------------------------------------------------------------
2354 11 magro732
  -- Update buffered data.
2355 4 magro732
  -----------------------------------------------------------------------------
2356
 
2357 11 magro732
  -- Append the new symbol content to the end of the
2358
  -- current frame content if the symbol is a data symbol.
2359
  frameContentSingle:
2360
  if (NUMBER_WORDS = 1) generate
2361
    frameContent <= symbolContent1;
2362
  end generate;
2363
  frameContentMulti:
2364
  if (NUMBER_WORDS > 1) generate
2365
    frameContent <=
2366
      (frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & symbolContent1) when (symbolData = '1') else
2367
      frameContent_i;
2368
  end generate;
2369 4 magro732
 
2370 11 magro732
  -- Update outputs.
2371
  enable_o <= symbolEnable1;
2372
  frameContent_o <= frameContent;
2373
  writeContentData_o <= frameContent;
2374 4 magro732
 
2375 11 magro732
  -----------------------------------------------------------------------------
2376
  -- Main inbound symbol handler.
2377
  -----------------------------------------------------------------------------
2378
  process(portInitialized_i, portEnable_i, writeFrameFull_i,
2379
          operational_i, ackId_i, frameIndex_i, frameWordCounter_i,
2380
          inputRetryStopped_i, inputErrorStopped_i,
2381
          symbolValid,
2382
          stype0Status,
2383
          stype1Start, stype1End, stype1Stomp, stype1Restart, stype1LinkRequest,
2384
          symbolData,
2385
          symbolContent1,
2386
          frameContent,
2387
          crc16Valid)
2388
  begin
2389
    operational_o <= operational_i;
2390
    ackId_o <= ackId_i;
2391
    frameIndex_o <= frameIndex_i;
2392
    frameWordCounter_o <= frameWordCounter_i;
2393
    inputRetryStopped_o <= inputRetryStopped_i;
2394
    inputErrorStopped_o <= inputErrorStopped_i;
2395
 
2396
    rxControlWrite <= '0';
2397
    rxControlSymbol <= (others => '0');
2398
 
2399
    writeFrame_o <= '0';
2400
    writeFrameAbort_o <= '0';
2401
    writeContent_o <= '0';
2402
    writeContentWords_o <= (others => '0');
2403 4 magro732
 
2404 11 magro732
    -- Act on the current state.
2405
    if (operational_i = '0') then
2406
      ---------------------------------------------------------------------
2407
      -- The port is not operational and is waiting for status control
2408
      -- symbols to be received on the link. Count the number
2409
      -- of error-free status symbols and considder the link operational
2410
      -- when enough of them has been received. Frames are not allowed
2411
      -- here.
2412
      ---------------------------------------------------------------------
2413
 
2414
      -- Check if the port is initialized.
2415
      if (portInitialized_i = '1') then
2416
        -- Port is initialized.
2417
 
2418
        -- Check if the control symbol has a valid checksum.
2419
        if (symbolValid = '1') then
2420
          -- The control symbol has a valid checksum.
2421 4 magro732
 
2422 11 magro732
          -- Check the stype0 part if we should count the number of
2423
          -- error-free status symbols.
2424
          if (stype0Status = '1') then
2425
            -- The symbol is a status.
2426 4 magro732
 
2427 11 magro732
            -- Check if enough status symbols have been received.
2428
            if (unsigned(frameIndex_i) = 7) then
2429
              -- Enough status symbols have been received.
2430 4 magro732
 
2431 11 magro732
              -- Reset all packets.
2432
              frameIndex_o <= (others => '0');
2433
              writeFrameAbort_o <= '1';
2434
 
2435
              -- Set the link as initialized.
2436
              operational_o <= '1';
2437 4 magro732
            else
2438 11 magro732
              -- Increase the number of error-free status symbols that
2439
              -- has been received.
2440
              frameIndex_o <= std_logic_vector(unsigned(frameIndex_i) + 1);
2441 4 magro732
            end if;
2442 11 magro732
          else
2443
            -- The symbol is not a status.
2444
            -- Dont do anything.
2445
          end if;
2446
        else
2447
          -- A control symbol with CRC5 error was recevied.
2448
          frameIndex_o <= (others => '0');
2449
        end if;
2450
      else
2451
        -- The port has become uninitialized.
2452
        frameIndex_o <= (others => '0');
2453
      end if;
2454
    else
2455
      ---------------------------------------------------------------------
2456
      -- The port has been initialized and enough error free status symbols
2457
      -- have been received. Forward data frames to the frame buffer
2458
      -- interface. This is the normal operational state.
2459
      ---------------------------------------------------------------------
2460
 
2461
      -- Check that the port is initialized.
2462
      if (portInitialized_i = '1') then
2463
        -- The port and link is initialized.
2464
 
2465
        -- Check if the control symbol has a valid CRC-5.
2466
        if (symbolValid = '1') then
2467
          -- The symbol is correct.
2468 4 magro732
 
2469 11 magro732
          if ((stype1Start = '1') and
2470
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2471
            -------------------------------------------------------------
2472
            -- Start the reception of a new frame or end a currently
2473
            -- ongoing frame and start a new one.
2474
            -------------------------------------------------------------
2475 4 magro732
 
2476 11 magro732
            -- Check if a frame has already been started.
2477
            if (unsigned(frameIndex_i) /= 0) then
2478
              -- A frame is already started.
2479
              -- Complete the last frame and start to ackumulate a new one
2480
              -- and update the ackId.
2481 4 magro732
 
2482 11 magro732
              if (unsigned(frameIndex_i) > 3) then
2483 4 magro732
 
2484 11 magro732
                -- Reset the frame index to indicate the frame is started.
2485
                frameIndex_o <= "0000001";
2486
                frameWordCounter_o <= (others=>'0');
2487
 
2488
                -- Check the CRC-16 and the length of the received frame.
2489
                if (crc16Valid = '1') then
2490
                  -- The CRC-16 is ok.
2491
 
2492
                  -- Check if there are any unwritten buffered packet content
2493
                  -- and write it if there is.
2494
                  -- REMARK: Multi-symbol support...
2495
                  if (unsigned(frameWordCounter_i) /= 0) then
2496
                    writeContent_o <= '1';
2497
                  end if;
2498 4 magro732
 
2499 11 magro732
                  -- Update the frame buffer to indicate that the frame has
2500
                  -- been completly received.
2501
                  writeFrame_o <= '1';
2502 4 magro732
 
2503 11 magro732
                  -- Update ackId.
2504
                  ackId_o <= ackId_i + 1;
2505 4 magro732
 
2506 11 magro732
                  -- Send packet-accepted.
2507
                  -- The buffer status is appended by the transmitter
2508
                  -- when sent to get the latest number.
2509
                  rxControlWrite <= '1';
2510
                  rxControlSymbol <= STYPE0_PACKET_ACCEPTED &
2511
                                     std_logic_vector(ackId_i) &
2512
                                     "11111";
2513
                else
2514
                  -- The CRC-16 is not ok.
2515 4 magro732
 
2516 11 magro732
                  -- Make the transmitter send a packet-not-accepted to indicate
2517
                  -- that the received packet contained a CRC error.
2518
                  rxControlWrite <= '1';
2519
                  rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2520
                                     "00000" &
2521
                                     PACKET_NOT_ACCEPTED_CAUSE_PACKET_CRC;
2522
                  inputErrorStopped_o <= '1';
2523 4 magro732
                end if;
2524
              else
2525 11 magro732
                -- This packet is too small.
2526
                -- Make the transmitter send a packet-not-accepted to indicated
2527
                -- that the received packet was too small.
2528
                rxControlWrite <= '1';
2529
                rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2530
                                   "00000" &
2531
                                   PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2532
                inputErrorStopped_o <= '1';
2533 4 magro732
              end if;
2534
            else
2535 11 magro732
              -- No frame has been started.
2536
 
2537
              -- Reset the frame index to indicate the frame is started.
2538
              frameIndex_o <= "0000001";
2539
              frameWordCounter_o <= (others=>'0');
2540 4 magro732
            end if;
2541 11 magro732
          end if;
2542
 
2543
          if ((stype1End = '1') and
2544
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2545
            -------------------------------------------------------------
2546
            -- End the reception of an old frame.
2547
            -------------------------------------------------------------
2548 4 magro732
 
2549 11 magro732
            -- Check if a frame has already been started.
2550
            if (unsigned(frameIndex_i) > 3) then
2551
              -- A frame has been started and it is large enough.
2552 4 magro732
 
2553 11 magro732
              -- Reset frame reception to indicate that no frame is ongoing.
2554
              frameIndex_o <= (others => '0');
2555
 
2556
              -- Check the CRC-16 and the length of the received frame.
2557
              if (crc16Valid = '1') then
2558
                -- The CRC-16 is ok.
2559 4 magro732
 
2560 11 magro732
                -- Check if there are any unwritten buffered packet content
2561
                -- and write it if there is.
2562
                -- REMARK: Multi-symbol support...
2563
                if (unsigned(frameWordCounter_i) /= 0) then
2564
                  writeContent_o <= '1';
2565
                end if;
2566 4 magro732
 
2567 11 magro732
                -- Update the frame buffer to indicate that the frame has
2568
                -- been completly received.
2569
                writeFrame_o <= '1';
2570 4 magro732
 
2571 11 magro732
                -- Update ackId.
2572
                ackId_o <= ackId_i + 1;
2573 4 magro732
 
2574 11 magro732
                -- Send packet-accepted.
2575
                -- The buffer status is appended by the transmitter
2576
                -- when sent to get the latest number.
2577
                rxControlWrite <= '1';
2578
                rxControlSymbol <= STYPE0_PACKET_ACCEPTED &
2579
                                   std_logic_vector(ackId_i) &
2580
                                   "11111";
2581
              else
2582
                -- The CRC-16 is not ok.
2583 4 magro732
 
2584 11 magro732
                -- Make the transmitter send a packet-not-accepted to indicate
2585
                -- that the received packet contained a CRC error.
2586
                rxControlWrite <= '1';
2587
                rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2588
                                   "00000" &
2589
                                   PACKET_NOT_ACCEPTED_CAUSE_PACKET_CRC;
2590
                inputErrorStopped_o <= '1';
2591 4 magro732
              end if;
2592
            else
2593 11 magro732
              -- This packet is too small.
2594
              -- Make the transmitter send a packet-not-accepted to indicate
2595
              -- that the received packet was too small.
2596
              rxControlWrite <= '1';
2597
              rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2598
                                 "00000" &
2599
                                 PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2600
              inputErrorStopped_o <= '1';
2601 4 magro732
            end if;
2602 11 magro732
          end if;
2603 4 magro732
 
2604 11 magro732
          if ((stype1Stomp = '1') and
2605
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2606
            -------------------------------------------------------------
2607
            -- Restart the reception of an old frame.
2608
            -------------------------------------------------------------
2609
            -- See 5.10 in the standard.
2610 4 magro732
 
2611 11 magro732
            -- Make the transmitter send a packet-retry to indicate
2612
            -- that the packet cannot be accepted.
2613
            rxControlWrite <= '1';
2614
            rxControlSymbol <= STYPE0_PACKET_RETRY &
2615
                               std_logic_vector(ackId_i) &
2616
                               "11111";
2617
 
2618
            -- Enter the input retry-stopped state.
2619
            inputRetryStopped_o <= '1';
2620
          end if;
2621 4 magro732
 
2622 11 magro732
          if (stype1Restart = '1') then
2623
            if (inputRetryStopped_i = '1') then
2624
              -------------------------------------------------------------
2625
              -- The receiver indicates a restart from a retry sent
2626
              -- from us.
2627
              -------------------------------------------------------------
2628 4 magro732
 
2629 11 magro732
              -- Abort the frame and reset frame reception.
2630
              frameIndex_o <= (others => '0');
2631
              writeFrameAbort_o <= '1';
2632
 
2633
              -- Go back to the normal operational state.
2634
              inputRetryStopped_o <= '0';
2635
            else
2636
              -------------------------------------------------------------
2637
              -- The receiver indicates a restart from a retry sent
2638
              -- from us.
2639
              -------------------------------------------------------------
2640
              -- See 5.10 in the standard.
2641
              -- Protocol error, this symbol should not be received here since
2642
              -- we should have been in input-retry-stopped. 
2643
 
2644
              -- Send a packet-not-accepted to indicate that a protocol
2645
              -- error has occurred.
2646
              rxControlWrite <= '1';
2647
              rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2648
                                 "00000" &
2649
                                 PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2650
              inputErrorStopped_o <= '1';
2651
            end if;
2652
          end if;
2653 4 magro732
 
2654 11 magro732
          if (stype1LinkRequest = '1') then
2655
            -------------------------------------------------------------
2656
            -- Reply to a LINK-REQUEST.
2657
            -------------------------------------------------------------
2658
 
2659
            -- Check the command part.
2660
            if (symbolContent1(15 downto 13) = "100") then
2661
              -- Return input port status command.
2662
              -- This functions as a link-request(restart-from-error)
2663
              -- control symbol under error situations.
2664 4 magro732
 
2665 11 magro732
              if (inputErrorStopped_i = '1') then
2666
                rxControlWrite <= '1';
2667
                rxControlSymbol <= STYPE0_LINK_RESPONSE &
2668
                                   std_logic_vector(ackId_i) &
2669
                                   "00101";
2670
              elsif (inputRetryStopped_i = '1') then
2671
                rxControlWrite <= '1';
2672
                rxControlSymbol <= STYPE0_LINK_RESPONSE &
2673
                                   std_logic_vector(ackId_i) &
2674
                                   "00100";
2675 4 magro732
              else
2676 11 magro732
                -- Send a link response containing an ok reply.
2677
                rxControlWrite <= '1';
2678
                rxControlSymbol <= STYPE0_LINK_RESPONSE &
2679
                                   std_logic_vector(ackId_i) &
2680
                                   "10000";
2681 4 magro732
              end if;
2682
            else
2683 11 magro732
              -- Reset device command or other unsupported command.
2684
              -- Discard this.
2685 4 magro732
            end if;
2686
 
2687 11 magro732
            -- Abort the frame and reset frame reception.
2688
            inputRetryStopped_o <= '0';
2689
            inputErrorStopped_o <= '0';
2690
            frameIndex_o <= (others=>'0');
2691
            writeFrameAbort_o <= '1';
2692
          end if;
2693 4 magro732
 
2694 11 magro732
          if ((symbolData = '1')  and
2695
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2696
            -------------------------------------------------------------
2697
            -- This is a data symbol.
2698
            -------------------------------------------------------------
2699
            -- REMARK: Add check for in-the-middle-crc here...
2700 4 magro732
 
2701 11 magro732
            case frameIndex_i is
2702
              when "0000000" | "1000110" =>
2703
                -- A frame has not been started or is too long.
2704
                -- Send packet-not-accepted.
2705
                rxControlWrite <= '1';
2706
                rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2707
                                   "00000" &
2708
                                   PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2709
                inputErrorStopped_o <= '1';
2710
              when "0000001" =>
2711
                if (unsigned(symbolContent1(31 downto 27)) = ackId_i) then
2712
                  if ((portEnable_i = '1') or
2713
                      (symbolContent1(19 downto 16) = FTYPE_MAINTENANCE_CLASS)) then
2714
 
2715
                    -- Check if there are buffers available to store the new
2716
                    -- packet.
2717
                    if (writeFrameFull_i = '0') then
2718
                      -- There are buffering space available to store the new
2719
                      -- data.
2720
 
2721
                      -- Check if the buffer entry is ready to be written
2722
                      -- into the packet buffer.
2723
                      -- REMARK: Multi-symbol support...
2724
                      if (unsigned(frameWordCounter_i) = (NUMBER_WORDS-1)) then
2725
                        -- Write the data to the frame FIFO.
2726
                        frameWordCounter_o <= (others=>'0');
2727
                        writeContent_o <= '1';
2728
                        writeContentWords_o <= frameWordCounter_i;
2729
                      else
2730
                        frameWordCounter_o <= std_logic_vector(unsigned(frameWordCounter_i) + 1);
2731
                      end if;
2732 4 magro732
 
2733 11 magro732
                      -- Increment the number of received data symbols.
2734
                      frameIndex_o <= std_logic_vector(unsigned(frameIndex_i) + 1);
2735
                    else
2736
                      -- The packet buffer is full.
2737
                      -- Let the link-partner resend the packet.
2738
                      rxControlWrite <= '1';
2739
                      rxControlSymbol <= STYPE0_PACKET_RETRY &
2740
                                         std_logic_vector(ackId_i) &
2741
                                         "11111";
2742
                      inputRetryStopped_o <= '1';
2743
                    end if;
2744 4 magro732
                  else
2745 11 magro732
                    -- A non-maintenance packets are not allowed.
2746
                    -- Send packet-not-accepted.
2747
                    rxControlWrite <= '1';
2748
                    rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2749
                                       "00000" &
2750
                                       PACKET_NOT_ACCEPTED_CAUSE_NON_MAINTENANCE_STOPPED;
2751
                    inputErrorStopped_o <= '1';
2752 4 magro732
                  end if;
2753
                else
2754 11 magro732
                  -- The ackId is unexpected.
2755
                  -- Send packet-not-accepted.
2756
                  rxControlWrite <= '1';
2757
                  rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2758
                                     "00000" &
2759
                                     PACKET_NOT_ACCEPTED_CAUSE_UNEXPECTED_ACKID;
2760
                  inputErrorStopped_o <= '1';
2761 4 magro732
                end if;
2762 11 magro732
              when others =>
2763
                -- A frame has been started and is not too long.
2764
                -- Check if the buffer entry is ready to be written
2765
                -- into the packet buffer.
2766
                -- REMARK: Multi-symbol support...
2767
                if (unsigned(frameWordCounter_i) = (NUMBER_WORDS-1)) then
2768
                  -- Write the data to the frame FIFO.
2769
                  frameWordCounter_o <= (others=>'0');
2770
                  writeContent_o <= '1';
2771
                  writeContentWords_o <= frameWordCounter_i;
2772
                else
2773
                  frameWordCounter_o <= std_logic_vector(unsigned(frameWordCounter_i) + 1);
2774
                end if;
2775 4 magro732
 
2776 11 magro732
                -- Increment the number of received data symbols.
2777
                frameIndex_o <= std_logic_vector(unsigned(frameIndex_i) + 1);
2778
            end case;
2779
          end if;
2780
        else
2781
          -- A control symbol contains a crc error.
2782 4 magro732
 
2783 11 magro732
          -- Send a packet-not-accepted to indicate that a corrupted
2784
          -- control-symbol has been received and change state.
2785
          rxControlWrite <= '1';
2786
          rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2787
                             "00000" &
2788
                             PACKET_NOT_ACCEPTED_CAUSE_CONTROL_CRC;
2789
          inputErrorStopped_o <= '1';
2790
        end if;
2791
      else
2792
        -- The port has become uninitialized.
2793
        -- Go back to the uninitialized state.
2794
        operational_o <= '0';
2795 4 magro732
      end if;
2796
    end if;
2797
  end process;
2798
 
2799 11 magro732
  -----------------------------------------------------------------------------
2800
  -- Fourth pipeline stage.
2801
  -----------------------------------------------------------------------------
2802
  -- REMARK: Do more stuff in this stage, convert a one-hot to the symbol to
2803
  -- send...
2804
  -- REMARK: Register other outputs here like writeContent_o...
2805
 
2806
  process(clk, areset_n)
2807
  begin
2808
    if (areset_n = '0') then
2809
      rxControlWrite_o <= '0';
2810
      rxControlSymbol_o <= (others=>'0');
2811
    elsif (clk'event and clk = '1') then
2812
      rxControlWrite_o <= rxControlWrite and symbolEnable1;
2813
      rxControlSymbol_o <= rxControlSymbol;
2814
    end if;
2815
  end process;
2816
 
2817 4 magro732
end architecture;
2818
 
2819
 
2820
 
2821
-------------------------------------------------------------------------------
2822
--
2823
---------------------------------------------------------------------------------
2824
library ieee;
2825
use ieee.std_logic_1164.all;
2826
 
2827
 
2828
-------------------------------------------------------------------------------
2829
-- 
2830
-------------------------------------------------------------------------------
2831
entity RioFifo1 is
2832
  generic(
2833
    WIDTH : natural);
2834
  port(
2835
    clk : in std_logic;
2836
    areset_n : in std_logic;
2837
 
2838
    empty_o : out std_logic;
2839
    read_i : in std_logic;
2840
    data_o : out std_logic_vector(WIDTH-1 downto 0);
2841
 
2842
    full_o : out std_logic;
2843
    write_i : in std_logic;
2844
    data_i : in std_logic_vector(WIDTH-1 downto 0));
2845
end entity;
2846
 
2847
 
2848
-------------------------------------------------------------------------------
2849
-- 
2850
-------------------------------------------------------------------------------
2851
architecture RioFifo1Impl of RioFifo1 is
2852
  signal empty : std_logic;
2853
  signal full : std_logic;
2854
begin
2855
 
2856
  empty_o <= empty;
2857
  full_o <= full;
2858
 
2859
  process(areset_n, clk)
2860
  begin
2861
    if (areset_n = '0') then
2862
      empty <= '1';
2863
      full <= '0';
2864
      data_o <= (others => '0');
2865
    elsif (clk'event and clk = '1') then
2866
      if (empty = '1') then
2867
        if (write_i = '1') then
2868
          empty <= '0';
2869
          full <= '1';
2870
          data_o <= data_i;
2871
        end if;
2872
      else
2873
        if (read_i = '1') then
2874
          empty <= '1';
2875
          full <= '0';
2876
        end if;
2877
      end if;
2878
    end if;
2879
  end process;
2880
 
2881
end architecture;
2882
 
2883
 
2884
 
2885
-------------------------------------------------------------------------------
2886
-- A CRC-5 calculator following the implementation proposed in the 2.2
2887
-- standard.
2888
-------------------------------------------------------------------------------
2889
library ieee;
2890
use ieee.std_logic_1164.all;
2891
 
2892
 
2893
-------------------------------------------------------------------------------
2894
-- 
2895
-------------------------------------------------------------------------------
2896
entity Crc5ITU is
2897
  port(
2898
    d_i : in  std_logic_vector(18 downto 0);
2899
    crc_o : out std_logic_vector(4 downto 0));
2900
end entity;
2901
 
2902
 
2903
-------------------------------------------------------------------------------
2904
-- 
2905
-------------------------------------------------------------------------------
2906
architecture Crc5Impl of Crc5ITU is
2907
  signal d : std_logic_vector(0 to 18);
2908
  signal c : std_logic_vector(0 to 4);
2909
 
2910
begin
2911
  -- Reverse the bit vector indexes to make them the same as in the standard.
2912
  d(18) <= d_i(0); d(17) <= d_i(1); d(16) <= d_i(2); d(15) <= d_i(3);
2913
  d(14) <= d_i(4); d(13) <= d_i(5); d(12) <= d_i(6); d(11) <= d_i(7);
2914
  d(10) <= d_i(8); d(9) <= d_i(9); d(8) <= d_i(10); d(7) <= d_i(11);
2915
  d(6) <= d_i(12); d(5) <= d_i(13); d(4) <= d_i(14); d(3) <= d_i(15);
2916
  d(2) <= d_i(16); d(1) <= d_i(17); d(0) <= d_i(18);
2917
 
2918
  -- Calculate the resulting crc.
2919
  c(0) <= d(18) xor d(16) xor d(15) xor d(12) xor
2920
          d(10) xor d(5) xor d(4) xor d(3) xor
2921
          d(1) xor d(0);
2922
  c(1) <= (not d(18)) xor d(17) xor d(15) xor d(13) xor
2923
          d(12) xor d(11) xor d(10) xor d(6) xor
2924
          d(3) xor d(2) xor d(0);
2925
  c(2) <= (not d(18)) xor d(16) xor d(14) xor d(13) xor
2926
          d(12) xor d(11) xor d(7) xor d(4) xor
2927
          d(3) xor d(1);
2928
  c(3) <= (not d(18)) xor d(17) xor d(16) xor d(14) xor
2929
          d(13) xor d(10) xor d(8) xor d(3) xor
2930
          d(2) xor d(1);
2931
  c(4) <= d(18) xor d(17) xor d(15) xor d(14) xor
2932
          d(11) xor d(9) xor d(4) xor d(3) xor
2933
          d(2) xor d(0);
2934
 
2935
  -- Reverse the bit vector indexes to make them the same as in the standard.
2936
  crc_o(4) <= c(0); crc_o(3) <= c(1); crc_o(2) <= c(2); crc_o(1) <= c(3);
2937
  crc_o(0) <= c(4);
2938
end architecture;
2939 11 magro732
 
2940
 
2941
-------------------------------------------------------------------------------
2942
-- 
2943
-------------------------------------------------------------------------------
2944
library ieee;
2945
use ieee.std_logic_1164.all;
2946
use ieee.numeric_std.all;
2947
use work.rio_common.all;
2948
 
2949
-------------------------------------------------------------------------------
2950
-- 
2951
-------------------------------------------------------------------------------
2952
entity Crc16Calculator is
2953
 
2954
  generic (
2955
    NUMBER_WORDS : natural range 1 to 8 := 1);
2956
 
2957
  port (
2958
    clk : in std_logic;
2959
    areset_n : in std_logic;
2960
 
2961
    write_i : in std_logic;
2962
    crc_i : in std_logic_vector(15 downto 0);
2963
    data_i : in std_logic_vector((32*NUMBER_WORDS)-1 downto 0);
2964
 
2965
    crc_o : out std_logic_vector(15 downto 0);
2966
    done_o : out std_logic);
2967
 
2968
end Crc16Calculator;
2969
 
2970
-------------------------------------------------------------------------------
2971
-- 
2972
-------------------------------------------------------------------------------
2973
architecture Crc16CalculatorImpl of Crc16Calculator is
2974
 
2975
  component Crc16CITT is
2976
    port(
2977
      d_i : in  std_logic_vector(15 downto 0);
2978
      crc_i : in  std_logic_vector(15 downto 0);
2979
      crc_o : out std_logic_vector(15 downto 0));
2980
  end component;
2981
 
2982
  signal iterator : natural range 0 to 2*NUMBER_WORDS;
2983
  signal crcData : std_logic_vector((32*NUMBER_WORDS)-1 downto 0);
2984
  signal crcCurrent : std_logic_vector(15 downto 0);
2985
  signal crcNext : std_logic_vector(15 downto 0);
2986
 
2987
begin
2988
 
2989
  process(areset_n, clk)
2990
  begin
2991
    if (areset_n = '0') then
2992
      iterator <= 0;
2993
      done_o <= '0';
2994
      crc_o <= (others => '0');
2995
    elsif (clk'event and clk = '1') then
2996
      if (write_i = '1') then
2997
        iterator <= 2*NUMBER_WORDS-1;
2998
        crcData <= data_i;
2999
        crcCurrent <= crc_i;
3000
        done_o <= '0';
3001
      else
3002
        if (iterator /= 0) then
3003
          iterator <= iterator - 1;
3004
          crcData <= crcData(((32*NUMBER_WORDS)-1)-16 downto 0) & x"0000";
3005
          crcCurrent <= crcNext;
3006
        else
3007
          crc_o <= crcNext;
3008
          done_o <= '1';
3009
        end if;
3010
      end if;
3011
    end if;
3012
  end process;
3013
 
3014
  Crc16Inst: Crc16CITT
3015
    port map(
3016
      d_i=>crcData((32*NUMBER_WORDS)-1 downto (32*NUMBER_WORDS)-16),
3017
      crc_i=>crcCurrent, crc_o=>crcNext);
3018
 
3019
end architecture;
3020
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.