cortex_m_0.5.0_39667d15/src/semihosting.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
------------------------------------------------------------------------------
--                                                                          --
--                  Copyright (C) 2016-2020, AdaCore                        --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of the copyright holder nor the names of its     --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
------------------------------------------------------------------------------

with Cortex_M.Debug;
with System.Machine_Code; use System.Machine_Code;
with HAL; use HAL;
with Ada.Unchecked_Conversion;
with Interfaces.C; use Interfaces.C;

package body Semihosting is

   type SH_u32_Array is array (Integer range <>) of SH_Word
     with Pack, Convention => C, Volatile_Components;

   function To_SH_u32 is new Ada.Unchecked_Conversion
     (Source => System.Address, Target => SH_Word);
   function To_SH_u32 is new Ada.Unchecked_Conversion
     (Source => Integer, Target => SH_Word);

   subtype Syscall is SH_Word;

   SYS_OPEN     : constant Syscall := 16#01#;
   SYS_CLOSE    : constant Syscall := 16#02#;
   SYS_WRITEC   : constant Syscall := 16#03#;
   SYS_WRITE0   : constant Syscall := 16#04#;
   SYS_WRITE    : constant Syscall := 16#05#;
   SYS_READ     : constant Syscall := 16#06#;
   --  SYS_READC    : constant Syscall := 16#07#;
   --  SYS_ISERROR  : constant Syscall := 16#08#;
   --  SYS_ISTTY    : constant Syscall := 16#09#;
   SYS_SEEK     : constant Syscall := 16#0A#;
   --  SYS_FLEN     : constant Syscall := 16#0C#;
   --  SYS_TMPNAM   : constant Syscall := 16#0D#;
   SYS_REMOVE   : constant Syscall := 16#0E#;
   --  SYS_RENAME   : constant Syscall := 16#0E#;
   --  SYS_CLOCK    : constant Syscall := 16#10#;
   --  SYS_TIME     : constant Syscall := 16#11#;
   SYS_ERRNO    : constant Syscall := 16#13#;
   --  SYS_GET_CMD  : constant Syscall := 16#15#;
   --  SYS_HEAPINFO : constant Syscall := 16#16#;
   --  SYS_ELAPSED  : constant Syscall := 16#30#;
   --  SYS_TICKFREQ : constant Syscall := 16#31#;

   function Semihosting_Enabled return Boolean is
     (Cortex_M.Debug.Halting_Debug_Enabled);
   function Generic_SH_Call (R0, R1 : SH_Word) return SH_Word;
   function Generic_SH_Call (R0 : SH_Word; R1 : System.Address) return SH_Word;

   ---------------------
   -- Generic_SH_Call --
   ---------------------

   function Generic_SH_Call (R0, R1 : SH_Word) return SH_Word is
      Ret : SH_Word;
   begin
      Asm ("mov r0, %1" & ASCII.LF & ASCII.HT &
           "mov r1, %2" & ASCII.LF & ASCII.HT &
           "bkpt #0xAB" & ASCII.LF & ASCII.HT &
           "mov %0, r0",
           Outputs  => (SH_Word'Asm_Output ("=r", Ret)),
           Inputs   => (SH_Word'Asm_Input ("r", R0),
                        SH_Word'Asm_Input ("r", R1)),
           Volatile => True,
           Clobber => ("r1, r0"));
      return Ret;
   end Generic_SH_Call;

   ---------------------
   -- Generic_SH_Call --
   ---------------------

   function Generic_SH_Call (R0 : SH_Word; R1 : System.Address)
      return SH_Word is
   begin
      return Generic_SH_Call (R0, To_SH_u32 (R1));
   end Generic_SH_Call;

   -----------
   -- Close --
   -----------

   function Close (File_Handle : SH_Word) return SH_Word is
      Block : SH_u32_Array (0 .. 0);
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return SH_Word'Last;
      end if;

      Block (0) := File_Handle;
      return Generic_SH_Call (SYS_CLOSE, Block'Address);
   end Close;

   ----------
   -- Open --
   ----------

   function Open (Filename : String; Mode : Flag) return SH_Word is
      Block  : SH_u32_Array (0 .. 2);
      C_Name : char_array (0 .. Filename'Length) with Volatile;
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return SH_Word'Last;
      end if;

      for J in Filename'Range loop
         C_Name (size_t (J - Filename'First)) :=
           char'Val (Character'Pos (Filename (J)));
      end loop;
      C_Name (C_Name'Last) := nul;

      Block (0) := To_SH_u32 (C_Name'Address);
      Block (1) := Mode;
      Block (2) := Filename'Length;

      return Generic_SH_Call (SYS_OPEN, Block'Address);
   end Open;

   ----------
   -- Read --
   ----------

   function Read (File_Handle     : SH_Word;
                  Buffer_Address  : System.Address;
                  Buffer_Size     : SH_Word) return SH_Word
   is
      Block  : SH_u32_Array (0 .. 2);
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return Buffer_Size;
      end if;

      Block (0) := File_Handle;
      Block (1) := To_SH_u32 (Buffer_Address);
      Block (2) := Buffer_Size;

      return Generic_SH_Call (SYS_READ, Block'Address);
   end Read;

   -----------
   -- Write --
   -----------

   function Write (File_Handle     : SH_Word;
                   Buffer_Address  : System.Address;
                   Buffer_Size     : SH_Word) return SH_Word
   is
      Block  : SH_u32_Array (0 .. 3);
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return Buffer_Size;
      end if;

      Block (0) := File_Handle;
      Block (1) := To_SH_u32 (Buffer_Address);
      Block (2) := Buffer_Size;

      return Generic_SH_Call (SYS_WRITE, Block'Address);
   end Write;

   ------------
   -- Remove --
   ------------

   function Remove (Filename : String) return SH_Word is
      Block  : SH_u32_Array (0 .. 1);
      C_Name : char_array (0 .. Filename'Length) with Volatile;
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return SH_Word'Last;
      end if;

      for J in Filename'Range loop
         C_Name (size_t (J - Filename'First)) :=
           char'Val (Character'Pos (Filename (J)));
      end loop;
      C_Name (C_Name'Last) := nul;

      Block (0) := To_SH_u32 (C_Name'Address);
      Block (1) := To_SH_u32 (Filename'Length);

      return Generic_SH_Call (SYS_REMOVE, Block'Address);
   end Remove;

   ----------
   -- Seek --
   ----------

   function Seek (File_Handle       : SH_Word;
                  Absolute_Position : SH_Word) return SH_Word
   is
      Block  : SH_u32_Array (0 .. 1);
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return SH_Word'Last;
      end if;

      Block (0) := File_Handle;
      Block (1) := Absolute_Position;

      return Generic_SH_Call (SYS_SEEK, Block'Address);
   end Seek;

   -----------
   -- Errno --
   -----------

   function Errno return SH_Word is
   begin
      return Generic_SH_Call (SYS_ERRNO, 0);
   end Errno;

   -------------
   -- Write_C --
   -------------

   procedure Write_C (C : Character) is
      Ret : SH_Word with Unreferenced;
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return;
      end if;

      Ret := Generic_SH_Call (SYS_WRITEC, C'Address);
   end Write_C;

   -------------
   -- Write_0 --
   -------------

   procedure Write_0 (Str : String) is
      type Byte_Array is new UInt8_Array with Volatile_Components;
      Data : Byte_Array (Str'First .. Str'Last + 1);
      Ret  : SH_Word with Unreferenced;
   begin
      if not Semihosting_Enabled then
         --  No debugger attached
         return;
      end if;

      for Index in Str'Range loop
         Data (Index) := Character'Pos (Str (Index));
      end loop;

      --  Add trailing zero
      Data (Str'Last + 1) := 0;

      Ret := Generic_SH_Call (SYS_WRITE0, Data'Address);
   end Write_0;

   --------------
   -- Log_Line --
   --------------

   procedure Log_Line (Str : String) is
   begin
      Log (Str);
      Log_New_Line;
   end Log_Line;

   ------------------
   -- Log_New_Line --
   ------------------

   procedure Log_New_Line is
   begin
      Write_C (ASCII.LF);
   end Log_New_Line;

end Semihosting;