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 281

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 281 jshamlet
  signal DP_B_Addr           : std_logic_vector(8 downto 0) := (others => '0');
136
  signal DP_B_Wr_Data        : DATA_TYPE := x"00";
137
  signal DP_B_Wr_En          : std_logic := '0';
138
  signal DP_B_Rd_Data        : DATA_TYPE := x"00";
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
  signal BClk_SR             : std_logic_vector(2 downto 0)  := (others => '0');
189
 
190
  constant CLK_RATIO_R       : real := Clock_Frequency / (1.0 * BitClock_Frequency);
191
  constant CLK_DEVIATION_5P  : real := CLK_RATIO_R * 0.05;
192
  constant CLK_RATIO_ADJ_R   : real := CLK_RATIO_R + CLK_DEVIATION_5P;
193
  constant CLK_RATIO_ADJ_I   : integer := integer(CLK_RATIO_ADJ_R);
194
 
195
  constant Threshold_bits    : integer := ceil_log2(CLK_RATIO_ADJ_I);
196
  constant THRESHOLD         : std_logic_vector(Threshold_bits - 1 downto 0) :=
197
                        conv_std_logic_vector(CLK_RATIO_ADJ_I,Threshold_bits);
198
 
199
  signal RE_Threshold_Ctr    : std_logic_vector(Threshold_Bits - 1 downto 0) :=
200
                                (others => '0');
201
  signal FE_Threshold_Ctr    : std_logic_vector(Threshold_Bits - 1 downto 0) :=
202
                                (others => '0');
203
 
204
  signal Ref_In_SR           : std_logic_vector(2 downto 0) := (others => '0');
205
  alias  Ref_In_q1           is Ref_In_SR(1);
206
  alias  Ref_In_q2           is Ref_In_SR(2);
207
  signal Ref_In_RE           : std_logic := '0';
208
  signal Ref_In_FE           : std_logic := '0';
209
 
210
  signal BClk_RE             : std_logic := '0';
211
  signal BClk_FE             : std_logic := '0';
212
  signal BClk_Okay           : std_logic := '0';
213
 
214
-- Packet Transmit state logic
215
  type TX_FSM_STATES is ( INIT_FLAG,
216
                          WR_CLOCK_STATE, WAIT_FOR_CLOCK,
217
                          WAIT_FOR_UPDATE,
218
                          RD_TX_REGISTER, TX_INIT,
219
                          TX_START_FLAG, TX_WAIT_START_FLAG,
220
                          TX_MESG_DATA, TX_ADV_ADDR, TX_WAIT_MESG_DATA,
221
                          TX_CRC_LB_WR, TX_CRC_LB_WAIT,
222
                          TX_CRC_UB_WR, TX_CRC_UB_WAIT,
223
                          TX_STOP_FLAG, TX_WAIT_STOP_FLAG, TX_SET_FLAG );
224
 
225
  signal TX_FSM_State        : TX_FSM_STATES := WR_CLOCK_STATE;
226
  signal TX_Length           : DATA_TYPE := x"00";
227
 
228
  signal BClk_q1, BClk_CoS   : std_logic := '0';
229
  signal TX_Int_pend         : std_logic := '0';
230
 
231
  signal TX_Wr_En            : std_logic := '0';
232
  signal TX_Wr_Flag          : std_logic := '0';
233
  signal TX_Wr_Data          : DATA_TYPE := x"00";
234
  signal TX_Req_Next         : std_logic := '0';
235
 
236
  signal TX_CRC_Clr          : std_logic := '0';
237
  signal TX_CRC_En           : std_logic := '0';
238
  signal TX_CRC_Data         : CRC_TYPE  := x"0000";
239
  signal TX_CRC_Valid        : std_logic := '0';
240
 
241
  alias  TX_CRC_Data_LB      is TX_CRC_Data(7 downto 0);
242
  alias  TX_CRC_Data_UB      is TX_CRC_Data(15 downto 8);
243
 
244
  signal TX_Arm              : std_logic := '0';
245
  signal TX_Flag             : std_logic := '0';
246
  signal TX_Buffer           : std_logic_vector(8 downto 0) := (others => '0');
247
  alias  TX_Buffer_Flag      is TX_Buffer(8);
248
  alias  TX_Buffer_Data      is TX_Buffer(7 downto 0);
249
 
250
-- SDLC transmitter
251
  type TX_STATES is (INIT, IDLE, XMIT, SPACE, TERM, LD_NEXT);
252
  signal TX_State            : TX_STATES := INIT;
253
 
254
  signal TX_ShftReg          : DATA_TYPE := (others => '0');
255
  signal TX_Next             : std_logic := '0';
256
  signal TX_BitStuff         : std_logic_vector(4 downto 0) := (others => '0');
257
  signal TX_BitCntr          : std_logic_vector(3 downto 0) := (others => '0');
258
  alias  TX_BitSel           is TX_BitCntr(2 downto 0);
259
  alias  TX_Term             is TX_BitCntr(3);
260
 
261
-- SDLC receiver
262
  signal RX_LatchEn_SR       : std_logic_vector(Clock_Offset downto 0) := (others => '0');
263
  alias  RX_LatchEn_M        is RX_LatchEn_SR(Clock_Offset);
264
  alias  RX_LatchEn_S        is BClk_RE;
265
  signal RX_LatchEn          : std_logic := '0';
266
 
267
  signal RX_Serial_SR        : std_logic_vector(1 downto 0) := (others => '0');
268
  alias  RX_Serial           is RX_Serial_SR(1);
269
 
270
  type RX_STATES is (INIT, IDLE, RCV_DATA, SKIP_ZERO, WRITE_DATA);
271
  signal RX_State            : RX_STATES := INIT;
272
  signal RX_Buffer           : DATA_TYPE := x"00";
273
  signal RX_BitStuff_SR      : std_logic_vector(4 downto 0) := (others => '0');
