33// SPDX-License-Identifier: Apache-2.0
44
55module gpio # (
6- GpiWidth = 8 ,
7- GpoWidth = 16
6+ parameter int unsigned GpiWidth = 8 ,
7+ parameter int unsigned GpoWidth = 16 ,
8+ parameter int unsigned AddrWidth = 32 ,
9+ parameter int unsigned DataWidth = 32 ,
10+ parameter int unsigned RegAddr = 12
811) (
9- input logic clk_i,
10- input logic rst_ni,
12+ input logic clk_i,
13+ input logic rst_ni,
1114
12- input logic device_req_i,
13- input logic [31 : 0 ] device_addr_i,
14- input logic device_we_i,
15- input logic [ 3 : 0 ] device_be_i,
16- input logic [31 : 0 ] device_wdata_i,
17- output logic device_rvalid_o,
18- output logic [31 : 0 ] device_rdata_o,
15+ input logic device_req_i,
16+ input logic [AddrWidth - 1 : 0 ] device_addr_i,
17+ input logic device_we_i,
18+ input logic [3 : 0 ] device_be_i,
19+ input logic [DataWidth - 1 : 0 ] device_wdata_i,
20+ output logic device_rvalid_o,
21+ output logic [DataWidth - 1 : 0 ] device_rdata_o,
1922
2023 input logic [GpiWidth- 1 : 0 ] gp_i,
2124 output logic [GpoWidth- 1 : 0 ] gp_o
@@ -25,17 +28,17 @@ module gpio #(
2528 localparam int unsigned GPIO_IN_REG = 32'h4 ;
2629 localparam int unsigned GPIO_IN_DBNC_REG = 32'h8 ;
2730
28- logic [11 : 0 ] reg_addr;
31+ logic [RegAddr - 1 : 0 ] reg_addr;
2932
3033 logic [2 : 0 ][GpiWidth- 1 : 0 ] gp_i_q;
3134 logic [GpiWidth- 1 : 0 ] gp_i_dbnc;
3235 logic [GpoWidth- 1 : 0 ] gp_o_d;
3336
34- logic gp_o_wr_en;
35- logic gp_i_rd_en_d, gp_i_rd_en_q;
36- logic gp_i_dbnc_rd_en_d, gp_i_dbnc_rd_en_q;
37+ logic gp_o_wr_en;
38+ logic gp_i_rd_en_d, gp_i_rd_en_q;
39+ logic gp_i_dbnc_rd_en_d, gp_i_dbnc_rd_en_q;
3740
38- // instantiate debouncers for all GP inputs
41+ // Instantiate debouncers for all GP inputs.
3942 for (genvar i = 0 ; i < GpiWidth; i++ ) begin : gen_debounce
4043 debounce # (
4144 .ClkCount (500 )
@@ -65,36 +68,41 @@ module gpio #(
6568 end
6669 end
6770
68- // assign gp_o_d regarding to device_be_i and GpoWidth
71+ logic [3 : 0 ] unused_device_be;
72+
73+ // Assign gp_o_d regarding to device_be_i and GpoWidth.
6974 for (genvar i_byte = 0 ; i_byte < 4 ; ++ i_byte) begin : gen_gp_o_d ;
7075 if (i_byte * 8 < GpoWidth) begin : gen_gp_o_d_inner
7176 localparam int gpo_byte_end = (i_byte + 1 ) * 8 <= GpoWidth ? (i_byte + 1 ) * 8 : GpoWidth;
7277 assign gp_o_d[gpo_byte_end - 1 : i_byte * 8 ] =
7378 device_be_i[i_byte] ? device_wdata_i[gpo_byte_end - 1 : i_byte * 8 ] :
7479 gp_o[gpo_byte_end - 1 : i_byte * 8 ];
80+ assign unused_device_be[i_byte] = 0 ;
81+ end else begin : gen_unused_device_be
82+ assign unused_device_be[i_byte] = device_be_i[i_byte];
7583 end
7684 end
7785
78- // decode write and read requests
79- assign reg_addr = device_addr_i[11 : 0 ];
80- assign gp_o_wr_en = device_req_i & device_we_i & (reg_addr == GPIO_OUT_REG [11 : 0 ]);
81- assign gp_i_rd_en_d = device_req_i & ~ device_we_i & (reg_addr == GPIO_IN_REG [11 : 0 ]);
82- assign gp_i_dbnc_rd_en_d = device_req_i & ~ device_we_i & (reg_addr == GPIO_IN_DBNC_REG [11 : 0 ]);
86+ // Decode write and read requests.
87+ assign reg_addr = device_addr_i[RegAddr - 1 : 0 ];
88+ assign gp_o_wr_en = device_req_i & device_we_i & (reg_addr == GPIO_OUT_REG [RegAddr - 1 : 0 ]);
89+ assign gp_i_rd_en_d = device_req_i & ~ device_we_i & (reg_addr == GPIO_IN_REG [RegAddr - 1 : 0 ]);
90+ assign gp_i_dbnc_rd_en_d = device_req_i & ~ device_we_i & (reg_addr == GPIO_IN_DBNC_REG [RegAddr - 1 : 0 ]);
8391
84- // assign device_rdata_o according to request type
92+ // Assign device_rdata_o according to request type.
8593 always_comb begin
8694 if (gp_i_dbnc_rd_en_q)
87- device_rdata_o = {{ (32 - GpiWidth){ 1'b0 }} , gp_i_dbnc} ;
95+ device_rdata_o = {{ (DataWidth - GpiWidth){ 1'b0 }} , gp_i_dbnc} ;
8896 else if (gp_i_rd_en_q)
89- device_rdata_o = {{ (32 - GpiWidth){ 1'b0 }} , gp_i_q[2 ]} ;
97+ device_rdata_o = {{ (DataWidth - GpiWidth){ 1'b0 }} , gp_i_q[2 ]} ;
9098 else
91- device_rdata_o = {{ (32 - GpoWidth){ 1'b0 }} , gp_o} ;
99+ device_rdata_o = {{ (DataWidth - GpoWidth){ 1'b0 }} , gp_o} ;
92100 end
93101
94- logic unused_device_addr, unused_device_be, unused_device_wdata;
102+ // Unused signals.
103+ logic [AddrWidth- 1 - RegAddr: 0 ] unused_device_addr;
104+ logic [DataWidth- 1 - GpoWidth: 0 ] unused_device_wdata;
95105
96- assign unused_device_addr = ^ device_addr_i[31 : 10 ];
97- // TODO: Do this more neatly
98- assign unused_device_be = ^ device_be_i;
99- assign unused_device_wdata = ^ device_wdata_i[31 : GpoWidth];
106+ assign unused_device_addr = device_addr_i[AddrWidth- 1 : RegAddr];
107+ assign unused_device_wdata = device_wdata_i[DataWidth- 1 : GpoWidth];
100108endmodule
0 commit comments