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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_sdlc_if.vhd] - Blame information for rev 280

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

Line No. Rev Author Line
1 192 jshamlet
-- Copyright (c)2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
--
24
-- VHDL Units :  o8_sdlc_if
25
-- Description:  Provides a full memory-mapped SDLC stack with automatic CRC16
26 273 jshamlet
--                Checksum insertion and integrity checking. Note that this
27
--                entity ONLY provides packet framing and checksum calculation.
28 192 jshamlet
--
29
-- Transmit Memory Map
30
-- "0_0000_0000" (0x000) TX Buffer START
31
-- "0_1111_1101" (0x0FD) TX Buffer END
32
-- "0_1111_1110" (0x0FE) Clock Status*
33
-- "0_1111_1111" (0x0FF) TX Length / Status**
34
--
35
-- Receive Memory Map
36
-- "1_0000_0000" (0x100) RX Buffer START
37 196 jshamlet
-- "1_1111_1101" (0x1FD) RX Buffer END
38 201 jshamlet
-- "1_1111_1110" (0x0FE) RX Checksum Status***
39
-- "1_1111_1111" (0x1FF) RX Length   Status****
40 192 jshamlet
--
41 201 jshamlet
-- *    Address 0xFE reports the SDLC bit clock status and updates on changes.
42
--      1) If BClk_Okay = '0' (Bitclock is NOT present), the field will report
43
--          0x00. Otherwise, it will report 0xFF if the bitclock is present.
44
--      2) Writing any value to the register will cause the controller to
45
--         silently reset the clock status without causing an interrupt.
46 199 jshamlet
--
47 201 jshamlet
-- **   This location serves as the control/status register for transmit
48
--      1) Writing a value between 1 and 253 will trigger the transmit engine,
49
--          using the write value as the packet length.
50
--      2) Values 0x00, 0xFE, or 0xFF are invalid, and will be ignored.
51
--      3) This value will change from the user written value to 0xFF once the
52
--          packet is transmitted to indicate the transmission is complete.
53 199 jshamlet
--
54 201 jshamlet
-- ***  This location serves as the status register for receive checksum test
55
--      1) A value of 0x00 indicates the CRC did NOT match, while a value
56
--         of 0xFF indicates that the recieved CRC matches the calculated CRC.
57
--
58
-- **** This location serves as the status register for the receive
59
--      1) This value is only updated on reception of a full frame, indicated
60
--          by a start followed by a stop flag. Incomplete frames are ignored.
61 204 jshamlet
--      2) If too many bytes are received (buffer overflow), a value of
62 201 jshamlet
--          ERR_LENGTH is written.
63 224 jshamlet
--
64
-- Revision History
65
-- Author          Date     Change
66
------------------ -------- ---------------------------------------------------
67 278 jshamlet
-- Seth Henry      12/09/20 Created from merged sub-entities into flat file
68 192 jshamlet
 
69
library ieee;
70
  use ieee.std_logic_1164.all;
71
  use ieee.std_logic_unsigned.all;
72
  use ieee.std_logic_arith.all;
73 278 jshamlet
  use ieee.std_logic_misc.all;
74 192 jshamlet
 
75
library work;
76
  use work.open8_pkg.all;
77
 
78
entity o8_sdlc_if is
79
generic(
80
  Poly_Init                  : std_logic_vector(15 downto 0) := x"0000";
81
  Set_As_Master              : boolean := true;
82
  Clock_Offset               : integer := 6;
83 224 jshamlet
  BitClock_Frequency         : real := 500000.0;
84
  Clock_Frequency            : real := 100000000.0;
85 192 jshamlet
  Address                    : ADDRESS_TYPE
86
);
87
port(
88 223 jshamlet
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
89 244 jshamlet
  Write_Qual                 : in  std_logic := '1';
90 192 jshamlet
  Rd_Data                    : out DATA_TYPE;
91 263 jshamlet
  TX_Interrupt               : out std_logic;
92
  RX_Interrupt               : out std_logic;
93 192 jshamlet
  -- Serial IO
94
  SDLC_In                    : in  std_logic;
95
  SDLC_SClk                  : in  std_logic;
96
  SDLC_MClk                  : out std_logic;
97
  SDLC_Out                   : out std_logic
98
);
99
end entity;
100
 
101
architecture behave of o8_sdlc_if is
102
 
103 278 jshamlet
  -- convenient subtypes & constants
104
  subtype CRC_TYPE           is std_logic_vector(15 downto 0);
105
 
106
  -- Bus interface
107 224 jshamlet
  alias Clock                is Open8_Bus.Clock;
108
  alias Reset                is Open8_Bus.Reset;
109
 
110 192 jshamlet
  constant Base_Addr         : std_logic_vector(15 downto 9)
111
                               := Address(15 downto 9);
112
 
113 223 jshamlet
  alias CPU_Upper_Addr       is Open8_Bus.Address(15 downto 9);
114 205 jshamlet
  signal Base_Addr_Match     : std_logic := '0';
115 192 jshamlet
 
116 244 jshamlet
  alias  DP_A_Addr           is Open8_Bus.Address(8 downto 0);
117 205 jshamlet
  signal DP_A_Wr_En          : std_logic := '0';
118 223 jshamlet
  alias  DP_A_Wr_Data        is Open8_Bus.Wr_Data;
119 244 jshamlet
  signal DP_A_Rd_En_d        : std_logic := '0';
120
  signal DP_A_Rd_En_q        : std_logic := '0';
121 205 jshamlet
  signal DP_A_Rd_Data        : DATA_TYPE := OPEN8_NULLBUS;
122 192 jshamlet
 
123 196 jshamlet
  constant Reg_Sub_Addr      : std_logic_vector(8 downto 1) := x"7F";
124 223 jshamlet
  alias Reg_Upper_Addr       is Open8_Bus.Address(8 downto 1);
125
  alias Reg_Lower_Addr       is Open8_Bus.Address(0);
126 196 jshamlet
 
127
  signal Reg_Addr            : std_logic_vector(8 downto 1) := (others => '0');
128 278 jshamlet
  signal Reg_Sel             : std_logic := '0';
129
  signal Reg_Wr_En_d         : std_logic := '0';
130
  signal Reg_Wr_En_q         : std_logic := '0';
131
  signal TX_Ctl_Clk          : std_logic := '0';
132
  signal TX_Ctl_Len          : std_logic := '0';
133 196 jshamlet
 
134 278 jshamlet
  -- Dual-port memory
135 280 jshamlet
  signal DP_B_Addr           : std_logic_vector(8 downto 0);
136
  signal DP_B_Wr_Data        : DATA_TYPE;
137
  signal DP_B_Wr_En          : std_logic;
138
  signal DP_B_Rd_Data        : DATA_TYPE;
139 192 jshamlet
 
140 278 jshamlet
  -- Internal definitions
141
  constant SDLC_Flag         : DATA_TYPE := x"7E";
142 202 jshamlet
 
143 278 jshamlet
  constant CK_REGISTER       : DATA_TYPE := x"FE";
