//
// consumer.h  ::  consumer module with inmaster
//


#ifndef SYSTEMC_H
#include "systemc.h"
#endif


SC_MODULE(consumer) 
{
  // Internal variable
  int x;
  
  // declare ports
  sc_inmaster<int,sc_fullHandshake<int> > Cin;  
  sc_in_clk	clk;
  int state;
  
  // inmaster process
  void consumerFunc()    {
      state=0;
      Cin->req=0;

      while (true) {
	  wait();
	  switch(state) {
	  case 0: 
	    Cin->req=0;
	    if (Cin->ack==false){
	      state=1;
	    }
	    break;
	  case 1: 
	    Cin->req=1;
	    x = Cin->d; // read the data
	    // cout << " data = "<< x << endl;
	    state=2;
	    break;
	  case 2: 
	    if (Cin->ack==true){
	      state=0;
	      Cin->req=0;
	    }
	    break;
	  }
      }	
    }
  
  SC_CTOR(consumer) 
    {
      SC_THREAD(consumerFunc);
      sensitive << clk;
    }
};
