Adder Implementation in Verilog

digital-logicverilogxilinx

In my digital electronics project I need to calculate dot product of two vectors a and b (256 length of each). Following the basic concept, I need to calculate \$ \sum_{k=1}^{256} a_kb_k \$. Each element of vectors is 4-bit binary number. This doesn't seems to a difficult task in verilog where I can implement this as in XILINX ISE.

sum=0;

for(i=0,i<256,i=i+1)

sum=sum+ \$a_ib_i \$

Furthur I need to implement this on FPGA. But here I got confused in two concept:

  1. Do I need to write the module for 'full adder' and added the inputs previous sum and \$a_i b_i\$ bit by bit and store output in sum. Or simply xilinx will automatically implement the full adder during synthsize.

  2. Is it valid to use for loop insted of writing always@(condition) beacause on FPGA we have clock as a control signal. Or in this case also xilinx itself will implement/control the for loop by clock signal.

Best Answer

Do I need to write the module for 'full adder' and added the inputs previous sum and aibi bit by bit and store output in sum. Or simply xilinx will automatically implement the full adder during synthsize.

You can expect any decent verilog synthesis tool to handle addition and subtraction operators. Most modern ones will also handle multiplication operators. Support for division operators is less common (and when they are implemented they tend to synthisize to very large slow blocks).

Most of the time it's fine to just use the addition, subtraction and multiplication operators. For really big adders and multipliers it is sometimes nessacery to break them up into smaller units to meet timing.

Is it valid to use for loop insted of writing always@(condition) beacause on FPGA we have clock as a control signal. Or in this case also xilinx itself will implement/control the for loop by clock signal.

synthesis tools will not turn a loop like that into sequential logic. Instead they will attempt to unroll it and implement it combinatorially. The result will be something that is both very big and very slow.