Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions lib/src/signals/logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,59 @@ class Logic {
void operator <=(Logic other) => gets(other);

/// Logical bitwise NOT.
Logic operator ~() => NotGate(this).out;
Logic operator ~() {
if (this is Const) {
if (value.isValid) {
Comment thread
mjayasim9 marked this conversation as resolved.
Outdated
if (value == LogicValue.of(0, width: width)) {
return Const(LogicValue.filled(width, LogicValue.one));
} else if (value == LogicValue.filled(width, LogicValue.one)) {
return Const(LogicValue.of(0, width: width));
}
} else {
return Const(LogicValue.x, width: width);
}
}
return NotGate(this).out;
}

/// Logical bitwise AND.
Logic operator &(Logic other) => And2Gate(this, other).out;
Logic operator &(Logic other) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon review, I realized that perhaps operators could be overridden on Const class instead of these special cases here.

For example:

if(other is Const) {
return other & this;
}

Then in Const you can just override @override Logic operator &(Logic other) to perform the const version of things.

if (this is Const || other is Const) {
if (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;
Comment thread
mjayasim9 marked this conversation as resolved.
Outdated
} else if (other.value == LogicValue.filled(width, LogicValue.one)) {
return this;
}
} else {
return Const(LogicValue.x, width: width);
Comment thread
mjayasim9 marked this conversation as resolved.
Outdated
}
}
return And2Gate(this, other).out;
}

/// Logical bitwise OR.
Logic operator |(Logic other) => Or2Gate(this, other).out;
Logic operator |(Logic other) {
Comment thread
mjayasim9 marked this conversation as resolved.
if (this is Const || other is Const) {
if (value.isValid && other.value.isValid) {
if (value == LogicValue.filled(width, LogicValue.one) ||
other.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 {
return Const(LogicValue.x, width: width);
}
}
return Or2Gate(this, other).out;
}

/// Logical bitwise XOR.
Logic operator ^(Logic other) => Xor2Gate(this, other).out;
Expand Down
103 changes: 103 additions & 0 deletions test/gate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,109 @@ void main() {
});
});

group('Const input Gate test', () {
test('NotGate single bit Constant input', () async {
final a = Logic();
Comment thread
mjayasim9 marked this conversation as resolved.
Outdated
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 result = ~b;
expect(result, isA<Const>());
expect(result.value, equals(LogicValue.one));

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

final result3 = ~d;
expect(result3, isA<Const>());
expect(result3.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 result = ~b;

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

final result2 = ~c;
expect(result2, isA<Logic>());
expect(result2.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);

final result = a & b;
expect(result, isA<Const>());
expect(result.value, equals(LogicValue.zero));

final result2 = a & c;
expect(result2, isA<Const>());
expect(result2.value, equals(LogicValue.x));

final result3 = a & d;
expect(result3, isA<Logic>());
expect(result3.value, equals(a.value));
});

test('And2Gate Multi bit Constant input', () async {
final a = Const(LogicValue.of(LogicValue.zero, width: 4));
final b = Const(bin('1111'), width: 4);
final c = Const(bin('0100'), width: 4);
final result = a & c;

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

final result2 = b & c;
expect(result2, isA<Logic>());
expect(result2.value, equals(c.value));
});

test('OR2Gate 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 result = a | b;
expect(result, isA<Logic>());
expect(result.value, equals(a.value));

final result2 = a | c;
expect(result2, isA<Const>());
expect(result2.value, equals(LogicValue.x));

final result3 = a | d;
expect(result3, isA<Const>());
expect(result3.value, equals(LogicValue.one));
});

test('OR2Gate Multi bit Constant input', () async {
final a = Const(LogicValue.of(LogicValue.zero, width: 4));
final b = Const(bin('1111'), width: 4);
final c = Const(bin('0100'), width: 4);
final result = a | c;

expect(result, isA<Logic>());
expect(result.value, equals(c.value));

final result2 = b | c;
expect(result2, isA<Const>());
expect(result2.value, equals(LogicValue.filled(4, LogicValue.one)));
});
});

group('simcompare', () {
test('NotGate single bit', () async {
final gtm = GateTestModule(Logic(), Logic());
Expand Down