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

Subversion Repositories rio

[/] [rio/] [trunk/] [rtl/] [vhdl/] [RioWbBridge.vhd] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 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 a bridge between a RapidIO network and a Wishbone bus. Packets
10
-- NWRITE and NREAD are currently supported.
11
-- 
12
-- To Do:
13
-- -
14
-- 
15
-- Author(s): 
16
-- - Nader Kardouni, nader.kardouni@se.transport.bombardier.com 
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
library ieee;
46
use ieee.numeric_std.ALL;
47
use ieee.std_logic_1164.all;
48
use ieee.std_logic_unsigned.all;
49
use work.rio_common.all;
50
 
51
-------------------------------------------------------------------------------
52
-- entity for RioWbBridge.
53
-------------------------------------------------------------------------------
54
Entity RioWbBridge is
55
  generic(
56
    DEVICE_IDENTITY : std_logic_vector(15 downto 0);
57
    DEVICE_VENDOR_IDENTITY : std_logic_vector(15 downto 0);
58
    DEVICE_REV : std_logic_vector(31 downto 0);
59
    ASSY_IDENTITY : std_logic_vector(15 downto 0);
60
    ASSY_VENDOR_IDENTITY : std_logic_vector(15 downto 0);
61
    ASSY_REV : std_logic_vector(15 downto 0);
62
    DEFAULT_BASE_DEVICE_ID : std_logic_vector(15 downto 0) := x"ffff");
63
  port(
64
    clk : in std_logic;                         -- Main clock 25MHz
65
    areset_n : in std_logic;                    -- Asynchronous reset, active low
66
 
67
    readFrameEmpty_i : in std_logic;
68
    readFrame_o : out std_logic;
69
    readContent_o : out std_logic;
70
    readContentEnd_i : in std_logic;
71
    readContentData_i : in std_logic_vector(31 downto 0);
72
 
73
    writeFrameFull_i : in std_logic;
74
    writeFrame_o : out std_logic;
75
    writeFrameAbort_o : out std_logic;
76
    writeContent_o : out std_logic;
77
    writeContentData_o : out std_logic_vector(31 downto 0);
78
 
79
    -- interface to the peripherals module
80
    wbStb_o : out std_logic;                     -- strob signal, active high
81
    wbWe_o : out std_logic;                      -- write signal, active high
82
    wbData_o : out std_logic_vector(7 downto 0); -- master data bus    
83
    wbAdr_o : out std_logic_vector(25 downto 0); -- master address bus 
84
    wbErr_i : in std_logic;                     -- error signal, high active
85
    wbAck_i : in std_logic;                     -- slave acknowledge
86
    wbData_i : in std_logic_vector(7 downto 0)  -- slave data bus
87
    );
88
end;
89
 
90
-------------------------------------------------------------------------------
91
-- Architecture for RioWbBridge.
92
-------------------------------------------------------------------------------
93
architecture rtl of RioWbBridge is
94
 
95
  component Crc16CITT is
96
    port(
97
      d_i : in  std_logic_vector(15 downto 0);
98
      crc_i : in  std_logic_vector(15 downto 0);
99
      crc_o : out std_logic_vector(15 downto 0));
100
  end component;
101
 
102
  constant RST_LVL        : std_logic := '0';
103
  constant BERR_UNKNOWN_DATA    : std_logic_vector(7 downto 0) := X"08"; -- not valid data
104
  constant BERR_FRAME_SIZE      : std_logic_vector(7 downto 0) := X"81"; -- Frame code size error
105
  constant BERR_FRAME_CODE      : std_logic_vector(7 downto 0) := X"80"; -- Frame code type error
106
  constant BERR_NOT_RESPONSE    : std_logic_vector(7 downto 0) := X"86"; -- Not response from the device
107
 
108
  type state_type_RioBrige is (IDLE, WAIT_HEADER_0, HEADER_0, HEADER_1, CHECK_OPERATION,
109
                               READ_ADDRESS, READ_FROM_FIFO, CHECK_ERROR, WRITE_DATA,
110
                               WRITE_TO_WB, WAIT_IDLE, SEND_DONE_0, SEND_DONE_1,
111
                               SEND_DONE_2, READ_FROM_WB, APPEND_CRC,
112
                               SEND_TO_FIFO, SEND_ERROR, SEND_FRAME, APPEND_CRC_AND_SEND,
113
                               SEND_MAINTENANCE_READ_RESPONSE_0, SEND_MAINTENANCE_READ_RESPONSE_1,
114
                               SEND_MAINTENANCE_WRITE_RESPONSE_0, SEND_MAINTENANCE_WRITE_RESPONSE_1);
115
  signal stateRB, nextStateRB : state_type_RioBrige;
116
  type byteArray8 is array (0 to 7) of std_logic_vector(7 downto 0);
117
  signal dataLane : byteArray8;
118
--  type byteArray4 is array (0 to 3) of std_logic_vector(7 downto 0);
119
--  signal dataLaneS : byteArray4;
120
  signal pos, byteOffset : integer range 0 to 7;
121
  signal numberOfByte, byteCnt, headLen : integer range 0 to 256;
122
  signal endianMsb, reserved, ready : std_logic;
123
  signal start : std_logic;
124
  signal wdptr : std_logic;
125
  signal wbStb : std_logic;
