Skip to content

Commit e5bd32a

Browse files
fa-jimenezErickOF
authored andcommitted
Add ethernetDecoder
1 parent 47f471f commit e5bd32a

File tree

8 files changed

+252
-9
lines changed

8 files changed

+252
-9
lines changed

modules/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
SYSTEMC?=$(SYSTEMC_HOME)
22
TARGET?=test
3+
SYSTEMC_AMS?=$(SYSTEMC_AMS_HOME)
34

45
# Compiler
56
CC=g++
@@ -18,8 +19,8 @@ LIBS=-lsystemc -lm
1819
SRCDIR=src
1920
OBJDIR=obj
2021
BINDIR=./
21-
INCDIR=-I. -I./include -I$(SYSTEMC)/include -Ibasic_protocol -I$(SYSTEMC)/include/tlm_core/tlm_2 -I/usr/local/sytemc-3.0.0/include/
22-
LIBDIR=-L. -L$(SYSTEMC)/lib-linux64 -L/usr/local/sytemc-3.0.0/lib-linux64/
22+
INCDIR=-I. -I./include -I$(SYSTEMC)/include -Ibasic_protocol -I$(SYSTEMC)/include/tlm_core/tlm_2 -I$(SYSTEMC_AMS)/include/
23+
LIBDIR=-L. -L$(SYSTEMC)/lib-linux64 -L$(SYSTEMC_AMS)/lib-linux64/
2324

2425
ifdef USING_TLM_TB_EN
2526
EDGE_DIR=../edge-detector

modules/ethernetAMS/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ ifdef RGB2GRAY_PV_EN
2727
LFLAGS += -DRGB2GRAY_PV_EN
2828
endif # RGB2GRAY_PV_EN
2929

30+
# all: encoder_testbench decoder_testbench
31+
32+
# encoder_testbench: tb_ethernet_encoder.o ethernetEncoder.o InputSignalGenerator.o
33+
# $(CXX) -o encoder_testbench tb_ethernet_encoder.o ethernetEncoder.o InputSignalGenerator.o $(CXXFLAGS) $(LDFLAGS)
34+
35+
# decoder_testbench: tb_ethernet_decoder.o ethernetEncoder.o ethernetDecoder.o InputSignalGenerator.o
36+
# $(CXX) -o decoder_testbench tb_ethernet_decoder.o ethernetEncoder.o ethernetDecoder.o InputSignalGenerator.o $(CXXFLAGS) $(LDFLAGS)
37+
38+
# tb_ethernet_encoder.o: $(SRC_DIR)/tb_ethernet_encoder.cpp
39+
# $(CXX) -c $(SRC_DIR)/tb_ethernet_encoder.cpp $(CXXFLAGS) -o tb_ethernet_encoder.o
40+
41+
# tb_ethernet_decoder.o: $(SRC_DIR)/tb_ethernet_decoder.cpp
42+
# $(CXX) -c $(SRC_DIR)/tb_ethernet_decoder.cpp $(CXXFLAGS) -o tb_ethernet_decoder.o
43+
44+
# ethernetEncoder.o: $(SRC_DIR)/ethernetEncoder.cpp
45+
# $(CXX) -c $(SRC_DIR)/ethernetEncoder.cpp $(CXXFLAGS) -o ethernetEncoder.o
46+
47+
# ethernetDecoder.o: $(SRC_DIR)/ethernetDecoder.cpp
48+
# $(CXX) -c $(SRC_DIR)/ethernetDecoder.cpp $(CXXFLAGS) -o ethernetDecoder.o
49+
50+
# InputSignalGenerator.o: $(SRC_DIR)/InputSignalGenerator.cpp
51+
# $(CXX) -c $(SRC_DIR)/InputSignalGenerator.cpp $(CXXFLAGS) -o InputSignalGenerator.o
52+
53+
# clean:
54+
# rm -f *.o encoder_testbench decoder_testbench
55+
3056
# Run the compiled file
3157
run:
3258
@./test
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef ETHERNET_DECODER_H
2+
#define ETHERNET_DECODER_H
3+
4+
#include <systemc-ams.h>
5+
#include <map>
6+
#include <systemc.h>
7+
#include <deque>
8+
9+
SCA_TDF_MODULE(ethernetDecoder)
10+
{
11+
public:
12+
sca_tdf::sca_in<double> mlt3_in; // MLT-3 input signal
13+
sca_tdf::sca_out<sc_dt::sc_bv<4>> data_out; // 4-bit output
14+
15+
std::map<std::string, std::string> decoding_map;
16+
17+
void set_attributes();
18+
void initialize();
19+
void processing();
20+
21+
SCA_CTOR(ethernetDecoder)
22+
: mlt3_in("mlt3_in"), data_out("data_out"), previous_level(0), bit_count(0), sample_count(0), found_sequence(false),
23+
received_first_eight_decodes(false), data_length(0), decode_count(0)
24+
{
25+
}
26+
27+
private:
28+
int previous_level;
29+
int current_level;
30+
int bit_count;
31+
int sample_count;
32+
std::deque<char> bit_sequence;
33+
bool found_sequence;
34+
const std::string target_sequence = "10110";
35+
const std::string end_sequence = "10111";
36+
37+
bool check_sequence(const std::deque<char>& sequence);
38+
39+
// New members
40+
bool received_first_eight_decodes;
41+
int data_length;
42+
sc_dt::sc_bv<4> first_eight_decodes[8];
43+
int decode_count;
44+
};
45+
46+
#endif // ETHERNET_DECODER_H

