Japanese
SGL User's ManualPROGRAMMER'S TUTORIAL
BackForward
PROGRAMMER'S TUTORIAL

13. Backup Library


The Sega Saturn has a built-in buffer memory of 32kbytes. This chapter explains the backup library for using this built-in buffer memory.
The main body of the backup library is compressed and stored in the Sega Saturn's B00TROM. This backup library can be used to transfer data to and from buffer memory and external cartridges.

13-1. Features of backup library

device

●Supported devices
Devices such as built-in backup memory and external cartridges (currently these two types) can be accessed in a file format that does not have a hierarchical structure (Table 13-1). Additionally, these devices are differentiated by partitions, as shown in Figure 13-1.

●Connection information for each device (Config)
Connection information (Config) for each device can be obtained when initializing the backup library.

●Information for each device (Status: connected/not connected, formatted/unformatted, etc.)
It can be obtained using the backup library function. You can also specify the size of the file you want to write and check if it is writable.

Table 13-1 Device list
Device number Device type
0 Built-in memory cartridge
1 Memory cartridge or parallel interface

Figure 13-1 Device configuration

File

Information for each file (Dir: name, creation time, etc.) can be obtained in the same way using the backup library function.
The file information settings at the time of file creation are the responsibility of the user, and the backup library itself does not create file information.

Deploying the library

The main body of the backup library is compressed and stored in the Sega Saturn's BOOTROM. When using the backup library, the user secures a program development area and a work area to perform initial processing. From now on, each function will be available for use.

 Note
The backup library will destroy data if the process is interrupted while writing data. Therefore, when initializing, formatting, writing, or deleting the backup library, use "slResetDisable()" (peripheral function) in the system library to disable the reset button.

13-2. Basic processing flow

We will explain the backup library functions along with the basic processing flow.

1) Initialization [BUP_Init()]
Reads the backup library from BOOTROM and expands it to the specified address space. Returns information about each device at that time.

2) Partition selection [BUP_SelPart()]
Specify the device partition to use. The default partition is number 0 (internal memory cartridge).

3) Get status [BUP_Stat()]
Get the status of the device you want to read and write to. For writing, specify the size of the data you want to write in bytes. The return value determines whether writing is possible.

4) Format [BUP_Format()]
If the device you want to use is unformatted by getting the status, you will need to format it. The scope of the format is the specified partition on the specified device.

5) Get directory information [BUP_Dir()]
Obtain directory information by specifying a file name (or for all files). If you want information about all files, specify NULL for the file name.

6) Write data [BUP_Write()]
Specify the device and write the file. The necessary file information is created by the user.

7) Query data [BUP_Verify()]
Specify the device and inquire about the file being written.

8) Read data [BUP_Read()]
Specify the device and read the file.

9) Delete data [BUP_Delete()]
Specify the device and delete the file.

10) Expand date data [BUP_GetDate()]
Compress file information and expand stored date data.

11) Compression of date data [BUP_SetDate()]
Performs compression to store date data in file information.

13-3. Sample program

Next, a flowchart shows the flow of a sample program that uses the backup library.

Flow 13-1 Sample program using backup library

Listing 13-1 shows the program list of the sample program.

Listing 13-1 Sample program

#include "sgl.h"
#define BUP_START_ADDR 0x6070000
#include "sega_bup.h"

#define FILE_NAME "FILE_NAME01"
#define BACKUP_DEVICE (Uint32)0
#define TEST_DATA "It's a pen"
#define TEST_SIZE 10
#define DIR_SIZE 8
#define HEX2DEC(x) ( ( 0x0f&(x) ) + ( (x)>> 4 )*10 )

/************************************************* **

  sample for backupram

************************************************** */

Uint32 BackUpRamWork[2048 + 30];

void BackUpInit(BupConfig cntb[3] )
{
	slResetDisable();
	BUP_Init((Uint32 *)BUP_START_ADDR,BackUpRamWork,cntb);
	slResetEnable();
}

Sint32 BackUpWrite(Uint32 device, BupDir *dir, Uint8 *data, Uint8 sw)
{
	Sint32 ret;
	SmpcDateTime *time;
	BupDate date;
	
	if(!dir-> date){
		time = &(Smpc_Status-> rtc);
		date.year = (Uint8 )( slDec2Hex((Uint32)time-> year)-1980 ); /* Modify KT */
		date.month = (Uint8 )( time-> month & 0x0f);
		date.week = (Uint8 )( time-> month>> Four );
		date.day = (Uint8 )( slDec2Hex((Uint32)time-> date)); /* Modify KT */
		date.time = (Uint8 )( slDec2Hex((Uint32)time-> hour)); /* Modify KT */
		date.min = (Uint8 )( slDec2Hex((Uint32)time-> minute)); /* Modify KT */
		dir-> date = BUP_SetDate(&date);
	}
	slResetDisable();
	ret = BUP_Write(device,dir,data,sw);
	slResetEnable();

	return(ret);
}

Sint32 BackUpDelete(Uint32 device,Uint8 *filename)
{
	Sint32 ret;

	slResetDisable();
	ret = BUP_Delete(device,filename);
	slResetEnable();

	return(ret);
}


Sint32 BackUpFormat(Uint32 device)
{
	Sint32 ret;

	slResetDisable();
	ret = BUP_Format(device);
	slResetEnable();

	return(ret);
}


void ss_main()
{
    BupConfig conf[3];
    BupStat sttb;
    BupDir dir, dirs[DIR_SIZE];
    BupDate datetb, date;
    Uint8 *time;
    Sint32 status;
    Uint8 buf[256];
    int i,lin=4;

    slInitSystem(TV_352x224,(TEXTURE *)NULL,1);

    slPrint("Sample program Backup Library" , slLocate(9,2));

    slGetStatus(); 
    for(i=0;i< 100;i++)
      {
	  slSynch();
      }

    BackUpInit( conf );
                                                
    if( ( status = BUP_Stat( BACKUP_DEVICE, 10, &sttb ) ) == BUP_UNFORMAT )
      {
	  status = BackUpFormat(BACKUP_DEVICE);
	  slPrint("Formatting device" , slLocate(10,lin));
	  BUP_Stat( BACKUP_DEVICE, TEST_SIZE, &sttb );
      }

    if( sttb.freeblock> 0)
      {
	  strncpy( dir.filename, FILE_NAME , 11 );
	  strncpy( dir.comment , "Test desu", 10 );
	  dir.language = BUP_ENGLISH;
	  dir.datasize = TEST_SIZE;
	  dir.date = 0;
 	  slPrint("Writing file." , slLocate(10,++lin));
	  slPrint("Filename= ", slLocate(13,++lin));
	  slPrint( FILE_NAME , slLocate(23,lin ));
	  status = BackUpWrite(BACKUP_DEVICE, &dir, (Uint8 *)TEST_DATA,OFF);
	  status = BUP_Verify (BACKUP_DEVICE,(Uint8 *)FILE_NAME,(Uint8 *)TEST_DATA);
      }

    status = BUP_Dir ( BACKUP_DEVICE, (Uint8 *)FILE_NAME , DIR_SIZE, dirs );
    status = BUP_Dir ( BACKUP_DEVICE, (Uint8 *)"", DIR_SIZE, dirs );
    for ( i = 0; i< status && i< 10; i++)
      {
	  char cmnt[11] = "Dirs = ";

	  cmnt[6] = (char)(i+20);
	  slPrint("Dirs = " , slLocate(10,++lin));
	  slPrint(dirs[i].filename, slLocate(20, lin));
      }
    status = BackUpWrite(BACKUP_DEVICE, &dir, (Uint8 *)TEST_DATA,OFF);
    status = BUP_Read( BACKUP_DEVICE, (Uint8 *)FILE_NAME, buf );
    slPrint("Reading file." , slLocate(10,++lin));
    slPrint("Filename= ", slLocate(13,++lin));
    slPrint( FILE_NAME , slLocate(23,lin ));
#if 0
    status = BackUpDelete( BACKUP_DEVICE, (Uint8 *)FILE_NAME );
    status = BUP_Dir ( BACKUP_DEVICE, (Uint8 *)"", DIR_SIZE, dirs );
    slPrint("Deleting =" , slLocate(10,++lin));
    slPrint("Filename= ", slLocate(13,++lin));
    slPrint( FILE_NAME , slLocate(23,lin ));
#endif
    for ( i = 0; i< status && i< 10; i++)
      {
	  char cmnt[11] = "Dirs = ";

	  cmnt[6] = (char)(i+20);
	  slPrint("Dirs = " , slLocate(10,++lin));
	  slPrint(dirs[i].filename, slLocate(20, lin));
      }

    for(;;)
      {
	  slSynch();
      }
}

Additional notes Backup library functions

Table 13-2 lists the functions used in the backup library.
For detailed reference of functions, please refer to PROGRAMMER'S GUIDE VOL.1.

Table 13-2 Backup library functions
 number
 Function name
   function
1 BUP_Init Initializing the backup library
2 BUP_SelPart Partition selection
3 BUP_Format Execute format
4 BUP_Stat Get status
5 BUP_Write writing data
6 BUP_Read Loading data
7 BUP_Delete Delete data
8 BUP_Dir Get directory information
9 BUP_Verify Query data
10 BUP_GetDate Expand date data
11 BUP_SetDate Compressing date data


BackForward
SGL User's ManualPROGRAMMER'S TUTORIAL
Copyright SEGA ENTERPRISES, LTD., 1997