//slave port consisting of indexed port and bus protocol port

#include "systemc.h"
#include "basic_type.h"

struct mod2 : public sc_module
{
	//port for clock
	sc_in_clk clocking2;

	//Refinable ports 
	sc_outslave<basic_type, sc_indexed<basic_type, 10> > P2;
	sc_inoutslave<basic_type, sc_indexed<basic_type, 10> > P2_inout;
	
	sc_outslave<basic_type, sc_fullHandshake<basic_type> > P2_bp;

        //sc_outslave		
	void slavefunc()
	{
		basic_type static val = 0;
	
		val += 2;
		P2.write(val);     //write new value of val to master

	}

	//sc_inslave
	void slavefunc2()
	{
		if (P2_inout -> input())
		{
			basic_type index;
			basic_type input = P2_inout;
			
			input = P2_inout.read();
			index = P2_inout.get_address();

			printf ("***** using indexed ports ****** \n ");
			printf ("obtained value: %d from master, index = %d \n", input,
			index);
			printf (" \n ");
		}					


	}

	void slavefunc_BP()
	{
		static int x = 4;

  		while(1)
		{
			P2_bp->ack = 0;
			do {
			  wait();               //waits till req = 1 
                        } while(!P2_bp->req.read());
  
			if (P2_bp->req)
			{
				printf ("writing value %d to the master \n",x);
				printf (" \n ");
				P2_bp->d = x;
				P2_bp->ack = 1;
			}
		
			x++;
			while (P2_bp->req == true)  //waits till req = 0 
				wait();
  		}
	}
	
	//Constructor to create & initialize an instance & data structures.
	SC_CTOR (mod2)
	{
		SC_SLAVE(slavefunc, P2); // Slave thread

		SC_SLAVE(slavefunc2, P2_inout); // Slave thread

		SC_THREAD(slavefunc_BP); // Clock sensitive thread
		sensitive<<clocking2;

	}
};


