--------------------------------------------------------------------------------
--
-- This VHDL file was generated by EASE/HDL 7.4 Revision 8 from HDL Works B.V.
--
-- Ease library  : work
-- HDL library   : work
-- Host name     : ilex
-- User name     : mgebyehu
-- Time stamp    : Mon Jul 15 14:09:13 2013
--
-- Designed by   : 
-- Company       : 
-- Project info  : 
--
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- Object        : Entity work.TransmitFrame
-- Last modified : Fri Jun 14 11:15:43 2013.
--------------------------------------------------------------------------------



library ieee, work;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;
use work.gencores_pkg.all;
use work.EMAC16bit_Package.all;
use work.V_ARRAY_Package.all;

entity TransmitFrame16bit is
  generic(
  		tx_data_swap : boolean := false);
  port(
    CRCOut     : out    std_logic_vector(31 downto 0);
    Clk        : in     std_logic;
    Rst        : in     std_logic;
    Rx_CRC_Err : in     std_logic;
    Rx_Fire    : in     std_logic;
    TxCharIsK  : out    std_logic_vector(1 downto 0);
    TxData     : out    std_logic_vector(15 downto 0));
end entity TransmitFrame16bit;

--------------------------------------------------------------------------------
-- Object        : Architecture work.TransmitFrame.fsm
-- Last modified : Fri Jun 14 11:15:43 2013.
--------------------------------------------------------------------------------

architecture fsm of TransmitFrame16bit is

  -- State Machine Options:
  --  Clock : Clk (Rising edge).
  --  State assignment : Enumerate.
  --  State decoding : Case construct.
  --  Actions on transitions : Clocked.
  --  Actions on states : Clocked.

  type state_type is (Idle, SendFrame, EndOfPacket, 
            StartOfPacket0, StartOfFrame, EndOfPacket2, CRC_0, CRC_1, 
            IFG_Idle) ;
  signal state : state_type  ;  -- Current State
--  Signal CRCint: Std_Logic_Vector(31 downto 0);
  signal crc_gen_enable: std_logic;
  signal crc_gen_rst: std_logic;
  signal crc_rst: std_logic;
  signal crc_value: Std_Logic_Vector(31 downto 0);
  Signal TxData_int: Std_Logic_Vector(15 downto 0);
  Signal TxData_merge_crc: Std_Logic_Vector(15 downto 0);
  Signal TxCharIsK_int: Std_Logic_Vector(1 downto 0);
  
  Constant IFG: Integer := 6;
  Constant FRAMECOUNT: Integer := 6;
  Constant MAC_Dst: std_logic_vector(47 downto 0) := x"0800306c2775"; -- WR MAC address
