data/full_repos/permissive/185287750/openmsp430/msp_core/omsp_dbg_i2c.v
1: b'%Error: data/full_repos/permissive/185287750/openmsp430/msp_core/omsp_dbg_i2c.v:45: Cannot find include file: openMSP430_defines.v\n`include "openMSP430_defines.v" \n         ^~~~~~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/185287750/openmsp430/msp_core,data/full_repos/permissive/185287750/openMSP430_defines.v\n             data/full_repos/permissive/185287750/openmsp430/msp_core,data/full_repos/permissive/185287750/openMSP430_defines.v.v\n             data/full_repos/permissive/185287750/openmsp430/msp_core,data/full_repos/permissive/185287750/openMSP430_defines.v.sv\n             openMSP430_defines.v\n             openMSP430_defines.v.v\n             openMSP430_defines.v.sv\n             obj_dir/openMSP430_defines.v\n             obj_dir/openMSP430_defines.v.v\n             obj_dir/openMSP430_defines.v.sv\n%Error: data/full_repos/permissive/185287750/openmsp430/msp_core/omsp_dbg_i2c.v:472: Cannot find include file: openMSP430_undefines.v\n`include "openMSP430_undefines.v" \n         ^~~~~~~~~~~~~~~~~~~~~~~~\n%Error: Exiting due to 2 error(s)\n'
module  omsp_dbg_i2c (


    dbg_addr,
    dbg_din,
    dbg_i2c_sda_out,
    dbg_rd,
    dbg_wr,


    dbg_clk,
    dbg_dout,
    dbg_i2c_addr,
    dbg_i2c_broadcast,
    dbg_i2c_scl,
    dbg_i2c_sda_in,
    dbg_rst,
    mem_burst,
    mem_burst_end,
    mem_burst_rd,
    mem_burst_wr,
    mem_bw
);



output        [5:0] dbg_addr;
output       [15:0] dbg_din;
output              dbg_i2c_sda_out;
output              dbg_rd;
output              dbg_wr;



input               dbg_clk;
input        [15:0] dbg_dout;
input         [6:0] dbg_i2c_addr;
input         [6:0] dbg_i2c_broadcast;
input               dbg_i2c_scl;
input               dbg_i2c_sda_in;
input               dbg_rst;
input               mem_burst;
input               mem_burst_end;
input               mem_burst_rd;
input               mem_burst_wr;
input               mem_bw;









wire scl_sync_n;
omsp_sync_cell sync_cell_i2c_scl (
    .data_out  (scl_sync_n),
    .data_in   (~dbg_i2c_scl),
    .clk       (dbg_clk),
    .rst       (dbg_rst)
);
wire scl_sync = ~scl_sync_n;

wire sda_in_sync_n;
omsp_sync_cell sync_cell_i2c_sda (
    .data_out  (sda_in_sync_n),
    .data_in   (~dbg_i2c_sda_in),
    .clk       (dbg_clk),
    .rst       (dbg_rst)
);
wire sda_in_sync = ~sda_in_sync_n;





reg  [1:0] scl_buf;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) scl_buf <=  2'h3;
  else         scl_buf <=  {scl_buf[0], scl_sync};

reg  [1:0] sda_in_buf;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) sda_in_buf <=  2'h3;
  else         sda_in_buf <=  {sda_in_buf[0], sda_in_sync};





wire scl         =  (scl_sync      & scl_buf[0])    |
                    (scl_sync      & scl_buf[1])    |
                    (scl_buf[0]    & scl_buf[1]);

wire sda_in      =  (sda_in_sync   & sda_in_buf[0]) |
                    (sda_in_sync   & sda_in_buf[1]) |
                    (sda_in_buf[0] & sda_in_buf[1]);






reg        sda_in_dly;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) sda_in_dly <=  1'b1;
  else         sda_in_dly <=  sda_in;

wire sda_in_fe   =  sda_in_dly & ~sda_in;
wire sda_in_re   = ~sda_in_dly &  sda_in;
wire sda_in_edge =  sda_in_dly ^  sda_in;


reg        scl_dly;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) scl_dly <=  1'b1;
  else         scl_dly <=  scl;

wire scl_fe      =  scl_dly    & ~scl;
wire scl_re      = ~scl_dly    &  scl;
wire scl_edge    =  scl_dly    ^  scl;



reg  [1:0] scl_re_dly;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) scl_re_dly <=  2'b00;
  else         scl_re_dly <=  {scl_re_dly[0], scl_re};

wire scl_sample  =  scl_re_dly[1];










wire start_detect = sda_in_fe & scl;





 wire stop_detect = sda_in_re & scl;








wire i2c_addr_not_valid;

reg  i2c_active_seq;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)                                 i2c_active_seq <= 1'b0;
  else if (start_detect)                       i2c_active_seq <= 1'b1;
  else if (stop_detect || i2c_addr_not_valid)  i2c_active_seq <= 1'b0;

wire i2c_active =  i2c_active_seq & ~stop_detect;
wire i2c_init   = ~i2c_active     |  start_detect;







reg   [2:0] i2c_state;
reg   [2:0] i2c_state_nxt;


reg   [8:0] shift_buf;
wire        shift_rx_done;
wire        shift_tx_done;
reg         dbg_rd;


parameter   RX_ADDR      =  3'h0;
parameter   RX_ADDR_ACK  =  3'h1;
parameter   RX_DATA      =  3'h2;
parameter   RX_DATA_ACK  =  3'h3;
parameter   TX_DATA      =  3'h4;
parameter   TX_DATA_ACK  =  3'h5;


always @(i2c_state or i2c_init or shift_rx_done or i2c_addr_not_valid or shift_tx_done or scl_fe or shift_buf or sda_in)
  case (i2c_state)
    RX_ADDR     : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~shift_rx_done      ?  RX_ADDR      :
                                    i2c_addr_not_valid ?  RX_ADDR      :
                                                          RX_ADDR_ACK;

    RX_ADDR_ACK : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~scl_fe             ?  RX_ADDR_ACK  :
                                    shift_buf[0]       ?  TX_DATA      :
                                                          RX_DATA;

    RX_DATA     : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~shift_rx_done      ?  RX_DATA      :
                                                          RX_DATA_ACK;

    RX_DATA_ACK : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~scl_fe             ?  RX_DATA_ACK  :
                                                          RX_DATA;

    TX_DATA     : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~shift_tx_done      ?  TX_DATA      :
                                                          TX_DATA_ACK;

    TX_DATA_ACK : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~scl_fe             ?  TX_DATA_ACK  :
                                   ~sda_in             ?  TX_DATA      :
                                                          RX_ADDR;

    default     : i2c_state_nxt =                         RX_ADDR;

  endcase


always @(posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)       i2c_state <= RX_ADDR;
  else               i2c_state <= i2c_state_nxt;






wire       shift_rx_en       = ((i2c_state==RX_ADDR) | (i2c_state    ==RX_DATA) | (i2c_state    ==RX_DATA_ACK));
wire       shift_tx_en       =                         (i2c_state    ==TX_DATA) | (i2c_state    ==TX_DATA_ACK);
wire       shift_tx_en_pre   =                         (i2c_state_nxt==TX_DATA) | (i2c_state_nxt==TX_DATA_ACK);

