Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

explanation of how to translate C code into verilog

keikaku

Newbie
Newbie level 3
Joined
Mar 5, 2024
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
43
I am brand new to Verilog. I am taking a course. If I am in the wrong place, please let me know.

I am trying to understand how to translate the following C code into Verilog. I just do not get how. Any help is appreciated.

x = 0;
for (i=0; i < 3; i++ ){
x = 2x + 1;
}

Any explanation of how to this can be done ? I suppose this is simple code, but Verilog seems difficult.
 
That should be trivial, a simple internet search will give you the results, you can do that on your own!

The bigger question, do you want to compute the value of x (which I am assuming would be of type reg), on every rising edge of the clock or you want to have the computation under 1 clock cycle?
 
Formally, the code can be converted to verilog by replacing { } with begin end.
An iteration loop in Verilog describes however parallel logic, respectively it will be calculated at once. Otherwise you have to design a clocked sequence (finite state machine).
As the given code has constant parameters, it will be reduced by Verilog synthesis tool to a constant assignment x = 7.
 
Hi,

while it is possible to translate C into verilog ... I still recommend to remind that
* C is a software language, where line after line is processed
* Verilog is a hardware description language, where all lines are processed in parallel

Thus C and Verilog have rather different timing output.
In C variables are updated line after line. The variable values are valid (at least for a short amount of time) until they become overwritten by another line .. some time later.
In Verilog this is different, because a variable value (result of a code line) may not be valid (not for a tiny moment), because it may be over ruled by a different line of code.

Verilog: Think as hardware. Don´t think as software at all.

Klaus
 
you are right, it should be "trivial." but if i knew how to do it i would not be asking this question here. while i do see there are conversion sites, they do not explain the process.
 
Formally, the code can be converted to verilog by replacing { } with begin end.
An iteration loop in Verilog describes however parallel logic, respectively it will be calculated at once. Otherwise you have to design a clocked sequence (finite state machine).
As the given code has constant parameters, it will be reduced by Verilog synthesis tool to a constant assignment x = 7.
thanks for the response. i have been attempting to use the EDA Playground site to run the code using the "begin" and "end". i am not understanding what could be the issue. i attached a screenshot.
 

Attachments

  • Screenshot from 2024-03-05 22-38-12.png
    Screenshot from 2024-03-05 22-38-12.png
    81.1 KB · Views: 147
I'm not familiar with Eda playground, but your code is incomplete, it misses module header and end, also variable declaration.
thanks for the response. i added the header and the endmodule in addition to declaring the input. i am getting a different type of error now. i attached a screenshot.
 

Attachments

  • Screenshot from 2024-03-05 23-43-35.png
    Screenshot from 2024-03-05 23-43-35.png
    108.1 KB · Views: 155
you are right, it should be "trivial." but if i knew how to do it i would not be asking this question here. while i do see there are conversion sites, they do not explain the process.
You still did not answer the fundamental question on the number of clock cycles, which is important to determine the final Verilog code!
Hence I did not give out the solution. Nevertheless we keep this for the future!

You have shown us an EDA playground screenshot with just 3 lines of code and nothing more..............
For this I conclude that first of all you need to use a good Verilog book or a good online tutorial, learn at least the basics and then come back here.
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top