?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*
  2.  
  3.   SjASMPlus Z80 Cross Compiler - modified - TZX extension
  4.  
  5.   Copyright (c) 2006 Sjoerd Mastijn (original SW)
  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. #include "sjdefs.h"
  28.  
  29. namespace TZXBlockId {
  30.         constexpr byte Standard = 0x10;
  31.         constexpr byte Turbo = 0x11;
  32.         constexpr byte Pause = 0x20;
  33. }
  34.  
  35. void TZX_CreateEmpty(const char* fname) {
  36.         FILE* ff;
  37.         if (!FOPEN_ISOK(ff, fname, "wb")) {
  38.                 Error("[TZX] Error opening file for write", fname, FATAL);
  39.         }
  40.  
  41.         constexpr byte tzx_major_version = 1;
  42.         constexpr byte tzx_minor_version = 10;
  43.         const char magic[10] = {
  44.                 'Z', 'X', 'T', 'a', 'p', 'e', '!',
  45.                 0x1A,
  46.                 tzx_major_version,  tzx_minor_version
  47.         };
  48.  
  49.         fwrite(magic, 1, 10, ff);
  50.         fclose(ff);
  51. }
  52.  
  53. void TZX_AppendPauseBlock(const char* fname, word pauseAfterMs) {
  54.         FILE* ff;
  55.         if (!FOPEN_ISOK(ff, fname, "a+b")) {
  56.                 Error("[TZX] Error opening file for append", fname, FATAL);
  57.         }
  58.         fputc(TZXBlockId::Pause, ff); // block id
  59.  
  60.         fputc(pauseAfterMs & 0xFF, ff);
  61.         fputc(pauseAfterMs >> 8, ff);
  62.         fclose(ff);
  63. }
  64.  
  65. void TZX_AppendStandardBlock(const char* fname, const byte* buf, const aint buflen, word pauseAfterMs, byte sync) {
  66.         FILE* ff;
  67.         if (!FOPEN_ISOK(ff, fname, "a+b")) {
  68.                 Error("[TZX] Error opening file for append", fname, FATAL);
  69.         }
  70.  
  71.         const aint totalDataLen = buflen + 2; // + sync byte + checksum
  72.  
  73.         fputc(TZXBlockId::Standard, ff); // block id
  74.  
  75.         fputc(pauseAfterMs & 0xFF, ff); // block header
  76.         fputc(pauseAfterMs >> 8, ff);
  77.         fputc(totalDataLen & 0xFF, ff);
  78.         fputc(totalDataLen >> 8, ff);
  79.  
  80.         fputc(sync, ff); // sync pattern
  81.         fwrite(buf, 1, buflen, ff); // payload
  82.         byte check = sync;
  83.         for (aint i = 0; i < buflen; ++i) check ^= buf[i];
  84.         fputc(check, ff); // checksum
  85.         fclose(ff);
  86. }
  87.  
  88. void TZX_AppendTurboBlock(const char* fname, const byte* buf, const aint buflen, const STZXTurboBlock& turbo) {
  89.         FILE* ff;
  90.         if (!FOPEN_ISOK(ff, fname, "a+b")) {
  91.                 Error("[TZX] Error opening file for append", fname, FATAL);
  92.         }
  93.  
  94.         fputc(TZXBlockId::Turbo, ff); // block id
  95.  
  96.         fputc(turbo.PilotPulseLen & 0xFF, ff); // block header
  97.         fputc(turbo.PilotPulseLen >> 8, ff);
  98.         fputc(turbo.FirstSyncLen & 0xFF, ff);
  99.         fputc(turbo.FirstSyncLen >> 8, ff);
  100.         fputc(turbo.SecondSyncLen & 0xFF, ff);
  101.         fputc(turbo.SecondSyncLen >> 8, ff);
  102.         fputc(turbo.ZeroBitLen & 0xFF, ff);
  103.         fputc(turbo.ZeroBitLen >> 8, ff);
  104.         fputc(turbo.OneBitLen & 0xFF, ff);
  105.         fputc(turbo.OneBitLen >> 8, ff);
  106.         fputc(turbo.PilotToneLen & 0xFF, ff);
  107.         fputc(turbo.PilotToneLen >> 8, ff);
  108.         fputc(turbo.LastByteUsedBits & 0xFF, ff);
  109.         fputc(turbo.PauseAfterMs & 0xFF, ff);
  110.         fputc(turbo.PauseAfterMs >> 8, ff);
  111.         fputc(buflen & 0xFF, ff); // total data len is a 24-bit number, LSB first
  112.         fputc(buflen >> 8, ff);
  113.         fputc(buflen >> 16, ff);
  114.  
  115.         // payload
  116.         fwrite(buf, 1, buflen, ff);
  117.         fclose(ff);
  118. }
  119.  
  120. // eof io_tzx.cpp
  121.