{% extends "base.html" %}

{%- macro reserved_field(high, low, low_to_high) -%}
<tr class="reserved-field">
    {%- if high == low %}
    <td>[{{high}}]</td>
    {%- elif low_to_high %}
    <td>[{{low}}:{{high}}]</td>
    {%- else %}
    <td>[{{high}}:{{low}}]</td>
    {%- endif %}
    <td>-</td>
    <td>-</td>
    <td>-</td>
    <td></td>
    <td>-</td>
    <td></td>
</tr>
{%- endmacro %}

{%- block content -%}
<table border="1">
    <tr>
        <th>Bits</th>
        <th>Identifier</th>
        <th>Access</th>
        <th>Reset</th>
        <th>Decoded</th>
        <th>Name</th>
        <th></th>
    </tr>
    {%- for field in reversed(list(node.fields(skip_not_present=skip_not_present))) %}
    {%- if loop.first and field.inst.high < (node.get_property('regwidth') - 1) %}
    {{reserved_field(node.get_property('regwidth') - 1, field.inst.high + 1, reg_fields_are_low_to_high(node))}}
    {%- elif (not loop.first) and field.inst.high < loop.previtem.inst.low - 1 %}
    {{reserved_field(loop.previtem.inst.low - 1, field.inst.high + 1, reg_fields_are_low_to_high(node))}}
    {%- endif %}
    <tr id="{{field.inst.inst_name}}">
        {%- if field.inst.msb == field.inst.lsb %}
        <td>[{{field.inst.lsb}}]</td>
        {%- else %}
        <td>[{{field.inst.msb}}:{{field.inst.lsb}}]</td>
        {%- endif %}
        <td>
            {%- if (has_description(field) or has_enum_encoding(field)) -%}
                <a href="#{{field.inst.inst_name}}.desc">{{field.inst.inst_name}}</a>
            {%- else -%}
                {{- field.inst.inst_name -}}
            {%- endif -%}
        </td>
        <td>
            <span title='{{friendly_access(field.get_property("sw"))}}'>{{field.get_property("sw").name}}</span>
            {%- if field.get_property("onread") -%}
                , <span title='{{friendly_access(field.get_property("onread"))}}'>{{ field.get_property("onread").name -}}</span>
            {%- endif -%}
            {%- if field.get_property("onwrite") -%}
                , <span title='{{friendly_access(field.get_property("onwrite"))}}'>{{ field.get_property("onwrite").name -}}</span>
            {%- endif -%}
        </td>
        <td>
            {%- if field.get_property("reset") != None -%}
                {%- if field.get_property("reset") is integer -%}
                    {{"0x%x" % field.get_property("reset")}}
                {%- else -%}
                    {{macros.reference_link(field.get_property("reset"))}}
                {%- endif -%}
            {%- else -%}
            -
            {%- endif %}
        </td>
        <td>
            <input id="_FieldValueTester{{field.inst.inst_name}}" class="field-value-tester"
                type="text" autocomplete="off"
                data-name="{{field.inst.inst_name}}" oninput="onDecodedFieldInput(this)"/>
            {%- if field.get_property("encode") != None %}
            <select id="_FieldValueEnumTester{{field.inst.inst_name}}" class="field-value-enum-tester"
                data-name="{{field.inst.inst_name}}" onchange="onDecodedFieldEnumChange(this)">
            {% for member in field.get_property("encode") %}
                <option value="{{'0x%x' % member.value}}">{{member.name}}</option>
            {% endfor %}
            </select>
            {%- endif %}
        </td>
        <td>{{(field.get_html_name() or "-")|safe}}</td>
        <td>
            <button type="button" id="_RadixButton{{field.inst.inst_name}}" class="radix-button"
                title="Switch radix" aria-label="Switch radix"
                data-name="{{field.inst.inst_name}}" onclick="onRadixSwitch(this)">H</button>
            <a class="headerlink" href="#{{field.inst.inst_name}}" title="Permalink to this row"></a>
        </td>
    </tr>
    {%- if loop.last and field.inst.low != 0 %}
    {{reserved_field(field.inst.low - 1, 0, reg_fields_are_low_to_high(node))}}
    {%- endif %}
    {%- endfor %}
</table>
<p>
    {%- block reg_value_edit %}
    <label>
        Encoded Register Value:
        <input id="_RegValueTester" type="text" autocomplete="off" oninput="onEncodedRegInput(this)"/>
        <button type="button" class="reset-button" title="Reset register values" aria-label="Reset register values" onclick="onResetRegValue(this)"></button>
    </label>
    {%- endblock reg_value_edit %}
</p>

{%- for field in reversed(list(node.fields(skip_not_present=skip_not_present))) if (has_description(field) or has_enum_encoding(field) or has_extra_property_doc(field)) %}
{%- if loop.first %}
<hr/><h2>Field Descriptions</h2>
{%- endif %}
{% include "field_description.html" %}
{%- endfor %}

{%- endblock content %}
