source: branches/with_autoconf/src/sc_signal.h @ 21

Last change on this file since 21 was 20, checked in by nipo, 16 years ago

Sync up with trunk changes

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