bluespec.com Forum Index bluespec.com
Bluespec Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

function with parameter

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ...
View previous topic :: View next topic  
Author Message
seeker_123



Joined: 26 Sep 2013
Posts: 8

PostPosted: Mon Sep 30, 2013 12:41 am    Post subject: function with parameter Reply with quote

Hii..
I have a function for comparator for 8 bit. I want to make this comparator bit width as parametrized. So that whenever I am calling that function I want to decide its width. How to pass this value ? can anyone help me?

Thanks
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 499

PostPosted: Mon Sep 30, 2013 12:59 am    Post subject: Re: function with parameter Reply with quote

Widths can be passed as type parameters. This feature of functions with type parameters is called "polymorphism", and the topic is explained in Chapter 9 of the BSV By Example book (which is available as a PDF file in the Bluespec release). An example would look like this:
Code:
function Bool compare(Bit#(n) value1, Bit#(n) value2);
   return (value1 == value2);
endfunction

In the type signature for this function, "n" is a variable. The variable "n" is implicitly a type parameter. This function can be applied to the type Bit#(n) for any value of "n":
Code:
Bit#(8) x=0;
Bit#(8) y = 1;
// Using as size 8
Bool b = compare(x,y);

Bit#(5) w = 2;
Bit#(5) z = 2;
// Using as size 5
Bool b2 = compare(w,z)

The type variable "n" can also be referred to in the body of the function:
Code:
function Bool isZero(Bit#(n) value);
   Bit#(n) z = 0;  // this uses the type "n"
   return (value == z);
endfunction

And numeric type values can be converted into Integer values using the pseudofunction "valueOf":
Code:
function Integer findFirstOne(Bit#(n) value);
   Integer res = -1;
   for (Integer i=valueOf(n)-1; i>=0; i=i-1)
      if (value[i] == 1)
         res = i;
   return res;
endfunction
Back to top
View user's profile Send private message
seeker_123



Joined: 26 Sep 2013
Posts: 8

PostPosted: Mon Sep 30, 2013 1:12 am    Post subject: Reply with quote

Thanks quark,
That was very helpful.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ... All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
bluespec.com topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP