Skip to content

Commit bc618ce

Browse files
committed
modified from ASIC website
1 parent fcc76a7 commit bc618ce

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

verilog_tutorial/cnt_tb.v

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//`include "first_counter.v" /*don't need to include it if do the iverilog compile*/
2+
module first_counter_tb();
3+
// Declare inputs as regs and outputs as wires
4+
reg clock, reset, enable;
5+
wire [3:0] counter_out;
6+
7+
// Initialize all variables
8+
initial begin
9+
$display ("time\t clk reset enable counter"); //Need to add these two lines to
10+
$dumpfile ("first_counter_tb.vcd"); //generate VCD file so as to gtkwave it
11+
$dumpvars (1, first_counter_tb);
12+
$monitor ("%g\t %b %b %b %b",
13+
$time, clock, reset, enable, counter_out);
14+
clock = 1; // initial value of clock
15+
reset = 0; // initial value of reset
16+
enable = 0; // initial value of enable
17+
#5 reset = 1; // Assert the reset
18+
#10 reset = 0; // De-assert the reset
19+
#10 enable = 1; // Assert enable
20+
#150 enable = 0; // De-assert enable
21+
#5 $finish; // Terminate simulation
22+
end
23+
24+
// Clock generator
25+
always begin
26+
#5 clock = ~clock; // Toggle clock every 5 ticks
27+
end
28+
29+
// Connect DUT to test bench
30+
first_counter U_counter (
31+
clock,
32+
reset,
33+
enable,
34+
counter_out
35+
);
36+
37+
endmodule
38+

verilog_tutorial/count.pdf

4.28 KB
Binary file not shown.

verilog_tutorial/first_counter.v

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//-----------------------------------------------------
2+
// This is my second Verilog Design
3+
// Design Name : first_counter
4+
// File Name : first_counter.v
5+
// Function : This is a 4 bit up-counter with
6+
// Synchronous active high reset and
7+
// with active high enable signal
8+
//-----------------------------------------------------
9+
module first_counter (
10+
clock , // Clock input of the design
11+
reset , // active high, synchronous Reset input
12+
enable , // Active high enable signal for counter
13+
counter_out // 4 bit vector output of the counter
14+
); // End of port list
15+
//-------------Input Ports-----------------------------
16+
input clock ;
17+
input reset ;
18+
input enable ;
19+
//-------------Output Ports----------------------------
20+
output [3:0] counter_out ;
21+
//-------------Input ports Data Type-------------------
22+
// By rule all the input ports should be wires
23+
wire clock ;
24+
wire reset ;
25+
wire enable ;
26+
//-------------Output Ports Data Type------------------
27+
// Output port can be a storage element (reg) or a wire
28+
reg [3:0] counter_out ;
29+
30+
//------------Code Starts Here-------------------------
31+
// Since this counter is a positive edge trigged one,
32+
// We trigger the below block with respect to positive
33+
// edge of the clock.
34+
always @ (posedge clock)
35+
begin : COUNTER // Block Name
36+
// At every rising edge of clock we check if reset is active
37+
// If active, we load the counter output with 4'b0000
38+
if (reset == 1'b1) begin
39+
counter_out <= #1 4'b0000;
40+
end
41+
// If enable is active, then we increment the counter
42+
else if (enable == 1'b1) begin
43+
counter_out <= #1 counter_out + 1;
44+
end
45+
end // End of Block COUNTER
46+
47+
endmodule // End of Module counter
48+

0 commit comments

Comments
 (0)