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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [bench/] [cpp/] [qspiflashsim.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:    spiflashsim.cpp
5
//
6
// Project:     Wishbone Controlled Quad SPI Flash Controller
7
//
8
// Purpose:     This library simulates the operation of a Quad-SPI commanded
9
//              flash, such as the S25FL032P used on the Basys-3 development
10
//              board by Digilent.  As such, it is defined by 32 Mbits of
11
//              memory (4 Mbyte).
12
//
13
//              This simulator is useful for testing in a Verilator/C++
14
//              environment, where this simulator can be used in place of
15
//              the actual hardware.
16
//
17
// Creator:     Dan Gisselquist
18
//              Gisselquist Technology, LLC
19
//
20
///////////////////////////////////////////////////////////////////////////
21
//
22
// Copyright (C) 2015, Gisselquist Technology, LLC
23
//
24
// This program is free software (firmware): you can redistribute it and/or
25
// modify it under the terms of  the GNU General Public License as published
26
// by the Free Software Foundation, either version 3 of the License, or (at
27
// your option) any later version.
28
//
29
// This program is distributed in the hope that it will be useful, but WITHOUT
30
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
31
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32
// for more details.
33
//
34
// You should have received a copy of the GNU General Public License along
35
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
36
// target there if the PDF file isn't present.)  If not, see
37
// <http://www.gnu.org/licenses/> for a copy.
38
//
39
// License:     GPL, v3, as defined and found on www.gnu.org,
40
//              http://www.gnu.org/licenses/gpl.html
41
//
42
//
43
///////////////////////////////////////////////////////////////////////////
44
#include <stdio.h>
45
#include <string.h>
46
#include <assert.h>
47
#include <stdlib.h>
48
 
49
#include "qspiflashsim.h"
50
 
51
#define MEMBYTES        (1<<22)
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
QSPIFLASHSIM::QSPIFLASHSIM(void) {
70
        m_mem = new char[MEMBYTES];
71
        m_pmem = new char[256];
72
        m_state = QSPIF_IDLE;
73
        m_last_sck = 1;
74
        m_write_count = 0;
75
        m_ireg = m_oreg = 0;
76
        m_sreg = 0x01c;
77
        m_creg = 0x001; // Iinitial creg on delivery
78
        m_quad_mode = false;
79
        m_mode_byte = 0;
80
 
81
        memset(m_mem, 0x0ff, MEMBYTES);
82
}
83
 
84
void    QSPIFLASHSIM::load(const unsigned addr, const char *fname) {
85
        FILE    *fp;
86
        size_t  len;
87
 
88
        if (addr >= MEMBYTES)
89
                return;
90
        len = MEMBYTES-addr*4;
91
 
92
        if (NULL != (fp = fopen(fname, "r"))) {
93
                int     nr = 0;
94
                nr = fread(&m_mem[addr], sizeof(char), len, fp);
95
                fclose(fp);
96
                if (nr == 0) {
97
                        fprintf(stderr, "SPI-FLASH: Could not read %s\n", fname);
98
                        perror("O/S Err:");
99
                }
100
        } else {
101
                fprintf(stderr, "SPI-FLASH: Could not open %s\n", fname);
102
                perror("O/S Err:");
103
        }
104
}
105
 
106
#define QOREG(A)        m_oreg = ((m_oreg & (~0x0ff))|(A&0x0ff))
107
 
108
int     QSPIFLASHSIM::operator()(const int csn, const int sck, const int dat) {
109
        // Keep track of a timer to determine when page program and erase
110
        // cycles complete.
111
 
112
        if (m_write_count > 0) {
113
                if (0 == (--m_write_count)) {// When done with erase/page pgm,
114
                        m_sreg &= 0x0fc; // Clear the write in progress bit
115
                        if (m_debug) printf("Write complete, clearing WIP (inside SIM)\n");
116
                }
117
        }
118
 
119
        if (csn) {
120
                m_last_sck = 1;
121
                m_ireg = 0; m_oreg = 0;
122
                m_count= 0;
123
 
124
                if ((QSPIF_PP == m_state)||(QSPIF_QPP == m_state)) {
125
                        // Start a page program
126
                        if (m_debug) printf("QSPI: Page Program write cycle begins\n");
127
                        if (m_debug) printf("CK = %d & 7 = %d\n", m_count, m_count & 0x07);
128
                        if (m_debug) printf("QSPI: pmem = %08lx\n", (unsigned long)m_pmem);
129
                        m_write_count = tPP;
130
                        m_state = QSPIF_IDLE;
131
                        m_sreg &= (~QSPIF_WEL_FLAG);
132
                        m_sreg |= (QSPIF_WIP_FLAG);
133
                        for(int i=0; i<256; i++) {
134
                                /*
135
                                if (m_debug) printf("%02x: m_mem[%02x] = %02x &= %02x = %02x\n",
136
                                        i, (m_addr&(~0x0ff))+i,
137
                                        m_mem[(m_addr&(~0x0ff))+i]&0x0ff, m_pmem[i]&0x0ff,
138
                                        m_mem[(m_addr&(~0x0ff))+i]& m_pmem[i]&0x0ff);
139
                                */
140
                                m_mem[(m_addr&(~0x0ff))+i] &= m_pmem[i];
141
                        }
142
                        m_quad_mode = false;
143
                } else if (m_state == QSPIF_SECTOR_ERASE) {
144
                        if (m_debug) printf("Actually Erasing sector, from %08x\n", m_addr);
145
                        m_write_count = tSE;
146
                        m_state = QSPIF_IDLE;
147
                        m_sreg &= (~QSPIF_WEL_FLAG);
148
                        m_sreg |= (QSPIF_WIP_FLAG);
149
                        m_addr &= (-1<<16);
150
                        for(int i=0; i<(1<<16); i++)
151
                                m_mem[m_addr + i] = 0x0ff;
152
                        if (m_debug) printf("Now waiting %d ticks delay\n", m_write_count);
153
                } else if (QSPIF_WRSR == m_state) {
154
                        if (m_debug) printf("Actually writing status register\n");
155
                        m_write_count = tW;
156
                        m_state = QSPIF_IDLE;
157
                        m_sreg &= (~QSPIF_WEL_FLAG);
158
                        m_sreg |= (QSPIF_WIP_FLAG);
159
                } else if (QSPIF_CLSR == m_state) {
160
                        if (m_debug) printf("Actually clearing the status register bits\n");
161
                        m_state = QSPIF_IDLE;
162
                        m_sreg &= 0x09f;
163
                } else if (m_state == QSPIF_BULK_ERASE) {
164
                        m_write_count = tBE;
165
                        m_state = QSPIF_IDLE;
166
                        m_sreg &= (~QSPIF_WEL_FLAG);
167
                        m_sreg |= (QSPIF_WIP_FLAG);
168
                        for(int i=0; i<MEMBYTES; i++)
169
                                m_mem[i] = 0x0ff;
170
                } else if (m_state == QSPIF_DEEP_POWER_DOWN) {
171
                        m_write_count = tDP;
172
                        m_state = QSPIF_IDLE;
173
                } else if (m_state == QSPIF_RELEASE) {
174
                        m_write_count = tRES;
175
                        m_state = QSPIF_IDLE;
176
                } else if (m_state == QSPIF_QUAD_READ_CMD) {
177
                        if ((m_mode_byte & 0x0f0)!=0x0a0)
178
                                m_quad_mode = false;
179
                        else
180
                                m_state = QSPIF_QUAD_READ_IDLE;
181
                } else if (m_state == QSPIF_QUAD_READ) {
182
                        if ((m_mode_byte & 0x0f0)!=0x0a0)
183
                                m_quad_mode = false;
184
                        else
185
                                m_state = QSPIF_QUAD_READ_IDLE;
186
                } else if (m_state == QSPIF_QUAD_READ_IDLE) {
187
                }
188
 
189
                m_oreg = 0x0fe;
190
                return dat;
191
        } else if ((!m_last_sck)||(sck == m_last_sck)) {
192
                // Only change on the falling clock edge
193
                // printf("SFLASH-SKIP, CLK=%d -> %d\n", m_last_sck, sck);
194
                m_last_sck = sck;
195
                if (m_quad_mode)
196
                        return (m_oreg>>8)&0x0f;
197
                else
198
                        // return ((m_oreg & 0x0100)?2:0) | (dat & 0x0d);
199
                        return (m_oreg & 0x0100)?2:0;
200
        }
201
 
202
        // We'll only get here if ...
203
        //      last_sck = 1, and sck = 0, thus transitioning on the
204
        //      negative edge as with everything else in this interface
205
        if (m_quad_mode) {
206
                m_ireg = (m_ireg << 4) | (dat & 0x0f);
207
                m_count+=4;
208
                m_oreg <<= 4;
209
        } else {
210
                m_ireg = (m_ireg << 1) | (dat & 1);
211
                m_count++;
212
                m_oreg <<= 1;
213
        }
214
 
215
 
216
        // printf("PROCESS, COUNT = %d, IREG = %02x\n", m_count, m_ireg);
217
        if (m_state == QSPIF_QUAD_READ_IDLE) {
218
                assert(m_quad_mode);
219
                if (m_count == 24) {
220
                        if (m_debug) printf("QSPI: Entering from Quad-Read Idle to Quad-Read\n");
221
                        if (m_debug) printf("QSPI: QI/O Idle Addr = %02x\n", m_ireg&0x0ffffff);
222
                        m_addr = (m_ireg) & 0x0ffffff;
223
                        assert((m_addr & 0xfc00000)==0);
224
                        m_state = QSPIF_QUAD_READ;
225
                } m_oreg = 0;
226
        } else if (m_count == 8) {
227
                QOREG(0x0a5);
228
                // printf("SFLASH-CMD = %02x\n", m_ireg & 0x0ff);
229
                // Figure out what command we've been given
230
                if (m_debug) printf("SPI FLASH CMD %02x\n", m_ireg&0x0ff);
231
                switch(m_ireg & 0x0ff) {
232
                case 0x01: // Write status register
233
                        if (2 !=(m_sreg & 0x203)) {
234
                                if (m_debug) printf("QSPI: WEL not set, cannot write status reg\n");
235
                                m_state = QSPIF_INVALID;
236
                        } else
237
                                m_state = QSPIF_WRSR;
238
                        break;
239
                case 0x02: // Page program
240
                        if (2 != (m_sreg & 0x203)) {
241
                                if (m_debug) printf("QSPI: Cannot program at this time, SREG = %x\n", m_sreg);
242
                                m_state = QSPIF_INVALID;
243
                        } else {
244
                                m_state = QSPIF_PP;
245
                                if (m_debug) printf("PAGE-PROGRAM COMMAND ACCEPTED\n");
246
                        }
247
                        break;
248
                case 0x03: // Read data bytes
249
                        // Our clock won't support this command, so go
250
                        // to an invalid state
251
                        if (m_debug) printf("QSPI INVALID: This sim does not support slow reading\n");
252
                        m_state = QSPIF_INVALID;
253
                        break;
254
                case 0x04: // Write disable
255
                        m_state = QSPIF_IDLE;
256
                        m_sreg &= (~QSPIF_WEL_FLAG);
257
                        break;
258
                case 0x05: // Read status register
259
                        m_state = QSPIF_RDSR;
260
                        if (m_debug) printf("QSPI: READING STATUS REGISTER: %02x\n", m_sreg);
261
                        QOREG(m_sreg);
262
                        break;
263
                case 0x06: // Write enable
264
                        m_state = QSPIF_IDLE;
265
                        m_sreg |= QSPIF_WEL_FLAG;
266
                        if (m_debug) printf("QSPI: WRITE-ENABLE COMMAND ACCEPTED\n");
267
                        break;
268
                case 0x0b: // Here's the read that we support
269
                        if (m_debug) printf("QSPI: FAST-READ (single-bit)\n");
270
                        m_state = QSPIF_FAST_READ;
271
                        break;
272
                case 0x30:
273
                        if (m_debug) printf("QSPI: CLEAR STATUS REGISTER COMMAND\n");
274
                        m_state = QSPIF_CLSR;
275
                        break;
276
                case 0x32: // QUAD Page program, 4 bits at a time
277
                        if (2 != (m_sreg & 0x203)) {
278
                                if (m_debug) printf("QSPI: Cannot program at this time, SREG = %x\n", m_sreg);
279
                                m_state = QSPIF_INVALID;
280
                        } else {
281
                                m_state = QSPIF_QPP;
282
                                if (m_debug) printf("QSPI: QUAD-PAGE-PROGRAM COMMAND ACCEPTED\n");
283
                                if (m_debug) printf("QSPI: pmem = %08lx\n", (unsigned long)m_pmem);
284
                        }
285
                        break;
286
                case 0x35: // Read configuration register
287
                        m_state = QSPIF_RDCR;
288
                        if (m_debug) printf("QSPI: READING CONFIGURATION REGISTER: %02x\n", m_creg);
289
                        QOREG(m_creg);
290
                        break;
291
                case 0x9f: // Read ID
292
                        m_state = QSPIF_RDID;
293
                        if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>24)&0x0ff);
294
                        QOREG(0xfe);
295
                        break;
296
                case 0xab: // Release from DEEP POWER DOWN
297
                        if (m_sreg & QSPIF_DEEP_POWER_DOWN_FLAG) {
298
                                if (m_debug) printf("QSPI: Release from deep power down\n");
299
                                m_sreg &= (~QSPIF_DEEP_POWER_DOWN_FLAG);
300
                                m_write_count = tRES;
301
                        } m_state = QSPIF_RELEASE;
302
                        break;
303
                case 0xb9: // DEEP POWER DOWN
304
                        if (0 != (m_sreg & 0x01)) {
305
                                if (m_debug) printf("QSPI: Cannot enter DEEP POWER DOWN, in middle of write/erase\n");
306
                                m_state = QSPIF_INVALID;
307
                        } else {
308
                                m_sreg  |= QSPIF_DEEP_POWER_DOWN_FLAG;
309
                                m_state  = QSPIF_IDLE;
310
                        }
311
                        break;
312
                case 0xc7: // Bulk Erase
313
                        if (2 != (m_sreg & 0x203)) {
314
                                if (m_debug) printf("QSPI: WEL not set, cannot erase device\n");
315
                                m_state = QSPIF_INVALID;
316
                        } else
317
                                m_state = QSPIF_BULK_ERASE;
318
                        break;
319
                case 0xd8: // Sector Erase
320
                        if (2 != (m_sreg & 0x203)) {
321
                                if (m_debug) printf("QSPI: WEL not set, cannot erase sector\n");
322
                                m_state = QSPIF_INVALID;
323
                        } else {
324
                                m_state = QSPIF_SECTOR_ERASE;
325
                                if (m_debug) printf("QSPI: SECTOR_ERASE COMMAND\n");
326
                        }
327
                        break;
328
                case 0x0eb: // Here's the (other) read that we support
329
                        // printf("QSPI: QUAD-I/O-READ\n");
330
                        m_state = QSPIF_QUAD_READ_CMD;
331
                        m_quad_mode = true;
332
                        break;
333
                default:
334
                        printf("QSPI: UNRECOGNIZED SPI FLASH CMD: %02x\n", m_ireg&0x0ff);
335
                        m_state = QSPIF_INVALID;
336
                        assert(0 && "Unrecognized command\n");
337
                        break;
338
                }
339
        } else if ((0 == (m_count&0x07))&&(m_count != 0)) {
340
                QOREG(0);
341
                switch(m_state) {
342
                case QSPIF_IDLE:
343
                        printf("TOO MANY CLOCKS, SPIF in IDLE\n");
344
                        break;
345
                case QSPIF_WRSR:
346
                        if (m_count == 16) {
347
                                m_sreg = (m_sreg & 0x061) | (m_ireg & 0x09c);
348
                                if (m_debug) printf("Request to set sreg to 0x%02x\n",
349
                                        m_ireg&0x0ff);
350
                        } else if (m_count == 24) {
351
                                m_creg = (m_creg & 0x0fd) | (m_ireg & 0x02);
352
                                if (m_debug) printf("Request to set creg to 0x%02x\n",
353
                                        m_ireg&0x0ff);
354
                        } else {
355
                                printf("TOO MANY CLOCKS FOR WRR!!!\n");
356
                                exit(-2);
357
                                m_state = QSPIF_IDLE;
358
                        }
359
                        break;
360
                case QSPIF_CLSR:
361
                        assert(0 && "Too many clocks for CLSR command!!\n");
362
                        break;
363
                case QSPIF_RDID:
364
                        if (m_count == 32) {
365
                                m_addr = m_ireg & 0x0ffffff;
366
                                if (m_debug) printf("READID, ADDR = %08x\n", m_addr);
367
                                QOREG((DEVID>>8));
368
                                if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>8)&0x0ff);
369
                        } else if (m_count > 32) {
370
                                if (((m_count-32)>>3)&1)
371
                                        QOREG((DEVID));
372
                                else
373
                                        QOREG((DEVID>>8));
374
                                if (m_debug) printf("QSPI: READING ID, %02x -- DONE\n", 0x00);
375
                        }
