scc 2026.07
SystemC components library
eth_tlm.h
1/*******************************************************************************
2 * Copyright 2024 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17#ifndef _ETH_ETH_TLM_H_
18#define _ETH_ETH_TLM_H_
19
20#include <atomic>
21#include <cci_configuration>
22#include <cstdint>
23#include <mutex>
24#include <nonstd/span.h>
25#include <scc/fifo_w_cb.h>
26#include <scc/peq.h>
27#include <scc/report.h>
28#include <scc/sc_variable.h>
29#include <tlm/nw/tlm_network_gp.h>
31#include <tlm/scc/tlm_gp_shared.h>
32#include <tlm/scc/tlm_mm.h>
33
35namespace eth {
36
37inline std::uint16_t bswap16(std::uint16_t x) { return static_cast<std::uint16_t>((x >> 8) | (x << 8)); }
38
39#pragma pack(push, 1)
41 std::array<std::uint8_t, 6> dst;
42 std::array<std::uint8_t, 6> src;
43 std::uint16_t type_or_len; // big-endian on the wire (e.g., 0x0800 IPv4, 0x86DD IPv6)
44};
45
47 std::uint16_t tpid; // 0x8100 (big-endian)
48 std::uint16_t tci; // PCP(3) | DEI(1) | VID(12) (big-endian)
49};
50#pragma pack(pop)
51
52static_assert(sizeof(ethernet_header) == 14);
53static_assert(sizeof(vlan_tag_8021Q) == 4);
54
55enum class ether_kind { ETHER_TYPE, LENGTH_8023 };
56
58 ethernet_header l2; // original 14-byte header
59 bool has_vlan = false;
60 std::uint16_t vlan_tpid = 0; // host order (e.g., 0x8100)
61 std::uint16_t vlan_id = 0; // 0..4095
62 std::uint16_t vlan_pcp = 0; // 0..7
63 bool vlan_dei = false;
64
65 ether_kind kind = ether_kind::ETHER_TYPE;
66 std::uint16_t etherType_or_len = 0; // host order: EtherType or 802.3 length
67 nonstd::span<const std::uint8_t> payload;
68 static ethernet_frame parse_ethernet(nonstd::span<const std::uint8_t> frame);
69};
70
72//
74enum class ETH { FRAME };
75struct eth_packet_payload : public tlm::nw::tlm_network_payload<ETH> {
76 eth_packet_payload() { m_data.resize(1536); };
77
78 explicit eth_packet_payload(tlm::nw::tlm_base_mm_interface* mm)
80 m_data.resize(1536);
81 }
82
83 const sc_core::sc_time& get_sender_clk_period() const { return sender_clk_period; }
84
85 void set_sender_clk_period(sc_core::sc_time period) { this->sender_clk_period = period; }
86
87 const uint64_t unique_id{get_id()};
88
89private:
90 sc_core::sc_time sender_clk_period{sc_core::SC_ZERO_TIME};
91
92 static uint64_t get_id() {
93 static std::atomic<uint64_t> id{0};
94 return id.fetch_add(1);
95 }
96};
97
99 using tlm_payload_type = eth_packet_payload;
100 using tlm_phase_type = ::tlm::tlm_phase;
101};
102
103} // namespace eth
104
105namespace tlm {
106namespace scc {
107// provide needed info for SCC memory manager
108template <> struct tlm_mm_traits<eth::eth_packet_types> {
109 using mm_if_type = tlm::nw::tlm_base_mm_interface;
110 using payload_base = tlm::nw::tlm_network_payload_base;
111};
112} // namespace scc
113} // namespace tlm
114
115namespace eth {
116template <int N = 1> using eth_pkt_initiator_socket = tlm::nw::tlm_network_initiator_socket<1, ETH, eth_packet_types, N>;
117template <int N = 1> using eth_pkt_target_socket = tlm::nw::tlm_network_target_socket<1, ETH, eth_packet_types, N>;
120
121struct eth_channel : public sc_core::sc_module,
122 public tlm::nw::tlm_network_fw_transport_if<eth_packet_types>,
123 public tlm::nw::tlm_network_bw_transport_if<eth_packet_types> {
124
125 using transaction_type = eth_packet_types::tlm_payload_type;
126 using phase_type = eth_packet_types::tlm_phase_type;
127
128 eth_pkt_target_socket<> rx{"rx"};
129
130 eth_pkt_initiator_socket<> tx{"tx"};
131
132 cci::cci_param<sc_core::sc_time> channel_delay{"channel_delay", sc_core::SC_ZERO_TIME, "delay of the ETH channel"};
133
134 eth_channel(sc_core::sc_module_name const& nm)
135 : sc_core::sc_module(nm)
136 , tx{"isckt"} {
137 tx(*this);
138 rx(*this);
139 }
140
141 void b_transport(transaction_type& trans, sc_core::sc_time& t) override {
142 t += trans.get_data().size() * 8 * trans.get_sender_clk_period() + channel_delay.get_value();
143 tx->b_transport(trans, t);
144 }
145 // TODO: fix non-blocking channel timing
146 tlm::tlm_sync_enum nb_transport_fw(transaction_type& trans, phase_type& phase, sc_core::sc_time& t) override {
147 SCCTRACE(SCMOD) << "Received non-blocking transaction in fw path with phase " << phase.get_name();
148 if(phase == tlm::nw::REQUEST) {
149 phase_type ph = tlm::nw::INDICATION;
150 auto ret = tx->nb_transport_fw(trans, ph, t);
151 if(ph == tlm::nw::RESPONSE)
152 phase = tlm::nw::CONFIRM;
153 return ret;
154 }
155 return tlm::TLM_ACCEPTED;
156 }
157
158 tlm::tlm_sync_enum nb_transport_bw(transaction_type& trans, phase_type& phase, sc_core::sc_time& t) override {
159 SCCTRACE(SCMOD) << "Received non-blocking transaction in bw path with phase " << phase.get_name();
160 if(phase == tlm::nw::RESPONSE) {
161 phase_type ph = tlm::nw::CONFIRM;
162 return rx->nb_transport_bw(trans, ph, t);
163 }
164 return tlm::TLM_ACCEPTED;
165 }
166
167 unsigned int transport_dbg(transaction_type& trans) override { return tx->transport_dbg(trans); }
168};
169} // namespace eth
170#endif // _ETH_ETH_TLM_H_
ETH TLM utilities.
Definition eth_tlm.cpp:2
SCC TLM utilities.
SystemC TLM.
Definition dmi_mgr.h:19
Definition of the tlm_network_initiator_socket class.
A base class for TLM network payloads.
A class for TLM network payloads with support for extensions and memory management.
std::vector< uint8_t > const & get_data() const
Gets the data from the payload.
Definition of the tlm_network_target_socket class.
a tlm memory manager
Definition tlm_mm.h:330
The SystemC Transaction Level Model (TLM) Network TLM utilities.