1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
| import uvm_pkg::*; `include "uvm_macros.svh"
interface switch_if (input bit clk); logic rstn; logic vld; logic [7:0] addr; logic [15:0] data; logic [7:0] addr_a; logic [15:0] data_a; logic [7:0] addr_b; logic [15:0] data_b; endinterface
class switch_item extends uvm_sequence_item; bit [7:0] addr; bit [15:0] data; bit [7:0] addr_a; bit [15:0] data_a; bit [7:0] addr_b; bit [15:0] data_b;
`uvm_object_utils_begin(switch_item) `uvm_field_int(addr, UVM_DEFAULT) `uvm_field_int(data, UVM_DEFAULT) `uvm_field_int(addr_a, UVM_DEFAULT) `uvm_field_int(data_a, UVM_DEFAULT) `uvm_field_int(addr_b, UVM_DEFAULT) `uvm_field_int(data_b, UVM_DEFAULT) `uvm_object_utils_end
virtual function string convert2str(); return $sformatf("addr=0x%0h data=0x%0h addr_a=0x%0h data_a=0x%0h addr_b=0x%0h data_b=0x%0h", addr, data, addr_a, data_a, addr_b, data_b); endfunction
function new(string name = "switch_item"); super.new(name); endfunction endclass
class driver extends uvm_driver#(switch_item); `uvm_component_utils(driver) function new(string name = "driver", uvm_component parent = null); super.new(name, parent); endfunction
virtual switch_if vif;
virtual function void build_phase(uvm_phase phase); super.build_phase(phase); if(!uvm_config_db#(virtual switch_if)::get(this, "", "switch_if", vif)) `uvm_fatal("DRV", "Could NOT get vif"); endfunction
virtual task run_phase(uvm_phase phase); super.run_phase(phase); forever begin switch_item m_item; `uvm_info("DRV", $sformatf("wait for item from sequencer"), UVM_LOW); seq_item_port.get_next_item(m_item); drive_item(m_item); seq_item_port.item_done(); end endtask
virtual task drive_item(switch_item m_item); vif.vld <= 1; vif.addr <= m_item.addr; vif.data <= m_item.data; @(posedge vif.clk);
vif.vld <= 0; endtask endclass
class monitor extends uvm_monitor; `uvm_component_utils(monitor) function new(string name = "Monitor", uvm_component parent = null); super.new(name, parent); endfunction
uvm_analysis_port #(switch_item) mon_analysis_port; virtual switch_if vif; semaphore sema4;
virtual function void build_phase(uvm_phase phase); super.build_phase(phase);
if(!uvm_config_db#(virtual switch_if)::get(this, "", "switch_if", vif)) begin `uvm_fatal("MON", "Could not get vif"); end else begin `uvm_info("MON", "Could get vif", UVM_LOW); end sema4 = new(1); mon_analysis_port = new("mon_analysis_port", this); endfunction
virtual task run_phase(uvm_phase phase); super.run_phase(phase);
fork sample_port("Thread1"); sample_port("Thread2"); join endtask
virtual task sample_port(string tag); `uvm_info("MON", $sformatf("thread:%s start", tag), UVM_LOW) forever begin @ (posedge vif.clk); if (vif.vld) begin switch_item item = new; sema4.get(); item.addr = vif.addr; item.data = vif.data; `uvm_info("MON", $sformatf("%s 1st step", tag), UVM_LOW) @ (posedge vif.clk); item.addr_a = vif.addr_a; item.data_a = vif.data_a; item.addr_b = vif.addr_b; item.data_b = vif.data_b; `uvm_info("MON", $sformatf("%s 2nd step", tag), UVM_LOW) mon_analysis_port.write(item); sema4.put(); end end endtask endclass
class agent extends uvm_agent; `uvm_component_utils(agent) function new(string name = "agent", uvm_component parent = null); super.new(name, parent); endfunction
monitor m0; driver d0; uvm_sequencer #(switch_item) s0;
virtual function void build_phase(uvm_phase phase); super.build_phase(phase); m0 = monitor::type_id::create("m0", this); d0 = driver::type_id::create("d0", this); s0 = uvm_sequencer#(switch_item)::type_id::create("s0", this); endfunction
virtual function void connect_phase(uvm_phase phase); super.connect_phase(phase); d0.seq_item_port.connect(s0.seq_item_export); endfunction endclass
class scoreboard extends uvm_scoreboard; `uvm_component_utils(scoreboard) function new(string name = "scoreboard", uvm_component parent = null); super.new(name, parent); endfunction
uvm_analysis_imp #(switch_item, scoreboard) m_analysis_imp;
virtual function void build_phase(uvm_phase phase); super.build_phase(phase); m_analysis_imp = new("m_analysis_imp", this); endfunction
virtual function write(switch_item item); if (item.addr <= 8'h3F) begin if (item.addr == item.addr_a && item.data == item.data_a && 0 == item.addr_b && 0 == item.data_b) begin `uvm_info(get_type_name(), $sformatf("item.addr <= ADDR_DIV expected result"), UVM_LOW); end else begin `uvm_error(get_type_name(), $sformatf("item.addr <= ADDR_DIV result error addr=0x%0h data=0x%0h addr_a=0x%0h data_a=0x%0h addr_b=0x%0h addr_b=0x%0h", item.addr, item.data, item.addr_a, item.data_a, item.addr_b, item.data_b) ); end end else begin if (item.addr == item.addr_b && item.data == item.data_b && 0 == item.addr_a && 0 == item.data_a) begin `uvm_info(get_type_name(), $sformatf("item.addr > ADDR_DIV expected result"), UVM_LOW); end else begin `uvm_error(get_type_name(), $sformatf("item.addr > ADDR_DIV result error addr=0x%0h data=0x%0h addr_a=0x%0h data_a=0x%0h addr_b=0x%0h addr_b=0x%0h", item.addr, item.data, item.addr_a, item.data_a, item.addr_b, item.data_b) ); end end endfunction endclass
class env extends uvm_env; `uvm_component_utils(env) function new(string name = "env", uvm_component parent = null); super.new(name, parent); endfunction
agent a0; scoreboard sb0;
virtual function void build_phase(uvm_phase phase); super.build_phase(phase); a0 = agent::type_id::create("a0", this); sb0 = scoreboard::type_id::create("sb0", this); endfunction
virtual function void connect_phase(uvm_phase phase); super.connect_phase(phase); a0.m0.mon_analysis_port.connect(sb0.m_analysis_imp); endfunction endclass
class gen_item_seq extends uvm_sequence; `uvm_object_utils(gen_item_seq) function new(string name = "gen_item_seq"); super.new(name); endfunction
int num = 10;
virtual task body(); for (int i = 0; i < num; i++) begin switch_item m_item = switch_item::type_id::create("m_item"); start_item(m_item); m_item.addr = (i + 8'h38); m_item.data = $random; `uvm_info("SEQ", $sformatf("Generate1 new item: "), UVM_LOW) m_item.print(); finish_item(m_item); end
`uvm_info("SEQ", $sformatf("done %0d time", num), UVM_LOW) endtask endclass
class test extends uvm_test; `uvm_component_utils(test) function new(string name = "test", uvm_component parent = null); super.new(name, parent); endfunction
env e0; virtual switch_if vif;
virtual function void build_phase(uvm_phase phase); super.build_phase(phase); e0 = env::type_id::create("e0", this); if (!uvm_config_db#(virtual switch_if)::get(this, "", "switch_if", vif)) `uvm_fatal("TEST", "Did not get vif")
uvm_config_db#(virtual switch_if)::set(this, "e0.a0.*", "switch_if", vif); endfunction
virtual function void end_of_elaboration_phase(uvm_phase phase); super.end_of_elaboration_phase(phase); uvm_top.print_topology(); endfunction
virtual task run_phase(uvm_phase phase); gen_item_seq seq = gen_item_seq::type_id::create("seq"); phase.raise_objection(this);
apply_reset(); seq.num = 25; $display("seq.start"); seq.start(e0.a0.s0); $display("seq gogo"); #1000 phase.drop_objection(this); endtask
virtual task apply_reset(); vif.rstn <= 0; $display("apply_reset vif.rstn:%0b", vif.rstn); repeat(5) @(posedge vif.clk); $display("apply_reset vif.rstn:%0b", vif.rstn); vif.rstn <= 1; repeat(10) @(posedge vif.clk); $display("apply_reset vif.rstn:%0b", vif.rstn); endtask endclass
module tb; reg clk = 0; always #10 clk = ~clk; switch_if _if(clk);
switch u0(.clk(clk), .rstn(_if.rstn), .vld(_if.vld), .addr(_if.addr), .data(_if.data), .addr_a(_if.addr_a), .data_a(_if.data_a), .addr_b(_if.addr_b), .data_b(_if.data_b) ); initial begin $display("tb_start"); uvm_config_db #(virtual switch_if)::set (null, "uvm_test_top", "switch_if", _if); run_test("test"); $display("tb_end"); end
initial begin $dumpvars; $dumpfile("dump.vcd"); end endmodule
|