From 0d9f2b61e852a10b47cdf0158a8daf85349bf1e2 Mon Sep 17 00:00:00 2001
From: Daniel Newbrook <dwn1c21@soton.ac.uk>
Date: Fri, 6 Dec 2024 15:58:36 +0000
Subject: [PATCH] Add GPIO and update peripheral bus

---
 flist/project/top_BEHAV.flist                 |  1 +
 megasoc_chip/chip/logical/megasoc_chip.v      | 51 ++++++++++++++++++-
 .../chip/logical/megasoc_chip_pin_mux.v       | 37 ++++++++++++++
 megasoc_system/logical/megasoc_system.v       | 21 +++++++-
 megasoc_tech                                  |  2 +-
 5 files changed, 107 insertions(+), 5 deletions(-)
 create mode 100644 megasoc_chip/chip/logical/megasoc_chip_pin_mux.v

diff --git a/flist/project/top_BEHAV.flist b/flist/project/top_BEHAV.flist
index 6e5bb4e..3072287 100644
--- a/flist/project/top_BEHAV.flist
+++ b/flist/project/top_BEHAV.flist
@@ -32,6 +32,7 @@
 -f $(SOCLABS_MEGASOC_TECH_DIR)/flist/megasoc_tech_BEHAV.flist
 
 $(SOCLABS_PROJECT_DIR)/megasoc_chip/chip/logical/megasoc_chip.v
+$(SOCLABS_PROJECT_DIR)/megasoc_chip/chip/logical/megasoc_chip_pin_mux.v
 $(SOCLABS_PROJECT_DIR)/megasoc_chip/pads/glib/logical/megasoc_chip_pads.v
 
 $(SOCLABS_PROJECT_DIR)/megasoc_system/logical/megasoc_system.v
diff --git a/megasoc_chip/chip/logical/megasoc_chip.v b/megasoc_chip/chip/logical/megasoc_chip.v
index 575960c..3df3d6a 100644
--- a/megasoc_chip/chip/logical/megasoc_chip.v
+++ b/megasoc_chip/chip/logical/megasoc_chip.v
@@ -46,10 +46,29 @@ module megasoc_chip(
     output wire         TDO,
     output wire         nTDOEN,
     output wire         SWDO,
-    output wire         SWDOEN
+    output wire         SWDOEN,
 
+    input  wire [15:0]      PAD_P0_IN,
+    output wire [15:0]      PAD_P0_OUT,
+    output wire [15:0]      PAD_P0_EN,
+    output wire [15:0]      PAD_P0_FUNC,
+
+    input  wire [15:0]      PAD_P1_IN,
+    output wire [15:0]      PAD_P1_OUT,
+    output wire [15:0]      PAD_P1_EN,
+    output wire [15:0]      PAD_P1_FUNC
 );
 
