Author Topic: Greater than N in combinatorial logic  (Read 19086 times)

0 Members and 1 Guest are viewing this topic.

Offline jeremyTopic starter

  • Super Contributor
  • ***
  • Posts: 1080
  • Country: au
Greater than N in combinatorial logic
« on: December 29, 2014, 07:26:30 pm »
Hi all,

I'm looking for something like an OR gate, but that needs at least N inputs to be high for the output to be high. N would be something like 2-3 and the number of inputs would be about 10. I am curious about doing it in discrete gates, not HDLs.

Is this is a solved problem? Or perhaps it is one of those problems with a solution so complicated that it is useless?
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: Greater than N in combinatorial logic
« Reply #1 on: December 29, 2014, 07:30:52 pm »
EPROM as a lookup table: 10 inputs go to the address lines and the precomputed lookup-table for every input combination stored into EPROM output the desired result at the data lines.

Edit: Maybe this cannot be classified as combinatorial logic, but at least the solution is quite flexible. For example, with 1Kx8 EPROM you can provide easily eight different outputs from which the user may select how many inputs 2 .. 9 need to be active in order the output to become "1".
« Last Edit: December 30, 2014, 02:37:34 pm by Kalvin »
 

Offline trophosphere

  • Frequent Contributor
  • **
  • Posts: 282
  • Country: us
Re: Greater than N in combinatorial logic
« Reply #2 on: December 29, 2014, 07:47:23 pm »
If your requirement doesn't change, you can try solving it by writing out the entire Boolean representation (likely SOP form) and minimize the expression either by hand or using software such as Wolfram's Boolean algebra calculator.
 

Offline richard.cs

  • Super Contributor
  • ***
  • Posts: 1224
  • Country: gb
  • Electronics engineer from Southampton, UK.
    • Random stuff I've built (mostly non-electronic and fairly dated).
Re: Greater than N in combinatorial logic
« Reply #3 on: December 29, 2014, 07:58:41 pm »
If an analogue solution is ok then buffer your 10 inputs, sum them with resistors and feed into a comparator with an apropriate threshold.
 

Offline Alex Eisenhut

  • Super Contributor
  • ***
  • Posts: 3732
  • Country: ca
  • Place text here.
Re: Greater than N in combinatorial logic
« Reply #4 on: December 29, 2014, 10:45:07 pm »
Hi all,

I'm looking for something like an OR gate, but that needs at least N inputs to be high for the output to be high. N would be something like 2-3 and the number of inputs would be about 10. I am curious about doing it in discrete gates, not HDLs.

Is this is a solved problem? Or perhaps it is one of those problems with a solution so complicated that it is useless?

https://en.wikipedia.org/wiki/Majority_function

Hoarder of 8-bit Commodore relics and 1960s Tektronix 500-series stuff. Unconventional interior decorator.
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Greater than N in combinatorial logic
« Reply #5 on: December 29, 2014, 11:05:27 pm »
Do it the same way you'd do it in VHDL.

Remember, when writing VHDL, you're writing a description of RTL (register transfer level logic).  If you ever forget this, and you write nonsense instead... well, yeah.  The practical difference between VHDL and traditional procedural languages like C is more than just when a statement is relevant: it's that C is compiled to sequential software, whereas VHDL is synthesized to static logic.  To do a very good job of C, you need to know what it compiles to; to do a very good job of VHDL, you need to know what it synthesizes to.

Anyway, last time I did that, it was:

Input debounce circuit.  Majority voting method.  Input is sampled at system clock frequency (which is several times the bitrate expected for this input; the input should also be analog filtered to a similar rate -- because after all, just because it's a 1-bit DAC doesn't mean sampling theory just goes out the window!), and sent through N shift registers.  The bits are added up as unary.  This looks something like, "q <= bit[0] + bit[1] + bit[2] + ... ;".  Q needs LG(N) bits to account for carries, and you use a comparison of Q > N/2 to determine if it's up or down.  (To enhance the input-debounce-iness of my circuit, I also added hysteresis, so that the output only gets set to '1' when Q > N/2 + E, or '0' when Q < N/2 - E, where E is some value less than N/2.  Example: if N = 3 to 6, E = 0 to 2 is reasonable.)

