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