modules/ethernetAMS/include/ethernetEncoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ SCA_TDF_MODULE(ethernetEncoder)
1515
sc_dt::sc_bv<5> code_out; // Internal signal for 5-bit code
1616
int currentLevel = 0;
1717
int nextLevel = 1;
18-
int bitCount = 4;
18+
int bitCount = 0;
1919
int sampleCount = 0;
2020
int lastMlt3Out = 0;
2121
std::string lastCodeOut = "";
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#ifndef ETHERNET_DECODER_CPP
2+
#define ETHERNET_DECODER_CPP
3+
4+
#include "ethernetDecoder.h"
5+
6+
void ethernetDecoder::set_attributes()
7+
{
8+
set_timestep(10, sc_core::SC_NS); // Set a sample period of 10 ns
9+
}
10+
11+
void ethernetDecoder::initialize()
12+
{
13+
previous_level = 0;
14+
current_level = 0;
15+
bit_count = 0;
16+
sample_count = 0;
17+
found_sequence = false;
18+
bit_sequence.clear();
19+
20+
// Reset new members
21+
received_first_eight_decodes = false;
22+
data_length = 0;
23+
decode_count = 0;
24+
25+
// Initialize 5B/4B decoding map
26+
decoding_map = {
27+
{"11110", "0000"}, {"01001", "0001"}, {"10100", "0010"},
28+
{"10101", "0011"}, {"01010", "0100"}, {"01011", "0101"},
29+
{"01110", "0110"}, {"01111", "0111"}, {"10010", "1000"},
30+
{"10011", "1001"}, {"10110", "1010"}, {"10111", "1011"},
31+
{"11010", "1100"}, {"11011", "1101"}, {"11100", "1110"},
32+
{"11101", "1111"}
33+
};
34+
}
35+
36+
bool ethernetDecoder::check_sequence(const std::deque<char>& sequence)
37+
{
38+
int target_count = 0;
39+
std::string bit_str(sequence.begin(), sequence.end());
40+
41+
for (size_t i = 0; i <= bit_str.size() - 5; ++i)
42+
{
43+
std::string current_bits = bit_str.substr(i, 5);
44+
45+
if (current_bits == target_sequence)
46+
{
47+
target_count++;
48+
}
49+
else if (target_count == 15 && current_bits == end_sequence)
50+
{
51+
return true;
52+
}
53+
}
54+
55+
return false;
56+
}
57+
58+
void ethernetDecoder::processing()
59+
{
60+
current_level = static_cast<int>(mlt3_in.read());
61+
62+
if (found_sequence && bit_sequence.size() >= 5)
63+
{
64+
bit_sequence.clear();
65+
}
66+
67+
// MLT-3 to binary conversion based on level change
68+
if (current_level != previous_level)
69+
{
70+
bit_sequence.push_back('1');
71+
}
72+
else
73+
{
74+
bit_sequence.push_back('0');
75+
}
76+
77+
if (bit_sequence.size() > 80)
78+
{
79+
bit_sequence.pop_front();
80+
}
81+
82+
previous_level = current_level;
83+
bit_count++;
84+
85+
if (found_sequence && bit_sequence.size() >= 5)
86+
{
87+
std::string last_five_bits(bit_sequence.end() - 5, bit_sequence.end());
88+
89+
// 5B/4B decoding
90+
if (decoding_map.find(last_five_bits) != decoding_map.end())
91+
{
92+
std::string decoded_bits = decoding_map[last_five_bits];
93+
sc_dt::sc_bv<4> decoded_bv;
94+
decoded_bv = decoded_bits.c_str(); // Assign the value using operator=
95+
96+
// Store the first eight decodes
97+
if (!received_first_eight_decodes)
98+
{
99+
first_eight_decodes[decode_count] = decoded_bv;
100+
decode_count++;
101+
if (decode_count == 8)
102+
{
103+
data_length = (first_eight_decodes[7].to_uint() << 28) |
104+
(first_eight_decodes[6].to_uint() << 24) |
105+
(first_eight_decodes[5].to_uint() << 20) |
106+
(first_eight_decodes[4].to_uint() << 16) |
107+
(first_eight_decodes[3].to_uint() << 12) |
108+
(first_eight_decodes[2].to_uint() << 8) |
109+
(first_eight_decodes[1].to_uint() << 4) |
110+
first_eight_decodes[0].to_uint();
111+
received_first_eight_decodes = true;
112+
std::cout << "/////////////////////////////////Data length: " << std::hex << data_length << std::endl;
113+
114+
decode_count = 0;
115+
}
116+
}
117+
else
118+
{
119+
data_out.write(decoded_bv);
120+
decode_count++;
121+
if (decode_count >= data_length * 2)
122+
{
123+
// Reset state to wait for the sequence again
124+
found_sequence = false;
125+
received_first_eight_decodes = false;
126+
decode_count = 0;
127+
data_length = 0;
128+
}
129+
}
130+
bit_sequence.erase(bit_sequence.begin(), bit_sequence.begin() + 5);
131+
}
132+
else
133+
{
134+
std::cout << "last_five_bits:" << last_five_bits;
135+
136+
std::cout << "********Sample: " << sample_count << ", mlt3_in: " << current_level
137+
<< ", bit_sequence: " << std::string(bit_sequence.begin(), bit_sequence.end())
138+
<< ", found_sequence: " << found_sequence << std::endl;
139+
SC_REPORT_ERROR("ethernetDecoder", "Invalid input for decoding");
140+
}
141+
}
142+
143+
if (bit_sequence.size() == 80 && !found_sequence)
144+
{
145+
found_sequence = check_sequence(bit_sequence);
146+
}
147+
148+
// Debugging output
149+
std::cout << "Sample: " << sample_count << ", mlt3_in: " << current_level
150+
<< ", bit_sequence: " << std::string(bit_sequence.begin(), bit_sequence.end())
151+
<< ", found_sequence: " << found_sequence << std::endl;
152+
153+
sample_count++;
154+
// Set the next trigger
155+
next_trigger(10, SC_NS);
156+
}
157+
158+
#endif // ETHERNET_DECODER_CPP

