How do I use clocking wizard to create a slower clock for my program? - verilog

Hey guys so I'm very new to Verilog and Fpga's so sorry if I am missing something very simple. I am trying to interface an external SRAM that I would like to test for functionality. I have written this code along with a test bench to verify it. I will be trying to just simply write and read some data to make sure the device works properly. Since I will be trying to test this on a breadboard I am first attempting to lower the clk frequency at which my program will be running down from the sys clock of 100 MHz to 12 MHz using the clocking wizard IP in Vivado. The problem seems to be that my output from the clocking wizard is not working properly and will only output a single pulse at the incorrect frequency.
top.v:
`timescale 1ns / 1ps
module TOP(
input clk,
input btn,
output reg[7:0] io,
output WE,
output OE,
output CE_0,
output CE_1
);
clk_wiz_0 clktwelve
(
// Clock out ports
.clk_out1(clk0), // output clk_out1
// Status and control signals
.clk_in1(clk) // input clk_in1
);
wire clk1 = clk0;
reg output_enable;
reg write_enable;
reg chip_enable_low;
reg chip_enable_high;
reg[7:0] cnt=8'd0;
always # (posedge clk1 )
if (btn==1)
begin
output_enable = 1'b0;
write_enable = 1'b1;
chip_enable_low = 1'b1;
chip_enable_high = 1'b0;
io[0] = 1'bz;
io[1] = 1'bz;
io[2] = 1'bz;
io[3] = 1'bz;
io[4] = 1'bz;
io[5] = 1'bz;
io[6] = 1'bz;
io[7] = 1'bz;
end
else begin
output_enable =1'b0;
write_enable = 1'b0;
chip_enable_high = 1'b1;
chip_enable_low = 1'b0;
if(cnt<255) cnt=cnt+1'b1;
else cnt=8'd0;
io=cnt;
end
assign OE = output_enable ;
assign WE = write_enable ;
assign CE_0 = chip_enable_low;
assign CE_1 = chip_enable_high;
endmodule
TESTBENCH:
`timescale 1ns / 1ps
module TOP_TB();
reg clk;
reg btn;
wire [7:0] io;
wire WE;
wire OE;
wire CE_0;
wire CE_1;
TOP testbench1 (
.clk(clk),
.btn(btn),
.io(io),
.WE(WE),
.OE(OE),
.CE_0(CE_0),
.CE_1(CE_1)
);
initial begin
btn = 1;
clk = 0;
#200 btn = 0;
#500000 $finish;
end
always #10 clk = ~clk;
endmodule
I plan to ultimately design a pcb board and test the asynchronous sram at maximum frequency of 100MHZ, any advice is helpful!
btw I am using a ARTY A7 35T
I tried this simple code to trouble shoot the problem and still can not get a clean response.
`timescale 1ns / 1ps
module top(
input clk,
output clk0
);
clk_wiz_0 instance1
(
// Clock out ports
.clk_out1(clk0), // output clk_out1
// Clock in ports
.clk_in1(clk) // input clk_in1
);
endmodule
and testbench:
`timescale 1ns / 1ps
module TB(
);
reg clk;
wire clk0;
top instance1 (
.clk(clk),
.clk0(clk0)
);
initial begin
clk = 0;
end
always #10 clk = ~clk;
endmodule
result:

The Xilinx MMCM (multi clock manager) generated by clocking Wizard has an active high reset.
Reset must be asserted for several clocks then released so that the MMCM can synchronize itself.
After reset is released, then the MMCM can take 1000's of clocks to synchronize.
Your simulation may just need to run longer.
The MMCM has a locked output, which asserts when it is internally synchronized.
I recommend that this signal be instrumented in your testbench, so than you can see when it has obtained synchronization.
The MMCM simulation model generated by Vivado must be compiled as part of the simulation.
Some Xilinx IP need to have fine grain time scale for simulation.
Try timescale 1ps / 1ps.

Related

wire output shows nothing in verilog simulation code

