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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [bench/] [cpp/] [qspiflashsim.cpp] - Diff between revs 17 and 49

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 17 Rev 49
Line 1... Line 1...
///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
//
//
// Filename:    spiflashsim.cpp
// Filename:    spiflashsim.cpp
//
//
// Project:     Wishbone Controlled Quad SPI Flash Controller
// Project:     Wishbone Controlled Quad SPI Flash Controller
Line 12... Line 12...
//
//
//              This simulator is useful for testing in a Verilator/C++
//              This simulator is useful for testing in a Verilator/C++
//              environment, where this simulator can be used in place of
//              environment, where this simulator can be used in place of
//              the actual hardware.
//              the actual hardware.
//
//
// Creator:     Dan Gisselquist
// Creator:     Dan Gisselquist, Ph.D.
//              Gisselquist Technology, LLC
//              Gisselquist Technology, LLC
//
//
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//
//
// Copyright (C) 2015, Gisselquist Technology, LLC
// Copyright (C) 2015,2017, Gisselquist Technology, LLC
//
//
// This program is free software (firmware): you can redistribute it and/or
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of  the GNU General Public License as published
// modify it under the terms of  the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
// your option) any later version.
Line 30... Line 30...
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// for more details.
//
//
// You should have received a copy of the GNU General Public License along
// You should have received a copy of the GNU General Public License along
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
// target there if the PDF file isn't present.)  If not, see
// target there if the PDF file isn't present.)  If not, see
// <http://www.gnu.org/licenses/> for a copy.
// <http://www.gnu.org/licenses/> for a copy.
//
//
// License:     GPL, v3, as defined and found on www.gnu.org,
// License:     GPL, v3, as defined and found on www.gnu.org,
//              http://www.gnu.org/licenses/gpl.html
//              http://www.gnu.org/licenses/gpl.html
//
//
//
//
///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
 
//
 
//
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <assert.h>
#include <assert.h>
#include <stdlib.h>
#include <stdlib.h>
 
 
#include "regdefs.h"
#include "regdefs.h"
#include "qspiflashsim.h"
#include "qspiflashsim.h"
 
 
#define MEMBYTES        (FLASHWORDS<<2)
#define MEMBYTES        (1<<24)
 
#define MEMMASK         ((MEMBYTES)-1)
 
 
static  const unsigned  DEVID = 0x0115,
static  const unsigned  DEVID = 0x0115,
        DEVESD = 0x014,
        DEVESD = 0x014,
        MICROSECONDS = 100,
        MICROSECONDS = 100,
        MILLISECONDS = MICROSECONDS * 1000,
        MILLISECONDS = MICROSECONDS * 1000,