274
  signal RX_BitCntr          : std_logic_vector(3 downto 0) := (others => '0');
275
  alias  RX_BitSel           is RX_BitCntr(2 downto 0);
276
  alias  RX_Term             is RX_BitCntr(3);
277
 
278
  signal RX_Flag_SR          : DATA_TYPE := x"00";
279
 
280
  signal RX_Idle_Cntr        : std_logic_vector(2 downto 0) := (others => '0');
281
 
282
  signal RX_Valid            : std_logic := '0';
283
  signal RX_Flag             : std_logic := '0';
284
  signal RX_Data             : DATA_TYPE := x"00";
285
  signal RX_Idle             : std_logic := '0';
286
 
287
-- Packet detection logic
288
  type PACKET_STATES is (IDLE, FRAME_START, FRAME_DATA, FRAME_STOP );
289
  signal Pkt_State           : PACKET_STATES := IDLE;
290
  signal First_Byte          : std_logic := '0';
291
 
292
  signal RX_Frame_Start      : std_logic := '0';
293
  signal RX_Frame_Stop       : std_logic := '0';
294
  signal RX_Frame_Valid      : std_logic := '0';
295
  signal RX_Frame_Data       : DATA_TYPE := x"00";
296
 
297
-- Receive data CRC calculation
298
  signal RX_CRC_Valid        : std_logic := '0';
299
  signal RX_CRC_Data         : CRC_TYPE  := x"0000";
300
 
301
  type CRC_HISTORY is array(0 to 2) of CRC_TYPE;
302
  signal RX_CRC_Hist         : CRC_HISTORY := (x"0000",x"0000",x"0000");
303
  alias  RX_CRC_Calc         is RX_CRC_Hist(2);
304
 
305
  signal RX_CRC_Rcvd         : CRC_TYPE  := x"0000";
306
  alias  RX_CRC_Rcvd_LB      is RX_CRC_Rcvd(7 downto 0);
307
  alias  RX_CRC_Rcvd_UB      is RX_CRC_Rcvd(15 downto 8);
308
 
309
-- Packet receive state logic
310
  type RX_FSM_STATES is ( WAIT_FOR_CLOCK, WAIT_FOR_FLAG,
311
                          RX_MESG_DATA, RX_WR_DATA,
312
                          RX_CRC_LB_RD, RX_CRC_UB_RD,
313
                          RX_WR_CRC, RX_WR_COUNT );
314
 
315
  signal RX_FSM_State        : RX_FSM_STATES := WAIT_FOR_CLOCK;
316
 
317
  signal RX_Length           : DATA_TYPE := x"00";
318
 
319 192 jshamlet
begin
320 278 jshamlet
 
321
-- ***************************************************************************
322
-- *          Open8 Bus Interface and Control Register Detection             *
323
-- ***************************************************************************
324
 
325
  -- This decode needs to happen immediately, to give the RAM a chance to
326
  --  do the lookup before we have to set Rd_Data
327
  Base_Addr_Match            <= '1' when Base_Addr = CPU_Upper_Addr else '0';
328
  Reg_Wr_En_d                <= Base_Addr_Match and
329
                                Open8_Bus.Wr_En and
330
                                Write_Qual;
331
 
332
  DP_A_Wr_En                 <= Base_Addr_Match and
333
                                Open8_Bus.Wr_En and
334
                                Write_Qual;
335
 
336
  DP_A_Rd_En_d               <= Base_Addr_Match and Open8_Bus.Rd_En;
337
 
338
  CPU_IF_proc: process( Reset, Clock )
339
  begin
340
    if( Reset = Reset_Level )then
341
      Reg_Addr               <= (others => '0');
342
      Reg_Wr_En_q            <= '0';
343
      TX_Ctl_Clk             <= '0';
344
      TX_Ctl_Len             <= '0';
345
      DP_A_Rd_En_q           <= '0';
346
      Rd_Data                <= OPEN8_NULLBUS;
347
    elsif( rising_edge(Clock) )then
348
      Reg_Addr               <= Reg_Upper_Addr;
349
      Reg_Sel                <= Reg_Lower_Addr;
350
      Reg_Wr_En_q            <= Reg_Wr_En_d;
351
 
352
      TX_Ctl_Clk             <= '0';
353
      TX_Ctl_Len             <= '0';
354
      if( Reg_Addr = Reg_Sub_Addr )then
355
        TX_Ctl_Clk           <= Reg_Wr_En_q and not Reg_Sel;
356
        TX_Ctl_Len           <= Reg_Wr_En_q and Reg_Sel;
357
      end if;
358
 
359
      DP_A_Rd_En_q           <= DP_A_Rd_En_d;
360
      Rd_Data                <= OPEN8_NULLBUS;
361
      if( DP_A_Rd_En_q = '1' )then
362
        Rd_Data              <= DP_A_Rd_Data;
363
      end if;
364
    end if;
365
  end process;
366
 
367
-- ***************************************************************************
368
-- *                     Shared Dual-Port Memory                             *
369
-- ***************************************************************************
370
 
371
  U_RAM : entity work.sdlc_dp512b_ram
372
  port map(
373
    clock                    => Clock,
374
    address_a                => DP_A_Addr,
375
    address_b                => DP_B_Addr,
376
    data_a                   => DP_A_Wr_Data,
377
    data_b                   => DP_B_Wr_Data,
378
    wren_a                   => DP_A_Wr_En,
379
    wren_b                   => DP_B_Wr_En,
380
    q_a                      => DP_A_Rd_Data,
381
    q_b                      => DP_B_Rd_Data
382
  );
383
 
384 202 jshamlet
-- ***************************************************************************
385 278 jshamlet
-- *                     Memory Arbitration                                  *
386 202 jshamlet
-- ***************************************************************************
387
 
388 278 jshamlet
  RAM_Arbitration_proc: process( Clock, Reset )
389
  begin
