Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] MultiThreshold by Binary Search #687

Closed
wants to merge 121 commits into from
Closed
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
3002e62
HDL for new thresholding by binary search.
preusser Sep 20, 2022
3c92c2f
IP core support files for thresholding module.
preusser Sep 20, 2022
09c6da9
[thresholding] FINN-44: Add skeleton class for Threshold (the RTL ver…
fionnodonohoe-xlnx Sep 21, 2022
1dde247
[thresholding] FINN-44: Update custom_op's __init__ to pick up new Th…
fionnodonohoe-xlnx Sep 21, 2022
95082d3
[thresholding] FINN-44: Add inital node attributes for Thresholding b…
fionnodonohoe-xlnx Sep 21, 2022
72832be
[thresholding] FINN-44: Add calc_tmem() method
fionnodonohoe-xlnx Sep 21, 2022
0d4e3be
[thresholding] FINN-44: Add methods for retrieving inut/output/weight…
fionnodonohoe-xlnx Sep 21, 2022
28568c6
[thresholding] FINN-44: Add methods for retrieving node input/output …
fionnodonohoe-xlnx Sep 21, 2022
280870d
Thresholding over signed inputs.
preusser Oct 24, 2022
2bf1a21
Introduce an optional threshold output bias.
preusser Oct 24, 2022
4c7b5ac
Exposing the thresholding bias through the AXI adapter.
preusser Oct 25, 2022
7663d3f
Have thresholding wrapper pass on bias parameter and compute derived …
preusser Oct 25, 2022
55e2eac
Fix typo.
preusser Oct 25, 2022
fa5d71a
Abandon IPI support files.
preusser Oct 25, 2022
174c0ff
[thresholding] allow for positive and negative bias values
fionnodonohoe-xlnx Nov 15, 2022
2ec20e5
[thresholding] pass bias from top module to thresholding.sv core
fionnodonohoe-xlnx Nov 15, 2022
8616148
[thresholding] pass O_BITS from top module to thresholding.sv core
fionnodonohoe-xlnx Nov 15, 2022
275abad
[thresholding] pass C_BITS from top module to thresholding.sv core
fionnodonohoe-xlnx Nov 15, 2022
8849c02
[thresholding] create & fill in RTL template values using FINN
fionnodonohoe-xlnx Nov 16, 2022
84704ed
[thresholding] add method get_weightstream_width()
fionnodonohoe-xlnx Nov 16, 2022
9aa7ff3
[thresholding] add method get_in/output_width()
fionnodonohoe-xlnx Nov 16, 2022
608b5da
[thresholding] add method body for code_generation_ipi()
fionnodonohoe-xlnx Nov 16, 2022
ca6e7e7
[thresholding] add method get_verilog_top_module_intf_names()
fionnodonohoe-xlnx Nov 16, 2022
7266ee9
[thresholding] retrieve axilite write sequence for runtime weight pro…
fionnodonohoe-xlnx Nov 16, 2022
f88bdbf
[thresholding] add methods for creating weight files for each simulat…
fionnodonohoe-xlnx Nov 16, 2022
560771a
[thresholding] add method generate_params()
fionnodonohoe-xlnx Nov 16, 2022
e763bf8
[thresholding] add method for preparing a Pyverilator object for RTL …
fionnodonohoe-xlnx Nov 16, 2022
84e08f1
[thresholding] add method to run rtlsim on a thresholding binary sear…
fionnodonohoe-xlnx Nov 16, 2022
b0be07a
[thresholding] add stubbed method for ipgen_singlenode_code()
fionnodonohoe-xlnx Nov 16, 2022
30d22f8
[thresholding] update class name to a more consistent naming convention
fionnodonohoe-xlnx Nov 16, 2022
3594edd
[thresholding] add fpgadataflow pytests for thresholding binary searc…
fionnodonohoe-xlnx Nov 17, 2022
0bee70d
[thresholding] add linter fixes
fionnodonohoe-xlnx Nov 17, 2022
0689c6a
[thresholding] add flake8 fixes
fionnodonohoe-xlnx Nov 17, 2022
e9a4a7b
[thresholding] change the pytest markers to omit tests from quicktest
fionnodonohoe-xlnx Nov 17, 2022
41c0b4b
[thresholding] update copyright banners of files I have added/changed
fionnodonohoe-xlnx Nov 25, 2022
71ef39b
Translate byte to parameter word addressing in AXI adapter.
preusser Dec 1, 2022
47a0cf9
Merge branch 'dev' into feature/thresholding_addressing
preusser Dec 1, 2022
bdd100f
Merge branch 'dev' into feature/thresholding
fionnodonohoe-xlnx Dec 13, 2022
d44a66c
[thresholding] remove unused attribute
fionnodonohoe-xlnx Dec 19, 2022
f79b9ec
[thresholding] remove unnecessary HLS bug prevention check
fionnodonohoe-xlnx Dec 19, 2022
7b82de2
[thresholding] align methods with hlscustom class by adding in additi…
fionnodonohoe-xlnx Dec 19, 2022
e2816d3
[thresholding] replace hardcoded tcl commands with node attributes
fionnodonohoe-xlnx Dec 19, 2022
61acc64
Merge branch 'feature/thresholding' into feature/thresholding_addressing
preusser Dec 20, 2022
bda05ae
Fix BIAS parameter specification.
preusser Dec 20, 2022
7388e76
[thresholding] remove unused ram_style attribute
fionnodonohoe-xlnx Dec 20, 2022
e965396
[thresholding] skip test for unsupported cppsim configuration and mer…
fionnodonohoe-xlnx Jan 5, 2023
2b8a674
[thresholding] moving find_next_power_of_2() to the util suite
fionnodonohoe-xlnx Jan 5, 2023
45bb19f
[thresholding] remove find_next_power_of_2() from thresholding binary…
fionnodonohoe-xlnx Jan 5, 2023
ca00422
[thresholding] replace math functions with existing functions
fionnodonohoe-xlnx Jan 5, 2023
7f3455f
[thresholding] remove convept of mem_mode for RTL thresholding binary…
fionnodonohoe-xlnx Jan 5, 2023
4bc69f1
[thresholding] add methods needed for convertingToHls transformation
fionnodonohoe-xlnx Jan 5, 2023
3b6a198
[thresholding] add convertingToHls transformation for thresholding bi…
fionnodonohoe-xlnx Jan 5, 2023
b3800cd
[thresholding] add test for convertingToHls transformation for thresh…
fionnodonohoe-xlnx Jan 5, 2023
11464d8
[thresholding] skip tests with unsupported folding factor input
fionnodonohoe-xlnx Jan 5, 2023
e71b1c0
[thresholding] add comments for attributes
fionnodonohoe-xlnx Jan 5, 2023
3be1140
[thresholding] replace min() with signed() function
fionnodonohoe-xlnx Jan 5, 2023
e05effc
[thresholding] fix formatting from pre-commit
fionnodonohoe-xlnx Jan 5, 2023
48c3304
[thresholding] fix more flake8 formatting
fionnodonohoe-xlnx Jan 5, 2023
1e8a36c
[thresholding] remove backslashes for flake8
fionnodonohoe-xlnx Jan 5, 2023
08f1b5f
[thresholding] more flake8 fixes
fionnodonohoe-xlnx Jan 5, 2023
481d773
[thresholding] undo flake8 fixes
fionnodonohoe-xlnx Jan 5, 2023
a51bef4
[thresholding] another flake8 fix
fionnodonohoe-xlnx Jan 5, 2023
2c313ad
[thresholding] remove cppsim test file generation
fionnodonohoe-xlnx Jan 6, 2023
49bdd28
[thresholding] remove unnecessary data generation functions for simul…
fionnodonohoe-xlnx Jan 6, 2023
e663030
[thresholding] remove potentially problematic helper function
fionnodonohoe-xlnx Jan 6, 2023
42dbf23
[thresholding] implement flake8 formatting
fionnodonohoe-xlnx Jan 6, 2023
933d747
[thresholding] remove unused imports
fionnodonohoe-xlnx Jan 6, 2023
5c6dcd9
[thresholding] remove last ununsed import
fionnodonohoe-xlnx Jan 6, 2023
51acd11
[thresholding] reformat existing import
fionnodonohoe-xlnx Jan 6, 2023
9dd4e67
Merge pull request #715 from Xilinx/feature/thresholding_addressing
auphelia Jan 10, 2023
412de82
Merge branch 'dev' into feature/thresholding
auphelia Jan 18, 2023
b886a5a
[Docs] Add bin search thresholding to docs generation
auphelia Jan 18, 2023
2c3de2a
Corrected address width in Verilog wrapper for thresholding.
preusser Jan 23, 2023
7c9f5d8
[thresholding] remove bug affecting input width in top level wrapper
fionnodonohoe-xlnx Jan 23, 2023
3a0d59d
[thresholding] adjust thresholding binary search tests to use word ad…
fionnodonohoe-xlnx Jan 23, 2023
757e3a1
[thresholding] adjust typo in exception
fionnodonohoe-xlnx Jan 23, 2023
479575b
[thresholding] undo copyright header change - only needed for new files
fionnodonohoe-xlnx Jan 23, 2023
0d99b6c
[thresholding] add docstring for migrated find_next_power_of_2() func…
fionnodonohoe-xlnx Jan 23, 2023
5a77a32
[thresholding] add docstrings for methods not in base class
fionnodonohoe-xlnx Jan 23, 2023
eeed070
[thresholding] remove unused method
fionnodonohoe-xlnx Jan 23, 2023
c270868
[thresholding] remove 'return' at end of function - not needed
fionnodonohoe-xlnx Jan 27, 2023
af22177
[thresholding] remove cppsim exec_mode from test - not exercised
fionnodonohoe-xlnx Jan 27, 2023
fab120b
[thresholding] remove unused attributes
fionnodonohoe-xlnx Jan 27, 2023
5d6c964
[thresholding] adjust i/o port names on thresholding RTL wrapper
fionnodonohoe-xlnx Jan 27, 2023
bdfa6cb
[thresholding] remove duplicated test helper function
fionnodonohoe-xlnx Jan 31, 2023
6809351
[thresholding] assert on finding unsupported memory mode for threshol…
fionnodonohoe-xlnx Jan 31, 2023
4515cf7
[thresholding] precommit fix
fionnodonohoe-xlnx Jan 31, 2023
b51498e
[thresholding] precommit fix 2
fionnodonohoe-xlnx Jan 31, 2023
ff3b201
[thresholding] precommit fix 3
fionnodonohoe-xlnx Jan 31, 2023
e0e263b
Merge branch 'dev' into feature/thresholding
auphelia Jan 31, 2023
fc7e00d
[thresholding] adjust templates so that .sv files are modular and can…
fionnodonohoe-xlnx Mar 23, 2023
f530aba
[thresholding]: remove SIGN template in thresholding RTL and create p…
fionnodonohoe-xlnx Mar 23, 2023
3cd600c
[thresholding]: decouple thresholding core from axi wrapper by removi…
fionnodonohoe-xlnx Mar 23, 2023
54afa63
[thresholding]: patch in PE value to the thresholding AXI module and …
fionnodonohoe-xlnx Mar 28, 2023
29f9e1c
[thresholding]: remove reset that erases the 0th stage threshold value
fionnodonohoe-xlnx Mar 30, 2023
2c4c8e2
[thresholding]: enable PE testing of RTL threhoslding binary search node
fionnodonohoe-xlnx Mar 31, 2023
5d07a43
[thresholding]: add comment about why bipolar activations skipped for…
fionnodonohoe-xlnx Mar 31, 2023
33fadc7
Merge branch 'dev' into feature/thresholding
fionnodonohoe-xlnx Mar 31, 2023
fcf579c
fix precommit issues
fionnodonohoe-xlnx Mar 31, 2023
6c9d1f5
[thresholding] only adjust MSB thresholding addressing bits when chan…
fionnodonohoe-xlnx Apr 5, 2023
b247ffb
[thresholding] update binary search to match qonnx 0.2.0
fionnodonohoe-xlnx Apr 5, 2023
7be5ce4
Defaulting BIAS and SIGNED parameters. Renaming M to K avoiding namin…
preusser Apr 17, 2023
a0120f2
Thresholding rework to support parameter readback.
preusser May 4, 2023
81cad6e
Merge branch 'dev' into feature/thresholding_readback
preusser May 4, 2023
c7c18d9
Merge branch 'dev' into feature/thresholding
preusser Jun 26, 2023
28e5ad7
Packaged IP for RTL thresholding implementation.
preusser Jun 27, 2023
bd7ec27
Merge branch 'dev' into feature/thresholding
preusser Nov 14, 2023
bc5b738
Allow for custom start-up intialization of thresholds.
preusser Nov 15, 2023
730bcf8
Make AXI-Lite threshold read/write interface optional.
preusser Nov 15, 2023
95d6a3e
Double wrapping for both structured SystemVerilog parameters and a IP…
preusser Nov 16, 2023
8003c91
Templating the wrapper layers for specialization by FINN compiler.
preusser Nov 16, 2023
d9db257
Replicate correct O_BITS computation for negative BIASes in inner wra…
preusser Nov 20, 2023
073844a
Correcting wrong unsigned interpretation of BIAS in inner wrapper.
preusser Nov 20, 2023
98184ac
Working around an LRM ambiguity when also having assignments in initi…
preusser Nov 20, 2023
528184d
Attempt to mitigate long elaboration times by more explicit configura…
preusser Nov 21, 2023
8fb250c
Switching to threshold initialization from hex data files.
preusser Nov 23, 2023
9de06be
Adding parameters to request local memories of a given depth to be ma…
preusser Nov 28, 2023
038a58a
Prevent BRAM use below specified trigger.
preusser Nov 29, 2023
f39187c
Fixing N vs. 2^N confusion in the module description.
preusser Dec 1, 2023
7284d2c
Add deep pipelining option to thresholding implementation.
preusser Dec 6, 2023
aa57255
Removing the inner wrapper. Exposing the DEEP_PIPELINING option and a…
preusser Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions finn-rtllib/thresholding/hdl/thresholding.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/******************************************************************************
* Copyright (C) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION). HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @brief Pipelined thresholding by binary search.
* @author Thomas B. Preußer <[email protected]>
*
* @description
* Produces the N-bit count of those among 2^N-1 thresholds that are not
* larger than the corresponding input:
* y = Σ(T_i <= x)
* The result is computed by binary search. The runtime-configurable
* thresholds must be written in ascending order:
* i < j => T_i < T_j
* The design supports channel folding allowing each input to be processed
* with respect to a selectable set of thresholds. The corresponding
* threshold configuration relies on a channel address prefix. Inputs are
* accompanied by a channel selector.
*****************************************************************************/
module $MODULE_NAME$ #(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@preusser for clarity, is this the variant that supports FP32 thresholding? or is this for integers only?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the base version for integers only at this point.

int unsigned N, // output precision
int unsigned M, // input/threshold precision
int unsigned C, // number of channels

int BIAS, // offsetting the output [0, 2^N-1) -> [-BIAS, 2^N-1 - BIAS)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@preusser is the comment here correct? L154 reads
assign odat = pipe[N].res + BIAS;

so to me it appears the comment here should read

int BIAS,  // offsetting the output [0, 2^N-1) -> [BIAS, 2^N-1 + BIAS)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! @fionnodonohoe-xlnx, please, copy this change in the comment associated with the parameter.


int unsigned C_BITS,
int unsigned O_BITS
)(
// Global Control
input logic clk,
input logic rst,

// Threshold Configuration
input logic twe,
input logic [$clog2(C)+N-1:0] twa,
input logic [ M-1:0] twd,

// Clock Enable for Stream Processing
input logic en,

// Input Stream
input logic ivld,
input logic [C_BITS-1:0] icnl, // Ignored for C == 1
input logic $SIGN$ [M -1:0] idat,

// Output Stream
output logic ovld,
output logic [C_BITS-1:0] ocnl,
output logic [O_BITS-1:0] odat
);

