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


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

#include "nand.h"

int nand_initlog(void *arguments, void *logic)
{
	struct nand_args *args = (struct nand_args *)arguments;
	struct nand_logic *log = (struct nand_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 nand(void *arguments, void *logic)
{
	struct nand_args *args = (struct nand_args *)arguments;
	struct nand_logic *log = (struct nand_logic *)logic;

	if(args->io.out)
	{
		if(!( (*log->in1_ptr & (1<<log->in1_bit_no)) &&  (*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);
	}
}