It is a simple asynchronous 2 bit counter, two JK flipflops are being used.
Here is my TwoBitCounter.v script.
`timescale 1ns / 1ps
module TwoBitCounter(
input wire clk,
output wire q0,
output wire q1
);
JK jk1(1, 1, clk, q0);
JK jk2(1, 1, q0, q1);
endmodule
module JK(
input wire J,
input wire K,
input wire clk,
output reg out
);
always #(posedge clk) begin
if(J==0&&K==0)
begin end
else if(J==0&&K==1) begin
out <= 1'b0;
end else if(J==1&&K==0) begin
out <= 1'b1;
end else if(J==1&&K==1) begin
out <= ~out;
end
end
endmodule
and this is my simulation code :
`timescale 1ns / 1ps
module TwoBitCounter_sim();
reg clk;
wire q0;
wire q1;
TwoBitCounter twoBitCounter(.clk(clk), .q0(q0));
initial clk = 1'b0;
always clk = #100 ~clk;
initial begin
#1000;
$finish;
end
endmodule
I have checked that JK module works properly alone. I tried disabling one JK flip flop to see if it has no errors while implemented in TwoBitCounter module, and it also did not work. Although I have checked several times to see if the algorithm itself is wrong, but got no clue what the fundamental problem is.
In your code there is only one J/K combination which you use: 1/1. In this state you just invert the out (out <= ~out). However you had never initialized it. Your out initially has x value. Inversion of x is also x. So, it never changes.
You need to figure out a way to initialize the flops either by manipulating J/K values or by other means.
As an example, adding initial out = 0; in the JK module will change the picture, but it will not play well with synthesis. So, you need to figure out your own way.

creating 12 Hz square signal 50MHz clock signal

I wrote code in Verilog that is supposed to create a 12Hz square signal at 50MHz clock signal.
Is it correct?
module w_m(clk, cnt);
input clk;
output [21:0] cnt;
reg [21:0] cnt = 0;
always #(posedge clk)
cnt = cnt + 1;
endmodule
module w_tf;
reg clk;
wire [21:0] cnt;
w_m uut
(
.clk(clk),
.cnt(cnt)
);
initial begin
// Initialize Inputs
clk = 0;
forever begin
#10
clk = !clk;
end
end
endmodule
Yes, it correctly simulates as described, assuming the timescale is set to 1ns.
The clk period is 20ns, and its frequency is 50MHz.
The MSB of cnt (cnt[21]) has a frequency of approximately 12Hz (11.921Hz, according to my simulation results).
You can verifiy this by running a simulation and looking at waveforms. You can post your code on edaplayground, for example.
The logic is right. However, there are some inaccuracies. I have varied the register size in order to run the simulation as allowed by EDA playground.
DUT:
module w_m(clk, clk_out);
input clk;
//output [21:0] cnt;
output clk_out;
reg [11:0] cnt = 0; // make 11 to 21
always #(posedge clk)
cnt = cnt + 1;
assign clk_out = cnt[11];
endmodule
Also, the testbench needs a $finish to stop the forever block from running infinite, taking up the disk space. Added a parallel path in order to run the sim, and exit after 10000ns. You can vary this as per your need.
TB:
module w_tf;
reg clk;
wire cnt;
w_m uut
(
.clk(clk),
.clk_out(cnt)
);
initial begin
// Initialize Inputs
clk = 0;
fork
begin
forever begin
#10 clk = !clk; end
end
begin
#10000000 $finish; // increase this number as per the simulation requirement
end
join
end
endmodule

The input and output signals are not shown in objects windows in Modelsim10.1c

