OpenCores
URL https://opencores.org/ocsvn/pid_controller/pid_controller/trunk

Subversion Repositories pid_controller

[/] [pid_controller/] [trunk/] [bench/] [PID_tb.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 m99
/*PID controller testbench
2
Author: Zhu Xu
3
Email: m99a1@yahoo.cn
4
*/
5
 
6
 
7
module  PID_tb;
8
reg     clk=0;
9
reg     rst=1;
10
 
11
always #1 clk=~clk;
12
 
13
initial begin
14
        #10 rst=0;
15
end
16
 
17
//scoreboard
18
reg     [15:0]kp=0;
19
reg     [15:0]ki=0;
20
reg     [15:0]kd=0;
21
reg     [15:0]sp=0;
22
reg     [15:0]pv=0;
23
reg     [15:0]kpd;
24
reg     [15:0]err[0:1];
25
reg     [31:0]sigma=0;
26
reg     [31:0]un=0;
27
reg     [31:0]a=0;
28
reg     [31:0]p=0;
29
reg     [4:0]of=0;
30
reg     [31:0]s=0;
31
initial begin
32
        err[0]=0;
33
        err[1]=0;
34
end
35
 
36
wire    [31:0]value_sb[0:10];
37
assign  value_sb[0]={{16{kp[15]}},kp};
38
assign  value_sb[1]={{16{ki[15]}},ki};
39
assign  value_sb[2]={{16{kd[15]}},kd};
40
assign  value_sb[3]={{16{sp[15]}},sp};
41
assign  value_sb[4]={{16{pv[15]}},pv};
42
assign  value_sb[5]={{16{kpd[15]}},kpd};
43
assign  value_sb[6]={{16{err[0][15]}},err[0]};
44
assign  value_sb[7]={{16{err[1][15]}},err[1]};
45
assign  value_sb[8]=un;
46
assign  value_sb[9]=sigma;
47
assign  value_sb[10]={27'b0,of};
48
 
49
function of_check_16bit;
50
        input   [15:0]a,b;
51
        begin
52
                s=a+b;
53
                of_check_16bit=(a[15]&b[15]&(~s[15]))|((~a[15])&(~b[15])&s[15]);
54
        end
55
endfunction
56
 
57
function of_check_32bit;
58
        input   [31:0]a,b;
59
        begin
60
                s=a+b;
61
                of_check_32bit=(a[31]&b[31]&(~s[31]))|((~a[31])&(~b[31])&s[31]);
62
        end
63
endfunction
64
 
65
task    update_sb;
66
        input   [15:0]adr;
67
        input   [15:0]data;
68
        begin
69
                case(adr[15:2])
70
                        0:begin
71
                                kp=data;
72
                                kpd=kp+kd;
73
                                of[0]=of_check_16bit(kp,kd);
74
                        end
75
                        1:begin
76
                                ki=data;
77
                        end
78
                        2:begin
79
                                kd=data;
80
                                kpd=kp+kd;
81
                                of[0]=of_check_16bit(kp,kd);
82
                        end
83
                        3:begin
84
                                sp=data;
85
                        end
86
                        4:begin
87
                                pv=data;
88
                                err[1]=(~err[0])+1;
89
                                of[2]=of[1];
90
                                err[0]=sp+(~pv)+1;
91
                                of[1]=of_check_16bit(sp,((~pv)+1));
92
                                p={{16{err[0][15]}},err[0]}*{{16{ki[15]}},ki};
93
                                of[4]=of[4]|of_check_32bit(sigma,p);
94
                                sigma=sigma+p;
95
                                p={{16{err[0][15]}},err[0]}*{{16{kpd[15]}},kpd};
96
                                of[3]=of[4]|of_check_32bit(sigma,p);
97
                                a=sigma+p;
98
                                p={{16{err[1][15]}},err[1]}*{{16{kd[15]}},kd};
99
                                of[3]=of[3]|of_check_32bit(a,p);
100
                                un=a+p;
101
                        end
102
                endcase
103
        end
104
endtask
105
 
106
//wishbone master
107
wire    [3:0]TAG_s2m;
108
wire    [3:0]TAG_m2s;
109
wire    ACK_s2m;
110
wire    [31:0]ADR_m2s;
111
wire    CYC_m2s;
112
wire    [31:0]DAT_s2m;
113
wire    [31:0]DAT_m2s;
114
wire    ERR_s2m,RTY_s2m,STB_m2s,WE_m2s;
115
wire    [3:0]SEL_m2s;
116
 
117
wb_master       wb_master_0(
118
clk,
119
rst,
120
TAG_s2m,
121
TAG_m2s,
122
ACK_s2m,
123
ADR_m2s,
124
CYC_m2s,
125
DAT_s2m,
126
DAT_m2s,
127
ERR_s2m,
128
RTY_s2m,
129
SEL_m2s,
130
STB_m2s,
131
WE_m2s
132
);
133
 
134
 
135
reg     [31:0]rdata;
136
task    check_sb;
137
        input   [31:0]adr;
138
        begin
139
                wb_master_0.rd(adr,rdata);
140
                if(adr<=10*4)begin
141
                        if(rdata==value_sb[adr[15:2]])begin
142
                                $display("%8dns read correct value from address=%8h     data=%8h",$time,adr,rdata);
143
                        end
144
                        else begin
145
                                $display("%8dns read incorrect value from address=%8h   rdata=%8h       scoreboard=%8h",$time,adr,rdata,value_sb[adr[15:2]]);
146
                        end
147
                end
148
                else begin
149
                        if(rdata==0)begin
150
                                $display("%8dns read correct value from address=%8h     data=%8h",$time,adr,rdata);
151
                        end
152
                        else begin
153
                                $display("%8dns read incorrect value from address=%8h   rdata=%8h       scoreboard=0",$time,adr,rdata);
154
                        end
155
                end
156
 
157
        end
158
endtask
159
 
160
 
161
 
162
//instantiation of PID
163
wire    [31:0]o_un;
164
wire    valid;
165
 
166
PID     PID_0(
167
clk,
168
rst,
169
CYC_m2s,
170
STB_m2s,
171
WE_m2s,
172
ADR_m2s[15:0],
173
DAT_m2s,
174
ACK_s2m,
175
DAT_s2m,
176
o_un,
177
valid
178
);
179
 
180
 
181
//test procedure
182
reg     signed[31:0]rdata_1=0;
183
initial begin
184
        while(rst)begin
185
                @(posedge clk);
186
        end
187
        wb_master_0.wr(0*4,32'h80,4'b1111);
188
        update_sb(0*4,16'h80);
189
        wb_master_0.wr(1*4,32'h5,4'b1111);
190
        update_sb(1*4,16'h5);
191
        wb_master_0.wr(2*4,32'h5,4'b1111);
192
        update_sb(2*4,16'h5);
193
        wb_master_0.wr(3*4,32'hf87,4'b1111);
194
        update_sb(3*4,16'hf87);
195
        wb_master_0.wr(4*4,32'h0,4'b1111);
196
        update_sb(4*4,0);
197
        repeat(1000)begin
198
                wb_master_0.rd(8*4,rdata_1);
199
                check_sb(8*4);
200
                rdata_1=rdata_1>>>8;
201
                wb_master_0.wr(4*4,rdata_1,4'b1111);
202
                update_sb(4*4,rdata_1[15:0]);
203
 
204
        end
205
        #10 $finish;
206
end
207
 
208
 
209
 
210
 
211
 
212
 
213
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.