-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.v
329 lines (314 loc) · 6.45 KB
/
train.v
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
`include "dport.vh"
module train(
input wire clk,
input wire [19:0] regauxaddr,
input wire [7:0] regauxwdata,
input wire regauxreq,
input wire regauxwr,
output reg regauxack,
output reg regauxerr,
output reg [7:0] regauxrdata,
input wire [31:0] phyctl,
output wire [31:0] physts,
output reg [19:0] auxaddr,
output reg [7:0] auxwdata,
output reg auxreq,
output reg auxwr,
input wire auxack,
input wire [7:0] auxrdata,
input wire auxerr,
output reg reset,
output reg speed,
output reg twolane,
output reg [1:0] preemph,
output reg [1:0] swing,
output reg [1:0] phymode,
output reg [2:0] prbssel
);
localparam TRAINPERIOD = 1_000_000;
localparam CHECKPERIOD = 25_000_000;
localparam TRAINLIMIT = 5;
localparam ACTLIMIT = 5;
wire automode = phyctl[31];
reg [1:0] muxstate, muxstate_;
localparam MUXIDLE = 0;
localparam MUXTR = 1;
localparam MUXREG = 2;
reg [19:0] traddr;
reg [7:0] trwdata, trrdata, trrdata_, regauxrdata_;
reg trreq, track, trerr, trwr, track_, trerr_, regauxack_, regauxerr_;
always @(posedge clk) begin
muxstate <= muxstate_;
track <= track_;
trerr <= trerr_;
trrdata <= trrdata_;
regauxack <= regauxack_;
regauxerr <= regauxerr_;
regauxrdata <= regauxrdata_;
end
always @(*) begin
auxaddr = 20'bx;
auxwdata = 8'bx;
auxreq = 0;
auxwr = 0;
muxstate_ = muxstate;
track_ = 0;
trrdata_ = trrdata;
trerr_ = trerr;
regauxack_ = 0;
regauxrdata_ = regauxrdata;
regauxerr_ = regauxerr;
case(muxstate)
MUXIDLE: begin
if(regauxreq)
muxstate_ = MUXREG;
else if(trreq)
muxstate_ = MUXTR;
end
MUXTR: begin
auxaddr = traddr;
auxwdata = trwdata;
auxreq = 1;
auxwr = trwr;
if(auxack) begin
track_ = 1;
trrdata_ = auxrdata;
trerr_ = auxerr;
muxstate_ = MUXIDLE;
end
end
MUXREG: begin
auxaddr = regauxaddr;
auxwdata = regauxwdata;
auxreq = 1;
auxwr = regauxwr;
if(auxack) begin
regauxack_ = 1;
regauxrdata_ = auxrdata;
regauxerr_ = auxerr;
muxstate_ = MUXIDLE;
end
end
endcase
end
reg [3:0] state, state_;
reg [3:0] actctr, actctr_;
reg [31:0] timer, timer_;
reg speed_, twolane_, reset_;
reg [1:0] swing_, preemph_, phymode_;
assign physts = {state != NOAUTO, 7'b0, actctr, state, 2'b0, reset, prbssel, phymode, swing, preemph, 2'b0, twolane, speed};
localparam NODEV = 0;
localparam CHECKNODEV = 1;
localparam SETLINKBW = 2;
localparam SETLANECNT = 3;
localparam SETTRAIN0 = 4;
localparam WAITTRAIN0 = 5;
localparam CHECKTRAIN0 = 6;
localparam SETTRAIN1 = 7;
localparam WAITTRAIN1 = 8;
localparam CHECKTRAIN1 = 9;
localparam SETACTIVE = 10;
localparam ACTIVE = 11;
localparam CHECKACTIVE = 12;
localparam CHANGESET = 13;
localparam NOAUTO = 14;
always @(posedge clk) begin
state <= state_;
speed <= speed_;
twolane <= twolane_;
preemph <= preemph_;
swing <= swing_;
phymode <= phymode_;
timer <= timer_;
actctr <= actctr_;
reset <= reset_;
end
always @(*) begin
traddr = 20'bx;
trwdata = 8'bx;
trreq = 0;
trwr = 0;
state_ = state;
speed_ = speed;
twolane_ = twolane;
preemph_ = preemph;
swing_ = swing;
phymode_ = phymode;
actctr_ = actctr;
reset_ = 1;
timer_ = timer != 0 ? timer - 1 : 0;
prbssel = 0;
case(state)
NODEV: begin
preemph_ = 0;
swing_ = 0;
speed_ = 1;
twolane_ = 1;
if(timer == 0)
state_ = CHECKNODEV;
end
CHECKNODEV: begin
traddr = 'h0;
trreq = 1;
if(track)
if(trerr) begin
state_ = NODEV;
timer_ = CHECKPERIOD;
end else
state_ = SETLINKBW;
end
SETLINKBW: begin
traddr = 'h100;
trwdata = speed ? 10 : 6;
trreq = 1;
trwr = 1;
if(track)
state_ = trerr ? NODEV : SETLANECNT;
end
SETLANECNT: begin
traddr = 'h101;
trwdata = twolane ? 2 : 1;
trreq = 1;
trwr = 1;
if(track)
state_ = trerr ? NODEV : SETTRAIN0;
end
SETTRAIN0: begin
traddr = 'h102;
trwdata = 'h21;
trreq = 1;
trwr = 1;
if(track)
if(trerr)
state_ = NODEV;
else begin
phymode_ = 2;
timer_ = TRAINPERIOD;
state_ = WAITTRAIN0;
actctr_ = 0;
end
end
WAITTRAIN0: begin
if(timer == 0)
state_ = CHECKTRAIN0;
end
CHECKTRAIN0: begin
traddr = 'h202;
trreq = 1;
if(track) begin
if(trerr)
state_ = NODEV;
else if(trrdata[0] && (!twolane || trrdata[4]))
state_ = SETTRAIN1;
else if(actctr == TRAINLIMIT)
state_ = CHANGESET;
else begin
state_ = WAITTRAIN0;
timer_ = TRAINPERIOD;
actctr_ = actctr + 1;
end
end
end
SETTRAIN1: begin
traddr = 'h102;
trwdata = 'h22;
trreq = 1;
trwr = 1;
if(track)
if(trerr)
state_ = NODEV;
else begin
phymode_ = 3;
state_ = WAITTRAIN1;
timer_ = TRAINPERIOD;
actctr_ = 0;
end
end
WAITTRAIN1: begin
if(timer == 0)
state_ = CHECKTRAIN1;
end
CHECKTRAIN1: begin
traddr = 'h202;
trreq = 1;
if(track) begin
if(trerr)
state_ = NODEV;
else if(trrdata[2:0] == 7 && (!twolane || trrdata[6:4] == 7))
state_ = SETACTIVE;
else if(actctr == TRAINLIMIT)
state_ = CHANGESET;
else begin
state_ = WAITTRAIN0;
timer_ = TRAINPERIOD;
actctr_ = actctr + 1;
end
end
end
SETACTIVE: begin
traddr = 'h102;
trwdata = 0;
trreq = 1;
trwr = 1;
if(track)
if(trerr)
state_ = NODEV;
else begin
state_ = ACTIVE;
timer_ = CHECKPERIOD;
phymode_ = 1;
actctr_ = 0;
end
end
ACTIVE: begin
reset_ = 0;
if(timer == 0)
state_ = CHECKACTIVE;
end
CHECKACTIVE: begin
reset_ = 0;
traddr = 'h202;
trreq = 1;
if(track)
if(trerr)
state_ = NODEV;
else if(trrdata[2:0] == 7 && (!twolane || trrdata[6:4] == 7)) begin
timer_ = CHECKPERIOD;
state_ = ACTIVE;
actctr_ = actctr == ACTLIMIT ? actctr : actctr + 1;
end else if(actctr == ACTLIMIT) begin
phymode_ = 2;
state_ = SETTRAIN0;
end else
state_ = CHANGESET;
end
CHANGESET: begin
preemph_ = preemph + 1;
state_ = SETTRAIN0;
if(swing + preemph == 3) begin
preemph_ = 0;
swing_ = swing + 1;
if(preemph == 3) begin
speed_ = !speed;
if(!speed)
twolane_ = !twolane;
state_ = SETLINKBW;
end
end
end
NOAUTO: begin
speed_ = phyctl[0];
twolane_ = phyctl[1];
preemph_ = phyctl[5:4];
swing_ = phyctl[7:6];
phymode_ = phyctl[9:8];
prbssel = phyctl[12:10];
reset_ = phyctl[13];
if(automode)
state_ = NODEV;
end
endcase
if(!automode)
state_ = NOAUTO;
end
endmodule