Verilog HDl and modelsim have been exposed to me for only one week. Today , I describe a Counter with 4 width using verilog HDL and simulate it by modelsim. However ,an unexpected simulating result occers.The following is my source verilog HDL counter:
Thanks for your reply ,but modelsim can not give an expexted simulating result using your sorce code ! Even thoiugh i modified my code very simple form as fllowws, modelsim can not give a proper result as well .Why? Could you show me a right counter described by verilog HDL? Thank you very much?
I found out that the counter described by you can work properly after being cleared. If it dose not be cleared ,it will not be count correctly? If so, is this rule suitbale for any kind of counters? Thanks
Yes, my example module requires a synchronous clear to initialize the counter to a known state. Notice how my simulation drives the clr signal for one clock.
If your FPGA provides global register initialization capability (such as Xilinx Spartan/Virtex), here is another way to initialize the counter (this may be Verilog 2001 syntax):
output reg [3:0] data_out = 0;
Thanks for your helps and replies. Now I have a better understanding about digital circuits.All of digital circuits from counter to microprocessor, if wanted to be work properly, must be reset before working . Is this right?
always @ (posedge clk)
begin
if (dir == 1'b1)
direction <= 1;
else
direction <= -1;
if (clr == 1'b1)
data_out <= {4{1'b0}};
else if (ce == 1'b1)
data_out <= data_out + direction;
else
data_out <= data_out;
end
endmodule
echo47 is right, you should use non-blocking "<=" assignments to synchronous signals. For digital design, use of nonblocking assignments instead of blocking assignments is highly recommended in places where concurrent data transfers take place after a common event. In such cases, blocking assignments can potentially cause race conditios because the final result depends on the order in which the assignments are evaluated.
On the downside, nonblocking assignments can potentially cause a degradation in the simulator performance and increase in memory usage.
I am very happy because the simulating result is right. The first code written by me using ifelse is also right . It can be chosed to work in count down or count up mode by manipulating dir input. I express many thanks to those who have been helping me!
Regards!!!