--  Constant MAC_Dst: std_logic_vector(47 downto 0) := x"00086c307527"; -- WR MAC address
  Constant MAC_Src: std_logic_vector(47 downto 0) := x"001517EBEE98"; -- Server PC MAC address

  type t_frames_type is record
	FrameLen: frame_length_array(FRAMECOUNT-1 downto 0) ;
	Frames: frame_array(FRAMECOUNT-1 downto 0, 1023 downto 0);
  end record;

  function f_init_frames  return t_frames_type is
     Variable FrameData: t_frames_type;
  begin
   -- Init:
     FrameData.FrameLen(0)       := 30;
     -- MAC Source and Destination Address and Ethernet Type-II
     FrameData.Frames(0,   0)    := MAC_Dst(47 downto 32);
     FrameData.Frames(0,   1)    := MAC_Dst(31 downto 16);
     FrameData.Frames(0,   2)    := MAC_Dst(15 downto 0);
     FrameData.Frames(0,   3)    := MAC_Src(47 downto 32);
     FrameData.Frames(0,   4)    := MAC_Src(31 downto 16);
     FrameData.Frames(0,   5)    := MAC_Src(15 downto 0);
     FrameData.Frames(0,   6)    := x"0800";

     -- IP Header
     FrameData.Frames(0,   7)    := x"4500";
     FrameData.Frames(0,   8)    := x"0026";
     FrameData.Frames(0,   9)    := x"0000";
     FrameData.Frames(0,   10)   := x"0000";
     FrameData.Frames(0,   11)   := x"8011";
	--check sum
     FrameData.Frames(0,   12)   := x"B771";
	--end check sum
     FrameData.Frames(0,   13)   := x"C0A8";	-- source:      Server IP 192.68.1.2
     FrameData.Frames(0,   14)   := x"0102";
     FrameData.Frames(0,   15)   := x"C0A8";	-- destination: CLB IP  192.68.1.3
     FrameData.Frames(0,   16)   := x"0103";

     -- UDP Header
     FrameData.Frames(0,   17)   := x"2000";	-- source:      Server UDP port 2000
     FrameData.Frames(0,   18)   := x"1000";	-- destination: CLB    UDP port 1000
     FrameData.Frames(0,   19)   := x"0012";
     FrameData.Frames(0,   20)   := x"0000";

     -- Payload
     FrameData.Frames(0,   21)   := x"0C00";
     FrameData.Frames(0,   22)   := x"1700";
     FrameData.Frames(0,   23)   := x"0000";
     FrameData.Frames(0,   24)   := x"0000";
     FrameData.Frames(0,   25)   := x"0000";

     -- Padding
     FrameData.Frames(0,   26)   := x"0000";
     FrameData.Frames(0,   27)   := x"0000";
     FrameData.Frames(0,   28)   := x"0000";
     FrameData.Frames(0,   29)   := x"0000";


     -- Definition of Frame 1:
     FrameData.FrameLen(1)       := 30;
     -- MAC Source and Destination Address and Ethernet Type-II
     FrameData.Frames(1,   0)    := MAC_Dst(47 downto 32);
     FrameData.Frames(1,   1)    := MAC_Dst(31 downto 16);
     FrameData.Frames(1,   2)    := MAC_Dst(15 downto 0);
     FrameData.Frames(1,   3)    := MAC_Src(47 downto 32);
     FrameData.Frames(1,   4)    := MAC_Src(31 downto 16);
     FrameData.Frames(1,   5)    := MAC_Src(15 downto 0);
     FrameData.Frames(1,   6)    := x"0800";

     -- IP Header
     FrameData.Frames(1,   7)    := x"4500";
     FrameData.Frames(1,   8)    := x"0026";
     FrameData.Frames(1,   9)    := x"0000";
     FrameData.Frames(1,   10)   := x"0000";
     FrameData.Frames(1,   11)   := x"8011";
	--check sum
     FrameData.Frames(1,   12)   := x"B771";
	--end check sum
     FrameData.Frames(1,   13)   := x"C0A8";	-- source:      Server IP 192.68.1.2
     FrameData.Frames(1,   14)   := x"0102";
     FrameData.Frames(1,   15)   := x"C0A8";	-- destination: CLB IP  192.68.1.3
     FrameData.Frames(1,   16)   := x"0103";
                                            
     -- UDP Header                          
     FrameData.Frames(1,   17)   := x"2001";	-- source:      Server UDP port 2001
     FrameData.Frames(1,   18)   := x"1001";	-- destination: CLB    UDP port 1001
     FrameData.Frames(1,   19)   := x"0012";
     FrameData.Frames(1,   20)   := x"0000";

     -- Payload
     FrameData.Frames(1,   21)   := x"0C00";
     FrameData.Frames(1,   22)   := x"1700";
     FrameData.Frames(1,   23)   := x"0000";
     FrameData.Frames(1,   24)   := x"0000";
     FrameData.Frames(1,   25)   := x"0000";

     -- Padding
     FrameData.Frames(1,   26)   := x"0000";
     FrameData.Frames(1,   27)   := x"0000";
     FrameData.Frames(1,   28)   := x"0000";
     FrameData.Frames(1,   29)   := x"0000";


     -- Definition of Frame 2:
     FrameData.FrameLen(2)       := 30;
     -- MAC Source and Destination Address and Ethernet Type-II
     FrameData.Frames(2,   0)    := MAC_Dst(47 downto 32);
     FrameData.Frames(2,   1)    := MAC_Dst(31 downto 16);
     FrameData.Frames(2,   2)    := MAC_Dst(15 downto 0);
     FrameData.Frames(2,   3)    := MAC_Src(47 downto 32);
     FrameData.Frames(2,   4)    := MAC_Src(31 downto 16);
     FrameData.Frames(2,   5)    := MAC_Src(15 downto 0);
     FrameData.Frames(2,   6)    := x"0800";

     -- IP Header
     FrameData.Frames(2,   7)    := x"4500";
     FrameData.Frames(2,   8)    := x"0026";
     FrameData.Frames(2,   9)    := x"0000";
     FrameData.Frames(2,   10)   := x"0000";
     FrameData.Frames(2,   11)   := x"8011";
	--check sum
     FrameData.Frames(2,   12)   := x"B771";
	--end check sum