I am a beginner in designing circuit using verilog in modelsim. I use a sample code and a tutorial to learn how modelsim works. The code and the testbench are compiled without any problem and even testbench is simulated without any error but the input and output signals are not shown in object windows and they are not under instance menu. please describe for me how can I find them and simulate the waveforms.
here is my code and the test bench.
the definition of a D flipflop
// module D_FF with synchronous reset
module D_FF(q, d, clk, reset);
output q;
input d, clk, reset;
reg q;
// Lots of new constructs. Ignore the functionality of the
// constructs.
// Concentrate on how the design block is built in a top-down fashion.
always #(negedge clk or posedge reset)
if (reset)
q <= 1'b0;
else
q <= d;
endmodule
the definition of a T flipflop from D
module T_FF(q, clk, reset);
output q;
input clk, reset;
wire d;
D_FF dff0(q, d, clk, reset);
not n1(d, q);
endmodule
counter codes:
module rcc4(q, clk, reset);
output [3:0] q;
input clk, reset;
//4 instances of the module T_FF are created.
T_FF tff0(q[0],clk, reset);
T_FF tff1(q[1],q[0], reset);
T_FF tff2(q[2],q[1], reset);
T_FF tff3(q[3],q[2], reset);
endmodule
testbench code:
module stimulus();
reg clk;
reg reset;
wire[3:0] q;
// instantiate the design block
rcc4 r1(q, clk, reset);
// Control the clk signal that drives the design block. Cycle time = 10
initial
clk = 1'b0; //set clk to 0
always
#5 clk = ~clk; //toggle clk every 5 time units
// Control the reset signal that drives the design block
// reset is asserted from 0 to 20 and from 200 to 220.
initial
begin
reset = 1'b1;
#15 reset = 1'b0;
#180 reset = 1'b1;
#10 reset = 1'b0;
#20 $finish; //terminate the simulation
end
// Monitor the outputs
initial
$monitor($time, " Output q = %d", q);
endmodule
I am using modelsim 10.1c on Windows 10.
The following picture is from my project and it shows my object and instance window.
The switch -voptargs=+acc will solve your issue.
vsim -voptargs=+acc modulename

Circuit behaves poorly in timing simulation but alright in behavioral - new to verilog

I'm new to verilog development and am having trouble seeing where I'm going wrong on a relatively simple counter and trigger output type design.
Here's the verilog code
Note the code returns the same result whether or not the reg is declared on the output_signal without the internal_output_buffer
`timescale 1ns / 1ps
module testcounter(
input wire clk,
input wire resetn,
input wire [31:0] num_to_count,
output reg [7:0] output_signal
);
reg [31:0] counter;
initial begin
output_signal = 0;
end
always#(negedge resetn) begin
counter = 0;
end
always#(posedge clk) begin
if (counter == num_to_count) begin
counter = 0;
if (output_signal == 0) begin
output_signal = 8'hff;
end
else begin
output_signal = 8'h00;
end
end
else begin
counter = counter + 1;
end
end
assign output_signal = internal_output_buffer;
endmodule
And the code is tested by
`timescale 1ns / 1ps
module testcounter_testbench(
);
reg clk;
reg resetn;
reg [31:0] num_to_count;
wire [7:0] output_signal;
initial begin
clk = 0;
forever #1 clk = ~clk;
end
initial begin
num_to_count = 20;
end
initial begin
#7 resetn = 1;
#35 resetn = 0;
end
testcounter A1(.clk(clk),.resetn(resetn),.num_to_count(num_to_count),.output_signal(output_signal));
endmodule
Behavioral simulation looks as I expected
But the timing simulation explodes
And for good measure: the actual probed execution blows up and looks like
Any tips would be appreciated. Thanks all.
The difference between the timing and functional simulations is that a timing simulation models the actual delay of logic gates while the functional simulation just checks if values are correct.
For e.g. if you have a simple combinational adder with two inputs a and b, and output c. A functional simulation will tell you that c=a+b. and c will change in the exact microsecond that a or b changes.
However, a timing simulation for the same circuit will only show you the result (a+b) on c after some time t, where t is the delay of the adder.
What is your platform? If you are using an FPGA it is very difficult to hit 500 MHz. Your clock statement:
forever #1 clk = ~clk;
shows that you toggle the clock every 1ns, meaning that your period is 2ns and your frequency is 500MHz.
The combinational delay through FPGA resources such as lookup tables, multiplexers and wire segments is probably more than 2ns. So your circuit violates timing constraints and gives wrong behaviour.
The first thing I would try is to use a much lower clock frequency, for example 100 MHz and test the circuit again. I expect it to produce the correct results.
forever #5 clk = ~clk;
Then to know the maximum safe frequency you can run at, look at your compilation reports in your design tools by running timing analysis. It is available in any FPGA CAD tool.
Your code seems working fine using Xilinx Vivado 14.2 but there is only one error which is the following line
assign output_signal = internal_output_buffer;
You can't assign registers by using "assign" and also "internal_output_buffer" is not defined.
I also personally recommend to set all registers to some values at initial. Your variables "resetn" and "counter" are not assigned initially. Basicly change your code like this for example
reg [31:0] counter = 32'b0;
Here is my result with your code:
Your verilog code in the testcounter looks broken: (a) you're having multiple drivers, and (b) like #StrayPointer notices, you're using blocking assignments for assigning Register (Flip-Flop) values.
I'm guessing your intent was the following, which could fix a lot of simulation mismatches:
module testcounter
(
input wire clk,
input wire resetn,
input wire [31:0] num_to_count,
output reg [7:0] output_signal
);
reg [31:0] counter;
always#(posedge clk or negedge resetn) begin
if (!resetn) begin
counter <= 0;
end else begin
if (counter == num_to_count) begin
counter <= 0;
end else begin
counter <= counter + 1;
end
end
end
assign output_signal = (counter == num_to_count) ? 8'hff : 8'h00;
endmodule

