<commit_before>// Copyright (c) 2012-2015 <NAME>
// See LICENSE for details

#include <gtk/gtk.h>
#include <glib.h>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <cassert>
#include "CPU.h"

namespace hcc {

struct ROM : public IROM {
    unsigned short *data;
    static const unsigned int size = 0x8000;
    ROM() {
        data = new unsigned short[size];
    }
    virtual ~ROM() {
        delete[] data;
    }

    bool load(const char *filename) {
        std::ifstream input(filename);
        std::string line;
        unsigned int counter = 0;
        while (input.good() && counter < size) {
            getline(input, line);
            if (line.size() == 0)
                continue;
            if (line.size() != 16)
                return false;

            unsigned int instruction = 0;
            for (unsigned int i = 0; i<16; ++i) {
                instruction <<= 1;
                switch (line[i]) {
                case '0':
                    break;
                case '1':
                    instruction |= 1;
                    break;
                default:
                    return false;
                }
            }
            data[counter++] = instruction;
        }
        // clear the rest
        while (counter < size) {
            data[counter++] = 0;
        }

        return true;
    }
    virtual unsigned short get(unsigned int address) const {
        if (address < size) {
            return data[address];
        } else {
            std::cerr << "requested memory at " << address << '\n';
            throw std::runtime_error("Memory::get");
        }
    }
};

} // end namespace

gboolean on_draw(GtkWidget*, cairo_t* cr, gpointer data)
{
    GdkPixbuf* pixbuf = reinterpret_cast<GdkPixbuf*>(data);
    gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
    cairo_paint(cr);
    return FALSE;
}

struct GUIEmulatorRAM : public hcc::IRAM {
    static const unsigned int CHANNELS = 3;
    static const unsigned int SCREEN_WIDTH = 512;
    static const unsigned int SCREEN_HEIGHT = 256;
    static const unsigned int size = 0x6001;

    unsigned short *data;
    GdkPixbuf *pixbuf;
    GtkWidget *screen;

    void putpixel(unsigned short x, unsigned short y, bool black) {
        int color = black ? 0x00 : 0xff;

        int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
        int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
        guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
        guchar* p = pixels + y * rowstride + x * n_channels;
        p[0] = color;
        p[1] = color;
        p[2] = color;
    }
public:
    GUIEmulatorRAM() {
        data = new unsigned short[size];

        pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, SCREEN_WIDTH, SCREEN_HEIGHT);
        gdk_pixbuf_fill(pixbuf, 0x0000000);

        screen = gtk_drawing_area_new();
        gtk_widget_set_size_request(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
        g_signal_connect(screen, "draw", G_CALLBACK(on_draw), pixbuf);
    }
    virtual ~GUIEmulatorRAM() {
        delete[] data;
    }
    void keyboard(unsigned short value) {
        data[0x6000] = value;
    }
    GtkWidget* getScreenWidget() {
        return screen;
    }
    virtual void set(unsigned int address, unsigned short value) {
        if (address >= size) {
            throw std::runtime_error("RAM::set");
        }

        data[address] = value;

        // check if we are writing to video RAM
        if (0x4000 <= address && address <0x6000) {
            address -= 0x4000;

            unsigned short y = address / 32;
            unsigned short x = 16*(address % 32);
            for (int bit = 0; bit<16; ++bit) {
                putpixel(x + bit, y, value & 1);
                value = value >> 1;
            }
            gdk_threads_enter();
            gtk_widget_queue_draw(screen);
            gdk_threads_leave();
        }
    }
    virtual unsigned short get(unsigned int address) const {
        if (address >= size) {
            throw std::runtime_error("RAM::get");
        }

        return data[address];
    }
};

struct emulator {
    emulator();
    void load_clicked();
    void run_clicked();
    void pause_clicked();
    gboolean keyboard_callback(GdkEventKey* event);
    void run_thread();
    GtkToolItem* create_button(const gchar* stock_id, const gchar* text, GCallback callback);

    hcc::ROM rom;
    GUIEmulatorRAM ram;
    hcc::CPU cpu;

    bool running = false;

    GtkWidget* window;
    GtkWidget* load_dialog;
    GtkWidget* error_dialog;
    GtkToolItem* button_load;
    GtkToolItem* button_run;
    GtkToolItem* button_pause;
};

gboolean c_keyboard_callback(GtkWidget*, GdkEventKey *event, gpointer user_data)
{ return reinterpret_cast<emulator*>(user_data)->keyboard_callback(event); }

void c_load_clicked(GtkButton*, gpointer user_data)
{ reinterpret_cast<emulator*>(user_data)->load_clicked(); }

