uart验证(3)
3.uart_uvc
uart_if.sv
interface uart_if(input clock, reset);
logic txd; // Transmit Data
logic rxd; // Receive Data
logic intrpt; // Interrupt
logic tx_intrpt;
logic rx_intrpt;
logic ms_intrpt; //modem status
logic rt_intrpt; //receive timeout
logic err_intrpt; //error
logic ri_n; // ring indicator
logic cts_n; // clear to send
logic dsr_n; // data set ready
logic rts_n; // request to send
logic dtr_n; // data terminal ready
logic dcd_n; // data carrier detect
logic out1_n;
logic out2_n;
logic baud_clk;//
endinterface : uart_if
uart_sequencer.sv
`ifndef UART_SEQUENCER_SVH
`define UART_SEQUENCER_SVH
class uart_sequencer extends uvm_sequencer #(uart_frame);
virtual interface uart_if vif;
uart_config cfg;
`uvm_component_utils_begin(uart_sequencer)
`uvm_field_object(cfg, UVM_DEFAULT | UVM_REFERENCE)
`uvm_component_utils_end
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// UVM build_phase
function void build_phase(uvm_phase phase);
super.build_phase(phase);
if(cfg == null)
if (!uvm_config_db#(uart_config)::get(this, "", "cfg", cfg))
`uvm_warning("NOCONFIG", "uart_config not set for this component")
endfunction : build_phase
task run_phase(uvm_phase phase);
phase.raise_objection(this);
`uvm_info(get_type_name(),"start uart_sequencer run_phase",UVM_LOW)
phase.drop_objection(this);
endtask
endclass : uart_sequencer
`endif // UART_SEQUENCER_SVH
uart_driver.sv
`ifndef UART_DRIVER_SV
`define UART_DRIVER_SV
class uart_driver extends uvm_driver #(uart_frame);
// The virtual interface used to drive and view HDL signals.
virtual interface uart_if vif;
// handle to a cfg class
uart_config cfg;
bit sample_clk;
bit [15:0] count;
int num_of_bits_sent;
int num_frames_sent;
// Provide implementations of virtual methods such as get_type_name and create
`uvm_component_utils_begin(uart_driver)
`uvm_field_object(cfg, UVM_DEFAULT | UVM_REFERENCE)
`uvm_field_int(sample_clk, UVM_DEFAULT + UVM_NOPRINT)
`uvm_field_int(count, UVM_DEFAULT + UVM_NOPRINT)
`uvm_component_utils_end
// Constructor - required UVM syntax
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
// Additional class methods
extern virtual function void build_phase(uvm_phase phase);
extern virtual function void connect_phase(uvm_phase phase);
extern virtual task run_phase(uvm_phase phase);
extern virtual task reset();
extern virtual task get_and_drive();
extern virtual task gen_sample_rate(ref bit [15:0] count, ref bit sample_clk);
extern virtual task send_tx_frame(input uart_frame frame);
extern virtual function void report_phase(uvm_phase phase);
endclass : uart_driver
// UVM build_phase
function void uart_driver::build_phase(uvm_phase phase);
super.build_phase(phase);
if(cfg == null)
if (!uvm_config_db#(uart_config)::get(this, "", "cfg", cfg))
`uvm_error("NOCONFIG", "uart_config not set for this component")
`uvm_info(get_type_name(),
$sprintf("driver get config data is %0d,%0d,%0d,%0d", cfg.ratio, cfg.char_length, cfg.nbstop, cfg.char_len_val),
UVM_LOW)
endfunction : build_phase
//UVM connect_phase
function void uart_driver::connect_phase(uvm_phase phase);
super.connect_phase(phase);
if (!uvm_config_db#(virtual uart_if)::get(this, "", "vif", vif))
`uvm_error("NOVIF",{"virtual interface must be set for: ",get_full_name(),".vif"})
endfunction : connect_phase
//UVM run_phase
task uart_driver::run_phase(uvm_phase phase);
fork
get_and_drive();
gen_sample_rate(count, sample_clk);
join
endtask : run_phase
// reset
task uart_driver::reset();
@(negedge vif.reset);
`uvm_info(get_type_name(), "Reset Asserted", UVM_MEDIUM)
vif.txd = 1; //Transmit Data
vif.rts_n = 0; //Request to Send
vif.dtr_n = 0; //Data Terminal Ready
vif.dcd_n = 0; //Data Carrier Detect
endtask : reset
// get_and_drive
task uart_driver::get_and_drive();
while (1) begin
reset();
fork
@(negedge vif.reset)
`uvm_info(get_type_name(), "Reset asserted", UVM_LOW)
begin
`uvm_info(get_full_name(),$sformatf("fcc_cfg_is %s\n",cfg.sprint()),UVM_LOW)
forever begin
@(posedge vif.clock iff (vif.reset))
seq_item_port.get_next_item(req);
`uvm_info(get_type_name(), "get item", UVM_LOW)
send_tx_frame(req);
seq_item_port.item_done();
end
end
join_any
disable fork;
//If we are in the middle of a transfer, need to end the tx. Also,
//do any reset cleanup here. The only way we got to this point is via
//a reset.
// if(req.is_active()) this.end_tr(req);
end
endtask : get_and_drive
task uart_driver::gen_sample_rate(ref bit [15:0] count, ref bit sample_clk);
forever begin
@(posedge vif.clock);
if (!vif.reset) begin
count = 0;
vif.baud_clk = 1'b0;
end else begin
if (count == cfg.ratio) begin
count = 0;
sample_clk = 1;
vif.baud_clk = 1;
end else begin
sample_clk = 0;
vif.baud_clk = 0;
count++;
end
end
end
endtask : gen_sample_rate
// ----------------------
// send_tx_frame
// ----------------------
task uart_driver::send_tx_frame(input uart_frame frame);
bit [7:0] payload_byte;
num_of_bits_sent = 0;
`uvm_info(get_type_name(),
$sprintf("Driver Sending TX Frame...\n%s", frame.sprint()),
UVM_LOW)
// void'(this.begin_tr(frame));
//wait((!cfg.rts_en)||(!vif.cts_n));////
//`uvm_info(get_type_name(), "Driver - Modem RTS or CTS asserted", UVM_LOW)
while(num_of_bits_sent <= (1 + cfg.char_len_val + cfg.parity_en + cfg.nbstop)) begin
@(posedge vif.clock);
if(sample_clk) begin
if(num_of_bits_sent == 0) begin
// Start sending tx_frame with "start bit"
vif.txd = frame.start_bit;
`uvm_info(get_type_name(),
$sprintf("Driver Sending Frame SOP: %b", frame.start_bit),
UVM_HIGH)
end
if((num_of_bits_sent > 0) && (num_of_bits_sent < (1 + cfg.char_len_val))) begin
// sending "data bits"
payload_byte = frame.payload[num_of_bits_sent-1];
vif.txd = frame.payload[num_of_bits_sent-1]; //from lowest to highest
`uvm_info(get_type_name(),
$sprintf("Driver Sending Frame data bit number:%0d value:'b%b",
(num_of_bits_sent-1), payload_byte), UVM_HIGH)
end
if((num_of_bits_sent == (1 + cfg.char_len_val)) && (cfg.parity_en)) begin
// sending "parity bit" if parity is enabled
vif.txd = frame.calc_parity(cfg.char_len_val, cfg.parity_mode);//**********************
`uvm_info(get_type_name(),
$sprintf("Driver Sending Frame Parity bit:'b%b",
frame.calc_parity(cfg.char_len_val, cfg.parity_mode)), UVM_HIGH)
end
if(num_of_bits_sent == (1 + cfg.char_len_val + cfg.parity_en)) begin
// sending "stop/error bits"
for(int i = 0; i < cfg.nbstop; i++) begin
`uvm_info(get_type_name(),
$sprintf("Driver Sending Frame Stop bit: 'b%b",
frame.stop_bits[i]), UVM_HIGH)
vif.txd = frame.stop_bits[i];
num_of_bits_sent++;
end
end
num_of_bits_sent++;
end //end of "if(sample_clk)"
end //end of while
num_frames_sent++;
`uvm_info(get_type_name(),
$sprintf("Frame **%0d** Sent...", num_frames_sent), UVM_LOW)
vif.txd = 1;
// frame.transmit_delay=5;
`uvm_info(get_type_name(),$sprintf("transmit_delay is %0d",frame.transmit_delay),UVM_HIGH)
repeat (frame.transmit_delay) begin
@(posedge sample_clk);
//@(posedge vif.clock);
end
`uvm_info(get_type_name(), "Frame complete...", UVM_LOW)
//this.end_tr(frame);
endtask : send_tx_frame
//UVM report_phase
function void uart_driver::report_phase(uvm_phase phase);
`uvm_info(get_type_name(),
$sprintf("UART Frames Sent:%0d", num_frames_sent),
UVM_LOW)
endfunction : report_phase
`endif // UART_TX_DRIVER
uart_monitor.sv
`ifndef UART_MONITOR_SVH
`define UART_MONITOR_SVH
class uart_monitor extends uvm_monitor;
// virtual interface
virtual interface uart_if vif;
// Handle to a cfg class
uart_config cfg;
int num_frames;
bit sample_clk;
bit [15:0] count;
// bit [7:0] ua_bdiv;
int num_of_bits_rcvd;
int transmit_delay;
bit sop_detected;
// bit tmp_bit0;
bit serial_d1;
bit serial_b;
bit [1:0] msb_lsb_data;
uart_frame cur_frame;
uvm_event mon_done;
uvm_event_pool event_pool;
`uvm_field_utils_begin(uart_monitor)
`uvm_field_object(cfg, UVM_DEFAULT | UVM_REFERENCE)
`uvm_field_object(cur_frame, UVM_DEFAULT | UVM_NOPRINT)
`uvm_field_utils_end
function new (string name = "", uvm_component parent);
super.new(name, parent);
endfunction: new
// Additional class methods;
extern virtual function void build_phase(uvm_phase phase);
extern virtual function void connect_phase(uvm_phase phase);
extern virtual task run_phase(uvm_phase phase);
extern virtual task gen_sample_rate(ref bit [15:0] count, ref bit sample_clk, ref bit sop_detected);
extern virtual task start_synchronizer(ref bit serial_d1, ref bit serial_b);
extern virtual task collect_frame();
extern virtual task wait_for_sop(ref bit sop_detected);
extern virtual task sample_and_store();
endclass: uart_monitor
// UVM build_phase
function void uart_monitor::build_phase(uvm_phase phase);
super.build_phase(phase);
if (cfg == null)
if (!uvm_config_db#(uart_config)::get(this, "", "cfg", cfg))
`uvm_error("NOCONFIG", "uart_config not set for this somponent")
endfunction : build_phase
function void uart_monitor::connect_phase(uvm_phase phase);
super.connect_phase(phase);
if (!uvm_config_db#(virtual uart_if)::get(this, "", "vif", vif))
`uvm_error("NOVIF",{"virtual interface must be set for: ",get_full_name(),".vif"})
endfunction : connect_phase
task uart_monitor::run_phase(uvm_phase phase);
event_pool = uvm_event_pool::get_global_pool();
mon_done = event_pool.get("mon_done");
`uvm_info(get_type_name(),"Start Running", UVM_LOW)
@(negedge vif.reset);
wait (vif.reset);
`uvm_info(get_type_name(), "Detected Reset Done", UVM_LOW)
num_frames = 0;
cur_frame = uart_frame::type_id::create("cur_frame", this);
fork
gen_sample_rate(count, sample_clk, sop_detected);
start_synchronizer(serial_d1, serial_b);
sample_and_store();
join
endtask : run_phase
task uart_monitor::gen_sample_rate(ref bit [15:0] count, ref bit sample_clk, ref bit sop_detected);
forever begin
@(posedge vif.clock);
if ((!vif.reset) || (sop_detected)) begin
`uvm_info(get_type_name(), "sample_clk- resetting count", UVM_HIGH)
count = 0;
sample_clk = 0;
sop_detected = 0;
end else begin
if (count == cfg.ratio) begin
count = 0;
sample_clk = 1;
end else begin
sample_clk = 0;
count++;
end
end
end
endtask
task uart_monitor::start_synchronizer(ref bit serial_d1, ref bit serial_b);
endtask
task uart_monitor::sample_and_store();
forever begin
wait_for_sop(sop_detected);
collect_frame();
end
endtask : sample_and_store
task uart_monitor::wait_for_sop(ref bit sop_detected);
transmit_delay = 0;
sop_detected = 0;
while (!sop_detected) begin
`uvm_info(get_type_name(), "trying to detect SOP", UVM_MEDIUM)
while (!(serial_d1 == 1 && serial_b == 0)) begin
//@(negedge sample_clk);
@(negedge vif.clock);
transmit_delay++;
end
if (serial_b != 0)
`uvm_info(get_type_name(), "Encountered a glitch in serial during SOP, shall continue", UVM_LOW)
else
begin
sop_detected = 1;
`uvm_info(get_type_name(), "SOP detected", UVM_MEDIUM)
end
end
endtask : wait_for_sop
task uart_monitor::collect_frame();
endtask : collect_frame
`endif // UART_MONITOR_SVH
uart_tx_monitor.sv
`ifndef UART_TX_MONITOR_SVH
`define UART_TX_MONITOR_SVH
class uart_tx_monitor extends uart_monitor;
uvm_analysis_port #(uart_frame) frame_collected_port;
// Handle to a cfg class
uart_config cfg;
`uvm_component_utils_begin(uart_tx_monitor)
`uvm_field_object(cfg, UVM_DEFAULT | UVM_REFERENCE)
`uvm_component_utils_end
function new (string name = "uart_tx_monitor", uvm_component parent);
super.new(name, parent);
frame_collected_port=new("frame_collected_port",this);
endfunction: new
// Additional class methods;
extern virtual task start_synchronizer(ref bit serial_d1, ref bit serial_b);
extern virtual function void report_phase(uvm_phase phase);
extern virtual task collect_frame();
endclass : uart_tx_monitor
task uart_tx_monitor::start_synchronizer(ref bit serial_d1, ref bit serial_b);
forever begin
@(posedge vif.clock);
if (!vif.reset) begin
serial_d1 = 1;
serial_b = 1;
end else begin
serial_d1 = serial_b;
serial_b = vif.txd;
// `uvm_info("start_synchronizer",$sprintf("serial_d1 is %0d",serial_d1),UVM_LOW)
// `uvm_info("start_synchronizer",$sprintf("serial_b is %0d",serial_b),UVM_LOW)
// `uvm_info("start_synchronizer",$sprintf("vif.txd is %0d",vif.txd),UVM_LOW)
end
end
endtask
function void uart_tx_monitor::report_phase(uvm_phase phase);
`uvm_info(get_type_name(), $sprintf("UART Frames Collected:%0d", num_frames), UVM_LOW)
endfunction : report_phase
task uart_tx_monitor::collect_frame();
bit [7:0] payload_byte;
cur_frame = new;
`uvm_info(get_type_name(), $sprintf("Collecting a frame: %0d", num_frames+1), UVM_HIGH)
// Begin Transaction Recording
void'(this.begin_tr(cur_frame, "UART Monitor", , get_full_name()));
cur_frame.transmit_delay = transmit_delay;
cur_frame.start_bit = 1'b0;
cur_frame.parity_type = HAVE_PARITY;
num_of_bits_rcvd = 0;
while (num_of_bits_rcvd < (1 + cfg.char_len_val + cfg.parity_en + cfg.nbstop)) begin
@(posedge vif.clock);
if (sample_clk) begin
num_of_bits_rcvd++;
if ((num_of_bits_rcvd > 0) && (num_of_bits_rcvd <= cfg.char_len_val)) begin // sending "data bits"
cur_frame.payload[num_of_bits_rcvd-1] = serial_b;
// payload_byte = cur_frame.payload[num_of_bits_rcvd-1];
// `uvm_info(get_type_name(), $sprintf("Received a Frame data bit:'b%0b", payload_byte), UVM_HIGH)
end
//msb_lsb_data[0] = cur_frame.payload[0] ;
// msb_lsb_data[1] = cur_frame.payload[cfg.char_len_val-1] ;
if ((num_of_bits_rcvd == (1 + cfg.char_len_val)) && (cfg.parity_en)) begin // sending "parity bit" if parity is enabled
cur_frame.parity = serial_b;
`uvm_info(get_type_name(), $sprintf("Received Parity bit:'b%0b", cur_frame.parity), UVM_HIGH)
if (serial_b == cur_frame.calc_parity(cfg.char_len_val, cfg.parity_mode))
`uvm_info(get_type_name(), "Received Parity is same as calculated Parity", UVM_MEDIUM)
end
if (num_of_bits_rcvd == (1 + cfg.char_len_val + cfg.parity_en)) begin // sending "stop/error bits"
for (int i = 0; i < cfg.nbstop; i++) begin
// wait (sample_clk);
cur_frame.stop_bits[i] = serial_b;
`uvm_info(get_type_name(), $sprintf("Received a Stop bit: 'b%0b", cur_frame.stop_bits[i]), UVM_HIGH)
num_of_bits_rcvd++;
// wait (!sample_clk);
end
end
// wait (!sample_clk);
end
end
`uvm_info(get_type_name(), $sprintf("current Frame :\npayload = %0b\n,parity = %0b\n,stop_b = %0b\n",cur_frame.payload,cur_frame.parity,cur_frame.stop_bits), UVM_MEDIUM)
num_frames++;
`uvm_info(get_type_name(), $sprintf("Collected the following Frame No:%0d\n%s", num_frames, cur_frame.sprint()), UVM_MEDIUM)
frame_collected_port.write(cur_frame);
this.end_tr(cur_frame);
mon_done.trigger();
endtask : collect_frame
`endif
uart_rx_monitor.sv
`ifndef UART_RX_MONITOR_SVH
`define UART_RX_MONITOR_SVH
class uart_rx_monitor extends uart_monitor;
uvm_analysis_port #(uart_frame) frame_collected_port;
// Handle to a cfg class
uart_config cfg;
`uvm_component_utils_begin(uart_rx_monitor)
`uvm_field_object(cfg, UVM_DEFAULT | UVM_REFERENCE)
`uvm_component_utils_end
function new (string name = "uart_rx_monitor", uvm_component parent);
super.new(name, parent);
frame_collected_port=new("frame_collected_port",this);
endfunction: new
// Additional class methods;
extern virtual task start_synchronizer(ref bit serial_d1, ref bit serial_b);
extern virtual function void report_phase(uvm_phase phase);
extern virtual task collect_frame();
endclass: uart_rx_monitor
task uart_rx_monitor::start_synchronizer(ref bit serial_d1, ref bit serial_b);
forever begin
@(posedge vif.clock);
if (!vif.reset) begin
serial_d1 = 1;
serial_b = 1;
end else begin
serial_d1 = serial_b;
serial_b = vif.rxd;
end
end
endtask
function void uart_rx_monitor::report_phase(uvm_phase phase);
`uvm_info(get_type_name(), $sprintf("UART Frames Collected:%0d", num_frames), UVM_LOW)
endfunction : report_phase
task uart_rx_monitor::collect_frame();
bit [7:0] payload_byte;
cur_frame = new;
`uvm_info(get_type_name(), $sprintf("Collecting a frame: %0d", num_frames+1), UVM_HIGH)
// Begin Transaction Recording
void'(this.begin_tr(cur_frame, "UART Monitor", , get_full_name()));
cur_frame.transmit_delay = transmit_delay;
cur_frame.start_bit = 1'b0;
cur_frame.parity_type = HAVE_PARITY;
num_of_bits_rcvd = 0;
while (num_of_bits_rcvd < (1 + cfg.char_len_val + cfg.parity_en + cfg.nbstop))
begin
@(posedge vif.clock);
if (sample_clk) begin
num_of_bits_rcvd++;
if ((num_of_bits_rcvd > 0) && (num_of_bits_rcvd <= cfg.char_len_val)) begin // sending "data bits"
cur_frame.payload[num_of_bits_rcvd-1] = serial_b;
payload_byte = cur_frame.payload[num_of_bits_rcvd-1];
`uvm_info(get_type_name(), $sprintf("Received a Frame data bit:'b%0b", payload_byte), UVM_HIGH)
end
msb_lsb_data[0] = cur_frame.payload[0] ;
msb_lsb_data[1] = cur_frame.payload[cfg.char_len_val-1] ;
if ((num_of_bits_rcvd == (1 + cfg.char_len_val)) && (cfg.parity_en)) begin // sending "parity bit" if parity is enabled
cur_frame.parity = serial_b;
`uvm_info(get_type_name(), $sprintf("Received Parity bit:'b%0b", cur_frame.parity), UVM_HIGH)
if (serial_b == cur_frame.calc_parity(cfg.char_len_val, cfg.parity_mode))
`uvm_info(get_type_name(), "Received Parity is same as calculated Parity", UVM_MEDIUM)
end
if (num_of_bits_rcvd == (1 + cfg.char_len_val + cfg.parity_en)) begin // sending "stop/error bits"
for (int i = 0; i < cfg.nbstop; i++) begin
//wait (sample_clk);
cur_frame.stop_bits[i] = serial_b;
`uvm_info(get_type_name(), $sprintf("Received a Stop bit: 'b%0b", cur_frame.stop_bits[i]), UVM_HIGH)
num_of_bits_rcvd++;
//wait (!sample_clk);
end
end
//wait (!sample_clk);
end
end
`uvm_info(get_type_name(), $sprintf("current Frame :\npayload = %0b\n,parity = %0b\n,stop_b = %0b\n",cur_frame.payload,cur_frame.parity,cur_frame.stop_bits), UVM_MEDIUM)
num_frames++;
`uvm_info(get_type_name(), $sprintf("Collected the following Frame No:%0d\n%s", num_frames, cur_frame.sprint()), UVM_MEDIUM)
frame_collected_port.write(cur_frame);
this.end_tr(cur_frame);
mon_done.trigger();
endtask : collect_frame
`endif
uart_agent.sv
`ifndef UART_TX_AGENT_SVH
`define UART_TX_AGENT_SVH
class uart_agent extends uvm_agent;
uart_config cfg;
uart_tx_monitor tx_monitor;
uart_rx_monitor rx_monitor;
uart_driver driver;
uart_sequencer sequencer;
//uvm_analysis_port #(uart_frame) frame_collected_port;
// Provides implementation of methods such as get_type_name and create
`uvm_component_utils_begin(uart_agent)
`uvm_field_object(cfg, UVM_DEFAULT | UVM_REFERENCE)
`uvm_component_utils_end
// Constructor - UVM required syntax
function new(string name = "", uvm_component parent);
super.new(name, parent);
endfunction
// Additional class methods
extern virtual function void build_phase(uvm_phase phase);
extern virtual function void connect_phase(uvm_phase phase);
extern virtual function void update_config(uart_config cfg);
endclass : uart_agent
// UVM build_phase
function void uart_agent::build_phase(uvm_phase phase);
super.build_phase(phase);
// Configure
if (cfg == null) begin
if (!uvm_config_db#(uart_config)::get(this, "", "cfg", cfg))
`uvm_warning("NOCONFIG", "Config not set for Tx agent")
end
if(is_active == UVM_ACTIVE) begin
tx_monitor = uart_tx_monitor::type_id::create("tx_monitor", this);
sequencer = uart_sequencer::type_id::create("sequencer", this);
driver = uart_driver::type_id::create("driver", this);
end
if(is_active == UVM_PASSIVE) begin
rx_monitor = uart_rx_monitor::type_id::create("rx_monitor", this);
end
endfunction : build_phase
// UVM connect() phase
function void uart_agent::connect_phase(uvm_phase phase);
super.connect_phase(phase);
if(is_active == UVM_ACTIVE) begin
driver.seq_item_port.connect(sequencer.seq_item_export);
end
else begin
//frame_collected_port=rx_monitor.frame_collected_port;////////////////
end
endfunction : connect_phase
// Assign the config to the agent's children
function void uart_agent::update_config( uart_config cfg);
tx_monitor.cfg = cfg;
rx_monitor.cfg = cfg;
driver.cfg = cfg;
sequencer.cfg = cfg;
endfunction : update_config
`endif
uart_env.sv
`ifndef UART_ENV_SVH
`define UART_ENV_SVH
class uart_env extends uvm_env;
// Virtual Interface variable
virtual interface uart_if vif;
// Environment Configuration Paramters
uart_config cfg;
// Components of the environment
uart_agent tx_agt;
uart_agent rx_agt;
// This macro provide implementation of get_type_name() and create()
`uvm_component_utils_begin(uart_env)
`uvm_field_object(cfg, UVM_DEFAULT)
`uvm_component_utils_end
// Constructor - required UVM syntax
function new( string name, uvm_component parent);
super.new(name, parent);
endfunction
// Additional class methods
extern virtual function void build_phase(uvm_phase phase);
extern virtual function void connect_phase(uvm_phase phase);
extern virtual function void update_config(uart_config cfg);
endclass : uart_env
//UVM build_phase
function void uart_env::build_phase(uvm_phase phase);
super.build_phase(phase);
// Configure
// if ( cfg == null)
if (!uvm_config_db#(uart_config)::get(this, "", "cfg", cfg)) begin //get from base_test
`uvm_info("NOCONFIG", "No uart_config, creating...", UVM_MEDIUM)
cfg = uart_config::type_id::create("cfg", this);
if (!cfg.randomize())
`uvm_error("RNDFAIL", "Could not randomize uart_config using default values")
`uvm_info(get_type_name(), {"Printing cfg:\n", cfg.sprint()}, UVM_MEDIUM)
end
//Tx agent create
tx_agt = uart_agent::type_id::create("tx_agt",this);
uvm_config_object::set(this, "tx_agt*", "cfg", cfg); // send to TX agent
tx_agt.is_active = UVM_ACTIVE;
//Rx agent create
rx_agt = uart_agent::type_id::create("rx_agt",this);
uvm_config_object::set(this, "rx_agt*", "cfg", cfg); // send to RX agent
rx_agt.is_active = UVM_PASSIVE;
endfunction : build_phase
//UVM connect_phase
function void uart_env::connect_phase(uvm_phase phase);
super.connect_phase(phase);
// Get the agent's virtual interface if set via config
if(!uvm_config_db#(virtual uart_if)::get(this, "", "vif", vif))
`uvm_error("NOVIF",{"virtual interface must be set for: ",get_full_name(),".vif"})
endfunction : connect_phase
// Update Agent config when config updates
function void uart_env::update_config(input uart_config cfg );
tx_agt.update_config(cfg);
rx_agt.update_config(cfg);
endfunction : update_config
`endif
uart_frame.sv
`ifndef UART_FRAME_SVH
`define UART_FRAME_SVH
// Parity Type Control knob
typedef enum bit {HAVE_PARITY, NO_PARITY} parity_e;
class uart_frame extends uvm_sequence_item; //lab1_note1
// UART Frame
rand bit start_bit;
rand bit [7:0] payload;
bit parity;
rand bit [1:0] stop_bits;
// Control Knobs
rand parity_e parity_type;
rand int transmit_delay; //int =32bit
// Default constraints //lab1_note2
constraint default_txmit_delay {transmit_delay >= 0; transmit_delay < 20;}
constraint default_start_bit { start_bit == 1'b0;}
constraint default_stop_bits { stop_bits == 2'b11;}
constraint default_parity_type { parity_type==HAVE_PARITY;}
// These declarations implement the create() and get_type_name()
// and enable automation of the uart_frame's fields //lab1_note3
`uvm_object_utils_begin(uart_frame)
`uvm_field_int(start_bit, UVM_DEFAULT)
`uvm_field_int(payload, UVM_DEFAULT)
`uvm_field_int(parity, UVM_DEFAULT)
`uvm_field_int(stop_bits, UVM_DEFAULT)
`uvm_field_enum(parity_e,parity_type, UVM_DEFAULT + UVM_NOCOMPARE)
`uvm_field_int(transmit_delay, UVM_DEFAULT + UVM_DEC + UVM_NOCOMPARE + UVM_NOCOPY)
`uvm_object_utils_end
// Constructor - required UVM syntax //lab1_note4
function new(string name = "uart_frame");
super.new(name);
endfunction
// This method calculates the parity
function bit calc_parity(int unsigned num_of_data_bits=8,
bit[1:0] ParityMode=0);
bit temp_parity;
if (num_of_data_bits == 6)
temp_parity = ^payload[5:0];
else if (num_of_data_bits == 7)
temp_parity = ^payload[6:0];
else
temp_parity = ^payload;
case(ParityMode)
2'b00: temp_parity = ~temp_parity; //odd
2'b01: temp_parity = temp_parity; //even
2'b10: temp_parity = 1'b1; //keep 1
2'b11: temp_parity = 1'b0; //keep 0
endcase
if (parity_type == NO_PARITY)
calc_parity = 1'bz;
else
calc_parity = temp_parity;
endfunction
// Parity is calculated in the post_randomize() method //lab1_note5
function void post_randomize();
parity = calc_parity();
endfunction : post_randomize
endclass
`endif
uart_config.sv
`ifndef UART_CONFIG_SV
`define UART_CONFIG_SV
class uart_config extends uvm_object;
// UART device parameters
rand bit [15:0] ratio; // The ratio of Frequency to Baud Rate
// Line Control Register Fields
rand bit [1:0] char_length; // Character length
rand bit nbstop; // Number stop bits
rand bit parity_en ; // Parity Enable
rand bit [1:0] parity_mode; // Parity Mode
int unsigned chrl;
int unsigned stop_bt;
// Control Register Fields
rand bit cts_en ; //
rand bit rts_en ; //
// Calculated in post_randomize() so not randomized
byte unsigned char_len_val; // (8, 7 or 6)
byte unsigned stop_bit_val; // (1, 1.5 or 2)
// These default constraints can be overriden
// Constrain configuration based on UVC/RTL capabilities
constraint c_rts_en { rts_en == 0;}
constraint c_cts_en { cts_en == 0;}
// These declarations implement the create() and get_type_name()
// as well as enable automation of the tx_frame's fields
`uvm_object_utils_begin(uart_config)
`uvm_field_int(ratio, UVM_DEFAULT + UVM_DEC)
`uvm_field_int(char_length, UVM_DEFAULT)
`uvm_field_int(nbstop, UVM_DEFAULT )
`uvm_field_int(parity_en, UVM_DEFAULT)
`uvm_field_int(parity_mode, UVM_DEFAULT)
`uvm_object_utils_end
// This requires for registration of the ptp_tx_frame
function new(string name = "uart_config");
super.new(name);
endfunction
function void post_randomize();
ConvToIntChrl();
ConvToIntStpBt();
endfunction
// Function to convert the 2 bit Character Length to integer
function void ConvToIntChrl();
case(char_length)
2'b00 : char_len_val = 5;
2'b01 : char_len_val = 6;
2'b10 : char_len_val = 7;
2'b11 : char_len_val = 8;
default : char_len_val = 8;
endcase
endfunction : ConvToIntChrl
// Function to convert the 2 bit stop bit to integer
function void ConvToIntStpBt();
case(nbstop)
2'b00 : stop_bit_val = 1;
2'b01 : stop_bit_val = 2;
default : stop_bit_val = 2;
endcase
endfunction : ConvToIntStpBt
endclass
`endif // UART_CONFIG_SV
uart_pkg.sv
`ifndef UART_PKG_SV
`define UART_PKG_SV
`include "uart_if.sv"
package uart_pkg;
// Import the UVM library and include the UVM macros
import uvm_pkg::*;
`include "uvm_macros.svh"
`include "uart_config.sv"
`include "uart_frame.sv"
`include "uart_monitor.sv"
`include "uart_tx_monitor.sv"
`include "uart_rx_monitor.sv"
`include "uart_sequencer.sv"
`include "uart_driver.sv"
`include "uart_agent.sv"
`include "uart_env.sv"
`include "uart_seq_lib.sv"
endpackage : uart_pkg
`endif // UART_PKG_SV
uart_seq_lib.sv
`ifndef UART_SEQ_LIB_SV
`define UART_SEQ_LIB_SV
//----------------------------------------------------------------------
// SEQUENCE: uart_base_seq
//----------------------------------------------------------------------
class uart_base_seq extends uvm_sequence #(uart_frame);
function new(string name="uart_base_seq");
super.new(name);
endfunction
`uvm_object_utils(uart_base_seq)
// Use a base sequence to raise/drop objections if this is a default sequence
virtual task pre_body();
if (starting_phase != null)
starting_phase.raise_objection(this, {"Running sequence '",
get_full_name(), "'"});
endtask
virtual task post_body();
if (starting_phase != null)
starting_phase.drop_objection(this, {"Completed sequence '",
get_full_name(), "'"});
endtask
endclass : uart_base_seq
//----------------------------------------------------------------------
// SEQUENCE: uart_gen_right_seq
//----------------------------------------------------------------------
class uart_gen_right_seq extends uart_base_seq;
// rand int unsigned cnt;
// rand bit [7:0] start_payload;
function new(string name="uart_gen_right_seq");
super.new(name);
endfunction
// register sequence with a sequencer
`uvm_object_utils(uart_gen_right_seq)
// constraint count_ct { cnt > 0; cnt <= 10;}
virtual task body();
`uvm_info(get_type_name(), "UART sequencer executing sequence...", UVM_LOW)
// cnt=10;
// for (int i = 0; i < cnt; i++) begin
// `uvm_do_with(req, {req.payload == (start_payload +i*3)%256; })
`uvm_do_with(req, {req.payload == 'd1; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd2; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd3; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd4; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd5; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd6; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd7; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd8; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd9; req.transmit_delay==5;})
#1ms;
`uvm_do_with(req, {req.payload == 'd10; req.transmit_delay==5;})
// end
`uvm_info(get_type_name(), "uart_gen_right_seq done", UVM_LOW)
endtask
endclass : uart_gen_right_seq
`endif // UART_SEQ_LIB_SV
二.scripts
filelist_tb
${WORKAREA}/lab6/env/apb_uvc/apb_pkg.sv
${WORKAREA}/lab6/env/uart_uvc/uart_pkg.sv
${WORKAREA}/lab6/env/uart_ctrl/sv/uart_ctrl_pkg.sv
${WORKAREA}/lab6/env/uart_ctrl/tb/top.sv
浙公网安备 33010602011771号