embedded_software_emulation : process

 procedure clean  is
 begin
   ce <= '0';
   we <= '0';
   address <= (others=>'0');
   datain  <= (others=>'0');
   wait_cycles(1);
 end procedure;

 procedure write_bus(addr : unsigned; data : std_logic_vector) is
 begin
   wait_cycles(1);
   ce <= '1';
   we <= '1';
   address <= addr;
   datain  <= data;
   wait_cycles(1);
   ce <= '0';
   we <= '0';
   address <= (others=>'0');
   datain  <= (others=>'0');
 end procedure;

 procedure read_bus(addr:unsigned) is
 begin
   wait_cycles(1);
   ce <= '1';
   we <= '0';
   address <= addr;
   wait_cycles(1);
   ce <= '0';
   address <= (others=>'0');
   datain  <= (others=>'0');
 end procedure;

 variable reg_val : unsigned(63 downto 0);
begin
  clean;
  report "running testbench for ip_ms_mergesort";
  report "waiting for asynchronous reset";
  wait until reset_n='1';
  wait_cycles(10);
  report "applying stimuli...";
  write_bus(ADDR_REG_AP_START,x"0000000000000001");
  write_bus(ADDR_REG_AP_START,x"0000000000000000");

  polling: while true loop
    read_bus(ADDR_REG_AP_DONE);
    reg_val := unsigned(dataout);
    report "dataout = " & integer'image(to_integer(reg_val(31 downto 0)));
    if reg_val=x"0000000000000001" then
      exit polling;
    end if;
  end loop;
  work.tunnels.dump_memory <= true;
  report "";
  wait_cycles(30);
  report "end of simulation";
  running <=false;
  wait;

end process;