Line 1... |
Line 1... |
///////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
|
//
|
//
|
// Filename: sdspisim.cpp
|
// Filename: sdspisim.cpp
|
//
|
//
|
// Project: Wishbone Controlled SD-Card Controller over SPI port
|
// Project: Wishbone Controlled SD-Card Controller over SPI port
|
//
|
//
|
Line 9... |
Line 8... |
// such as might be found on a XuLA2-LX25 board made by xess.com.
|
// such as might be found on a XuLA2-LX25 board made by xess.com.
|
//
|
//
|
// This simulator is for testing use in a Verilator/C++ environment, where
|
// This simulator is for testing use in a Verilator/C++ environment, where
|
// it would be used in place of the actual hardware.
|
// it would be used in place of the actual hardware.
|
//
|
//
|
// Creator: Dan Gisselquist
|
// Creator: Dan Gisselquist, Ph.D.
|
// Gisselquist Technology, LLC
|
// Gisselquist Technology, LLC
|
//
|
//
|
///////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
// Copyright (C) 2015-2016, 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 27... |
Line 26... |
// 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>
|
|
|
Line 67... |
Line 68... |
// tSE = 1500 * MILLISECONDS;
|
// tSE = 1500 * MILLISECONDS;
|
*/
|
*/
|
static const unsigned
|
static const unsigned
|
CCS = 1; // 0: SDSC card, 1: SDHC or SDXC card
|
CCS = 1; // 0: SDSC card, 1: SDHC or SDXC card
|
|
|
SDSPISIM::SDSPISIM(void) {
|
SDSPISIM::SDSPISIM(const bool debug) {
|
m_dev = NULL;
|
m_dev = NULL;
|
m_last_sck = 1;
|
m_last_sck = 1;
|
m_block_address = (CCS==1);
|
m_block_address = (CCS==1);
|
m_host_supports_high_capacity = false;
|
m_host_supports_high_capacity = false;
|
m_powerup_busy = -1;
|
m_powerup_busy = -1;
|
Line 118... |
Line 119... |
// m_creg = 0x001; // Iinitial creg on delivery
|
// m_creg = 0x001; // Iinitial creg on delivery
|
|
|
//
|
//
|
m_reading_data = false;
|
m_reading_data = false;
|
m_have_token = false;
|
m_have_token = false;
|
|
m_debug = debug;
|
}
|
}
|
|
|
void SDSPISIM::load(const char *fname) {
|
void SDSPISIM::load(const char *fname) {
|
m_dev = fopen(fname, "r+b");
|
m_dev = fopen(fname, "r+b");
|
|
|
Line 131... |
Line 133... |
devln = ftell(m_dev);
|
devln = ftell(m_dev);
|
fseek(m_dev, 0l, SEEK_SET);
|
fseek(m_dev, 0l, SEEK_SET);
|
|
|
m_devblocks = devln>>9;
|
m_devblocks = devln>>9;
|
|
|
printf("SDCARD: NBLOCKS = %ld\n", m_devblocks);
|
if (m_debug) printf("SDCARD: NBLOCKS = %ld\n", m_devblocks);
|
}
|
}
|
}
|
}
|
|
|
int SDSPISIM::operator()(const int csn, const int sck, const int mosi) {
|
int SDSPISIM::operator()(const int csn, const int sck, const int mosi) {
|
// Keep track of a timer to determine when page program and erase
|
// Keep track of a timer to determine when page program and erase
|
Line 179... |
Line 181... |
}
|
}
|
|
|
// Only change our output on the falling edge
|
// Only change our output on the falling edge
|
|
|
m_last_sck = sck;
|
m_last_sck = sck;
|
printf("SDSPI: (%3d) [%d,%d,%d] ", m_delay, csn, sck, m_mosi);
|
if (m_debug) printf("SDSPI: (%3d) [%d,%d,%d] ", m_delay, csn, sck, m_mosi);
|
// assert(m_delay > 20);
|
// assert(m_delay > 20);
|
|
|
m_bitpos++;
|
m_bitpos++;
|
m_dat_in = (m_dat_in<<1)|m_mosi;
|
m_dat_in = (m_dat_in<<1)|m_mosi;
|
|
|
|
|
printf("(bitpos=%d,dat_in=%02x)\n", m_bitpos&7, m_dat_in&0x0ff);
|
if (m_debug) printf("(bitpos=%d,dat_in=%02x)\n", m_bitpos&7, m_dat_in&0x0ff);
|
|
|
if ((m_bitpos&7)==0) {
|
if ((m_bitpos&7)==0) {
|
printf("SDSPI--RX BYTE %02x\n", m_dat_in&0x0ff);
|
if (m_debug) printf("SDSPI--RX BYTE %02x\n", m_dat_in&0x0ff);
|
m_dat_out = 0xff;
|
m_dat_out = 0xff;
|
if (m_reading_data) {
|
if (m_reading_data) {
|
if (m_have_token) {
|
if (m_have_token) {
|
m_block_buf[m_rxloc++] = m_dat_in;
|
m_block_buf[m_rxloc++] = m_dat_in;
|
printf("SDSPI: WR[%3d] = %02x\n", m_rxloc-1,
|
if (m_debug) printf("SDSPI: WR[%3d] = %02x\n", m_rxloc-1,
|
m_dat_in&0x0ff);
|
m_dat_in&0x0ff);
|
if (m_rxloc >= 512+2) {
|
if (m_rxloc >= 512+2) {
|
unsigned crc, rxcrc;
|
unsigned crc, rxcrc;
|
crc = blockcrc(512, m_block_buf);
|
crc = blockcrc(512, m_block_buf);
|
rxcrc = ((m_block_buf[512]&0x0ff)<<8)
|
rxcrc = ((m_block_buf[512]&0x0ff)<<8)
|
|(m_block_buf[513]&0x0ff);
|
|(m_block_buf[513]&0x0ff);
|
|
|
printf("LEN = %d\n", m_rxloc);
|
if (m_debug) printf("LEN = %d\n", m_rxloc);
|
printf("CHECKING CRC: (rx) %04x =? %04x (calc)\n",
|
if (m_debug) printf("CHECKING CRC: (rx) %04x =? %04x (calc)\n",
|
crc, rxcrc);
|
crc, rxcrc);
|
m_reading_data = false;
|
m_reading_data = false;
|
m_have_token = false;
|
m_have_token = false;
|
if (rxcrc == crc)
|
if (rxcrc == crc)
|
m_dat_out = 5;
|
m_dat_out = 5;
|
Line 216... |
Line 218... |
assert(rxcrc == crc);
|
assert(rxcrc == crc);
|
}
|
}
|
}
|
}
|
} else {
|
} else {
|
if ((m_dat_in&0x0ff) == 0x0fe) {
|
if ((m_dat_in&0x0ff) == 0x0fe) {
|
printf("SDSPI: TOKEN!!\n");
|
if (m_debug) printf("SDSPI: TOKEN!!\n");
|
m_have_token = true;
|
m_have_token = true;
|
m_rxloc = 0;
|
m_rxloc = 0;
|
} else printf("SDSPI: waiting on token\n");
|
} else if (m_debug)
|
|
printf("SDSPI: waiting on token\n");
|
}
|
}
|
} else if (m_cmdidx < 6) {
|
} else if (m_cmdidx < 6) {
|
printf("SDSPI: CMDIDX = %d\n", m_cmdidx);
|
if (m_debug) printf("SDSPI: CMDIDX = %d\n", m_cmdidx);
|
// All commands *must* start with a 01... pair of bits.
|
// All commands *must* start with a 01... pair of bits.
|
if (m_cmdidx == 0)
|
if (m_cmdidx == 0)
|
assert((m_dat_in&0xc0)==0x40);
|
assert((m_dat_in&0xc0)==0x40);
|
|
|
// Record the command for later processing
|
// Record the command for later processing
|
Line 234... |
Line 237... |
} else if (m_cmdidx == 6) {
|
} else if (m_cmdidx == 6) {
|
// We're going to start a response from here ...
|
// We're going to start a response from here ...
|
m_rspidx = 0;
|
m_rspidx = 0;
|
m_blkdly = 0;
|
m_blkdly = 0;
|
m_blkidx = SDSPI_MAXBLKLEN;
|
m_blkidx = SDSPI_MAXBLKLEN;
|
|
if (m_debug) {
|
printf("SDSPI: CMDIDX = %d -- WE HAVE A COMMAND! [ ", m_cmdidx);
|
printf("SDSPI: CMDIDX = %d -- WE HAVE A COMMAND! [ ", m_cmdidx);
|
for(int i=0; i<6; i++)
|
for(int i=0; i<6; i++)
|
printf("%02x ", m_cmdbuf[i] & 0xff);
|
printf("%02x ", m_cmdbuf[i] & 0xff);
|
printf("]\n"); fflush(stdout);
|
printf("]\n"); fflush(stdout);
|
|
}
|
|
|
unsigned arg;
|
unsigned arg;
|
arg = ((((((m_cmdbuf[1]<<8)|(m_cmdbuf[2]&0x0ff))<<8)
|
arg = ((((((m_cmdbuf[1]<<8)|(m_cmdbuf[2]&0x0ff))<<8)
|
|(m_cmdbuf[3]&0x0ff))<<8)
|
|(m_cmdbuf[3]&0x0ff))<<8)
|
|(m_cmdbuf[4]&0x0ff));
|
|(m_cmdbuf[4]&0x0ff));
|
Line 290... |
Line 295... |
assert(0 && "Not Implemented");
|
assert(0 && "Not Implemented");
|
} m_altcmd_flag = false;
|
} m_altcmd_flag = false;
|
} else {
|
} else {
|
m_altcmd_flag = false;
|
m_altcmd_flag = false;
|
memset(m_rspbuf, 0x0ff, SDSPI_RSPLEN);
|
memset(m_rspbuf, 0x0ff, SDSPI_RSPLEN);
|
printf("SDSPI: Received a command 0x%02x (%d)\n",
|
if (m_debug) printf("SDSPI: Received a command 0x%02x (%d)\n",
|
m_cmdbuf[0], m_cmdbuf[0]&0x03f);
|
m_cmdbuf[0], m_cmdbuf[0]&0x03f);
|
switch(m_cmdbuf[0]&0x3f) {
|
switch(m_cmdbuf[0]&0x3f) {
|
case 0: // CMD0 -- GO_IDLE_STATE
|
case 0: // CMD0 -- GO_IDLE_STATE
|
m_rspbuf[0] = 0x01;
|
m_rspbuf[0] = 0x01;
|
m_rspdly = 4;
|
m_rspdly = 4;
|
Line 365... |
Line 370... |
case 17: // CMD17 -- READ_SINGLE_BLOCK
|
case 17: // CMD17 -- READ_SINGLE_BLOCK
|
assert(m_reset_state == SDSPI_IN_OPERATION);
|
assert(m_reset_state == SDSPI_IN_OPERATION);
|
m_rspbuf[0] = 0x00;
|
m_rspbuf[0] = 0x00;
|
memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
|
memset(m_block_buf, 0x0ff, SDSPI_MAXBLKLEN);
|
if (m_dev) {
|
if (m_dev) {
|
printf("Reading from block %08x of %08lx\n", arg, m_devblocks);
|
if (m_debug) printf("Reading from block %08x of %08lx\n", arg, m_devblocks);
|
if (m_block_address) {
|
if (m_block_address) {
|
assert(arg < m_devblocks);
|
assert(arg < m_devblocks);
|
fseek(m_dev, arg<<9, SEEK_SET);
|
fseek(m_dev, arg<<9, SEEK_SET);
|
} else {
|
} else {
|
assert(arg < m_devblocks<<9);
|
assert(arg < m_devblocks<<9);
|
fseek(m_dev, arg, SEEK_SET);
|
fseek(m_dev, arg, SEEK_SET);
|
}
|
}
|
} m_block_buf[0] = 0x0fe;
|
} m_block_buf[0] = 0x0fe;
|
m_blklen = 512; // (1<<m_csd[5]);
|
m_blklen = 512; // (1<<m_csd[5]);
|
if (m_dev)
|
if (m_dev)
|
fread(&m_block_buf[1], m_blklen, 1, m_dev);
|
m_blklen = fread(&m_block_buf[1], m_blklen, 1, m_dev);
|
else
|
else
|
memset(&m_block_buf[1], 0, m_blklen);
|
memset(&m_block_buf[1], 0, m_blklen);
|
|
m_blklen = (m_blklen != 512) ? 512 : m_blklen;
|
add_block_crc(m_blklen, m_block_buf);
|
add_block_crc(m_blklen, m_block_buf);
|
|
|
m_blkdly = 60;
|
m_blkdly = 60;
|
m_blkidx = 0;
|
m_blkidx = 0;
|
break;
|
break;
|
Line 421... |
Line 427... |
case 38: // CMD38 -- ERASE
|
case 38: // CMD38 -- ERASE
|
case 56: // CMD56 -- GEN_CMD
|
case 56: // CMD56 -- GEN_CMD
|
default: // Unimplemented command
|
default: // Unimplemented command
|
m_rspbuf[0] = 0x04;
|
m_rspbuf[0] = 0x04;
|
m_rspdly = 4;
|
m_rspdly = 4;
|
printf("SDSPI ERR: Command CMD%d not implemented!\n", m_cmdbuf[0]&0x03f);
|
if (m_debug) printf("SDSPI ERR: Command CMD%d not implemented!\n", m_cmdbuf[0]&0x03f);
|
fflush(stdout);
|
fflush(stdout);
|
assert(0 && "Not Implemented");
|
assert(0 && "Not Implemented");
|
}
|
}
|
} m_cmdidx++;
|
} m_cmdidx++;
|
|
|
Line 476... |
Line 482... |
return fill;
|
return fill;
|
}
|
}
|
|
|
bool SDSPISIM::check_cmdcrc(char *buf) const {
|
bool SDSPISIM::check_cmdcrc(char *buf) const {
|
unsigned fill = cmdcrc(5, buf);
|
unsigned fill = cmdcrc(5, buf);
|
printf("SDSPI: CRC-CHECK, should have a CRC of %02x\n", fill);
|
if (m_debug) printf("SDSPI: CRC-CHECK, should have a CRC of %02x\n", fill);
|
return (fill == (buf[5]&0x0ff));
|
return (fill == (buf[5]&0x0ff));
|
}
|
}
|
|
|
unsigned SDSPISIM::blockcrc(int len, char *buf) const {
|
unsigned SDSPISIM::blockcrc(int len, char *buf) const {
|
unsigned int fill = 0, taps = 0x1021;
|
unsigned int fill = 0, taps = 0x1021;
|