Author Topic: HDL problem: find the first bit equal to zero, and set it to one  (Read 11337 times)

0 Members and 1 Guest are viewing this topic.

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
so, you have a vector of 1024 bit (it might be 4x bigger), they are initially all zero, and sometimes, inside a process, you need to find the lower bit set to zero, and once found you need to set it to one.

It's like find the first bit equal to zero, and set it to one so it will be skipped on future searches.

it's 1024 bit, how could you implement it in a decent way?
As a giant circuit made on the cascade of "IF" vector(x) branches ?  :-//


edit:
OT but interesting: some ISA supports ffs, even if their implementation is 32bit/64bit. Here I need the circuit for a different purpose. Happy to know that also MIPS comes with something similar.

Code: [Select]
MIPS clz Count Leading Zeros in Word 32, 64 clz input size
MIPS clo Count Leading Ones in Word 32, 64 clo input size



« Last Edit: July 04, 2017, 05:00:07 pm by legacy »
 

Offline filssavi

  • Frequent Contributor
  • **
  • Posts: 433
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #1 on: July 04, 2017, 05:14:55 pm »
You need to provide more informations if you want more precise answers.

That said assuming that by hdl you mean hardware description language, and thus FPGA there is more than One way to skin this Digital cat  if resource usage is a problem you can do this by setting up a pipeline that fetches data from a dual porta ram,  lsb to msb, compares It and makes necessary changes and if necessary writes them back

If speed Is the focus i'd divide and conquer, so split the Vector in smaller chunks, do as above on the chunks once you are Shure you found the lowest 0 bit in the lowest chunks stop the process
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #2 on: July 04, 2017, 05:30:57 pm »
Usually I prefer vhdl. I am planning a design for a small fpga.

Code: [Select]
procedure find_first_set
(
   v              : in bit_vector;
   found          : out boolean;
   first_set_index: out natural
)
     is
begin
  for index in v'range
    loop
      if v(index)
         then
           found           := true;
           first_set_index := index;
           return;
         end if;
    end loop
  found := false;
end procedure;

Something like this works on the paper as well as on simulators, I have some doubt about the implementation.

I have to use it in a clocked process