// Pipeline Links & Feed
typedef struct packed {
logic vld; // Valid data identification
logic [C_BITS-1:0] cnl; // Channel
logic $SIGN$ [M -1:0] val; // Original input value
logic [0:N-1] res; // Assembling result with valid prefix [0:stage] after stage #stage
} pipe_t;
uwire pipe_t pipe[0:N];
assign pipe[0] = pipe_t'{ vld: ivld, cnl: icnl, val: idat, res: {N{1'bx}} }; // Feed original input

// Stages: 0, 1, ..., N-1
uwire [0:N-1] tws = (twa[N-1:0]+1) & ~twa[N-1:0]; // Write Select per stage by address suffix
for(genvar stage = 0; stage < N; stage++) begin : genStages

// Threshold Memory
uwire $SIGN$ [M-1:0] thresh;
if(1) begin : blkUpdate

// Write control: local select from global address
uwire we = twe && tws[stage];
if((C == 1) && (stage == 0)) begin
logic $SIGN$ [M-1:0] Thresh = 'x;
always_ff @(posedge clk) begin
if(rst) Thresh <= 'x;
else if(we) Thresh <= twd;
end
assign thresh = Thresh;
end
else begin
logic $SIGN$ [M-1:0] Threshs[C * 2**stage];
uwire [$clog2(C)+stage-1:0] wa = twa[$left(twa):N-stage];
uwire [$clog2(C)+stage-1:0] ra;
if(C > 1) assign ra[stage+:C_BITS] = pipe[stage].cnl;
if(stage) assign ra[stage-1:0] = pipe[stage].res[0:stage-1];

