-
Notifications
You must be signed in to change notification settings - Fork 0
/
logisim.c
59 lines (52 loc) · 1.21 KB
/
logisim.c
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
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
typedef bool wire; // Wires
typedef struct
{
bool value;
wire *in, *out;
} reg; // Flip-flops
// Circuit constructs
#define CLOCK for (;; END_CYCLE)
#define NAND(X, Y) (!((X) && (Y)))
#define NOT(X) (NAND(X, 1))
#define AND(X, Y) (NOT(NAND(X, Y)))
#define OR(X, Y) (NAND(NOT(X), NOT(Y)))
// Circuit emulation helpers
#define END_CYCLE ({ end_cycle(); putchar('\n'); fflush(stdout); sleep(1); })
#define PRINT(X) printf(#X " = %d; ", X)
// Wire and register specification
wire X, Y, X1, Y1, A, B, C, D, E, F, G;
reg b1 = {.in = &X1, .out = &X};
reg b0 = {.in = &Y1, .out = &Y};
// Dump wire values at the end of each cycle
void end_cycle()
{
PRINT(A);
PRINT(B);
PRINT(C);
PRINT(D);
PRINT(E);
PRINT(F);
PRINT(G);
}
int main()
{
CLOCK
{
// 1. Wire network specification (logic gates)
X1 = AND(NOT(X), Y);
Y1 = NOT(OR(X, Y));
A = D = E = NOT(Y);
B = 1;
C = NOT(X);
F = Y1;
G = X;
// 2. Lock data in flip-flops and propagate output to wires
b0.value = *b0.in;
b1.value = *b1.in;
*b0.out = b0.value;
*b1.out = b1.value;
}
}