Skip to content

Commit 9c5b8ec

Browse files
committed
Start removing clib dependencies on Amiga, so we can easily try out newer GCC.
Various cleanups and improvements to malloc() debugging.
1 parent 0ad5bf2 commit 9c5b8ec

19 files changed

+603
-329
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ build/
44
src/sourcediffs.h
55
game
66
frontier
7-
SDL2.dll

CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ add_executable(
4747
)
4848

4949
# Define DEBUG c/c++ macro when compiling in debug mode
50-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
51-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
50+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG -DM_MEM_DEBUG")
51+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG -DM_MEM_DEBUG")
5252

5353
IF(MINGW)
5454
include_directories(C:/msys64/mingw32/include)
@@ -57,12 +57,12 @@ IF(MINGW)
5757
target_compile_options(fintro PRIVATE -mconsole)
5858
ENDIF()
5959

60-
target_compile_definitions(fintro PRIVATE -DUSE_SDL -DFINTRO_SCREEN_RES=3)
60+
target_compile_definitions(fintro PRIVATE -DUSE_SDL -DFINTRO_SCREEN_RES=3 -DM_CLIB_USE_ALIGNED)
6161
target_compile_options(fintro PRIVATE -ggdb)
6262

6363
# Point to your actual AmigaOS NDK
6464
target_compile_definitions(fintro-amiga PRIVATE -DAMIGA -DFINTRO_SCREEN_RES=2)
65-
target_include_directories(fintro-amiga PRIVATE ../../m68k-amigaos/ndk-include ../../m68k-amigaos/include)
65+
target_include_directories(fintro-amiga PRIVATE C:/Amiga/m68k-amigaos/ndk-include C:/Amiga/m68k-amigaos/include)
6666

6767
IF(APPLE)
6868
find_library(SDL2_LIBRARY SDL2 REQUIRED)

amiga/build-ami-gcc.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ set -x # echo on
4343
# -fomit-frame-pointer? - gives a 2% speedup and reduces exe size (less moves)
4444
# -fno-strict-aliasing ?
4545

46+
#DEBUG_FLAGS="-g -DM_MEM_DEBUG"
47+
OPT_FLAGS="-m68040 -O3 -mregparm=4 -fomit-frame-pointer -fweb -frename-registers"
48+
4649
GCC="/opt/amiga/bin/m68k-amigaos-gcc"
4750
VASM="/opt/amiga/bin/vasmm68k_mot"
48-
CCFLAGS="-m68040 -O3 -DFINTRO_SCREEN_RES=2 -DSURFACE_HEIGHT=360 $PROFILE_OPTS -mregparm=4 -fomit-frame-pointer -fweb -frename-registers"
51+
CCFLAGS="-DFINTRO_SCREEN_RES=2 -DSURFACE_HEIGHT=360 -DM_CLIB_DISABLE ${DEBUG_FLAGS} ${OPT_FLAGS} ${PROFILE_OPTS}"
4952

5053
# Embed version info into exe for custom profiling code
5154
BUILD_DATE=`date +"%Y/%m/%d %H:%M:%S"`
@@ -72,3 +75,4 @@ ${GCC} src/assets.c $CCFLAGS -I src -c -o build/assets.o
7275
# Link with -noixemul for smaller exe size, this disables the UNIX emulation layer, this can be buggy and we don't need
7376
# it anyway.
7477
${GCC} -lm -noixemul $CCFLAGS build/main.o build/mlib.o build/fmath.o build/fintro.o build/render.o build/assets.o build/audio.o ${POST_LDFLAGS} -o build/fintro-gcc
78+
/opt/amiga/bin/m68k-amigaos-objdump -S --disassemble build/fintro-gcc > build/fintro-gcc.asm

amiga/readme.txt

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
Frontier Elite II Intro Player
1+
Frontier: Elite II Intro Player
22
---
33

44
---
55
About
66
---
77

88
This is a RTG player for the Frontier intro, it's a full implementation of the
9-
Frontier Elite II renderer. The 3d models and music are read from the official
9+
Frontier: Elite II renderer. The 3d models and music are read from the official
1010
Frontier exe. You must already own Frontier or alternatively download the
1111
shareware version and send 5 pounds to Frontier Developments.
1212

