How to count zeros in registers?

Hi all,

reg[7:0] register; The register contains data like [0 0 0 1 0 1 0 1] And I want to know the number of the zeros before the first 1 (in this example is 3 zeros).

How to do this in a combinational logic?

Best regards, Davy

Reply to
Davy
Loading thread data ...

See...

formatting link

You'll need to modify it for "zeroes" and to stop at the first occurrence of a "one"

...Jim Thompson

--
|  James E.Thompson, P.E.                           |    mens     |
|  Analog Innovations, Inc.                         |     et      |
|  Analog/Mixed-Signal ASIC\'s and Discrete Systems  |    manus    |
|  Phoenix, Arizona            Voice:(480)460-2350  |             |
|  E-mail Address at Website     Fax:(480)460-2142  |  Brass Rat  |
|       http://www.analog-innovations.com           |    1962     |
             
I love to cook with wine.      Sometimes I even put it in the food.
Reply to
Jim Thompson

Some processors have Count Leading Zeros/Ones instructions to quickly determine how many bits to left shift data. I had to write HDL code for a custom ASIC processor with a clz instruction. Neat little combinational logic problem...

Pete

Reply to
Petrov_101

The only time I had to do this, I had it easier, because I'd been asked to produced a floating point output on a multiplexed multi-character display, which was being driven from a shift register, back in the days before PICs.

I don't think the system ever worked (and in fact I just found the hardware out in the attic) but it should have worked, A colleague was building his own photon counter, and wanted to display a wide range of photon counts without paying for a large number of seven-segment displays. He dumped the half-finished system on me when he went off to concentrate on being an acadmeic chemist.

----------- Bill Sloman, Nijmegen

Reply to
bill.sloman

Use the register bits as address inputs to a ROM in which the data is coded for the results.

Register --> ROM --> Output

Examples:

ADDR DATA

00 8 01 7 02 6 03 6 04 5 05 5 06 5 07 5 08 4 ... 0f 4 10 3 ... 1f 3 20 2

etc.

Or you can write a mathematical expression involving logs to the base 2.

Reply to
Richard Henry

In order to get combinational logic, your best bet is to use a Karnaugh Map for each individual case. Also - never begin a sentence with a preposition.

-Derek

Reply to
soxmax

Pretty easy (a few minutes) if you can use a behavioral description. What have you done to try and solve this (homework?) problem yourself?

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

Sure, suppose you want to normalize a floating-point mantissa using a barrel shifter.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

It synthesized to 9 4-in LUTs on an FPGA, or 5 slices, with 6 levels of logic.

Heh. Nonvolatile memory? ;-)

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

He's probably looking for a 'clever,' in this case meaning 'low gate count' or 'fast' solution. There are plenty of digital design problems out there that are entirely straightforward to just 'code up' a solution to in, e.g., VHDL or TTL logic, but can be an order of magnitude slower or larger than more clever solutions.

Counting the number of consecutive zero bits is one of these problems.

This sort of problem comes up somewhat more frequently in software, where people stuck with, e.g., 1MHz CPUs really do need every last CPU cycle they can spare... solutions are found all over, e.g.,

formatting link
.

Reply to
Joel Kolstad

With a priority tree:

