diff --git a/inc/Timer.h b/inc/Timer.h index 331af38..3771275 100644 --- a/inc/Timer.h +++ b/inc/Timer.h @@ -21,51 +21,53 @@ #include "BusCtrl.h" -namespace riscv_tlm::peripherals { +namespace riscv_tlm { + namespace 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 1ef2fcb..7062b3e 100644 --- a/inc/Trace.h +++ b/inc/Trace.h @@ -19,46 +19,48 @@ #include "tlm.h" #include "tlm_utils/simple_target_socket.h" -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: - +namespace riscv_tlm { + namespace peripherals { /** - * @brief Bus socket - */ - tlm_utils::simple_target_socket socket; + * @brief Simple trace peripheral + * + * This peripheral outputs to cout any character written to its unique register + */ + class Trace : sc_core::sc_module { + public: - /** - * @brief Constructor - * @param name Module name - */ - explicit Trace(sc_core::sc_module_name const &name); + /** + * @brief Bus socket + */ + tlm_utils::simple_target_socket socket; - /** - * @brief Destructor - */ - ~Trace() override; + /** + * @brief Constructor + * @param name Module name + */ + explicit Trace(sc_core::sc_module_name const &name); - private: + /** + * @brief Destructor + */ + ~Trace() override; - // TLM-2 blocking transport method - virtual void b_transport(tlm::tlm_generic_payload &trans, - sc_core::sc_time &delay); + private: - void xtermLaunch(char *slaveName) const; + // TLM-2 blocking transport method + virtual void b_transport(tlm::tlm_generic_payload &trans, + sc_core::sc_time &delay); - void xtermKill(); + void xtermLaunch(char *slaveName) const; - void xtermSetup(); + void xtermKill(); - int ptSlave{}; - int ptMaster{}; - int xtermPid{}; - }; + void xtermSetup(); + + int ptSlave{}; + int ptMaster{}; + int xtermPid{}; + }; + } } #endif diff --git a/src/Timer.cpp b/src/Timer.cpp index f82e5a2..7163915 100644 --- a/src/Timer.cpp +++ b/src/Timer.cpp @@ -9,7 +9,8 @@ #include #include "Timer.h" -namespace riscv_tlm::peripherals { +namespace riscv_tlm { +namespace peripherals { SC_HAS_PROCESS(Timer); Timer::Timer(sc_core::sc_module_name const &name) : @@ -101,5 +102,5 @@ namespace riscv_tlm::peripherals { trans.set_response_status(tlm::TLM_OK_RESPONSE); } - + } } \ No newline at end of file diff --git a/src/Trace.cpp b/src/Trace.cpp index 55440bd..46bd698 100644 --- a/src/Trace.cpp +++ b/src/Trace.cpp @@ -20,97 +20,98 @@ #include "Trace.h" -namespace riscv_tlm::peripherals { +namespace riscv_tlm { + namespace 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); + 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); } - char *argv[3]; - argv[0] = (char *) ("xterm"); - argv[1] = arg; - argv[2] = nullptr; + void Trace::xtermKill() { - execvp("xterm", argv); - } + if (-1 != ptSlave) { // Close down the slave + close(ptSlave); // Close the FD + ptSlave = -1; + } - void Trace::xtermKill() { + if (-1 != ptMaster) { // Close down the master + close(ptMaster); + ptMaster = -1; + } - if (-1 != ptSlave) { // Close down the slave - close(ptSlave); // Close the FD - ptSlave = -1; - } - - if (-1 != ptMaster) { // Close down the master - close(ptMaster); - ptMaster = -1; - } - - 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); + 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); + } } - - 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