Too long to read
Examples are as follows:
% Port definition
fprintf(fID,[...
'\n'...
'input clk;' '\n'...
'input rst_n;' '\n'...
'input [%d:0] msg;' '\n'...%row-1
'input in_valid;' '\n'...
'output reg [%d:0] codeword;' '\n'...%col-1
'output reg out_valid;' '\n'...
],row-1,col-1);
Add brackets; divide a long string into multiple substrings; at least one space between substrings; use ...
for line breaks
text
I’ve been working on the design recently, and one of them needs to write a very troublesome Verilog file. It is troublesome but not complicated. The encoder of LDPC is an input signal with a width of more than 200 bits and an output signal with a width of more than 500 bits. Each output bit is obtained by the same operation of some input bits, and the corresponding relationship is represented by a matrix with a width of more than 200 * 500. If you want to write Verilog, it will be dead. If you write generation logic in Verilog, it will take up a lot of memory to store the generation matrix, and the logic is difficult to write. So I thought of using matlab code to generate Verilog code.
There is fprintf function in MATLAB to output data to file.
Take a chestnut, fprintf (FID, 'hello world! ");
can put” Hello world! ” Output to the file corresponding to FID.
The function to open a file is FID = fopen ('filepath ')/ filename.postfix ','w');
where w means write, and if it is read, it will be changed to R. for more usage, please help fopen
So the problem is, if I output every line of Verilog code with one fprintf, then my. M file will be full of a lot of fprintf, and I can’t bear this kind of highly repetitive code. But if I write each function block in an fprintf, the readability is really much higher, but the fprintf line will be very long, also can’t bear, and the modification will be very painful!
So I went to see if there was a newline character like ‘\’ in a Linux script, and let the interpreter execute multiple lines of code as one line.
There is this newline character,
...
← three points are the newline character in MATLAB. You can connect the end of the row with the head of the next row.
But another problem comes out. This newline character can’t be used in the middle of a string. I regard a whole block of code as a string. If a newline character is added in the middle, the interpreter will output the newline character as the content to be output.
The parameters of fprintf are file handler, output array and output data (if any).
Take chestnuts as an example: fprintf (FID, 'data1% D, n', data1);
the string in the middle will be regarded as an array with only one element.
So the solution is to add a few more elements to the middle array to split the long string and add line breaks between the substrings.
Take chestnuts:
% Port definition
fprintf(fID,[...
'\n'...
'input clk;' '\n'...
'input rst_n;' '\n'...
'input [%d:0] msg;' '\n'...%row-1
'input in_valid;' '\n'...
'output reg [%d:0] codeword;' '\n'...%col-1
'output reg out_valid;' '\n'...
],row-1,col-1);
Each small string enclosed in single quotation marks will be interpreted as an element of the output array. The substrings are separated by spaces (multiple, at least one) or commas, that is, a single line matrix (array).
Bracket is indispensable, which is the identification of combining each substring into an array.
In addition, Verilog often needs to output things like 3b'101
, and single quotation marks are used as the start / stop characters of elements. If you want to output single quotation marks, you need to type two single quotation marks in succession. For example: fprintf (FID, 'outdata & lt; = 3' 'B101')
. This is also why there should be at least one space between two substrings. If there is no space, it will be connected and there will be a single quotation mark in the middle.