source: sources/src/sc_signal.h @ 23

Last change on this file since 23 was 22, checked in by buchmann, 16 years ago

Fix:

  • add missing system header include
  • add a namespace path to atoi

And... code cleanup:

  • bunch of various operators that share the same implementation now are using the same #define.
  • indent and tab/space
File size: 8.5 KB
Line 
1/*------------------------------------------------------------\
2|                                                             |
3| Tool    :                  systemcass                       |
4|                                                             |
5| File    :                   sc_signal.h                     |
6|                                                             |
7| Author  :                 Buchmann Richard                  |
8|                           Taktak Sami                       |
9|                                                             |
10| Date    :                   09_07_2004                      |
11|                                                             |
12\------------------------------------------------------------*/
13#ifndef __SC_SIGNAL_H__
14#define __SC_SIGNAL_H__
15
16// Define registers writing method
17#include<iostream>
18#include<cstdlib>
19#include"sc_fwd.h"
20#include"sc_nbdefs.h"
21//#include"sc_event_finder.h"
22//#include"sc_event.h"
23#include"sc_time.h" // SC_ZERO_TIME
24#include"sc_object.h"
25#include"sc_interface.h"
26#include"internal_ext.h"
27#include "fsm_rules.h"
28
29namespace sc_core {
30
31//
32#if ((__GNUC__ < 3) || (__GNUC_MINOR__ < 4))
33#define INLINE __attribute__((always_inline))
34#else
35/* gcc3.4 doesn't support */ 
36#define INLINE
37#endif
38
39#define READ_SIGNAL(value_type_,pointer_) \
40        ((value_type_&) (*((value_type_*) (pointer_))))
41
42        ///////////////////// DEPRECATED
43// C ANSI-only since it is needed to link with extern "C"
44// this declaration is not in casc.h since the CHECK_FSM_RULES macro
45// is not defined.
46
47extern void bind (sc_port_base&,sc_port_base&);
48extern void bind (sc_port_base&,sc_signal_base&);
49extern void bind (sc_signal_base &x);
50extern void bind (sc_port_base   &x);
51typedef tab_t base_type;
52struct pending_write {
53        base_type   *pointer;
54        base_type    value;
55        //pending_write (base_type *const pointer_, const base_type value_)
56        //{     pointer = pointer_; value = value_; }
57        friend std::ostream& operator << (std::ostream &o, const pending_write &p)
58        { return o << "(pointer = " << p.pointer << "; value = " << p.value << ")\n"; }
59};
60
61// Check pending_writing to register
62extern void pending_writing2register_clear  ();
63extern void pending_writing2register_record_and_check (const tab_t *);
64
65// Pending write to register (simple stack)
66typedef pending_write *pending_write_vector_t;
67extern pending_write_vector_t pending_write_vector;
68extern "C" unsigned int pending_write_vector_nb;
69extern unsigned int pending_write_vector_capacity;
70
71
72template <typename T>
73inline void post_write (base_type *const pointer_,
74                        const T          value_) /*INLINE*/;
75template <typename T>
76inline void post_multiwrite (base_type *const pointer_,
77                             const T          value_)
78{
79        size_t size = (sizeof (T)-1) / sizeof (base_type);
80        size_t i = 0;
81        const base_type *pvalue = (const base_type*)(&value_);
82        do {
83#if 0
84    cout << "post_multiwrite 0x" << hex << pvalue[i] << " @" << (pointer_ + i) << "\n";
85#endif
86                post_write (pointer_ + i, pvalue[i]);
87        } while (i++ < size);
88}
89template <typename T>
90inline void post_write (base_type *const pointer_, 
91                        const T          value_)
92{
93  if (sizeof (T) > sizeof (base_type)) {
94#if 0
95  std::cout << "sizeof (T) = " << sizeof (T)
96            << " (base_type = " << sizeof (base_type) << "\n";
97#endif
98  post_multiwrite (pointer_,value_);
99  } else {
100#if defined(DEBUG)
101    if (pending_write_vector_nb >= pending_write_vector_capacity) {
102      //if (pending_write_vector_nb >= pending_write_vector_capacity * sizeof(pending_write)) {
103      std::cerr << "Error : The array for posted writing on register is too small.\n";
104      std::cerr << "Up to 1 writing per register is allowed during a cycle.\n";
105      std::cerr << "Please check the hardware description.\n";
106      exit (-1);
107    }
108#endif // DEBUG
109    pending_write_vector[pending_write_vector_nb].pointer = pointer_;
110//      pending_write_vector[pending_write_vector_nb++].value = *(reinterpret_cast<const base_type*const>(&value_)); => bug !
111    pending_write_vector[pending_write_vector_nb++].value = value_; // => bug avec blues !
112
113        // -> fix to use user-defined struct in sc_signal/sc_in/sc_out/sc_inout
114        // pending_write_vector[pending_write_vector_nb++].value = *((base_type*)&value_); => bug !
115#if 0
116        std::cerr << "posted write : ptr = " << pointer_ << ", val = " << value_ << "\n";
117#endif
118#if 0 
119        // introduce bug on using trace functions
120        if (value_ == READ_SIGNAL(T,pointer_))
121                return;
122#endif
123  };
124}
125
126inline bool is_posted_write ()
127{
128  return pending_write_vector_nb > 0;
129}
130
131extern "C" void update (void);
132
133// ----------------------------------------------------------------------------
134//  CLASS : sc_signal_base
135//
136//  The sc_signal_base<T> primitive channel class.
137// ----------------------------------------------------------------------------
138
139class sc_signal_base : public sc_object, public sc_interface
140{
141  //////
142  // Internal
143  friend class sc_clock;
144  friend class sc_port_base;
145  void init ();
146  //////                                 
147 
148
149public: 
150  // LRM (?)
151  //virtual const sc_event /*&*/ default_event () const;
152  static const char* const kind_string;
153  //virtual const char *kind () const;
154
155  //
156public:
157  sc_signal_base();
158  sc_signal_base(const char* name_);
159  sc_signal_base(const char* name_, void*);
160  ~sc_signal_base();
161};
162
163template <typename T>
164class sc_signal : public sc_signal_base
165{
166private:
167  T val;
168  typedef T                data_type;
169  typedef sc_signal < T >  this_type;
170
171  ///////////
172  // Internal
173public: void init ();
174  ///////////
175
176  //  virtual void update ();
177  void check_writer ();
178public:
179  // constructors, destructor
180  sc_signal () 
181  { init (); }
182  explicit sc_signal (const char *name_): sc_signal_base(name_)
183  { init (); }
184  /*virtual */~ sc_signal () 
185  {}
186  // methods
187  /*
188  virtual void register_port (sc_port_base &, const char *)
189  {}
190  virtual const sc_event & default_event () const
191  {}
192  virtual const sc_event & value_changed_event () const
193  {}
194  */
195  /*virtual*/ inline const data_type & read () const INLINE;
196/*
197  virtual const T & get_data_ref () const
198  {}
199  virtual bool event () const
200  {}
201  */
202  /*virtual*/ inline void write (const data_type &) /*INLINE*/;
203  inline operator const data_type & () const
204  { return this->read(); }
205  inline this_type& operator = (const data_type & a)
206  { sc_signal<T>::write (a); return *this; }
207  inline this_type& operator = (const sc_signal < T > &a)
208  { sc_signal<T>::write (a.read()); return *this; }
209  inline this_type& operator += (const data_type & a)
210  { sc_signal<T>::write (read() + a); return *this; }
211  inline this_type& operator += (const sc_signal < T > &a)
212  { sc_signal<T>::write (read()+a.read()); return *this; }
213  const data_type & get_new_value () const;
214//  void trace (sc_trace_file * tf) const;
215  /*
216        virtual void print (std::ostream &o) const
217  { o << *this; }
218  virtual void dump (std::ostream &o) const
219  { o << *this; }
220        */
221private:
222  // disabled
223  sc_signal (const sc_signal < T > &);
224
225};
226
227template <typename T>
228void
229sc_signal<T>::init()
230{
231  set_pointer ((tab_t*)&val);
232  set_kind    (kind_string);
233  sc_interface::init (sizeof (data_type)); 
234  val = 0; /* The simulator initializes the signal/register to 0.    */
235           /* However, hardware initialization still has to be done. */
236           /* This kind of initialization is for trace diffing.      */
237}
238// read the value
239template <typename T>
240/*virtual*/ 
241inline 
242const T & 
243sc_signal<T>::read() const
244{
245#ifdef DUMP_READ
246  std::cerr << "read " << READ_SIGNAL(const T, get_pointer())
247                << " on signal " << name () << "\n";
248#endif
249#ifdef CHECK_FSM_RULES
250        // we can read value from sc_signal type (used like a register) at any time
251#endif 
252    return READ_SIGNAL(const T, get_pointer());
253}
254
255// write the new value
256template <typename T>
257inline
258void
259sc_signal<T>::write( const data_type& value_ )
260{
261#ifdef CHECK_FSM_RULES
262        if ((casc_fsm_step != TRANSITION) 
263                        && ( casc_fsm_step != STIMULI)) {
264                std::cerr << "FSM rules error : trying to write on signal " 
265                          << name () 
266                          << " from " << get_step_name () << " function.\n";
267                exit (-1);
268        }               
269#endif
270#ifdef DEBUG
271  if (get_pointer() == NULL)
272  {
273    std::cerr << "Error : Unable to write into '" << name () << "'.";
274    exit (24032005);
275  }
276#endif
277#ifdef CHECK_MULTIWRITING2REGISTER
278  pending_writing2register_record_and_check (get_pointer ());
279#endif
280#ifdef DUMP_WRITE
281  if (sc_signal<T>::read() == value_)
282    return;
283  std::cerr << "write (posted) " << value_
284                << " on sc_signal (writing into register) '" << name () << "'\n";
285#endif
286  post_write (/*(tab_t*)&val*/ get_pointer(), value_);
287}
288
289#undef INLINE
290
291#undef READ_SIGNAL
292
293} // end of namespace sc_core
294
295#endif /* __SC_SIGNAL_H__ */
296
Note: See TracBrowser for help on using the repository browser.