390
    if( Reset = Reset_Level )then
391
      DP_Arb_State           <= IDLE;
392
      DP_Last_Port           <= '0';
393 280 jshamlet
      DP_B_Addr              <= (others => '0');
394
      DP_B_Wr_Data           <= x"00";
395
      DP_B_Wr_En             <= '0';
396 278 jshamlet
      DP_Port0_RdData        <= x"00";
397
      DP_Port0_Ack           <= '0';
398
      DP_Port1_RdData        <= x"00";
399
      DP_Port1_Ack           <= '0';
400
    elsif( rising_edge(Clock) )then
401
      DP_Port0_Ack           <= '0';
402
      DP_Port1_Ack           <= '0';
403 280 jshamlet
      DP_B_Wr_En               <= '0';
404 192 jshamlet
 
405 278 jshamlet
      case( DP_Arb_State )is
406
        when IDLE =>
407
          if( DP_Port0_Req = '1' and (DP_Port1_Req = '0' or DP_Last_Port = '1') )then
408
            DP_Arb_State     <= PORT0_AD;
409
          elsif( DP_Port1_Req = '1' and (DP_Port0_Req = '0' or DP_Last_Port = '0') )then
410
            DP_Arb_State     <= PORT1_AD;
411
          end if;
412 244 jshamlet
 
413 278 jshamlet
        when PORT0_AD =>
414
          DP_Last_Port       <= '0';
415 280 jshamlet
          DP_B_Addr          <= '0' & DP_Port0_Addr;
416
          DP_B_Wr_Data       <= DP_Port0_WrData;
417
          DP_B_Wr_En         <= not DP_Port0_RWn;
418 278 jshamlet
          if( DP_Port0_RWn = '1' )then
419
            DP_Arb_State     <= PORT0_RD0;
420
          else
421
            DP_Port0_Ack     <= '1';
422
            DP_Arb_State     <= PORT0_WR;
423
          end if;
424 244 jshamlet
 
425 278 jshamlet
        when PORT0_WR =>
426
          DP_Arb_State       <= IDLE;
427
 
428
        when PORT0_RD0 =>
429
          DP_Arb_State       <= PORT0_RD1;
430
 
431
        when PORT0_RD1 =>
432
          DP_Port0_Ack       <= '1';
433 280 jshamlet
          DP_Port0_RdData    <= DP_B_Rd_Data;
434 278 jshamlet
          DP_Arb_State       <= PAUSE;
435
 
436
        when PORT1_AD =>
437
          DP_Last_Port       <= '1';
438 280 jshamlet
          DP_B_Addr          <= '1' & DP_Port1_Addr;
439
          DP_B_Wr_Data       <= DP_Port1_WrData;
440
          DP_B_Wr_En         <= not DP_Port1_RWn;
441 278 jshamlet
          if( DP_Port0_RWn = '1' )then
442
            DP_Arb_State     <= PORT1_RD0;
443
          else
444
            DP_Port1_Ack     <= '1';
445
            DP_Arb_State     <= PORT1_WR;
446
          end if;
447
 
448
        when PORT1_WR =>
449
          DP_Arb_State       <= IDLE;
450
 
451
        when PORT1_RD0 =>
452
          DP_Arb_State       <= PORT1_RD1;
453
 
454
        when PORT1_RD1 =>
455
          DP_Port1_Ack       <= '1';
456 280 jshamlet
          DP_Port1_RdData    <= DP_B_Rd_Data;
457 278 jshamlet
          DP_Arb_State       <= PAUSE;
458
 
459
        when PAUSE =>
460
          DP_Arb_State       <= IDLE;
461
 
462
        when others => null;
463
 
464
      end case;
465
    end if;
466
  end process;
467
 
468
-- ****************************************************************************
469
-- * Bit clock generation                                                     *
470
-- ****************************************************************************
471
 
472
Clock_Master: if( Set_As_Master )generate
473
 
474
  Clock_Gen_proc: process( Clock, Reset )
475 192 jshamlet
  begin
476
    if( Reset = Reset_Level )then
477 278 jshamlet
      BClk_Cntr              <= DLY_VEC;
478
      BClk_Adv               <= '0';
479
      BClk_Accum             <= (others => '0');
480
      BClk_Div               <= '0';
481
      BClk_Okay_SR           <= (others => '0');
482
      BClk_RE                <= '0';
483
      BClk_FE                <= '0';
484
      SDLC_MClk              <= '0';
485
    elsif( rising_edge( Clock ) )then
486
      BClk_Cntr              <= BClk_Cntr - 1;
487
      BClk_Adv               <= '0';
488
      if( or_reduce(BClk_Cntr) = '0' )then
489
        BClk_Cntr            <= DLY_VEC;
490
        BClk_Adv             <= '1';
491
        BClk_Okay_SR         <= BClk_Okay_SR(2 downto 0) & '1';
492
      end if;
493
      BClk_Accum             <= BClk_Accum + BClk_Adv;
494
      BClk_Div               <= BClk_Div xor BClk_Adv;
495
      BClk_RE                <= (not BClk_Div) and BClk_Adv;
496
      BClk_FE                <= BClk_Div and BClk_Adv;
497
      SDLC_MClk              <= BClk_Div;
498
    end if;
499
  end process;
500
 
501
  BClk_Okay                  <= BClk_Okay_SR(3);
502
 
503
end generate;
504
 
505
Clock_Slave: if( not Set_As_Master )generate
506
 
507
  Clock_Edge_proc: process( Clock, Reset )
508
  begin
509
    if( Reset = Reset_Level )then
510
      BClk_SR                <= (others => '0');
511
      BClk_FE                <= '0';
512
      BClk_RE                <= '0';
513 192 jshamlet
    elsif( rising_edge(Clock) )then
514 278 jshamlet
      BClk_SR                <= BClk_SR(1 downto 0) & SDLC_SClk;
