Skip to content

File print_buf.h

File List > bindings > simple > print_buf.h

Go to the documentation of this file

#pragma once

#include <systemc.h>
#include <tlm.h>
#include <tlm_utils/simple_target_socket.h>

#include <sol/sol.hpp>
#include <sstream>
#include <unordered_map>

namespace simple {

class PrintBuf : public sc_module {
 public:
  tlm_utils::simple_target_socket<PrintBuf> port_;

  PrintBuf(const sc_module_name &name, uint64_t num_entries,
           std::string filename);
  ~PrintBuf();

  // Callbacks for TLM transactions
  virtual tlm::tlm_sync_enum nb_transport_fw(tlm::tlm_generic_payload &trans,
                                             tlm::tlm_phase &phase, sc_time &t);

  virtual unsigned int transport_dbg(tlm::tlm_generic_payload &trans);

  void ProcessRequest();
  void Response();

  // Lua API implementations
  using LuaBytes = sol::as_table_t<std::vector<uint8_t>>;
  void write_bytes(uint64_t addr, const LuaBytes &data);

  void set_clock(sc_clock *clock);
  sc_clock *clock() const;

  uint64_t num_entries() const { return num_entries_; }

  void FlushBuffer(uint64_t addr);
  void FlushAll();
  void CloseFile();

 private:
  unsigned int ReceiveData(tlm::tlm_generic_payload *trans);

  uint64_t num_entries_;
  std::unordered_map<uint64_t, std::stringstream> buf_;
  sc_clock *clock_;    // pointer to the clock source
  sc_in_clk clock_i_;  // input clock signal
  sc_fifo<tlm::tlm_generic_payload *> req_fifo_;
  sc_fifo<tlm::tlm_generic_payload *> resp_fifo_;
  sc_event resp_end_event_;

  std::ofstream file_;
};

}  // namespace simple