126
  signal xamsbs : std_logic_vector(1 downto 0);
127
  signal ftype : std_logic_vector(3 downto 0);
128
  signal ttype : std_logic_vector(3 downto 0);
129
  signal size : std_logic_vector(3 downto 0);
130
  signal tid : std_logic_vector(7 downto 0);
131
  signal tt : std_logic_vector(1 downto 0);
132
  signal errorCode : std_logic_vector(7 downto 0);
133
  signal sourceId : std_logic_vector(15 downto 0);
134
  signal destinationId : std_logic_vector(15 downto 0);
135
  signal writeContentData : std_logic_vector(31 downto 0);
136
  signal crc16Current, crc16Temp, crc16Next: std_logic_vector(15 downto 0);
137
  signal tempAddr : std_logic_vector(25 downto 0);
138
  signal timeOutCnt : std_logic_vector(14 downto 0);
139
 
140
  -- Configuration memory signal declaration.
141
  signal configEnable : std_logic;
142
  signal configWrite : std_logic;
143
  signal configAddress : std_logic_vector(23 downto 0);
144
  signal configDataWrite : std_logic_vector(31 downto 0);
145
  signal configDataRead : std_logic_vector(31 downto 0);
146
  signal componentTag : std_logic_vector(31 downto 0);
147
  signal baseDeviceId : std_logic_vector(15 downto 0) := DEFAULT_BASE_DEVICE_ID;
148
  signal hostBaseDeviceIdLocked : std_logic;
149
  signal hostBaseDeviceId : std_logic_vector(15 downto 0) := (others => '1');
150
 
151
begin
152
  wbStb_o <= wbStb;
153
  writeContentData_o <= writeContentData;
154
 
155
  Crc16High: Crc16CITT
156
    port map(
157
      d_i=>writeContentData(31 downto 16), crc_i=>crc16Current, crc_o=>crc16Temp);
158
  Crc16Low: Crc16CITT
159
    port map(
160
      d_i=>writeContentData(15 downto 0), crc_i=>crc16Temp, crc_o=>crc16Next);
161
 
162
 
163
 
164
  -----------------------------------------------------------------------------
165
  -- wbInterfaceCtrl
166
  -- This process handle the Wishbone interface to the RioWbBridge module.
167
  -----------------------------------------------------------------------------
168
  wbInterfaceCtrl: process(clk, areset_n)
169
  variable Temp : std_logic_vector(2 downto 0);
170
  begin
171
    if areset_n = RST_LVL then
172
      start <= '0';
173
      wdptr <= '0';
174
      wbStb <= '0';
175
      wbWe_o <= '0';
176
      byteCnt <= 0;
177
      headLen <= 0;
178
      byteOffset <= 0;
179
      readFrame_o <= '0';
180
      readContent_o <= '0';
181
      writeFrame_o <= '0';
182
      writeContent_o <= '0';
183
      writeFrameAbort_o <= '0';
184
      configWrite <= '0';
185
      configEnable <= '0';
186
      ready <= '0';
187
      endianMsb <= '0';
188
      stateRB <= IDLE;
189
      nextStateRB <= IDLE;
190
      tt <= (others => '0');
191
      tid <= (others => '0');
192
      size <= (others => '0');
193
      ttype <= (others => '0');
194
      ftype <= (others => '0');
195
      xamsbs <= (others => '0');
196
      sourceId <= (others => '0');
197
      configDataWrite <= (others => '0');
198
      destinationId <= (others => '0');
199
      errorCode <= (others => '0');
200
      tempAddr <= (others => '0');
201
      wbAdr_o <= (others => '0');
202
      wbData_o <= (others => '0');
203
      writeContentData <= (others => '0');
204
      dataLane <= (others =>(others => '0'));
205
--      dataLaneS <= (others =>(others => '0')); 
206
      crc16Current <= (others => '0');
207
      timeOutCnt <= (others => '0');
208
      Temp := (others => '0');
209
    elsif clk'event and clk ='1' then
210
 
211
      case stateRB is
212
        when IDLE =>
213
          if (readFrameEmpty_i = '0') and (writeFrameFull_i = '0') then
214
            readContent_o <= '1';
215
            byteCnt <= 0;
216
            ready <= '0';
217
            endianMsb <= '1';
218
            timeOutCnt <= (others => '0');
219
            crc16Current <= (others => '1');
220
            stateRB <= WAIT_HEADER_0;
221
          else
222
            start <= '0';
223
            readFrame_o <= '0';
224
            readContent_o <= '0';
225
            writeFrame_o <= '0';
226
            writeContent_o <= '0';
227
            writeFrameAbort_o <= '0';
228
            errorCode <= (others => '0');
229
            writeContentData <= (others => '0');
230
            dataLane <= (others =>(others => '0'));
231
--            dataLaneS <= (others =>(others => '0')); 
232
            Temp := (others => '0');
233
          end if;
234
 
235
        when WAIT_HEADER_0 =>
236
          stateRB <= HEADER_0;
237
 
238
        when HEADER_0 =>
239
          readContent_o <= '1';                          -- read the header (frame 0)
240
          tt <= readContentData_i(21 downto 20);
241
          ftype <= readContentData_i(19 downto 16);
242
          destinationId <= readContentData_i(15 downto 0);
243
          stateRB <= HEADER_1;
