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

Subversion Repositories rio

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

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 15 magro732
                    ((unsigned(ackIdWindow_i)+1) /= unsigned(ackId_i))) then
1392 14 magro732
                  -- 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 15 magro732
 
1432 13 magro732
              if (unsigned(frameWordCounter_i) /= 0) then
1433
                frameWordCounter_o <=
1434
                  std_logic_vector(unsigned(frameWordCounter_i) - 1);
1435
                frameContent_o <=
1436
                  frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & x"00000000";
1437
              else
1438
                frameWordCounter_o <= readContentWords_i;
1439
                frameContent_o <= readContentData_i;
1440
                readContentOut <= '1';
1441
              end if;
1442 15 magro732
 
1443 13 magro732
              -- Continue to send the rest of the body of the packet.
1444 14 magro732
              frameState_o <= FRAME_MIDDLE;
1445 13 magro732
 
1446 14 magro732
            when FRAME_MIDDLE =>
1447 13 magro732
              ---------------------------------------------------------------
1448
              -- The frame has not been fully sent.
1449 14 magro732
              -- Send a data symbol until the last part of the packet is
1450
              -- detected.
1451 13 magro732
              ---------------------------------------------------------------
1452
              -- REMARK: Dont send anything if there is a pending symbol in the
1453
              -- rx-control fifo to let it be transmitted in the middle of the
1454
              -- packet...
1455
 
1456
              -- Write a new data symbol.
1457
              symbolDataOut <= '1';
1458
              symbolDataContentOut <=
1459
                frameContent_i((32*NUMBER_WORDS)-1 downto (32*(NUMBER_WORDS-1)));
1460 4 magro732
 
1461 13 magro732
              if (unsigned(frameWordCounter_i) /= 0) then
1462
                frameWordCounter_o <=
1463
                  std_logic_vector(unsigned(frameWordCounter_i) - 1);
1464
                frameContent_o <=
1465
                  frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & x"00000000";
1466
              else
1467 14 magro732
                frameWordCounter_o <= readContentWords_i;
1468
                frameContent_o <= readContentData_i;
1469
 
1470 13 magro732
                -- Check if the frame is ending.
1471 14 magro732
                -- REMARK: Dont forget to update RioPacketBuffer to support this...
1472 13 magro732
                if (readContentEnd_i = '1') then
1473
                  -- The frame is ending.
1474 11 magro732
 
1475 13 magro732
                  -- Update the window to the next frame.
1476
                  -- It takes one tick for the output from the frame
1477
                  -- buffer to get updated.
1478
                  readWindowNextOut <= '1';
1479 11 magro732
 
1480 13 magro732
                  -- Proceed to check if there is another frame to start
1481
                  -- with directly.
1482 14 magro732
                  frameState_o <= FRAME_LAST;
1483 4 magro732
                else
1484 13 magro732
                  readContentOut <= '1';
1485 4 magro732
                end if;
1486 13 magro732
              end if;
1487
 
1488 14 magro732
            when FRAME_LAST =>
1489 13 magro732
              ---------------------------------------------------------------
1490
              -- A frame has ended and the window has been updated.
1491
              -- Check if the next symbol should end the frame or if a
1492
              -- new one should be started.
1493
              ---------------------------------------------------------------
1494 4 magro732
 
1495 14 magro732
              -- Write a new data symbol.
1496
              symbolDataOut <= '1';
1497
              symbolDataContentOut <=
1498
                frameContent_i((32*NUMBER_WORDS)-1 downto (32*(NUMBER_WORDS-1)));
1499 11 magro732
 
1500 14 magro732
              if (unsigned(frameWordCounter_i) /= 0) then
1501
                frameWordCounter_o <=
1502
                  std_logic_vector(unsigned(frameWordCounter_i) - 1);
1503
                frameContent_o <=
1504
                  frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & x"00000000";
1505
              else
1506
                -- Update the window ackId.
1507
                ackIdWindow_o <= std_logic_vector(unsigned(ackIdWindow_i) + 1);
1508 11 magro732
 
1509 14 magro732
                -- Start timeout supervision for transmitted frame.
1510
                timeSentSet_o <= '1';
1511 11 magro732
 
1512 14 magro732
                -- Note that the packet must be ended if the link-partner
1513
                -- cannot receive it.
1514
                if ((readWindowEmpty_i = '0') and
1515
                    (bufferStatus_i /= "00000") and
1516 15 magro732
                    ((unsigned(ackIdWindow_i)+2) /= unsigned(ackId_i))) then
1517 14 magro732
                  readContentOut <= '1';
1518
                  frameState_o <= FRAME_START;
1519
                else
1520
                  frameState_o <= FRAME_END;
1521
                end if;
1522
              end if;
1523
 
1524
            when FRAME_END =>
1525
              -----------------------------------------------------------------
1526
              -- 
1527
              -----------------------------------------------------------------
1528
 
1529 15 magro732
              -- Send a control symbol to end the packet.
1530
              symbolControlEndOut <= '1';
1531
 
1532 13 magro732
              -- Start a new frame the next time.
1533 14 magro732
              frameState_o <= FRAME_IDLE;
1534 13 magro732
 
1535 14 magro732
            when FRAME_DISCARD =>
1536
              ---------------------------------------------------------------
1537
              -- 
1538
              ---------------------------------------------------------------
1539
              -- The packet should be discarded.
1540
              -- Send idle-sequence.
1541
 
1542
              -- Check that there are no outstanding packets that
1543
              -- has not been acknowledged.
1544
              if(unsigned(ackIdWindow_i) = unsigned(ackId_i)) then
1545
                -- No unacknowledged packets.
1546
                -- It is now safe to remove the unallowed frame.
1547
                -- REMARK: Discard packet; readFrameOut <= '1';
1548
 
1549
                -- Go back and send a new frame.
1550
                frameState_o <= FRAME_IDLE;
1551
              end if;
1552
 
1553 13 magro732
            when others =>
1554
              ---------------------------------------------------------------
1555
              -- 
1556
              ---------------------------------------------------------------
1557
              null;
1558
 
1559
          end case;
1560 11 magro732
        end if;
1561
      end if;
1562
    end if;
1563
  end process;
1564
 
1565
  -----------------------------------------------------------------------------
1566
  -- N-1
1567
  -- Create the stype0 and stype1 part of a control symbol.
1568
  -- This process makes sure that the buffer status are transmitted at least
1569
  -- every 255 symbol.
1570
  -- At startup it makes sure that at least 16 status symbols are transmitted
1571
  -- before the operational-state is entered.
1572
  -- Input:  symbolControlStart, symbolControlEnd, symbolControlRestart,
1573
  --         symbolControlLinkRequest, symbolData, symbolDataContent
1574
  -- Output: symbolsTransmitted_o, operational_o, 
1575
  --         symbolControl, stype0, parameter0, parameter1, stype1, cmd,
1576
  --         symbolData1, symbolData1Content
1577
  -----------------------------------------------------------------------------
1578
 
1579
  process(clk, areset_n)
1580
  begin
1581
    if (areset_n = '0') then
1582 13 magro732
      rxControlUpdate_o <= '0';
1583
 
1584 11 magro732
      controlValid <= '0';
1585
      stype0 <= (others=>'0');
1586
      parameter0 <= (others=>'0');
1587
      parameter1 <= (others=>'0');
1588
      stype1 <= STYPE1_NOP;
1589
      cmd <= (others=>'0');
1590
      dataValid <= '0';
1591
      dataContent <= (others=>'0');