How to sign-extend a number in Verilog

I'm working on a simple sign-extender in Verilog for a processor I'm creating for Computer Architecture.
Here's what I've got so far: [EDIT: Changed the selection statement slightly]
`timescale 1ns / 1ps
module SignExtender( CLK, extend, extended );
input[7:0] extend;
input CLK;
output[15:0] extended;
reg[15:0] extended;
wire[7:0] extend;
always
begin
while (CLK == 1)
extended[7:0] = extend[7:0];
extended[15:8] = {8{extend[7]}};
end
endmodule
I added the while (CLK == 1) thinking that would solve my problem, which I believe is an infinite loop. When I try to test this in iSim, the circuit never initializes.
I also tried removing the copying syntax and just doing extended[8] = extend[7] etc. for [8]-[15], but the same result occurs, so I'm pretty sure that the innermost syntax is correct.
Here's the test file:
`timescale 1ns / 1ps
module SignExtender_testbench0;
// Inputs
reg [7:0] extend;
reg CLK;
// Outputs
wire [15:0] extended;
// Instantiate the Unit Under Test (UUT)
SignExtender uut (
.extend(extend),
.extended(extended)
);
initial begin
// Initialize Inputs
extend = 0;
#100; // Wait 100 ns for global reset to finish
extend = -30;
CLK = 1;
#10;
CLK = 0;
if (extended == -30)
$display("okay 1");
else
$display("fail 1");
extend = 40;
#10;
if (extended == 40)
$display("okay 2");
else
$display("fail 2");
end
endmodule
Any ideas how I can do this successfully?
You nearly got it...
always #( posedge clk ) begin
extended[15:0] <= { {8{extend[7]}}, extend[7:0] };
end
You're also missing a clock edge for the '40' test. Try this, & let me know how you get on...
We can use the syntax $signed to sign extend
module signextender(
input [7:0] unextended,//the msb bit is the sign bit
input clk,
output reg [15:0] extended
);
always#(posedge clk)
begin
extended <= $signed(unextended);
end
endmodule
By the way your module assign is pure combinational so it should not contain a clk, this is another way of doing your module:
module sign_ext
(
unextend,
extended
);
input [15:0] unextend;
output [31:0] extended;
assign extended = {{16{unextend[15]}}, unextend};
endmodule
//TB
module tb_sign_ext;
reg [15:0] unex;
wire [31:0] ext;
sign_ext TBSIGNEXT
(
.unextend(unex),
.extended(ext)
);
initial
begin
unex = 16'd0;
end
initial
begin
#10 unex = 16'b0000_0000_1111_1111;
#20 unex = 16'b1000_0000_1111_1111;
end
endmodule
;)

Resources