| 1 | #include <systemc.h> |
|---|
| 2 | |
|---|
| 3 | #define ASSERT(x) \ |
|---|
| 4 | { errnum++; \ |
|---|
| 5 | if (!(x)) \ |
|---|
| 6 | { \ |
|---|
| 7 | cerr << "ASSERT : " #x "\n"; \ |
|---|
| 8 | exit (errnum); \ |
|---|
| 9 | } \ |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | using namespace std; |
|---|
| 13 | |
|---|
| 14 | struct inner : sc_module { |
|---|
| 15 | sc_in_clk clk; |
|---|
| 16 | sc_in<int> i1; |
|---|
| 17 | int reg; |
|---|
| 18 | |
|---|
| 19 | void save (ostream &o) |
|---|
| 20 | { |
|---|
| 21 | o << reg << endl; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | void save_state (FILE *fic) |
|---|
| 25 | { |
|---|
| 26 | cerr << "saving " << name () << "\n"; |
|---|
| 27 | fprintf (fic, "%d\n", reg); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | void restore_state (FILE *fic) |
|---|
| 31 | { |
|---|
| 32 | cerr << "restoring " << name () << "\n"; |
|---|
| 33 | int i; |
|---|
| 34 | fscanf (fic, "%d\n", &i); |
|---|
| 35 | reg = i; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | void trans () |
|---|
| 39 | { |
|---|
| 40 | reg = i1.read(); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | SC_HAS_PROCESS(inner); |
|---|
| 44 | inner (sc_module_name n) : sc_module (n), |
|---|
| 45 | clk("clk"), |
|---|
| 46 | i1("i1") |
|---|
| 47 | { |
|---|
| 48 | SC_METHOD(trans); |
|---|
| 49 | sensitive << clk.pos(); |
|---|
| 50 | dont_initialize(); |
|---|
| 51 | #ifdef SYSTEMCASS_SPECIFIC |
|---|
| 52 | //SAVE_HANDLER(save); |
|---|
| 53 | SAVE_HANDLER(save_state); |
|---|
| 54 | #endif |
|---|
| 55 | }; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | struct test : sc_module { |
|---|
| 59 | sc_in_clk clk; |
|---|
| 60 | sc_in<bool> i1; |
|---|
| 61 | sc_in<int> i2; |
|---|
| 62 | sc_in<int> i3; |
|---|
| 63 | inner inner1; |
|---|
| 64 | |
|---|
| 65 | int tab[16]; |
|---|
| 66 | bool b; |
|---|
| 67 | |
|---|
| 68 | void trans () |
|---|
| 69 | { |
|---|
| 70 | b = i1.read() ^ b; |
|---|
| 71 | tab[i3.read() % 16] = i2; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | void save (ostream &o) |
|---|
| 76 | { |
|---|
| 77 | o << ((b)?1:0) << endl; |
|---|
| 78 | int i; |
|---|
| 79 | for (i = 0; i < 16; ++i) |
|---|
| 80 | o << tab[i] << endl; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void save_state (FILE *fic) |
|---|
| 84 | { |
|---|
| 85 | cerr << "saving " << name () << "\n"; |
|---|
| 86 | fprintf (fic, "%c\n", ((b)?'1':'0')); |
|---|
| 87 | int i; |
|---|
| 88 | for (i = 0; i < 16; ++i) |
|---|
| 89 | { |
|---|
| 90 | fprintf (fic, "%d\n", tab[i]); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void restore_state (FILE *fic) |
|---|
| 95 | { |
|---|
| 96 | cerr << "restoring " << name () << "\n"; |
|---|
| 97 | int j; |
|---|
| 98 | fscanf (fic, "%d\n", &j); |
|---|
| 99 | b = (j > 0); |
|---|
| 100 | int i; |
|---|
| 101 | for (i = 0; i < 16; ++i) |
|---|
| 102 | { |
|---|
| 103 | fscanf (fic, "%d\n", &j); |
|---|
| 104 | tab[i] = j; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | SC_HAS_PROCESS(test); |
|---|
| 109 | test (sc_module_name n) : sc_module (n), |
|---|
| 110 | clk("clk"), |
|---|
| 111 | i1("i1"), i2("i2"), i3("i3"), |
|---|
| 112 | inner1 ("inner1") |
|---|
| 113 | { |
|---|
| 114 | SC_METHOD(trans); |
|---|
| 115 | sensitive << clk.pos(); |
|---|
| 116 | dont_initialize(); |
|---|
| 117 | #ifdef SYSTEMCASS_SPECIFIC |
|---|
| 118 | //SAVE_HANDLER(save); |
|---|
| 119 | SAVE_HANDLER(save_state); |
|---|
| 120 | #endif |
|---|
| 121 | }; |
|---|
| 122 | }; |
|---|
| 123 | |
|---|
| 124 | int |
|---|
| 125 | usage (const char *com) |
|---|
| 126 | { |
|---|
| 127 | cout << "Usage :\n" << com << " [#cycles]\n"; |
|---|
| 128 | return EXIT_FAILURE; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | sc_signal<bool> s01 ("bool"); |
|---|
| 132 | sc_signal<int> s02 ("tab_index"), |
|---|
| 133 | s03 ("value_to_write_in_tab"); |
|---|
| 134 | |
|---|
| 135 | void* |
|---|
| 136 | func () |
|---|
| 137 | { |
|---|
| 138 | cerr << "func () at #" << sc_time_stamp () << endl; |
|---|
| 139 | int i = (int)(sc_time_stamp ().to_double ()) / 1000; |
|---|
| 140 | s01 = (i & 1) > 0; |
|---|
| 141 | s02 = (i + (i << 1)) << 6; |
|---|
| 142 | s03 = i; |
|---|
| 143 | ++i; |
|---|
| 144 | return 0; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void |
|---|
| 148 | save () |
|---|
| 149 | { |
|---|
| 150 | #ifdef SYSTEMCASS_SPECIFIC |
|---|
| 151 | int current_cycle = ((int)sc_time_stamp().to_double() / 1000); |
|---|
| 152 | if (current_cycle != 20) |
|---|
| 153 | return; |
|---|
| 154 | char name[256]; |
|---|
| 155 | sprintf (name,"test2_systemcass_%d.dat",current_cycle); |
|---|
| 156 | sc_save_simulation (name); |
|---|
| 157 | #endif |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | int sc_main (int argc, char *argv[]) |
|---|
| 161 | { |
|---|
| 162 | int errnum = 0; |
|---|
| 163 | sc_clock signal_clk("my_clock",1, 0.5); |
|---|
| 164 | |
|---|
| 165 | test test1("test1"); |
|---|
| 166 | test1.clk (signal_clk); |
|---|
| 167 | test1.i1 (s01); |
|---|
| 168 | test1.i2 (s02); |
|---|
| 169 | test1.i3 (s03); |
|---|
| 170 | test1.inner1.clk (signal_clk); |
|---|
| 171 | test1.inner1.i1 (s02); |
|---|
| 172 | |
|---|
| 173 | // Init & run |
|---|
| 174 | sc_start (0); |
|---|
| 175 | |
|---|
| 176 | #ifndef SOCVIEW |
|---|
| 177 | if (argc != 2) |
|---|
| 178 | { |
|---|
| 179 | return usage (argv[0]); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | int nb = atoi(argv[1]); |
|---|
| 183 | |
|---|
| 184 | if (nb == 0) |
|---|
| 185 | { |
|---|
| 186 | return usage (argv[0]); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | int i = 0; |
|---|
| 190 | while (i++ < nb) |
|---|
| 191 | { |
|---|
| 192 | func (); |
|---|
| 193 | sc_start (1); |
|---|
| 194 | save (); |
|---|
| 195 | } |
|---|
| 196 | #else |
|---|
| 197 | debug(&func); |
|---|
| 198 | #endif |
|---|
| 199 | |
|---|
| 200 | return EXIT_SUCCESS; |
|---|
| 201 | } |
|---|
| 202 | |
|---|