1 | /*------------------------------------------------------------\ |
---|
2 | | | |
---|
3 | | Tool : systemcass | |
---|
4 | | | |
---|
5 | | File : serializations.cc | |
---|
6 | | | |
---|
7 | | Author : Buchmann Richard | |
---|
8 | | | |
---|
9 | | Date : 10_01_2006 | |
---|
10 | | | |
---|
11 | \------------------------------------------------------------*/ |
---|
12 | |
---|
13 | /* |
---|
14 | * This file is part of the Disydent Project |
---|
15 | * Copyright (C) Laboratoire LIP6 - Département ASIM |
---|
16 | * Universite Pierre et Marie Curie |
---|
17 | * |
---|
18 | * Home page : http://www-asim.lip6.fr/disydent |
---|
19 | * E-mail : mailto:richard.buchmann@lip6.fr |
---|
20 | * |
---|
21 | * This library is free software; you can redistribute it and/or modify it |
---|
22 | * under the terms of the GNU Library General Public License as published |
---|
23 | * by the Free Software Foundation; either version 2 of the License, or (at |
---|
24 | * your option) any later version. |
---|
25 | * |
---|
26 | * Disydent is distributed in the hope that it will be |
---|
27 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
29 | * Public License for more details. |
---|
30 | * |
---|
31 | * You should have received a copy of the GNU General Public License along |
---|
32 | * with the GNU C Library; see the file COPYING. If not, write to the Free |
---|
33 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
34 | */ |
---|
35 | |
---|
36 | #include <cassert> |
---|
37 | #include <map> |
---|
38 | #include <fstream> |
---|
39 | |
---|
40 | #include "internal_ext.h" // tab_t |
---|
41 | #include "serialization.h" |
---|
42 | #include "entity.h" |
---|
43 | #include "sc_module.h" |
---|
44 | #include "sc_object.h" |
---|
45 | #include "hex2string.h" |
---|
46 | |
---|
47 | #ifdef HAVE_CONFIG_H |
---|
48 | #include "config.h" |
---|
49 | #endif |
---|
50 | |
---|
51 | using namespace std; |
---|
52 | using namespace sc_core; |
---|
53 | |
---|
54 | |
---|
55 | namespace sc_core { |
---|
56 | |
---|
57 | |
---|
58 | static void dec2string(char * buf, const tab_t * val, int bit_number) { |
---|
59 | assert(bit_number <= 64); |
---|
60 | if (bit_number == 1) { |
---|
61 | bool v = *((const bool *) val); |
---|
62 | sprintf(buf, "%d", (v) ? 1 : 0); |
---|
63 | } |
---|
64 | else if (bit_number <= 8) { |
---|
65 | uint8 v = *((const uint8 *) val); |
---|
66 | int v2 = v; |
---|
67 | sprintf(buf, "%d", v2); |
---|
68 | } |
---|
69 | else if (bit_number <= 16) { |
---|
70 | uint16 v = *((const uint16 *) val); |
---|
71 | sprintf(buf, "%u", (uint32) v); |
---|
72 | } |
---|
73 | else if (bit_number <= 32) { |
---|
74 | uint32 v = *((const uint32 *) val); |
---|
75 | sprintf(buf, "%u", v); |
---|
76 | } |
---|
77 | else if (bit_number <= 64) { |
---|
78 | uint64 v = *((const uint64 *) val); |
---|
79 | sprintf(buf, "%llu", v); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | static void save_signal_table(ostream & o, int hex = true) { |
---|
85 | const equi_list_t &eq_l = get_equi_list(); |
---|
86 | equi_list_t::const_iterator it; |
---|
87 | for (it = eq_l.begin(); it != eq_l.end(); ++it) { |
---|
88 | const equi_t & eq = *it; |
---|
89 | equi_t::const_iterator it2 = eq.begin(); |
---|
90 | const entity & en = *it2; |
---|
91 | const sc_interface * f = en.interface; |
---|
92 | o << dec << get_name(eq) << endl; |
---|
93 | char buf[128]; |
---|
94 | if (hex) { |
---|
95 | buf[0] = '0'; |
---|
96 | buf[1] = 'x'; |
---|
97 | hex2string(buf + 2, f->get_pointer(), f->data_size_in_bytes() << 3); |
---|
98 | } |
---|
99 | else { |
---|
100 | dec2string(buf, f->get_pointer(), f->data_size_in_bytes() << 3); |
---|
101 | } |
---|
102 | o << buf; |
---|
103 | o << endl; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | typedef map<const sc_module *, save_fct_t1> sc_module2save_fct_t1; |
---|
109 | |
---|
110 | sc_module2save_fct_t1 save_handler_table; |
---|
111 | |
---|
112 | void set_save_handler(const sc_module & mod, save_fct_t1 fct) { |
---|
113 | save_handler_table[&mod] = fct; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | static void save_modules(FILE * o) { |
---|
118 | sc_module2save_fct_t1::const_iterator it; |
---|
119 | for (it = save_handler_table.begin(); it != save_handler_table.end(); ++it) { |
---|
120 | const sc_module * mod = it->first; |
---|
121 | save_fct_t1 fct = it->second; |
---|
122 | assert(mod != NULL); |
---|
123 | fprintf(o, "module\n%s\n", mod->name()); |
---|
124 | if (fct != NULL) { |
---|
125 | (((sc_module *) mod)->*fct)(o); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | void sc_save_simulation(const char * filename) { |
---|
132 | update(); |
---|
133 | if (dump_stage) { |
---|
134 | cerr << "Saving simulation into \"" << filename << "\"... "; |
---|
135 | } |
---|
136 | |
---|
137 | ofstream file; |
---|
138 | file.open(filename); |
---|
139 | file << "CABA Save!" << endl; |
---|
140 | file << (sc_time_stamp() / 1000) << endl; |
---|
141 | file << sc_time_stamp().to_string() << endl; |
---|
142 | save_signal_table(file, true); |
---|
143 | file.close(); |
---|
144 | FILE * f = fopen(filename, "a+"); |
---|
145 | assert(f != NULL); |
---|
146 | save_modules(f); |
---|
147 | fclose(f); |
---|
148 | |
---|
149 | if (dump_stage) { |
---|
150 | cerr << "done.\n"; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | } // end of sc_core namespace |
---|
156 | |
---|
157 | |
---|
158 | /* |
---|
159 | # Local Variables: |
---|
160 | # tab-width: 4; |
---|
161 | # c-basic-offset: 4; |
---|
162 | # c-file-offsets:((innamespace . 0)(inline-open . 0)); |
---|
163 | # indent-tabs-mode: nil; |
---|
164 | # End: |
---|
165 | # |
---|
166 | # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
167 | */ |
---|
168 | |
---|