// Write
always_ff @(posedge clk) begin
if(we) Threshs[wa] <= twd;
end

// Read
logic $SIGN$ [M-1:0] RdReg;
always_ff @(posedge clk) begin
if(en) RdReg <= Threshs[ra];
end
assign thresh = RdReg;
end

end : blkUpdate

// Pipeline regs simply copying the input
pipe_t State = '{ vld: 0, cnl: 'x, val: 'x, res: 'x };
always_ff @(posedge clk) begin
if(rst) State <= '{ vld: 0, cnl: 'x, val: 'x, res: 'x };
else if(en) State <= pipe[stage];
end

// Assemble pipeline data
logic [0:N-1] res;
always_comb begin
res = State.res;
res[stage] = thresh <= State.val; // Patch in next result bit
end
assign pipe[stage+1] = '{
vld: State.vld,
cnl: State.cnl,
val: State.val,
res: res
};

end : genStages

// Output
assign ovld = pipe[N].vld;
assign ocnl = pipe[N].cnl;
assign odat = pipe[N].res + BIAS;

endmodule : $MODULE_NAME$
202 changes: 202 additions & 0 deletions finn-rtllib/thresholding/hdl/thresholding_axi.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/******************************************************************************
* Copyright (C) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION). HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @brief All-AXI interface adapter for thresholding module.
* @author Thomas B. Preußer <[email protected]>
*****************************************************************************/

