Пример #1
0
int main(int argc, char **argv)
{
	char str[256];
	int16 i16;
	HANDLE rom_fh;
	const char *rom_path;
	uint32 rom_size;
	DWORD actual;
	uint8 *rom_tmp;

	// Initialize variables
	RAMBase = 0;

	// Print some info
	printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
	printf(" %s\n", GetString(STR_ABOUT_TEXT2));

	// Read preferences
	PrefsInit(NULL, argc, argv);

	// Parse command line arguments
	for (int i=1; i<argc; i++) {
		if (strcmp(argv[i], "--help") == 0) {
			usage(argv[0]);
		} else if (argv[i][0] == '-') {
			fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
			usage(argv[0]);
		}
	}

	// Check we are using a Windows NT kernel >= 4.0
	OSVERSIONINFO osvi;
	ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	if (!GetVersionEx(&osvi)) {
		ErrorAlert("Could not determine OS type");
		QuitEmulator();
	}
	win_os = osvi.dwPlatformId;
	win_os_major = osvi.dwMajorVersion;
	if (win_os != VER_PLATFORM_WIN32_NT || win_os_major < 4) {
		ErrorAlert(GetString(STR_NO_WIN32_NT_4));
		QuitEmulator();
	}

	// Check that drivers are installed
	if (!check_drivers())
		QuitEmulator();

	// Load win32 libraries
	KernelInit();

	// FIXME: default to DIB driver
	if (getenv("SDL_VIDEODRIVER") == NULL)
	    putenv("SDL_VIDEODRIVER=windib");

	// Initialize SDL system
	int sdl_flags = 0;
#ifdef USE_SDL_VIDEO
	sdl_flags |= SDL_INIT_VIDEO;
#endif
#ifdef USE_SDL_AUDIO
	sdl_flags |= SDL_INIT_AUDIO;
#endif
	assert(sdl_flags != 0);
	if (SDL_Init(sdl_flags) == -1) {
		char str[256];
		sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError());
		ErrorAlert(str);
		goto quit;
	}
	atexit(SDL_Quit);

#ifdef ENABLE_MON
	// Initialize mon
	mon_init();