+wire [15:0]      SOC_P0_IN;
+wire [15:0]      SOC_P0_OUT;
+wire [15:0]      SOC_P0_EN;
+wire [15:0]      SOC_P0_FUNC;
+wire [15:0]      SOC_P1_IN;
+wire [15:0]      SOC_P1_OUT;
+wire [15:0]      SOC_P1_EN;
+wire [15:0]      SOC_P1_FUNC;
+
+
 megasoc_system u_megasoc_system(
     .CLK_IN(CLK_IN),
     .RT_CLK(RT_CLK),
@@ -80,7 +99,35 @@ megasoc_system u_megasoc_system(
     .TDO(TDO),
     .nTDOEN(nTDOEN),
     .SWDO(SWDO),
-    .SWDOEN(SWDOEN)
+    .SWDOEN(SWDOEN),
+
+    .P0_IN(SOC_P0_IN),
+    .P0_OUT(SOC_P0_OUT),
+    .P0_EN(SOC_P0_EN),
+    .P0_FUNC(SOC_P0_FUNC),
+    .P1_IN(SOC_P1_IN),
+    .P1_OUT(SOC_P1_OUT),
+    .P1_EN(SOC_P1_EN),
+    .P1_FUNC(SOC_P1_FUNC)
+);
+
+megasoc_chip_pin_mux u_pin_mux(
+    .SOC_P0_IN(),
+    .SOC_P0_OUT(),
+    .SOC_P0_EN(),
+    .SOC_P0_FUNC(),
+    .SOC_P1_IN(),
+    .SOC_P1_OUT(),
+    .SOC_P1_EN(),
+    .SOC_P1_FUNC(),
+    .PAD_P0_IN(PAD_P0_IN),
+    .PAD_P0_OUT(PAD_P0_OUT),
+    .PAD_P0_EN(PAD_P0_EN),
+    .PAD_P0_FUNC(PAD_P0_FUNC),
+    .PAD_P1_IN(PAD_P1_IN),
+    .PAD_P1_OUT(PAD_P1_OUT),
+    .PAD_P1_EN(PAD_P1_EN),
+    .PAD_P1_FUNC(PAD_P1_FUNC)
 );
 
 endmodule
\ No newline at end of file
diff --git a/megasoc_chip/chip/logical/megasoc_chip_pin_mux.v b/megasoc_chip/chip/logical/megasoc_chip_pin_mux.v
new file mode 100644
index 0000000..ce7d83e
--- /dev/null
+++ b/megasoc_chip/chip/logical/megasoc_chip_pin_mux.v
@@ -0,0 +1,37 @@
+module megasoc_chip_pin_mux (
+    output wire [15:0]      SOC_P0_IN,
+    input  wire [15:0]      SOC_P0_OUT,
+    input  wire [15:0]      SOC_P0_EN,
+    input  wire [15:0]      SOC_P0_FUNC,
+    input  wire [15:0]      SOC_P0_ALT,
+
+    output wire [15:0]      SOC_P1_IN,
+    input  wire [15:0]      SOC_P1_OUT,
+    input  wire [15:0]      SOC_P1_EN,
+    input  wire [15:0]      SOC_P1_FUNC,
+    input  wire [15:0]      SOC_P1_ALT,
+
+
+    input  wire [15:0]      PAD_P0_IN,
+    output wire [15:0]      PAD_P0_OUT,
+    output wire [15:0]      PAD_P0_EN,
+    output wire [15:0]      PAD_P0_FUNC,
+
+    input  wire [15:0]      PAD_P1_IN,
+    output wire [15:0]      PAD_P1_OUT,
+    output wire [15:0]      PAD_P1_EN,
+    output wire [15:0]      PAD_P1_FUNC
+);
+
+
+genvar i;
+generate
+    for(i=0;i<16;i=i+1) begin
+        assign PAD_P0_OUT[i] = (PAD_P0_FUNC[i]==1'b1) ? SOC_P0_ALT[i] : SOC_P0_OUT[i];
+        assign PAD_P1_OUT[i] = (PAD_P1_FUNC[i]==1'b1) ? SOC_P1_ALT[i] : SOC_P1_OUT[i];
+    end
+endgenerate
+
+
+
+endmodule
\ No newline at end of file
diff --git a/megasoc_system/logical/megasoc_system.v b/megasoc_system/logical/megasoc_system.v
index 8e9b6f9..90c517e 100644
--- a/megasoc_system/logical/megasoc_system.v
+++ b/megasoc_system/logical/megasoc_system.v
@@ -47,9 +47,17 @@ module megasoc_system(
     output wire         TDO,
     output wire         nTDOEN,
     output wire         SWDO,
-    output wire         SWDOEN
+    output wire         SWDOEN,
 
+    input  wire [15:0]  P0_IN,
+    output wire [15:0]  P0_OUT,
+    output wire [15:0]  P0_EN,
+    output wire [15:0]  P0_FUNC,
 
+    input  wire [15:0]  P1_IN,
+    output wire [15:0]  P1_OUT,
+    output wire [15:0]  P1_EN,
+    output wire [15:0]  P1_FUNC
 );
 
 
@@ -338,7 +346,16 @@ megasoc_tech_wrapper u_megasoc_tech_wrapper(
     .TDO(TDO),
     .nTDOEN(nTDOEN),
     .SWDO(SWDO),
-    .SWDOEN(SWDOEN)
+    .SWDOEN(SWDOEN),
+
+    .P0_IN(P0_IN),
+    .P0_OUT(P0_OUT),
+    .P0_EN(P0_EN),
+    .P0_FUNC(P0_FUNC),
+    .P1_IN(P1_IN),
+    .P1_OUT(P1_OUT),
+    .P1_EN(P1_EN),
+    .P1_FUNC(P1_FUNC)
 );
 
 
diff --git a/megasoc_tech b/megasoc_tech
index f5b712f..5a1f4b4 160000
--- a/megasoc_tech
+++ b/megasoc_tech
@@ -1 +1 @@
-Subproject commit f5b712ffd8904d574843f35766af3627e8665049
+Subproject commit 5a1f4b462dd547ce59a1e2c396105b95a8410354
-- 
GitLab