summaryrefslogtreecommitdiff
path: root/logic.c
blob: 1775de09ec28e926cfef8189abbcca71a0619b26 (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 <stdlib.h>

#include "logic.h"
#include "error.h"

enum BINARY
logic_not(enum BINARY input) {
	switch (input) {
		case LOGIC_HIGH:
			return LOGIC_LOW;
		case LOGIC_LOW:
			return LOGIC_HIGH;
		default:
			emit_error("Severe: Invalid logic value found\n");
			abort();
	}
}

enum BINARY
logic_and(enum BINARY a, enum BINARY b) {
	if (a == LOGIC_HIGH && b == LOGIC_HIGH)
		return LOGIC_HIGH;
	else
		return LOGIC_LOW;
}

enum BINARY
logic_or(enum BINARY a, enum BINARY b) {
	if (a == LOGIC_HIGH || b == LOGIC_HIGH)
		return LOGIC_HIGH;
	else
		return LOGIC_LOW;
}

enum BINARY
logic_xor(enum BINARY a, enum BINARY b) {
	if (   (a == LOGIC_HIGH && b == LOGIC_LOW)
	    || (a == LOGIC_LOW  && b == LOGIC_HIGH))
		return LOGIC_HIGH;
	else
		return LOGIC_LOW;
}

enum BINARY
logic_nand(enum BINARY a, enum BINARY b) {
	return logic_not(logic_and(a, b));
}

enum BINARY
logic_nor(enum BINARY a, enum BINARY b) {
	return logic_not(logic_or(a, b));
}