#endif

	// Install SIGSEGV handler for CPU emulator
	if (!sigsegv_install_handler(sigsegv_handler)) {
		sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
		ErrorAlert(str);
		goto quit;
	}

	// Initialize VM system
	vm_init();

	// Get system info
	PVR = 0x00040000;			// Default: 604
	CPUClockSpeed = 100000000;	// Default: 100MHz
	BusClockSpeed = 100000000;	// Default: 100MHz
	TimebaseSpeed =  25000000;	// Default:  25MHz
	PVR = 0x000c0000;			// Default: 7400 (with AltiVec)
	D(bug("PVR: %08x (assumed)\n", PVR));

	// Init system routines
	SysInit();

	// Show preferences editor
	if (!PrefsFindBool("nogui"))
		if (!PrefsEditor())
			goto quit;

	// Create areas for Kernel Data
	if (!kernel_data_init())
		goto quit;
	kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
	emulator_data = &kernel_data->ed;
	KernelDataAddr = KERNEL_DATA_BASE;
	D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE));
	D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed)));

	// Create area for DR Cache
	if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
		sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
		ErrorAlert(str);
		goto quit;
	}
	dr_emulator_area_mapped = true;
	if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
		sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
		ErrorAlert(str);
		goto quit;
	}
	dr_cache_area_mapped = true;
	DRCacheAddr = (uint32)Mac2HostAddr(DR_CACHE_BASE);
	D(bug("DR Cache at %p (%08x)\n", DRCacheAddr, DR_CACHE_BASE));

	// Create area for SheepShaver data
	if (!SheepMem::Init()) {
		sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
		ErrorAlert(str);
		goto quit;
	}

	// Create area for Mac ROM
	if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) {
		sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
		ErrorAlert(str);
		goto quit;
	}
	ROMBase = ROM_BASE;
	ROMBaseHost = Mac2HostAddr(ROMBase);
	rom_area_mapped = true;
	D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROMBase));

	// Create area for Mac RAM
	RAMSize = PrefsFindInt32("ramsize");
	if (RAMSize < 8*1024*1024) {
		WarningAlert(GetString(STR_SMALL_RAM_WARN));
		RAMSize = 8*1024*1024;
	}
	RAMBase = 0;
	if (vm_mac_acquire(RAMBase, RAMSize) < 0) {
		sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
		ErrorAlert(str);
		goto quit;
	}
	RAMBaseHost = Mac2HostAddr(RAMBase);
	ram_area_mapped = true;
	D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase));

	if (RAMBase > ROMBase) {
		ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
		goto quit;
	}

	// Load Mac ROM
	rom_path = PrefsFindString("rom");
	rom_fh = CreateFile(rom_path && *rom_path ? rom_path : ROM_FILE_NAME,
						GENERIC_READ, 0, NULL, OPEN_EXISTING,
						FILE_ATTRIBUTE_NORMAL, NULL);

	if (rom_fh == INVALID_HANDLE_VALUE) {
		rom_fh = CreateFile(ROM_FILE_NAME2,
							GENERIC_READ, 0, NULL, OPEN_EXISTING,
							FILE_ATTRIBUTE_NORMAL, NULL);

		if (rom_fh == INVALID_HANDLE_VALUE) {
			ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
			goto quit;
		}
	}
	printf(GetString(STR_READING_ROM_FILE));
	rom_size = GetFileSize(rom_fh, NULL);
	rom_tmp = new uint8[ROM_SIZE];
	ReadFile(rom_fh, (void *)rom_tmp, ROM_SIZE, &actual, NULL);
	CloseHandle(rom_fh);
	
	// Decode Mac ROM
	if (!DecodeROM(rom_tmp, actual)) {
		if (rom_size != 4*1024*1024) {
			ErrorAlert(GetString(STR_ROM_SIZE_ERR));
			goto quit;
		} else {
			ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
			goto quit;
		}
	}
	delete[] rom_tmp;
	
	// Initialize native timers
	timer_init();

	// Initialize everything
	if (!InitAll(NULL))
		goto quit;
	D(bug("Initialization complete\n"));

	// Write protect ROM
	vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ);

	// Start 60Hz thread
	tick_thread_cancel = false;
	tick_thread_active = ((tick_thread = create_thread(tick_func)) != NULL);
	SetThreadPriority(tick_thread, THREAD_PRIORITY_ABOVE_NORMAL);
	D(bug("Tick thread installed (%ld)\n", tick_thread));

	// Start NVRAM watchdog thread
	memcpy(last_xpram, XPRAM, XPRAM_SIZE);
	nvram_thread_cancel = false;
	nvram_thread_active = ((nvram_thread = create_thread(nvram_func, NULL)) != NULL);
	SetThreadPriority(nvram_thread, THREAD_PRIORITY_BELOW_NORMAL);
	D(bug("NVRAM thread installed (%ld)\n", nvram_thread));

	// Get my thread ID and jump to ROM boot routine
	emul_thread = GetCurrentThread();
	D(bug("Jumping to ROM\n"));
#ifdef _MSC_VER
	__try {
#endif
		jump_to_rom(ROMBase + 0x310000);
#ifdef _MSC_VER
	} __except (main_exception_filter(GetExceptionInformation())) {}
#endif
	D(bug("Returned from ROM\n"));

