home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / emulate / x_comp / 8048dis / 8048dis.c next >
Text File  |  1987-09-26  |  9KB  |  300 lines

  1. /*
  2.     8048/8049 Disassembler for the PC.
  3.         Written in Turbo 'C' by:
  4.         Kirk Lanctot
  5.         4511 E. Wood St.
  6.         Phoenix,AZ 85040
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <dos.h>
  11. #include <dir.h>
  12. #include <fcntl.h>
  13. #include <stat.h>
  14. char *menom_array[256] =
  15. {
  16.      "NOP","X","OUTL BUS,A","ADD A,","JMP 0","EN I","X","DEC A",
  17.      "INS A,BUS","IN A,P1","IN A,P2","X","MOVD A,P4","MOVD A,P5",
  18.      "MOVD A,P6","MOVD A,P7",
  19.      "INC @ R0","INC @ R1","JB0,","ADDC A,","CALL 0","DIS I","JTF ","INC A",
  20.      "INC R0","INC R1","INC R2","INC R3","INC R4","INC R5","INC R6","INC R7",
  21.      "XCHA,@ R0","XCHA,@ R1","X","MOV A,","JMP 1","EN TCNTI","JNT0,","CLR A",
  22.      "XCH A,R0","XCH A,R1","XCH A,R2","XCH A,R3","XCH A,R4","XCH A,R5","XCH A,R6","XCH A,R7",
  23.      "XCHD A,@ R0","XCHD A,@ R1","JB1,","X","CALL 1","DIS TCNTI","JT0,","CPL A",
  24.      "X","OUTL P1,A","OUTL P2,A","X","MOVD P4,A","MOVD P5,A","MOVD P6,A","MOVD P7,A",
  25.      "ORL A,@ R0","ORL A,@ R1","MOV A,T","ORL A,","JMP 2","STRT CNT","JNT1,","SWAP A",
  26.      "ORL A,R0","ORL A,R1","ORL A,R2","ORL A,R3","ORL A,R4","ORL A,R5","ORL A,R6","ORL A,R7",
  27.      "ANL A,@ R0","ANL A,@ R1","JB2,","ANL A,","CALL 2","STRT T","JT1,","DAA A",
  28.      "ANL A,R0","ANL A,R1","ANL A,R2","ANL A,R3","ANL A,R4","ANL A,R5","ANL A,R6","ANL A,R7",
  29.      "ADD A,@ R0","ADD A,@ R1","MOV T,A","X","JMP 3","STOP TCNT","X","RRC A",
  30.      "ADD A,R0","ADD A,R1","ADD A,R2","ADD A,R3","ADD A,R4","ADD A,R5","ADD A,R6","ADD A,R7",
  31.      "ADC A,@ R0","ADC A,@ R1","JB3,","X","CALL 3","ENTO CLK","JF1,","RR A",
  32.      "ADC A,R0","ADC A,R1","ADC A,R2","ADC A,R3","ADC A,R4","ADC A,R5","ADC A,R6","ADC A,R7",
  33.      "MOVX A,@ R0","MOVX A,@ R1","X","RET","JMP 4","CLR F0","JNI,","X",
  34.      "ORL BUS,","ORL P1,","ORL P2,","X","ORLD P4,A","ORLD P5,A","ORLD P6,A","ORLD P7,A",
  35.      "MOVX @ R0,A","MOVX @ R1,A","JB4,","RETR","CALL 4","CPL F0","JNZ,","CLR C",
  36.      "X","ANL P1,","ANL P2,","X","ANLD P4,A","ANLD P5,A","ANLD P6,A","ANLD P7,A",
  37.      "MOV @ R0,A","MOV @ R1,A","X","MOVP A,@ A","JMP 5","CLR F1","X","CPL C",
  38.      "MOV R0,A","MOV R1,A","MOV R2,A","MOV R3,A","MOV R4,A","MOV R5,A","MOV R6,A","MOV R7,A",
  39.      "MOV @ R0,","MOV @ R1,","JB5 ","JMPP @ A","CALL 5","CPL F1","JF0 ","X",
  40.      "MOV R0,","MOV R1,","MOV R2,","MOV R3,","MOV R4,","MOV R5,","MOV R6,","MOV R7,",
  41.      "X","X","X","X","JMP 6","SEL RB0","JZ ","MOV A,PSW",
  42.      "DEC R0","DEC R1","DEC R2","DEC R3","DEC R4","DEC R5","DEC R6","DEC R7",
  43.      "XRL A,@ R0","XRL A,@ R1","JB6 ","XRL A,","CALL 6","SEL RB1","X","MOV PSW,A",
  44.      "XRL A,R0","XRL A,R1","XRL A,R2","XRL A,R3","XRL A,R4","XRL A,R5","XRL A,R6","XRL A,R7",
  45.      "X","X","X","MOVP3 A,@ A","JMP 7","SEL MB0","JNC ","RL A",
  46.      "DJNZ R0 ","DJNZ R1 ","DJNZ R2 ","DJNZ R3 ","DJNZ R4 ","DJNZ R5 ","DJNZ R6 ","DJNZ R7 ",
  47.      "MOV A,@ R0","MOV A,@ R1","JB7 ","X","CALL 7","SEL MB1","JC ","RLC A",
  48.      "MOV A,R0","MOV A,R1","MOV A,R2","MOV A,R3","MOV A,R4","MOV A,R5","MOV A,R6","MOV A,R7"
  49. };
  50.  
  51. int count_array[256] =
  52. {
  53.      0,2,0,1,1,0,2,0,0,0,0,2,0,0,0,0,
  54.      0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,
  55.      0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,
  56.      0,0,1,2,1,0,1,0,2,0,0,2,0,0,0,0,
  57.      0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,
  58.      0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,
  59.      0,0,0,2,1,0,2,0,0,0,0,0,0,0,0,0,
  60.      0,0,1,2,1,0,1,0,0,0,0,0,0,0,0,0,
  61.      0,0,2,0,1,0,1,2,1,1,1,2,0,0,0,0,
  62.      0,0,1,0,1,0,1,0,2,1,1,2,0,0,0,0,
  63.      0,0,2,0,1,0,2,0,0,0,0,0,0,0,0,0,
  64.      1,1,1,0,1,0,1,2,1,1,1,1,1,1,1,1,
  65.      2,2,2,2,1,0,1,0,0,0,0,0,0,0,0,0,
  66.      0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,
  67.      2,2,2,0,1,0,1,0,1,1,1,1,1,1,1,1,
  68.      0,0,1,2,1,0,1,0,0,0,0,0,0,0,0,0
  69.      };
  70.  
  71. int repeat,ok_value,read_byte,count_val,read_byte2;
  72. int found,status,handle,offset,offsetread,startoff,endoff;
  73. char response,filename[32],filename_out[32];
  74. FILE *input_file,*output_file,*dummy_file;
  75. union REGS REG;
  76.  
  77. /* start of main program */
  78.  
  79. main()
  80. {
  81. repeat=1;
  82. while (repeat==1)
  83.   {
  84.   cls();
  85.   gotoxy(0,0);
  86.   directory("*.B48");
  87.   printf("\n");
  88.   open_file_input();
  89.   printf("\n");
  90.   directory("*.DIS");
  91.   printf("\n");
  92.   open_file_output();
  93.   get_offset();
  94.   process_line();
  95.   dump_file();
  96.   printf("\n");
  97.   printf(" Type E to exit or Return to restart  ");
  98.   if ((response=toupper(getche()))=='E')
  99.     repeat=0;
  100.   }
  101.   fcloseall();
  102. }
  103. /* end of main program */
  104.  
  105. open_file_input()
  106.         {
  107.         found=0;
  108.         while (found==0)
  109.             {
  110.                 printf("Enter file name to disassemble \n");
  111.                 scanf("%s",filename);
  112.         handle=open(filename,O_RDONLY|O_BINARY|O_DENYALL);
  113.                 input_file=fdopen(handle,"rb");
  114.         if (input_file==NULL)
  115.               {
  116.              printf("failed to find file - try again \n");
  117.                     }
  118.                 else
  119.                 {
  120.           printf("file found - please wait \n");
  121.           found=1;
  122.         }
  123.           }
  124.     }
  125.  
  126. open_file_output()
  127.         {
  128.         found=0;
  129.         while (found==0)
  130.               {
  131.             printf("Enter destination file name \n");
  132.             scanf("%s",filename_out);
  133.         handle=creat(filename_out,O_CREAT|O_TEXT|S_IWRITE);
  134.               output_file=fdopen(handle,"wt");
  135.                 if (output_file==NULL)
  136.                printf("failed to open file - try again \n");
  137.                 else
  138.            {
  139.            printf("file opened - please wait \n");
  140.            found=1;
  141.            }
  142.             } /* end of found==1 */
  143.     } /* end of open_file_output */
  144.  
  145. get_offset()
  146.   {
  147.   startoff=0x0;
  148.   endoff=0x0;
  149.   ok_value=0;
  150.   while (ok_value==0)
  151.       {
  152.       printf("\n");
  153.     printf("Enter starting offset (0-7fff)  ");
  154.     scanf("%4x",&startoff);
  155.     printf("Enter ending offset (0-7fff)    ");
  156.     scanf("%4x",&endoff);
  157.     if (startoff>=0 && endoff<=0x7fff && endoff>=startoff)
  158.     ok_value=1;
  159.     }
  160.   }
  161.  
  162. process_line()
  163. {
  164. pout_eol();
  165. fprintf(output_file,"DISASSEMBLY LISTING OF ");
  166. fprintf(output_file,"%s",filename);
  167. pout_eol();
  168. pout_eol();
  169. status=fseek(input_file,startoff,SEEK_SET);
  170. for (offsetread=startoff;offsetread<=endoff;offsetread++)
  171.   {
  172.   read_byte=getc(input_file);
  173.   count_val=count_array[read_byte];
  174.  
  175.    if (count_val== 0)
  176.           {
  177.           pout_address(offsetread);
  178.           pout_byte(read_byte);
  179.           pout_space();
  180.           pout_menomic(read_byte);
  181.              pout_eol();    
  182.           };
  183.    if (count_val== 1)
  184.           {
  185.           pout_address(offsetread);
  186.           pout_byte(read_byte);
  187.        read_byte2=getc(input_file);
  188.        pout_byte(read_byte2);
  189.        pout_menomic(read_byte);
  190.          pout_bytetail(read_byte2);
  191.           offsetread++;
  192.        pout_eol();
  193.           };
  194.    if (count_val == 2)
  195.        {
  196.           pout_address(offsetread);
  197.           pout_byte(read_byte);
  198.           pout_space();
  199.           pout_illeg_menom();
  200.        pout_eol();
  201.           };
  202.   }
  203. }  /* end of process_line */
  204.  
  205. /* functions */
  206.  
  207. cls()         /* Clear the screen  */
  208.    {
  209.    REG.x.ax = 0x0600;
  210.    REG.h.bh = 0x07;
  211.    REG.x.cx = 0X0000;
  212.    REG.x.dx = 0x184f;
  213.    int86(0x10, ®, ®);
  214.    }
  215.  
  216. gotoxy(x, y)   /* position cursor at x,y  0,0 being upper left */
  217. int x, y;
  218. {
  219.     union REGS REG;
  220.  
  221.     REG.h.ah = 02;
  222.     REG.h.bh = 0; /* Get current page */
  223.     REG.h.dh = x;
  224.     REG.h.dl = y;
  225.     int86(0x10, ®, ®);
  226. }
  227.  
  228. directory(char *sear)
  229.    {
  230.    struct ffblk ffblk;
  231.    int done;
  232.    printf("Directory of files \n");
  233.    done=findfirst(sear,&ffblk,0);
  234.    while (!done)
  235.       {
  236.       printf("  %s\n", ffblk.ff_name);
  237.       done=findnext(&ffblk);
  238.       }
  239.    pout_eol();
  240.    }
  241.  
  242.  
  243. pout_address(addr)
  244.    {
  245.    fprintf(output_file,"%.4X:  ",addr);
  246.    }
  247. pout_menomic(opcode)
  248.    {
  249.    fprintf(output_file,"%s",menom_array[opcode]);
  250.    }
  251. pout_space()
  252.    {
  253.    fprintf(output_file,"    ");
  254.    }
  255. pout_byte(code)
  256.    {
  257.    fprintf(output_file,"%.2X  ",code);
  258.    }
  259. pout_byte2(code)
  260.    {
  261.    fprintf(output_file,"%.2X",code);
  262.    }
  263. pout_bytetail(code)
  264.    {
  265.    fprintf(output_file,"%-.2X",code);
  266.    }
  267. pout_illeg_menom()
  268.    {
  269.    fprintf(output_file,"*** ILLEGAL *** ");
  270.    }
  271. pout_eol()
  272.    {
  273.    fprintf(output_file,"\n");
  274.    }
  275.  
  276. dump_file()
  277.    {
  278.    pout_eol();
  279.    pout_eol();
  280.    printf("Starting Hex Dump Listing\n");
  281.    fprintf(output_file,"Hex Dump Listing\n");
  282.    get_offset();
  283.    status=fseek(input_file,startoff,SEEK_SET);
  284.    for(offsetread=startoff;offsetread<=endoff;offsetread++)
  285.       {
  286.       pout_address(offsetread);
  287.       offsetread=offsetread+15;
  288.       for(count_val=0;count_val<16 || !eof(input_file);count_val++)
  289.         {
  290.         if (count_val % 4 == 0 && count_val != 0)
  291.            fprintf(output_file," - ");
  292.         else
  293.            fprintf(output_file," ");
  294.         read_byte=getc(input_file);
  295.         pout_byte2(read_byte);
  296.         }
  297.       pout_eol();
  298.       }
  299.    }
  300.