readback!
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
{% block body %}
|
||||
// Request
|
||||
logic is_active;
|
||||
always_ff {{get_always_ff_event(cpuif_reset)}} begin
|
||||
if({{cpuif_reset.activehigh_identifier}}) begin
|
||||
always_ff {{get_always_ff_event(cpuif.reset)}} begin
|
||||
if({{cpuif.reset.activehigh_identifier}}) begin
|
||||
is_active <= '0;
|
||||
cpuif_req <= '0;
|
||||
cpuif_req_is_wr <= '0;
|
||||
@@ -16,7 +16,11 @@ always_ff {{get_always_ff_event(cpuif_reset)}} begin
|
||||
is_active <= '1;
|
||||
cpuif_req <= '1;
|
||||
cpuif_req_is_wr <= {{cpuif.signal("pwrite")}};
|
||||
cpuif_addr <= {{cpuif.signal("paddr")}}[ADDR_WIDTH-1:0];
|
||||
{%- if cpuif.data_width == 8 %}
|
||||
cpuif_addr <= {{cpuif.signal("paddr")}}[{{cpuif.addr_width-1}}:0];
|
||||
{%- else %}
|
||||
cpuif_addr <= { {{-cpuif.signal("paddr")}}[{{cpuif.addr_width-1}}:{{clog2(cpuif.data_width//8)}}], {{clog2(cpuif.data_width//8)}}'b0};
|
||||
{%- endif %}
|
||||
cpuif_wr_data <= {{cpuif.signal("pwdata")}};
|
||||
end
|
||||
end else begin
|
||||
@@ -27,7 +31,7 @@ always_ff {{get_always_ff_event(cpuif_reset)}} begin
|
||||
end
|
||||
end
|
||||
end
|
||||
assign cpuif_wr_bitstrb = '0;
|
||||
assign cpuif_wr_biten = '1;
|
||||
|
||||
// Response
|
||||
assign {{cpuif.signal("pready")}} = cpuif_rd_ack | cpuif_wr_ack;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ..utils import get_always_ff_event
|
||||
from ..utils import get_always_ff_event, clog2
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..exporter import RegblockExporter
|
||||
@@ -11,7 +11,7 @@ class CpuifBase:
|
||||
|
||||
def __init__(self, exp:'RegblockExporter', cpuif_reset:'SignalBase', data_width:int=32, addr_width:int=32):
|
||||
self.exp = exp
|
||||
self.cpuif_reset = cpuif_reset
|
||||
self.reset = cpuif_reset
|
||||
self.data_width = data_width
|
||||
self.addr_width = addr_width
|
||||
|
||||
@@ -22,10 +22,8 @@ class CpuifBase:
|
||||
def get_implementation(self) -> str:
|
||||
context = {
|
||||
"cpuif": self,
|
||||
"cpuif_reset": self.cpuif_reset,
|
||||
"data_width": self.data_width,
|
||||
"addr_width": self.addr_width,
|
||||
"get_always_ff_event": get_always_ff_event,
|
||||
"clog2": clog2,
|
||||
}
|
||||
|
||||
template = self.exp.jj_env.get_template(self.template_path)
|
||||
|
||||
@@ -3,17 +3,19 @@ from typing import Union
|
||||
|
||||
import jinja2 as jj
|
||||
from systemrdl.node import AddrmapNode, RootNode
|
||||
from systemrdl.walker import RDLWalker
|
||||
|
||||
from .addr_decode import AddressDecode
|
||||
from .field_logic import FieldLogic
|
||||
from .dereferencer import Dereferencer
|
||||
from .readback import Readback
|
||||
from .signals import InferredSignal, SignalBase
|
||||
from .signals import InferredSignal, RDLSignal
|
||||
|
||||
from .cpuif import CpuifBase
|
||||
from .cpuif.apb3 import APB3_Cpuif
|
||||
from .hwif import Hwif
|
||||
from .utils import get_always_ff_event
|
||||
from .scan_design import DesignScanner
|
||||
|
||||
class RegblockExporter:
|
||||
def __init__(self, **kwargs):
|
||||
@@ -29,10 +31,9 @@ class RegblockExporter:
|
||||
self.cpuif = None # type: CpuifBase
|
||||
self.address_decode = AddressDecode(self)
|
||||
self.field_logic = FieldLogic(self)
|
||||
self.readback = Readback(self)
|
||||
self.readback = None # type: Readback
|
||||
self.dereferencer = Dereferencer(self)
|
||||
self.default_resetsignal = InferredSignal("rst")
|
||||
self.cpuif_reset = self.default_resetsignal
|
||||
|
||||
|
||||
if user_template_dir:
|
||||
@@ -67,41 +68,55 @@ class RegblockExporter:
|
||||
|
||||
|
||||
cpuif_cls = kwargs.pop("cpuif_cls", APB3_Cpuif)
|
||||
hwif_cls = kwargs.pop("hwif_cls", Hwif)
|
||||
module_name = kwargs.pop("module_name", self.top_node.inst_name)
|
||||
package_name = kwargs.pop("package_name", module_name + "_pkg")
|
||||
module_file_path = os.path.join(output_dir, module_name + ".sv")
|
||||
package_file_path = os.path.join(output_dir, package_name + ".sv")
|
||||
|
||||
# Pipelining options
|
||||
retime_read_response = kwargs.pop("retime_read_response", True)
|
||||
retime_read_fanin = kwargs.pop("retime_read_fanin", False)
|
||||
|
||||
# Check for stray kwargs
|
||||
if kwargs:
|
||||
raise TypeError("got an unexpected keyword argument '%s'" % list(kwargs.keys())[0])
|
||||
|
||||
|
||||
# Scan the design for any unsupported features
|
||||
# Also collect pre-export information
|
||||
scanner = DesignScanner(self)
|
||||
RDLWalker().walk(self.top_node, scanner)
|
||||
if scanner.msg.had_error:
|
||||
scanner.msg.fatal(
|
||||
"Unable to export due to previous errors"
|
||||
)
|
||||
raise ValueError
|
||||
|
||||
# TODO: Scan design...
|
||||
|
||||
# TODO: derive this from somewhere
|
||||
self.cpuif_reset = self.default_resetsignal
|
||||
reset_signals = set([self.cpuif_reset, self.default_resetsignal])
|
||||
cpuif_reset_tmp = self.top_node.cpuif_reset
|
||||
if cpuif_reset_tmp:
|
||||
cpuif_reset = RDLSignal(cpuif_reset_tmp)
|
||||
else:
|
||||
cpuif_reset = self.default_resetsignal
|
||||
reset_signals = set([cpuif_reset, self.default_resetsignal])
|
||||
|
||||
self.cpuif = cpuif_cls(
|
||||
self,
|
||||
cpuif_reset=self.cpuif_reset, # TODO:
|
||||
data_width=32, # TODO: derive from the regwidth used by regs
|
||||
addr_width=32 # TODO:
|
||||
cpuif_reset=cpuif_reset,
|
||||
data_width=scanner.cpuif_data_width,
|
||||
addr_width=self.top_node.size.bit_length()
|
||||
)
|
||||
|
||||
self.hwif = hwif_cls(
|
||||
self.hwif = Hwif(
|
||||
self,
|
||||
package_name=package_name,
|
||||
)
|
||||
|
||||
self.readback = Readback(
|
||||
self,
|
||||
retime_read_fanin
|
||||
)
|
||||
|
||||
# Build Jinja template context
|
||||
context = {
|
||||
"module_name": module_name,
|
||||
"data_width": 32, # TODO:
|
||||
"addr_width": 32, # TODO:
|
||||
"reset_signals": reset_signals,
|
||||
"user_signals": [], # TODO:
|
||||
"interrupts": [], # TODO:
|
||||
@@ -110,13 +125,17 @@ class RegblockExporter:
|
||||
"address_decode": self.address_decode,
|
||||
"field_logic": self.field_logic,
|
||||
"readback": self.readback,
|
||||
"get_always_ff_event": get_always_ff_event,
|
||||
"retime_read_response": retime_read_response,
|
||||
}
|
||||
|
||||
# Write out design
|
||||
package_file_path = os.path.join(output_dir, package_name + ".sv")
|
||||
template = self.jj_env.get_template("package_tmpl.sv")
|
||||
stream = template.stream(context)
|
||||
stream.dump(package_file_path)
|
||||
|
||||
module_file_path = os.path.join(output_dir, module_name + ".sv")
|
||||
template = self.jj_env.get_template("module_tmpl.sv")
|
||||
stream = template.stream(context)
|
||||
stream.dump(module_file_path)
|
||||
|
||||
@@ -19,20 +19,17 @@ module {{module_name}} (
|
||||
{{hwif.port_declaration|indent(8)}}
|
||||
);
|
||||
|
||||
localparam ADDR_WIDTH = {{addr_width}};
|
||||
localparam DATA_WIDTH = {{data_width}};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// CPU Bus interface logic
|
||||
//--------------------------------------------------------------------------
|
||||
logic cpuif_req;
|
||||
logic cpuif_req_is_wr;
|
||||
logic [ADDR_WIDTH-1:0] cpuif_addr;
|
||||
logic [DATA_WIDTH-1:0] cpuif_wr_data;
|
||||
logic [DATA_WIDTH-1:0] cpuif_wr_bitstrb;
|
||||
logic [{{cpuif.addr_width-1}}:0] cpuif_addr;
|
||||
logic [{{cpuif.data_width-1}}:0] cpuif_wr_data;
|
||||
logic [{{cpuif.data_width-1}}:0] cpuif_wr_biten;
|
||||
|
||||
logic cpuif_rd_ack;
|
||||
logic [DATA_WIDTH-1:0] cpuif_rd_data;
|
||||
logic [{{cpuif.data_width-1}}:0] cpuif_rd_data;
|
||||
logic cpuif_rd_err;
|
||||
|
||||
logic cpuif_wr_ack;
|
||||
@@ -47,8 +44,8 @@ module {{module_name}} (
|
||||
decoded_reg_strb_t decoded_reg_strb;
|
||||
logic decoded_req;
|
||||
logic decoded_req_is_wr;
|
||||
logic [DATA_WIDTH-1:0] decoded_wr_data;
|
||||
logic [DATA_WIDTH-1:0] decoded_wr_bitstrb;
|
||||
logic [{{cpuif.data_width-1}}:0] decoded_wr_data;
|
||||
logic [{{cpuif.data_width-1}}:0] decoded_wr_biten;
|
||||
|
||||
always_comb begin
|
||||
{{address_decode.get_implementation()|indent(8)}}
|
||||
@@ -62,7 +59,7 @@ module {{module_name}} (
|
||||
assign decoded_req = cpuif_req;
|
||||
assign decoded_req_is_wr = cpuif_req_is_wr;
|
||||
assign decoded_wr_data = cpuif_wr_data;
|
||||
assign decoded_wr_bitstrb = cpuif_wr_bitstrb;
|
||||
assign decoded_wr_biten = cpuif_wr_biten;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Field logic
|
||||
@@ -76,6 +73,27 @@ module {{module_name}} (
|
||||
//--------------------------------------------------------------------------
|
||||
// Readback
|
||||
//--------------------------------------------------------------------------
|
||||
logic readback_err;
|
||||
logic readback_done;
|
||||
logic [{{cpuif.data_width-1}}:0] readback_data;
|
||||
{{readback.get_implementation()|indent}}
|
||||
|
||||
{% if retime_read_response %}
|
||||
always_ff {{get_always_ff_event(cpuif.reset)}} begin
|
||||
if({{cpuif.reset.activehigh_identifier}}) begin
|
||||
cpuif_rd_ack <= '0;
|
||||
cpuif_rd_data <= '0;
|
||||
cpuif_rd_err <= '0;
|
||||
end else begin
|
||||
cpuif_rd_ack <= readback_done;
|
||||
cpuif_rd_data <= readback_data;
|
||||
cpuif_rd_err <= readback_err;
|
||||
end
|
||||
end
|
||||
{% else %}
|
||||
assign cpuif_rd_ack = readback_done;
|
||||
assign cpuif_rd_data = readback_data;
|
||||
assign cpuif_rd_err = readback_err;
|
||||
{% endif %}
|
||||
|
||||
endmodule
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from typing import TYPE_CHECKING
|
||||
import math
|
||||
|
||||
from .generators import ReadbackAssignmentGenerator
|
||||
from ..utils import get_always_ff_event
|
||||
@@ -8,8 +9,9 @@ if TYPE_CHECKING:
|
||||
from systemrdl.node import AddrmapNode
|
||||
|
||||
class Readback:
|
||||
def __init__(self, exp:'RegblockExporter'):
|
||||
def __init__(self, exp:'RegblockExporter', do_fanin_stage: bool):
|
||||
self.exp = exp
|
||||
self.do_fanin_stage = do_fanin_stage
|
||||
|
||||
@property
|
||||
def top_node(self) -> 'AddrmapNode':
|
||||
@@ -18,13 +20,49 @@ class Readback:
|
||||
def get_implementation(self) -> str:
|
||||
gen = ReadbackAssignmentGenerator(self.exp)
|
||||
array_assignments = gen.get_content(self.top_node)
|
||||
array_size = gen.current_offset
|
||||
|
||||
# Enabling the fanin stage doesnt make sense if readback fanin is
|
||||
# small. This also avoids pesky corner cases
|
||||
if array_size < 4:
|
||||
self.do_fanin_stage = False
|
||||
|
||||
context = {
|
||||
"array_assignments" : array_assignments,
|
||||
"array_size" : gen.current_offset,
|
||||
"array_size" : array_size,
|
||||
"get_always_ff_event": get_always_ff_event,
|
||||
"cpuif_reset": self.exp.cpuif_reset,
|
||||
"cpuif": self.exp.cpuif,
|
||||
"do_fanin_stage": self.do_fanin_stage,
|
||||
}
|
||||
|
||||
if self.do_fanin_stage:
|
||||
# If adding a fanin pipeline stage, goal is to try to
|
||||
# split the fanin path in the middle so that fanin into the stage
|
||||
# and the following are roughly balanced.
|
||||
fanin_target = math.sqrt(array_size)
|
||||
|
||||
# Size of fanin group to consume per fanin element
|
||||
fanin_stride = math.floor(fanin_target)
|
||||
|
||||
# Number of array elements to reduce to.
|
||||
# Round up to an extra element in case there is some residual
|
||||
fanin_array_size = math.ceil(array_size / fanin_stride)
|
||||
|
||||
# leftovers are handled in an extra array element
|
||||
fanin_residual_stride = array_size % fanin_stride
|
||||
|
||||
if fanin_residual_stride != 0:
|
||||
# If there is a partial fanin element, reduce the number of
|
||||
# loops performed in the bulk fanin stage
|
||||
fanin_loop_iter = fanin_array_size - 1
|
||||
else:
|
||||
fanin_loop_iter = fanin_array_size
|
||||
|
||||
context['fanin_stride'] = fanin_stride
|
||||
context['fanin_array_size'] = fanin_array_size
|
||||
context['fanin_residual_stride'] = fanin_residual_stride
|
||||
context['fanin_loop_iter'] = fanin_loop_iter
|
||||
|
||||
template = self.exp.jj_env.get_template(
|
||||
"readback/templates/readback.sv"
|
||||
)
|
||||
|
||||
@@ -1,45 +1,68 @@
|
||||
{% if array_assignments is not none %}
|
||||
logic readback_err;
|
||||
logic readback_done;
|
||||
logic [DATA_WIDTH-1:0] readback_data;
|
||||
logic [DATA_WIDTH-1:0] readback_array[{{array_size}}];
|
||||
|
||||
// Assign readback values to a flattened array
|
||||
logic [{{cpuif.data_width-1}}:0] readback_array[{{array_size}}];
|
||||
{{array_assignments}}
|
||||
|
||||
{% if do_fanin_stage %}
|
||||
// fanin stage
|
||||
logic [31:0] readback_array_c[{{fanin_array_size}}];
|
||||
for(genvar g=0; g<{{fanin_loop_iter}}; g++) begin
|
||||
always_comb begin
|
||||
automatic logic [31:0] readback_data_var;
|
||||
readback_data_var = '0;
|
||||
for(int i=g*{{fanin_stride}}; i<((g+1)*{{fanin_stride}}); i++) readback_data_var |= readback_array[i];
|
||||
readback_array_c[g] = readback_data_var;
|
||||
end
|
||||
end
|
||||
{%- if fanin_residual_stride == 1 %}
|
||||
assign readback_array_c[{{fanin_array_size-1}}] = readback_array[{{array_size-1}}];
|
||||
{%- elif fanin_residual_stride > 1 %}
|
||||
always_comb begin
|
||||
automatic logic [DATA_WIDTH-1:0] readback_data_var;
|
||||
automatic logic [31:0] readback_data_var;
|
||||
readback_data_var = '0;
|
||||
for(int i={{(fanin_array_size-1) * fanin_stride}}; i<{{array_size-1}}; i++) readback_data_var |= readback_array[i];
|
||||
readback_array_c[{{fanin_array_size-1}}] = readback_data_var;
|
||||
end
|
||||
{%- endif %}
|
||||
|
||||
logic [31:0] readback_array_r[{{fanin_array_size}}];
|
||||
logic readback_done_r;
|
||||
always_ff @(posedge clk) begin
|
||||
if(rst) begin
|
||||
for(int i=0; i<{{fanin_array_size}}; i++) readback_array_r[i] <= '0;
|
||||
readback_done_r <= '0;
|
||||
end else begin
|
||||
readback_array_r <= readback_array_c;
|
||||
readback_done_r <= decoded_req & ~decoded_req_is_wr;
|
||||
end
|
||||
end
|
||||
|
||||
// Reduce the array
|
||||
always_comb begin
|
||||
automatic logic [31:0] readback_data_var;
|
||||
readback_done = decoded_req & ~decoded_req_is_wr;
|
||||
readback_err = '0;
|
||||
|
||||
readback_data_var = '0;
|
||||
for(int i=0; i<{{array_size}}; i++) begin
|
||||
readback_data_var |= readback_array[i];
|
||||
end
|
||||
for(int i=0; i<{{fanin_array_size}}; i++) readback_data_var |= readback_array_r[i];
|
||||
readback_data = readback_data_var;
|
||||
end
|
||||
|
||||
always_ff {{get_always_ff_event(cpuif_reset)}} begin
|
||||
if({{cpuif_reset.activehigh_identifier}}) begin
|
||||
cpuif_rd_ack <= '0;
|
||||
cpuif_rd_data <= '0;
|
||||
cpuif_rd_err <= '0;
|
||||
end else begin
|
||||
cpuif_rd_ack <= readback_done;
|
||||
cpuif_rd_data <= readback_data;
|
||||
cpuif_rd_err <= readback_err;
|
||||
end
|
||||
{%- else %}
|
||||
// Reduce the array
|
||||
always_comb begin
|
||||
automatic logic [{{cpuif.data_width-1}}:0] readback_data_var;
|
||||
readback_done = decoded_req & ~decoded_req_is_wr;
|
||||
readback_err = '0;
|
||||
readback_data_var = '0;
|
||||
for(int i=0; i<{{array_size}}; i++) readback_data_var |= readback_array[i];
|
||||
readback_data = readback_data_var;
|
||||
end
|
||||
{%- endif %}
|
||||
|
||||
|
||||
|
||||
{%- else %}
|
||||
always_ff {{get_always_ff_event(cpuif_reset)}} begin
|
||||
if({{cpuif_reset.activehigh_identifier}}) begin
|
||||
cpuif_rd_ack <= '0;
|
||||
end else begin
|
||||
cpuif_rd_ack <= decoded_req & ~decoded_req_is_wr;
|
||||
end
|
||||
end
|
||||
assign cpuif_rd_data = '0;
|
||||
assign cpuif_rd_err = '0;
|
||||
assign readback_done = decoded_req & ~decoded_req_is_wr;
|
||||
assign readback_data = '0;
|
||||
assign readback_err = '0;
|
||||
{% endif %}
|
||||
|
||||
@@ -1,14 +1,53 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
"""
|
||||
- Signal References
|
||||
Collect any references to signals that lie outside of the hierarchy
|
||||
These will be added as top-level signals
|
||||
- top-level interrupts
|
||||
from systemrdl.walker import RDLListener
|
||||
from systemrdl.node import AddrmapNode
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from systemrdl.node import Node, RegNode, SignalNode, MemNode
|
||||
from .exporter import RegblockExporter
|
||||
|
||||
|
||||
Validate:
|
||||
- Error if a property references a non-signal component, or property reference from
|
||||
outside the export hierarchy
|
||||
- No Mem components allowed
|
||||
- Uniform regwidth, accesswidth, etc.
|
||||
"""
|
||||
class DesignScanner(RDLListener):
|
||||
"""
|
||||
Scans through the register model and validates that any unsupported features
|
||||
are not present.
|
||||
|
||||
Also collects any information that is required prior to the start of the export process.
|
||||
"""
|
||||
def __init__(self, exp:'RegblockExporter'):
|
||||
self.exp = exp
|
||||
self.cpuif_data_width = 0
|
||||
self.msg = exp.top_node.env.msg
|
||||
|
||||
def enter_Reg(self, node: 'RegNode') -> None:
|
||||
# The CPUIF's bus width is sized according to the largest register in the design
|
||||
self.cpuif_data_width = max(self.cpuif_data_width, node.get_property("regwidth"))
|
||||
|
||||
# TODO: Collect any references to signals that lie outside of the hierarchy
|
||||
# These will be added as top-level signals
|
||||
|
||||
def enter_Component(self, node: 'Node') -> None:
|
||||
if not isinstance(node, AddrmapNode) and node.external:
|
||||
self.msg.error(
|
||||
"Exporter does not support external components",
|
||||
node.inst.inst_src_ref
|
||||
)
|
||||
|
||||
def enter_Signal(self, node: 'SignalNode') -> None:
|
||||
# If encountering a CPUIF reset that is nested within the register model,
|
||||
# warn that it will be ignored.
|
||||
# Only cpuif resets in the top-level node or above will be honored
|
||||
if node.get_property('cpuif_reset') and (node.parent != self.exp.top_node):
|
||||
self.msg.warning(
|
||||
"Only cpuif_reset signals that are instantiated in the top-level "
|
||||
+ "addrmap or above will be honored. Any cpuif_reset signals nested "
|
||||
+ "within children of the addrmap being exported will be ignored.",
|
||||
node.inst.inst_src_ref
|
||||
)
|
||||
|
||||
def enter_Mem(self, node: 'MemNode') -> None:
|
||||
self.msg.error(
|
||||
"Cannot export a register block that contains a memory",
|
||||
node.inst.inst_src_ref
|
||||
)
|
||||
|
||||
@@ -30,3 +30,6 @@ def get_always_ff_event(resetsignal: 'Optional[SignalBase]') -> str:
|
||||
elif resetsignal.is_async and not resetsignal.is_activehigh:
|
||||
return f"@(posedge clk or negedge {resetsignal.identifier})"
|
||||
return "@(posedge clk)"
|
||||
|
||||
def clog2(n: int) -> int:
|
||||
return n.bit_length() - 1
|
||||
|
||||
Reference in New Issue
Block a user