@@ -23,7 +23,7 @@ Requirements
2323
Tested on FS-UAE / WinUAE and on Vampire V4, reported to work well on a V1200
2424
by Dr Procton!
2525

26-
Frontier Elite II exe must exist in same directory as 'fintro'. Use the exe
26+
'Frontier: Elite II' exe must exist in same directory as 'fintro'. Use the exe
2727
from the CD32 version or the final Elite Club shareware version. This can be
2828
downloaded from here:
2929

@@ -44,7 +44,7 @@ Controls
4444
Escape - Exit
4545
Left Mouse - Exit
4646

47-
Somewhat OS friendly, multitasking is still enabled. But stop any music players
47+
Somewhat OS friendly, multi-tasking is still enabled. But stop any music players
4848
before starting as it trounces audio registers.
4949

5050

@@ -70,6 +70,17 @@ Q. Could this be patched into original Amiga exe so we can play FE2 in RTG?
7070
A. Yes it could be, it would be work, but the original exe had jump tables for
7171
the renderer that this code mostly re-implements.
7272

73+
Q. It crashes on ApolloOS / AROS!
74+
A. For me too. There's at least two issues:
75+
- The software interrupt that generates the sound can get corrupted. This
76+
only happens if you run the startup sequence.
77+
- PC or status register gets corrupted during rendering, likely also an
78+
interrupt issue.
79+
Of course, it could be a bug on my side - but it's not for lack of looking -
80+
let me just say that!
81+
A solution would be to turn off the OS while running, and use HW interrrupts
82+
for the sound.
83+
7384

7485
---
7586
Feedback

data/model-overrides-be.dat

4.92 KB
Binary file not shown.

src/assets.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,24 @@ void Assets_LoadAmigaFiles(AssetsDataAmiga* assets, MReadFileRet* amigaExeData,
9393
assets->bitmapFontData = amigaExeData->data + 0x3b034;
9494

9595
assets->mainStrings = Assets_LoadStringPointers16BE(amigaExeData->data + 0x4c70d, 112);
96+
assets->mainStringsLen = 112;
9697
} else if (assetsRead == AssetsRead_Amiga_EliteClub || assetsRead == AssetsRead_Amiga_EliteClub2) {
9798
Assets_LoadModelPointers32BE(amigaExeData->data + 0x39ea0, 16, &assets->galmapModels);
9899
ARRAY_REWRITE_BE16(amigaExeData->data + 0x3a328, 0x18fc);
99100

100101
assets->bitmapFontData = amigaExeData->data + 0x3c9a6;
101102

102103
assets->mainStrings = Assets_LoadStringPointers16BE(amigaExeData->data + 0x4ff9e, 126);
104+
assets->mainStringsLen = 126;
103105
}
104106

105107
assets->assetsRead = assetsRead;
106108
}
107109

108110
void Assets_FreeAmigaFiles(AssetsDataAmiga* assets) {
109-
MFree(assets->mainExeData); assets->mainExeData = 0;
111+
MFree(assets->mainExeData, assets->mainExeSize); assets->mainExeData = 0;
110112
MArrayFree(assets->galmapModels);
111-
MFree(assets->mainStrings);
113+
MFree(assets->mainStrings, assets->mainStringsLen * sizeof(u8*));
112114
}
113115

