<commit_before>#include <gdb.hpp>
#include <array>
#include <cassert>

namespace narcissus {

    //XXX
    namespace h8_3069f {

        gdb_server::gdb_server(
                std::array<std::uint8_t, (std::uint32_t)h8_3069f::mem_info::rom_size>&& memory,
                boost::asio::io_service& io_service,
                std::uint16_t port)
            : cpu_(std::make_shared<h8_3069f::cpu>(std::move(memory))), 
            acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
            socket_(io_service)
        {}

        auto gdb_server::run() -> void
        {
            cpu_->before_run();
            acceptor_.accept(socket_);
            try
            {
                while (true) 
                {
                    std::array<char, 1024> data{0};

                    boost::system::error_code error;
                    size_t length = socket_.read_some(boost::asio::buffer(data), error);
                    if(error == boost::asio::error::eof)
                    {
                        std::cout << "eof" << std::endl;
                        break;
                    }
                    else if(error)
                    {
                        throw boost::system::system_error(error);
                    }
                    std::cout << "receive: " << std::string(data.data()) << std::endl;

                    auto r = work(data, length);
                    if(!r.empty())
                    {
                        std::cout << "send:    " << r.c_str() << std::endl;
                        boost::asio::write(socket_, boost::asio::buffer(r.c_str(), r.length()));
                    }
                }
            }
            catch(std::exception& e)
            {
                std::cout << e.what() << std::endl;
            }
        }

