Forum Discussion
How to force manually in ModelSim one dada bus with value like sin(x)
- 1 year ago
In that case, you may create a module for the sine function:
module sine_generator (
input wire clk,
input wire reset,
output reg signed [10:0] sine_out
);
reg [6:0] index; // 7-bit index for 128 samples
reg signed [10:0] sine_lut [0:127]; // 128-point lookup table
initial begin
// Precompute sine values and load them into the LUT
sine_lut[0] = 0;
sine_lut[1] = 50;
sine_lut[2] = 100;
sine_lut[3] = 150;
sine_lut[4] = 200;
sine_lut[5] = 250;
// ... (continue with the full sine wave values up to 127)
sine_lut[127] = -50;
end
always @(posedge clk or posedge reset) begin
if (reset) begin
index <= 0;
sine_out <= 0;
end else begin
sine_out <= sine_lut[index]; // Output the sine value
index <= index + 1; // Increment index for next sample
end
end
endmodule
You will need to write a testbench for stimulus.
We don't usually offer the code in the forum, but here is some example you can try out:
module testbench; reg [10:0] input_signal; integer i; real pi = 3.14159; real sin_value; initial begin for (i = 0; i < 100; i = i + 1) begin sin_value = $sin(2 * pi * i / 100); // Generate sine wave input_signal = sin_value * ((1 << 10) - 1); // Scale to 11-bit range #10; // Wait 10 time units end end endmodule.
Also, please take a look https://www.youtube.com/watch?v=Dm7Ow4XI22g on how to setup the testbench.