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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [rtl/] [soc/] [driver_sd/] [dat.v] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 alfik
/*
2
 * This file is subject to the terms and conditions of the BSD License. See
3
 * the file "LICENSE" in the main directory of this archive for more details.
4
 *
5
 * Copyright (C) 2014 Aleksander Osman
6
 */
7
 
8
module dat(
9
    input               clk,
10
    input               rst_n,
11
 
12
    //
13
    input               sd_clk_is_one,
14
 
15
    //
16
    input               wr_async_data_ready,
17
    input       [31:0]  wr_async_data,
18
    output reg          wr_data_done,
19
    output reg          wr_data_last_in_sector,
20
    output reg          wr_error,
21
    output reg          wr_finished_sector,
22
 
23
    //
24
    input               rd_async_start,
25
    input               rd_async_abort,
26
    output reg          rd_data_done,
27
    output reg          rd_data_last_in_sector,
28
    output reg  [31:0]  rd_data,
29
    output reg          rd_error,
30
 
31
    //
32
    output              current_dat0,
33
 
34
    //
35
    inout       [3:0]   sd_dat
36
);
37
 
38
//------------------------------------------------------------------------------
39
//------------------------------------------------------------------------------
40
//------------------------------------------------------------------------------
41
 
42
reg sd_dat_enable;
43
always @(posedge clk or negedge rst_n) begin
44
    if(rst_n == 1'b0)       sd_dat_enable <= 1'b0;
45
    else if(sd_clk_is_one)  sd_dat_enable <= wr_start || wr_cnt > 11'd0;
46
end
47
 
48
reg [3:0] sd_dat_output;
49
always @(posedge clk or negedge rst_n) begin
50
    if(rst_n == 1'b0)       sd_dat_output <= 4'hF;
51
    else if(sd_clk_is_one)  sd_dat_output <= (wr_start)? 4'h0 : (wr_cnt <= 11'd17 && wr_cnt >= 11'd2)? { wr_crc_3[0], wr_crc_2[0], wr_crc_1[0], wr_crc_0[0] } : { wr_val_3[7], wr_val_2[7], wr_val_1[7], wr_val_0[7] };
52
end
53
 
54
assign sd_dat = (sd_dat_enable)? sd_dat_output : 4'bZ;
55
 
56
reg [3:0] sd_dat_input;
57
always @(posedge clk or negedge rst_n) begin
58
    if(rst_n == 1'b0)           sd_dat_input <= 4'b1111;
59
    else if(~(sd_clk_is_one))   sd_dat_input <= sd_dat;
60
end
61
 
62
assign current_dat0 = sd_dat_input[0];
63
 
64
//------------------------------------------------------------------------------
65
//------------------------------------------------------------------------------
66
//------------------------------------------------------------------------------
67
 
68
/*
69
input               wr_async_data_ready,
70
input       [31:0]  wr_async_data,
71
output reg          wr_data_done,
72
output reg          wr_error,
73
output reg          wr_finished,
74
*/
75
 
76
reg [1:0] wr_start_delay_cnt;
77
always @(posedge clk or negedge rst_n) begin
78
    if(rst_n == 1'b0)                                                                                                   wr_start_delay_cnt <= 2'd2;
79
    else if(sd_clk_is_one && wr_async_data_ready && wr_cnt == 11'd0 && ~(wr_in_progress) && wr_start_delay_cnt > 2'd0)  wr_start_delay_cnt <= wr_start_delay_cnt - 2'd1;
80
    else if(wr_cnt > 11'd0)                                                                                             wr_start_delay_cnt <= 2'd2;
81
end
82
 
83
wire wr_start = sd_clk_is_one && wr_async_data_ready && wr_cnt == 11'd0 && ~(wr_in_progress) && wr_start_delay_cnt == 2'd0;
84
wire wr_load  = wr_start || (sd_clk_is_one && wr_async_data_ready && wr_cnt >= 11'd26 && wr_cnt[2:0] == 3'b010) || (wr_missed && wr_async_data_ready);
85
 
86
reg wr_missed;
87
always @(posedge clk or negedge rst_n) begin
88
    if(rst_n == 1'b0)                                                                               wr_missed <= 1'b0;
89
    else if(sd_clk_is_one && ~(wr_async_data_ready) && wr_cnt >= 11'd26 && wr_cnt[2:0] == 3'b010)   wr_missed <= 1'b1;
90
    else if(wr_async_data_ready)                                                                    wr_missed <= 1'b0;
91
end
92
 
93
always @(posedge clk or negedge rst_n) begin
94
    if(rst_n == 1'b0)   wr_data_done <= 1'b0;
95
    else                wr_data_done <= wr_load;
96
end
97
 
98
always @(posedge clk or negedge rst_n) begin
99
    if(rst_n == 1'b0)   wr_data_last_in_sector <= 1'b0;
100
    else                wr_data_last_in_sector <= wr_load && (wr_cnt == 11'd26 || wr_cnt == 11'd25);
101
end
102
 
103
reg [10:0] wr_cnt;
104
always @(posedge clk or negedge rst_n) begin
105
    if(rst_n == 1'b0)                           wr_cnt <= 11'd0;
106
    else if(wr_start)                           wr_cnt <= 11'd1041;
107
    else if(sd_clk_is_one && wr_cnt > 11'd0)    wr_cnt <= wr_cnt - 11'd1;
108
end
109
 
110
wire wr_resp_start = sd_clk_is_one && wr_cnt == 11'd1;
111
 
112
reg wr_resp_awaiting;
113
always @(posedge clk or negedge rst_n) begin
114
    if(rst_n == 1'b0)                                                       wr_resp_awaiting <= 1'b0;
115
    else if(wr_resp_start)                                                  wr_resp_awaiting <= 1'b1;
116
    else if(sd_clk_is_one && wr_resp_awaiting && sd_dat_input[0] == 1'b0)   wr_resp_awaiting <= 1'b0;
117
end
118
 
119
reg [2:0] wr_resp_cnt;
120
always @(posedge clk or negedge rst_n) begin
121
    if(rst_n == 1'b0)                                                       wr_resp_cnt <= 3'd0;
122
    else if(sd_clk_is_one && wr_resp_awaiting && sd_dat_input[0] == 1'b0)   wr_resp_cnt <= 3'd4;
123
    else if(sd_clk_is_one && wr_resp_cnt > 3'd0)                            wr_resp_cnt <= wr_resp_cnt - 3'd1;
124
end
125
 
126
wire wr_in_progress_end = wr_in_progress && ((sd_clk_is_one && wr_error_cnt == 27'h7FFFFFF) || (sd_clk_is_one && wr_cnt == 11'd0 && ~(wr_resp_awaiting) && wr_resp_cnt == 3'd0 && sd_dat_input[0] == 1'b1));
127
 
128
reg wr_in_progress;
129
always @(posedge clk or negedge rst_n) begin
130
    if(rst_n == 1'b0)               wr_in_progress <= 1'b0;
131
    else if(wr_start)               wr_in_progress <= 1'b1;
132
    else if(wr_in_progress_end)     wr_in_progress <= 1'b0;
133
end
134
 
135
always @(posedge clk or negedge rst_n) begin
136
    if(rst_n == 1'b0)   wr_finished_sector <= 1'b0;
137
    else                wr_finished_sector <= wr_in_progress_end;
138
end
139
 
140
//------------------------------------------------------------------------------
141
 
142
wire wr_resp_now_in_error = sd_clk_is_one && (
143
    (wr_resp_cnt == 3'd4 && sd_dat_input[0] == 1'b1) || //crc status invalid
144
    (wr_resp_cnt == 3'd3 && sd_dat_input[0] == 1'b0) || //crc status invalid
145
    (wr_resp_cnt == 3'd2 && sd_dat_input[0] == 1'b1) || //crc status invalid
146
    (wr_resp_cnt == 3'd1 && sd_dat_input[0] == 1'b0)    //end bit is '0'
147
);
148
 
149
reg [26:0] wr_error_cnt;
150
always @(posedge clk or negedge rst_n) begin
151
    if(rst_n == 1'b0)                               wr_error_cnt <= 27'd0;
152
    else if(~(wr_in_progress))                      wr_error_cnt <= 27'd0;
153
    else if(wr_resp_start)                          wr_error_cnt <= 27'd1;
154
    else if(sd_clk_is_one && wr_error_cnt > 27'd0)  wr_error_cnt <= wr_error_cnt + 27'd1;
155
end
156
 
157
always @(posedge clk or negedge rst_n) begin
158
    if(rst_n == 1'b0)                                       wr_error <= 1'b0;
159
    else if(wr_start)                                       wr_error <= 1'b0;
160
    else if(wr_resp_now_in_error)                           wr_error <= 1'b1;
161
    else if(sd_clk_is_one && wr_error_cnt == 27'h7FFFFFF)   wr_error <= 1'b1;
162
end
163
 
164
//------------------------------------------------------------------------------
165
 
166
reg [7:0] wr_val_0;
167
always @(posedge clk or negedge rst_n) begin
168
    if(rst_n == 1'b0)       wr_val_0 <= 8'd0;
169
    else if(wr_load)        wr_val_0 <= { wr_async_data[4], wr_async_data[0], wr_async_data[12], wr_async_data[8], wr_async_data[20], wr_async_data[16], wr_async_data[28], wr_async_data[24] };
170
    else if(sd_clk_is_one)  wr_val_0 <= { wr_val_0[6:0], 1'b1 }; //fill with 1 important
171
end
172
 
173
reg [7:0] wr_val_1;
174
always @(posedge clk or negedge rst_n) begin
175
    if(rst_n == 1'b0)       wr_val_1 <= 8'd0;
176
    else if(wr_load)        wr_val_1 <= { wr_async_data[5], wr_async_data[1], wr_async_data[13], wr_async_data[9], wr_async_data[21], wr_async_data[17], wr_async_data[29], wr_async_data[25] };
177
    else if(sd_clk_is_one)  wr_val_1 <= { wr_val_1[6:0], 1'b1 }; //fill with 1 important
178
end
179
 
180
reg [7:0] wr_val_2;
181
always @(posedge clk or negedge rst_n) begin
182
    if(rst_n == 1'b0)       wr_val_2 <= 8'd0;
183
    else if(wr_load)        wr_val_2 <= { wr_async_data[6], wr_async_data[2], wr_async_data[14], wr_async_data[10], wr_async_data[22], wr_async_data[18], wr_async_data[30], wr_async_data[26] };
184
    else if(sd_clk_is_one)  wr_val_2 <= { wr_val_2[6:0], 1'b1 }; //fill with 1 important
185
end
186
 
187
reg [7:0] wr_val_3;
188
always @(posedge clk or negedge rst_n) begin
189
    if(rst_n == 1'b0)       wr_val_3 <= 8'd0;
190
    else if(wr_load)        wr_val_3 <= { wr_async_data[7], wr_async_data[3], wr_async_data[15], wr_async_data[11], wr_async_data[23], wr_async_data[19], wr_async_data[31], wr_async_data[27] };
191
    else if(sd_clk_is_one)  wr_val_3 <= { wr_val_3[6:0], 1'b1 }; //fill with 1 important
192
end
193
 
194
reg [15:0] wr_crc_0;
195
always @(posedge clk or negedge rst_n) begin
196
    if(rst_n == 1'b0)                           wr_crc_0 <= 16'd0;
197
    else if(sd_clk_is_one && wr_cnt >= 11'd18)  wr_crc_0 <= { wr_val_0[7] ^ wr_crc_0[0], wr_crc_0[15:12], wr_crc_0[11] ^ wr_val_0[7] ^ wr_crc_0[0], wr_crc_0[10:5], wr_crc_0[4] ^ wr_val_0[7] ^ wr_crc_0[0], wr_crc_0[3:1] };
198
    else if(sd_clk_is_one)                      wr_crc_0 <= { 1'b0, wr_crc_0[15:1] }; //fill with 0 important
199
end
200
 
201
reg [15:0] wr_crc_1;
202
always @(posedge clk or negedge rst_n) begin
203
    if(rst_n == 1'b0)                           wr_crc_1 <= 16'd0;
204
    else if(sd_clk_is_one && wr_cnt >= 11'd18)  wr_crc_1 <= { wr_val_1[7] ^ wr_crc_1[0], wr_crc_1[15:12], wr_crc_1[11] ^ wr_val_1[7] ^ wr_crc_1[0], wr_crc_1[10:5], wr_crc_1[4] ^ wr_val_1[7] ^ wr_crc_1[0], wr_crc_1[3:1] };
205
    else if(sd_clk_is_one)                      wr_crc_1 <= { 1'b0, wr_crc_1[15:1] }; //fill with 0 important
206
end
207
 
208
reg [15:0] wr_crc_2;
209
always @(posedge clk or negedge rst_n) begin
210
    if(rst_n == 1'b0)                           wr_crc_2 <= 16'd0;
211
    else if(sd_clk_is_one && wr_cnt >= 11'd18)  wr_crc_2 <= { wr_val_2[7] ^ wr_crc_2[0], wr_crc_2[15:12], wr_crc_2[11] ^ wr_val_2[7] ^ wr_crc_2[0], wr_crc_2[10:5], wr_crc_2[4] ^ wr_val_2[7] ^ wr_crc_2[0], wr_crc_2[3:1] };
212
    else if(sd_clk_is_one)                      wr_crc_2 <= { 1'b0, wr_crc_2[15:1] }; //fill with 0 important
213
end
214
 
215
reg [15:0] wr_crc_3;
216
always @(posedge clk or negedge rst_n) begin
217
    if(rst_n == 1'b0)                           wr_crc_3 <= 16'd0;
218
    else if(sd_clk_is_one && wr_cnt >= 11'd18)  wr_crc_3 <= { wr_val_3[7] ^ wr_crc_3[0], wr_crc_3[15:12], wr_crc_3[11] ^ wr_val_3[7] ^ wr_crc_3[0], wr_crc_3[10:5], wr_crc_3[4] ^ wr_val_3[7] ^ wr_crc_3[0], wr_crc_3[3:1] };
219
    else if(sd_clk_is_one)                      wr_crc_3 <= { 1'b0, wr_crc_3[15:1] }; //fill with 0 important
220
end
221
 
222
//------------------------------------------------------------------------------
223
//------------------------------------------------------------------------------
224
//------------------------------------------------------------------------------
225
 
226
/*
227
input               rd_async_start,
228
input               rd_async_abort,
229
output reg          rd_data_done,
230
output reg          rd_data_last_in_sector,
231
output reg  [31:0]  rd_data,
232
output reg          rd_error,
233
*/
234
 
235
wire rd_start       = sd_clk_is_one && rd_async_in_progress && ~(rd_now_in_error) && ~(rd_error) && ~(rd_awaiting) && rd_cnt <= 11'd1;
236
wire rd_start_block = sd_clk_is_one && rd_awaiting && sd_dat_input == 4'h0;
237
wire rd_active      = sd_clk_is_one && rd_cnt > 11'd0;
238
wire rd_load        = sd_clk_is_one && rd_cnt >= 11'd18 && rd_cnt[2:0] == 3'b010 && ~(rd_async_abort);
239
 
240
reg rd_async_in_progress;
241
always @(posedge clk or negedge rst_n) begin
242
    if(rst_n == 1'b0)       rd_async_in_progress <= 1'b0;
243
    else if(rd_async_abort) rd_async_in_progress <= 1'b0;
244
    else if(rd_async_start) rd_async_in_progress <= 1'b1;
245
end
246
 
247
reg rd_awaiting;
248
always @(posedge clk or negedge rst_n) begin
249
    if(rst_n == 1'b0)       rd_awaiting <= 1'b0;
250
    else if(rd_async_abort) rd_awaiting <= 1'b0;
251
    else if(rd_start)       rd_awaiting <= 1'b1;
252
    else if(rd_start_block) rd_awaiting <= 1'b0;
253
end
254
 
255
reg [10:0] rd_cnt;
256
always @(posedge clk or negedge rst_n) begin
257
    if(rst_n == 1'b0)       rd_cnt <= 11'd0;
258
    else if(rd_async_abort) rd_cnt <= 11'd0;
259
    else if(rd_start_block) rd_cnt <= 11'd1041;
260
    else if(rd_active)      rd_cnt <= rd_cnt - 11'd1;
261
end
262
 
263
always @(posedge clk or negedge rst_n) begin
264
    if(rst_n == 1'b0)       rd_data_done <= 1'b0;
265
    else                    rd_data_done <= rd_load;
266
end
267
 
268
always @(posedge clk or negedge rst_n) begin
269
    if(rst_n == 1'b0)       rd_data_last_in_sector <= 1'b0;
270
    else                    rd_data_last_in_sector <= rd_load && rd_cnt == 11'd18;
271
end
272
 
273
always @(posedge clk or negedge rst_n) begin
274
    if(rst_n == 1'b0)       rd_data <= 32'b0;
275
    else if(rd_load)        rd_data <= { rd_val[3:0], sd_dat_input, rd_val[11:4], rd_val[19:12], rd_val[27:20] };
276
end
277
 
278
//------------------------------------------------------------------------------
279
 
280
reg [26:0] rd_error_cnt;
281
always @(posedge clk or negedge rst_n) begin
282
    if(rst_n == 1'b0)                               rd_error_cnt <= 27'd0;
283
    else if(rd_async_abort)                         rd_error_cnt <= 27'd0;
284
    else if(rd_start_block)                         rd_error_cnt <= 27'd0;
285
    else if(rd_start)                               rd_error_cnt <= 27'd1;
286
    else if(sd_clk_is_one && rd_error_cnt > 27'd0)  rd_error_cnt <= rd_error_cnt + 27'd1;
287
end
288
 
289
wire rd_now_in_error = rd_active && (
290
    (rd_cnt == 11'd1 && sd_dat_input != 4'hF) ||                                                    //end bit is '0'
291
    (rd_cnt <= 11'd17 && rd_cnt >= 11'd2 && sd_dat_input != { rd_3[0], rd_2[0], rd_1[0], rd_0[0] }) //crc16 invalid
292
);
293
 
294
always @(posedge clk or negedge rst_n) begin
295
    if(rst_n == 1'b0)                                       rd_error <= 1'b0;
296
    else if(rd_async_abort)                                 rd_error <= 1'b0;
297
    else if(rd_now_in_error)                                rd_error <= 1'b1;
298
    else if(sd_clk_is_one && rd_error_cnt == 27'h7FFFFFF)   rd_error <= 1'b1;
299
end
300
 
301
//------------------------------------------------------------------------------
302
 
303
reg [27:0] rd_val;
304
always @(posedge clk or negedge rst_n) begin
305
    if(rst_n == 1'b0)   rd_val <= 28'd0;
306
    else if(rd_active)  rd_val <= { rd_val[23:0], sd_dat_input };
307
end
308
 
309
reg [15:0] rd_0;
310
always @(posedge clk or negedge rst_n) begin
311
    if(rst_n == 1'b0)                           rd_0 <= 16'd0;
312
    else if(sd_clk_is_one && rd_cnt >= 11'd18)  rd_0 <= { sd_dat_input[0] ^ rd_0[0], rd_0[15:12], rd_0[11] ^ sd_dat_input[0] ^ rd_0[0], rd_0[10:5], rd_0[4] ^ sd_dat_input[0] ^ rd_0[0], rd_0[3:1] };
313
    else if(sd_clk_is_one)                      rd_0 <= { 1'b0, rd_0[15:1] }; //fill with 0 important
314
end
315
 
316
reg [15:0] rd_1;
317
always @(posedge clk or negedge rst_n) begin
318
    if(rst_n == 1'b0)                           rd_1 <= 16'd0;
319
    else if(sd_clk_is_one && rd_cnt >= 11'd18)  rd_1 <= { sd_dat_input[1] ^ rd_1[0], rd_1[15:12], rd_1[11] ^ sd_dat_input[1] ^ rd_1[0], rd_1[10:5], rd_1[4] ^ sd_dat_input[1] ^ rd_1[0], rd_1[3:1] };
320
    else if(sd_clk_is_one)                      rd_1 <= { 1'b0, rd_1[15:1] }; //fill with 0 important
321
end
322
 
323
reg [15:0] rd_2;
324
always @(posedge clk or negedge rst_n) begin
325
    if(rst_n == 1'b0)                           rd_2 <= 16'd0;
326
    else if(sd_clk_is_one && rd_cnt >= 11'd18)  rd_2 <= { sd_dat_input[2] ^ rd_2[0], rd_2[15:12], rd_2[11] ^ sd_dat_input[2] ^ rd_2[0], rd_2[10:5], rd_2[4] ^ sd_dat_input[2] ^ rd_2[0], rd_2[3:1] };
327
    else if(sd_clk_is_one)                      rd_2 <= { 1'b0, rd_2[15:1] }; //fill with 0 important
328
end
329
 
330
reg [15:0] rd_3;
331
always @(posedge clk or negedge rst_n) begin
332
    if(rst_n == 1'b0)                           rd_3 <= 16'd0;
333
    else if(sd_clk_is_one && rd_cnt >= 11'd18)  rd_3 <= { sd_dat_input[3] ^ rd_3[0], rd_3[15:12], rd_3[11] ^ sd_dat_input[3] ^ rd_3[0], rd_3[10:5], rd_3[4] ^ sd_dat_input[3] ^ rd_3[0], rd_3[3:1] };
334
    else if(sd_clk_is_one)                      rd_3 <= { 1'b0, rd_3[15:1] }; //fill with 0 important
335
end
336
 
337
//------------------------------------------------------------------------------
338
 
339
endmodule

powered by: WebSVN 2.1.0

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