void c_run_clicked(GtkButton*, gpointer user_data)
{ reinterpret_cast<emulator*>(user_data)->run_clicked(); }<commit_msg>Use std::vector for heap allocated contiguous memory<commit_after>// Copyright (c) 2012-2015 <NAME>
// See LICENSE for details

#include <gtk/gtk.h>
#include <glib.h>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <cassert>
#include <vector>
#include "CPU.h"

struct ROM : public hcc::IROM {
    ROM() : data(0x8000, 0) { }

    bool load(const char *filename)
    {
        std::ifstream input(filename);
        std::string line;
        auto it = begin(data);
        while (input.good() && it != end(data)) {
            getline(input, line);
            if (line.size() == 0) {
                continue;
            }
            if (line.size() != 16) {
                return false;
            }

            unsigned int instruction = 0;
            for (unsigned int i = 0; i<16; ++i) {
                instruction <<= 1;
                switch (line[i]) {
                case '0':
                    break;
                case '1':
                    instruction |= 1;
                    break;
                default:
                    return false;
                }
            }
            *it++ = instruction;
        }

        // clear the rest
        while (it != end(data)) {
            *it++ = 0;
        }

        return true;
    }

    unsigned short get(unsigned int address) const override
    { return data.at(address); }

private:
    std::vector<unsigned short> data;
};

gboolean on_draw(GtkWidget*, cairo_t* cr, gpointer data)
{
    GdkPixbuf* pixbuf = reinterpret_cast<GdkPixbuf*>(data);
    gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
    cairo_paint(cr);
    return FALSE;
}

struct RAM : public hcc::IRAM {
    static const unsigned int CHANNELS = 3;
    static const unsigned int SCREEN_WIDTH = 512;
    static const unsigned int SCREEN_HEIGHT = 256;

    std::vector<unsigned short> data;
    GdkPixbuf *pixbuf;
    GtkWidget *screen;

    void putpixel(unsigned short x, unsigned short y, bool black) {
        int color = black ? 0x00 : 0xff;

        int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
        int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
        guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
        guchar* p = pixels + y * rowstride + x * n_channels;
        p[0] = color;
        p[1] = color;
        p[2] = color;
    }
public:
    RAM() : data(0x6001, 0)
    {
        pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, SCREEN_WIDTH, SCREEN_HEIGHT);
        gdk_pixbuf_fill(pixbuf, 0x0000000);

        screen = gtk_drawing_area_new();
        gtk_widget_set_size_request(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
        g_signal_connect(screen, "draw", G_CALLBACK(on_draw), pixbuf);
    }

    void keyboard(unsigned short value) {
        data[0x6000] = value;
    }

    GtkWidget* getScreenWidget() {
        return screen;
    }

    void set(unsigned int address, unsigned short value) override
    {
        data.at(address) = value;

        // check if we are writing to video RAM
        if (0x4000 <= address && address <0x6000) {
            address -= 0x4000;

            unsigned short y = address / 32;
            unsigned short x = 16*(address % 32);
            for (int bit = 0; bit<16; ++bit) {
                putpixel(x + bit, y, value & 1);
                value = value >> 1;
            }
            gdk_threads_enter();
            gtk_widget_queue_draw(screen);
            gdk_threads_leave();
        }
    }

    unsigned short get(unsigned int address) const override
    { return data.at(address); }
};

struct emulator {
    emulator();
    void load_clicked();
    void run_clicked();
    void pause_clicked();
    gboolean keyboard_callback(GdkEventKey* event);
    void run_thread();
    GtkToolItem* create_button(const gchar* stock_id, const gchar* text, GCallback callback);

    ROM rom;
    RAM ram;
    hcc::CPU cpu;

    bool running = false;

    GtkWidget* window;
    GtkWidget* load_dialog;
    GtkWidget* error_dialog;
    GtkToolItem* button_load;
    GtkToolItem* button_run;
    GtkToolItem* button_pause;
};

gboolean c_keyboard_callback(GtkWidget*, GdkEventKey *event, gpointer user_data)
{ return reinterpret_cast<emulator*>(user_data)->keyboard_callback(event); }

void c_load_clicked(GtkButton*, gpointer user_data)
{ reinterpret_cast<emulator*>(user_data)->load_clicked(); }

void c_run_clicked(GtkButton*, gpointer user_data)
{ reinterpret_cast<emulator*>(user_data)->run_clicked(); }
{'fraction_non_alphanumeric': 0.11023206751054852, 'fraction_numerical': 0.018459915611814346, 'mean_word_length': 2.2019587977034787, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5658243', 'n_tokens_mistral': 3159, 'n_tokens_neox': 2930, 'n_words': 746}