The direct RTL implementation is a bunch of 1-bit adders chained together.  The carries, in turn, get summed, and so on, until all LG(N) bits of Q are formed.

Whether this finally synthesizes to chained logic (as the above RTL description suggests -- implementing each 1-bit full adder as the traditional XOR + AND), or something more complex and compact (carry look-ahead, array logic, look-up tables?) depends on the required timing constraints and available logic structure.  A CPLD would probably use a couple macrocells to do it in array logic, without much wiggle room on timing.  An FPGA would use quite a few more LUTs to do much the same thing with varying amounts of chained or parallel activity, depending on timing.

Implementing it in SSI (small scale integration -- the three elementary logic gates and their complements), you'd have to run through the logic tables to see how many minterms and maxterms are needed.  I don't know offhand.

You could even implement an even more compact solution with analog: each input connects through a resistor R to the summing junction (virtual ground) of an inverting amplifier.  Thus, each input delivers a constant current, the total of which counts the number of inputs, independent of order.  A comparator on the output then determines count against a reference.  (To implement the debounce circuit I was talking about earlier, a pair of comparators at the respective thresholds, feeding an R-S flip-flop, would complete the logic.)

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 4034
  • Country: 00
Re: Greater than N in combinatorial logic
« Reply #6 on: December 30, 2014, 03:03:16 am »
Note that there is also a part, not sure the eaxct partnumber, as a 8bit comparator.

74*688 is an magnitude comparator (equality detector), and a few numbers around there is the desired function: less than /same or higher comparator. I think it is 74*684, but not sure. Try wikipedia and 7400 logic family.

688 is/was quite often used part, so it is still possible to get them cheap. Instead of your "greater than" function, which is hard to get and these chips are quite expensive.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Greater than N in combinatorial logic
« Reply #7 on: December 30, 2014, 03:51:39 am »
You could even implement an even more compact solution with analog: each input connects through a resistor R to the summing junction (virtual ground) of an inverting amplifier.  Thus, each input delivers a constant current, the total of which counts the number of inputs, independent of order.  A comparator on the output then determines count against a reference.  (To implement the debounce circuit I was talking about earlier, a pair of comparators at the respective thresholds, feeding an R-S flip-flop, would complete the logic.)

This. If you're doing it in discrete gates, a few resistors and a comparator should be much simpler than a bunch of logic devices. You don't even need the inverting amplifier - just tie them all together, it becomes a voltage divider with R/N on top and R/(total-N) on the bottom. Use a reference voltage divider to compare the two ratiometrically.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6598
  • Country: 00
Re: Greater than N in combinatorial logic
« Reply #8 on: December 30, 2014, 05:11:07 am »
You can treat the N input as zero/one values, construct a tree of adders that will sum them and then add a detector (that simple) for N>=2 or N>=3. Constructing adders from gates is straight forward. This will give you and easy to understand circuit but not necessarily optimize (for number of gates or propagation time).

Another option is to compute it serially using a clock and down counter. Start with 3, each each cycle decrement the counter if the next input is high (e.g. using a shift register or multiplexer), at the end check if the counter had underflow.

Another options is to use a small microcontroller that will do the same in software.
 

Offline German_EE

  • Super Contributor
  • ***
  • Posts: 2399
  • Country: de
Re: Greater than N in combinatorial logic
« Reply #9 on: December 30, 2014, 12:18:32 pm »
1) Examine your inputs one by one using a 4017 counter and a series of AND gates.

2) If the output of an AND gate is high then it increments a second 4017

3) Examine the result of your operation by reading the output of the second counter

4) Reset all counters and go back to step one

This first guess is a bit messy but you've given me something interesting to think about whilst I'm dragged around various clothes shops this afternoon, thanks for an interesting exercise.
Should you find yourself in a chronically leaking boat, energy devoted to changing vessels is likely to be more productive than energy devoted to patching leaks.

Warren Buffett
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2100
  • Country: sk
Re: Greater than N in combinatorial logic
« Reply #10 on: December 30, 2014, 12:38:59 pm »
If an analogue solution is ok then buffer your 10 inputs, sum them with resistors and feed into a comparator with an apropriate threshold.

+1
 

Offline German_EE

  • Super Contributor
  • ***
  • Posts: 2399
  • Country: de
