Performing basic VMU file I/O operations?

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
Silent Marauder
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 24
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Wed Jul 21, 2004 8:06 pm
Has thanked: 0
Been thanked: 0

Performing basic VMU file I/O operations?

Post by Silent Marauder »

I've been digging through the KOS headers and I can't quite figure out how to access the VMU's filesystem. I'm trying to find out how to read a file, create and write to a file, and check the VMU for the existence of a file or files matching a given pattern. There aren't any tutorials as far as I can see, and the .h files are less than helpful, particularly since there are two headers that provide VMU file I/O. And because there are two headers, I can't tell if the example program in KOS is using the "right" one.

If you've worked with the VMU filesystem before, I'd very much appreciate any advice you could give me about it.

Thanks!
OneThirty8
Damn Dirty Ape
Damn Dirty Ape
Posts: 5031
Joined: Thu Nov 07, 2002 11:11 pm
Location: Saugerties, NY
Has thanked: 0
Been thanked: 0

Re: Performing basic VMU file I/O operations?

Post by OneThirty8 »

This isn't really part of your question, but the main thing you need to keep in mind with VMU files that differs from other files is that they will need to have the correct header if you want the files to be visible via the Dreamcast's BIOS menu. If this is a concern for you, you will want to create your VMU file in ram using the tools KOS provides before writing it to your VMU. I believe that you actually write files to the VMU just like any other filesystem. You'll just need to be aware that you can have multiple slots, so you will need to know which VMU you're writing to. In my Wolf3D port, I think I just always wrote to /vmu/a1. I had Wolf3D write its save files to /ram, then I compressed them with bz2, then tacked the header on, and then wrote the whole thing to the vmu after all that. It has been a while since I did any of this and I'm not looking at any code at the moment, so be advised that I may not be giving 100% accurate info.
User avatar
emptythought
DC Developer
DC Developer
Posts: 2015
Joined: Wed Jan 30, 2002 9:14 am
Location: UNITED STATES NRN
Has thanked: 0
Been thanked: 0
Contact:

Re: Performing basic VMU file I/O operations?

Post by emptythought »

First, you need the main header file.

Code: Select all

#include <kos.h>
If you want compression, you'll need to include zlib's header file.

Code: Select all

#include <zlib/zlib.h>
Now our saving function.

Code: Select all

int DC_SaveToVMU(char *src) {
    char dst[32];
    file_t file;
    int filesize = 0;
    unsigned long zipsize = 0;
    uint8 *data;
    uint8 *zipdata;
    vmu_pkg_t pkg;
    uint8 *pkg_out;
    int pkg_size;

    // Our VMU + full save name
    strcpy(dst, "/vmu/a1/");
    strcat(dst, src);

    // Reads in the file from the CWD
    file = fs_open(src, O_RDONLY);
    filesize = fs_total(file);
    data = (uint8*)malloc(filesize);
    fs_read(file, data, filesize);
    fs_close(file);

    // Allocate some memory for compression
    zipsize = filesize * 2;
    zipdata = (uint8*)malloc(zipsize);

    // The compressed save
    compress(zipdata, &zipsize, data, filesize);

    // Required VMU header
    // You will have to have a VMU icon defined under icon_data
    strcpy(pkg.desc_short, "Wolf4SDL\\DC");
    strcpy(pkg.desc_long, "Game Save");
    strcpy(pkg.app_id, "Wolf4SDL\\DC");
    pkg.icon_cnt = 1;
    pkg.icon_anim_speed = 0;
    memcpy(&pkg.icon_pal[0], icon_data, 32);
    pkg.icon_data = icon_data + 32;
    pkg.eyecatch_type = VMUPKG_EC_NONE;
    pkg.data_len = zipsize;
    pkg.data = zipdata;
    vmu_pkg_build(&pkg, &pkg_out, &pkg_size);

    // Save the newly created VMU save to the VMU
    fs_unlink(dst);
    file = fs_open(dst, O_WRONLY);
    fs_write(file, pkg_out, pkg_size);
    fs_close(file);

    // Free unused memory
    free(pkg_out);
    free(data);
    free(zipdata);

    return 0;
}
...and our loading\reading function.

Code: Select all

int DC_LoadFromVMU(char *dst) {
    char src[32];
    int file;
    int filesize;
    unsigned long unzipsize;
    uint8* data;
    uint8* unzipdata;
    vmu_pkg_t pkg;

    // Our VMU + full save name
    strcpy(src, "/vmu/a1/");
    strcat(src, dst);

    // Remove VMU header
    file = fs_open(src, O_RDONLY);
    if(file == 0) return -1;
    filesize = fs_total(file);
    if(filesize <= 0) return -1;
    data = (uint8*)malloc(filesize);
    fs_read(file, data, filesize);
    vmu_pkg_parse(data, &pkg);
    fs_close(file);

    // Allocate memory for the uncompressed data
    unzipdata = (uint8 *)malloc(65536);
    unzipsize = 65536;

    // Uncompress the data to the CWD
    uncompress(unzipdata, &unzipsize, (uint8 *)pkg.data, pkg.data_len);
    fs_unlink(dst);
    file = fs_open(dst, O_WRONLY);
    fs_write(file, unzipdata, unzipsize);
    fs_close(file);

    // Free unused memory
    free(data);
    free(unzipdata);

    return 0;
}
That's from the dc_vmu.cpp file from Wolf4SDL\DC, only simplified and with more comments. It should give you an idea of how to do read\writes to the first VMU (slot A) plugged in the first controller (port A).

Credits to OneThirty8 for the code from sdlWolf, I simply modified it to use zlib rather than bzip2.
Silent Marauder
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 24
Joined: Wed Jul 21, 2004 8:06 pm
Has thanked: 0
Been thanked: 0

Re: Performing basic VMU file I/O operations?

Post by Silent Marauder »

Thank you very much! :grin:
User avatar
emptythought
DC Developer
DC Developer
Posts: 2015
Joined: Wed Jan 30, 2002 9:14 am
Location: UNITED STATES NRN
Has thanked: 0
Been thanked: 0
Contact:

Re: Performing basic VMU file I/O operations?

Post by emptythought »

Sure, I hope it's useful!
DrNicholas
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 31
Joined: Sun Oct 02, 2011 5:33 pm
Has thanked: 0
Been thanked: 0

Re: Performing basic VMU file I/O operations?

Post by DrNicholas »

how might you activate the savign and the loading?
Post Reply