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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sim/] [verilated/] [sdspisim.cpp] - Blame information for rev 58

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 58 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(const bool debug) {
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
        m_debug = debug;
124
}
125
 
126
void    SDSPISIM::load(const char *fname) {
127
        m_dev = fopen(fname, "r+b");
128
 
129
        if (m_dev) {
130
                unsigned long devln;
131
                fseek(m_dev, 0l, SEEK_END);
132
                devln = ftell(m_dev);
133
                fseek(m_dev, 0l, SEEK_SET);
134
 
135
                m_devblocks = devln>>9;
136
 
137
                if (m_debug) printf("SDCARD: NBLOCKS = %ld\n", m_devblocks);
138
        }
139
}
140
 
141
int     SDSPISIM::operator()(const int csn, const int sck, const int mosi) {
142
        // Keep track of a timer to determine when page program and erase
143
        // cycles complete.
144
 
145
        /*
146
        if (m_write_count > 0) {
147
                //
148
        }
149
        */
150
 
151
        m_delay++;
152
        if (m_powerup_busy>0)
153
                m_powerup_busy--;
154
 
155
        if (csn) {
156
                m_delay = 0;
157
                m_cmdidx= 0;
158
                m_rspidx= 0;
159
                m_bitpos= 0;
160
                m_delay = 0;
161
                m_busy  = false;
162
                m_last_sck = sck;
163
                m_syncd = false;
164
                m_last_miso = 1;
165
                m_dat_out = 0x0ff;
166
                // Reset everything when not selected
167
                return 0;
168
        } else if (sck == m_last_sck) {
169
                m_last_sck = sck;
170
                return m_last_miso;
171
        } else if (!m_last_sck) {
172
                // Register our input on the rising edge
173
                m_mosi = mosi;
174
                m_syncd= true;
175
                m_last_sck = sck;
176
                return m_last_miso;
177
        } if (!m_syncd) {
178
                m_last_sck = sck;
179
                return m_last_miso;
180
        }
181
 
182
        // Only change our output on the falling edge
183
 
184
        m_last_sck = sck;
185
        if (m_debug) printf("SDSPI: (%3d) [%d,%d,%d] ", m_delay, csn, sck, m_mosi);
186
        // assert(m_delay > 20);
187
 
188
        m_bitpos++;
189
        m_dat_in = (m_dat_in<<1)|m_mosi;
190
 
191
 
192
        if (m_debug) printf("(bitpos=%d,dat_in=%02x)\n", m_bitpos&7, m_dat_in&0x0ff);
193
 
194
        if ((m_bitpos&7)==0) {
195
                if (m_debug) printf("SDSPI--RX BYTE %02x\n", m_dat_in&0x0ff);
196
                m_dat_out = 0xff;
197
                if (m_reading_data) {
198
                        if (m_have_token) {
199
                                m_block_buf[m_rxloc++] = m_dat_in;
200
                                if (m_debug) printf("SDSPI: WR[%3d] = %02x\n", m_rxloc-1,
201
                                        m_dat_in&0x0ff);
202
                                if (m_rxloc >= 512+2) {
203
                                        unsigned crc, rxcrc;
204
                                        crc = blockcrc(512, m_block_buf);
205
                                        rxcrc = ((m_block_buf[512]&0x0ff)<<8)
206
                                                |(m_block_buf[513]&0x0ff);
207
 
208
                                        if (m_debug) printf("LEN = %d\n", m_rxloc);
209
                                        if (m_debug) printf("CHECKING CRC: (rx) %04x =? %04x (calc)\n",
210
                                                crc, rxcrc);
211
                                        m_reading_data = false;
212
                                        m_have_token = false;
213
                                        if (rxcrc == crc)
214
                                                m_dat_out = 5;
215
                                        else {
216
                                                m_dat_out = 0x0b;
217
                                                assert(rxcrc == crc);
218
                                        }
219
                                }
220
                        } else {
221
                                if ((m_dat_in&0x0ff) == 0x0fe) {
222
                                        if (m_debug) printf("SDSPI: TOKEN!!\n");
223
                                        m_have_token = true;
224
                                        m_rxloc = 0;
225
                                } else if (m_debug)
226
                                        printf("SDSPI: waiting on token\n");
227
                        }
228
                } else if (m_cmdidx < 6) {
229
                        if (m_debug) printf("SDSPI: CMDIDX = %d\n", m_cmdidx);
230
                        // All commands *must* start with a 01... pair of bits.
231
                        if (m_cmdidx == 0)
232
                                assert((m_dat_in&0xc0)==0x40);
233
 
234
                        // Record the command for later processing
235
                        m_cmdbuf[m_cmdidx++] = m_dat_in;
236
                } else if (m_cmdidx == 6) {
237
                        // We're going to start a response from here ...
238
                        m_rspidx = 0;
239
                        m_blkdly = 0;
240
                        m_blkidx = SDSPI_MAXBLKLEN;
241
                        if (m_debug) {
242
                                printf("SDSPI: CMDIDX = %d -- WE HAVE A COMMAND! [ ", m_cmdidx);
243
                                for(int i=0; i<6; i++)
244
                                        printf("%02x ", m_cmdbuf[i] & 0xff);
245
                                printf("]\n"); fflush(stdout);
246
                        }
247
 
248
                        unsigned        arg;
249
                        arg = ((((((m_cmdbuf[1]<<8)|(m_cmdbuf[2]&0x0ff))<<8)
250
                                |(m_cmdbuf[3]&0x0ff))<<8)
251
                                |(m_cmdbuf[4]&0x0ff));
252
                        arg &= 0x0ffffffff;
253
 
254
                        // Check the CRC
255
                        if (!check_cmdcrc(m_cmdbuf)) {
256
                                assert(0 && "BAD CRC");
257
                                m_rspbuf[0] = 0x09;
258
                                m_rspdly = 1;
259
                        } else if (m_altcmd_flag) {
260
                                switch(m_cmdbuf[0]&0x03f) {
261
                                case 41: // ACMD41 -- SD_SEND_OP_COND
262
                                        // and start initialization sequence
263
                                        assert((m_reset_state == SDSPI_RCVD_CMD8)||(m_reset_state == SDSPI_RCVD_ACMD41)||(m_reset_state == SDSPI_RESET_COMPLETE));
264
                                        if((unsigned)m_powerup_busy>tRESET)
265
 
266
                                                m_powerup_busy = tRESET;
267
                                        assert((arg&0x0bfffffff) == 0);
268
                                        m_rspbuf[0] = (m_powerup_busy)?1:0;
269
                                        m_rspdly = 2;
270
                                        m_host_supports_high_capacity = (m_cmdbuf[1]&0x40)?1:0;
271
                                        m_reset_state = (m_powerup_busy)?
272
                                                SDSPI_RCVD_ACMD41
273
                                                :SDSPI_RESET_COMPLETE;
274
                                        break;
275
                                case 51: // ACMD51
276
                                        m_block_buf[0] = 0x0fe;
277
                                        for(int j=0; j<8; j++)
278
                                                m_block_buf[j+1] = m_csd[j];
279
                                        m_blklen = 8;
280
                                        add_block_crc(m_blklen, m_block_buf);
281
 
282
                                        m_blkdly = 0;
283
                                        m_blkidx = 0;
284
                                        m_dat_out = 0;
285
                                        break;
286
                                case 13: // ACMD13
287
                                case 22: // ACMD22
288
                                case 23: // ACMD23
289
                                case 42: // ACMD42
290
                                default: // Unimplemented command!
291
                                        m_rspbuf[0] = 0x04;
292
                                        m_rspdly = 4;
293
                                        fprintf(stderr, "SDSPI ERR: Alt command ACMD%d not implemented!\n", m_cmdbuf[0]&0x03f);
294
                                        assert(0 && "Not Implemented");
295
                                } m_altcmd_flag = false;
296
                        } else {
297
                                m_altcmd_flag = false;
298
                                memset(m_rspbuf, 0x0ff, SDSPI_RSPLEN);
299
                                if (m_debug) printf("SDSPI: Received a command 0x%02x (%d)\n",
300
                                        m_cmdbuf[0], m_cmdbuf[0]&0x03f);
301
                                switch(m_cmdbuf[0]&0x3f) {
302
                                case  0: // CMD0  -- GO_IDLE_STATE
303
                                        m_rspbuf[0] = 0x01;
304
                                        m_rspdly = 4;
305
                                        m_reset_state = SDSPI_CMD0_IDLE;
306
                                        break;
307
                                case  1: // CMD1  -- SEND_OP_COND
308
                                        assert((arg&0x0bfffffff) == 0);
309
                                        m_rspbuf[0] = 0x02;
310
                                        m_rspdly = 4;
311
                                        m_host_supports_high_capacity = (m_cmdbuf[1]&0x40)?1:0;
312
                                        break;
313
                                case  8: // CMD8  -- SEND_IF_COND
314
                                        assert((arg&0x0fffff000) == 0);
315
                                        m_rspbuf[0] = 0x00;
316
                                        // See p82 for this format
317
                                        m_rspbuf[1] = 0;
318
                                        m_rspbuf[2] = 0;
319
                                        // If we do not accept the voltage range
320
                                        m_rspbuf[3] = 0;
321
                                        // Now, check if we accept it
322
                                        // We only accept 2.7-3.6V in this
323
                                        // simulation.
324
                                        if ((arg&0x0f00)==0x0100)
325
                                                m_rspbuf[3] = 1;
326
                                        m_rspbuf[4] = (char)(arg&0x0ff);
327
                                        m_rspdly = 4;
328
                                        assert((m_reset_state == SDSPI_CMD0_IDLE)||(m_reset_state == SDSPI_RCVD_CMD8));
329
                                        m_reset_state = SDSPI_RCVD_CMD8;
330
                                        break;
331
                                case  9: // CMD9  -- SEND_CSD
332
                                        // Block read, returning start token,
333
                                        // 16 bytes, then 2 crc bytes
334
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
335
                                        m_rspbuf[0] = 0x00;
336
                                        memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
337
                                        m_block_buf[0] = 0x0fe;
338
                                        for(int j=0; j<16; j++)
339
                                                m_block_buf[j+1] = m_csd[j];
340
                                        m_blklen = 16;
341
                                        add_block_crc(m_blklen, m_block_buf);
342
 
343
                                        m_blkdly = 60;
344
                                        m_blkidx = 0;
345
                                        break;
346
                                case 10: // CMD10 -- SEND_CID
347
                                        // Block read, returning start token,
348
                                        // 16 bytes, then 2 crc bytes
349
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
350
                                        m_rspbuf[0] = 0x00;
351
                                        memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
352
                                        m_block_buf[0] = 0x0fe;
353
                                        for(int j=0; j<16; j++)
354
                                                m_block_buf[j+1] = m_cid[j];
355
                                        m_blklen = 16;
356
                                        add_block_crc(m_blklen, m_block_buf);
357
 
358
                                        m_blkdly = 60;
359
                                        m_blkidx = 0;
360
                                        break;
361
                                case 13: // CMD13 -- SEND_STATUS
362
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
363
                                        m_rspbuf[0] = 0x00;
364
                                        m_rspbuf[1] = 0x00;
365
                                        // if (m_wp_fault) m_rspbuf[1]|=0x20;
366
                                        // if (m_err) m_rspbuf[1]|=0x04;
367
                                        m_rspdly = 4;
368
                                        break;
369
                                case 17: // CMD17 -- READ_SINGLE_BLOCK
370
                                        assert(m_reset_state == SDSPI_IN_OPERATION);
371
                                        m_rspbuf[0] = 0x00;
372
                                        memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
373
                                        if (m_dev) {
374
                                                if (m_debug) printf("Reading from block %08x of %08lx\n", arg, m_devblocks);
375
                                                if (m_block_address) {
376
                                                        assert(arg < m_devblocks);
377
                                                        fseek(m_dev, arg<<9, SEEK_SET);
378
                                                } else {
379
                                                        assert(arg < m_devblocks<<9);
380
                                                        fseek(m_dev, arg, SEEK_SET);
381
                                                }
382
                                        } m_block_buf[0] = 0x0fe;
383
                                        m_blklen = 512; // (1<<m_csd[5]);
384
                                        if (m_dev)
385
                                                m_blklen = fread(&m_block_buf[1], m_blklen, 1, m_dev);
386
                                        else
387
                                                memset(&m_block_buf[1], 0, m_blklen);
388
                                        m_blklen = (m_blklen != 512) ? 512 : m_blklen;
389
                                        add_block_crc(m_blklen, m_block_buf);
390
 
391
                                        m_blkdly = 60;
392
                                        m_blkidx = 0;
393
                                        break;
394
                                case 24: // CMD24 -- WRITE_BLOCK
395
                                        m_reading_data = true;
396
                                        m_have_token = false;
397
                                        m_dat_out = 0;
398
                                        break;
399
                                case 55: // CMD55 -- APP_CMD
400
                                        m_rspbuf[0] = 0x00;
401
                                        m_rspdly = 2;
402
                                        m_altcmd_flag = true;
403
                                        break;
404
                                case 58: // CMD58 -- READ_OCR, respond R7
405
                                        // argument is stuff bits/dont care
406
                                        m_rspbuf[0] = 0x00;
407
                                        // See p112, Tbl 5-1 for this format
408
                                        m_rspbuf[1] = ((m_powerup_busy)?0x80:0)
409
                                                        |(CCS?0x40:0);
410
                                        m_rspbuf[2] = 0xff;// 2.7-3.6V supported
411
                                        m_rspbuf[3] = 0x80;
412
                                        m_rspbuf[4] = 0; // No low-voltage supt
413
                                        m_rspdly = 4;
414
 
415
                                        if (m_reset_state == SDSPI_RESET_COMPLETE)
416
                                                m_reset_state = SDSPI_IN_OPERATION;
417
                                        break;
418
                                case  6: // CMD6  -- SWITCH_FUNC
419
                                case 12: // CMD12 -- STOP_TRANSMISSION (!impl)
420
                                case 16: // CMD16 -- SET_BLOCKLEN
421
                                case 18: // CMD18 -- READ_MULTIPLE_BLOCK
422
                                case 25: // CMD25 -- WRITE_MULTIPLE_BLOCK
423
                                case 27: // CMD27 -- PROGRAM_CSD
424
                                case 32: // CMD32 -- ERASE_WR_BLK_START_ADDR
425
                                case 33: // CMD33 -- ERASE_WR_BLK_END_ADDR
426
                                case 38: // CMD38 -- ERASE
427
                                case 56: // CMD56 -- GEN_CMD
428
                                default: // Unimplemented command
429
                                        m_rspbuf[0] = 0x04;
430
                                        m_rspdly = 4;
431
                                        if (m_debug) printf("SDSPI ERR: Command CMD%d not implemented!\n", m_cmdbuf[0]&0x03f);
432
                                        fflush(stdout);
433
                                        assert(0 && "Not Implemented");
434
                                }
435
                        } m_cmdidx++;
436
 
437
                        // If we are using blocks, add bytes for the start
438
                        // token and the two CRC bytes
439
                        m_blklen += 3;
440
                } else if (m_rspdly > 0) {
441
                        assert((m_dat_in&0x0ff) == 0x0ff);
442
                        // A delay until a response is given
443
                        if (m_busy)
444
                                m_dat_out = 0;
445
                        m_rspdly--;
446
                } else if (m_rspidx < SDSPI_RSPLEN) {
447
                        assert((m_dat_in&0x0ff) == 0x0ff);
448
                        m_dat_out = m_rspbuf[m_rspidx++];
449
                } else if (m_blkdly > 0) {
450
                        assert((m_dat_in&0x0ff) == 0x0ff);
451
                        m_blkdly--;
452
                } else if (m_blkidx < SDSPI_MAXBLKLEN) {
453
                        assert((m_dat_in&0x0ff) == 0x0ff);
454
                        m_dat_out = m_block_buf[m_blkidx++];
455
                }
456
                        // else m_dat_out = 0x0ff; // So set already above
457
        }
458
 
459
        int result = (m_dat_out&0x80)?1:0;
460
        m_dat_out <<= 1;
461
        m_delay = 0;
462
        m_last_miso = result;
463
        fflush(stdout);
464
        return result;
465
}
466
 
