 /*
  * UAE - The Un*x Amiga Emulator
  *
  * Debugger
  *
  * (c) 1995 Bernd Schmidt
  *
  */
 /* modified to work with systems without standard input/output stream (TW) */

#include "sysconfig.h"
#include "sysdeps.h"

#include <ctype.h>
#include <signal.h>

#include "config.h"
#include "options.h"
#include "threaddep/penguin.h"
#include "uae.h"
#include "memory.h"
#include "custom.h"
#include "readcpu.h"
#include "newcpu.h"
#include "debug.h"
#include "cia.h"
#include "xwin.h"
#include "gui.h"

static int debugger_active = 0;
static uaecptr skipaddr;
static int do_skip;
int debugging = 0;

void activate_debugger (void)
{
    do_skip = 0;
    if (debugger_active)
	return;
    debugger_active = 1;
    regs.spcflags |= SPCFLAG_BRK;
    debugging = 1;
    /* use_debugger = 1; */
}

int firsthist = 0;
int lasthist = 0;
#ifdef NEED_TO_DEBUG_BADLY
struct regstruct history[MAX_HIST];
union flagu historyf[MAX_HIST];
#else
uaecptr history[MAX_HIST];
#endif

static void ignore_ws (char **c)
{
    while (**c && isspace(**c)) (*c)++;
}

static uae_u32 readhex (char **c)
{
    uae_u32 val = 0;
    char nc;

    ignore_ws (c);

    while (isxdigit(nc = **c)) {
	(*c)++;
	val *= 16;
	nc = toupper(nc);
	if (isdigit(nc)) {
	    val += nc - '0';
	} else {
	    val += nc - 'A' + 10;
	}
    }
    return val;
}

static char next_char( char **c)
{
    ignore_ws (c);
    return *(*c)++;
}

static int more_params (char **c)
{
    ignore_ws (c);
    return (**c) != 0;
}

static void dumpmem (uaecptr addr, uaecptr *nxmem, int lines)
{
    broken_in = 0;
    for (;lines-- && !broken_in;) {
	int i;
	console_out ("%08lx ", addr);
	for (i = 0; i < 16; i++) {
	    console_out ("%04x ", get_word(addr)); addr += 2;
	}
	console_out ("\n");
    }
    *nxmem = addr;
}

static void foundmod (uae_u32 ptr, char *type)
{
    char name[21];
    uae_u8 *ptr2 = chipmemory + ptr;
    int i,length;

    console_out ("Found possible %s module at 0x%lx.\n", type, ptr);
    memcpy (name, ptr2, 20);
    name[20] = '\0';

    /* Browse playlist */
    length = 0;
    for (i = 0x3b8; i < 0x438; i++)
	if (ptr2[i] > length)
	    length = ptr2[i];

    length = (length+1)*1024 + 0x43c;

    /* Add sample lengths */
    ptr2 += 0x2A;
    for (i = 0; i < 31; i++, ptr2 += 30)
	length += 2*((ptr2[0]<<8)+ptr2[1]);
    
    console_out ("Name \"%s\", Length 0x%lx bytes.\n", name, length);
}

static void modulesearch (void)
{
    uae_u8 *p = get_real_address (0);
    uae_u32 ptr;

    for (ptr = 0; ptr < allocated_chipmem - 40; ptr += 2, p += 2) {
	/* Check for Mahoney & Kaktus */
	/* Anyone got the format of old 15 Sample (SoundTracker)modules? */
	if (ptr >= 0x438 && p[0] == 'M' && p[1] == '.' && p[2] == 'K' && p[3] == '.')
	    foundmod (ptr - 0x438, "ProTracker (31 samples)");

	if (ptr >= 0x438 && p[0] == 'F' && p[1] == 'L' && p[2] == 'T' && p[3] == '4')
	    foundmod (ptr - 0x438, "Startrekker");

	if (strncmp ((char *)p, "SMOD", 4) == 0) {
	    console_out ("Found possible FutureComposer 1.3 module at 0x%lx, length unknown.\n", ptr);
	}
	if (strncmp ((char *)p, "FC14", 4) == 0) {
	    console_out ("Found possible FutureComposer 1.4 module at 0x%lx, length unknown.\n", ptr);
	}
	if (p[0] == 0x48 && p[1] == 0xe7 && p[4] == 0x61 && p[5] == 0
	    && p[8] == 0x4c && p[9] == 0xdf && p[12] == 0x4e && p[13] == 0x75
	    && p[14] == 0x48 && p[15] == 0xe7 && p[18] == 0x61 && p[19] == 0
	    && p[22] == 0x4c && p[23] == 0xdf && p[26] == 0x4e && p[27] == 0x75) {
	    console_out ("Found possible Whittaker module at 0x%lx, length unknown.\n", ptr);
	}
	if (p[4] == 0x41 && p[5] == 0xFA) {
	    int i;

	    for (i = 0; i < 0x240; i += 2)
		if (p[i] == 0xE7 && p[i + 1] == 0x42 && p[i + 2] == 0x41 && p[i + 3] == 0xFA)
		    break;
	    if (i < 0x240) {
		uae_u8 *p2 = p + i + 4;
		for (i = 0; i < 0x30; i += 2)
		    if (p2[i] == 0xD1 && p2[i + 1] == 0xFA) {
			console_out ("Found possible MarkII module at %lx, length unknown.\n", ptr);
		    }
	    }
		
	}
    }
}

