<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://dreamcast.wiki/wiki/index.php?action=history&amp;feed=atom&amp;title=Save%2FLoad_file</id>
	<title>Save/Load file - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://dreamcast.wiki/wiki/index.php?action=history&amp;feed=atom&amp;title=Save%2FLoad_file"/>
	<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Save/Load_file&amp;action=history"/>
	<updated>2026-05-16T09:46:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://dreamcast.wiki/wiki/index.php?title=Save/Load_file&amp;diff=2117&amp;oldid=prev</id>
		<title>Unknown user: Created page with &quot;= VMU =  The following code will demonstrate how to write a compressed file to the VMU using KallistiOS. It is possible to just write raw data to the VMU, but in order for...&quot;</title>
		<link rel="alternate" type="text/html" href="https://dreamcast.wiki/wiki/index.php?title=Save/Load_file&amp;diff=2117&amp;oldid=prev"/>
		<updated>2023-03-01T23:09:35Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= VMU =  The following code will demonstrate how to write a compressed file to the VMU using &lt;a href=&quot;/KallistiOS&quot; title=&quot;KallistiOS&quot;&gt;KallistiOS&lt;/a&gt;. It is possible to just write raw data to the VMU, but in order for...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= VMU =&lt;br /&gt;
&lt;br /&gt;
The following code will demonstrate how to write a compressed file to the VMU using [[KallistiOS]].&lt;br /&gt;
It is possible to just write raw data to the VMU, but in order for the file to show up properly in the VMU menu, a VMU package header with a description and icon is specified as well.&lt;br /&gt;
&lt;br /&gt;
The following headers are required for the VMU header and compression.&lt;br /&gt;
 #include &amp;lt;kos.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;zlib/zlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Saving ==&lt;br /&gt;
The save function will write to the first controller&amp;#039;s first VMU.&lt;br /&gt;
&lt;br /&gt;
  int DC_SaveToVMU(char *src) {&lt;br /&gt;
    char dst[32];&lt;br /&gt;
    file_t file;&lt;br /&gt;
    int filesize = 0;&lt;br /&gt;
    unsigned long zipsize = 0;&lt;br /&gt;
    uint8 *data;&lt;br /&gt;
    uint8 *zipdata;&lt;br /&gt;
    vmu_pkg_t pkg;&lt;br /&gt;
    uint8 *pkg_out;&lt;br /&gt;
    int pkg_size;&lt;br /&gt;
&lt;br /&gt;
    // Our VMU + full save name&lt;br /&gt;
    strcpy(dst, &amp;quot;/vmu/a1/&amp;quot;);&lt;br /&gt;
    strcat(dst, src);&lt;br /&gt;
&lt;br /&gt;
    // Reads in the file from the CWD&lt;br /&gt;
    file = fs_open(src, O_RDONLY);&lt;br /&gt;
    filesize = fs_total(file);&lt;br /&gt;
    data = (uint8*)malloc(filesize);&lt;br /&gt;
    fs_read(file, data, filesize);&lt;br /&gt;
    fs_close(file);&lt;br /&gt;
&lt;br /&gt;
    // Allocate some memory for compression&lt;br /&gt;
    zipsize = filesize * 2;&lt;br /&gt;
    zipdata = (uint8*)malloc(zipsize);&lt;br /&gt;
&lt;br /&gt;
    // The compressed save&lt;br /&gt;
    compress(zipdata, &amp;amp;zipsize, data, filesize);&lt;br /&gt;
&lt;br /&gt;
    // Required VMU header&lt;br /&gt;
    // You will have to have a VMU icon defined under icon_data&lt;br /&gt;
    strcpy(pkg.desc_short, &amp;quot;Wolf4SDL\\DC&amp;quot;);&lt;br /&gt;
    strcpy(pkg.desc_long, &amp;quot;Game Save&amp;quot;);&lt;br /&gt;
    strcpy(pkg.app_id, &amp;quot;Wolf4SDL\\DC&amp;quot;);&lt;br /&gt;
    pkg.icon_cnt = 1;&lt;br /&gt;
    pkg.icon_anim_speed = 0;&lt;br /&gt;
    memcpy(&amp;amp;pkg.icon_pal[0], icon_data, 32);&lt;br /&gt;
    pkg.icon_data = icon_data + 32;&lt;br /&gt;
    pkg.eyecatch_type = VMUPKG_EC_NONE;&lt;br /&gt;
    pkg.data_len = zipsize;&lt;br /&gt;
    pkg.data = zipdata;&lt;br /&gt;
    vmu_pkg_build(&amp;amp;pkg, &amp;amp;pkg_out, &amp;amp;pkg_size);&lt;br /&gt;
&lt;br /&gt;
    // Save the newly created VMU save to the VMU&lt;br /&gt;
    fs_unlink(dst);&lt;br /&gt;
    file = fs_open(dst, O_WRONLY);&lt;br /&gt;
    fs_write(file, pkg_out, pkg_size);&lt;br /&gt;
    fs_close(file);&lt;br /&gt;
&lt;br /&gt;
    // Free unused memory&lt;br /&gt;
    free(pkg_out);&lt;br /&gt;
    free(data);&lt;br /&gt;
    free(zipdata);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Loading ==&lt;br /&gt;
  int DC_LoadFromVMU(char *dst) {&lt;br /&gt;
    char src[32];&lt;br /&gt;
    int file;&lt;br /&gt;
    int filesize;&lt;br /&gt;
    unsigned long unzipsize;&lt;br /&gt;
    uint8* data;&lt;br /&gt;
    uint8* unzipdata;&lt;br /&gt;
    vmu_pkg_t pkg;&lt;br /&gt;
&lt;br /&gt;
    // Our VMU + full save name&lt;br /&gt;
    strcpy(src, &amp;quot;/vmu/a1/&amp;quot;);&lt;br /&gt;
    strcat(src, dst);&lt;br /&gt;
&lt;br /&gt;
    // Remove VMU header&lt;br /&gt;
    file = fs_open(src, O_RDONLY);&lt;br /&gt;
    if(file == 0) return -1;&lt;br /&gt;
    filesize = fs_total(file);&lt;br /&gt;
    if(filesize &amp;lt;= 0) return -1;&lt;br /&gt;
    data = (uint8*)malloc(filesize);&lt;br /&gt;
    fs_read(file, data, filesize);&lt;br /&gt;
    vmu_pkg_parse(data, &amp;amp;pkg);&lt;br /&gt;
    fs_close(file);&lt;br /&gt;
&lt;br /&gt;
    // Allocate memory for the uncompressed data&lt;br /&gt;
    unzipdata = (uint8 *)malloc(65536);&lt;br /&gt;
    unzipsize = 65536;&lt;br /&gt;
&lt;br /&gt;
    // Uncompress the data to the CWD&lt;br /&gt;
    uncompress(unzipdata, &amp;amp;unzipsize, (uint8 *)pkg.data, pkg.data_len);&lt;br /&gt;
    fs_unlink(dst);&lt;br /&gt;
    file = fs_open(dst, O_WRONLY);&lt;br /&gt;
    fs_write(file, unzipdata, unzipsize);&lt;br /&gt;
    fs_close(file);&lt;br /&gt;
&lt;br /&gt;
    // Free unused memory&lt;br /&gt;
    free(data);&lt;br /&gt;
    free(unzipdata);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Unknown user</name></author>
	</entry>
</feed>