keroroishappy Posted May 19, 2023 Report Share Posted May 19, 2023 /////////////////stim.h///////////////////////////////////////////////// #ifndef _STIM_H_ #define _STIM_H_ #include <iomanip> #include "short_path.h" using std::setw; SC_MODULE(stim) { public: sc_in_clk i_clk; int temp_a; int temp_b; int temp_c; sc_out<int> o_src_port; sc_fifo_out<sc_uint<WIDTH> > o_a_port; sc_fifo_out<sc_uint<WIDTH> > o_b_port; sc_fifo_out<sc_uint<WIDTH> > o_c_port; sc_fifo_in<sc_uint<WIDTH+1> > i_dis_port; sc_fifo_in<sc_uint<WIDTH+1> > i_point_port; //Store the previous inputs to FIFOs int point; int dis; int a_arr[17]={0,0,1,1,2,2,2,3,3,4,5,6,6,7,7,9,11}; int b_arr[17]={1,7,2,1,3,8,5,4,5,5,6,7,8,8,3,1,2}; int c_arr[17]={4,8,8,11,7,2,4,9,14,10,2,1,6,7,3,2,3}; SC_CTOR(stim) { SC_THREAD(stim_gen); sensitive << i_clk.pos(); dont_initialize(); SC_THREAD(sink); sensitive << i_clk.pos(); dont_initialize(); } void stim_gen() { int src_val = 2; o_src_port.write(src_val); cout << setw(12) << "time" << setw(12) << "a" << setw(12) << "b" << setw(12) << "dis"<<endl; for (int i = 0; i < 17; i++) { temp_a = a_arr[i]; temp_b = b_arr[i]; temp_c = c_arr[i]; o_a_port.write(temp_a); o_b_port.write(temp_b); o_c_port.write(temp_c); cout << setw(12) << sc_time_stamp(); cout << setw(12) << a_arr[i]; cout << setw(12) << b_arr[i]; cout << setw(12) << c_arr[i] << endl; } } void sink() { while (true) { point=i_point_port.read(); dis=i_dis_port.read(); cout << setw(12) << sc_time_stamp(); cout << setw(12) << point; cout << setw(12) << dis << endl; wait(); } } }; #endif ////////////////////short_path.h///////////////////////////////// #include <iostream> using namespace std; #include <systemc> #include <list> #include <vector> #include <queue> using namespace sc_dt; using namespace sc_core; typedef pair<int, int> iPair; const int WIDTH = 10; #define INF 0x3f3f3f3f class short_path : public sc_module { public: int src; int V; list<pair<int, int>> *adj; sc_in_clk i_clk; sc_in<int> i_src_port; sc_fifo_in<sc_uint<WIDTH> > i_a_port; sc_fifo_in<sc_uint<WIDTH> > i_b_port; sc_fifo_in<sc_uint<WIDTH> > i_c_port; sc_fifo_out<sc_uint<WIDTH+1> > o_point_port; sc_fifo_out<sc_uint<WIDTH+1> > o_dis_port; SC_CTOR(short_path){ SC_THREAD(addEdge); sensitive << i_clk.pos(); dont_initialize(); SC_THREAD(shortestPathingraph); sensitive << i_clk.pos(); dont_initialize(); } void addEdge() { while (true) { _i_a = i_a_port.read(); cout <<"_i_a : "<< _i_a << endl; _i_b = i_b_port.read(); cout <<"_i_b : "<< _i_b << endl; _i_c = i_c_port.read(); cout <<"_i_c : "<< _i_c << endl; wait(); adj[_i_a].push_back(make_pair(_i_b, _i_c)); adj[_i_b].push_back(make_pair(_i_a, _i_c)); } } void shortestPathingraph(){ while(true){ src = i_src_port.read(); cout<<"src received and src value is : "<<src<<endl; priority_queue<iPair, vector<iPair>, greater<iPair>> pq; vector<int> dist(V, INF); pq.push(make_pair(0, src)); dist[src] = 0; while (!pq.empty()) { int u = pq.top().second; pq.pop(); list<pair<int, int>>::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) { int v = (*i).first; int weight = (*i).second; if (dist[v] > dist[u] + weight) { dist[v] = dist[u] + weight; pq.push(make_pair(dist[v], v)); } } } for (int i = 0; i < V; ++i){ printf("%d \t\t %d\n", i, dist[i]); o_point_port.write(dist[i]); o_dis_port.write(i); } } } //sc_uint<WIDTH> i_a() { return _i_a; } //sc_uint<WIDTH> i_b() { return _i_b; } //sc_uint<WIDTH> o_sum() { return _o_sum; } private: sc_uint<WIDTH> _i_a; sc_uint<WIDTH> _i_b; sc_uint<WIDTH> _i_c; //sc_uint<WIDTH + 1> _o_point; //sc_uint<WIDTH + 1> _o_dis; }; ///////////////main/////////////////////////////// #include "stim.h" #define CLOCK_PERIOD 1.0 int sc_main(int argc, char *argv[]) { //Create modules and signals stim testbench("testbench"); short_path dut("dut"); sc_clock clk("clk", CLOCK_PERIOD, SC_NS); sc_signal<int> src("src"); //Create FIFO channels sc_fifo<sc_uint<WIDTH> > fifo_i_a; sc_fifo<sc_uint<WIDTH> > fifo_i_b; sc_fifo<sc_uint<WIDTH> > fifo_i_c; sc_fifo<sc_uint<WIDTH+1> > fifo_o_dis; sc_fifo<sc_uint<WIDTH+1> > fifo_o_point; //Connect FIFO channels with modules testbench.i_clk(clk); testbench.o_src_port(src); dut.i_clk(clk); dut.i_src_port(src); testbench.o_a_port(fifo_i_a); testbench.o_b_port(fifo_i_b); testbench.o_c_port(fifo_i_c); testbench.i_dis_port(fifo_o_dis); testbench.i_point_port(fifo_o_point); dut.i_a_port(fifo_i_a); dut.i_b_port(fifo_i_b); dut.i_c_port(fifo_i_c); dut.o_dis_port(fifo_o_dis); dut.o_point_port(fifo_o_point); sc_start(); return 0; } Hi everyone the code above is my systemC practice i use "stim.h" to deliver my data to "short_path.h" (dijkstra algo) through fifo but "short_path.h" cannot receive the data from "stim.h" i try my effort to debug and want to find the problem but have no idea. hope someone could tell me where is the problem thank u Quote Link to comment Share on other sites More sharing options...
Andy Goodrich Posted May 19, 2023 Report Share Posted May 19, 2023 I believe your problem is with the pointer variable, adj. I assume you want an array of lists to be associated with adj, but you have never allocated storage for that array of lists. So when you access adj in short_path.h you get a segment fault. keroroishappy 1 Quote Link to comment Share on other sites More sharing options...
keroroishappy Posted May 19, 2023 Author Report Share Posted May 19, 2023 1 hour ago, Andy Goodrich said: I believe your problem is with the pointer variable, adj. I assume you want an array of lists to be associated with adj, but you have never allocated storage for that array of lists. So when you access adj in short_path.h you get a segment fault. thank you i sloved the problem, but i have another question when i use c++(not systemC), it is ok without specifying "adj" with specific value like " list<pair<int, int>> adj[10] ". I can just use pointer " list<pair<int, int>> *adj " what make me have to use this form ( list<pair<int, int>> adj[] ) instead of pointer thank u Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.