axis_error_cnt =0;
  \  bit axis_error_found =0;
  \  xil_axi4stream_uint unique_slv_data_width_q[$];
  \  xil_axi4stream_uint unique_cnt;
  \  slv_monitor_data_width_q = new[$num_axis_slv];
  }]
  if {$num_axis_mst >0 } {
    append ipfile_str [subst -nocommands {
    \  mst_monitor_data_width_q = new[$num_axis_mst];

    \  for(xil_axi4stream_uint mst=0; mst< NUM_AXIS_MST; mst++) begin
    \    mst_s = mst_name_q.pop_front();
    \    if(mst_monitor_tran_q[mst].size()>0) begin
    \      mst_monitor_data_width_q[mst] = mst_monitor_tran_q[mst][0].get_data_width();
    \    end 
    \    else begin 
    \      \$info("Master monitor transaction queue %s %1d is empty. ", mst_s, mst);
    \      mst_monitor_data_width_q[mst] = 0;
    \    end
    \  end
    }] 
  }
  append ipfile_str [subst -nocommands {
    \  for(xil_axi4stream_uint slv=0; slv< NUM_AXIS_SLV; slv++) begin
    \  slv_s = slv_name_q.pop_front();
    \    slv_monitor_data_width_q[slv] = slv_monitor_tran_q[slv][0].get_data_width();
    \      comparison_cnt =0;
  }]

  if {$num_axis_mst >0 } {
    append ipfile_str [subst -nocommands { 
      \      for (xil_axi4stream_uint mst=0; mst< NUM_AXIS_MST; mst++) begin
      \        if((comparison_cnt ==0)&&(mst_monitor_tran_q[mst].size()>0)) begin
      \           \$info("Check slv %s against mst %s", slv_s, mst_s);
      \          // \$info("check slv_monitor_data_width_q[%d] =%h agaist mst_monitor_data_width_q[%d] =%h",slv, slv_monitor_data_width_q[slv], mst,mst_monitor_data_width_q[mst]);
      \          check_monitor_result(slv_monitor_tran_q[slv],mst_monitor_tran_q[mst], axis_error_cnt, axis_error_found);
      \          comparison_cnt++;
      \        end
      \      end
      \      if (comparison_cnt ==0) begin
      \        \$info("Check slv %s against autogeneration", slv_s);
      \        // \$info(" check slv_monitor_data_width_q[%d] =%h  data is autogenerated ", slv, slv_monitor_data_width_q[slv]);
      \        check_autogen_monitor_result(slv_monitor_tran_q[slv],axis_error_cnt,axis_error_found);
      \      end
      \    end
    }]  
  } else {
    append ipfile_str [subst -nocommands {
      // \$info("Check slv_monitor_data_width_q[%d] =%h  data is autogenerated ", slv, slv_monitor_data_width_q[slv]);
      \        check_autogen_monitor_result(slv_monitor_tran_q[slv],axis_error_cnt,axis_error_found);
    \  end  
    }]
  }  

  append ipfile_str [subst -nocommands {
    \  return(axis_error_found);
     endfunction
  }]
}

#TODO: Do we need # delays of 200ns?
if {$num_axis > 0} {
  append ipfile_str [subst -nocommands {
    initial begin
    \  #200001;
    \  fork
  }]

  set num_axis_mst 0
  set num_axis_slv  0
  for {set x 0} {$x < $num_axis} {incr x} {
    set xx [format {%02d} $x]
    set name [dict get $config AXIS${xx}_NAME]
    set mode [dict get $config AXIS${xx}_MODE]
    set type [dict get $config AXIS${xx}_TYPE]
    set num_bytes [dict get $config AXIS${xx}_NUM_BYTES]
    set slv_or_mst [expr {[string equal -nocase [dict get $config AXIS${xx}_MODE] {write_only}] ? "slv" : "mst"}]
    if {[string equal -nocase {write_only} $mode]} {
      append ipfile_str [subst -nocommands {
        \  forever begin
        \    ${name}_${slv_or_mst}.monitor.item_collected_port.get(${name}_${slv_or_mst}_monitor_transaction);
        \    if (${name}_${slv_or_mst}_monitor_transaction.get_last())
        \      axis_tlast_received[$x] = 1'b1;
        \    ${slv_or_mst}_monitor_tran_q[$num_axis_mst].push_back(${name}_${slv_or_mst}_monitor_transaction);
        \  end
      }]  
     incr num_axis_mst
    } else {
      append ipfile_str [subst -nocommands {
      \  forever begin
      \    ${name}_${slv_or_mst}.monitor.item_collected_port.get(${name}_${slv_or_mst}_monitor_transaction);
      \    if (${name}_${slv_or_mst}_monitor_transaction.get_last())
      \      axis_tlast_received[$x] = 1'b1;
      \    ${slv_or_mst}_monitor_tran_q[$num_axis_slv].push_back(${name}_${slv_or_mst}_monitor_transaction);
      \  end
      }]
     incr num_axis_slv
    }  
  }

  append ipfile_str [subst -nocommands {
   join_none  
  end
  }]
}

append ipfile_str [subst -nocommands {
  /////////////////////////////////////////////////////////////////////////////////////////////////
  //Instantiate AXI4 LITE VIP 
  initial begin : STIMULUS
  \  #200000;
  \  start_vips();
  \  check_scalar_registers(error_found);
  \  if (error_found == 1) begin
  \    \$display( "Test Failed!");
  \    \$finish();
  \  end
}]
if {$num_m_axi > 0} { 
  append ipfile_str [subst -nocommands {
    \  check_pointer_registers(error_found);
    \  if (error_found == 1) begin
    \    \$display( "Test Failed!");
    \    \$finish();
    \  end
  }]
}


if {$has_interrupt} { 
  append ipfile_str [subst -nocommands {
  \  enable_interrupts();
  }]
}
append ipfile_str [subst -nocommands {
  \  multiple_iteration(1, error_found);
  \  if (error_found == 1) begin
  \    \$display( "Test Failed!");
  \    \$finish();
  \  end
  \  multiple_iteration(5, error_found);
}]

if {$num_resets >= 1 && $num_clocks >=2} { 
  append ipfile_str [subst -nocommands {
    \  if (error_found == 1) begin
    \    \$display( "Test Failed!");
    \    \$finish();
    \  end
    \  /////////////////////////////////////////////////////////////////////////////////////////////////
    \  // Reset Recovery testing
    \  system_reset_sequence();
    \  check_scalar_registers(error_found);
    \  if (error_found == 1) begin
    \    \$display( "Test Failed!");
    \    \$finish();
    \  end
    \  check_pointer_registers(error_found);
    \  if (error_found == 1) begin
    \    \$display( "Test Failed!");
    \    \$finish();
    \  end
  }]
  if {$has_interrupt} { 
    append ipfile_str [subst -nocommands {
    \  enable_interrupts();
    }]
  }
  append ipfile_str [subst -nocommands {
    \  multiple_iteration(1, error_found);  
  }]
}
append ipfile_str [subst -nocommands {
  \  if (error_found == 1) begin
  \    \$display( "Test Failed!");
  \    \$finish();
  \  end else begin
  \    \$display( "Test completed successfully");
  \  end
  \  \$finish;
  end

endmodule
`default_nettype wire
}]

# Output file
puts_ipfile $ipfile $ipfile_str
unset ipfile_str
close_ipfile $ipfile


