Files

117 lines
2.7 KiB
C++
Raw Permalink Normal View History

2020-11-29 08:16:43 +00:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2020 whitequark <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifndef FMT_H
#define FMT_H
#include "kernel/yosys.h"
YOSYS_NAMESPACE_BEGIN
// Verilog format argument, such as the arguments in:
// $display("foo %d bar %01x", 4'b0, $signed(2'b11))
struct VerilogFmtArg {
enum {
STRING = 0,
INTEGER = 1,
2023-06-28 11:51:20 +10:00
TIME = 2,
2020-11-29 08:16:43 +00:00
} type;
// All types
std::string filename;
unsigned first_line;
// STRING type
std::string str;
// INTEGER type
RTLIL::SigSpec sig;
bool signed_ = false;
2023-06-28 11:51:20 +10:00
// TIME type
bool realtime = false;
2020-11-29 08:16:43 +00:00
};
// RTLIL format part, such as the substitutions in:
// "foo {4:> 4du} bar {2:<01hs}"
// Must be kept in sync with `struct fmt_part` in backends/cxxrtl/runtime/cxxrtl/cxxrtl.h!
2020-11-29 08:16:43 +00:00
struct FmtPart {
enum {
LITERAL = 0,
2020-11-29 08:16:43 +00:00
INTEGER = 1,
STRING = 2,
2024-03-28 07:55:46 +00:00
UNICHAR = 3,
VLOG_TIME = 4,
2020-11-29 08:16:43 +00:00
} type;
// LITERAL type
2020-11-29 08:16:43 +00:00
std::string str;
2024-03-28 07:55:46 +00:00
// INTEGER/STRING/UNICHAR types
2020-11-29 08:16:43 +00:00
RTLIL::SigSpec sig;
2023-06-28 11:51:20 +10:00
// INTEGER/STRING/VLOG_TIME types
2020-11-29 08:16:43 +00:00
enum {
RIGHT = 0,
LEFT = 1,
NUMERIC = 2,
2020-11-29 08:16:43 +00:00
} justify = RIGHT;
char padding = '\0';
size_t width = 0;
2020-11-29 08:16:43 +00:00
// INTEGER type
unsigned base = 10;
bool signed_ = false;
enum {
MINUS = 0,
PLUS_MINUS = 1,
SPACE_MINUS = 2,
} sign = MINUS;
bool hex_upper = false;
bool show_base = false;
bool group = false;
2023-06-28 11:51:20 +10:00
2024-01-19 13:33:14 +00:00
// VLOG_TIME type
2023-06-28 11:51:20 +10:00
bool realtime = false;
2020-11-29 08:16:43 +00:00
};
struct Fmt {
public:
2020-11-29 08:16:43 +00:00
std::vector<FmtPart> parts;
void append_literal(const std::string &str);
2020-11-29 08:16:43 +00:00
2023-06-28 11:51:17 +10:00
void parse_rtlil(const RTLIL::Cell *cell);
2020-11-29 08:16:43 +00:00
void emit_rtlil(RTLIL::Cell *cell) const;
void parse_verilog(const std::vector<VerilogFmtArg> &args, bool sformat_like, int default_base, RTLIL::IdString task_name, RTLIL::IdString module_name);
std::vector<VerilogFmtArg> emit_verilog() const;
2024-01-19 13:33:14 +00:00
void emit_cxxrtl(std::ostream &os, std::string indent, std::function<void(const RTLIL::SigSpec &)> emit_sig, const std::string &context) const;
2023-06-28 11:51:17 +10:00
2020-11-29 08:16:43 +00:00
std::string render() const;
private:
void apply_verilog_automatic_sizing_and_add(FmtPart &part);
2020-11-29 08:16:43 +00:00
};
YOSYS_NAMESPACE_END
#endif