BinToCode.Mod 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. MODULE BinToCode; (** AUTHOR "negelef"; PURPOSE "Binary Code to Oberon code converter"; *)
  2. IMPORT Files, Commands, Streams;
  3. CONST
  4. BufferAddressName = "a";
  5. MaxBufferSize = 4;
  6. TYPE
  7. Buffer = ARRAY MaxBufferSize OF CHAR;
  8. VAR
  9. hexdigit: ARRAY 17 OF CHAR;
  10. PROCEDURE WriteBuffer (w: Streams.Writer; CONST buffer: Buffer; offset, size: LONGINT);
  11. VAR
  12. i: LONGINT;
  13. BEGIN
  14. w.String ("SYSTEM.PUT"); w.Int (size * 8, 0);
  15. w.Char ("("); w.String (BufferAddressName); w.String (", 0");
  16. FOR i := size - 1 TO 0 BY -1 DO
  17. w.Char (hexdigit[ORD (buffer[offset + i]) DIV 10H]);
  18. w.Char (hexdigit[ORD (buffer[offset + i]) MOD 10H]);
  19. END;
  20. w.String ("H); INC ("); w.String (BufferAddressName);
  21. IF size > 1 THEN
  22. w.String (", "); w.Int (size, 0);
  23. END;
  24. w.String (");"); w.Ln;
  25. END WriteBuffer;
  26. (* Usage: Bin2Code.Convert binaryfile *)
  27. PROCEDURE Convert* (context: Commands.Context);
  28. VAR fileName: Files.FileName;
  29. reader: Files.Reader;
  30. buffer: Buffer;
  31. file: Files.File;
  32. size: LONGINT;
  33. offset: LONGINT;
  34. power: LONGINT;
  35. BEGIN
  36. context.arg.SkipWhitespace; context.arg.String (fileName);
  37. file := Files.Old (fileName);
  38. IF file = NIL THEN
  39. context.error.String ("Failed to open file ");
  40. context.error.String (fileName);
  41. context.error.Ln;
  42. RETURN;
  43. END;
  44. Files.OpenReader (reader, file, 0);
  45. size := 0; offset := 0;
  46. context.out.String ("(* put binary code copy of ");
  47. context.out.String (fileName);
  48. context.out.String (" to address ");
  49. context.out.String (BufferAddressName);
  50. context.out.String (" (cf. BinToCode.Mod ) *)");
  51. context.out.Ln;
  52. WHILE reader.res = Files.Ok DO
  53. reader.Char (buffer[size]);
  54. IF reader.res = Files.Ok THEN
  55. INC (size);
  56. IF size = MaxBufferSize THEN
  57. WriteBuffer (context.out, buffer, 0, size);
  58. size := 0;
  59. END;
  60. END;
  61. END;
  62. WHILE size # 0 DO
  63. IF size > 1 THEN
  64. power := (size DIV 2) * 2;
  65. ELSE
  66. power := 1;
  67. END;
  68. WriteBuffer (context.out, buffer, offset, power);
  69. INC (offset, power);
  70. DEC (size, power);
  71. END;
  72. END Convert;
  73. BEGIN
  74. hexdigit := "0123456789ABCDEF";
  75. END BinToCode.
  76. System.Free BinToCode~
  77. BinToCode.Convert Tutorials.Book ~