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

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

void adder::thread(SPACECOMP_THREAD_PARAMS(adder, 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 = add(operand1, operand2);
		send_result(result);
	}
}

int32_t adder::add(int32_t operand1, int32_t operand2) {
	return operand1 + operand2;
}

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

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

#include "spacecomp_post_cpp.h"
