Clean up file access + allow using BFile as an option

BFile can now be selected in <platform.h> by defining CGDOOM_WAD_BFILE
instead of CGDOOM_WAD_MAPPING. The DMA option is not implemented yet.

BFile works as expected - a lot of stuttering due to reads during
gameplay. But the status bar texture still doesn't load properly!
This commit is contained in:
Lephenixnoir 2021-07-27 11:12:31 +02:00
parent f2f2cd1ea4
commit f477b87e50
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 79 additions and 112 deletions

View File

@ -2,6 +2,27 @@
#define PLATFORM_H
// real Casio fx-CG HW
//---
// WAD file access method (enable exactly one)
//---
/* Use BFile dynamically (slow but entirely accurate, a good reference) */
// #define CGDOOM_WAD_BFILE
/* Search fragments in physical ROM and copy by hand (faster) */
#define CGDOOM_WAD_MAPPING
/* Search fragments in phyiscal ROM and copy with DMA (even faster) */
// #define CGDOOM_WAD_MAPPING_DMA
/* Settings for file mappings: traverse the whole 32-MiB Flash */
#define FLASH_START (0xA0000000)
#define FLASH_END (0xA2000000)
/* Storage unit is a cluster of 512 bytes */
#define FLASH_PAGE_SIZE 512
#define FLASH_PAGE_SIZE_LOG2 9
#define FLASH_PAGE_COUNT ((FLASH_END-FLASH_START) / FLASH_PAGE_SIZE)
//---
typedef struct{
int x;
int y;
@ -44,7 +65,7 @@ typedef struct{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define WIDTH 384
@ -87,16 +108,6 @@ extern TSysCallFuncPtr fnSysCallFuncPtr;
#define ASSERT(x)
//flash file mapping
#define FLASH_START (0xA0000000)
#define FLASH_END (0xA2000000)
//Lephe: Yatis established that allocation is in sectors of 512 bytes
#define FLASH_PAGE_SIZE 512
#define FLASH_PAGE_SIZE_LOG2 9
#define FLASH_PAGE_OFFSET_MASK (FLASH_PAGE_SIZE - 1)
//
#define FLASH_PAGE_COUNT ((FLASH_END-FLASH_START) >> FLASH_PAGE_SIZE_LOG2)
#define InitFlashSimu(filename)
#endif //#ifndef PLATFORM_H

View File

@ -276,25 +276,35 @@ return i;
}
*/
int gWADHandle;
const unsigned short wadfile[] = {'\\','\\','f','l','s','0','\\','d','o','o','m','.','w','a','d',0};
///////////////////////////////////////////////////////////////////////////////
// WAD file access mechanism
//
// Since BFile is too slow to access the WAD file in real-time, the fragments
// are searched in ROM and their physical addresses are stored for direct
// access later. This is similar to mmap() except that a manual translation is
// used instead of the MMU. As far as I know, the first version of this system
// was implemented by Martin Poupe.
//
// The file is obviously fragmented and Yatis reverse-engineered Fugue enough
// to determine that storage units are sectors of 512 bytes. While clusters of
// 4 kiB are used too, a file might not start on the first sector of a cluster,
// and some sectors might also be dysfunctional.
//
// See <platform.h> for Flash traversal parameters.
///////////////////////////////////////////////////////////////////////////////
static uint16_t const *cgdoom_wad_path = u"\\\\fls0\\doom.wad";
#ifdef CGDOOM_WAD_BFILE
/* File descriptor to WAD file, used in Flash_ReadFile calls from w_wad.c. */
static int cgdoom_wad_fd = -1;
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//The whole sound doesn't fir onto the RAM.
//Reading per partes is not possible as this is synchronnous player (there would be silences when reading).
//So I read each page (4KB)of the wav file and try to find it in the flash.
//Simply finding start of the file is not enough because of fragmentation.
//Seach the whole flash, do not assume FS start at 0xA1000000
//(already tried interval 0xA1000000 - 0xA1FFFFFF, but some parts of the file were outside of this interval)
// move this to platform.h, simulator will use static buffer
//#define FLASH_START 0xA0000000
//page has 4 KB (I hope)
//#define FLASH_PAGE_SIZE 4096
//8K pages
//#define FLASH_PAGE_COUNT (4096*2)
//allocate 1024 items for max 1024 fragments of the file.
// 640 KB should to be enough for everyone ;-)
#define MAX_FRAGMENTS 1024
@ -449,6 +459,20 @@ lbExit:
void I_Error (char *error, ...);
static FileMapping *gpWADMap = 0;
#ifdef CGDOOM_WAD_BFILE
int FindInFlash(void **buf, int size, int readpos)
{
return 0;
}
int Flash_ReadFile(void *buf, int size, int readpos)
{
return Bfile_ReadFile_OS(cgdoom_wad_fd, buf, size, readpos);
}
#else /* CGDOOM_WAD_MAPPING, CGDOOM_WAD_MAPPING_DMA */
int FindInFlash(void **buf, int size, int readpos)
{
int iPageReq = readpos >>FLASH_PAGE_SIZE_LOG2;
@ -506,71 +530,7 @@ int Flash_ReadFile(void *buf, int size, int readpos)
return iRet;
}
int Flash_ReadFileX(void *buf, int size, int readpos)
{
int iRet = 0;
if(gpWADMap->miTotalLength - readpos < size)
{
size = gpWADMap->miTotalLength - readpos;
}
/*
typedef struct
{
short msOffset;//page index (0 ~ 8K)
short msCount;//count of pages in this fragment
}FileMappingItem;
typedef struct
{
FileMappingItem mTable[MAX_FRAGMENTS];//table of fragments
int miItemCount;
int miTotalLength;//length of the file
}FileMapping;*/
ASSERT(readpos >=0);
ASSERT(FLASH_PAGE_SIZE == 1<< FLASH_PAGE_SIZE_LOG2);
for(;;)
{
int iPageReq = readpos >>FLASH_PAGE_SIZE_LOG2;
int iPageIndx = 0;
int iCurrOffset = 0, iCurrLen;
int iSubOffset;
//find item
for(;;)
{
if(iPageIndx >= gpWADMap->miItemCount)
{
return -1;
}
if(iPageReq < gpWADMap->mTable[iPageIndx].msCount)
{
ASSERT(iCurrOffset <= readpos);
break;
}
iPageReq -= gpWADMap->mTable[iPageIndx].msCount;
iCurrOffset += ((int)gpWADMap->mTable[iPageIndx].msCount) << FLASH_PAGE_SIZE_LOG2;
iPageIndx++;
}
iSubOffset = readpos - iCurrOffset;
iCurrLen = (gpWADMap->mTable[iPageIndx].msCount * FLASH_PAGE_SIZE) - iSubOffset;
ASSERT(iCurrLen > 0);
if(iCurrLen > size)
{
iCurrLen = size;
}
memcpy(buf,((char *)FLASH_START)+(gpWADMap->mTable[iPageIndx].msOffset << FLASH_PAGE_SIZE_LOG2)+iSubOffset,iCurrLen);
readpos += iCurrLen;
size -= iCurrLen;
buf = ((char *)buf) +iCurrLen;
iRet += iCurrLen;
if(size ==0)
{
break;
}
}
return iRet;
}
#endif /* CGDOOM_WAD_* access method */
void abort(void){
int x=0,y=160;
@ -581,7 +541,7 @@ void abort(void){
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void main(void){
InitFlashSimu(wadfile); //load wad file to flash simulation on simulator, do nothing on real HW
InitFlashSimu(cgdoom_wad_path); //load wad file to flash simulation on simulator, do nothing on real HW
#ifdef CG_EMULATOR
SaveVRAMBuffer = aSaveVRAMBuffer;
SystemStack = aSystemStack;
@ -594,26 +554,33 @@ void main(void){
VRAM = (unsigned short*)GetVRAMAddress();
EnableColor(1);
memset(VRAM,0,WIDTH*HEIGHT*2);
/* Setup access to WAD file */
#ifdef CGDOOM_WAD_BFILE
cgdoom_wad_fd = Bfile_OpenFile_OS(cgdoom_wad_path, 0, 0);
#else
gpWADMap = (FileMapping *)(SaveVRAMBuffer + 2*65536);
ASSERT(2*65536 + sizeof(FileMapping) < SAVE_VRAM_SIZE);
gWADHandle = CreateFileMapping(wadfile,gpWADMap);
switch(gWADHandle)
{
case -1:
int size = CreateFileMapping(cgdoom_wad_path,gpWADMap);
if(size == -1) {
I_Error ("File read error");
return;
break;
case -2:
}
else if(size == -2) {
I_Error ("Page not found");
return;
break;
case -3:
}
else if(size == -3) {
I_Error ("File too fragmented");
return;
break;
default:
break;
}
else if(size < 0) {
I_ErrorI ("CreateFileMapping", size, 0, 0, 0);
return;
}
#endif
D_DoomMain();
}

View File

@ -32,8 +32,6 @@ void CGDAppendNum0_999(const char *pszText,int iNum,int iMinDigits,char *pszBuf)
int abs(int x);
extern int gWADHandle;
//force compiler error on use following:
#define strcpy 12
#define strnicmp 22

View File

@ -91,16 +91,9 @@ static int W_AddFile ()
filelump_t* fileinfo;
filelump_t* fileinfo_mem;
if (gWADHandle < 0)
{
I_Error("Couldn't open doom.wad");
return 0; // CX port
}
//printf ("Adding %s\n",filename);
startlump = numlumps;
//Bfile_ReadFile_OS(gWADHandle, &header, sizeof(header),0);
Flash_ReadFile(&header, sizeof(header),0);
if (CGDstrncmp(header.identification,"IWAD",4))
{
@ -113,7 +106,6 @@ static int W_AddFile ()
header.infotableofs = LONG(header.infotableofs);
length = header.numlumps * sizeof(filelump_t);
fileinfo = fileinfo_mem= (filelump_t *)CGDMalloc(length);
//Bfile_ReadFile_OS(gWADHandle, fileinfo, length,header.infotableofs);
Flash_ReadFile(fileinfo, length,header.infotableofs);
numlumps += header.numlumps;
@ -271,7 +263,6 @@ void W_ReadLump(int lump, void* dest )
}
l = lumpinfo+lump;
//c = Bfile_ReadFile_OS(gWADHandle,dest, l->size,l->position);
c = Flash_ReadFile(dest, l->size,l->position);