1 /*
2  * libtilemcore - Graphing calculator emulation library
3  *
4  * Copyright (C) 2009 Benjamin Moody
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include "tilem.h"
28 
find_string(const char * str,FILE * romfile,dword start,dword limit)29 static int find_string(const char *str, FILE *romfile,
30 		       dword start, dword limit)
31 {
32 	char buf[256];
33 	int pos = 0;
34 	int len, i;
35 
36 	len = strlen(str);
37 
38 	fseek(romfile, (long int) start, SEEK_SET);
39 
40 	for (i = 0; i < len-1; i++) {
41 		buf[pos] = fgetc(romfile);
42 		pos = (pos+1)%256;
43 		limit--;
44 	}
45 
46 	while (limit > 0 && !feof(romfile) && !ferror(romfile)) {
47 		buf[pos] = fgetc(romfile);
48 		pos = (pos+1)%256;
49 		limit--;
50 
51 		for (i = 0; i < len; i++) {
52 			if (str[i] != buf[(pos + 256 - len + i)%256])
53 				break;
54 		}
55 		if (i == len)
56 			return 1;
57 	}
58 	return 0;
59 }
60 
tilem_guess_rom_type(FILE * romfile)61 char tilem_guess_rom_type(FILE* romfile)
62 {
63 	unsigned long initpos;
64 	dword size;
65 	char result;
66 
67 	initpos = ftell(romfile);
68 
69 	fseek(romfile, 0L, SEEK_END);
70 	size = ftell(romfile);
71 
72 	if (size >= 0x8000 && size < 0x9000) {
73 		/* 32k: TI-81 (old or new) */
74 		result = TILEM_CALC_TI81;
75 	}
76 	else if (size >= 0x20000 && size < 0x2C000) {
77 		/* 128k: TI-82 or TI-86 */
78 		if (find_string("CATALOG", romfile, 0, 0x20000))
79 			result = TILEM_CALC_TI85;
80 		else
81 			result = TILEM_CALC_TI82;
82 	}
83 	else if (size >= 0x40000 && size < 0x4C000) {
84 		/* 256k: TI-83 (or a variant) or TI-86 */
85 		if (!find_string("TI82", romfile, 0, 0x40000))
86 			result = TILEM_CALC_TI86;
87 		else if (find_string("Termin\x96", romfile, 0, 0x40000))
88 			result = TILEM_CALC_TI76;
89 		else
90 			result = TILEM_CALC_TI83;
91 	}
92 	else if (size >= 0x80000 && size < 0x8C000) {
93 		/* 512k: TI-83 Plus or TI-73 */
94 		if (find_string("TI-83 Plus", romfile, 0, 8 * 0x4000))
95 			result = TILEM_CALC_TI83P;
96 		else
97 			result = TILEM_CALC_TI73;
98 	}
99 	else if (size >= 0x100000 && size < 0x124000) {
100 		/* 1024k: TI-84 Plus */
101 		result = TILEM_CALC_TI84P;
102 	}
103 	else if (size >= 0x200000 && size < 0x224000) {
104 		/* 2048k: TI-83 Plus SE, TI-84 Plus SE */
105 		if (find_string("\xed\xef", romfile, 0x1FC000, 0x4000))
106 			result = TILEM_CALC_TI84P_NSPIRE;
107 		else if (find_string("Operating", romfile, 0x1FC000, 0x4000))
108 			result = TILEM_CALC_TI84P_SE;
109 		else
110 			result = TILEM_CALC_TI83P_SE;
111 	}
112 	else {
113 		result = 0;
114 	}
115 
116 	fseek(romfile, initpos, SEEK_SET);
117 	return result;
118 }
119