/* cheat-search by Holger Jakob */
static void cheatsearch (char **c)
{
    uae_u8 *p = get_real_address (0);
    static uae_u32 *vlist = NULL;
    uae_u32 ptr;
    uae_u32 val = 0;
    uae_u32 type = 0; /* not yet */
    uae_u32 count = 0;
    uae_u32 fcount = 0;
    uae_u32 full = 0;
    char nc;

    ignore_ws (c);

    while (isxdigit (nc = **c)) {
	(*c)++;
	val *= 10;
	nc = toupper (nc);
	if (isdigit (nc)) {
	    val += nc - '0';
	}
    }
    if (vlist == NULL) {
	vlist = malloc (256*4);
	if (vlist != 0) {
	    for (count = 0; count<255; count++)
		vlist[count] = 0;
	    count = 0;
	    for (ptr = 0; ptr < allocated_chipmem - 40; ptr += 2, p += 2) {
		if (ptr >= 0x438 && p[3] == (val & 0xff)
		    && p[2] == (val >> 8 & 0xff)
		    && p[1] == (val >> 16 & 0xff)
		    && p[0] == (val >> 24 & 0xff))
		{
		    if (count < 255) {
			vlist[count++]=ptr;
			console_out ("%08x: %x%x%x%x\n",ptr,p[0],p[1],p[2],p[3]);
		    } else
			full = 1;
		}
	    }
	    console_out ("Found %d possible addresses with %d\n",count,val);
	    console_out ("Now continue with 'g' and use 'C' with a different value\n");
	}
    } else {
	for (count = 0; count<255; count++) {
	    if (p[vlist[count]+3] == (val & 0xff)
		&& p[vlist[count]+2] == (val>>8 & 0xff) 
		&& p[vlist[count]+1] == (val>>16 & 0xff)
		&& p[vlist[count]] == (val>>24 & 0xff))
	    {
		fcount++;
		console_out ("%08x: %x%x%x%x\n", vlist[count], p[vlist[count]],
			p[vlist[count]+1], p[vlist[count]+2], p[vlist[count]+3]);
	    }
	}
	console_out ("%d hits of %d found\n",fcount,val);
	free (vlist);
	vlist = NULL;
    }
}

static void writeintomem (char **c)
{
    uae_u8 *p = get_real_address (0);
    uae_u32 addr = 0;
    uae_u32 val = 0;
    char nc;

    ignore_ws(c);
    while (isxdigit(nc = **c)) {
	(*c)++;
	addr *= 16;
	nc = toupper(nc);
	if (isdigit(nc)) {
	    addr += nc - '0';
	} else {
	    addr += nc - 'A' + 10;
	}
    }
    ignore_ws(c);
    while (isxdigit(nc = **c)) {
	(*c)++;
	val *= 10;
	nc = toupper(nc);
	if (isdigit(nc)) {
	    val += nc - '0';
	}
    }

    if (addr < allocated_chipmem) {
      p[addr] = val>>24 & 0xff;
      p[addr+1] = val>>16 & 0xff;
      p[addr+2] = val>>8 & 0xff;
      p[addr+3] = val & 0xff;
      console_out("Wrote %d at %08x\n",val,addr);
    } else
      console_out("Invalid address %08x\n",addr);
}
 
