1
+ #include " systemc.h"
2
+ #include " iostream"
3
+ #define CLOCK_PULSE 2 // In nano Seconds
4
+ using namespace std ;
5
+ class HCLK :public sc_module {
6
+ private:
7
+
8
+ int w;
9
+
10
+ public:
11
+
12
+ sc_signal<sc_bit> clk;
13
+
14
+ SC_HAS_PROCESS (HCLK);
15
+
16
+ HCLK (sc_module_name name,int v):sc_module(name),w(v){
17
+
18
+ clk=sc_bit (' 1' );
19
+
20
+ SC_THREAD (start_clk);
21
+
22
+ }
23
+
24
+ void start_clk (){
25
+
26
+ for (int i=0 ;i<10 ;i++){
27
+
28
+ cout<<" # " <<sc_time_stamp ()<<" Clock Pulse: " <<false <<endl;
29
+
30
+ clk=sc_bit (' 0' );
31
+
32
+ wait (w,SC_NS);
33
+
34
+ clk=sc_bit (' 1' );
35
+
36
+ cout<<" # " <<sc_time_stamp ()<<" Clock Pulse: " <<true <<endl;
37
+
38
+ wait (w,SC_NS);
39
+ }
40
+
41
+
42
+ }
43
+ };
44
+ HCLK t (" C" ,CLOCK_PULSE);
45
+
46
+ class master :public sc_module {
47
+ public:
48
+
49
+ sc_port<sc_fifo_out_if<sc_uint<32 > > > send_list;
50
+
51
+ sc_signal<sc_bit> hwrite; // high -- write to slave; //low -- read from slave
52
+
53
+ int r_val; // random value to be written
54
+
55
+ SC_HAS_PROCESS (master);
56
+
57
+ master (sc_module_name name):sc_module(name){
58
+
59
+ SC_THREAD (writer_function);
60
+
61
+ }
62
+ void writer_function (){
63
+ wait (CLOCK_PULSE,SC_NS);
64
+ for (int i=0 ;i<5 ;i++){
65
+ if (t.clk ==sc_bit (' 0' ))
66
+ {
67
+ cout<<" # Random value written at: " <<sc_time_stamp ()<<endl;
68
+ cout<<" # Value: " <<r_val<<" \n\n " ;
69
+ r_val=rand ()%1024 ;
70
+ send_list->write (r_val);
71
+ wait (CLOCK_PULSE,SC_NS);
72
+ }
73
+ }
74
+ }
75
+ };
76
+
77
+ class slave :public sc_module {
78
+ public:
79
+
80
+ sc_port<sc_fifo_in_if<sc_uint<32 > > > reciever_list;
81
+
82
+ int r_val;
83
+
84
+ SC_HAS_PROCESS (slave);
85
+
86
+ slave (sc_module_name name):sc_module(name){
87
+
88
+ SC_THREAD (reader_function);
89
+
90
+ }
91
+ void reader_function ()
92
+ {
93
+ wait (CLOCK_PULSE,SC_NS);
94
+ for (int i=0 ;i<5 ;i++){
95
+ if (t.clk ==sc_bit (' 0' ))
96
+ {
97
+ wait (CLOCK_PULSE,SC_NS);
98
+ int temp=reciever_list->read ();
99
+ cout<<" @ Value Read From Master at: " <<sc_time_stamp ()<<endl;
100
+ cout<<" @ Value: " <<temp<<endl<<endl;
101
+
102
+ }
103
+ }
104
+
105
+ }
106
+ };
107
+ class top : public sc_module
108
+ {
109
+ public:
110
+
111
+ sc_fifo <sc_uint<32 > > *fifo_inst;
112
+
113
+ master *m;
114
+ slave *s;
115
+
116
+ top (sc_module_name name) : sc_module(name)
117
+ {
118
+
119
+ fifo_inst = new sc_fifo<sc_uint<32 > >(100 );
120
+
121
+ m = new master (" Master" );
122
+ m->send_list (*fifo_inst);
123
+
124
+ s = new slave (" Slave1" );
125
+ s->reciever_list (*fifo_inst);
126
+ }
127
+ };
128
+
129
+ int sc_main (int argc , char *argv[])
130
+ {
131
+ // sc_clock clock ("my_clock",10,0.5);
132
+ // setting clock frequency to 10 Ns.
133
+ top t1 (" Main" );
134
+ sc_start (); // starting the functions
135
+ return 0 ; // terminate code
136
+ }
0 commit comments