1592
    elsif (clk'event and clk = '1') then
1593
      if (txFull_i = '0') then
1594 13 magro732
        rxControlUpdate_o <= rxControlUpdateOut;
1595
 
1596 11 magro732
        controlValid <= controlValidOut;
1597
        stype0 <= stype0Out;
1598
        parameter0 <= parameter0Out;
1599
        parameter1 <= parameter1Out;
1600
        stype1 <= STYPE1_NOP;
1601
        cmd <= "000";
1602
        dataValid <= symbolData;
1603
        dataContent <= symbolDataContent;
1604
 
1605
        if (symbolControlStart = '1') then
1606
          stype1 <= STYPE1_START_OF_PACKET;
1607
        end if;
1608
        if (symbolControlEnd = '1') then
1609
          stype1 <= STYPE1_END_OF_PACKET;
1610
        end if;
1611
        if (symbolControlRestart = '1') then
1612
          stype1 <= STYPE1_RESTART_FROM_RETRY;
1613
        end if;
1614
        if (symbolControlLinkRequest = '1') then
1615
          stype1 <= STYPE1_LINK_REQUEST;
1616
          cmd <= LINK_REQUEST_CMD_INPUT_STATUS;
1617
        end if;
1618
      end if;
1619
    end if;
1620
  end process;
1621
 
1622
  symbolControlStype1 <=
1623
    symbolControlRestart or symbolControlLinkRequest or
1624
    symbolControlStart or symbolControlEnd;
1625
 
1626 13 magro732
  process(linkInitialized_i, ackIdStatus_i, portInitialized_i,
1627
          operational_i, counter_i, statusReceived_i, symbolsTransmitted_i,
1628
          rxControlEmpty_i,
1629
          symbolControlStype1, symbolData,
1630 14 magro732
          rxControlStype0, rxControlParameter0, rxControlParameter1,
1631
          fatalError_i)
1632 11 magro732
  begin
1633
    operational_o <= operational_i;
1634
    counter_o <= counter_i;
1635 12 magro732
    symbolsTransmitted_o <= symbolsTransmitted_i;
1636 13 magro732
    rxControlUpdateOut <= '0';
1637 11 magro732
 
1638
    controlValidOut <= '0';
1639
    stype0Out <= STYPE0_STATUS;
1640
    parameter0Out <= ackIdStatus_i;
1641
    parameter1Out <= "11111";
1642
 
1643 14 magro732
    if (fatalError_i = '1') then
1644
      operational_o <= '0';
1645
      counter_o <= NUMBER_STATUS_TRANSMIT;
1646
      symbolsTransmitted_o <= (others=>'0');
1647
    else
1648
      -- Check the operational state.
1649
      if (operational_i = '0') then
1650
        -----------------------------------------------------------------------
1651
        -- This state is entered at startup. A port that is not initialized
1652
        -- should only transmit idle sequences.
1653
        -----------------------------------------------------------------------
1654
 
1655
        -- Check if the port is initialized.
1656
        if (portInitialized_i = '1') then
1657
          ---------------------------------------------------------------------
1658
          -- The specification requires a status control symbol being sent at
1659
          -- least every 1024 code word until an error-free status has been
1660
          -- received. This implies that at most 256 idle sequences should be
1661
          -- sent in between status control symbols. Once an error-free status 
1662
          -- has been received, status symbols may be sent more rapidly. At
1663
          -- least 15 statuses has to be transmitted once an error-free status
1664
          -- has been received.
1665
          ---------------------------------------------------------------------
1666 11 magro732
 
1667 14 magro732
          -- Check if we are ready to change state to operational.
1668
          if ((linkInitialized_i = '1') and
1669
              (unsigned(counter_i) = 0)) then
1670
            -- Receiver has received enough error free status symbols and we
1671
            -- have transmitted enough.
1672
 
1673
            -- Considder ourselfs operational.
1674
            operational_o <= '1';
1675
          else
1676
            -- Not ready to change state to operational.
1677
            -- Dont do anything.
1678
          end if;
1679 13 magro732
 
1680 14 magro732
          -- Check if idle sequence or a status symbol should be transmitted.
1681
          if (((statusReceived_i = '0') and (symbolsTransmitted_i = x"ff")) or
1682
              ((statusReceived_i = '1') and (symbolsTransmitted_i > x"0f"))) then
1683
            -- A status symbol should be transmitted.
1684
 
1685
            -- Send a status control symbol to the link partner.
1686
            controlValidOut <= '1';
1687 11 magro732
 
1688 14 magro732
            -- Reset idle sequence transmission counter.
1689
            symbolsTransmitted_o <= (others=>'0');
1690 11 magro732
 
1691 14 magro732
            -- Check if the number of transmitted statuses should be updated.
1692
            if (statusReceived_i = '1') and (unsigned(counter_i) /= 0) then
1693
              counter_o <= std_logic_vector(unsigned(counter_i) - 1);
1694
            end if;
1695
          else
1696
            -- Increment the idle sequence transmission counter.
1697
            symbolsTransmitted_o <= std_logic_vector(unsigned(symbolsTransmitted_i) + 1);
1698 4 magro732
          end if;
1699 11 magro732
        else
1700 14 magro732
          -- The port is not initialized.
1701
          -- Reset initialization variables.
1702
          counter_o <= NUMBER_STATUS_TRANSMIT;
1703 11 magro732
        end if;
1704
      else
1705 14 magro732
        ---------------------------------------------------------------------
1706
        -- This is the operational state.
1707
        -- It is entered once the link has been considdered up and running.
1708
        ---------------------------------------------------------------------
1709 4 magro732
 
1710 14 magro732
        -- Check if the port is still initialized.
1711
        if (portInitialized_i = '1') then
1712
          -- The port is still initialized.
1713
 
1714
          -- Check if a status must be sent.
1715
          -- A status must be sent when there are not other stype0 value to
1716
          -- send or if too many symbols without buffer-status has been sent.
1717
          -- REMARK: Is there a risk of a race when a generated status-symbol
1718
          -- is sent before another symbol stored in the rx-control fifo???
1719
          if (((symbolControlStype1 = '1') and (rxControlEmpty_i = '1')) or
1720
              ((symbolControlStype1 = '0') and (symbolData = '0') and
1721
               (symbolsTransmitted_i = x"ff"))) then
1722
            -- A control symbol is about to be sent without pending symbol from
1723
            -- receiver or too many idle symbols has been sent.
1724 13 magro732
 
1725 14 magro732
            -- Force the sending of a status containing the bufferStatus.
1726
            controlValidOut <= '1';
1727 11 magro732
            symbolsTransmitted_o <= (others=>'0');
1728 14 magro732
          elsif ((symbolData = '0') and (rxControlEmpty_i = '0')) then
1729
            -- A control symbol is about to be sent and there is a pending
1730
            -- symbol from the receiver.
1731
 
1732
            -- Remove the symbol from the fifo.
1733
            rxControlUpdateOut <= '1';
1734
 
1735
            -- Send the receiver symbol.
1736
            controlValidOut <= '1';
1737
            stype0Out <= rxControlStype0;
1738
            parameter0Out <= rxControlParameter0;
1739
            parameter1Out <= rxControlParameter1;
1740
 
1741
            -- Check if the transmitted symbol contains status about
1742
            -- available buffers.
1743
            if ((rxControlStype0 = STYPE0_PACKET_ACCEPTED) or
1744
                (rxControlStype0 = STYPE0_PACKET_RETRY)) then
1745
              -- A symbol containing the bufferStatus has been sent.
1746
              symbolsTransmitted_o <= (others=>'0');
1747
            else
1748
              -- A symbol not containing the bufferStatus has been sent.
1749
              symbolsTransmitted_o <= std_logic_vector(unsigned(symbolsTransmitted_i) + 1);
1750
            end if;
1751 4 magro732
          else
1752 11 magro732
            -- A symbol not containing the bufferStatus has been sent.
1753 14 magro732
            controlValidOut <= '0';
1754 11 magro732
            symbolsTransmitted_o <= std_logic_vector(unsigned(symbolsTransmitted_i) + 1);
1755 4 magro732
          end if;
1756 11 magro732
        else
1757 14 magro732
          -- The port is not initialized anymore.
1758
          -- Change the operational state.
1759
          operational_o <= '0';
1760
        end if;
1761
      end if;
1762 11 magro732
    end if;
1763
  end process;
1764
 
1765
  -----------------------------------------------------------------------------
1766
  -- N pipeline stage.
1767
  -- Make all symbols ready for transmission, i.e. calculate the CRC5 on
1768
  -- control symbols.
1769
  -- Inputs: controlValid, stype0, parameter0, parameter1, stype1, cmd,
1770
  --         symbolData1, symbolData1Content 
1771
  -----------------------------------------------------------------------------
1772 4 magro732
 
1773 11 magro732
  controlContent(31 downto 29) <= stype0;
1774
  controlContent(28 downto 24) <= parameter0;
1775
  controlContent(23 downto 19) <= parameter1;
1776
  controlContent(18 downto 16) <= stype1;
1777
  controlContent(15 downto 13) <= cmd;
1778
  controlContent(12 downto 8) <= crc5;
1779
  controlContent(7 downto 0) <= x"00";
1780 4 magro732
 
1781 11 magro732
  Crc5Calculator: Crc5ITU
1782
    port map(
1783
      d_i=>controlContent(31 downto 13), crc_o=>crc5);
1784
 
1785
  txWrite_o <= not txFull_i;
1786
  process(clk, areset_n)
1787
  begin
1788
    if (areset_n = '0') then
1789
      txType_o <= SYMBOL_IDLE;
1790
      txData_o <= (others=>'0');
1791
    elsif (clk'event and clk = '1') then
1792
      if (txFull_i = '0') then
1793
        txType_o <= SYMBOL_IDLE;
1794
        if (controlValid = '1') then
1795
          txType_o <= SYMBOL_CONTROL;
1796
          txData_o <= controlContent;
1797
        end if;
1798
        if (dataValid = '1') then
1799
          txType_o <= SYMBOL_DATA;
1800
          txData_o <= dataContent;
1801
        end if;
1802
      end if;
1803 4 magro732
    end if;
1804 11 magro732
  end process;
1805
 
1806 4 magro732
end architecture;
1807
 
1808
 
1809
 
1810
-------------------------------------------------------------------------------
1811
-- 
1812
-------------------------------------------------------------------------------
1813
library ieee;
1814
use ieee.std_logic_1164.all;
1815
use ieee.numeric_std.all;
1816
use work.rio_common.all;
1817
 
1818
 
1819 11 magro732
entity RioReceiver is
1820
  generic(
1821
    NUMBER_WORDS : natural range 1 to 4 := 1);
1822
  port(
1823
    clk : in std_logic;
1824
    areset_n : in std_logic;
1825
 
1826
    portEnable_i : in std_logic;
1827
 
1828
    localAckIdWrite_i : in std_logic;
1829
    inboundAckId_i : in std_logic_vector(4 downto 0);
1830
    inboundAckId_o : out std_logic_vector(4 downto 0);
1831
 
1832
    portInitialized_i : in std_logic;
1833
    rxEmpty_i : in std_logic;
1834
    rxRead_o : out std_logic;
1835
    rxType_i : in std_logic_vector(2*NUMBER_WORDS-1 downto 0);
1836
    rxData_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1837
 
1838
    txControlWrite_o : out std_logic_vector(NUMBER_WORDS-1 downto 0);
1839
    txControlSymbol_o : out std_logic_vector(12*NUMBER_WORDS downto 0);
1840
    rxControlWrite_o : out std_logic_vector(NUMBER_WORDS-1 downto 0);
1841
    rxControlSymbol_o : out std_logic_vector(12*NUMBER_WORDS downto 0);
1842
 
1843
    ackIdStatus_o : out std_logic_vector(4 downto 0);
1844
    linkInitialized_o : out std_logic;
1845
 
1846
    writeFrameFull_i : in std_logic;
1847
    writeFrame_o : out std_logic;
1848
    writeFrameAbort_o : out std_logic;
1849
    writeContent_o : out std_logic;
1850
    writeContentWords_o : out std_logic_vector(1 downto 0);
1851
    writeContentData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0));
1852
end entity;
1853
 
1854 4 magro732
-------------------------------------------------------------------------------
1855
-- 
1856
-------------------------------------------------------------------------------
1857 11 magro732
architecture RioReceiverImpl of RioReceiver is
1858
 
1859
  component RioReceiverCore is
1860
    generic(
1861
      NUMBER_WORDS : natural range 1 to 4 := 1);
1862
    port(
1863
      clk : in std_logic;
1864
      areset_n : in std_logic;
1865
 
1866
      -- Status signals used for maintenance.
1867
      portEnable_i : in std_logic;
1868
 
1869
      -- Support for localAckIdCSR.
1870
      -- REMARK: Add support for this???
1871
      localAckIdWrite_i : in std_logic;
1872
      inboundAckId_i : in std_logic_vector(4 downto 0);
1873
      inboundAckId_o : out std_logic_vector(4 downto 0);
1874
 
1875
      -- Port input interface.
1876
      portInitialized_i : in std_logic;
1877
      rxEmpty_i : in std_logic;
1878
      rxRead_o : out std_logic;
1879
      rxType_i : in std_logic_vector(1 downto 0);
1880
      rxData_i : in std_logic_vector(31 downto 0);
1881
 
1882
      -- Receiver has received a control symbol containing:
1883
      -- packet-accepted, packet-retry, packet-not-accepted, 
1884
      -- status, VC_status, link-response
1885
      txControlWrite_o : out std_logic;
1886
      txControlSymbol_o : out std_logic_vector(12 downto 0);
1887
 
1888
      -- Reciever wants to signal the link partner:
1889
      -- a new frame has been accepted => packet-accepted(rxAckId, bufferStatus)
1890
      -- a frame needs to be retransmitted due to buffering =>
1891
      -- packet-retry(rxAckId, bufferStatus)
1892
      -- a frame is rejected due to errors => packet-not-accepted
1893
      -- a link-request should be answered => link-response
1894
      rxControlWrite_o : out std_logic;
1895
      rxControlSymbol_o : out std_logic_vector(12 downto 0);
1896
 
1897
      -- Status signals used internally.
1898
      ackIdStatus_o : out std_logic_vector(4 downto 0);
1899
      linkInitialized_o : out std_logic;
1900
 
1901
      -- Core->Core cascading signals.
1902
      enable_o : out std_logic;
1903
      operational_i : in std_logic;
1904
      operational_o : out std_logic;
1905
      inputRetryStopped_i : in std_logic;
1906
      inputRetryStopped_o : out std_logic;
1907
      inputErrorStopped_i : in std_logic;
1908
      inputErrorStopped_o : out std_logic;
1909
      ackId_i : in unsigned(4 downto 0);
1910
      ackId_o : out unsigned(4 downto 0);
1911
      frameIndex_i : in std_logic_vector(6 downto 0);
1912
      frameIndex_o : out std_logic_vector(6 downto 0);
1913
      frameWordCounter_i : in std_logic_vector(1 downto 0);
1914
      frameWordCounter_o : out std_logic_vector(1 downto 0);
1915
      frameContent_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1916
      frameContent_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1917
      crc_i : in std_logic_vector(15 downto 0);
1918
      crc_o : out std_logic_vector(15 downto 0);
1919
 
1920
      -- Frame buffering interface.
1921
      writeFrameFull_i : in std_logic;
1922
      writeFrame_o : out std_logic;
1923
      writeFrameAbort_o : out std_logic;
1924
      writeContent_o : out std_logic;
1925
      writeContentWords_o : out std_logic_vector(1 downto 0);
1926
      writeContentData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0));
