SlideShare a Scribd company logo
Program 1 Legal uses of the wire element
1 wire A, B, C, D, E; // simple 1-bit wide wires
2 wire [8:0] Wide; // a 9-bit wide wire
3 reg I;
4
5 assign A = B & C; // using a wire with an assign statement
6
7 always @(B or C) begin
8 I = B | C; // using wires on the right -hand side of an always@
9 // assignment
10 end
11
12 mymodule mymodule_instance (.In (D),
13 .Out(E)); // using a wire as the output of a module
1
Verilog: wire vs. reg
Ahmed Abdelazeem
KAUST, PGDip
Version 0.2023.11.20
November, 20, 2023
1 Introduction
Sections 1.1 to 1.3 discuss the difference between wire and reg in Verilog, and when to use each of them.
1.1 wire Elements (Combinational logic)
wire elements are simple wires (or busses of arbitrary width) in Verilog designs. The following are syntax
rules when using wires:
1. wire elements are used to connect input and output ports of a module instantiation together with
some other element in your design.
2. wire elements are used as inputs and outputs within an actual module declaration.
3. wire elements must be driven by something, and cannot store a value without being driven.
4. wire elements cannot be used as the left-hand side of an = or <= sign in an always@ block.
5. wire elements are the only legal type on the left-hand side of an assign statement.
6. wire elements are a stateless way of connecting two peices in a Verilog-based design.
7. wire elements can only be used to model combinational logic.
Program 1 shows various legal uses of the wire element.
1.2 reg Elements (Combinational and Sequential logic)
reg are similar to wires, but can be used to store information (‘state’) like registers. The following are
syntax rules when using reg elements.
1. reg elements can be connected to the input port of a module instantiation.
2. reg elements cannot be connected to the output port of a module instantiation.
3. reg elements can be used as outputs within an actual module declaration.
4. reg elements cannot be used as inputs within an actual module declaration.
5. reg is the only legal type on the left-hand side of an always@ block = or <= sign.
6. reg is the only legal type on the left-hand side of an initial block = sign (used in Test Benches).
7. reg cannot be used on the left-hand side of an assign statement.
8. reg can be used to create registers when used in conjunction with always@(posedge Clock) blocks.
9. reg can, therefore, be used to create both combinational and sequential logic.
Program 2 shows various legal uses of the reg element.
Program 2 Legal uses of the reg element
1 wire A, B;
2 reg I, J, K; // simple 1-bit wide reg elements
3 reg [8:0] Wide; // a 9-bit wide reg element
4
5 always @(A or B) begin
6 I = A | B; // using a reg as the left -hand side of an always@
7 // assignment
8 end
9
10 initial begin // using a reg in an initial block
11 J = 1’b1;
12 #1
13 J = 1’b0;
14 end
15
16 always @(posedge Clock) begin
17 K <= I; // using a reg to create a positive -edge -triggered register
18 end
1.3 When wire and reg Elements are Interchangable
wire and reg elements can be used interchangably in certain situations:
1. Both can appear on the right-hand side of assign statements and always@ block = or <= signs.
2. Both can be connected to the input ports of module instantiations.
2

More Related Content

Similar to wire vs. reg.pdf

IRJET- Intelligent Car Parking System
IRJET-  	  Intelligent Car Parking SystemIRJET-  	  Intelligent Car Parking System
IRJET- Intelligent Car Parking System
IRJET Journal
 
IRJET- Braille Oriented Classroom
IRJET-  	  Braille Oriented ClassroomIRJET-  	  Braille Oriented Classroom
IRJET- Braille Oriented Classroom
IRJET Journal
 
IRJET- To Design 16 bit Synchronous Microprocessor using VHDL on FPGA
IRJET-  	  To Design 16 bit Synchronous Microprocessor using VHDL on FPGAIRJET-  	  To Design 16 bit Synchronous Microprocessor using VHDL on FPGA
IRJET- To Design 16 bit Synchronous Microprocessor using VHDL on FPGA
IRJET Journal
 
Microprocessorlabmanual ee0310
Microprocessorlabmanual ee0310Microprocessorlabmanual ee0310
Microprocessorlabmanual ee0310
Hari Prakash
 
