1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id: interface.c 2792 2008-05-26 16:48:30Z roms $ */
3 
4 /*  TiEmu - Tiemu Is an EMUlator
5  *
6  *  Copyright (c) 2000-2001, Thomas Corvazier, Romain Lievin
7  *  Copyright (c) 2001-2003, Romain Lievin
8  *  Copyright (c) 2003, Julien Blache
9  *  Copyright (c) 2004, Romain Li�vin
10  *  Copyright (c) 2005-2007, Romain Li�vin, Kevin Kofler
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
25  */
26 
27 /*
28     Interface: exported & misc routines
29 */
30 
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
39 
40 #ifdef __MINGW32__
41 #include <windows.h>
42 #endif
43 
44 #include "libuae.h"
45 #include "hw.h"
46 #include "m68k.h"
47 #include "dbus.h"
48 #include "logging.h"
49 #ifndef NO_SOUND
50 #include "audio.h"
51 #endif
52 
53 #include "ti68k_int.h"
54 #include "ti68k_err.h"
55 #include "mem_size.h"
56 #include "romcalls.h"
57 #include "iodefs.h"
58 #include "mem_map.h"
59 
60 /**********************/
61 /* Internal variables */
62 /**********************/
63 
64 
65 Ti68kParameters     params = { 0 };
66 Ti68kHardware       tihw   = { 0 };
67 Ti68kLinkPort	    linkp  = { 0 };
68 Ti68kBreakpoints	bkpts  = { 0 };
69 Ti68kLogging		logger = { 0 };
70 
71 /***********************************/
72 /* Entry points for initialization */
73 /***********************************/
74 
75 
76 /*
77   Initialization order (checked by the runlevel):
78   - load the default config
79   - load a ROM (init ROM)
80   - init the lib (init HW, UAE, HID)
81   - reset the lib
82 */
83 
is_win_9x(void)84 static int is_win_9x(void)
85 {
86 #ifdef __WIN32__
87 	OSVERSIONINFO os;
88 
89 	memset(&os, 0, sizeof(OSVERSIONINFO));
90 	os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
91   	GetVersionEx(&os);
92 
93 	return (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
94 #else
95 	return 0;
96 #endif
97 }
98 
99 /*
100   This should be the FIRST function to call (unless the 'params'
101   structure has been properly initialized.
102  */
ti68k_config_load_default(void)103 int ti68k_config_load_default(void)
104 {
105 	params.restricted = 1;
106 	params.cpu_rate = -1;
107 	params.hw_rate = -1;
108 	params.lcd_rate = -1;
109 	params.hw_protect = 1;
110 	params.recv_file = 1;
111 #ifndef NO_SOUND
112 	audio_disable();
113 #endif
114 
115 	linkp.cable_delay = DFLT_DELAY;
116 	linkp.cable_timeout = is_win_9x() ? 600 : DFLT_TIMEOUT;
117 	linkp.cable_port = PORT_1;
118 	linkp.cable_model = CABLE_ILP;
119 	linkp.calc_model = ti68k_calc_to_libti_calc();
120 
121     return 0;
122 }
123 
124 /*
125   This should be the SECOND function to call.
126   Load a ROM image (images.c).
127 */
128 int ti68k_load_image(const char *filename);
129 
130 /*
131   This is the THIRD function to call for completely initializing the
132   emulation engine.
133 */
ti68k_init(void)134 int ti68k_init(void)
135 {
136 	// init libs
137     ticables_library_init();
138 	tifiles_library_init();
139 	ticalcs_library_init();
140 
141 	// check if image has been loaded
142 	if(img_loaded == 0)
143 		return ERR_NO_IMAGE;
144 
145 	// set calc type and init hardware
146 	memset(&tihw, 0, sizeof(Ti68kHardware));
147     tihw.calc_type = img_infos.calc_type;
148 	TRY(hw_init());
149 
150 	return 0;
151 }
152 
153 /*
154   This should be the FOURTH function to call.
155   It simply resets the hardware engine.
156 */
ti68k_reset(void)157 int ti68k_reset(void)
158 {
159 	hw_reset();
160 
161 	return 0;
162 }
163 
164 /*
165   Close the library by exiting the emulation engine
166   (free ressources).
167 */
ti68k_exit(void)168 int ti68k_exit(void)
169 {
170     TRY(hw_exit());
171 
172     ticables_library_exit();
173 	tifiles_library_exit();
174 	ticalcs_library_exit();
175 
176 	return 0;
177 }
178 
ti68k_get_cycle_count(int reset,unsigned int * diff)179 unsigned int ti68k_get_cycle_count(int reset, unsigned int *diff)
180 {
181 	static unsigned int old_cnt = 0;
182 	unsigned int new_cnt;
183 
184 	new_cnt = hw_m68k_get_cycle_count(reset);
185 
186 	if(diff != NULL)
187 		*diff = new_cnt - old_cnt;
188 	old_cnt = new_cnt;
189 
190 	return new_cnt;
191 }
192 
193 /******************/
194 /* Link functions */
195 /******************/
196 
ti68k_linkport_send_file(const char * filename)197 int ti68k_linkport_send_file(const char *filename)
198 {
199     return send_ti_file(filename);
200 }
201 
ti68k_linkport_unconfigure(void)202 int ti68k_linkport_unconfigure(void)
203 {
204 	return hw_dbus_exit();
205 }
206 
ti68k_linkport_reconfigure(void)207 int ti68k_linkport_reconfigure(void)
208 {
209 	return hw_dbus_init();
210 }
211 
ti68k_calc_to_libti_calc()212 int ti68k_calc_to_libti_calc()
213 {
214 	switch(tihw.calc_type)
215 	{
216     case TI89:  return CALC_TI89;  break;
217 	case TI89t: return CALC_TI89T; break;
218 	case TI92:  return CALC_TI92;  break;
219 	case TI92p: return CALC_TI92P; break;
220 	case V200:  return CALC_V200;  break;
221 	default: return CALC_NONE; break;
222 	}
223 
224 	return CALC_NONE;
225 }
226 
227 /******************/
228 /* Misc functions */
229 /******************/
230 
231 const int ti_rom_base[]  = { ROM_BASE_TI92_I, ROM_BASE_TI89, ROM_BASE_TI92P, ROM_BASE_V200, ROM_BASE_TI89T };
232 const int ti_rom_sizes[] = { ROM_SIZE_TI92_I, ROM_SIZE_TI89, ROM_SIZE_TI92P, ROM_SIZE_V200, ROM_SIZE_TI89T };
233 const int ti_ram_sizes[] = {RAM_SIZE_TI92_II, RAM_SIZE_TI89, RAM_SIZE_TI92P, RAM_SIZE_V200, RAM_SIZE_TI89T };
234 
235 const int ti_io_sizes[]  = { IO1_SIZE_TI92_I, IO1_SIZE_TI89, IO1_SIZE_TI92P, IO1_SIZE_V200, IO1_SIZE_TI89T };
236 const int ti_io2_sizes[] = { IO2_SIZE_TI92_I, IO2_SIZE_TI89, IO2_SIZE_TI92P, IO2_SIZE_V200, IO2_SIZE_TI89T };
237 const int ti_io3_sizes[] = { IO3_SIZE_TI92_I, IO3_SIZE_TI89, IO3_SIZE_TI92P, IO3_SIZE_V200, IO3_SIZE_TI89T };
238 
log_b2(int i)239 static int log_b2(int i)
240 {
241     int j, v;
242 
243     for(j = 0, v = i; v != 0; v >>= 1, j++);
244 
245     return j-1;
246 }
247 
ti68k_get_rom_size(int calc_type)248 int ti68k_get_rom_size(int calc_type)
249 {
250     if(calc_type > CALC_MAX)
251     {
252         tiemu_error(_("Bad argument!"));
253         exit(0);
254     }
255 
256     return ti_rom_sizes[log_b2(calc_type)];
257 }
258 
ti68k_get_ram_size(int calc_type)259 int ti68k_get_ram_size(int calc_type)
260 {
261     if(calc_type > CALC_MAX)
262     {
263         tiemu_error(_("Bad argument!"));
264         exit(0);
265     }
266 
267     return ti_ram_sizes[log_b2(calc_type)];
268 }
269 
ti68k_get_io_size(int calc_type)270 int ti68k_get_io_size(int calc_type)
271 {
272 	if(calc_type > CALC_MAX)
273     {
274         tiemu_error(_("Bad argument!"));
275         exit(0);
276     }
277 
278     return ti_io_sizes[log_b2(calc_type)];
279 }
280 
ti68k_get_io2_size(int calc_type)281 int ti68k_get_io2_size(int calc_type)
282 {
283 	if(calc_type > CALC_MAX)
284     {
285         tiemu_error(_("Bad argument!"));
286         exit(0);
287     }
288 
289     return ti_io2_sizes[log_b2(calc_type)];
290 }
291 
ti68k_get_io3_size(int calc_type)292 int ti68k_get_io3_size(int calc_type)
293 {
294 	if(calc_type > CALC_MAX)
295     {
296         tiemu_error(_("Bad argument!"));
297         exit(0);
298     }
299 
300     return ti_io3_sizes[log_b2(calc_type)];
301 }
302 
303 /********/
304 /* Misc */
305 /********/
306 
ti68k_unprotect_64KB_range(uint32_t addr)307 void ti68k_unprotect_64KB_range(uint32_t addr)
308 {
309     unsigned blockid = addr >> 12, i;
310 
311     for (i = blockid; i <= blockid + 16 && i < 64; i++)
312         tihw.ram_exec[i] = 0;
313 }
314 
ti68k_debug_load_symbols(const char * path)315 int ti68k_debug_load_symbols(const char *path)
316 {
317 	if(!strcmp(path, ""))
318 		return 0;
319 
320 	return romcalls_load(path);
321 }
322 
ti68k_debug_load_iodefs(const char * path)323 int ti68k_debug_load_iodefs(const char *path)
324 {
325 	if(!strcmp(path, ""))
326 		return 0;
327 
328 	return iodefs_load(path);
329 }
330 
ti68k_debug_load_memmap(const char * path)331 int ti68k_debug_load_memmap(const char *path)
332 {
333 	if(!strcmp(path, ""))
334 		return 0;
335 
336 	return memmap_load(path);
337 }
338