Module: Udb::Helpers::WavedromUtil

Included in:
TemplateHelpers
Defined in:
lib/udb_helpers/backend_helpers.rb

Instance Method Summary collapse

Instance Method Details

#fix_entities(text) ⇒ Object



26
27
28
29
30
31
# File 'lib/udb_helpers/backend_helpers.rb', line 26

def fix_entities(text)
  text.to_s.gsub("≠", "")
          .gsub("±", "±")
          .gsub("-∞", "−∞")
          .gsub("+∞", "+∞")
end

#json_dump_with_hex_literals(data) ⇒ Object

Custom JSON converter for wavedrom that handles hexadecimal literals



34
35
36
37
38
39
40
41
42
43
# File 'lib/udb_helpers/backend_helpers.rb', line 34

def json_dump_with_hex_literals(data)
  # First convert to standard JSON
  json_string = JSON.dump(data)

  # Replace string hex values with actual hex literals
  json_string.gsub(/"0x([0-9a-fA-F]+)"/) do |match|
    # Remove the quotes, leaving just the hex literal
    "0x#{$1}"
  end.gsub(/"name":/, '"name": ') # Add space after colon for name field
end

#process_wavedrom(json_data) ⇒ Object

Helper to process wavedrom data



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
# File 'lib/udb_helpers/backend_helpers.rb', line 46

def process_wavedrom(json_data)
  result = json_data.dup

  # Process reg array if it exists
  if result["reg"].is_a?(Array)
    result["reg"].each do |item|
      # For fields that are likely opcodes or immediates (type 2)
      if item["type"] == 2

        # Convert to number first (if it's a string)
        if item["name"].is_a?(String)
          if item["name"].start_with?("0x")
            # Already hexadecimal
            numeric_value = item["name"].to_i(16)
          elsif item["name"] =~ /^[01]+$/
            # Binary string without prefix
            numeric_value = item["name"].to_i(2)
          elsif item["name"] =~ /^\d+$/
            # Decimal
            numeric_value = item["name"].to_i
          else
            # Not a number, leave it alone
            next
          end
        else
          # Already a number
          numeric_value = item["name"]
        end

        # Convert to hexadecimal string
        hex_str = numeric_value.to_s(16).downcase

        # Set the name to a specially formatted string that will be converted
        # to a hex literal in our custom JSON converter
        item["name"] = "0x" + hex_str
      end

      # Ensure bits is a number
      if item["bits"].is_a?(String) && item["bits"] =~ /^\d+$/
        item["bits"] = item["bits"].to_i
      end
    end
  end

  result
end