-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcmd_int.v
285 lines (277 loc) · 6.37 KB
/
cmd_int.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
`define CMOD 'h434D4F44
`define DMOD 'h444D4F44
`define SADR 'h53414452
`define SLEN 'h534C454E
`define SRST 'h53525354
`define PICP 'h50494350
`define RST1 'h52535431
`define RST2 'h52535432
`define SCFQ 'h53434651
`define CINT 'h43494E54
`define NCON 'h4E434F4E
module cmd_int(
input wire i_clk,
input wire i_rst,
input wire i_data_valid,
input wire [7:0] i_data,
output reg o_buff_en,
output wire o_data_valid,
output wire [7:0] o_data,
output reg o_srst,
output reg o_pgrm_icap,
output wire [26:0] o_ddr_strt_addr,
output wire [19:0] o_trans_len,
output reg o_load,
input wire [19:0]i_icap_clk_cnt,
input wire [19:0]i_total_clk_cnt,
input wire i_uart_tx_buffer_full,
output reg o_config_clk
);
reg [7:0] cmd_reg [3:0];
reg [3:0] state;
wire [31:0] cmd;
reg [3:0] len_reg[0:5];
reg [3:0] addr_reg[0:5];
reg [4:0] len_cnt;
reg [7:0] tx_data;
reg tx_valid;
reg buff_wr_en;
reg [1:0] buff_wr_addr;
reg [1:0] buff_rd_addr;
reg [7:0] config_cnter[0:3];
wire [26:0] buff_ddr_strt_addr;
wire [19:0] buff_trans_len;
parameter CMOD = 'd0,
DMOD = 'd1,
SADR = 'd2,
SLEN = 'd3,
SRST = 'd4,
PICP = 'd5,
RST1 = 'd6,
RST2 = 'd7,
SCFQ = 'd8,
CINT = 'd9,
START = 'd10,
NCON = 'd11;
assign cmd = {cmd_reg[3],cmd_reg[2],cmd_reg[1],cmd_reg[0]};
assign o_data_valid = (state == DMOD) ? 1'b0: i_data_valid|tx_valid;
assign o_data = tx_valid ? tx_data : i_data;
assign o_trans_len = (state == START) ? buff_trans_len : len_reg[0]*100000 + len_reg[1]*10000 + len_reg[2]*1000 + len_reg[3]*100 + len_reg[4]*10 + len_reg[5];
assign o_ddr_strt_addr = (state == START) ? buff_ddr_strt_addr: addr_reg[0]*100000 + addr_reg[1]*10000 + addr_reg[2]*1000 + addr_reg[3]*100 + addr_reg[4]*10 + addr_reg[5];
//Store each valid byte into a shift register for generating commands.
always @(posedge i_clk)
begin
if(i_rst)
begin
cmd_reg[0] <= 0;
cmd_reg[1] <= 0;
cmd_reg[2] <= 0;
cmd_reg[3] <= 0;
end
else
begin
if(i_data_valid)
begin
cmd_reg[0] <= i_data;
cmd_reg[1] <= cmd_reg[0];
cmd_reg[2] <= cmd_reg[1];
cmd_reg[3] <= cmd_reg[2];
end
end
end
//Main state machine
always @(posedge i_clk)
begin
if(i_rst)
begin
state <= CMOD;
o_buff_en <= 1'b0;
len_cnt <= 0;
o_srst <= 1'b0;
o_pgrm_icap <= 1'b0;
o_load <= 1'b0;
tx_valid <= 1'b0;
o_config_clk <= 1'b0;
buff_wr_en <= 1'b0;
buff_wr_addr <= 2'b00;
config_cnter[0] <= 0;
config_cnter[1] <= 0;
config_cnter[2] <= 0;
config_cnter[3] <= 0;
end
else
begin
case(state)
CMOD:begin
o_buff_en <= 1'b0;
o_srst <= 1'b0;
o_pgrm_icap <= 1'b0;
o_load <= 1'b0;
len_cnt <= 0;
tx_valid <= 1'b0;
if(cmd == `DMOD)
begin
state <= DMOD;
buff_wr_en <= 1'b1;
end
else if(cmd == `SADR)
begin
state <= SADR;
end
else if(cmd == `SLEN)
begin
state <= SLEN;
end
else if(cmd == `SRST)
begin
state <= SRST;
end
else if(cmd == `PICP)
begin
state <= PICP;
o_pgrm_icap <= 1'b1;
end
else if(cmd == `RST1)
begin
state <= RST1;
len_cnt <= 19;
end
else if(cmd == `RST2)
begin
state <= RST2;
len_cnt <= 19;
end
else if(cmd == `SCFQ)
begin
state <= SCFQ;
o_config_clk <= 1'b1;
end
else if(cmd == `CINT)
begin
state <= CINT;
end
else if(cmd == `NCON)
begin
state <= NCON;
end
end
DMOD:begin
buff_wr_en <= 1'b0;
if(cmd == `CMOD)
begin
state <= CMOD;
o_buff_en <= 1'b0;
buff_wr_addr <= buff_wr_addr + 1;
end
o_buff_en <= 1'b1;
end
SADR:begin
if(i_data_valid)
begin
addr_reg[len_cnt] <= i_data - 'h30;
len_cnt <= len_cnt + 1;
if(len_cnt == 5)
begin
state <= CMOD;
o_load <= 1'b1;
end
end
end
SLEN:begin
if(i_data_valid)
begin
len_reg[len_cnt] <= i_data - 'h30;
len_cnt <= len_cnt + 1;
if(len_cnt == 5)
begin
state <= CMOD;
o_load <= 1'b1;
end
end
end
SRST:begin
if(cmd == `CMOD)
begin
state <= CMOD;
o_srst <= 1'b0;
end
else
o_srst <= 1'b1;
end
PICP:begin
o_pgrm_icap <= 1'b0;
if(cmd == `CMOD)
begin
state <= CMOD;
end
end
RST1:begin
if(~i_uart_tx_buffer_full & len_cnt != 31)
begin
tx_data <= i_icap_clk_cnt[len_cnt] + 'h30;
len_cnt <= len_cnt - 1;
tx_valid <= 1'b1;
end
else
tx_valid <= 1'b0;
if(cmd == `CMOD)
begin
state <= CMOD;
end
end
RST2:begin
if(~i_uart_tx_buffer_full & len_cnt != 31)
begin
tx_data <= i_total_clk_cnt[len_cnt] + 'h30;
len_cnt <= len_cnt - 1;
tx_valid <= 1'b1;
end
else
tx_valid <= 1'b0;
if(cmd == `CMOD)
begin
state <= CMOD;
end
end
SCFQ:begin
if(cmd == `CMOD)
begin
state <= CMOD;
end
o_config_clk <= 1'b0;
end
CINT:begin
if(i_data_valid)
begin
buff_rd_addr <= i_data - 'h30;
o_load <= 1'b1;
state <= START;
end
end
START:begin
o_pgrm_icap <= 1'b1;
o_load <= 1'b0;
state <= PICP;
config_cnter[buff_rd_addr] <= config_cnter[buff_rd_addr] + 1;
end
NCON:begin
if(i_data_valid)
begin
tx_data <= config_cnter[i_data - 'h30] + 'h30;
tx_valid <= 1'b1;
tx_valid <= 1'b1;
state <= CMOD;
end
end
endcase
end
end
buffer addr_len_buffer(
.i_clk(i_clk),
.i_wr_addr(buff_wr_addr),
.i_rd_addr(buff_rd_addr),
.i_wr_en(buff_wr_en),
.i_wr_data({o_ddr_strt_addr,o_trans_len}),
.o_rd_data({buff_ddr_strt_addr,buff_trans_len})
);
endmodule