Beruflich Dokumente
Kultur Dokumente
Object
Object11
10
4
5
Object 7
1
Object 9
3
Introduction
If you refer to any book on programming languages, it starts with an "Hello
World" program; once you have written it, you can be sure that you can do
something in that language
Well I am also going to show how to write a "hello world" program, followed by
a "counter" design, in Verilog.
Object
Object12
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Words in green are comments, blue are reserved words. Any program in Verilog
starts with reserved word 'module' <module_name>. In the above example line 8
contains module hello_world. (Note: We can have compiler pre-processor
statements like `include', `define' before module declaration)
Line 10 contains the initial block: this block gets executed only once after the
simulation starts, at time=0 (0ns). This block contains two statements which are
enclosed within begin, at line 10, and end, at line 13. In Verilog, if you have
multiple lines within a block, you need to use begin and end. Module ends with
'endmodule' reserved word, in this case at line 15.
Counter Design
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
The counter testbench consists of clock generator, reset control, enable control
and monitor/checker logic. Below is the simple code of testbench without the
monitor/checker logic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
`include "first_counter.v"
module first_counter_tb();
// Declare inputs as regs and outputs as wires
reg clock, reset, enable;
wire [3:0] counter_out;
// Initialize all variables
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 = 0;
// initial value of reset
enable = 0;
// 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
#5 $finish;
// Terminate simulation
end
// Clock generator
always begin
#5 clock = ~clock; // Toggle clock every 5 ticks
end
// Connect DUT to test bench
first_counter U_counter (
clock,
reset,
enable,
32 counter_out
33 );
34
35 endmodule
You could download file first_counter_tb.v here
time
0
5
10
11
15
20
25
30
31
35
40
41
45
50
51
55
60
61
65
70
71
75
80
81
85
90
91
95
100
101
105
110
111
115
120
121
125
clk
1
0
1
1
0
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
reset
0
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Counter Waveform
enable
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
counter
xxxx
xxxx
xxxx
0000
0000
0000
0000
0000
0001
0001
0001
0010
0010
0010
0011
0011
0011
0100
0100
0100
0101
0101
0101
0110
0110
0110
0111
0111
0111
1000
1000
1000
1001
1001
1001
1010
1010
Copyright 1998-2014
Deepak Kumar Tala - All rights reserved
Do you have any Comment? mail me at:deepak@asic-world.com
Introduction
If you refer to any book on programming languages, it starts with an "Hello
World" program; once you have written it, you can be sure that you can do
something in that language
Well I am also going to show how to write a "hello world" program, followed by
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Words in green are comments, blue are reserved words. Any program in Verilog
starts with reserved word 'module' <module_name>. In the above example line 8
contains module hello_world. (Note: We can have compiler pre-processor
statements like `include', `define' before module declaration)
Line 10 contains the initial block: this block gets executed only once after the
simulation starts, at time=0 (0ns). This block contains two statements which are
enclosed within begin, at line 10, and end, at line 13. In Verilog, if you have
multiple lines within a block, you need to use begin and end. Module ends with
'endmodule' reserved word, in this case at line 15.
Counter Design
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
The counter testbench consists of clock generator, reset control, enable control
and monitor/checker logic. Below is the simple code of testbench without the
monitor/checker logic.
1 `include "first_counter.v"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module first_counter_tb();
// Declare inputs as regs and outputs as wires
reg clock, reset, enable;
wire [3:0] counter_out;
// Initialize all variables
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 = 0;
// initial value of reset
enable = 0;
// 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
#5 $finish;
// Terminate simulation
end
// Clock generator
always begin
#5 clock = ~clock; // Toggle clock every 5 ticks
end
// Connect DUT to test bench
first_counter U_counter (
clock,
reset,
enable,
counter_out
);
endmodule
time
0
5
10
11
15
20
25
30
31
35
40
41
45
50
51
55
60
61
65
70
71
75
clk
1
0
1
1
0
1
0
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
reset
0
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
enable
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
counter
xxxx
xxxx
xxxx
0000
0000
0000
0000
0000
0001
0001
0001
0010
0010
0010
0011
0011
0011
0100
0100
0100
0101
0101
80
81
85
90
91
95
100
101
105
110
111
115
120
121
125
1
1
0
1
1
0
1
1
0
1
1
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0101
0110
0110
0110
0111
0111
0111
1000
1000
1000
1001
1001
1001
1010
1010
Counter Waveform
Copyright 1998-2014
Deepak Kumar Tala - All rights reserved
Do you have any Comment? mail me at:deepak@asic-world.com