# Clock delay using case statement

**URL:** <https://verificationacademy.com/forums/t/clock-delay-using-case-statement/41221>\
**Category:** SystemVerilog\
**Tags:** Case-statement-in-sv, SystemVerilog, Delay, time\
**Created:** [January 30, 2023, 9:37am UTC](https://verificationacademy.com/forums/t/clock-delay-using-case-statement/41221 "2023-01-30T09:37:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oglick](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/o/c6cbf5/32.png) [@oglick](https://verificationacademy.com/forums/u/oglick)\
**Post date:** [January 30, 2023, 9:37am UTC](https://verificationacademy.com/forums/t/clock-delay-using-case-statement/41221/1 "2023-01-30T09:37:14Z")

</div>

## Hi, Can I use in tb the following to create clock delay?

## case (a%4) 1: #1; 2: #2; 3: #3; default: #0; endcase

This will be implemented inside a clock, and i dont want to create 4 clocks for each case, so i thought of this way, but in simulation nothing happens.

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [January 30, 2023, 1:44pm UTC](https://verificationacademy.com/forums/t/clock-delay-using-case-statement/41221/2 "2023-01-30T13:44:02Z")

</div>

_In reply to [oglick](https://verificationacademy.com/forums/t/clock-delay-using-case-statement/41221):_

It is helpful to post a complete example which demonstrates your issue. The following works fine:

```verilog

`timescale 1ns/1ns
module case_delay();
  
  initial begin
    for (int a=0; a < 100; a++) begin
      $display("%t: A is %0d", $time, a);
      case (a%4)
        1: #1;
        2: #2;
        3: #3;
        default: #0;
      endcase
    end
  end
endmodule

```

If you are generating a clock, it is recommended to use a variable delay and change the delay as required:

```verilog

`timescale 1ns/1ns
module clock_generator();
  
  bit clk;
  shortint clk_delay = 10;
  
  initial begin
    clk = 0;
    forever #clk_delay clk = ~clk;
  end
  
  task update_clk_delay(input shortint new_clk_delay=10);
    @(posedge clk);
    clk_delay = new_clk_delay;
  endtask
  
  initial begin
    #500;
    update_clk_delay(20);
    #500;
    update_clk_delay(50);
    #500;
    update_clk_delay(5);
    #500;
    $finish();
  end
  
  always @(posedge clk) $display("Time is %t", $time);
endmodule

```
