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

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

struct mod1 : public sc_module
{
	// Refinable ports
	sc_inoutmaster<basic_type, sc_indexed<basic_type, 10> > P1_inout;
	sc_inmaster<basic_type, sc_fullHandshake<basic_type> > P1_bp;

	sc_in_clk clocking;
	int y;

	void masterfunc ()
	{
		int static x = 0;
		int static a = 0;
		bool static state = true;
		basic_type val;


		P1_bp->req = 0;


		while (1)
		{

			if (state)
			{
				P1_inout[x] = a;  //writes the value to slave
			}
			else
			{
				val = P1_inout[x];
				printf (" ****** using indexed ports ****** \n");
				printf ("value read in from the slave = %d \n",val);
				printf (" \n ");
			}
	
			x++;
			a++;
		 	state = !(state);     //toggles values of state
	
			do {
			  wait();            //waits till ack = 0 
	       	         } while(P1_bp->ack);

			if (P1_bp->ack == false)
			{
				P1_bp->req = 1;
				y = P1_bp->d;
				printf ("***** using BP ***** \n");
				printf ("obtained the value %d \n",y);
			}

			while(P1_bp->ack == false)  //waits till ack = 1
				wait();
			P1_bp->req = 0;

		}
	 
	}

	 //Constructor to create & initialize an instance & data structures.
	 SC_CTOR(mod1)
	 {
		SC_THREAD(masterfunc); // Clock sensitive thread
		sensitive<<clocking;

	 }	

};
