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

  1. #include <cpu_prog_model.h>
  2. #include <stdio.h>
  3.  
  4. #define FALSE  0
  5. #define TRUE   1
  6.  
  7. int open();
  8.  
  9. void obj_reader( code, file_name)
  10. PM *code;
  11. char   file_name [];
  12. {
  13.    int    file_pointer;
  14.  
  15.    unsigned short int address_counter;
  16.  
  17.    char   in_buffer   [0x200],
  18.           name_buffer [256];
  19.    short  new_line,
  20.           bytes;
  21.  
  22.    sprintf( name_buffer, "%s>%s", APPLE_PROGS, file_name);
  23.    file_pointer = open( name_buffer, 0);    /* 0 = read */
  24.    if (file_pointer == -1)
  25.    {
  26.       printf("Couldn't open the file %s\n", file_name);
  27.       exit (-1);
  28.    }
  29.  
  30.    printf( "Reading from file %s :\n", file_name);
  31.    read( file_pointer, in_buffer, 2);
  32.    address_counter = (in_buffer [1] << 8) | in_buffer [0];
  33.    code->St = address_counter;
  34.  
  35. /*   printf("Start at $%04x:\n", address_counter);*/
  36.    new_line = TRUE;
  37.    while (bytes = read( file_pointer, in_buffer, 0x100) )
  38.    {
  39.       short loop;
  40.  
  41.       for (loop = 0;loop < bytes;loop ++)
  42.       {
  43.          code->Me [address_counter++] = in_buffer [loop];
  44.  
  45.          if (!(address_counter & 0x1FF) )
  46.          {
  47.             printf(".");
  48.             new_line = FALSE;
  49.          }
  50.  
  51.          if (!(address_counter & 0x7FFF) )
  52.          {
  53.             printf("\n");
  54.             new_line = TRUE;
  55.          }
  56.       }
  57.    }
  58. /*   printf("Ended to $%04x\n\n", (address_counter - 1) & 0xFFFF);*/
  59.  
  60.    if (!new_line)
  61.       printf("\n");
  62.  
  63.    close( file_pointer);
  64. }
  65.  
  66.  
  67.  
  68. short hex_reader( code, file_name)
  69. PM   * code;
  70. char   file_name [];
  71. {
  72.    FILE * file_pointer;
  73.  
  74.    short action_flag;
  75.  
  76.    unsigned short int address;
  77.  
  78.    int  length,
  79.         type,
  80.         byte,
  81.         checksum;
  82.  
  83.    action_flag = FALSE;
  84.    file_pointer = fopen( file_name, "r");
  85.    if (file_pointer)
  86.    {
  87.       printf("Reading from file %s :\n", file_name);
  88.       fscanf( file_pointer, ":%02x%04hx%02x", &length, &address, &type);
  89.       printf("Start at $%04x\n", address);
  90.       code->St = address;
  91.       action_flag = TRUE;
  92.  
  93.       do  /* Until type == 0x10 */
  94.       {
  95.          char buffer [256];
  96.  
  97.          while (length--)
  98.          {
  99.             fscanf( file_pointer, "%02x", &byte);
  100.             code->Me [address++] = byte;
  101.          }
  102.          fscanf( file_pointer, "%02x\n", &checksum);
  103.          printf(".");
  104.  
  105.          fscanf( file_pointer, ":%02x%04hx%02x", &length, &address, &type);
  106.       }  while (type != 0x01);
  107.  
  108.       printf("\n");
  109.       fclose( file_pointer);
  110.    }
  111.  
  112.    else
  113.       printf("Failed to open file \"%s\"\n", file_name);
  114.  
  115.    return action_flag;
  116. }
  117.  
  118.  
  119.