41class char_backend_stdio :
public sc_core::sc_module
44 cci::cci_param<bool> p_read_write;
45 cci::cci_param<std::string> p_expect;
46 cci::cci_param<std::string> p_highlight;
49 std::atomic_bool m_running;
50 std::thread rcv_thread_id;
51 std::atomic<bool> c_flag;
55 sc_core::sc_event ecmdev;
56 bool processing =
false;
57 static descriptor_t stdin_fd;
60 const static size_t input_buffer_size = 128;
61 INPUT_RECORD irInBuf[input_buffer_size];
66 static console_mode_t old_console_mode;
67 static bool old_console_mode_valid;
71 static void restore_old_mode()
73 BOOL res = SetConsoleMode(stdin_fd, old_console_mode);
79 static void set_raw_mode()
81 if (!old_console_mode_valid && GetConsoleMode(stdin_fd, &old_console_mode)) {
82 old_console_mode_valid =
true;
85 BOOL res = SetConsoleMode(stdin_fd, ENABLE_PROCESSED_INPUT);
88 static void set_default_mode()
90 SetConsoleMode(stdin_fd, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
93 descriptor_t get_stdin_descriptor()
95 descriptor_t stdin_file_descriptor = GetStdHandle(STD_INPUT_HANDLE);
96 if (stdin_file_descriptor == INVALID_HANDLE_VALUE) {
97 SCP_WARN(())(
"char_backend_stdio: Failed to get standard input descriptor. Error: {}", GetLastError());
100 return stdin_file_descriptor;
103 void enqueue_multiple(
const INPUT_RECORD irInBuf[],
const DWORD cNumRead)
105 for (DWORD i = 0; i < cNumRead; ++i) {
106 const INPUT_RECORD& record = irInBuf[i];
107 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
108 if (record.Event.KeyEvent.uChar.AsciiChar == 0x1c) {
112 enqueue(record.Event.KeyEvent.uChar.AsciiChar);
122 DWORD waitResult = WaitForSingleObject(stdin_fd, RCV_POLL_TIMEOUT_MS);
124 if (waitResult == WAIT_TIMEOUT) {
130 }
else if (waitResult == WAIT_OBJECT_0) {
131 if (ReadConsoleInput(stdin_fd, irInBuf, input_buffer_size, &cNumRead)) {
132 enqueue_multiple(irInBuf, cNumRead);
134 SCP_WARN(())(
"char_backend_stdio: Failed to read console input. Error: {}", GetLastError());
142 descriptor_t get_stdin_descriptor() {
return STDIN_FILENO; }
144 static void restore_old_mode()
146 if (tcsetattr(stdin_fd, TCSANOW, &old_console_mode) == -1) {
151 static void set_raw_mode()
154 tcgetattr(stdin_fd, &tty);
155 if (!old_console_mode_valid) {
156 old_console_mode = tty;
157 old_console_mode_valid =
true;
159 tty.c_lflag &= ~(ECHO | ICANON | IEXTEN);
160 tcsetattr(stdin_fd, TCSANOW, &tty);
163 static void set_default_mode()
166 tcgetattr(stdin_fd, &tty);
167 tty.c_lflag |= ECHO | ICANON | IEXTEN;
168 tcsetattr(stdin_fd, TCSANOW, &tty);
173 int fd = STDIN_FILENO;
174 struct pollfd rcv_monitor;
176 rcv_monitor.events = POLLIN;
179 int ret = poll(&rcv_monitor, 1, RCV_POLL_TIMEOUT_MS);
180 if ((ret == -1 && errno == EINTR) || (ret == 0) ) {
186 }
else if (rcv_monitor.revents & POLLIN) {
188 int r = read(fd, &c, 1);
203 void catch_fn(
int signo) {}
205 static void tty_reset()
207 if (old_console_mode_valid) {
215 std::string& rtrim(std::string& s)
217 const char* t =
"\n\r\f\v";
218 s.erase(s.find_last_not_of(t) + 1);
221 std::string getecmdstr(
const std::string& cmd)
223 return ecmd.substr(cmd.length(), ecmd.find_first_of(
"\n") - cmd.length());
230 void expect_process()
232 if (!processing)
return;
233 const std::string expectstr =
"expect ";
234 const std::string sendstr =
"send ";
235 const std::string waitstr =
"wait ";
236 const std::string exitstr =
"exit";
238 if (ecmd.substr(0, expectstr.length()) == expectstr) {
239 std::string str = getecmdstr(expectstr);
240 std::regex restr(str);
241 if (std::regex_search(line, restr)) {
242 SCP_WARN(())(
"Found expect string {}", rtrim(line));
243 ecmd.erase(0, ecmd.find_first_of(
"\n"));
244 if (ecmd !=
"") ecmd.erase(0, 1);
245 if (ecmd.substr(0, expectstr.length()) == expectstr)
break;
249 if (ecmd.substr(0, sendstr.length()) == sendstr) {
250 SCP_WARN(())(
"Sending expect string {}", getecmdstr(sendstr));
251 for (
char c : getecmdstr(sendstr)) {
255 ecmd.erase(0, ecmd.find_first_of(
"\n"));
256 if (ecmd !=
"") ecmd.erase(0, 1);
259 if (ecmd.substr(0, waitstr.length()) == waitstr) {
260 SCP_WARN(())(
"Expect waiting {}s", getecmdstr(waitstr));
262 ecmdev.notify(stod(getecmdstr(waitstr)), sc_core::SC_SEC);
263 ecmd.erase(0, ecmd.find_first_of(
"\n"));
264 if (ecmd !=
"") ecmd.erase(0, 1);
267 if (ecmd.substr(0, exitstr.length()) == exitstr) {
268 SCP_WARN(())(
"Expect caused exit");
286 char_backend_stdio(sc_core::sc_module_name name)
287 : sc_core::sc_module(name)
288 , p_read_write(
"read_write",
true,
"read_write if true start rcv_thread")
289 , p_expect(
"expect",
"",
"string of expect commands")
290 , p_highlight(
"ansi_highlight",
"",
"ANSI highlight code to use for output, default bold")
291 , socket(
"biflow_socket")
295 SCP_TRACE(()) <<
"CharBackendStdio constructor";
297 stdin_fd = get_stdin_descriptor();
302 if (p_expect.get_value() !=
"") {
305 SCP_WARN(())(
"Processing expect string {}", ecmd);
308 gs::SigHandler::get().register_on_exit_cb(std::string(this->name()) +
".char_backend_stdio::tty_reset",
310 gs::SigHandler::get().add_sigint_handler(gs::Handler_CB::PASS);
311 gs::SigHandler::get().register_handler(std::string(this->name()) +
".char_backend_stdio::SIGINT_handler",
313 if (signo == SIGINT) {
317 if (p_read_write) rcv_thread_id = std::thread(&char_backend_stdio::rcv_thread,
this);
319 socket.register_b_transport(
this, &char_backend_stdio::writefn);
320 std::atexit(tty_reset);
323 void end_of_elaboration() { socket.can_receive_any(); }
325 void enqueue(
char c) { socket.enqueue(c); }
327 void writefn(tlm::tlm_generic_payload& txn, sc_core::sc_time& t)
329 uint8_t* data = txn.get_data_ptr();
330 if (!p_highlight.get_value().empty()) std::cout << p_highlight.get_value();
331 for (
int i = 0; i < txn.get_streaming_width(); i++) {
333 if ((
char)data[i] ==
'\n') {
337 line = line + (char)data[i];
340 if (!p_highlight.get_value().empty()) std::cout <<
"\x1B[0m";
344 void start_of_simulation()
346 if (!old_console_mode_valid) {
351 void end_of_simulation() { tty_reset(); }
353 ~char_backend_stdio()
355 gs::SigHandler::get().deregister_on_exit_cb(std::string(name()) +
".char_backend_stdio::tty_reset");
356 gs::SigHandler::get().deregister_handler(std::string(name()) +
".char_backend_stdio::SIGINT_handler");
358 if (rcv_thread_id.joinable()) rcv_thread_id.join();