2020-08-20 16:58:20 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.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 3 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "anlogic.hpp"
|
2020-08-21 14:07:28 +02:00
|
|
|
#include "anlogicBitParser.hpp"
|
2020-08-20 16:58:20 +02:00
|
|
|
#include "jtag.hpp"
|
|
|
|
|
#include "device.hpp"
|
2020-08-21 14:07:28 +02:00
|
|
|
#include "progressBar.hpp"
|
|
|
|
|
|
|
|
|
|
#define REFRESH 0x01
|
|
|
|
|
#define IDCODE 0x06
|
|
|
|
|
#define JTAG_PROGRAM 0x30
|
|
|
|
|
#define SPI_PROGRAM 0x39
|
|
|
|
|
#define CFG_IN 0x3b
|
|
|
|
|
#define JTAG_START 0x3d
|
|
|
|
|
#define BYPASS 0xFF
|
2020-08-20 16:58:20 +02:00
|
|
|
|
|
|
|
|
#define IRLENGTH 8
|
|
|
|
|
|
|
|
|
|
Anlogic::Anlogic(Jtag *jtag, const std::string &filename, bool verbose):
|
|
|
|
|
Device(jtag, filename, verbose), _svf(_jtag, _verbose)
|
|
|
|
|
{
|
|
|
|
|
if (_filename != "") {
|
2020-08-21 14:07:28 +02:00
|
|
|
if (_file_extension == "svf" || _file_extension == "bit")
|
2020-08-20 16:58:20 +02:00
|
|
|
_mode = Device::MEM_MODE;
|
|
|
|
|
else
|
|
|
|
|
throw std::exception();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Anlogic::~Anlogic()
|
|
|
|
|
{}
|
2020-08-21 14:07:28 +02:00
|
|
|
|
2020-08-20 16:58:20 +02:00
|
|
|
void Anlogic::reset()
|
|
|
|
|
{
|
2020-08-21 14:07:28 +02:00
|
|
|
_jtag->shiftIR(BYPASS, IRLENGTH);
|
|
|
|
|
_jtag->shiftIR(REFRESH, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(15);
|
|
|
|
|
_jtag->shiftIR(BYPASS, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(200000);
|
2020-08-20 16:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Anlogic::program(unsigned int offset)
|
|
|
|
|
{
|
|
|
|
|
if (_mode == Device::NONE_MODE)
|
|
|
|
|
return;
|
|
|
|
|
if (_mode == Device::MEM_MODE) {
|
2020-08-21 14:07:28 +02:00
|
|
|
if (_file_extension == "svf") {
|
|
|
|
|
_svf.parse(_filename);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
AnlogicBitParser bit(_filename, _verbose);
|
|
|
|
|
bit.parse();
|
|
|
|
|
|
|
|
|
|
// Loading device with 'bypass' instruction.
|
|
|
|
|
_jtag->shiftIR(BYPASS, IRLENGTH);
|
|
|
|
|
_jtag->shiftIR(BYPASS, IRLENGTH);
|
|
|
|
|
// Verify Device id.
|
|
|
|
|
// SIR 8 TDI (06) ;
|
|
|
|
|
// SDR 32 TDI (00000000) TDO (0a014c35) MASK (ffffffff) ;
|
|
|
|
|
// Boundary Scan Chain Contents
|
|
|
|
|
// Position 1: BG256
|
|
|
|
|
// Loading device with 'refresh' instruction.
|
|
|
|
|
_jtag->shiftIR(REFRESH, IRLENGTH);
|
|
|
|
|
// Loading device with 'bypass' & 'spi_program' instruction.
|
|
|
|
|
_jtag->shiftIR(BYPASS, IRLENGTH);
|
|
|
|
|
_jtag->shiftIR(SPI_PROGRAM, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(50000);
|
|
|
|
|
// Loading device with 'jtag program' instruction.
|
|
|
|
|
_jtag->shiftIR(JTAG_PROGRAM, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(15);
|
|
|
|
|
// Loading device with a `cfg_in` instruction.
|
|
|
|
|
_jtag->shiftIR(CFG_IN, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(15);
|
|
|
|
|
|
|
|
|
|
uint8_t *data = bit.getData();
|
|
|
|
|
int len = bit.getLength() / 8;
|
|
|
|
|
_jtag->set_state(Jtag::SHIFT_DR);
|
|
|
|
|
ProgressBar progress("Loading", len, 50);
|
|
|
|
|
int pos = 0;
|
|
|
|
|
uint8_t *ptr = data;
|
|
|
|
|
while (len > 0) {
|
|
|
|
|
int xfer_len = (len > 512)?512:len;
|
|
|
|
|
_jtag->read_write(ptr, NULL, xfer_len*8, (len - xfer_len == 0));
|
|
|
|
|
len -= xfer_len;
|
|
|
|
|
progress.display(pos);
|
|
|
|
|
pos += xfer_len;
|
|
|
|
|
ptr+=xfer_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_jtag->set_state(Jtag::RUN_TEST_IDLE);
|
|
|
|
|
progress.done();
|
|
|
|
|
_jtag->toggleClk(100);
|
|
|
|
|
// Loading device with a `jtag start` instruction.
|
|
|
|
|
_jtag->shiftIR(JTAG_START, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(15);
|
|
|
|
|
// Loading device with 'bypass' instruction.
|
|
|
|
|
_jtag->shiftIR(BYPASS, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(1000);
|
|
|
|
|
// ??
|
|
|
|
|
_jtag->shiftIR(0x31, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(100);
|
|
|
|
|
_jtag->shiftIR(JTAG_START, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(15);
|
|
|
|
|
_jtag->shiftIR(BYPASS, IRLENGTH);
|
|
|
|
|
_jtag->toggleClk(15);
|
2020-08-20 16:58:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-08-21 14:07:28 +02:00
|
|
|
|
2020-08-20 16:58:20 +02:00
|
|
|
int Anlogic::idCode()
|
|
|
|
|
{
|
2020-08-21 14:07:28 +02:00
|
|
|
unsigned char tx_data[4];
|
2020-08-20 16:58:20 +02:00
|
|
|
unsigned char rx_data[4];
|
2020-08-21 14:07:28 +02:00
|
|
|
|
|
|
|
|
tx_data[0] = IDCODE;
|
2020-08-20 16:58:20 +02:00
|
|
|
_jtag->go_test_logic_reset();
|
|
|
|
|
_jtag->shiftIR(tx_data, NULL, IRLENGTH);
|
|
|
|
|
memset(tx_data, 0, 4);
|
|
|
|
|
_jtag->shiftDR(tx_data, rx_data, 32);
|
|
|
|
|
return ((rx_data[0] & 0x000000ff) |
|
|
|
|
|
((rx_data[1] << 8) & 0x0000ff00) |
|
|
|
|
|
((rx_data[2] << 16) & 0x00ff0000) |
|
|
|
|
|
((rx_data[3] << 24) & 0xff000000));
|
|
|
|
|
}
|