package test_pkg;
typedef enum bit {rd, wr} typ_dir;
typedef logic [15:0] typ_data;
typedef logic [15:0] typ_addr;
typedef class stream;
class packet;
static int next_ID;
const int ID;
rand typ_data data;
rand typ_addr addr;
rand typ_dir dir;
function new();
ID = next_ID++;
endfunction
function packet copy();
packet clone;
clone.dir = dir;
clone.addr = addr;
clone.data = data;
return clone;
endfunction
function bit compare(packet other);
if (other.dir !== dir) return 0;
if (other.addr !== addr) return 0;
if (other.data !== data) return 0;
return 1;
endfunction
function string psprint();
string kind;
kind = (dir==rd) ? "Read" : "Write";;
$sformat (psprint, "%s packet %0d: a=%h, D=%h...id=%h",kind,data,addr,dir,ID );
endfunction
endclass
class stream;
string name;
packet history [$];
function packet get_trans(bit print = 1'b0);
packet t;
t = new();
void'(t.randomize());
history.push_back(t);
if (print)
$display ("Stream %s generated %s\n", name, t.psprint());
return t;
endfunction : get_trans
function new(string name = "default");
this.name = name;
endfunction
endclass
endpackage
module top;
import test_pkg::*;
stream one = new ();
packet t, t1, t2;
initial begin
repeat (10)
begin
t = one.get_trans(1);
#1000;
$display ("%t.....",$time);
end
for (int i=0; i<one.history.size(); i++)
$display( "%d: %s", i, one.history[i].psprint() );
t = one.get_trans();
t1 = t.one.copy();
t2 = one.get_trans();
end
endmodule