module $MODULE_NAME_AXI$ #(
int unsigned N, // output precision
int unsigned M, // input/threshold precision
int unsigned C, // Channels

int BIAS, // offsetting the output [0, 2^N-1) -> [-BIAS, 2^N-1 - BIAS)

int unsigned O_BITS
)(
//- Global Control ------------------
input logic ap_clk,
input logic ap_rst_n,

//- AXI Lite ------------------------
// Writing
input logic s_axilite_AWVALID,
output logic s_axilite_AWREADY,
input logic [$clog2(C)+N-1:0] s_axilite_AWADDR,

input logic s_axilite_WVALID,
output logic s_axilite_WREADY,
input logic [31:0] s_axilite_WDATA,
input logic [ 3:0] s_axilite_WSTRB,

output logic s_axilite_BVALID,
input logic s_axilite_BREADY,
output logic [1:0] s_axilite_BRESP,

// Reading
input logic s_axilite_ARVALID,
output logic s_axilite_ARREADY,
input logic [0:0] s_axilite_ARADDR,

output logic s_axilite_RVALID,
input logic s_axilite_RREADY,
output logic [31:0] s_axilite_RDATA,
output logic [ 1:0] s_axilite_RRESP,

//- AXI Stream - Input --------------
output logic s_axis_tready,
input logic s_axis_tvalid,
input logic [((M+7)/8)*8-1:0] s_axis_tdata,

//- AXI Stream - Output -------------
input logic m_axis_tready,
output logic m_axis_tvalid,
output logic [((O_BITS+7)/8)*8-1:0] m_axis_tdata
);
//- Global Control ------------------------------------------------------
uwire clk = ap_clk;
uwire rst = !ap_rst_n;