515
      BClk_FE                <= BClk_SR(2) and (not BClk_SR(1));
516
      BClk_RE                <= (not BClk_SR(2)) and BClk_SR(1);
517
    end if;
518
  end process;
519 192 jshamlet
 
520 278 jshamlet
  SDLC_MClk                  <= '0';
521
 
522
  Clock_Detect_proc: process( Clock, Reset )
523
  begin
524
    if( Reset = Reset_Level )then
525
      Ref_In_SR              <= (others => '0');
526
      Ref_In_RE              <= '0';
527
      Ref_In_FE              <= '0';
528
      RE_Threshold_Ctr       <= (others => '0');
529
      FE_Threshold_Ctr       <= (others => '0');
530
      BClk_Okay              <= '0';
531
 
532
    elsif( rising_edge(Clock) )then
533
      Ref_In_SR              <= Ref_In_SR(1 downto 0) & SDLC_SClk;
534
      Ref_In_RE              <= Ref_In_q1 and (not Ref_In_q2);
535
      Ref_In_FE              <= (not Ref_In_q1) and Ref_In_q2;
536
 
537
      RE_Threshold_Ctr       <= RE_Threshold_Ctr - 1;
538
      if( Ref_In_RE = '1' )then
539
        RE_Threshold_Ctr     <= THRESHOLD;
540
      elsif( or_reduce(RE_Threshold_Ctr) = '0' )then
541
        RE_Threshold_Ctr     <= (others => '0');
542 192 jshamlet
      end if;
543
 
544 278 jshamlet
      FE_Threshold_Ctr       <= FE_Threshold_Ctr - 1;
545
      if( Ref_In_FE = '1' )then
546
        FE_Threshold_Ctr     <= THRESHOLD;
547
      elsif( or_reduce(FE_Threshold_Ctr) = '0' )then
548
        FE_Threshold_Ctr     <= (others => '0');
549 192 jshamlet
      end if;
550 278 jshamlet
 
551
 
552
      BClk_Okay              <= or_reduce(RE_Threshold_Ctr) and
553
                                or_reduce(FE_Threshold_Ctr);
554
 
555 192 jshamlet
    end if;
556
  end process;
557
 
558 278 jshamlet
end generate;
559
 
560 202 jshamlet
-- ***************************************************************************
561 278 jshamlet
-- *                     Serial Transmit Path                                *
562 202 jshamlet
-- ***************************************************************************
563
 
564 278 jshamlet
  TX_Packet_RAM_proc: process( Reset, Clock )
565
  begin
566
    if( Reset = Reset_Level )then
567
      TX_FSM_State           <= INIT_FLAG;
568 192 jshamlet
 
569 278 jshamlet
      DP_Port0_Addr          <= x"00";
570
      DP_Port0_RWn           <= '1';
571
      DP_Port0_WrData        <= x"00";
572
      DP_Port0_Req           <= '0';
573 202 jshamlet
 
574 278 jshamlet
      TX_Length              <= x"00";
575 202 jshamlet
 
576 278 jshamlet
      TX_Wr_En               <= '0';
577
      TX_Wr_Flag             <= '0';
578
      TX_Wr_Data             <= x"00";
579 202 jshamlet
 
580 278 jshamlet
      TX_CRC_Clr             <= '0';
581
      TX_CRC_En              <= '0';
582 192 jshamlet
 
583 278 jshamlet
      BClk_q1                <= '0';
584
      BClk_CoS               <= '0';
585 202 jshamlet
 
586 278 jshamlet
      TX_Int_pend            <= '0';
587
      TX_Interrupt           <= '0';
588 202 jshamlet
 
589 278 jshamlet
    elsif( rising_edge(Clock) )then
590
 
591
      DP_Port0_RWn           <= '1';
592
      DP_Port0_WrData        <= x"00";
593
      DP_Port0_Req           <= '0';
594
 
595
      TX_Wr_En               <= '0';
596
      TX_Wr_Flag             <= '0';
597
      TX_Wr_Data             <= x"00";
598
 
599
      TX_CRC_Clr             <= '0';
600
      TX_CRC_En              <= '0';
601
 
602
      BClk_q1                <= BClk_Okay;
603
      BClk_CoS               <= BClk_q1 xor BClk_Okay;
604
 
605
      TX_Interrupt           <= '0';
606
 
607
      case( TX_FSM_State )is
608
 
609
        when INIT_FLAG =>
610
          DP_Port0_Addr      <= TX_REGISTER;
611
          DP_Port0_Req       <= '1';
612
          DP_Port0_WrData    <= FLAG_DONE;
613
          DP_Port0_RWn       <= '0';
614
          if( DP_Port0_Ack = '1' )then
615
            DP_Port0_Req     <= '0';
616
            TX_FSM_State     <= WR_CLOCK_STATE;
617
          end if;
618
 
619
        when WAIT_FOR_UPDATE =>
620
          if( TX_Ctl_Clk = '1' )then
621
            TX_FSM_State     <= WR_CLOCK_STATE;
622
          end if;
623
          if( TX_Ctl_Len = '1' )then
624
            TX_FSM_State     <= RD_TX_REGISTER;
625
          end if;
626
 
627
        when WR_CLOCK_STATE =>
628
          DP_Port0_Addr      <= CK_REGISTER;
629
          DP_Port0_Req       <= '1';
630
          DP_Port0_WrData    <= (others => BClk_Okay);
631
          DP_Port0_RWn       <= '0';
632
          if( DP_Port0_Ack = '1' )then
633
            TX_Interrupt     <= TX_Int_pend;
634
            TX_Int_pend      <= '0';
635
            DP_Port0_Req     <= '0';
636
            TX_FSM_State     <= WAIT_FOR_CLOCK;
637
          end if;
638
 
639
        when WAIT_FOR_CLOCK =>
640
          if( BClk_Okay = '1' )then
641
            TX_FSM_State     <= WAIT_FOR_UPDATE;
642
          end if;
643
 
644
        when RD_TX_REGISTER =>
645
          DP_Port0_Addr      <= TX_REGISTER;
646
          DP_Port0_Req       <= '1';
647
          if( DP_Port0_Ack = '1' )then
648
            DP_Port0_Req     <= '0';
649
            TX_Length        <= DP_Port0_RdData;
650
            TX_FSM_State     <= TX_INIT;
651
          end if;
652
 
653
        when TX_INIT =>
654
          TX_FSM_State       <= WAIT_FOR_UPDATE;
655
          if( TX_Length > TX_RESERVED_LOW and
656
              TX_Length < TX_RESERVED_HIGH )then
657
            TX_CRC_Clr       <= '1';
658
            TX_FSM_State     <= TX_START_FLAG;
659
          end if;
660
 
661
        when TX_START_FLAG =>
662
          TX_Wr_En           <= '1';
663
          TX_Wr_Flag         <= '1';
664
          TX_Wr_Data         <= SDLC_FLAG;
665
          TX_FSM_State       <= TX_WAIT_START_FLAG;
666
 
667
        when TX_WAIT_START_FLAG =>
668
          if( TX_Req_Next = '1' )then
669
            DP_Port0_Addr    <= x"00";
670
            TX_FSM_State     <= TX_ADV_ADDR;
671
          end if;
672
 
673
        when TX_ADV_ADDR =>
674
          DP_Port0_Req       <= '1';
675
          if( DP_Port0_Ack = '1' )then
676
            DP_Port0_Req     <= '0';
677
            DP_Port0_Addr    <= DP_Port0_Addr + 1;
678
            TX_Length        <= TX_Length - 1;
679
            TX_FSM_State     <= TX_MESG_DATA;
680
          end if;
681
 
682
        when TX_MESG_DATA =>
683
          TX_Wr_En           <= '1';
684
          TX_Wr_Data         <= DP_Port0_RdData;
685
          TX_CRC_En          <= '1';
686
          TX_FSM_State       <= TX_WAIT_MESG_DATA;
687
 
688
        when TX_WAIT_MESG_DATA =>
689
          if( TX_Req_Next = '1' )then
690
            TX_FSM_State     <= TX_ADV_ADDR;
691
            if( TX_Length = 0 )then
692
              TX_FSM_State   <= TX_CRC_LB_WR;
693
            end if;
694
          end if;
695
 
696
        when TX_CRC_LB_WR =>
697
          TX_Wr_En           <= '1';
698
          TX_Wr_Data         <= TX_CRC_Data_LB;
699
          TX_FSM_State       <= TX_CRC_LB_WAIT;
700
 
701
        when TX_CRC_LB_WAIT =>
702
          if( TX_Req_Next = '1' )then
703
              TX_FSM_State   <= TX_CRC_UB_WR;
704
          end if;
705
 
706
        when TX_CRC_UB_WR =>
707
          TX_Wr_En           <= '1';
708
          TX_Wr_Data         <= TX_CRC_Data_UB;
709
          TX_FSM_State       <= TX_CRC_UB_WAIT;
710
 
711
        when TX_CRC_UB_WAIT =>
712
          if( TX_Req_Next = '1' )then
713
              TX_FSM_State   <= TX_STOP_FLAG;
714
          end if;
715
 
716
        when TX_STOP_FLAG =>
717
          TX_Wr_En           <= '1';
718
          TX_Wr_Flag         <= '1';
719
          TX_Wr_Data         <= SDLC_FLAG;
720
          TX_FSM_State       <= TX_WAIT_STOP_FLAG;
721
 
722
        when TX_WAIT_STOP_FLAG =>
723
          if( TX_Req_Next = '1' )then
724
            TX_FSM_State     <= TX_SET_FLAG;
725
          end if;
726
 
727
        when TX_SET_FLAG =>
728
          DP_Port0_Addr      <= TX_REGISTER;
729
          DP_Port0_Req       <= '1';
730
          DP_Port0_WrData    <= FLAG_DONE;
731
          DP_Port0_RWn       <= '0';
732
          if( DP_Port0_Ack = '1' )then
733
            DP_Port0_Req     <= '0';
734
            TX_FSM_State     <= WAIT_FOR_UPDATE;
735
          end if;
736
 
737
        when others => null;
738
      end case;
739
 
740
      if( BClk_CoS = '1' )then
741
        TX_Int_pend          <= '1';
742
        TX_FSM_State         <= WR_CLOCK_STATE;
743
      end if;
744
 
745
    end if;
746
  end process;
747
 
748 202 jshamlet
  U_TX_CRC : entity work.sdlc_crc16_ccitt
749
  generic map(
750
    Poly_Init                => Poly_Init,
751
    Reset_Level              => Reset_Level
752
  )
753
  port map(
754
    Clock                    => Clock,
755
    Reset                    => Reset,
756 192 jshamlet
    --
757 202 jshamlet
    Clear                    => TX_CRC_Clr,
758
    Wr_En                    => TX_CRC_En,
759
    Wr_Data                  => TX_Wr_Data,
760 192 jshamlet
    --
761 202 jshamlet
    CRC16_Valid              => TX_CRC_Valid,
762
    CRC16_Out                => TX_CRC_Data
763 192 jshamlet
  );
764
 
765 278 jshamlet
  TX_Serial_proc: process( Clock, Reset )
766
  begin
767
    if( Reset = Reset_Level )then
768
      TX_State               <= IDLE;
769
      SDLC_Out               <= '1';
770
      TX_Arm                 <= '0';
771
      TX_Buffer              <= (others => '0');
772
      TX_Flag                <= '0';
773
      TX_ShftReg             <= (others => '0');
774
      TX_BitStuff            <= (others => '0');
775
      TX_BitCntr             <= (others => '1');
776
      TX_Req_Next               <= '0';
