From 983fb7ff88ab5f398073e2aa1d5e44eb03d01ee3 Mon Sep 17 00:00:00 2001
From: David Shah <dave@ds0.me>
Date: Wed, 10 Oct 2018 16:35:19 +0100
Subject: [PATCH] ecp5: First BRAM type maps successfully

Signed-off-by: David Shah <dave@ds0.me>
---
 techlibs/ecp5/.gitignore    |  1 +
 techlibs/ecp5/Makefile.inc  | 23 ++++++++++++++++--
 techlibs/ecp5/bram.txt      |  4 ++--
 techlibs/ecp5/bram_map.v    |  0
 techlibs/ecp5/brams_init.py |  4 ++--
 techlibs/ecp5/brams_map.v   | 47 +++++++++++++++++++++++++++++++++++++
 techlibs/ecp5/cells_sim.v   |  2 ++
 techlibs/ecp5/synth_ecp5.cc |  5 +---
 8 files changed, 76 insertions(+), 10 deletions(-)
 delete mode 100644 techlibs/ecp5/bram_map.v
 create mode 100644 techlibs/ecp5/brams_map.v

diff --git a/techlibs/ecp5/.gitignore b/techlibs/ecp5/.gitignore
index 3f66045f8..54c329735 100644
--- a/techlibs/ecp5/.gitignore
+++ b/techlibs/ecp5/.gitignore
@@ -6,3 +6,4 @@ bram_conn_2.vh
 bram_conn_4.vh
 bram_conn_9.vh
 bram_conn_18.vh
+brams_connect.mk
diff --git a/techlibs/ecp5/Makefile.inc b/techlibs/ecp5/Makefile.inc
index 95d40fccf..f45859392 100644
--- a/techlibs/ecp5/Makefile.inc
+++ b/techlibs/ecp5/Makefile.inc
@@ -5,19 +5,38 @@ $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/cells_map.v))
 $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/cells_sim.v))
 $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/drams_map.v))
 $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/dram.txt))
+$(eval $(call add_share_file,share/ecp5,techlibs/ecp5/brams_map.v))
 $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram.txt))
 $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/arith_map.v))
 
-EXTRA_OBJS += techlibs/ecp5/brams_init.mk
-.SECONDARY: techlibs/ecp5/brams_init.mk
+EXTRA_OBJS += techlibs/ecp5/brams_init.mk techlibs/ecp5/brams_connect.mk
+.SECONDARY: techlibs/ecp5/brams_init.mk techlibs/ecp5/brams_connect.mk
 
 techlibs/ecp5/brams_init.mk: techlibs/ecp5/brams_init.py
 	$(Q) mkdir -p techlibs/ecp5
 	$(P) python3 $<
 	$(Q) touch $@
 
+techlibs/ecp5/brams_connect.mk: techlibs/ecp5/brams_connect.py
+	$(Q) mkdir -p techlibs/ecp5
+	$(P) python3 $<
+	$(Q) touch $@
+
+
 techlibs/ecp5/bram_init_1_2_4.vh: techlibs/ecp5/brams_init.mk
 techlibs/ecp5/bram_init_9_18_36.vh: techlibs/ecp5/brams_init.mk
 
+techlibs/ecp5/bram_conn_1.vh: techlibs/ecp5/brams_connect.mk
+techlibs/ecp5/bram_conn_2.vh: techlibs/ecp5/brams_connect.mk
+techlibs/ecp5/bram_conn_4.vh: techlibs/ecp5/brams_connect.mk
+techlibs/ecp5/bram_conn_9.vh: techlibs/ecp5/brams_connect.mk
+techlibs/ecp5/bram_conn_18.vh: techlibs/ecp5/brams_connect.mk
+
 $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram_init_1_2_4.vh))
 $(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram_init_9_18_36.vh))
+
+$(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram_conn_1.vh))
+$(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram_conn_2.vh))
+$(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram_conn_4.vh))
+$(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram_conn_9.vh))
+$(eval $(call add_share_file,share/ecp5,techlibs/ecp5/bram_conn_18.vh))
diff --git a/techlibs/ecp5/bram.txt b/techlibs/ecp5/bram.txt
index 9d22e4afe..f223a42b8 100644
--- a/techlibs/ecp5/bram.txt
+++ b/techlibs/ecp5/bram.txt
@@ -18,8 +18,8 @@ bram $__ECP5_DP16KD
   enable 2 1 @a10d18
   enable 1 1 @a11d9 @a12d4 @a13d2 @a14d1
   transp 0 2
-  clocks 1 2
-  clkpol 2 2
+  clocks 2 3
+  clkpol 2 3
 endbram
 
 match $__ECP5_DP16KD