144
  constant TX_REGISTER       : DATA_TYPE := x"FF";
145
  constant CS_REGISTER       : DATA_TYPE := x"FE";
146
  constant RX_REGISTER       : DATA_TYPE := x"FF";
147 192 jshamlet
 
148 278 jshamlet
  constant TX_RESERVED_LOW   : integer := 0;
149
  constant TX_RESERVED_HIGH  : integer := 254;
150 192 jshamlet
 
151 278 jshamlet
  constant FLAG_DONE         : DATA_TYPE := x"FF";
152 192 jshamlet
 
153 278 jshamlet
  constant ERR_LENGTH        : DATA_TYPE := x"00";
154 192 jshamlet
 
155 278 jshamlet
  -- RAM Arbitration logic
156
  type DP_ARB_STATES is (PAUSE, IDLE,
157
                         PORT0_AD, PORT0_WR, PORT0_RD0, PORT0_RD1,
158
                         PORT1_AD, PORT1_WR, PORT1_RD0, PORT1_RD1  );
159
  signal DP_Arb_State        : DP_ARB_STATES := IDLE;
160
  signal DP_Last_Port        : std_logic := '0';
161 192 jshamlet
 
162 278 jshamlet
  signal DP_Port0_Addr       : DATA_TYPE := x"00";
163
  signal DP_Port0_RWn        : std_logic := '0';
164
  signal DP_Port0_WrData     : DATA_TYPE := x"00";
165
  signal DP_Port0_RdData     : DATA_TYPE := x"00";
166
  signal DP_Port0_Req        : std_logic := '0';
167
  signal DP_Port0_Ack        : std_logic := '0';
168 202 jshamlet
 
169 278 jshamlet
  signal DP_Port1_Addr       : DATA_TYPE := x"00";
170
  signal DP_Port1_RWn        : std_logic := '0';
171
  signal DP_Port1_WrData     : DATA_TYPE := x"00";
172
  signal DP_Port1_RdData     : DATA_TYPE := x"00";
173
  signal DP_Port1_Req        : std_logic := '0';
174
  signal DP_Port1_Ack        : std_logic := '0';
175
 
176
-- Clock generation
177
  constant DLY_VAL           : integer := integer(Clock_Frequency / (2.0 * BitClock_Frequency) );
178
  constant DLY_WDT           : integer := ceil_log2(DLY_VAL - 1);
179
  constant DLY_VEC           : std_logic_vector :=
180
                               conv_std_logic_vector( DLY_VAL - 1, DLY_WDT);
181
  signal BClk_Cntr           : std_logic_vector( DLY_WDT - 1 downto 0 ) := (others => '0');
182
 
183
  signal BClk_Adv            : std_logic := '0';
184
  signal BClk_Accum          : std_logic_vector(31 downto 0) := (others => '0');
185
  signal BClk_Div            : std_logic := '0';
186
  signal BClk_Okay_SR        : std_logic_vector(3 downto 0)  := (others => '0');
187
 
188
 
189
  signal BClk_SR             : std_logic_vector(2 downto 0)  := (others => '0');
190
 
191
  constant CLK_RATIO_R       : real := Clock_Frequency / (1.0 * BitClock_Frequency);
192
  constant CLK_DEVIATION_5P  : real := CLK_RATIO_R * 0.05;
193
  constant CLK_RATIO_ADJ_R   : real := CLK_RATIO_R + CLK_DEVIATION_5P;
194
  constant CLK_RATIO_ADJ_I   : integer := integer(CLK_RATIO_ADJ_R);
195
 
196
  constant Threshold_bits    : integer := ceil_log2(CLK_RATIO_ADJ_I);
197
  constant THRESHOLD         : std_logic_vector(Threshold_bits - 1 downto 0) :=
198
                        conv_std_logic_vector(CLK_RATIO_ADJ_I,Threshold_bits);
199
 
200
  signal RE_Threshold_Ctr    : std_logic_vector(Threshold_Bits - 1 downto 0) :=
201
                                (others => '0');
202
  signal FE_Threshold_Ctr    : std_logic_vector(Threshold_Bits - 1 downto 0) :=
203
                                (others => '0');
204
 
205
  signal Ref_In_SR           : std_logic_vector(2 downto 0) := (others => '0');
206
  alias  Ref_In_q1           is Ref_In_SR(1);
207
  alias  Ref_In_q2           is Ref_In_SR(2);
208
  signal Ref_In_RE           : std_logic := '0';
209
  signal Ref_In_FE           : std_logic := '0';
210
 
211
  signal BClk_RE             : std_logic := '0';
212
  signal BClk_FE             : std_logic := '0';
213
  signal BClk_Okay           : std_logic := '0';
214
 
215
-- Packet Transmit state logic
216
  type TX_FSM_STATES is ( INIT_FLAG,
217
                          WR_CLOCK_STATE, WAIT_FOR_CLOCK,
218
                          WAIT_FOR_UPDATE,
219
                          RD_TX_REGISTER, TX_INIT,
220
                          TX_START_FLAG, TX_WAIT_START_FLAG,
221
                          TX_MESG_DATA, TX_ADV_ADDR, TX_WAIT_MESG_DATA,
222
                          TX_CRC_LB_WR, TX_CRC_LB_WAIT,
223
                          TX_CRC_UB_WR, TX_CRC_UB_WAIT,
224
                          TX_STOP_FLAG, TX_WAIT_STOP_FLAG, TX_SET_FLAG );
225
 
226
  signal TX_FSM_State        : TX_FSM_STATES := WR_CLOCK_STATE;
227
  signal TX_Length           : DATA_TYPE := x"00";
228
 
229
  signal BClk_q1, BClk_CoS   : std_logic := '0';
230
  signal TX_Int_pend         : std_logic := '0';
231
 
232
  signal TX_Wr_En            : std_logic := '0';
233
  signal TX_Wr_Flag          : std_logic := '0';
234
  signal TX_Wr_Data          : DATA_TYPE := x"00";
235
  signal TX_Req_Next         : std_logic := '0';
236
 
237
  signal TX_CRC_Clr          : std_logic := '0';
238
  signal TX_CRC_En           : std_logic := '0';
239
  signal TX_CRC_Data         : CRC_TYPE  := x"0000";
240
  signal TX_CRC_Valid        : std_logic := '0';
241
 
242
  alias  TX_CRC_Data_LB      is TX_CRC_Data(7 downto 0);
243
  alias  TX_CRC_Data_UB      is TX_CRC_Data(15 downto 8);
244
 
245
  signal TX_Arm              : std_logic := '0';
246
  signal TX_Flag             : std_logic := '0';
247
  signal TX_Buffer           : std_logic_vector(8 downto 0) := (others => '0');
248
  alias  TX_Buffer_Flag      is TX_Buffer(8);
249
  alias  TX_Buffer_Data      is TX_Buffer(7 downto 0);
250
 
251
-- SDLC transmitter
252
  type TX_STATES is (INIT, IDLE, XMIT, SPACE, TERM, LD_NEXT);
253
  signal TX_State            : TX_STATES := INIT;
254
 
