Library of reusable VHDL components
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.0 KiB

  1. --
  2. -- File Name: MessagePkg.vhd
  3. -- Design Unit Name: MessagePkg
  4. -- Revision: STANDARD VERSION, revision 2014.01
  5. --
  6. -- Maintainer: Jim Lewis email: jim@synthworks.com
  7. -- Contributor(s):
  8. -- Jim Lewis SynthWorks
  9. --
  10. --
  11. -- Package Defines
  12. -- Data structure for name and message handling.
  13. --
  14. -- Developed for:
  15. -- SynthWorks Design Inc.
  16. -- VHDL Training Classes
  17. -- 11898 SW 128th Ave. Tigard, Or 97223
  18. -- http://www.SynthWorks.com
  19. --
  20. -- Latest standard version available at:
  21. -- http://www.SynthWorks.com/downloads
  22. --
  23. -- Revision History:
  24. -- Date Version Description
  25. -- 06/2010: 0.1 Initial revision
  26. --
  27. --
  28. -- Copyright (c) 2010 - 2013 by SynthWorks Design Inc. All rights reserved.
  29. --
  30. -- Verbatim copies of this source file may be used and
  31. -- distributed without restriction.
  32. --
  33. -- This source file is free software; you can redistribute it
  34. -- and/or modify it under the terms of the ARTISTIC License
  35. -- as published by The Perl Foundation; either version 2.0 of
  36. -- the License, or (at your option) any later version.
  37. --
  38. -- This source is distributed in the hope that it will be
  39. -- useful, but WITHOUT ANY WARRANTY; without even the implied
  40. -- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  41. -- PURPOSE. See the Artistic License for details.
  42. --
  43. -- You should have received a copy of the license with this source.
  44. -- If not download it from,
  45. -- http://www.perlfoundation.org/artistic_license_2_0
  46. --
  47. library ieee ;
  48. use ieee.std_logic_1164.all ;
  49. use ieee.numeric_std.all ;
  50. use ieee.math_real.all ;
  51. use std.textio.all ;
  52. package MessagePkg is
  53. type MessagePType is protected
  54. procedure SetName (NameIn : String) ;
  55. impure function GetName return string ;
  56. impure function IsSetName return boolean ;
  57. procedure SetMessage (MessageIn : String) ;
  58. impure function GetMessage (ItemNumber : integer) return string ;
  59. impure function GetMessageCount return integer ;
  60. procedure DeallocateName ; -- clear name
  61. procedure DeallocateMessage ; -- clear message
  62. procedure Deallocate ; -- clear all
  63. end protected MessagePType ;
  64. end package MessagePkg ;
  65. package body MessagePkg is
  66. -- Local Data Structure Types
  67. type LineArrayType is array (natural range <>) of line ;
  68. type LineArrayPtrType is access LineArrayType ;
  69. ------------------------------------------------------------
  70. -- Local. Get first word from a string
  71. function GetWord (Message : string) return string is
  72. ------------------------------------------------------------
  73. alias aMessage : string( 1 to Message'length) is Message ;
  74. begin
  75. for i in aMessage'range loop
  76. if aMessage(i) = ' ' or aMessage(i) = HT then
  77. return aMessage(1 to i-1) ;
  78. end if ;
  79. end loop ;
  80. return aMessage ;
  81. end function GetWord ;
  82. type MessagePType is protected body
  83. variable NamePtr : line := new string'("") ;
  84. variable MessageCount : integer := 0 ;
  85. constant INITIAL_ITEM_COUNT : integer := 25 ;
  86. variable MaxMessageCount : integer := INITIAL_ITEM_COUNT ;
  87. variable MessagePtr : LineArrayPtrType := new LineArrayType(1 to INITIAL_ITEM_COUNT) ;
  88. ------------------------------------------------------------
  89. procedure SetName (NameIn : String) is
  90. ------------------------------------------------------------
  91. begin
  92. deallocate(NamePtr) ;
  93. NamePtr := new string'(NameIn) ;
  94. end procedure SetName ;
  95. ------------------------------------------------------------
  96. impure function GetName return string is
  97. ------------------------------------------------------------
  98. begin
  99. if NamePtr.all /= "" or MessagePtr(1) = NULL then
  100. return NamePtr.all ;
  101. else
  102. return GetWord( MessagePtr(1).all ) ;
  103. end if ;
  104. end function GetName ;
  105. ------------------------------------------------------------
  106. impure function IsSetName return boolean is
  107. ------------------------------------------------------------
  108. begin
  109. return NamePtr.all /= "" ;
  110. end function IsSetName ;
  111. ------------------------------------------------------------
  112. procedure SetMessage (MessageIn : String) is
  113. ------------------------------------------------------------
  114. variable NamePtr : line ;
  115. variable OldMaxMessageCount : integer ;
  116. variable OldMessagePtr : LineArrayPtrType ;
  117. begin
  118. MessageCount := MessageCount + 1 ;
  119. if MessageCount > MaxMessageCount then
  120. OldMaxMessageCount := MaxMessageCount ;
  121. MaxMessageCount := OldMaxMessageCount * 2 ;
  122. OldMessagePtr := MessagePtr ;
  123. MessagePtr := new LineArrayType(1 to MaxMessageCount) ;
  124. for i in 1 to OldMaxMessageCount loop
  125. MessagePtr(i) := OldMessagePtr(i) ;
  126. end loop ;
  127. Deallocate( OldMessagePtr ) ;
  128. end if ;
  129. MessagePtr(MessageCount) := new string'(MessageIn) ;
  130. end procedure SetMessage ;
  131. ------------------------------------------------------------
  132. impure function GetMessage (ItemNumber : integer) return string is
  133. ------------------------------------------------------------
  134. begin
  135. if MessageCount > 0 then
  136. if ItemNumber >= 1 and ItemNumber <= MessageCount then
  137. return MessagePtr(ItemNumber).all ;
  138. else
  139. report LF & "%% MessagePkg:MessagePType.GetMessage input value out of range" severity failure ;
  140. return "" ; -- error if this happens
  141. end if ;
  142. else
  143. return NamePtr.all ;
  144. end if ;
  145. end function GetMessage ;
  146. ------------------------------------------------------------
  147. impure function GetMessageCount return integer is
  148. ------------------------------------------------------------
  149. begin
  150. return MessageCount ;
  151. end function GetMessageCount ;
  152. ------------------------------------------------------------
  153. procedure DeallocateName is -- clear name
  154. ------------------------------------------------------------
  155. begin
  156. deallocate(NamePtr) ;
  157. NamePtr := new string'("") ;
  158. end procedure DeallocateName ;
  159. ------------------------------------------------------------
  160. procedure DeallocateMessage is -- clear message
  161. ------------------------------------------------------------
  162. variable CurPtr : LineArrayPtrType ;
  163. begin
  164. for i in 1 to MessageCount loop
  165. deallocate( MessagePtr(i) ) ;
  166. end loop ;
  167. MessageCount := 0 ;
  168. -- Do NOT Do this: deallocate( MessagePtr ) ;
  169. end procedure DeallocateMessage ;
  170. ------------------------------------------------------------
  171. procedure Deallocate is -- clear all
  172. ------------------------------------------------------------
  173. begin
  174. DeallocateName ;
  175. DeallocateMessage ;
  176. end procedure Deallocate ;
  177. end protected body MessagePType ;
  178. end package body MessagePkg ;