/*
 * mki7_2.c
 *
 *  Created on: 02-08-2024
 *      Author: Krzysztof Jakubczyk
 */

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

#include "mki7_2.h"
#include "helper.h"

volatile u8 mki7_2_sfplinks = 0xFF;

int mki7_2_initlog(void *arguments, void *logic)
{
	struct mki7_2_args *args = (struct mki7_2_args *)arguments;
	struct mki7_2_logic *log = (struct mki7_2_logic *)logic;

	if(set_bit_ptr_struct(args->io.z145_out,&log->Z145))
		return -1;
	if(set_bit_ptr_struct(args->io.z146_out,&log->Z146))
		return -1;
	if(set_bit_ptr_struct(args->io.z147_out,&log->Z147))
		return -1;
	if(set_bit_ptr_struct(args->io.z148_out,&log->Z148))
		return -1;
	if(set_bit_ptr_struct(args->io.err_out,&log->err))
		return -1;

	return 0;
}

void mki7_2_20hz(void *arguments, void *logic)
{
	struct mki7_2_logic *log = (struct mki7_2_logic *)logic;

	if(mki7_2_sfplinks == 0xFF)
	{
		set_struct(&log->err);
		clear_struct(&log->Z145);
		clear_struct(&log->Z146);
		clear_struct(&log->Z147);
		clear_struct(&log->Z148);
	}
	else
	{
		clear_struct(&log->err);
		check_and_set_struct(mki7_2_sfplinks & 0x01,&log->Z148);
		check_and_set_struct(mki7_2_sfplinks & 0x02,&log->Z146);
		check_and_set_struct(mki7_2_sfplinks & 0x04,&log->Z147);
		check_and_set_struct(mki7_2_sfplinks & 0x08,&log->Z148);
	}
}
