// Filename : divider.cpp
//
// This file was initially generated by SpaceStudio but is meant to be modified
// by the user.
#include "divider.h"
#include "spacecomp_pre_cpp.h"

divider::divider(SPACECOMP_CTOR_PARAMS(divider, INDEX))
	: abstract_module(SPACECOMP_MBASE_ARGS(divider, INDEX))
	, SPACECOMP_START_ILIST(divider, INDEX)
	// Other initializers may go here
{
	SPACECOMP_THREAD(divider, INDEX, thread);
	request_stack(16 * 1024);
}

void divider::thread(SPACECOMP_THREAD_PARAMS(divider, INDEX, thread)) {
	spacecomp_thread_initialize();
	int32_t operand1;
	int32_t operand2;
	int32_t result;
	while (1) {
		const bool initializing = spacecomp_thread_loop_start();
		if (initializing) {}
		operand1 = read_operand();
		operand2 = read_operand();
		result = divide(operand1, operand2);
		send_result(result);
	}
}

int32_t divider::divide(int32_t operand1, int32_t operand2) {
	return operand1 / operand2;
}

int32_t divider::read_operand() {
	int32_t data;
	ModuleRead(CONTROLLER0_ID, SPACE_BLOCKING, data);
    return data;
}

void divider::send_result(int32_t data) {
	ModuleWrite(CONTROLLER0_ID, SPACE_BLOCKING, data);
}

#include "spacecomp_post_cpp.h"