--     FrameData.Frames(2,   6)    := x"AAAA";
--     FrameData.Frames(2,   7)    := x"AAAA";
--     FrameData.Frames(2,   8)    := x"AAAA";
--     FrameData.Frames(2,   9)    := x"AAAA";
--     FrameData.Frames(2,   10)    := x"AAAA";
--     FrameData.Frames(2,   11)    := x"AAAA";
--     FrameData.Frames(2,   12)    := x"AAAA";
  
  
     FrameData.Frames(2,   13)   := x"C0A8";	-- source:      Server IP 192.68.1.2
     FrameData.Frames(2,   14)   := x"0102";
     FrameData.Frames(2,   15)   := x"C0A8";	-- destination: CLB IP  192.68.1.3
     FrameData.Frames(2,   16)   := x"0103";
                                            
     -- UDP Header                          
     FrameData.Frames(2,   17)   := x"2002";	-- source:      Server UDP port 2002
     FrameData.Frames(2,   18)   := x"1002";	-- destination: CLB    UDP port 1002
     FrameData.Frames(2,   19)   := x"0012";
     FrameData.Frames(2,   20)   := x"0000";

     -- Payload
     FrameData.Frames(2,   21)   := x"0C00";
     FrameData.Frames(2,   22)   := x"1700";
     FrameData.Frames(2,   23)   := x"0000";
     FrameData.Frames(2,   24)   := x"0000";
     FrameData.Frames(2,   25)   := x"0000";

     -- Padding
     FrameData.Frames(2,   26)   := x"0000";
     FrameData.Frames(2,   27)   := x"0000";
     FrameData.Frames(2,   28)   := x"0000";
     FrameData.Frames(2,   29)   := x"0000";

     -- Definition of Frame 3:
     FrameData.FrameLen(3)       := 30;
     -- MAC Source and Destination Address and Ethernet Type-II
     FrameData.Frames(3,   0)    := MAC_Dst(47 downto 32);
     FrameData.Frames(3,   1)    := MAC_Dst(31 downto 16);
     FrameData.Frames(3,   2)    := MAC_Dst(15 downto 0);
     FrameData.Frames(3,   3)    := MAC_Src(47 downto 32);
     FrameData.Frames(3,   4)    := MAC_Src(31 downto 16);
     FrameData.Frames(3,   5)    := MAC_Src(15 downto 0);
     FrameData.Frames(3,   6)    := x"0800";

     -- IP Header
     FrameData.Frames(3,   7)    := x"4500";
     FrameData.Frames(3,   8)    := x"0026";
     FrameData.Frames(3,   9)    := x"0000";
     FrameData.Frames(3,   10)   := x"0000";
     FrameData.Frames(3,   11)   := x"8011";
	--check sum
     FrameData.Frames(3,   12)   := x"B771";
	--end check sum
     FrameData.Frames(3,   13)   := x"C0A8";	-- source:      Server IP 192.68.1.2
     FrameData.Frames(3,   14)   := x"0102";
     FrameData.Frames(3,   15)   := x"C0A8";	-- destination: CLB IP  192.68.1.3
     FrameData.Frames(3,   16)   := x"0103";
                                            
     -- UDP Header                          
     FrameData.Frames(3,   17)   := x"2003";	-- source:      Server UDP port 2003
     FrameData.Frames(3,   18)   := x"1003";	-- destination: CLB    UDP port 1003
     FrameData.Frames(3,   19)   := x"0012";
     FrameData.Frames(3,   20)   := x"0000";

     -- Payload
     FrameData.Frames(3,   21)   := x"0C00";
     FrameData.Frames(3,   22)   := x"1700";
     FrameData.Frames(3,   23)   := x"0000";
     FrameData.Frames(3,   24)   := x"0000";
     FrameData.Frames(3,   25)   := x"0000";

     -- Padding
     FrameData.Frames(3,   26)   := x"0000";
     FrameData.Frames(3,   27)   := x"0000";
     FrameData.Frames(3,   28)   := x"0000";
     FrameData.Frames(3,   29)   := x"0000";

    -- Definition of Frame 4:
     FrameData.FrameLen(4)       := 30;
     -- MAC Source and Destination Address and Ethernet Type-II
     FrameData.Frames(4,   0)    := MAC_Dst(47 downto 32);
     FrameData.Frames(4,   1)    := MAC_Dst(31 downto 16);
     FrameData.Frames(4,   2)    := MAC_Dst(15 downto 0);
     FrameData.Frames(4,   3)    := MAC_Src(47 downto 32);
     FrameData.Frames(4,   4)    := MAC_Src(31 downto 16);
     FrameData.Frames(4,   5)    := MAC_Src(15 downto 0);
     FrameData.Frames(4,   6)    := x"0800";

     -- IP Header
     FrameData.Frames(4,   7)    := x"4500";
     FrameData.Frames(4,   8)    := x"0026";
     FrameData.Frames(4,   9)    := x"0000";
     FrameData.Frames(4,   10)   := x"0000";
     FrameData.Frames(4,   11)   := x"8011";
	--check sum
     FrameData.Frames(4,   12)   := x"B771";
	--end check sum
     FrameData.Frames(4,   13)   := x"C0A8";	-- source:      Server IP 192.68.1.2
     FrameData.Frames(4,   14)   := x"0102";
     FrameData.Frames(4,   15)   := x"C0A8";	-- destination: CLB IP  192.68.1.3
     FrameData.Frames(4,   16)   := x"0103";
                                            
     -- UDP Header                          
     FrameData.Frames(4,   17)   := x"20FF";	-- source:      Server UDP port 20FF
     FrameData.Frames(4,   18)   := x"10FF";	-- destination: CLB    UDP port 10FF -> routed to "others" = Rx stream 4
     FrameData.Frames(4,   19)   := x"0012";
     FrameData.Frames(4,   20)   := x"0000";

     -- Payload
     FrameData.Frames(4,   21)   := x"0C00";
     FrameData.Frames(4,   22)   := x"1700";
     FrameData.Frames(4,   23)   := x"0000";
     FrameData.Frames(4,   24)   := x"0000";
     FrameData.Frames(4,   25)   := x"0000";

     -- Padding
     FrameData.Frames(4,   26)   := x"0000";
     FrameData.Frames(4,   27)   := x"0000";
     FrameData.Frames(4,   28)   := x"0000";
     FrameData.Frames(4,   29)   := x"0000";

     -- Definition of Frame 5:
     FrameData.FrameLen(5)       := 30;
     -- MAC Source and Destination Address and Ethernet Type PTPv2
     FrameData.Frames(5,   0)    := x"011B";	-- PTP multicast
     FrameData.Frames(5,   1)    := x"1900";
     FrameData.Frames(5,   2)    := x"0000";
     FrameData.Frames(5,   3)    := MAC_Src(47 downto 32);
     FrameData.Frames(5,   4)    := MAC_Src(31 downto 16);
     FrameData.Frames(5,   5)    := MAC_Src(15 downto 0);
     FrameData.Frames(5,   6)    := x"88F7";	-- PTPv2 over Ethernet

     -- PTP
     FrameData.Frames(5,   7)    := x"0002";	-- Sync Message 0x00, VersionPTP 0x02
     FrameData.Frames(5,   8)    := x"002C";	-- message length 0x2C
     FrameData.Frames(5,   9)    := x"0000";	-- Domain number 0x0000
     FrameData.Frames(5,   10)   := x"0200";	-- flags 0x0200
     FrameData.Frames(5,   11)   := x"0000";	-- Corrction 0x0000:0000:0000:0000:0000:0000 ns
     FrameData.Frames(5,   12)   := x"0000";
     FrameData.Frames(5,   13)   := x"0000";
     FrameData.Frames(5,   14)   := x"0000";
     FrameData.Frames(5,   15)   := x"0000";	
     FrameData.Frames(5,   16)   := x"0000";
     FrameData.Frames(5,   17)   := x"0800";	-- Clock Identity 0x08:00:30:FF:FE:6C:27:75
     FrameData.Frames(5,   18)   := x"30FF";
     FrameData.Frames(5,   19)   := x"FE6C";
     FrameData.Frames(5,   20)   := x"2775";
     FrameData.Frames(5,   21)   := x"0001";	-- Source port 0x0001
     FrameData.Frames(5,   22)   := x"0000";	-- Sequence ID 0x0000
     FrameData.Frames(5,   23)   := x"0000";	-- Sync Message 0x00, log messagePeriod 0x00
     FrameData.Frames(5,   24)   := x"0000";	-- origin timestamp 0x0000:0000:0000 sec
     FrameData.Frames(5,   25)   := x"0000";
     FrameData.Frames(5,   26)   := x"0000";
     FrameData.Frames(5,   27)   := x"0000";	-- origin timestamp 0x0000:0000 ns
     FrameData.Frames(5,   28)   := x"0000";
     -- Padding
     FrameData.Frames(5,   29)   := x"0000";

     -- Definition of Frame 6:
     -- FrameData.FrameLen(6)       := 30;
     -- ARP
     -- FrameData.Frames(6,   0)    := x"FFFF";
     -- FrameData.Frames(6,   1)    := x"FFFF";
     -- FrameData.Frames(6,   2)    := x"FFFF";
     -- FrameData.Frames(6,   3)    := MAC_Src(47 downto 32);
     -- FrameData.Frames(6,   4)    := MAC_Src(31 downto 16);
     -- FrameData.Frames(6,   5)    := MAC_Src(15 downto 0);
     -- FrameData.Frames(6,   6)    := x"0806";
     -- FrameData.Frames(6,   7)    := x"0001";
     -- FrameData.Frames(6,   8)    := x"0800";
     -- FrameData.Frames(6,   9)    := x"0604";
     -- FrameData.Frames(6,   10)   := x"0001";
     -- FrameData.Frames(6,   11)   := x"001B";
     -- FrameData.Frames(6,   12)   := x"21B0";
     -- FrameData.Frames(6,   13)   := x"AA75";
     -- FrameData.Frames(6,   14)   := x"C0A8";
     -- FrameData.Frames(6,   15)   := x"0102";
     -- FrameData.Frames(6,   16)   := x"0000";
     -- FrameData.Frames(6,   17)   := x"0000";
     -- FrameData.Frames(6,   18)   := x"0000";
     -- FrameData.Frames(6,   19)   := x"C0A8";
     -- FrameData.Frames(6,   20)   := x"0103";

     -- Padding
     -- FrameData.Frames(6,   21)   := x"0000";
     -- FrameData.Frames(6,   22)   := x"0000";
     -- FrameData.Frames(6,   23)   := x"0000";
     -- FrameData.Frames(6,   24)   := x"0000";
     -- FrameData.Frames(6,   25)   := x"0000";
     -- FrameData.Frames(6,   26)   := x"0000";
     -- FrameData.Frames(6,   27)   := x"0000";
     -- FrameData.Frames(6,   28)   := x"0000";
     -- FrameData.Frames(6,   29)   := x"0000";

	 return FrameData;
  end f_init_frames;