void debug (void)
{
    char input[80];
    uaecptr nextpc,nxdis,nxmem;

    bogusframe = 1;

    if (do_skip && (m68k_getpc() != skipaddr/* || regs.a[0] != 0x1e558*/)) {
	regs.spcflags |= SPCFLAG_BRK;
	return;
    }
    do_skip = 0;

#ifdef NEED_TO_DEBUG_BADLY
    history[lasthist] = regs;
    historyf[lasthist] = regflags;
#else
    history[lasthist] = m68k_getpc();
#endif
    if (++lasthist == MAX_HIST) lasthist = 0;
    if (lasthist == firsthist) {
	if (++firsthist == MAX_HIST) firsthist = 0;
    }

    m68k_dumpstate (&nextpc);
    nxdis = nextpc; nxmem = 0;

    for (;;) {
	char cmd, *inptr;

	console_out (">");
    console_flush ();

    if (!console_get (input, 80)) return;

	inptr = input;
	cmd = next_char (&inptr);
	switch (cmd) {
	 case 'c': dumpcia (); dumpcustom (); break;
	 case 'r': m68k_dumpstate (&nextpc); break;
	 case 'M': modulesearch (); break;
	 case 'C': cheatsearch (&inptr); break; 
	 case 'W': writeintomem (&inptr); break;
	 case 'S':
	    {
		uae_u8 *memp;
		uae_u32 src, len;
		char *name;
		FILE *fp;

		if (!more_params (&inptr))
		    goto S_argh;

		name = inptr;
		while (*inptr != '\0' && !isspace (*inptr))
		    inptr++;
		if (!isspace (*inptr))
		    goto S_argh;

		*inptr = '\0';
		inptr++;
		if (!more_params (&inptr))
		    goto S_argh;
		src = readhex (&inptr);
		if (!more_params (&inptr))
		    goto S_argh;
		len = readhex (&inptr);
		if (! valid_address (src, len)) {
		    console_out ("Invalid memory block\n");
		    break;
		}
		memp = get_real_address (src);
		fp = fopen (name, "w");
		if (fp == NULL) {
		    console_out ("Couldn't open file\n");
		    break;
		}
		if (fwrite (memp, 1, len, fp) != len) {
		    console_out ("Error writing file\n");
		}
		fclose (fp);
		break;

		S_argh:
		console_out ("S command needs more arguments!\n");
		break;
	    }
	 case 'd':
	    {
		uae_u32 daddr;
		int count;

		if (more_params(&inptr))
		    daddr = readhex(&inptr);
		else
		    daddr = nxdis;
		if (more_params(&inptr))
		    count = readhex(&inptr);
		else
		    count = 10;
		m68k_disasm(daddr, &nxdis, count);
	    }
	    break;
	 case 't': regs.spcflags |= SPCFLAG_BRK; return;
	 case 'z':
	    skipaddr = nextpc;
	    do_skip = 1;
	    regs.spcflags |= SPCFLAG_BRK;
	    return;

	 case 'f':
	    skipaddr = readhex(&inptr);
	    do_skip = 1;
	    regs.spcflags |= SPCFLAG_BRK;
	    return;

	 case 'q': uae_quit();
	    debugger_active = 0;
	    debugging = 0;
	    return;

	 case 'g':
	    if (more_params (&inptr))
		m68k_setpc (readhex (&inptr));
	    fill_prefetch_0 ();
	    debugger_active = 0;
	    debugging = 0;
	    return;

	 case 'H':
	    {
		int count;
		int temp;
#ifdef NEED_TO_DEBUG_BADLY
		struct regstruct save_regs = regs;
		union flagu save_flags = regflags;
#endif

		if (more_params(&inptr))
		    count = readhex(&inptr);
		else
		    count = 10;
		if (count < 0)
		    break;
		temp = lasthist;
		while (count-- > 0 && temp != firsthist) {
		    if (temp == 0) temp = MAX_HIST-1; else temp--;
		}
		while (temp != lasthist) {
#ifdef NEED_TO_DEBUG_BADLY
		    regs = history[temp];
		    regflags = historyf[temp];
		    m68k_dumpstate(NULL);
#else
		    m68k_disasm(history[temp], NULL, 1);
#endif
		    if (++temp == MAX_HIST) temp = 0;
		}
#ifdef NEED_TO_DEBUG_BADLY
		regs = save_regs;
		regflags = save_flags;
#endif
	    }
	    break;
	 case 'm':
	    {
		uae_u32 maddr; int lines;
		if (more_params(&inptr))
		    maddr = readhex(&inptr);
		else
		    maddr = nxmem;
		if (more_params(&inptr))
		    lines = readhex(&inptr);
		else
		    lines = 16;
		dumpmem(maddr, &nxmem, lines);
	    }
	    break;
	  case 'h':
	  case '?':
	    {
		console_out ("          HELP for UAE Debugger\n");
		console_out ("         -----------------------\n\n");
		console_out ("  g: <address>          Start execution at the current address or <address>\n");
		console_out ("  c:                    Dump state of the CIA and custom chips\n");
		console_out ("  r:                    Dump state of the CPU\n");
		console_out ("  m <address> <lines>:  Memory dump starting at <address>\n");
		console_out ("  d <address> <lines>:  Disassembly starting at <address>\n");
		console_out ("  t:                    Step one instruction\n");
		console_out ("  z:                    Step through one instruction - useful for JSR, DBRA etc\n");
		console_out ("  f <address>:          Step forward until PC == <address>\n");
		console_out ("  H <count>:            Show PC history <count> instructions\n");
		console_out ("  M:                    Search for *Tracker sound modules\n");
		console_out ("  C <value>:            Search for values like energy or lifes in games\n");
		console_out ("  W <address> <value>:  Write into Amiga memory\n");
		console_out ("  S <file> <addr> <n>:  Save a block of Amiga memory\n");
		console_out ("  h,?:                  Show this help page\n");
		console_out ("  q:                    Quit the emulator. You don't want to use this command.\n\n");
	    }
	    break;

	}
    }
}
