#ifndef __delay_H
#define __delay_H
/*
 * Copyright 2005-2008 Stephen Williams
 *
 *    This source code is free software; you can redistribute it
 *    and/or modify it in source code form under the terms of the GNU
 *    General Public License as published by the Free Software
 *    Foundation; either version 2 of the License, or (at your option)
 *    any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

# include  "vvp_net.h"
# include  "schedule.h"

enum delay_edge_t {
      DELAY_EDGE_01 = 0, DELAY_EDGE_10, DELAY_EDGE_0z,
      DELAY_EDGE_z1,     DELAY_EDGE_1z, DELAY_EDGE_z0,
      DELAY_EDGE_0x,     DELAY_EDGE_x1, DELAY_EDGE_1x,
      DELAY_EDGE_x0,     DELAY_EDGE_xz, DELAY_EDGE_zx,
      DELAY_EDGE_COUNT
};

/*
 * Instances of this object are functions that calculate the delay for
 * the transition from the source vvp_bit4_t value to the destination
 * value.
 */
class vvp_delay_t {

    public:
      vvp_delay_t(vvp_time64_t rise, vvp_time64_t fall);
      vvp_delay_t(vvp_time64_t rise, vvp_time64_t fall, vvp_time64_t decay);
      ~vvp_delay_t();

      vvp_time64_t get_delay(vvp_bit4_t from, vvp_bit4_t to);
      vvp_time64_t get_min_delay() const;

      void set_rise(vvp_time64_t val);
      void set_fall(vvp_time64_t val);
      void set_decay(vvp_time64_t val);

    private:
      vvp_time64_t rise_, fall_, decay_;
      vvp_time64_t min_delay_;

      void calculate_min_delay_();
};

/* vvp_fun_delay
 * This is a lighter weight version of vvp_fun_drive, that only
 * carries delays. The output that it propagates is vvp_vector4_t so
 * drive strengths are lost, but then again it doesn't go through the
 * effort of calculating strength values either.
 *
 * The node needs a pointer to the vvp_net_t input so that it knows
 * how to find its output when propagating delayed output.
 *
 * NOTE: This node supports vec4 and real by repeating whatever was
 * input. This is a bit of a hack, as it may be more efficient to
 * create the right type of vvp_fun_delay_real.
 */
class vvp_fun_delay  : public vvp_net_fun_t, private vvp_gen_event_s {

      struct event_ {
	    event_(vvp_time64_t s) : sim_time(s) { }
	    void (vvp_fun_delay::*run_run_ptr)(struct vvp_fun_delay::event_*cur);
	    const vvp_time64_t sim_time;
	    vvp_vector4_t ptr_vec4;
	    vvp_vector8_t ptr_vec8;
	    double ptr_real;
	    struct event_*next;
      };

    public:
      vvp_fun_delay(vvp_net_t*net, vvp_bit4_t init, const vvp_delay_t&d);
      ~vvp_fun_delay();

      void recv_vec4(vvp_net_ptr_t port, const vvp_vector4_t&bit);
      void recv_vec8(vvp_net_ptr_t port, const vvp_vector8_t&bit);
      void recv_real(vvp_net_ptr_t port, double bit);
	//void recv_long(vvp_net_ptr_t port, long bit);

    private:
      virtual void run_run();


      void run_run_vec4_(struct vvp_fun_delay::event_*cur);
      void run_run_vec8_(struct vvp_fun_delay::event_*cur);
      void run_run_real_(struct vvp_fun_delay::event_*cur);

    private:
      vvp_net_t*net_;
      vvp_delay_t delay_;
      bool initial_; // Indicates if the value is still the initial value.

      vvp_vector4_t cur_vec4_;
      vvp_vector8_t cur_vec8_;
      double cur_real_;

      struct event_ *list_;
      void enqueue_(struct event_*cur)
      {
	    if (list_) {
		  cur->next = list_->next;
		  list_->next = cur;
		  list_ = cur;
	    } else {
		  cur->next = cur;
		  list_ = cur;
	    }
      }
      struct event_* dequeue_(void)
      {
	    if (list_ == 0)
		  return 0;
	    struct event_*cur = list_->next;
	    if (list_ == cur)
		  list_ = 0;
	    else
		  list_->next = cur->next;
	    return cur;
      }
      void clean_pulse_events_(vvp_time64_t use_delay);
};

/*
* These objects implement module delay paths. The fun_modpath functor
* is the output of the modpath, and the vvp_fun_modpath_src is the
* source of the modpath. The modpath source tracks events on the
* inputs to enable delays, and the vvp_fun_modpath, when it's time to
* schedule, looks at the associated modpath_src objects for which
* paths are active.
*/
class vvp_fun_modpath;
class vvp_fun_modpath_src;

class vvp_fun_modpath  : public vvp_net_fun_t, private vvp_gen_event_s {

    public:
      vvp_fun_modpath(vvp_net_t*net);
      ~vvp_fun_modpath();

      void add_modpath_src(vvp_fun_modpath_src*that, bool ifnone);

      void recv_vec4(vvp_net_ptr_t port, const vvp_vector4_t&bit);

    private:
      virtual void run_run();

    private:
      vvp_net_t*net_;

      vvp_vector4_t cur_vec4_;

      vvp_fun_modpath_src*src_list_;
      vvp_fun_modpath_src*ifnone_list_;

    private: // not implemented
      vvp_fun_modpath(const vvp_fun_modpath&);
      vvp_fun_modpath& operator= (const vvp_fun_modpath&);
};

class vvp_fun_modpath_src  : public vvp_net_fun_t {

      friend class vvp_fun_modpath;

    public:
      vvp_fun_modpath_src(vvp_time64_t d[12]);
    protected:
      ~vvp_fun_modpath_src();

    public:
      void recv_vec4(vvp_net_ptr_t port, const vvp_vector4_t&bit);
      virtual bool test_vec4(const vvp_vector4_t&bit);

      void get_delay12(vvp_time64_t out[12]) const;
      void put_delay12(const vvp_time64_t in[12]);

    private:
	// FIXME: Needs to be a 12-value array
      vvp_time64_t delay_[12];
	// Used by vvp_fun_modpath to keep a list of modpath_src objects.
      vvp_fun_modpath_src*next_;

      vvp_time64_t wake_time_;
      bool condition_flag_;

    private:
      vvp_fun_modpath_src(const vvp_fun_modpath_src&);
      vvp_fun_modpath_src& operator = (const vvp_fun_modpath_src&);
};

class vvp_fun_modpath_edge  : public vvp_fun_modpath_src {

    public:
      vvp_fun_modpath_edge(vvp_time64_t del[12], bool pos, bool neg);

      bool test_vec4(const vvp_vector4_t&bit);

    private:
      vvp_bit4_t old_value_;
      bool posedge_;
      bool negedge_;
};

#endif // __delay_H
