Creating protocol transfers

In reply to yaohe:

Hi,
Thanks for sharing the code. I understand the code and how parity is calculated based on the randomized payload.
But my question is different. Let me give you a snippet of my code that i have written so far for the AHB master:

class driver extends uvm_driver…

task run_phase();

while(1) begin
seq_item_port.get_next_item(req);
req.print();
if($cast(pkt,req)) begin
for(i=0;i<pkt.beats;) begin
if(ahb_if.cb_master.HREADY) begin
ahb_if.HWRITE = pkt.HWRITE;
ahb_if.HBURST = pkt.HBURST;
ahb_if.HSIZE = pkt.HSIZE;
ahb_if.HPROT = pkt.HPROT;
ahb_if.HTRANS = pkt.HTRANS[i];
ahb_if.HWDATA = $random;
i=i+1;
end
@(posedge ahb_if.HCLKM);
end
end
else begin
`uvm_fatal(“CASTERR”, $sformatf(“AHB packet couldn’t be cast”));
end
seq_item_port.item_done();
end

endtask
endclass

class ahb_sequence extends uvm_sequence…
rand logic [31:0] HADDR;
logic [ 1:0] HTRANS ;
rand logic HWRITE;
rand logic [ 2:0] HSIZE;
rand logic [ 2:0] HBURST;
rand logic [ 2:0] HPROT;
//logic [31:0] HWDATA [$];
int beats, addr_incr;
logic [31:0] beat_addr;

function void post_randomize();
beats = 0;
case(HBURST)
3’b000 : beats = 1;
3’b011 : beats = 4;
3’b101 : beats = 8;
3’b111 : beats = 16;
endcase
case(HSIZE)
3’b000 : addr_incr = 1;
3’b001 : addr_incr = 2;
3’b010 : addr_incr = 4;
3’b011 : addr_incr = 8;
endcase
beat_addr = new[beats];
HTRANS = new[beats];
HTRANS[0] = 2’b10;
beat_addr[0] = HADDR;
foreach(beat_addr[i]) begin
if(i>0) begin
HTRANS[i] = 2’b11;
beat_addr[i] = beat_addr[i-1] + addr_incr;
end
end
endfunction
endclass

In the above example, i cannot directly randomize my HTRANS because set of values and the number of values is completely dependent on HBURST. Example, if HBURST = INCR4, HTRANS should have values (NSEQ, SEQ,BUSY) and the number of such values should be 4. For HBURST = INCR8, HTRANS set should have 8 values. If HBURST=SINGLE, HTRANS can be {IDLE, NSEQ} and number of values in the set is 1. What i am trying to say is the number of values to be gathered for a particular variable is not fixed, hence i cannot randomize. Similar is the case for HWDATA (hence i am handling that in the driver). I can still handle HADDR by randomizing the starting address and using addr_incr to increment the address based on the HSIZE.

My question is what is the best way to do that. Also, how to handle weights while randomizing? Like I want HBURST tobe 40% of type INCR4 and remaining 60% of type INCR8.

Thanks.