Add VCS links
[debian-dgen.git] / dz80 / example.c
blob312a7c30118aac548aebe565dd3b08933416f36f
1 /*
2 example.c
4 How to use dZ80's disassembler in your own programs.
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <malloc.h>
10 #include <stdlib.h>
12 #include "types.h"
13 #include "dissz80.h"
15 /* Use the example Z80 file supplied with the command line version of dZ80 */
16 const char *pZ80Filename = "mayhem.sna";
18 int main(void)
20 FILE *f;
21 BYTE *pMem; /* Pointer to our Z80's memory */
22 DISZ80 *d; /* Pointer to the Disassembly structure */
23 int line, err; /* line count */
24 WORD dAddr; /* Disassembly address */
26 /* Allocate the dZ80 structure */
27 d = malloc(sizeof(DISZ80));
28 if (d == NULL)
30 printf("Cannot allocate %d bytes\n", sizeof(DISZ80));
31 exit(1);
34 memset(d, 0, sizeof(DISZ80));
36 /* Allocate the memory space for our virtual Z80 */
37 pMem = malloc(Z80MEMSIZE);
38 if (pMem == NULL)
40 free(d);
41 printf("Cannot allocate %d bytes\n", Z80MEMSIZE);
42 exit(1);
45 memset(pMem, 0, Z80MEMSIZE);
47 f = fopen(pZ80Filename, "rb");
48 if (f == NULL)
50 printf("Stone the crows - couldn't open %s\n", pZ80Filename);
51 exit(1);
53 else
55 fseek(f, 27, SEEK_SET); /* Skip the .sna's header - go straight to the memory dump */
56 fread(pMem + 16384, 49152, 1, f);
57 fclose(f);
60 /* Starting disassembly address */
61 dAddr = 0x8000;
63 /* Set up dZ80's structure - it's not too fussy */
64 memset(d, 0, sizeof(DISZ80));
66 /* Set the default radix and strings (comments and "db") */
67 dZ80_SetDefaultOptions(d);
69 /* Set the CPU type */
70 d->cpuType = DCPU_Z80;
72 /* Set the start of the Z80's memory space */
73 d->mem0Start = pMem;
75 /* Indicate we're disassembling a single instruction */
76 d->flags |= DISFLAG_SINGLE;
78 /* And we're off! Let's disassemble 20 instructions from dAddr */
79 for(line=0; line < 20; line++)
81 /* Set the disassembly address */
82 d->start = d->end = dAddr;
84 err = dZ80_Disassemble(d);
85 if (err != DERR_NONE)
87 printf("**** dZ80 error: %s\n", dZ80_GetErrorText(err));
88 break;
91 /* Display the disassembled line, using the hex dump and disassembly buffers in the DISZ80 structure */
92 printf("%04x: %10s %s\n", dAddr, d->hexDisBuf, d->disBuf);
94 /* Point to the next instruction (bytesProcessed holds the number of bytes for the last instruction disassembled) */
95 dAddr += (WORD)d->bytesProcessed;
98 free(d);
99 free(pMem);
101 getc(stdin);
103 exit(0);