Trying to workout `ifdef syntax to assign a specific localparam in SystemVerilog - Page 1
Forum section: electronics / general

author: BrianHG
author_rank: Super Contributor
timestamp: August 23, 2021, 01:54:18 am
content: Ok, another workaround needed for Quartus and Modelsim FPGA function library stupid bugs...Here is what I have:Code:`ifdef Cyclone_IIlocalparam FPGA_FAMILY_altera_string_bug = "Cyclone II";`elsif Cyclone_IIIlocalparam FPGA_FAMILY_altera_string_bug = "Cyclone III";`elsif Cyclone_IV_Elocalparam FPGA_FAMILY_altera_string_bug = "Cyclone IV E";`elsif Cyclone_IV_GXlocalparam FPGA_FAMILY_altera_string_bug = "Cyclone IV GX";`elsif Cyclone_10_LPlocalparam FPGA_FAMILY_altera_string_bug = "Cyclone 10 LP";`elsif Arria_II_GXlocalparam FPGA_FAMILY_altera_string_bug = "Arria II GX";`elselocalparam FPGA_FAMILY_altera_string_bug = "MAX 10";`endifBecause of the way the altera PLL accepts strings in it's parameters, I need to select one of the above parameters. However, I do have a source 'FPGA_FAMILY' parameter string coming into this module. Altera/Modelsim wont properly accept it even if it have the correct name inside the brackets. It needs me to set one of the above.Is there a way to select one of those localparams based on my 'FPGA_FAMILY' parameter string or a `define?NO:Code:localparam FPGA_FAMILY_altera_string_bug = FPGA_FAMILY ;orCode:localparam string FPGA_FAMILY_altera_string_bug = FPGA_FAMILY ;Will not work. This is the stupid altera megafunction string bug.Only:Code:localparam FPGA_FAMILY_altera_string_bug = "Cyclone III";will work. Even this will not work:Code:localparam string FPGA_FAMILY_altera_string_bug = "Cyclone III";It is so F'ed up...If you think this is bad, wait until we have to deal with Cyclone V's PLL which has numeric specifications with a scale in the parameters as strings. And that one is nothing like Lattice ECP5's PLL which requires numeric specifications inserted into the /* comments */ inside the PLL function call to properly compile. I don't have a clue how to deal with that BS.

author: BrianHG
author_rank: Super Contributor
timestamp: August 23, 2021, 06:28:23 am
content: Anyone?My target solution would be:Code:if (FPGA_FAMILY!="Cyclone IV E") localparam FPGA_FAMILY_string = "Cyclone IV E";else if (FPGA_FAMILY!="MAX 10" ) localparam FPGA_FAMILY_string = "MAX 10";else localparam FPGA_FAMILY_string = "Cyclone III";But we all know you cannot assign localparams like that.Too bad I cant set a localparam FPGA_FAMILY_string = case(FPGA_FAMILY).Or, a 7 position switch...

author: BrianHG
author_rank: Super Contributor
timestamp: August 23, 2021, 06:32:47 am
content: OMG, this actually worked:Code:localparam FPGA_FAMILY_string = (FPGA_FAMILY=="MAX 10") ? "MAX 10" : "Cyclone III";Except, this is a switch with only an A/B entry.Does a switch format exist where I can have 8 selections?How would that look?

author: BrianHG
author_rank: Super Contributor
timestamp: August 23, 2021, 06:39:15 am
content: Ok, bask in the utter silliness I had to go through to make a phony altera string = a real string:Code:localparam FPGA_FAMILY_string = (FPGA_FAMILY=="MAX 10") ? "MAX 10" :(FPGA_FAMILY=="Cyclone III") ? "Cyclone III" :(FPGA_FAMILY=="Cyclone IV E") ? "Cyclone IV E" :(FPGA_FAMILY=="MAX 10") ? "MAX 10" :"Cyclone IV GX" ;Yes, this utter silliness functions...

author: dolbeau
author_rank: Regular Contributor
timestamp: August 23, 2021, 06:55:58 am
content: My solution would be a bit 'unclean' in the eyes of many, but it works for almost any languages; just start your build process with a pass of good ol'cpp(the C preprocessor). Portability is what it's for - it came originally with C, but it's just a simple-to-use conditional rewrite engine.e.g. just rename your file and do something likecpp -DFPGA_CYCLONE_III -P mydesign.v.pre > mydesign.v(you need -P to avoid the #line numbering).then in your code it become easy to adapt to any weirdness:Code:#if defined(FPGA_CYCLONE_III)localparam FPGA_FAMILY_string = "Cyclone III";PLL = (...)#elif defined(FPGA_MAX10)localparam FPGA_FAMILY_string = "MAX10";PLL = (...)#endifor you can segregate all the FPGA-specific parts in dedicated files and do something like:Code:#if defined(FPGA_CYCLONE_III)#include "mydesign_cycloneIII_spec.v.inc"#elif defined(FPGA_MAX10)#include "mydesign_max10_spec.v.inc"#endifor you can directly use the rewrite engine of cpp to do something like:Code:localparam FPGA_FAMILY_string = FPGA_FAMILY_NAME;and call cpp ascpp -DFPGA_FAMILY_NAME=\"Cyclone\ III\" -P mydesign.v.pre > mydesign.vcpp is almost everywhere and most people will understand the syntax. And in any was you can always ship all the pre-processed files alongside the actual source files for those without cpp (no idea how common it is in Windows, I'm thinking of Linux primarily I must say).

author: mrflibble
author_rank: Super Contributor
timestamp: September 01, 2021, 07:11:43 am
content: Quote from: BrianHG on August 23, 2021, 06:39:15 amThere's a certain retarded elegance to it. I like it already!But if you don't like that particular silliness, you could always go for a different flavor of silly. Like using a dummy function to assign your local friendly param maybe?Code:function string stringify_my_string_that_is_already_a_string(fpga_family_goes_here);$sformat(stringify_my_string_that_is_already_a_string, "%s", fpga_family_goes_here);endfunctionlocalparam FPGA_FAMILY_string = stringify_my_string_that_is_already_a_string(FPGA_FAMILY);If I understand the problem correctly (big if!) then the above has a most definite non-zero chance of working...