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