home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.tar / ftp.whtech.com / Geneve / 9640news / CAT36 / EMULSRC.ZIP / DSK1.C < prev    next >
Text File  |  1993-05-13  |  3KB  |  137 lines

  1. /* DISK emulation */
  2.  
  3. void DISK(void)
  4. {
  5.     word i,        /* dummy */
  6.     dsrnamelength,    /* length of the DSR name (len(DSK1)=4) */
  7.     filenameptr,    /* left pointing after the DSR name (on the dot) */
  8.     startofpab,    /* VDP address of the PAB */
  9.     filenamelength,    /* length of the name specified after the dot */
  10.     opcode,        /* I/O opcode (PAB byte 0) */
  11.     vdpbufferaddress,    /* read/write VDP buffer (PAB bytes 2,3) */
  12.     length;        /* save file size or max. load file size */
  13.  
  14.     static int row=1;
  15.  
  16.     char filename[13];
  17.  
  18.     if ((PC<0x4000)||(PC>0x41FF)) ILL();
  19.  
  20.     memory_write(0x837C,memory_read(0x837C)&0xff);
  21.     dsrnamelength=memory_read(0x8354);
  22.     filenameptr=memory_read(0x8356);
  23.     startofpab=filenameptr-dsrnamelength-10;
  24.     filenamelength=*(vdp+startofpab+9)-dsrnamelength-1;
  25.  
  26.     /* decode filename from the PAB into DOS file name */
  27.  
  28.     if ((filenamelength<1)||(filenamelength>8))
  29.     {
  30.         DISK_ERROR(startofpab,7);
  31.         return;
  32.     }
  33.     for(i=0;i<filenamelength;i++)
  34.     filename[i]=toupper(*(vdp+filenameptr+i+1));
  35.     filename[i]=0;
  36.     strcat(filename,".TI");
  37.  
  38.     opcode=*(vdp+startofpab);
  39.  
  40.     gotoxy(42,row++);
  41.     row=((row-1)%24)+1;
  42.     cprintf("Opcode %d, name: %s",opcode,filename);
  43.  
  44.     vdpbufferaddress=((*(vdp+startofpab+2)<<8)+*(vdp+startofpab+3))&0x3fff;
  45.     length=(*(vdp+startofpab+6)<<8)+*(vdp+startofpab+7);
  46.     if (length==0)
  47.     {
  48.         DISK_ERROR(startofpab,7);
  49.         return;
  50.     }
  51.     if (vdpbufferaddress+length>0x4000) length=0x4000-vdpbufferaddress;
  52.  
  53.     switch(opcode)
  54.     {
  55.     case 1:
  56.     {
  57.         DISK_ERROR(startofpab,0);
  58.         return;
  59.     }
  60.     case 5:
  61.     {
  62.         FILE *infile;
  63.  
  64.         /* Open file for read and check result */
  65.  
  66.         infile=fopen(filename,"rb");
  67.         if (infile==NULL)
  68.         {
  69.             DISK_ERROR(startofpab,7);
  70.             return;
  71.         }
  72.  
  73.         /* Check for "TIFILES" header and skip it if present */
  74.  
  75.         if (fgetc(infile)==7) /* Tifiles header byte */
  76.         {
  77.             char test[9];
  78.             fgets(test,8,infile);
  79.             if (!strcmp(test,"TIFILES"))
  80.                 for(i=8;i<128;i++) fgetc(infile);
  81.         }
  82.         else rewind(infile);
  83.  
  84.         /* Load file until eof or max length is reached */
  85.  
  86.         i=0;
  87.         while((!feof(infile))&&(i<length))
  88.             *(vdp+vdpbufferaddress+i++)=fgetc(infile);
  89.  
  90.         /* Close file and report no errors */
  91.  
  92.         fclose(infile);
  93.         DISK_ERROR(startofpab,0);
  94.         break;
  95.     }
  96.  
  97.     case 6:
  98.     {
  99.         FILE *outfile;
  100.         outfile=fopen(filename,"wb");
  101.         if (outfile==NULL)
  102.         {
  103.             DISK_ERROR(startofpab,6);
  104.             return;
  105.         }
  106.  
  107.         /* First output TIFILES header */
  108.  
  109.         {
  110.         word NS,LB;
  111.         NS=(word)length/256;    /* number of sectors */
  112.         LB=(word)length%256;    /* EOF pointer in last sector */
  113.         if (LB) NS++;        /* count last sector */
  114.         fprintf(outfile,"%c%s",7,"TIFILES");
  115.         fprintf(outfile,"%c%c%c%c%c",NS/256,NS%256,1,0,LB,0);
  116.         for(i=13;i<128;i++) fputc(0,outfile);
  117.         }
  118.  
  119.         /* SAVE data from VDP */
  120.  
  121.         for(i=0;i<length;i++) fputc(*(vdp+vdpbufferaddress+i),outfile);
  122.         fclose(outfile);
  123.         DISK_ERROR(startofpab,0);
  124.         break;
  125.     }
  126.  
  127.     default: DISK_ERROR(startofpab,3);
  128.     }
  129. }
  130.  
  131. void DISK_ERROR(word pab,word errno)
  132. {
  133.     *(vdp+pab+1)=(*(vdp+pab+1)&0x1f)|((errno&7)<<5);
  134. }
  135.  
  136.  
  137.