rocessed and the input clock rate is high enough.
*/
//------------------------------------------------------------------------------

`timescale 1 ps / 1ps

module v_uhdsdi_audio_v2_0_1_sd_demux_aes
#(  parameter           STRICT_EXT_PKT_MODE = 1)    // bit width of timeout counter
(
//
// Input clock and control signals
//
    input  wire         clk,            // input clock
    input  wire         rst,            // async reset
    input  wire         in_ce,          // input side clock enable
    input  wire         out_ce,         // output side clock enable
    input  wire         ignore_cs_errs, // if 1, audio samples will be retained
//                                      // if packet has a checksum error
// Input video data
//
    input  wire         adf,            // ADF detect
    input  wire         sav,            // SAV detect: asserted on SAV XYZ word
    input  wire [9:0]   din,            // input video data
    input  wire [3:0]   stream_in,      // stream number
//
// Output data and handshaking
//
    output reg          sf,             // 0 = subframe 0, 1 = subframe 1
    output reg  [23:0]  audio,          // audio sample
    output wire         audio_is_24b,   // 1=24-bit audio, 0=20-bit audio
    output wire [3:0]   stream_out,     // output stream number
    output wire [1:0]   audio_group,    // audio group number
    output reg          channel_pair,   // indicates channel pair in audio group
    output reg          chan,           // channel bit
    output reg          z,              // Z flag
    output reg          c,              // channel status bit
    output reg          u,              // user data bit
    output reg          v,              // valid bit
    output reg  [7:0]   dbn,            // audio packet data block number
    output reg          parity_err,     // parity error
    output wire         output_ready,   // 1 = output data is ready
    input  wire         output_ack,     // 1 = output data acknowledge
//
// Group/channel pair present flags: For each possible input video stream, there
// is an output port consisting of 8 bits, one bit for each channel pair in each
// possible audio group for that stream. These bits indicate whether each
// channel pair in each audio group has been detected. These bits are "sticky".
// Once a channel pair has been detected, its status signal will stay set until
// the clr_grp_present input is asserted. Each stream has a clr_present input
// that is used to clear its present flags.
//
    input  wire [15:0]  clr_present,    // clear inputs for present flags
    output wire [7:0]   strm0_present,  // group & ch pairs present stream0
    output wire [7:0]   strm1_present,  // group & ch pairs present stream1
    output wire [7:0]   strm2_present,  // group & ch pairs present stream2
    output wire [7:0]   strm3_present,  // group & ch pairs present stream3
    output wire [7:0]   strm4_present,  // group & ch pairs present stream4
    output wire [7:0]   strm5_present,  // group & ch pairs present stream5
    output wire [7:0]   strm6_present,  // group & ch pairs present stream6
    output wire [7:0]   strm7_present,  // group & ch pairs present stream7
    output wire [7:0]   strm8_present,  // group & ch pairs present stream8
    output wire [7:0]   strm9_present,  // group & ch pairs present stream9
    output wire [7:0]   strm10_present, // group & ch pairs present stream10
    output wire [7:0]   strm11_present, // group & ch pairs present stream11
    output wire [7:0]   strm12_present, // group & ch pairs present stream12
    output wire [7:0]   strm13_present, // group & ch pairs present stream13
    output wire [7:0]   strm14_present, // group & ch pairs present stream14
    output wire [7:0]   strm15_present, // group & ch pairs present stream15
    
//
// Demux selection control
//
// These ports allow selection of which channel pairs are to be demuxed and
// output from the demux module. The fltr_stream port indicates the input stream
// currently being processed by the output state machine. This port can be used
// by external logic to specify, on the four input ports, which audio channel
// pairs are to be processed and output and which are to be ignored. There is
// a 2-bit input port for each of the four audio groups with one bit for each
// channel pair in the group. To enable a channel pair for processing and
// output, its input bit must be '1'.
//
    output wire [3:0]   fltr_stream,    // stream number for filtering info
    input  wire [1:0]   fltr_pairs_grp1,// active channel pairs for audio grp 1
    input  wire [1:0]   fltr_pairs_grp2,// active channel pairs for audio grp 2
    input  wire [1:0]   fltr_pairs_grp3,// active channel pairs for audio grp 3
    input  wire [1:0]   fltr_pairs_grp4,// active channel pairs for audio grp 4

//
// Packet related signals
//
// These ports indicate packet error conditions.
//
// drop_pkt_err is asserted by the input state machine if it drops a packet
// because of an error condition. The reason for the dropped packet is indicated
// on the drop_pkt_code port. These signals are controlled by the input state
// machine.
//
// The pkt_start signal is asserted by the output state machine coincident with
// the first sample from each audio packet. pkt_cs_err is asserted by the
// output state machine coincident with the last sample of an audio packet when
// that packet has a checksum error.
//
    output wire         drop_pkt_err,   // an input packet was dropped
    output wire [2:0]   drop_pkt_code,  // reason for dropped packet
    output reg          pkt_cs_err,     // CS error detected in saved packet
                                        //   (only when ignore_cs_errs == 1)
    output reg          pkt_start,      // 1 on first sample of input packet

//
// Audio control packet outputs
//
// Control packet data is output from the demux as a series of 10-bit words.
// All 18 words of the payload portion of the audio control packet are output,
// including the two reserved words. The ctrl_pkt_strobe signal is asserted
// whenever a data word is output on ctrl_pkt_data. The audio group to which
// the audio control packet pertains is output on ctrl_pkt_group. A 5-bit
// value is output on ctrl_pkt_word, indicating which word of the control packet
// is being output at the current time.
//
// The audio control packet information is output by the input state machine.
// The control packet related outputs are enabled by in_ce, not out_ce. Control
// packet information could be output simultaneously with audio data. And,
// the stream that contains the audio control packet is indicated by stream_in,
// not stream_out.
//
    output wire         ctrl_pkt_strobe,// 1 when control packet data is output
    output wire [9:0]   ctrl_pkt_data,  // control packet data port
    output wire [1:0]   ctrl_pkt_group, // control packet audio group
    output wire [4:0]   ctrl_pkt_word,  // control packet word #

//
// These signals support audio packet deletion in the single stream case. They
// are used to control the audio packet deletion module.
//
    output wire         audio_did,      // current DID = embedded audio packet:
                                        // audio data, extended data, or control
    output wire [1:0]   group_now,      // decoded DID group code
    output wire         cs_word);       // current word is CS word
        
//------------------------------------------------------------------------------
// Local parameters
//

// ADR_WIDTH specifies the number of address bits used for the sample buffer
// address paths. The default value of 9 supports a buffer 512 samples deep.
// This value is passed to the input and output state machines to configure
// the size of the address generation logic. 
localparam ADR_WIDTH = 9;
localparam ADR_MSB = ADR_WIDTH - 1;

// The following parameters define the bit positions of the various fields as
// stored in the block RAM.
localparam ARAM_Z           = 0;
localparam ARAM_CHAN        = 1;
localparam ARAM_PAIR        = 2;
localparam ARAM_GRP_L       = 3;
localparam ARAM_GRP_M       = 4;
localparam ARAM_STM_L       = 5;
localparam ARAM_STM_M       = 7;
localparam ARAM_V           = 8;
localparam ARAM_U           = 9;
localparam ARAM_C           = 10;
localparam ARAM_AUD_L       = 11;
localparam ARAM_AUD_M       = 30;
localparam ARAM_P_ERR       = 31;
localparam ARAM_CS_ERR      = 32;
localparam ARAM_STM_MSB     = 33;
localparam ARAM_START       = 34;
localparam ARAM_SF          = 35;
localparam XRAM_SF1_L       = 0;
localparam XRAM_SF1_M       = 3;
localparam XRAM_SF2_L       = 4;
localparam XRAM_SF2_M       = 7;
localparam XRAM_A           = 8;
localparam XRAM_VALID       = 9;
localparam XRAM_DBN_L       = 10;
localparam XRAM_DBN_M       = 17;

//------------------------------------------------------------------------------
// Internal signal definitions
//

 
// RAM  signals 
wire [ADR_MSB:0]    wr_addr;                // BRAM write address
wire [ADR_MSB:0]    wp;                     // write pointer
wire [ADR_MSB:0]    rp;                     // read pointer
wire                empty;                  // 1 when ring buffer is empty
wire                wr_audio_ram;           // write enable for audio RAM
wire                wr_ext_ram;             // write enable for ext data RAM
wire [17:0]         ext_ram_out;            // ext data RAM output data
wire [35:0]         audio_ram_din;          // audio RAM input vector
wire [17:0]         ext_ram_din;            // ext data RAM input vector
wire [35:0]         aram_out;               // full 36-bits of RAM output
wire                ram_out_z;              // Z bit from RAM
wire                ram_out_chan;           // channel bit from RAM
wire                ram_out_pair;           // channel pair bit from RAM
wire [1:0]          ram_out_group;          // group number from RAM
wire [3:0]          ram_out_stream;         // stream number from RAM
wire                ram_out_v;              // V bit from RAM
wire                ram_out_u;              // U bit from RAM
wire                ram_out_c;              // C bit from RAM
wire [19:0]         ram_out_audio;          // 20-bit audio sample from RAM
wire                ram_out_p_err;          // parity error from RAM
wire                ram_out_cs_err;         // checksum error from RAM
wire                ram_out_start;          // packet start bit from RAM
wire                ram_out_sf;             // subframe bit from RAM
wire [3:0]          ram_out_ext_sf1;        // ext data for subframe 1 from RAM
wire [3:0]          ram_out_ext_sf2;        // ext data for subframe 2 from RAM
wire                ram_out_x_a;            // extended data A bit from RAM
wire                ram_out_x_valid;        // extended data valid from RAM
wire [7:0]          ram_out_dbn;            // DBN from RAM

// Input registers
reg  [9:0]          din_reg;                // video data input register
reg  [3:0]          stream_in_reg;          // input stream number register
reg                 sav_reg;                // SAV detected input register
reg                 adf_reg;                // ADF detected input register

// RAM input format registers and load enables
reg  [15:0]         fmt_reg0;               // RAM input format register 0
reg  [ 8:0]         fmt_reg1;               // RAM input format register 1
reg  [ 9:0]         fmt_reg2;               // RAM input format register 2
reg  [ 7:0]         dbn_reg;                // DBN input register
reg  [ 9:0]         ext_reg;                // Extended data RAM format register
wire                ld_fmt_reg0;            // load fmt_reg0
wire                ld_fmt_reg1;            // load fmt_reg1
wire                ld_fmt_reg2;            // load fmt_reg2
wire                ld_dbn_reg;             // load dbn_reg
wire                ld_ext_reg;             // load ext_reg
wire                clr_ext_reg;            // clear ext_reg to all zeros
wire                parity_err_in;          // input parity error
wire [27:0]         parity_vector;          // bits used in parity error calc

//
// Signals from input FSM
//
wire                sf_in;                  // subframe indicator
wire [1:0]          group_in;               // audio group
wire                cs_err_in;              // packet checksum error
wire                pkt_start_in;           // start of packet flag

// 
// RAM output registers and load enables
//
reg  [5:0]          sf1_reg;                // holds stream & group for sf2
reg  [3:0]          ext_sf2;                // holds ext data for sf2
wire                ld_out;                 // load sf1_reg or sf2_reg
wire [3:0]          audio_ext_mux;          // mux for extended audio data
         
//
// Signals to/from output FSM      
//
reg                 chpair_ok;              // channel pair OK to output
reg                 audio24_ok;             // 1 = extended data is OK to insert
wire                audio24_ok_sf1;         // audio24_ok for subframe 1
reg  [1:0]          fltr_pairs_mux;         // channel pair filter mux

//
// Audio group presence detection signals
//
wire [7:0]          bit_mask;               // audio group/ch pair unary mask
genvar              i;                      // generate loop variable

//------------------------------------------------------------------------------
// Input FSM
//
v_uhdsdi_audio_v2_0_1_sd_demux_infsm #(
    .WADR_WIDTH             (ADR_WIDTH),
    .STRICT_EXT_PKT_MODE    (STRICT_EXT_PKT_MODE))
INFSM (
    .clk            (clk),
    .rst            (rst),
    .ce             (in_ce),
    .adf            (adf_reg),
    .sav            (sav_reg),
    .ignore_cs_errs (ignore_cs_errs),
    .din            (din_reg[8:0]),
    .drop_pkt_err   (drop_pkt_err),
    .drop_pkt_code  (drop_pkt_code),
    .ld_fmt_reg0    (ld_fmt_reg0),
    .ld_fmt_reg1    (ld_fmt_reg1),
    .ld_fmt_reg2    (ld_fmt_reg2),
    .ld_dbn_reg     (ld_dbn_reg),
    .ld_ext_reg     (ld_ext_reg),
    .clr_ext_reg    (clr_ext_reg),
    .wr_audio_ram   (wr_audio_ram),
    .wr_ext_ram     (wr_ext_ram),
    .sf             (sf_in),
    .group          (group_in),
    .wr_addr        (wr_addr),
    .wr_pointer     (wp),
    .cs_err         (cs_err_in),
    .audio_did      (audio_di