/*
 * sr.c
 *
 *  Created on: 12-05-2014
 *      Author: KJ
 */


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

#include "sr.h"

int sr_initlog(void *arguments, void *logic)
{
	struct sr_args *args = (struct sr_args *)arguments;
	struct sr_logic *log = (struct sr_logic *)logic;

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

	log->s_ptr = log_manager.nets_data + (args->io.s_in >> 3);
	log->s_bit_no = args->io.s_in & 0x07;

	log->r_ptr = log_manager.nets_data + (args->io.r_in >> 3);
	log->r_bit_no = args->io.r_in & 0x07;

	if(log->out_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	if(log->s_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	if(log->r_ptr >= (log_manager.nets_data+sizeof(log_manager.nets_data)))
		return -1;

	return 0;
}

void sr(void *arguments, void *logic)
{
	//struct sr_args *args = (struct sr_args *)arguments;
	struct sr_logic *log = (struct sr_logic *)logic;

	if((*log->s_ptr & (1<<log->s_bit_no)))
		*log->out_ptr|=(1<<log->out_bit_no);
	else if((*log->r_ptr & (1<<log->r_bit_no)))
		*log->out_ptr&=~(1<<log->out_bit_no);
}
