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

Subversion Repositories sdspi

[/] [sdspi/] [trunk/] [bench/] [cpp/] [sdspisim.cpp] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
3
//
4
// Filename:    sdspisim.cpp
5
//
6
// Project:     Wishbone Controlled SD-Card Controller over SPI port
7
//
8
// Purpose:     This library simulates the operation of a SPI commanded SD-Card,
9
//              such as might be found on a XuLA2-LX25 board made by xess.com.
10
//
11
//      This simulator is for testing use in a Verilator/C++ environment, where
12
//      it would be used in place of the actual hardware.
13
//
14
// Creator:     Dan Gisselquist
15
//              Gisselquist Technology, LLC
16
//
17
///////////////////////////////////////////////////////////////////////////
18
//
19
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
20
//
21
// This program is free software (firmware): you can redistribute it and/or
22
// modify it under the terms of  the GNU General Public License as published
23
// by the Free Software Foundation, either version 3 of the License, or (at
24
// your option) any later version.
25
//
26
// This program is distributed in the hope that it will be useful, but WITHOUT
27
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
28
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29
// for more details.
30
//
31
// You should have received a copy of the GNU General Public License along
32
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
33
// target there if the PDF file isn't present.)  If not, see
34
// <http://www.gnu.org/licenses/> for a copy.
35
//
36
// License:     GPL, v3, as defined and found on www.gnu.org,
37
//              http://www.gnu.org/licenses/gpl.html
38
//
39
//
40
///////////////////////////////////////////////////////////////////////////
41
#include <stdio.h>
42
#include <string.h>
43
#include <assert.h>
44
#include <stdlib.h>
45
 
46
#include "sdspisim.h"
47
 
48
static  const unsigned
49
        MICROSECONDS = 80, // Clocks in a microsecond
50
        MILLISECONDS = MICROSECONDS * 1000,
51
        tRESET = 4*MILLISECONDS; // Just a wild guess
52
/*
53
static  const unsigned  DEVID = 0x0115,
54
        DEVESD = 0x014,
55
        MICROSECONDS = 100,
56
        MILLISECONDS = MICROSECONDS * 1000,
57
        SECONDS = MILLISECONDS * 1000,
58
        tW     =   50 * MICROSECONDS, // write config cycle time
59
        tBE    =   32 * SECONDS,
60
        tDP    =   10 * SECONDS,
61
        tRES   =   30 * SECONDS,
62
// Shall we artificially speed up this process?
63
        tPP    = 12 * MICROSECONDS,
64
        tSE    = 15 * MILLISECONDS;
65
// or keep it at the original speed
66
        // tPP    = 1200 * MICROSECONDS,
67
        // tSE    = 1500 * MILLISECONDS;
68
*/
69
static  const   unsigned
70
        CCS = 1; // 0: SDSC card, 1: SDHC or SDXC card
71
 