255
  signal TX_ShftReg          : DATA_TYPE := (others => '0');
256
  signal TX_Next             : std_logic := '0';
257
  signal TX_BitStuff         : std_logic_vector(4 downto 0) := (others => '0');
258
  signal TX_BitCntr          : std_logic_vector(3 downto 0) := (others => '0');
259
  alias  TX_BitSel           is TX_BitCntr(2 downto 0);
260
  alias  TX_Term             is TX_BitCntr(3);
261
 
262
-- SDLC receiver
263
  signal RX_LatchEn_SR       : std_logic_vector(Clock_Offset downto 0) := (others => '0');
264
  alias  RX_LatchEn_M        is RX_LatchEn_SR(Clock_Offset);
265
  alias  RX_LatchEn_S        is BClk_RE;
266
  signal RX_LatchEn          : std_logic := '0';
267
 
268
  signal RX_Serial_SR        : std_logic_vector(1 downto 0) := (others => '0');
269
  alias  RX_Serial           is RX_Serial_SR(1);
270
 
271
  type RX_STATES is (INIT, IDLE, RCV_DATA, SKIP_ZERO, WRITE_DATA);
272
  signal RX_State            : RX_STATES := INIT;
273
  signal RX_Buffer           : DATA_TYPE := x"00";
274
  signal RX_BitStuff_SR      : std_logic_vector(4 downto 0) := (others => '0');
275
  signal RX_BitCntr          : std_logic_vector(3 downto 0) := (others => '0');
276
  alias  RX_BitSel           is RX_BitCntr(2 downto 0);
277
  alias  RX_Term             is RX_BitCntr(3);
278
 
279
  signal RX_Flag_SR          : DATA_TYPE := x"00";
280
 
281
  signal RX_Idle_Cntr        : std_logic_vector(2 downto 0) := (others => '0');
282
 
283
  signal RX_Valid            : std_logic := '0';
284
  signal RX_Flag             : std_logic := '0';
285
  signal RX_Data             : DATA_TYPE := x"00";
286
  signal RX_Idle             : std_logic := '0';
287
 
288
-- Packet detection logic
289
  type PACKET_STATES is (IDLE, FRAME_START, FRAME_DATA, FRAME_STOP );
290
  signal Pkt_State           : PACKET_STATES := IDLE;
291
  signal First_Byte          : std_logic := '0';
292
 
293
  signal RX_Frame_Start      : std_logic := '0';
294
  signal RX_Frame_Stop       : std_logic := '0';
295
  signal RX_Frame_Valid      : std_logic := '0';
296
  signal RX_Frame_Data       : DATA_TYPE := x"00";
297
 
298
-- Receive data CRC calculation
299
  signal RX_CRC_Valid        : std_logic := '0';
300
  signal RX_CRC_Data         : CRC_TYPE  := x"0000";
301
 
302
  type CRC_HISTORY is array(0 to 2) of CRC_TYPE;
303
  signal RX_CRC_Hist         : CRC_HISTORY := (x"0000",x"0000",x"0000");
304
  alias  RX_CRC_Calc         is RX_CRC_Hist(2);
305
 
306
  signal RX_CRC_Rcvd         : CRC_TYPE  := x"0000";
307
  alias  RX_CRC_Rcvd_LB      is RX_CRC_Rcvd(7 downto 0);
308
  alias  RX_CRC_Rcvd_UB      is RX_CRC_Rcvd(15 downto 8);
309
 
310
-- Packet receive state logic
311
  type RX_FSM_STATES is ( WAIT_FOR_CLOCK, WAIT_FOR_FLAG,
312
                          RX_MESG_DATA, RX_WR_DATA,
313
                          RX_CRC_LB_RD, RX_CRC_UB_RD,
314
                          RX_WR_CRC, RX_WR_COUNT );
315
 
316
  signal RX_FSM_State        : RX_FSM_STATES := WAIT_FOR_CLOCK;
317
 
318
  signal RX_Length           : DATA_TYPE := x"00";
319
 
320 192 jshamlet
begin
321 278 jshamlet
 
322
-- ***************************************************************************
323
-- *          Open8 Bus Interface and Control Register Detection             *
324
-- ***************************************************************************
325
 
326
  -- This decode needs to happen immediately, to give the RAM a chance to
327
  --  do the lookup before we have to set Rd_Data
328
  Base_Addr_Match            <= '1' when Base_Addr = CPU_Upper_Addr else '0';
329
  Reg_Wr_En_d                <= Base_Addr_Match and
330
                                Open8_Bus.Wr_En and
331
                                Write_Qual;
332
 
333
  DP_A_Wr_En                 <= Base_Addr_Match and
334
                                Open8_Bus.Wr_En and
335
                                Write_Qual;
336
 
337
  DP_A_Rd_En_d               <= Base_Addr_Match and Open8_Bus.Rd_En;
338
 
339
  CPU_IF_proc: process( Reset, Clock )
340
  begin
341
    if( Reset = Reset_Level )then
342
      Reg_Addr               <= (others => '0');
343
      Reg_Wr_En_q            <= '0';
344
      TX_Ctl_Clk             <= '0';
345
      TX_Ctl_Len             <= '0';
346
      DP_A_Rd_En_q           <= '0';
347
      Rd_Data                <= OPEN8_NULLBUS;
348
    elsif( rising_edge(Clock) )then
349
      Reg_Addr               <= Reg_Upper_Addr;
350
      Reg_Sel                <= Reg_Lower_Addr;
351
      Reg_Wr_En_q            <= Reg_Wr_En_d;
352
 
353
      TX_Ctl_Clk             <= '0';
354
      TX_Ctl_Len             <= '0';
355
      if( Reg_Addr = Reg_Sub_Addr )then
356
        TX_Ctl_Clk           <= Reg_Wr_En_q and not Reg_Sel;
357
        TX_Ctl_Len           <= Reg_Wr_En_q and Reg_Sel;
358
      end if;
359
 
360
      DP_A_Rd_En_q           <= DP_A_Rd_En_d;
361
      Rd_Data                <= OPEN8_NULLBUS;
362
      if( DP_A_Rd_En_q = '1' )then
363
        Rd_Data              <= DP_A_Rd_Data;
364
      end if;
365
    end if;
366
  end process;
367
 
368
-- ***************************************************************************
369
-- *                     Shared Dual-Port Memory                             *
370
-- ***************************************************************************
371
 
372
  U_RAM : entity work.sdlc_dp512b_ram
373
  port map(
374
    clock                    => Clock,
375
    address_a                => DP_A_Addr,
376
    address_b                => DP_B_Addr,
377
    data_a                   => DP_A_Wr_Data,
378
    data_b                   => DP_B_Wr_Data,
379
    wren_a                   => DP_A_Wr_En,
380
    wren_b                   => DP_B_Wr_En,
381
    q_a                      => DP_A_Rd_Data,
382
    q_b                      => DP_B_Rd_Data
383
  );
384
 
385 202 jshamlet
-- ***************************************************************************
386 278 jshamlet
-- *                     Memory Arbitration                                  *
387 202 jshamlet
-- ***************************************************************************
388
 
