/* 
 * Multiprocessor Packet counting
 * 
 */

#include "ixp.h"
#include "CSR.h"
#include "buffer.h"

/*
 * Receive signal from microprocessor 0 and count packets
 * Run this code on microprocessor 1
 */


void main(void) 
{
	
	unsigned int temp;
	
	volatile __declspec(sram) unsigned int * pktadr;
	volatile __declspec(scratch) unsigned int *ready;
	__declspec(sram_write_reg) unsigned int mask;
	__declspec(sram_read_reg) unsigned int data;
	buffer_handle_t buf_handle;
	__declspec(scratch) unsigned int* cntr;
	ready = (__declspec(scratch) unsigned int *) 0x0;
	pktadr = (__declspec(sram) unsigned int *) 0x50;
	cntr = (__declspec(scratch)unsigned int *) 0x20;
	mask = 1; 	
	*cntr=0;

    while (1) {
		ctx_wait(inter_thread); // wait for processor 0 to send signal, valid packet address at pktadr
		sram_read_lock(&data, pktadr, 1, queue_default, ctx_swap);		buf_handle = data;
		scratch_incr(cntr, no_signal);
		buffer_free(buf_handle); // freeing the buffer in which the packet is stored by microprocessor 0
		sram_unlock(pktadr);
		scratch_set_bits(&mask, ready,  ctx_swap);
	}
}


void exit(unsigned int code) {
	/* empty */
}