source: latest/src/serialization.cc @ 1

Last change on this file since 1 was 1, checked in by buchmann, 17 years ago

Initial import from CVS repository

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