Verilog coding style guidance needed - Page 1
Forum section: electronics / general

author: blackblade
author_rank: Newbie
timestamp: October 26, 2022, 03:15:44 pm
content: 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:always @(posedge 1st clk or negedge rst)if (~rst)a <= 0;elsea <= data_in;always @(posedge 2nd clk or negedge rst)if (~rst)b <= 0;elseb <= a + c;always @(posedge 3rd clk or negedge rst)if (~rst)d <= 0;elsed <= a * b;

author: fourfathom
author_rank: Super Contributor
timestamp: October 26, 2022, 03:36:25 pm
content: 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.

author: woofy
author_rank: Frequent Contributor
timestamp: October 26, 2022, 03:44:59 pm
content: 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.

author: blackblade
author_rank: Newbie
timestamp: October 26, 2022, 04:29:33 pm
content: Quote from: fourfathom on October 26, 2022, 03:36:25 pmYeah, it's one common clock.

author: blackblade
author_rank: Newbie
timestamp: October 26, 2022, 04:30:11 pm
content: Quote from: woofy on October 26, 2022, 03:44:59 pmWhat do you think would be better? A counter or a state machine?

author: BrianHG
author_rank: Super Contributor
timestamp: October 26, 2022, 07:08:34 pm
content: 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/#msg4077400You 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.)

author: ejeffrey
author_rank: Super Contributor
timestamp: October 26, 2022, 09:01:55 pm
content: Is this pipelined or an operation that takes 3 cycles during which the logic is "busy"?For pipelined it's easy:Code:always @(posedge clk or negedge rstn) beginif (~rstn) begina <= 0;a_d <= 0;b <= 0;d <= 0;end else begin// Stage 1a <= data_in;// Stage 2a_d <= a; // delay a one cycle to use in stage 3b <= a + c; // Where did c come from? Is it constant?// Stage 3d <= a_d * bendendIf 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.