how to design sin wave in varilog?

This is pure electronics like Processor, transistor, diode, ROM, RAM related discussion..
ravi vadi
Posts: 2
Joined: Tue Jul 22, 2014 6:09 pm
Contact:

how to design sin wave in varilog?

Postby ravi vadi » Mon Sep 01, 2014 7:09 pm

Can we design analog circuit using verilog?
How can I check waveform of sine wave ?


Guest

Re: how to design sin wave in varilog?

Postby Guest » Fri Jun 19, 2015 8:26 pm

You can write below code to design sin wave.

Code: Select all

module sine_cos(clk, reset, en, sine, cos);
   input clk, reset, en;
   output [7:0] sine,cos;
   reg [7:0] sine_r, cos_r;
   assign      sine = sine_r + {cos_r[7], cos_r[7], cos_r[7], cos_r[7:3]};
   assign      cos  = cos_r - {sine[7], sine[7], sine[7], sine[7:3]};
   always@(posedge clk or negedge reset)
     begin
         if (!reset) begin
             sine_r <= 0;
             cos_r <= 120;
         end else begin
             if (en) begin
                 sine_r <= sine;
                 cos_r <= cos;
             end
         end
     end
endmodule // sine_cos

Guest

Re: how to design sin wave in varilog?

Postby Guest » Fri Jun 19, 2015 8:27 pm

If you want to generate sin stimulus in a testbench, you can create sin wave in other tools such as matlab or SPW then write out the data to a file.

finally you can read the file in testbench written in verilog.

Guest

Re: how to design sin wave in varilog?

Postby Guest » Fri Jun 19, 2015 8:29 pm

Two capabilities in SystemVerilog allow for the creation of a module that can produce a sine wave as an output: the ability to pass real values through port connections and DPI.

Obviously, to produce a sine wave, you need access to the sin function. This is where DPI is handy to add the math functions to your simulation. Here is an example of a package I created to contain the math functions:

package math_pkg;

//import dpi task C Name = SV function name

import "DPI" pure function real cos (input real rTheta);

import "DPI" pure function real sin (input real rTheta);

import "DPI" pure function real log (input real rVal);

import "DPI" pure function real log10 (input real rVal);

endpackage : math_pkg



The import"DPI" construct defines a new function that you can use in your code that refers to a C function. In the case of the math functions listed above, they already exist in the libmath.so library built into Linux and so there is no additional code required. Now that I have my math functions, I can create my module.

Code: Select all

module sine_wave(output real sine_out);

  import math_pkg::*;

 

  parameter  sampling_time = 5;

  const real pi = 3.1416;

  real       time_us, time_s ;

  bit        sampling_clock;

  real       freq = 20;

  real       offset = 2.5;

  real       ampl = 2.5;

 

  always sampling_clock = #(sampling_time) ~sampling_clock;

 

  always @(sampling_clock) begin

    time_us = $time/1000;

    time_s = time_us/1000000;

  end

  assign sine_out = offset + (ampl * sin(2*pi*freq*time_s));

endmodule



Return to “Electronics and VLSI Design”

Who is online

Users browsing this forum: No registered users and 1 guest