Re: Greater than N in combinatorial logic
« Reply #11 on: December 30, 2014, 01:03:56 pm »
Your task was familiar and I have just remembered what it is called, you're seeking to build something called a Hamming decoder and doing this in discrete logic will be very hard. Here's an example using only four bits.

1) Start by dividing your inputs into pairs of bits and wire each pair into an AND gate and an XOR gate. If the AND gate is high then you have two high bits, if the XOR is high then you have one high bit. Note that there is no output from either gate if both bits are at zero.

You now have two 'two bits high' outputs and two 'one bit high' outputs

2) Connect both 'two bits high' outputs into both an AND gate and an XOR gate. If the AND gate is high then you have four high bits and if the XOR gate is high then you have two high bits (but see later)

3) Connect both 'one bit high' outputs into both an AND gate and an XOR gate. If the AND gate is high then you have two high bits (but see later) and if the XOR gate is high then you have one high bit.

4) Connect the XOR gate output in step (2) and the AND gate in step (3)  to an OR gate. If this gate goes high then you have two high bits.

I haven't figured out in my head how to account for three bits high and this is for only a four bit input, now imagine how complex it would be for ten  :scared:

Being an Aspie I carry logic designs like this around in my head for fun, some regard it as torture. YMMV

Should you find yourself in a chronically leaking boat, energy devoted to changing vessels is likely to be more productive than energy devoted to patching leaks.

Warren Buffett
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2100
  • Country: sk
Re: Greater than N in combinatorial logic
« Reply #12 on: December 30, 2014, 01:56:59 pm »
or just do it in a MCU :D you just need to make sure the clock speed and "sampling" rate of the MCU will be fast enough and it will not interfere with other stuff ;) ot you can even make it interrupt driven (PIN change interrupts).
 

Offline paulie

  • Frequent Contributor
  • **
  • !
  • Posts: 849
  • Country: us
Re: Greater than N in combinatorial logic
« Reply #13 on: December 30, 2014, 02:12:31 pm »
Being an Aspie I carry logic designs like this around in my head for fun, some regard it as torture. YMMV

Aren't using terms like that considered politically incorrect? :) https://www.eevblog.com/forum/projects/my-new-favorite-opamp-tda1308/msg576953/#new

I agree that for 25 cents or less and dozen or so lines of code an MCU is the only reasonable solution. Unless of course this is another one of those "educational exercises".
 

Offline Mr Smiley

  • Frequent Contributor
  • **
  • Posts: 324
  • Country: gb
Re: Greater than N in combinatorial logic
« Reply #14 on: December 30, 2014, 03:10:06 pm »
Draw out your Truth Table, then use a Karnaugh Map

http://electronics-course.com/karnaugh-map-b?utm_expid=62927315-1.bej7MhpPSKGZZ9PUdQa-Vg.1&utm_referrer=http%3A%2F%2Felectronics-course.com%2Fboolean-algebra

to reduce the table down to an expression, then use boolean-algebra

http://electronics-course.com/boolean-algebra

to convert your and/or's to either all and's or all or's. job done.

 :)
There is enough on this planet to sustain mans needs. There will never be enough on this planet to sustain mans greed.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6598
  • Country: 00
Re: Greater than N in combinatorial logic
« Reply #15 on: December 30, 2014, 03:37:38 pm »
...You now have two 'two bits high' outputs and two 'one bit high' outputs ...

Will be useful to mention throughout the expansion 'exactly' or 'at least'.
 

Offline gregallenwarner

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Greater than N in combinatorial logic
« Reply #16 on: December 30, 2014, 05:22:05 pm »
I made this out of a bunch of half and full adders, and it does the trick. It's a 4-of-12 detector. A high on any 4 of the 12 inputs will make the output go high. If you only want to have a 10 input detector, then just connect the extra two inputs to ground. Want to make it a 3-of-10 detector? Connect one of the extra inputs to Vcc. Connecting both extra inputs to Vcc makes it a 2-of-10 detector.
 

Offline jeremyTopic starter

  • Super Contributor
  • ***
  • Posts: 1080
  • Country: au
Re: Greater than N in combinatorial logic
« Reply #17 on: December 30, 2014, 06:25:05 pm »
Hi all,

Thanks for your responses. I understand that this could be easily done with a micro, I was just curious. Basically, my question was "am I being stupid for doing that" and I think the answer seems to be a resounding no. I just wanted to make sure that there wasn't a secret 74HCXXX that completely solved it or something.

