|
| 1 | +<% |
| 2 | +# Helper to substitute problematic entity strings with proper Unicode characters. |
| 3 | +def fix_entities(text) |
| 4 | + text.to_s.gsub("≠", "≠") |
| 5 | + .gsub("±", "±") |
| 6 | + .gsub("-∞", "−∞") |
| 7 | + .gsub("+∞", "+∞") |
| 8 | +end |
| 9 | + |
| 10 | +# Custom JSON converter for wavedrom that handles hexadecimal literals |
| 11 | +def json_dump_with_hex_literals(data) |
| 12 | + # First convert to standard JSON |
| 13 | + json_string = JSON.dump(data) |
| 14 | + |
| 15 | + # Replace string hex values with actual hex literals |
| 16 | + json_string.gsub(/"0x([0-9a-fA-F]+)"/) do |match| |
| 17 | + # Remove the quotes, leaving just the hex literal |
| 18 | + "0x#{$1}" |
| 19 | + end.gsub(/"name":/, '"name": ') # Add space after colon for name field |
| 20 | +end |
| 21 | + |
| 22 | +# Helper to process wavedrom data |
| 23 | +def process_wavedrom(json_data) |
| 24 | + result = json_data.dup |
| 25 | + |
| 26 | + # Process reg array if it exists |
| 27 | + if result["reg"].is_a?(Array) |
| 28 | + result["reg"].each do |item| |
| 29 | + # For fields that are likely opcodes or immediates (type 2) |
| 30 | + if item["type"] == 2 |
| 31 | + # Convert to number first (if it's a string) |
| 32 | + if item["name"].is_a?(String) |
| 33 | + if item["name"].start_with?("0x") |
| 34 | + # Already hexadecimal |
| 35 | + numeric_value = item["name"].to_i(16) |
| 36 | + elsif item["name"] =~ /^[01]+$/ |
| 37 | + # Binary string without prefix |
| 38 | + numeric_value = item["name"].to_i(2) |
| 39 | + elsif item["name"] =~ /^\d+$/ |
| 40 | + # Decimal |
| 41 | + numeric_value = item["name"].to_i |
| 42 | + else |
| 43 | + # Not a number, leave it alone |
| 44 | + next |
| 45 | + end |
| 46 | + else |
| 47 | + # Already a number |
| 48 | + numeric_value = item["name"] |
| 49 | + end |
| 50 | + |
| 51 | + # Convert to hexadecimal string |
| 52 | + hex_str = numeric_value.to_s(16).downcase |
| 53 | + |
| 54 | + # Set the name to a specially formatted string that will be converted |
| 55 | + # to a hex literal in our custom JSON converter |
| 56 | + item["name"] = "0x" + hex_str |
| 57 | + end |
| 58 | + |
| 59 | + # Ensure bits is a number |
| 60 | + if item["bits"].is_a?(String) && item["bits"] =~ /^\d+$/ |
| 61 | + item["bits"] = item["bits"].to_i |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + result |
| 67 | +end |
| 68 | +%> |
| 69 | += Instruction Appendix |
| 70 | +:doctype: book |
| 71 | +:wavedrom: <%= $root %>/node_modules/.bin/wavedrom-cli |
| 72 | +// Now the document header is complete and the wavedrom attribute is active. |
| 73 | + |
| 74 | +<% instructions.sort_by(&:name).each do |inst| %> |
| 75 | +<%= anchor_for_inst(inst.name) %> |
| 76 | +== <%= inst.name %> |
| 77 | + |
| 78 | +Synopsis:: |
| 79 | +<%= inst.long_name %> |
| 80 | + |
| 81 | +Encoding:: |
| 82 | +<%- if inst.multi_encoding? -%> |
| 83 | +[NOTE] |
| 84 | +This instruction has different encodings in RV32 and RV64 |
| 85 | + |
| 86 | +RV32:: |
| 87 | +[wavedrom, ,svg,subs='attributes',width="100%"] |
| 88 | +.... |
| 89 | +<%= fix_entities(json_dump_with_hex_literals(process_wavedrom(inst.wavedrom_desc(32)))) %> |
| 90 | +.... |
| 91 | + |
| 92 | +RV64:: |
| 93 | +[wavedrom, ,svg,subs='attributes',width="100%"] |
| 94 | +.... |
| 95 | +<%= fix_entities(json_dump_with_hex_literals(process_wavedrom(inst.wavedrom_desc(64)))) %> |
| 96 | +.... |
| 97 | +<%- else -%> |
| 98 | +[wavedrom, ,svg,subs='attributes',width="100%"] |
| 99 | +.... |
| 100 | +<%= fix_entities(json_dump_with_hex_literals(process_wavedrom(inst.wavedrom_desc(inst.base.nil? ? 64 : inst.base)))) %> |
| 101 | +.... |
| 102 | +<%- end -%> |
| 103 | + |
| 104 | +Description:: |
| 105 | +<%= fix_entities(inst.description) %> |
| 106 | + |
| 107 | +Decode Variables:: |
| 108 | +<%- if inst.multi_encoding? ? (inst.decode_variables(32).empty? && inst.decode_variables(64).empty?) : inst.decode_variables(inst.base.nil? ? 64 : inst.base).empty? -%> |
| 109 | +<%= inst.name %> has no decode variables. |
| 110 | +<%- else -%> |
| 111 | +<%- if inst.multi_encoding? -%> |
| 112 | +<%- unless inst.decode_variables(32).empty? -%> |
| 113 | +*RV32:* |
| 114 | + |
| 115 | +[width="100%", cols="1,2", options="header"] |
| 116 | +|=== |
| 117 | +|Variable Name |Location |
| 118 | +<%- inst.decode_variables(32).each do |d| -%> |
| 119 | +|<%= d.name %> |<%= d.extract %> |
| 120 | +<%- end -%> |
| 121 | +|=== |
| 122 | +<%- end -%> |
| 123 | +<%- unless inst.decode_variables(64).empty? -%> |
| 124 | + |
| 125 | +*RV64:* |
| 126 | + |
| 127 | +[width="100%", cols="1,2", options="header"] |
| 128 | +|=== |
| 129 | +|Variable Name |Location |
| 130 | +<%- inst.decode_variables(64).each do |d| -%> |
| 131 | +|<%= d.name %> |<%= d.extract %> |
| 132 | +<%- end -%> |
| 133 | +|=== |
| 134 | +<%- end -%> |
| 135 | +<%- else -%> |
| 136 | +[width="100%", cols="1,2", options="header"] |
| 137 | +|=== |
| 138 | +|Variable Name |Location |
| 139 | +<%- inst.decode_variables(inst.base.nil? ? 64 : inst.base).each do |d| -%> |
| 140 | +|<%= d.name %> |<%= d.extract %> |
| 141 | +<%- end -%> |
| 142 | +|=== |
| 143 | +<%- end -%> |
| 144 | +<%- end -%> |
| 145 | + |
| 146 | +Included in:: |
| 147 | +<%- if inst.defined_by_condition.flat? -%> |
| 148 | +[options="autowrap,autowidth"] |
| 149 | +|=== |
| 150 | +| Extension | Version |
| 151 | +<% inst.defined_by_condition.flat_versions.each do |r| %> |
| 152 | +| *<%= r.name %>* | <%= fix_entities(r.requirement_specs_to_s) %> |
| 153 | +<% end %> |
| 154 | +|=== |
| 155 | +<%- else -%> |
| 156 | +<%= fix_entities(inst.defined_by_condition.to_asciidoc) %> |
| 157 | +<%- end -%> |
| 158 | + |
| 159 | +<<< |
| 160 | +<% end %> |
0 commit comments