atari_exe_tools_1.5.0_cfa98435/src/exe_tools-segment_print.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
---------------------------------------------------------------- {{{ ----------
--: Copyright © 2020 … 2023 Martin Krischik «krischik@users.sourceforge.net»
-------------------------------------------------------------------------------
--: This library is free software; you can redistribute it and/or modify it
--: under the terms of the GNU Library General Public License as published by
--: the Free Software Foundation; either version 2 of the License, or (at your
--: option) any later version.
--:
--: This library is distributed in the hope that it will be useful, but WITHOUT
--: ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
--: FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
--: License for more details.
--:
--: You should have received a copy of the GNU Library General Public License
--: along with this library; if not, write to the Free Software Foundation,
--: Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
---------------------------------------------------------------- }}} ----------
pragma License (Modified_Gpl);
pragma Ada_2022;

with Ada.Strings.Wide_Wide_Fixed;
with Ada.Text_IO;
with Ada.Wide_Wide_Text_IO;
with AdaCL.Strings.Hex;
with AdaCL.Wide_Wide_Strings.Hex;
with AdaCL.Trace;
with EXE_Tools.Byte_IO;
with EXE_Tools.Segment_Body;
with EXE_Tools.Segment_Header;
with EXE_Tools.CommandLine;

---
--
--: @ value
procedure EXE_Tools.Segment_Print
   (File_Name : in String; Operation : in EXE_Tools.CommandLine.Operation_Type := EXE_Tools.CommandLine.Print_All)
is
   Trace : AdaCL.Trace.Object := AdaCL.Trace.Function_Trace;
   pragma Unreferenced (Trace);

   package CL renames EXE_Tools.CommandLine;
   package Text_IO renames Ada.Text_IO;

   File : Byte_IO.File_Type;
begin
   AdaCL.Trace.Write ("Filename         : " & File_Name);

   Byte_IO.Open
      (File,
       Byte_IO.In_File,
       File_Name,
       Form => "");

   declare
      use Segment_Header;
      use Segment_Body;
      use type Word;

      procedure Write_Segment_Header (Header : in Header_Type'Class) is
         package Hex renames AdaCL.Strings.Hex;
      begin
         Text_IO.Put ("Magic: $");
         Text_IO.Put (Hex.Image (Header.Magic));
         Text_IO.Put ("; Start: $");
         Text_IO.Put (Hex.Image (Header.Start_Address));
         Text_IO.Put ("; End: $");
         Text_IO.Put (Hex.Image (Header.End_Address));
         Text_IO.Put ("; Length:");
         Text_IO.Put (Header.Length'Image);
      end Write_Segment_Header;

      ---
      --  Create a memory dump
      --
      --: @value Segment to be dumped.
      procedure Write_Segment_Data (Segment : in Body_Type) is
         use Ada.Wide_Wide_Text_IO;

         package Hex renames AdaCL.Wide_Wide_Strings.Hex;
         package Fixed renames Ada.Strings.Wide_Wide_Fixed;

         Line_Len     : constant := 16;
         Byte_Len     : constant := 2;
         Address_Len  : constant := 4;
         Byte_Offset  : constant := Address_Len + 3;   --  «0130: »
         ASCII_Offset : constant := Byte_Offset + Line_Len * (Byte_Len + 1) + 1;
         Text_Len     : constant := ASCII_Offset + Line_Len;

         Text     : Wide_Wide_String (1 .. Text_Len);
         Line     : Word := Segment'First;
         Col      : Word := 0;
         Byte_Col : Integer;
      begin
         Dump_Line :
         while Line <= Segment'Last loop
            declare
               Address_Text : constant Wide_Wide_String := Hex.Image (Line);
            begin
               Fixed.Move (Source => Address_Text & ": ", Target => Text);
            end;

            Col      := 0;
            Byte_Col := Byte_Offset;

            Dump_Column :
            while Col < Line_Len and then Line + Col <= Segment'Last loop
               declare
                  Byte_Value : constant Byte                := Segment (Line + Col);
                  Byte_Text  : constant Wide_Wide_String    := Hex.Image (Byte_Value);
                  Byte_Char  : constant Wide_Wide_Character := ATASCII_US (Byte_Value);
               begin
                  Text (Byte_Col .. Byte_Col + 1)     := Byte_Text;
                  Text (Natural (ASCII_Offset + Col)) := Byte_Char;
               end;

               Col      := Col + 1;
               Byte_Col := Byte_Col + (Byte_Len + 1);
            end loop Dump_Column;

            Put_Line (Text);
            Line := Line + Line_Len;
         end loop Dump_Line;
      end Write_Segment_Data;

      ---
      --  Create a memory dump
      --
      --  String to be written
      procedure Write_Execution_Info (Segment : in Body_Type; Prefix : in String) is
         package Hex renames AdaCL.Strings.Hex;

         New_Line_Needed : Boolean := Prefix'Length > 0;
      begin
         if Segment_Body.Has_Run_Address (Segment) then
            Text_IO.Put (Prefix);
            Text_IO.Put ("Run: $");
            Text_IO.Put (Hex.Image (Segment_Body.Get_Word (Segment, Run_Address)));
            New_Line_Needed := True;
         end if;
         if Segment_Body.Has_Init_Address (Segment) then
            Text_IO.Put (Prefix);
            Text_IO.Put ("Init: $");
            Text_IO.Put (Hex.Image (Segment_Body.Get_Word (Segment, Init_Address)));
            New_Line_Needed := True;
         end if;
         if New_Line_Needed then
            Text_IO.New_Line;
         end if;
      end Write_Execution_Info;

   begin
      Text_IO.Put ("File: ");
      Text_IO.Put (File_Name);
      Text_IO.New_Line;

      All_Segments :
      loop
         One_Segment :
         declare
            Header  : constant Header_Type'Class := Read (File);
            Segment : constant Body_Type         := Read (File, Header);
         begin
            case Operation is
               when CL.Print_All =>
                  Write_Segment_Header (Header);
                  Write_Execution_Info (Segment, "; ");
                  Write_Segment_Data (Segment);
               when CL.Print_Header =>
                  Write_Segment_Header (Header);
                  Write_Execution_Info (Segment, "; ");
               when CL.Print_Data =>
                  Write_Segment_Data (Segment);
                  Write_Execution_Info (Segment, "");
            end case;
         end One_Segment;

         exit All_Segments when Byte_IO.End_Of_File (File);
      end loop All_Segments;
   end;

   Byte_IO.Close (File);

   return;
end EXE_Tools.Segment_Print;

---------------------------------------------------------------- {{{ ----------
--: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab :
--: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr :
--: vim: set spell spelllang=en_gb :