Jump to content

walk thru an enumeration


Recommended Posts

I needed to step thru an enum in a testbench today.  As it took me a while to figure out how to do it, I post a small example here.
I want to do it without making any assumptions of the values of the enums (values are the default type of int, in this case).
Reference: SystemVerilog doc "1800-2012.pdf" Section 6.19 Enumerations
 
module top;
   //typedef enum {alpha=0, beta=1, gamma=2, delta=3, epsilon=4} greek;  //to show default assignments
   typedef enum {alpha, beta, gamma, delta, epsilon} greek;
   greek letters2;

   initial begin
      $display("****** Walk thru an enumeration example. ***********");

      for (greek letters=letters.first(), int walk=0;
           walk < letters.num();
           letters=letters.next(), walk++) begin
           $display(" %0d *** %0s", letters, letters.name);
      end
   end
endmodule : top
Output:
****** Walk thru an enumeration example. ***********
 0 *** alpha
 1 *** beta
 2 *** gamma
 3 *** delta
 4 *** epsilon
I'm also posting here, because when I am trying to remember how to do something, I find it often easier to find my postings online than an example in my own code.
I thought I did this nicely with a foreach loop, but cannot find it, so may be imagining it.
 
I was not keen on having to use variable walk.  If someone can show me how to do this with a foreach loop or without using an extra variable, like I did with "walk", please do.
 
Noted failures:
      for (greek letters=letters.first(); letters!=letters.last();    letters=letters.next()) begin //shows only 0-3
      for (greek letters=letters.first(); letters<=(letters.num()-1); letters=letters.next()) begin //neverending loop

 

Link to comment
Share on other sites

You can use a do-while loop:

module top;
   //typedef enum {alpha=0, beta=1, gamma=2, delta=3, epsilon=4} greek;  //to show default assignments
   typedef enum {alpha, beta, gamma, delta, epsilon} greek;
   greek letters;
   
   initial begin
      $display("****** Walk thru an enumeration example. ***********");
      letters = letters.first;
      do begin
         $display(" %0d *** %0s", letters, letters.name);
     letters = letters.next;
      end
      while (letters != letters.first);
  end
endmodule : top
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...