777
    elsif( rising_edge(Clock) )then
778 192 jshamlet
 
779 278 jshamlet
      if( TX_Wr_En = '1' and TX_Arm = '0')then
780
        TX_Arm               <= '1';
781
        TX_Buffer_Flag       <= TX_Wr_Flag;
782
        TX_Buffer_Data       <= TX_Wr_Data;
783
      end if;
784
 
785
      TX_Req_Next               <= '0';
786
 
787
      case( TX_State )is
788
        when INIT =>
789
          SDLC_Out           <= '1';
790
          TX_State           <= IDLE;
791
 
792
        when IDLE =>
793
          SDLC_Out           <= '1';
794
          if( TX_Arm = '1' and BClk_FE = '1' )then
795
            TX_Arm           <= '0';
796
            TX_BitCntr       <= (others => '0');
797
            TX_BitStuff      <= (others => '0');
798
            TX_Flag          <= TX_Buffer_Flag;
799
            TX_ShftReg       <= TX_Buffer_Data;
800
            TX_Req_Next      <= '1';
801
            TX_State         <= XMIT;
802
          end if;
803
 
804
        when XMIT =>
805
          SDLC_Out           <= TX_ShftReg(conv_integer(TX_BitSel));
806
          TX_BitCntr         <= TX_BitCntr + BClk_FE;
807
          if( BClk_RE = '1' )then
808
            TX_BitStuff      <= TX_BitStuff(3 downto 0) &
809
                                TX_ShftReg(conv_integer(TX_BitSel));
810
          end if;
811
          if( BClk_FE = '1' )then
812
            if( TX_BitCntr >= 7 )then
813
              TX_State       <= TERM;
814
            elsif( and_reduce(TX_BitStuff) = '1' and TX_Flag = '0' )then
815
              TX_BitStuff    <= (others => '0');
816
              TX_State       <= SPACE;
817
            else
818
              TX_BitCntr     <= TX_BitCntr + 1;
819
            end if;
820
          end if;
821
 
822
        when SPACE =>
823
          SDLC_Out           <= '0';
824
          if( BClk_FE = '1' )then
825
            TX_State         <= XMIT;
826
          end if;
827
 
828
        when TERM =>
829
          if( TX_Arm = '1' )then
830
            TX_State         <= LD_NEXT;
831
          else
832
            TX_State         <= IDLE;
833
          end if;
834
 
835
        when LD_NEXT =>
836
          TX_Arm             <= '0';
837
          TX_BitCntr         <= (others => '0');
838
          TX_Flag            <= TX_Buffer_Flag;
839
          TX_ShftReg         <= TX_Buffer_Data;
840
          TX_Req_Next        <= '1';
841
          TX_State           <= XMIT;
842
          if( and_reduce(TX_BitStuff) = '1' and TX_Flag = '0' )then
843
            TX_BitStuff      <= (others => '0');
844
            TX_State         <= SPACE;
845
          end if;
846
 
847
        when others => null;
848
      end case;
849
 
850
      if( BClk_Okay = '0' )then
851
        TX_State                <= INIT;
852
      end if;
853
 
854
    end if;
855
  end process;
856
 
857 202 jshamlet
-- ***************************************************************************
858
-- *                     Serial Receive Path                                 *
859
-- ***************************************************************************
860 192 jshamlet
 
861 278 jshamlet
IF_Is_Master: if( Set_As_Master )generate
862 192 jshamlet
 
863 278 jshamlet
  Input_proc: process( Clock, Reset )
864
  begin
865
    if( Reset = Reset_Level )then
866
      RX_LatchEn_SR          <= (others => '0');
867
      RX_Serial_SR           <= (others => '0');
868
    elsif( rising_edge(Clock) )then
869
      RX_LatchEn_SR          <= RX_LatchEn_SR(Clock_Offset - 1 downto 0) & BClk_RE;
870
      RX_Serial_SR           <= RX_Serial_SR(0) & SDLC_In;
871
    end if;
872
  end process;
873 202 jshamlet
 
874 278 jshamlet
  RX_LatchEn                 <= RX_LatchEn_M;
875
 
876
end generate;
877
 
878
IF_Is_Slave: if( not Set_As_Master )generate
879
 
880
  Input_proc: process( Clock, Reset )
881
  begin
882
    if( Reset = Reset_Level )then
883
      RX_Serial_SR           <= (others => '0');
884
    elsif( rising_edge(Clock) )then
885
      RX_Serial_SR           <= RX_Serial_SR(0) & SDLC_In;
886
    end if;
887
  end process;
888
 
889
  RX_LatchEn                 <= RX_LatchEn_S;
890
 
891
end generate;
892
 
893
  RX_Serial_proc: process( Clock, Reset )
894
  begin
895
    if( Reset = Reset_Level )then
896
 
897
      RX_BitStuff_SR         <= (others => '0');
898
      RX_Flag_SR             <= (others => '0');
899
      RX_Idle_Cntr           <= (others => '0');
900
 
901
      RX_State               <= IDLE;
902
      RX_Idle                <= '0';
903
 
904
      RX_Buffer              <= (others => '0');
905
      RX_BitCntr             <= (others => '0');
906
 
907
      RX_Valid               <= '0';
908
      RX_Flag                <= '0';
909
      RX_Data                <= (others => '0');
910
 
911
    elsif( rising_edge(Clock) )then
912
 
913
      if( RX_LatchEn = '1' )then
914
        RX_Flag_SR           <= RX_Flag_SR(6 downto 0) & RX_Serial;
915
        if( RX_State = IDLE )then
916
          RX_Flag_SR         <= (others => '0');
917
        end if;
918
 
919
        RX_Idle_Cntr         <= RX_Idle_Cntr + RX_Serial;
920
        if( and_reduce(RX_Idle_Cntr) = '1' )then
921
          RX_Idle_Cntr       <= "111";
922
        end if;
923
      end if;
