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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [bench/] [cpp/] [sdspisim.cpp] - Blame information for rev 75

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

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

powered by: WebSVN 2.1.0

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