389 278 jshamlet
  RAM_Arbitration_proc: process( Clock, Reset )
390
  begin
391
    if( Reset = Reset_Level )then
392
      DP_Arb_State           <= IDLE;
393
      DP_Last_Port           <= '0';
394 280 jshamlet
      DP_B_Addr              <= (others => '0');
395
      DP_B_Wr_Data           <= x"00";
396
      DP_B_Wr_En             <= '0';
397 278 jshamlet
      DP_Port0_RdData        <= x"00";
398
      DP_Port0_Ack           <= '0';
399
      DP_Port1_RdData        <= x"00";
400
      DP_Port1_Ack           <= '0';
401
    elsif( rising_edge(Clock) )then
402
      DP_Port0_Ack           <= '0';
403
      DP_Port1_Ack           <= '0';
404 280 jshamlet
      DP_B_Wr_En               <= '0';
405 192 jshamlet
 
406 278 jshamlet
      case( DP_Arb_State )is
407
        when IDLE =>
408
          if( DP_Port0_Req = '1' and (DP_Port1_Req = '0' or DP_Last_Port = '1') )then
409
            DP_Arb_State     <= PORT0_AD;
410
          elsif( DP_Port1_Req = '1' and (DP_Port0_Req = '0' or DP_Last_Port = '0') )then
411
            DP_Arb_State     <= PORT1_AD;
412
          end if;
413 244 jshamlet
 
414 278 jshamlet
        when PORT0_AD =>
415
          DP_Last_Port       <= '0';
416 280 jshamlet
          DP_B_Addr          <= '0' & DP_Port0_Addr;
417
          DP_B_Wr_Data       <= DP_Port0_WrData;
418
          DP_B_Wr_En         <= not DP_Port0_RWn;
419 278 jshamlet
          if( DP_Port0_RWn = '1' )then
420
            DP_Arb_State     <= PORT0_RD0;
421
          else
422
            DP_Port0_Ack     <= '1';
423
            DP_Arb_State     <= PORT0_WR;
424
          end if;
425 244 jshamlet
 
426 278 jshamlet
        when PORT0_WR =>
427
          DP_Arb_State       <= IDLE;
428
 
429
        when PORT0_RD0 =>
430
          DP_Arb_State       <= PORT0_RD1;
431
 
432
        when PORT0_RD1 =>
433
          DP_Port0_Ack       <= '1';
434 280 jshamlet
          DP_Port0_RdData    <= DP_B_Rd_Data;
435 278 jshamlet
          DP_Arb_State       <= PAUSE;
436
 
437
        when PORT1_AD =>
438
          DP_Last_Port       <= '1';
439 280 jshamlet
          DP_B_Addr          <= '1' & DP_Port1_Addr;
440
          DP_B_Wr_Data       <= DP_Port1_WrData;
441
          DP_B_Wr_En         <= not DP_Port1_RWn;
442 278 jshamlet
          if( DP_Port0_RWn = '1' )then
443
            DP_Arb_State     <= PORT1_RD0;
444
          else
445
            DP_Port1_Ack     <= '1';
446
            DP_Arb_State     <= PORT1_WR;
447
          end if;
448
 
449
        when PORT1_WR =>
450
          DP_Arb_State       <= IDLE;
451
 
452
        when PORT1_RD0 =>
453
          DP_Arb_State       <= PORT1_RD1;
454
 
455
        when PORT1_RD1 =>
456
          DP_Port1_Ack       <= '1';
457 280 jshamlet
          DP_Port1_RdData    <= DP_B_Rd_Data;
458 278 jshamlet
          DP_Arb_State       <= PAUSE;
459
 
460
        when PAUSE =>
461
          DP_Arb_State       <= IDLE;
462
 
463
        when others => null;
464
 
465
      end case;
466
    end if;
467
  end process;
468
 
469
-- ****************************************************************************
470
-- * Bit clock generation                                                     *
471
-- ****************************************************************************
472
 
473
Clock_Master: if( Set_As_Master )generate
474
 
475
  Clock_Gen_proc: process( Clock, Reset )
476 192 jshamlet
  begin
477
    if( Reset = Reset_Level )then
478 278 jshamlet
      BClk_Cntr              <= DLY_VEC;
479
      BClk_Adv               <= '0';
480
      BClk_Accum             <= (others => '0');
481
      BClk_Div               <= '0';
482
      BClk_Okay_SR           <= (others => '0');
483
      BClk_RE                <= '0';
484
      BClk_FE                <= '0';
485
      SDLC_MClk              <= '0';
486
    elsif( rising_edge( Clock ) )then
487
      BClk_Cntr              <= BClk_Cntr - 1;
488
      BClk_Adv               <= '0';
489
      if( or_reduce(BClk_Cntr) = '0' )then
490
        BClk_Cntr            <= DLY_VEC;
491
        BClk_Adv             <= '1';
492
        BClk_Okay_SR         <= BClk_Okay_SR(2 downto 0) & '1';
493
      end if;
494
      BClk_Accum             <= BClk_Accum + BClk_Adv;
495
      BClk_Div               <= BClk_Div xor BClk_Adv;
496
      BClk_RE                <= (not BClk_Div) and BClk_Adv;
497
      BClk_FE                <= BClk_Div and BClk_Adv;
498
      SDLC_MClk              <= BClk_Div;
499
    end if;
500
  end process;
501
 
502
  BClk_Okay                  <= BClk_Okay_SR(3);
503
 
504
end generate;
505
 
506
Clock_Slave: if( not Set_As_Master )generate
507
 
508
  Clock_Edge_proc: process( Clock, Reset )
509
  begin
510
    if( Reset = Reset_Level )then
511
      BClk_SR                <= (others => '0');
512
      BClk_FE                <= '0';
513
      BClk_RE                <= '0';
514 192 jshamlet
    elsif( rising_edge(Clock) )then
515 278 jshamlet
      BClk_SR                <= BClk_SR(1 downto 0) & SDLC_SClk;
516
      BClk_FE                <= BClk_SR(2) and (not BClk_SR(1));
517
      BClk_RE                <= (not BClk_SR(2)) and BClk_SR(1);
518
    end if;
519
  end process;
520 192 jshamlet
 
521 278 jshamlet
  SDLC_MClk                  <= '0';
522
 
523
  Clock_Detect_proc: process( Clock, Reset )
524
  begin
525
    if( Reset = Reset_Level )then
526
      Ref_In_SR              <= (others => '0');
527
      Ref_In_RE              <= '0';
528
      Ref_In_FE              <= '0';
529
      RE_Threshold_Ctr       <= (others => '0');
530
      FE_Threshold_Ctr       <= (others => '0');
531
      BClk_Okay              <= '0';
532
 
533
    elsif( rising_edge(Clock) )then
534
      Ref_In_SR              <= Ref_In_SR(1 downto 0) & SDLC_SClk;
535
      Ref_In_RE              <= Ref_In_q1 and (not Ref_In_q2);
536
      Ref_In_FE              <= (not Ref_In_q1) and Ref_In_q2;
