UCLONG / NetEmulation

Software Simulation and Hardware Synthesis of Electrical and Optical Interconnection Networks
GNU General Public License v3.0
9 stars 6 forks source link

Log2 is allready defined in this scope? #10

Closed DannyNicholls closed 10 years ago

DannyNicholls commented 10 years ago

Did you work out an answer to this? I just tried synthesising some code and got the error.

DannyNicholls commented 10 years ago

I have a long list of other errors to get to first though!

DannyNicholls commented 10 years ago

Ok I got it, it does what it says on the tin. If `include config.sv is in multiple files it will try and instantiate the log2 definition multiple times, hence error.

DannyNicholls commented 10 years ago

Tell a lie. Fault is still there!

BorisDosen commented 10 years ago

The only thing I did with this is wrote another log2 function that can be used if the defined number of ports is not a multiple of 2. It will work for simulation, but not for synthesis.I am looking into this.

DannyNicholls commented 10 years ago

Ok, no rush realy because I got it to synthesise, it was instantiating it multiple times somehow so I stopped using config.sv and functions.sv and wrote it myself. I will have a better think about it at some point.

DannyNicholls commented 10 years ago

My single cycle network ran at nearly 70 MHz. Well chuffed!

DannyNicholls commented 10 years ago

Oh btw, did you not check the log2 function I sent you on fb? I can't remember if I wrote it or copied it but it's pretty simple.

function int log2(input int n);
  begin
    log2 = 0;         // log2 is zero to start
    n--;                 // decrement 'n'
    while (n > 0)    // While n is greater than 0
      begin
        log2++;       // Increment 'log2'
        n >>= 1;     // Bitwise shift 'n' to the right by one position
      end
  end
endfunction
BorisDosen commented 10 years ago

I have tried that in the past, but it kept giving me the same error as the other code: "Function identifier is already defined in the present scope"

BorisDosen commented 10 years ago

I know it is nice when you have a function defined and you can just call it and don't worry about instantiation (if that is a word) and that's probably a preferred way of doing it. I am just wondering if there are any other reasons why log2 couldn't be defined as a regular module rather than a function?

DannyNicholls commented 10 years ago

There is no reason, the function is essentially describing combinational logic. But defining it as a function makes it easy to call and use.

The problem is not to do with the log2 code (although I have only checked my version of it), I think it is to do with the rest of the code trying to define the function log2 multiple times, hence the error 'is already defined'.

i.e. As functions.sv is included by the file config.sv, every time you include config.sv it includes functions.sv. When you include a file, it is the same as typing the contents in to the source. Thus, if you have more than one file including config.sv you also have more than one file including functions.sv, and when the compiler comes across the second instance, it throws an error as it has already compiled a function called log2.

I could be wrong, but I changed it so that I was only calling it once, and it fixed the problem.

DannyNicholls commented 10 years ago

Perhaps try different methods of declaring the function and see how that works. i.e move the include functions.sv from config.sv to the file that uses the function.

pmwatts commented 10 years ago

I have also found this irritating. The problem is that modelsim requires the define config.sv in every file, i.e. it compiles each file separately before putting the system together at the end. On the other hand, most synthesisers (including synopsys and Xilinx) compile retaining the definitions that have been made in other files, so stop with these errors. I had hoped to find a way of automating this using thedefine SIMULATION in the config file but I have not found a good way so far.

The functions.sv file is a very old piece of code which could be eliminated. On the other hand, maybe it would be more logical to keep functions in a separate location.

Phil


From: Danny Nicholls notifications@github.com Sent: 17 November 2013 12:42 To: DannyNicholls/NetEmulation Subject: Re: [NetEmulation] Log2 is allready defined in this scope? (#10)

Perhaps try different methods of declaring the function and see how that works. i.e move the include functions.sv from config.sv to the file that uses the function.

Reply to this email directly or view it on GitHubhttps://github.com/DannyNicholls/NetEmulation/issues/10#issuecomment-28647812.

BorisDosen commented 10 years ago

I got rid of that error. thanks for the tip

DannyNicholls commented 10 years ago

Ok, so the definitive answer... I hope! As suspected, the error message is correct, and this is definitely due to the log2 function being declared multiple times. To fix this ...

1) Make sure only one module declares the function. If the function is called from an included functions.sv file, then make sure only one module contains the line `include "functions.sv".

2) If the function is declared in a separate functions.sv file, do not add this file to the Quartus project.

Note: If `include "functions.sv" is declared once in config.sv, but then config.sv is included in several files, this will effectively break rule 1.

pmwatts commented 10 years ago

This issue is a pain as I think I have written before. Some synthesisers/simulators compile each file separately (so require the config file to be included in every file). Others compile them together (so give errors if anything is declared twice).

One solution which may work for NetEmulation in which the top level module is both a simulation test bench and a synthesisable unit is to add the following at the top of netemulation.sv:

include “config.sv” (log2 is defined in here or included in functions.sv) include “/network.sv”

Then at the top of network.sv, include all the sub-module files. This is like compiling everything in a single file (while also keeping the modules in separate files for clarity). You just need to make sure that you point to the correct network.sv if you change from electrical to optical etc.

See if this works!

Phil

From: Danny Nicholls [mailto:notifications@github.com] Sent: 20 February 2014 17:14 To: DannyNicholls/NetEmulation Cc: Watts, Philip Subject: Re: [NetEmulation] Log2 is allready defined in this scope? (#10)

Ok, so the definitive answer! ... I hope! As suspected, the error message is correct, and this is definitely due to the 'log2' function being declared multiple times. To fix this ...

1) Make sure only one module declares the function. If the function is called from an included 'functions.sv' file, then make sure only one module contains the line '''include "functions.sv"''.

2) If the function is declared in a separate 'functions.sv' file, do not add this file to the Quartus project.

Note: If '''include "functions.sv"'' is declared once in 'config.sv', but then 'config.sv' is included in several files, this will effectively break rule 1.

— Reply to this email directly or view it on GitHubhttps://github.com/DannyNicholls/NetEmulation/issues/10#issuecomment-35644892.