/*
 * demux8.c
 *
 *  Created on: 22-05-2017
 *      Author: Krzysztof Jakubczyk
 */

#include <math.h>

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

#include "helper.h"
#include "demux8.h"

int demux8_initlog(void *arguments, void *logic)
{
	struct demux8_args *args = (struct demux8_args *)arguments;
	struct demux8_logic *log = (struct demux8_logic *)logic;

	if(set_bit_ptr_struct(args->io.we_in,&log->we))
		return -1;
	if(set_bit_ptr_struct(args->io.a0_in,&log->a0))
		return -1;
	if(set_bit_ptr_struct(args->io.a1_in,&log->a1))
		return -1;
	if(set_bit_ptr_struct(args->io.a2_in,&log->a2))
		return -1;

	if(set_bit_ptr_struct(args->io.wy1_out,&log->wy[0]))
		return -1;
	if(set_bit_ptr_struct(args->io.wy2_out,&log->wy[1]))
		return -1;
	if(set_bit_ptr_struct(args->io.wy3_out,&log->wy[2]))
		return -1;
	if(set_bit_ptr_struct(args->io.wy4_out,&log->wy[3]))
		return -1;
	if(set_bit_ptr_struct(args->io.wy5_out,&log->wy[4]))
		return -1;
	if(set_bit_ptr_struct(args->io.wy6_out,&log->wy[5]))
		return -1;
	if(set_bit_ptr_struct(args->io.wy7_out,&log->wy[6]))
		return -1;
	if(set_bit_ptr_struct(args->io.wy8_out,&log->wy[7]))
		return -1;

	return 0;
}

void demux8(void *arguments, void *logic)
{
	struct demux8_logic *log = (struct demux8_logic *)logic;
	u8 choice,i;

	choice = check_struct(&log->a0)?1:0;
	choice |= check_struct(&log->a1)?2:0;
	choice |= check_struct(&log->a2)?4:0;

	for(i=0;i<8;i++)
		clear_struct(&log->wy[i]);

	if (check_struct(&log->we))
		set_struct(&log->wy[choice]);
}