1927
  end component;
1928
 
1929
  signal enable : std_logic_vector(NUMBER_WORDS-1 downto 0);
1930
  signal operationalCurrent, operationalNext : std_logic_vector(NUMBER_WORDS-1 downto 0);
1931
  signal inputRetryStoppedCurrent, inputRetryStoppedNext : std_logic_vector(NUMBER_WORDS-1 downto 0);
1932
  signal inputErrorStoppedCurrent, inputErrorStoppedNext : std_logic_vector(NUMBER_WORDS-1 downto 0);
1933
  signal ackIdCurrent, ackIdNext : unsigned(5*NUMBER_WORDS-1 downto 0);
1934
  signal frameIndexCurrent, frameIndexNext : std_logic_vector(7*NUMBER_WORDS-1 downto 0);
1935
  signal frameWordCounterCurrent, frameWordCounterNext : std_logic_vector(2*NUMBER_WORDS-1 downto 0);
1936
  signal frameContentCurrent, frameContentNext : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1937
  signal crcCurrent, crcNext : std_logic_vector(16*NUMBER_WORDS-1 downto 0);
1938
 
1939
  signal txControlWrite : std_logic_vector(NUMBER_WORDS-1 downto 0);
1940
  signal rxControlWrite : std_logic_vector(NUMBER_WORDS-1 downto 0);
1941
 
1942
  signal writeFrame : std_logic_vector(NUMBER_WORDS-1 downto 0);
1943
  signal writeFrameAbort : std_logic_vector(NUMBER_WORDS-1 downto 0);
1944
  signal writeContent : std_logic_vector(NUMBER_WORDS-1 downto 0);
1945
  signal writeContentWords : std_logic_vector(2*NUMBER_WORDS-1 downto 0);
1946
  signal writeContentData : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
1947
 
1948
begin
1949
 
1950
  -----------------------------------------------------------------------------
1951
  -- Output generation to packet buffer.
1952
  -----------------------------------------------------------------------------
1953
  process(enable, writeFrame, writeFrameAbort,
1954
          writeContent, writeContentWords, writeContentData)
1955
  begin
1956
    writeFrame_o <= '0';
1957
    writeFrameAbort_o <= '0';
1958
    writeContent_o <= '0';
1959
    writeContentWords_o <= (others=>'0');
1960
    writeContentData_o <= (others=>'0');
1961
    for i in 0 to NUMBER_WORDS-1 loop
1962
      if ((writeFrame(i) = '1') and (enable(i) = '1')) then
1963
        writeFrame_o <= '1';
1964
      end if;
1965
 
1966
      if ((writeFrameAbort(i) = '1') and (enable(i) = '1')) then
1967
        writeFrameAbort_o <= '1';
1968
      end if;
1969
 
1970
      if ((writeContent(i) = '1') and (enable(i) = '1')) then
1971
        writeContent_o <= '1';
1972
        writeContentWords_o <= writeContentWords(2*(i+1)-1 downto 2*i);
1973
        writeContentData_o <= writeContentData(32*(i+1)-1 downto 32*i);
1974
      end if;
1975
    end loop;
1976
  end process;
1977
 
1978
  -----------------------------------------------------------------------------
1979
  -- Protocol core and synchronization.
1980
  -----------------------------------------------------------------------------
1981
  process(clk, areset_n)
1982
  begin
1983
    if (areset_n = '0') then
1984
      operationalCurrent <= (others=>'0');
1985
      inputRetryStoppedCurrent <= (others=>'0');
1986
      inputErrorStoppedCurrent <= (others=>'0');
1987
      ackIdCurrent <= (others=>'0');
1988
      frameIndexCurrent <= (others => '0');
1989
      frameWordCounterCurrent <= (others=>'0');
1990
      frameContentCurrent <= (others=>'0');
1991
      crcCurrent <= (others=>'0');
