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.

[SOLVED] verilog code for addition of contents in the memory

Status
Not open for further replies.

ecasha

Junior Member level 2
Joined
Feb 28, 2017
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
197
Code:
module opr(clock,di,dread,adr,in1,sum1,cmct);
input clock;
input [0:5] di;
reg [3:0] pn1;
input [3:0] in1;
output [0:5] dread;
input [1:0] adr;
 reg [3:0] k;
output [5:0]sum1;
reg [5:0]sum1;
output [3:0] cmct;
reg [3:0] cmct;
integer i,j;
 reg [0:5] ram1[0:3];
 reg en=1;
initial begin
 for (i=0; i<=3; i=i+1)
      begin
        ram1[i] <= 2'b00;   // memory initialization
      end				
end
assign  dread = ram1[adr];
   always @(posedge clock)
        begin
              if(en) begin        //storing   in memory
                 ram1[adr] <= di;
				 end
        end
initial  begin sum1=0; k=4'bzzzz; end
always @ (in1) begin
 sum1=0;
   for (i=0; i<=3; i=i+1)begin
	   for(j=0; j<6; j=j+1)begin      // calculating number of 1's
        sum1= sum1+ram1[i][j];
	
      end
  end
  
  if (sum1 >=6'b000110) begin
  pn1 = in1-4'b0001;
     cmct= pn1;
      k=pn1;	  end 
  else  cmct=k;	  //storing counter value in an array
  end 
  
endmodule


I have written code for storing the results in memory and calculating the number of one's.in1 input is counter input. counter counts after every 4 clock pulses.For every counter input (i.e in1)number of one's in memory is calculated.if that sum is greater than 6 counter value is stored in an array. I have simulated code.There are no errors. But when i dump on vertex-5 FPGA kit,Correct values are not coming.Is there any problem in this code? how should I find it? Please Help me...(No sysnthesis errors)
 

for (i=0; i<=3; i=i+1)begin
for(j=0; j<6; j=j+1)begin // calculating number of 1's
sum1= sum1+ram1[j];
end

Doesn't work for FPGA block RAM which can be only accessed one memory address per clock cycle (or two addresses using dual-port RAM).
 

Basically don't use for loops until you fully understand they are for replicating logic not iterating through code. SW for loops iterating over some code, HW for loops make copies of logic as they are fully unrolled at compile time.
 

My first thought is that the combinatorial always block doesn't have all inputs in the sensitivity list. k has Z values, but the tools probably will resolve these to 0 for this case.

The for loops are probably fine. The code describes a 24b population count followed by an inequality compare with constant. I'm not sure if the tools will pick up the adder-tree correctly or if your version of ISE does a good job optimizing adder-trees in this case.


This is the type of code where I would make sure to inspect the resulting logic. The last I tried this problem, maybe five years ago, the for-loop did not work better than a basic optimized implementation nor a more highly optimized implementation. As you are using a V5, which has tools from that era, I would suspect the for-loop could have timing issues depending on the clock rate.
 

Then how do I write code. Actually, I want to calculate a number of one's in every 4 locations of memory. i.e from 0 to 3 then 4 to 7.
 

Then how do I write code. Actually, I want to calculate a number of one's in every 4 locations of memory. i.e from 0 to 3 then 4 to 7.

You dont start with code you start by knowing what the circuit looks like that does what you want. Once you have that you translate it into VHDL or Verilog. VHDL/Verilog describe hardware.

In your case you need RAM, a counter, compare to 3, an adder, etc
 

So many things you have to learn. Please start by understanding that initial statements do not generate hardware, they are only simulation assets.

Then study what sensitivity lists are, and how combinational and sequential logic are entirely different for that matter.

Finally, learn about memories and how to model them. Usually you can only get one address at a time. If you need concurrent access to all 'addresses', then your solution must use flops.
 

There is an assumption being made that RAM must be used to solve this problem. A 24 bit ram on a Virtex-5 FPGA specifically. The scale of the problem allows registers to be entirely viable.
 

I find this problem to be interesting. The OP's problem may be more on the simulation mismatch or potential lack of timing constraints or similar issues. I don't believe the OP is an experienced RTL developer, so I don't attribute the coding style to an insight into how the synthesizer works in corner cases.

That said, the code is not obviously "bad" -- except for the intentional simulation mismatch due to the sensitivity list.

The code potentially infers a 24 bit population count -- a rarely used operation that may not be well optimized -- followed/joined by an inequality comparison with constant.

From my experiments, popcount was not well optimized in even the single for-loop style in the ISE era. This doesn't make the coding style fundamentally bad. Just not ideally implemented -- five years ago.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top