-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a54d986
commit ef851cc
Showing
7 changed files
with
97 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,65 @@ | ||
module counter_4bit ( | ||
input wire clk, // Clock input | ||
input wire rst, // Reset input | ||
output reg [3:0] count // 4-bit counter output | ||
input wire clk, // Clock input | ||
input wire rst, // Reset input | ||
output reg [3:0] count // 4-bit counter output | ||
); | ||
|
||
// Sequential logic block | ||
always @(posedge clk) begin | ||
if (rst) begin | ||
// Synchronous reset | ||
count <= 4'b0000; | ||
end | ||
else begin | ||
// Increment counter | ||
count <= count + 1; | ||
end | ||
// Sequential logic block | ||
always @(posedge clk) begin | ||
if (rst) begin | ||
// Synchronous reset | ||
count <= 4'b0000; | ||
end else begin | ||
// Increment counter | ||
count <= count + 1; | ||
end | ||
|
||
end | ||
endmodule | ||
|
||
// Testbench | ||
module counter_4bit_tb; | ||
|
||
// Testbench signals | ||
reg clk; | ||
reg rst; | ||
wire [3:0] count; | ||
|
||
// Instantiate the counter | ||
counter_4bit counter_inst ( | ||
.clk(clk), | ||
.rst(rst), | ||
.count(count) | ||
); | ||
// Test bench | ||
module main; | ||
// Test bench signals | ||
reg clk; | ||
reg rst; | ||
wire [3:0] count; | ||
|
||
// Clock generation | ||
initial begin | ||
clk = 0; | ||
forever #5 clk = ~clk; | ||
end | ||
// Instantiate the counter | ||
counter_4bit counter_inst ( | ||
.clk (clk), | ||
.rst (rst), | ||
.count(count) | ||
); | ||
|
||
// Test stimulus | ||
initial begin | ||
// Initialize | ||
rst = 1; | ||
// Clock generation | ||
initial begin | ||
clk = 0; | ||
forever #5 clk = ~clk; | ||
end | ||
|
||
// Wait for 2 clock cycles | ||
#20; | ||
// Test stimulus | ||
initial begin | ||
// Initialize | ||
rst = 1; | ||
|
||
// Release reset | ||
rst = 0; | ||
// Wait for 2 clock cycles | ||
#20; | ||
|
||
// Let it count for a while | ||
#160; | ||
// Release reset | ||
rst = 0; | ||
|
||
// Apply reset again | ||
rst = 1; | ||
#20; | ||
// Let it count for a while | ||
#160; | ||
|
||
// End simulation | ||
$finish; | ||
end | ||
// Apply reset again | ||
rst = 1; | ||
#20; | ||
|
||
// Monitor changes | ||
initial begin | ||
$monitor("Time=%0t rst=%b count=%b", $time, rst, count); | ||
end | ||
// End simulation | ||
$finish; | ||
end | ||
|
||
// Monitor changes | ||
initial begin | ||
$monitor("Time=%0t rst=%b count=%b", $time, rst, count); | ||
end | ||
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters