aboutsummaryrefslogtreecommitdiff
path: root/jindroush/chkrom/chkrom.cpp
blob: 32c8979623b85e468e27ec323e61e9fb668db9fd (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
//    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 <unistd.h>
#include "pub.def"
#include "jintypes.h"
#include "at_dis.h"

BOOL g_bDisassemble = FALSE;

int hIn = -1;

char* g_szInfile = 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 ];

	hIn = open( g_szInfile, O_RDONLY | O_BINARY );

	if ( hIn == -1 )
	{
		fprintf(stderr, "Unable to open file\n\n%s", g_szInfile );
		exit( 1 );
	}

	LONG lFileLen = lseek( hIn, 0, SEEK_END );

	lseek( hIn, 0, SEEK_SET );

	if ( ( lFileLen != 0x2000 ) && ( lFileLen != 0x4000 ) )
	{
		printf( "%s: Unknown ROM size\n", g_szInfile );
		close( hIn );
		exit( 1 );
	}

	printf( "ROM file:  %s\n", g_szInfile );

	BYTE pbtMem[ 0x4000 ];

	read( hIn, pbtMem, lFileLen );

	WORD wInit = pbtMem[ lFileLen - 2 ] + ( pbtMem[ lFileLen - 1 ] << 8 );
	WORD wFlags = pbtMem[ lFileLen - 4 ] + ( pbtMem[ lFileLen - 3 ] << 8 );
	WORD wRun = pbtMem[ lFileLen - 6 ] + ( pbtMem[ lFileLen - 5 ] << 8 );

	WORD wStart = ( lFileLen == 0x2000 ) ? 0xA000 : 0x8000;

	printf( "Init Addr :%04X\n", wInit );
	printf( "Run Addr  :%04X\n", wRun );
	printf( "Flags     :%04X ( ", wFlags );

	if ( wFlags & 0x0400 )
	{
		printf( "INIT&RUN " );
	}
	else
		printf( "INIT " );

	if ( wFlags & 0x0100 )
		printf( "BOOT " );

	if ( wFlags & 0x8000 )
		printf( "TESTMOD " );

	printf( ")\n" );

	if ( g_bDisassemble )
	{
		printf( "\n" );
		OutputBlockDiss( pbtMem, wStart, wStart + lFileLen - 1 - 6 );
		printf( "\n" );
	}

	printf( "Ok!\n" );

	close( hIn );

	return 0;
}