244
 
245
        when HEADER_1 =>                                 -- read the header (frame 1)
246
          readContent_o <= '1';
247
          ttype <= readContentData_i(15 downto 12);
248
          size <= readContentData_i(11 downto 8);
249
          tid <= readContentData_i(7 downto 0);
250
          sourceId <= readContentData_i(31 downto 16);
251
          stateRB <= READ_ADDRESS;
252
 
253
        when READ_ADDRESS =>
254
          readContent_o <= '0';
255
          wdptr <= readContentData_i(2);
256
          xamsbs <= readContentData_i(1 downto 0);
257
          tempAddr <= readContentData_i(25 downto 3) & "000";  -- Wishbone address bus is 26 bits width
258
          configAddress <= readContentData_i(23 downto 0);     -- this line is in case of maintenance pakage (config-offset(21-bits)+wdptr(1-bit)+rsv(2-bits))
259
          stateRB <= CHECK_ERROR;
260
 
261
        when CHECK_ERROR =>
262
          byteOffset <= pos;               -- first byte position in the first payload
263
          tempAddr <= tempAddr + pos;      -- first address
264
          if readContentEnd_i = '1' then   -- check if data not valid i the switch buffer
265
            wbStb <= '0';
266
            wbWe_o <= '0';
267
            byteOffset <= 0;
268
            writeFrameAbort_o <= '1';               -- over write the frame with an error frame
269
            errorCode <= BERR_UNKNOWN_DATA;         -- not valid data
270
            stateRB <= SEND_ERROR;
271
 
272
          -- check if error in the frame size for write pakage
273
          elsif (reserved = '1') and (ftype = FTYPE_WRITE_CLASS) then
274
            wbStb <= '0';
275
            wbWe_o <= '0';
276
            byteOffset <= 0;
277
            writeFrameAbort_o <= '1';               -- over write the frame with an error frame
278
            errorCode <= BERR_FRAME_SIZE;           -- Frame code size error
279
            stateRB <= SEND_ERROR;
280
 
281
          -- type 5 pakage formate, NWRITE transaction (write to peripherals) read payload from the buffer
282
          elsif (ftype = FTYPE_WRITE_CLASS) and (ttype = "0100") and (tt = "01") then
283
            readContent_o <= '1';
284
            stateRB <= READ_FROM_FIFO;          -- read the payload
285
            nextStateRB <= SEND_ERROR;     -- this is in case not valid data in switch buffer
286
            headLen <= 12;
287
 
288
          -- Type 2 pakage formate, NREAD transaction, (read from peripherals) write payload to the buffer
289
          elsif (ftype = FTYPE_REQUEST_CLASS) and (ttype = "0100") and (tt = "01") then
290
            writeContent_o <= '1';  -- write the header-0 of the Read Response pakage
291
            writeContentData(15 downto 0) <= sourceId;      -- write to the source address
292
            writeContentData(19 downto 16) <= "1101";       -- Response pakage type 13, ftype Response
293
            writeContentData(21 downto 20) <= "01";         -- tt
294
            writeContentData(31 downto 22) <= "0000000000"; -- acckId, vc, cfr, prio           
295
            stateRB <= SEND_DONE_0; --
296
            headLen <= 8;
297
 
298
          -- Type 8 pakage formate, maintenance Read request
299
          elsif (ftype = FTYPE_MAINTENANCE_CLASS) and (ttype = TTYPE_MAINTENANCE_READ_REQUEST) and (tt = "01") then
300
            configWrite <= '0';                                        -- read config operation
301
            configEnable <= '1';                                       -- enable signal to the memoryConfig process
302
            writeContent_o <= '1';
303
            -- write the header-0 of the Read Response pakage
304
            writeContentData(15 downto 0) <= sourceId;                 -- write to the source address, this is a response pakage
305
            writeContentData(19 downto 16) <= FTYPE_MAINTENANCE_CLASS; -- ftype, maintenance
306
            writeContentData(21 downto 20) <= "01";                    -- tt
307
            writeContentData(31 downto 22) <= "0000000000";            -- acckId, vc, cfr, prio           
308
            stateRB <= SEND_MAINTENANCE_READ_RESPONSE_0;
309
 
310
          -- Type 8 pakage formate, maintenance Write request
311
          elsif (ftype = FTYPE_MAINTENANCE_CLASS) and (ttype = TTYPE_MAINTENANCE_WRITE_REQUEST) and (tt = "01") then
312
            configWrite <= '1';                                        -- write config operation
313
            writeContent_o <= '1';                                     -- write the header-0
314
            writeContentData(15 downto 0) <= sourceId;                 -- write to the source address, this is a response pakage
315
            writeContentData(19 downto 16) <= FTYPE_MAINTENANCE_CLASS; -- ftype, maintenance
316
            writeContentData(21 downto 20) <= "01";                    -- tt
317
            writeContentData(31 downto 22) <= "0000000000";            -- acckId, vc, cfr, prio           
318
            stateRB <= SEND_MAINTENANCE_WRITE_RESPONSE_0;
319
 
320
          -- Error: unexpected ftype or ttype
321
          else
322
            wbStb <= '0';
323
            wbWe_o <= '0';
324
            byteOffset <= 0;
325
            writeFrameAbort_o <= '1';               -- over write the frame with an error frame
326
            errorCode <= BERR_FRAME_CODE;
327
            stateRB <= SEND_ERROR;     -- next state after the dataLane is stored in the switch buffer
328
          end if;
329
 
330
        when SEND_MAINTENANCE_READ_RESPONSE_0 =>
331
          byteCnt <= 0;
332
          configEnable <= '0';                             -- disable signal to the memoryConfig process
333
          -- write the header-1 of the Read Response pakage
334
          writeContentData(7 downto 0) <= tid;
335
          writeContentData(11 downto 8) <= "0000";         -- size/status
336
          writeContentData(15 downto 12) <= TTYPE_MAINTENANCE_READ_RESPONSE; -- transaction type, Maintenance Read Response 
337
          writeContentData(31 downto 16) <= baseDeviceId; -- destination address, because this is a response pakage
338
          crc16Current <= crc16Next;                      -- first frame's CRC
339
          stateRB <= SEND_MAINTENANCE_READ_RESPONSE_1;
340
 
341
        when SEND_MAINTENANCE_READ_RESPONSE_1 =>
342
          byteCnt <= byteCnt + 1;                         -- using byteCnt as a counter
343
          if byteCnt = 0 then
344
            writeContentData <= X"FF" & X"000000";        -- write the filed with HOP + reserved
345
            crc16Current <= crc16Next;                    -- second frame's CRC
346
          elsif byteCnt = 1 then
347
            if configAddress(2) = '0' then                -- check the wdptr bit in the config offset field
348
              writeContentData <= configDataRead;         -- write payload-0 with data if wdptr='0'
349
            else
350
              writeContentData <= (others => '0');        -- write zeros 
351
            end if;
352
            crc16Current <= crc16Next;                    -- third frame's CRC
353
          elsif byteCnt = 2 then
354
            if configAddress(2) = '0' then                -- check the wdptr bit in the config offset field
355
              writeContentData <= (others => '0');        -- write zeros
356
            else
357
              writeContentData <= configDataRead;         -- write payload-1 with data if wdptr='1'
358
            end if;
359
            crc16Current <= crc16Next;                    -- forth frame's CRC
360
          elsif byteCnt = 3 then
361
            writeContentData <= crc16Next & X"0000";      -- write the CRC field
362
          else
363
            writeContent_o <= '0';
364
            stateRB <= SEND_FRAME;
365
          end if;
366
 
367
        when SEND_MAINTENANCE_WRITE_RESPONSE_0 =>
368
          byteCnt <= 0;
369
          readContent_o <= '1';                           -- read the config offset
370
          if configAddress(2) = '0' then                  -- check the wdptr bit in the config offset field
371
            configDataWrite <= readContentData_i;         -- copy payload-0 if wdptr='0'
372
          else
373
            configDataWrite <= configDataWrite;           -- do nothing
374
          end if;
375
          writeContentData(7 downto 0) <= tid;
376
          writeContentData(11 downto 8) <= "0000";        -- size/status
377
          writeContentData(15 downto 12) <= TTYPE_MAINTENANCE_WRITE_RESPONSE; -- transaction type, Maintenance Write Response 
378
          writeContentData(31 downto 16) <= baseDeviceId; -- destination address, because this is a response pakage
379
          crc16Current <= crc16Next;                      -- first frame's CRC
380
          stateRB <= SEND_MAINTENANCE_WRITE_RESPONSE_1;
381
 
382
        when SEND_MAINTENANCE_WRITE_RESPONSE_1 =>
383
          byteCnt <= byteCnt + 1;                         -- using byteCnt as a counter
384
          if byteCnt = 0 then
385
            writeContentData <= X"FF" & X"000000";        -- write the filed with HOP + reserved
386
            crc16Current <= crc16Next;                    -- second frame's CRC
387
          elsif byteCnt = 1 then
388
            configEnable <= '1';                          -- enable signal to the memoryConfig process
389
            writeContentData <= crc16Next & X"0000";      -- write the CRC field
390
            if configAddress(2) = '0' then                -- check the wdptr bit in the config offset field
391
              configDataWrite <= configDataWrite;         -- do nothing
392
            else
393
              configDataWrite <= readContentData_i;       -- copy payload-1 if wdptr='1'
394
            end if;
395
          else
396
            configEnable <= '0';                          -- disable signal to the memoryConfig process
397
            readContent_o <= '0';                         -- at this point even the frame's CRC is read from the buffer
398
            writeContent_o <= '0';
399
            stateRB <= SEND_FRAME;
400
          end if;
401
 
402
        when SEND_DONE_0 =>
403
          writeContent_o <= '1';
404
          writeContentData(7 downto 0) <= tid;
405
          writeContentData(11 downto 8) <= "0000";        -- size/status
406
          writeContentData(15 downto 12) <= "1000";       -- ttype
407
          writeContentData(31 downto 16) <= baseDeviceId;
408
          crc16Current <= crc16Next;                      -- first frame's CRC
409
          stateRB <= SEND_DONE_1;
410
 
411
        when SEND_DONE_1 =>
412
          byteCnt <= 0;
413
          dataLane <= (others =>(others => '0'));
414
          writeContent_o <= '0';  -- this line is to make sure that the CRC is complete read
415
          crc16Current <= crc16Next;                      -- second frame's CRC
