EmbDev.net

Forum: FPGA, VHDL & Verilog Can size of a port be input as a parameter?


von Kevin S. (kvnsmnsn)


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

: Edited by User
von Andy (Guest)


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.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.