Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Interview Questions Set 2: How can you generate a sine wave using the Verilog co

Verilog Examples

Runnable example from the Verilog Interview Questions Set 2 tutorial (How can you generate a sine wave using the Verilog coding style ?)

Open in IDE
testbench.v
module sine_wave_generator (
  input  clk,
  input  reset,
  output reg signed [15:0] sine
);
  reg signed [15:0] sin_table [0:255];
  reg [7:0] index;
  integer i;

  initial begin
    for (i = 0; i < 256; i = i + 1)
      sin_table[i] = $rtoi(32767.0 * $sin(2.0 * 3.14159265358979 * i / 256.0));
  end

  // One table step per clock: output frequency = f_clk / 256
  always @(posedge clk) begin
    if (reset)
      index <= 0;
    else
      index <= index + 1;
    sine <= sin_table[index];
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.