//- AXI Lite: Threshold Configuration -----------------------------------
uwire twe;
uwire [$clog2(C)+N-1:0] twa;
uwire [ M-1:0] twd;
if(1) begin : blkAxiLite
logic WABusy = 0;
logic WDBusy = 0;
logic [$clog2(C)+N-1:0] Addr = 'x;
logic [ M-1:0] Data = 'x;

assign twe = WABusy && WDBusy;
assign twa = Addr;
assign twd = Data;

uwire clr_wr = rst || (twe && s_axilite_BREADY);
always_ff @(posedge clk) begin : blockName
if(clr_wr) begin
WABusy <= 0;
Addr <= 'x;
WDBusy <= 0;
Data <= 'x;
end
else begin
if(!WABusy) begin
WABusy <= s_axilite_AWVALID;
Addr <= s_axilite_AWADDR[$clog2(C)+N-1:0];
end
if(!WDBusy) begin
WDBusy <= s_axilite_WVALID;
Data <= s_axilite_WDATA[M-1:0];
end
end
end
assign s_axilite_AWREADY = !WABusy;
assign s_axilite_WREADY = !WDBusy;
assign s_axilite_BVALID = WABusy && WDBusy;
assign s_axilite_BRESP = '0; // OK

// Answer all reads with '1
logic RValid = 0;
uwire clr_rd = rst || (RValid && s_axilite_RREADY);
always_ff @(posedge clk) begin
if(clr_rd) RValid <= 0;
else if(!RValid) RValid <= s_axilite_ARVALID;
end
assign s_axilite_ARREADY = !RValid;
assign s_axilite_RVALID = RValid;
assign s_axilite_RDATA = '1;
assign s_axilite_RRESP = '0; // OK