Code: [Select]
process(clk)
begin
  if rising_edge(clk) then
    find_first_set(...
  end if;
end process;

Where clk needs to be around 100MHz (it's 33Mhz x3, using a PLL) on a Spartan3E-250 device.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #3 on: July 04, 2017, 05:47:15 pm »
I can relax a constraint: "find_first_set" can use a first clock's edge, and bit_set can stay on a second one.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3149
  • Country: ca
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #4 on: July 04, 2017, 06:06:18 pm »
If speed Is the focus i'd divide and conquer

Divide is certainly the way to go. If you split the vector in half and do the analysis for each half separately you'll get:

found1, first_set_index1 - for the LS half
found2, first_set_index2 - for the MS half.

Now:

Code: [Select]
if found1 = '1' then
  found <= '1';
  first_set_index(first_set_index'left) <= '0';
  first_set_index(first_set_index'left-1 downto 0) <= first_set_index1;
else
  found <= found2;
  first_set_index(first_set_index'left) <= '1';
  first_set_index(first_set_index'left-1 downto 0) <= first_set_index2;
end if

And so you continue dividing until you get to the array of size 1 which is trivial.

This will give you 10 logic levels which is Ok for 100 MHz. It'll be quite big though.

 

Offline marshallh

  • Supporter
  • ****
  • Posts: 1462
  • Country: us
    • retroactive
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #5 on: July 04, 2017, 07:09:51 pm »
If you need single cycle it will require a lot of combinational logic. Otherwise you can just usea counter and shift by 1 bit every clock, until you find hte bit, modify, it and shift everything back until the counter resets.
Verilog tips
BGA soldering intro

11:37 <@ktemkin> c4757p: marshall has transcended communications media
11:37 <@ktemkin> He speaks protocols directly.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #6 on: July 04, 2017, 07:52:30 pm »
Isn't what you are describing a long shift register, initially set to all zero, and then ones shifted in from one end (or you can look at it as the lowest zero bit being set)

Or am I missing something?
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #7 on: July 04, 2017, 07:53:51 pm »
If you need single cycle

constraint: find and set has be within two clock edges.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #8 on: July 04, 2017, 07:56:58 pm »
Isn't what you are describing a long shift register

Well, it looks more like a giant priority encoder than a shift register.
And I cannot shift one bit per clock edge  :D
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #9 on: July 04, 2017, 07:58:24 pm »
it will require a lot of combinational logic

exactly the feeling I had  :palm: :palm: :palm:
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3149
  • Country: ca
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #10 on: July 04, 2017, 08:01:13 pm »
Isn't what you are describing a long shift register, initially set to all zero, and then ones shifted in from one end (or you can look at it as the lowest zero bit being set)

Or am I missing something?

As I understand, there's a 1024-bit register. You scan it from one end. When (if) you find '1', you replace it with '0' and stop.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #11 on: July 04, 2017, 08:33:53 pm »
This will give you 10 logic levels which is Ok for 100 MHz. It'll be quite big though.

Thanks, good idea :D

Perhaps I'd better switch to a bigger fpga, like S3-500. I have to check the whole project utilization, currently it's just a proof, I mean the project is composed by different modules, a lot of them haven't yet been analyzed, therefore I don't know the current logic utilization, but now I know the "find the first" module will consume a lot of logic.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #12 on: July 04, 2017, 08:50:33 pm »
As I understand, there's a 1024-bit register. You scan it from one end. When (if) you find '1', you replace it with '0' and stop.

yes, but you cannot "scan" :D

Scanning implies that you check one bit per rising clock edge: it can't be applied to my problem, since I need to find the first bit equal to zero in a vector of 1024-bit  ... within one clock rising edge. As constraint the search mustn't take more than one clock, and the bit-set to 1 mustn't take more than one clock, in other words the whole mustn't take more than two clock rising edges.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #13 on: July 04, 2017, 08:55:10 pm »
Isn't what you are describing a long shift register, initially set to all zero, and then ones shifted in from one end (or you can look at it as the lowest zero bit being set)

Or am I missing something?

As I understand, there's a 1024-bit register. You scan it from one end. When (if) you find '1', you replace it with '0' and stop.

so, you have a vector of 1024 bit (it might be 4x bigger), they are initially all zero,

0000... ...0000

and sometimes, inside a process, you need to find the lower bit set to zero, and once found you need to set it to one.

After each sometimes:

0000... ...0001
0000... ...0011
0000... ...0111
0000... ...1111
...
0001... ...1111
0011... ...1111
0111... ...1111
1111... ...1111

Looks like a shift register to me.  :-//

Either that or we are not being told the whole story :D

Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3149
  • Country: ca
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #14 on: July 04, 2017, 09:06:40 pm »
Looks like a shift register to me.  :-//

I assumed the register is modified by other means too, so the regular shift pattern will eventually dissipate.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #15 on: July 04, 2017, 09:21:13 pm »
Either that or we are not being told the whole story :D

ummm, yup  :D

Forgot to say that sometimes another process needs to reset the bit in the vector to zero, and since you can't assume any predefined scheme it's like it was a random process which re-sets bit to zero randomly, therefore the shift-register-scheme is broken.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #16 on: July 04, 2017, 10:35:58 pm »
Smallest/slowest answer is something like

Set up a chain from high to low, checking for when the first zero is seen.

   all_zeros(1023) <= NOT vector(n);
   for n in 0 to 1022 loop;
       all_zeros(n) <= all_zeros(n+1) AND NOT vector(n);
   end loop;

...and then leave the tools to optimize it for the underlying architecture (they will most likely do a poor job :D )
   
During the update just set the bit
   if rising_edge(clk) and something = '1' then
     vector(0) <= '1';
     for n in 1 to 1023 loop
        vector(n) <= vector(n) OR (all_zeros(n) AND vector(n-1));
     end loop;
   end;

There is no way getting around it, that the new state for bit one depends on the state of all other bits in the vector. Somehow the state from the 1023 other bits must fan in to allow it to be updated.

If you need to do that in one cycle the best you can do is celling(log(1024)/log(LUT width)) levels of LUTs...

...unless you also maintain a vector of "this block of n bits is all zeros" alongside the main one, allowing you to move some of the work a cycle earlier.
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline Flip-Flop

  • Newbie
  • Posts: 1
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #17 on: July 05, 2017, 06:09:11 pm »
Try something like this:
Code: [Select]
Process(clk)
Variable a, b, c : unsigned(1024 downto 0);
Begin
   If rising_edge(clk) then
      a := unsigned('0' & vector);
      b := a + 1;
      c := not a and b; -- contains a one at the position of the first zero
      vector_out <= vector xor c(1023 downto 0); -- Toggle first zero
   End if;
End process;
 

Offline kmike

  • Regular Contributor
  • *
  • Posts: 59
  • Country: de
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #18 on: July 06, 2017, 12:26:11 pm »
You clould shift the register to the right with 0s and to the left with 1s.

edit:
Like this: (may contain errors  ;))
shift_reg <= shift_reg(REGSIZE-1 downto 0) & '1'
shift_reg <= '0' & shift_reg(REGSIZE downto 1)

Br,
mike
« Last Edit: July 06, 2017, 12:30:22 pm by kmike »
 

Offline Gribo

  • Frequent Contributor
  • **
  • Posts: 632
  • Country: ca
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #19 on: July 06, 2017, 03:13:25 pm »
Is that 1024 bit vector a status register for lots of other processes?
I am available for freelance work.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #20 on: July 06, 2017, 03:40:41 pm »
You might be able to reduce the complexity by creating for example 32  identical macroblocks with 32 bits each. Each block will output 1 if any of its input bits are zero, and the bit position of the zero value bit (5 bits). With some simple decoding you should be able to do the tricks you want to perform with decent propagation delay.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #21 on: July 06, 2017, 04:31:48 pm »
@Kalvin
good trick, I think I will follow your idea  :D
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #22 on: July 06, 2017, 04:56:59 pm »
Is that 1024 bit vector a status register for lots of other processes?

yes it's a status register, but only two processes are involved with it.
 

Offline Gribo

  • Frequent Contributor
  • **
  • Posts: 632
  • Country: ca
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #23 on: July 06, 2017, 06:01:36 pm »
So, let me understand it, one process is filling the vector with bits at an unknown rate, the other is emptying it, again, at an unknown rate?
I am available for freelance work.
 

Offline JacobPilsen

  • Regular Contributor
  • *
  • Posts: 144
  • Country: cz
Re: HDL problem: find the first bit equal to zero, and set it to one
« Reply #24 on: July 06, 2017, 07:35:55 pm »
It sounds (very similar) like "Flash ADC" to me.
Don't you know how "highest 1 of 1024" to "binary number" is implemented:
« Last Edit: July 06, 2017, 07:38:33 pm by JacobPilsen »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf