aboutsummaryrefslogtreecommitdiff
path: root/jindroush/chkexe/chkexe.cpp
blob: 4d651f0018929e9872073caa60dc9d712e7c15b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "pub.def"

#include "jintypes.h"

#include "at_dis.h"
#include "cfile.h"

#define AT_HEADER		0xFFFF

#define AT_RUNL		0x02E0
#define AT_RUNH		0x02E1
#define AT_RUNPREV	( AT_RUNL - 1 )

#define AT_INITL		0x02E2
#define AT_INITH		0x02E3
#define AT_INITPAST	( AT_INITH + 1 )

#define AT_HWSTART	0xD000
#define AT_HWEND		0xD7FF

typedef struct block
{
	struct block *pNext;
	WORD wBlkStart;
	WORD wBlkEnd;
} FILE_BLOCK;


BOOL BlockRead( CFile&, CFile& );
void BlockInsert( WORD, WORD );
void BlocksWrite( CFile& );
void PrintOffset( CFile& );
void BlockDisassemble( WORD, WORD );

FILE_BLOCK *g_pBlocks = NULL;
BYTE *g_pbtPool;
BOOL g_bHasRunAddress = FALSE;
LONG g_lFileLen;
BOOL g_bDisassemble = FALSE;
BOOL g_bSilent = FALSE;
BOOL g_bIsNotOk = TRUE;
BOOL g_bSplit = FALSE;
BOOL g_bOut = FALSE;

WORD g_iSplitNum = 0;

WORD g_wGapFill = 0;

char* g_szInfile = NULL;
char* g_szOutfile = NULL;

#define SHEADER PRG_NAME " v" PRG_VERSION " (c) " PRG_COPYRIGHT " " PRG_AUTHOR "\n"

#define HEADER SHEADER \
	PRG_DESC "\n" \
	"  Latest version can be found at " PRG_URL "\n" \
	"  Published under GPL. See GPL.TXT.\n\n"

#define USAGE HEADER "Usage:  " PRG_NAME " " PRG_USAGE

#include "switches.cpp"

int main (int argc,char *argv[])
{
	setbuf( stdout, NULL );
	setbuf( stderr, NULL );

	if ( !SWITCHES_Init( &argc, argv ) )
		return 1;

	if ( ( argc < 2 ) )
	{
		SWFN_HELP( USAGE );
		return 1;
	}

	fprintf( stderr, SHEADER );

	g_szInfile = argv[ 1 ];

	if ( argc == 3 )
		g_szOutfile = argv[ 2 ];

	CFile cfIn;
	CFile cfOut;

	if ( !cfIn.Open( g_szInfile ) )
	{
		fprintf( stderr, "Unable to open file\n\n%s", g_szInfile );
		return -1;
	}

	if ( AT_HEADER != cfIn.readLEw() )
	{
		printf( "%s: Not an Atari 8-bit binary load format file\n", g_szInfile );
		cfIn.Close();
		return -1;
	}

	if ( g_bSilent )
		printf( "%s: ", g_szInfile );
	else
		printf( "Binary file:  %s\n", g_szInfile );

	if ( argc > 2 )
	{
		if ( !cfOut.Create( g_szOutfile ) )
		{
			fprintf( stderr, "Unable to open file\n\n%s", g_szOutfile );
			return -1;
		}
		g_bOut = TRUE;
	}

	if ( g_bOut )
	{
		cfOut.writeLEw( AT_HEADER );
	}

	g_pbtPool = new BYTE [ 0x10000 ];
	memset( g_pbtPool,0, 0x10000 );

	if ( !g_pbtPool )
	{
		fprintf( stderr, "Unable to allocate memory for address space.\n" );
		return -1;
	}

	g_lFileLen = cfIn.GetLength();

	cfIn.Seek( 2, SEEK_SET );

	while ( cfIn.Tell() < g_lFileLen )
	{
		if ( ( g_bIsNotOk = !BlockRead( cfIn, cfOut ) ) )
			break;
	}

	cfIn.Close();

	if ( g_bHasRunAddress )
		BlockInsert( AT_RUNL, AT_RUNH );

	if ( g_bOut )
	{
		BlocksWrite( cfOut ); // Write all blocks since last init
		cfOut.Close();
	}

	delete [] g_pbtPool;

	if ( !g_bIsNotOk )
		printf( "Ok!\n" );

	return 0;
}


BOOL BlockRead( CFile& cfIn, CFile& cfOut )
{
	WORD wBlkStart;
	WORD wBlkEnd;
	WORD wBlkLen;

	do
	{
		PrintOffset( cfIn );

		if ( cfIn.Tell() >= g_lFileLen )
		{
			printf( "Error:  Unexpected end of file\n" );
			return FALSE;
		}

		wBlkStart = cfIn.readLEw();

		if ( wBlkStart == AT_HEADER )
		{
			if ( !g_bSilent )
				printf( "     :  Unexpected second 0xffff format header\n" );
		}

	} while ( wBlkStart == AT_HEADER );

	if ( cfIn.Tell() >= g_lFileLen )
	{
		printf( "Error:  Unexpected end of file in load range start address\n" );
		return FALSE;
	}

	wBlkEnd = cfIn.readLEw();

	wBlkLen = wBlkEnd - wBlkStart + 1;

	if ( cfIn.Tell() >= g_lFileLen )
	{
		printf( "Error:  Unexpected end of file in load range end address\n" );
		return FALSE;
	}

	if ( ( wBlkStart == wBlkEnd ) && ( ( wBlkStart >> 8 ) == ( wBlkStart & 0xFF ) ) )
	{
		WORD wLen = g_lFileLen - cfIn.Tell() + 4;

		//garbage was limited to be max $500 long, run address must exist!
		if ( ( wLen < 0x500 ) && ( wBlkStart ) )
		{
			printf( "Error:  Apparent garbage fill at end of file (%04X bytes)\n", wLen );
			return FALSE;
		}

	}

	if ( wBlkEnd < wBlkStart )
	{
		if ( !g_bSilent )
			printf( "Block:  %04X-%04X (%04X)\n", wBlkStart, wBlkEnd, wBlkLen );

		printf( "Error:  %ld bytes in file after invalid load range\n", g_lFileLen - cfIn.Tell() );
		return FALSE;
	}

	if ( g_lFileLen < cfIn.Tell() + wBlkLen )
	{
		if ( !g_bSilent )
			printf( "Block:  %04X-%04X (%04X)\n", wBlkStart, wBlkEnd, wBlkLen );

		printf( "Error:  Truncated file:  missing data in load block (%04lX bytes missing)\n", cfIn.Tell() + wBlkLen - g_lFileLen );
		return FALSE;
	}

	// Read in the data for this block
	cfIn.Read( &g_pbtPool[wBlkStart], wBlkLen );

	// Check for run address
	if ( wBlkStart <= AT_RUNH && wBlkStart + wBlkLen >= AT_RUNL )
	{
		if (wBlkStart==AT_RUNH || wBlkStart+wBlkLen==AT_RUNL)
		{
			if ( !g_bSilent )
				printf( "     :  Partial run address\n" );
		}
		if ( g_bHasRunAddress )
			if ( !g_bSilent )
				printf( "     :  Unexpected second run address\n" );

		g_bHasRunAddress = TRUE;

		if ( !g_bSilent )
			printf( "Run  :  %04X\n", g_pbtPool[ AT_RUNL ] + 256 * g_pbtPool[ AT_RUNH ] );

		if ( ( wBlkStart >= AT_RUNL ) && ( wBlkEnd <= AT_RUNH ) )
			return TRUE;

		// Other data in this block
		if ( wBlkStart == AT_RUNL || wBlkStart == AT_RUNH )
		{
			// Run and init in the same block--split
			wBlkStart = AT_INITL;
			wBlkLen = wBlkEnd - wBlkStart + 1;
		}
		else if ( wBlkEnd == AT_RUNL || wBlkEnd==AT_RUNH)
		{
			// other stuff before the run address--split
			wBlkEnd = AT_RUNPREV;
			wBlkLen = wBlkEnd - wBlkStart + 1;
		}
		else
		{
			// Run address in the middle of the block
			if ( !g_bSilent )
				printf( "Block:  %04X-%04X (%04X)\n", wBlkStart, AT_RUNPREV, AT_RUNPREV - wBlkStart + 1 );

			BlockInsert( wBlkStart, AT_RUNPREV );
			wBlkStart = AT_INITL;
			wBlkLen = wBlkEnd - wBlkStart + 1;
		}
	}

	// Check for init address
	// We know there's nothing before the address in the block,
	// as we would have split it off above as a run address.
	if ( wBlkStart <= AT_INITH && wBlkStart + wBlkLen >= AT_INITL )
	{
		if ( wBlkStart == AT_INITH || wBlkStart + wBlkLen == AT_INITL )
		{
			if ( !g_bSilent )
				printf( "     :  Partial init address\n" );
		}
		if ( !g_bSilent )
			printf( "Init :  %04X\n", g_pbtPool[ AT_INITL ] + 256 * g_pbtPool[ AT_INITH ] );

		// Other data in this block?
		if ( wBlkEnd > AT_INITH )
		{
			// More stuff past init--split
			if ( !g_bSilent )
				printf( "Block:  %04X-%04X (%04X)\n", AT_INITPAST, wBlkEnd, wBlkLen );

			BlockInsert( AT_INITPAST, wBlkEnd );
			wBlkEnd = AT_INITH;
			wBlkLen = wBlkEnd - wBlkStart + 1;
		}

		BlockInsert( wBlkStart, wBlkEnd );

		if ( g_bDisassemble )
			BlockDisassemble( wBlkStart, wBlkEnd );

		// Write everything out to avoid cross-init merges
		BlocksWrite( cfOut );
		return TRUE;
	}

	// Print data block load message
	if ( !g_bSilent )
		printf( "Block:  %04X-%04X (%04X)\n", wBlkStart, wBlkEnd, wBlkLen );

	if ( ( !wBlkStart ) && ( !wBlkEnd ) )
		return TRUE;

	BlockInsert( wBlkStart, wBlkEnd );

	if ( g_bDisassemble )
		BlockDisassemble( wBlkStart, wBlkEnd );

	return TRUE;
}


