Re: two dimensional input port in verilog
Thanks
Didnt use the Macro as got afraid it might be used somewhere else in the design but the idea is nice and i just packed and unpacked each array manually.
Glad it was of some use.
You say you got afraid
it might be used somewhere else.. What "it"? The macro names PACK_ARRAY and UNPACK_ARRAY?
If so, that's a valid concern since the names I chose are not super unique. I chose them to be somewhat intuitive.
There is something that you can do to at least make sure that
your part of the project is clean.
file "util/array_pack_unpack.v":
`ifndef ARRAY_PACK_UNPACK_V
`ifdef PACK_ARRAY
$finish; // macro PACK_ARRAY already exists. refusing to redefine.
`endif
`ifdef UNPACK_ARRAY
$finish; // macro UNPACK_ARRAY already exists. refusing to redefine.
`endif
`define ARRAY_PACK_UNPACK_V 1
`define PACK_ARRAY(PK_WIDTH,PK_LEN,PK_SRC,PK_DEST) genvar pk_idx; generate for (pk_idx=0; pk_idx<(PK_LEN); pk_idx=pk_idx+1) begin; assign PK_DEST[((PK_WIDTH)*pk_idx+((PK_WIDTH)-1))
(PK_WIDTH)*pk_idx)] = PK_SRC[pk_idx][((PK_WIDTH)-1):0]; end; endgenerate
`define UNPACK_ARRAY(PK_WIDTH,PK_LEN,PK_DEST,PK_SRC) genvar unpk_idx; generate for (unpk_idx=0; unpk_idx<(PK_LEN); unpk_idx=unpk_idx+1) begin; assign PK_DEST[unpk_idx][((PK_WIDTH)-1):0] = PK_SRC[((PK_WIDTH)*unpk_idx+(PK_WIDTH-1))
(PK_WIDTH)*unpk_idx)]; end; endgenerate
`endif
Then in your code:
Code:
`include "util/array_pack_unpack.v"
Incidentally I've been using this for some time now and no problems so far.
It's a bit verbose due to all the sanity checks, but at least now your code will play nice with other stuff.
Note that I said
your part of the project is clean. If your project also contains Other Peoples Code [tm], then you can only do so much. Suppose your includes come first, and then some infidel's code that is parsed later overwrites your macro ... The infidel's macro will redefine your macro. His stuff will work, and yours will not.
But that sort of thing is no reason to not use macro's. Why? Because if you want to take that line of reasoning you can never do anything when you don't know 100% of the entire project. What you can do is a few simple checks to make sure the macros you want to use are not already taken by someone else.
Simply do:
Code:
grep -r '.*define.*PACK_ARRAY' your_source_dir
or the windoze Find equivalent. Then you know if anywhere in the project-you-dont-quite-know it's used.
So far I have not had any name collisions, so this method is anecdotally safe to use.
And before some clever soul points it out ... the use of $finish up there is not 100% pedantic person proof. By the language standard you are not allowed to use a $finish there.
However, the sole purpose of that line is to 1) throw an error, and 2) throw an error that you can easily translate to "what the hell is happening?". And it does just that. For example from ISE if I do synthesize, when the "PACK_ARRAY" macro already exists (because I already defined it on purpose to test if my macro handles this cleanly) I get an error on that $finish line. This gives me enough info to know what's going on. I even added some comments.
On a related note I've just played around a little with systemverilog for synthesis. Precisely because of plain verilog deficiencies like this. In verilog you have to resort to hacky macros like this, and in systemverilog things are a whole lot cleaner. Too bad xilinx ISE does not support systemverilog, despite their periodic claim of "we will support it Real-Soon-Now".
Does anyone familiar with the Altera tools know if they support systemverilog for synthesis + simulation? If so, is it any good?