|
| 1 | +import 'package:rohd/rohd.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | +import 'helper.dart'; |
| 4 | + |
| 5 | +class FullSubtractor extends Module { |
| 6 | + FullSubtractor(Logic a, Logic b, Logic borrowIn) |
| 7 | + : super(name: 'full_subtractor') { |
| 8 | + // Add Input |
| 9 | + a = addInput('a', a); |
| 10 | + b = addInput('b', b); |
| 11 | + borrowIn = addInput('borrowIn', borrowIn); |
| 12 | + |
| 13 | + // Add Output |
| 14 | + final borrowOut = addOutput('borrowOut'); |
| 15 | + final diff = addOutput('diff'); |
| 16 | + |
| 17 | + // Logic |
| 18 | + final xorAB = a ^ b; |
| 19 | + diff <= xorAB ^ borrowIn; |
| 20 | + borrowOut <= (~xorAB & borrowIn) | (~a & b); |
| 21 | + } |
| 22 | + // getter for output |
| 23 | + Logic get borrowOut => output('borrowOut'); |
| 24 | + Logic get diff => output('diff'); |
| 25 | +} |
| 26 | + |
| 27 | +void main() async { |
| 28 | + final a = Logic(name: 'a'); |
| 29 | + final b = Logic(name: 'b'); |
| 30 | + final borrowIn = Logic(name: 'borrow_in'); |
| 31 | + |
| 32 | + final fSub = FullSubtractor(a, b, borrowIn); |
| 33 | + await fSub.build(); |
| 34 | + |
| 35 | + // ignore: avoid_print |
| 36 | + print(fSub.generateSynth()); |
| 37 | + |
| 38 | + test('should return 0 when a and b equal 1', () async { |
| 39 | + a.put(1); |
| 40 | + b.put(1); |
| 41 | + borrowIn.put(0); |
| 42 | + |
| 43 | + expect(fSub.diff.value.toInt(), equals(0)); |
| 44 | + }); |
| 45 | + |
| 46 | + test('should return true if results matched truth table', () async { |
| 47 | + for (var i = 0; i <= 1; i++) { |
| 48 | + for (var j = 0; j <= 1; j++) { |
| 49 | + for (var k = 0; k <= 1; k++) { |
| 50 | + a.put(i); |
| 51 | + b.put(j); |
| 52 | + borrowIn.put(k); |
| 53 | + |
| 54 | + final res = fsTruthTable(i, j, k); |
| 55 | + |
| 56 | + expect(fSub.diff.value.toInt(), res.diff); |
| 57 | + expect(fSub.borrowOut.value.toInt(), res.borrowOut); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | +} |
0 commit comments