aboutsummaryrefslogtreecommitdiff
path: root/src/common.c
blob: 402320eeb2d5451a518d85a427e7a2e57a45339d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <assert.h>
#include "types.h"
#include "instset.h"
#include "common.h"

static Sender g_sender;
static Receiver g_receiver;

void sal_comm_set_sender(Sender sender)
{
	/* Set sender functor. Whenever an organism calls the SEND instruction,
	this function will get called. When unset, SEND instruction is ignored.
	*/
	assert(sender);
	g_sender = sender;
}

void sal_comm_set_receiver(Receiver receiver)
{
	/* Set receiver functor. Whenever an organism calls the RCVE instruction,
	this function will get called. When unset, RCVE instruction is ignored.
	*/
	assert(receiver);
	g_receiver = receiver;
}

void _sal_comm_send(uint8 inst)
{
	/* Send a single byte (instruction) to the sender. This function is called
	by processes that execute the SEND instruction.
	*/
	assert(sal_is_inst(inst));

	if (g_sender) {
		g_sender(inst);
	}
}

uint8 _sal_comm_receive(void)
{
	/* Receive a single byte (instruction) from the receiver. This function is
	called by processes that execute the RCVE instruction. It returns NOP0 is
	receiver is unset.
	*/
	if (g_receiver) {
		uint8 inst = g_receiver();
		assert(sal_is_inst(inst));
		return inst;
	} else {
		return NOP0;
	}
}