Hi,
I want to randomize enumerated datatype. How can I do this?
I tried doing with the below code, didn’t work…
typedef enum{a,b,c}d;
rand d d1;
The following works for me
package mem_pkg;
typedef enum {IDLE, RST, RD, WR} mem_op;
endpackage
import mem_pkg::*;
class mem_xactn extends uvm_sequence_item;
rand bit[7:0] data;
rand mem_op kind;
// ...
endclass : mem_xactn
The package also allows you to reuse the type in other classes
Ben Cohen http://SystemVerilog.us
Perhaps you need to explain what “didn’t work” means?
Got an error? What was the error message?
Didn’t get the result you were expecting? What did you expect? and what did you actually get?
In reply to dave_59:
below code is an excerpt from my sequencer.
package RFile_parameters;
typedef enum { READ, WRITE, RD_WR } RFile_mode_e ;
endpackage
import RFile_parameters::*;
class RFile_transaction extends uvm_sequence_item;
rand RFile_mode_e mode;
…
endclass:RFile_transaction
class RFile_sequence extends uvm_sequence#(RFile_transaction);
`uvm_object_utils(RFile_sequence)
function new(string name = “”);
super.new(name);
endfunction
task body();
…
RFile_tr.randomize()
RFile_transaction RFile_tr;
RFile_tr = RFile_transaction::type_id::create( .name(“RFile_tr”), .contxt(get_full_name()) );
start_item(RFile_tr);
assert ( RFile_tr.randomize() );
…
endtask
endclass
while randomizing the transaction, mode is always holding the first element of enum data type RFile_mode_e. In this case “READ”. When I changed the order in RFile_mode_e, mode was holding again the first element which was “WRITE”. Not sure where I went wrong.
In reply to arunj:
arunj, I can’t see what would produce those results with the code you have shown here. The only thing wrong I see is that RFile_tr is declared after you randomize it, which is not legal syntax. But that my be just a typo in your post.