537
 
538
      RE_Threshold_Ctr       <= RE_Threshold_Ctr - 1;
539
      if( Ref_In_RE = '1' )then
540
        RE_Threshold_Ctr     <= THRESHOLD;
541
      elsif( or_reduce(RE_Threshold_Ctr) = '0' )then
542
        RE_Threshold_Ctr     <= (others => '0');
543 192 jshamlet
      end if;
544
 
545 278 jshamlet
      FE_Threshold_Ctr       <= FE_Threshold_Ctr - 1;
546
      if( Ref_In_FE = '1' )then
547
        FE_Threshold_Ctr     <= THRESHOLD;
548
      elsif( or_reduce(FE_Threshold_Ctr) = '0' )then
549
        FE_Threshold_Ctr     <= (others => '0');
550 192 jshamlet
      end if;
551 278 jshamlet
 
552
 
553
      BClk_Okay              <= or_reduce(RE_Threshold_Ctr) and
554
                                or_reduce(FE_Threshold_Ctr);
555
 
556 192 jshamlet
    end if;
557
  end process;
558
 
559 278 jshamlet
end generate;
560
 
561 202 jshamlet
-- ***************************************************************************
562 278 jshamlet
-- *                     Serial Transmit Path                                *
563 202 jshamlet
-- ***************************************************************************
564
 
565 278 jshamlet
  TX_Packet_RAM_proc: process( Reset, Clock )
566
  begin
567
    if( Reset = Reset_Level )then
568
      TX_FSM_State           <= INIT_FLAG;
569 192 jshamlet
 
570 278 jshamlet
      DP_Port0_Addr          <= x"00";
571
      DP_Port0_RWn           <= '1';
572
      DP_Port0_WrData        <= x"00";
573
      DP_Port0_Req           <= '0';
574 202 jshamlet
 
575 278 jshamlet
      TX_Length              <= x"00";
576 202 jshamlet
 
577 278 jshamlet
      TX_Wr_En               <= '0';
578
      TX_Wr_Flag             <= '0';
579
      TX_Wr_Data             <= x"00";
580 202 jshamlet
 
581 278 jshamlet
      TX_CRC_Clr             <= '0';
582
      TX_CRC_En              <= '0';
583 192 jshamlet
 
584 278 jshamlet
      BClk_q1                <= '0';
585
      BClk_CoS               <= '0';
586 202 jshamlet
 
587 278 jshamlet
      TX_Int_pend            <= '0';
588
      TX_Interrupt           <= '0';
589 202 jshamlet
 
590 278 jshamlet
    elsif( rising_edge(Clock) )then
591
 
592
      DP_Port0_RWn           <= '1';
593
      DP_Port0_WrData        <= x"00";
594
      DP_Port0_Req           <= '0';
595
 
596
      TX_Wr_En               <= '0';
597
      TX_Wr_Flag             <= '0';
598
      TX_Wr_Data             <= x"00";
599
 
600
      TX_CRC_Clr             <= '0';
601
      TX_CRC_En              <= '0';
602
 
603
      BClk_q1                <= BClk_Okay;
604
      BClk_CoS               <= BClk_q1 xor BClk_Okay;
605
 
606
      TX_Interrupt           <= '0';
607
 
608
      case( TX_FSM_State )is
609
 
610
        when INIT_FLAG =>
611
          DP_Port0_Addr      <= TX_REGISTER;
612
          DP_Port0_Req       <= '1';
613
          DP_Port0_WrData    <= FLAG_DONE;
614
          DP_Port0_RWn       <= '0';
615
          if( DP_Port0_Ack = '1' )then
616
            DP_Port0_Req     <= '0';
617
            TX_FSM_State     <= WR_CLOCK_STATE;
618
          end if;
619
 
620
        when WAIT_FOR_UPDATE =>
621
          if( TX_Ctl_Clk = '1' )then
622
            TX_FSM_State     <= WR_CLOCK_STATE;
623
          end if;
624
          if( TX_Ctl_Len = '1' )then
625
            TX_FSM_State     <= RD_TX_REGISTER;
626
          end if;
627
 
628
        when WR_CLOCK_STATE =>
629
          DP_Port0_Addr      <= CK_REGISTER;
630
          DP_Port0_Req       <= '1';
631
          DP_Port0_WrData    <= (others => BClk_Okay);
632
          DP_Port0_RWn       <= '0';
633
          if( DP_Port0_Ack = '1' )then
634
            TX_Interrupt     <= TX_Int_pend;
635
            TX_Int_pend      <= '0';
636
            DP_Port0_Req     <= '0';
637
            TX_FSM_State     <= WAIT_FOR_CLOCK;
638
          end if;
639
 
640
        when WAIT_FOR_CLOCK =>
641
          if( BClk_Okay = '1' )then
642
            TX_FSM_State     <= WAIT_FOR_UPDATE;
643
          end if;
644
 
645
        when RD_TX_REGISTER =>
646
          DP_Port0_Addr      <= TX_REGISTER;
647
          DP_Port0_Req       <= '1';
648
          if( DP_Port0_Ack = '1' )then
649
            DP_Port0_Req     <= '0';
650
            TX_Length        <= DP_Port0_RdData;
651
            TX_FSM_State     <= TX_INIT;
652
          end if;
653
 
654
        when TX_INIT =>
655
          TX_FSM_State       <= WAIT_FOR_UPDATE;
656
          if( TX_Length > TX_RESERVED_LOW and
657
              TX_Length < TX_RESERVED_HIGH )then
658
            TX_CRC_Clr       <= '1';
659
            TX_FSM_State     <= TX_START_FLAG;
660
          end if;
661
 
662
        when TX_START_FLAG =>
663
          TX_Wr_En           <= '1';
664
          TX_Wr_Flag         <= '1';
665
          TX_Wr_Data         <= SDLC_FLAG;
666
          TX_FSM_State       <= TX_WAIT_START_FLAG;
667
 
668
        when TX_WAIT_START_FLAG =>
669
          if( TX_Req_Next = '1' )then
670
            DP_Port0_Addr    <= x"00";
671
            TX_FSM_State     <= TX_ADV_ADDR;
672
          end if;
673
 
674
        when TX_ADV_ADDR =>
675
          DP_Port0_Req       <= '1';
676
          if( DP_Port0_Ack = '1' )then
677
            DP_Port0_Req     <= '0';
678
            DP_Port0_Addr    <= DP_Port0_Addr + 1;
679
            TX_Length        <= TX_Length - 1;
680
            TX_FSM_State     <= TX_MESG_DATA;
681
          end if;
682
 
683
        when TX_MESG_DATA =>
684
          TX_Wr_En           <= '1';
685
          TX_Wr_Data         <= DP_Port0_RdData;
686
          TX_CRC_En          <= '1';
687
          TX_FSM_State       <= TX_WAIT_MESG_DATA;
688
 
689
        when TX_WAIT_MESG_DATA =>
690
          if( TX_Req_Next = '1' )then
691
            TX_FSM_State     <= TX_ADV_ADDR;