416
          wbAdr_o <= tempAddr;
417
          tempAddr <= tempAddr + 1;
418
          wbStb <= '1';
419
          wbWe_o <= '0';
420
          byteOffset <= pos;
421
          stateRB <= READ_FROM_WB;
422
 
423
        when READ_FROM_WB =>
424
          if wbAck_i = '1' then
425
            timeOutCnt <= (others => '0');          -- reset the time out conter
426
            if wbErr_i = '0' then                   -- check if no error occur
427
              if (byteCnt < numberOfByte - 1) then  -- check if reach the last data byte
428
                byteCnt <= byteCnt + 1;
429
                if (byteCnt + headLen = 80) then    -- when current position in payload is a CRC position 
430
                  dataLane(0) <= crc16Current(15 downto 8);
431
                  dataLane(1) <= crc16Current(7 downto 0);
432
                  dataLane(2) <= wbData_i;
433
                  byteOffset <= 3;
434
                elsif byteOffset < 7 then
435
                  dataLane(byteOffset) <= wbData_i;
436
                  byteOffset <= byteOffset + 1;
437
                else                                 -- dataLane vector is ready to send to fifo
438
                  dataLane(7) <= wbData_i;
439
                  byteOffset <= 0;                   -- Here, sets byteOffset for other response
440
                  stateRB <= SEND_TO_FIFO;
441
                  nextStateRB <= READ_FROM_WB;       -- 
442
                end if;
443
              else                                   -- get last data from Wishbone
444
                wbStb <= '0';
445
                byteCnt <= 0;                        -- Here, using byteCnt and reset it for other response
446
                dataLane(byteOffset) <= wbData_i;
447
                stateRB <= APPEND_CRC_AND_SEND;
448
                if byteOffset < 7 then               -- checking for CRC appending position
449
                  byteOffset <= byteOffset + 1;
450
                else
451
                  byteOffset <= 0;
452
                end if;
453
              end if;
454
 
455
            -- when Wishbone error occur
456
            else
457
              wbStb <= '0';
458
              wbWe_o <= '0';
459
              byteOffset <= 0;
460
              writeFrameAbort_o <= '1';               -- over write the frame with an error frame
461
              errorCode <= wbData_i;
462
              stateRB <= SEND_ERROR;
463
            end if;
464
          else                                       -- when no acknowledge received
465
            if timeOutCnt(13) = '1' then  -- when waiting more than 1 ms for response from the device
466
              wbStb <= '0';
467
              wbWe_o <= '0';
468
              byteOffset <= 0;
469
              writeFrameAbort_o <= '1';              -- over write the frame with an error frame
470
              errorCode <= BERR_NOT_RESPONSE;
471
              stateRB <= SEND_ERROR;
472
            else
473
              timeOutCnt <= timeOutCnt + 1;
474
            end if;
475
          end if;
476
 
477
        -- appending CRC and write to the fifo when frame is shorter then 80 bytes
478
        when APPEND_CRC_AND_SEND =>
479
          writeContent_o <= '0';
480
          byteCnt <= byteCnt + 1;
481
          -- check if frame is shorter than 80 bytes
482
          if (numberOfByte < 65) then
483
            -- Yes, frame is shorter then 80 bytes
484
            if byteCnt = 0 then
485
              -- first write the current double word to the fifo
486
              -- then put the CRC in the next double word
487
              byteOffset <= 0;
488
              stateRB <= SEND_TO_FIFO;
489
              nextStateRB <= APPEND_CRC_AND_SEND;
490
            elsif byteCnt = 1 then
491
              -- append the CRC
492
              writeContent_o <= '1';
493
              writeContentData <= crc16Current & X"0000";
494
            else
495
              stateRB <= SEND_FRAME;      -- store in the switch buffer
496
            end if;
497
          else
498
            --No, appending CRC and write to the fifo when frame is longer then 80 bytes
499
            if byteCnt = 0 then
500
              -- check if the last byte was placed in the second half of the double word,
501
              -- in that case write the first word to the fifo.
502
              writeContentData <= dataLane(0) & dataLane(1) & dataLane(2) & dataLane(3);
503
            elsif byteCnt = 1 then
504
              crc16Current <= crc16Temp; -- calcylate the crc for the 16 most significant bits 
505
            elsif byteCnt = 2 then
506
              writeContent_o <= '1';
507
              writeContentData <= dataLane(0) & dataLane(1) & crc16Current;
508
            else
509
              stateRB <= SEND_FRAME;      -- store in the switch buffer
510
            end if;
511
          end if;
512
 
513
 
514
        when SEND_TO_FIFO =>
515
          if byteOffset = 0 then       -- using byteOffset as a counter
516
            byteOffset <= 1;
517
            writeContent_o <= '1';
518
            writeContentData <= dataLane(0) & dataLane(1) & dataLane(2) & dataLane(3);
519
          elsif byteOffset = 1 then    -- using byteOffset as a counter
520
            byteOffset <= 2;
521
            writeContent_o <= '0';
522
            crc16Current <= crc16Next; -- calcylate the crc
523
          elsif byteOffset = 2 then
524
            byteOffset <= 3;
525
            writeContent_o <= '1';
526
            writeContentData <= dataLane(4) & dataLane(5) & dataLane(6) & dataLane(7);
