From ff348d47402a8dbb2cfcb3a96f740a61a0d79d14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20B=2E=20Preu=C3=9Fer?= <thomas.preusser@xilinx.com>
Date: Fri, 14 Oct 2022 06:41:57 +0100
Subject: [PATCH] Add sanity checking for generics.

---
 finn-rtllib/fmpadding/hdl/fmpadding.sv | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/finn-rtllib/fmpadding/hdl/fmpadding.sv b/finn-rtllib/fmpadding/hdl/fmpadding.sv
index b3e7f2b65..82ccba132 100644
--- a/finn-rtllib/fmpadding/hdl/fmpadding.sv
+++ b/finn-rtllib/fmpadding/hdl/fmpadding.sv
@@ -71,6 +71,46 @@ module fmpadding #(
 	uwire  clk = ap_clk;
 	uwire  rst = !ap_rst_n;
 
+	//-----------------------------------------------------------------------
+	// Parameter Sanity Checking
+	initial begin
+		automatic bit  fail = 0;
+
+		if(XCOUNTER_BITS < $clog2(1+INIT_XEND)) begin
+			$error("XCounter size too small to accommodate end count.");
+			fail = 1;
+		end
+		if(XCOUNTER_BITS < $clog2(1+INIT_XON)) begin
+			$error("XCounter size too small to accommodate ON count.");
+			fail = 1;
+		end
+		if(XCOUNTER_BITS < $clog2(1+INIT_XOFF)) begin
+			$error("XCounter size too small to accommodate OFF count.");
+			fail = 1;
+		end
+		if(YCOUNTER_BITS < $clog2(1+INIT_YEND)) begin
+			$error("YCounter size too small to accommodate end count.");
+			fail = 1;
+		end
+		if(YCOUNTER_BITS < $clog2(1+INIT_YON)) begin
+			$error("YCounter size too small to accommodate ON count.");
+			fail = 1;
+		end
+		if(YCOUNTER_BITS < $clog2(1+INIT_YOFF)) begin
+			$error("YCounter size too small to accommodate OFF count.");
+			fail = 1;
+		end
+
+		if((INIT_XEND < INIT_XON) || (INIT_XOFF <= INIT_XON)) begin
+			$warning("Initial empty X output range.");
+		end
+		if((INIT_YEND < INIT_YON) || (INIT_YOFF <= INIT_YON)) begin
+			$warning("Initial empty Y output range.");
+		end
+
+		if(fail)  $finish();
+	end
+
 	//-----------------------------------------------------------------------
 	// Dynamically configurable state
 	typedef logic [XCOUNTER_BITS-1:0]  xcount_t;
-- 
GitLab