692
            if( TX_Length = 0 )then
693
              TX_FSM_State   <= TX_CRC_LB_WR;
694
            end if;
695
          end if;
696
 
697
        when TX_CRC_LB_WR =>
698
          TX_Wr_En           <= '1';
699
          TX_Wr_Data         <= TX_CRC_Data_LB;
700
          TX_FSM_State       <= TX_CRC_LB_WAIT;
701
 
702
        when TX_CRC_LB_WAIT =>
703
          if( TX_Req_Next = '1' )then
704
              TX_FSM_State   <= TX_CRC_UB_WR;
705
          end if;
706
 
707
        when TX_CRC_UB_WR =>
708
          TX_Wr_En           <= '1';
709
          TX_Wr_Data         <= TX_CRC_Data_UB;
710
          TX_FSM_State       <= TX_CRC_UB_WAIT;
711
 
712
        when TX_CRC_UB_WAIT =>
713
          if( TX_Req_Next = '1' )then
714
              TX_FSM_State   <= TX_STOP_FLAG;
715
          end if;
716
 
717
        when TX_STOP_FLAG =>
718
          TX_Wr_En           <= '1';
719
          TX_Wr_Flag         <= '1';
720
          TX_Wr_Data         <= SDLC_FLAG;
721
          TX_FSM_State       <= TX_WAIT_STOP_FLAG;
722
 
723
        when TX_WAIT_STOP_FLAG =>
724
          if( TX_Req_Next = '1' )then
725
            TX_FSM_State     <= TX_SET_FLAG;
726
          end if;
727
 
728
        when TX_SET_FLAG =>
729
          DP_Port0_Addr      <= TX_REGISTER;
730
          DP_Port0_Req       <= '1';
731
          DP_Port0_WrData    <= FLAG_DONE;
732
          DP_Port0_RWn       <= '0';
733
          if( DP_Port0_Ack = '1' )then
734
            DP_Port0_Req     <= '0';
735
            TX_FSM_State     <= WAIT_FOR_UPDATE;
736
          end if;
737
 
738
        when others => null;
739
      end case;
740
 
741
      if( BClk_CoS = '1' )then
742
        TX_Int_pend          <= '1';
743
        TX_FSM_State         <= WR_CLOCK_STATE;
744
      end if;
745
 
746
    end if;
747
  end process;
748
 
749 202 jshamlet
  U_TX_CRC : entity work.sdlc_crc16_ccitt
750
  generic map(
751
    Poly_Init                => Poly_Init,
752
    Reset_Level              => Reset_Level
753
  )
754
  port map(
755
    Clock                    => Clock,
756
    Reset                    => Reset,
757 192 jshamlet
    --
758 202 jshamlet
    Clear                    => TX_CRC_Clr,
759
    Wr_En                    => TX_CRC_En,
760
    Wr_Data                  => TX_Wr_Data,
761 192 jshamlet
    --
762 202 jshamlet
    CRC16_Valid              => TX_CRC_Valid,
763
    CRC16_Out                => TX_CRC_Data
764 192 jshamlet
  );
765
 
766 278 jshamlet
  TX_Serial_proc: process( Clock, Reset )
767
  begin
768
    if( Reset = Reset_Level )then
769
      TX_State               <= IDLE;
770
      SDLC_Out               <= '1';
771
      TX_Arm                 <= '0';
772
      TX_Buffer              <= (others => '0');
773
      TX_Flag                <= '0';
774
      TX_ShftReg             <= (others => '0');
775
      TX_BitStuff            <= (others => '0');
776
      TX_BitCntr             <= (others => '1');
777
      TX_Req_Next               <= '0';
778
    elsif( rising_edge(Clock) )then
779 192 jshamlet
 
780 278 jshamlet
      if( TX_Wr_En = '1' and TX_Arm = '0')then
781
        TX_Arm               <= '1';
782
        TX_Buffer_Flag       <= TX_Wr_Flag;
783
        TX_Buffer_Data       <= TX_Wr_Data;
784
      end if;
785
 
786
      TX_Req_Next               <= '0';
787
 
788
      case( TX_State )is
789
        when INIT =>
790
          SDLC_Out           <= '1';
791
          TX_State           <= IDLE;
792
 
793
        when IDLE =>
794
          SDLC_Out           <= '1';
795
          if( TX_Arm = '1' and BClk_FE = '1' )then
796
            TX_Arm           <= '0';
797
            TX_BitCntr       <= (others => '0');
798
            TX_BitStuff      <= (others => '0');
799
            TX_Flag          <= TX_Buffer_Flag;
800
            TX_ShftReg       <= TX_Buffer_Data;
801
            TX_Req_Next      <= '1';
802
            TX_State         <= XMIT;
803
          end if;
804
 
805
        when XMIT =>
806
          SDLC_Out           <= TX_ShftReg(conv_integer(TX_BitSel));
807
          TX_BitCntr         <= TX_BitCntr + BClk_FE;
808
          if( BClk_RE = '1' )then
809
            TX_BitStuff      <= TX_BitStuff(3 downto 0) &
810
                                TX_ShftReg(conv_integer(TX_BitSel));
811
          end if;
812
          if( BClk_FE = '1' )then
813
            if( TX_BitCntr >= 7 )then
814
              TX_State       <= TERM;
815
            elsif( and_reduce(TX_BitStuff) = '1' and TX_Flag = '0' )then
816
              TX_BitStuff    <= (others => '0');
817
              TX_State       <= SPACE;
818
            else
819
              TX_BitCntr     <= TX_BitCntr + 1;
820
            end if;
821
          end if;
822
 
823
        when SPACE =>
824
          SDLC_Out           <= '0';
825
          if( BClk_FE = '1' )then
826
            TX_State         <= XMIT;
827
          end if;
828
 
829
        when TERM =>
830
          if( TX_Arm = '1' )then
831
            TX_State         <= LD_NEXT;
832
          else
833
            TX_State         <= IDLE;
834
          end if;
835
 
836
        when LD_NEXT =>
837
          TX_Arm             <= '0';
838
          TX_BitCntr         <= (others => '0');
839
          TX_Flag            <= TX_Buffer_Flag;
840
          TX_ShftReg         <= TX_Buffer_Data;
841
          TX_Req_Next        <= '1';
842
          TX_State           <= XMIT;
843
          if( and_reduce(TX_BitStuff) = '1' and TX_Flag = '0' )then
844
            TX_BitStuff      <= (others => '0');
845
            TX_State         <= SPACE;
846
          end if;
847
 
848
        when others => null;
849
      end case;
850
 
851
      if( BClk_Okay = '0' )then
852
        TX_State                <= INIT;
853
      end if;
854
 
855
    end if;
856
  end process;
857
 
858 202 jshamlet
-- ***************************************************************************
859
-- *                     Serial Receive Path                                 *
860
-- ***************************************************************************
861 192 jshamlet
 
862 278 jshamlet
IF_Is_Master: if( Set_As_Master )generate
863 192 jshamlet
 
864 278 jshamlet
  Input_proc: process( Clock, Reset )
