LogIn E-mail
¼³°èÀ̾߱â
[Verilog]vstring
# 41 JMJS    17.9.27 15:36

Strings in Verilog
A quick reference on a couple of ways to manipulate strings in Verilog HDL.
Declaration
There is no string data type is Verilog, so use the following to declare a register to hold a string.

reg[n*8:0] string;

Where the maximum length of the string is n characters. It takes 8 bits to store each character.
Usage
string = "Hello World"; $sdisplay("%s", string); // will get "Hello World"

Also, normal concatination will work, for the most part. So something like:

string = {"/path/to/file","/","filename",".txt"};

will result in a string useful to pass to $open to open a file.

For anything more advanced, a very good function is

$sformat(string,"%s %d %s",str1,num,str2);

would produce the string with the three varibles concatinated together and spaces in between.

//------------------------------------------------------------------------------------------------------------------------------------------

module vstring ();

function [10 *8 -1:0] int2str;
input [31:0] inum;
begin
        int2str = 0;
        while(inum > 0) begin
                int2str = {"0" + (inum - (inum/10)*10), int2str[79:8]};
                inum = inum / 10;
        end
end
endfunction

function [256 *8 -1:0] strfilter;
input [256 *8 -1:0] iline;
input [7:0] cnum;
integer i;
begin
        strfilter = 0;
        for(i=0;(i<256)&&(cnum>0); i=i+1) begin
                if(iline[2047:2040] != 0) begin
                        strfilter = {strfilter, iline[2047:2040]};
                        cnum = cnum -1;
                end
                iline = iline<<8;
        end
        while(cnum>0) begin
                strfilter = {strfilter, 8'h0};
                cnum = cnum -1;
        end
end
endfunction

`define ULINE "./cpu/u"
reg [32 *8 -1:0] num32;
reg [ 2 *8 -1:0] num2;
integer fp;
integer i;
initial begin
        for(i=8;i<12;i=i+1) begin
                num2 = "0" +i -(i/10)*10;
                if(i/10) num2[15:8] = "0" + i/10;
                num32 = strfilter({`ULINE, num2, "_A_",int2str(i),".txt"},32);
                $display("%s",num32);
                fp = $fopen(num32);
                $fwrite(fp,"%s",num32);
                $fclose(fp);
        end
end
endmodule