72
SDSPISIM::SDSPISIM(void) {
73
        m_dev = NULL;
74
        m_last_sck = 1;
75
        m_block_address = (CCS==1);
76
        m_host_supports_high_capacity = false;
77
        m_powerup_busy = -1;
78
        m_reset_state = SDSPI_POWERUP_RESET;
79
        //
80
        m_csd[ 0] = 0; // Normal SDcard, not high capacity
81
        m_csd[ 1] = 0x0f;
82
        m_csd[ 2] = 0x0f;
83
        m_csd[ 3] = 0x32; // Can be either 0x32 (25MHz) or 0x5a (50MHz)
84
        m_csd[ 4] = 0x5b; // Could also be 0x07b, if we supported more comands(?)
85
        m_csd[ 5] = 0x59; // 9-> 2^9,or 512 bytes (10->1024, 11->2048, no othrs)
86
        m_csd[ 6] = 0x00; // partial blocks allowed?
87
        m_csd[ 7] = 0x00; // C_SIZE, 2'b00, then top 6 bits
88
        m_csd[ 8] = 0;   // C_SIZE, 22-bits, mid 8 bits
89
        m_csd[ 9] = 0;   // C_SIZE, 22-bits, bottom 8 bits
90
        m_csd[10] = 0x7f;
91
        m_csd[11] = 0x80;
92
        m_csd[12] = 0x0a;
93
        m_csd[13] = 0x40;
94
        m_csd[14] = 0; // R/W: file format, copy, write protect, etc.
95
        m_csd[15] = cmdcrc(15, m_csd);
96
 
97
        // CID Register
98
        m_cid[ 0] = 0xba;
99
        m_cid[ 1] = 0xd0;
100
        m_cid[ 2] = 0xda;
101
        m_cid[ 3] = 0xdd;
102
        m_cid[ 4] = 0;
103
        m_cid[ 5] = 0xde;
104
        m_cid[ 6] = 0xad;
105
        m_cid[ 7] = 0xbe;
106
        m_cid[ 8] = 0xef;
107
        m_cid[ 9] = 0x20;
108
        m_cid[10] = 0x16;
109
        m_cid[11] = 0x05;
110
        m_cid[12] = 0x26;
111
        m_cid[13] = 0;
112
        m_cid[14] = 0;
113
        m_cid[15] = cmdcrc(15, m_cid);
114
 
115
        // m_write_count = 0;
116
        // m_ireg = m_oreg = 0;
117
        // m_sreg = 0x01c;
118
        // m_creg = 0x001;      // Iinitial creg on delivery
119
 
120
        //
121
        m_reading_data = false;
122
        m_have_token = false;
123
}
124
 
125
void    SDSPISIM::load(const char *fname) {
126
        m_dev = fopen(fname, "r+b");
127
 
128
        if (m_dev) {
129
                unsigned long devln;
130
                fseek(m_dev, 0l, SEEK_END);
131
                devln = ftell(m_dev);
132
                fseek(m_dev, 0l, SEEK_SET);
133
 
134
                m_devblocks = devln>>9;
135
 
136
                printf("SDCARD: NBLOCKS = %ld\n", m_devblocks);
137
        }
138
}
139
 
