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"
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<Timer> socket;
class Timer : sc_core::sc_module {
public:
// TLM-2 socket, defaults to 32-bits wide, base protocol
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
* @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

View File

@ -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<Trace> socket;
/**
* @brief Bus socket
*/
tlm_utils::simple_target_socket<Trace> 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

View File

@ -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);
}
}