source: sources/src/serialization.cc @ 21

Last change on this file since 21 was 12, checked in by buchmann, 16 years ago

Changes:

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