void BlockInsert(WORD wBlkStart,WORD wBlkEnd)
{
	if ( g_bSplit )
	{
		char szPom[ 255 ];
		sprintf( szPom, "blk%04X.blk", g_iSplitNum );

		CFile cf;
		if ( cf.Create( szPom ) )
		{
			cf.writeLEw( AT_HEADER );
			cf.writeLEw( wBlkStart );
			cf.writeLEw( wBlkEnd );
			cf.Write( g_pbtPool + wBlkStart, wBlkEnd - wBlkStart + 1 );
			cf.Close();
		}

		g_iSplitNum++;
	}

	FILE_BLOCK *b,*bp;

	bp = NULL; // previous block
	b = g_pBlocks;

	while( b )
	{
		//this fills the ultra short blks
		if ( ( ( b->wBlkEnd + 1 ) >= 0x400 ) && ( b->wBlkStart < 0xC000 ) )
		{
			if ( ( b->wBlkEnd + 1 ) < wBlkStart )
			{
				if ( ( wBlkStart - ( b->wBlkEnd + 1 ) ) < g_wGapFill )
				{
					if ( !g_bSilent )
						printf( "\t\tBlock merges with a previous block (gap fill)\n" );

					b->wBlkEnd = wBlkEnd;
					return;
				}
			}

		}

		// Check for merge
		if ( b->wBlkEnd + 1 == wBlkStart )
		{
			if ( !g_bSilent )
				printf( "\t\tBlock merges with a previous block\n" );

			b->wBlkEnd = wBlkEnd;
			return;
		}
		if ( b->wBlkStart - 1 == wBlkEnd )
		{
			if ( !g_bSilent )
				printf( "\t\tBlock merges with a previous block (unexpected ordering)\n" );

			b->wBlkStart = wBlkStart;
			return;
		}
		// Check for overlap
		if ( b->wBlkStart <= wBlkStart && wBlkEnd <= b->wBlkEnd )
		{
			if ( !g_bSilent )
				printf( "\t\tWarning:  Completely overlaps a previous block--merged\n" );

			return;
		}
		if (b->wBlkStart <= wBlkEnd && b->wBlkStart >= wBlkStart)
		{
			b->wBlkStart = wBlkStart;
			if (b->wBlkEnd<wBlkEnd) b->wBlkEnd = wBlkEnd;
			if ( !g_bSilent )
				printf( "\t\tWarning:  Partially overlaps a previous block--merged\n" );

			return;
		}
		if ( b->wBlkEnd <= wBlkEnd && b->wBlkEnd >= wBlkStart )
		{
			b->wBlkEnd = wBlkEnd;

			if ( b->wBlkStart > wBlkStart )
				b->wBlkStart = wBlkStart;

			if ( !g_bSilent )
				printf( "\t\tWarning:  Partially overlaps a previous block--merged\n" );

			return;
		}
		bp = b;
		b = b->pNext;
	}

	// Add this block to the end of the list
	b = new FILE_BLOCK;

	if( !b )
	{
		fprintf(stderr,"Unable to alloc memory--aborted\n" );
	}

	b->wBlkStart = wBlkStart;
	b->wBlkEnd = wBlkEnd;
	b->pNext = NULL;

	if (bp)
		bp->pNext = b;
	else
		g_pBlocks = b;
}


void BlocksWrite( CFile& cf )
{
	FILE_BLOCK* b;
	FILE_BLOCK* bp;

	b = g_pBlocks;
	while (b)
	{
		if ( g_bOut)
		{
			cf.writeLEw( b->wBlkStart );
			cf.writeLEw( b->wBlkEnd );

			cf.Write( &g_pbtPool[ b->wBlkStart ], b->wBlkEnd - b->wBlkStart + 1 );
		}
		bp = b; // To allow free call after last use
		b = b->pNext;
		delete bp;
	}
	g_pBlocks = NULL;
}


void BlockDisassemble(WORD wBlkStart,WORD wBlkEnd)
{
	//don't disassemble run & init block
	if ( ( wBlkStart >= AT_RUNL )	&& ( wBlkEnd <= AT_INITH ) )
		goto BDEND;

	//don't disassemble hw addresses
	if ( ( wBlkStart >= AT_HWSTART ) && ( wBlkEnd <= AT_HWEND ) )
		goto BDEND;

	OutputBlockDiss( g_pbtPool + wBlkStart, wBlkStart, wBlkEnd );

BDEND:
	printf( "\n" );
	return;
}

void PrintOffset( CFile& cf )
{
	if ( !g_bSilent )
		printf( "[%04lX] ", cf.Tell() );
}