diff --git a/techlibs/ecp5/bram_map.v b/techlibs/ecp5/bram_map.v
deleted file mode 100644
index e69de29bb..000000000
diff --git a/techlibs/ecp5/brams_init.py b/techlibs/ecp5/brams_init.py
index 6f650b2a9..96a47bdcd 100755
--- a/techlibs/ecp5/brams_init.py
+++ b/techlibs/ecp5/brams_init.py
@@ -8,7 +8,7 @@ with open("techlibs/ecp5/bram_init_1_2_4.vh", "w") as f:
         init_snippets = list(reversed(init_snippets))
         for k in range(8, 64, 8):
             init_snippets[k] = "\n          " + init_snippets[k]
-        print(".INIT_%02X({%s})," % (i, ", ".join(init_snippets)), file=f)
+        print(".INITVAL_%02X({%s})," % (i, ", ".join(init_snippets)), file=f)
 
 with open("techlibs/ecp5/bram_init_9_18_36.vh", "w") as f:
     for i in range(0, 0x40):
@@ -19,4 +19,4 @@ with open("techlibs/ecp5/bram_init_9_18_36.vh", "w") as f:
         init_snippets = list(reversed(init_snippets))
         for k in range(8, 32, 8):
             init_snippets[k] = "\n          " + init_snippets[k]
-        print(".INIT_%02X({%s})," % (i, ", ".join(init_snippets)), file=f)
+        print(".INITVAL_%02X({%s})," % (i, ", ".join(init_snippets)), file=f)
diff --git a/techlibs/ecp5/brams_map.v b/techlibs/ecp5/brams_map.v
new file mode 100644
index 000000000..894f5c46f
--- /dev/null
+++ b/techlibs/ecp5/brams_map.v
@@ -0,0 +1,47 @@
+module \$__ECP5_DP16KD (CLK2, CLK3, A1ADDR, A1DATA, A1EN, B1ADDR, B1DATA, B1EN);
+	parameter CFG_ABITS = 10;
+	parameter CFG_DBITS = 18;
+	parameter CFG_ENABLE_A = 2;
+
+	parameter CLKPOL2 = 1;
+	parameter CLKPOL3 = 1;
+	parameter [18431:0] INIT = 18432'bx;
+	parameter TRANSP2 = 0;
+
+	input CLK2;
+	input CLK3;
+
+	input [CFG_ABITS-1:0] A1ADDR;
+	input [CFG_DBITS-1:0] A1DATA;
+	input [CFG_ENABLE_A-1:0] A1EN;
+
+	input [CFG_ABITS-1:0] B1ADDR;
+	output [CFG_DBITS-1:0] B1DATA;
+	input B1EN;
+
+	localparam CLKAMUX = CLKPOL2 ? "CLKA" : "INV";
+	localparam CLKBMUX = CLKPOL3 ? "CLKB" : "INV";
+
+	localparam WRITEMODE_A = TRANSP2 ? "WRITETHROUGH" : "NORMAL";
+
+
+	generate if (CFG_DBITS == 1) begin
+		DP16KD #(
+			`include "bram_init_1_2_4.vh"
+			.DATA_WIDTH_A(1),
+			.DATA_WIDTH_B(1),
+			.CLKAMUX(CLKAMUX),
+			.CLKBMUX(CLKBMUX),
+			.WRITEMODE_A(WRITEMODE_A),
+			.GSR("DISABLED")
+		) _TECHMAP_REPLACE_ (
+			`include "bram_conn_1.vh"
+			.CLKA(CLK2), .CLKB(CLK3),
+			.WEA(1'b1), .CEA(|A1EN), .OCEA(1'b1),
+			.WEB(1'b0), .CEB(B1EN), .OCEB(1'b1),
+			.RSTA(1'b0), .RSTB(1'b0)
+		);
+	end else begin
+		wire TECHMAP_FAIL = 1'b1;
+	end endgenerate
+endmodule
diff --git a/techlibs/ecp5/cells_sim.v b/techlibs/ecp5/cells_sim.v
index 4732381ca..e43632c64 100644
--- a/techlibs/ecp5/cells_sim.v
+++ b/techlibs/ecp5/cells_sim.v
@@ -475,6 +475,8 @@ module DP16KD(
   parameter WRITEMODE_A = "NORMAL";
   parameter WRITEMODE_B = "NORMAL";
 
+  parameter CLKAMUX = "CLKA";
+  parameter CLKBMUX = "CLKB";
 
   parameter GSR = "ENABLED";
 
diff --git a/techlibs/ecp5/synth_ecp5.cc b/techlibs/ecp5/synth_ecp5.cc
index a13dd8d41..ab56a9444 100644
--- a/techlibs/ecp5/synth_ecp5.cc
+++ b/techlibs/ecp5/synth_ecp5.cc
@@ -222,11 +222,8 @@ struct SynthEcp5Pass : public ScriptPass
 
 		if (!nobram && check_label("bram", "(skip if -nobram)"))
 		{
-			//TODO
-#if 0
-			run("memory_bram -rules +/ecp5/brams.txt");
+			run("memory_bram -rules +/ecp5/bram.txt");
 			run("techmap -map +/ecp5/brams_map.v");
-#endif
 		}
 
 		if (!nodram && check_label("dram", "(skip if -nodram)"))