Can we design analog circuit using verilog?
How can I check waveform of sine wave ?
how to design sin wave in varilog?
Re: how to design sin wave in varilog?
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
Re: how to design sin wave in varilog?
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.
finally you can read the file in testbench written in verilog.
Re: how to design sin wave in varilog?
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.
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