home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 405.lha / AppleII_Emulators_src / src-2 / ap_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-28  |  6.4 KB  |  296 lines

  1. #include <cpu_prog_model.h>
  2. #include <stdio.h>
  3. #include <term.h>
  4.  
  5. #define FALSE 0
  6. #define TRUE  1
  7.  
  8.  
  9. struct term term_buffer;
  10.  
  11. fortran void mkon$p();
  12.  
  13. void    reset(),
  14.         cpu(),
  15.         reg_dump(),
  16.       * malloc(),
  17.         obj_reader(),
  18.         auto_list();
  19.  
  20. short   hex_reader();
  21.  
  22. char  * lsdir(),
  23.       * dis_ass();
  24.  
  25. strtoupper( string)
  26. char *string;
  27. {
  28.    while (*string)    *string++ = toupper( *string);
  29. }
  30.  
  31. without_sufix( path_name)
  32. char *path_name;
  33. {
  34.    char *temp_pointer;
  35.  
  36.    temp_pointer = path_name + strlen( path_name);
  37.    while( (temp_pointer > path_name) && (*temp_pointer != '.') )
  38.       temp_pointer--;
  39.  
  40.    *temp_pointer = NULL;
  41. }
  42.  
  43. show_dis( code)
  44. PM *code;
  45. {
  46.    short loop;
  47.    char  key;
  48.  
  49.    printf("...");
  50.    do
  51.    {
  52.       printf("\010\010\010");
  53.       for (loop = 0;loop < 20;loop ++)
  54.       {
  55.          short new_line_flag,
  56.                length;
  57.  
  58.          if (code->St == code->PC)
  59.          {
  60.             printf( "                     ORG   $%04x\n\n", code->PC );
  61.             loop += 2;
  62.          }
  63.  
  64.          new_line_flag = FALSE;
  65.          if (code->Me [code->PC] == 0x60)
  66.          new_line_flag = TRUE;
  67.  
  68.          printf( "%s\n", dis_ass( code, &length) );
  69.          code->PC += length;
  70.          if (new_line_flag)
  71.          {
  72.             printf("\n");
  73.             loop++;
  74.          }
  75.       }
  76.  
  77.       printf("...");
  78.    }  while ( (key = toupper( getchar() )), key != 'N' &&
  79.                                             key != 'Q'    );
  80.    printf("\n");
  81. }
  82.  
  83. short * code_Qu,
  84.       * code_Mo,
  85.         Reset_Flg;
  86.  
  87. fortran Ctrl_P( dummy)
  88. int *dummy;
  89. {
  90.    *code_Qu  = TRUE;
  91.    Reset_Flg = TRUE;
  92.    *code_Mo  = Mode_FAST;
  93.    char_take_back( 0x8D);
  94. }
  95.  
  96. main( argc, argv)
  97. int   argc;
  98. char *argv [];
  99. {
  100.    PM   code;
  101.  
  102.    int file_unit;
  103.  
  104.    char   file_name   [256],
  105.           list_buffer [256],
  106.         * temp_pointer;
  107.  
  108.    short  old_term,
  109.           arg_counter,
  110.           list_buff_pos,
  111.           number_of_files,
  112.           loop;
  113.  
  114.    gterm( &term_buffer);
  115.    old_term = term_buffer.tt_flags;
  116.    term_buffer.tt_flags = BREAK | RAW;
  117.    sterm( &term_buffer);
  118.  
  119.    code_Qu   = &(code.Qu);
  120.    code_Mo   = &(code.Mo);
  121.    Reset_Flg = FALSE;
  122.  
  123.    code.Me = (unsigned short int *) malloc( sizeof (short int) * 65536);
  124.    if (code.Me == NIL)
  125.    {
  126.       printf("Couldn't allocate the memory for you.\n");
  127.       exit (-1);
  128.    }
  129.  
  130.    file_unit = open( APPLE_PROGS, 0);
  131.    if (file_unit == -1)
  132.    {
  133.       printf("Can't find the library.\n");
  134.       exit (-1);
  135.    }
  136.  
  137.    while (temp_pointer = lsdir( file_unit) )
  138.    {
  139.       char buffer [256];
  140.  
  141.       strcpy( file_name, temp_pointer);
  142.       sprintf( buffer, "%s>%s", APPLE_PROGS, file_name);
  143.       if (ftype( buffer) > 1)
  144.          continue;
  145.  
  146.       /* Make shoure that is only with .OBJ as a sufix */
  147.       without_sufix( file_name);
  148.       strcat( file_name, ".OBJ");
  149.       obj_reader( &code, file_name);
  150.  
  151.    }
  152.    close( file_unit);
  153.  
  154.    reset( &code);
  155.    code.Mo = Mode_FAST;
  156.    code.Ex = FALSE;
  157.    code.Tr = FALSE;
  158.    code.Tl = 0;
  159.    code.Sl = 0x00;
  160.  
  161.    /* Check if trace is requested */
  162.    mem_check( 0xC000, &(code.Ac), &code, Func_LOAD);
  163.  
  164.    if (argc >= 2)
  165.    {
  166.       arg_counter     = 1;
  167.       list_buff_pos   = 0;
  168.       number_of_files = 0;
  169.  
  170.       while (argc > arg_counter)
  171.       {
  172.          strtoupper( argv [arg_counter]);
  173.  
  174.          switch (*argv [arg_counter])
  175.          {
  176.             case '-':
  177.                switch (argv [arg_counter][1])
  178.                {
  179.                   case 'D':
  180.                      if ( (argc > (arg_counter + 1) ) &&
  181.                           (*argv [arg_counter + 1] == '$') )
  182.                      {
  183.                         sscanf( argv [arg_counter + 1] + 1, "%x", &code.St);
  184.                         arg_counter += 2;
  185.                      }
  186.                      else
  187.                         arg_counter ++;
  188.  
  189.                      show_dis( &code);
  190.                      break;
  191.  
  192.                   case 'T':
  193.                      code.Tr = TRUE;
  194.                      arg_counter++;
  195.                      break;
  196.  
  197.                   default:
  198.                      printf("Can't undersatnd %s.\n", argv [2]);
  199.                      exit (-1);
  200.                }
  201.                break;
  202.  
  203.             default:
  204.                strcpy( file_name, argv [arg_counter]);
  205.                strcat( file_name, ".HEX");
  206.                if (hex_reader( &code, file_name) )
  207.                {
  208.                   short temp_pos;
  209.  
  210.                   list_buffer [list_buff_pos++] = code.St & 0xFF;
  211.                   list_buffer [list_buff_pos++] = code.St >> 8;
  212.                   temp_pos = 0;
  213.                   while (file_name [temp_pos])
  214.                      list_buffer [list_buff_pos++] = file_name [temp_pos++];
  215.                   list_buffer [list_buff_pos++] = NULL;
  216.                   number_of_files ++;
  217.                   arg_counter++;
  218.                   break;
  219.                }
  220.  
  221.                printf("Can't open the file %s\n", argv [arg_counter]);
  222.                exit( -1);
  223.  
  224.          }  /* End of switch (*argv [arg_counter]) */
  225.       }  /* End of while (argc > arg_counter) */
  226.  
  227.       list_buffer [list_buff_pos + 2] = NULL;
  228.  
  229.       if (number_of_files)
  230.       {
  231.          auto_list( &code, list_buffer, number_of_files);
  232.          temp_pointer = "\nCALL 49424 : RUN\n";
  233.          loop = 0;
  234.          while (temp_pointer [loop])
  235.             char_take_back( temp_pointer [loop++]);
  236.       }
  237.  
  238.    }  /* End if( argc <= 2) */
  239.  
  240. /*   mkon$p( "QUIT$", 5, Ctrl_P);   /* Quit$ = 5 letters */
  241.  
  242.    while (!code.Er && !code.Ex)
  243.    {
  244.       printf("\nEntering mode (");
  245.  
  246.       switch (code.Mo)
  247.       {
  248.          case Mode_FAST:
  249.             printf("Fast)\n");
  250.             code.Qu = FALSE;
  251.             fast_cpu( &code);
  252.             break;
  253.  
  254.          case Mode_SLOW:
  255.             printf("Slow)\n");
  256.             do
  257.             {
  258.                slow_cpu( &code);
  259.             }  while( !code.Qu);
  260.             break;
  261.       }
  262.  
  263.       if (Reset_Flg)
  264.       {
  265.          home( &code);
  266.          printf("<RESET>    Press Esc two times and Q to Quit.\n");
  267.          reset( &code);
  268.          Reset_Flg = FALSE;
  269.          code.Qu   = FALSE;
  270.       }
  271.  
  272.       if (code.Er)
  273.       {
  274.          short steps;
  275.  
  276.          printf("Error:\n");
  277.          printf("%s\n", dis_ass( &code, &steps) );
  278.          reg_dump( &code);
  279.          printf("Quit or Continue  :");
  280.          if (toupper( getchar()) == 'C')
  281.          {
  282.             code.Tr = FALSE;
  283.             code.Qu = FALSE;
  284.             reset( &code);
  285.          }
  286.          else
  287.             printf("\n");
  288.       }
  289.  
  290.    }
  291.  
  292.    term_buffer.tt_flags = old_term;
  293.    sterm( &term_buffer);
  294. }
  295.  
  296.