467
unsigned SDSPISIM::cmdcrc(int len, char *buf) const {
468
        unsigned int fill = 0, taps = 0x12;
469
 
470
        for(int i=0; i<len; i++) {
471
                fill ^= buf[i];
472
                for(int j=0; j<8; j++) {
473
                        if (fill&0x80)
474
                                fill = (fill<<1)^taps;
475
                        else
476
                                fill <<= 1;
477
                }
478
        }
479
 
480
        fill &= 0x0fe; fill |= 1;
481
        return fill;
482
}
483
 
484
bool    SDSPISIM::check_cmdcrc(char *buf) const {
485
        unsigned fill = cmdcrc(5, buf);
486
        if (m_debug) printf("SDSPI: CRC-CHECK, should have a CRC of %02x\n", fill);
487
        return (fill == (buf[5]&0x0ff));
488
}
489
 
490
unsigned SDSPISIM::blockcrc(int len, char *buf) const {
491
        unsigned int fill = 0, taps = 0x1021;
492
        bool    dbg = (len == 512);
493
 
494
        for(int i=0; i<len; i++) {
495
                if (dbg) { printf("BUF[%3d] = %02x\n", i, buf[i]&0x0ff); }
496
                fill ^= ((buf[i]&0x0ff) << 8);
497
                for(int j=0; j<8; j++) {
498
                        if (fill&0x8000)
499
                                fill = (fill<<1)^taps;
500
                        else
501
                                fill <<= 1;
502
                }
503
        }
504
 
505
        fill &= 0x0ffff;
506
        if (dbg) { printf("BLOCKCRC(%d,...) = %04x\n", len, fill); }
507
        return fill;
508
}
509
 
510
void    SDSPISIM::add_block_crc(int len, char *buf) const {
511
        unsigned fill = blockcrc(len, &buf[1]);
512
 
513
        buf[len+1] = (fill >> 8)&0x0ff;
514
        buf[len+2] = (fill     )&0x0ff;
515
}
516
 

powered by: WebSVN 2.1.0

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