Micro Processor Lab Manual!
Micro Processor Lab Manual!Micro Processor Lab Manual!
Micro Processor Lab Manual!
PRABHAHARAN429
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directives
SARITHA REDDY
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directives
SARITHA REDDY
 
IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...
IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...
IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...
IRJET Journal
 
IRJET- Bluetooth Controlled Metal Detecting Robot with Message Alert
IRJET- Bluetooth Controlled Metal Detecting Robot with Message AlertIRJET- Bluetooth Controlled Metal Detecting Robot with Message Alert
IRJET- Bluetooth Controlled Metal Detecting Robot with Message Alert
IRJET Journal
 
IRJET- Design & Implementation of Black Box in Automobiles System
IRJET-  	  Design & Implementation of Black Box in Automobiles SystemIRJET-  	  Design & Implementation of Black Box in Automobiles System
IRJET- Design & Implementation of Black Box in Automobiles System
IRJET Journal
 
Boimetric using 8051
Boimetric  using 8051 Boimetric  using 8051
Boimetric using 8051
Shilpa Nayak
 
IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...
IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...
IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...
IRJET Journal
 
Tutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applicationsTutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applications
Edgefxkits & Solutions
 
IRJET- Single Precision Floating Point Arithmetic using VHDL Coding
IRJET-  	  Single Precision Floating Point Arithmetic using VHDL CodingIRJET-  	  Single Precision Floating Point Arithmetic using VHDL Coding
IRJET- Single Precision Floating Point Arithmetic using VHDL Coding
IRJET Journal
 
IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...
IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...
IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...
IRJET Journal
 
Design and Construction of Parcel Wrapping System using Arduino
Design and Construction of Parcel Wrapping System using ArduinoDesign and Construction of Parcel Wrapping System using Arduino
Design and Construction of Parcel Wrapping System using Arduino
ijtsrd
 
Robotics Report final.compressed (1)
Robotics Report final.compressed (1)Robotics Report final.compressed (1)
Robotics Report final.compressed (1)Kael Kristjanson
 
Analysis Of Transmission Line Using MATLAB Software
Analysis Of Transmission Line Using MATLAB SoftwareAnalysis Of Transmission Line Using MATLAB Software
Analysis Of Transmission Line Using MATLAB Software
Allison Thompson
 

Similar to wire vs. reg.pdf (20)

IRJET- Intelligent Car Parking System
IRJET-  	  Intelligent Car Parking SystemIRJET-  	  Intelligent Car Parking System
IRJET- Intelligent Car Parking System
 
IRJET- Braille Oriented Classroom
IRJET-  	  Braille Oriented ClassroomIRJET-  	  Braille Oriented Classroom
IRJET- Braille Oriented Classroom
 
IRJET- To Design 16 bit Synchronous Microprocessor using VHDL on FPGA
IRJET-  	  To Design 16 bit Synchronous Microprocessor using VHDL on FPGAIRJET-  	  To Design 16 bit Synchronous Microprocessor using VHDL on FPGA
IRJET- To Design 16 bit Synchronous Microprocessor using VHDL on FPGA
 
Microprocessorlabmanual ee0310
Microprocessorlabmanual ee0310Microprocessorlabmanual ee0310
Microprocessorlabmanual ee0310
 
Micro Processor Lab Manual!
Micro Processor Lab Manual!Micro Processor Lab Manual!
Micro Processor Lab Manual!
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directives
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directives
 
IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...
IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...
IRJET- FPGA Implementation of Orthogonal Codes for Efficient Digital Communic...
 
IRJET- Bluetooth Controlled Metal Detecting Robot with Message Alert
IRJET- Bluetooth Controlled Metal Detecting Robot with Message AlertIRJET- Bluetooth Controlled Metal Detecting Robot with Message Alert
IRJET- Bluetooth Controlled Metal Detecting Robot with Message Alert
 
IRJET- Design & Implementation of Black Box in Automobiles System
IRJET-  	  Design & Implementation of Black Box in Automobiles SystemIRJET-  	  Design & Implementation of Black Box in Automobiles System
IRJET- Design & Implementation of Black Box in Automobiles System
 
Boimetric using 8051
Boimetric  using 8051 Boimetric  using 8051
Boimetric using 8051
 
IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...
IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...
IRJET-Hardware Co-Simulation of Classical Edge Detection Algorithms using Xil...
 
Tutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applicationsTutorial on avr atmega8 microcontroller, architecture and its applications
Tutorial on avr atmega8 microcontroller, architecture and its applications
 
IRJET- Single Precision Floating Point Arithmetic using VHDL Coding
IRJET-  	  Single Precision Floating Point Arithmetic using VHDL CodingIRJET-  	  Single Precision Floating Point Arithmetic using VHDL Coding
IRJET- Single Precision Floating Point Arithmetic using VHDL Coding
 
IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...
IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...
IRJET- Line following and Obstacle avoiding Bluetooth Controlled Surveillance...
 
Design and Construction of Parcel Wrapping System using Arduino
Design and Construction of Parcel Wrapping System using ArduinoDesign and Construction of Parcel Wrapping System using Arduino
Design and Construction of Parcel Wrapping System using Arduino
 
Robotics Report final.compressed (1)
Robotics Report final.compressed (1)Robotics Report final.compressed (1)
Robotics Report final.compressed (1)
 
Project-Synopsis
Project-SynopsisProject-Synopsis
Project-Synopsis
 
Hd6
Hd6Hd6
Hd6
 
Analysis Of Transmission Line Using MATLAB Software
Analysis Of Transmission Line Using MATLAB SoftwareAnalysis Of Transmission Line Using MATLAB Software
Analysis Of Transmission Line Using MATLAB Software
 

More from Ahmed Abdelazeem

Memory compiler tutorial – TSMC 40nm technology
Memory compiler tutorial – TSMC 40nm technologyMemory compiler tutorial – TSMC 40nm technology
Memory compiler tutorial – TSMC 40nm technology
Ahmed Abdelazeem
 
10. Signoff.pdf
10. Signoff.pdf10. Signoff.pdf
10. Signoff.pdf
Ahmed Abdelazeem
 
9. Routing.pdf
9. Routing.pdf9. Routing.pdf
9. Routing.pdf
Ahmed Abdelazeem
 
8. Clock Tree Synthesis.pdf
8. Clock Tree Synthesis.pdf8. Clock Tree Synthesis.pdf
8. Clock Tree Synthesis.pdf
Ahmed Abdelazeem
 
7. Placement.pdf
7. Placement.pdf7. Placement.pdf
7. Placement.pdf
Ahmed Abdelazeem
 
6. Design Planning.pdf
6. Design Planning.pdf6. Design Planning.pdf
6. Design Planning.pdf
Ahmed Abdelazeem
 
5. Data and Timing Setup Introduction.pdf
5. Data and Timing Setup Introduction.pdf5. Data and Timing Setup Introduction.pdf
5. Data and Timing Setup Introduction.pdf
Ahmed Abdelazeem
 
What is the Difference Between the target_library and link_library Variables?
What is the Difference Between the target_library and link_library Variables?What is the Difference Between the target_library and link_library Variables?
What is the Difference Between the target_library and link_library Variables?
Ahmed Abdelazeem
 
Physical Implementation: I/O Pad Insertion for Innovus.pdf
Physical Implementation: I/O Pad Insertion for Innovus.pdfPhysical Implementation: I/O Pad Insertion for Innovus.pdf
Physical Implementation: I/O Pad Insertion for Innovus.pdf
Ahmed Abdelazeem
 
Learn Makefiles With the tastiest examples
Learn Makefiles With the tastiest examplesLearn Makefiles With the tastiest examples
Learn Makefiles With the tastiest examples
Ahmed Abdelazeem
 
ready_valid_interface.pdf
ready_valid_interface.pdfready_valid_interface.pdf
ready_valid_interface.pdf
Ahmed Abdelazeem
 
always_at_blocks.pdf
always_at_blocks.pdfalways_at_blocks.pdf
always_at_blocks.pdf
Ahmed Abdelazeem
 
1. Introduction to PnR.pptx
1. Introduction to PnR.pptx1. Introduction to PnR.pptx
1. Introduction to PnR.pptx
Ahmed Abdelazeem
 
5. DFT.pptx
5. DFT.pptx5. DFT.pptx
5. DFT.pptx
Ahmed Abdelazeem
 