        auto gdb_server::work(std::array<char, 1024>& data, size_t length) -> std::string
        {
            auto i = 0;
            for ( i = 0; i < length; i++) {
                if(data[i] == '$')
                {
                    break;
                }
            }

            if(i >= length)
            {
                return "";
            }

            std::stringstream stream;
            auto ack = [this]{
                boost::asio::write(socket_, boost::asio::buffer("+", 1));
            };

            switch (data[++i]) {
                // read register
                case '?':
                    assert(false);
                    break;

                    // return cpu register
                case 'g':
                    assert(false);
                    break;

                    // single step
                case 's':
                    {
                        auto pc = cpu_->cycle();
                        return "+";
                    }

                case 'q':
                    {

                        switch (data[++i]) {
                            case 'S':
                                {
                                    //$qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+#c9
                                    ack();
//                                     std::string stubfeature("multiprocess-;swbreak-;hwbreak-;qRelocInsn-");
                                    std::string stubfeature("multiprocess-");

                                    stream << "$" << stubfeature << "#" 
                                        << std::hex << std::setfill('0') << std::setw(2) 
                                        << (check_sum(stubfeature.c_str()) % 0x100);
                                    return stream.str();
                                }
                            case 'T':
                                {
                                    switch (data[++i]) {
                                        case 'S':
                                            {
                                                //$qTStatus#49
                                                ack();

                                                std::string stubfeature("T0;tnotrun:0");
                                                stream << "$" << stubfeature << "#" 
                                                    << std::hex << std::setfill('0') << std::setw(2) 
                                                    << (check_sum(stubfeature.c_str()) % 0x100);
                                                return stream.str();
                                            }

                                        case 'f':
                                            {
                                                //TODO $qTfV#81
                                                ack();
//                                                 std::string qtdv("QTDV:0000000000000000:0000000000000000:1:74657374DA");
                                                std::string qtdv("1:0000000000000000:1:61");
                                                stream << "$" << qtdv << "#"
                                                    << std::hex << std::setfill('0') << std::setw(2)
                                                    << (check_sum(qtdv.c_str()) % 0x100);
                                                return stream.str();<commit_msg>$qTsV#8e<commit_after>#include <gdb.hpp>
#include <array>
#include <cassert>

namespace narcissus {

    //XXX
    namespace h8_3069f {

        gdb_server::gdb_server(
                std::array<std::uint8_t, (std::uint32_t)h8_3069f::mem_info::rom_size>&& memory,
                boost::asio::io_service& io_service,
                std::uint16_t port)
            : cpu_(std::make_shared<h8_3069f::cpu>(std::move(memory))), 
            acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
            socket_(io_service)
        {}

        auto gdb_server::run() -> void
        {
            cpu_->before_run();
            acceptor_.accept(socket_);
            try
            {
                while (true) 
                {
                    std::array<char, 1024> data{0};

                    boost::system::error_code error;
                    size_t length = socket_.read_some(boost::asio::buffer(data), error);
                    if(error == boost::asio::error::eof)
                    {
                        std::cout << "eof" << std::endl;
                        break;
                    }
                    else if(error)
                    {
                        throw boost::system::system_error(error);
                    }
                    std::cout << "receive: " << std::string(data.data()) << std::endl;

                    auto r = work(data, length);
                    if(!r.empty())
                    {
                        std::cout << "send:    " << r.c_str() << std::endl;
                        boost::asio::write(socket_, boost::asio::buffer(r.c_str(), r.length()));
                    }
                }
            }
            catch(std::exception& e)
            {
                std::cout << e.what() << std::endl;
            }
        }

        auto gdb_server::work(std::array<char, 1024>& data, size_t length) -> std::string
        {
            auto i = 0;
            for ( i = 0; i < length; i++) {
                if(data[i] == '$')
                {
                    break;
                }
            }

            if(i >= length)
            {
                return "";
            }

            std::stringstream stream;
            auto ack = [this]{
                boost::asio::write(socket_, boost::asio::buffer("+", 1));
            };

            switch (data[++i]) {
                // read register
                case '?':
                    assert(false);
                    break;

                    // return cpu register
                case 'g':
                    assert(false);
                    break;

                    // single step
                case 's':
                    {
                        auto pc = cpu_->cycle();
                        return "+";
                    }

                case 'q':
                    {

                        switch (data[++i]) {
                            case 'S':
                                {
                                    //$qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+#c9
                                    ack();
//                                     std::string stubfeature("multiprocess-;swbreak-;hwbreak-;qRelocInsn-");
                                    std::string stubfeature("multiprocess-");

                                    stream << "$" << stubfeature << "#" 
                                        << std::hex << std::setfill('0') << std::setw(2) 
                                        << (check_sum(stubfeature.c_str()) % 0x100);
                                    return stream.str();
                                }
                            case 'T':
                                {
                                    switch (data[++i]) {
                                        case 'S':
                                            {
                                                //$qTStatus#49
                                                ack();

                                                std::string stubfeature("T0;tnotrun:0");
                                                stream << "$" << stubfeature << "#" 
                                                    << std::hex << std::setfill('0') << std::setw(2) 
                                                    << (check_sum(stubfeature.c_str()) % 0x100);
                                                return stream.str();
                                            }

                                        case 'f':
                                            {
                                                //TODO $qTfV#81
                                                ack();
                                                std::string qtfv("1:0000000000000000:1:61");
                                                stream << "$" << qtfv << "#"
                                                    << std::hex << std::setfill('0') << std::setw(2)
                                                    << (check_sum(qtfv.c_str()) % 0x100);
                                                return stream.str();
                                            }

                                        case 's':
                                            {
                                                //$qTsV#8e
                                                ack();
//                                                 std::string qtsv("1:0000000000000000:1:61");
                                                std::string qtsv("l");

                                                stream << "$" << qtsv << "#"
                                                    << std::hex << std::setfill('0') << std::setw(2)
                                                    << (check_sum(qtsv.c_str()) % 0x100);
                                                return stream.str();
{'fraction_non_alphanumeric': 0.12010699955416852, 'fraction_numerical': 0.020062416406598307, 'mean_word_length': 0.7019726858877087, 'pattern_counts': {'":': 0, '<': 157, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 122, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '457098', 'n_tokens_mistral': 2679, 'n_tokens_neox': 2341, 'n_words': 376}