assign     shift_rx_done     = shift_rx_en & scl_fe & shift_buf[8];
assign     shift_tx_done     = shift_tx_en & scl_fe & (shift_buf==9'h100);

wire       shift_buf_rx_init = i2c_init | ((i2c_state==RX_ADDR_ACK) & scl_fe & ~shift_buf[0]) |
                                          ((i2c_state==RX_DATA_ACK) & scl_fe);
wire       shift_buf_rx_en   = shift_rx_en     & scl_sample;

wire       shift_buf_tx_init =            ((i2c_state==RX_ADDR_ACK) & scl_re &  shift_buf[0]) |
                                          ((i2c_state==TX_DATA_ACK) & scl_re);
wire       shift_buf_tx_en   = shift_tx_en_pre & scl_fe & (shift_buf!=9'h100);

wire [7:0] shift_tx_val;

wire [8:0] shift_buf_nxt     = shift_buf_rx_init  ? 9'h001                   :
                               shift_buf_tx_init  ? {shift_tx_val,   1'b1}   :
                               shift_buf_rx_en    ? {shift_buf[7:0], sda_in} :
                               shift_buf_tx_en    ? {shift_buf[7:0], 1'b0}   :
                                                     shift_buf[8:0];

always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) shift_buf <= 9'h001;
  else         shift_buf <= shift_buf_nxt;


assign i2c_addr_not_valid =  (i2c_state == RX_ADDR) && shift_rx_done && (
`ifdef DBG_I2C_BROADCAST
                              (shift_buf[7:1] != dbg_i2c_broadcast[6:0]) &&
`endif
                              (shift_buf[7:1] != dbg_i2c_addr[6:0]));
`ifdef DBG_I2C_BROADCAST
`else
wire [6:0] UNUSED_dbg_i2c_broadcast = dbg_i2c_broadcast;
`endif


wire        shift_rx_data_done = shift_rx_done & (i2c_state==RX_DATA);
wire        shift_tx_data_done = shift_tx_done;






reg dbg_i2c_sda_out;

always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)     dbg_i2c_sda_out <= 1'b1;
  else if (scl_fe) dbg_i2c_sda_out <= ~((i2c_state_nxt==RX_ADDR_ACK) ||
                                        (i2c_state_nxt==RX_DATA_ACK) ||
                                       (shift_buf_tx_en & ~shift_buf[8]));







reg   [2:0] dbg_state;
reg   [2:0] dbg_state_nxt;


reg         dbg_bw;


parameter  RX_CMD     = 3'h0;
parameter  RX_BYTE_LO = 3'h1;
parameter  RX_BYTE_HI = 3'h2;
parameter  TX_BYTE_LO = 3'h3;
parameter  TX_BYTE_HI = 3'h4;


always @(dbg_state    or shift_rx_data_done or shift_tx_data_done or shift_buf     or dbg_bw or
         mem_burst_wr or mem_burst_rd       or mem_burst          or mem_burst_end or mem_bw)
  case (dbg_state)
    RX_CMD     : dbg_state_nxt =  mem_burst_wr                ? RX_BYTE_LO  :
                                  mem_burst_rd                ? TX_BYTE_LO  :
                                  ~shift_rx_data_done         ? RX_CMD      :
                                   shift_buf[7]               ? RX_BYTE_LO  :
                                                                TX_BYTE_LO;

    RX_BYTE_LO : dbg_state_nxt = (mem_burst &  mem_burst_end) ? RX_CMD      :
                                  ~shift_rx_data_done         ? RX_BYTE_LO  :
                                 (mem_burst & ~mem_burst_end) ?
                                 (mem_bw                      ? RX_BYTE_LO  :
                                                                RX_BYTE_HI) :
                                  dbg_bw                      ? RX_CMD      :
                                                                RX_BYTE_HI;

    RX_BYTE_HI : dbg_state_nxt =  ~shift_rx_data_done         ? RX_BYTE_HI  :
                                 (mem_burst & ~mem_burst_end) ? RX_BYTE_LO  :
                                                                RX_CMD;

    TX_BYTE_LO : dbg_state_nxt =  ~shift_tx_data_done         ? TX_BYTE_LO  :
                                 ( mem_burst &  mem_bw)       ? TX_BYTE_LO  :
                                 ( mem_burst & ~mem_bw)       ? TX_BYTE_HI  :
                                  ~dbg_bw                     ? TX_BYTE_HI  :
                                                                RX_CMD;

    TX_BYTE_HI : dbg_state_nxt =  ~shift_tx_data_done         ? TX_BYTE_HI  :
                                   mem_burst                  ? TX_BYTE_LO  :
                                                                RX_CMD;


    default    : dbg_state_nxt =                                RX_CMD;

  endcase


always @(posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) dbg_state <= RX_CMD;
  else         dbg_state <= dbg_state_nxt;


wire cmd_valid   = (dbg_state==RX_CMD)     & shift_rx_data_done;
wire rx_lo_valid = (dbg_state==RX_BYTE_LO) & shift_rx_data_done;
wire rx_hi_valid = (dbg_state==RX_BYTE_HI) & shift_rx_data_done;






parameter MEM_DATA = 6'h06;


reg [5:0] dbg_addr;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)
    begin
       dbg_bw   <= 1'b0;
       dbg_addr <= 6'h00;
    end
  else if (cmd_valid)
    begin
       dbg_bw   <= shift_buf[6];
       dbg_addr <= shift_buf[5:0];
    end
  else if (mem_burst)
    begin
       dbg_bw   <= mem_bw;
       dbg_addr <= MEM_DATA;
    end



reg [7:0] dbg_din_lo;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)          dbg_din_lo <= 8'h00;
  else if (rx_lo_valid) dbg_din_lo <= shift_buf[7:0];

reg [7:0] dbg_din_hi;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)          dbg_din_hi <= 8'h00;
  else if (rx_lo_valid) dbg_din_hi <= 8'h00;
  else if (rx_hi_valid) dbg_din_hi <= shift_buf[7:0];

assign dbg_din = {dbg_din_hi, dbg_din_lo};



reg  dbg_wr;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) dbg_wr <= 1'b0;
  else         dbg_wr <= (mem_burst &  mem_bw) ? rx_lo_valid :
                         (mem_burst & ~mem_bw) ? rx_hi_valid :
                         dbg_bw                ? rx_lo_valid :
                                                 rx_hi_valid;



always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) dbg_rd <= 1'b0;
  else         dbg_rd <= (mem_burst &  mem_bw) ? (shift_tx_data_done & (dbg_state==TX_BYTE_LO)) :
                         (mem_burst & ~mem_bw) ? (shift_tx_data_done & (dbg_state==TX_BYTE_HI)) :
                         cmd_valid             ?  ~shift_buf[7]                                 :
                                                  1'b0;



assign shift_tx_val = (dbg_state==TX_BYTE_HI) ? dbg_dout[15:8] :
                                                dbg_dout[7:0];

endmodule
module  omsp_dbg_i2c (


    dbg_addr,
    dbg_din,
    dbg_i2c_sda_out,
    dbg_rd,
    dbg_wr,


    dbg_clk,
    dbg_dout,
    dbg_i2c_addr,
    dbg_i2c_broadcast,
    dbg_i2c_scl,
    dbg_i2c_sda_in,
    dbg_rst,
    mem_burst,
    mem_burst_end,
    mem_burst_rd,
    mem_burst_wr,
    mem_bw
);




output        [5:0] dbg_addr;
output       [15:0] dbg_din;
output              dbg_i2c_sda_out;
output              dbg_rd;
output              dbg_wr;



input               dbg_clk;
input        [15:0] dbg_dout;
input         [6:0] dbg_i2c_addr;
input         [6:0] dbg_i2c_broadcast;
input               dbg_i2c_scl;
input               dbg_i2c_sda_in;
input               dbg_rst;
input               mem_burst;
input               mem_burst_end;
input               mem_burst_rd;
input               mem_burst_wr;
input               mem_bw;









wire scl_sync_n;
omsp_sync_cell sync_cell_i2c_scl (
    .data_out  (scl_sync_n),
    .data_in   (~dbg_i2c_scl),
    .clk       (dbg_clk),
    .rst       (dbg_rst)
);
wire scl_sync = ~scl_sync_n;

wire sda_in_sync_n;
omsp_sync_cell sync_cell_i2c_sda (
    .data_out  (sda_in_sync_n),
    .data_in   (~dbg_i2c_sda_in),
    .clk       (dbg_clk),
    .rst       (dbg_rst)
);
wire sda_in_sync = ~sda_in_sync_n;





reg  [1:0] scl_buf;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) scl_buf <=  2'h3;
  else         scl_buf <=  {scl_buf[0], scl_sync};

reg  [1:0] sda_in_buf;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) sda_in_buf <=  2'h3;
  else         sda_in_buf <=  {sda_in_buf[0], sda_in_sync};





wire scl         =  (scl_sync      & scl_buf[0])    |
                    (scl_sync      & scl_buf[1])    |
                    (scl_buf[0]    & scl_buf[1]);

wire sda_in      =  (sda_in_sync   & sda_in_buf[0]) |
                    (sda_in_sync   & sda_in_buf[1]) |
                    (sda_in_buf[0] & sda_in_buf[1]);






reg        sda_in_dly;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) sda_in_dly <=  1'b1;
  else         sda_in_dly <=  sda_in;

wire sda_in_fe   =  sda_in_dly & ~sda_in;
wire sda_in_re   = ~sda_in_dly &  sda_in;
wire sda_in_edge =  sda_in_dly ^  sda_in;


reg        scl_dly;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) scl_dly <=  1'b1;
  else         scl_dly <=  scl;

wire scl_fe      =  scl_dly    & ~scl;
wire scl_re      = ~scl_dly    &  scl;
wire scl_edge    =  scl_dly    ^  scl;



reg  [1:0] scl_re_dly;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) scl_re_dly <=  2'b00;
  else         scl_re_dly <=  {scl_re_dly[0], scl_re};

wire scl_sample  =  scl_re_dly[1];










wire start_detect = sda_in_fe & scl;





 wire stop_detect = sda_in_re & scl;








wire i2c_addr_not_valid;

reg  i2c_active_seq;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)                                 i2c_active_seq <= 1'b0;
  else if (start_detect)                       i2c_active_seq <= 1'b1;
  else if (stop_detect || i2c_addr_not_valid)  i2c_active_seq <= 1'b0;

wire i2c_active =  i2c_active_seq & ~stop_detect;
wire i2c_init   = ~i2c_active     |  start_detect;







reg   [2:0] i2c_state;
reg   [2:0] i2c_state_nxt;


reg   [8:0] shift_buf;
wire        shift_rx_done;
wire        shift_tx_done;
reg         dbg_rd;


parameter   RX_ADDR      =  3'h0;
parameter   RX_ADDR_ACK  =  3'h1;
parameter   RX_DATA      =  3'h2;
parameter   RX_DATA_ACK  =  3'h3;
parameter   TX_DATA      =  3'h4;
parameter   TX_DATA_ACK  =  3'h5;


always @(i2c_state or i2c_init or shift_rx_done or i2c_addr_not_valid or shift_tx_done or scl_fe or shift_buf or sda_in)
  case (i2c_state)
    RX_ADDR     : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~shift_rx_done      ?  RX_ADDR      :
                                    i2c_addr_not_valid ?  RX_ADDR      :
                                                          RX_ADDR_ACK;

    RX_ADDR_ACK : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~scl_fe             ?  RX_ADDR_ACK  :
                                    shift_buf[0]       ?  TX_DATA      :
                                                          RX_DATA;

    RX_DATA     : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~shift_rx_done      ?  RX_DATA      :
                                                          RX_DATA_ACK;

    RX_DATA_ACK : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~scl_fe             ?  RX_DATA_ACK  :
                                                          RX_DATA;

    TX_DATA     : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~shift_tx_done      ?  TX_DATA      :
                                                          TX_DATA_ACK;

    TX_DATA_ACK : i2c_state_nxt =   i2c_init           ?  RX_ADDR      :
                                   ~scl_fe             ?  TX_DATA_ACK  :
                                   ~sda_in             ?  TX_DATA      :
                                                          RX_ADDR;

    default     : i2c_state_nxt =                         RX_ADDR;

  endcase


always @(posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)       i2c_state <= RX_ADDR;
  else               i2c_state <= i2c_state_nxt;






wire       shift_rx_en       = ((i2c_state==RX_ADDR) | (i2c_state    ==RX_DATA) | (i2c_state    ==RX_DATA_ACK));
wire       shift_tx_en       =                         (i2c_state    ==TX_DATA) | (i2c_state    ==TX_DATA_ACK);
wire       shift_tx_en_pre   =                         (i2c_state_nxt==TX_DATA) | (i2c_state_nxt==TX_DATA_ACK);

assign     shift_rx_done     = shift_rx_en & scl_fe & shift_buf[8];
assign     shift_tx_done     = shift_tx_en & scl_fe & (shift_buf==9'h100);

wire       shift_buf_rx_init = i2c_init | ((i2c_state==RX_ADDR_ACK) & scl_fe & ~shift_buf[0]) |
                                          ((i2c_state==RX_DATA_ACK) & scl_fe);
wire       shift_buf_rx_en   = shift_rx_en     & scl_sample;

wire       shift_buf_tx_init =            ((i2c_state==RX_ADDR_ACK) & scl_re &  shift_buf[0]) |
                                          ((i2c_state==TX_DATA_ACK) & scl_re);
wire       shift_buf_tx_en   = shift_tx_en_pre & scl_fe & (shift_buf!=9'h100);

wire [7:0] shift_tx_val;

wire [8:0] shift_buf_nxt     = shift_buf_rx_init  ? 9'h001                   :
                               shift_buf_tx_init  ? {shift_tx_val,   1'b1}   :
                               shift_buf_rx_en    ? {shift_buf[7:0], sda_in} :
                               shift_buf_tx_en    ? {shift_buf[7:0], 1'b0}   :
                                                     shift_buf[8:0];

always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) shift_buf <= 9'h001;
  else         shift_buf <= shift_buf_nxt;


assign i2c_addr_not_valid =  (i2c_state == RX_ADDR) && shift_rx_done && (
`ifdef DBG_I2C_BROADCAST
                              (shift_buf[7:1] != dbg_i2c_broadcast[6:0]) &&
`endif
                              (shift_buf[7:1] != dbg_i2c_addr[6:0]));
`ifdef DBG_I2C_BROADCAST
`else
wire [6:0] UNUSED_dbg_i2c_broadcast = dbg_i2c_broadcast;
`endif


wire        shift_rx_data_done = shift_rx_done & (i2c_state==RX_DATA);
wire        shift_tx_data_done = shift_tx_done;






reg dbg_i2c_sda_out;

always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)     dbg_i2c_sda_out <= 1'b1;
  else if (scl_fe) dbg_i2c_sda_out <= ~((i2c_state_nxt==RX_ADDR_ACK) ||
                                        (i2c_state_nxt==RX_DATA_ACK) ||
                                       (shift_buf_tx_en & ~shift_buf[8]));







reg   [2:0] dbg_state;
reg   [2:0] dbg_state_nxt;


reg         dbg_bw;


parameter  RX_CMD     = 3'h0;
parameter  RX_BYTE_LO = 3'h1;
parameter  RX_BYTE_HI = 3'h2;
parameter  TX_BYTE_LO = 3'h3;
parameter  TX_BYTE_HI = 3'h4;


always @(dbg_state    or shift_rx_data_done or shift_tx_data_done or shift_buf     or dbg_bw or
         mem_burst_wr or mem_burst_rd       or mem_burst          or mem_burst_end or mem_bw)
  case (dbg_state)
    RX_CMD     : dbg_state_nxt =  mem_burst_wr                ? RX_BYTE_LO  :
                                  mem_burst_rd                ? TX_BYTE_LO  :
                                  ~shift_rx_data_done         ? RX_CMD      :
                                   shift_buf[7]               ? RX_BYTE_LO  :
                                                                TX_BYTE_LO;

    RX_BYTE_LO : dbg_state_nxt = (mem_burst &  mem_burst_end) ? RX_CMD      :
                                  ~shift_rx_data_done         ? RX_BYTE_LO  :
                                 (mem_burst & ~mem_burst_end) ?
                                 (mem_bw                      ? RX_BYTE_LO  :
                                                                RX_BYTE_HI) :
                                  dbg_bw                      ? RX_CMD      :
                                                                RX_BYTE_HI;

    RX_BYTE_HI : dbg_state_nxt =  ~shift_rx_data_done         ? RX_BYTE_HI  :
                                 (mem_burst & ~mem_burst_end) ? RX_BYTE_LO  :
                                                                RX_CMD;

    TX_BYTE_LO : dbg_state_nxt =  ~shift_tx_data_done         ? TX_BYTE_LO  :
                                 ( mem_burst &  mem_bw)       ? TX_BYTE_LO  :
                                 ( mem_burst & ~mem_bw)       ? TX_BYTE_HI  :
                                  ~dbg_bw                     ? TX_BYTE_HI  :
                                                                RX_CMD;

    TX_BYTE_HI : dbg_state_nxt =  ~shift_tx_data_done         ? TX_BYTE_HI  :
                                   mem_burst                  ? TX_BYTE_LO  :
                                                                RX_CMD;


    default    : dbg_state_nxt =                                RX_CMD;

  endcase


always @(posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) dbg_state <= RX_CMD;
  else         dbg_state <= dbg_state_nxt;


wire cmd_valid   = (dbg_state==RX_CMD)     & shift_rx_data_done;
wire rx_lo_valid = (dbg_state==RX_BYTE_LO) & shift_rx_data_done;
wire rx_hi_valid = (dbg_state==RX_BYTE_HI) & shift_rx_data_done;






parameter MEM_DATA = 6'h06;


reg [5:0] dbg_addr;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)
    begin
       dbg_bw   <= 1'b0;
       dbg_addr <= 6'h00;
    end
  else if (cmd_valid)
    begin
       dbg_bw   <= shift_buf[6];
       dbg_addr <= shift_buf[5:0];
    end
  else if (mem_burst)
    begin
       dbg_bw   <= mem_bw;
       dbg_addr <= MEM_DATA;
    end



reg [7:0] dbg_din_lo;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)          dbg_din_lo <= 8'h00;
  else if (rx_lo_valid) dbg_din_lo <= shift_buf[7:0];

