Skip to content

Commit 536d1ba

Browse files
authored
Merge pull request #4720 from dirkx/feat-multi-input-and-or
Multi input AND/OR;
2 parents 6f90560 + d019900 commit 536d1ba

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎src/hal/components/and.comp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
component and "Multi input AND gate";
2+
pin in bool in##[128:personality] = 1 "input pins, up to personality-1";
3+
pin out bool out "output pin; the AND of all inputs";
4+
5+
description """
6+
The *out* pin is set to *TRUE* if all the inputs are all *TRUE*; or *FALSE* if one or more are *FALSE*.
7+
8+
The value for a unconnected pin is TRUE; so it is fine to leave a pin unconnected.
9+
10+
Set the number of pins via personality. The default value is a single *AND* with 8 inputs; so personality=8. The maximum number of inputs is 128 (hardcoded). If there are no inputs specified (personality=0); then the output value
11+
is TRUE.
12+
""";
13+
14+
function _ "Logical AND";
15+
16+
see_also """
17+
*or*(9),
18+
and2(9)
19+
and
20+
or2(9).
21+
""";
22+
23+
option period no;
24+
option default_personality 8;
25+
option default_count 1;
26+
27+
license "ASLv2";
28+
author "Dirk-Willem van Gulik <dirkx(at)webweaving(punto)org";
29+
;;
30+
FUNCTION(_) {
31+
bool tmp = 1;
32+
// we prefer constant time; so do not return 0 on the first 0.
33+
for(int i = 0; i < personality; i++)
34+
tmp = tmp && in(i);
35+
out_set(tmp);
36+
}

‎src/hal/components/or.comp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
component or "Arbitrary input OR gate";
2+
pin in bool in##[128:personality] = 0 "in";
3+
pin out bool out "output pin; the OR of all inputs";
4+
5+
description """
6+
The *out* pin is set to *TRUE* if any the inputs is *TRUE*; or *FALSE* if all are *FALSE*.
7+
8+
The value for a unconnected pin is FALSE; so it is fine to leave a pin unconnected.
9+
10+
Set the number of pins via personality. The default value is a single *OR* with 8 inputs; so personality=8. The
11+
maximum number of inputs is 128 (hardcoded). If there are no input pins at all (personality=0); then
12+
the output is *FALSE*.
13+
"""
14+
;
15+
function _;
16+
see_also """
17+
and(9), and2(9) and or2(9).
18+
""";
19+
20+
option period no;
21+
option default_personality 8;
22+
option default_count 1;
23+
24+
license "ASLv2";
25+
author "Dirk-Willem van Gulik <dirkx(at)webweaving(punto)org";
26+
;;
27+
FUNCTION(_) {
28+
bool tmp = 0;
29+
// we prefer constant time; so do not return 0 on the first true
30+
for(int i = 0; i < personality; i++)
31+
tmp = tmp || in(i);
32+
out_set(tmp);
33+
}

0 commit comments

Comments
 (0)