-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathcombinational_logic.dart
More file actions
174 lines (140 loc) · 4.83 KB
/
Copy pathcombinational_logic.dart
File metadata and controls
174 lines (140 loc) · 4.83 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import 'dart:math';
import 'package:rohd/rohd.dart';
import 'package:test/test.dart';
import '../chapter_3/helper.dart';
import 'case.dart';
import 'if_block.dart';
class FullAdderResult {
final sum = Logic(name: 'sum');
final cOut = Logic(name: 'c_out');
}
enum FACond { ifblock, caseBlock }
class FullAdder extends Module {
// Constructor
FullAdder({
required Logic a,
required Logic b,
required Logic carryIn,
FACond selection = FACond.ifblock,
super.name = 'full_adder',
}) {
// Declare Input Node
a = addInput('a', a, width: a.width);
b = addInput('b', b, width: b.width);
carryIn = addInput('carry_in', carryIn, width: carryIn.width);
// Declare Output Node
final carryOut = addOutput('carry_out');
final sum = addOutput('sum');
if (selection == FACond.ifblock) {
Combinational([truthTableIf(a, b, carryIn, sum, carryOut)]);
} else {
Combinational([truthTableCase(a, b, carryIn, sum, carryOut)]);
}
}
FullAdderResult get fullAdderRes {
final fullAdderresult = FullAdderResult();
fullAdderresult.sum <= output('sum');
fullAdderresult.cOut <= output('carry_out');
return fullAdderresult;
}
}
class NBitAdder extends Module {
// Add Input and output port
final sum = <Logic>[];
Logic carry = Const(0);
Logic a;
Logic b;
NBitAdder(this.a, this.b) {
// Declare Input Node
a = addInput('a', a, width: a.width);
b = addInput('b', b, width: b.width);
carry = addInput('carry_in', carry, width: carry.width);
final n = a.width;
FullAdder? res;
assert(a.width == b.width, 'a and b should have same width.');
for (var i = 0; i < n; i++) {
res = FullAdder(a: a[i], b: b[i], carryIn: carry);
carry = res.fullAdderRes.cOut;
sum.add(res.fullAdderRes.sum);
}
sum.add(carry);
}
LogicValue get sumRes => sum.rswizzle().value;
}
void main() async {
group('full adder', () {
test(
'should return true if result sum and cout similar to truth table when'
' using If as conditionals.', () async {
final a = Logic(name: 'a');
final b = Logic(name: 'b');
final cIn = Logic(name: 'carry_in');
final fullAdder = FullAdder(a: a, b: b, carryIn: cIn);
await fullAdder.build();
for (var i = 0; i <= 1; i++) {
for (var j = 0; j <= 1; j++) {
for (var k = 0; k <= 1; k++) {
a.put(i);
b.put(j);
cIn.put(k);
final actualSum = fullAdder.fullAdderRes.sum.value.toInt();
final expectedSum = faTruthTable(i, j, k).sum;
final actualCout = fullAdder.fullAdderRes.cOut.value.toInt();
final expectedCout = faTruthTable(i, j, k).cOut;
expect(actualSum, expectedSum,
reason: 'a: $i, b: $j, c: $k, ActualSum: $actualSum, '
'ExpectedSum: $expectedSum');
expect(actualCout, expectedCout,
reason: 'a: $i, b: $j, c: $k, ActualCout: $actualCout, '
'ExpectedCout: $expectedCout');
}
}
}
});
test(
'should return true if result sum and cout similar to truth table '
'when using Case as conditionals.', () async {
final a = Logic(name: 'a');
final b = Logic(name: 'b');
final cIn = Logic(name: 'carry_in');
final fullAdder =
FullAdder(a: a, b: b, carryIn: cIn, selection: FACond.caseBlock);
await fullAdder.build();
for (var i = 0; i <= 1; i++) {
for (var j = 0; j <= 1; j++) {
for (var k = 0; k <= 1; k++) {
a.put(i);
b.put(j);
cIn.put(k);
final actualSum = fullAdder.fullAdderRes.sum.value.toInt();
final expectedSum = faTruthTable(i, j, k).sum;
final actualCout = fullAdder.fullAdderRes.cOut.value.toInt();
final expectedCout = faTruthTable(i, j, k).cOut;
expect(actualSum, expectedSum,
reason: 'a: $i, b: $j, c: $k, ActualSum: $actualSum, '
'ExpectedSum: $expectedSum');
expect(actualCout, expectedCout,
reason: 'a: $i, b: $j, c: $k, ActualCout: $actualCout, '
'ExpectedCout: $expectedCout');
}
}
}
});
});
group('nBitAdder', () {
test('should return correct results when nbitadder A and B perform add.',
() async {
final a = Logic(name: 'a', width: 8);
final b = Logic(name: 'b', width: 8);
final nbitAdder = NBitAdder(a, b);
await nbitAdder.build();
final randA = Random().nextInt(10);
final randB = Random().nextInt(10);
final addResult = randA + randB;
a.put(randA);
b.put(randB);
expect(nbitAdder.sumRes.toInt(), equals(addResult),
reason: 'randA: $randA, randB: $randB, addResult: $addResult');
});
});
}