Some cleaning and slight emulator progress

This commit is contained in:
Lephenixnoir 2021-09-19 23:34:30 +02:00
parent 3df65755bc
commit 5f9e27b07a
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
19 changed files with 146 additions and 225 deletions

View File

@ -499,7 +499,7 @@ void AM_loadPics(void)
char sBuf[8];
for (i=0;i<10;i++)
{
CGDAppendNum09("AMMNUM",i,sBuf);
sprintf (sBuf, "AMMNUM%d", i);
marknums[i] =W_CacheLumpNamePatch(sBuf, PU_STATIC);
}
}

View File

@ -1,7 +1,6 @@
#ifndef CGDOOM_ALLOC_H
#define CGDOOM_ALLOC_H
#include "platform.h"
#include <stddef.h>
/* The simple CGDoom allocator from SPU2 memory

View File

@ -32,7 +32,7 @@ int CGD_Frag_Map(const char *lumpname, CGD_Frag *frag)
return 0;
}
byte CGD_Frag_u8(CGD_Frag const *frag, int offset)
uint8_t CGD_Frag_u8(CGD_Frag const *frag, int offset)
{
int f = 0;
while(frag->size[f] != 0 && offset >= frag->size[f]) {
@ -43,24 +43,24 @@ byte CGD_Frag_u8(CGD_Frag const *frag, int offset)
if(frag->size[f] == 0)
return 0; /* read out-of-bounds */
const byte *data = frag->data[f];
const uint8_t *data = frag->data[f];
return data[offset];
}
short CGD_Frag_i16LE(CGD_Frag const *frag, int offset)
{
byte b1 = CGD_Frag_u8(frag, offset);
byte b2 = CGD_Frag_u8(frag, offset + 1);
uint8_t b1 = CGD_Frag_u8(frag, offset);
uint8_t b2 = CGD_Frag_u8(frag, offset + 1);
return (b2 << 8) | b1;
}
int CGD_Frag_i32LE(CGD_Frag const *frag, int offset)
{
byte b1 = CGD_Frag_u8(frag, offset);
byte b2 = CGD_Frag_u8(frag, offset + 1);
byte b3 = CGD_Frag_u8(frag, offset + 2);
byte b4 = CGD_Frag_u8(frag, offset + 3);
uint8_t b1 = CGD_Frag_u8(frag, offset);
uint8_t b2 = CGD_Frag_u8(frag, offset + 1);
uint8_t b3 = CGD_Frag_u8(frag, offset + 2);
uint8_t b4 = CGD_Frag_u8(frag, offset + 3);
return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
}

View File

@ -1,7 +1,7 @@
#ifndef CGDOOM_FRAG_H
#define CGDOOM_FRAG_H
#include "doomtype.h"
#include <stdint.h>
/* CGDoom's abstracted access to fragmented lumps
@ -37,7 +37,7 @@ typedef struct {
int CGD_Frag_Map(const char *lumpname, CGD_Frag *frag);
/* Read different types within the lump */
byte CGD_Frag_u8(CGD_Frag const *frag, int offset);
uint8_t CGD_Frag_u8(CGD_Frag const *frag, int offset);
short CGD_Frag_i16LE(CGD_Frag const *frag, int offset);
int CGD_Frag_i32LE(CGD_Frag const *frag, int offset);

View File

@ -1,7 +1,6 @@
#ifndef CGDOOM_UI_H
#define CGDOOM_UI_H
#include "platform.h"
#include "cgdoom.h"
#include <stdarg.h>
#include <stdint.h>

View File