1992
    elsif (clk'event and clk = '1') then
1993
      if (enable(0) = '1') then
1994
        operationalCurrent <= operationalNext;
1995
        inputRetryStoppedCurrent <= inputRetryStoppedNext;
1996
        inputErrorStoppedCurrent <= inputErrorStoppedNext;
1997
        ackIdCurrent <= ackIdNext;
1998
        frameIndexCurrent <= frameIndexNext;
1999
        frameWordCounterCurrent <= frameWordCounterNext;
2000
        frameContentCurrent <= frameContentNext;
2001
        crcCurrent <= crcNext;
2002
      end if;
2003
    end if;
2004
  end process;
2005
 
2006
  CoreGeneration: for i in 0 to NUMBER_WORDS-1 generate
2007
    txControlWrite_o(i) <= txControlWrite(i) and enable(i);
2008
    rxControlWrite_o(i) <= rxControlWrite(i);
2009
 
2010
    ReceiverCore: RioReceiverCore
2011
      generic map(NUMBER_WORDS=>NUMBER_WORDS)
2012
      port map(
2013
        clk=>clk,
2014
        areset_n=>areset_n,
2015
        portEnable_i=>portEnable_i,
2016
        localAckIdWrite_i=>localAckIdWrite_i,
2017
        inboundAckId_i=>inboundAckId_i,
2018
        inboundAckId_o=>inboundAckId_o,
2019
        portInitialized_i=>portInitialized_i,
2020
        rxEmpty_i=>rxEmpty_i,
2021
        rxRead_o=>rxRead_o,
2022
        rxType_i=>rxType_i,
2023
        rxData_i=>rxData_i,
2024
        txControlWrite_o=>txControlWrite(i),
2025
        txControlSymbol_o=>txControlSymbol_o(13*(i+1)-1 downto 13*i),
2026
        rxControlWrite_o=>rxControlWrite(i),
2027
        rxControlSymbol_o=>rxControlSymbol_o(13*(i+1)-1 downto 13*i),
2028
        ackIdStatus_o=>ackIdStatus_o,
2029
        linkInitialized_o=>linkInitialized_o,
2030
        enable_o=>enable(i),
2031
        operational_i=>operationalCurrent(i),
2032
        operational_o=>operationalNext(i),
2033
        inputRetryStopped_i=>inputRetryStoppedCurrent(i),
2034
        inputRetryStopped_o=>inputRetryStoppedNext(i),
2035
        inputErrorStopped_i=>inputErrorStoppedCurrent(i),
2036
        inputErrorStopped_o=>inputErrorStoppedNext(i),
2037
        ackId_i=>ackIdCurrent(5*(i+1)-1 downto 5*i),
2038
        ackId_o=>ackIdNext(5*(i+1)-1 downto 5*i),
2039
        frameIndex_i=>frameIndexCurrent(7*(i+1)-1 downto 7*i),
2040
        frameIndex_o=>frameIndexNext(7*(i+1)-1 downto 7*i),
2041
        frameWordCounter_i=>frameWordCounterCurrent(2*(i+1)-1 downto 2*i),
2042
        frameWordCounter_o=>frameWordCounterNext(2*(i+1)-1 downto 2*i),
2043
        frameContent_i=>frameContentCurrent(32*(i+1)-1 downto 32*i),
2044
        frameContent_o=>frameContentNext(32*(i+1)-1 downto 32*i),
2045
        crc_i=>crcCurrent(16*(i+1)-1 downto 16*i),
2046
        crc_o=>crcNext(16*(i+1)-1 downto 16*i),
2047
        writeFrameFull_i=>writeFrameFull_i,
2048
        writeFrame_o=>writeFrame(i),
2049
        writeFrameAbort_o=>writeFrameAbort(i),
2050
        writeContent_o=>writeContent(i),
2051
        writeContentWords_o=>writeContentWords(2*(i+1)-1 downto 2*i),
2052
        writeContentData_o=>writeContentData(32*(i+1)-1 downto 32*i));
2053
  end generate;
2054
 
2055
end architecture;
2056
 
2057
-------------------------------------------------------------------------------
2058
-- RioReceiverCore
2059
-------------------------------------------------------------------------------
2060
library ieee;
2061
use ieee.std_logic_1164.all;
2062
use ieee.numeric_std.all;
2063
use work.rio_common.all;
2064
 
2065
 
2066
-------------------------------------------------------------------------------
2067
-- 
2068
-------------------------------------------------------------------------------
2069
entity RioReceiverCore is
2070
  generic(
2071
    NUMBER_WORDS : natural range 1 to 4 := 1);
2072 4 magro732
  port(
2073
    clk : in std_logic;
2074
    areset_n : in std_logic;
2075
 
2076
    -- Status signals used for maintenance.
2077
    portEnable_i : in std_logic;
2078
 
2079
    -- Support for localAckIdCSR.
2080 11 magro732
    -- REMARK: Add support for this???
2081 4 magro732
    localAckIdWrite_i : in std_logic;
2082
    inboundAckId_i : in std_logic_vector(4 downto 0);
2083
    inboundAckId_o : out std_logic_vector(4 downto 0);
2084
 
2085
    -- Port input interface.
2086
    portInitialized_i : in std_logic;
2087
    rxEmpty_i : in std_logic;
2088
    rxRead_o : out std_logic;
2089 11 magro732
    rxType_i : in std_logic_vector(1 downto 0);
2090 4 magro732
    rxData_i : in std_logic_vector(31 downto 0);
2091
 
2092
    -- Receiver has received a control symbol containing:
2093
    -- packet-accepted, packet-retry, packet-not-accepted, 
2094
    -- status, VC_status, link-response
2095
    txControlWrite_o : out std_logic;
2096
    txControlSymbol_o : out std_logic_vector(12 downto 0);
2097
 
2098
    -- Reciever wants to signal the link partner:
2099
    -- a new frame has been accepted => packet-accepted(rxAckId, bufferStatus)
2100
    -- a frame needs to be retransmitted due to buffering =>
2101
    -- packet-retry(rxAckId, bufferStatus)
2102
    -- a frame is rejected due to errors => packet-not-accepted
2103
    -- a link-request should be answered => link-response
2104
    rxControlWrite_o : out std_logic;
2105
    rxControlSymbol_o : out std_logic_vector(12 downto 0);
2106
 
2107
    -- Status signals used internally.
2108
    ackIdStatus_o : out std_logic_vector(4 downto 0);
2109
    linkInitialized_o : out std_logic;
2110
 
2111 11 magro732
    -- Core->Core cascading signals.
2112
    enable_o : out std_logic;
2113
    operational_i : in std_logic;
2114
    operational_o : out std_logic;
2115
    inputRetryStopped_i : in std_logic;
2116
    inputRetryStopped_o : out std_logic;
2117
    inputErrorStopped_i : in std_logic;
2118
    inputErrorStopped_o : out std_logic;
2119
    ackId_i : in unsigned(4 downto 0);
2120
    ackId_o : out unsigned(4 downto 0);
2121
    frameIndex_i : in std_logic_vector(6 downto 0);
2122
    frameIndex_o : out std_logic_vector(6 downto 0);
2123
    frameWordCounter_i : in std_logic_vector(1 downto 0);
2124
    frameWordCounter_o : out std_logic_vector(1 downto 0);
2125
    frameContent_i : in std_logic_vector(32*NUMBER_WORDS-1 downto 0);
2126
    frameContent_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0);
2127
    crc_i : in std_logic_vector(15 downto 0);
2128
    crc_o : out std_logic_vector(15 downto 0);
2129
 
2130 4 magro732
    -- Frame buffering interface.
2131
    writeFrameFull_i : in std_logic;
2132
    writeFrame_o : out std_logic;
2133
    writeFrameAbort_o : out std_logic;
2134
    writeContent_o : out std_logic;
2135 11 magro732
    writeContentWords_o : out std_logic_vector(1 downto 0);
2136
    writeContentData_o : out std_logic_vector(32*NUMBER_WORDS-1 downto 0));
2137 4 magro732
end entity;
2138
 
2139
 
2140
-------------------------------------------------------------------------------
2141
-- 
2142
-------------------------------------------------------------------------------
2143 11 magro732
architecture RioReceiverCoreImpl of RioReceiverCore is
2144 4 magro732
 
2145
  component Crc5ITU is
2146
    port(
2147
      d_i : in  std_logic_vector(18 downto 0);
2148
      crc_o : out std_logic_vector(4 downto 0));
2149
  end component;
2150
 
2151
  component Crc16CITT is
2152
    port(
2153
      d_i : in  std_logic_vector(15 downto 0);
2154
      crc_i : in  std_logic_vector(15 downto 0);
2155
      crc_o : out std_logic_vector(15 downto 0));
2156
  end component;
2157
 
2158 11 magro732
  signal symbolEnable0, symbolEnable1 : std_logic;
2159
  signal symbolType0 : std_logic_vector(1 downto 0);
2160
  signal symbolContent0, symbolContent1 : std_logic_vector(31 downto 0);
2161
  signal crc5Valid : std_logic;
2162 4 magro732
  signal crc5 : std_logic_vector(4 downto 0);
2163
 
2164 11 magro732
  signal symbolValid : std_logic;
2165
  signal stype0Status : std_logic;
2166
  signal stype1Start : std_logic;
2167
  signal stype1End : std_logic;
2168
  signal stype1Stomp : std_logic;
2169
  signal stype1Restart : std_logic;
2170
  signal stype1LinkRequest : std_logic;
2171
  signal symbolData : std_logic;
2172
 