modules/ethernetAMS/src/ethernetEncoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void ethernetEncoder::processing()
3434

3535
std::string input = data_in.read().to_string();
3636

37-
if (bitCount == 4) // Process new input only when bitCount is 4 (rightmost bit)
37+
if (bitCount == 0) // Process new input only when bitCount is 4 (rightmost bit)
3838
{
3939
std::cout << "Processing sample: " << sampleCount << ", received input: " << input << std::endl;
4040

@@ -66,7 +66,7 @@ void ethernetEncoder::processing()
6666

6767
lastMlt3Out = currentLevel;
6868
next_mlt3_out = currentLevel; // Store the computed value for the next cycle
69-
bitCount = (bitCount == 0) ? 4 : bitCount - 1; // Decrement bitCount from 4 to 0
69+
bitCount = (bitCount == 4) ? 0 : bitCount + 1; // Decrement bitCount from 4 to 0
7070

7171
// Debugging output
7272
std::cout << "Sample: " << sampleCount << ", data_in: " << input

modules/ethernetAMS/src/tb_ethernet_encoder.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
#include <systemc.h>
66
#include "ethernetEncoder.h"
77
#include "packetGenerator.h"
8+
#include "ethernetDecoder.h"
89

9-
int sc_main()
10+
int sc_main(int argc, char* argv[])
1011
{
1112
sca_tdf::sca_signal<bool> valid;
1213
sca_tdf::sca_signal<double> mlt3_out_signal;
1314

1415
sca_tdf::sca_signal<bool> data_out_valid;
16+
sca_tdf::sca_signal<sc_dt::sc_bv<4>> data_out_signal;
1517
sca_tdf::sca_signal<sc_dt::sc_bv<4>> data_out;
1618

1719
sca_tdf::sca_signal<bool> tmp_data_out_valid;
@@ -40,10 +42,13 @@ int sc_main()
4042
unsigned char data[16];
4143

4244
ethernetEncoder encoder("encoder", sample_time);
45+
ethernetDecoder decoder("decoder");
4346
packetGenerator pkt_gen("pkt_gen", sample_time);
4447

4548
encoder.data_in(data_out);
4649
encoder.mlt3_out(mlt3_out_signal);
50+
decoder.mlt3_in(mlt3_out_signal);
51+
decoder.data_out(data_out_signal);
4752
encoder.valid(data_out_valid);
4853

4954
pkt_gen.data_out_valid(data_out_valid);
@@ -77,6 +82,7 @@ int sc_main()
7782
if (traceFile)
7883
{
7984
sca_util::sca_trace(traceFile, mlt3_out_signal, "mlt3_out");
85+
sca_util::sca_trace(traceFile, data_out_signal, "data_out_signal");
8086
sca_util::sca_trace(traceFile, data_out_valid, "data_out_valid");
8187
sca_util::sca_trace(traceFile, data_out, "data_out");
8288
sca_util::sca_trace(traceFile, data_in, "pkt_gen_data_in");
@@ -117,6 +123,10 @@ int sc_main()
117123

118124
sc_start(3455, SC_NS); // Run the simulation for 500 ns
119125

126+
pkt_gen.fill_data(data, 8);
127+
128+
sc_start(3500, SC_NS); // Run the simulation for 500 ns
129+
120130
if (traceFile)
121131
{
122132
sca_util::sca_close_vcd_trace_file(traceFile);

modules/rgb2gray/utils.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/bin/sh
2-
export SYSTEMC=/usr/local/systemc-3.0.0
2+
export SYSTEMC_HOME=/usr/local/systemc-3.0.0
3+
export SYSTEMC_AMS_HOME=/usr/local/sytemc-3.0.0
34
export OPENCV_SO_DIR=/lib/x86_64-linux-gnu
45
export OPENCV_H_DIR=/usr/include/opencv4
5-
export LD_LIBRARY_PATH=$SYSTEMC/lib-linux64:$OPENCV_SO_DIR
6+
export LD_LIBRARY_PATH=$SYSTEMC_HOME/lib-linux64:$OPENCV_SO_DIR:$SYSTEMC_AMS_HOME/lib-linux64
67
export USER_DEF_SYSTEMC_DIR=1
7-
export INCLUDE_OPENCV=1
8+
# export INCLUDE_OPENCV=1
9+
export USE_CPP17=1

0 commit comments

Comments
 (0)