Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.         example.c
  3.  
  4.         How to use dZ80's disassembler in your own programs.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <stdlib.h>
  11.  
  12. #include "types.h"
  13. #include "dissz80.h"
  14.  
  15. /* Use the example Z80 file supplied with the command line version of dZ80 */
  16. const char *pZ80Filename = "mayhem.sna";
  17.  
  18. int main(void)
  19. {
  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 */
  25.  
  26. /* Allocate the dZ80 structure */
  27.         d = malloc(sizeof(DISZ80));
  28.         if (d == NULL)
  29.                 {
  30.                 printf("Cannot allocate %d bytes\n", sizeof(DISZ80));
  31.                 exit(1);
  32.                 }
  33.  
  34.         memset(d, 0, sizeof(DISZ80));
  35.  
  36. /* Allocate the memory space for our virtual Z80 */
  37.         pMem = malloc(Z80MEMSIZE);
  38.         if (pMem == NULL)
  39.                 {
  40.                 free(d);
  41.                 printf("Cannot allocate %d bytes\n", Z80MEMSIZE);
  42.                 exit(1);
  43.                 }
  44.  
  45.         memset(pMem, 0, Z80MEMSIZE);
  46.  
  47.         f = fopen(pZ80Filename, "rb");
  48.         if (f == NULL)
  49.                 {
  50.                 printf("Stone the crows - couldn't open %s\n", pZ80Filename);
  51.                 exit(1);
  52.                 }
  53.         else
  54.                 {
  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);
  58.                 }
  59.  
  60. /* Starting disassembly address */
  61.         dAddr = 0x8000;
  62.  
  63. /* Set up dZ80's structure - it's not too fussy */
  64.         memset(d, 0, sizeof(DISZ80));
  65.  
  66. /* Set the default radix and strings (comments and "db") */
  67.         dZ80_SetDefaultOptions(d);             
  68.  
  69. /* Set the CPU type */
  70.     d->cpuType = DCPU_Z80;
  71.  
  72. /* Set the start of the Z80's memory space */
  73.         d->mem0Start = pMem;
  74.  
  75. /* Indicate we're disassembling a single instruction */
  76.         d->flags |= DISFLAG_SINGLE;
  77.  
  78. /* And we're off! Let's disassemble 20 instructions from dAddr */
  79.         for(line=0; line < 20; line++)
  80.                 {
  81. /* Set the disassembly address */
  82.                 d->start = d->end = dAddr;
  83.                
  84.                 err = dZ80_Disassemble(d);
  85.                 if (err != DERR_NONE)
  86.                         {
  87.                         printf("**** dZ80 error:  %s\n", dZ80_GetErrorText(err));
  88.                         break;
  89.                         }
  90.                
  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);
  93.                
  94. /* Point to the next instruction (bytesProcessed holds the number of bytes for the last instruction disassembled) */
  95.                 dAddr += (WORD)d->bytesProcessed;
  96.                 }
  97.  
  98.         free(d);
  99.         free(pMem);
  100.  
  101.         getc(stdin);
  102.  
  103.         exit(0);
  104. }
  105.