-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid Module creation for Constant Gate scenarios #481
base: main
Are you sure you want to change the base?
Changes from 3 commits
292bf2d
81734f2
efa4b95
7a3974a
cbf0c26
8e7789b
abb0d87
4393d10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,13 +319,43 @@ class Logic { | |
void operator <=(Logic other) => gets(other); | ||
|
||
/// Logical bitwise NOT. | ||
Logic operator ~() => NotGate(this).out; | ||
Logic operator ~() => this is Const ? Const(~value) : NotGate(this).out; | ||
|
||
/// Logical bitwise AND. | ||
Logic operator &(Logic other) => And2Gate(this, other).out; | ||
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; | ||
} | ||
} | ||
return And2Gate(this, other).out; | ||
} | ||
|
||
/// Logical bitwise OR. | ||
Logic operator |(Logic other) => Or2Gate(this, other).out; | ||
Logic operator |(Logic other) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here for |
||
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)) { | ||
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; | ||
} | ||
} | ||
return Or2Gate(this, other).out; | ||
} | ||
|
||
/// Logical bitwise XOR. | ||
Logic operator ^(Logic other) => Xor2Gate(this, other).out; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,6 +353,115 @@ void main() { | |
}); | ||
}); | ||
|
||
group('Const input Gate test', () { | ||
test('NotGate single bit Constant input', () async { | ||
final a = Logic(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. naming the variables could be helpful for understanding the intent of the test, e.g. |
||
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(~b, isA<Const>()); | ||
expect((~b).value, equals(LogicValue.one)); | ||
|
||
expect(~c, isA<Const>()); | ||
expect((~c).value, equals(LogicValue.x)); | ||
|
||
expect(~d, isA<Const>()); | ||
expect((~d).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); | ||
|
||
expect(~b, isA<Const>()); | ||
expect((~b).value, equals(LogicValue.of(LogicValue.zero, width: 4))); | ||
|
||
expect(~c, isA<Logic>()); | ||
expect((~c).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>()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anything |
||
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)); | ||
}); | ||
|
||
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)); | ||
|
||
expect(a & c, isA<Const>()); | ||
expect((a & c).value, equals(LogicValue.of(LogicValue.zero, width: 4))); | ||
|
||
expect(b & c, isA<Logic>()); | ||
expect((b & c).value, equals(c.value)); | ||
|
||
expect(d & e, isA<Const>()); | ||
expect((d & e).value, equals(LogicValue.of('10110', width: 32))); | ||
|
||
expect(y & z, isA<Logic>()); | ||
expect((y & z).value, equals(LogicValue.of('11x01', width: 5))); | ||
}); | ||
|
||
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); | ||
final z = Const('11111', width: 5); | ||
final y = Const(LogicValue.of('11x01', width: 5)); | ||
a.put(LogicValue.one); | ||
|
||
expect(a | b, isA<Logic>()); | ||
expect((a | b).value, equals(a.value)); | ||
|
||
expect(a | c, isA<Logic>()); | ||
expect((a | c).value, equals(LogicValue.one)); | ||
|
||
expect(a | d, isA<Logic>()); | ||
expect((a | d).value, equals(LogicValue.one)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anything |
||
|
||
expect(y | z, isA<Logic>()); | ||
expect((y | z).value, equals(LogicValue.of('11111', width: 5))); | ||
}); | ||
|
||
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); | ||
|
||
expect(a | c, isA<Logic>()); | ||
expect((a | c).value, equals(c.value)); | ||
|
||
expect(b | c, isA<Const>()); | ||
expect((b | c).value, equals(LogicValue.filled(4, LogicValue.one))); | ||
}); | ||
}); | ||
|
||
group('simcompare', () { | ||
test('NotGate single bit', () async { | ||
final gtm = GateTestModule(Logic(), Logic()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be made a little more general? If either one is a Const, then you can do something special:
The current logic only covers if both are const.
You could remove some duplication in the implementation by making local variables to point to "the constant one" and "the other one", so you don't need to consider it both ways for all cases.