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


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

#include "and.h"

int and_initlog(void *arguments, void *logic)
{
	struct and_args *args = (struct and_args *)arguments;
	struct and_logic *log = (struct and_logic *)logic;

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

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

	log->in2_ptr = log_manager.nets_data + (args->io.in2 >> 3);
	log->in2_bit_mask = 1<<(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 and(void *arguments, void *logic)
{
//	struct and_args *args = (struct and_args *)arguments;
	struct and_logic *log = (struct and_logic *)logic;

	if((*log->in1_ptr & log->in1_bit_mask) &&  (*log->in2_ptr & log->in2_bit_mask))
		*log->out_ptr|=log->out_bit_mask;
	else
		*log->out_ptr&=~log->out_bit_mask;
}
