Some problems lead themselves to a recursive solution. Is recursive instantiation possible in Verilog? Is it possible for a module to instantiate itself?
相关问题
- PHP Recursively File Folder Scan Sorted by Modific
- Using single ended port in logic expecting diff-pa
- recursion, Python, countup, countdown
- JavaScript fibonacci using recursion
- Warning: (vsim-7) Failed to open readmem file “mem
相关文章
- Recursively replace keys in an array
- Python: Maximum recursion depth exceeded when prin
- Is recursion good in SQL Server?
- How does this list comprehension over the inits of
- Find all subfolders of the Inbox folder using EWS
- What is the definition of “natural recursion”?
- Recursively decrement a list by 1
- When is tail recursion guaranteed in Rust?
Some problems lead themselves to a recursive solution. Finding the minimum of a set of numbers is just such a job: basically, the minimum of a set of numbers is the minimum of the minimum of the first half and the minimum of the second half.
Take a set of numbers. Divide that set in half. Find the minimum of each half. The minimum of the whole set is the lesser of the minimums of each half. How do you find the minimum of each half? Well, each half is a set of numbers, so for each half, go back to the beginning of this paragraph...
It's recursive. Eventually, you get a set of one number. The minimum of that set is obviously that number, so the problem becomes trivial, which is the point of working something out recursively. So, how can we do this in hardware using recursive instantiation?
We need a parameterizable module with N inputs. Inside the module, if N is greater than 2, we instantiate 2 copies of the module and route half the inputs to one and half the inputs to the other. The output of the module is then the lesser of the outputs of these 2 submodules. If N is 1, however, we just connect the input straight to the output. That's it.
To do this in Verilog, we're going to have to use parameters and, given we might instantiate something or we might not, generate statements.
So, here is a Verilog implementation of this algorithm:
There are two parameters: N is the number of inputs and W is the width of each. Unfortunately, there is a complication: Verilog does not allow array ports. Instead we have to use a single vector input of width N*W. You can see this in the code. This complicates the code, because calculations then have to be done to index the bits of this input vector.
So, the core of our code is the generate block. Inside that, we test the parameter N to see how many inputs we are dealing with. If we are dealing with one input, then the job is easy: the minimum value just equal to that one input. Otherwise, we are dealing with more than one input and so send half the inputs to one instance of the MinN module and the rest to the other instance. Outside the generate block we determine the overall minimum by comparing the outputs of each subblock.
Of course, this would be possible in SystemVerilog, because Verilog is a subset of SystemVerilog. But it would actually be easier in SystemVerilog, because you are allowed array ports.
And this is synthesizable.