quit:
	Quit();
	return 0;
}
Пример #2
0
int main(int argc, char* argv[]) {
	int c;
	char *dbname = (char *)DBNAME;
	char *dbinst = (char *)DBINST;
	char *hostname = (char *)HOSTNAME;
	char *datadir = NULL;
	char *verdesc = NULL;
	char *verstr = NULL;
	char *prefix = NULL;
	unsigned int version = 0;
	char *schemastr = NULL;
	unsigned short usage = 0;
	unsigned short dblist = 0;
	unsigned short schemalist = 0;
	unsigned short prefixlist = 0;
	unsigned short trusted = 1;
	unsigned char term = '\0';

	// source files
	char udd_sql3[BUFLEN] = NULLSTR;
	char cert_yaml[BUFLEN] = NULLSTR;
	char gfxids_yaml[BUFLEN] = NULLSTR;
	char iconids_yaml[BUFLEN] = NULLSTR;
	char typeids_yaml[BUFLEN] = NULLSTR;
	char blueprints_yaml[BUFLEN] = NULLSTR;

	// SQL things
	int db3_rc;
	SQLRETURN ret;
	SQLCHAR connStrOut[BUFLEN] = NULLSTR;
	SQLCHAR dsn[BUFLEN] = NULLSTR;
	SQLCHAR auth[BUFLEN] = NULLSTR;
	SQLCHAR usr[BUFLEN] = NULLSTR;
	SQLCHAR pwd[BUFLEN] = NULLSTR;
	SQLSMALLINT connStrLen;

	// set globals
	GZIP_FLAG = 1;
	JSONP_FLAG = 1;
	SCHEMA = 0;
	JSON_DIR = NULL;
	H_ENV = SQL_NULL_HENV;
	H_DBC = SQL_NULL_HDBC;
	H_DBC2 = SQL_NULL_HDBC;
	DB3_UD = NULL;

	while ((c = getopt(argc, argv, "i:o:d:u:p:n:N:s:x:hvXDSZP")) != -1) {
		switch (c) {
		case 'i':
			datadir = (char *)malloc(BUFLEN);
			strlcpy(datadir, optarg, BUFLEN);
			term = datadir[strlen(datadir) - 1];
			if (term != PATHSEP) {
				strlcat(datadir, SZPATHSEP, BUFLEN);
			}
			break;
		case 'o':
			JSON_DIR = (char *)malloc(BUFLEN);
			strlcpy(JSON_DIR, optarg, BUFLEN);
			term = JSON_DIR[strlen(JSON_DIR) - 1];
			if (term != PATHSEP) {
				strlcat(JSON_DIR, SZPATHSEP, BUFLEN);
			}
			break;
		case 'x':
			prefix = (char *)malloc(BUFLEN);
			strlcpy(prefix, optarg, BUFLEN);
			break;
		case 'd':
			dbname = (char *)malloc(BUFLEN);
			strlcpy(dbname, optarg, BUFLEN);
			break;
		case 'n':
			verstr = (char *)malloc(BUFLEN);
			strlcpy(verstr, optarg, BUFLEN);
			break;
		case 'N':
			verdesc = (char *)malloc(BUFLEN);
			strlcpy(verdesc, optarg, BUFLEN);
			break;
		case 's':
			schemastr = (char *)malloc(BUFLEN);
			strlcpy(schemastr, optarg, BUFLEN);
			break;
		case 'S':
			schemalist = 1;
			break;
		case 'v':
			printf("sdd2json version %d.%d.%d", SDD2JSON_V_MAJOR, SDD2JSON_V_MINOR, SDD2JSON_V_PATCH);
			return 0;
		case 'D':
			dblist = 1;
			break;
		case 'u':
			trusted = 0;
			strlcpy(usr, optarg, BUFLEN);
			break;
		case 'p':
			trusted = 0;
			strlcpy(pwd, optarg, BUFLEN);
			break;
		case 'P':
			JSONP_FLAG = 0;
			break;
		case 'Z':
			GZIP_FLAG = 0;
			break;
		case 'X':
			prefixlist = 1;
		case '?':
			printf("\n");
			usage = 1;
			break;
		}
	}

	if (1 == schemalist) {
		for (unsigned int i = 0; i < VERS_N; i++) {
			printf("%s:\tversion %d;\tschema %d\n", VERS[i].version_desc, VERS[i].version_id, VERS[i].schema_id);
		}
		return 0;
	}

	if (1 == prefixlist) {
		printf("\tcrp\n");
		printf("\tdgm\n");
		printf("\tinv\n");
		printf("\tmap\n");
		printf("\tram\n");
		printf("\tsta\n");
		return 0;
	}

	if (usage < 1 && datadir == NULL) {
		fprintf(stderr, "EVE static data dir path is required\n");
		usage = 1;
	}

	if (usage < 1 && verstr == NULL) {
		fprintf(stderr, "static data version ID is required\n");
		usage = 1;
	}

	if (usage > 0) return dump_usage();

	// parse version/schema from params and known values
	if (verstr != NULL) {
		version = atoi(verstr);
	}
	if (version == 0) {
		fprintf(stderr, "the static data version ID provided is invalid: %s\n", verstr);
		return 1;
	}
	unsigned int found_version = 0;
	for (unsigned int i = 0; i < VERS_N; i++) {
		if (VERS[i].version_id == version) {
			found_version = 1;
			SCHEMA = VERS[i].schema_id;
			if (NULL == verdesc) {
				verdesc = (char *)malloc(BUFLEN);
				strlcpy(verdesc, VERS[i].version_desc, BUFLEN);
			}
			break;
		}
	}
	if (1 != found_version) {
		fprintf(stderr, "warning: using unknown static data version ID: %d\n", version);
	}
	if (schemastr != NULL) {
		if (0 != SCHEMA) {
			fprintf(stderr, "warning: overriding known schema ID\n");
		}
		SCHEMA = atoi(schemastr);
	}
	if (0 == SCHEMA) {
		fprintf(stderr, "schema ID is required\n");
		return 1;
	}
	printf("static data: '%s', version %d, schema %d\n", verdesc, version, SCHEMA);

	// validate input/schema
	printf("checking input: %s - ", datadir);
	if (ACCESS(datadir, 0) != 0) {
		printf("err\n");
		fprintf(stderr, "could not access output path\n");
		return 1;
	}
		
	if (SCHEMA >= 100038) {
		strlcpy(cert_yaml, datadir, BUFLEN);
		strlcat(cert_yaml, "certificates.yaml", BUFLEN);
		if (ACCESS(cert_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", cert_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(gfxids_yaml, datadir, BUFLEN);
		strlcat(gfxids_yaml, "graphicIDs.yaml", BUFLEN);
		if (ACCESS(gfxids_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", gfxids_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(iconids_yaml, datadir, BUFLEN);
		strlcat(iconids_yaml, "iconIDs.yaml", BUFLEN);
		if (ACCESS(iconids_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", iconids_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(typeids_yaml, datadir, BUFLEN);
		strlcat(typeids_yaml, "typeIDs.yaml", BUFLEN);
		if (ACCESS(typeids_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", typeids_yaml);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(udd_sql3, datadir, BUFLEN);
		strlcat(udd_sql3, "universeDataDx.db", BUFLEN);
		if (ACCESS(udd_sql3, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", udd_sql3);
			return 1;
		}
	}

	if (SCHEMA >= 100038) {
		strlcpy(blueprints_yaml, datadir, BUFLEN);
		strlcat(blueprints_yaml, "blueprints.yaml", BUFLEN);
		if (ACCESS(blueprints_yaml, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access %s\n", blueprints_yaml);
			return 1;
		}
	}

	printf("OK\n");

	// validate output
	if (JSON_DIR == NULL) {
		JSON_DIR = (char *)malloc(2 * BUFLEN);
		strlcpy(JSON_DIR, datadir, 2 * BUFLEN);
		strlcat(JSON_DIR, SDD, 2* BUFLEN);
		strlcat(JSON_DIR, SZPATHSEP, 2 * BUFLEN);
	}
	printf("checking output: %s - ", JSON_DIR);
	if (ACCESS(JSON_DIR, 0) != 0) {
		if (MKDIR(JSON_DIR) != 0) {
			printf("err\n");
			fprintf(stderr, "could not create output path\n");
			return 1;
		}
		if (ACCESS(JSON_DIR, 0) != 0) {
			printf("err\n");
			fprintf(stderr, "could not access output path\n");
			return 1;
		}
	}
	printf("OK\n");

	// connect to SQLLITE dbs
	printf("connecting to [%s] - ", udd_sql3);

	db3_rc = sqlite3_open(udd_sql3, &DB3_UD);
	if (SQLITE_OK != db3_rc) return dump_db3_error(DB3_UD, 1);
	printf("OK\n");

	// connect to SQL server
	if (0 != trusted) {
		SNPRINTF(auth, BUFLEN, "Trusted_Connection=yes");
	}
	else {
		SNPRINTF(auth, BUFLEN, "User ID=%s;Password=%s", usr, pwd);
	}
	printf("connecting to [%s\\%s] using [%s] - ", hostname, dbinst, auth);

	ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, &H_ENV);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);
	ret = SQLSetEnvAttr(H_ENV, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);

	if (dblist > 0) {
		printf("list drivers\n");
		unsigned short ret = check_drivers(1);
		close_handles();
		return ret;
	}

	if (0 != check_drivers(0)) {
		printf("err\n");
		fprintf(stderr, "SQL server driver not found\n");
		close_handles();
		return 1;
	}

	ret = SQLAllocHandle(SQL_HANDLE_DBC, H_ENV, &H_DBC);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);
	ret = SQLAllocHandle(SQL_HANDLE_DBC, H_ENV, &H_DBC2);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_ENV, H_ENV, 1);

	SNPRINTF(dsn, BUFLEN, "Driver={%s};Server=%s\\%s;Database=%s;%s;", SQLDRV, hostname, dbinst, dbname, auth);
	ret = SQLDriverConnect(H_DBC, NULL, dsn, strnlen(dsn, BUFLEN), connStrOut, BUFLEN, &connStrLen, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_DBC, H_DBC, 1);
	ret = SQLDriverConnect(H_DBC2, NULL, dsn, strnlen(dsn, BUFLEN), connStrOut, BUFLEN, &connStrLen, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(ret)) return dump_sql_error(ret, SQL_HANDLE_DBC, H_DBC2, 1);

	printf("OK\n");

	// create metainfo file
	char metafile[BUFLEN] = NULLSTR;
	strlcpy(metafile, JSON_DIR, BUFLEN);
	strlcat(metafile, "metainf.json", BUFLEN);
	FILE *f = fopen(metafile, "w");
	if (f == NULL) {
		printf("err\n");
		fprintf(stderr, "error opening meta file: %s", metafile);
		close_handles();
		return 1;
	}
	printf("OK\n");
	fprintf(f, "{\n");
	fprintf(f, "\"formatID\":%d,\n", FORMAT_ID);
	fprintf(f, "\"schema\":%d,\n", SCHEMA);
	fprintf(f, "\"copy\":\"%s\",\n", CCPR);
	fprintf(f, "\"version\":%d,\n", version);
	fprintf(f, "\"verdesc\":\"%s\",\n", verdesc);
	fprintf(f, "\"tables\":{\n");

	// do stuff
	int rc = 0;
	int comma = 0;

	if (NULL == prefix || strncmp(prefix, "crp", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_crp(f);
		if (rc != 0) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "dgm", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_dgm(f, cert_yaml);
		if (rc != 0) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "inv", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_inv(f, typeids_yaml, iconids_yaml);
		if (rc != 0) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "map", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		rc = create_map(f);
		if (0 != rc) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "ram", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		ret = create_ram(f, blueprints_yaml);
		if (!SQL_SUCCEEDED(ret)) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	if (NULL == prefix || strncmp(prefix, "sta", 3) == 0) {
		if (comma++ > 0) fprintf(f, ",\n");
		ret = create_sta(f);
		if (!SQL_SUCCEEDED(ret)) {
			close_handles();
			fclose(f);
			return 1;
		}
	}

	fprintf(f, "\n}\n"); // end of sources

	// clean up connections
	fprintf(f, "}\n");
	fclose(f);
	printf("metainf - ", metafile);
	post_file("metainf");

	close_handles();

	free(datadir);
	free(verdesc);
	free(verstr);
	free(prefix);
	free(JSON_DIR);

	printf("\nall done!\n");

	return 0;
}