@ -6,127 +6,39 @@
#include "doomtype.h"
#include "m_misc.h"
#include "d_main.h"
#ifndef CG_EMULATOR
# include "cgdoom-alloc.h"
# include "cgdoom-ui.h"
#endif
#include "cgdoom-alloc.h"
#include "cgdoom-ui.h"
void * CGDMalloc(int iSize)
void *CGD_malloc(int size)
{
void *p = malloc(iSize);
void *p = malloc(size);
if (!p)
I_Error ("CGDMalloc failure");
I_Error ("CGD_malloc(%d) failure", size);
return p;
}
void * CGDCalloc(int iSize)
void *CGD_calloc(int size)
{
void *p = CGDMalloc(iSize);
if(p != NULL)
{
memset(p,0,iSize);
}
void *p = CGD_malloc(size);
if(p)
memset(p, 0, size);
return p;
}
void * CGDRealloc (void *p, int iSize)
void *CGD_realloc(void *p, int size)
{
if(p == NULL)
return malloc(iSize);
return malloc(size);
else
return realloc(p,iSize);
return realloc(p, size);
}
unsigned short *VRAM;
unsigned char *SaveVRAMBuffer;
unsigned char *SystemStack;
void CGDAppendNum09(const char *pszText,int iNum,char *pszBuf)
{
int i = 0;
while(pszText[i])
{
pszBuf[i] = pszText[i];
i++;
}
ASSERT(iNum < 10);
ASSERT(iNum >= 0);
pszBuf[i] = (char)('0'+iNum);
pszBuf[i+1] = 0;
}
void CGDAppendNum0_999(const char *pszText,int iNum,int iMinDigits,char *pszBuf)
{
int i = 0;
int z = 0;
while(pszText[i])
{
pszBuf[i] = pszText[i];
i++;
}
ASSERT(iNum < 1000000);
ASSERT(iNum >= 0);
if((iNum > 999999) || (iMinDigits>6))
{
pszBuf[i] = (char)('0'+(iNum / 1000000));
iNum %= 1000000;
i++;
z = 1;
}
if(z || (iNum > 99990) || (iMinDigits>5))
{
pszBuf[i] = (char)('0'+(iNum / 100000));
iNum %= 100000;
i++;
z = 1;
}
if(z || (iNum > 9999) || (iMinDigits>4))
{
pszBuf[i] = (char)('0'+(iNum / 10000));
iNum %= 10000;
i++;
z = 1;
}
if(z || (iNum > 999) || (iMinDigits>3))
{
pszBuf[i] = (char)('0'+(iNum / 1000));
iNum %= 1000;
i++;
z = 1;
}
if(z || (iNum > 99) || (iMinDigits>2))
{
pszBuf[i] = (char)('0'+(iNum / 100));
iNum %= 100;
i++;
z = 1;
}
if(z || (iNum > 9) || (iMinDigits>1))
{
pszBuf[i] = (char)('0'+(iNum / 10));
iNum %= 10;
i++;
}
pszBuf[i] = (char)('0'+iNum);
pszBuf[i+1] = 0;
}
void CGDAppendHex32(const char *pszText,int iNum,int iDigits,char *pszBuf)
{
strcpy(pszBuf, pszText);
pszBuf += strlen(pszText);
for(int i = 0; i < iDigits; i++)
{
int c = (iNum >> (i * 4)) & 0xf;
c = c + '0' + 7 * (c > 9);
pszBuf[iDigits-i-1] = c;
}
pszBuf[iDigits] = 0;
}
int strnicmp(const char *s1,const char *s2,int count)
{
for(int i = 0; i < count; i++) {
@ -275,7 +187,7 @@ const void *ReadNextSector(FileAccessCache *fc, int *size)
return NULL;
}
*size = min(fc->size, FLASH_PAGE_SIZE);
*size = (fc->size < FLASH_PAGE_SIZE) ? fc->size : FLASH_PAGE_SIZE;
fc->size -= *size;
const void *sector = fc->data + fc->offset;
fc->offset += *size;
@ -454,7 +366,8 @@ int FindInFlash(const void **buf, int size, int readpos)
*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 = min(iFragEnd - readpos, size);
int iAvailableLen = (iFragEnd-readpos < size) ? iFragEnd-readpos : size;
ASSERT(iAvailableLen > 0);
return iAvailableLen;
}
@ -483,16 +396,6 @@ int Flash_ReadFile(void *buf, int size, int readpos)
return iRet;
}
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 */
static int FindZeroedMemory(void *start)
{
/* Look for zero-longwords every 16 bytes */
@ -507,6 +410,16 @@ static int FindZeroedMemory(void *start)
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 */
static void DelayedWriteFile(int i)
{
CGD_DelayedFileWrite const *dfw = &CGD_DelayedSaves[i];

View File

@ -1,10 +1,10 @@
#ifndef CGDOOM_H
#define CGDOOM_H
// CGDoom-specific definitions that cover both calculator and native builds.
#include <stdint.h>
#include "libprof.h"
#include <stdint.h>
// CGDoom-specific definitions that cover both calculator and native builds.
/* VRAM pointer and size */
extern uint16_t *VRAM;
@ -27,6 +27,16 @@ typedef struct
int size;
} CGD_DelayedFileWrite;
/* File access method (numbering is used in checkbox; keep as is) */
enum {
/* Use BFile (100% accurate but slows down the game quite a bit because of
reads happening all the time; mostly a good reference for testing) */
CGDOOM_WAD_BFILE = 0,
/* Search fragments in physical ROM when loading the game, and copy by hand
from ROM to RAM during accesses (much faster) */
CGDOOM_WAD_MMAP = 1,
};
/* Delay file saves until exit to avoid quitting immediately */
#define CGDOOM_DELAY_SAVES

View File

@ -629,8 +629,7 @@ void F_BunnyScroll (void)
laststage = stage;
}
//sprintf (name,"END%i",stage);
CGDAppendNum09("END",stage,name);
sprintf (name,"END%d",stage);
V_DrawPatch ((SCREENWIDTH-13*8)/2, (SCREENHEIGHT-8*8)/2,0, (const patch_t*)W_CacheLumpNameConst(name,PU_CACHE));
}

