Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.     BSD 2-Clause License
  3.  
  4.     Copyright (c) 2020, Anton Krotov
  5.     All rights reserved.
  6. *)
  7.  
  8. MODULE HEX;
  9.  
  10. IMPORT FILES, WRITER, CHL := CHUNKLISTS;
  11.  
  12.  
  13. PROCEDURE hexdgt (n: BYTE): BYTE;
  14. BEGIN
  15.     IF n < 10 THEN
  16.         n := n + ORD("0")
  17.     ELSE
  18.         n := n - 10 + ORD("A")
  19.     END
  20.  
  21.     RETURN n
  22. END hexdgt;
  23.  
  24.  
  25. PROCEDURE Byte (file: FILES.FILE; byte: BYTE);
  26. BEGIN
  27.     WRITER.WriteByte(file, hexdgt(byte DIV 16));
  28.     WRITER.WriteByte(file, hexdgt(byte MOD 16));
  29.     INC(file.chksum, byte);
  30. END Byte;
  31.  
  32.  
  33. PROCEDURE NewLine (file: FILES.FILE);
  34. BEGIN
  35.     Byte(file, (-file.chksum) MOD 256);
  36.     file.chksum := 0;
  37.     WRITER.WriteByte(file, 0DH);
  38.     WRITER.WriteByte(file, 0AH)
  39. END NewLine;
  40.  
  41.  
  42. PROCEDURE StartCode (file: FILES.FILE);
  43. BEGIN
  44.     WRITER.WriteByte(file, ORD(":"));
  45.     file.chksum := 0
  46. END StartCode;
  47.  
  48.  
  49. PROCEDURE Data* (file: FILES.FILE; mem: ARRAY OF BYTE; idx, cnt: INTEGER);
  50. VAR
  51.     i, len: INTEGER;
  52.  
  53. BEGIN
  54.     WHILE cnt > 0 DO
  55.         len := MIN(cnt, 16);
  56.         StartCode(file);
  57.         Byte(file, len);
  58.         Byte(file, idx DIV 256);
  59.         Byte(file, idx MOD 256);
  60.         Byte(file, 0);
  61.         FOR i := 1 TO len DO
  62.             Byte(file, mem[idx]);
  63.             INC(idx)
  64.         END;
  65.         DEC(cnt, len);
  66.         NewLine(file)
  67.     END
  68. END Data;
  69.  
  70.  
  71. PROCEDURE ExtLA* (file: FILES.FILE; LA: INTEGER);
  72. BEGIN
  73.     ASSERT((0 <= LA) & (LA <= 0FFFFH));
  74.     StartCode(file);
  75.     Byte(file, 2);
  76.     Byte(file, 0);
  77.     Byte(file, 0);
  78.     Byte(file, 4);
  79.     Byte(file, LA DIV 256);
  80.     Byte(file, LA MOD 256);
  81.     NewLine(file)
  82. END ExtLA;
  83.  
  84.  
  85. PROCEDURE Data2* (file: FILES.FILE; mem: CHL.BYTELIST; idx, cnt, LA: INTEGER);
  86. VAR
  87.     i, len, offset: INTEGER;
  88.  
  89. BEGIN
  90.     ExtLA(file, LA);
  91.     offset := 0;
  92.     WHILE cnt > 0 DO
  93.         ASSERT(offset <= 65536);
  94.         IF offset = 65536 THEN
  95.             INC(LA);
  96.             ExtLA(file, LA);
  97.             offset := 0
  98.         END;
  99.         len := MIN(cnt, 16);
  100.         StartCode(file);
  101.         Byte(file, len);
  102.         Byte(file, offset DIV 256);
  103.         Byte(file, offset MOD 256);
  104.         Byte(file, 0);
  105.         FOR i := 1 TO len DO
  106.             Byte(file, CHL.GetByte(mem, idx));
  107.             INC(idx);
  108.             INC(offset)
  109.         END;
  110.         DEC(cnt, len);
  111.         NewLine(file)
  112.     END
  113. END Data2;
  114.  
  115.  
  116. PROCEDURE End* (file: FILES.FILE);
  117. BEGIN
  118.     StartCode(file);
  119.     Byte(file, 0);
  120.     Byte(file, 0);
  121.     Byte(file, 0);
  122.     Byte(file, 1);
  123.     NewLine(file)
  124. END End;
  125.  
  126.  
  127. END HEX.