4 How to use dZ80's disassembler in your own programs.
15 /* Use the example Z80 file supplied with the command line version of dZ80 */
16 const char *pZ80Filename
= "mayhem.sna";
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
));
30 printf("Cannot allocate %d bytes\n", sizeof(DISZ80
));
34 memset(d
, 0, sizeof(DISZ80
));
36 /* Allocate the memory space for our virtual Z80 */
37 pMem
= malloc(Z80MEMSIZE
);
41 printf("Cannot allocate %d bytes\n", Z80MEMSIZE
);
45 memset(pMem
, 0, Z80MEMSIZE
);
47 f
= fopen(pZ80Filename
, "rb");
50 printf("Stone the crows - couldn't open %s\n", pZ80Filename
);
55 fseek(f
, 27, SEEK_SET
); /* Skip the .sna's header - go straight to the memory dump */
56 fread(pMem
+ 16384, 49152, 1, f
);
60 /* Starting disassembly address */
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 */
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
);
87 printf("**** dZ80 error: %s\n", dZ80_GetErrorText(err
));
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
;