Author Topic: Verilog coding style guidance needed  (Read 1026 times)

0 Members and 1 Guest are viewing this topic.

Offline blackbladeTopic starter

  • Newbie
  • Posts: 6
  • Country: in
Verilog coding style guidance needed
« on: October 26, 2022, 03:15:44 pm »
I want to do 3 operations in 3 different clock cycles, data in, addition and multiplication and repeat. Can anyone give me a Verilog template to implement the above? Something like this pseudocode:

Code: [Select]
always @(posedge 1st clk or negedge rst)
if (~rst)
a <= 0;
else
a <= data_in;

always @(posedge 2nd clk or negedge rst)
if (~rst)
b <= 0;
else
b <= a + c;

always @(posedge 3rd clk or negedge rst)
if (~rst)
d <= 0;
else
d <= a * b;
 

Offline fourfathom

  • Super Contributor
  • ***
  • Posts: 1969
  • Country: us
Re: Verilog coding style guidance needed
« Reply #1 on: October 26, 2022, 03:36:25 pm »
What is the phase relationship of these three clocks? 

If these clocks are all derived from one common clock then (depending on speed) your task is probably easy.  If these are random clocks then we need to have a long discussion about synchronization across clock domains.
We'll search out every place a sick, twisted, solitary misfit might run to! -- I'll start with Radio Shack.
 
The following users thanked this post: blackblade

Offline woofy

  • Frequent Contributor
  • **
  • Posts: 367
  • Country: gb
    • Woofys Place
Re: Verilog coding style guidance needed
« Reply #2 on: October 26, 2022, 03:44:59 pm »
Assuming 1st, 2nd and 3rd are sequential states of the same clock, then you need a state machine.
google "verilog state" and you'll get some useful results.
 
The following users thanked this post: blackblade

Offline blackbladeTopic starter

  • Newbie
  • Posts: 6
  • Country: in
Re: Verilog coding style guidance needed
« Reply #3 on: October 26, 2022, 04:29:33 pm »
What is the phase relationship of these three clocks? 

If these clocks are all derived from one common clock then (depending on speed) your task is probably easy.  If these are random clocks then we need to have a long discussion about synchronization across clock domains.

Yeah, it's one common clock.
 

Offline blackbladeTopic starter

  • Newbie
  • Posts: 6
  • Country: in
Re: Verilog coding style guidance needed
« Reply #4 on: October 26, 2022, 04:30:11 pm »
Assuming 1st, 2nd and 3rd are sequential states of the same clock, then you need a state machine.
google "verilog state" and you'll get some useful results.

What do you think would be better? A counter or a state machine?
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 8089
  • Country: ca
Re: Verilog coding style guidance needed
« Reply #5 on: October 26, 2022, 07:08:34 pm »
Here is the dumb method using simple counter and an if() to run multiple actions in order, in this case to consolidate a single multiplier and single adder to accumulate a number of math operations on many input sources together:

Audio mixer-filter lines 73-170

(The important part is highlighted in yellow at the bottom..)

Here is a complete lesson I have given another user on how to setup a state machine using the case() statement:

https://www.eevblog.com/forum/fpga/fpga-output-not-consistent-with-modelsim/msg4077400/#msg4077400

You will also find attached .zip files with Modelsim setups so you can simulate to see how the code works and how to monitor/debug your state machine.

(It boils down to a case(program_counter), then program_counter = 0 on reset, =+1 the rest of the time while inside the 'case' positions, you can force the program_counter to a unique position or hold it's current position based on an if(), sorta like a goto command.)
« Last Edit: October 26, 2022, 07:22:21 pm by BrianHG »
 
The following users thanked this post: blackblade

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 3889
  • Country: us
Re: Verilog coding style guidance needed
« Reply #6 on: October 26, 2022, 09:01:55 pm »
Is this pipelined or an operation that takes 3 cycles during which the logic is "busy"?

For pipelined it's easy:

Code: [Select]
always @(posedge clk or negedge rstn) begin
if (~rstn) begin
a <= 0;
                a_d <= 0;
                b <= 0;
                d <= 0;
end else begin
                // Stage 1
a <= data_in;

                // Stage 2
                a_d <= a;   // delay a one cycle to use in stage 3
b <= a + c; // Where did c come from?  Is it constant?

                // Stage 3
                d <= a_d * b
         end
end

If I get any more complicated than this I use a naming convention where all signals are labeled with the stage of the pipeline where they are assigned so that you can quickly verify that all of the assignments for stage N only depend on data from stage N-1.
 
The following users thanked this post: blackblade


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf