?login_element?

Subversion Repositories NedoOS

Rev

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

  1. /*
  2.  
  3.   SjASMPlus Z80 Cross Assembler
  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_snapshots.cpp
  28.  
  29. #include "sjdefs.h"
  30.  
  31. // report error and close the file
  32. static bool writeError(const char* fname, FILE* & fileToClose) {
  33.         Error("Write error (disk full?)", fname, IF_FIRST);
  34.         fclose(fileToClose);
  35.         return false;
  36. }
  37.  
  38. bool SaveSNA_ZX(const char* fname, word start) {
  39.         // for Lua
  40.         if (!DeviceID) {
  41.                 Error("[SAVESNA] Only for real device emulation mode.");
  42.                 return false;
  43.         } else if (!IsZXSpectrumDevice(DeviceID)) {
  44.                 Error("[SAVESNA] Device must be ZXSPECTRUM48 or ZXSPECTRUM128.");
  45.                 return false;
  46.         }
  47.  
  48.         FILE* ff;
  49.         if (!FOPEN_ISOK(ff, fname, "wb")) {
  50.                 Error("opening file for write", fname);
  51.                 return false;
  52.         }
  53.  
  54.         constexpr int SNA_HEADER_48_SIZE = 27;
  55.         constexpr int SNA_HEADER_128_SIZE = 4;
  56.         byte snbuf[SNA_HEADER_48_SIZE + SNA_HEADER_128_SIZE] = {
  57.         //      I     L'    H'    E'    D'    C'    B'    F'    A'    L     H     E     D     C **  B **
  58.                 0x3F, 0x58, 0x27, 0x9B, 0x36, 0x00, 0x00, 0x44, 0x00, 0x2B, 0x2D, 0xDC, 0x5C, 0x00, 0x00,
  59.         //      IYL   IYH   IXL   IXH   IFF2  R     F     A     SP(L) SP(H) IM #  border
  60.                 0x3A, 0x5C, 0x3C, 0xFF, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x01, 0x07,
  61.         // end of 48k SNA header, following 4 bytes are extra 128k SNA header "interlude" after 48k data
  62.         //      PC(L) PC(H) 7FFD  TR-DOS
  63.                 0x00, 0x00, 0x00, 0x00
  64.         };
  65.  
  66.         // set BC to start address
  67.         snbuf[13] = start & 0xFF;
  68.         snbuf[14] = (start>>8) & 0xFF;
  69.  
  70.         // check if default ZX-like stack was modified - if not, it will be used for snapshot
  71.         bool is48kSnap = !strcmp(DeviceID, "ZXSPECTRUM48");
  72.         bool defaultStack = true;
  73.         aint stackAdr = Device->ZxRamTop + 1 - sizeof(ZX_STACK_DATA);
  74.         for (aint ii = is48kSnap ? -2 : 0; ii < aint(sizeof(ZX_STACK_DATA)); ++ii) {
  75.                 // will check for 48k snap if there is `00 00` ahead of fake stack data
  76.                 const byte cmpValue = (0 <= ii) ? ZX_STACK_DATA[ii] : 0;
  77.                 CDeviceSlot* slot = Device->GetSlot(Device->GetSlotOfA16(stackAdr + ii));
  78.                 CDevicePage* page = Device->GetPage(slot->InitialPage);
  79.                 defaultStack &= (cmpValue == page->RAM[(stackAdr + ii) & (page->Size-1)]);
  80.         }
  81.         if (defaultStack) {
  82.                 if (is48kSnap) {
  83.                         --stackAdr;
  84.                         // inject PC under default stack
  85.                         CDeviceSlot* slot = Device->GetSlot(Device->GetSlotOfA16(stackAdr));
  86.                         CDevicePage* page = Device->GetPage(slot->InitialPage);
  87.                         page->RAM[stackAdr & (page->Size-1)] = (start>>8) & 0xFF;
  88.                         --stackAdr;
  89.                         slot = Device->GetSlot(Device->GetSlotOfA16(stackAdr));
  90.                         page = Device->GetPage(slot->InitialPage);
  91.                         page->RAM[stackAdr & (page->Size-1)] = start & 0xFF;
  92.                 }
  93.                 // SP (may point to injected start address for 48k snap)
  94.                 snbuf[23] = (stackAdr) & 0xFF;
  95.                 snbuf[24] = (stackAdr>>8) & 0xFF;
  96.         } else {
  97.                 if (is48kSnap) {
  98.                         WarningById(W_SNA_48);
  99.                         snbuf[23] = 0x00; //sp
  100.                         snbuf[24] = 0x40; //sp
  101.                         Device->GetPage(1)->RAM[0] = start & 0xFF;      //pc
  102.                         Device->GetPage(1)->RAM[1] = start >> 8;        //pc
  103.                 } else {
  104.                         snbuf[23] = 0x00; //sp
  105.                         snbuf[24] = 0x60; //sp
  106.                 }
  107.         }
  108.  
  109.         if (fwrite(snbuf, 1, SNA_HEADER_48_SIZE, ff) != SNA_HEADER_48_SIZE) {
  110.                 return writeError(fname, ff);
  111.         }
  112.  
  113.         const int pages48[3] = { 1, 2, 3 };
  114.         const int pages128[3] = { 5, 2, Device->GetSlot(3)->Page->Number };
  115.  
  116.         for (const int page : is48kSnap ? pages48 : pages128) {
  117.                 if ((aint) fwrite(Device->GetPage(page)->RAM, 1, Device->GetPage(page)->Size, ff) != Device->GetPage(page)->Size) {
  118.                         return writeError(fname, ff);
  119.                 }
  120.         }
  121.  
  122.         if (!is48kSnap) {
  123.                 // 128k snapshot extra header fields
  124.                 snbuf[27] = start & 0xFF; //pc
  125.                 snbuf[28] = (start>>8) & 0xFF; //pc
  126.                 snbuf[29] = 0x10 + Device->GetSlot(3)->Page->Number; //7ffd
  127.                 snbuf[30] = 0; //tr-dos
  128.                 if (fwrite(snbuf + SNA_HEADER_48_SIZE, 1, SNA_HEADER_128_SIZE, ff) != SNA_HEADER_128_SIZE) {
  129.                         return writeError(fname, ff);
  130.                 }
  131.                 // 128k banks
  132.                 for (aint i = 0; i < 8; i++) {
  133.                         if (i != Device->GetSlot(3)->Page->Number && i != 2 && i != 5) {
  134.                                 if ((aint) fwrite(Device->GetPage(i)->RAM, 1, Device->GetPage(i)->Size, ff) != Device->GetPage(i)->Size) {
  135.                                         return writeError(fname, ff);
  136.                                 }
  137.                         }
  138.                 }
  139.         }
  140.  
  141.         if (128*1024 < Device->PagesCount * Device->GetPage(0)->Size) {
  142.                 WarningById(W_SNA_128, fname);
  143.         }
  144.  
  145.         fclose(ff);
  146.         return true;
  147. }
  148.  
  149. //eof io_snapshots.cpp
  150.