Line 73... Line 76...
        m_state = QSPIF_IDLE;
        m_state = QSPIF_IDLE;
        m_last_sck = 1;
        m_last_sck = 1;
        m_write_count = 0;
        m_write_count = 0;
        m_ireg = m_oreg = 0;
        m_ireg = m_oreg = 0;
        m_sreg = 0x01c;
        m_sreg = 0x01c;
        m_creg = 0x001; // Iinitial creg on delivery
        m_creg = 0x003; // Initial creg on delivery--assumes quad mode enabled
        m_quad_mode = false;
        m_quad_mode = false;
        m_mode_byte = 0;
        m_mode_byte = 0;
 
 
        memset(m_mem, 0x0ff, MEMBYTES);
        memset(m_mem, 0x0ff, MEMBYTES);
}
}
 
 
void    QSPIFLASHSIM::load(const unsigned addr, const char *fname) {
void    QSPIFLASHSIM::load(const unsigned addr, const char *fname) {
        FILE    *fp;
        FILE    *fp;
 
        int     nr = 0;
        size_t  len;
        size_t  len;
 
 
        if (addr >= MEMBYTES)
        if (addr >= MEMBYTES)
                return;
                return;
        len = MEMBYTES-addr*4;
        len = MEMBYTES-addr*4;
 
 
        if (NULL != (fp = fopen(fname, "r"))) {
        if (NULL != (fp = fopen(fname, "r"))) {
                int     nr = 0;
 
                nr = fread(&m_mem[addr], sizeof(char), len, fp);
                nr = fread(&m_mem[addr], sizeof(char), len, fp);
                fclose(fp);
                fclose(fp);
                if (nr == 0) {
                if (nr == 0) {
                        fprintf(stderr, "SPI-FLASH: Could not read %s\n", fname);
                        fprintf(stderr, "SPI-FLASH: Could not read %s\n", fname);
                        perror("O/S Err:");
                        perror("O/S Err:");
Line 102... Line 105...
                fprintf(stderr, "SPI-FLASH: Could not open %s\n", fname);
                fprintf(stderr, "SPI-FLASH: Could not open %s\n", fname);
                perror("O/S Err:");
                perror("O/S Err:");
        }
        }
}
}
 
 
void    QSPIFLASHSIM::write(const unsigned addr, const unsigned len, const uint32_t *buf) {
void    QSPIFLASHSIM::write(const unsigned offset, const unsigned len, const char *buf) {
        char    *ptr;
        uint32_t        moff = (offset & (MEMBYTES-1));
        if ((addr+len < SPIFLASH)||(addr >= SPIFLASH+MEMBYTES/4))
 
                return;
        memcpy(&m_mem[moff], buf, len);
        printf("FLASH: Copying into memory at S6Add4 %08x, my addr %08x, %d values\n",
 
                addr, (addr-SPIFLASH)<<2, len<<2);
 
        ptr = &m_mem[(addr-SPIFLASH)<<2];
 
        memcpy(ptr, buf, len<<2);
 
        printf("%02x %02x %02x %02x\n", ptr[0]&0x0ff, ptr[1]&0x0ff, ptr[2]&0x0ff, ptr[3]&0x0ff);
 
}
}
 
 
#define QOREG(A)        m_oreg = ((m_oreg & (~0x0ff))|(A&0x0ff))
#define QOREG(A)        m_oreg = ((m_oreg & (~0x0ff))|(A&0x0ff))
 
 
int     QSPIFLASHSIM::operator()(const int csn, const int sck, const int dat) {
int     QSPIFLASHSIM::operator()(const int csn, const int sck, const int dat) {
Line 228... Line 226...
        // printf("PROCESS, COUNT = %d, IREG = %02x\n", m_count, m_ireg);
        // printf("PROCESS, COUNT = %d, IREG = %02x\n", m_count, m_ireg);
        if (m_state == QSPIF_QUAD_READ_IDLE) {
        if (m_state == QSPIF_QUAD_READ_IDLE) {
                assert(m_quad_mode);
                assert(m_quad_mode);
                if (m_count == 24) {
                if (m_count == 24) {
                        if (m_debug) printf("QSPI: Entering from Quad-Read Idle to Quad-Read\n");
                        if (m_debug) printf("QSPI: Entering from Quad-Read Idle to Quad-Read\n");
                        if (m_debug) printf("QSPI: QI/O Idle Addr = %02x\n", m_ireg&0x0ffffff);
                        if (m_debug) printf("QSPI: QI/O Idle Addr = %02x\n", m_ireg&MEMMASK);
                        m_addr = (m_ireg) & 0x0ffffff;
                        m_addr = (m_ireg) & MEMMASK;
                        assert((m_addr & 0xfc00000)==0);
                        assert((m_addr & (~(MEMMASK)))==0);
                        m_state = QSPIF_QUAD_READ;
                        m_state = QSPIF_QUAD_READ;
                } m_oreg = 0;
                } m_oreg = 0;
        } else if (m_count == 8) {
        } else if (m_count == 8) {
                QOREG(0x0a5);
                QOREG(0x0a5);
                // printf("SFLASH-CMD = %02x\n", m_ireg & 0x0ff);
                // printf("SFLASH-CMD = %02x\n", m_ireg & 0x0ff);
Line 372... Line 370...
                case QSPIF_CLSR:
                case QSPIF_CLSR:
                        assert(0 && "Too many clocks for CLSR command!!\n");
                        assert(0 && "Too many clocks for CLSR command!!\n");
                        break;
                        break;
                case QSPIF_RDID:
                case QSPIF_RDID:
                        if (m_count == 32) {
                        if (m_count == 32) {
                                m_addr = m_ireg & 0x0ffffff;
                                m_addr = m_ireg & MEMMASK;
                                if (m_debug) printf("READID, ADDR = %08x\n", m_addr);
                                if (m_debug) printf("READID, ADDR = %08x\n", m_addr);
                                QOREG((DEVID>>8));
                                QOREG((DEVID>>8));
                                if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>8)&0x0ff);
                                if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>8)&0x0ff);
                        } else if (m_count > 32) {
                        } else if (m_count > 32) {
                                if (((m_count-32)>>3)&1)
                                if (((m_count-32)>>3)&1)
Line 396... Line 394...
                        if (m_debug) printf("Read CREG = %02x\n", m_creg);
                        if (m_debug) printf("Read CREG = %02x\n", m_creg);
                        QOREG(m_creg);
                        QOREG(m_creg);
                        break;
                        break;
                case QSPIF_FAST_READ:
                case QSPIF_FAST_READ:
                        if (m_count == 32) {
                        if (m_count == 32) {
                                m_addr = m_ireg & 0x0ffffff;
                                m_addr = m_ireg & MEMMASK;
                                if (m_debug) printf("FAST READ, ADDR = %08x\n", m_addr);
                                if (m_debug) printf("FAST READ, ADDR = %08x\n", m_addr);
                                QOREG(0x0c3);
                                QOREG(0x0c3);
                                assert((m_addr & 0xfc00000)==0);
                                if (m_addr & (~(MEMMASK))) {
 
                                        printf("QSPI: ADDR = %08x ? !!\n", m_addr);
 
                                } assert((m_addr & (~(MEMMASK)))==0);
                        } else if ((m_count >= 40)&&(0 == (m_sreg&0x01))) {
                        } else if ((m_count >= 40)&&(0 == (m_sreg&0x01))) {
                                //if (m_count == 40)
 
                                        //printf("DUMMY BYTE COMPLETE ...\n");
 
                                QOREG(m_mem[m_addr++]);
                                QOREG(m_mem[m_addr++]);
                                // if (m_debug) printf("SPIF[%08x] = %02x\n", m_addr-1, m_oreg);
                                if (m_debug) printf("SPIF[%08x] = %02x\n", m_addr-1, m_oreg);
                        } else m_oreg = 0;
                        } else m_oreg = 0;
                        break;
                        break;
                case QSPIF_QUAD_READ_CMD:
                case QSPIF_QUAD_READ_CMD:
                        // The command to go into quad read mode took 8 bits
                        // The command to go into quad read mode took 8 bits
                        // that changes the timings, else we'd use quad_Read
                        // that changes the timings, else we'd use quad_Read
                        // below
                        // below
                        if (m_count == 32) {
                        if (m_count == 32) {
                                m_addr = m_ireg & 0x0ffffff;
                                m_addr = m_ireg & MEMMASK;
                                // printf("FAST READ, ADDR = %08x\n", m_addr);
                                if (m_debug) printf("QSPI: QUAD READ, ADDR = %06x\n", m_addr);
                                // printf("QSPI: QUAD READ, ADDR = %06x\n", m_addr);
                                assert((m_addr & (~(MEMMASK)))==0);
                                assert((m_addr & 0xfc00000)==0);
 
                        } else if (m_count == 32+24) {
                        } else if (m_count == 32+24) {
                                m_mode_byte = (m_ireg>>16) & 0x0ff;
                                m_mode_byte = (m_ireg>>16) & 0x0ff;
                                // printf("QSPI: MODE BYTE = %02x\n", m_mode_byte);
                                if (m_debug) printf("QSPI: MODE BYTE = %02x\n", m_mode_byte);
 
                                // Send the first byte in the response ... since it's time to start sending bytes
 
                                QOREG(m_mem[m_addr++]);
                        } else if ((m_count > 32+24)&&(0 == (m_sreg&0x01))) {
                        } else if ((m_count > 32+24)&&(0 == (m_sreg&0x01))) {
                                QOREG(m_mem[m_addr++]);
                                QOREG(m_mem[m_addr++]);
                                // printf("QSPIF[%08x]/QR = %02x\n",
                                if (m_debug) printf("QSPIF[%08x]/QR = %02x\n",
                                        // m_addr-1, m_oreg);
                                        m_addr-1, m_oreg);
                        } else m_oreg = 0;
                        } else m_oreg = 0;
                        break;
                        break;
                case QSPIF_QUAD_READ:
                case QSPIF_QUAD_READ:
                        if (m_count == 32) {
                        if (m_count == 32) {
                                m_mode_byte = (m_ireg & 0x0ff);
                                m_mode_byte = (m_ireg & 0x0ff);
                                // printf("QSPI/QR: MODE BYTE = %02x\n", m_mode_byte);
                                // printf("QSPI/QR: MODE BYTE = %02x\n", m_mode_byte);
                        } else if ((m_count >= 32+16)&&(0 == (m_sreg&0x01))) {
                        } else if ((m_count >= 32+16)&&(0 == (m_sreg&0x01))) {
                                QOREG(m_mem[m_addr++]);
                                QOREG(m_mem[m_addr++]);
                                // printf("QSPIF[%08x]/QR = %02x\n", m_addr-1, m_oreg & 0x0ff);
                                if (m_debug) printf("QSPIF[%08x]/QR = %02x\n", m_addr-1, m_oreg & 0x0ff);
                        } else m_oreg = 0;
                        } else m_oreg = 0;
                        break;
                        break;
                case QSPIF_PP:
                case QSPIF_PP:
                        if (m_count == 32) {
                        if (m_count == 32) {
                                m_addr = m_ireg & 0x0ffffff;
                                m_addr = m_ireg & MEMMASK;
                                if (m_debug) printf("QSPI: PAGE-PROGRAM ADDR = %06x\n", m_addr);
                                if (m_debug) printf("QSPI: PAGE-PROGRAM ADDR = %06x\n", m_addr);
                                assert((m_addr & 0xfc00000)==0);
                                assert((m_addr & (~(MEMMASK)))==0);
                                // m_page = m_addr >> 8;
                                // m_page = m_addr >> 8;
                                for(int i=0; i<256; i++)
                                for(int i=0; i<256; i++)
                                        m_pmem[i] = 0x0ff;
                                        m_pmem[i] = 0x0ff;
                        } else if (m_count >= 40) {
                        } else if (m_count >= 40) {
                                m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
                                m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
                                // printf("QSPI: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff));
                                // printf("QSPI: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff));
                                m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff);
                                m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff);
                        } break;
                        } break;
                case QSPIF_QPP:
                case QSPIF_QPP:
                        if (m_count == 32) {
                        if (m_count == 32) {
                                m_addr = m_ireg & 0x0ffffff;
                                m_addr = m_ireg & MEMMASK;
                                m_quad_mode = true;
                                m_quad_mode = true;
                                if (m_debug) printf("QSPI/QR: PAGE-PROGRAM ADDR = %06x\n", m_addr);
                                if (m_debug) printf("QSPI/QR: PAGE-PROGRAM ADDR = %06x\n", m_addr);
                                assert((m_addr & 0xfc00000)==0);
                                assert((m_addr & (~(MEMMASK)))==0);
                                // m_page = m_addr >> 8;
                                // m_page = m_addr >> 8;
                                for(int i=0; i<256; i++)
                                for(int i=0; i<256; i++)
                                        m_pmem[i] = 0x0ff;
                                        m_pmem[i] = 0x0ff;
                        } else if (m_count >= 40) {
                        } else if (m_count >= 40) {
                                m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
                                m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
Line 465... Line 464...
                        } break;
                        } break;
                case QSPIF_SECTOR_ERASE:
                case QSPIF_SECTOR_ERASE:
                        if (m_count == 32) {
                        if (m_count == 32) {
                                m_addr = m_ireg & 0x0ffc000;
                                m_addr = m_ireg & 0x0ffc000;
                                if (m_debug) printf("SECTOR_ERASE ADDRESS = %08x\n", m_addr);
                                if (m_debug) printf("SECTOR_ERASE ADDRESS = %08x\n", m_addr);
                                assert((m_addr & 0xfc00000)==0);
                                assert((m_addr & (~(MEMMASK)))==0);
                        } break;
                        } break;
                case QSPIF_RELEASE:
                case QSPIF_RELEASE:
                        if (m_count >= 32) {
                        if (m_count >= 32) {
                                QOREG(DEVESD);
                                QOREG(DEVESD);
                        } break;
                        } break;

powered by: WebSVN 2.1.0

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