924
 
925
      if( RX_Serial = '0' )then
926
        RX_Idle_Cntr         <= (others => '0');
927
      end if;
928
 
929
      RX_Valid               <= '0';
930
      RX_Flag                <= '0';
931
      RX_Idle                <= '0';
932
 
933
      case( RX_State )is
934
 
935
        when INIT =>
936
          RX_Idle            <= '1';
937
          RX_State           <= IDLE;
938
 
939
        when IDLE =>
940
          RX_Idle            <= '1';
941
          RX_BitCntr         <= (others => '0');
942
          RX_BitStuff_SR     <= (others => '0');
943
          if( RX_Serial = '0' )then
944
            RX_State         <= RCV_DATA;
945
          end if;
946
 
947
        when RCV_DATA =>
948
          if( RX_Term = '1' )then
949
            RX_State         <= WRITE_DATA;
950
          end if;
951
          if( RX_LatchEn = '1' )then
952
            RX_Buffer(conv_integer(RX_BitSel)) <= RX_Serial;
953
            RX_BitStuff_SR   <= RX_BitStuff_SR(3 downto 0) & RX_Serial;
954
            RX_BitCntr       <= RX_BitCntr + 1;
955
 
956
            if( and_reduce(RX_BitStuff_SR) = '1' )then
957
              RX_BitStuff_SR <= (others => '0');
958
              if( RX_Serial = '0' )then
959
                RX_BitCntr   <= RX_BitCntr;
960
                RX_State     <= SKIP_ZERO;
961
              end if;
962
            end if;
963
          end if;
964
 
965
        when SKIP_ZERO =>
966
          RX_State           <= RCV_DATA;
967
 
968
        when WRITE_DATA =>
969
          RX_BitCntr         <= (others => '0');
970
          RX_Valid           <= '1';
971
          RX_Data            <= RX_Buffer;
972
          if( RX_Flag_SR = SDLC_Flag )then
973
            RX_Flag          <= '1';
974
          end if;
975
          RX_State           <= RCV_DATA;
976
 
977
        when others => null;
978
      end case;
979
 
980
      -- If we just shifted in the flag character, and the bit counter isn't
981
      --  0x0, then our bit counter is out of alignment. Reset it to zero so
982
      --  that the next word is clocked in correctly.
983
      if( RX_Flag_SR = SDLC_Flag and RX_BitCntr > 0 )then
984
         RX_BitCntr          <= (others => '0');
985
      end if;
986
 
987
      -- If the serial line goes idle (In the marking state for more than 7
988
      --  bit times), and the FSM isn't already in IDLE, force it to IDLE.
989
      if( and_reduce(RX_Idle_Cntr) = '1' and RX_State /= IDLE )then
990
        RX_State             <= IDLE;
991
      end if;
992
 
993
      -- If the bit clock is no longer valid, soft-reset to the INIT state.
994
      if( BClk_Okay = '0' )then
995
        RX_State             <= INIT;
996
      end if;
997
 
998
    end if;
999
  end process;
1000
 
1001
  Packet_Marker_proc: process( Clock, Reset )
1002
  begin
1003
    if( Reset = Reset_Level )then
1004
      Pkt_State              <= IDLE;
1005
      First_Byte             <= '0';
1006
      RX_Frame_Start         <= '0';
1007
      RX_Frame_Stop          <= '0';
1008
      RX_Frame_Valid         <= '0';
1009
      RX_Frame_Data          <= x"00";
1010
    elsif( rising_edge(Clock) )then
1011
      RX_Frame_Start         <= '0';
1012
      RX_Frame_Stop          <= '0';
1013
      RX_Frame_Valid         <= '0';
1014
 
1015
      case( Pkt_State )is
1016
        when IDLE =>
1017
          if( RX_Valid = '1' and RX_Flag = '1' )then
1018
            Pkt_State        <= FRAME_START;
1019
          end if;
1020
 
1021
        when FRAME_START =>
1022
            if( RX_Valid = '1' and RX_Flag = '0' )then
1023
              RX_Frame_Start <= '1';
1024
              First_Byte     <= '1';
1025
              Pkt_State      <= FRAME_DATA;
1026
            end if;
1027
 
1028
        when FRAME_DATA =>
1029
          First_Byte         <= '0';
1030
          if( (RX_Valid = '1' and RX_Flag = '0') or
1031
            First_Byte = '1' )then
1032
            RX_Frame_Valid   <= '1';
1033
            RX_Frame_Data    <= RX_Data;
1034
          elsif( RX_Valid = '1' and RX_Flag = '1' )then
1035
            Pkt_State        <= FRAME_STOP;
1036
          end if;
1037
 
1038
        when FRAME_STOP =>
1039
          RX_Frame_Stop      <= not RX_Idle;
1040
          Pkt_State          <= IDLE;
1041
 
1042
        when others => null;
1043
      end case;
1044
 
1045
      if( RX_Idle = '1' and Pkt_State /= IDLE )then
1046
        Pkt_State            <= FRAME_STOP;
1047
      end if;
1048
 
1049
    end if;
1050
  end process;
1051
 
1052 192 jshamlet
  U_RX_CRC : entity work.sdlc_crc16_ccitt
1053
  generic map(
1054
    Poly_Init                => Poly_Init,
1055
    Reset_Level              => Reset_Level
1056
  )
1057
  port map(
1058
    Clock                    => Clock,
1059
    Reset                    => Reset,
1060
    --
1061 202 jshamlet
    Clear                    => RX_Frame_Start,
1062
    Wr_En                    => RX_Frame_Valid,
1063
    Wr_Data                  => RX_Frame_Data,
1064 192 jshamlet
    --
1065 202 jshamlet
    CRC16_Valid              => RX_CRC_Valid,
1066
    CRC16_Out                => RX_CRC_Data
1067 192 jshamlet
  );
1068
 
