1 |
/* |
/* |
2 |
* Copyright (C) 2003-2006 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2007 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: emul.c,v 1.272 2006/10/31 08:26:56 debug Exp $ |
* $Id: emul.c,v 1.278 2007/02/10 14:37:38 debug Exp $ |
29 |
* |
* |
30 |
* Emulation startup and misc. routines. |
* Emulation startup and misc. routines. |
31 |
*/ |
*/ |
57 |
#include "x11.h" |
#include "x11.h" |
58 |
|
|
59 |
|
|
60 |
|
/* #define ISO_DEBUG */ |
61 |
|
|
62 |
|
|
63 |
extern int extra_argc; |
extern int extra_argc; |
64 |
extern char **extra_argv; |
extern char **extra_argv; |
65 |
|
|
226 |
dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + |
dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + |
227 |
((uint64_t)buf[0x8f] << 24)) * 2048; |
((uint64_t)buf[0x8f] << 24)) * 2048; |
228 |
|
|
229 |
/* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ |
#ifdef ISO_DEBUG |
230 |
|
debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); |
231 |
|
#endif |
232 |
|
|
233 |
dirbuf = malloc(dirlen); |
dirbuf = malloc(dirlen); |
234 |
if (dirbuf == NULL) { |
if (dirbuf == NULL) { |
263 |
if (p == NULL) |
if (p == NULL) |
264 |
p = strchr(filename, '\\'); |
p = strchr(filename, '\\'); |
265 |
|
|
266 |
/* debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? |
#ifdef ISO_DEBUG |
267 |
" [CURRENT]" : "", x, y); */ |
debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? |
268 |
|
" [CURRENT]" : "", x, y); |
269 |
|
#endif |
270 |
for (i=0; i<nlen && i<sizeof(direntry)-1; i++) |
for (i=0; i<nlen && i<sizeof(direntry)-1; i++) |
271 |
if (dp[i]) { |
if (dp[i]) { |
272 |
direntry[i] = dp[i]; |
direntry[i] = dp[i]; |
273 |
/* debug("%c", dp[i]); */ |
#ifdef ISO_DEBUG |
274 |
|
debug("%c", dp[i]); |
275 |
|
#endif |
276 |
} else |
} else |
277 |
break; |
break; |
278 |
/* debug("\"\n"); */ |
#ifdef ISO_DEBUG |
279 |
|
debug("\"\n"); |
280 |
|
#endif |
281 |
direntry[i] = '\0'; |
direntry[i] = '\0'; |
282 |
|
|
283 |
/* A directory name match? */ |
/* A directory name match? */ |
284 |
if (p != NULL && strncasecmp(filename, direntry, nlen) == 0 |
if ((p != NULL && strncasecmp(filename, direntry, nlen) == 0 |
285 |
&& nlen == (size_t)p - (size_t)filename && found_dir == y) { |
&& nlen == (size_t)p - (size_t)filename && found_dir == y) |
286 |
|
|| (p == NULL && direntry[0] == '\0') ) { |
287 |
found_dir = filenr; |
found_dir = filenr; |
288 |
filename = p+1; |
if (p != NULL) |
289 |
|
filename = p+1; |
290 |
dirofs = 2048 * (int64_t)x; |
dirofs = 2048 * (int64_t)x; |
291 |
} |
} |
292 |
|
|
545 |
iso_type, retval = 0; |
iso_type, retval = 0; |
546 |
unsigned char minibuf[0x20]; |
unsigned char minibuf[0x20]; |
547 |
unsigned char *bootblock_buf; |
unsigned char *bootblock_buf; |
548 |
uint64_t bootblock_offset; |
uint64_t bootblock_offset, base_offset; |
549 |
uint64_t bootblock_loadaddr, bootblock_pc; |
uint64_t bootblock_loadaddr, bootblock_pc; |
550 |
|
|
551 |
boot_disk_id = diskimage_bootdev(m, &boot_disk_type); |
boot_disk_id = diskimage_bootdev(m, &boot_disk_type); |
552 |
if (boot_disk_id < 0) |
if (boot_disk_id < 0) |
553 |
return 0; |
return 0; |
554 |
|
|
555 |
|
base_offset = diskimage_get_baseoffset(m, boot_disk_id, boot_disk_type); |
556 |
|
|
557 |
switch (m->machine_type) { |
switch (m->machine_type) { |
558 |
|
|
559 |
|
case MACHINE_DREAMCAST: |
560 |
|
if (!diskimage_is_a_cdrom(cpu->machine, boot_disk_id, |
561 |
|
boot_disk_type)) { |
562 |
|
fatal("The Dreamcast emulation mode can only boot" |
563 |
|
" from CD images, not from other disk types.\n"); |
564 |
|
exit(1); |
565 |
|
} |
566 |
|
|
567 |
|
bootblock_buf = malloc(32768); |
568 |
|
if (bootblock_buf == NULL) { |
569 |
|
fprintf(stderr, "Out of memory.\n"); |
570 |
|
exit(1); |
571 |
|
} |
572 |
|
|
573 |
|
debug("loading Dreamcast IP.BIN from %s id %i\n", |
574 |
|
diskimage_types[boot_disk_type], boot_disk_id); |
575 |
|
|
576 |
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
577 |
|
0, base_offset, bootblock_buf, 0x8000); |
578 |
|
if (!res) { |
579 |
|
fatal("Couldn't read the disk image. Aborting.\n"); |
580 |
|
return 0; |
581 |
|
} |
582 |
|
|
583 |
|
if (strncmp((char *)bootblock_buf, "SEGA ", 5) != 0) { |
584 |
|
fatal("This is not a Dreamcast IP.BIN header.\n"); |
585 |
|
free(bootblock_buf); |
586 |
|
return 0; |
587 |
|
} |
588 |
|
|
589 |
|
/* Store IP.BIN at 0x8c008000, and set entry point. */ |
590 |
|
store_buf(cpu, 0x8c008000, (char *)bootblock_buf, 32768); |
591 |
|
cpu->pc = 0x8c008300; |
592 |
|
|
593 |
|
/* Remember the name of the file to boot (1ST_READ.BIN): */ |
594 |
|
if (cpu->machine->boot_kernel_filename == NULL || |
595 |
|
cpu->machine->boot_kernel_filename[0] == '\0') { |
596 |
|
int i = 0x60; |
597 |
|
while (i < 0x70) { |
598 |
|
if (bootblock_buf[i] == ' ') |
599 |
|
bootblock_buf[i] = 0; |
600 |
|
i ++; |
601 |
|
} |
602 |
|
cpu->machine->boot_kernel_filename = strdup( |
603 |
|
(char *)bootblock_buf + 0x60); |
604 |
|
} |
605 |
|
|
606 |
|
debug("boot filename: %s\n", |
607 |
|
cpu->machine->boot_kernel_filename); |
608 |
|
|
609 |
|
free(bootblock_buf); |
610 |
|
|
611 |
|
break; |
612 |
|
|
613 |
case MACHINE_PMAX: |
case MACHINE_PMAX: |
614 |
/* |
/* |
615 |
* The first few bytes of a disk contains information about |
* The first few bytes of a disk contains information about |
711 |
|
|
712 |
debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); |
debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); |
713 |
return 1; |
return 1; |
|
|
|
|
case MACHINE_X86: |
|
|
/* TODO: "El Torito" etc? */ |
|
|
if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id, |
|
|
boot_disk_type)) |
|
|
break; |
|
|
|
|
|
bootblock_buf = malloc(512); |
|
|
if (bootblock_buf == NULL) { |
|
|
fprintf(stderr, "Out of memory.\n"); |
|
|
exit(1); |
|
|
} |
|
|
|
|
|
debug("loading PC bootsector from %s id %i\n", |
|
|
diskimage_types[boot_disk_type], boot_disk_id); |
|
|
|
|
|
res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, |
|
|
bootblock_buf, 512); |
|
|
if (!res) { |
|
|
fatal("Couldn't read the disk image. Aborting.\n"); |
|
|
return 0; |
|
|
} |
|
|
|
|
|
if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa) |
|
|
debug("WARNING! The 0x55,0xAA marker is missing! " |
|
|
"Booting anyway.\n"); |
|
|
store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512); |
|
|
free(bootblock_buf); |
|
|
|
|
|
return 1; |
|
714 |
} |
} |
715 |
|
|
716 |
|
|
718 |
* Try reading a kernel manually from the disk. The code here |
* Try reading a kernel manually from the disk. The code here |
719 |
* does not rely on machine-dependent boot blocks etc. |
* does not rely on machine-dependent boot blocks etc. |
720 |
*/ |
*/ |
721 |
/* ISO9660: (0x800 bytes at 0x8000) */ |
/* ISO9660: (0x800 bytes at 0x8000 + base_offset) */ |
722 |
bootblock_buf = malloc(0x800); |
bootblock_buf = malloc(0x800); |
723 |
if (bootblock_buf == NULL) { |
if (bootblock_buf == NULL) { |
724 |
fprintf(stderr, "Out of memory.\n"); |
fprintf(stderr, "Out of memory.\n"); |
726 |
} |
} |
727 |
|
|
728 |
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
res = diskimage_access(m, boot_disk_id, boot_disk_type, |
729 |
0, 0x8000, bootblock_buf, 0x800); |
0, base_offset + 0x8000, bootblock_buf, 0x800); |
730 |
if (!res) { |
if (!res) { |
731 |
fatal("Couldn't read the disk image. Aborting.\n"); |
fatal("Couldn't read the disk image. Aborting.\n"); |
732 |
return 0; |
return 0; |
787 |
* |
* |
788 |
* Returns a reasonably initialized struct emul. |
* Returns a reasonably initialized struct emul. |
789 |
*/ |
*/ |
790 |
struct emul *emul_new(char *name) |
struct emul *emul_new(char *name, int id) |
791 |
{ |
{ |
792 |
struct emul *e; |
struct emul *e; |
793 |
e = malloc(sizeof(struct emul)); |
e = malloc(sizeof(struct emul)); |
798 |
|
|
799 |
memset(e, 0, sizeof(struct emul)); |
memset(e, 0, sizeof(struct emul)); |
800 |
|
|
801 |
|
e->path = malloc(15); |
802 |
|
if (e->path == NULL) { |
803 |
|
fprintf(stderr, "out of memory\n"); |
804 |
|
exit(1); |
805 |
|
} |
806 |
|
snprintf(e->path, 15, "emul[%i]", id); |
807 |
|
|
808 |
e->settings = settings_new(); |
e->settings = settings_new(); |
809 |
|
|
810 |
settings_add(e->settings, "n_machines", 0, |
settings_add(e->settings, "n_machines", 0, |
875 |
char tmpstr[20]; |
char tmpstr[20]; |
876 |
int i; |
int i; |
877 |
|
|
878 |
m = machine_new(name, e); |
m = machine_new(name, e, e->n_machines); |
879 |
m->serial_nr = (e->next_serial_nr ++); |
m->serial_nr = (e->next_serial_nr ++); |
880 |
|
|
881 |
i = e->n_machines; |
i = e->n_machines; |
1289 |
} |
} |
1290 |
} |
} |
1291 |
|
|
|
/* Special things required _before_ loading the file: */ |
|
|
switch (m->arch) { |
|
|
case ARCH_X86: |
|
|
/* |
|
|
* X86 machines normally don't need to load any files, |
|
|
* they can boot from disk directly. Therefore, an x86 |
|
|
* machine usually boots up in 16-bit real mode. When |
|
|
* loading a 32-bit (or even 64-bit) ELF, that's not |
|
|
* very nice, hence this special case. |
|
|
*/ |
|
|
pc_bios_simple_pmode_setup(cpu); |
|
|
break; |
|
|
} |
|
|
|
|
1292 |
byte_order = NO_BYTE_ORDER_OVERRIDE; |
byte_order = NO_BYTE_ORDER_OVERRIDE; |
1293 |
|
|
1294 |
/* |
/* |
1330 |
} |
} |
1331 |
break; |
break; |
1332 |
|
|
|
case ARCH_AVR32: |
|
|
cpu->pc = (uint32_t) cpu->pc; |
|
|
if (cpu->pc & 1) { |
|
|
fatal("AVR32: lowest bit of pc set: TODO\n"); |
|
|
exit(1); |
|
|
} |
|
|
break; |
|
|
|
|
1333 |
case ARCH_RCA180X: |
case ARCH_RCA180X: |
1334 |
cpu->pc &= 0xffff; |
cpu->pc &= 0xffff; |
1335 |
break; |
break; |
1336 |
|
|
|
case ARCH_HPPA: |
|
|
break; |
|
|
|
|
|
case ARCH_I960: |
|
|
break; |
|
|
|
|
|
case ARCH_IA64: |
|
|
break; |
|
|
|
|
1337 |
case ARCH_M68K: |
case ARCH_M68K: |
1338 |
break; |
break; |
1339 |
|
|
1371 |
cpu->pc &= 0xffffffffULL; |
cpu->pc &= 0xffffffffULL; |
1372 |
break; |
break; |
1373 |
|
|
|
case ARCH_X86: |
|
|
/* |
|
|
* NOTE: The toc field is used to indicate an ELF32 |
|
|
* or ELF64 load. |
|
|
*/ |
|
|
switch (toc) { |
|
|
case 0: /* 16-bit? TODO */ |
|
|
cpu->pc &= 0xffffffffULL; |
|
|
break; |
|
|
case 1: /* 32-bit. */ |
|
|
cpu->pc &= 0xffffffffULL; |
|
|
break; |
|
|
case 2: /* 64-bit: TODO */ |
|
|
fatal("64-bit x86 load. TODO\n"); |
|
|
exit(1); |
|
|
} |
|
|
break; |
|
|
|
|
1374 |
default: |
default: |
1375 |
fatal("emul_machine_setup(): Internal error: " |
fatal("emul_machine_setup(): Internal error: " |
1376 |
"Unimplemented arch %i\n", m->arch); |
"Unimplemented arch %i\n", m->arch); |
1467 |
debug("0x%016llx", (long long)entrypoint); |
debug("0x%016llx", (long long)entrypoint); |
1468 |
break; |
break; |
1469 |
|
|
|
case ARCH_X86: |
|
|
debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], |
|
|
(long long)cpu->pc); |
|
|
break; |
|
|
|
|
1470 |
default: |
default: |
1471 |
if (cpu->is_32bit) |
if (cpu->is_32bit) |
1472 |
debug("0x%08x", (int)cpu->pc); |
debug("0x%08x", (int)cpu->pc); |
1552 |
* |
* |
1553 |
* Create an emul struct by reading settings from a configuration file. |
* Create an emul struct by reading settings from a configuration file. |
1554 |
*/ |
*/ |
1555 |
struct emul *emul_create_from_configfile(char *fname) |
struct emul *emul_create_from_configfile(char *fname, int id) |
1556 |
{ |
{ |
1557 |
int iadd = DEBUG_INDENTATION; |
int iadd = DEBUG_INDENTATION; |
1558 |
struct emul *e = emul_new(fname); |
struct emul *e = emul_new(fname, id); |
1559 |
|
|
1560 |
debug("Creating emulation from configfile \"%s\":\n", fname); |
debug("Creating emulation from configfile \"%s\":\n", fname); |
1561 |
debug_indentation(iadd); |
debug_indentation(iadd); |