865
  begin
866
    if( Reset = Reset_Level )then
867
      RX_LatchEn_SR          <= (others => '0');
868
      RX_Serial_SR           <= (others => '0');
869
    elsif( rising_edge(Clock) )then
870
      RX_LatchEn_SR          <= RX_LatchEn_SR(Clock_Offset - 1 downto 0) & BClk_RE;
871
      RX_Serial_SR           <= RX_Serial_SR(0) & SDLC_In;
872
    end if;
873
  end process;
874 202 jshamlet
 
875 278 jshamlet
  RX_LatchEn                 <= RX_LatchEn_M;
876
 
877
end generate;
878
 
879
IF_Is_Slave: if( not Set_As_Master )generate
880
 
881
  Input_proc: process( Clock, Reset )
882
  begin
883
    if( Reset = Reset_Level )then
884
      RX_Serial_SR           <= (others => '0');
885
    elsif( rising_edge(Clock) )then
886
      RX_Serial_SR           <= RX_Serial_SR(0) & SDLC_In;
887
    end if;
888
  end process;
889
 
890
  RX_LatchEn                 <= RX_LatchEn_S;
891
 
892
end generate;
893
 
894
  RX_Serial_proc: process( Clock, Reset )
895
  begin
896
    if( Reset = Reset_Level )then
897
 
898
      RX_BitStuff_SR         <= (others => '0');
899
      RX_Flag_SR             <= (others => '0');
900
      RX_Idle_Cntr           <= (others => '0');
901
 
902
      RX_State               <= IDLE;
903
      RX_Idle                <= '0';
904
 
905
      RX_Buffer              <= (others => '0');
906
      RX_BitCntr             <= (others => '0');
907
 
908
      RX_Valid               <= '0';
909
      RX_Flag                <= '0';
910
      RX_Data                <= (others => '0');
911
 
912
    elsif( rising_edge(Clock) )then
913
 
914
      if( RX_LatchEn = '1' )then
915
        RX_Flag_SR           <= RX_Flag_SR(6 downto 0) & RX_Serial;
916
        if( RX_State = IDLE )then
917
          RX_Flag_SR         <= (others => '0');
918
        end if;
919
 
920
        RX_Idle_Cntr         <= RX_Idle_Cntr + RX_Serial;
921
        if( and_reduce(RX_Idle_Cntr) = '1' )then
922
          RX_Idle_Cntr       <= "111";
923
        end if;
924
      end if;
925
 
926
      if( RX_Serial = '0' )then
927
        RX_Idle_Cntr         <= (others => '0');
928
      end if;
929
 
930
      RX_Valid               <= '0';
931
      RX_Flag                <= '0';
932
      RX_Idle                <= '0';
933
 
934
      case( RX_State )is
935
 
936
        when INIT =>
937
          RX_Idle            <= '1';
938
          RX_State           <= IDLE;
939
 
940
        when IDLE =>
941
          RX_Idle            <= '1';
942
          RX_BitCntr         <= (others => '0');
943
          RX_BitStuff_SR     <= (others => '0');
944
          if( RX_Serial = '0' )then
945
            RX_State         <= RCV_DATA;
946
          end if;
947
 
948
        when RCV_DATA =>
949
          if( RX_Term = '1' )then
950
            RX_State         <= WRITE_DATA;
951
          end if;
952
          if( RX_LatchEn = '1' )then
953
            RX_Buffer(conv_integer(RX_BitSel)) <= RX_Serial;
954
            RX_BitStuff_SR   <= RX_BitStuff_SR(3 downto 0) & RX_Serial;
955
            RX_BitCntr       <= RX_BitCntr + 1;
956
 
957
            if( and_reduce(RX_BitStuff_SR) = '1' )then
958
              RX_BitStuff_SR <= (others => '0');
959
              if( RX_Serial = '0' )then
960
                RX_BitCntr   <= RX_BitCntr;
961
                RX_State     <= SKIP_ZERO;
962
              end if;
963
            end if;
964
          end if;
965
 
966
        when SKIP_ZERO =>
967
          RX_State           <= RCV_DATA;
968
 
969
        when WRITE_DATA =>
970
          RX_BitCntr         <= (others => '0');
971
          RX_Valid           <= '1';
972
          RX_Data            <= RX_Buffer;
973
          if( RX_Flag_SR = SDLC_Flag )then
974
            RX_Flag          <= '1';
975
          end if;
976
          RX_State           <= RCV_DATA;
977
 
978
        when others => null;
979
      end case;
980
 
981
      -- If we just shifted in the flag character, and the bit counter isn't
982
      --  0x0, then our bit counter is out of alignment. Reset it to zero so
983
      --  that the next word is clocked in correctly.
984
      if( RX_Flag_SR = SDLC_Flag and RX_BitCntr > 0 )then
985
         RX_BitCntr          <= (others => '0');
986
      end if;
987
 
988
      -- If the serial line goes idle (In the marking state for more than 7
989
      --  bit times), and the FSM isn't already in IDLE, force it to IDLE.
990
      if( and_reduce(RX_Idle_Cntr) = '1' and RX_State /= IDLE )then
991
        RX_State             <= IDLE;
992
      end if;
993
 
994
      -- If the bit clock is no longer valid, soft-reset to the INIT state.
995
      if( BClk_Okay = '0' )then
996
        RX_State             <= INIT;
997
      end if;
998
 
999
    end if;
1000
  end process;
1001
 
1002
  Packet_Marker_proc: process( Clock, Reset )
1003
  begin
1004
    if( Reset = Reset_Level )then
1005
      Pkt_State              <= IDLE;
1006
      First_Byte             <= '0';
1007
      RX_Frame_Start         <= '0';
1008
      RX_Frame_Stop          <= '0';
1009
      RX_Frame_Valid         <= '0';
1010
      RX_Frame_Data          <= x"00";
1011
    elsif( rising_edge(Clock) )then
1012
      RX_Frame_Start         <= '0';
1013
      RX_Frame_Stop          <= '0';
1014
      RX_Frame_Valid         <= '0';
1015
 
1016
      case( Pkt_State )is
1017
        when IDLE =>
1018
          if( RX_Valid = '1' and RX_Flag = '1' )then
1019
            Pkt_State        <= FRAME_START;
1020
          end if;
1021
 
1022
        when FRAME_START =>
1023
            if( RX_Valid = '1' and RX_Flag = '0' )then
1024
              RX_Frame_Start <= '1';
1025
              First_Byte     <= '1';
1026
              Pkt_State      <= FRAME_DATA;
1027
            end if;
1028
 
1029
        when FRAME_DATA =>
1030
          First_Byte         <= '0';
1031
          if( (RX_Valid = '1' and RX_Flag = '0') or
1032
            First_Byte = '1' )then
1033
            RX_Frame_Valid   <= '1';
1034
            RX_Frame_Data    <= RX_Data;
1035
          elsif( RX_Valid = '1' and RX_Flag = '1' )then
1036
            Pkt_State        <= FRAME_STOP;
1037
          end if;
1038
 
1039
        when FRAME_STOP =>