4. Formal Equivalence Checking (Formality).pptx
4. Formal Equivalence Checking (Formality).pptx4. Formal Equivalence Checking (Formality).pptx
4. Formal Equivalence Checking (Formality).pptx
Ahmed Abdelazeem
 
3. Synthesis.pptx
3. Synthesis.pptx3. Synthesis.pptx
3. Synthesis.pptx
Ahmed Abdelazeem
 
Routing.pdf
Routing.pdfRouting.pdf
Routing.pdf
Ahmed Abdelazeem
 
Clock Tree Synthesis.pdf
Clock Tree Synthesis.pdfClock Tree Synthesis.pdf
Clock Tree Synthesis.pdf
Ahmed Abdelazeem
 
ASIC_Design.pdf
ASIC_Design.pdfASIC_Design.pdf
ASIC_Design.pdf
Ahmed Abdelazeem
 
Placement.pdf
Placement.pdfPlacement.pdf
Placement.pdf
Ahmed Abdelazeem
 

More from Ahmed Abdelazeem (20)

Memory compiler tutorial – TSMC 40nm technology
Memory compiler tutorial – TSMC 40nm technologyMemory compiler tutorial – TSMC 40nm technology
Memory compiler tutorial – TSMC 40nm technology
 
10. Signoff.pdf
10. Signoff.pdf10. Signoff.pdf
10. Signoff.pdf
 
9. Routing.pdf
9. Routing.pdf9. Routing.pdf
9. Routing.pdf
 
8. Clock Tree Synthesis.pdf
8. Clock Tree Synthesis.pdf8. Clock Tree Synthesis.pdf
8. Clock Tree Synthesis.pdf
 
7. Placement.pdf
7. Placement.pdf7. Placement.pdf
7. Placement.pdf
 
6. Design Planning.pdf
6. Design Planning.pdf6. Design Planning.pdf
6. Design Planning.pdf
 
5. Data and Timing Setup Introduction.pdf
5. Data and Timing Setup Introduction.pdf5. Data and Timing Setup Introduction.pdf
5. Data and Timing Setup Introduction.pdf
 
What is the Difference Between the target_library and link_library Variables?
What is the Difference Between the target_library and link_library Variables?What is the Difference Between the target_library and link_library Variables?
What is the Difference Between the target_library and link_library Variables?
 
Physical Implementation: I/O Pad Insertion for Innovus.pdf
Physical Implementation: I/O Pad Insertion for Innovus.pdfPhysical Implementation: I/O Pad Insertion for Innovus.pdf
Physical Implementation: I/O Pad Insertion for Innovus.pdf
 
Learn Makefiles With the tastiest examples
Learn Makefiles With the tastiest examplesLearn Makefiles With the tastiest examples
Learn Makefiles With the tastiest examples
 
ready_valid_interface.pdf
ready_valid_interface.pdfready_valid_interface.pdf
ready_valid_interface.pdf
 
always_at_blocks.pdf
always_at_blocks.pdfalways_at_blocks.pdf
always_at_blocks.pdf
 
1. Introduction to PnR.pptx
1. Introduction to PnR.pptx1. Introduction to PnR.pptx
1. Introduction to PnR.pptx
 
5. DFT.pptx
5. DFT.pptx5. DFT.pptx
5. DFT.pptx
 
4. Formal Equivalence Checking (Formality).pptx
4. Formal Equivalence Checking (Formality).pptx4. Formal Equivalence Checking (Formality).pptx
4. Formal Equivalence Checking (Formality).pptx
 
3. Synthesis.pptx
3. Synthesis.pptx3. Synthesis.pptx
3. Synthesis.pptx
 
Routing.pdf
Routing.pdfRouting.pdf
Routing.pdf
 
Clock Tree Synthesis.pdf
Clock Tree Synthesis.pdfClock Tree Synthesis.pdf
Clock Tree Synthesis.pdf
 
ASIC_Design.pdf
ASIC_Design.pdfASIC_Design.pdf
ASIC_Design.pdf
 
Placement.pdf
Placement.pdfPlacement.pdf
Placement.pdf
 

Recently uploaded

Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Peter Gallagher
 
MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...
MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...
MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...
PinkySharma900491
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
peuce
 
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
andreassenrolf537
 
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
aozcue
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
aozcue
 
