Saturday, March 03, 2012

Hierarchy- Nested Modules

Hierarchy- Nested Modules

A module can be declared within another module. The outer name space is visible to the inner module, so that any name declared there can be used, unless hidden by a local name, provided the module is declared and instantiated in the same scope.

module first_counter (clock ,reset ,enable ,counter_out );
module ports()
input clock ;
input reset ;
input enable ;
output [3:0] counter_out ;
wire clock ;
wire reset ;
wire enable ;
reg [3:0] counter_out ;
endmodule

always @ (posedge clock)
begin : COUNTER // Block Name
if (reset == 1'b1) begin
counter_out <= #1 4'b0000;
end
else if (enable == 1'b1) begin
counter_out <= #1 counter_out + 1;
end
end
endmodule

module first_counter_tb();
reg clock, reset, enable;
wire [3:0] counter_out;

module functionality ()
initial begin
$display ("time\t clk reset enable counter");
$monitor ("%g\t %b %b %b %b", $time, clock, reset, enable, counter_out);
clock = 1; // initial value of clock
reset = 1; // initial value of reset
enable = 1; // initial value of enable
#5 reset = 1; // Assert the reset
#10 reset = 0; // De-assert the reset
#10 enable = 1; // Assert enable
#100 enable = 0; // De-assert enable
endmodule
#5 $finish; // Terminate simulation
end

always begin
#5 clock = ~clock; // Toggle clock every 5 ticks
end

first_counter U_counter (
clock,
reset,
enable,
counter_out
);
endmodule

No comments:

Post a Comment

Popular Posts