This short tutorial is to show junior ASIC design and verification engineers or EE students how to simulate PLL in an ASIC project.
AboveĀ is a typical PLL architecture and it is used as an example PLL in this tutorial. The analog core consists of PFD (phase frequency difference detection), CP (charge pump which translates PF difference to charging current to control VCO freq), filter (lowpass filter), and VCO. PFD takes in two inputs, reference clk divided by a programmable value (refClk_divN[7:0]) and VCO output clock divided by another programmable value (pllFB_divN[7:0]). There are multiple output clocks which are derived from VCO output and each has a independent divider.
In analog design, PFD and VCO each have an operating range. In our example, PFD is assumed to operate from 4 to 8MHz and the nominal is 6MHz. VCO is assumed to operate from 200 to 400MHz and the nominal is 300MHz.
Therefore, one application case is refClk is 4.5MHz, VCO output is 288MHz, refClk_divN=1, and pllFB_divN=64 (so 288/64=4.5). Just anther example. It can be refClk is 24MHz, VCO output is still 288MHz, refClk_divN=4 (so 24/5=4.8Mhz which is within PFD range), and pllFB_divN=64 (so 288/60=4.8)
Other input signals are:
rst_n:Ā active low reset signal.
sleep_n: active low. when active, pll is in sleep mode to save pwr and no clk out.
forceBypass: when high, PLL analog core is bypassed and divided refclk goes to VCO output directly (PLL digital part still functions so dividers are not bypassed).
Other output signals are:
pllLock: active high indicating PLL is in lock. Normally this signal comes from PFD block and is asserted when PFD sees alignment for successive tens of PFD clocks and deasserted otherwise.
sysrst_n: this signal follows pllLock and is optional. It can be used to reset digital logics and idea is if clock is not stable (not locked) digital logics should be in reset state. It is up to chip integrator how to use it.
When analog team delivers a PLL core to ASIC digital team for integration, the delivery normally contains GDS file (layout),LEF file (for floorplanning/etc), lib file (for BE timing analysis), and some simulation model in Verilog or VHDL.
1. Simulation model IO signals need to match those in lib, lef, and gds file otherwise BE side will flag error. Therefore they need to be properly connected at RTL level. Some pins can be just tie high or tie low. An example is supply and ground pins. If design flow support UPF, power and ground pins are not actually in verilog model.
2. From simulation point of view, lots of time our simulation goal is to check digital logic functionality. In this case, we can even bypass PLL beh model and just use forever loop to generate PLL output clock. This can speed up your simulation.
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 |
`ifdef PLL_BEH_MODEL_BYPASS initial begin half_oClk01_period = 20; half_oClk02_period = 30; half_oClk03_period = 40; end always oClk01 = #half_oClk01_period ~oClk01; always oClk02 = #half_oClk02_period ~oClk01; always oClk03 = #half_oClk03_period ~oClk01; `else pllacore u_pllaCore( .divClkin(divClk), .rst_n(rst_n), .sleep_n(sleep_n), .c_pllfb_divN(c_pllfb_divN), .VCODelayed(vcoClk), .fbClk(fbClk), .lockDetAsync(lockDetAsync), .vcoClk(vcoClk), .filterVoltageBus(filterVoltageBus)); plldcore u_plldCore (.rst_n(rst_n), .sleep_n(sleep_n), .lockDetAsync(lockDetAsync), .refClk(refClk), .vcoClk(vcoClk), .forceBypass(forceBypass), .divClk(divClk), .c_refclk_divN(c_refclk_divN), .oClk01(oClk01), .oClk02(oClk02), .oClk03(oClk03), .pllLock(pllLock), .sysrst_n(sysrst_n)); `endif |
3. However, if the simulation goal is to check the control and timing of PLL (remember PLL has control inputs for programmable divider values and reset/sleep/bypass control inputs and pllLock and sysrst_n control outputs) or to simulate chip behavior at bootup or sleep entry/exit, we need to use PLL beh model delivered by analog group to include PLL control and timing.
4. PLL beh model delivered needs to model PLL reset beh, start/stop timing and sequence, and its response to divider value controls. In our example, PLL settling time can be controlled by verification engineer. It is normally a large value but to speed up simulation it can be set small in certain simulation.
5. Note normally PLL beh model delivered by analog group doesn’t fully or 100% accurately model PLL beh. For example:
5.1 clock jitter is normally not modeled in verilog model.
5.2 we mentioned our example PLL PFD and VCO have operating range. If out of range,our example PLL model can detect it and make outputs idle. This is hardly the real case. First, PLL out-of-range beh is more soft than hard which means PLL may still function when it is out of range and even if it malfunctions its output is not just idle and some outputs may be there. The range mentioned is to guarantee all chips can work in this range across PVT corners.
The following is our example PLL core beh model and a simple TB to drive and check this PLL core. The PLL core beh model is an example but to show how a PLL model delivered by analog team may look at.
Waveform is as below.
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 |
///////////////////////////////////////////////////////////////////////////// // A simple pll verilog simulation model and its TB ///////////////////////////////////////////////////////////////////////////// `timescale 1ns/1ps module tb_pllcr(); reg rst_n, refClk, sleep_n, forceBypass; reg [7:0] c_pllfb_divN; reg [7:0] c_refclk_divN; wire oClk01; wire oClk02; wire oClk03; wire pllLock, sysrst_n; real half_refClk_period; pllcore u_pllcore (.rst_n(rst_n), .sleep_n(sleep_n), .refClk(refClk), .forceBypass(forceBypass), .c_refclk_divN(c_refclk_divN), .c_pllfb_divN(c_pllfb_divN), .oClk01(oClk01), .oClk02(oClk02), .oClk03(oClk03), .pllLock(pllLock), .sysrst_n(sysrst_n)); initial begin refClk = 1'b0 ; sleep_n = 1'b1; rst_n = 1'b0; forceBypass = 1'b0; c_refclk_divN = 0; c_pllfb_divN = 63; half_refClk_period = 111.111111; doReset ; $display("Reset at time = %f",$realtime); #46000.000000 doSleep; $display("Exit sleep state at time = %f",$realtime); #34500.000000 c_pllfb_divN = 36; half_refClk_period = 85.535882; $display("set c_pllfb_divN = 36 at time = %f",$realtime); #5750.000000 doReset; $display("Reset at time = %f to resume correct operation",$realtime); #34500.000000 c_pllfb_divN = 113; half_refClk_period = 158.503725; $display("set c_pllfb_divN = 113 at time = %f",$realtime); #5750.000000 doReset; $display("Reset at time = %f to resume correct operation",$realtime); #34500.000000 c_pllfb_divN = 255; #5750.000000 doReset; $display("set c_pllfb_divN = 255 at time = %f. This an invalid selection",$realtime); half_refClk_period = 111.111111; #5750.000000 c_pllfb_divN = 63; #5750.000000 doReset; $display("Reset at time = %f to resume correct operation",$realtime); #46000.000000 forceBypass = 1'b1; doReset; $display("going into byPass mode at time = %f",$realtime); end always refClk = #half_refClk_period ~refClk; task doReset ; begin rst_n = 1'b0 ; #1000 rst_n = 1'b1; end endtask task doSleep ; begin sleep_n = 1'b0 ; #1000 sleep_n = 1'b1; end endtask endmodule |
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
///////////////////////////////////////////////////////////////////////////// // A simple pll verilog simulation model and its TB // vco_freq = (ref_freq/M)*N, so c_refclk_divN = M-1 and c_pllfb_divN = N-1 // Set SETTLINGTIMEpll (PLL settling time) small for fast simulation ///////////////////////////////////////////////////////////////////////////// `timescale 1ns/1ps module pllcore (rst_n, sleep_n, refClk, forceBypass, c_pllfb_divN, c_refclk_divN, oClk01, oClk02, oClk03, pllLock, sysrst_n ); input rst_n; input sleep_n; input refClk; input forceBypass; input [7:0] c_refclk_divN; input [7:0] c_pllfb_divN; output oClk01; output oClk02; output oClk03; output pllLock; output sysrst_n; wire divClk, vcoClk, fbClk, lockDetAsync; wire [63:0] filterVoltageBus; `ifdef PLL_BEH_MODEL_BYPASS initial begin half_oClk01_period = 20; half_oClk02_period = 30; half_oClk03_period = 40; end always oClk01 = #half_oClk01_period ~oClk01; always oClk02 = #half_oClk02_period ~oClk01; always oClk03 = #half_oClk03_period ~oClk01; `else pllacore u_pllaCore( .divClkin(divClk), .rst_n(rst_n), .sleep_n(sleep_n), .c_pllfb_divN(c_pllfb_divN), .VCODelayed(vcoClk), .fbClk(fbClk), .lockDetAsync(lockDetAsync), .vcoClk(vcoClk), .filterVoltageBus(filterVoltageBus)); plldcore u_plldCore (.rst_n(rst_n), .sleep_n(sleep_n), .lockDetAsync(lockDetAsync), .refClk(refClk), .vcoClk(vcoClk), .forceBypass(forceBypass), .divClk(divClk), .c_refclk_divN(c_refclk_divN), .oClk01(oClk01), .oClk02(oClk02), .oClk03(oClk03), .pllLock(pllLock), .sysrst_n(sysrst_n)); `endif endmodule `define vpllinit1 1 `define vpllstart1 6 `define vpllhi1 4 `define vplllo1 6 `define vpllinit2 1 `define vpllstart2 6 `define vpllhi2 6 `define vplllo2 6 `define vpllinit3 1 `define vpllstart3 10 `define vpllhi3 8 `define vplllo3 10 module plldcore (rst_n, sleep_n, lockDetAsync, refClk, vcoClk, forceBypass, divClk, c_refclk_divN, oClk01, oClk02, oClk03, pllLock, sysrst_n); input rst_n; input sleep_n; input lockDetAsync; input refClk; input vcoClk; input forceBypass; input [7:0] c_refclk_divN; output oClk01; output oClk02; output oClk03; output divClk; output pllLock; output sysrst_n; reg iDivClk; reg [8:0] countc_refclk_divN; reg oClk01; reg oClk02; reg oClk03; wire divClk; wire c_refclk_divNReset, clkResetL, intSleepL, intLockDetAsync, clkin; assign intSleepL = forceBypass ? 1'b1 : sleep_n; assign intLockDetAsync = forceBypass ? 1'b1 : lockDetAsync; assign clkin = forceBypass ? refClk : vcoClk; resetBlockpll iResetBlock (.rst_n(rst_n), .sleep_n(intSleepL), .lockDetAsync(intLockDetAsync), .vcoClk(clkin), .slowestClk(oClk03), .refClk(refClk), .pllLock(pllLock), .sysrst_n(sysrst_n), .c_refclk_divNReset(c_refclk_divNReset), .clkResetL(clkResetL)); initial iDivClk = 0; always @(posedge c_refclk_divNReset) begin @(posedge refClk or c_refclk_divNReset); while(c_refclk_divNReset) begin iDivClk = !iDivClk; repeat(c_refclk_divN+1) @(refClk or c_refclk_divNReset); iDivClk = !iDivClk; if(c_refclk_divNReset) repeat(c_refclk_divN+1) @(refClk or c_refclk_divNReset); end end assign divClk = (c_refclk_divN==0) ? refClk : iDivClk; initial oClk01 = `vpllinit1; always @(posedge clkResetL) begin oClk01 = `vpllinit1; repeat(`vpllstart1) @(clkin or clkResetL); while(clkResetL) begin oClk01 = !oClk01; repeat(`vpllhi1) @(clkin or clkResetL); oClk01 = !oClk01; if(clkResetL) repeat(`vplllo1) @(clkin or clkResetL); end end initial oClk02 = `vpllinit2; always @(posedge clkResetL) begin oClk02 = `vpllinit2; repeat(`vpllstart2) @(clkin or clkResetL); while(clkResetL) begin oClk02 = !oClk02; repeat(`vpllhi2) @(clkin or clkResetL); oClk02 = !oClk02; if(clkResetL) repeat(`vplllo2) @(clkin or clkResetL); end end initial oClk03 = `vpllinit3; always @(posedge clkResetL) begin oClk03 = `vpllinit3; repeat(`vpllstart3) @(clkin or clkResetL); while(clkResetL) begin oClk03 = !oClk03; repeat(`vpllhi3) @(clkin or clkResetL); oClk03 = !oClk03; if(clkResetL) repeat(`vplllo3) @(clkin or clkResetL); end end endmodule module resetBlockpll (rst_n, sleep_n, lockDetAsync, vcoClk, slowestClk, refClk, pllLock, sysrst_n, c_refclk_divNReset, clkResetL); input rst_n; input sleep_n; input lockDetAsync; input vcoClk; input slowestClk; input refClk; output pllLock; output sysrst_n; output c_refclk_divNReset; output clkResetL; reg c_refclk_divNReset; reg intc_refclk_divNReset; reg pllActive; reg intPllActive1; reg intPllActive2; reg iClkResetL; reg intClkResetL1; reg intClkResetL2; reg iPllLock; reg intPllLock1; reg intPllLock2; reg iSysResetL; reg intSysResetL1; reg intSysResetL2; wire iResetL; initial begin c_refclk_divNReset = 0; intc_refclk_divNReset = 0; iClkResetL = 0; intClkResetL1 = 0; intClkResetL2 = 0; pllActive = 0; intPllActive1 = 0; intPllActive2 = 0; iPllLock = 0; intPllLock1 = 0; intPllLock2 = 0; iSysResetL = 0; intSysResetL1 = 0; intSysResetL2 = 0; end always @(posedge vcoClk or negedge iResetL) if(!iResetL) begin intPllLock1 <= 1'b0; intPllLock2 <= 1'b0; iPllLock <= 1'b0; end else begin intPllLock1 <= lockDetAsync; intPllLock2 <= intPllLock1; iPllLock <= intPllLock2; end always @(posedge slowestClk or negedge iResetL) if(!iResetL) begin intSysResetL1 <= 1'b0; intSysResetL2 <= 1'b0; iSysResetL <= 1'b0; end else begin intSysResetL1 <= iPllLock; intSysResetL2 <= intSysResetL1; iSysResetL <= iSysResetL | intSysResetL2; end always @(posedge vcoClk or negedge iResetL) if(!iResetL) begin intPllActive1 <= 1'b0; intPllActive2 <= 1'b0; pllActive <= 1'b0; end else begin intPllActive1 <= 1'b1; intPllActive2 <= intPllActive1; pllActive <= intPllActive2; end //latch and triple bank pllActive to generate clkResetL always @(posedge vcoClk or negedge iResetL) if(!iResetL) begin intClkResetL1 <= 1'b0; intClkResetL2 <= 1'b0; iClkResetL <= 1'b0; end else begin intClkResetL1 <= iPllLock | intClkResetL1; intClkResetL2 <= intClkResetL1; iClkResetL <= intClkResetL2; end //generate c_refclk_divNReset always @(posedge refClk or negedge iResetL) if(!iResetL) begin intc_refclk_divNReset <= 1'b0; c_refclk_divNReset <= 1'b0; end else begin intc_refclk_divNReset <= 1'b1; c_refclk_divNReset <= intc_refclk_divNReset; end assign iResetL = rst_n && sleep_n; assign pllLock = iResetL && iPllLock; assign sysrst_n = iResetL && iSysResetL; assign clkResetL = iResetL && iClkResetL; endmodule //`define SETTLINGTIMEpll 237000 `define SETTLINGTIMEpll 23000 `define DIVCLKNOMpll 4.500000e-3 `define VCOCLKNOMpll 288.000000e-3 `define DIVCLKTOLpll 0.300000 `define VCOCLKTOLpll 0.250000 `define KVCOTYPpll 966.571429 `define CLKTOL 0.01 `define Vt 0.5 module pllacore ( divClkin, rst_n, sleep_n, VCODelayed, c_pllfb_divN, fbClk, lockDetAsync, vcoClk, filterVoltageBus); input divClkin; input rst_n; input sleep_n; input VCODelayed; input [7:0] c_pllfb_divN; output fbClk; output lockDetAsync; output vcoClk; output [63:0] filterVoltageBus; reg iFbClk, vcoClk, lockDetAsync; wire divClk,fbClk; reg divClkPeriodChanged, c_pllfb_divNChanged; reg noDivClk, invalidDivClk, invalidVcoClk, resetDone; reg startTimeSet, settlingTimeComplete; reg [8:0] countc_pllfb_divN; reg [7:0] c_pllfb_divNPrev; reg invert,done; reg divClkReset,intDivClkReset1,intDivClkReset2; reg vcoValid,intVcoValid1,intVcoValid2; wire invalidInput, iResetL, resetDoneDelayed; real divClkRe1, divClkRe2, divClkPeriod, divClkPeriodPrev; real vcoClkHalfPeriod; real vcoClkRe,VCODelayedRe,tdbuf; real lockStartTime; real filterVoltage; real starttime,currtime,reqtime,delta; integer i; wire [63:0] filterVoltageBus = $realtobits(filterVoltage); assign divClk = divClkin; initial begin divClkPeriodChanged = 0; divClkPeriod = 1/`DIVCLKNOMpll; divClkPeriodPrev = 0; c_pllfb_divNChanged = 0; resetDone = 0; divClkReset = 0; intDivClkReset1 = 0; intDivClkReset2 = 0; divClkRe1 = 0; divClkRe2 = 0; vcoClkHalfPeriod = 0; vcoClkRe = 0; VCODelayedRe = 0; tdbuf = 0; noDivClk = 0; invalidDivClk = 0; invalidVcoClk = 0; settlingTimeComplete = 0; lockStartTime = 0; startTimeSet = 0; vcoClk = 0; lockDetAsync = 0; invert = 0; vcoValid = 0; intVcoValid1 = 0; intVcoValid2 = 0; done = 0; starttime = 0; currtime = 0; reqtime = 0; delta = 0; end // and rst_n and sleep_n assign iResetL = rst_n & sleep_n; //Wait until there have been several divClk cycles after reset before monitoring for invalid input always @(posedge divClk or negedge iResetL) if(!iResetL) begin intDivClkReset1 <= 1'b0; intDivClkReset2 <= 1'b0; divClkReset <= 1'b0; end else begin intDivClkReset1 <= 1'b1; intDivClkReset2 <= intDivClkReset1; divClkReset <= intDivClkReset2; end //Wait until there have been several vcoClk cycles to issue a valid signal always @(posedge vcoClk or negedge iResetL) if(!iResetL) begin intVcoValid1 <= 1'b0; intVcoValid2 <= 1'b0; vcoValid <= 1'b0; end else begin intVcoValid1 <= 1'b1; intVcoValid2 <= intVcoValid1; vcoValid <= intVcoValid2; end //Calculate the input clock period, monitor changes in it and make sure that its within range always @ (posedge divClkin) begin divClkRe1 = divClkRe2; divClkRe2 = $realtime; divClkPeriodPrev = divClkPeriod; divClkPeriod = divClkRe2 - divClkRe1; if(((divClkPeriod - divClkPeriodPrev) > `CLKTOL) || ((divClkPeriodPrev - divClkPeriod) > `CLKTOL) ) divClkPeriodChanged = 1; else divClkPeriodChanged = 0; if(divClkReset) if( ((1/divClkPeriod) < (`DIVCLKNOMpll-(`DIVCLKNOMpll*`DIVCLKTOLpll))) || ((1/divClkPeriod) > (`DIVCLKNOMpll+(`DIVCLKNOMpll*`DIVCLKTOLpll))) ) invalidDivClk = 1; else invalidDivClk = 0; else invalidDivClk = 0; end //because if there are no divClk edges then invalidDivClk cannot become active as it posedge divClk triggered always #(1/(2.0*`DIVCLKNOMpll)) noDivClk = (($realtime - divClkRe2)> (2.0/(`DIVCLKNOMpll-(`DIVCLKNOMpll*`DIVCLKTOLpll)))) ? 1 : 0; //Monitor for changes in the feedback divider number always @ (posedge divClk) begin if(c_pllfb_divNPrev != c_pllfb_divN) c_pllfb_divNChanged = 1; else c_pllfb_divNChanged = 0; c_pllfb_divNPrev = c_pllfb_divN; end //Calculate the vco clock period, monitor changes in it and make sure that its within range always @ (posedge divClkin) begin vcoClkHalfPeriod = divClkPeriod/((c_pllfb_divN+1)*2.0); if(vcoValid) begin if( ((1/(vcoClkHalfPeriod*2.0)) < (`VCOCLKNOMpll-(`VCOCLKNOMpll*`VCOCLKTOLpll))) || ((1/(vcoClkHalfPeriod*2.0)) > (`VCOCLKNOMpll+(`VCOCLKNOMpll*`VCOCLKTOLpll))) ) invalidVcoClk = 1; else invalidVcoClk = 0; end else invalidVcoClk = 0; end //Measure buffer delay always @ (posedge vcoClk) begin if(!vcoValid) begin tdbuf = 0; done = 0; end else if (done) tdbuf = tdbuf; else begin vcoClkRe = $realtime; @(posedge VCODelayed) VCODelayedRe = $realtime; if((VCODelayedRe-vcoClkRe)>(vcoClkHalfPeriod*2.0)) begin tdbuf = VCODelayedRe-vcoClkRe-(vcoClkHalfPeriod*2.0); invert = 0; end else if((VCODelayedRe-vcoClkRe)>vcoClkHalfPeriod) begin tdbuf = (vcoClkHalfPeriod*2.0)-(VCODelayedRe-vcoClkRe); invert = 0; end else begin tdbuf = VCODelayedRe-vcoClkRe; tdbuf = (divClkPeriod/2.0)-tdbuf; invert = 1; end done = 1; end end //Check that always get an initial rst_n and also after going to an invalid input or vco clock frequency and back assign invalidInput = divClkReset ? (invalidDivClk || invalidVcoClk || noDivClk || c_pllfb_divNChanged || divClkPeriodChanged) : 0; always @(rst_n or posedge invalidInput) begin if(invalidInput) resetDone = 0; else if(rst_n && (!invalidInput)) resetDone = 1; else if(!rst_n) resetDone = 0; else resetDone = resetDone; end assign #(6.0*vcoClkHalfPeriod) resetDoneDelayed = resetDone; //Generate lock detect signal always @ (posedge divClk or sleep_n or invalidInput or resetDone or c_pllfb_divNChanged or divClkPeriodChanged) begin if(!(resetDone && sleep_n) || invalidInput || c_pllfb_divNChanged || divClkPeriodChanged) begin lockDetAsync = 0; settlingTimeComplete = 0; startTimeSet = 0; end else begin if(!startTimeSet) begin lockStartTime = $realtime; startTimeSet = 1; end if(($realtime - lockStartTime) > `SETTLINGTIMEpll) settlingTimeComplete = 1; if(settlingTimeComplete) lockDetAsync = 1; end end //Generate Vco Clock always begin if(!sleep_n) #1 vcoClk = 0; else if((!divClkReset) || (!resetDoneDelayed)) //add memory so that there can be an extra couple of vcoClk cycles to #1 vcoClk = 1'bx; //clear the pllLock in the digital else begin @(posedge divClk) vcoClk = 1; starttime = $realtime; i=1; #vcoClkHalfPeriod vcoClk = 0; repeat(c_pllfb_divN) begin #vcoClkHalfPeriod vcoClk = 1; currtime = $realtime-starttime; reqtime = 2.0*vcoClkHalfPeriod*i; delta=reqtime-currtime; i=i+1; if((vcoClkHalfPeriod+delta) < 0) #(vcoClkHalfPeriod) vcoClk = 0; else #(vcoClkHalfPeriod+delta) vcoClk = 0; end end end //Generate Feedback Clock always @(posedge VCODelayed or sleep_n or resetDone) begin if((!divClkReset) || (!resetDone)) begin iFbClk = 1'bx; countc_pllfb_divN = 0; end else if(!sleep_n) begin iFbClk = 0; countc_pllfb_divN = 0; end else begin if(countc_pllfb_divN == (c_pllfb_divN+1)) begin iFbClk = 1; countc_pllfb_divN = 0; end else iFbClk = 0; countc_pllfb_divN = countc_pllfb_divN + 1; end end assign fbClk = (c_pllfb_divN == 0) ? VCODelayed : iFbClk; //Generate Filter Voltage always @(vcoClk) begin if((!resetDone) || (!sleep_n)) filterVoltage = 0; //reals cannot be x else filterVoltage = (1/(`KVCOTYPpll*vcoClkHalfPeriod*2.0*1e-3))+`Vt; end endmodule |
PllTb.v and Pll.v are here: example_pll_behsim.zip