From 5f9a8835da80dd51fb756ad05a3ab567aec07ceb Mon Sep 17 00:00:00 2001 From: Gwenhael Goavec-Merou Date: Thu, 18 Feb 2021 21:09:34 +0100 Subject: [PATCH] devices: simplify write RAM/Flash --- src/anlogic.cpp | 5 ++--- src/anlogic.hpp | 2 +- src/device.hpp | 8 ++++++++ src/gowin.cpp | 6 ++---- src/gowin.hpp | 2 +- src/lattice.cpp | 8 ++++---- src/lattice.hpp | 2 +- src/main.cpp | 31 +++++++++++++++---------------- src/xilinx.cpp | 8 ++------ src/xilinx.hpp | 2 +- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/anlogic.cpp b/src/anlogic.cpp index ef97e26..8b53983 100644 --- a/src/anlogic.cpp +++ b/src/anlogic.cpp @@ -34,15 +34,14 @@ #define IRLENGTH 8 Anlogic::Anlogic(Jtag *jtag, const std::string &filename, - bool flash_wr, bool sram_wr, int8_t verbose): + Device::prog_type_t prg_type, int8_t verbose): Device(jtag, filename, verbose), _svf(_jtag, _verbose) { - (void)flash_wr; if (_filename != "") { if (_file_extension == "svf") _mode = Device::MEM_MODE; else if (_file_extension == "bit") { - if (sram_wr) + if (prg_type == Device::WR_SRAM) _mode = Device::MEM_MODE; else _mode = Device::SPI_MODE; diff --git a/src/anlogic.hpp b/src/anlogic.hpp index 139845a..7e20db8 100644 --- a/src/anlogic.hpp +++ b/src/anlogic.hpp @@ -29,7 +29,7 @@ class Anlogic: public Device, SPIInterface { public: Anlogic(Jtag *jtag, const std::string &filename, - bool flash_wr, bool sram_wr, int8_t verbose); + Device::prog_type_t prg_type, int8_t verbose); ~Anlogic(); void program(unsigned int offset = 0) override; diff --git a/src/device.hpp b/src/device.hpp index f320ac6..fe9ddaa 100644 --- a/src/device.hpp +++ b/src/device.hpp @@ -2,6 +2,7 @@ #define DEVICE_HPP #include +#include #include "jtag.hpp" @@ -18,11 +19,18 @@ class Device { FLASH_MODE = 1, MEM_MODE = 2 }; + + typedef enum { + WR_SRAM = 0, + WR_FLASH = 1 + } prog_type_t; + Device(Jtag *jtag, std::string filename, int8_t verbose = false); virtual ~Device(); virtual void program(unsigned int offset = 0) = 0; virtual int idCode() = 0; virtual void reset(); + protected: Jtag *_jtag; std::string _filename; diff --git a/src/gowin.cpp b/src/gowin.cpp index da58e66..3b1bfb9 100644 --- a/src/gowin.cpp +++ b/src/gowin.cpp @@ -69,15 +69,13 @@ using namespace std; #define EF_PROGRAM 0x71 #define EFLASH_ERASE 0x75 -Gowin::Gowin(Jtag *jtag, const string filename, bool flash_wr, bool sram_wr, +Gowin::Gowin(Jtag *jtag, const string filename, Device::prog_type_t prg_type, int8_t verbose): Device(jtag, filename, verbose), is_gw1n1(false) { _fs = NULL; if (_filename != "") { if (_file_extension == "fs") { - if (flash_wr && sram_wr) - throw std::runtime_error("both write-flash and write-sram can't be set"); - if (flash_wr) + if (prg_type == Device::WR_FLASH) _mode = Device::FLASH_MODE; else _mode = Device::MEM_MODE; diff --git a/src/gowin.hpp b/src/gowin.hpp index 0fdf10c..b1559d3 100644 --- a/src/gowin.hpp +++ b/src/gowin.hpp @@ -30,7 +30,7 @@ class Gowin: public Device { public: - Gowin(Jtag *jtag, std::string filename, bool flash_wr, bool sram_wr, + Gowin(Jtag *jtag, std::string filename, Device::prog_type_t prg_type, int8_t verbose); ~Gowin(); int idCode() override; diff --git a/src/lattice.cpp b/src/lattice.cpp index 8f03365..8a98b16 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -68,19 +68,19 @@ using namespace std; # define REG_STATUS_EXEC_ERR (1 << 26) Lattice::Lattice(Jtag *jtag, const string filename, - bool flash_wr, bool sram_wr, int8_t verbose): + Device::prog_type_t prg_type, int8_t verbose): Device(jtag, filename, verbose), _fpga_family(UNKNOWN_FAMILY) { - (void)sram_wr; if (_filename != "") { if (_file_extension == "jed" || _file_extension == "mcs") { _mode = Device::FLASH_MODE; } else if (_file_extension == "bit") { - if (flash_wr) + if (prg_type == Device::WR_FLASH) _mode = Device::FLASH_MODE; else _mode = Device::MEM_MODE; - } else if (flash_wr) { // for raw bin to flash at offset != 0 + } else if (prg_type == Device::WR_FLASH) { + // for raw bin to flash at offset != 0 _mode = Device::FLASH_MODE; } else { throw std::exception(); diff --git a/src/lattice.hpp b/src/lattice.hpp index 6e5041e..ccf8919 100644 --- a/src/lattice.hpp +++ b/src/lattice.hpp @@ -31,7 +31,7 @@ class Lattice: public Device, SPIInterface { public: - Lattice(Jtag *jtag, std::string filename, bool flash_wr, bool sram_wr, + Lattice(Jtag *jtag, std::string filename, Device::prog_type_t prg_type, int8_t verbose); int idCode() override; int userCode(); diff --git a/src/main.cpp b/src/main.cpp index f5da813..6d2498f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,8 +58,7 @@ struct arguments { bool list_cables; bool list_boards; bool list_fpga; - bool write_flash; - bool write_sram; + Device::prog_type_t prg_type; bool is_list_command; bool spi; }; @@ -76,7 +75,7 @@ int main(int argc, char **argv) /* command line args. */ struct arguments args = {0, false, false, 0, "", "", "-", "", -1, 6000000, "-", - false, false, false, false, false, true, false, false}; + false, false, false, false, Device::WR_SRAM, false, false}; /* parse arguments */ try { if (parse_opt(argc, argv, &args, &pins_config)) @@ -91,6 +90,11 @@ int main(int argc, char **argv) return EXIT_SUCCESS; } + if (args.prg_type == Device::WR_SRAM) + cout << "write to ram" << endl; + if (args.prg_type == Device::WR_FLASH) + cout << "write to flash" << endl; + if (args.board[0] != '-' && board_list.find(args.board) != board_list.end()) { board = &(board_list[args.board]); } @@ -263,18 +267,18 @@ int main(int argc, char **argv) Device *fpga; try { if (fab == "xilinx") { - fpga = new Xilinx(jtag, args.bit_file, args.write_flash, args.write_sram, + fpga = new Xilinx(jtag, args.bit_file, args.prg_type, args.verbose); } else if (fab == "altera") { fpga = new Altera(jtag, args.bit_file, args.verbose); } else if (fab == "anlogic") { - fpga = new Anlogic(jtag, args.bit_file, args.write_flash, args.write_sram, + fpga = new Anlogic(jtag, args.bit_file, args.prg_type, args.verbose); } else if (fab == "Gowin") { - fpga = new Gowin(jtag, args.bit_file, args.write_flash, args.write_sram, + fpga = new Gowin(jtag, args.bit_file, args.prg_type, args.verbose); } else if (fab == "lattice") { - fpga = new Lattice(jtag, args.bit_file, args.write_flash, args.write_sram, + fpga = new Lattice(jtag, args.bit_file, args.prg_type, args.verbose); } else { printError("Error: manufacturer " + fab + " not supported"); @@ -414,15 +418,10 @@ int parse_opt(int argc, char **argv, struct arguments *args, jtag_pins_conf_t *p throw std::exception(); } - if (result.count("write-flash")) { - args->write_flash = true; - args->write_sram = false; - } - - if (result.count("write-sram")) { - args->write_flash = false; - args->write_sram = true; - } + if (result.count("write-flash")) + args->prg_type = Device::WR_FLASH; + else if (result.count("write-sram")) + args->prg_type = Device::WR_SRAM; if (result.count("freq")) { double freq; diff --git a/src/xilinx.cpp b/src/xilinx.cpp index 754bdc8..16df4bb 100644 --- a/src/xilinx.cpp +++ b/src/xilinx.cpp @@ -14,18 +14,14 @@ #include "progressBar.hpp" Xilinx::Xilinx(Jtag *jtag, const std::string &filename, - bool flash_wr, bool sram_wr, int8_t verbose): + Device::prog_type_t prg_type, int8_t verbose): Device(jtag, filename, verbose) { if (_filename != ""){ - if (flash_wr && sram_wr) { - printError("both write-flash and write-sram can't be set"); - throw std::exception(); - } if (_file_extension == "mcs") { _mode = Device::SPI_MODE; } else if (_file_extension == "bit" || _file_extension == "bin") { - if (sram_wr) + if (prg_type == Device::WR_SRAM) _mode = Device::MEM_MODE; else _mode = Device::SPI_MODE; diff --git a/src/xilinx.hpp b/src/xilinx.hpp index 578d9c4..4a29728 100644 --- a/src/xilinx.hpp +++ b/src/xilinx.hpp @@ -11,7 +11,7 @@ class Xilinx: public Device, SPIInterface { public: Xilinx(Jtag *jtag, const std::string &filename, - bool flash_wr, bool sram_wr, int8_t verbose); + Device::prog_type_t prg_type, int8_t verbose); ~Xilinx(); void program(unsigned int offset = 0) override;