Skip to content

Commit

Permalink
Added fixes based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mjayasim9 committed Jul 9, 2024
1 parent 7a3974a commit 8e7789b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 73 deletions.
58 changes: 37 additions & 21 deletions lib/src/signals/logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -387,35 +387,51 @@ class Logic {

/// Logical bitwise AND.
Logic operator &(Logic other) {
if (this is Const &&
other is Const &&
value.isValid &&
other.value.isValid) {
if (value == LogicValue.of(0, width: width) ||
other.value == LogicValue.of(0, width: width)) {
return Const(LogicValue.of(0, width: width));
} else if (value == LogicValue.filled(width, LogicValue.one)) {
return other;
} else if (other.value == LogicValue.filled(width, LogicValue.one)) {
return this;
if (this is Const && other is Const) {
return Const((this as Const).value & other.value);
}
Const? constOperand;
Logic? otherOperand;

if (this is Const && value.isValid) {
constOperand = Const(this);
} else if (other is Const && other.value.isValid) {
constOperand = other;
otherOperand = this;
}

if (constOperand != null) {
if (constOperand.value == LogicValue.filled(width, LogicValue.zero)) {
return Const(LogicValue.filled(width, LogicValue.zero));
} else if (constOperand.value ==
LogicValue.filled(width, LogicValue.one)) {
return otherOperand!;
}
}
return And2Gate(this, other).out;
}

/// Logical bitwise OR.
Logic operator |(Logic other) {
if (this is Const &&
other is Const &&
value.isValid &&
other.value.isValid) {
if (value == LogicValue.filled(width, LogicValue.one) ||
other.value == LogicValue.filled(width, LogicValue.one)) {
if (this is Const && other is Const) {
return Const((this as Const).value | other.value);
}
Const? constOperand;
Logic? otherOperand;

if (this is Const && value.isValid) {
constOperand = Const(this);
} else if (other is Const && other.value.isValid) {
constOperand = other;
otherOperand = this;
}

if (constOperand != null) {
if (constOperand.value == LogicValue.filled(width, LogicValue.one)) {
return Const(LogicValue.filled(width, LogicValue.one));
} else if (value == LogicValue.of(LogicValue.zero, width: width)) {
return other;
} else if (other.value == LogicValue.of(LogicValue.zero, width: width)) {
return this;
} else if (constOperand.value ==
LogicValue.filled(width, LogicValue.zero)) {
return otherOperand!;
}
}
return Or2Gate(this, other).out;
Expand Down
98 changes: 46 additions & 52 deletions test/gate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -355,76 +355,70 @@ void main() {

group('Const input Gate test', () {
test('NotGate single bit Constant input', () async {
final a = Logic();
final b = Const(LogicValue.zero, width: 1);
final c = Const(LogicValue.x, width: 1);
final d = Const(LogicValue.one, width: 1);
a.put(LogicValue.one);
final normalLogic = Logic();
final const0 = Const(LogicValue.zero, width: 1);
final constX = Const(LogicValue.x, width: 1);
final const1 = Const(LogicValue.one, width: 1);
normalLogic.put(LogicValue.one);

expect(~b, isA<Const>());
expect((~b).value, equals(LogicValue.one));
expect(~const0, isA<Const>());
expect((~const0).value, equals(LogicValue.one));

expect(~c, isA<Const>());
expect((~c).value, equals(LogicValue.x));
expect(~constX, isA<Const>());
expect((~constX).value, equals(LogicValue.x));

expect(~d, isA<Const>());
expect((~d).value, equals(LogicValue.zero));
expect(~const1, isA<Const>());
expect((~const1).value, equals(LogicValue.zero));
});

test('NotGate Multi bit Constant input', () async {
final b = Const(bin('1111'), width: 4);
final c = Const(bin('0100'), width: 4);
final const1 = Const(bin('1111'), width: 4);
final const4 = Const(bin('0100'), width: 4);

expect(~b, isA<Const>());
expect((~b).value, equals(LogicValue.of(LogicValue.zero, width: 4)));
expect(~const1, isA<Const>());
expect((~const1).value, equals(LogicValue.of(LogicValue.zero, width: 4)));

expect(~c, isA<Logic>());
expect((~c).value, equals(LogicValue.of(11, width: 4)));
expect(~const4, isA<Logic>());
expect((~const4).value, equals(LogicValue.of(11, width: 4)));
});

test('And2Gate Single bit Constant input', () async {
final a = Logic();
final b = Const(LogicValue.zero, width: 1);
final c = Const(LogicValue.x, width: 1);
final d = Const(LogicValue.one, width: 1);
a.put(LogicValue.one);

expect(a & b, isA<Logic>());
expect((a & b).value, equals(LogicValue.zero));

expect(a & c, isA<Logic>());
expect((a & c).value, equals(LogicValue.x));

expect(b & c, isA<Logic>());
expect((b & c).value, equals(LogicValue.zero));

expect(a & d, isA<Logic>());
expect((a & d).value, equals(a.value));

expect(b & d, isA<Const>());
expect((b & d).value, equals(LogicValue.zero));
final normalLogic = Logic();
final const0 = Const(LogicValue.zero);
final logicX = Logic();
final const1 = Const(LogicValue.one);
normalLogic.put(LogicValue.one);
logicX.put(LogicValue.x);

expect(normalLogic & const0, isA<Const>());
expect((normalLogic & const0).value, equals(LogicValue.zero));
expect(logicX & const0, isA<Const>());
expect((logicX & const0).value, equals(LogicValue.zero));
expect(normalLogic & const1, isA<Logic>());
expect((normalLogic & const1).value, equals(normalLogic.value));
expect(normalLogic & logicX, isA<Logic>());
expect((normalLogic & logicX).value, equals(LogicValue.x));
expect(const0 & const1, isA<Const>());
});

test('And2Gate Multi bit Constant input', () async {
final a = Const(LogicValue.of(LogicValue.zero, width: 4));
final b = Const('1111', width: 4);
final c = Const('0100', width: 4);
final d = Const(1, width: 32, fill: true);
final e = Const('10110', width: 32);
final z = Const('11111', width: 5);
final y = Const(LogicValue.of('11x01', width: 5));
final const1 = Const(bin('1111'), width: 4);
final const4 = Const(bin('0100'), width: 4);
final logic0 = Logic(width: 4)..put(LogicValue.zero);
final logic1 = Logic(width: 4)..put(LogicValue.one);
final logicX = Logic(width: 4)..put(LogicValue.x);

expect(a & c, isA<Const>());
expect((a & c).value, equals(LogicValue.of(LogicValue.zero, width: 4)));
expect(const1 & const4, isA<Const>());
expect((const1 & const4).value, equals(LogicValue.of('0100', width: 4)));

expect(b & c, isA<Logic>());
expect((b & c).value, equals(c.value));
expect(logic0 & const1, isA<Logic>());
expect((logic0 & const1).value, equals(LogicValue.of('0000', width: 4)));

expect(d & e, isA<Const>());
expect((d & e).value, equals(LogicValue.of('10110', width: 32)));
expect(logic1 & const1, isA<Logic>());
expect((logic1 & const1).value, equals(LogicValue.of('0001', width: 4)));

expect(y & z, isA<Logic>());
expect((y & z).value, equals(LogicValue.of('11x01', width: 5)));
expect(logicX & const1, isA<Logic>());
expect((logicX & const1).value, equals(LogicValue.of('xxxx', width: 4)));
});

test('OR2Gate Single bit Constant input', () async {
Expand Down

0 comments on commit 8e7789b

Please sign in to comment.