begin
  --End_o_Packet delimiter is either
  --/T/R/R/ or /T/R/K28.5/
  --(where /T/ = K29.7 and /R/ = K23.7)
  --IEEE 802.3 36.2.4.14.1 EPD rules:
  --If /R/ is transmitted in an even-numbered code-group
  --position, the PCS appends a single additional /R/ to
  --the code-group stream to ensure that the subsequent
  --/I/ is aligned on an evennumbered code-group boundary
  --and EPD transmission is complete;
  --CRC Must be initialzed to 0xFFFFFFFF
  --To force wrong Checksum initialze CRC to 0
  --
  --Inter Frame Gap (IFG) must be at least 96 bits
  --(= 6 * /I/ = 6 * <K28.5><D16.2>) long
  TxData_merge_crc <= crc_value(31 downto 16) when state = CRC_0 else crc_value(15 downto 0) when state = CRC_1 else  TxData_int;
  
  TxData <= TxData_merge_crc when tx_data_swap = true else TxData_merge_crc(7 downto 0) & TxData_merge_crc(15 downto 8);
  TxCharIsK <= TxCharIsK_int when tx_data_swap = true else TxCharIsK_int(0) & TxCharIsK_int(1);

  crc_gen_rst <= Rst Or crc_rst;
  
  U_tx_crc_generator : gc_crc_gen
    generic map (
      g_polynomial              => x"04C11DB7",
      g_init_value              => x"ffffffff",
      g_residue                 => x"38fb2284",
      g_data_width              => 16,
      g_half_width              => 8,
      g_sync_reset              => 1,
      g_dual_width              => 1,
      g_registered_match_output => false)
    port map (
      clk_i   => Clk,
      rst_i   => crc_gen_rst,
      en_i    => crc_gen_enable,
      half_i  => '0',
      data_i  => TxData_int,
      match_o => open,
      crc_o   => crc_value);
  
  state_decoding: process (Clk, Rst) is
    Variable FrameData: t_frames_type;
    Variable Idx: Integer Range 0 to 1024;
    Variable FrameIdx: Integer Range 0 to 15;

  begin
    if (Rst = '1') then
      state <= Idle ;
      TxData_int <= K28_5 & D16_2;
      TxCharIsK_int <= "10";
      -- Init:
      Idx := 0;
      FrameIdx := 0;
      crc_rst <= '1';
      crc_gen_enable <= '0';

    elsif (rising_edge(Clk)) then
      case state is
        when Idle =>

          crc_rst <= '1';
          crc_gen_enable <= '0';

          if (Rx_Fire = '1') then
            state <= StartOfPacket0 ;
            TxData_int <=  K27_7 & x"55";	-- <k27.7><preamble>
            TxCharIsK_int <= "10";
            Idx := 0;
          else
            state <= Idle ;
            TxData_int <= K28_5 & D16_2;
            TxCharIsK_int <= "10";
            -- InitFrames:
			FrameData := f_init_frames;
            -- Init:
            Idx := 0;
            FrameIdx := 0;
          end if;  

        when StartOfPacket0 =>

		  crc_rst <= '1';
          crc_gen_enable <= '0';

          if (Idx = 2) then
            state <= StartOfFrame ;
            TxData_int <= x"55" & x"D5" ;
            TxCharIsK_int <= "00";
            If Rx_CRC_Err = '1' then
			   -- also take start of frame into account for CRC; this will lead to an error!
			   crc_rst <= '0';
			   crc_gen_enable <= '1';
            End If;
          else
            TxData_int <= x"55" & x"55";
            TxCharIsK_int <= "00";
            Idx := Idx + 1;
          end if ;
        when StartOfFrame =>
            state <= SendFrame ;
            Idx := 0;
            TxData_int <= FrameData.Frames(FrameIdx,Idx);
			TxCharIsK_int <= "00";
            crc_rst <= '0';
            crc_gen_enable <= '1';
            Idx := Idx + 1;
        when SendFrame =>
          if (Idx = FrameData.FrameLen(FrameIdx)) then
            state <= CRC_0 ;
            Idx := 0;
			-- TxData_int is merged combinatorial with crc_value (31 downto 16) in state CRC_0
            TxCharIsK_int <= "00";
            crc_rst <= '0';
            crc_gen_enable <= '0';
            Idx := Idx + 1;
          else
            state <= SendFrame ;
            TxData_int <= FrameData.Frames(FrameIdx,Idx);
            TxCharIsK_int <= "00";
            crc_rst <= '0';
            crc_gen_enable <= '1';
            Idx := Idx + 1;
          end if ;
        when CRC_0 =>
            state <= CRC_1 ;
			-- TxData_int is merged combinatorial with crc_value (15 downto 0) in state CRC_1
            TxCharIsK_int <= "00";
            crc_rst <= '0';
            crc_gen_enable <= '0';
        when CRC_1 =>
            state <= EndOfPacket ;
            TxData_int <= K29_7 & K23_7;
            TxCharIsK_int <= "11";
            FrameIdx := FrameIdx + 1;
            crc_rst <= '1';
            crc_gen_enable <= '0';
        when EndOfPacket =>
            state <= IFG_Idle ;
            Idx := 0;
            TxData_int <= K28_5 & D16_2;
            TxCharIsK_int <= "10";
            Idx := Idx + 1;
            crc_rst <= '1';
            crc_gen_enable <= '0';
        when IFG_Idle =>
          if (Idx = IFG And FrameIdx /= FRAMECOUNT) then
            state <= StartOfPacket0 ;
            TxData_int <= K27_7 & x"55";	-- <k27.7><preamble>
            TxCharIsK_int <= "10";
            Idx := 0;
            crc_rst <= '1';
            crc_gen_enable <= '0';
          elsif (Idx = IFG And FrameIdx = FRAMECOUNT) then
            state <= Idle ;
            TxData_int <= K28_5 & D16_2;
            TxCharIsK_int <= "10";
            -- InitFrames:
			FrameData := f_init_frames;
            -- Init:
            Idx := 0;
            FrameIdx := 0;
            crc_rst <= '1';
            crc_gen_enable <= '0';

          else
            state <= IFG_Idle ;
            TxData_int <= K28_5 & D16_2;
            TxCharIsK_int <= "10";
            Idx := Idx + 1;
            crc_rst <= '1';
            crc_gen_enable <= '0';
          end if ;
        when others => state <= Idle ;
      end case ;
    end if ; --  Reset & Clock
  end process state_decoding ;

end architecture fsm ; -- of TransmitFrame