2173 4 magro732
  signal crc16Data : std_logic_vector(31 downto 0);
2174
  signal crc16Current : std_logic_vector(15 downto 0);
2175
  signal crc16Temp : std_logic_vector(15 downto 0);
2176
  signal crc16Next : std_logic_vector(15 downto 0);
2177 11 magro732
  signal crc16Valid : std_logic;
2178
 
2179
  signal frameContent : std_logic_vector(32*NUMBER_WORDS-1 downto 0);
2180
 
2181
  signal rxControlWrite : std_logic;
2182
  signal rxControlSymbol : std_logic_vector(12 downto 0);
2183 4 magro732
 
2184
begin
2185
 
2186 11 magro732
  linkInitialized_o <= operational_i;
2187
  ackIdStatus_o <= std_logic_vector(ackId_i);
2188
  inboundAckId_o <= std_logic_vector(ackId_i);
2189
 
2190 4 magro732
  -----------------------------------------------------------------------------
2191 11 magro732
  -- First pipeline stage.
2192
  -- Check the validity of the symbol, CRC5 on control symbols, and save the
2193
  -- symbol content for the next stage.
2194 4 magro732
  -----------------------------------------------------------------------------
2195
 
2196 11 magro732
  -- Read the fifo immediatly.
2197
  rxRead_o <= not rxEmpty_i;
2198 4 magro732
 
2199 11 magro732
  Crc5Calculator: Crc5ITU
2200
    port map(
2201
      d_i=>rxData_i(31 downto 13), crc_o=>crc5);
2202
 
2203
  process(clk, areset_n)
2204
  begin
2205
    if (areset_n = '0') then
2206
      crc5Valid <= '0';
2207
      symbolType0 <= (others => '0');
2208
      symbolContent0 <= (others => '0');
