Subversion Repositories NedoOS

Rev

Rev 2297 | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <tcp.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <graphic.h>
  9. #include <../common/terminal.c>
  10.  
  11. #define true 1
  12. #define false 0
  13. //
  14. FILE *fp1;
  15.  
  16. struct headers
  17. {
  18.   unsigned char marker[4]; // SCA
  19.   unsigned char version;   // 01
  20.   unsigned int width;      // 00 01
  21.   unsigned int height;     // C0 00
  22.   unsigned char border;    // 00
  23.   unsigned int frames;     // 03 00
  24.   unsigned char ptype;     // 00
  25.   unsigned int poffset;    // 0E 00
  26.   unsigned int doffset;
  27.   unsigned long filesize;
  28.   unsigned char pagesNeeded;
  29.   unsigned char isAtm;
  30.   unsigned char totalMem;
  31.   unsigned char freeMem;
  32.   unsigned char scr0high;
  33.   unsigned char scr1high;
  34.   unsigned char curScreen;
  35.   unsigned char curPage;
  36.   unsigned int curFrame;
  37.   unsigned int headerSize;
  38. } header;
  39.  
  40. unsigned char buf[4096];
  41. unsigned char framesDelays[1024];
  42. unsigned char mem[260]; // reserved pages
  43. void quit(void)
  44. {
  45.   OS_SETGFX(0x86);
  46.   exit(0);
  47. }
  48. char waitKey(void)
  49. {
  50.   char key;
  51.   do
  52.   {
  53.     key = OS_GETKEY();
  54.   } while (key == 0);
  55.   return key;
  56. }
  57.  
  58. void delayInt(unsigned long counter)
  59. {
  60.   unsigned long start, finish;
  61.   start = time();
  62.   finish = start + counter;
  63.   if (counter == 0)
  64.   {
  65.     if (OS_GETKEY() != 0)
  66.     {
  67.       quit();
  68.     }
  69.   }
  70.  
  71.   while (time() < finish)
  72.   {
  73.     if (OS_GETKEY() != 0)
  74.     {
  75.       quit();
  76.     }
  77.   }
  78. }
  79.  
  80. void delayLong(unsigned long counter)
  81. {
  82.   unsigned long start, finish;
  83.   counter = counter / 20;
  84.   if (counter < 1)
  85.   {
  86.     counter = 1;
  87.   }
  88.   start = time();
  89.   finish = start + counter;
  90.  
  91.   while (start < finish)
  92.   {
  93.     start = time();
  94.     YIELD();
  95.   }
  96. }
  97.  
  98. unsigned char getFreeMem(void)
  99. {
  100.   unsigned char freeMem = 0, counter;
  101.   for (counter = 0; counter < header.totalMem; counter++)
  102.   {
  103.     unsigned char owner;
  104.     owner = OS_GETPAGEOWNER(~counter);
  105.     if (owner == 0)
  106.     {
  107.       freeMem++;
  108.     }
  109.   }
  110.   return freeMem - 8;
  111. }
  112.  
  113. char getMem(char numOfPages)
  114. {
  115.   unsigned int pageCount;
  116.   unsigned char page;
  117.   unsigned int newPage;
  118.   for (pageCount = 0; pageCount < numOfPages; pageCount++)
  119.   {
  120.     newPage = OS_NEWPAGE();
  121.     if (newPage > 255)
  122.     {
  123.       return false;
  124.     }
  125.     page = newPage;
  126.     mem[pageCount] = page;
  127.   }
  128.   return true;
  129. }
  130.  
  131. void loadFile(void)
  132. {
  133.   const char *marker;
  134.   unsigned int counter;
  135.  
  136.   OS_READHANDLE(buf, fp1, header.headerSize);
  137.  
  138.   header.marker[0] = buf[0];
  139.   header.marker[1] = buf[1];
  140.   header.marker[2] = buf[2];
  141.   header.marker[3] = 0;
  142.   header.version = buf[3];
  143.   header.width = buf[4] + buf[5] * 256;
  144.   header.height = buf[6] + (buf[7] * 256);
  145.   header.border = buf[8];
  146.   header.frames = buf[9] + (buf[10] * 256);
  147.   header.ptype = buf[11];
  148.   header.poffset = buf[12] + (buf[13] * 256);
  149.   header.doffset = header.poffset + header.frames;
  150.   header.filesize = OS_GETFILESIZE(fp1);
  151.   header.pagesNeeded = (header.filesize / 16384) + 1;
  152.   header.curScreen = 1;
  153.   header.curPage = 0;
  154.   /*
  155.       printf("Size : %lu bytes\r\n", header.filesize);
  156.       printf("Total pages  : %u\r\n", header.totalMem);
  157.       printf("Pages needed : %u\r\n", header.pagesNeeded);
  158.       printf("Free pages   : %u\r\n", header.freeMem);
  159.       printf("Marker: %s\r\n", header.marker);
  160.       printf("Width: %u\r\n", header.width);
  161.       printf("Height: %u\r\n", header.height);
  162.       printf("Border: %u\r\n", header.border);
  163.       printf("Frames: %u\r\n", header.frames);
  164.       printf("Payload type: %u\r\n", header.ptype);
  165.       printf("payload offset: %u\r\n", header.poffset);
  166.       printf("data offset: %u\r\n", header.doffset);
  167.   */
  168.   marker = strstr(header.marker, "SCA");
  169.   if (marker == NULL)
  170.   {
  171.     printf("File is not a SCA animation [%s] \r\n", header.marker);
  172.     waitKey();
  173.     quit();
  174.   }
  175.  
  176.   if (header.pagesNeeded > header.freeMem)
  177.   {
  178.     printf("Not enough memory. Needed %u pages more  \r\n", header.pagesNeeded - header.freeMem);
  179.     waitKey();
  180.     quit();
  181.   }
  182.   if (!getMem(header.pagesNeeded))
  183.   {
  184.     printf("Memory allocation error\r\n");
  185.     waitKey();
  186.     quit();
  187.   }
  188.  
  189.   OS_READHANDLE(framesDelays, fp1, header.frames);
  190.  
  191.   ///////////////////////LOADER///////////////////////
  192.   for (counter = 0; counter < header.pagesNeeded; counter++)
  193.   {
  194.     OS_SETPG8000(mem[counter]);
  195.     OS_READHANDLEMEM(0x8000, fp1, 16384);
  196.     // printf("Page %02u loaded   \r", counter);
  197.     // printf("%02u [%u]", counter, mem[counter]);
  198.     ///////////////////////LOADER///////////////////////
  199.   }
  200. }
  201. void init(void)
  202. {
  203.   unsigned char pgbak;
  204.   union APP_PAGES main_pg;
  205.   header.scr0high = OS_GETSCR0() >> 8;
  206.   header.scr1high = OS_GETSCR1() >> 8;
  207.   header.isAtm = (unsigned char)OS_GETCONFIG(); // 1-Evo 2-ATM2 3-ATM3 6-p2.666
  208.   header.headerSize = 14;
  209.   header.totalMem = 255;
  210.   main_pg.l = OS_GETMAINPAGES();
  211.   pgbak = main_pg.pgs.window_2;
  212.   OS_DELPAGE(pgbak);
  213.   pgbak = main_pg.pgs.window_3;
  214.   OS_DELPAGE(pgbak);
  215.   header.freeMem = getFreeMem();
  216. }
  217.  
  218. void clearScreens(void)
  219. {
  220.   SETPG32KHIGH(header.scr1high);
  221.   CLEARC000();
  222.   SETPG32KHIGH(header.scr0high);
  223.   CLEARC000();
  224. }
  225.  
  226. unsigned int viewScreen6912NoKeyGraph_c(unsigned int bufAdr, unsigned int bufOffset)
  227. {
  228.   if (header.curScreen == 1)
  229.   {
  230.     SETPG32KHIGH(header.scr0high);
  231.     header.curScreen = 0;
  232.   }
  233.   else
  234.   {
  235.     SETPG32KHIGH(header.scr1high);
  236.     header.curScreen = 1;
  237.   }
  238.  
  239.   // disable_interrupt();
  240.  
  241.   if (bufOffset < 9473)
  242.   {
  243.     memcpy((unsigned char *)(0xc000), (unsigned char *)(bufAdr + bufOffset), 6912);
  244.     bufOffset = bufOffset + 6912;
  245.   }
  246.   else
  247.   {
  248.     unsigned int shiftAdr, shift;
  249.     shift = 16384 - bufOffset;
  250.     shiftAdr = 49152 + shift;
  251.     memcpy((unsigned char *)(0xc000), (unsigned char *)(bufAdr + bufOffset), shift);
  252.     OS_SETPG8000(mem[++header.curPage]);
  253.     bufOffset = bufOffset + shift;
  254.     memcpy((unsigned char *)(shiftAdr), (unsigned char *)(bufAdr), 6912 - shift);
  255.     bufOffset = 6912 - shift;
  256.   }
  257.   // enable_interrupt();
  258.  
  259.   OS_SETSCREEN(header.curScreen);
  260.    OS_HALT();
  261.   if (header.curScreen == 0)
  262.   {
  263.     SETPG32KHIGH(header.scr1high);
  264.   }
  265.   else
  266.   {
  267.     SETPG32KHIGH(header.scr0high);
  268.   }
  269.   return bufOffset;
  270. }
  271.  
  272. C_task main(int argc, char *argv[])
  273. {
  274.   unsigned int bufOffset, koef;
  275.   unsigned long delays, start, finish;
  276.   OS_HIDEFROMPARENT();
  277.   OS_SETGFX(0x86);
  278.   OS_CLS(0);
  279.   init();
  280.  
  281.   if (argc < 2)
  282.   {
  283.     OS_SETCOLOR(67);
  284.     printf("Error: File name required.[argc=%d]", argc);
  285.     OS_SETCOLOR(6);
  286.     waitKey();
  287.     quit();
  288.   }
  289.  
  290.   fp1 = OS_OPENHANDLE(argv[1], 0x80);
  291.  
  292.   if (((int)fp1) & 0xff)
  293.   {
  294.     printf("Error: %s opening error\r\n", argv[1]);
  295.     waitKey();
  296.     quit();
  297.   }
  298.  
  299.   loadFile();
  300.   clearScreens();
  301.   OS_SETGFX(0x83);
  302. label:
  303.   header.curFrame = 0;
  304.   bufOffset = 0;
  305.   header.curPage = 0;
  306.   OS_SETPG8000(mem[header.curPage]);
  307.   OS_SETBORDER(header.border);
  308.  
  309.   if (header.isAtm == 2)
  310.   {
  311.     koef = 10;
  312.   }
  313.   else
  314.   {
  315.     koef = 20;
  316.   }
  317.   do
  318.   {
  319.     start = time();
  320.     bufOffset = viewScreen6912NoKeyGraph_c(0x8000, bufOffset);
  321.     finish = time() - start;
  322.     delays = framesDelays[header.curFrame];
  323.  
  324.     if (delays >= finish)
  325.     {
  326.       delayInt(delays - finish);
  327.     }
  328.     else
  329.     {
  330.       if (OS_GETKEY() != 0)
  331.       {
  332.         quit();
  333.       }
  334.     }
  335.  
  336.     header.curFrame++;
  337.  
  338.   } while (header.curFrame < header.frames);
  339.   // waitKey();
  340.   goto label;
  341. }
  342.