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 | |
---|
37 | #include <map> |
---|
38 | #include <fstream> |
---|
39 | //#include <vector> // save_module_hierarchy |
---|
40 | |
---|
41 | #include "internal_ext.h" // tab_t |
---|
42 | #include "serialization.h" |
---|
43 | #include "entity.h" |
---|
44 | #include "sc_module.h" |
---|
45 | #include "sc_object.h" |
---|
46 | #include "hex2string.h" |
---|
47 | #include <cassert> |
---|
48 | #ifdef HAVE_CONFIG_H |
---|
49 | #include "config.h" |
---|
50 | #endif |
---|
51 | |
---|
52 | using namespace std; |
---|
53 | using namespace sc_core; |
---|
54 | |
---|
55 | namespace sc_core { |
---|
56 | |
---|
57 | #if 0 |
---|
58 | static |
---|
59 | void |
---|
60 | save_module_hierarchy (ostream &o, |
---|
61 | sc_object *obj) |
---|
62 | { |
---|
63 | const std::vector<sc_object*> &children = obj->get_child_objects(); |
---|
64 | for (unsigned i = 0; i < children.size(); i++) |
---|
65 | { |
---|
66 | if (children[i]) |
---|
67 | save_module_hierarchy (o,children[i]); |
---|
68 | }; |
---|
69 | // if (obj->kind () != sc_module::kind_string) |
---|
70 | // o << obj->name () << " " << obj->kind() << endl; |
---|
71 | } |
---|
72 | |
---|
73 | static |
---|
74 | void |
---|
75 | save_module_hierarchy (ostream &o, |
---|
76 | const std::vector<sc_object*> &obj_list) |
---|
77 | { |
---|
78 | for (signed int i = obj_list.size(); i >= 0; i--) |
---|
79 | save_module_hierarchy(o, obj_list[i]); |
---|
80 | } |
---|
81 | #endif |
---|
82 | |
---|
83 | static |
---|
84 | void |
---|
85 | dec2string (char *buf, |
---|
86 | const tab_t *val, |
---|
87 | int bit_number) |
---|
88 | { |
---|
89 | assert(bit_number <= 64); |
---|
90 | if (bit_number == 1) { |
---|
91 | bool v = *((const bool*) val); |
---|
92 | sprintf (buf, "%d", (v)?1:0); |
---|
93 | } else if (bit_number <= 8) { |
---|
94 | uint8 v = *((const uint8*)val); |
---|
95 | int v2 = v; |
---|
96 | sprintf (buf, "%d", v2); |
---|
97 | } else if (bit_number <= 16) { |
---|
98 | uint16 v = *((const uint16*)val); |
---|
99 | sprintf (buf, "%d", v); |
---|
100 | } else if (bit_number <= 32) { |
---|
101 | uint32 v = *((const uint32*)val); |
---|
102 | sprintf (buf, "%d", v); |
---|
103 | } else if (bit_number <= 64) { |
---|
104 | uint64 v = *((const uint64*)val); |
---|
105 | sprintf (buf, "%lld", v); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | static |
---|
110 | void |
---|
111 | save_signal_table (std::ostream &o, |
---|
112 | int hex = true) |
---|
113 | { |
---|
114 | const equi_list_t &eq_l = get_equi_list (); |
---|
115 | equi_list_t::const_iterator it; |
---|
116 | for (it = eq_l.begin (); it != eq_l.end (); ++it) |
---|
117 | { |
---|
118 | const equi_t &eq = *it; |
---|
119 | equi_t::const_iterator it2 = eq.begin(); |
---|
120 | const entity &en = *it2; |
---|
121 | const sc_interface *f = en.interface; |
---|
122 | o << dec << get_name (eq) << endl; |
---|
123 | // print_value (o, *f); |
---|
124 | char buf[128]; |
---|
125 | if (hex) |
---|
126 | { |
---|
127 | buf[0] = '0'; |
---|
128 | buf[1] = 'x'; |
---|
129 | hex2string (buf+2, f->get_pointer (), f->data_size_in_bytes() << 3); |
---|
130 | } else { |
---|
131 | dec2string (buf, f->get_pointer (), f->data_size_in_bytes() << 3); |
---|
132 | } |
---|
133 | o << buf; |
---|
134 | o << endl; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | typedef std::map<const sc_module *, save_fct_t1> sc_module2save_fct_t1; |
---|
139 | sc_module2save_fct_t1 save_handler_table; |
---|
140 | |
---|
141 | void |
---|
142 | set_save_handler (const sc_module &mod, |
---|
143 | save_fct_t1 fct) |
---|
144 | { |
---|
145 | //assert(fct != NULL); |
---|
146 | //sc_module2save_fct_t1::value_type pair(&mod,fct); |
---|
147 | //save_handler_table.insert (pair); |
---|
148 | save_handler_table[&mod] = fct; |
---|
149 | } |
---|
150 | |
---|
151 | static |
---|
152 | void |
---|
153 | //save_modules (ostream &o) |
---|
154 | save_modules (FILE *o) |
---|
155 | { |
---|
156 | sc_module2save_fct_t1::const_iterator it; |
---|
157 | for (it = save_handler_table.begin(); |
---|
158 | it != save_handler_table.end(); |
---|
159 | ++it) |
---|
160 | { |
---|
161 | const sc_module *mod = it->first; |
---|
162 | save_fct_t1 fct = it->second; |
---|
163 | assert(mod != NULL); |
---|
164 | // assert(fct != NULL); |
---|
165 | //o << mod->name () << endl; |
---|
166 | fprintf (o,"module\n%s\n",mod->name ()); |
---|
167 | if (fct != NULL) |
---|
168 | (((sc_module*)mod)->*fct) (o); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | void |
---|
173 | sc_save_simulation (const char *filename) |
---|
174 | { |
---|
175 | update (); |
---|
176 | if (dump_stage) |
---|
177 | cerr << "Saving simulation into \"" << filename << "\"... "; |
---|
178 | |
---|
179 | ofstream file; |
---|
180 | file.open (filename); |
---|
181 | file << "CABA Save!" << endl; |
---|
182 | file << (sc_time_stamp () / 1000) << endl; |
---|
183 | file << sc_time_stamp ().to_string () << endl; |
---|
184 | //save_module_hierarchy (file,sc_get_top_level_objects ()); |
---|
185 | save_signal_table (file, true); |
---|
186 | //save_modules (file); |
---|
187 | //file.close (); |
---|
188 | file.close (); |
---|
189 | FILE *f = fopen (filename, "a+"); |
---|
190 | assert(f != NULL); |
---|
191 | save_modules (f); |
---|
192 | fclose (f); |
---|
193 | |
---|
194 | if (dump_stage) |
---|
195 | cerr << "done.\n"; |
---|
196 | } |
---|
197 | |
---|
198 | } // end of sc_core namespace |
---|
199 | |
---|
200 | |
---|