140
int     SDSPISIM::operator()(const int csn, const int sck, const int mosi) {
141
        // Keep track of a timer to determine when page program and erase
142
        // cycles complete.
143
 
144
        /*
145
        if (m_write_count > 0) {
146
                //
147
        }
148
        */
149
 
150
        m_delay++;
151
        if (m_powerup_busy>0)
152
                m_powerup_busy--;
153
 
154
        if (csn) {
155
                m_delay = 0;
156
                m_cmdidx= 0;
157
                m_rspidx= 0;
158
                m_bitpos= 0;
159
                m_delay = 0;
160
                m_busy  = false;
161
                m_last_sck = sck;
162
                m_syncd = false;
163
                m_last_miso = 1;
164
                m_dat_out = 0x0ff;
165
                // Reset everything when not selected
166
                return 0;
167
        } else if (sck == m_last_sck) {
168
                m_last_sck = sck;
169
                return m_last_miso;
170
        } else if (!m_last_sck) {
171
                // Register our input on the rising edge
172
                m_mosi = mosi;
173
                m_syncd= true;
174
                m_last_sck = sck;
175
                return m_last_miso;
176
        } if (!m_syncd) {
177
                m_last_sck = sck;
178
                return m_last_miso;
179
        }
180
 
181
        // Only change our output on the falling edge
182
 
183
        m_last_sck = sck;
184
        printf("SDSPI: (%3d) [%d,%d,%d] ", m_delay, csn, sck, m_mosi);
185
        // assert(m_delay > 20);
186
 
187
        m_bitpos++;
188
        m_dat_in = (m_dat_in<<1)|m_mosi;
189
 
190
 
191
        printf("(bitpos=%d,dat_in=%02x)\n", m_bitpos&7, m_dat_in&0x0ff);
192
 
193
        if ((m_bitpos&7)==0) {
194
                printf("SDSPI--RX BYTE %02x\n", m_dat_in&0x0ff);
195
                m_dat_out = 0xff;
196
                if (m_reading_data) {
197
                        if (m_have_token) {
198
                                m_block_buf[m_rxloc++] = m_dat_in;
199
                                printf("SDSPI: WR[%3d] = %02x\n", m_rxloc-1,
200
                                        m_dat_in&0x0ff);
201
                                if (m_rxloc >= 512+2) {
202
                                        unsigned crc, rxcrc;
203
                                        crc = blockcrc(512, m_block_buf);
204
                                        rxcrc = ((m_block_buf[512]&0x0ff)<<8)
205
                                                |(m_block_buf[513]&0x0ff);
206
 
207
                                        printf("LEN = %d\n", m_rxloc);
208
                                        printf("CHECKING CRC: (rx) %04x =? %04x (calc)\n",
209
                                                crc, rxcrc);
210
                                        m_reading_data = false;
211
                                        m_have_token = false;
212
                                        if (rxcrc == crc)
213
                                                m_dat_out = 5;
214
                                        else {
215
                                                m_dat_out = 0x0b;
216
                                                assert(rxcrc == crc);
217
                                        }
218
                                }
219
                        } else {
220
                                if ((m_dat_in&0x0ff) == 0x0fe) {
221
                                        printf("SDSPI: TOKEN!!\n");
222
                                        m_have_token = true;
223
                                        m_rxloc = 0;
224
                                } else printf("SDSPI: waiting on token\n");
225
                        }
226
                } else if (m_cmdidx < 6) {
227
                        printf("SDSPI: CMDIDX = %d\n", m_cmdidx);
228
                        // All commands *must* start with a 01... pair of bits.
229
                        if (m_cmdidx == 0)
230
                                assert((m_dat_in&0xc0)==0x40);
231
 
232
                        // Record the command for later processing
233
                        m_cmdbuf[m_cmdidx++] = m_dat_in;
234
                } else if (m_cmdidx == 6) {
235
                        // We're going to start a response from here ...
236
                        m_rspidx = 0;
237
                        m_blkdly = 0;
238
                        m_blkidx = SDSPI_MAXBLKLEN;
239
                        printf("SDSPI: CMDIDX = %d -- WE HAVE A COMMAND! [ ", m_cmdidx);
240
                        for(int i=0; i<6; i++)
241
                                printf("%02x ", m_cmdbuf[i] & 0xff);
242
                        printf("]\n"); fflush(stdout);
243
 
244
                        unsigned        arg;
245
                        arg = ((((((m_cmdbuf[1]<<8)|(m_cmdbuf[2]&0x0ff))<<8)
246
                                |(m_cmdbuf[3]&0x0ff))<<8)
247
                                |(m_cmdbuf[4]&0x0ff));
248
                        arg &= 0x0ffffffff;
249
 
250
                        // Check the CRC
251
                        if (!check_cmdcrc(m_cmdbuf)) {
252
                                assert(0 && "BAD CRC");
253
                                m_rspbuf[0] = 0x09;
254
                                m_rspdly = 1;
255
                        } else if (m_altcmd_flag) {
256
                                switch(m_cmdbuf[0]&0x03f) {
257
                                case 41: // ACMD41 -- SD_SEND_OP_COND
258
                                        // and start initialization sequence
259
                                        assert((m_reset_state == SDSPI_RCVD_CMD8)||(m_reset_state == SDSPI_RCVD_ACMD41)||(m_reset_state == SDSPI_RESET_COMPLETE));
260
                                        if((unsigned)m_powerup_busy>tRESET)
261
 
262
                                                m_powerup_busy = tRESET;
263
                                        assert((arg&0x0bfffffff) == 0);
264
                                        m_rspbuf[0] = (m_powerup_busy)?1:0;
265
                                        m_rspdly = 2;
266
                                        m_host_supports_high_capacity = (m_cmdbuf[1]&0x40)?1:0;
267
                                        m_reset_state = (m_powerup_busy)?
268
                                                SDSPI_RCVD_ACMD41
269
                                                :SDSPI_RESET_COMPLETE;
270
                                        break;
271
                                case 51: // ACMD51
272
                                        m_block_buf[0] = 0x0fe;
273
                                        for(int j=0; j<8; j++)
274
                                                m_block_buf[j+1] = m_csd[j];
275
                                        m_blklen = 8;
276
                                        add_block_crc(m_blklen, m_block_buf);
277
 
278
                                        m_blkdly = 0;
279
                                        m_blkidx = 0;
280
                                        m_dat_out = 0;
281
                                        break;
282
                                case 13: // ACMD13
283
                                case 22: // ACMD22
284
                                case 23: // ACMD23
285
                                case 42: // ACMD42
286
                                default: // Unimplemented command!
287
                                        m_rspbuf[0] = 0x04;
288
                                        m_rspdly = 4;
289
                                        fprintf(stderr, "SDSPI ERR: Alt command ACMD%d not implemented!\n", m_cmdbuf[0]&0x03f);
290
                                        assert(0 && "Not Implemented");
291
                                } m_altcmd_flag = false;
292
                        } else {
293
                                m_altcmd_flag = false;
294
                                memset(m_rspbuf, 0x0ff, SDSPI_RSPLEN);
295
                                printf("SDSPI: Received a command 0x%02x (%d)\n",
296
                                        m_cmdbuf[0], m_cmdbuf[0]&0x03f);
297
                                switch(m_cmdbuf[0]&0x3f) {
298
                                case  0: // CMD0  -- GO_IDLE_STATE
299
                                        m_rspbuf[0] = 0x01;
300
                                        m_rspdly = 4;
301
                                        m_reset_state = SDSPI_CMD0_IDLE;
302
                                        break;
303
                                case  1: // CMD1  -- SEND_OP_COND
304
                                        assert((arg&0x0bfffffff) == 0);
305
                                        m_rspbuf[0] = 0x02;
306
                                        m_rspdly = 4;
307
                                        m_host_supports_high_capacity = (m_cmdbuf[1]&0x40)?1:0;
308
                                        break;
309
                                case  8: // CMD8  -- SEND_IF_COND
310
                                        assert((arg&0x0fffff000) == 0);
311
                                        m_rspbuf[0] = 0x00;
312
                                        // See p82 for this format
313
                                        m_rspbuf[1] = 0;
314
                                        m_rspbuf[2] = 0;
315
                                        // If we do not accept the voltage range
316
                                        m_rspbuf[3] = 0;
317
                                        // Now, check if we accept it
318
                                        // We only accept 2.7-3.6V in this
319
                                        // simulation.
320
                                        if ((arg&0x0f00)==0x0100)
321
                                                m_rspbuf[3] = 1;
322
                                        m_rspbuf[4] = (char)(arg&0x0ff);
323
                                        m_rspdly = 4;
324
                                        assert((m_reset_state == SDSPI_CMD0_IDLE)||(m_reset_state == SDSPI_RCVD_CMD8));
325
                                        m_reset_state = SDSPI_RCVD_CMD8;
326
                                        break;
327
                                case  9: // CMD9  -- SEND_CSD
328
                                        // Block read, returning start token,
329
                                        // 16 bytes, then 2 crc bytes
330
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
331
                                        m_rspbuf[0] = 0x00;
332
                                        memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
333
                                        m_block_buf[0] = 0x0fe;
334
                                        for(int j=0; j<16; j++)
335
                                                m_block_buf[j+1] = m_csd[j];
336
                                        m_blklen = 16;
337
                                        add_block_crc(m_blklen, m_block_buf);
338
 
339
                                        m_blkdly = 60;
340
                                        m_blkidx = 0;
341
                                        break;
342
                                case 10: // CMD10 -- SEND_CID
343
                                        // Block read, returning start token,
344
                                        // 16 bytes, then 2 crc bytes
345
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
346
                                        m_rspbuf[0] = 0x00;
347
                                        memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
348
                                        m_block_buf[0] = 0x0fe;
349
                                        for(int j=0; j<16; j++)
350
                                                m_block_buf[j+1] = m_cid[j];
351
                                        m_blklen = 16;
352
                                        add_block_crc(m_blklen, m_block_buf);
353
 
354
                                        m_blkdly = 60;
355
                                        m_blkidx = 0;
356
                                        break;
357
                                case 13: // CMD13 -- SEND_STATUS
358
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
359
                                        m_rspbuf[0] = 0x00;
360
                                        m_rspbuf[1] = 0x00;
361
                                        // if (m_wp_fault) m_rspbuf[1]|=0x20;
362
                                        // if (m_err) m_rspbuf[1]|=0x04;
363
                                        m_rspdly = 4;
364
                                        break;
365
                                case 17: // CMD17 -- READ_SINGLE_BLOCK
366
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
367
                                        m_rspbuf[0] = 0x00;
368
                                        memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
369
                                        if (m_dev) {
370
                                                printf("Reading from block %08x of %08lx\n", arg, m_devblocks);
371
                                                if (m_block_address) {
372
                                                        assert(arg < m_devblocks);
373
                                                        fseek(m_dev, arg<<9, SEEK_SET);
374
                                                } else {
375
                                                        assert(arg < m_devblocks<<9);
376
                                                        fseek(m_dev, arg, SEEK_SET);
377
                                                }
378
                                        } m_block_buf[0] = 0x0fe;
379
                                        m_blklen = 512; // (1<<m_csd[5]);
380
                                        if (m_dev)
381
                                                fread(&m_block_buf[1], m_blklen, 1, m_dev);
382
                                        else
383
                                                memset(&m_block_buf[1], 0, m_blklen);
384
                                        add_block_crc(m_blklen, m_block_buf);
385
 
386
                                        m_blkdly = 60;
387
                                        m_blkidx = 0;
388
                                        break;
389
                                case 24: // CMD24 -- WRITE_BLOCK
390
                                        m_reading_data = true;
391
                                        m_have_token = false;
392
                                        m_dat_out = 0;
393
                                        break;
394
                                case 55: // CMD55 -- APP_CMD
395
                                        m_rspbuf[0] = 0x00;
396
                                        m_rspdly = 2;
397
                                        m_altcmd_flag = true;
398
                                        break;
399
                                case 58: // CMD58 -- READ_OCR, respond R7
400
                                        // argument is stuff bits/dont care
401
                                        m_rspbuf[0] = 0x00;
402
                                        // See p112, Tbl 5-1 for this format
403
                                        m_rspbuf[1] = ((m_powerup_busy)?0x80:0)
404
                                                        |(CCS?0x40:0);
405
                                        m_rspbuf[2] = 0xff;// 2.7-3.6V supported
406
                                        m_rspbuf[3] = 0x80;
407
                                        m_rspbuf[4] = 0; // No low-voltage supt
408
                                        m_rspdly = 4;
409
 
410
                                        if (m_reset_state == SDSPI_RESET_COMPLETE)
411
                                                m_reset_state = SDSPI_IN_OPERATION;
412
                                        break;
413
                                case  6: // CMD6  -- SWITCH_FUNC
414
                                case 12: // CMD12 -- STOP_TRANSMISSION (!impl)
415
                                case 16: // CMD16 -- SET_BLOCKLEN
416
                                case 18: // CMD18 -- READ_MULTIPLE_BLOCK
417
                                case 25: // CMD25 -- WRITE_MULTIPLE_BLOCK
418
                                case 27: // CMD27 -- PROGRAM_CSD
419
                                case 32: // CMD32 -- ERASE_WR_BLK_START_ADDR
420
                                case 33: // CMD33 -- ERASE_WR_BLK_END_ADDR
421
                                case 38: // CMD38 -- ERASE
422
                                case 56: // CMD56 -- GEN_CMD
423
                                default: // Unimplemented command
424
                                        m_rspbuf[0] = 0x04;
425
                                        m_rspdly = 4;
426
                                        printf("SDSPI ERR: Command CMD%d not implemented!\n", m_cmdbuf[0]&0x03f);
427
                                        fflush(stdout);
428
                                        assert(0 && "Not Implemented");
429
                                }
430
                        } m_cmdidx++;
431
 
432
                        // If we are using blocks, add bytes for the start
433
                        // token and the two CRC bytes
434
                        m_blklen += 3;
435
                } else if (m_rspdly > 0) {
436
                        assert((m_dat_in&0x0ff) == 0x0ff);
437
                        // A delay until a response is given
438
                        if (m_busy)
439
                                m_dat_out = 0;
440
                        m_rspdly--;
441
                } else if (m_rspidx < SDSPI_RSPLEN) {
442
                        assert((m_dat_in&0x0ff) == 0x0ff);
443
                        m_dat_out = m_rspbuf[m_rspidx++];
444
                } else if (m_blkdly > 0) {
445
                        assert((m_dat_in&0x0ff) == 0x0ff);
446
                        m_blkdly--;
447
                } else if (m_blkidx < SDSPI_MAXBLKLEN) {
448
                        assert((m_dat_in&0x0ff) == 0x0ff);
449
                        m_dat_out = m_block_buf[m_blkidx++];
450
                }
451
                        // else m_dat_out = 0x0ff; // So set already above
452
        }
453
 
454
        int result = (m_dat_out&0x80)?1:0;
455
        m_dat_out <<= 1;
456
        m_delay = 0;
457
        m_last_miso = result;
458
        fflush(stdout);
459
        return result;
460
}
461
 