°Ô½Ã¹°: 96 °Ç, ÇöÀç: 1 / 1 ÂÊ
¹øÈ£ Á¦       ¸ñ ÀÛ¼ºÀÚ µî·ÏÀÏ ¹æ¹®
98  interface JMJS 25.1.20 278
97  test plusargs value plusargs JMJS 24.9.5 318
96  color text JMJS 24.7.13 337
95  draw_hexa.v JMJS 10.6.17 2519
94  jmjsxram3.v JMJS 10.4.9 2324
93  Verilog document JMJS 11.1.24 2941
92  [verilog]o=(c1)? (c2)? 0:1 : (c3)? 2:3; JMJS 09.3.31 2509
91  [verilog]forever, repeat, strobe, realtime, ... JMJS 09.7.6 3935
90  gtkwave PC version JMJS 09.3.30 2303
89  ncsim option example JMJS 08.12.1 4670
88  [¿µ»ó]keywords for web search JMJS 08.12.1 2297
87  [Verilog]fdisplay fopen fscanf JMJS 11.1.24 6514
86  ncverilog option example JMJS 10.6.8 8137
85  [Verilog]Latch example JMJS 08.12.1 2884
84  Pad verilog example JMJS 01.3.16 4802
83  [ModelSim] vector JMJS 01.3.16 2492
82  RTL Code ºÐ¼®¼ø¼­ JMJS 09.4.29 2758
81  [temp]PIPE JMJS 08.10.2 2148
80  [temp]always-forever ¹«ÇÑ·çÇÁ JMJS 08.10.2 2239
79  YCbCr2RGB.v JMJS 10.5.12 2420
78  [VHDL]rom64x8 JMJS 09.3.27 1993
77  [function]vector_compare JMJS 02.6.19 1901
76  [function]vector2integer JMJS 02.6.19 2069
75  [VHDL]ram8x4x8 JMJS 08.12.1 1868
74  [¿¹]shift JMJS 02.6.19 2294
73  test JMJS 09.7.20 2102
72  test JMJS 09.7.20 1770
71  test JMJS 09.7.20 1811
70  test JMJS 09.7.20 1910
69  test JMJS 09.7.20 1954
68  test JMJS 09.7.20 1888
67  test JMJS 09.7.20 1823
66  test JMJS 09.7.20 1790
65  test JMJS 09.7.20 1886
64  test JMJS 09.7.20 2097
63  test JMJS 09.7.20 2118
62  test JMJS 09.7.20 2038
61  VHDLÀÇ ¿¬»êÀÚ ¿ì¼±¼øÀ§ JMJS 09.7.20 3837
60  test JMJS 09.7.20 1704
59  test JMJS 09.7.20 1906
58  test JMJS 09.7.20 1866
57  test JMJS 09.7.20 1829
56  test JMJS 09.7.20 1872
55  verilog Çаú »ùÇð­ÀÇ JMJS 16.5.30 2393
54  [verilog]create_generated_clock JMJS 15.4.28 2374
53  [Verilog]JDIFF JMJS 14.7.4 1675
52  [verilog]parameter definition JMJS 14.3.5 1978
51  [verilog]sformat fopen fscanf fwrite fclose JMJS 12.1.31 4922
50  Verilog File I/0,Verilog file handling JMJS 12.1.30 2639
49  Verdi JMJS 10.4.22 3446
48  draw hexa JMJS 10.4.9 2008
47  asfifo - Async FIFO JMJS 10.4.8 1877
46  VHDLÀ» ÀÌ¿ëÇÑ È¸·Î¼³°èÀÇ ÀåÁ¡ JMJS 02.3.14 3540
45  synplify batch JMJS 10.3.8 2680
44  ÀüÀڽðè Type A JMJS 08.11.28 2182
43  I2C Webpage JMJS 08.2.25 2017
42  PC¿¡¼­ °£´ÜÈ÷ Verilog ½ÇÇàÇØº¸±â (Icarus Verilog) JMJS 13.1.14 6173
41  [Verilog]vstring JMJS 17.9.27 2241
40  Riviera Simple Case JMJS 09.4.29 3344
39  [VHDL]DES Example JMJS 07.6.15 3173
38  [verilog]RAM example JMJS 09.6.5 2934
37  ROM example [VerilogHDL, RTL] JMJS 04.5.27 2208
36  Jamie's VHDL Handbook JMJS 08.11.28 2866
35  Dualport RAM example [VerilogHDL, RTL] JMJS 04.5.27 3479
34  RTL Job JMJS 09.4.29 2347
33  [VHDL]type example - package TYPES JMJS 06.2.2 1929
32  [verilog]`define `ifdef `elsif `else `endif ... JMJS 10.5.11 9541
30  [verilog]array_module JMJS 05.12.8 2449
29  [verilog-2001]generate JMJS 05.12.8 3567
28  protected JMJS 05.11.18 2228
27  design¿¡ latch°¡ ÀÖÀ¸¸é ¾ÈµÇ³ª¿ä? JMJS 09.7.20 3006
26  busÀÇ µ¥ÀÌŸ¸¦ °¢ bitº°·Î Ãâ·ÂÇÏ´Â ¹æ¹ýÀº? JMJS 04.11.9 1988
25  component¸¦ »ý¼ºÇؼ­ ´Ù¸¥ °÷¿¡¼­ È£ÃâÇÏ´Â ¹æ¹ý JMJS 04.11.4 2637
23  Array Of Array JMJS 04.8.16 2153
22  dumpfile, dumpvars JMJS 04.7.19 3799
21  Vending Machine Jamie 02.12.16 10246
20  Mini Vending Machine1 Jamie 02.12.10 7132
19  Mini Vending Machine Jamie 02.12.6 9981
18  Key Jamie 02.11.29 5146
17  Stop Watch Jamie 02.11.25 5769
16  Mealy Machine Jamie 02.8.29 6904
15  Moore Machine Jamie 02.8.29 18215
14  Up Down Counter Jamie 02.8.29 4241
13  Up Counter Jamie 02.8.29 2930
12  Edge Detecter Jamie 02.8.29 3157
11  Concept4 Jamie 02.8.28 2197
10  Concept3 Jamie 02.8.28 2241
9  Concept2_1 Jamie 02.8.28 2123
8  Concept2 Jamie 02.8.28 2219
7  Concept1 Jamie 02.8.26 2336
6  Tri State Buffer Jamie 02.8.26 3756
5  8x3 Encoder Jamie 02.8.28 4361
4  3x8 Decoder Jamie 02.8.28 4000
3  4bit Comparator Jamie 02.8.26 3382
2  °¡À§ ¹ÙÀ§ º¸ °ÔÀÓ Jamie 02.8.26 5626
1  Two Input Logic Jamie 02.8.26 2617
[1]