114116
MReadFileRet Assets_LoadModelOverrides(const char* filePath, ModelsArray* modelsArray) {

src/assets.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ typedef struct sAssetsDataAmiga {
4949

5050
ModelsArray galmapModels;
5151
u8** mainStrings;
52+
u32 mainStringsLen;
5253
u8* bitmapFontData;
5354
AssetsReadEnum assetsRead;
5455
} AssetsDataAmiga;

src/audio.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static void Audio_CopyAndFixSamples(AudioContext* audio) {
212212
audio->samplesDataSize = 0x20000;
213213
#ifdef AMIGA
214214
// Allocate some chip ram that will be used to store the fixed up samples
215-
audio->samplesData = (u8*) AllocMem(audio->samplesDataSize, MEMF_PUBLIC | MEMF_CHIP);
215+
audio->samplesData = (u8*) AllocMem(audio->samplesDataSize, MEMF_CHIP);
216216
#else
217217
audio->samplesData = (u8*) MMalloc(audio->samplesDataSize);
218218
#endif
@@ -385,13 +385,13 @@ void Audio_Exit(AudioContext* audio) {
385385
SDL_CloseAudioDevice(audio->sdlAudioID); audio->sdlAudioID = 0;
386386

387387
if (audio->audioOutputBuffer != NULL) {
388-
MFree(audio->audioOutputBuffer); audio->audioOutputBuffer = NULL;
388+
MFree(audio->audioOutputBuffer, audio->audioOutputBufferSize); audio->audioOutputBuffer = NULL;
389389
}
390390

391391
for (int i = 0; i < AUDIO_SAMPLE_CACHE_SIZE; ++i) {
392392
SampleConvert* s = audio->sampleCache + i;
393393
if (s->sampleConverted != NULL) {
394-
MFree(s->sampleConverted); s->sampleConverted = NULL;
394+
MFree(s->sampleConverted, s->sampleConvertedBufferSize); s->sampleConverted = NULL;
395395
}
396396
}
397397
#endif
@@ -401,7 +401,7 @@ void Audio_Exit(AudioContext* audio) {
401401

402402
FreeMem(audio->samplesData, audio->samplesDataSize);
403403
#else
404-
MFree(audio->samplesData);
404+
MFree(audio->samplesData, audio->samplesDataSize);
405405
#endif
406406
audio->samplesData = 0;
407407
}
@@ -1182,7 +1182,8 @@ SampleConvert* Audio_ConvertSample(AudioContext* audio, ChannelRegisters* hw) {
11821182
}
11831183

11841184
if (sampleConvert->sampleConverted) {
1185-
MFree(sampleConvert->sampleConverted); sampleConvert->sampleConverted = NULL;
1185+
MFree(sampleConvert->sampleConverted, sampleConvert->sampleConvertedBufferSize);
1186+
sampleConvert->sampleConverted = NULL;
11861187
}
11871188

11881189
// period = ticks per second / samples per second
@@ -1195,14 +1196,16 @@ SampleConvert* Audio_ConvertSample(AudioContext* audio, ChannelRegisters* hw) {
11951196
AUDIO_S16, 1, PLAYBACK_FEQ);
11961197

11971198
cvt.len = hw->len * 2;
1198-
cvt.buf = (Uint8 *) MMalloc(cvt.len * cvt.len_mult);
1199+
int buffReqSize = cvt.len * cvt.len_mult;
1200+
cvt.buf = (Uint8 *) MMalloc(buffReqSize);
11991201

12001202
memcpy(cvt.buf, hw->pos, cvt.len);
12011203

12021204
int r = SDL_ConvertAudio(&cvt);
12031205
if (r == 0) {
12041206
sampleConvert->sampleConverted = (i16*)cvt.buf;
12051207
sampleConvert->sampleConvertedLen = cvt.len_cvt / 2;
1208+
sampleConvert->sampleConvertedBufferSize = buffReqSize;
12061209
sampleConvert->samplePtr = hw->pos;
12071210
sampleConvert->sampleLen = hw->len;
12081211
sampleConvert->period = hw->period;
@@ -1213,7 +1216,9 @@ SampleConvert* Audio_ConvertSample(AudioContext* audio, ChannelRegisters* hw) {
12131216
sampleConvert->sampleLen = 0;
12141217
sampleConvert->period = 0;
12151218
sampleConvert->sampleConvertedLen = 0;
1219+
sampleConvert->sampleConvertedBufferSize = 0;
12161220
sampleConvert->used = 0;
1221+
MFree(cvt.buf, buffReqSize);
12171222
}
12181223

12191224
if (sDebugLog) {
@@ -1260,7 +1265,7 @@ void Audio_Render(AudioContext* audio, int ticks, b32 output) {
12601265

12611266
if (output) {
12621267
if (audio->audioOutputBufferSize < outputSize) {
1263-
audio->audioOutputBuffer = (i16*)MRealloc(audio->audioOutputBuffer, outputSize);
1268+
audio->audioOutputBuffer = (i16*)MRealloc(audio->audioOutputBuffer, audio->audioOutputBufferSize, outputSize);
12641269
audio->audioOutputBufferSize = outputSize;
12651270
}
12661271
audio->audioOutputContentSize = outputSize;

src/audio.h

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ typedef struct sSampleConvert {
138138

139139
i16* sampleConverted;
140140
u32 sampleConvertedLen;
141+
u32 sampleConvertedBufferSize;
141142

142143
u32 cacheClock;
143144
u8 used;

src/fintro.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void Intro_InitPC(Intro* intro, SceneSetup* sceneSetup, AssetsDataPC* assetsData
6262
#endif
6363

6464
sceneSetup->moduleStrings = Assets_LoadStringPointers16LE(introFileData + 0x1d8, 33);
65+
sceneSetup->moduleStringNum = 33;
6566

6667
Assets_LoadModelPointers16LE(introFileData + 0x3b0, 120, &sceneSetup->assets.models);
6768
Assets_LoadModelPointers16LE(assetsData->vectorFontData, 15, &sceneSetup->assets.galmapModels);
@@ -151,6 +152,7 @@ void Intro_InitAmiga(Intro* intro, SceneSetup* sceneSetup, AssetsDataAmiga* asse
151152
(0x6a34 - (planetByteCodeStart + planetByteCodeLen)));
152153

153154
sceneSetup->moduleStrings = Assets_LoadStringPointers16BE(fileData + 0x76f88, 33);
155+
sceneSetup->moduleStringNum = 33 ;
154156
intro->creditsStringData = (u16*)(fileData + 0x81e16);
155157
} else if (assetsData->assetsRead == AssetsRead_Amiga_EliteClub) {
156158
Assets_LoadModelPointers32BE(fileData + 0x7ffdc, 120, &sceneSetup->assets.models);
@@ -162,6 +164,7 @@ void Intro_InitAmiga(Intro* intro, SceneSetup* sceneSetup, AssetsDataAmiga* asse
162164
ARRAY_REWRITE_BE16(fileData + 0x829f8, 28);
163165

164166
sceneSetup->moduleStrings = Assets_LoadStringPointers16BE(fileData + 0x7fe0e, 33);
167+
sceneSetup->moduleStringNum = 33;
165168
intro->creditsStringData = (u16*)(fileData + 0x8b774);
166169
} else if (assetsData->assetsRead == AssetsRead_Amiga_EliteClub2) {
167170
Assets_LoadModelPointers32BE(fileData + 0x7ffdc, 120, &sceneSetup->assets.models);
@@ -173,6 +176,7 @@ void Intro_InitAmiga(Intro* intro, SceneSetup* sceneSetup, AssetsDataAmiga* asse
173176
ARRAY_REWRITE_BE16(fileData + 0x829f8, 28);
174177

175178
sceneSetup->moduleStrings = Assets_LoadStringPointers16BE(fileData + 0x7fe0e, 33);
179+
sceneSetup->moduleStringNum = 33;
176180
intro->creditsStringData = (u16*)(fileData + 0x8b774);
177181
}
178182

@@ -230,19 +234,19 @@ void Intro_InitAmiga(Intro* intro, SceneSetup* sceneSetup, AssetsDataAmiga* asse
230234
#if FINTRO_SCREEN_RES == 3
231235
Image8Bit* upScaledImage = MArrayAddPtr(intro->imageStore.images);
232236
Render_ImageUpscale2x(&image, upScaledImage);
233-
MFree(image.data);
237+
MFree(image.data, image.w * image.h);
234238
#endif
235239
}
236240
}
237241

238242
void Intro_Free(Intro* intro, SceneSetup* sceneSetup) {
239243
MArrayFree(sceneSetup->assets.models);
240-
MFree(sceneSetup->moduleStrings);
244+
MFree(sceneSetup->moduleStrings, sceneSetup->moduleStringNum * sizeof(u8*));
241245

242246
Images8Bit* images = &intro->imageStore.images;
243247
for (int i = 0; i < MArraySize(*images); i++) {
244248
Image8Bit* image = MArrayGetPtr(*images, i);
245-
MFree(image->data);
249+
MFree(image->data, image->w * image->h);
246250
}
247251

248252
MArrayFree(*images);

0 commit comments

Comments
 (0)