| 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 | //#include <stdint.h> |
|---|
| 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 | |
|---|
| 48 | #ifdef HAVE_CONFIG_H |
|---|
| 49 | #include "config.h" |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | using namespace std; |
|---|
| 53 | using namespace sc_core; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | namespace sc_core { |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | static void dec2string(char * buf, const tab_t * val, int bit_number) { |
|---|
| 60 | assert(bit_number <= 64); |
|---|
| 61 | if (bit_number == 1) { |
|---|
| 62 | bool v = *((const bool *) val); |
|---|
| 63 | sprintf(buf, "%d", (v) ? 1 : 0); |
|---|
| 64 | } |
|---|
| 65 | else if (bit_number <= 8) { |
|---|
| 66 | uint8 v = *((const uint8 *) val); |
|---|
| 67 | int v2 = v; |
|---|
| 68 | sprintf(buf, "%d", v2); |
|---|
| 69 | } |
|---|
| 70 | else if (bit_number <= 16) { |
|---|
| 71 | uint16 v = *((const uint16 *) val); |
|---|
| 72 | sprintf(buf, "%u", (uint32) v); |
|---|
| 73 | } |
|---|
| 74 | else if (bit_number <= 32) { |
|---|
| 75 | uint32 v = *((const uint32 *) val); |
|---|
| 76 | sprintf(buf, "%u", v); |
|---|
| 77 | } |
|---|
| 78 | else if (bit_number <= 64) { |
|---|
| 79 | uint64 v = *((const uint64 *) val); |
|---|
| 80 | sprintf(buf, "%llu", v); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | static void save_signal_table(ostream & o, int hex = true) { |
|---|
| 86 | const equi_list_t &eq_l = get_equi_list(); |
|---|
| 87 | equi_list_t::const_iterator it; |
|---|
| 88 | for (it = eq_l.begin(); it != eq_l.end(); ++it) { |
|---|
| 89 | const equi_t & eq = *it; |
|---|
| 90 | equi_t::const_iterator it2 = eq.begin(); |
|---|
| 91 | const entity & en = *it2; |
|---|
| 92 | const sc_interface * f = en.interface; |
|---|
| 93 | o << dec << get_name(eq) << endl; |
|---|
| 94 | char buf[128]; |
|---|
| 95 | if (hex) { |
|---|
| 96 | buf[0] = '0'; |
|---|
| 97 | buf[1] = 'x'; |
|---|
| 98 | hex2string(buf + 2, f->get_pointer(), f->data_size_in_bytes() << 3); |
|---|
| 99 | } |
|---|
| 100 | else { |
|---|
| 101 | dec2string(buf, f->get_pointer(), f->data_size_in_bytes() << 3); |
|---|
| 102 | } |
|---|
| 103 | o << buf; |
|---|
| 104 | o << endl; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | typedef map<const sc_module *, save_fct_t1> sc_module2save_fct_t1; |
|---|
| 110 | |
|---|
| 111 | sc_module2save_fct_t1 save_handler_table; |
|---|
| 112 | |
|---|
| 113 | void set_save_handler(const sc_module & mod, save_fct_t1 fct) { |
|---|
| 114 | save_handler_table[&mod] = fct; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | static void save_modules(FILE * o) { |
|---|
| 119 | sc_module2save_fct_t1::const_iterator it; |
|---|
| 120 | for (it = save_handler_table.begin(); it != save_handler_table.end(); ++it) { |
|---|
| 121 | const sc_module * mod = it->first; |
|---|
| 122 | save_fct_t1 fct = it->second; |
|---|
| 123 | assert(mod != NULL); |
|---|
| 124 | fprintf(o, "module\n%s\n", mod->name()); |
|---|
| 125 | if (fct != NULL) { |
|---|
| 126 | (((sc_module *) mod)->*fct)(o); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | void sc_save_simulation(const char * filename) { |
|---|
| 133 | update(); |
|---|
| 134 | if (dump_stage) { |
|---|
| 135 | cerr << "Saving simulation into \"" << filename << "\"... "; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | ofstream file; |
|---|
| 139 | file.open(filename); |
|---|
| 140 | file << "CABA Save!" << endl; |
|---|
| 141 | file << (sc_time_stamp() / 1000) << endl; |
|---|
| 142 | file << sc_time_stamp().to_string() << endl; |
|---|
| 143 | save_signal_table(file, true); |
|---|
| 144 | file.close(); |
|---|
| 145 | FILE * f = fopen(filename, "a+"); |
|---|
| 146 | assert(f != NULL); |
|---|
| 147 | save_modules(f); |
|---|
| 148 | fclose(f); |
|---|
| 149 | |
|---|
| 150 | if (dump_stage) { |
|---|
| 151 | cerr << "done.\n"; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | } // end of sc_core namespace |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /* |
|---|
| 160 | # Local Variables: |
|---|
| 161 | # tab-width: 4; |
|---|
| 162 | # c-basic-offset: 4; |
|---|
| 163 | # c-file-offsets:((innamespace . 0)(inline-open . 0)); |
|---|
| 164 | # indent-tabs-mode: nil; |
|---|
| 165 | # End: |
|---|
| 166 | # |
|---|
| 167 | # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 168 | */ |
|---|
| 169 | |
|---|