end : blkAxiLite

//- IO-Sandwich with two-stage output buffer for containing a local enable
uwire en;
uwire [O_BITS-1:0] odat;
uwire ovld;
if(1) begin : blkOutputDecouple
typedef struct {
logic vld;
logic [O_BITS-1:0] dat;
} buf_t;
buf_t Buf[2] = '{ default: '{ vld: 0, dat: 'x } };
always_ff @(posedge clk) begin
if(rst) Buf <= '{ default: '{ vld: 0, dat: 'x } };
else begin
if(!Buf[1].vld || m_axis_tready) begin
Buf[1] <= '{
vld: Buf[0].vld || ovld,
dat: Buf[0].vld? Buf[0].dat : odat
};
end
Buf[0].vld <= Buf[1].vld && !m_axis_tready && (Buf[0].vld || ovld);
if(!Buf[0].vld) Buf[0].dat <= odat;
end
end
assign en = !Buf[0].vld;

assign m_axis_tvalid = Buf[1].vld;
assign m_axis_tdata = Buf[1].dat;

end : blkOutputDecouple

localparam int unsigned C_BITS = C < 2? 1 : $clog2(C);
uwire ivld = s_axis_tvalid;
uwire [C_BITS-1:0] icnl;
uwire [M -1:0] idat = s_axis_tdata[M-1:0];
assign s_axis_tready = en;
if(C == 1) assign icnl = 'x;
else begin
logic [C_BITS-1:0] Chnl = 0;
logic Last = 0;
uwire inc = ivld && en;
uwire clr = rst || (Last && inc);
always_ff @(posedge clk) begin
if(clr) begin
Chnl <= 0;
Last <= 0;
end
else if(inc) begin
Chnl <= Chnl + 1;
Last <= (~Chnl & (C-2)) == 0;
end
end
assign icnl = Chnl;
end

// Core Thresholding Module
$MODULE_NAME$ #(.N(N), .M(M), .C(C), .BIAS(BIAS), .O_BITS(O_BITS), .C_BITS(C_BITS)) core (
.clk, .rst,
.twe, .twa, .twd,
.en,
.ivld, .icnl, .idat,
.ovld, .ocnl(), .odat
);

endmodule : $MODULE_NAME_AXI$
Loading