376
                        // m_oreg = (DEVID >> (2-(m_count>>3)-1)) & 0x0ff;
377
                        break;
378
                case QSPIF_RDSR:
379
                        // printf("Read SREG = %02x, wait = %08x\n", m_sreg,
380
                                // m_write_count);
381
                        QOREG(m_sreg);
382
                        break;
383
                case QSPIF_RDCR:
384
                        if (m_debug) printf("Read CREG = %02x\n", m_creg);
385
                        QOREG(m_creg);
386
                        break;
387
                case QSPIF_FAST_READ:
388
                        if (m_count == 32) {
389
                                m_addr = m_ireg & 0x0ffffff;
390
                                if (m_debug) printf("FAST READ, ADDR = %08x\n", m_addr);
391
                                QOREG(0x0c3);
392
                                assert((m_addr & 0xfc00000)==0);
393
                        } else if ((m_count >= 40)&&(0 == (m_sreg&0x01))) {
394
                                //if (m_count == 40)
395
                                        //printf("DUMMY BYTE COMPLETE ...\n");
396
                                QOREG(m_mem[m_addr++]);
397
                                // if (m_debug) printf("SPIF[%08x] = %02x\n", m_addr-1, m_oreg);
398
                        } else m_oreg = 0;
399
                        break;
400
                case QSPIF_QUAD_READ_CMD:
