|
| 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 |
0 commit comments