if (reg[7:0] == 8'b00000000) clz = 3'h8; else if (reg[7:1] == 7'b0000000) clz = 3'h7; else if ...

You can play some tricks to make it faster - particularly useful if your register is bigger than 8 bits. It will always be quite large.

John

--
John Penton, posting as an individual unless specifically indicated
otherwise.
Reply to
John Penton

It depends on the meaning of "initially"-- remember it's to their advantage to obfuscate a bit. Note the distinctions in the description between what *you* must do and what the "spectators" can be allowed to do (they can flip the switches, but presumably not decide *which* switches to initially flip).

If it's previously been programmed, then you can turn it on "initially" and it will remember the previous programming (and the lights can be turned on in any order-- but not re-ordered with the power off). Their early model might not have done that.

I don't think there's anything fancy-schmancy like that going on. Just switches and probably detection of bulbs removed. You're thinking way too hard about the electronics and not hard enough about the obfuscation. It's a kind of magic trick.

Reminds me of the flashing devil horns that I bought for a couple of dollars on the street one Halloween:

formatting link

Ran off of AA cells in the black cylinders on the sides, with a light in each horn. I postulated some kind of COB ASIC with a bipolar transistor driving each lamp. Felt pretty silly when I found one fractional-cent thermal flasher Xmas mini-light in each horn and no active circuitry at all. ;-)

Might be a fun project for a student. In VHDL, Verilog, with a microcontroller or whatever.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

Hey why don't tell us a little about the application, because this sounds like something totally divorced from reality. Is the real application about cheating on a homework assignment? I could understand if applied an iota of effort, but there's no evidence of that. Aren't you the lazy punk-assed juvenile sh_t with the loud mouth about plonking people and shooting your f____ing mouth off about how clever you are? Well how damned clever are you when it comes to doing something constructive, punk-ass?

Reply to
Fred Bloggs

And the answer is (the following is pseudo code in Verilog ) :

function [3:0] CountLeadingZeros ; input [7:0] source ; begin // synopsys_full_case or something like that casex ( source ) 8'b1xxxxxx : CountLeadingZeros = 4'd0 ; 8'b01xxxxx : CountLeadingZeros = 4'd1 ; 8'b001xxxx : CountLeadingZeros = 4'd2 ; ... ( you can fill in the rest here ) 8'b0000000 : CountLeadingZeros = 4'd8 ; default : CountLeadingZeros = 4'dx ; // I always put in defaults for simulation sake endcasex endfunction

This should do the trick. I have no clue what the other 21 people on this thread are talking about, or why this wasn't the first answer. but Here it is.

-Art

Reply to
Art Stamness

You are thinking too much like an engineer and not enough as a magician. It's magic, so trickery is normal. The way it's usually done is that under each bulb there are four magnetic reed relays. Notice the white cylinders? Each has a small magnet underneath. The trick is the color is actually encoded by the position of the white cylinder, not the bulb. You just need to remember to rotate the cylinder to the correct position while screwing in the bulb.

A simple schematic for a single bulb (in ASCII of course, use propotional font):

red | \\ | | green__/ ____bulb____M____blue | | \\ | yellow

M = Small magnet for closing the contact of the reed relay. Rotate this magnet to select which switch turns on this bulb.

Reply to
slebetman

Don't reinvent the wheel. Invert the SR outputs, and run them into a 74x148. If you look at the data sheet,the circuit is obvious. You want INPUT7 to be the *least* significant bit. If you have more than an 8 bit SR, you have to do some cascading. There is also a 10 bit version.

Tam

Reply to
Tam/WB2TT

I don't think this is what the OP wanted. Perhaps he could comment.

John

--
John Penton, posting as an individual unless specifically indicated
otherwise.
Reply to
John Penton

It's pretty fundamental, actually; there's at least one TTL priority encoder (the '278). A couple of obvious applications are normalisation and interrupt prioritisation.

Rick

Reply to
Rick Jackson

Google '"priority encoder" logic' - include the double-quote (") and don't include the single-quote (').

There used to be a chip, the 74148, that would give you the answer in one cycle.

Good Luck! Rich

Reply to
Rich Grise

"Davy" schreef in bericht news: snipped-for-privacy@f14g2000cwb.googlegroups.com...

Use a chain of 2-input or-gates, output to input.

Tie the other input of each gate to your register.

The outputs of each or-gate goes to an inverter.

The outputs of the inverters go to a 8-3 binary encoder.

--
Thanks, Frank.
(remove \'q\' and \'.invalid\' when replying by email)
Reply to
Frank Bemelman

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.