Use nested namespaces

This commit is contained in:
Màrius Montón 2022-10-06 17:23:14 +02:00
parent 61fbe8cecd
commit 6fa13b2056
No known key found for this signature in database
GPG Key ID: FA199E7A752699F0
3 changed files with 145 additions and 151 deletions

View File

@ -21,53 +21,51 @@
#include "BusCtrl.h" #include "BusCtrl.h"
namespace riscv_tlm { namespace riscv_tlm::peripherals {
namespace peripherals {
/** /**
* @brief Simple timer peripheral * @brief Simple timer peripheral
* *
* It runs a 1 ns (nanoseconds) pace * It runs a 1 ns (nanoseconds) pace
* *
*/ */
class Timer : sc_core::sc_module { class Timer : sc_core::sc_module {
public: public:
// TLM-2 socket, defaults to 32-bits wide, base protocol // TLM-2 socket, defaults to 32-bits wide, base protocol
tlm_utils::simple_target_socket<Timer> socket; tlm_utils::simple_target_socket<Timer> socket;
tlm_utils::simple_initiator_socket<Timer> irq_line; tlm_utils::simple_initiator_socket<Timer> irq_line;
/** /**
* *
* @brief Constructor * @brief Constructor
* @param name module name * @param name module name
*/ */
explicit Timer(sc_core::sc_module_name const &name); explicit Timer(sc_core::sc_module_name const &name);
/** /**
* @brief Waits for event timer_event and triggers an IRQ * @brief Waits for event timer_event and triggers an IRQ
* *
* Waits for event timer_event and triggers an IRQ (if it is not already * Waits for event timer_event and triggers an IRQ (if it is not already
* triggered). * triggered).
* After that, it posts the timer_event to 20 ns in the future to clear the IRQ * After that, it posts the timer_event to 20 ns in the future to clear the IRQ
* line. * line.
* *
*/ */
[[noreturn]] void run(); [[noreturn]] void run();
/** /**
* *
* @brief TLM-2.0 socket implementation * @brief TLM-2.0 socket implementation
* @param trans TLM-2.0 transaction * @param trans TLM-2.0 transaction
* @param delay transaction delay time * @param delay transaction delay time
*/ */
virtual void b_transport(tlm::tlm_generic_payload &trans, virtual void b_transport(tlm::tlm_generic_payload &trans,
sc_core::sc_time &delay); sc_core::sc_time &delay);
private: private:
sc_dt::sc_uint<64> m_mtime; /**< mtime register */ sc_dt::sc_uint<64> m_mtime; /**< mtime register */
sc_dt::sc_uint<64> m_mtimecmp; /**< mtimecmp register */ sc_dt::sc_uint<64> m_mtimecmp; /**< mtimecmp register */
sc_core::sc_event timer_event; /**< event */ sc_core::sc_event timer_event; /**< event */
}; };
}
} }
#endif #endif

View File

@ -19,48 +19,46 @@
#include "tlm.h" #include "tlm.h"
#include "tlm_utils/simple_target_socket.h" #include "tlm_utils/simple_target_socket.h"
namespace riscv_tlm { namespace riscv_tlm::peripherals {
namespace 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 * @brief Bus socket
*
* This peripheral outputs to cout any character written to its unique register
*/ */
class Trace : sc_core::sc_module { tlm_utils::simple_target_socket<Trace> socket;
public:
/** /**
* @brief Bus socket * @brief Constructor
*/ * @param name Module name
tlm_utils::simple_target_socket<Trace> socket; */
explicit Trace(sc_core::sc_module_name const &name);
/** /**
* @brief Constructor * @brief Destructor
* @param name Module name */
*/ ~Trace() override;
explicit Trace(sc_core::sc_module_name const &name);
/** private:
* @brief Destructor
*/
~Trace() override;
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 void xtermLaunch(char *slaveName) const;
virtual void b_transport(tlm::tlm_generic_payload &trans,
sc_core::sc_time &delay);
void xtermLaunch(char *slaveName) const; void xtermKill();
void xtermKill(); void xtermSetup();
void xtermSetup(); int ptSlave{};
int ptMaster{};
int ptSlave{}; int xtermPid{};
int ptMaster{}; };
int xtermPid{};
};
}
} }
#endif #endif

View File

@ -20,98 +20,96 @@
#include "Trace.h" #include "Trace.h"
namespace riscv_tlm { namespace riscv_tlm::peripherals {
namespace peripherals {
void Trace::xtermLaunch(char *slaveName) const { void Trace::xtermLaunch(char *slaveName) const {
char *arg; char *arg;
char *fin = &(slaveName[strlen(slaveName) - 2]); char *fin = &(slaveName[strlen(slaveName) - 2]);
if (nullptr == strchr(fin, '/')) { if (nullptr == strchr(fin, '/')) {
arg = new char[2 + 1 + 1 + 20 + 1]; arg = new char[2 + 1 + 1 + 20 + 1];
sprintf(arg, "-S%c%c%d", fin[0], fin[1], ptMaster); sprintf(arg, "-S%c%c%d", fin[0], fin[1], ptMaster);
} else { } else {
char *slaveBase = ::basename(slaveName); char *slaveBase = ::basename(slaveName);
arg = new char[2 + strlen(slaveBase) + 1 + 20 + 1]; arg = new char[2 + strlen(slaveBase) + 1 + 20 + 1];
sprintf(arg, "-S%s/%d", slaveBase, ptMaster); sprintf(arg, "-S%s/%d", slaveBase, ptMaster);
}
char *argv[3];
argv[0] = (char *) ("xterm");
argv[1] = arg;
argv[2] = nullptr;
execvp("xterm", argv);
} }
void Trace::xtermKill() { char *argv[3];
argv[0] = (char *) ("xterm");
argv[1] = arg;
argv[2] = nullptr;
if (-1 != ptSlave) { // Close down the slave execvp("xterm", argv);
close(ptSlave); // Close the FD }
ptSlave = -1;
}
if (-1 != ptMaster) { // Close down the master void Trace::xtermKill() {
close(ptMaster);
ptMaster = -1;
}
if (xtermPid > 0) { // Kill the terminal if (-1 != ptSlave) { // Close down the slave
kill(xtermPid, SIGKILL); close(ptSlave); // Close the FD
waitpid(xtermPid, nullptr, 0); ptSlave = -1;
}
} }
void Trace::xtermSetup() { if (-1 != ptMaster) { // Close down the master
ptMaster = open("/dev/ptmx", O_RDWR); close(ptMaster);
ptMaster = -1;
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); if (xtermPid > 0) { // Kill the terminal
kill(xtermPid, SIGKILL);
Trace::Trace(sc_core::sc_module_name const &name) : waitpid(xtermPid, nullptr, 0);
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);
} }
} }
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);
}
} }