/***************************************************************************
* Copyright (C) 2005 by Dominic Rath *
* Dominic.Rath@gmx.de *
* *
* Copyright (C) 2007,2008 Øyvind Harboe *
* oyvind.harboe@zylin.com *
* *
* Copyright (C) 2008 Peter Hettkamp *
* peter.hettkamp@htp-tel.de *
* *
* Copyright (C) 2009 SoftPLC Corporation. http://softplc.com *
* Dick Hollenbeck <dick@softplc.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/
/* The specification for SVF is available here:
* http://www.asset-intertech.com/support/svf.pdf
* Below, this document is refered to as the "SVF spec".
*
* The specification for XSVF is available here:
* http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
* Below, this document is refered to as the "XSVF spec".
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "xsvf.h"
#include <jtag/jtag.h>
#include <svf/svf.h>
/* XSVF commands, from appendix B of xapp503.pdf */
#define XCOMPLETE 0x00
#define XTDOMASK 0x01
#define XSIR 0x02
#define XSDR 0x03
#define XRUNTEST 0x04
#define XREPEAT 0x07
#define XSDRSIZE 0x08
#define XSDRTDO 0x09
#define XSETSDRMASKS 0x0A
#define XSDRINC 0x0B
#define XSDRB 0x0C
#define XSDRC 0x0D
#define XSDRE 0x0E
#define XSDRTDOB 0x0F
#define XSDRTDOC 0x10
#define XSDRTDOE 0x11
#define XSTATE 0x12
#define XENDIR 0x13
#define XENDDR 0x14
#define XSIR2 0x15
#define XCOMMENT 0x16
#define XWAIT 0x17
/* XWAITSTATE is not in the xilinx XSVF spec, but the svf2xsvf.py translator
* generates this. Arguably it is needed because the XSVF XRUNTEST command
* was ill conceived and does not directly flow out of the SVF RUNTEST command.
* This XWAITSTATE does map directly from the SVF RUNTEST command.
*/
#define XWAITSTATE 0x18
/* Lattice has extended the SVF file format, and Dick Hollenbeck's python based
* SVF2XSVF converter supports these 3 additional XSVF opcodes, LCOUNT, LDELAY, LSDR.
* Here is an example of usage of the 3 lattice opcode extensions:
! Set the maximum loop count to 25.
LCOUNT 25;
! Step to DRPAUSE give 5 clocks and wait for 1.00e + 000 SEC.
LDELAY DRPAUSE 5 TCK 1.00E-003 SEC;
! Test for the completed status. Match means pass.
! Loop back to LDELAY line if not match and loop count less than 25.
LSDR 1 TDI (0)
TDO (1);
*/
#define LCOUNT 0x19
#define LDELAY 0x1A
#define LSDR 0x1B
#define XTRST 0x1C
/* XSVF valid state values for the XSTATE command, from appendix B of xapp503.pdf */
#define XSV_RESET 0x00
#define XSV_IDLE 0x01
#define XSV_DRSELECT 0x02
#define XSV_DRCAPTURE 0x03
#define XSV_DRSHIFT 0x04
#define XSV_DREXIT1 0x05
#define XSV_DRPAUSE 0x06
#define XSV_DREXIT2 0x07
#define XSV_DRUPDATE 0x08
#define XSV_IRSELECT 0x09
#define XSV_IRCAPTURE 0x0A
#define XSV_IRSHIFT 0x0B
#define XSV_IREXIT1 0x0C
#define XSV_IRPAUSE 0x0D
#define XSV_IREXIT2 0x0E
#define XSV_IRUPDATE 0x0F
/* arguments to XTRST */
#define XTRST_ON 0
#define XTRST_OFF 1
#define XTRST_Z 2
#define XTRST_ABSENT 3
#define XSTATE_MAX_PATH 12
static int xsvf_fd;
/* map xsvf tap state to an openocd "tap_state_t" */
static tap_state_t xsvf_to_tap(int xsvf_state)
{
tap_state_t ret;
switch (xsvf_state) {
case XSV_RESET:
ret = TAP_RESET;
break;
case XSV_IDLE:
ret = TAP_IDLE;
break;
case XSV_DRSELECT:
ret = TAP_DRSELECT;
break;
case XSV_DRCAPTURE:
ret = TAP_DRCAPTURE;
break;
case XSV_DRSHIFT:
ret = TAP_DRSHIFT;
break;
case XSV_DREXIT1:
ret = TAP_DREXIT1;
break;
case XSV_DRPAUSE:
ret = TAP_DRPAUSE;
break;
case XSV_DREXIT2:
ret = TAP_DREXIT2;
break;
case XSV_DRUPDATE:
ret = TAP_DRUPDATE;
break;
case XSV_IRSELECT:
ret = TAP_IRSELECT;
break;
case XSV_IRCAPTURE:
ret = TAP_IRCAPTURE;
break;
case XSV_IRSHIFT:
ret = TAP_IRSHIFT;
break;
case XSV_IREXIT1:
ret = TAP_IREXIT1;
break;
case XSV_IRPAUSE:
ret = TAP_IRPAUSE;
break;
case XSV_IREXIT2:
ret = TAP_IREXIT2;
break;
case XSV_IRUPDATE:
ret = TAP_IRUPDATE;
break;
default:
LOG_ERROR("UNKNOWN XSVF STATE 0x%02X", xsvf_state);
exit(1);
}
return ret;
}
static int xsvf_read_buffer(int num_bits, int fd, uint8_t *buf)
{
int num_bytes;
for (num_bytes = (num_bits + 7) / 8; num_bytes > 0; num_bytes--) {
/* reverse the order of bytes as they are read sequentially from file */
if (read(fd, buf + num_bytes - 1, 1) < 0)
return ERROR_XSVF_EOF;
}
return ERROR_OK;
}
COMMAND_HANDLER(handle_xsvf_command)
{
uint8_t *dr_out_buf = NULL; /* from host to device (TDI) */
uint8_t *dr_in_buf = NULL; /* from device to host (TDO) */
uint8_t *dr_in_mask = NULL;
int xsdrsize = 0