11.4 HierarchyThe module is the basic unit of code in the Verilog language [Verilog LRM 12.1], module holiday_1(sat, sun, weekend); input sat, sun; output weekend; assign weekend = sat | sun; endmodule We do not have to explicitly
declare the scalar wires: Within a module we may instantiate other modules, but we cannot declare other modules. Ports are linked using named association or positional association, `timescale 100s/1s // Units are 100 seconds with precision of 1s. module life; wire [3:0] n; integer days; wire wake_7am, wake_8am; // Wake at 7 on weekdays else at 8. assign n = 1 + (days % 7); // n is day of the week (1-7) always@(wake_8am or wake_7am) ("Day=",n," hours=%0d ",(/36)%24," 8am = ", wake_8am," 7am = ",wake_7am," m2.weekday = ", m2.weekday); initial days = 0; initial begin #(24*36*10);; end // Run for 10 days. always #(24*36) days = days + 1; // Bump day every 24hrs. rest m1(n, wake_8am); // Module instantiation. // Creates a copy of module rest with instance name m1, // ports are linked using positional notation. work m2(.weekday(wake_7am), .day(n)); // Creates a copy of module work with instance name m2, // Ports are linked using named association. endmodule module rest(day, weekend); // Module definition. // Notice the port names are different from the parent. input [3:0] day; output weekend; reg weekend; always begin #36 weekend = day > 5; end // Need a delay here. endmodule module work(day, weekday); input [3:0] day; output weekday; reg weekday; always begin #36 weekday = day < 6; end // Need a delay here. endmodule Day= 1 hours=0 8am = 0 7am = 0 m2.weekday = 0 Day= 1 hours=1 8am = 0 7am = 1 m2.weekday = 1 Day= 6 hours=1 8am = 1 7am = 0 m2.weekday = 0 Day= 1 hours=1 8am = 0 7am = 1 m2.weekday = 1 The port names in a module definition
and the port names in the parent module may be different. We can associate
(link or map) ports using the same order in the instantiating statement
as we use in the module definition--such as instance |
|||||||||||||
|
|||||||||||||
|