diff --git a/inc/Timer.h b/inc/Timer.h index 3771275..331af38 100644 --- a/inc/Timer.h +++ b/inc/Timer.h @@ -21,53 +21,51 @@ #include "BusCtrl.h" -namespace riscv_tlm { - namespace peripherals { +namespace riscv_tlm::peripherals { /** * @brief Simple timer peripheral * * It runs a 1 ns (nanoseconds) pace * */ - class Timer : sc_core::sc_module { - public: - // TLM-2 socket, defaults to 32-bits wide, base protocol - tlm_utils::simple_target_socket socket; + class Timer : sc_core::sc_module { + public: + // TLM-2 socket, defaults to 32-bits wide, base protocol + tlm_utils::simple_target_socket socket; - tlm_utils::simple_initiator_socket irq_line; + tlm_utils::simple_initiator_socket irq_line; - /** - * - * @brief Constructor - * @param name module name - */ - explicit Timer(sc_core::sc_module_name const &name); + /** + * + * @brief Constructor + * @param name module name + */ + explicit Timer(sc_core::sc_module_name const &name); - /** - * @brief Waits for event timer_event and triggers an IRQ - * - * Waits for event timer_event and triggers an IRQ (if it is not already - * triggered). - * After that, it posts the timer_event to 20 ns in the future to clear the IRQ - * line. - * - */ - [[noreturn]] void run(); + /** + * @brief Waits for event timer_event and triggers an IRQ + * + * Waits for event timer_event and triggers an IRQ (if it is not already + * triggered). + * After that, it posts the timer_event to 20 ns in the future to clear the IRQ + * line. + * + */ + [[noreturn]] void run(); - /** - * - * @brief TLM-2.0 socket implementation - * @param trans TLM-2.0 transaction - * @param delay transaction delay time - */ - virtual void b_transport(tlm::tlm_generic_payload &trans, - sc_core::sc_time &delay); + /** + * + * @brief TLM-2.0 socket implementation + * @param trans TLM-2.0 transaction + * @param delay transaction delay time + */ + virtual void b_transport(tlm::tlm_generic_payload &trans, + sc_core::sc_time &delay); - private: - sc_dt::sc_uint<64> m_mtime; /**< mtime register */ - sc_dt::sc_uint<64> m_mtimecmp; /**< mtimecmp register */ - sc_core::sc_event timer_event; /**< event */ - }; - } + private: + sc_dt::sc_uint<64> m_mtime; /**< mtime register */ + sc_dt::sc_uint<64> m_mtimecmp; /**< mtimecmp register */ + sc_core::sc_event timer_event; /**< event */ + }; } #endif diff --git a/inc/Trace.h b/inc/Trace.h index 7062b3e..ce1a79a 100644 --- a/inc/Trace.h +++ b/inc/Trace.h @@ -19,48 +19,46 @@ #include "tlm.h" #include "tlm_utils/simple_target_socket.h" -namespace riscv_tlm { - namespace peripherals { +namespace riscv_tlm::peripherals { + /** + * @brief Simple trace peripheral + * + * This peripheral outputs to cout any character written to its unique register + */ + class Trace : sc_core::sc_module { + public: + /** - * @brief Simple trace peripheral - * - * This peripheral outputs to cout any character written to its unique register + * @brief Bus socket */ - class Trace : sc_core::sc_module { - public: + tlm_utils::simple_target_socket socket; - /** - * @brief Bus socket - */ - tlm_utils::simple_target_socket socket; + /** + * @brief Constructor + * @param name Module name + */ + explicit Trace(sc_core::sc_module_name const &name); - /** - * @brief Constructor - * @param name Module name - */ - explicit Trace(sc_core::sc_module_name const &name); + /** + * @brief Destructor + */ + ~Trace() override; - /** - * @brief Destructor - */ - ~Trace() override; + private: - private: + // TLM-2 blocking transport method + virtual void b_transport(tlm::tlm_generic_payload &trans, + sc_core::sc_time &delay); - // TLM-2 blocking transport method - virtual void b_transport(tlm::tlm_generic_payload &trans, - sc_core::sc_time &delay); + void xtermLaunch(char *slaveName) const; - void xtermLaunch(char *slaveName) const; + void xtermKill(); - void xtermKill(); + void xtermSetup(); - void xtermSetup(); - - int ptSlave{}; - int ptMaster{}; - int xtermPid{}; - }; - } + int ptSlave{}; + int ptMaster{}; + int xtermPid{}; + }; } #endif diff --git a/src/Trace.cpp b/src/Trace.cpp index 46bd698..a8bd187 100644 --- a/src/Trace.cpp +++ b/src/Trace.cpp @@ -20,98 +20,96 @@ #include "Trace.h" -namespace riscv_tlm { - namespace peripherals { +namespace riscv_tlm::peripherals { - void Trace::xtermLaunch(char *slaveName) const { - char *arg; - char *fin = &(slaveName[strlen(slaveName) - 2]); + void Trace::xtermLaunch(char *slaveName) const { + char *arg; + char *fin = &(slaveName[strlen(slaveName) - 2]); - if (nullptr == strchr(fin, '/')) { - arg = new char[2 + 1 + 1 + 20 + 1]; - sprintf(arg, "-S%c%c%d", fin[0], fin[1], ptMaster); - } else { - char *slaveBase = ::basename(slaveName); - arg = new char[2 + strlen(slaveBase) + 1 + 20 + 1]; - sprintf(arg, "-S%s/%d", slaveBase, ptMaster); - } - - char *argv[3]; - argv[0] = (char *) ("xterm"); - argv[1] = arg; - argv[2] = nullptr; - - execvp("xterm", argv); + if (nullptr == strchr(fin, '/')) { + arg = new char[2 + 1 + 1 + 20 + 1]; + sprintf(arg, "-S%c%c%d", fin[0], fin[1], ptMaster); + } else { + char *slaveBase = ::basename(slaveName); + arg = new char[2 + strlen(slaveBase) + 1 + 20 + 1]; + sprintf(arg, "-S%s/%d", slaveBase, ptMaster); } - void Trace::xtermKill() { + char *argv[3]; + argv[0] = (char *) ("xterm"); + argv[1] = arg; + argv[2] = nullptr; - if (-1 != ptSlave) { // Close down the slave - close(ptSlave); // Close the FD - ptSlave = -1; - } + execvp("xterm", argv); + } - if (-1 != ptMaster) { // Close down the master - close(ptMaster); - ptMaster = -1; - } + void Trace::xtermKill() { - if (xtermPid > 0) { // Kill the terminal - kill(xtermPid, SIGKILL); - waitpid(xtermPid, nullptr, 0); - } + if (-1 != ptSlave) { // Close down the slave + close(ptSlave); // Close the FD + ptSlave = -1; } - void Trace::xtermSetup() { - ptMaster = open("/dev/ptmx", O_RDWR); - - if (ptMaster != -1) { - grantpt(ptMaster); - - unlockpt(ptMaster); - - char *ptSlaveName = ptsname(ptMaster); - ptSlave = open(ptSlaveName, O_RDWR); // In and out are the same - - struct termios termInfo{}; - tcgetattr(ptSlave, &termInfo); - - termInfo.c_lflag &= ~ECHO; - termInfo.c_lflag &= ~ICANON; - tcsetattr(ptSlave, TCSADRAIN, &termInfo); - - xtermPid = fork(); - - if (xtermPid == 0) { - xtermLaunch(ptSlaveName); - } - } + if (-1 != ptMaster) { // Close down the master + close(ptMaster); + ptMaster = -1; } - SC_HAS_PROCESS(Trace); - - Trace::Trace(sc_core::sc_module_name const &name) : - sc_module(name), socket("socket") { - - socket.register_b_transport(this, &Trace::b_transport); - - xtermSetup(); - } - - Trace::~Trace() { - xtermKill(); - } - - void Trace::b_transport(tlm::tlm_generic_payload &trans, - sc_core::sc_time &delay) { - - unsigned char *ptr = trans.get_data_ptr(); - delay = sc_core::SC_ZERO_TIME; - - ssize_t a = write(ptSlave, ptr, 1); - (void) a; - - trans.set_response_status(tlm::TLM_OK_RESPONSE); + if (xtermPid > 0) { // Kill the terminal + kill(xtermPid, SIGKILL); + waitpid(xtermPid, nullptr, 0); } } + + void Trace::xtermSetup() { + ptMaster = open("/dev/ptmx", O_RDWR); + + if (ptMaster != -1) { + grantpt(ptMaster); + + unlockpt(ptMaster); + + char *ptSlaveName = ptsname(ptMaster); + ptSlave = open(ptSlaveName, O_RDWR); // In and out are the same + + struct termios termInfo{}; + tcgetattr(ptSlave, &termInfo); + + termInfo.c_lflag &= ~ECHO; + termInfo.c_lflag &= ~ICANON; + tcsetattr(ptSlave, TCSADRAIN, &termInfo); + + xtermPid = fork(); + + if (xtermPid == 0) { + xtermLaunch(ptSlaveName); + } + } + } + + SC_HAS_PROCESS(Trace); + + Trace::Trace(sc_core::sc_module_name const &name) : + sc_module(name), socket("socket") { + + socket.register_b_transport(this, &Trace::b_transport); + + xtermSetup(); + } + + Trace::~Trace() { + xtermKill(); + } + + void Trace::b_transport(tlm::tlm_generic_payload &trans, + sc_core::sc_time &delay) { + + unsigned char *ptr = trans.get_data_ptr(); + delay = sc_core::SC_ZERO_TIME; + + ssize_t a = write(ptSlave, ptr, 1); + (void) a; + + trans.set_response_status(tlm::TLM_OK_RESPONSE); + } } \ No newline at end of file