SV随机约束使用方法---测试例子说明

// ********************************************************
// Copyright(c) 2018
// Author : gujiangtao
// File name : random_gen.v
// Module name : random_gen
// Created Time : 2018/8/14 20:25
// Last Modified : ---
// Abstract:

// ========================================================
// Revision Date Author Comment
// -------- --------- --------- ---------
// 1.0 2018/08/14 gujiangtao
//
// ********************************************************

`include "parameter.vh"

class Random_id;
rand bit[9:0] destxr;
rand bit[9:0] destyr;
rand bit[7:0] startr;
rand bit signed [7:0] noiser;
constraint start_N {startr<`per_cyc ;noiser<(`noise_per); noiser>-(`noise_per); }

function new(int seed);
this.srandom(seed);
endfunction
endclass//endclass*/


module random_gen
#(
parameter per_cyc = 10,
parameter x_cord_width_p = "inv",
parameter y_cord_width_p = "inv"
)(
input clk_i,
input reset_i,
input [x_cord_width_p-1:0] my_x_i,
input [y_cord_width_p-1:0] my_y_i,
input out_ready_lo,

output reg destv_en,
output [x_cord_width_p-1:0] dest_x_o,
output [y_cord_width_p-1:0] dest_y_o
);

// tile coordinates
wire [x_cord_width_p-1:0] my_x_i_cast;
wire [y_cord_width_p-1:0] my_y_i_cast;

logic [x_cord_width_p-1:0] dest_x_o_cast;
logic [y_cord_width_p-1:0] dest_y_o_cast;

assign my_x_i_cast = my_x_i;
assign my_y_i_cast = my_y_i;

assign dest_x_o = dest_x_o_cast;
assign dest_y_o = dest_y_o_cast;

logic cp;
int fid;
int random_array[2000-1:0];
int rd, value;
Random_id destxy_random = new(100);
int i, tn;

initial begin
i = 0;
tn = 0;
cp = 0;
rd = 0;
/* fid=$fopen("./random_data.txt","r");
while( rd<1000 ) begin
if(!$fscanf(fid,"%d\n",value))
$display("reading the random_data.txt file is error!");
random_array[rd]= value;
// $display("reading rd=%d ihe random_data%d\n", rd, random_array[rd]);
rd = rd +1;
end
$fclose(fid); */
$readmemh("random_data.txt", random_array);
@(negedge clk_i); // in order to prevent the my_x_i == unkown value x when the initial start
destxy_random.srandom(random_array[my_x_i_cast*my_y_i_cast]);
// $display(">>>random seed (y,x)(%d,%d) \n",my_y_i, my_x_i);
$display(">>>random seed (y,x)(%d,%d) from seed=%d \n",my_y_i, my_x_i, random_array[(my_x_i_cast+1)*(my_y_i_cast+1)]);
// destxy_random.randomize();
end

int launch_N;

logic reset_i_r, reset_i_rr;

always_ff @(posedge clk_i)
begin
reset_i_r <= reset_i;
reset_i_rr <= reset_i_r;
end

logic [9:0] destx, desty;
logic [7:0] start;
logic signed [7:0] noise;

always_ff @(posedge clk_i )
begin
// destxy_random.randomize(destxr);
// destxy_random.randomize(destyr);
// destxy_random.randomize(startr);
// destxy_random.randomize(noiser);
destx <= destxy_random.destxr;
desty <= destxy_random.destyr;
start <= destxy_random.startr;
noise <= destxy_random.noiser;
// $display("(x,y,s,n)(%d,%d,%d,%d) \n",destx, desty, start, noise);

end

always @(posedge clk_i)
begin
if(reset_i_rr) begin
launch_N <= noise + `per_cyc;
end
else if(i==launch_N-1 && ~cp && out_ready_lo)
begin
destxy_random.randomize(noiser);
launch_N <= noise + `per_cyc;
end
end

always @(posedge clk_i)
begin
if(reset_i_rr) begin
i <= start%launch_N;
tn <= 0;
end
else begin
if (i==launch_N-1) begin
i <= 0;
if( out_ready_lo ) tn <= tn + 1;
end
else begin
i <= i+1;
end
end
end

always @(posedge clk_i)
begin
if(reset_i_rr) begin
destv_en <= 0;
cp <= 0;
end
else if(i==launch_N-1 && tn>=10 && tn < `packet_num && out_ready_lo )
begin
destv_en <= 1'b1;
cp <= ~cp;
// $display("cp=%d \n",cp);
end
else
destv_en <= 1'b0;
end

always @(posedge clk_i)
begin
if(reset_i_rr) begin
dest_x_o_cast <= destx;
end
else if(i==launch_N-1 && ~cp && out_ready_lo)
begin
destxy_random.randomize(destxr);
dest_x_o_cast <= destx % `logic_add_x;
end
end

always @(posedge clk_i)
begin
if(reset_i_rr) begin
dest_y_o_cast <= desty;
end
else if(i==launch_N-1 && ~cp && out_ready_lo)
begin
destxy_random.randomize(destyr);
dest_y_o_cast <= desty % `logic_add_y;
end
end

endmodule

posted @ 2019-05-07 23:03  大海在倾听  阅读(2078)  评论(0编辑  收藏  举报