View File

@ -193,7 +193,7 @@ void HU_Init(void)
j = HU_FONTSTART;
for (i=0;i<HU_FONTSIZE;i++)
{
CGDAppendNum0_999("STCFN0",j++,2,buffer);
sprintf(buffer, "STCFN0%d", j++);
hu_font[i] = (const patch_t *) W_CacheLumpNameConst(buffer, PU_STATIC);
}

View File

@ -3,23 +3,16 @@
#define SYSTEM_STACK_SAFE (64*1024)
#define USER_STACK_SAFE (32*1024)
// SaveVRAMBuffer 165888 bytes
#define SAVE_VRAM_SIZE (WIDTH*HEIGHT*2-3)
// SaveVRAMBuffer (384*216*2 bytes)
extern unsigned char *SaveVRAMBuffer;
// system stack (512 kB).
#define SYSTEM_STACK_SIZE (512*1024-SYSTEM_STACK_SAFE)
extern unsigned char *SystemStack;
extern unsigned short *VRAM;
void * CGDMalloc(int iSize);
void * CGDCalloc(int iSize);
void * CGDRealloc (void *p, int iSize);
void D_DoomMain();
void CGDAppendNum09(const char *pszText,int iNum,char *pszBuf);
void CGDAppendNum0_999(const char *pszText,int iNum,int iMinDigits,char *pszBuf);
void CGDAppendHex32(const char *pszText,int iNum, int iDigits,char *pszBuf);
void *CGD_malloc(int size);
void *CGD_calloc(int size);
void *CGD_realloc(void *p, int size);
int abs(int x);
int strnicmp (const char*s1,const char*s2,int iLen);
@ -30,9 +23,3 @@ void I_Error (const char *error, ...);
int FindInFlash(const void **buf, int size, int readpos);
//direct read from flash
int Flash_ReadFile(void *buf, int size, int readpos);
#define min(x,y) ({ \
__auto_type __x = (x); \
__auto_type __y = (y); \
__x < __y ? __x : __y; \
})

View File

