How to count zeros in registers?

"Spehro Pefhany" schreef in bericht news: snipped-for-privacy@4ax.com...

A few minutes indeed. Here's another nice puzzle I stumbled on yesterday:

formatting link

Took me about 3 minutes to figure out half a solution. The problem I have is the requirement: "With this improved version, when the board is initially turned on you are able to light the bulbs in any order.".

Spending another 30 minutes didn't bring me any luck ;)

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

"Spehro Pefhany" schreef in bericht news: snipped-for-privacy@4ax.com...

I don't know. If you mount the bulbs and caps in random order, you could "program" the board by flipping the switches in the same sequence as the lamps are mounted. So, if the bulbs are mounted as B-Y-G-R, flip the switches in B-Y-G-R sequence, thus telling the board which switch belongs to which bulb. Not many would notice that turning on the switches in that sequence, is actually a learning cycle for the controller. Only needed once after power-on reset, and then the game can continue.

But then there is the claim that you can turn on the lamps in any order, no matter in what sequence they are mounted.

Perhaps there is enough difference in resistance between the colored bulbs to indentify them reliably. The controller could then measure the cold resistance to find out which bulb is in which socket.

Sounds like a fun project, bit of shame I don't need a fun project at this moment ;)

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

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

Well, Davy,

The question is clear enough but I miss the background. I cannot believe this to be a serious design problem. Anyone who has some basic knowledge of digital design should be able to answer immediately. A practical solution depends on the background I'm missing. You can use a bunch of logic gates, an EPROM or a PLD to name a few.

petrus bitbyter

Reply to
petrus bitbyter

I think your answer is about the best, given that the OP seems to want verilog (although he didn't explicitly SAY that). But the original post also is visible here in sci.electronics.design (as well as a few other groups), where many of the other answers are more-or-less on-topic.

Probably the OP should have posted to comp.lang.verilog only.

I can't imagine that it is ever a good idea to post to comp.lang.verilog and comp.lang.vhdl. ;-)

--Mac

Reply to
Mac

Hmm, yes, looks a bit complicated to me. It would require a lot of fiddling with the cylinders, with the added risk that people will notice that.

A mini version can be seen here:

formatting link

Here we see no cylinders, only sockets screwed on a wood base. The caps on the tumbler switches don't allow for much trickery either. Although there are some large black mountings around it, which perhaps can be rotated. That could be part of another deception, make people believe these can be rotated. And when they try that, it turns out to make no difference at all. Something like that ;)

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

I'm not native speaker, but I don't agree :

formatting link

Anyway, I wouldn't start a sentence with "and"...

Reply to
Stephane

The book "Hacker's Delight" from Henry S. Warren, ISBN 0201914654, available at Amazon lists several algorithms to do this. Some use loops and others only logical test. Very interesting reading.

Meindert

Reply to
Meindert Sprang

I agree about the others, but this wouldn't be my first answer. To me the obvious solution is a for-loop over the word that returns the loop counter as soon a '1' is found.

The reason why this "obvious" solution apparently isn't that obvious to many people may be that this is one more occasion in which Verilog doesn't really help. Many languages have a 'return' or 'break' statement to break out of a loop early and cleanly. But in Verilog, one has to use that awkward 'disable' statement to emulate this behavior.

Jan

--
Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com
Losbergenlaan 16, B-3010 Leuven, Belgium
     From Python to silicon:
     http://myhdl.jandecaluwe.com
Reply to
Jan Decaluwe

Jan Decaluwe skrev:

yep loop is better, I've always been told that casex is evil :)

you could just run the loop backwards i.e. starting from the lsb

-Lasse

Reply to
langwadt

Holy Mole, that seems like a contest winner how to do the job with the most gates possible (altho a modern compiler will optimize it down some)

Just draw the K-map and loop the functions.

The problem tells us there are 8 bits, people

One thing, the problem doesn't specify: what is wanted when the register contains 0 0 0 0 0 0 0 0

Wood

Reply to
Woody Brison

Or just left shift until a "1" appears?

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

Just half-adders.

If you have that luxury.

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

[snip]

The OP (for HowManyOnes) wanted to count ALL 1's, not just leading.

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

What's 'C'? I'm a hardware man ;-)

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

you know, if i was coding a processor and doing something like the Intel fams, i would something like this. ASM Mov BH, SourceByte; Mov BL, $80 ; Create a stop bit; XOr AL, AL ;clear Zero count; @LOop: Shl BX, 1 ;shift them to the left. Jc @done: ; if bit on then done. inc AL ; other wise, inc ct. Jnz @Loop; ; Return back for another @Done: End;

// AL would be the 0 count. etc..

oh well, just a thought i guess.

--
Real Programmers Do things like this.
http://webpages.charter.net/jamie_5
Reply to
Jamie

This is what the VHDL code could look like-- takes about 5ns maximum input to output in my ModelSim simulation. It's maybe 50 'real' gates total. I arbitrarily decided to output 0x1000 for all zeros.

library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity zerocount is Port ( reg : in std_logic_vector(7 downto 0); n : out std_logic_vector(3 downto 0)); end zerocount;

architecture Behavioral of zerocount is

begin countz: process(reg) begin if (reg = "00000000") then n

Reply to
Spehro Pefhany

I thought "before the first 1" meant "leading". ;-)

Here's the part:

formatting link

Might have to invert the bits - this chip actually counts '1's. Or you could do it in Xilinx schematic mode. ;-)

Cheers! Rich

Reply to
Rich Grise

Then you use a while loop.

NZeros = 0; loop_count = 7; // or maybe this should be 8; While ((X & 0x80) && loop_count) ( NZeros++; X

Reply to
Rich Grise

I found on the web some discussions, with this as the best observation:

somewhere (yet another "thick base"). If you notice, the trick is performed, so that every time the switches are thrown, the lights light up in the order they are in the sockets, from right to left. (This is using the Carson performance as reference). So, I'd assume that it's some sort of circuit so that no matter what switch is thrown, the first goes on, then the second switch thrown turns on the second one, as so forth.

Which sounds like some very simple sequential logic.

Also, suggestions were made that tracking the order in which lamps were removed, then replaced, allows the unit to "memorize" the positions. But that in itself would not explain the final part of the trick with the exchanging of the switch caps, in that part the operation (sequencing) of the switches must be special.

--
Regards,
Arie de Muynck
Reply to
Arie de Muynck

On 30/11/2005 the venerable snipped-for-privacy@ieee.org etched in runes:

. .

And look for what? You'd have to look ahead at the remaining bits for all zeroes.

Wouldn't it be better to right shift until the result contains zero and then subtract the number of right shifts from the length

--
John B
Reply to
John B

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.