527
          elsif byteOffset = 3 then
528
            crc16Current <= crc16Next; -- calcylate the crc
529
            writeContent_o <= '0';
530
            byteOffset <= 0;
531
            stateRB <= nextStateRB;
532
            dataLane <= (others =>(others => '0'));
533
          end if;
534
 
535
        when READ_FROM_FIFO =>
536
          if (endianMsb = '1') then
537
            if (readContentEnd_i = '0') then
538
            endianMsb <= '0';
539
            dataLane(0 to 3) <= (readContentData_i(31 downto 24), readContentData_i(23 downto 16),
540
                                 readContentData_i(15 downto 8), readContentData_i(7 downto 0));
541
            else
542
              wbStb <= '0';
543
              wbWe_o <= '0';
544
              byteOffset <= 0;
545
              readContent_o <= '0';
546
              writeFrameAbort_o <= '1';               -- over write the frame with an error frame
547
              errorCode <= BERR_FRAME_SIZE;
548
              stateRB <= SEND_ERROR;
549
--              stateRB <= IDLE;         
550
            end if;
551
          else
552
            endianMsb <= '1';
553
            readContent_o <= '0';
554
            dataLane(4 to 7) <= (readContentData_i(31 downto 24), readContentData_i(23 downto 16),
555
                                 readContentData_i(15 downto 8), readContentData_i(7 downto 0));
556
            if ready = '1' then
557
              stateRB <= nextStateRB;
558
            else
559
              stateRB <= WRITE_TO_WB;
560
            end if;
561
          end if;
562
 
563
        when WRITE_TO_WB =>
564
          if wbStb = '0' then
565
            wbStb <= '1';
566
            wbWe_o <= '1';
567
            byteCnt <= 1;
568
            byteOffset <= byteOffset + 1;   -- increase number of counted byte
569
            tempAddr <= tempAddr + 1;       -- increase the memory sddress address
570
            wbAdr_o <= tempAddr;
571
            wbData_o <= dataLane(byteOffset);
572
          else
573
            if wbAck_i = '1' then
574
              timeOutCnt <= (others => '0');   -- reset the time out conter
575
              if wbErr_i = '0' then            -- check the peripherals error signal
576
                if byteCnt < numberOfByte then
577
                  tempAddr <= tempAddr + 1; -- increase the memory sddress address 
578
                  wbAdr_o <= tempAddr;
579
                  wbData_o <= dataLane(byteOffset);
580
                  byteCnt <= byteCnt + 1;   -- increase number of counted byte 
581
                  if byteOffset < 7 then
582
                    if (byteCnt + headLen = 79) then  -- check for the CRC-byte position 80 in the frame  
583
                      byteOffset <= byteOffset + 3;
584
                    else
585
                      byteOffset <= byteOffset + 1;
586
                    end if;
587
                  else
588
                    if (byteCnt + headLen = 79) then  -- check for the CRC-byte position 80 in the frame  
589
                      byteOffset <= 2;
590
                    else
591
                      byteOffset <= 0;
592
                    end if;
593
                    if byteCnt < numberOfByte - 1 then
594
                      readContent_o <= '1';
595
                      stateRB <= READ_FROM_FIFO;
596
                    end if;
597
                  end if;
598
                else                        -- no more data to send to the peripherals
599
                  wbStb <= '0';
600
                  wbWe_o <= '0';
601
                  ready <= '1';
602
                  stateRB <= SEND_FRAME;
603
                end if;
604
              else                          -- if the peripheral generates an error, send an error Response
605
                wbStb <= '0';
606
                wbWe_o <= '0';
607
                byteOffset <= 0;
608
                writeFrameAbort_o <= '1';               -- over write the frame with an error frame
609
                errorCode <= wbData_i;
610
                stateRB <= SEND_ERROR;
611
              end if;
612
            else
613
--              if readContentEnd_i = '1' then  -- when unvalid data in the switch buffer
614
--                wbStb <= '0';
615
--                wbWe_o <= '0';
616
--                readFrame_o <= '1';
617
--                byteOffset <= 0;
618
--                writeFrameAbort_o <= '1';               -- over write the frame with an error frame
619
--                errorCode <= BERR_FRAME_SIZE; -- more data content is expected, Frame size error
620
--                stateRB <= SEND_ERROR;
621
--              else
622
                if timeOutCnt(13) = '1' then  -- when waiting more than 1 ms for response from the device
623
                  wbStb <= '0';
624
                  wbWe_o <= '0';
625
                  readFrame_o <= '1';
626
                  byteOffset <= 0;
627
                  writeFrameAbort_o <= '1';               -- over write the frame with an error frame
628
                  errorCode <= BERR_NOT_RESPONSE;
629
                  stateRB <= SEND_ERROR;
630
                else
631
                  timeOutCnt <= timeOutCnt + 1;
632
                end if;
633
--              end if;                  
634
            end if;
635
          end if;
636
 
637
        when SEND_ERROR =>  -- Generate a Response Class, an error pakage ftype=13, ttype=8, status="1111"
638
          readFrame_o <= '0';
639
          writeFrameAbort_o <= '0';
640
          byteOffset <= byteOffset + 1;
641
          if byteOffset = 0 then
642
            writeContent_o <= '1';                   -- start write to the buffer
643
            crc16Current <= (others => '1');