@ -711,7 +711,7 @@ void P_GroupLines (void)
void P_SetupLevel ( int episode, int map, int playermask)
{
//int i;
char lumpname[9];
char lumpname[16];
int lumpnum;
totalkills = playermask = totalitems = totalsecret = wminfo.maxfrags = 0;
@ -741,7 +741,10 @@ void P_SetupLevel ( int episode, int map, int playermask)
// find map name
if ( gamemode == commercial)
{
CGDAppendNum0_999("map",map,2,lumpname);
if (map<10)
sprintf (lumpname,"map0%i", map);
else
sprintf (lumpname,"map%i", map);
}
else
{

View File

@ -442,7 +442,7 @@ void R_InitTextures (void)
names = (const char *)W_CacheLumpNameConst ("PNAMES", PU_STATIC);
nummappatches = INTFromBytes((const byte*)names );
name_p = names+4;
patchlookup = (int*)CGDMalloc(nummappatches*sizeof(*patchlookup));
patchlookup = (int*)CGD_malloc(nummappatches*sizeof(*patchlookup));
for (i=0 ; i<nummappatches ; i++)
{
@ -776,7 +776,7 @@ void R_PrecacheLevel (void)
// Precache flats.
//flatpresent = alloca(numflats);
flatpresent = (char*)CGDMalloc(numflats);
flatpresent = (char*)CGD_malloc(numflats);
memset (flatpresent,0,numflats);
for (i=0 ; i<numsectors ; i++)
@ -799,7 +799,7 @@ void R_PrecacheLevel (void)
// Precache textures.
//texturepresent = alloca(numtextures);
texturepresent = (char*)CGDMalloc(numtextures);
texturepresent = (char*)CGD_malloc(numtextures);
memset (texturepresent,0, numtextures);
for (i=0 ; i<numsides ; i++)
@ -835,7 +835,7 @@ void R_PrecacheLevel (void)
// Precache sprites.
//spritepresent = alloca(numsprites);
spritepresent = (char*)CGDMalloc (numsprites);
spritepresent = (char*)CGD_malloc (numsprites);
memset (spritepresent,0, numsprites);
for (th = thinkercap.next ; th != &thinkercap ; th=th->next)

View File

@ -940,12 +940,10 @@ void ST_loadGraphics(void)
// Load the numbers, tall and short
for (i=0;i<10;i++)
{
//sprintf(namebuf, "STTNUM%d", i);
CGDAppendNum09("STTNUM",i,namebuf);
sprintf(namebuf, "STTNUM%d", i);
tallnum[i] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
//sprintf(namebuf, "STYSNUM%d", i);
CGDAppendNum09("STYSNUM",i,namebuf);
sprintf(namebuf, "STYSNUM%d", i);
shortnum[i] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
}
@ -977,8 +975,7 @@ void ST_loadGraphics(void)
arms_5 = shortnum[7];
// face backgrounds for different color players
//sprintf(namebuf, "STFB%d", consoleplayer);
CGDAppendNum09("STFB",consoleplayer,namebuf);
sprintf(namebuf, "STFB%d", consoleplayer);
if (netgame)
{
faceback = W_CacheLumpNamePatch(namebuf, PU_STATIC);
@ -993,26 +990,18 @@ void ST_loadGraphics(void)
{
for (j=0;j<ST_NUMSTRAIGHTFACES;j++)
{
//sprintf(namebuf, "STFST%d%d", i, j);
//CGD: use big, really big hammer
CGDAppendNum0_999("STFST",i*10+j,2,namebuf);
sprintf(namebuf, "STFST%d%d", i, j);
faces[facenum++] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
}
//CGD: now the previous, smaller one
//sprintf(namebuf, "STFTR%d0", i); // turn right
CGDAppendNum0_999("STFTR",i*10,2,namebuf);
sprintf(namebuf, "STFTR%d0", i); // turn right
faces[facenum++] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
//sprintf(namebuf, "STFTL%d0", i); // turn left
CGDAppendNum0_999("STFTL",i*10,2,namebuf);
sprintf(namebuf, "STFTL%d0", i); // turn left
faces[facenum++] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
//sprintf(namebuf, "STFOUCH%d", i); // ouch!
CGDAppendNum09("STFOUCH",i,namebuf);
sprintf(namebuf, "STFOUCH%d", i); // ouch!
faces[facenum++] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
//sprintf(namebuf, "STFEVL%d", i); // evil grin ;)
CGDAppendNum09("STFEVL",i,namebuf);
sprintf(namebuf, "STFEVL%d", i); // evil grin ;)
faces[facenum++] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
//sprintf(namebuf, "STFKILL%d", i); // pissed off
CGDAppendNum09("STFKILL",i,namebuf);
sprintf(namebuf, "STFKILL%d", i); // pissed off
faces[facenum++] = W_CacheLumpNamePatch(namebuf, PU_STATIC);
}
faces[facenum++] = W_CacheLumpNamePatch("STFGOD0", PU_STATIC);

View File

@ -107,25 +107,12 @@ static int W_AddFile ()
header.numlumps = LONG(header.numlumps);
header.infotableofs = LONG(header.infotableofs);
length = header.numlumps * sizeof(filelump_t);
fileinfo = fileinfo_mem= (filelump_t *)CGDMalloc(length);
fileinfo = fileinfo_mem = CGD_malloc(length);
Flash_ReadFile(fileinfo, length,header.infotableofs);
numlumps += header.numlumps;
// Fill in lumpinfo
// TODO: Actual realloc
if (!lumpinfo)
{
lumpinfo = (lumpinfo_t *)Z_Malloc (numlumps*sizeof(lumpinfo_t), PU_STATIC, 0);
}
else
{
I_Error ("realloc lumpinfo!");
lumpinfo = (lumpinfo_t *)CGDRealloc (lumpinfo, numlumps*sizeof(lumpinfo_t));
}
if (!lumpinfo)
I_Error ("CGDrealloc(lumpinfo) failed (%d lumps, %d bytes)", numlumps,
numlumps * sizeof(lumpinfo_t));
// Fill in lumpinfo (when handling multiples files, should realloc)
lumpinfo = (lumpinfo_t *)Z_Malloc (numlumps*sizeof(lumpinfo_t), PU_STATIC, 0);
lump_p = &lumpinfo[startlump];
@ -138,7 +125,6 @@ static int W_AddFile ()
strncpy (lump_p->name, fileinfo->name, 8);
}
//Nspire has no alloca() function for temporary memory, so we have to use malloc() and free() to compensate
free(fileinfo_mem);
return 1; // CX port
}

View File

@ -968,16 +968,13 @@ static void WI_loadData(void)
{
int i;
int j;
char name[9];
char name[17];
wi_stuff_anim_t* a;
if (gamemode == commercial)
strcpy(name, "INTERPIC");
else
{
//sprintf(name, "WIMAP%d", wbs->epsd);
CGDAppendNum09("WIMAP",wbs->epsd,name);
}
sprintf(name, "WIMAP%d", wbs->epsd);
if ( gamemode == retail )
{
@ -1006,10 +1003,8 @@ static void WI_loadData(void)
NUMCMAPS = 32;
lnames = (const patch_t **) Z_Malloc(sizeof(patch_t*) * NUMCMAPS,PU_STATIC, 0);
for (i=0 ; i<NUMCMAPS ; i++)
{
// sprintf(name, "CWILV%2.2d", i);
//sprintf(name, "CWILV%02d", i); // CX port
CGDAppendNum0_999("CWILV",i,2,name);
{
sprintf(name, "CWILV%02d", i);
lnames[i] = W_CacheLumpNamePatch(name, PU_STATIC);
}
}
@ -1018,8 +1013,7 @@ static void WI_loadData(void)
lnames = (const patch_t **) Z_Malloc(sizeof(patch_t*) * NUMMAPS, PU_STATIC, 0);
for (i=0 ; i<NUMMAPS ; i++)
{
//sprintf(name, "WILV%d%d", wbs->epsd, i);
CGDAppendNum0_999("WILV",wbs->epsd*10+i,2,name);
sprintf(name, "WILV%d%d", wbs->epsd, i);
lnames[i] = W_CacheLumpNamePatch(name, PU_STATIC);
}
@ -1048,10 +1042,7 @@ static void WI_loadData(void)
// MONDO HACK!
if (wbs->epsd != 1 || j != 8)
{
CGDAppendNum0_999("WIA",wbs->epsd,1,name);
CGDAppendNum0_999(name,j,2,name);
CGDAppendNum09(name,0,name);
CGDAppendNum0_999(name,i,1,name);
sprintf(name, "WIA%d%02d%02d", wbs->epsd, j, i);
a->p[i] = W_CacheLumpNamePatch(name, PU_STATIC);
}
else
@ -1070,8 +1061,7 @@ static void WI_loadData(void)
for (i=0;i<10;i++)
{
// numbers 0-9
//sprintf(name, "WINUM%d", i);
CGDAppendNum09("WINUM",i,name);
sprintf(name, "WINUM%d", i);
num[i] = W_CacheLumpNamePatch(name, PU_STATIC);
}

View File

@ -6,16 +6,6 @@
// WAD file access in Flash
//---
/* File access method (numbering is used in checkbox; keep as is) */
enum {
/* Use BFile (100% accurate but slows down the game quite a bit because of
reads happening all the time; mostly a good reference for testing) */
CGDOOM_WAD_BFILE = 0,
/* Search fragments in physical ROM when loading the game, and copy by hand
from ROM to RAM during accesses (much faster) */
CGDOOM_WAD_MMAP = 1,
};
/* Settings for file mappings: traverse the whole 32-MiB Flash */
#define FLASH_START ((const void *)0xA0000000)
#define FLASH_END ((const void *)0xA2000000)

13
src-sdl2/libprof.c Normal file
View File

@ -0,0 +1,13 @@
#include "libprof.h"
uint64_t prof_current_time(void)
{
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
return (uint64_t)tp.tv_sec * 1000000000ull + tp.tv_nsec;
}
uint32_t prof_time(prof_t prof)
{
return -prof.elapsed / 1000;
}

42
src-sdl2/libprof.h Normal file
View File

@ -0,0 +1,42 @@
//---
// This is a dummy version of libprof for Linux with clock_gettime(2).
//---
#ifndef LIBPROF_LIBPROF
#define LIBPROF_LIBPROF
#include <inttypes.h>
#include <time.h>
#define prof_init()
#define prof_quit()
typedef struct prof_t
{
int64_t elapsed; /* in nanoseconds, generally */
uint32_t rec;
} prof_t;
#define prof_make() ((prof_t){ 0, 0 })
uint64_t prof_current_time(void);
#define prof_enter(prof) { \
if(!prof.rec++) prof.elapsed += prof_current_time(); \
}
#define prof_leave(prof) { \
if(!--prof.rec) prof.elapsed -= prof_current_time(); \
}
#define prof_exec(code) ({ \
prof_t prof = prof_make(); \
prof_enter(prof); \
code; \
prof_leave(prof); \
prof_time(prof); \
})
uint32_t prof_time(prof_t prof);
#endif /* LIBPROF_LIBPROF */

View File

@ -1,9 +1,15 @@
/* <platform.h> file for Linux/SDL2 */
#ifndef PLATFORM_H
#define PLATFORM_H
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdint.h>
#include "keyboard.hpp"
#include "../../../include/fxcg/file.h"
/* On the emulator all lumps are allocated with malloc(), so there are not
pointers-to-flash to account for when freeing. */
#define PTR_TO_FLASH(p) 0
@ -46,13 +52,8 @@ void assert(int iLine,const char *pszFilename,const char *pszAassert);
#define ASSERT(x) if(!(x)){assert(__LINE__,__FILE__,#x);}
int Bfile_OpenFile_OS( const unsigned short*filename, int mode );
int Bfile_SeekFile_OS( int HANDLE, int pos );
int Bfile_ReadFile_OS( int HANDLE, void *buf, int size, int readpos );
int Bfile_CloseFile_OS( int HANDLE );
extern const unsigned char *gpcFlashBuffer;
void InitFlashSimu(const char *filename);
#endif /* PLATFORM_H */