Reading Files

nzjeux26
Posts: 4
Joined: Wed Oct 06, 2021 8:43 am

Reading Files

Postby nzjeux26 » Thu Oct 07, 2021 9:33 am

Hi Team,

I have made a CHIP8 emulator and now have to read the ROM's for it. I have created a folder and added some ROMs into there. Now at first, I was just trying to read them as you would on your PC and quickly worked out that doesn't work like that! After some reading, I managed to get a SPIFFS partition made, allocated space, and followed along through the demo code example from the documentation and after some tinkering, it doesn't bring up any errors and runs fine(i think).

My program is structured so that i have a menu in app_main, which goes to the emulator, which reads a file path, passes on the file name to a read file function, and then starts the emulator.

But my read still doesn't find the files and I'm back to where I started.

I have spent the last two days searching and trying to find something, but I'm just getting information overload and getting frustrated since nothing is clearly standing out that matches my needs, which is a fairly common thing I would have thought.

I'm looking for some clear advice on how to store a file on the board to later be read by my program. Maybe SPIFFS isn't the correct way and something like Fat or jsut NVS would work, but idk. The next option is to try Mkspiffs I read in one post, but I'm sick of chasing empty leads and spinning my wheels. it's no longer fun!

I'm using Vscode with PlatformIO

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: Reading Files

Postby ESP_Sprite » Fri Oct 08, 2021 7:50 am

Hard to say what you're doing wrong; all these options should basically just work. Can you post the code and the errors you get, if any, somewhere?

nzjeux26
Posts: 4
Joined: Wed Oct 06, 2021 8:43 am

Re: Reading Files

Postby nzjeux26 » Sat Oct 09, 2021 5:04 am

So here is the code, The load ROM takes the file name in and then does the opening part, while the chipmain does the SPIFFS stuff and opens the emulator. Now some of this is temporary, i intend of later on allowing the user to pick from a list of ROMS loaded in memory but for now, am hardcoding paths just to simplify things and know they work, hence the messy layout.

I was reading in a book (can't remember which now) that I need to generate a SPIFFS image with the PIO tool, which isn't mentioned or implied in the demo code so I'm not sure I've simply missed that or it's missing from the demo.

I'm not set on SPIFFS so if you think another way is just as effective then i'm happy to change.

I have added the serial output which i write my errors to, maybe that gives more away.

Code: Select all

int load_ROM(char* filename){
    FILE* fp = fopen(filename, "rb");
    
    if(fp == NULL) return errno;

    struct stat st;
    stat(filename, &st);
    size_t fsize = st.st_size;

    size_t bytes_read = fread(memory + 0x200, 1, sizeof(memory) - 0x200, fp);

    if(bytes_read != fsize){
        return -1;
    }

    fclose(fp);
    
    return 0;
}[code]



[code]void chipmain(){

    esp_vfs_spiffs_conf_t conf = {
    .base_path = "/spiffs",
    .partition_label = NULL,
    .max_files = 5,
    .format_if_mount_failed = true
     };

     esp_err_t ret = esp_vfs_spiffs_register(&conf);

      if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            printf("Failed to mount or format filesystem\n");
            vTaskDelay(250);
        } else if (ret == ESP_ERR_NOT_FOUND) {
            printf("Failed to find SPIFFS partition\n");
            vTaskDelay(250);
        } else {
            printf("Failed to initialize SPIFFS (%s)\n", esp_err_to_name(ret));
            vTaskDelay(250);
        }
        return;
      }

      size_t total = 0, used = 0;
      ret = esp_spiffs_info(conf.partition_label,&total,&used);
      if(ret != ESP_OK){
          printf("Failed to get SPIFFS partition information (%s)\n", esp_err_to_name(ret));
      }
      else{
          printf("Partition size: total: %d, used: %d\n", total, used);
      }
    

    printf("[PENDING] Initializing CHIP-8 arch...\n");
    gprintf("[PENDING] Initializing CHIP-8 arch...\n");

    init_CPU();
    printf("[OK!] Done!\n");
    gprintf("[OK!] Done!\n");

    char* rom_filename = "/spiffs/test_opcode.ch8";//need to sort this location stuff.
    printf("[PENDING] Loading rom %s...\n",rom_filename);

    int status = load_ROM(rom_filename);

    if(status == -1){
        printf("FAILURE fread() failure: the return value was not = to the rom file size.\n");
        vTaskDelay(250);
        exit(1);
        //return 1;
    }

    else if(status !=0){
        perror("Error while loading rom!\n");
        vTaskDelay(250);
        exit(1);
       // return 1;
    }

    printf("[OK] Rom loaded!\n");

    //graphics_init();//init_display(); Might not be needed since i do this in main but lets do it
    printf("[OK] Display Online!\n");
    
    while(1){
        cls(0);//the cycle is handling the frame stuff so maybe it doesn't need to be in here
        emulate_cycle();
        //something something keypad

     /*    if(shoud_quit()){
            break;
        } */

        if(draw_flag){
            draw(display);
        }
        flip_frame();
        usleep(1500);
    }
    flip_frame();
    vTaskDelay(500/portTICK_PERIOD_MS);
    while(get_input());
    while(get_input()!=RIGHT_DOWN)
    vTaskDelay(1);
    //return 0;
}
Attachments
Capture.JPG
Capture.JPG (19.34 KiB) Viewed 2142 times

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: Reading Files

Postby ESP_Sprite » Sat Oct 09, 2021 6:04 am

Okay, I think the issue is that you don't have any files on the spiffs (see the 'used: 0') line in your console. You need to indeed send the ROMs to the spiffs some way. You can either do this at runtime (e.g. by running a webserver with uploading capabilities on your ESP32) or at compiletime, using (some functionality that wraps) the above-mentioned mkspiffs tool. Unfortunately, I have no idea how PlatformIO handles this...

nzjeux26
Posts: 4
Joined: Wed Oct 06, 2021 8:43 am

Re: Reading Files

Postby nzjeux26 » Sat Oct 09, 2021 7:04 am

-EDIT- Found out why, stupidly had my serial monitor running in the BG.

So I built the filesystem image, and in the screenshot, the console shows it putting the info in the spiffs.bin.

I also run the upload filesystem image command and that writes fine, but when i try to read the file i still get the same result:
Partition size: total: 233681, used: 0
[PENDING] Initializing CHIP-8 arch...
[OK!] Done!
[PENDING] Loading rom /spiffs/test_opcode.ch8...
Error while loading rom!
: No such file or directory

The parition size has gone down a little, but the used is still stuck at 0.
Attachments
Capture.JPG
Capture.JPG (17.9 KiB) Viewed 2067 times
Last edited by nzjeux26 on Sat Oct 09, 2021 8:11 am, edited 1 time in total.

Who is online

Users browsing this forum: No registered users and 77 guests