644
            writeContentData <= "00000000" & "00" & "01" & "1101" & sourceId;
645
          elsif byteOffset = 1 then
646
            writeContentData <= baseDeviceId & "1000" & "1111" & tid;
647
            crc16Current <= crc16Next;               -- first frame's CRC
648
          elsif byteOffset = 2 then
649
            writeContentData <= errorCode & x"000000";
650
            crc16Current <= crc16Next;               -- second frame's CRC
651
          elsif byteOffset = 3 then
652
            writeContentData <= x"00000000";
653
            crc16Current <= crc16Next;               -- third frame's CRC
654
          elsif byteOffset = 4 then
655
            writeContentData <= crc16Next & X"0000"; -- write the CRC field
656
          else
657
            writeContent_o <= '0';
658
            writeFrame_o <= '1';
659
            readFrame_o <= '1';
660
            stateRB <= WAIT_IDLE;
661
          end if;
662
 
663
        when SEND_FRAME =>
664
          if (ftype = FTYPE_WRITE_CLASS) and (ttype = TTYPE_NWRITE_TRANSACTION) and (tt = "01") then    -- check what type of pakage we got
665
            readFrame_o <= '1';
666
          elsif (ftype = FTYPE_REQUEST_CLASS) and (ttype = TTYPE_NREAD_TRANSACTION) and (tt = "01") then -- write payload to the buffer is done
667
            readFrame_o <= '1';
668
            writeFrame_o <= '1';
669
          else                         -- the operation was not valid 
670
            readFrame_o <= '1';
671
            writeFrame_o <= '1';
672
          end if;
673
            stateRB <= WAIT_IDLE;
674
 
675
        when WAIT_IDLE =>
676
          readFrame_o <= '0';
677
          writeFrame_o <= '0';
678
          readContent_o <= '0';   -- this line is to make sure that the CRC is complete read
679
          stateRB <= IDLE;
680
 
681
        when others =>
682
          stateRB <= IDLE;
683
 
684
      end case;
685
 
686
    end if;
687
 
688
  end process;
689
 
690
  -----------------------------------------------------------------------------
691
  -- Configuration memory.
692
  -----------------------------------------------------------------------------
693
  memoryConfig : process(clk, areset_n)
694
  begin
695
    if (areset_n = '0') then
696
      configDataRead <= (others => '0');
697
      baseDeviceId <= DEFAULT_BASE_DEVICE_ID;
698
      componentTag <= (others => '0');
699
      hostBaseDeviceIdLocked <= '0';
700
      hostBaseDeviceId <= (others => '1');