Bonus points for the analog comparator answer though, didn't think about that one!
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Greater than N in combinatorial logic
« Reply #18 on: December 30, 2014, 06:30:40 pm »
Bonus points for the analog comparator answer though, didn't think about that one!

I frankly don't understand why any discrete-gates solution would ever be preferable to a few resistors and a single comparator! Maybe if there were a single 7400 chip that could do it, but these days many of the cool ones are obsolete... all of the proposed discrete logic solutions involve multiple chips and a bunch of wiring!

Of course, if something programmable like a MCU or FPGA is available, connecting them all to that and doing it in software is the obvious solution - it's by far the most flexible.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6598
  • Country: 00
Re: Greater than N in combinatorial logic
« Reply #19 on: December 30, 2014, 07:12:04 pm »
Connect one of the extra inputs to Vcc.

That's a nice trick. Didn't occurred to me. Also using CIN as a third input.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6598
  • Country: 00
Re: Greater than N in combinatorial logic
« Reply #20 on: December 30, 2014, 07:18:47 pm »
all of the proposed discrete logic solutions involve multiple chips and a bunch of wiring!

The adders can be reduced to gates as requested in the original request. The resistors do not. ;-)
 

Offline gregallenwarner

  • Regular Contributor
  • *
  • Posts: 144
  • Country: us
Re: Greater than N in combinatorial logic
« Reply #21 on: December 30, 2014, 07:21:30 pm »
Connect one of the extra inputs to Vcc.

That's a nice trick. Didn't occurred to me. Also using CIN as a third input.

Yep. When the OP mentioned he wanted either a 2 or 3 out of 10 detector, it occurred to me, making this circuit configurable would be difficult, unless it was designed with extra inputs, which, depending on their tied value, results in the whole circuit being "reconfigured". Also, the full adders can essentially just be thought of as a 3-term half-adder (no carry capability).

Of course, my solution would be hell to actually implement with discrete gates. Mainly, I just like designing those types of circuits just to see if I can do it.

Edit: Also, going with an analog comparator solution defeats the purpose of having a digital circuit. True, something this simple could probably be reliably implemented with resistors, however, the intention should be to keep it digital, as it's a digital problem and making it analog just introduces an opportunity for error due to noise.
« Last Edit: December 30, 2014, 07:23:39 pm by gregallenwarner »
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Greater than N in combinatorial logic
« Reply #22 on: December 30, 2014, 07:30:52 pm »
Edit: Also, going with an analog comparator solution defeats the purpose of having a digital circuit.

What exactly is that purpose? Unless there's other programmable logic in the circuit, it seems to me to be something akin to beating in a screw with a hammer.

Quote
as it's a digital problem and making it analog just introduces an opportunity for error due to noise.

Nah, it's easily a digital or analog problem. Digital circuits can count things, but electric currents flowing into a junction are quite good at summing all by themselves!

As for noise - that could be an issue if you're building something really low-power where you end up using insanely large resistors to avoid wasting current, or if you have a vast number of switches such that the voltage step for each one becomes tiny, but otherwise, you'd have to try terribly hard to have noise issues with this...
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline rob77

  • Super Contributor
  • ***
  • Posts: 2100
  • Country: sk
Re: Greater than N in combinatorial logic
« Reply #23 on: December 30, 2014, 08:34:18 pm »
making it analog just introduces an opportunity for error due to noise.

according to this logic DACs and ADCs should be highly unreliable ;)
one good example when analog is preferable in digital circuitry - interfacing a 4x4 keypad with a single ADC input on a micro... the savings are simply huge - 1 ADCpin instead of 8 digital pins.... 
the very same is valid for the solution of the topic with summing currents followed by a comparator - the amount of components and complexity is at least order of magnitude less than the implementation with adders and gates.
 

Offline paulie

  • Frequent Contributor
  • **
  • !
  • Posts: 849
  • Country: us
Re: Greater than N in combinatorial logic
« Reply #24 on: December 30, 2014, 09:16:49 pm »
summing currents followed by a comparator - the amount of components and complexity is at least order of magnitude less than the implementation with adders and gates.

Using a PIC or AVR with ZERO external components is an order of magnitude less parts than the comparator solution.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->