reg [7:0] dbg_din_hi;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst)          dbg_din_hi <= 8'h00;
  else if (rx_lo_valid) dbg_din_hi <= 8'h00;
  else if (rx_hi_valid) dbg_din_hi <= shift_buf[7:0];

assign dbg_din = {dbg_din_hi, dbg_din_lo};



reg  dbg_wr;
always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) dbg_wr <= 1'b0;
  else         dbg_wr <= (mem_burst &  mem_bw) ? rx_lo_valid :
                         (mem_burst & ~mem_bw) ? rx_hi_valid :
                         dbg_bw                ? rx_lo_valid :
                                                 rx_hi_valid;



always @ (posedge dbg_clk or posedge dbg_rst)
  if (dbg_rst) dbg_rd <= 1'b0;
  else         dbg_rd <= (mem_burst &  mem_bw) ? (shift_tx_data_done & (dbg_state==TX_BYTE_LO)) :
                         (mem_burst & ~mem_bw) ? (shift_tx_data_done & (dbg_state==TX_BYTE_HI)) :
                         cmd_valid             ?  ~shift_buf[7]                                 :
                                                  1'b0;



assign shift_tx_val = (dbg_state==TX_BYTE_HI) ? dbg_dout[15:8] :
                                                dbg_dout[7:0];

endmodule