<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Casting Strings to Enums in SystemVerilog</title>
	<atom:link href="http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/</link>
	<description>Verilab</description>
	<pubDate>Thu, 17 May 2012 19:34:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: JL Gray</title>
		<link>http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/#comment-148</link>
		<dc:creator>JL Gray</dc:creator>
		<pubDate>Wed, 28 Nov 2007 19:25:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/#comment-148</guid>
		<description>Avidan,

I just looked again at your example... it actually *requires* parameterizable classes (I missed that the first time I read through the code).  For that reason, the code won't work with VCS, though it should work just fine with Modelsim.

JL</description>
		<content:encoded><![CDATA[<p>Avidan,</p>
<p>I just looked again at your example&#8230; it actually *requires* parameterizable classes (I missed that the first time I read through the code).  For that reason, the code won&#8217;t work with VCS, though it should work just fine with Modelsim.</p>
<p>JL</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JL Gray</title>
		<link>http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/#comment-145</link>
		<dc:creator>JL Gray</dc:creator>
		<pubDate>Mon, 22 Oct 2007 13:52:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/#comment-145</guid>
		<description>Avidan,

You're right, there are some significant issues with enums in SV.  However, though I haven't tried writing the code, I believe you could simplify things slightly in your example above by using parameterizable classes.

JL</description>
		<content:encoded><![CDATA[<p>Avidan,</p>
<p>You&#8217;re right, there are some significant issues with enums in SV.  However, though I haven&#8217;t tried writing the code, I believe you could simplify things slightly in your example above by using parameterizable classes.</p>
<p>JL</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avidan Efody</title>
		<link>http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/#comment-144</link>
		<dc:creator>Avidan Efody</dc:creator>
		<pubDate>Mon, 22 Oct 2007 08:22:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.verilab.com/blog/2007/10/casting-strings-to-enums-in-systemverilog/#comment-144</guid>
		<description>Hi JL,
Your entry touched a raw SV nerve :-). I guess your intention was to demonstrate the use of hashes and to provide everyone with a useful function, but on the way you also demonstrated one of SV's enums main shortcomings - although all enums have a similar behavior and share a basic set of functions, there is no such thing as "enum base class", that is common to all of them. So, in the case above, although your function might be useful for every enum, you would have to rewrite it for each different enum type...
I have come across this problem in the past as well...One way of working around it, which is not perfect, is to define a class that wrapps around enums. Here's your example rewritten (without the hash, just using brute force method):

virtual class enum_wrapper;
   // functions defined by the standard go here.
   // these are implemented by the specification
   virtual function int first();endfunction
   virtual function int last();endfunction
   virtual function int next(int current);endfunction
   virtual function int prev(int current);endfunction
   virtual function int num();endfunction
   virtual function string name(int value);endfunction
	
   // more user defined functions go here
   // here's a brute force method for searching...
   // could be made more efficient by adding the hash...
   
   function int string_to_enum(string enum_name);
      for (int i=first(); i!=last(); i=next(i))
	if (enum_name == name(i)) return i;
   endfunction // int

endclass // enum_wrapper

// just a garbage enum for default
typedef enum {false, true} bool;

class specific_enum#(type enum_type = bool) extends enum_wrapper;
   enum_type m_enum;
   
   function int first();
      first =  m_enum.first();
   endfunction; // int

   function int last();
      last =  m_enum.last();
   endfunction; // int

   function int next(int current);
      m_enum = enum_type'(current);
      next =  m_enum.next();
   endfunction; // int

   function int prev(int current);
      m_enum = enum_type'(current);
      prev =  m_enum.prev();
   endfunction; // int

   function int num();
      num =  m_enum.num();
   endfunction; // int
   
   function string name(int value);
      m_enum = enum_type'(value);
      name =  m_enum.name();
   endfunction; // int
endclass // specific_enum

typedef enum {UNKNOWN, ADD, SUB, MULT} cmd_e;

module test;
   initial begin
      string cmd_name = "ADD";
      specific_enum#(cmd_e) temp = new();

      $display("The int value for string %s is %d", cmd_name, temp.string_to_enum(cmd_name));
   end
endmodule
   

The disadvantage is that you have to define a class instance for the enum wrapper. The advantage is you could have a full library of such enum utilities written only once...

Avidan</description>
		<content:encoded><![CDATA[<p>Hi JL,<br />
Your entry touched a raw SV nerve :-). I guess your intention was to demonstrate the use of hashes and to provide everyone with a useful function, but on the way you also demonstrated one of SV&#8217;s enums main shortcomings - although all enums have a similar behavior and share a basic set of functions, there is no such thing as &#8220;enum base class&#8221;, that is common to all of them. So, in the case above, although your function might be useful for every enum, you would have to rewrite it for each different enum type&#8230;<br />
I have come across this problem in the past as well&#8230;One way of working around it, which is not perfect, is to define a class that wrapps around enums. Here&#8217;s your example rewritten (without the hash, just using brute force method):</p>
<p>virtual class enum_wrapper;<br />
   // functions defined by the standard go here.<br />
   // these are implemented by the specification<br />
   virtual function int first();endfunction<br />
   virtual function int last();endfunction<br />
   virtual function int next(int current);endfunction<br />
   virtual function int prev(int current);endfunction<br />
   virtual function int num();endfunction<br />
   virtual function string name(int value);endfunction</p>
<p>   // more user defined functions go here<br />
   // here&#8217;s a brute force method for searching&#8230;<br />
   // could be made more efficient by adding the hash&#8230;</p>
<p>   function int string_to_enum(string enum_name);<br />
      for (int i=first(); i!=last(); i=next(i))<br />
	if (enum_name == name(i)) return i;<br />
   endfunction // int</p>
<p>endclass // enum_wrapper</p>
<p>// just a garbage enum for default<br />
typedef enum {false, true} bool;</p>
<p>class specific_enum#(type enum_type = bool) extends enum_wrapper;<br />
   enum_type m_enum;</p>
<p>   function int first();<br />
      first =  m_enum.first();<br />
   endfunction; // int</p>
<p>   function int last();<br />
      last =  m_enum.last();<br />
   endfunction; // int</p>
<p>   function int next(int current);<br />
      m_enum = enum_type&#8217;(current);<br />
      next =  m_enum.next();<br />
   endfunction; // int</p>
<p>   function int prev(int current);<br />
      m_enum = enum_type&#8217;(current);<br />
      prev =  m_enum.prev();<br />
   endfunction; // int</p>
<p>   function int num();<br />
      num =  m_enum.num();<br />
   endfunction; // int</p>
<p>   function string name(int value);<br />
      m_enum = enum_type&#8217;(value);<br />
      name =  m_enum.name();<br />
   endfunction; // int<br />
endclass // specific_enum</p>
<p>typedef enum {UNKNOWN, ADD, SUB, MULT} cmd_e;</p>
<p>module test;<br />
   initial begin<br />
      string cmd_name = &#8220;ADD&#8221;;<br />
      specific_enum#(cmd_e) temp = new();</p>
<p>      $display(&#8221;The int value for string %s is %d&#8221;, cmd_name, temp.string_to_enum(cmd_name));<br />
   end<br />
endmodule</p>
<p>The disadvantage is that you have to define a class instance for the enum wrapper. The advantage is you could have a full library of such enum utilities written only once&#8230;</p>
<p>Avidan</p>
]]></content:encoded>
	</item>
</channel>
</rss>

