OpenCores
URL https://opencores.org/ocsvn/sdhc-sc-core/sdhc-sc-core/trunk

Subversion Repositories sdhc-sc-core

[/] [sdhc-sc-core/] [trunk/] [grpSdVerification/] [unitSdCardModel/] [src/] [SdCardModel.sv] - Blame information for rev 123

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 44 rkastl
//
2
// file: SdCardModel.sv
3
// author: Rainer Kastl
4
//
5
// Models a SDCard for verification
6
//
7
 
8
const logic cActivated = 1;
9
const logic cInactivated = 0;
10
 
11 100 rkastl
include "../../unitSdCardModel/src/Crc.sv";
12 58 rkastl
include "../../unitSdCardModel/src/SdCommand.sv";
13 100 rkastl
include "../../unitSdCardModel/src/SdData.sv";
14 44 rkastl
 
15
class SDCard;
16 122 rkastl
        local virtual ISdCard.Card ICard;
17 50 rkastl
 
18 53 rkastl
        local SDCardState state;
19 63 rkastl
        local RCA_t rca;
20 53 rkastl
        local SDCommandToken recvcmd;
21 59 rkastl
        local logic CCS;
22 100 rkastl
        local Mode_t mode;
23
        local DataMode_t datamode;
24 44 rkastl
 
25 53 rkastl
        local event CmdReceived, InitDone;
26 50 rkastl
 
27 122 rkastl
        function new(virtual ISdCard CardInterface, event CmdReceived, event InitDone);
28
                ICard = CardInterface;
29 57 rkastl
                state = new();
30 50 rkastl
                this.CmdReceived = CmdReceived;
31 53 rkastl
                this.InitDone = InitDone;
32 123 rkastl
                this.CCS = 0;
33 63 rkastl
                rca = 0;
34 100 rkastl
                mode = standard;
35 122 rkastl
                ICard.cbcard.Data <= 'z;
36 44 rkastl
        endfunction
37
 
38
        task reset();
39
        endtask
40
 
41 53 rkastl
        // Receive a command token and handle it
42 50 rkastl
        task recv();
43 122 rkastl
                ICard.cbcard.Cmd <= 'z;
44 96 rkastl
 
45 122 rkastl
                repeat(8) @ICard.cbcard;
46 95 rkastl
 
47 50 rkastl
                recvcmd = new();
48
 
49 122 rkastl
                wait(ICard.cbcard.Cmd == 0);
50 50 rkastl
                // Startbit
51 122 rkastl
                recvcmd.startbit = ICard.cbcard.Cmd;
52 50 rkastl
 
53 122 rkastl
                @ICard.cbcard;
54 50 rkastl
                // Transbit
55 122 rkastl
                recvcmd.transbit = ICard.cbcard.Cmd;
56 50 rkastl
 
57
                // CmdID
58
                for (int i = 5; i >= 0; i--) begin
59 122 rkastl
                        @ICard.cbcard;
60
                        recvcmd.id[i] = ICard.cbcard.Cmd;
61 50 rkastl
                end
62
 
63
                // Arg
64
                for (int i = 31; i >= 0; i--) begin
65 122 rkastl
                        @ICard.cbcard;
66
                        recvcmd.arg[i] = ICard.cbcard.Cmd;
67 50 rkastl
                end
68
 
69
                // CRC
70
                for (int i = 6; i >= 0; i--) begin
71 122 rkastl
                        @ICard.cbcard;
72
                        recvcmd.crc7[i] = ICard.cbcard.Cmd;
73 50 rkastl
                end
74
 
75
                // Endbit
76 122 rkastl
                @ICard.cbcard;
77
                recvcmd.endbit = ICard.cbcard.Cmd;
78 50 rkastl
 
79 53 rkastl
                recvcmd.checkFromHost();
80 50 rkastl
                -> CmdReceived;
81
        endtask
82
 
83 53 rkastl
        task automatic init();
84
                SDCommandR7 voltageresponse;
85 57 rkastl
                SDCommandR1 response;
86 59 rkastl
                SDCommandR3 acmd41response;
87 62 rkastl
                SDCommandR2 cidresponse;
