forked from aolofsson/oh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio.v
243 lines (208 loc) · 8.11 KB
/
gpio.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//#############################################################################
//# Function: General Purpose Software Programmable IO #
//# (See README.md for complete documentation) #
//#############################################################################
//# Author: Andreas Olofsson #
//# License: MIT (see LICENSE file in this repository) #
//#############################################################################
`include "gpio_regmap.vh"
module gpio #( parameter integer N = 24, // number of gpio pins
parameter integer AW = 32, // architecture address width
parameter integer PW = 104 // packet width
)
(
input nreset, // asynchronous active low reset
input clk, // clock
input access_in, // register access
input [PW-1:0] packet_in, // data/address
output wait_out, // pushback from mesh
output access_out, // register access
output [PW-1:0] packet_out, // data/address
input wait_in, // pushback from mesh
output reg [N-1:0] gpio_out, // data to drive to IO pins
output reg [N-1:0] gpio_dir, // gpio direction(0=input)
input [N-1:0] gpio_in, // data from IO pins
output gpio_irq // OR of GPIO_ILAT register
);
//################################
//# wires/regs/ params
//################################
//registers
reg [N-1:0] gpio_imask;
reg [N-1:0] gpio_itype;
reg [N-1:0] gpio_ipol;
reg [N-1:0] gpio_ilat;
reg [N-1:0] read_data;
wire [N-1:0] gpio_in_sync;
//wires
reg [N-1:0] data_old; //shadow
wire [N-1:0] ilat_clr;
wire [N-1:0] reg_wdata;
wire [N-1:0] out_dmux;
wire [N-1:0] rising_edge;
wire [N-1:0] falling_edge;
wire [N-1:0] irq_event;
wire reg_write;
wire reg_read;
wire reg_double;
wire dir_write;
wire imask_write;
wire itype_write;
wire ipol_write;
wire ilatclr_write;
wire out_write;
wire outset_write;
wire outclr_write;
wire outxor_write;
wire outreg_write;
/*AUTOWIRE*/
// Beginning of automatic wires (for undeclared instantiated-module outputs)
wire [4:0] ctrlmode_in; // From p2e of packet2emesh.v
wire [AW-1:0] data_in; // From p2e of packet2emesh.v
wire [1:0] datamode_in; // From p2e of packet2emesh.v
wire [AW-1:0] dstaddr_in; // From p2e of packet2emesh.v
wire [AW-1:0] srcaddr_in; // From p2e of packet2emesh.v
wire write_in; // From p2e of packet2emesh.v
// End of automatics
//################################
//# DECODE LOGIC
//################################
packet2emesh #(.AW(AW),
.PW(PW))
p2e(
/*AUTOINST*/
// Outputs
.write_in (write_in),
.datamode_in (datamode_in[1:0]),
.ctrlmode_in (ctrlmode_in[4:0]),
.dstaddr_in (dstaddr_in[AW-1:0]),
.srcaddr_in (srcaddr_in[AW-1:0]),
.data_in (data_in[AW-1:0]),
// Inputs
.packet_in (packet_in[PW-1:0]));
assign reg_write = access_in & write_in;
assign reg_read = access_in & ~write_in;
assign reg_double = datamode_in[1:0]==2'b11;
assign reg_wdata[N-1:0] = data_in[N-1:0];
assign dir_write = reg_write & (dstaddr_in[6:3]==`GPIO_DIR);
assign outreg_write = reg_write & (dstaddr_in[6:3]==`GPIO_OUT);
assign imask_write = reg_write & (dstaddr_in[6:3]==`GPIO_IMASK);
assign itype_write = reg_write & (dstaddr_in[6:3]==`GPIO_ITYPE);
assign ipol_write = reg_write & (dstaddr_in[6:3]==`GPIO_IPOL);
assign ilatclr_write = reg_write & (dstaddr_in[6:3]==`GPIO_ILATCLR);
assign outclr_write = reg_write & (dstaddr_in[6:3]==`GPIO_OUTCLR);
assign outset_write = reg_write & (dstaddr_in[6:3]==`GPIO_OUTSET);
assign outxor_write = reg_write & (dstaddr_in[6:3]==`GPIO_OUTXOR);
assign out_write = outreg_write |
outclr_write |
outset_write |
outxor_write;
//################################
//# GPIO_DIR
//################################
//0=input
//1=output
always @ (posedge clk or negedge nreset)
if(!nreset)
gpio_dir[N-1:0] <= 'b0;
else if(dir_write)
gpio_dir[N-1:0] <= reg_wdata[N-1:0];
//################################
//# GPIO_IN
//################################
oh_dsync oh_dsync[N-1:0] (.dout (gpio_in_sync[N-1:0]),
.clk (clk),
.nreset (nreset),
.din (gpio_in[N-1:0]));
always @ (posedge clk)
data_old[N-1:0] <= gpio_in_sync[N-1:0];
//################################
//# GPIO_OUT
//################################
oh_mux4 #(.DW(N))
oh_mux4 (.out (out_dmux[N-1:0]),
// Inputs
.in0 (reg_wdata[N-1:0]), .sel0 (outreg_write),
.in1 (gpio_out[N-1:0] & ~reg_wdata[N-1:0]),.sel1 (outclr_write),
.in2 (gpio_out[N-1:0] | reg_wdata[N-1:0]), .sel2 (outset_write),
.in3 (gpio_out[N-1:0] ^ reg_wdata[N-1:0]), .sel3 (outxor_write));
always @ (posedge clk or negedge nreset)
if(!nreset)
gpio_out[N-1:0] <= 'b0;
else if(out_write)
gpio_out[N-1:0] <= out_dmux[N-1:0];
//################################
//# GPIO_IMASK
//################################
always @ (posedge clk or negedge nreset)
if(!nreset)
gpio_imask[N-1:0] <= {(N){1'b1}};
else if(imask_write)
gpio_imask[N-1:0] <= reg_wdata[N-1:0];
//################################
//# GPIO_ITYPE
//################################
always @ (posedge clk)
if(itype_write)
gpio_itype[N-1:0] <= reg_wdata[N-1:0];
//################################
//# GPIO_IPOL
//################################
always @ (posedge clk)
if(ipol_write)
gpio_ipol[N-1:0] <= reg_wdata[N-1:0];
//################################
//# INTERRUPT LOGIC (DEFAULT EDGE)
//################################
assign rising_edge[N-1:0] = gpio_in_sync[N-1:0] & ~data_old[N-1:0];
assign falling_edge[N-1:0] = ~gpio_in_sync[N-1:0] & data_old[N-1:0];
assign irq_event[N-1:0] = (rising_edge[N-1:0] & ~gpio_itype[N-1:0] & gpio_ipol[N-1:0]) |
(falling_edge[N-1:0] & ~gpio_itype[N-1:0] & ~gpio_ipol[N-1:0]) |
(gpio_in_sync[N-1:0] & gpio_itype[N-1:0] & gpio_ipol[N-1:0]) |
(~gpio_in_sync[N-1:0] & gpio_itype[N-1:0] & ~gpio_ipol[N-1:0]);
//################################
//# ILAT
//################################
assign ilat_clr[N-1:0] = ilatclr_write ? reg_wdata[N-1:0] : 'b0;
always @ (posedge clk or negedge nreset)
if(!nreset)
gpio_ilat[N-1:0] <= 'b0;
else
gpio_ilat[N-1:0] <= (gpio_ilat[N-1:0] & ~ilat_clr[N-1:0]) | //old values
(irq_event[N-1:0] & ~gpio_imask[N-1:0]); //new interrupts
//################################
//# ONE CYCLE IRQ PULSE
//################################
assign gpio_irq = |gpio_ilat[N-1:0];
//################################
//# READBACK
//################################
always @ (posedge clk)
if(reg_read)
case(dstaddr_in[6:3])
`GPIO_IN : read_data[N-1:0] <= gpio_in_sync[N-1:0];
`GPIO_ILAT : read_data[N-1:0] <= gpio_ilat[N-1:0];
`GPIO_DIR : read_data[N-1:0] <= gpio_dir[N-1:0];
`GPIO_IMASK: read_data[N-1:0] <= gpio_imask[N-1:0];
`GPIO_IPOL : read_data[N-1:0] <= gpio_ipol[N-1:0];
`GPIO_ITYPE: read_data[N-1:0] <= gpio_itype[N-1:0];
default : read_data[N-1:0] <='b0;
endcase // case (dstaddr_in[7:3])
emesh_readback #(.AW(AW),
.PW(PW))
emesh_readback (/*AUTOINST*/
// Outputs
.wait_out (wait_out),
.access_out (access_out),
.packet_out (packet_out[PW-1:0]),
// Inputs
.nreset (nreset),
.clk (clk),
.access_in (access_in),
.packet_in (packet_in[PW-1:0]),
.read_data (read_data[63:0]),
.wait_in (wait_in));
endmodule // gpio
// Local Variables:
// verilog-library-directories:("." "../../emesh/hdl" "../../common/hdl")
// End: