CGDoom/cgdoom/cgdoom.c

657 lines
18 KiB
C
Raw Normal View History

2015-04-15 02:16:51 +02:00
#include "platform.h"
#include "os.h"
2021-08-14 11:55:20 +02:00
#include "cgdoom.h"
#include <stdlib.h>
2021-08-25 11:09:35 +02:00
#include "libprof.h"
2021-09-10 22:34:03 +02:00
#include "doomtype.h"
#include "m_misc.h"
#include "d_main.h"
2021-08-14 11:55:20 +02:00
#include "cgdoom-alloc.h"
#include "cgdoom-ui.h"
2015-04-15 02:16:51 +02:00
void *CGD_malloc(int size)
2015-04-15 02:16:51 +02:00
{
void *p = malloc(size);
2021-08-04 09:55:36 +02:00
if (!p)
I_Error ("CGD_malloc(%d) failure", size);
2021-08-04 09:55:36 +02:00
return p;
2015-04-15 02:16:51 +02:00
}
void *CGD_calloc(int size)
2015-04-15 02:16:51 +02:00
{
void *p = CGD_malloc(size);
if(p)
memset(p, 0, size);
2015-04-15 02:16:51 +02:00
return p;
}
void *CGD_realloc(void *p, int size)
2015-04-15 02:16:51 +02:00
{
if(p == NULL)
return malloc(size);
2015-04-15 02:16:51 +02:00
else
return realloc(p, size);
2015-04-15 02:16:51 +02:00
}
unsigned short *VRAM;
2021-09-19 20:19:30 +02:00
unsigned char *SaveVRAMBuffer;
unsigned char *SystemStack;
2015-04-15 02:16:51 +02:00
2021-09-19 19:16:50 +02:00
int strnicmp(const char *s1,const char *s2,int count)
2015-04-15 02:16:51 +02:00
{
2021-09-19 19:16:50 +02:00
for(int i = 0; i < count; i++) {
int c1 = (s1[i] >= 'a' && s1[i] <= 'z') ? s1[i] & ~0x20 : s1[i];
int c2 = (s2[i] >= 'a' && s2[i] <= 'z') ? s2[i] & ~0x20 : s2[i];
2015-04-15 02:16:51 +02:00
2021-09-19 19:16:50 +02:00
if(c1 != c2 || !c1 || !c2)
return c1 - c2;
2015-04-15 02:16:51 +02:00
}
2021-09-19 19:16:50 +02:00
return 0;
2015-04-15 02:16:51 +02:00
}
///////////////////////////////////////////////////////////////////////////////
// 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
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
// 4 kiB are used in general, a file might not start on the first sector of a
// cluster, and some sectors might also simply be dead.
//
// Although all 65536 Flash sectors are searched when needed, several
// heuristics are used:
// * The region between FLASH_FS_HINT and FLASH_END is searched first, since
// this is roughly where the filesystem is located.
// * All sectors aligned on 4-kiB boundaries between FLASH_FS_HINT and
// FLASH_END (of which there are 4096) are indexed by their first 4 bytes and
// binary searched for matches before anything else.
//
// See <src-cg/platform.h> for Flash traversal parameters.
///////////////////////////////////////////////////////////////////////////////
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
/* Index of most likely ROM sectors. */
typedef struct {
const void *sector;
uint32_t start_bytes;
} SectorIndexInfo;
2015-04-15 02:16:51 +02:00
2021-08-04 09:56:24 +02:00
/* Description of a fragment of a WAD file, in Flash. This is placed in PRAM0
to save RAM elsewhere. PRAM0 only supports 32-bit accesses, so we make this
structure a volatile bitfield where each entry has a 32-bit supporting type,
and build with -fstrict-volatile-bitfields which instructs GCC to use
accesses of the size of the supporting type without compromising on the
naming of the fields. */
typedef volatile struct {
/* Index of first sector in Flash (0...64k) */
2021-08-04 09:56:24 +02:00
uint32_t flash_address :16;
/* Corresponding sector in the WAD file */
2021-08-04 09:56:24 +02:00
uint32_t file_address :16;
} FileMappingItem;
2015-04-15 02:16:51 +02:00
2021-08-04 09:56:24 +02:00
/* Maximum number of fragments in the file map. Fragment information is stored
in PRAM0, so up to ~40k entries can be stored; this limit is mostly to catch
failures in fragment detection. (I have witnessed Ultimate Doom WADs with
up to 500 fragments.)*/
#define MAX_FRAGMENTS 2048
2015-04-15 02:16:51 +02:00
2021-08-04 09:56:24 +02:00
typedef struct {
/* Table of fragments */
FileMappingItem *mTable;
2015-04-15 02:16:51 +02:00
int miItemCount;
2021-08-04 09:56:24 +02:00
/* Length of the file */
int miTotalLength;
} FileMapping;
2015-04-15 02:16:51 +02:00
2021-08-02 21:51:08 +02:00
/* WAD file access method. */
static int gWADmethod = CGDOOM_WAD_MMAP;
/* File descriptor to WAD, used in Flash_ReadFile calls. (CGDOOM_WAD_BFILE) */
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
static int gWADfd = -1;
2021-08-02 21:51:08 +02:00
/* Memory map of WAD file. (CGDOOM_WAD_MMAP) */
2021-08-04 09:56:24 +02:00
static FileMapping gWADMap;
2021-08-02 21:51:08 +02:00
/* Index of most likely sectors for fragment search. (CGDOOM_WAD_MMAP) */
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
static SectorIndexInfo *gIndex = NULL;
/* Global options */
2021-09-10 22:38:37 +02:00
int CGD_TrustUnalignedLumps = 1;
int CGD_EnableDemos = 0;
int CGD_SingleEpisodeUltimate = 0;
int CGD_2MBLineMemory = 0;
int CGD_Frameskip = 1;
const char *CGD_WADFileName = NULL;
2021-08-02 21:11:13 +02:00
/* Delayed file accesses */
CGD_DelayedFileWrite CGD_DelayedSaves[6] = { 0 };
2021-08-25 11:09:35 +02:00
/* Performance counters */
struct CGD_Perf CGD_Perf;
/* Developer statistics */
struct CGD_Stats CGD_Stats;
2021-08-25 11:09:35 +02:00
#ifndef CG_EMULATOR
//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).
/* Fast memcmp() for 512-byte sectors. */
int CGD_sector_memcmp(const void *fast_ram, const void *rom, size_t _512);
/* Caching structure to read WAD files by larger chunks than Flash sectors. */
typedef struct {
int fd;
int size, offset;
char *data; /* of size FLASH_BFILE_UNIT */
} FileAccessCache;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
/* Read next sector from file, while caching into a buffer. */
const void *ReadNextSector(FileAccessCache *fc, int *size)
{
if(fc->size == 0)
{
fc->size = Bfile_ReadFile_OS(fc->fd, fc->data, FLASH_BFILE_UNIT, -1);
fc->offset = 0;
}
if(fc->size <= 0)
{
*size = -1;
return NULL;
}
*size = (fc->size < FLASH_PAGE_SIZE) ? fc->size : FLASH_PAGE_SIZE;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
fc->size -= *size;
const void *sector = fc->data + fc->offset;
fc->offset += *size;
return sector;
}
/* Compare two sectors in ROM for the index. */
int IndexCompareSectors(const void *p1, const void *p2)
{
const SectorIndexInfo *i1 = p1;
const SectorIndexInfo *i2 = p2;
return i1->start_bytes - i2->start_bytes;
}
/* Find all matching sectors in index (returns in-out interval). */
void IndexSearchSector(SectorIndexInfo *index, const void *buf, int *lo_ptr, int *hi_ptr)
{
uint32_t needle = *(const uint32_t *)buf;
*lo_ptr = *hi_ptr = -1;
/* Find the first occurrence, set it in *lo_ptr */
int lo=0, hi=FLASH_INDEX_SIZE;
while(lo < hi) {
int m = (lo + hi) / 2;
if(index[m].start_bytes < needle) lo = m + 1;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
else hi = m;
}
if(lo >= FLASH_INDEX_SIZE || index[lo].start_bytes != needle) return;
*lo_ptr = hi = lo;
/* Store last occurrence in *hi_ptr */
do hi++;
while(hi < FLASH_INDEX_SIZE && index[hi].start_bytes == needle);
*hi_ptr = hi;
}
/* Find a flash sector which contains the same data as buf. */
int FindSectorInFlash(const void *buf, int size)
{
typeof(&memcmp) memcmp_fun = &memcmp;
if(size == FLASH_PAGE_SIZE) memcmp_fun = &CGD_sector_memcmp;
CGD_Stats.WADFragments++;
2021-08-02 21:11:13 +02:00
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
#ifdef FLASH_INDEX
/* If an index has been built, search in it */
int lo, hi;
IndexSearchSector(gIndex, buf, &lo, &hi);
for(int i = lo; i < hi; i++) {
2021-08-02 21:11:13 +02:00
if(!memcmp_fun(buf, gIndex[i].sector, size)) {
CGD_Stats.WADIndexHits++;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
return (gIndex[i].sector - FLASH_START) / FLASH_PAGE_SIZE;
2021-08-02 21:11:13 +02:00
}
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
}
#endif
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
const void *sector = FLASH_FS_HINT;
do {
if(!memcmp_fun(buf, sector, size))
return (sector - FLASH_START) / FLASH_PAGE_SIZE;
sector += FLASH_PAGE_SIZE;
if(sector == FLASH_END)
sector = FLASH_START;
}
while(sector != FLASH_FS_HINT);
return -1;
}
int CreateFileMapping(int fd, FileMapping *pMap)
{
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
/* Cache accesses through a larger buffer */
FileAccessCache fc = {
.data = (void *)0xe5007000, /* XRAM */
.fd = fd
};
int iLength = 0;
int iFileSize = Bfile_GetFileSize_OS(fd);
2015-04-15 02:16:51 +02:00
pMap->miItemCount = 0;
pMap->miTotalLength = 0;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
int iLastProgress = 0;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
const void *pFileData = ReadNextSector(&fc, &iLength);
2015-04-15 02:16:51 +02:00
while(iLength > 0)
{
/* Don't show this too often, or it will eat several seconds */
if((pMap->miTotalLength - iLastProgress) * 20 > iFileSize) {
UI_ProgressBar(HEIGHT-10, pMap->miTotalLength, iFileSize);
iLastProgress = pMap->miTotalLength;
}
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
int iSectorID = FindSectorInFlash(pFileData, iLength);
if(iSectorID == -1)
return -2; // Page not found!
2015-04-15 02:16:51 +02:00
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
pMap->miItemCount++;
2015-04-15 02:16:51 +02:00
if(pMap->miItemCount >= MAX_FRAGMENTS)
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
return -3; // File too fragmented!
pMap->mTable[pMap->miItemCount-1].flash_address = iSectorID;
pMap->mTable[pMap->miItemCount-1].file_address = pMap->miTotalLength / FLASH_PAGE_SIZE;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
/* Look for consecutive sectors in the same fragment */
const void *pFragment = FLASH_START + (iSectorID * FLASH_PAGE_SIZE);
if(!CGD_Stats.WADLowestFragment || pFragment<CGD_Stats.WADLowestFragment)
CGD_Stats.WADLowestFragment = pFragment;
2021-08-02 21:51:08 +02:00
2015-04-15 02:16:51 +02:00
for(;;)
{
pMap->miTotalLength += iLength;
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
iSectorID++;
pFragment += FLASH_PAGE_SIZE;
2015-04-15 02:16:51 +02:00
if(iLength < FLASH_PAGE_SIZE)
{
//this was the last page
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
return pMap->miTotalLength;
2015-04-15 02:16:51 +02:00
}
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
pFileData = ReadNextSector(&fc, &iLength);
2015-04-15 02:16:51 +02:00
if(iLength <= 0)
break;
2021-09-15 08:18:09 +02:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overread"
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
if((iLength == FLASH_PAGE_SIZE)
? CGD_sector_memcmp(pFileData, pFragment, iLength)
: memcmp(pFileData, pFragment, iLength))
break;
2021-09-15 08:18:09 +02:00
#pragma GCC diagnostic pop
2015-04-15 02:16:51 +02:00
}
}
if(iLength < 0)
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
return -1;
return pMap->miTotalLength;
}
/* Find a fragment in the file map by binary search. */
int FindFragmentInMap(FileMapping *map, int sector_number)
{
int lo=0, hi=map->miItemCount;
while (lo < hi)
{
int m = (lo + hi) / 2;
if (map->mTable[m].file_address > sector_number) hi = m;
else lo = m + 1;
}
return hi - 1;
}
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
int FindInFlash(const void **buf, int size, int readpos)
2015-04-15 02:16:51 +02:00
{
2021-08-02 21:51:08 +02:00
if(gWADmethod == CGDOOM_WAD_BFILE)
return 0;
ASSERT(readpos >= 0);
2021-08-04 09:56:24 +02:00
if (readpos + size > gWADMap.miTotalLength)
return -1;
2021-08-04 09:56:24 +02:00
int iFragIndx = FindFragmentInMap(&gWADMap, readpos / FLASH_PAGE_SIZE);
int iFragOffset = gWADMap.mTable[iFragIndx].file_address * FLASH_PAGE_SIZE;
int iSubOffset = readpos - iFragOffset;
int iFragEnd;
2021-08-04 09:56:24 +02:00
if (iFragIndx+1 < gWADMap.miItemCount)
iFragEnd = gWADMap.mTable[iFragIndx+1].file_address * FLASH_PAGE_SIZE;
else
2021-08-04 09:56:24 +02:00
iFragEnd = gWADMap.miTotalLength;
2021-08-04 09:56:24 +02:00
*buf = FLASH_CACHED_START + (gWADMap.mTable[iFragIndx].flash_address * FLASH_PAGE_SIZE) + iSubOffset;
/* Return how many bytes can be read off the fragment (up to size). */
int iAvailableLen = (iFragEnd-readpos < size) ? iFragEnd-readpos : size;
ASSERT(iAvailableLen > 0);
return iAvailableLen;
2015-04-15 02:16:51 +02:00
}
static int FindZeroedMemory(void *start)
{
/* Look for zero-longwords every 16 bytes */
int size = 0;
/* Limit to 6 MB since the fx-CG 50 doesn't have any more memory and
anything after the RAM is likely to be zero non-writable */
while(size < CGDOOM_2MBLINEMEMORY_MAX && !*(uint32_t *)(start + size))
size += 16;
/* Round down to a multiple of 4096 */
return size & ~0xfff;
}
void abort(void){
int x=0,y=160;
PrintMini(&x,&y,"Abort called",0,0xFFFFFFFF,0,0,0xFFFF,0,1,0);
int key;
for(;;)
GetKey(&key);
}
#endif /* CG_EMULATOR */
2015-04-15 02:16:51 +02:00
int Flash_ReadFile(void *buf, int size, int readpos)
{
2021-08-02 21:51:08 +02:00
if(gWADmethod == CGDOOM_WAD_BFILE)
return Bfile_ReadFile_OS(gWADfd, buf, size, readpos);
Optimize loading speed (x2.7) and game speed (+35%) Loading is measured by RTC_GetTicks(). * Initial version: 9.8s This was a regression due to using 512-byte sectors instead of 4 kiB clusters as previously. * Do BFile reads of 4 kiB: 5.2s (-47%) Feels similar to original code, I'll take this as my baseline. * Test second half of Flash first: 3.6s (-31%) By reading from FLASH_FS_HINT to FLASH_END first many OS sectors can be skipped (without missing on other sectors just in case). * Load to XRAM instead or RAM with BFile The DMA is 10% slower to XRAM than to RAM, but this benefits memcmp() because of faster memory accesses through the operand bus. No effect at this point, but ends up saving 8% after memcmp is optimized. * Optimize memcmp for sectors: 3376 ms (-8%) The optimized memcmp uses word accesses for ROM (which is fastest), and weaves loop iterations to exploit superscalar parallelism. * Search sectors most likely to contain data first: 2744 ms (-19%) File fragments almost always start on 4-kiB boundaries between FLASH_FS_HINT and FLASH_END, so these are tested first. * Index most likely sectors, improve FLASH_FS_HINT: 2096 ms (-24%) Most likely sectors are indexed by first 4 bytes and binary searched, and a slightly larger region is considered for hints. The cache hits 119/129 fragments in my case. * Use optimized memcmp for consecutive fragments: 1408 ms (-33%) I only set it for the search of the first sector in each fragment and forgot to use it where it is really needed. x) Game speed is measured roughly by the time it takes to hit a wall by walking straight after spawning in Hangar. * Initial value: 4.4s * Use cached ROM when loading data from the WAD: 2.9s (-35%) Cached accesses are quite detrimental for sector search, I assume because everything is aligned like crazy, but it's still a major help when reading sequential data in real-time.
2021-07-28 22:51:03 +02:00
const void *pSrc;
2015-04-15 02:16:51 +02:00
int iRet = 0;
while(size >0)
{
int i = FindInFlash(&pSrc,size, readpos);
if(i<0) {
2021-09-15 08:11:17 +02:00
I_Error ("Flash_ReadFile: cannot find position %d (size %d)", readpos,
size);
2015-04-15 02:16:51 +02:00
return i;
}
memcpy(buf,pSrc,i);
2015-04-15 02:16:51 +02:00
buf = ((char*)buf)+i;
readpos +=i;
size -=i;
iRet +=i;
}
return iRet;
}
/* Find WAD files in the filesystem. */
int FindWADs(CGD_WADFileInfo *files, int max)
{
uint16_t path[32];
Bfile_FileInfo info;
int sd, rc, total=0;
rc = Bfile_FindFirst(u"\\\\fls0\\*.wad", &sd, path, &info);
while(rc != -16 && total < max)
{
memcpy(files[total].path, u"\\\\fls0\\", 14);
memcpy(files[total].path+7, path, 32*2);
Bfile_NameToStr_ncpy(files[total].name, path, 32);
files[total].size = info.fsize;
total++;
rc = Bfile_FindNext(sd, path, &info);
}
Bfile_FindClose(sd);
return total;
}
2021-09-19 17:33:33 +02:00
static void DelayedWriteFile(int i)
{
CGD_DelayedFileWrite const *dfw = &CGD_DelayedSaves[i];
uint16_t fc_path[100] = u"\\\\fls0\\";
int j=7, rc, fd;
for (int i = 0; dfw->filename[i]; i++)
fc_path[j++] = dfw->filename[i];
fc_path[j++] = 0x0000;
UI_DelayedWrites(CGD_DelayedSaves, 6, i, -1);
Bfile_DeleteEntry(fc_path);
2021-09-19 21:36:51 +02:00
rc = Bfile_CreateEntry_OS(fc_path, BFILE_CREATEMODE_FILE,
(size_t *)&dfw->size);
2021-09-19 17:33:33 +02:00
if (rc < 0) {
I_Error("Bfile_CreateEntry_OS(%s, %d bytes): %d", dfw->filename,
dfw->size, rc);
return;
}
2021-09-19 21:36:51 +02:00
fd = Bfile_OpenFile_OS(fc_path, BFILE_WRITE, 0);
2021-09-19 17:33:33 +02:00
if (fd < 0) {
I_Error("Bfile_OpenFile_OS(%s): %d", dfw->filename, fd);
return;
}
/* Write chunks of 4096 bytes and show progress in-between chunks */
int size = dfw->size;
const void *source = dfw->data;
while(size > 0) {
int chunk_size = (size >= 4096) ? 4096 : size;
Bfile_WriteFile_OS(fd, source, chunk_size);
source += chunk_size;
size -= chunk_size;
UI_DelayedWrites(CGD_DelayedSaves, 6, i, dfw->size - size);
}
Bfile_CloseFile_OS(fd);
}
2015-04-15 02:16:51 +02:00
///////////////////////////////////////////////////////////////////////////////////////////////////
2021-08-14 11:55:20 +02:00
int main(void)
{
2021-09-10 22:34:03 +02:00
extern boolean autostart;
int autostart_ = 0;
2021-08-14 11:55:20 +02:00
startmap = 1;
startepisode = 1;
VRAM = (unsigned short*)GetVRAMAddress();
2021-08-14 11:55:20 +02:00
int time, ms_index=0, ms_mmap=0;
EnableColor(1);
EnableStatusArea(3);
Bdisp_FrameAndColor(3, 16);
Bdisp_FrameAndColor(1, 0);
2021-08-25 11:09:35 +02:00
prof_init();
CGD_WADFileInfo wads[16];
2021-08-02 21:11:13 +02:00
int wad_count = FindWADs(wads, 16);
int dev_info = 0;
/* Allow the user to use memory past the 2 MB line on known OS versions */
int *enable_2MBline=NULL;
char const *osv = GetOSVersion();
2021-09-18 12:04:05 +02:00
if(!strncmp(osv, "03.", 3) && osv[3] <= '6') // 3.60 or earlier
enable_2MBline = &CGD_2MBLineMemory;
int choice = UI_Main(wads, wad_count, &dev_info, &gWADmethod,
&startmap, &startepisode, &CGD_TrustUnalignedLumps, &autostart_,
&CGD_EnableDemos, enable_2MBline);
if(choice < 0)
return 1;
2021-09-10 22:34:03 +02:00
autostart = autostart_;
/* Parameters unavailable on the SDL2 build */
#ifdef CG_EMULATOR
gWADmethod = CGDOOM_WAD_BFILE;
CGD_TrustUnalignedLumps = 0;
CGD_2MBLineMemory = 0;
#endif
/* Override version detection for single-episode Ultimate Doom WADs */
2021-09-19 19:16:50 +02:00
if (!strcmp(wads[choice].name, "doomu1.wad"))
CGD_SingleEpisodeUltimate = 1;
2021-09-19 19:16:50 +02:00
if (!strcmp(wads[choice].name, "doomu2.wad"))
CGD_SingleEpisodeUltimate = 2;
2021-09-19 19:16:50 +02:00
if (!strcmp(wads[choice].name, "doomu3.wad"))
CGD_SingleEpisodeUltimate = 3;
2021-09-19 19:16:50 +02:00
if (!strcmp(wads[choice].name, "doomu4.wad"))
CGD_SingleEpisodeUltimate = 4;
uintptr_t secondary_vram = ((uintptr_t)GetSecondaryVRAMAddress() | 3) + 1;
SaveVRAMBuffer = (void *)secondary_vram;
/* fx-CG 50 / Graph 90+E: RAM starts at 0x0c000000 in physical memory */
SystemStack = (void *)0xac0f0000;
/* Determine how much RAM is zeroed out at 2 MB */
if(CGD_2MBLineMemory)
CGD_2MBLineMemory = FindZeroedMemory((void *)0xac200000);
/* Remember WAD file name for saves and loads */
static char wad_name[32] = { 0 };
for (int i = 0; wads[choice].name[i] != '.'; i++)
wad_name[i] = wads[choice].name[i];
CGD_WADFileName = wad_name;
/* Setup access to WAD file */
2021-08-02 21:51:08 +02:00
if(gWADmethod == CGDOOM_WAD_BFILE)
{
gWADfd = Bfile_OpenFile_OS(wads[choice].path, 0, 0);
}
else
{
#ifdef FLASH_INDEX
2021-08-02 21:51:08 +02:00
/* Index most likely flash sectors into a sorted array, so that
sectors can be hit quickly. The index contains every sector
on a 4-kiB boundary (where fragments are most likely to
start) between FLASH_FS_HINT and FLASH_END. */
time = RTC_GetTicks();
2021-08-02 21:51:08 +02:00
gIndex = (void *)SystemStack;
for(int i = 0; i < FLASH_INDEX_SIZE; i++) {
SectorIndexInfo *info = &gIndex[i];
info->sector = FLASH_FS_HINT + (i * 4096);
info->start_bytes = *(const uint32_t *)info->sector;
}
qsort(gIndex, FLASH_INDEX_SIZE, sizeof *gIndex, IndexCompareSectors);
ms_index = (RTC_GetTicks() - time) * 8;
#endif /* FLASH_INDEX */
time = RTC_GetTicks();
2021-08-04 09:56:24 +02:00
gWADMap.mTable = (void *)0xfe200000; /* PRAM0 */
2021-08-02 21:51:08 +02:00
int fd = Bfile_OpenFile_OS(wads[choice].path, 0, 0);
2021-08-04 09:56:24 +02:00
int size = CreateFileMapping(fd, &gWADMap);
2021-08-02 21:51:08 +02:00
Bfile_CloseFile_OS(fd);
UI_ProgressBar(HEIGHT-10, 1, 1);
ms_mmap = (RTC_GetTicks() - time) * 8;
2021-08-02 21:51:08 +02:00
if(size == -1) {
I_Error ("File read error");
return 1;
}
else if(size == -2) {
I_Error ("Page not found");
return 1;
}
else if(size == -3) {
I_Error ("File too fragmented");
return 1;
}
else if(dev_info) {
Layout l;
Layout_Init(&l);
Bdisp_AllClr_VRAM();
Layout_CenteredText(&l, "Developer info");
Layout_Spacing(&l, 12);
Layout_Text(&l, "Fragments:", "%d", CGD_Stats.WADFragments);
Layout_Text(&l, "Index hits:", "%d (%d%%)", CGD_Stats.WADIndexHits,
(CGD_Stats.WADIndexHits * 100 / CGD_Stats.WADFragments));
2021-08-02 21:51:08 +02:00
Layout_Text(&l, "Lowest fragment:", "%p",
CGD_Stats.WADLowestFragment);
Layout_Spacing(&l, 12);
Layout_Text(&l, "Index build time:", "%d ms", ms_index);
Layout_Text(&l, "File mapping time:", "%d ms", ms_mmap);
Layout_Spacing(&l, 12);
Layout_Text(&l, "Memory beyond the 2MB line:", "%d kB",
CGD_2MBLineMemory >> 10);
2021-08-02 21:51:08 +02:00
Bdisp_PutDisp_DD();
int key;
GetKey(&key);
}
/* Initialize the PRAM allocator */
void *PRAM0_start = (void *)0xfe200000;
void *PRAM0_end = (void *)0xfe228000;
PRAM0_start += gWADMap.miItemCount * sizeof(FileMappingItem);
CGD_PRAM_Init(PRAM0_start, PRAM0_end);
2021-08-02 21:11:13 +02:00
}
memset(VRAM, 0, WIDTH*HEIGHT*2);
D_DoomMain();
2021-08-02 21:51:08 +02:00
if(gWADfd >= 0)
Bfile_CloseFile_OS(gWADfd);
int delayed_writes = sizeof(CGD_DelayedSaves) / sizeof(CGD_DelayedSaves[0]);
for(int i = 0; i < delayed_writes; i++) {
CGD_DelayedFileWrite *dfw = &CGD_DelayedSaves[i];
2021-09-19 17:33:33 +02:00
if(dfw->data != NULL)
DelayedWriteFile(i);
}
if(dev_info) {
Layout l;
Layout_Init(&l);
Bdisp_AllClr_VRAM();
Layout_CenteredText(&l, "Developer info");
Layout_Spacing(&l, 12);
Layout_Text(&l, "Memory allocated:", "%d kB",
CGD_Stats.MemoryAllocated >> 10);
Layout_Spacing(&l, 12);
Layout_Text(&l, "Lumps loaded:", "%d (%d kB)",
CGD_Stats.LumpsLoaded,
(int)(CGD_Stats.LumpsLoadedTotal >> 10));
Layout_Text(&l, "... of which unaligned:", "%d (%d kB)",
CGD_Stats.UnalignedLumpsLoaded,
(int)(CGD_Stats.UnalignedLumpsLoadedTotal >> 10));
Layout_Text(&l, "Lumps referenced:", "%d (%d kB)",
CGD_Stats.LumpsReferenced,
(int)(CGD_Stats.LumpsReferencedTotal >> 10));
Bdisp_FrameAndColor(3, 16);
Bdisp_FrameAndColor(1, 0);
Bdisp_PutDisp_DD();
while(PRGM_GetKey() != 0) {}
int key;
GetKey(&key);
}
2021-08-25 11:09:35 +02:00
prof_quit();
2021-07-27 11:34:35 +02:00
return 1;
2015-04-15 02:16:51 +02:00
}
//todo: wrapper pro (patch_t*), + flash