Ignore:
Timestamp:
Jan 22, 2013, 4:23:22 PM (11 years ago)
Author:
meunier
Message:

Code formatting in all source files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sources/src/serialization.cc

    r42 r52  
    3434 */
    3535
     36#include <cassert>
     37#include <map>
     38#include <fstream>
    3639
    3740#include "internal_ext.h" // tab_t
     
    4245#include "hex2string.h"
    4346
    44 #include <cassert>
    45 #include <map>
    46 #include <fstream>
    47 //#include <vector> // save_module_hierarchy
    48 
    4947#ifdef HAVE_CONFIG_H
    5048#include "config.h"
     
    5452using namespace sc_core;
    5553
     54
    5655namespace sc_core {
    5756
    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;
     57
     58static void dec2string(char * buf, const tab_t * val, int bit_number) {
     59    assert(bit_number <= 64);
     60    if (bit_number == 1) {
     61        bool v = *((const bool *) val);
     62        sprintf(buf, "%d", (v) ? 1 : 0);
     63    }
     64    else if (bit_number <= 8) {
     65        uint8 v = *((const uint8 *) val);
     66        int v2 = v;
     67        sprintf(buf, "%d", v2);
     68    }
     69    else if (bit_number <= 16) {
     70        uint16 v = *((const uint16 *) val);
     71        sprintf(buf, "%u", (uint32) v);
     72    }
     73    else if (bit_number <= 32) {
     74        uint32 v = *((const uint32 *) val);
     75        sprintf(buf, "%u", v);
     76    }
     77    else if (bit_number <= 64) {
     78        uint64 v = *((const uint64 *) val);
     79        sprintf(buf, "%llu", v);
     80    }
    7281}
    7382
    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
    8383
    84 static
    85 void
    86 dec2string (char        *buf,
    87             const tab_t *val,
    88             int          bit_number)
    89 {
    90   assert(bit_number <= 64);
    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, "%u", (uint32) v);
    101   } else if (bit_number <= 32) {
    102     uint32 v = *((const uint32*)val);
    103     sprintf (buf, "%u", v);
    104   } else if (bit_number <= 64) {
    105     uint64 v = *((const uint64*)val);
    106     sprintf (buf, "%llu", v);
    107   }
     84static void save_signal_table(ostream & o, int hex = true) {
     85    const equi_list_t &eq_l = get_equi_list();
     86    equi_list_t::const_iterator it;
     87    for (it = eq_l.begin(); it != eq_l.end(); ++it) {
     88        const equi_t & eq = *it;
     89        equi_t::const_iterator it2 = eq.begin();
     90        const entity & en = *it2;
     91        const sc_interface * f = en.interface;
     92        o << dec << get_name(eq) << endl;
     93        char buf[128];
     94        if (hex) {
     95            buf[0] = '0';
     96            buf[1] = 'x';
     97            hex2string(buf + 2, f->get_pointer(), f->data_size_in_bytes() << 3);
     98        }
     99        else {
     100            dec2string(buf, f->get_pointer(), f->data_size_in_bytes() << 3);
     101        }
     102        o << buf;
     103        o << endl;
     104    }
    108105}
    109106
    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   }
     107
     108typedef map<const sc_module *, save_fct_t1> sc_module2save_fct_t1;
     109
     110sc_module2save_fct_t1 save_handler_table;
     111
     112void set_save_handler(const sc_module & mod, save_fct_t1 fct) {
     113    save_handler_table[&mod] = fct;
    137114}
    138115
    139 typedef std::map<const sc_module *, save_fct_t1> sc_module2save_fct_t1;
    140 sc_module2save_fct_t1 save_handler_table;
    141116
    142 void
    143 set_save_handler   (const sc_module &mod,
    144                     save_fct_t1      fct)
    145 {
    146   //assert(fct != NULL);
    147   //sc_module2save_fct_t1::value_type pair(&mod,fct);
    148   //save_handler_table.insert (pair);
    149   save_handler_table[&mod] = fct;
     117static void save_modules(FILE * o) {
     118    sc_module2save_fct_t1::const_iterator it;
     119    for (it = save_handler_table.begin(); it != save_handler_table.end(); ++it) {
     120        const sc_module * mod = it->first;
     121        save_fct_t1 fct = it->second;
     122        assert(mod != NULL);
     123        fprintf(o, "module\n%s\n", mod->name());
     124        if (fct != NULL) {
     125            (((sc_module *) mod)->*fct)(o);
     126        }
     127    }
    150128}
    151129
    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;
    164     assert(mod != NULL);
    165 //    assert(fct != NULL);
    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   }
     130
     131void sc_save_simulation(const char * filename) {
     132    update();
     133    if (dump_stage) {
     134        cerr << "Saving simulation into \"" << filename << "\"... ";
     135    }
     136
     137    ofstream file;
     138    file.open(filename);
     139    file << "CABA Save!" << endl;
     140    file << (sc_time_stamp() / 1000) << endl;
     141    file << sc_time_stamp().to_string() << endl;
     142    save_signal_table(file, true);
     143    file.close();
     144    FILE * f = fopen(filename, "a+");
     145    assert(f != NULL);
     146    save_modules(f);
     147    fclose(f);
     148
     149    if (dump_stage) {
     150        cerr << "done.\n";
     151    }
    171152}
    172153
    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+");
    191   assert(f != NULL);
    192   save_modules (f);
    193   fclose (f);
    194 
    195   if (dump_stage)
    196     cerr << "done.\n";
    197 }
    198154
    199155} // end of sc_core namespace
    200156
    201157
     158/*
     159# Local Variables:
     160# tab-width: 4;
     161# c-basic-offset: 4;
     162# c-file-offsets:((innamespace . 0)(inline-open . 0));
     163# indent-tabs-mode: nil;
     164# End:
     165#
     166# vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
     167*/
     168
Note: See TracChangeset for help on using the changeset viewer.