?login_element?

Subversion Repositories NedoOS

Rev

Rev 539 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  
  3. SjASMPlus Z80 Cross Compiler
  4.  
  5. Copyright (c) 2004-2008 Aprisobal
  6.  
  7. This software is provided 'as-is', without any express or implied warranty.
  8. In no event will the authors be held liable for any damages arising from the
  9. use of this software.
  10.  
  11. Permission is granted to anyone to use this software for any purpose,
  12. including commercial applications, and to alter it and redistribute it freely,
  13. subject to the following restrictions:
  14.  
  15. 1. The origin of this software must not be misrepresented; you must not claim
  16. that you wrote the original software. If you use this software in a product,
  17. an acknowledgment in the product documentation would be appreciated but is
  18. not required.
  19.  
  20. 2. Altered source versions must be plainly marked as such, and must not be
  21. misrepresented as being the original software.
  22.  
  23. 3. This notice may not be removed or altered from any source distribution.
  24.  
  25. */
  26.  
  27. // io_tape.cpp
  28.  
  29. #include "sjdefs.h"
  30. #include "io_tape_ldrs.h"
  31.  
  32. static unsigned char parity;
  33.  
  34. static void writebyte(unsigned char, FILE *);
  35. static void writenumber(unsigned int, FILE *);
  36. static void writeword(unsigned int, FILE *);
  37. static void writeheader(unsigned char, const char *, unsigned short, unsigned short, unsigned short, FILE *);
  38. static void writecode(unsigned char* block, aint length, unsigned short loadaddr, bool header, FILE *fp);
  39. static void remove_basic_sp(unsigned char* ram);
  40. static void detect_vars_changes();
  41. static bool has_screen_changes();
  42. static aint remove_unused_space(unsigned char* ram, aint length);
  43. static aint detect_ram_start(unsigned char* ram, aint length);
  44.  
  45. int TAP_SaveEmpty(char* fname) {
  46.         FILE* ff;
  47.         if (!FOPEN_ISOK(ff, fname, "wb")) {
  48.                 Error("opening file for write", fname); return 0;
  49.         }
  50.         fclose(ff);
  51.         return 1;
  52. }
  53.  
  54. int TAP_SaveBlock(char* fname, unsigned char flag, const char *ftapname, int start, int length, int param2, int param3) {
  55.         FILE* fpout;
  56.         if (!FOPEN_ISOK(fpout, fname, "ab")) {
  57.                 Error("opening file for append", fname, FATAL);
  58.         }
  59.  
  60.         if (length + start > 0x10000) {
  61.             length = -1;
  62.         }
  63.         if (length <= 0) {
  64.             length = 0x10000 - start;
  65.         }
  66.  
  67.         int varBase = 0x80, defaultValue = 0x8000;
  68.         switch (flag) {
  69.                 case BASIC:
  70.                         if (param2 < 0) {
  71.                                 param2 = defaultValue; // no autostart
  72.                         } else if (param2 >= 16384 && param2 != defaultValue) {
  73.                                 ErrorInt("[SAVETAP] Autostart LINE out of range", param2);
  74.                         }
  75.                         if (param3 < 0) {
  76.                                 param3 = length;
  77.                         }
  78.                         break;
  79.  
  80.                 case CHARS:
  81.                         varBase = 0xC0;
  82.                 case NUMBERS:
  83.                         if (param2 <= 0) {
  84.                                 param2 = 1; // reset to default A variable
  85.                         }
  86.                         param2 &= 0x1F;
  87.                         if (param2 < 1 || param2 > 26) { // A..Z
  88.                                 Error("[SAVETAP] Variable name out of range");
  89.                         }
  90.                         param2 = (param2 | varBase) << 8;
  91.                         param3 = defaultValue;
  92.                         break;
  93.  
  94.                 case CODE:
  95.                         if (param2 < 0) {
  96.                                 param2 = start;
  97.                         }
  98.                         if (param3 < 0) {
  99.                                 param3 = defaultValue;
  100.                         }
  101.                         break;
  102.         }
  103.  
  104.         if (flag == HEADLESS) {
  105.                 flag = param3 & 0xff;
  106.         } else if (ftapname) {
  107.                 writeheader(flag, ftapname, length, param2, param3, fpout);
  108.                 flag = 0xff;
  109.         }
  110.  
  111.         writeword(length + 2, fpout);
  112.         parity = 0;
  113.         writebyte(flag, fpout);
  114.  
  115.         CDeviceSlot *S;
  116.         for (aint i = 0, ptr; i < Device->SlotsCount; i++) {
  117.             S = Device->GetSlot(i);
  118.                 if (S->Address + S->Size <= start) continue;
  119.                 if (length <= 0) break;
  120.                 assert(S->Address <= start && start < S->Address + S->Size);
  121.                 ptr = (start - S->Address);
  122.  
  123.                 while (length && ptr < S->Size) {
  124.                         writebyte(S->Page->RAM[ptr], fpout);
  125.                         ++start;
  126.                         ++ptr;
  127.                         --length;
  128.                 }
  129.         }
  130.  
  131.         writebyte(parity, fpout);
  132.         fclose(fpout);
  133.         return 1;
  134. }
  135.  
  136. int TAP_SaveSnapshot(char* fname, unsigned short start) {
  137.         FILE* fpout;
  138.         if (!FOPEN_ISOK(fpout, fname, "wb")) {
  139.                 Error("opening file for write", fname, FATAL);
  140.         }
  141.  
  142.         aint datastart = 0x5E00;
  143.         aint exeat = 0x5E00;
  144.         aint basiclen = 0x1e + 2;
  145.  
  146.         /* Filetype (0: Basic), with autostart "LINE 10" */
  147.         writeheader(0, "LOADER", basiclen, 10, basiclen, fpout);
  148.  
  149.         writeword(basiclen + 2, fpout); // length of block
  150.         parity = 0;
  151.         writebyte(0xff, fpout);
  152.         writebyte(0, fpout);
  153.         writebyte(10, fpout);           // LINE 10
  154.         writebyte(basiclen, fpout);     // basic line length
  155.         writebyte(0, fpout);
  156.  
  157.         // :CLEAR VAL "xxxxx"
  158.         writebyte(0xfd, fpout);                 // CLEAR
  159.         writebyte(0xb0, fpout);                 // VAL
  160.         writebyte('\"', fpout);
  161.         writenumber(datastart-1, fpout);
  162.         writebyte('\"', fpout);
  163.  
  164.         // :INK VAL "7"
  165.         /*writebyte(':', fpout);
  166.         writebyte(0xd9, fpout);                 // INK
  167.         writebyte(0xb0, fpout);                 // VAL
  168.         writebyte('\"', fpout);
  169.         writenumber(7, fpout);
  170.         writebyte('\"', fpout);
  171.  
  172.         // :PAPER VAL "0"
  173.         writebyte(':', fpout);
  174.         writebyte(0xda, fpout);                 // PAPER
  175.         writebyte(0xb0, fpout);                 // VAL
  176.         writebyte('\"', fpout);
  177.         writenumber(0, fpout);
  178.         writebyte('\"', fpout);
  179.  
  180.         // :BORDER VAL "0"
  181.         writebyte(':', fpout);
  182.         writebyte(0xe7, fpout);                 // BORDER
  183.         writebyte(0xb0, fpout);                 // VAL
  184.         writebyte('\"', fpout);
  185.         writenumber(0, fpout);
  186.         writebyte('\"', fpout);*/
  187.  
  188.         // :CLS
  189.         writebyte(':', fpout);
  190.         writebyte(0xfb, fpout);                 // CLS
  191.  
  192.         writebyte(':', fpout);
  193.         writebyte(0xef, fpout);      /* LOAD */
  194.         writebyte('\"', fpout);
  195.         writebyte('\"', fpout);
  196.         writebyte(0xaf, fpout);      /* CODE */
  197.         writebyte(':', fpout);
  198.         writebyte(0xf9, fpout);      /* RANDOMIZE */
  199.         writebyte(0xc0, fpout);      /* USR */
  200.         writebyte(0xb0, fpout);      /* VAL */
  201.         writebyte('\"', fpout);
  202.         writenumber(exeat, fpout);
  203.         writebyte('\"', fpout);
  204.         writebyte(0x0d, fpout);
  205.         writebyte(parity, fpout);
  206.  
  207.         if (!strcmp(DeviceID, "ZXSPECTRUM48")) {
  208.                 // prepare code block
  209.                 aint ram_length = 0xA200;
  210.                 aint ram_start = 0x0000;
  211.                 unsigned char* ram = (unsigned char*)malloc(ram_length);
  212.                 if (ram == NULL) ErrorOOM();
  213.                 memcpy(ram, (unsigned char*)Device->GetSlot(1)->Page->RAM + 0x1E00, 0x2200);
  214.                 memcpy(ram + 0x2200, (unsigned char*)Device->GetSlot(2)->Page->RAM, 0x4000);
  215.                 memcpy(ram + 0x6200, (unsigned char*)Device->GetSlot(3)->Page->RAM, 0x4000);
  216.  
  217.                 // remove basic vars
  218.                 remove_basic_sp(ram + ram_length - sizeof(ZX_STACK_DATA));
  219.  
  220.                 detect_vars_changes();
  221.  
  222.                 ram_length = remove_unused_space(ram, ram_length);
  223.                 ram_start = detect_ram_start(ram, ram_length);
  224.                 ram_length -= ram_start;
  225.  
  226.                 // write loader
  227.                 unsigned char *loader = new unsigned char[SaveTAP_ZX_Spectrum_48K_SZ];
  228.                 memcpy(loader, (char*)&SaveTAP_ZX_Spectrum_48K[0], SaveTAP_ZX_Spectrum_48K_SZ);
  229.                 if (loader == NULL) ErrorOOM();
  230.                 // Settings.LoadScreen
  231.                 loader[SaveTAP_ZX_Spectrum_48K_SZ - 7] = char(has_screen_changes());
  232.                 loader[SaveTAP_ZX_Spectrum_48K_SZ - 6] = char(start & 0x00FF);
  233.                 loader[SaveTAP_ZX_Spectrum_48K_SZ - 5] = char(start >> 8);
  234.                 loader[SaveTAP_ZX_Spectrum_48K_SZ - 4] = char((ram_start + 0x5E00) & 0x00FF);
  235.                 loader[SaveTAP_ZX_Spectrum_48K_SZ - 3] = char((ram_start + 0x5E00) >> 8);
  236.                 loader[SaveTAP_ZX_Spectrum_48K_SZ - 2] = char(ram_length & 0x00FF);
  237.                 loader[SaveTAP_ZX_Spectrum_48K_SZ - 1] = char(ram_length >> 8);
  238.                 writecode(loader, SaveTAP_ZX_Spectrum_48K_SZ, 0x5E00, true, fpout);
  239.  
  240.                 // write screen$
  241.                 if (loader[SaveTAP_ZX_Spectrum_48K_SZ - 7]) {
  242.                         writecode((unsigned char*)Device->GetSlot(1)->Page->RAM, 6912, 16384, false, fpout);
  243.                 }
  244.  
  245.                 // write code block
  246.                 writecode(ram + ram_start, ram_length, 0x5E00 + ram_start, false, fpout);
  247.  
  248.                 free(ram);
  249.         } else {
  250.                 detect_vars_changes();
  251.  
  252.                 // prepare main code block
  253.                 aint ram_length = 0x6200, ram_start = 0x0000;
  254.                 unsigned char* ram = (unsigned char*)malloc(ram_length);
  255.                 if (ram == NULL) ErrorOOM();
  256.                 memcpy(ram, (unsigned char*)Device->GetSlot(1)->Page->RAM + 0x1E00, 0x2200);
  257.                 memcpy(ram + 0x2200, (unsigned char*)Device->GetSlot(2)->Page->RAM, 0x4000);
  258.  
  259.                 ram_length = remove_unused_space(ram, ram_length);
  260.                 ram_start = detect_ram_start(ram, ram_length);
  261.                 ram_length -= ram_start;
  262.  
  263.                 // init loader
  264.                 aint loader_defsize;
  265.                 unsigned char* loader_code;
  266.                 if (!strcmp(DeviceID, "ZXSPECTRUM128")) {
  267.                         loader_defsize = SaveTAP_ZX_Spectrum_128K_SZ;
  268.                         loader_code = (unsigned char*)&SaveTAP_ZX_Spectrum_128K[0];
  269.                 } else {
  270.                         loader_defsize = SaveTAP_ZX_Spectrum_256K_SZ;
  271.                         loader_code = (unsigned char*)&SaveTAP_ZX_Spectrum_256K[0];
  272.                 }
  273.                 aint loader_len = loader_defsize + (Device->PagesCount - 2)*5;
  274.                 unsigned char *loader = new unsigned char[loader_len];
  275.                 memcpy(loader, loader_code, loader_defsize);
  276.                 if (loader == NULL) ErrorOOM();
  277.                 // Settings.Start
  278.                 loader[loader_defsize - 8] = char(start & 0x00FF);
  279.                 loader[loader_defsize - 7] = char(start >> 8);
  280.                 // Settings.MainBlockStart
  281.                 loader[loader_defsize - 6] = char((ram_start + 0x5E00) & 0x00FF);
  282.                 loader[loader_defsize - 5] = char((ram_start + 0x5E00) >> 8);
  283.                 // Settings.MainBlockLength
  284.                 loader[loader_defsize - 4] = char(ram_length & 0x00FF);
  285.                 loader[loader_defsize - 3] = char(ram_length >> 8);
  286.                 // Settings.Page
  287.                 loader[loader_defsize - 2] = char(Device->GetSlot(3)->Page->Number);
  288.  
  289.                 //
  290.                 unsigned char* pages_ram[1024];
  291.                 aint pages_len[1024];
  292.                 aint pages_start[1024];
  293.  
  294.                 // build pages table
  295.                 aint count = 0;
  296.                 for (aint i=0;i < Device->PagesCount;i++) {
  297.                         if (Device->GetSlot(2)->Page->Number != i && Device->GetSlot(1)->Page->Number != i) {
  298.                                 aint length = 0x4000;
  299.                                 length = remove_unused_space((unsigned char*)Device->GetPage(i)->RAM, length);
  300.                                 if (length > 0) {
  301.                                         pages_ram[count] = (unsigned char*)Device->GetPage(i)->RAM;
  302.                                         pages_start[count] = detect_ram_start(pages_ram[count], length);
  303.                                         pages_len[count] = length - pages_start[count];
  304.  
  305.                                         loader[loader_defsize + (count*5) + 0] = char(i);
  306.                                         loader[loader_defsize + (count*5) + 1] = char((pages_start[count] + 0xC000) & 0x00FF);
  307.                                         loader[loader_defsize + (count*5) + 2] = char((pages_start[count] + 0xC000) >> 8);
  308.                                         loader[loader_defsize + (count*5) + 3] = char(pages_len[count] & 0x00FF);
  309.                                         loader[loader_defsize + (count*5) + 4] = char(pages_len[count] >> 8);
  310.  
  311.                                         count++;
  312.                                 }
  313.                         }
  314.                 }
  315.  
  316.                 // Table_BlockList.Count
  317.                 loader[loader_defsize - 1] = char(count);
  318.  
  319.                 // Settings.LoadScreen
  320.                 loader[loader_defsize - 9] = char(has_screen_changes());
  321.  
  322.                 // write loader
  323.                 writecode(loader, loader_len, 0x5E00, true, fpout);
  324.  
  325.                 // write screen$
  326.                 if (loader[loader_defsize - 9]) {
  327.                         writecode((unsigned char*)Device->GetSlot(1)->Page->RAM, 6912, 0x4000, false, fpout);
  328.                 }
  329.  
  330.                 // write code blocks
  331.                 for (aint i=0;i < count;i++) {
  332.                         writecode(pages_ram[i] + pages_start[i], pages_len[i], 0xC000 + pages_start[i], false, fpout);
  333.                 }
  334.  
  335.                 // write main code block
  336.                 writecode(ram + ram_start, ram_length, 0x5E00 + ram_start, false, fpout);
  337.  
  338.                 free(ram);
  339.         }
  340.  
  341.         fclose(fpout);
  342.         return 1;
  343. }
  344.  
  345. static void writenumber(unsigned int i, FILE *fp) {
  346.         int c;
  347.         c=i/10000;
  348.         i-=c*10000;
  349.         writebyte(c+48, fp);
  350.         c=i/1000;
  351.         i-=c*1000;
  352.         writebyte(c+48, fp);
  353.         c=i/100;
  354.         i-=c*100;
  355.         writebyte(c+48, fp);
  356.         c=i/10;
  357.         writebyte(c+48, fp);
  358.         i%=10;
  359.         writebyte(i+48, fp);
  360. }
  361.  
  362. static void writeword(unsigned int i, FILE *fp) {
  363.         writebyte(i%256,fp);
  364.         writebyte(i/256,fp);
  365. }
  366.  
  367. static void writebyte(unsigned char c, FILE *fp) {
  368.         fputc(c,fp);
  369.         parity^=c;
  370. }
  371.  
  372. static void writeheader(unsigned char flag, const char *fname, unsigned short param1, unsigned short param2, unsigned short param3, FILE *fp) {
  373.         /* Write out the code header file */
  374.         writeword(19, fp);              /* Header len */
  375.         writebyte(0, fp);               /* Header is 0 */
  376.         parity = 0;
  377.         writebyte(flag, fp);    /* Filetype flag */
  378.  
  379.         const char *ptr = fname;
  380.         for (int i = 0; i < 10; i++) {
  381.                 if (*ptr) {
  382.                         writebyte(*ptr++, fp);
  383.                 } else {
  384.                         writebyte(' ', fp);
  385.                 }
  386.         }
  387.  
  388.         writeword(param1, fp);
  389.         writeword(param2, fp);
  390.         writeword(param3, fp);
  391.         writebyte(parity, fp);
  392. }
  393.  
  394. static void writecode(unsigned char* block, aint length, unsigned short loadaddr, bool header, FILE *fp) {
  395.         if (header) {
  396.                 /* Filetype (3: Code) */
  397.                 writeheader(3, "loader", length, loadaddr, 0, fp);
  398.         }
  399.  
  400.         /* Now onto the data bit */
  401.         writeword(length + 2, fp);      /* Length of next block */
  402.         parity = 0;
  403.         writebyte(255, fp); /* Data... */
  404.         for (aint i = 0; i < length; i++) {
  405.                 writebyte(block[i], fp);
  406.         }
  407.         writebyte(parity, fp);
  408. }
  409.  
  410. static void remove_basic_sp(unsigned char* ram) {
  411.         bool remove = true;
  412.         for (size_t i=0; i < sizeof(ZX_STACK_DATA);i++) {
  413.                 if (ZX_STACK_DATA[i] != ram[i]) {
  414.                         remove = false;
  415.                 }
  416.         }
  417.         if (remove) {
  418.                 for (size_t i=0; i < sizeof(ZX_STACK_DATA);i++) {
  419.                         ram[i] = 0;
  420.                 }
  421.         }
  422. }
  423.  
  424. static void detect_vars_changes() {
  425.         unsigned char *psys = (unsigned char*)Device->GetSlot(1)->Page->RAM + 0x1C00;
  426.  
  427.         bool nobas48 = false;
  428.         for (size_t i=0; i < sizeof(ZX_SYSVARS_DATA);i++) {
  429.                 if (ZX_SYSVARS_DATA[i] != psys[i]) {
  430.                         nobas48 = true;
  431.                 }
  432.         }
  433.  
  434.         if (nobas48) {
  435.                 Warning("[SAVETAP] Tape file will not contain data from 0x5B00 to 0x5E00");
  436.         }
  437. }
  438.  
  439. static bool has_screen_changes() {
  440.         unsigned char *pscr = (unsigned char*)Device->GetSlot(1)->Page->RAM;
  441.  
  442.         for (int i=0; i < 0x1800;i++) {
  443.                 if (0 != pscr[i]) {
  444.                         return true;
  445.                 }
  446.         }
  447.  
  448.         for (int i=0x1800; i < 0x1B00;i++) {
  449.                 if (0x38 != pscr[i]) {
  450.                         return true;
  451.                 }
  452.         }
  453.  
  454.         return false;
  455. }
  456.  
  457. static aint remove_unused_space(unsigned char* ram, aint length) {
  458.         while (length > 0 && ram[length-1] == 0) {
  459.                 length--;
  460.         }
  461.  
  462.         return length;
  463. }
  464.  
  465. static aint detect_ram_start(unsigned char* ram, aint length) {
  466.         aint start = 0;
  467.  
  468.         while (start < length && ram[start] == 0) {
  469.                 start++;
  470.         }
  471.  
  472.         return start;
  473. }
  474.  
  475. //eof io_tape.cpp
  476.