1040
          RX_Frame_Stop      <= not RX_Idle;
1041
          Pkt_State          <= IDLE;
1042
 
1043
        when others => null;
1044
      end case;
1045
 
1046
      if( RX_Idle = '1' and Pkt_State /= IDLE )then
1047
        Pkt_State            <= FRAME_STOP;
1048
      end if;
1049
 
1050
    end if;
1051
  end process;
1052
 
1053 192 jshamlet
  U_RX_CRC : entity work.sdlc_crc16_ccitt
1054
  generic map(
1055
    Poly_Init                => Poly_Init,
1056
    Reset_Level              => Reset_Level
1057
  )
1058
  port map(
1059
    Clock                    => Clock,
1060
    Reset                    => Reset,
1061
    --
1062 202 jshamlet
    Clear                    => RX_Frame_Start,
1063
    Wr_En                    => RX_Frame_Valid,
1064
    Wr_Data                  => RX_Frame_Data,
1065 192 jshamlet
    --
1066 202 jshamlet
    CRC16_Valid              => RX_CRC_Valid,
1067
    CRC16_Out                => RX_CRC_Data
1068 192 jshamlet
  );
1069
 
1070 278 jshamlet
  CRC_History_proc: process( Clock, Reset )
1071
  begin
1072
    if( Reset = Reset_Level )then
1073
      RX_CRC_Hist(0)         <= x"0000";
1074
      RX_CRC_Hist(1)         <= x"0000";
1075
      RX_CRC_Hist(2)         <= x"0000";
1076
    elsif( rising_edge(Clock) )then
1077
      if( RX_CRC_Valid = '1' )then
1078
        RX_CRC_Hist(2)       <= RX_CRC_Hist(1);
1079
        RX_CRC_Hist(1)       <= RX_CRC_Hist(0);
1080
        RX_CRC_Hist(0)       <= RX_CRC_Data;
1081
      end if;
1082
    end if;
1083
  end process;
1084 202 jshamlet
 
1085 278 jshamlet
  RX_Packet_RAM_proc: process( Reset, Clock )
1086
  begin
1087
    if( Reset = Reset_Level )then
1088
      RX_FSM_State           <= WAIT_FOR_CLOCK;
1089
 
1090
      DP_Port1_Addr          <= x"00";
1091
      DP_Port1_RWn           <= '1';
1092
      DP_Port1_WrData        <= x"00";
1093
      DP_Port1_Req           <= '0';
1094
 
1095
      RX_Length              <= x"00";
1096
 
1097
      RX_CRC_Rcvd            <= x"0000";
1098
 
1099
      RX_Interrupt           <= '0';
1100
 
1101
    elsif( rising_edge(Clock) )then
1102
 
1103
      DP_Port1_Addr          <= x"00";
1104
      DP_Port1_RWn           <= '1';
1105
      DP_Port1_WrData        <= x"00";
1106
      DP_Port1_Req           <= '0';
1107
 
1108
      RX_Interrupt           <= '0';
1109
 
1110
      case( RX_FSM_State )is
1111
 
1112
        when WAIT_FOR_CLOCK =>
1113
          RX_FSM_State       <= WAIT_FOR_FLAG;
1114
 
1115
        when WAIT_FOR_FLAG =>
1116
          if( RX_Frame_Start = '1' )then
1117
            RX_Length        <= x"00";
1118
            RX_FSM_State     <= RX_MESG_DATA;
1119
          end if;
1120
 
1121
        when RX_MESG_DATA =>
1122
          if( RX_Frame_Stop = '1' )then
1123
            RX_Length        <= RX_Length - 1;
1124
            RX_FSM_State         <= RX_CRC_UB_RD;
1125
          elsif( RX_Frame_Valid = '1' )then
1126
            RX_FSM_State     <= RX_WR_DATA;
1127
            if( RX_Length > 254 )then
1128
              RX_Length      <= ERR_LENGTH;
1129
              RX_FSM_State   <= RX_WR_COUNT;
1130
            end if;
1131
          end if;
1132
 
1133
        when RX_WR_DATA  =>
1134
          RX_Length          <= RX_Length + DP_Port1_Ack;
1135
          DP_Port1_Addr      <= RX_Length;
1136
          DP_Port1_WrData    <= RX_Frame_Data;
1137
          DP_Port1_RWn       <= '0';
1138
          DP_Port1_Req       <= '1';
1139
          if( DP_Port1_Ack = '1' )then
1140
            DP_Port1_Req     <= '0';
1141
            RX_FSM_State     <= RX_MESG_DATA;
1142
          end if;
1143
 
1144
        when RX_CRC_UB_RD =>
1145
          RX_Length          <= RX_Length - DP_Port1_Ack;
1146
          DP_Port1_Addr      <= RX_Length;
1147
          DP_Port1_Req       <= '1';
1148
          if( DP_Port1_Ack = '1' )then
1149
            DP_Port1_Req     <= '0';
1150
            RX_CRC_Rcvd_UB   <= DP_Port1_RdData;
1151
            RX_FSM_State     <= RX_CRC_LB_RD;
1152
          end if;
1153
 
1154
        when RX_CRC_LB_RD =>
1155
          DP_Port1_Addr      <= RX_Length;
1156
          DP_Port1_Req       <= '1';
1157
          if( DP_Port1_Ack = '1' )then
1158
            DP_Port1_Req     <= '0';
1159
            RX_CRC_Rcvd_LB   <= DP_Port1_RdData;
1160
            RX_FSM_State     <= RX_WR_CRC;
1161
          end if;
1162
 
1163
        when RX_WR_CRC =>
1164
          DP_Port1_Addr      <= CS_REGISTER;
1165
          DP_Port1_WrData    <= x"FF";
1166
          if( RX_CRC_Rcvd /= RX_CRC_Calc )then
1167
            DP_Port1_WrData  <= x"00";
1168
          end if;
1169
          DP_Port1_RWn       <= '0';
1170
          DP_Port1_Req       <= '1';
1171
          if( DP_Port1_Ack = '1' )then
1172
            DP_Port1_Req     <= '0';
1173
            RX_FSM_State     <= RX_WR_COUNT;
1174
          end if;
1175
 
1176
        when RX_WR_COUNT =>
1177
          DP_Port1_Addr      <= RX_REGISTER;
1178
          DP_Port1_WrData    <= RX_Length;
1179
          DP_Port1_RWn       <= '0';
1180
          DP_Port1_Req       <= '1';
1181
          if( DP_Port1_Ack = '1' )then
1182
            DP_Port1_Req     <= '0';
1183
            RX_Interrupt     <= '1';
1184
            RX_FSM_State     <= WAIT_FOR_FLAG;
1185
          end if;
1186
 
1187
        when others => null;
1188
      end case;
1189
 
1190
      if( BClk_Okay = '0' )then
1191
        RX_FSM_State         <= WAIT_FOR_FLAG;
1192
      end if;
1193
 
1194
    end if;
1195
  end process;
1196
 
1197 192 jshamlet
end architecture;

powered by: WebSVN 2.1.0

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