From 19a5b01f0b2a4a3609a8a8ed32fba8dd376e1905 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Fri, 27 Oct 2023 00:37:29 +0200 Subject: move utils into subfolder and different files --- src/browse.cc | 2 +- src/event.cc | 2 +- src/lt_bus.cc | 2 +- src/minimal_socket.cc | 2 +- src/models/lt_bus.h | 196 +++++--------------------------- src/sc_export.cc | 2 +- src/sc_export2.cc | 2 +- src/utils.h | 32 ------ src/utils/log.h | 32 ++++++ src/utils/range.h | 25 ++++ src/utils/sysc.h | 24 ++++ src/utils/tlm_initiator_socket_tagged.h | 48 ++++++++ src/utils/tlm_target_socket_tagged.h | 69 +++++++++++ src/utils/types.h | 11 ++ 14 files changed, 246 insertions(+), 203 deletions(-) delete mode 100644 src/utils.h create mode 100644 src/utils/log.h create mode 100644 src/utils/range.h create mode 100644 src/utils/sysc.h create mode 100644 src/utils/tlm_initiator_socket_tagged.h create mode 100644 src/utils/tlm_target_socket_tagged.h create mode 100644 src/utils/types.h (limited to 'src') diff --git a/src/browse.cc b/src/browse.cc index afc132e..e151bc2 100644 --- a/src/browse.cc +++ b/src/browse.cc @@ -1,5 +1,5 @@ #include -#include "utils.h" +#include "utils/log.h" using sc_core::sc_module; using sc_core::sc_module_name; diff --git a/src/event.cc b/src/event.cc index 96750bc..655beb2 100644 --- a/src/event.cc +++ b/src/event.cc @@ -1,5 +1,5 @@ #include -#include "utils.h" +#include "utils/log.h" using sc_core::sc_event; using sc_core::sc_module; diff --git a/src/lt_bus.cc b/src/lt_bus.cc index 078a8d8..42495c0 100644 --- a/src/lt_bus.cc +++ b/src/lt_bus.cc @@ -1,5 +1,5 @@ #include "models/lt_bus.h" -#include "utils.h" +#include "utils/log.h" #include #include diff --git a/src/minimal_socket.cc b/src/minimal_socket.cc index 7fe7f38..1daac6b 100644 --- a/src/minimal_socket.cc +++ b/src/minimal_socket.cc @@ -1,5 +1,5 @@ #include -#include "utils.h" +#include "utils/log.h" using sc_core::sc_export; using sc_core::sc_interface; diff --git a/src/models/lt_bus.h b/src/models/lt_bus.h index bab1866..f52f8b0 100644 --- a/src/models/lt_bus.h +++ b/src/models/lt_bus.h @@ -1,154 +1,16 @@ #ifndef SYSC_PLAYGROUND_LT_BUS #define SYSC_PLAYGROUND_LT_BUS -#include -#include +#include "utils/range.h" +#include "utils/sysc.h" +#include "utils/tlm_initiator_socket_tagged.h" +#include "utils/tlm_target_socket_tagged.h" +#include "utils/types.h" #include -#include #include #include -struct scoped_push_hierarchy { - [[nodiscard]] explicit scoped_push_hierarchy(sc_core::sc_module& mod) - : m_mod(mod), m_simctx(sc_core::sc_get_curr_simcontext()) { - assert(m_simctx); - m_simctx->hierarchy_push(&m_mod); - } - - ~scoped_push_hierarchy() { - const auto* top = m_simctx->hierarchy_pop(); - assert(top == &m_mod); - } - - private: - sc_core::sc_simcontext* m_simctx{nullptr}; - sc_core::sc_module& m_mod; -}; - -struct range { - constexpr explicit range(std::uint64_t start, std::uint64_t end) - : start{start}, end{end} { - assert(start < end); - } - - constexpr bool overlaps(range rhs) const { - return start <= rhs.end && rhs.start <= end; - } - - constexpr bool contains(range rhs) const { - return start <= rhs.start && rhs.end <= end; - } - - std::uint64_t start; - std::uint64_t end; -}; - -template -class tlm_initiator_socket_tagged : public tlm::tlm_initiator_socket<>, - public tlm::tlm_bw_transport_if<> { - using cb_invalidate_direct_mem_ptr = void (Module::*)(std::size_t, - sc_dt::uint64, - sc_dt::uint64); - - public: - explicit tlm_initiator_socket_tagged(const char* name, - std::size_t id, - Module* mod, - cb_invalidate_direct_mem_ptr i) - : tlm_initiator_socket<>{name}, - m_mod{mod}, - m_id{id}, - m_invalidate_direct_mem_ptr{i} { - bind(*static_cast*>(this)); - } - - private: - // -- TLM_BW_TRANSPORT_IF ---------------------------------------------------- - - virtual void invalidate_direct_mem_ptr(sc_dt::uint64 start, - sc_dt::uint64 end) override { - (m_mod->*m_invalidate_direct_mem_ptr)(m_id, start, end); - } - - virtual tlm::tlm_sync_enum nb_transport_bw(tlm::tlm_generic_payload&, - tlm::tlm_phase&, - sc_core::sc_time&) override { - std::fprintf( - stderr, "tlm_initiator_socket_tagged: nb_transport_bw not supported\n"); - std::abort(); - } - - // -- MEMBER ----------------------------------------------------------------- - - std::size_t m_id{0}; - Module* m_mod{nullptr}; - cb_invalidate_direct_mem_ptr m_invalidate_direct_mem_ptr{nullptr}; -}; - -template -class tlm_target_socket_tagged : public tlm::tlm_target_socket<>, - public tlm::tlm_fw_transport_if<> { - using cb_b_transport = void (Module::*)(std::size_t, - tlm::tlm_generic_payload&, - sc_core::sc_time&); - - using cb_get_direct_mem_ptr = bool (Module::*)(std::size_t, - tlm::tlm_generic_payload&, - tlm::tlm_dmi&); - using cb_transport_dbg = unsigned int (Module::*)(std::size_t, - tlm::tlm_generic_payload&); - - public: - explicit tlm_target_socket_tagged(const char* name, - std::size_t id, - Module* mod, - cb_b_transport b, - cb_get_direct_mem_ptr m, - cb_transport_dbg d) - : tlm_target_socket<>{name}, - m_mod{mod}, - m_id{id}, - m_b_transport{b}, - m_get_direct_mem_ptr{m}, - m_transport_dbg{d} { - bind(*static_cast*>(this)); - } - - private: - // -- TLM_FW_TRANSPORT_IF ---------------------------------------------------- - - virtual void b_transport(tlm::tlm_generic_payload& tx, - sc_core::sc_time& t) override { - return (m_mod->*m_b_transport)(m_id, tx, t); - } - - virtual bool get_direct_mem_ptr(tlm::tlm_generic_payload& tx, - tlm::tlm_dmi& dmi_data) override { - return (m_mod->*m_get_direct_mem_ptr)(m_id, tx, dmi_data); - } - - virtual unsigned int transport_dbg(tlm::tlm_generic_payload& tx) override { - return (m_mod->*m_transport_dbg)(m_id, tx); - } - - virtual tlm::tlm_sync_enum nb_transport_fw(tlm::tlm_generic_payload& tx, - tlm::tlm_phase& phase, - sc_core::sc_time& t) override { - std::fprintf(stderr, - "tlm_target_socket_tagged: nb_transport_fw not supported\n"); - std::abort(); - } - - // -- MEMBER ----------------------------------------------------------------- - - std::size_t m_id{0}; - Module* m_mod{nullptr}; - cb_b_transport m_b_transport{nullptr}; - cb_get_direct_mem_ptr m_get_direct_mem_ptr{nullptr}; - cb_transport_dbg m_transport_dbg{nullptr}; -}; - class lt_bus : public sc_core::sc_module { using target_socket = tlm_target_socket_tagged; using target_socket_ptr = std::unique_ptr; @@ -163,7 +25,7 @@ class lt_bus : public sc_core::sc_module { // -- ATTACH BUS INITIATOR --------------------------------------------------- void attach_initiator(tlm::tlm_base_initiator_socket_b<>& init) { - const std::size_t id = m_initiators.size(); + const usize id = m_initiators.size(); const std::string name = "init" + std::to_string(id); { // Add current module on top of module stack for tlm sockets. @@ -183,8 +45,8 @@ class lt_bus : public sc_core::sc_module { // -- ATTACH BUS TARGET ------------------------------------------------------ void attach_target(tlm::tlm_base_target_socket_b<>& target, - std::uint64_t start, - std::uint64_t end) { + u64 start, + u64 end) { const range addr{start, end}; // Check if new range overlaps with any registered memory map range. @@ -199,7 +61,7 @@ class lt_bus : public sc_core::sc_module { } } - const std::size_t id = m_targets.size(); + const usize id = m_targets.size(); const std::string name = "target" + std::to_string(id); { // Add current module on top of module stack for tlm sockets. @@ -218,10 +80,20 @@ class lt_bus : public sc_core::sc_module { m_mappings.push_back({addr, id}); } + // -- DUMP ------------------------------------------------------------------- + + void dump() const { + // Dump memory map. + for (const auto& map : m_mappings) { + std::printf("%08lx - %08lx :[%2ld] %s\n", map.addr.start, map.addr.end, + map.idx, m_targets[map.idx].get()->name()); + } + } + private: // -- TLM_BW_TRANSPORT_IF ---------------------------------------------------- - void invalidate_direct_mem_ptr(std::size_t id, + void invalidate_direct_mem_ptr(usize id, sc_dt::uint64 start, sc_dt::uint64 end) { for (const auto& map : m_mappings) { @@ -238,11 +110,11 @@ class lt_bus : public sc_core::sc_module { // -- TLM_FW_TRANSPORT_IF ---------------------------------------------------- - void b_transport(std::size_t id, + void b_transport(usize id, tlm::tlm_generic_payload& tx, sc_core::sc_time& t) { - std::uint64_t start = tx.get_address(); - std::uint64_t end = start + tx.get_data_length() - 1; + u64 start = tx.get_address(); + u64 end = start + tx.get_data_length() - 1; if (auto res = decode(range{start, end})) { assert(res.base <= start); @@ -255,11 +127,11 @@ class lt_bus : public sc_core::sc_module { } } - bool get_direct_mem_ptr(std::size_t, + bool get_direct_mem_ptr(usize, tlm::tlm_generic_payload& tx, tlm::tlm_dmi& dmi) { - std::uint64_t start = tx.get_address(); - std::uint64_t end = start + tx.get_data_length() - 1; + u64 start = tx.get_address(); + u64 end = start + tx.get_data_length() - 1; bool ret = false; if (auto res = decode(range{start, end})) { @@ -272,9 +144,9 @@ class lt_bus : public sc_core::sc_module { return ret; } - unsigned int transport_dbg(std::size_t, tlm::tlm_generic_payload& tx) { - std::uint64_t start = tx.get_address(); - std::uint64_t end = start + tx.get_data_length() - 1; + unsigned int transport_dbg(usize, tlm::tlm_generic_payload& tx) { + u64 start = tx.get_address(); + u64 end = start + tx.get_data_length() - 1; unsigned int ret = 0; if (auto res = decode(range{start, end})) { @@ -291,7 +163,7 @@ class lt_bus : public sc_core::sc_module { struct decode_result { initiator_socket* sock{nullptr}; - std::uint64_t base; + u64 base; constexpr explicit operator bool() const { return sock != nullptr; @@ -315,19 +187,13 @@ class lt_bus : public sc_core::sc_module { [](const mapping& lhs, const mapping& rhs) { return lhs.addr.start < rhs.addr.start; }); - - // Dump memory map. - // for (const auto& map : m_mappings) { - // std::printf("%08lx - %08lx :[%2ld] %s\n", map.addr.start, map.addr.end, - // map.idx, m_targets[map.idx].get()->name()); - // } } // -- LOCAL CLASSES ---------------------------------------------------------- struct mapping { range addr; - std::size_t idx; + usize idx; }; // -- MEMBER ----------------------------------------------------------------- diff --git a/src/sc_export.cc b/src/sc_export.cc index 7acca58..e3d475b 100644 --- a/src/sc_export.cc +++ b/src/sc_export.cc @@ -1,5 +1,5 @@ #include -#include "utils.h" +#include "utils/log.h" using sc_core::sc_export; using sc_core::sc_in; diff --git a/src/sc_export2.cc b/src/sc_export2.cc index c5905bc..42381d2 100644 --- a/src/sc_export2.cc +++ b/src/sc_export2.cc @@ -1,5 +1,5 @@ #include -#include "utils.h" +#include "utils/log.h" // This file gives an example on how to define an own interface and use that // interface to communicate between two sc_modules. diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index 1373b90..0000000 --- a/src/utils.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H - -#include - -#define LOG(FMT, ...) \ - do { \ - std::fprintf(stderr, "%10s | %4lld:%2lld | %12s | " FMT "\n", \ - sc_core::sc_time_stamp().to_string().c_str(), \ - sc_core::sc_delta_count(), \ - sc_core::sc_delta_count_at_current_time(), __FUNCTION__, \ - ##__VA_ARGS__); \ - } while (0) - -#define LOGM(FMT, ...) LOG("%12s | " FMT, name(), ##__VA_ARGS__) - -// -- COLOR LOGS --------------------------------------------------------------- - -#define RESET "\e[0m" -#define BLACK "\e[0;30m" -#define RED "\e[0;31m" -#define GREEN "\e[0;32m" -#define YELLOW "\e[0;33m" -#define BLUE "\e[0;34m" -#define MAGENTA "\e[0;35m" -#define CYAN "\e[0;36m" -#define WHITE "\e[0;37m" - -#define CLOG(COL, FMT, ...) LOG(COL FMT "\e[0m", ##__VA_ARGS__) -#define CLOGM(COL, FMT, ...) LOGM(COL FMT "\e[0m", ##__VA_ARGS__) - -#endif diff --git a/src/utils/log.h b/src/utils/log.h new file mode 100644 index 0000000..49b7ca8 --- /dev/null +++ b/src/utils/log.h @@ -0,0 +1,32 @@ +#ifndef SYSC_PLAYGROUND_LOG +#define SYSC_PLAYGROUND_LOG + +#include + +#define LOG(FMT, ...) \ + do { \ + std::fprintf(stderr, "%10s | %4lld:%2lld | %16s | " FMT "\n", \ + sc_core::sc_time_stamp().to_string().c_str(), \ + sc_core::sc_delta_count(), \ + sc_core::sc_delta_count_at_current_time(), __FUNCTION__, \ + ##__VA_ARGS__); \ + } while (0) + +#define LOGM(FMT, ...) LOG("%12s | " FMT, name(), ##__VA_ARGS__) + +// -- COLOR LOGS --------------------------------------------------------------- + +#define RESET "\e[0m" +#define BLACK "\e[0;30m" +#define RED "\e[0;31m" +#define GREEN "\e[0;32m" +#define YELLOW "\e[0;33m" +#define BLUE "\e[0;34m" +#define MAGENTA "\e[0;35m" +#define CYAN "\e[0;36m" +#define WHITE "\e[0;37m" + +#define CLOG(COL, FMT, ...) LOG(COL FMT "\e[0m", ##__VA_ARGS__) +#define CLOGM(COL, FMT, ...) LOGM(COL FMT "\e[0m", ##__VA_ARGS__) + +#endif diff --git a/src/utils/range.h b/src/utils/range.h new file mode 100644 index 0000000..ce25298 --- /dev/null +++ b/src/utils/range.h @@ -0,0 +1,25 @@ +#ifndef SYSC_PLAYGROUND_RANGE +#define SYSC_PLAYGROUND_RANGE + +#include "utils/types.h" + +#include + +struct range { + constexpr explicit range(u64 start, u64 end) : start{start}, end{end} { + assert(start < end); + } + + constexpr bool overlaps(range rhs) const { + return start <= rhs.end && rhs.start <= end; + } + + constexpr bool contains(range rhs) const { + return start <= rhs.start && rhs.end <= end; + } + + u64 start; + u64 end; +}; + +#endif diff --git a/src/utils/sysc.h b/src/utils/sysc.h new file mode 100644 index 0000000..02ad412 --- /dev/null +++ b/src/utils/sysc.h @@ -0,0 +1,24 @@ +#ifndef SYSC_PLAYGROUND_SYSC +#define SYSC_PLAYGROUND_SYSC + +#include +#include + +struct scoped_push_hierarchy { + [[nodiscard]] explicit scoped_push_hierarchy(sc_core::sc_module& mod) + : m_mod(mod), m_simctx(sc_core::sc_get_curr_simcontext()) { + assert(m_simctx); + m_simctx->hierarchy_push(&m_mod); + } + + ~scoped_push_hierarchy() { + const auto* top = m_simctx->hierarchy_pop(); + assert(top == &m_mod); + } + + private: + sc_core::sc_simcontext* m_simctx{nullptr}; + sc_core::sc_module& m_mod; +}; + +#endif diff --git a/src/utils/tlm_initiator_socket_tagged.h b/src/utils/tlm_initiator_socket_tagged.h new file mode 100644 index 0000000..f85ed91 --- /dev/null +++ b/src/utils/tlm_initiator_socket_tagged.h @@ -0,0 +1,48 @@ +#ifndef SYSC_PLAYGROUND_TAGGED_INITIATOR +#define SYSC_PLAYGROUND_TAGGED_INITIATOR + +#include + +template +class tlm_initiator_socket_tagged : public tlm::tlm_initiator_socket<>, + public tlm::tlm_bw_transport_if<> { + using cb_invalidate_direct_mem_ptr = void (Module::*)(std::size_t, + sc_dt::uint64, + sc_dt::uint64); + + public: + explicit tlm_initiator_socket_tagged(const char* name, + std::size_t id, + Module* mod, + cb_invalidate_direct_mem_ptr i) + : tlm_initiator_socket<>{name}, + m_mod{mod}, + m_id{id}, + m_invalidate_direct_mem_ptr{i} { + bind(*static_cast*>(this)); + } + + private: + // -- TLM_BW_TRANSPORT_IF ---------------------------------------------------- + + virtual void invalidate_direct_mem_ptr(sc_dt::uint64 start, + sc_dt::uint64 end) override { + (m_mod->*m_invalidate_direct_mem_ptr)(m_id, start, end); + } + + virtual tlm::tlm_sync_enum nb_transport_bw(tlm::tlm_generic_payload&, + tlm::tlm_phase&, + sc_core::sc_time&) override { + std::fprintf( + stderr, "tlm_initiator_socket_tagged: nb_transport_bw not supported\n"); + std::abort(); + } + + // -- MEMBER ----------------------------------------------------------------- + + std::size_t m_id{0}; + Module* m_mod{nullptr}; + cb_invalidate_direct_mem_ptr m_invalidate_direct_mem_ptr{nullptr}; +}; + +#endif diff --git a/src/utils/tlm_target_socket_tagged.h b/src/utils/tlm_target_socket_tagged.h new file mode 100644 index 0000000..99ed547 --- /dev/null +++ b/src/utils/tlm_target_socket_tagged.h @@ -0,0 +1,69 @@ +#ifndef SYSC_PLAYGROUND_TAGGED_TARGET +#define SYSC_PLAYGROUND_TAGGED_TARGET + +#include + +template +class tlm_target_socket_tagged : public tlm::tlm_target_socket<>, + public tlm::tlm_fw_transport_if<> { + using cb_b_transport = void (Module::*)(std::size_t, + tlm::tlm_generic_payload&, + sc_core::sc_time&); + + using cb_get_direct_mem_ptr = bool (Module::*)(std::size_t, + tlm::tlm_generic_payload&, + tlm::tlm_dmi&); + using cb_transport_dbg = unsigned int (Module::*)(std::size_t, + tlm::tlm_generic_payload&); + + public: + explicit tlm_target_socket_tagged(const char* name, + std::size_t id, + Module* mod, + cb_b_transport b, + cb_get_direct_mem_ptr m, + cb_transport_dbg d) + : tlm_target_socket<>{name}, + m_mod{mod}, + m_id{id}, + m_b_transport{b}, + m_get_direct_mem_ptr{m}, + m_transport_dbg{d} { + bind(*static_cast*>(this)); + } + + private: + // -- TLM_FW_TRANSPORT_IF ---------------------------------------------------- + + virtual void b_transport(tlm::tlm_generic_payload& tx, + sc_core::sc_time& t) override { + return (m_mod->*m_b_transport)(m_id, tx, t); + } + + virtual bool get_direct_mem_ptr(tlm::tlm_generic_payload& tx, + tlm::tlm_dmi& dmi_data) override { + return (m_mod->*m_get_direct_mem_ptr)(m_id, tx, dmi_data); + } + + virtual unsigned int transport_dbg(tlm::tlm_generic_payload& tx) override { + return (m_mod->*m_transport_dbg)(m_id, tx); + } + + virtual tlm::tlm_sync_enum nb_transport_fw(tlm::tlm_generic_payload& tx, + tlm::tlm_phase& phase, + sc_core::sc_time& t) override { + std::fprintf(stderr, + "tlm_target_socket_tagged: nb_transport_fw not supported\n"); + std::abort(); + } + + // -- MEMBER ----------------------------------------------------------------- + + std::size_t m_id{0}; + Module* m_mod{nullptr}; + cb_b_transport m_b_transport{nullptr}; + cb_get_direct_mem_ptr m_get_direct_mem_ptr{nullptr}; + cb_transport_dbg m_transport_dbg{nullptr}; +}; + +#endif diff --git a/src/utils/types.h b/src/utils/types.h new file mode 100644 index 0000000..a13b8ef --- /dev/null +++ b/src/utils/types.h @@ -0,0 +1,11 @@ +#ifndef SYSC_PLAYGROUND_TYPES +#define SYSC_PLAYGROUND_TYPES + +#include + +using u32 = std::uint32_t; +using u64 = std::uint64_t; + +using usize = std::size_t; + +#endif -- cgit v1.2.3