88 59 rkastl
                SDOCR ocr;
89 63 rkastl
                SDCommandR6 rcaresponse;
90 100 rkastl
                logic data[$];
91
                SdData sddata;
92 53 rkastl
 
93
                // expect CMD0 so that state is clear
94
                recv();
95
                assert(recvcmd.id == cSdCmdGoIdleState);
96
 
97
                // expect CMD8: voltage and SD 2.00 compatible
98
                recv();
99
                assert(recvcmd.id == cSdCmdSendIfCond);
100
                assert(recvcmd.arg[12:8] == 'b0001); // Standard voltage
101
 
102
                // respond with R7: we are SD 2.00 compatible and compatible to the
103
                // voltage
104
                voltageresponse = new(recvcmd.arg);
105 122 rkastl
                voltageresponse.send(ICard);
106 53 rkastl
 
107 102 rkastl
                recvCMD55(0);
108 57 rkastl
 
109
                // expect ACMD41 with HCS = 1
110
                recv();
111
                assert(recvcmd.id == cSdCmdACMD41);
112
                assert(recvcmd.arg == cSdArgACMD41HCS);
113
 
114 59 rkastl
                // respond with R3
115
                ocr = new(CCS, cSdVoltageWindow);
116
                acmd41response = new(ocr);
117 122 rkastl
                acmd41response.send(ICard);
118 60 rkastl
 
119 102 rkastl
                recvCMD55(0);
120 59 rkastl
 
121 60 rkastl
                // expect ACMD41 with HCS = 1
122
                recv();
123
                assert(recvcmd.id == cSdCmdACMD41);
124
                assert(recvcmd.arg == cSdArgACMD41HCS);
125 98 rkastl
                state.AppCmd = 0;
126 60 rkastl
 
127
                // respond with R3
128
                ocr.setBusy(cOCRDone);
129
                acmd41response = new(ocr);
130 122 rkastl
                acmd41response.send(ICard);
131 60 rkastl
 
132 62 rkastl
                // expect CMD2
133
                recv();
134
                assert(recvcmd.id == cSdCmdAllSendCID);
135
 
136
                // respond with R2
137
                cidresponse = new();
138 122 rkastl
                cidresponse.send(ICard);
139 62 rkastl
 
140 63 rkastl
                // expect CMD3
141
                recv();
142
                assert(recvcmd.id == cSdCmdSendRelAdr);
143
 
144
                // respond with R3
145
                rcaresponse = new(rca, state);
146 122 rkastl
                rcaresponse.send(ICard);
147 63 rkastl
 
148 96 rkastl
                // expect CMD7
149
                recv();
150
                assert(recvcmd.id == cSdCmdSelCard);
151
                assert(recvcmd.arg[31:16] == rca);
152
 
153 98 rkastl
                // respond with R1, no busy
154
                state.ReadyForData = 1;
155
                response = new(cSdCmdSelCard, state);
156 122 rkastl
                response.send(ICard);
157 96 rkastl
 
158 99 rkastl
                // expect ACMD51
159 102 rkastl
                recvCMD55(rca);
160 99 rkastl
                recv();
161
                assert(recvcmd.id == cSdCmdSendSCR);
162
 
163
                // respond with R1
164
                response = new(cSdCmdSendSCR, state);
165 122 rkastl
                response.send(ICard);
166 99 rkastl
 
167 122 rkastl
                repeat(2) @ICard.cbcard;
168 100 rkastl
 
169
                // send dummy SCR
170 101 rkastl
                for (int i = 0; i < 64; i++)
171 100 rkastl
                        data.push_back(0);
172
 
173 101 rkastl
                data[63-50] = 1;
174
                data[63-48] = 1;
175 100 rkastl
 
176
                sddata = new(standard, widewidth);
177 122 rkastl
                sddata.send(ICard, data);
178 100 rkastl
 
179 102 rkastl
                // expect ACMD6
180
                recvCMD55(rca);
181
                recv();
182
                assert(recvcmd.id == cSdCmdSetBusWidth);
183
                assert(recvcmd.arg == 'h00000002);
184
 
185
                response = new(cSdCmdSetBusWidth, state);
186 122 rkastl
                response.send(ICard);
187 102 rkastl
 
188 103 rkastl
                sddata.mode = wide;
189 123 rkastl
                mode = wide;
190 103 rkastl
 
191
                // expect CMD6
192
                recv();
193
                assert(recvcmd.id == cSdCmdSwitchFuntion);
194
                assert(recvcmd.arg == 'h00FFFFF1);
195 122 rkastl
                response.send(ICard);
196 103 rkastl
 
197
                // send status data structure
198
                data = {};
199
 
200
                for (int i = 0; i < 512; i++)
201
                        data.push_back(0);
202
 
203
                data[511-400] = 1;
204
                data[511-376] = 1;
205 122 rkastl
                sddata.send(ICard, data);
206 104 rkastl
 
207
                // expect CMD6 with set
208
                recv();
209
                assert(recvcmd.id == cSdCmdSwitchFuntion);
210
                assert(recvcmd.arg == 'h80FFFFF1);
211 122 rkastl
                response.send(ICard);
212 104 rkastl
 
213
                // send status data structure
214
                data = {};
215 103 rkastl
 
216 104 rkastl
                for (int i = 0; i < 512; i++)
217
                        data.push_back(0);
218 103 rkastl
 
219 104 rkastl
                data[511-400] = 1;
220
                data[511-376] = 1;
221 122 rkastl
                sddata.send(ICard, data);
222 104 rkastl
 
223
                // switch to 50MHz
224 105 rkastl
                // expect CMD13
225
                recv();
226
                assert(recvcmd.id == cSdCmdSendStatus);
227
                assert(recvcmd.arg == rca);
228
                response = new(cSdCmdSendStatus, state);
229 122 rkastl
                response.send(ICard);
230 104 rkastl
 
231 122 rkastl
                -> InitDone;
232
 
233
        endtask
234
 
235
        task read();
236
                SDCommandR1 response;
237
                logic data[$];
238 123 rkastl
                SdData sddata = new(mode, usual);
239 122 rkastl
 
240 113 rkastl
                // expect Read
241
                recv();
242
                assert(recvcmd.id == cSdCmdReadSingleBlock);
243
                // recvcmd.arg = address
244
                response = new(cSdCmdReadSingleBlock, state);
245 122 rkastl
                response.send(ICard);
246 113 rkastl
 
247
                data = {};
248 114 rkastl
                for(int i = 0; i < (512 * 8); i++)
249 113 rkastl
                        data.push_back(1);
250
 
251 122 rkastl
                sddata.send(ICard, data);
252 123 rkastl
        endtask
253 113 rkastl
 
254 123 rkastl
        task write();
255
                SDCommandR1 response;
256 53 rkastl
 
257 123 rkastl
                // expect Write
258
                recv();
259
                assert(recvcmd.id == cSdCmdWriteSingleBlock);
260
                // recvcmd.arg = address
261
                response = new(cSdCmdWriteSingleBlock, state);
262
                response.send(ICard);
263
 
264
                // TODO: receive data
265
 
266 53 rkastl
        endtask
267 102 rkastl
 
268
        task recvCMD55(RCA_t rca);
269
                SDCommandR1 response;
270
 
271
                // expect CMD55
272
                recv();
273
                assert(recvcmd.id == cSdCmdNextIsACMD);
274
                assert(recvcmd.arg[31:16] == rca);
275
                state.recvCMD55();
276
 
277
                // respond with R1
278
                response = new(cSdCmdNextIsACMD, state);
279 122 rkastl
                response.send(ICard);
280 102 rkastl
        endtask
281 50 rkastl
 
282
        function automatic SDCommandToken getCmd();
283
                return recvcmd;
284
        endfunction
285
 
286 44 rkastl
endclass
287
 
288 106 rkastl
class NoSDCard extends SDCard;
289
 
290 122 rkastl
        function new(virtual ISdCard CardInterface, event CmdReceived, event InitDone);
291
                super.new(CardInterface, CmdReceived, InitDone);
292 106 rkastl
        endfunction
293
 
294
        task automatic init();
295
        endtask
296
 
297
endclass

powered by: WebSVN 2.1.0

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