/*
example.c
How to use dZ80's disassembler in your own programs.
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include "types.h"
#include "dissz80.h"
/* Use the example Z80 file supplied with the command line version of dZ80 */
const char *pZ80Filename = "mayhem.sna";
int main(void)
{
FILE *f;
BYTE *pMem; /* Pointer to our Z80's memory */
DISZ80 *d; /* Pointer to the Disassembly structure */
int line, err; /* line count */
WORD dAddr; /* Disassembly address */
/* Allocate the dZ80 structure */
if (d == NULL)
{
printf("Cannot allocate %d bytes\n", sizeof(DISZ80
));
}
/* Allocate the memory space for our virtual Z80 */
if (pMem == NULL)
{
printf("Cannot allocate %d bytes\n", Z80MEMSIZE
);
}
f
= fopen(pZ80Filename
, "rb");
if (f == NULL)
{
printf("Stone the crows - couldn't open %s\n", pZ80Filename
);
}
else
{
fseek(f
, 27, SEEK_SET
); /* Skip the .sna's header - go straight to the memory dump */
fread(pMem
+ 16384, 49152, 1, f
);
}
/* Starting disassembly address */
dAddr = 0x8000;
/* Set up dZ80's structure - it's not too fussy */
/* Set the default radix and strings (comments and "db") */
dZ80_SetDefaultOptions(d);
/* Set the CPU type */
d->cpuType = DCPU_Z80;
/* Set the start of the Z80's memory space */
d->mem0Start = pMem;
/* Indicate we're disassembling a single instruction */
d->flags |= DISFLAG_SINGLE;
/* And we're off! Let's disassemble 20 instructions from dAddr */
for(line=0; line < 20; line++)
{
/* Set the disassembly address */
d->start = d->end = dAddr;
err = dZ80_Disassemble(d);
if (err != DERR_NONE)
{
printf("**** dZ80 error: %s\n", dZ80_GetErrorText
(err
));
break;
}
/* Display the disassembled line, using the hex dump and disassembly buffers in the DISZ80 structure */
printf("%04x: %10s %s\n", dAddr
, d
->hexDisBuf
, d
->disBuf
);
/* Point to the next instruction (bytesProcessed holds the number of bytes for the last instruction disassembled) */
dAddr += (WORD)d->bytesProcessed;
}
}