401
                        // The command to go into quad read mode took 8 bits
402
                        // that changes the timings, else we'd use quad_Read
403
                        // below
404
                        if (m_count == 32) {
405
                                m_addr = m_ireg & 0x0ffffff;
406
                                // printf("FAST READ, ADDR = %08x\n", m_addr);
407
                                // printf("QSPI: QUAD READ, ADDR = %06x\n", m_addr);
408
                                assert((m_addr & 0xfc00000)==0);
409
                        } else if (m_count == 32+24) {
410
                                m_mode_byte = (m_ireg>>16) & 0x0ff;
411
                                // printf("QSPI: MODE BYTE = %02x\n", m_mode_byte);
412
                        } else if ((m_count > 32+24)&&(0 == (m_sreg&0x01))) {
413
                                QOREG(m_mem[m_addr++]);
414
                                // printf("QSPIF[%08x]/QR = %02x\n",
415
                                        // m_addr-1, m_oreg);
416
                        } else m_oreg = 0;
417
                        break;
418
                case QSPIF_QUAD_READ:
419
                        if (m_count == 32) {
420
                                m_mode_byte = (m_ireg & 0x0ff);
421
                                // printf("QSPI/QR: MODE BYTE = %02x\n", m_mode_byte);
422
                        } else if ((m_count >= 32+16)&&(0 == (m_sreg&0x01))) {
423
                                QOREG(m_mem[m_addr++]);
424
                                // printf("QSPIF[%08x]/QR = %02x\n", m_addr-1, m_oreg & 0x0ff);
425
                        } else m_oreg = 0;
