-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalu.cpp
100 lines (94 loc) · 1.67 KB
/
alu.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "alu.hpp"
/*
op1 op0 output
0 0 A and B
0 1 A or B
1 0 A xor B
1 1 invert A
*/
bus logic_unit(bit op1, bit op0, bus a, bus b)
{
return select_bus(
nand(op0, op1),
select_bus(
op0,
or_bus(a, b),
select_bus(
op1,
xor_bus(a, b),
and_bus(a, b)
)
),
inv_bus(a)
);
}
/*
op1 op0 output
0 0 A + B
0 1 A + 1
1 0 A - B
1 1 A - 1
*/
bus arithmetic_unit(bit op1, bit op0, bus a, bus b)
{
return select_bus(
nand(op0, op1),
select_bus(
op0,
adder(a, VCC),
select_bus(
op1,
subtractor(a, b),
adder(a, b)
)
),
subtractor(a, VCC)
);
}
/*
sel : logic or arithmetic unit
Input Output
sel op1 op0
0 0 0 A and B
0 0 1 A or B
0 1 0 A xor B
0 1 1 invert A
1 0 0 A + B
1 1 0 A - B
1 0 1 A + 1
1 1 1 A - 1
if zl : zeros left operand
if sw : swaps operands
*/
bus alu(bit sel, bit op1, bit op0, bit zl, bit sw, bus a, bus b)
{
bus l = select_bus(
zl,
GND,
select_bus(
sw,
b,
a
)
);
bus r = select_bus(
sw,
a,
b
);
return select_bus(
sel,
arithmetic_unit(op1, op0, l, r),
logic_unit(op1, op0, l, r)
);
}
bit condition(bit lt, bit eq, bit gt, bus x)
{
return or_gate(
or_gate(
and_gate(eq, eq_zero(x)),
and_gate(gt, gt_zero(x))
),
and_gate(lt, lt_zero(x))
);
}