1069 278 jshamlet
  CRC_History_proc: process( Clock, Reset )
1070
  begin
1071
    if( Reset = Reset_Level )then
1072
      RX_CRC_Hist(0)         <= x"0000";
1073
      RX_CRC_Hist(1)         <= x"0000";
1074
      RX_CRC_Hist(2)         <= x"0000";
1075
    elsif( rising_edge(Clock) )then
1076
      if( RX_CRC_Valid = '1' )then
1077
        RX_CRC_Hist(2)       <= RX_CRC_Hist(1);
1078
        RX_CRC_Hist(1)       <= RX_CRC_Hist(0);
1079
        RX_CRC_Hist(0)       <= RX_CRC_Data;
1080
      end if;
1081
    end if;
1082
  end process;
1083 202 jshamlet
 
1084 278 jshamlet
  RX_Packet_RAM_proc: process( Reset, Clock )
1085
  begin
1086
    if( Reset = Reset_Level )then
1087
      RX_FSM_State           <= WAIT_FOR_CLOCK;
1088
 
1089
      DP_Port1_Addr          <= x"00";
1090
      DP_Port1_RWn           <= '1';
1091
      DP_Port1_WrData        <= x"00";
1092
      DP_Port1_Req           <= '0';
1093
 
1094
      RX_Length              <= x"00";
1095
 
1096
      RX_CRC_Rcvd            <= x"0000";
1097
 
1098
      RX_Interrupt           <= '0';
1099
 
1100
    elsif( rising_edge(Clock) )then
1101
 
1102
      DP_Port1_Addr          <= x"00";
1103
      DP_Port1_RWn           <= '1';
1104
      DP_Port1_WrData        <= x"00";
1105
      DP_Port1_Req           <= '0';
1106
 
1107
      RX_Interrupt           <= '0';
1108
 
1109
      case( RX_FSM_State )is
1110
 
1111
        when WAIT_FOR_CLOCK =>
1112
          RX_FSM_State       <= WAIT_FOR_FLAG;
1113
 
1114
        when WAIT_FOR_FLAG =>
1115
          if( RX_Frame_Start = '1' )then
1116
            RX_Length        <= x"00";
1117
            RX_FSM_State     <= RX_MESG_DATA;
1118
          end if;
1119
 
1120
        when RX_MESG_DATA =>
1121
          if( RX_Frame_Stop = '1' )then
1122
            RX_Length        <= RX_Length - 1;
1123
            RX_FSM_State         <= RX_CRC_UB_RD;
1124
          elsif( RX_Frame_Valid = '1' )then
1125
            RX_FSM_State     <= RX_WR_DATA;
1126
            if( RX_Length > 254 )then
1127
              RX_Length      <= ERR_LENGTH;
1128
              RX_FSM_State   <= RX_WR_COUNT;
1129
            end if;
1130
          end if;
1131
 
1132
        when RX_WR_DATA  =>
1133
          RX_Length          <= RX_Length + DP_Port1_Ack;
1134
          DP_Port1_Addr      <= RX_Length;
1135
          DP_Port1_WrData    <= RX_Frame_Data;
1136
          DP_Port1_RWn       <= '0';
1137
          DP_Port1_Req       <= '1';
1138
          if( DP_Port1_Ack = '1' )then
1139
            DP_Port1_Req     <= '0';
1140
            RX_FSM_State     <= RX_MESG_DATA;
1141
          end if;
1142
 
1143
        when RX_CRC_UB_RD =>
1144
          RX_Length          <= RX_Length - DP_Port1_Ack;
1145
          DP_Port1_Addr      <= RX_Length;
1146
          DP_Port1_Req       <= '1';
1147
          if( DP_Port1_Ack = '1' )then
1148
            DP_Port1_Req     <= '0';
1149
            RX_CRC_Rcvd_UB   <= DP_Port1_RdData;
1150
            RX_FSM_State     <= RX_CRC_LB_RD;
1151
          end if;
1152
 
1153
        when RX_CRC_LB_RD =>
1154
          DP_Port1_Addr      <= RX_Length;
1155
          DP_Port1_Req       <= '1';
1156
          if( DP_Port1_Ack = '1' )then
1157
            DP_Port1_Req     <= '0';
1158
            RX_CRC_Rcvd_LB   <= DP_Port1_RdData;
1159
            RX_FSM_State     <= RX_WR_CRC;
1160
          end if;
1161
 
1162
        when RX_WR_CRC =>
1163
          DP_Port1_Addr      <= CS_REGISTER;
1164
          DP_Port1_WrData    <= x"FF";
1165
          if( RX_CRC_Rcvd /= RX_CRC_Calc )then
1166
            DP_Port1_WrData  <= x"00";
1167
          end if;
1168
          DP_Port1_RWn       <= '0';
1169
          DP_Port1_Req       <= '1';
1170
          if( DP_Port1_Ack = '1' )then
1171
            DP_Port1_Req     <= '0';
1172
            RX_FSM_State     <= RX_WR_COUNT;
1173
          end if;
1174
 
1175
        when RX_WR_COUNT =>
1176
          DP_Port1_Addr      <= RX_REGISTER;
1177
          DP_Port1_WrData    <= RX_Length;
1178
          DP_Port1_RWn       <= '0';
1179
          DP_Port1_Req       <= '1';
1180
          if( DP_Port1_Ack = '1' )then
1181
            DP_Port1_Req     <= '0';
1182
            RX_Interrupt     <= '1';
1183
            RX_FSM_State     <= WAIT_FOR_FLAG;
1184
          end if;
1185
 
1186
        when others => null;
1187
      end case;
1188
 
1189
      if( BClk_Okay = '0' )then
1190
        RX_FSM_State         <= WAIT_FOR_FLAG;
1191
      end if;
1192
 
1193
    end if;
1194
  end process;
1195
 
1196 192 jshamlet
end architecture;

powered by: WebSVN 2.1.0

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