2209
    elsif (clk'event and clk = '1') then
2210
      if (rxEmpty_i = '0') then
2211
        if (crc5 = rxData_i(12 downto 8)) then
2212
          crc5Valid <= '1';
2213
        else
2214
          crc5Valid <= '0';
2215
        end if;
2216
        symbolEnable0 <= '1';
2217
        symbolType0 <= rxType_i;
2218
        symbolContent0 <= rxData_i;
2219
      else
2220
        symbolEnable0 <= '0';
2221
      end if;
2222
    end if;
2223
  end process;
2224
 
2225 4 magro732
  -----------------------------------------------------------------------------
2226 11 magro732
  -- Second pipeline stage.
2227
  -- Separate the part of the control symbol that are going to the transmitter
2228
  -- side and check the type of symbol for this side. 
2229 4 magro732
  -----------------------------------------------------------------------------
2230
 
2231 11 magro732
  process(clk, areset_n)
2232
  begin
2233
    if (areset_n = '0') then
2234
      txControlWrite_o <= '0';
2235
      txControlSymbol_o <= (others => '0');
2236 4 magro732
 
2237 11 magro732
      symbolValid <= '0';
2238
      stype0Status <= '0';
2239
      stype1Start <= '0';
2240
      stype1End <= '0';
2241
      stype1Stomp <= '0';
2242
      stype1Restart <= '0';
2243
      stype1LinkRequest <= '0';
2244
      symbolData <= '0';
2245
 
2246
      symbolContent1 <= (others => '0');
2247
    elsif (clk'event and clk = '1') then
2248
      if (symbolEnable0 = '1') then
2249
        symbolEnable1 <= '1';
2250
        symbolContent1 <= symbolContent0;
2251
 
2252
        if (symbolType0 = SYMBOL_CONTROL) then
2253
          if (crc5Valid = '1') then
2254
            -- REMARK: Check if stype0 is nop and dont forward it???
2255
            symbolValid <= '1';
2256
            txControlWrite_o <= '1';
2257
            txControlSymbol_o <= symbolContent0(31 downto 19);
2258
          else
2259
            symbolValid <= '0';
2260
            txControlWrite_o <= '0';
2261
            txControlSymbol_o <= (others => '0');
2262
          end if;
2263
        else
2264
          symbolValid <= '1';
2265
        end if;
2266
 
2267
        if ((symbolType0 = SYMBOL_CONTROL) and
2268
            (symbolContent0(31 downto 29) = STYPE0_STATUS)) then
2269
          stype0Status <= '1';
2270
        else
2271
          stype0Status <= '0';
2272
        end if;
2273
        if ((symbolType0 = SYMBOL_CONTROL) and
2274
            (symbolContent0(18 downto 16) = STYPE1_START_OF_PACKET)) then
2275
          stype1Start <= '1';
2276
        else
2277
          stype1Start <= '0';
2278
        end if;
2279
        if ((symbolType0 = SYMBOL_CONTROL) and
2280
            (symbolContent0(18 downto 16) = STYPE1_END_OF_PACKET)) then
2281
          stype1End <= '1';
2282
        else
2283
          stype1End <= '0';
2284
        end if;
2285
        if ((symbolType0 = SYMBOL_CONTROL) and
2286
            (symbolContent0(18 downto 16) = STYPE1_STOMP)) then
2287
          stype1Stomp <= '1';
2288
        else
2289
          stype1Stomp <= '0';
2290
        end if;
2291
        if ((symbolType0 = SYMBOL_CONTROL) and
2292
            (symbolContent0(18 downto 16) = STYPE1_RESTART_FROM_RETRY)) then
2293
          stype1Restart <= '1';
2294
        else
2295
          stype1Restart <= '0';
2296
        end if;
2297
        if ((symbolType0 = SYMBOL_CONTROL) and
2298
            (symbolContent0(18 downto 16) = STYPE1_LINK_REQUEST)) then
2299
          stype1LinkRequest <= '1';
2300
        else
2301
          stype1LinkRequest <= '0';
2302
        end if;
2303
        if (symbolType0 = SYMBOL_DATA) then
2304
          symbolData <= '1';
2305
        else
2306
          symbolData <= '0';
2307
        end if;
2308
      else
2309
        symbolEnable1 <= '0';
2310
      end if;
2311
    end if;
2312
  end process;
2313
 
2314 4 magro732
  -----------------------------------------------------------------------------
2315 11 magro732
  -- Third pipeline stage.
2316
  -- Update the CRC16 for the packet.
2317
  -- Update the buffered data and write it to the packet buffer if needed.
2318
  -- Update the main receiver state machine.
2319
  -- Generate reply symbols to the link-partner.
2320
  -- Note that this stage cannot contain any registers as it could be cascaded
2321
  -- to other cores.
2322 4 magro732
  -----------------------------------------------------------------------------
2323 11 magro732
 
2324
  -----------------------------------------------------------------------------
2325
  -- CRC-calculation.
2326
  -- Add a data symbol to the calculated CRC for the packet.
2327
  -- controlSymbol->just forward old crc16.
2328
  -- first data-symbol in packet->crc_o is product of 11111 and the
2329
  -- symbolContent1 without ackid.
2330
  -- not first data-symbol->crc_o is product of crc_i and symbolContent1.
2331
  -----------------------------------------------------------------------------
2332 4 magro732
 
2333 11 magro732
  crc16Data(31 downto 26) <= "000000" when (unsigned(frameIndex_i) = 1) else
2334
                             symbolContent1(31 downto 26);
2335
  crc16Data(25 downto 0) <= symbolContent1(25 downto 0);
2336 4 magro732
 
2337 11 magro732
  crc16Current <= crc_i when (unsigned(frameIndex_i) /= 1) else
2338
                  (others => '1');
2339
 
2340
  Crc16Msb: Crc16CITT
2341 4 magro732
    port map(
2342
      d_i=>crc16Data(31 downto 16), crc_i=>crc16Current, crc_o=>crc16Temp);
2343 11 magro732
  Crc16Lsb: Crc16CITT
2344 4 magro732
    port map(
2345
      d_i=>crc16Data(15 downto 0), crc_i=>crc16Temp, crc_o=>crc16Next);
2346
 
2347 11 magro732
  crc_o <= crc_i when (symbolData = '0') else
2348
           crc16Next;
2349
 
2350
  crc16Valid <= '1' when (crc_i = x"0000") else '0';
2351
 
2352 4 magro732
  -----------------------------------------------------------------------------
2353 11 magro732
  -- Update buffered data.
2354 4 magro732
  -----------------------------------------------------------------------------
2355
 
2356 11 magro732
  -- Append the new symbol content to the end of the
2357
  -- current frame content if the symbol is a data symbol.
2358
  frameContentSingle:
2359
  if (NUMBER_WORDS = 1) generate
2360
    frameContent <= symbolContent1;
2361
  end generate;
2362
  frameContentMulti:
2363
  if (NUMBER_WORDS > 1) generate
2364
    frameContent <=
2365
      (frameContent_i((32*(NUMBER_WORDS-1))-1 downto 0) & symbolContent1) when (symbolData = '1') else
2366
      frameContent_i;
2367
  end generate;
2368 4 magro732
 
2369 11 magro732
  -- Update outputs.
2370
  enable_o <= symbolEnable1;
2371
  frameContent_o <= frameContent;
2372
  writeContentData_o <= frameContent;
2373 4 magro732
 
2374 11 magro732
  -----------------------------------------------------------------------------
2375
  -- Main inbound symbol handler.
2376
  -----------------------------------------------------------------------------
2377
  process(portInitialized_i, portEnable_i, writeFrameFull_i,
2378
          operational_i, ackId_i, frameIndex_i, frameWordCounter_i,
2379
          inputRetryStopped_i, inputErrorStopped_i,
2380
          symbolValid,
2381
          stype0Status,
2382
          stype1Start, stype1End, stype1Stomp, stype1Restart, stype1LinkRequest,
2383
          symbolData,
2384
          symbolContent1,
2385
          frameContent,
2386
          crc16Valid)
2387
  begin
2388
    operational_o <= operational_i;
2389
    ackId_o <= ackId_i;
2390
    frameIndex_o <= frameIndex_i;
2391
    frameWordCounter_o <= frameWordCounter_i;
2392
    inputRetryStopped_o <= inputRetryStopped_i;
2393
    inputErrorStopped_o <= inputErrorStopped_i;
2394
 
2395
    rxControlWrite <= '0';
2396
    rxControlSymbol <= (others => '0');
2397
 
2398
    writeFrame_o <= '0';
2399
    writeFrameAbort_o <= '0';
2400
    writeContent_o <= '0';
2401
    writeContentWords_o <= (others => '0');
2402 4 magro732
 
2403 11 magro732
    -- Act on the current state.
2404
    if (operational_i = '0') then
2405
      ---------------------------------------------------------------------
2406
      -- The port is not operational and is waiting for status control
2407
      -- symbols to be received on the link. Count the number
2408
      -- of error-free status symbols and considder the link operational
2409
      -- when enough of them has been received. Frames are not allowed
2410
      -- here.
2411
      ---------------------------------------------------------------------
2412
 
2413
      -- Check if the port is initialized.
2414
      if (portInitialized_i = '1') then
2415
        -- Port is initialized.
2416
 
2417
        -- Check if the control symbol has a valid checksum.
2418
        if (symbolValid = '1') then
2419
          -- The control symbol has a valid checksum.
2420 4 magro732
 
2421 11 magro732
          -- Check the stype0 part if we should count the number of
2422
          -- error-free status symbols.
2423
          if (stype0Status = '1') then
2424
            -- The symbol is a status.
2425 4 magro732
 
2426 11 magro732
            -- Check if enough status symbols have been received.
2427
            if (unsigned(frameIndex_i) = 7) then
2428
              -- Enough status symbols have been received.
2429 4 magro732
 
2430 11 magro732
              -- Reset all packets.
2431
              frameIndex_o <= (others => '0');
2432
              writeFrameAbort_o <= '1';
2433
 
2434
              -- Set the link as initialized.
2435
              operational_o <= '1';
2436 4 magro732
            else
2437 11 magro732
              -- Increase the number of error-free status symbols that
2438
              -- has been received.
2439
              frameIndex_o <= std_logic_vector(unsigned(frameIndex_i) + 1);
2440 4 magro732
            end if;
2441 11 magro732
          else
2442
            -- The symbol is not a status.
2443
            -- Dont do anything.
2444
          end if;
2445
        else
2446
          -- A control symbol with CRC5 error was recevied.
2447
          frameIndex_o <= (others => '0');
2448
        end if;
2449
      else
2450
        -- The port has become uninitialized.
2451
        frameIndex_o <= (others => '0');
2452
      end if;
2453
    else
2454
      ---------------------------------------------------------------------
2455
      -- The port has been initialized and enough error free status symbols
2456
      -- have been received. Forward data frames to the frame buffer
2457
      -- interface. This is the normal operational state.
2458
      ---------------------------------------------------------------------
2459
 
2460
      -- Check that the port is initialized.
2461
      if (portInitialized_i = '1') then
2462
        -- The port and link is initialized.
2463
 
2464
        -- Check if the control symbol has a valid CRC-5.
2465
        if (symbolValid = '1') then
2466
          -- The symbol is correct.
2467 4 magro732
 
2468 11 magro732
          if ((stype1Start = '1') and
2469
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2470
            -------------------------------------------------------------
2471
            -- Start the reception of a new frame or end a currently
2472
            -- ongoing frame and start a new one.
2473
            -------------------------------------------------------------
2474 4 magro732
 
2475 11 magro732
            -- Check if a frame has already been started.
2476
            if (unsigned(frameIndex_i) /= 0) then
2477
              -- A frame is already started.
2478
              -- Complete the last frame and start to ackumulate a new one
2479
              -- and update the ackId.
2480 4 magro732
 
2481 11 magro732
              if (unsigned(frameIndex_i) > 3) then
2482 4 magro732
 
2483 11 magro732
                -- Reset the frame index to indicate the frame is started.
2484
                frameIndex_o <= "0000001";
2485
                frameWordCounter_o <= (others=>'0');
2486
 
2487
                -- Check the CRC-16 and the length of the received frame.
2488
                if (crc16Valid = '1') then
2489
                  -- The CRC-16 is ok.
2490
 
2491
                  -- Check if there are any unwritten buffered packet content
2492
                  -- and write it if there is.
2493
                  -- REMARK: Multi-symbol support...
2494
                  if (unsigned(frameWordCounter_i) /= 0) then
2495
                    writeContent_o <= '1';
2496
                  end if;
2497 4 magro732
 
2498 11 magro732
                  -- Update the frame buffer to indicate that the frame has
2499
                  -- been completly received.
2500
                  writeFrame_o <= '1';
2501 4 magro732
 
2502 11 magro732
                  -- Update ackId.
2503
                  ackId_o <= ackId_i + 1;
2504 4 magro732
 
2505 11 magro732
                  -- Send packet-accepted.
2506
                  -- The buffer status is appended by the transmitter
2507
                  -- when sent to get the latest number.
2508
                  rxControlWrite <= '1';
2509
                  rxControlSymbol <= STYPE0_PACKET_ACCEPTED &
2510
                                     std_logic_vector(ackId_i) &
2511
                                     "11111";
2512
                else
2513
                  -- The CRC-16 is not ok.
2514 4 magro732
 
2515 11 magro732
                  -- Make the transmitter send a packet-not-accepted to indicate
2516
                  -- that the received packet contained a CRC error.
2517
                  rxControlWrite <= '1';
2518
                  rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2519
                                     "00000" &
2520
                                     PACKET_NOT_ACCEPTED_CAUSE_PACKET_CRC;
2521
                  inputErrorStopped_o <= '1';
2522 4 magro732
                end if;
2523
              else
2524 11 magro732
                -- This packet is too small.
2525
                -- Make the transmitter send a packet-not-accepted to indicated
2526
                -- that the received packet was too small.
2527
                rxControlWrite <= '1';
2528
                rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2529
                                   "00000" &
2530
                                   PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2531
                inputErrorStopped_o <= '1';
2532 4 magro732
              end if;
2533
            else
2534 11 magro732
              -- No frame has been started.
2535
 
2536
              -- Reset the frame index to indicate the frame is started.
2537
              frameIndex_o <= "0000001";
2538
              frameWordCounter_o <= (others=>'0');
2539 4 magro732
            end if;
2540 11 magro732
          end if;
2541
 
2542
          if ((stype1End = '1') and
2543
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2544
            -------------------------------------------------------------
2545
            -- End the reception of an old frame.
2546
            -------------------------------------------------------------
2547 4 magro732
 
2548 11 magro732
            -- Check if a frame has already been started.
2549
            if (unsigned(frameIndex_i) > 3) then
2550
              -- A frame has been started and it is large enough.
2551 4 magro732
 
2552 11 magro732
              -- Reset frame reception to indicate that no frame is ongoing.
2553
              frameIndex_o <= (others => '0');
2554
 
2555
              -- Check the CRC-16 and the length of the received frame.
2556
              if (crc16Valid = '1') then
2557
                -- The CRC-16 is ok.
2558 4 magro732
 
2559 11 magro732
                -- Check if there are any unwritten buffered packet content
2560
                -- and write it if there is.
2561
                -- REMARK: Multi-symbol support...
2562
                if (unsigned(frameWordCounter_i) /= 0) then
2563
                  writeContent_o <= '1';
2564
                end if;
2565 4 magro732
 
2566 11 magro732
                -- Update the frame buffer to indicate that the frame has
2567
                -- been completly received.
2568
                writeFrame_o <= '1';
2569 4 magro732
 
2570 11 magro732
                -- Update ackId.
2571
                ackId_o <= ackId_i + 1;
2572 4 magro732
 
2573 11 magro732
                -- Send packet-accepted.
2574
                -- The buffer status is appended by the transmitter
2575
                -- when sent to get the latest number.
2576
                rxControlWrite <= '1';
2577
                rxControlSymbol <= STYPE0_PACKET_ACCEPTED &
2578
                                   std_logic_vector(ackId_i) &
2579
                                   "11111";
2580
              else
2581
                -- The CRC-16 is not ok.
2582 4 magro732
 
2583 11 magro732
                -- Make the transmitter send a packet-not-accepted to indicate
2584
                -- that the received packet contained a CRC error.
2585
                rxControlWrite <= '1';
2586
                rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2587
                                   "00000" &
2588
                                   PACKET_NOT_ACCEPTED_CAUSE_PACKET_CRC;
2589
                inputErrorStopped_o <= '1';
2590 4 magro732
              end if;
2591
            else
2592 11 magro732
              -- This packet is too small.
2593
              -- Make the transmitter send a packet-not-accepted to indicate
2594
              -- that the received packet was too small.
2595
              rxControlWrite <= '1';
2596
              rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2597
                                 "00000" &
2598
                                 PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2599
              inputErrorStopped_o <= '1';
2600 4 magro732
            end if;
2601 11 magro732
          end if;
2602 4 magro732
 
2603 11 magro732
          if ((stype1Stomp = '1') and
2604
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2605
            -------------------------------------------------------------
2606
            -- Restart the reception of an old frame.
2607
            -------------------------------------------------------------
2608
            -- See 5.10 in the standard.
2609 4 magro732
 
2610 11 magro732
            -- Make the transmitter send a packet-retry to indicate
2611
            -- that the packet cannot be accepted.
2612
            rxControlWrite <= '1';
2613
            rxControlSymbol <= STYPE0_PACKET_RETRY &
2614
                               std_logic_vector(ackId_i) &
2615
                               "11111";
2616
 
2617
            -- Enter the input retry-stopped state.
2618
            inputRetryStopped_o <= '1';
2619
          end if;
2620 4 magro732
 
2621 11 magro732
          if (stype1Restart = '1') then
2622
            if (inputRetryStopped_i = '1') then
2623
              -------------------------------------------------------------
2624
              -- The receiver indicates a restart from a retry sent
2625
              -- from us.
2626
              -------------------------------------------------------------
2627 4 magro732
 
2628 11 magro732
              -- Abort the frame and reset frame reception.
2629
              frameIndex_o <= (others => '0');
2630
              writeFrameAbort_o <= '1';
2631
 
2632
              -- Go back to the normal operational state.
2633
              inputRetryStopped_o <= '0';
2634
            else
2635
              -------------------------------------------------------------
2636
              -- The receiver indicates a restart from a retry sent
2637
              -- from us.
2638
              -------------------------------------------------------------
2639
              -- See 5.10 in the standard.
2640
              -- Protocol error, this symbol should not be received here since
2641
              -- we should have been in input-retry-stopped. 
2642
 
2643
              -- Send a packet-not-accepted to indicate that a protocol
2644
              -- error has occurred.
2645
              rxControlWrite <= '1';
2646
              rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2647
                                 "00000" &
2648
                                 PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2649
              inputErrorStopped_o <= '1';
2650
            end if;
2651
          end if;
2652 4 magro732
 
2653 11 magro732
          if (stype1LinkRequest = '1') then
2654
            -------------------------------------------------------------
2655
            -- Reply to a LINK-REQUEST.
2656
            -------------------------------------------------------------
2657
 
2658
            -- Check the command part.
2659
            if (symbolContent1(15 downto 13) = "100") then
2660
              -- Return input port status command.
2661
              -- This functions as a link-request(restart-from-error)
2662
              -- control symbol under error situations.
2663 4 magro732
 
2664 11 magro732
              if (inputErrorStopped_i = '1') then
2665
                rxControlWrite <= '1';
2666
                rxControlSymbol <= STYPE0_LINK_RESPONSE &
2667
                                   std_logic_vector(ackId_i) &
2668
                                   "00101";
2669
              elsif (inputRetryStopped_i = '1') then
2670
                rxControlWrite <= '1';
2671
                rxControlSymbol <= STYPE0_LINK_RESPONSE &
2672
                                   std_logic_vector(ackId_i) &
2673
                                   "00100";
2674 4 magro732
              else
2675 11 magro732
                -- Send a link response containing an ok reply.
2676
                rxControlWrite <= '1';
2677
                rxControlSymbol <= STYPE0_LINK_RESPONSE &
2678
                                   std_logic_vector(ackId_i) &
2679
                                   "10000";
2680 4 magro732
              end if;
2681
            else
2682 11 magro732
              -- Reset device command or other unsupported command.
2683
              -- Discard this.
2684 4 magro732
            end if;
2685
 
2686 11 magro732
            -- Abort the frame and reset frame reception.
2687
            inputRetryStopped_o <= '0';
2688
            inputErrorStopped_o <= '0';
2689
            frameIndex_o <= (others=>'0');
2690
            writeFrameAbort_o <= '1';
2691
          end if;
2692 4 magro732
 
2693 11 magro732
          if ((symbolData = '1')  and
2694
              (inputRetryStopped_i = '0') and (inputErrorStopped_i = '0')) then
2695
            -------------------------------------------------------------
2696
            -- This is a data symbol.
2697
            -------------------------------------------------------------
2698
            -- REMARK: Add check for in-the-middle-crc here...
2699 4 magro732
 
2700 11 magro732
            case frameIndex_i is
2701
              when "0000000" | "1000110" =>
2702
                -- A frame has not been started or is too long.
2703
                -- Send packet-not-accepted.
2704
                rxControlWrite <= '1';
2705
                rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2706
                                   "00000" &
2707
                                   PACKET_NOT_ACCEPTED_CAUSE_GENERAL_ERROR;
2708
                inputErrorStopped_o <= '1';
2709
              when "0000001" =>
2710
                if (unsigned(symbolContent1(31 downto 27)) = ackId_i) then
2711
                  if ((portEnable_i = '1') or
2712
                      (symbolContent1(19 downto 16) = FTYPE_MAINTENANCE_CLASS)) then
2713
 
2714
                    -- Check if there are buffers available to store the new
2715
                    -- packet.
2716
                    if (writeFrameFull_i = '0') then
2717
                      -- There are buffering space available to store the new
2718
                      -- data.
2719
 
2720
                      -- Check if the buffer entry is ready to be written
2721
                      -- into the packet buffer.
2722
                      -- REMARK: Multi-symbol support...
2723
                      if (unsigned(frameWordCounter_i) = (NUMBER_WORDS-1)) then
2724
                        -- Write the data to the frame FIFO.
2725
                        frameWordCounter_o <= (others=>'0');
2726
                        writeContent_o <= '1';
2727
                        writeContentWords_o <= frameWordCounter_i;
2728
                      else
2729
                        frameWordCounter_o <= std_logic_vector(unsigned(frameWordCounter_i) + 1);
2730
                      end if;
2731 4 magro732
 
2732 11 magro732
                      -- Increment the number of received data symbols.
2733
                      frameIndex_o <= std_logic_vector(unsigned(frameIndex_i) + 1);
2734
                    else
2735
                      -- The packet buffer is full.
2736
                      -- Let the link-partner resend the packet.
2737
                      rxControlWrite <= '1';
2738
                      rxControlSymbol <= STYPE0_PACKET_RETRY &
2739
                                         std_logic_vector(ackId_i) &
2740
                                         "11111";
2741
                      inputRetryStopped_o <= '1';
2742
                    end if;
2743 4 magro732
                  else
2744 11 magro732
                    -- A non-maintenance packets are not allowed.
2745
                    -- Send packet-not-accepted.
2746
                    rxControlWrite <= '1';
2747
                    rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2748
                                       "00000" &
2749
                                       PACKET_NOT_ACCEPTED_CAUSE_NON_MAINTENANCE_STOPPED;
2750
                    inputErrorStopped_o <= '1';
2751 4 magro732
                  end if;
2752
                else
2753 11 magro732
                  -- The ackId is unexpected.
2754
                  -- Send packet-not-accepted.
2755
                  rxControlWrite <= '1';
2756
                  rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2757
                                     "00000" &
2758
                                     PACKET_NOT_ACCEPTED_CAUSE_UNEXPECTED_ACKID;
2759
                  inputErrorStopped_o <= '1';
2760 4 magro732
                end if;
2761 11 magro732
              when others =>
2762
                -- A frame has been started and is not too long.
2763
                -- Check if the buffer entry is ready to be written
2764
                -- into the packet buffer.
2765
                -- REMARK: Multi-symbol support...
2766
                if (unsigned(frameWordCounter_i) = (NUMBER_WORDS-1)) then
2767
                  -- Write the data to the frame FIFO.
2768
                  frameWordCounter_o <= (others=>'0');
2769
                  writeContent_o <= '1';
2770
                  writeContentWords_o <= frameWordCounter_i;
2771
                else
2772
                  frameWordCounter_o <= std_logic_vector(unsigned(frameWordCounter_i) + 1);
2773
                end if;
2774 4 magro732
 
2775 11 magro732
                -- Increment the number of received data symbols.
2776
                frameIndex_o <= std_logic_vector(unsigned(frameIndex_i) + 1);
2777
            end case;
2778
          end if;
2779
        else
2780
          -- A control symbol contains a crc error.
2781 4 magro732
 
2782 11 magro732
          -- Send a packet-not-accepted to indicate that a corrupted
2783
          -- control-symbol has been received and change state.
2784
          rxControlWrite <= '1';
2785
          rxControlSymbol <= STYPE0_PACKET_NOT_ACCEPTED &
2786
                             "00000" &
2787
                             PACKET_NOT_ACCEPTED_CAUSE_CONTROL_CRC;
2788
          inputErrorStopped_o <= '1';
2789
        end if;
2790
      else
2791
        -- The port has become uninitialized.
2792
        -- Go back to the uninitialized state.
2793
        operational_o <= '0';
2794 4 magro732
      end if;
2795
    end if;
2796
  end process;
2797
 
2798 11 magro732
  -----------------------------------------------------------------------------
2799
  -- Fourth pipeline stage.
2800
  -----------------------------------------------------------------------------
2801
  -- REMARK: Do more stuff in this stage, convert a one-hot to the symbol to
2802
  -- send...
2803
  -- REMARK: Register other outputs here like writeContent_o...
2804
 
2805
  process(clk, areset_n)
2806
  begin
2807
    if (areset_n = '0') then
2808
      rxControlWrite_o <= '0';
2809
      rxControlSymbol_o <= (others=>'0');
2810
    elsif (clk'event and clk = '1') then
2811
      rxControlWrite_o <= rxControlWrite and symbolEnable1;
2812
      rxControlSymbol_o <= rxControlSymbol;
2813
    end if;
2814
  end process;
2815
 
2816 4 magro732
end architecture;
2817
 
2818
 
2819
 
2820
-------------------------------------------------------------------------------
2821
--
2822
---------------------------------------------------------------------------------
2823
library ieee;
2824
use ieee.std_logic_1164.all;
2825
 
2826
 
2827
-------------------------------------------------------------------------------
2828
-- 
2829
-------------------------------------------------------------------------------
2830
entity RioFifo1 is
2831
  generic(
2832
    WIDTH : natural);
2833
  port(
2834
    clk : in std_logic;
2835
    areset_n : in std_logic;
2836
 
2837
    empty_o : out std_logic;
2838
    read_i : in std_logic;
2839
    data_o : out std_logic_vector(WIDTH-1 downto 0);
2840
 
2841
    full_o : out std_logic;
2842
    write_i : in std_logic;
2843
    data_i : in std_logic_vector(WIDTH-1 downto 0));
2844
end entity;
2845
 
2846
 
2847
-------------------------------------------------------------------------------
2848
-- 
2849
-------------------------------------------------------------------------------
2850
architecture RioFifo1Impl of RioFifo1 is
2851
  signal empty : std_logic;
2852
  signal full : std_logic;
2853
begin
2854
 
2855
  empty_o <= empty;
2856
  full_o <= full;
2857
 
2858
  process(areset_n, clk)
2859
  begin
2860
    if (areset_n = '0') then
2861
      empty <= '1';
2862
      full <= '0';
2863
      data_o <= (others => '0');
2864
    elsif (clk'event and clk = '1') then
2865
      if (empty = '1') then
2866
        if (write_i = '1') then
2867
          empty <= '0';
2868
          full <= '1';
2869
          data_o <= data_i;
2870
        end if;
2871
      else
2872
        if (read_i = '1') then
2873
          empty <= '1';
2874
          full <= '0';
2875
        end if;
2876
      end if;
2877
    end if;
2878
  end process;
2879
 
2880
end architecture;
2881
 
2882
 
2883
 
2884
-------------------------------------------------------------------------------
2885
-- A CRC-5 calculator following the implementation proposed in the 2.2
2886
-- standard.
2887
-------------------------------------------------------------------------------
2888
library ieee;
2889
use ieee.std_logic_1164.all;
2890
 
2891
 
2892
-------------------------------------------------------------------------------
2893
-- 
2894
-------------------------------------------------------------------------------
2895
entity Crc5ITU is
2896
  port(
2897
    d_i : in  std_logic_vector(18 downto 0);
2898
    crc_o : out std_logic_vector(4 downto 0));
2899
end entity;
2900
 
2901
 
2902
-------------------------------------------------------------------------------
2903
-- 
2904
-------------------------------------------------------------------------------
2905
architecture Crc5Impl of Crc5ITU is
2906
  signal d : std_logic_vector(0 to 18);
2907
  signal c : std_logic_vector(0 to 4);
2908
 
2909
begin
2910
  -- Reverse the bit vector indexes to make them the same as in the standard.
2911
  d(18) <= d_i(0); d(17) <= d_i(1); d(16) <= d_i(2); d(15) <= d_i(3);
2912
  d(14) <= d_i(4); d(13) <= d_i(5); d(12) <= d_i(6); d(11) <= d_i(7);
2913
  d(10) <= d_i(8); d(9) <= d_i(9); d(8) <= d_i(10); d(7) <= d_i(11);
2914
  d(6) <= d_i(12); d(5) <= d_i(13); d(4) <= d_i(14); d(3) <= d_i(15);
2915
  d(2) <= d_i(16); d(1) <= d_i(17); d(0) <= d_i(18);
2916
 
2917
  -- Calculate the resulting crc.
2918
  c(0) <= d(18) xor d(16) xor d(15) xor d(12) xor
2919
          d(10) xor d(5) xor d(4) xor d(3) xor
2920
          d(1) xor d(0);
2921
  c(1) <= (not d(18)) xor d(17) xor d(15) xor d(13) xor
2922
          d(12) xor d(11) xor d(10) xor d(6) xor
2923
          d(3) xor d(2) xor d(0);
2924
  c(2) <= (not d(18)) xor d(16) xor d(14) xor d(13) xor
2925
          d(12) xor d(11) xor d(7) xor d(4) xor
2926
          d(3) xor d(1);
2927
  c(3) <= (not d(18)) xor d(17) xor d(16) xor d(14) xor
2928
          d(13) xor d(10) xor d(8) xor d(3) xor
2929
          d(2) xor d(1);
2930
  c(4) <= d(18) xor d(17) xor d(15) xor d(14) xor
2931
          d(11) xor d(9) xor d(4) xor d(3) xor
2932
          d(2) xor d(0);
2933
 
2934
  -- Reverse the bit vector indexes to make them the same as in the standard.
2935
  crc_o(4) <= c(0); crc_o(3) <= c(1); crc_o(2) <= c(2); crc_o(1) <= c(3);
2936
  crc_o(0) <= c(4);
2937
end architecture;
2938 11 magro732
 
2939
 
2940
-------------------------------------------------------------------------------
2941
-- 
2942
-------------------------------------------------------------------------------
2943
library ieee;
2944
use ieee.std_logic_1164.all;
2945
use ieee.numeric_std.all;
2946
use work.rio_common.all;
2947
 
2948
-------------------------------------------------------------------------------
2949
-- 
2950
-------------------------------------------------------------------------------
2951
entity Crc16Calculator is
2952
 
2953
  generic (
2954
    NUMBER_WORDS : natural range 1 to 8 := 1);
2955
 
2956
  port (
2957
    clk : in std_logic;
2958
    areset_n : in std_logic;
2959
 
2960
    write_i : in std_logic;
2961
    crc_i : in std_logic_vector(15 downto 0);
2962
    data_i : in std_logic_vector((32*NUMBER_WORDS)-1 downto 0);
2963
 
2964
    crc_o : out std_logic_vector(15 downto 0);
2965
    done_o : out std_logic);
2966
 
2967
end Crc16Calculator;
2968
 
2969
-------------------------------------------------------------------------------
2970
-- 
2971
-------------------------------------------------------------------------------
2972
architecture Crc16CalculatorImpl of Crc16Calculator is
2973
 
2974
  component Crc16CITT is
2975
    port(
2976
      d_i : in  std_logic_vector(15 downto 0);
2977
      crc_i : in  std_logic_vector(15 downto 0);
2978
      crc_o : out std_logic_vector(15 downto 0));
2979
  end component;
2980
 
2981
  signal iterator : natural range 0 to 2*NUMBER_WORDS;
2982
  signal crcData : std_logic_vector((32*NUMBER_WORDS)-1 downto 0);
2983
  signal crcCurrent : std_logic_vector(15 downto 0);
2984
  signal crcNext : std_logic_vector(15 downto 0);
2985
 
2986
begin
2987
 
2988
  process(areset_n, clk)
2989
  begin
2990
    if (areset_n = '0') then
2991
      iterator <= 0;
2992
      done_o <= '0';
2993
      crc_o <= (others => '0');
2994
    elsif (clk'event and clk = '1') then
2995
      if (write_i = '1') then
2996
        iterator <= 2*NUMBER_WORDS-1;
2997
        crcData <= data_i;
2998
        crcCurrent <= crc_i;
2999
        done_o <= '0';
3000
      else
3001
        if (iterator /= 0) then
3002
          iterator <= iterator - 1;
3003
          crcData <= crcData(((32*NUMBER_WORDS)-1)-16 downto 0) & x"0000";
3004
          crcCurrent <= crcNext;
3005
        else
3006
          crc_o <= crcNext;
3007
          done_o <= '1';
3008
        end if;
3009
      end if;
3010
    end if;
3011
  end process;
3012
 
3013
  Crc16Inst: Crc16CITT
3014
    port map(
3015
      d_i=>crcData((32*NUMBER_WORDS)-1 downto (32*NUMBER_WORDS)-16),
3016
      crc_i=>crcCurrent, crc_o=>crcNext);
3017
 
3018
end architecture;
3019
 

powered by: WebSVN 2.1.0

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