web-tech-lab-manual-final-abhas.pdf. Jer
web-tech-lab-manual-final-abhas.pdf. Jerweb-tech-lab-manual-final-abhas.pdf. Jer
web-tech-lab-manual-final-abhas.pdf. Jer
freshgammer09
 

Recently uploaded (7)

Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
Building a Raspberry Pi Robot with Dot NET 8, Blazor and SignalR - Slides Onl...
 
MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...
MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...
MATHEMATICS BRIDGE COURSE (TEN DAYS PLANNER) (FOR CLASS XI STUDENTS GOING TO ...
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证如何办理
 
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
欧洲杯冠军-欧洲杯冠军网站-欧洲杯冠军|【​网址​🎉ac123.net🎉​】领先全球的买球投注平台
 
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
一比一原版(UCSB毕业证)圣塔芭芭拉社区大学毕业证如何办理
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证如何办理
 
web-tech-lab-manual-final-abhas.pdf. Jer
web-tech-lab-manual-final-abhas.pdf. Jerweb-tech-lab-manual-final-abhas.pdf. Jer
web-tech-lab-manual-final-abhas.pdf. Jer
 

wire vs. reg.pdf

  • 1. Program 1 Legal uses of the wire element 1 wire A, B, C, D, E; // simple 1-bit wide wires 2 wire [8:0] Wide; // a 9-bit wide wire 3 reg I; 4 5 assign A = B & C; // using a wire with an assign statement 6 7 always @(B or C) begin 8 I = B | C; // using wires on the right -hand side of an always@ 9 // assignment 10 end 11 12 mymodule mymodule_instance (.In (D), 13 .Out(E)); // using a wire as the output of a module 1 Verilog: wire vs. reg Ahmed Abdelazeem KAUST, PGDip Version 0.2023.11.20 November, 20, 2023 1 Introduction Sections 1.1 to 1.3 discuss the difference between wire and reg in Verilog, and when to use each of them. 1.1 wire Elements (Combinational logic) wire elements are simple wires (or busses of arbitrary width) in Verilog designs. The following are syntax rules when using wires: 1. wire elements are used to connect input and output ports of a module instantiation together with some other element in your design. 2. wire elements are used as inputs and outputs within an actual module declaration. 3. wire elements must be driven by something, and cannot store a value without being driven. 4. wire elements cannot be used as the left-hand side of an = or <= sign in an always@ block. 5. wire elements are the only legal type on the left-hand side of an assign statement. 6. wire elements are a stateless way of connecting two peices in a Verilog-based design. 7. wire elements can only be used to model combinational logic. Program 1 shows various legal uses of the wire element.
  • 2. 1.2 reg Elements (Combinational and Sequential logic) reg are similar to wires, but can be used to store information (‘state’) like registers. The following are syntax rules when using reg elements. 1. reg elements can be connected to the input port of a module instantiation. 2. reg elements cannot be connected to the output port of a module instantiation. 3. reg elements can be used as outputs within an actual module declaration. 4. reg elements cannot be used as inputs within an actual module declaration. 5. reg is the only legal type on the left-hand side of an always@ block = or <= sign. 6. reg is the only legal type on the left-hand side of an initial block = sign (used in Test Benches). 7. reg cannot be used on the left-hand side of an assign statement. 8. reg can be used to create registers when used in conjunction with always@(posedge Clock) blocks. 9. reg can, therefore, be used to create both combinational and sequential logic. Program 2 shows various legal uses of the reg element. Program 2 Legal uses of the reg element 1 wire A, B; 2 reg I, J, K; // simple 1-bit wide reg elements 3 reg [8:0] Wide; // a 9-bit wide reg element 4 5 always @(A or B) begin 6 I = A | B; // using a reg as the left -hand side of an always@ 7 // assignment 8 end 9 10 initial begin // using a reg in an initial block 11 J = 1’b1; 12 #1 13 J = 1’b0; 14 end 15 16 always @(posedge Clock) begin 17 K <= I; // using a reg to create a positive -edge -triggered register 18 end 1.3 When wire and reg Elements are Interchangable wire and reg elements can be used interchangably in certain situations: 1. Both can appear on the right-hand side of assign statements and always@ block = or <= signs. 2. Both can be connected to the input ports of module instantiations. 2