426
                        break;
427
                case QSPIF_PP:
428
                        if (m_count == 32) {
429
                                m_addr = m_ireg & 0x0ffffff;
430
                                if (m_debug) printf("QSPI: PAGE-PROGRAM ADDR = %06x\n", m_addr);
431
                                assert((m_addr & 0xfc00000)==0);
432
                                // m_page = m_addr >> 8;
433
                                for(int i=0; i<256; i++)
434
                                        m_pmem[i] = 0x0ff;
435
                        } else if (m_count >= 40) {
436
                                m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
437
                                // printf("QSPI: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff));
438
                                m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff);
439
                        } break;
440
                case QSPIF_QPP:
441
                        if (m_count == 32) {
442
                                m_addr = m_ireg & 0x0ffffff;
443
                                m_quad_mode = true;
444
                                if (m_debug) printf("QSPI/QR: PAGE-PROGRAM ADDR = %06x\n", m_addr);
445
                                assert((m_addr & 0xfc00000)==0);
446
                                // m_page = m_addr >> 8;
447
                                for(int i=0; i<256; i++)
448
                                        m_pmem[i] = 0x0ff;
449
                        } else if (m_count >= 40) {
450
                                m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
451
                                // printf("QSPI/QR: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff));
452
                                m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff);
453
                        } break;
454
                case QSPIF_SECTOR_ERASE:
455
                        if (m_count == 32) {
456
                                m_addr = m_ireg & 0x0ffc000;
457
                                if (m_debug) printf("SECTOR_ERASE ADDRESS = %08x\n", m_addr);
458
                                assert((m_addr & 0xfc00000)==0);
459
                        } break;
460
                case QSPIF_RELEASE:
461
                        if (m_count >= 32) {
462
                                QOREG(DEVESD);
463
                        } break;
464
                default:
465
                        break;
466
                }
467
        } // else printf("SFLASH->count = %d\n", m_count);
468
 
469
        m_last_sck = sck;
470
        if (m_quad_mode)
471
                return (m_oreg>>8)&0x0f;
472
        else
473
                // return ((m_oreg & 0x0100)?2:0) | (dat & 0x0d);
474
                return (m_oreg & 0x0100)?2:0;
475
}
476
 

powered by: WebSVN 2.1.0

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