Initial code

This commit is contained in:
Miodrag Milanovic 2024-11-19 08:24:49 +01:00
commit 0007e3e36c
13 changed files with 961 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
# GateMate FPGAs Bitstream Documentation and Tools

20
libgm/.gitignore vendored Normal file
View File

@ -0,0 +1,20 @@
Makefile
cmake_install.cmake
compile_commands.json
*.o
CMakeFiles
CMakeScripts
CTestTestfile.cmake
CMakeCache.txt
*.bit
*.cfg
*.so
*.dll
*.a
install_manifest.txt
*~
generated/
.vscode/
# Linux
gmunpack
gmpack

140
libgm/CMakeLists.txt Normal file
View File

@ -0,0 +1,140 @@
cmake_minimum_required(VERSION 3.5)
project(libgatemate)
option(BUILD_SHARED "Build shared GateMate library" OFF)
option(STATIC_BUILD "Create static build of GateMate tools" ON)
set(PROGRAM_PREFIX "" CACHE STRING "Name prefix for executables")
set(CMAKE_CXX_STANDARD 14)
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -bigobj -EHsc")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -O3")
endif()
set(CMAKE_DEFIN)
set(link_param "")
if (STATIC_BUILD)
set(Boost_USE_STATIC_LIBS ON)
if(MSVC)
add_definitions(-DBOOST_PYTHON_STATIC_LIB)
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
elseif (NOT APPLE)
set(link_param "-static")
endif()
else()
if(MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
endif()
if (WASI)
set(USE_THREADS OFF)
add_definitions(
-DBOOST_EXCEPTION_DISABLE
-DBOOST_NO_EXCEPTIONS
-DBOOST_SP_NO_ATOMIC_ACCESS
-DBOOST_AC_DISABLE_THREADS
-DBOOST_NO_CXX11_HDR_MUTEX
-DBOOST_NO_CXX11_HDR_ATOMIC
)
else()
set(USE_THREADS ON)
endif()
set(boost_libs filesystem program_options system)
if (USE_THREADS)
list(APPEND boost_libs thread)
else()
add_definitions(-DNO_THREADS)
endif()
find_package(Boost REQUIRED COMPONENTS ${boost_libs})
find_package(Git)
include_directories(include/ ${Boost_INCLUDE_DIRS})
aux_source_directory(include/ INCLUDE_FILES)
aux_source_directory(src/ SOURCE_FILES)
if (BUILD_SHARED)
add_library(gatemate SHARED ${INCLUDE_FILES} ${SOURCE_FILES})
else()
add_library(gatemate STATIC ${INCLUDE_FILES} ${SOURCE_FILES})
endif()
target_link_libraries(gatemate LINK_PUBLIC ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
include(GNUInstallDirs)
file(RELATIVE_PATH GATEMATE_RPATH_LIBDIR /${CMAKE_INSTALL_BINDIR} /${CMAKE_INSTALL_LIBDIR})
file(RELATIVE_PATH GATEMATE_RPATH_DATADIR /${CMAKE_INSTALL_BINDIR} /${CMAKE_INSTALL_DATADIR})
function(setup_rpath name)
if(APPLE)
set_target_properties(${name} PROPERTIES
BUILD_WITH_INSTALL_RPATH ON
INSTALL_RPATH "@loader_path/${GATEMATE_RPATH_LIBDIR}/${PROGRAM_PREFIX}gatemate"
INSTALL_NAME_DIR "@rpath")
elseif(UNIX)
set_target_properties(${name} PROPERTIES
BUILD_WITH_INSTALL_RPATH ON
INSTALL_RPATH "\$ORIGIN/${GATEMATE_RPATH_LIBDIR}/${PROGRAM_PREFIX}gatemate")
endif()
endfunction()
# Avoid perturbing build if git version hasn't changed
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/generated")
set(LAST_GIT_VERSION "")
if (NOT DEFINED CURRENT_GIT_VERSION)
execute_process(COMMAND git describe --tags OUTPUT_VARIABLE CURRENT_GIT_VERSION WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()
string(STRIP "${CURRENT_GIT_VERSION}" CURRENT_GIT_VERSION)
if (EXISTS "${CMAKE_BINARY_DIR}/generated/last_git_version")
file(READ "${CMAKE_BINARY_DIR}/generated/last_git_version" LAST_GIT_VERSION)
endif()
if (NOT ("${LAST_GIT_VERSION}" STREQUAL "${CURRENT_GIT_VERSION}") OR NOT GIT_EXECUTABLE)
configure_file(
${CMAKE_SOURCE_DIR}/tools/version.cpp.in
${CMAKE_BINARY_DIR}/generated/version.cpp
)
endif()
file(WRITE "${CMAKE_BINARY_DIR}/generated/last_git_version" CURRENT_GIT_VERSION)
add_executable(${PROGRAM_PREFIX}gmunpack ${INCLUDE_FILES} tools/gmunpack.cpp "${CMAKE_BINARY_DIR}/generated/version.cpp")
target_include_directories(${PROGRAM_PREFIX}gmunpack PRIVATE tools)
target_compile_definitions(${PROGRAM_PREFIX}gmunpack PRIVATE GATEMATE_RPATH_DATADIR="${GATEMATE_RPATH_DATADIR}" GATEMATE_PREFIX="${CMAKE_INSTALL_PREFIX}" GATEMATE_PROGRAM_PREFIX="${PROGRAM_PREFIX}")
target_link_libraries(${PROGRAM_PREFIX}gmunpack gatemate ${Boost_LIBRARIES} ${CMAKE_DL_LIBS} ${link_param})
setup_rpath(${PROGRAM_PREFIX}gmunpack)
add_executable(${PROGRAM_PREFIX}gmpack ${INCLUDE_FILES} tools/gmpack.cpp "${CMAKE_BINARY_DIR}/generated/version.cpp")
target_include_directories(${PROGRAM_PREFIX}gmpack PRIVATE tools)
target_compile_definitions(${PROGRAM_PREFIX}gmpack PRIVATE GATEMATE_RPATH_DATADIR="${GATEMATE_RPATH_DATADIR}" GATEMATE_PREFIX="${CMAKE_INSTALL_PREFIX}" GATEMATE_PROGRAM_PREFIX="${PROGRAM_PREFIX}")
target_link_libraries(${PROGRAM_PREFIX}gmpack gatemate ${Boost_LIBRARIES} ${CMAKE_DL_LIBS} ${link_param})
setup_rpath(${PROGRAM_PREFIX}gmpack)
if (WASI)
foreach (tool gmunpack gmpack)
# set(CMAKE_EXECUTABLE_SUFFIX) breaks CMake tests for some reason
set_property(TARGET ${PROGRAM_PREFIX}${tool} PROPERTY SUFFIX ".wasm")
endforeach()
endif()
if (BUILD_SHARED)
install(TARGETS gatemate ${PROGRAM_PREFIX}gmunpack ${PROGRAM_PREFIX}gmpack
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROGRAM_PREFIX}gatemate
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
else()
install(TARGETS ${PROGRAM_PREFIX}gmunpack ${PROGRAM_PREFIX}gmpack
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
file(GLOB_RECURSE CLANGFORMAT_FILES *.cpp *.hpp)
add_custom_target(
clangformat
COMMAND clang-format
-style=file
-i
${CLANGFORMAT_FILES}
)

View File

@ -0,0 +1,51 @@
#ifndef LIBGATEMATE_BITSTREAM_HPP
#define LIBGATEMATE_BITSTREAM_HPP
#include <cstdint>
#include <memory>
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <map>
#include <boost/optional.hpp>
namespace GateMate {
class Chip;
class Bitstream
{
public:
static Bitstream read(std::istream &in);
// Serialise a Chip back to a bitstream
static Bitstream serialise_chip(const Chip &chip);
// Deserialise a bitstream to a Chip
Chip deserialise_chip();
void write_bit(std::ostream &out);
private:
Bitstream(const std::vector<uint8_t> &data);
// Bitstream raw data
std::vector<uint8_t> data;
};
class BitstreamParseError : std::runtime_error
{
public:
explicit BitstreamParseError(const std::string &desc);
BitstreamParseError(const std::string &desc, size_t offset);
const char *what() const noexcept override;
private:
std::string desc;
int offset;
};
} // namespace GateMate
#endif // LIBGATEMATE_BITSTREAM_HPP

17
libgm/include/Chip.hpp Normal file
View File

@ -0,0 +1,17 @@
#ifndef LIBGATEMATE_CHIP_HPP
#define LIBGATEMATE_CHIP_HPP
#include <string>
using namespace std;
namespace GateMate {
class Chip
{
public:
explicit Chip();
};
}
#endif //LIBGATEMATE_CHIP_HPP

109
libgm/include/Util.hpp Normal file
View File

@ -0,0 +1,109 @@
#ifndef LIBGATEMATE_UTIL_HPP
#define LIBGATEMATE_UTIL_HPP
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdint>
#include <vector>
#include <boost/range/adaptor/reversed.hpp>
using namespace std;
namespace GateMate {
enum class VerbosityLevel {
ERROR,
NOTE,
DEBUG,
};
extern VerbosityLevel verbosity;
inline string uint32_to_hexstr(uint32_t val) {
ostringstream os;
os << "0x" << hex << setw(8) << setfill('0') << val;
return os.str();
}
// Hex is not allowed in JSON, to avoid an ugly decimal integer use a string instead
// But we need to parse this back to a uint32_t
inline uint32_t parse_uint32(string str) {
return uint32_t(strtoul(str.c_str(), nullptr, 0));
}
inline string to_string(const vector<bool> &bv) {
ostringstream os;
for (auto bit : boost::adaptors::reverse(bv))
os << (bit ? '1' : '0');
return os.str();
}
inline istream &operator>>(istream &in, vector<bool> &bv) {
bv.clear();
string s;
in >> s;
for (auto c : boost::adaptors::reverse(s)) {
assert((c == '0') || (c == '1'));
bv.push_back((c == '1'));
}
return in;
}
// Skip whitespace, optionally including newlines
inline void skip_blank(istream &in, bool nl = false) {
int c = in.peek();
while (in && (((c == ' ') || (c == '\t')) || (nl && ((c == '\n') || (c == '\r'))))) {
in.get();
c = in.peek();
}
}
// Return true if end of line (or file)
inline bool skip_check_eol(istream &in) {
skip_blank(in, false);
if (!in)
return false;
int c = in.peek();
// Comments count as end of line
if (c == '#') {
in.get();
c = in.peek();
while (in && c != EOF && c != '\n') {
in.get();
c = in.peek();
}
return true;
}
return (c == EOF || c == '\n');
}
// Skip past blank lines and comments
inline void skip(istream &in) {
skip_blank(in, true);
while (in && (in.peek() == '#')) {
// Skip comment line
skip_check_eol(in);
skip_blank(in, true);
}
}
// Return true if at the end of a record (or file)
inline bool skip_check_eor(istream &in) {
skip(in);
int c = in.peek();
return (c == EOF || c == '.');
}
// Return true if at the end of file
inline bool skip_check_eof(istream &in) {
skip(in);
int c = in.peek();
return (c == EOF);
}
}
#define fmt(x) (static_cast<const std::ostringstream&>(std::ostringstream() << x).str())
#define UNUSED(x) (void)(x)
#endif //LIBGATEMATE_UTIL_HPP

456
libgm/src/Bitstream.cpp Normal file
View File

@ -0,0 +1,456 @@
#include "Bitstream.hpp"
#include "Chip.hpp"
#include "Util.hpp"
#include <bitset>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/optional.hpp>
#include <cstring>
#include <iostream>
namespace GateMate {
static constexpr const uint8_t CMD_PLL = 0xc1;
static constexpr const uint8_t CMD_CFGMODE = 0xc2;
static constexpr const uint8_t CMD_CFGRST = 0xc3; //
static constexpr const uint8_t CMD_FLASH = 0xc5;
static constexpr const uint8_t CMD_DLXP = 0xc6; //
static constexpr const uint8_t CMD_DLYP = 0xc7; //
static constexpr const uint8_t CMD_LXLYS = 0xc8;
static constexpr const uint8_t CMD_ACLCU = 0xc9;
static constexpr const uint8_t CMD_DLCU = 0xca;
static constexpr const uint8_t CMD_DRXP = 0xcc; //
static constexpr const uint8_t CMD_RXRYS = 0xce;
static constexpr const uint8_t CMD_FRAM = 0xd2;
static constexpr const uint8_t CMD_SERDES = 0xd7; //
static constexpr const uint8_t CMD_D2D = 0xd8; //
static constexpr const uint8_t CMD_PATH = 0xd9;
static constexpr const uint8_t CMD_JUMP = 0xda; //
static constexpr const uint8_t CMD_CHG_STATUS = 0xdb;
static constexpr const uint8_t CMD_WAIT_PLL = 0xdc; //
static constexpr const uint8_t CMD_SPLL = 0xdd;
static constexpr const uint8_t CMD_SLAVE_MODE = 0xde;
static const uint16_t crc_table_x25[256] = {
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 };
class Crc16 {
public:
uint16_t crc16 = 0xFFFF;
void update_crc16(uint8_t val) {
crc16 = (crc16 >> 8) ^ crc_table_x25[(crc16 & 0xFF) ^ val];
}
uint16_t finalise_crc16() {
return crc16 ^ 0xFFFF;
}
void reset_crc16() {
crc16 = 0xFFFF;
}
};
// The BitstreamReadWriter class stores state (including CRC16) whilst reading
// the bitstream
class BitstreamReadWriter {
public:
BitstreamReadWriter() : data(), iter(data.begin()) {};
BitstreamReadWriter(const vector<uint8_t> &data) : data(data), iter(this->data.begin()) {};
vector<uint8_t> data;
vector<uint8_t>::iterator iter;
Crc16 crc16;
// Return a single byte and update CRC
inline uint8_t get_byte() {
assert(iter < data.end());
uint8_t val = *(iter++);
crc16.update_crc16(val);
return val;
}
// Read a big endian uint16 from the bitstream and update CRC
uint16_t get_uint16() {
uint8_t tmp[2];
get_bytes(tmp, 2);
return (tmp[0] << 8UL) | (tmp[1]);
}
// CRC is in little endian order
uint16_t get_crc() {
uint8_t tmp[2];
get_bytes(tmp, 2);
return (tmp[1] << 8UL) | (tmp[0]);
}
// The command opcode is a byte so this works like get_byte
inline uint8_t get_command_opcode() {
assert(iter < data.end());
uint8_t val = *(iter++);
crc16.update_crc16(val);
return val;
}
// Write a single byte and update CRC
inline void write_byte(uint8_t b) {
data.push_back(b);
crc16.update_crc16(b);
}
// Copy multiple bytes into an OutputIterator and update CRC
template<typename T>
void get_bytes(T out, size_t count) {
for (size_t i = 0; i < count; i++) {
*out = get_byte();
++out;
}
}
void get_vector(std::vector<uint8_t> &out, size_t count) {
for (size_t i = 0; i < count; i++) {
out.push_back(get_byte());
}
}
// Write multiple bytes from an InputIterator and update CRC
template<typename T>
void write_bytes(T in, size_t count) {
for (size_t i = 0; i < count; i++)
write_byte(*(in++));
}
// Skip over bytes while updating CRC
void skip_bytes(size_t count) {
for (size_t i = 0; i < count; i++) get_byte();
}
// Write a big endian uint16_t into the bitstream
void write_uint16(uint16_t val) {
write_byte(uint8_t((val >> 8UL) & 0xFF));
write_byte(uint8_t(val & 0xFF));
}
// Get the offset into the bitstream
size_t get_offset() {
return size_t(distance(data.begin(), iter));
}
// Check the calculated CRC16 against an actual CRC16, expected in the next 2 bytes
void check_crc16() {
uint8_t crc_bytes[2];
uint16_t actual_crc = crc16.finalise_crc16();
get_bytes(crc_bytes, 2);
// cerr << hex << int(crc_bytes[0]) << " " << int(crc_bytes[1]) << endl;
uint16_t exp_crc = (crc_bytes[0] << 8) | crc_bytes[1];
if (actual_crc != exp_crc) {
ostringstream err;
err << "crc fail, calculated 0x" << hex << actual_crc << " but expecting 0x" << exp_crc;
throw BitstreamParseError(err.str(), get_offset());
}
crc16.reset_crc16();
}
// Insert the calculated CRC16 into the bitstream, and then reset it
void insert_crc16() {
uint16_t actual_crc = crc16.finalise_crc16();
write_byte(uint8_t((actual_crc >> 8) & 0xFF));
write_byte(uint8_t((actual_crc) & 0xFF));
crc16.reset_crc16();
}
bool is_end() {
return (iter >= data.end());
}
const vector<uint8_t> &get() {
return data;
};
};
void check_crc(BitstreamReadWriter &rd)
{
uint16_t actual_crc = rd.crc16.finalise_crc16();
uint16_t exp_crc = rd.get_crc(); // crc
if (actual_crc != exp_crc) {
ostringstream err;
err << "crc fail, calculated 0x" << hex << actual_crc << " but expecting 0x" << exp_crc;
throw BitstreamParseError(err.str());
}
}
#define BITSTREAM_DEBUG(x) if (verbosity >= VerbosityLevel::DEBUG) cerr << "bitstream: " << x << endl
#define BITSTREAM_NOTE(x) if (verbosity >= VerbosityLevel::NOTE) cerr << "bitstream: " << x << endl
#define BITSTREAM_FATAL(x, pos) { ostringstream ss; ss << x; throw BitstreamParseError(ss.str(), pos); }
Bitstream::Bitstream(const std::vector<uint8_t> &data) : data(data) { }
Bitstream Bitstream::read(std::istream &in)
{
std::vector<uint8_t> bytes;
in.seekg(0, in.end);
size_t length = size_t(in.tellg());
in.seekg(0, in.beg);
bytes.resize(length);
in.read(reinterpret_cast<char *>(&(bytes[0])), length);
return Bitstream(bytes);
}
Chip Bitstream::deserialise_chip()
{
cerr << "bitstream size: " << data.size() * 8 << " bits" << endl;
Chip chip;
BitstreamReadWriter rd(data);
while(!rd.is_end()) {
rd.crc16.reset_crc16();
uint8_t cmd = rd.get_command_opcode();
uint16_t length = (cmd==CMD_FRAM) ? rd.get_uint16() : rd.get_byte();
std::vector<uint8_t> block;
bool is_block_ram = false;
uint8_t x_pos, y_pos;
switch (cmd) {
case CMD_DLCU:
BITSTREAM_DEBUG("CMD_DLCU");
//if (length>112)
// BITSTREAM_FATAL("DLCU data longer than expected", rd.get_offset());
if (is_block_ram) {
if (length>27)
BITSTREAM_FATAL("RAM configuration must be up to 27 bytes", rd.get_offset());
} else {
if (length>112)
BITSTREAM_FATAL("Tile configuration must be up to 112 bytes", rd.get_offset());
}
// Check header CRC
check_crc(rd);
// Read data block
rd.get_vector(block, length);
// Check data CRC
check_crc(rd);
break;
case CMD_PATH:
BITSTREAM_DEBUG("CMD_PATH");
if (length!=1)
BITSTREAM_FATAL("PATH data must be one byte long", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_byte();
// Check data CRC
check_crc(rd);
// Skip bytes
rd.skip_bytes(9);
break;
case CMD_SPLL:
BITSTREAM_DEBUG("CMD_SPLL");
if (length!=1)
BITSTREAM_FATAL("SPLL data must be one byte long", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_byte();
// Check data CRC
check_crc(rd);
break;
case CMD_PLL:
BITSTREAM_DEBUG("CMD_PLL");
if (length<12)
BITSTREAM_FATAL("PLL data smaller than expected", rd.get_offset());
if (length>24)
BITSTREAM_FATAL("PLL data longer than expected", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_vector(block, length);
// Check data CRC
check_crc(rd);
// Skip bytes
rd.skip_bytes(6);
break;
case CMD_LXLYS:
if (length!=2)
BITSTREAM_FATAL("LXLYS data must be two bytes long", rd.get_offset());
// Check header CRC
check_crc(rd);
BITSTREAM_DEBUG("CMD_LXLYS");
is_block_ram = false;
x_pos = rd.get_byte();
if (x_pos > 81)
BITSTREAM_FATAL("Tile column (X) must be in range 0-81, current value " << x_pos, rd.get_offset());
y_pos = rd.get_byte();
if (y_pos > 65)
BITSTREAM_FATAL("Tile row (Y) must be in range 0-65, current value " << y_pos, rd.get_offset());
// Check data CRC
check_crc(rd);
break;
case CMD_ACLCU:
BITSTREAM_DEBUG("CMD_ACLCU");
if (length!=2)
BITSTREAM_FATAL("ACLCU data must be two bytes long", rd.get_offset());
// Check header CRC
check_crc(rd);
rd.get_uint16();
// Check data CRC
check_crc(rd);
break;
case CMD_RXRYS:
BITSTREAM_DEBUG("CMD_RXRYS");
if (length!=2)
BITSTREAM_FATAL("RXRYS data must be two bytes long", rd.get_offset());
// Check header CRC
check_crc(rd);
is_block_ram = true;
x_pos = rd.get_byte();
if (x_pos > 3)
BITSTREAM_FATAL("RAM column (X) must be in range 0-3, current value " << x_pos, rd.get_offset());
y_pos = rd.get_byte();
if (y_pos > 7)
BITSTREAM_FATAL("RAM row (Y) must be in range 0-7, current value " << y_pos, rd.get_offset());
// Check data CRC
check_crc(rd);
break;
case CMD_FRAM:
BITSTREAM_DEBUG("CMD_FRAM");
if (length>5120)
BITSTREAM_FATAL("FRAM data longer than expected", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_vector(block, length);
// Check data CRC
check_crc(rd);
break;
case CMD_CHG_STATUS:
BITSTREAM_DEBUG("CMD_CHG_STATUS");
if (length>12)
BITSTREAM_FATAL("CHG_STATUS data longer than expected", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_vector(block, length);
// Check data CRC
check_crc(rd);
// Skip bytes
rd.skip_bytes(9);
break;
case CMD_SLAVE_MODE:
BITSTREAM_DEBUG("CMD_SLAVE_MODE");
if (length>1)
BITSTREAM_FATAL("SLAVE_MODE must be one byte long", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_byte();
// Check data CRC
check_crc(rd);
// Skip bytes
rd.skip_bytes(3);
break;
case CMD_FLASH:
BITSTREAM_DEBUG("CMD_FLASH");
if (length>11)
BITSTREAM_FATAL("FLASH data longer than expected", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_vector(block, length);
// Check data CRC
check_crc(rd);
break;
case CMD_CFGMODE:
BITSTREAM_DEBUG("CMD_CFGMODE");
if (length>20)
BITSTREAM_FATAL("PLL data longer than expected", rd.get_offset());
// Check header CRC
check_crc(rd);
// Read data block
rd.get_vector(block, length);
// Check data CRC
check_crc(rd);
// Skip bytes
rd.skip_bytes(3);
break;
default:
BITSTREAM_FATAL("Unhandled command 0x" << hex << setw(2) << setfill('0') << int(cmd),
rd.get_offset());
break;
}
}
return chip;
}
Bitstream Bitstream::serialise_chip(const Chip &/*chip*/) {
return Bitstream({});
}
void Bitstream::write_bit(std::ostream &out)
{
// Dump raw bitstream
out.write(reinterpret_cast<const char *>(&(data[0])), data.size());
}
BitstreamParseError::BitstreamParseError(const std::string &desc) : runtime_error(desc.c_str()), desc(desc), offset(-1)
{
}
BitstreamParseError::BitstreamParseError(const std::string &desc, size_t offset)
: runtime_error(desc.c_str()), desc(desc), offset(int(offset))
{
}
const char *BitstreamParseError::what() const noexcept
{
std::ostringstream ss;
ss << "Bitstream Parse Error: ";
ss << desc;
if (offset != -1)
ss << " [at 0x" << std::hex << setw(8) << setfill('0') << offset << "]";
return strdup(ss.str().c_str());
}
} // namespace GateMate

11
libgm/src/Chip.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "Chip.hpp"
#include "Util.hpp"
using namespace std;
namespace GateMate {
Chip::Chip()
{}
}

5
libgm/src/Util.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "Util.hpp"
namespace GateMate {
VerbosityLevel verbosity = VerbosityLevel::DEBUG;
}

66
libgm/tools/gmpack.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "Bitstream.hpp"
#include "version.hpp"
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <stdexcept>
#include <streambuf>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
using namespace GateMate;
namespace po = boost::program_options;
po::options_description options("Allowed options");
options.add_options()("help,h", "show help");
options.add_options()("verbose,v", "verbose output");
po::positional_options_description pos;
options.add_options()("input", po::value<std::string>()->required(), "input textual configuration");
pos.add("input", 1);
options.add_options()("bit", po::value<std::string>(), "output bitstream file");
pos.add("bit", 1);
po::variables_map vm;
try {
po::parsed_options parsed = po::command_line_parser(argc, argv).options(options).positional(pos).run();
po::store(parsed, vm);
po::notify(vm);
}
catch (po::required_option& e) {
cerr << "Error: input file is mandatory." << endl << endl;
goto help;
}
catch (std::exception& e) {
cerr << "Error: " << e.what() << endl << endl;
goto help;
}
if (vm.count("help")) {
help:
boost::filesystem::path path(argv[0]);
cerr << "Open Source Tools for GateMate FPGAs Version " << git_describe_str << endl;
cerr << "Copyright (C) 2024 YosysHQ GmbH" << endl;
cerr << endl;
cerr << path.stem().c_str() << ": GateMate bitstream packer" << endl;
cerr << endl;
cerr << "Usage: " << argv[0] << " input.config [output.bit] [options]" << endl;
cerr << endl;
cerr << options << endl;
return vm.count("help") ? 0 : 1;
}
ifstream config_file(vm["input"].as<string>());
if (!config_file) {
cerr << "Failed to open input file" << endl;
return 1;
}
string textcfg((std::istreambuf_iterator<char>(config_file)), std::istreambuf_iterator<char>());
return 0;
}

78
libgm/tools/gmunpack.cpp Normal file
View File

@ -0,0 +1,78 @@
#include "Chip.hpp"
#include "Bitstream.hpp"
#include "version.hpp"
#include <iostream>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <stdexcept>
#include <streambuf>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
using namespace GateMate;
namespace po = boost::program_options;
po::options_description options("Allowed options");
options.add_options()("help,h", "show help");
options.add_options()("verbose,v", "verbose output");
po::positional_options_description pos;
options.add_options()("input", po::value<std::string>()->required(), "input bitstream file");
pos.add("input", 1);
options.add_options()("textcfg", po::value<std::string>()->required(), "output textual configuration");
pos.add("textcfg", 1);
po::variables_map vm;
try {
po::parsed_options parsed = po::command_line_parser(argc, argv).options(options).positional(pos).run();
po::store(parsed, vm);
po::notify(vm);
}
catch (po::required_option &e) {
cerr << "Error: input file is mandatory." << endl << endl;
goto help;
}
catch (std::exception &e) {
cerr << "Error: " << e.what() << endl << endl;
goto help;
}
if (vm.count("help")) {
help:
boost::filesystem::path path(argv[0]);
cerr << "Open Source Tools for GateMate FPGAs Version " << git_describe_str << endl;
cerr << "Copyright (C) 2024 YosysHQ GmbH" << endl;
cerr << endl;
cerr << path.stem().c_str() << ": GateMate bitstream to text config converter" << endl;
cerr << endl;
cerr << "Usage: " << argv[0] << " input.bit [output.config] [options]" << endl;
cerr << endl;
cerr << options << endl;
return vm.count("help") ? 0 : 1;
}
ifstream bit_file(vm["input"].as<string>(), ios::binary);
if (!bit_file) {
cerr << "Failed to open input file" << endl;
return 1;
}
try {
Bitstream::read(bit_file).deserialise_chip();
ofstream out_file(vm["textcfg"].as<string>());
if (!out_file) {
cerr << "Failed to open output file" << endl;
return 1;
}
return 0;
} catch (BitstreamParseError &e) {
cerr << "Failed to process input bitstream: " << e.what() << endl;
return 1;
} catch (runtime_error &e) {
cerr << "Failed to process input bitstream: " << e.what() << endl;
return 1;
}
}

View File

@ -0,0 +1,2 @@
#include "version.hpp"
const std::string git_describe_str = "@CURRENT_GIT_VERSION@";

5
libgm/tools/version.hpp Normal file
View File

@ -0,0 +1,5 @@
#ifndef VERSION_H
#define VERSION_H
#include <string>
extern const std::string git_describe_str;
#endif