<commit_before>#include <satiator.h>
#include <iapetus.h>
#include <string.h>
#include <stdio.h>
#include "gui/gmenu.h"

#define AR_FLASH_ADDR   ((volatile u16 *)0x22000000)
static flash_info_struct flash_info;
int flash_inited = 0;

static int init_flash(void) {
    if (flash_inited)
        return 1;

    int ret = ar_init_flash_io(&flash_info);
    char msg_buf[64];
    uint16_t vid, pid;
    if (ret != IAPETUS_ERR_OK) {
        switch (ret) {
            case IAPETUS_ERR_UNSUPPORTED:
                ar_get_product_id(&vid, &pid);
                sprintf(msg_buf, "Unsupported Flash ID %04x:%04x", vid, pid);
                menu_error("Action Replay", msg_buf);
                return 0;

            case IAPETUS_ERR_HWNOTFOUND:
                menu_error("Action Replay", "No AR cart found!");
                return 0;

            default:
                menu_error("Action Replay", "Unknown error");
                return 0;
        }
    }

    flash_inited = 1;
    return 1;
}

static void flash_backup_ar(void) {
    if (!init_flash())
        return;

    int fd = s_open("ar_backup.bin", FA_READ|FA_WRITE|FA_CREATE_ALWAYS);
    if (fd < 0) {
        menu_error("Action Replay", "Could not open ar_backup.bin for writing");
        return;
    }

    int length = flash_info.page_size * flash_info.num_pages;
    int done = 0;
    uint8_t *ptr = (void*)AR_FLASH_ADDR;

    menu_progress_begin("Reading...", length);

    while (done < length) {
        int to_copy = length;
        if (to_copy > S_MAXBUF)
            to_copy = S_MAXBUF;
        s_write(fd, ptr, to_copy);
        done += to_copy;
        ptr += to_copy;
        menu_progress_update(done);
        for (volatile int i=0; i<10000; i++);
    }

    menu_progress_complete();

    s_close(fd);
    menu_error("Action Replay", "Successfully backed up to ar_backup.bin.");
}

static void flash_erase_ar(void) {
    if (!init_flash())
        return;

    menu_progress_begin("Erasing...", flash_info.num_pages);
    ar_erase_flash_all(&flash_info);
    menu_progress_complete();
}


static int bin_file_filter(file_ent *entry) {
    if (entry->isdir)
        return 1;

    int len = strlen(entry->name);
    if (!strcasecmp(&entry->name[len-4], ".bin"))
        return 1;

    return 0;
}

static void flash_flash_ar(void) {
    if (!init_flash())
        return;

    int nents;
    file_ent *list = file_list_create(".", &nents, bin_file_filter);
    file_list_sort(list, nents);

    int entry = menu_picklist(list, nents, "Choose image to flash");
    if (entry < 0)
        return;

    int fd = s_open(list[entry].name, FA_READ);
    file_list_free(list, nents);

    if (fd < 0) {
        menu_error("Action Replay", "Could not open file");
        return;
    }

    int file_size = s_seek(fd, 0, 2);
    s_seek(fd, 0, 0);


    uint8_t buf[S_MAXBUF], empty[S_MAXBUF];
    int write_size = S_MAXBUF;
    int blocks = (file_size + write_size - 1) / write_size;
    int pages_per = write_size / flash_info.page_size;
    int page_words = write_size / 2;
    volatile u16 *write_ptr = AR_FLASH_ADDR;

    memset(empty, 0xff, sizeof(empty));

    menu_progress_begin("Writing...", blocks);

    for (int i=0; i<blocks; i++) {
        menu_progress_update(i);<commit_msg>ar: use the correct # words in a page<commit_after>#include <satiator.h>
#include <iapetus.h>
#include <string.h>
#include <stdio.h>
#include "gui/gmenu.h"

#define AR_FLASH_ADDR   ((volatile u16 *)0x22000000)
static flash_info_struct flash_info;
int flash_inited = 0;

static int init_flash(void) {
    if (flash_inited)
        return 1;

    int ret = ar_init_flash_io(&flash_info);
    char msg_buf[64];
    uint16_t vid, pid;
    if (ret != IAPETUS_ERR_OK) {
        switch (ret) {
            case IAPETUS_ERR_UNSUPPORTED:
                ar_get_product_id(&vid, &pid);
                sprintf(msg_buf, "Unsupported Flash ID %04x:%04x", vid, pid);
                menu_error("Action Replay", msg_buf);
                return 0;

            case IAPETUS_ERR_HWNOTFOUND:
                menu_error("Action Replay", "No AR cart found!");
                return 0;

            default:
                menu_error("Action Replay", "Unknown error");
                return 0;
        }
    }

    flash_inited = 1;
    return 1;
}

static void flash_backup_ar(void) {
    if (!init_flash())
        return;

    int fd = s_open("ar_backup.bin", FA_READ|FA_WRITE|FA_CREATE_ALWAYS);
    if (fd < 0) {
        menu_error("Action Replay", "Could not open ar_backup.bin for writing");
        return;
    }

    int length = flash_info.page_size * flash_info.num_pages;
    int done = 0;
    uint8_t *ptr = (void*)AR_FLASH_ADDR;

    menu_progress_begin("Reading...", length);

    while (done < length) {
        int to_copy = length;
        if (to_copy > S_MAXBUF)
            to_copy = S_MAXBUF;
        s_write(fd, ptr, to_copy);
        done += to_copy;
        ptr += to_copy;
        menu_progress_update(done);
        for (volatile int i=0; i<10000; i++);
    }

    menu_progress_complete();

    s_close(fd);
    menu_error("Action Replay", "Successfully backed up to ar_backup.bin.");
}

static void flash_erase_ar(void) {
    if (!init_flash())
        return;

    menu_progress_begin("Erasing...", flash_info.num_pages);
    ar_erase_flash_all(&flash_info);
    menu_progress_complete();
}


static int bin_file_filter(file_ent *entry) {
    if (entry->isdir)
        return 1;

    int len = strlen(entry->name);
    if (!strcasecmp(&entry->name[len-4], ".bin"))
        return 1;

    return 0;
}

static void flash_flash_ar(void) {
    if (!init_flash())
        return;

    int nents;
    file_ent *list = file_list_create(".", &nents, bin_file_filter);
    file_list_sort(list, nents);

    int entry = menu_picklist(list, nents, "Choose image to flash");
    if (entry < 0)
        return;

    int fd = s_open(list[entry].name, FA_READ);
    file_list_free(list, nents);

    if (fd < 0) {
        menu_error("Action Replay", "Could not open file");
        return;
    }

    int file_size = s_seek(fd, 0, 2);
    s_seek(fd, 0, 0);


    uint8_t buf[S_MAXBUF], empty[S_MAXBUF];
    int write_size = S_MAXBUF;
    int blocks = (file_size + write_size - 1) / write_size;
    int page_words = write_size / 2;
    int pages_per = page_words / flash_info.page_size;
    volatile u16 *write_ptr = AR_FLASH_ADDR;

    memset(empty, 0xff, sizeof(empty));

    menu_progress_begin("Writing...", blocks);

    for (int i=0; i<blocks; i++) {
        menu_progress_update(i);
{'fraction_non_alphanumeric': 0.12373968835930339, 'fraction_numerical': 0.015887564925145127, 'mean_word_length': 2.533189422558014, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '686701', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2255, 'n_words': 526}