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

// Do NOT move macros, functions and includes beginning with "spacecomp".

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

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

int32_t subtractor::subtract(int32_t operand1, int32_t operand2) {
	return operand1 - operand2;
}

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

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


#include "spacecomp_post_cpp.h"