462
unsigned SDSPISIM::cmdcrc(int len, char *buf) const {
463
        unsigned int fill = 0, taps = 0x12;
464
 
465
        for(int i=0; i<len; i++) {
466
                fill ^= buf[i];
467
                for(int j=0; j<8; j++) {
468
                        if (fill&0x80)
469
                                fill = (fill<<1)^taps;
470
                        else
471
                                fill <<= 1;
472
                }
473
        }
474
 
475
        fill &= 0x0fe; fill |= 1;
476
        return fill;
477
}
478
 
479
bool    SDSPISIM::check_cmdcrc(char *buf) const {
480
        unsigned fill = cmdcrc(5, buf);
481
        printf("SDSPI: CRC-CHECK, should have a CRC of %02x\n", fill);
482
        return (fill == (buf[5]&0x0ff));
483
}
484
 
485
unsigned SDSPISIM::blockcrc(int len, char *buf) const {
486
        unsigned int fill = 0, taps = 0x1021;
487
        bool    dbg = (len == 512);
488
 
489
        for(int i=0; i<len; i++) {
490
                if (dbg) { printf("BUF[%3d] = %02x\n", i, buf[i]&0x0ff); }
491
                fill ^= ((buf[i]&0x0ff) << 8);
492
                for(int j=0; j<8; j++) {
493
                        if (fill&0x8000)
494
                                fill = (fill<<1)^taps;
495
                        else
496
                                fill <<= 1;
497
                }
498
        }
499
 
500
        fill &= 0x0ffff;
501
        if (dbg) { printf("BLOCKCRC(%d,??) = %04x\n", len, fill); }
502
        return fill;
503
}
504
 
505
void    SDSPISIM::add_block_crc(int len, char *buf) const {
506
        unsigned fill = blockcrc(len, &buf[1]);
507
 
508
        buf[len+1] = (fill >> 8)&0x0ff;
509
        buf[len+2] = (fill     )&0x0ff;
510
}
511
 

powered by: WebSVN 2.1.0

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