/*
 * xor.c
 *
 *  Created on: 29-04-2014
 *      Author: KJ
 */


#include "tdefs.h"
#include "misc.h"

#include "xor.h"

int xor_initlog(void *arguments, void *logic)
{
	struct xor_args *args = (struct xor_args *)arguments;
	struct xor_logic *log = (struct xor_logic *)logic;

	log->out_ptr = log_manager.nets_data + (args->io.out >> 3);
	log->out_bit_no = args->io.out & 0x07;

	log->in1_ptr = log_manager.nets_data + (args->io.in1 >> 3);
	log->in1_bit_no = args->io.in1 & 0x07;

	log->in2_ptr = log_manager.nets_data + (args->io.in2 >> 3);
	log->in2_bit_no = args->io.in2 & 0x07;

	if(log->out_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	if(log->in1_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	if(log->in2_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	return 0;
}

void xor(void *arguments, void *logic)
{
	struct xor_args *args = (struct xor_args *)arguments;
	struct xor_logic *log = (struct xor_logic *)logic;

	if(args->io.out)
	{
		if(*log->in1_ptr & (1<<log->in1_bit_no))
		{
			if(*log->in2_ptr & (1<<log->in2_bit_no))
				*log->out_ptr&=~(1<<log->out_bit_no);
			else
				*log->out_ptr|=(1<<log->out_bit_no);
		}
		else
		{
			if(*log->in2_ptr & (1<<log->in2_bit_no))
				*log->out_ptr|=(1<<log->out_bit_no);
			else
				*log->out_ptr&=~(1<<log->out_bit_no);
		}
	}
}
