Velocity Reviews > VHDL > How to count zeros in registers?

# How to count zeros in registers?

Davy
Guest
Posts: n/a

 11-29-2005
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

John Penton
Guest
Posts: n/a

 11-29-2005
Davy wrote:
> 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?

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.

Stephane
Guest
Posts: n/a

 11-29-2005
John Penton wrote:
> Davy wrote:
>
>>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?

>
>
> 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
>

This would waste a lot of resources if the reg is large... I would have
done it sequentially with a counter!

Fred Bloggs
Guest
Posts: n/a

 11-29-2005

Davy wrote:
> 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
>

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?

Jim Thompson
Guest
Posts: n/a

 11-29-2005
On 29 Nov 2005 06:12:56 -0800, "Davy" <(E-Mail Removed)> wrote:

>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

See...

http://www.analog-innovations.com/SED/HowManyOnes.pdf

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/Mixed-Signal ASIC's and Discrete Systems | manus |
| Phoenix, Arizona Voice480)460-2350 | |
| E-mail Address at Website Fax480)460-2142 | Brass Rat |
| http://www.analog-innovations.com | 1962 |

I love to cook with wine. Sometimes I even put it in the food.

Petrov_101@hotmail.com
Guest
Posts: n/a

 11-29-2005

Fred Bloggs wrote:
> Davy wrote:
> > 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
> >

>
> Hey why don't tell us a little about the application, because this
> sounds like something totally divorced from reality.

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

John Penton
Guest
Posts: n/a

 11-29-2005
Jim Thompson wrote:
> On 29 Nov 2005 06:12:56 -0800, "Davy" <(E-Mail Removed)> wrote:
>
>> 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

>
> See...
>
> http://www.analog-innovations.com/SED/HowManyOnes.pdf
>
> You'll need to modify it for "zeroes" and to stop at the first
> occurrence of a "one"

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.

bill.sloman@ieee.org
Guest
Posts: n/a

 11-29-2005

Fred Bloggs wrote:
> Davy wrote:
> > 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
> >

>
> Hey why don't tell us a little about the application, because this
> sounds like something totally divorced from reality.

<snipped Fred being conversational>

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

Richard Henry
Guest
Posts: n/a

 11-29-2005

"Davy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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?

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

Register --> ROM --> Output

Examples:

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.

soxmax
Guest
Posts: n/a

 11-29-2005

Davy wrote:
> 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

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