Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to define global variable in verilog?

Status
Not open for further replies.

saurabhs

Newbie level 4
Joined
Aug 6, 2009
Messages
5
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Location
Varanasi
Activity points
1,307
Hi all
I wanna define global variable in verilog so that i can use them in different module.
How it can be done.
Thanks in advance.
 

skyfaye

Member level 5
Joined
Feb 25, 2008
Messages
82
Helped
25
Reputation
50
Reaction score
11
Trophy points
1,288
Activity points
1,876
Re: Global Variable in Verilog

With verilog, pretty much everything's "global". You can use hierarchical reference to access any variable in any module.

For example:

module glb_var_mod; // define all global vars in this module
reg glb_x = 1'b1;
endmodule

module dut;
glb_var_mod.glb_x = 1'b0; // change value of a global var
endmodule

- Hung
 

Jack// ani

Advanced Member level 3
Joined
Dec 2, 2004
Messages
759
Helped
107
Reputation
222
Reaction score
58
Trophy points
1,308
Activity points
5,006
Re: Global Variable in Verilog

skyfaye said:
With verilog, pretty much everything's "global". You can use hierarchical reference to access any variable in any module.

For example:

module glb_var_mod; // define all global vars in this module
reg glb_x = 1'b1;
endmodule

module dut;
glb_var_mod.glb_x = 1'b0; // change value of a global var
endmodule

- Hung

Very insightful indeed. Thanks!!

Curious to know can we do the same in VHDL too?
 

ghazalaz

Newbie level 2
Joined
May 1, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,297
Re: Global Variable in Verilog

With verilog, pretty much everything's "global". You can use hierarchical reference to access any variable in any module.

For example:

module glb_var_mod; // define all global vars in this module
reg glb_x = 1'b1;
endmodule

module dut;
glb_var_mod.glb_x = 1'b0; // change value of a global var
endmodule

- Hung

i did so but this error was the result:
Error :(on line 7 ) : near "=": syntax error, unexpected '=', expecting IDENTIFIER or TYPE_IDENTIFIER
what is this?

i did something like this another time on another project , and there was an error "Undersolved reference to 'glb_var_mod"
and what is this?!
 

Laleh92

Newbie level 1
Joined
Sep 27, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
6
Re: Global Variable in Verilog

Hi
Maybe you got this error because you didn't use 'always' or 'initial' blocks. Assigning values to reg must be in one of these blocks depending on situation.
For example:
module glb_var_mod; // define all global vars in this module
initial
reg glb_x = 1'b1;
endmodule
 

dave_59

Advanced Member level 3
Joined
Dec 15, 2011
Messages
821
Helped
363
Reputation
730
Reaction score
357
Trophy points
1,353
Location
Fremont, CA, USA
Activity points
6,743
Re: Global Variable in Verilog

Just a note that global variables are not synthesizable, and in general, not a good programming practice in any language.
If instead you really need a global value, use a `define in Verilog or a parameter declared in a package in SystemVerilog.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top