701
    elsif (clk'event and clk = '1') then
702
 
703
      if (configEnable = '1') then
704
        case (configAddress) is
705
          when x"000000" =>
706
            -- Device Identity CAR. Read-only.
707
            configDataRead(31 downto 16) <= DEVICE_IDENTITY;
708
            configDataRead(15 downto 0) <= DEVICE_VENDOR_IDENTITY;
709
          when x"000004" =>
710
            -- Device Information CAR. Read-only.
711
            configDataRead(31 downto 0) <= DEVICE_REV;
712
          when x"000008" =>
713
            -- Assembly Identity CAR. Read-only.
714
            configDataRead(31 downto 16) <= ASSY_IDENTITY;
715
            configDataRead(15 downto 0) <= ASSY_VENDOR_IDENTITY;
716
          when x"00000c" =>
717
            -- Assembly Informaiton CAR. Read-only.
718
            -- Extended features pointer to "0000".
719
            configDataRead(31 downto 16) <= ASSY_REV;
720
            configDataRead(15 downto 0) <= x"0000";
721
          when x"000010" =>
722
            -- Processing Element Features CAR. Read-only.
723
            -- Bridge(31), Memory(30), Processor(29), Switch(28).
724
            configDataRead(31) <= '1';
725
            configDataRead(30 downto 4) <= (others => '0');
726
            configDataRead(3) <= '1';            -- support 16 bits common transport large system
727
            configDataRead(2 downto 0) <= "001"; -- support 34 bits address
728
          when x"000018" =>
729
            -- Source Operations CAR. Read-only.
730
            configDataRead(31 downto 0) <= (others => '0');
731
          when x"00001C" =>
732
            -- Destination Operations CAR. Read-only.
733
            configDataRead(31 downto 16) <= (others => '0');
734
            configDataRead(15) <= '1';
735
            configDataRead(14) <= '1';
736
            configDataRead(13 downto 0) <= (others => '0');
737
          when x"00004C" =>
738
            -- Processing Element Logical Layer Control CSR.
739
            configDataRead(31 downto 3) <= (others => '0');
740
            configDataRead(2 downto 0) <= "001"; -- support 34 bits address
741
          when x"000060" =>
742
            -- Base Device ID CSR.
743
            -- Only valid for end point devices.
744
            if (configWrite = '1') then
745
              baseDeviceId <= configDataWrite(15 downto 0);
746
            else
747
              configDataRead(15 downto 0) <= baseDeviceId;
748
            end if;
749
          when x"000068" =>
750
            -- Host Base Device ID Lock CSR.
751
            if (configWrite = '1') then
752
              -- Check if this field has been written before.
753
              if (hostBaseDeviceIdLocked = '0') then
754
                -- The field has not been written.
755
                -- Lock the field and set the host base device id.
756
                hostBaseDeviceIdLocked <= '1';
757
                hostBaseDeviceId <= configDataWrite(15 downto 0);
758
              else
759
                -- The field has been written.
760
                -- Check if the written data is the same as the stored.
761
                if (hostBaseDeviceId = configDataWrite(15 downto 0)) then
762
                  -- Same as stored, reset the value to its initial value.
763
                  hostBaseDeviceIdLocked <= '0';
764
                  hostBaseDeviceId <= (others => '1');
765
                else
766
                  -- Not writing the same as the stored value.
767
                  -- Ignore the write.
768
                end if;
769
              end if;
770
            else
771
              configDataRead(31 downto 16) <= (others => '0');
772
              configDataRead(15 downto 0) <= hostBaseDeviceId;
773
            end if;
774
          when x"00006C" =>
775
            -- Component TAG CSR.
776
            if (configWrite = '1') then
777
              componentTag <= configDataWrite;
778
            else
779
              configDataRead <= componentTag;
780
            end if;
781
 
782
          when others =>
783
            configDataRead <= (others => '0');
784
        end case;
785
      else
786
        -- Config memory not enabled.
787
      end if;
788
    end if;
789
  end process;
790
 
791
  -----------------------------------------------------------------------------
792
  -- findInPayload
793
  -- find out number of the bytes and first byte's position in the payload.
794
  -----------------------------------------------------------------------------
795
  findInPayload: process(wdptr, size)
796
  begin
797
    case size is
798
      when "0000" =>
799
        reserved <= '0';
800
        numberOfByte <= 1;
801
        if wdptr = '1' then
802
          pos <= 4;
803
        else
804
          pos <= 0;
805
        end if;
806
      when "0001" =>
807
        reserved <= '0';
808
        numberOfByte <= 1;
809
        if wdptr = '1' then
810
          pos <= 5;
811
        else
812
          pos <= 1;
813
        end if;
814
      when "0010" =>
815
        reserved <= '0';
816
        numberOfByte <= 1;
817
        if wdptr = '1' then
818
          pos <= 6;
819
        else
820
          pos <= 2;
821
        end if;
822
      when "0011" =>
823
        reserved <= '0';
824
        numberOfByte <= 1;
825
        if wdptr = '1' then
826
          pos <= 7;
827
        else
828
          pos <= 3;
829
        end if;
830
      when "0100" =>
831
        reserved <= '0';
832
        numberOfByte <= 2;
833
        if wdptr = '1' then
834
          pos <= 4;
835
        else
836
          pos <= 0;
837
        end if;
838
      when "0101" =>
839
        reserved <= '0';
840
        numberOfByte <= 3;
841
        if wdptr = '1' then
842
          pos <= 5;
843
        else
844
          pos <= 0;
845
        end if;
846
      when "0110" =>
847
        reserved <= '0';
848
        numberOfByte <= 2;
849
        if wdptr = '1' then
850
          pos <= 6;
851
        else
852
          pos <= 2;
853
        end if;
854
      when "0111" =>
855
        reserved <= '0';
856
        numberOfByte <= 5;
857
        if wdptr = '1' then
858
          pos <= 3;
859
        else
860
          pos <= 0;
861
        end if;
862
      when "1000" =>
863
        reserved <= '0';
864
        numberOfByte <= 4;
865
        if wdptr = '1' then
866
          pos <= 4;
867
        else
868
          pos <= 0;
869
        end if;
870
      when "1001" =>
871
        reserved <= '0';
872
        numberOfByte <= 6;
873
        if wdptr = '1' then
874
          pos <= 2;
875
        else
876
          pos <= 0;
877
        end if;
878
      when "1010" =>
879
        reserved <= '0';
880
        numberOfByte <= 7;
881
        if wdptr = '1' then
882
          pos <= 1;
883
        else
884
          pos <= 0;
885
        end if;
886
      when "1011" =>
887
        reserved <= '0';
888
        if wdptr = '1' then
889
          numberOfByte <= 16;
890
        else
891
          numberOfByte <= 8;
892
        end if;
893
        pos <= 0;
894
      when "1100" =>
895
        reserved <= '0';
896
        if wdptr = '1' then
897
          numberOfByte <= 64;
898
        else
899
          numberOfByte <= 32;
900
        end if;
901
        pos <= 0;
902
      when "1101" =>
903
        if wdptr = '1' then
904
          reserved <= '0';
905
          numberOfByte <= 128;
906
        else
907
          reserved <= '1';
908
          numberOfByte <= 96;
909
        end if;
910
        pos <= 0;
911
      when "1110" =>
912
        if wdptr = '1' then
913
          numberOfByte <= 192;
914
        else
915
          numberOfByte <= 160;
916
        end if;
917
        reserved <= '1';
918
        pos <= 0;
919
      when "1111" =>
920
        if wdptr = '1' then
921
          reserved <= '0';
922
          numberOfByte <= 256;
923
        else
924
          reserved <= '1';
925
          numberOfByte <= 224;
926
        end if;
927
        pos <= 0;
928
      when others =>
929
        reserved <= '1';
930
        numberOfByte <= 0;
931
        pos <= 0;
932
    end case;
933
  end process;
934
 
935
end architecture;

powered by: WebSVN 2.1.0

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