Skip to content

Commit 78403cd

Browse files
committed
Added instantiation templates and testbenches for selected modules
1 parent c1b04ec commit 78403cd

16 files changed

+462
-15
lines changed

ClkDivider.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
// Äåëèòåëü îñíîâíîãî òàêòîâîãî ñèãíàëà.
88
// Ïîçâîëÿåò ïîëó÷èòü ïðîèçâîäíûå ìåäëåííûå êëîêè ñèíõðîííûå ñ îïîðíûì.
99

10+
11+
/*ClkDivider CD1 (
12+
.clk(),
13+
.nrst(),
14+
.out()
15+
);
16+
defparam CD1.WIDTH = 32;*/
17+
18+
1019
module ClkDivider(clk,nrst,out);
1120

1221
input wire clk;

DeBounce.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
// Ðàáîòàåò ïî äâóì îòñ÷åòàì (ìåäëåííîãî) êëîêà.
88
// Ïåðåêëþ÷àåòñÿ â îáà ñîñòîÿíèÿ ñ çàäåðæêîé íà äâà (ìåäëåííûõ) êëîêà
99

10+
11+
/* DeBounce DB1 (
12+
.clk(),
13+
.nrst(),
14+
.in(),
15+
.out()
16+
);
17+
defparam DB1.WIDTH = 1;*/
18+
19+
1020
module DeBounce(clk,nrst,in,out);
1121

1222
input wire clk;

DynDelay.v

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44
//--------------------------------------------------------------------------------
55

66
// INFO --------------------------------------------------------------------------------
7-
// Ãåíåðàòîð çàäåðæêè ïðîèçâîëüíîãî ñèãíàëà
7+
// ƒåíåðàòîð çàäåðæêè ïðîèçâîëüíîãî ñèãíàëà
88
//
99

10+
11+
/* DynDelay DD1 (
12+
.clk(),
13+
.in(),
14+
.sel(),
15+
.out()
16+
);
17+
defparam DD1.LENGTH = 8;*/
18+
19+
1020
//(* keep_hierarchy = "yes" *)
1121
module DynDelay(clk, in, sel, out);
1222

EdgeDetect.v

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,27 @@
88
// Âûäàåò åäèíè÷íûé èìïóëüñ ïî ôðîíòó, ñïàäó è êîìáèíèðîâàííûé
99
// Çàäåðæêà íà îäèí òàêò
1010

11-
module EdgeDetect(clk, nrst, rising, falling, in, out);
11+
12+
/*EdgeDetect ED1 (
13+
.clk(),
14+
.nrst(),
15+
.in(),
16+
.rising(),
17+
.falling(),
18+
.both()
19+
);
20+
defparam ED1.WIDTH = 1;*/
21+
22+
23+
module EdgeDetect(clk, nrst, in, rising, falling, both);
1224

1325
input wire clk;
1426
input wire nrst;
1527

1628
input wire [(WIDTH-1):0] in;
1729
output reg [(WIDTH-1):0] rising = 0;
1830
output reg [(WIDTH-1):0] falling = 0;
19-
output wire [(WIDTH-1):0] out;
31+
output wire [(WIDTH-1):0] both;
2032

2133
parameter WIDTH = 1;
2234

@@ -38,6 +50,6 @@ always @ (posedge clk) begin
3850
end
3951

4052
assign
41-
out[(WIDTH-1):0] = rising[(WIDTH-1):0] | falling[(WIDTH-1):0];
53+
both[(WIDTH-1):0] = rising[(WIDTH-1):0] | falling[(WIDTH-1):0];
4254

4355
endmodule

Encoder.v

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//--------------------------------------------------------------------------------
2+
// Encoder.v
3+
// Konstantin Pavlov, pavlovconst@gmail.com
4+
//--------------------------------------------------------------------------------
5+
6+
// INFO --------------------------------------------------------------------------------
7+
// Îáðàáîòêà ñèãíàëîâ ñ ýíêîäåðà
8+
//
9+
10+
/*Encoder E1(
11+
.clk(),
12+
.nrst(),
13+
.incA(),
14+
.incB(),
15+
.plus1(),
16+
.minus1()
17+
);*/
18+
19+
20+
module Encoder(clk,nrst,incA,incB,plus1,minus1);
21+
22+
input wire clk;
23+
input wire nrst;
24+
input wire incA, incB; // present input values
25+
output reg plus1 = 0, minus1 = 0;
26+
27+
reg bufA = 0, bufB = 0; // previous inputvalues
28+
29+
always @ (posedge clk) begin
30+
if (~nrst) begin
31+
bufA <= 0;
32+
bufB <= 0;
33+
plus1 <= 0;
34+
minus1 <= 0;
35+
end
36+
else begin
37+
plus1 <= (bufA^incB)&~(incA^bufB);
38+
minus1 <= (incA^bufB)&~(bufA^incB);
39+
bufA <= incA;
40+
bufB <= incB;
41+
end // if
42+
end
43+
44+
endmodule

