avrada_lib_2.1.0_fe38c6dc/src/avr-programspace.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
---------------------------------------------------------------------------
-- The AVR-Ada Library is free software;  you can redistribute it and/or --
-- modify it under terms of the  GNU General Public License as published --
-- by  the  Free Software  Foundation;  either  version 2, or  (at  your --
-- option) any later version.  The AVR-Ada 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 General Public License for more details.         --
--                                                                       --
-- As a special exception, if other files instantiate generics from this --
-- unit,  or  you  link  this  unit  with  other  files  to  produce  an --
-- executable   this  unit  does  not  by  itself  cause  the  resulting --
-- executable to  be  covered by the  GNU General  Public License.  This --
-- exception does  not  however  invalidate  any  other reasons why  the --
-- executable file might be covered by the GNU Public License.           --
---------------------------------------------------------------------------

with System.Machine_Code;             use System.Machine_Code;
with System.Storage_Elements;
with AVR.MCU;

package body AVR.Programspace is


   --  set to true, if the the MCU supports the enhanced LPM
   --  instruction with direct addressing in the Z pointer.
   Have_lpm_rd_Zplus : constant Boolean := AVR.MCU.Have_lpm_rd_Zplus;


   --  read bytes (8bit), words (16bit) or double words (32bit) from
   --  an address in programm memory (flash).
   function Get_Byte (Addr : Program_Address) return Unsigned_8 is
      Result : Unsigned_8;
   begin
      if not Have_lpm_rd_Zplus then
         Asm ("lpm"          & ASCII.LF &
              "mov %0, r0",
              Outputs => Unsigned_8'Asm_Output ("=r", Result),
              Inputs  => Program_Address'Asm_Input ("z", Addr),
              Clobber => "r0");
      else
         Asm ("lpm %0, Z",
              Outputs => Unsigned_8'Asm_Output ("=r", Result),
              Inputs  => Program_Address'Asm_Input ("z", Addr));
      end if;
      return Result;
   end Get_Byte;


   function Get_Char (Addr : Program_Address) return Character is
      U : Unsigned_8;
   begin
      U := Get_Byte (Addr);
      return Character'Val (U);
   end Get_Char;


   function Get_Word (Addr : Program_Address) return Unsigned_16 is
      Result     : Unsigned_16;
      Dummy_Addr : Program_Address := Addr;
   begin
      if not Have_lpm_rd_Zplus then
         Asm ("lpm"         & ASCII.LF &
              "mov %A0, r0" & ASCII.LF &
              "adiw r30, 1" & ASCII.LF &
              "lpm"         & ASCII.LF &
              "mov %B0, r0",
              Outputs => (Unsigned_16'Asm_Output ("=r", Result),
                          Program_Address'Asm_Output ("=z", Dummy_Addr)),
              Inputs  => Program_Address'Asm_Input ("1", Addr),
              Clobber => "r0");
      else
         Asm ("lpm %A0, Z+" & ASCII.LF &
              "lpm %B0, Z",
              Outputs => (Unsigned_16'Asm_Output ("=r", Result),
                          Program_Address'Asm_Output ("=z", Dummy_Addr)),
              Inputs  => Program_Address'Asm_Input ("1", Addr));
      end if;
      return Result;
   end Get_Word;


   function Get_DWord (Addr : Program_Address) return Unsigned_32 is
      Result     : Unsigned_32;
      Dummy_Addr : Program_Address := Addr;
   begin
      if not Have_lpm_rd_Zplus then
         Asm ("lpm"         & ASCII.LF &
              "mov %A0, r0" & ASCII.LF &
              "adiw r30, 1" & ASCII.LF &
              "lpm"         & ASCII.LF &
              "mov %B0, r0" & ASCII.LF &
              "adiw r30, 1" & ASCII.LF &
              "lpm"         & ASCII.LF &
              "mov %C0, r0" & ASCII.LF &
              "adiw r30, 1" & ASCII.LF &
              "lpm"         & ASCII.LF &
              "mov %D0, r0" & ASCII.LF,
              Outputs => (Unsigned_32'Asm_Output ("=r", Result),
                          Program_Address'Asm_Output ("=z", Dummy_Addr)),
              Inputs  => Program_Address'Asm_Input ("1", Dummy_Addr),
              Clobber => "r0");
      else
         Asm ("lpm %A0, Z+" & ASCII.LF &
              "lpm %B0, Z+" & ASCII.LF &
              "lpm %C0, Z+" & ASCII.LF &
              "lpm %D0, Z",
              Outputs => (Unsigned_32'Asm_Output ("=r", Result),
                          Program_Address'Asm_Output ("=z", Dummy_Addr)),
              Inputs  => Program_Address'Asm_Input ("1", Addr));
      end if;
      return Result;
   end Get_DWord;


   function Get (Addr : Far_Program_Address) return Unsigned_8 is
      pragma Unreferenced (Addr);
   begin
      return 0;
   end Get;


   function Get (Addr : Far_Program_Address) return Unsigned_16 is
      pragma Unreferenced (Addr);
   begin
      return 0;
   end Get;


   function Get (Addr : Far_Program_Address) return Unsigned_32 is
      pragma Unreferenced (Addr);
   begin
      return 0;
   end Get;


   procedure Inc (Addr : in out Program_Address) -- increment address by 1
   is
      use System.Storage_Elements;
   begin
      Addr := Addr + 1;
   end Inc;

   procedure Dec (Addr : in out Program_Address) -- decrement address by 1
   is
      use System.Storage_Elements;
   begin
      Addr := Addr - 1;
   end Dec;
end AVR.Programspace;