Module: HDLRuby::Verilog

Defined in:
lib/HDLRuby/hruby_verilog_name.rb

Overview

Program with inverse conversion last update 2019 01 29

Instance Method Summary collapse

Instance Method Details

#name_to_verilog(name) ⇒ Object

Since it is possible to use $ and numbers other than the beginning of the character string, it is divided.



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
# File 'lib/HDLRuby/hruby_verilog_name.rb', line 12

def name_to_verilog(name)
  ref = ""         # For storing the converted character.
  name = name.to_s # Ensure name is a string
  
  if (name[0] =~ /[a-zA-Z]/) then
    ref << name[0]
    # _ To convert it to __.
  elsif (name[0] == "_") then
    ref << "__"
  # If it does not satisfy the above, it is another character.
  # In that case, convert it to UTF-8 and convert it to a usable character string.
  else
    l = name[0].bytes.map{|v| v.to_s(16)}.join # Conversion to UTF-8 hexadecimal number.
 
    ref << "_" + l.rjust(6,"0")      # Add an underscore indicating conversion.
                                     # The remainder of 6 digits is filled with 0.
  end
  
  name[1..-1].each_char do |c|
    # Confirmation of characters in array.
    # If it is a-zA-Z 0 - 9, it is added to ref as it is.
    if (c =~ /[a-zA-Z0-9]|\$/) then
      ref << c
    # _ To convert it to __.
    elsif (c == "_") then
      ref << "__"
    # If it does not satisfy the above, it is another character.
    # In that case, convert it to UTF-8 and convert it to a usable character string.
    else
      l = c.bytes.map{|v| v.to_s(16)}.join # Conversion to UTF-8 hexadecimal number.
   
      ref << "_" + l.rjust(6,"0")      # Add an underscore indicating conversion.
                                       # The remainder of 6 digits is filled with 0.
    end
  end
  return ref
end