Encoder_tb.v

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//--------------------------------------------------------------------------------
2+
// Encoder_test project, 201601
3+
// Main_tb.v
4+
// Konstantin Pavlov, pavlovconst@gmail.com
5+
//--------------------------------------------------------------------------------
6+
7+
// INFO --------------------------------------------------------------------------------
8+
//
9+
//
10+
11+
`timescale 1ns / 1ps
12+
13+
module Main_tb();
14+
15+
reg clk200;
16+
initial begin
17+
#0 clk200 = 1;
18+
forever
19+
#2.5 clk200 = ~clk200;
20+
end
21+
22+
reg rst;
23+
initial begin
24+
#10.2 rst = 1;
25+
#5 rst = 0;
26+
//#10000;
27+
forever begin
28+
#9985 rst = ~rst;
29+
#5 rst = ~rst;
30+
end
31+
end
32+
wire nrst = ~rst;
33+
34+
wire [31:0] DerivedClocks;
35+
ClkDivider CD1 (
36+
.clk(clk200),
37+
.nrst,
38+
.out(DerivedClocks[31:0]));
39+
defparam CD1.WIDTH = 32;
40+
41+
wire [31:0] E_DerivedClocks;
42+
EdgeDetect ED1 (
43+
.clk(clk200),
44+
.nrst,
45+
.in(DerivedClocks[31:0]),
46+
.rising(E_DerivedClocks[31:0]),
47+
.falling(),
48+
.both()
49+
);
50+
defparam ED1.WIDTH = 32;
51+
52+
wire [15:0] RandomNumber1;
53+
reg rst_once;
54+
initial begin
55+
#10.2 rst_once = 1;
56+
#5 rst_once = 0;
57+
end
58+
59+
c_rand RNG1 (
60+
.clk(clk200),
61+
.rst(rst_once),
62+
.reseed(1'b0),
63+
.seed_val(DerivedClocks[15:0]),
64+
.out(RandomNumber1[15:0]));
65+
66+
reg start;
67+
initial begin
68+
#100.2 start = 1;
69+
#5 start = 0;
70+
end
71+
72+
//=================================================
73+
74+
wire p,m;
75+
Encoder E1(
76+
.clk(clk200),
77+
.nrst,
78+
.incA(RandomNumber1[0]),
79+
.incB(RandomNumber1[1]),
80+
.plus1(p),
81+
.minus1(m)
82+
);
83+
84+
endmodule
85+

Main_TB.v

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,36 @@ initial begin
2929
#5 rst = ~rst;
3030
end
3131
end
32+
wire nrst = ~rst;
3233

3334
wire [31:0] DerivedClocks;
3435
ClkDivider CD1 (
3536
.clk(clk200),
36-
.nrst(1'b1),
37+
.nrst,
3738
.out(DerivedClocks[31:0]));
3839
defparam CD1.WIDTH = 32;
3940

41+
wire [31:0] E_DerivedClocks;
42+
EdgeDetect ED1 (
43+
.clk(clk200),
44+
.nrst,
45+
.in(DerivedClocks[31:0]),
46+
.rising(E_DerivedClocks[31:0]),
47+
.falling(),
48+
.both()
49+
);
50+
defparam ED1.WIDTH = 32;
51+
4052
wire [15:0] RandomNumber1;
41-
reg rst1;
53+
reg rst_once;
4254
initial begin
43-
#10.2 rst1 = 1;
44-
#5 rst1 = 0;
55+
#10.2 rst_once = 1;
56+
#5 rst_once = 0;
4557
end
4658

4759
c_rand RNG1 (
4860
.clk(clk200),
49-
.rst(rst1),
61+
.rst(rst_once),
5062
.reseed(1'b0),
5163
.seed_val(DerivedClocks[15:0]),
5264
.out(RandomNumber1[15:0]));
@@ -57,6 +69,8 @@ initial begin
5769
#5 start = 0;
5870
end
5971

72+
//=================================================
73+
6074
wire out1,out2;
6175
Main M( // module under test
6276
TB_clk,~TB_clk,

PulseGen.v

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
// Ãåíåðàòîð èìïóëüñà
88
// Ðàáîòàåò íà÷èíàÿ ñ low_wdth = 1, high_wdth = 1
99

10+
11+
/*PulseGen PG1(
12+
.clk(),
13+
.nrst(),
14+
.low_wdth(),
15+
.high_wdth(),
16+
.rpt(),
17+
.start(),
18+
.busy(),
19+
.out()
20+
);*/
21+
22+
1023
module PulseGen(clk,nrst,low_wdth,high_wdth,rpt,start,busy,out);
1124

1225
input wire clk;

PulseGen_tb.v

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//--------------------------------------------------------------------------------
2+
// PulseGen_test project, 201512
3+
// Main_tb.v
4+
// Konstantin Pavlov, pavlovconst@gmail.com
5+
//--------------------------------------------------------------------------------
6+
7+
// INFO --------------------------------------------------------------------------------
8+
//
9+
//
10+
11+
`timescale 1ns / 1ps
12+
13+
module Main_tb();
14+
15+
reg clk200;
16+
initial begin
17+
#0 clk200 = 1;
18+
forever
19+
#2.5 clk200 = ~clk200;
20+
end
21+
22+
reg rst;
23+
initial begin
24+
#10.2 rst = 1;
25+
#5 rst = 0;
26+
//#10000;
27+
forever begin
28+
#9985 rst = ~rst;
29+
#5 rst = ~rst;
30+
end
31+
end
32+
33+
wire [31:0] DerivedClocks;
34+
ClkDivider CD1 (
35+
.clk(clk200),
36+
.nrst(1'b1),
37+
.out(DerivedClocks[31:0]));
38+
defparam CD1.WIDTH = 32;
39+
40+
wire [15:0] RandomNumber1;
41+
reg rst1;
42+
initial begin
43+
#10.2 rst1 = 1;
44+
#5 rst1 = 0;
45+
end
46+
47+
c_rand RNG1 (
48+
.clk(clk200),
49+
.rst(rst1),
50+
.reseed(1'b0),
51+
.seed_val(DerivedClocks[15:0]),
52+
.out(RandomNumber1[15:0]));
53+
54+
reg start;
55+
initial begin
56+
#100.2 start = 1;
57+
#5 start = 0;
58+
end
59+
60+
wire busy1,out1;
61+
PulseGen PG1 (clk200,~rst,1,1,1'b1,start,busy1,out1);
62+
63+
wire busy2,out2;
64+
PulseGen PG2 (clk200,~rst,{28'b0,RandomNumber1[3:0]},{28'b0,RandomNumber1[7:4]},1'b0,&RandomNumber1[2:0],busy2,out2);
65+
66+
endmodule
67+

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55

66
**/Advanced Synthesis Cookbook/** useful code from Altera`s cookbook
77

8-
*Main_TB.v** - basic testbench template
8+
**Main_TB.v** - basic testbench template
99

1010
**ClkDivider.v** - wide reference clock divider
1111
**DeBounce.v** - two-cycle debounce for input buttons
12+
**DynDelay.v** - dynamic delay made on general-purpose trigger elements
1213
**EdgeDetect.v** - edge detector, gives one-tick pulses on every signal edge
14+
**Encoder.v** encoder input module
15+
**PulseGen.v** - generates pulses with given width and delay
1316
**ResetSet.v** - SR trigger variant w/o metastable state, set dominates here
1417
**SetReset.v** - SR trigger variant w/o metastable state, reset dominates here
18+
**SimplePulseGen.v** - generates one-cycle pulse with given delay
1519
**Synch.v** - input syncnronizer (and also "static delay module"), standard way to get rid of metastability issues
1620

17-
**DynDelay.v** - dynamic delay made on general-purpose trigger elements
18-
**PulseGen.v** - generates pulses with given width and delay
19-
**SimplePulseGen.v** - generates one-cycle pulse with given delay
21+
Also added some simple testbenches for selected modules
22+
2023

24+
Author: Konstantin Pavlov, pavlovconst@gmail.com
2125

0 commit comments

Comments
 (0)