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