/*
 * or4.c
 *
 *  Created on: 07-04-2025
 *      Author: Pawel Skoczykloda
 */


#include <math.h>
#include "tdefs.h"
#include "misc.h"
#include "helper.h"


#include "od_bl.h"

int od_bl_initlog(void *arguments, void *logic)
{
	struct od_bl_args *args = (struct od_bl_args *)arguments;
	struct od_bl_logic *log = (struct od_bl_logic *)logic;

	if(set_bit_ptr_mask(args->io.in1_odst_in,&log->in_odst[0],&log->in_odst_bit_mask[0]))
		return -1;
	if(set_bit_ptr_mask(args->io.in2_odst_in,&log->in_odst[1],&log->in_odst_bit_mask[1]))
		return -1;	
	if(set_bit_ptr_mask(args->io.in3_odst_in,&log->in_odst[2],&log->in_odst_bit_mask[2]))
		return -1;
			
	if(set_bit_ptr_mask(args->io.in1_bl_in,&log->in_bl[0],&log->in_bl_bit_mask[0]))
		return -1;
	if(set_bit_ptr_mask(args->io.in2_bl_in,&log->in_bl[1],&log->in_bl_bit_mask[1]))
		return -1;	
	if(set_bit_ptr_mask(args->io.in3_bl_in,&log->in_bl[2],&log->in_bl_bit_mask[2]))
		return -1;
					
	if(set_bit_ptr_mask(args->io.bl_out,&log->out_bl,&log->out_bl_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.odst_out,&log->out_odst,&log->out_odst_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.od_bl_out,&log->out_od_bl,&log->out_od_bl_bit_mask))
		return -1;
	
	log->odstawione = (args->params.bity != 0) ? 1 : 0;
	
	return 0;
}

void od_bl(void *arguments, void *logic)
{
	struct od_bl_logic *log = (struct od_bl_logic *)logic;

	u8 temp_odst, temp_blok;
	
	temp_odst = log->odstawione || (*log->in_odst[0] & log->in_odst_bit_mask[0]) ||
				(*log->in_odst[1] & log->in_odst_bit_mask[1]) || (*log->in_odst[2] & log->in_odst_bit_mask[2]);

	if (temp_odst)
		temp_blok = 0;
	else
		temp_blok = (*log->in_bl[0] & log->in_bl_bit_mask[0]) || (*log->in_bl[1] & log->in_bl_bit_mask[1]) ||
				(*log->in_bl[2] & log->in_bl_bit_mask[2]);
				
	check_and_set_mask(temp_odst,log->out_odst,log->out_odst_bit_mask);
	check_and_set_mask(temp_blok,log->out_bl,log->out_bl_bit_mask);
	check_and_set_mask((temp_blok || temp_odst),log->out_od_bl,log->out_od_bl_bit_mask);

}
