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