Can size of a port be input as a parameter?

OP #5991541
Rate this post
• ▲ useful
▼ not useful
I have a hardware algorithm that compares two operands, each (keyBits) 
bits long, and returns a logical one if the first operand is less than 
the second, and returns a logical zero otherwise. This algorithm works 
for any integer value of (keyBits) greater than one. So can I write my 
algorithm with the following Verilog code, by passing (keyBits) in as a 
parameter? I haven't tried compiling or simulating this code; quite 
frankly I don't know how to do either. Can anyone help me with that?
1
module lessThan ( lfLessThanRg, left, right);
2

3
parameter integer keyBits = 2;
4
integer maxBit = keyBits - 1;
5

6
output lfLessThanRg;
7
input [maxBit:0] left, right;
8

9
wire[maxBit:0] notLeft;
10
wire[maxBit:1] ltHere, ltEve, ltBelow, equal;
11

12
or( lfLessThanRg, ltHere[ maxBit], ltEve[ maxBit]);
13

14
genvar integer bit;
15

16
generate
17
  for (bit = maxBit; 0 < bit; bit = bit - 1)
18
  begin
19
    and( ltHere[ bit], notLeft[ bit], right[ bit]);
20
    and( ltEve[ bit], equal[ bit], ltBelow[ bit]);
21
    or( equal[ bit], notLeft[ bit], right[ bit]);
22
    not( notLeft[ bit], left[ bit]);
23
    if (1 < bit)
24
    begin
25
      or( ltBelow[ bit], ltHere[ bit - 1], ltEve[ bit - 1]);
26
    end
27
  end
28
endgenerate
29

30
and( ltBelow[ 1], notLeft[ 0], right[ 0]);
31
not( notLeft[ 0], left[ 0]);
32

33
endmodule
Guest #5991673
Rate this post
• ▲ useful
▼ not useful
Why can you not just write it like that:
1
module lessThan ( lfLessThanRg, left, right);
2

3
parameter keyBits = 2;
4

5
output lfLessThanRg;
6
input [keyBits-1:0] left, right;
7

8
assign lfLessThanRg = left < right;
9

10
endmodule

How you compile or simulate depends alot on the tools you use. I would 
try  icarus verilog. I'm pretty sure you can find several tutorials in 
the web for it.

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.