quic/qbox
Loading...
Searching...
No Matches
char_backend_file.h
1/*
2 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
12#ifndef _GS_UART_BACKEND_FILE_H_
13#define _GS_UART_BACKEND_FILE_H_
14
15#include <systemc>
16#include <tlm.h>
17#include <tlm_utils/simple_target_socket.h>
18
19#include <unistd.h>
20
21#include <async_event.h>
22#include <uutils.h>
23#include <ports/biflow-socket.h>
24#include <module_factory_registery.h>
25
26#include <queue>
27#include <stdlib.h>
28#include <scp/report.h>
29class char_backend_file : public sc_core::sc_module
30{
31protected:
32 cci::cci_param<std::string> p_read_file;
33 cci::cci_param<std::string> p_write_file;
34 cci::cci_param<unsigned int> p_baudrate;
35
36private:
37 FILE* r_file;
38 FILE* w_file;
39 double delay;
40 SCP_LOGGER();
41
42public:
44 sc_core::sc_event update_event;
45
46#ifdef WIN32
47#pragma message("char_backend_file not yet implemented for WIN32")
48#endif
54 char_backend_file(sc_core::sc_module_name name)
55 : sc_core::sc_module(name)
56 , p_read_file("read_file", "", "read file path")
57 , p_write_file("write_file", "", "write file path")
58 , p_baudrate("baudrate", 0, "number of bytes per second")
59 , socket("biflow_socket")
60 {
61 SCP_TRACE(()) << "constructor";
62
63 SC_THREAD(rcv_thread);
64 sensitive << update_event;
65
66 socket.register_b_transport(this, &char_backend_file::writefn);
67 }
68 void start_of_simulation()
69 {
70 if (!p_read_file.get_value().empty()) {
71 r_file = fopen(p_read_file.get_value().c_str(), "r");
72
73 if (r_file == NULL) SCP_ERR(()) << "Error opening the file.\n";
74 update_event.notify(sc_core::SC_ZERO_TIME);
75 } else {
76 SCP_ERR(()) << "Error reading the path of p_read_file.\n";
77 }
78
79 if (!p_write_file.get_value().empty()) {
80 w_file = fopen(p_write_file.get_value().c_str(), "w");
81
82 if (w_file == NULL) SCP_ERR(()) << "Error opening the file.\n";
83
84 socket.can_receive_any();
85 } else {
86 SCP_ERR(()) << "Error reading the path of p_write_file.\n";
87 }
88 }
89 void end_of_elaboration() {}
90
91 void rcv_thread()
92 {
93 if (p_baudrate.get_value() == 0)
94 delay = 0;
95 else
96 delay = (1.0 / p_baudrate.get_value());
97 char c;
98 while (fread(&c, sizeof(char), 1, r_file) == 1) {
99 socket.enqueue(c);
100 sc_core::wait(delay, sc_core::SC_SEC);
101 }
102 socket.enqueue(EOF);
103 fclose(r_file);
104 }
105
106 void writefn(tlm::tlm_generic_payload& txn, sc_core::sc_time& t)
107 {
108 uint8_t* data = txn.get_data_ptr();
109 for (int i = 0; i < txn.get_streaming_width(); i++) {
110 size_t ret = fwrite(&data[i], sizeof(uint8_t), 1, w_file);
111 if (ret != 1) {
112 SCP_ERR(()) << "Error writing to the file.\n";
113 }
114 }
115 fclose(w_file);
116 }
117
119};
120// GSC_MODULE_REGISTER(char_backend_file);
121extern "C" void module_register();
122#endif
Definition target.h:160
Definition char_backend_file.h:30
Definition biflow-socket.h:73
void can_receive_any()
can_receive_any Allow unlimited items to arrive.
Definition biflow-socket.h:264
void enqueue(T data)
enqueue Enqueue data to be sent (unlimited queue size) NOTE: Thread safe.
Definition biflow-socket.h:277
void register_b_transport(MODULE *mod, void(MODULE::*cb)(tlm::tlm_generic_payload &, sc_core::sc_time &))
Register b_transport to be called whenever data is received from the socket.
Definition biflow-socket.h:227