/
r5p_mouse_soc_top.sv
324 lines (282 loc) · 10.3 KB
/
r5p_mouse_soc_top.sv
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
////////////////////////////////////////////////////////////////////////////////
// R5P: Mouse SoC
////////////////////////////////////////////////////////////////////////////////
// Copyright 2022 Iztok Jeras
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////////
module r5p_mouse_soc_top #(
/////////////////////////////////////////////////////////////////////////////
// SoC peripherals
/////////////////////////////////////////////////////////////////////////////
bit ENA_GPIO = 1'b1,
bit ENA_UART = 1'b0,
// GPIO
int unsigned GW = 32,
/////////////////////////////////////////////////////////////////////////////
// interconnect/memories
/////////////////////////////////////////////////////////////////////////////
// TCB bus
int unsigned AW = 14, // TCB address width (byte address)
int unsigned DW = 32, // TCB data width
// memory size (in bytes) and initialization file name
int unsigned IMS = (DW/8)*(2**AW),
string IFN = "mem_cpu.vmem",
/////////////////////////////////////////////////////////////////////////////
// implementation device (ASIC/FPGA vendor/device)
/////////////////////////////////////////////////////////////////////////////
string CHIP = ""
)(
// system signals
input logic clk, // clock
input logic rst, // reset (active low)
// GPIO
output logic [GW-1:0] gpio_o, // output
output logic [GW-1:0] gpio_e, // enable
input logic [GW-1:0] gpio_i, // input
// UART
output logic uart_txd,
input logic uart_rxd
);
///////////////////////////////////////////////////////////////////////////////
// local parameters and checks
////////////////////////////////////////////////////////////////////////////////
// in this SoC the data address space is split in half between memory and peripherals
localparam int unsigned RAW = AW-1;
// TODO: check if instruction address bus width and instruction memory size fit
// TODO: check if data address bus width and data memory size fit
///////////////////////////////////////////////////////////////////////////////
// local signals
////////////////////////////////////////////////////////////////////////////////
// system busses
tcb_if #(.AW (AW), .DW (DW)) bus_cpu (.clk (clk), .rst (rst));
tcb_if #(.AW (AW), .DW (DW)) bus_mem [3-1:0] (.clk (clk), .rst (rst));
////////////////////////////////////////////////////////////////////////////////
// R5P Mouse core instance
////////////////////////////////////////////////////////////////////////////////
r5p_mouse #(
.RST_ADR (32'h0000_0000),
.GPR_ADR (32'h0000_0000)
) cpu (
// system signals
.clk (clk),
.rst (rst),
// TCL system bus (shared by instruction/load/store)
.bus_vld (bus_cpu.vld),
.bus_wen (bus_cpu.wen),
.bus_adr (bus_cpu.adr),
.bus_ben (bus_cpu.ben),
.bus_wdt (bus_cpu.wdt),
.bus_rdt (bus_cpu.rdt),
.bus_err (bus_cpu.err),
.bus_rdy (bus_cpu.rdy)
);
////////////////////////////////////////////////////////////////////////////////
// load/store bus decoder
////////////////////////////////////////////////////////////////////////////////
tcb_dec #(
.AW (AW),
.DW (DW),
.PN (3), // port number
.AS ({ {1'b1, 14'bxx_xxxx_x1xx_xxxx} , // 0x20_0000 ~ 0x2f_ffff - 0x40 ~ 0x7f - UART controller
{1'b1, 14'bxx_xxxx_x0xx_xxxx} , // 0x20_0000 ~ 0x2f_ffff - 0x00 ~ 0x3f - GPIO controller
{1'b0, 14'bxx_xxxx_xxxx_xxxx} }) // 0x00_0000 ~ 0x1f_ffff - data memory
) ls_dec (
.sub (bus_cpu ),
.man (bus_mem[2:0])
);
////////////////////////////////////////////////////////////////////////////////
// memory instances
////////////////////////////////////////////////////////////////////////////////
generate
if (CHIP == "ARTIX_XPM") begin: gen_artix_xpm
// xpm_memory_spram: Single Port RAM
// Xilinx Parameterized Macro, version 2021.2
xpm_memory_spram #(
.ADDR_WIDTH_A (RAW-$clog2(4)), // DECIMAL
.AUTO_SLEEP_TIME (0), // DECIMAL
.BYTE_WRITE_WIDTH_A (8), // DECIMAL
.CASCADE_HEIGHT (0), // DECIMAL
.ECC_MODE ("no_ecc"), // String
.MEMORY_INIT_FILE ("none"), // String
.MEMORY_INIT_PARAM ("0"), // String
.MEMORY_OPTIMIZATION ("true"), // String
.MEMORY_PRIMITIVE ("auto"), // String
.MEMORY_SIZE (8 * 2**RAW), // DECIMAL
.MESSAGE_CONTROL (0), // DECIMAL
.READ_DATA_WIDTH_A (DW), // DECIMAL
.READ_LATENCY_A (1), // DECIMAL
.READ_RESET_VALUE_A ("0"), // String
.RST_MODE_A ("SYNC"), // String
.SIM_ASSERT_CHK (0), // DECIMAL; 0=disable simulation messages, 1=enable simulation messages
.USE_MEM_INIT (1), // DECIMAL
.USE_MEM_INIT_MMI (0), // DECIMAL
.WAKEUP_TIME ("disable_sleep"), // String
.WRITE_DATA_WIDTH_A (DW), // DECIMAL
.WRITE_MODE_A ("read_first"), // String
.WRITE_PROTECT (1) // DECIMAL
) mem (
// unused control/status signals
.injectdbiterra (1'b0),
.injectsbiterra (1'b0),
.dbiterra (),
.sbiterra (),
.sleep (1'b0),
.regcea (1'b1),
// system bus
.clka (bus_mem[0].clk),
.rsta (bus_mem[0].rst),
.ena (bus_mem[0].vld),
.wea (bus_mem[0].ben & {4{bus_mem[0].wen}}),
.addra (bus_mem[0].adr[RAW-1:$clog2(4)]),
.dina (bus_mem[0].wdt),
.douta (bus_mem[0].rdt)
);
assign bus_mem[0].err = 1'b0;
assign bus_mem[0].rdy = 1'b1;
end: gen_artix_xpm
else if (CHIP == "ARTIX_GEN") begin: gen_artix_gen
blk_mem_gen_0 mem (
.clka (bus_mem[0].clk),
.ena (bus_mem[0].vld),
.wea (bus_mem[0].ben & {4{bus_mem[0].wen}}),
.addra (bus_mem[0].adr[RAW-1:$clog2(4)]),
.dina (bus_mem[0].wdt),
.douta (bus_mem[0].rdt)
);
assign bus_mem[0].err = 1'b0;
assign bus_mem[0].rdy = 1'b1;
end: gen_artix_gen
else if (CHIP == "CYCLONE_V") begin: gen_cyclone_v
ram32x4096 mem (
.clock (bus_mem[0].clk),
.wren (bus_mem[0].vld & bus_mem[0].wen),
.rden (bus_mem[0].vld & ~bus_mem[0].wen),
.address (bus_mem[0].adr[RAW-1:$clog2(4)]),
.byteena (bus_mem[0].ben),
.data (bus_mem[0].wdt),
.q (bus_mem[0].rdt)
);
assign bus_mem[0].err = 1'b0;
assign bus_mem[0].rdy = 1'b1;
end: gen_cyclone_v
else if (CHIP == "ECP5") begin: gen_ecp5
// file:///usr/local/diamond/3.12/docs/webhelp/eng/index.htm#page/Reference%20Guides/IPexpress%20Modules/pmi_ram_dp.htm#
// TODO: use a single port or a true dual port memory
pmi_ram_dp_be #(
.pmi_wr_addr_depth (8 * 2**RAW),
.pmi_wr_addr_width (RAW-$clog2(4)),
.pmi_wr_data_width (32),
.pmi_rd_addr_depth (8 * 2**RAW),
.pmi_rd_addr_width (RAW-$clog2(4)),
.pmi_rd_data_width (32),
.pmi_regmode ("reg"),
.pmi_gsr ("disable"),
.pmi_resetmode ("sync"),
.pmi_optimization ("speed"),
.pmi_init_file ("none"),
.pmi_init_file_format ("binary"),
.pmi_byte_size (8),
.pmi_family ("ECP5")
) mem (
.WrClock (bus_mem[0].clk),
.WrClockEn (bus_mem[0].vld),
.WE (bus_mem[0].wen),
.WrAddress (bus_mem[0].adr[RAW-1:$clog2(4)]),
.ByteEn (bus_mem[0].ben),
.Data (bus_mem[0].wdt),
.RdClock (bus_mem[0].clk),
.RdClockEn (bus_mem[0].vld),
.Reset (bus_mem[0].rst),
.RdAddress (bus_mem[0].adr[RAW-1:$clog2(4)]),
.Q (bus_mem[0].rdt)
);
assign bus_mem[0].err = 1'b0;
assign bus_mem[0].rdy = 1'b1;
end: gen_ecp5
else begin: gen_default
// data memory
r5p_soc_mem #(
//.FN (),
.AW (RAW-1),
.DW (DW)
) mem (
.bus (bus_mem[0])
);
end: gen_default
endgenerate
////////////////////////////////////////////////////////////////////////////////
// GPIO
////////////////////////////////////////////////////////////////////////////////
generate
if (ENA_GPIO) begin: gen_gpio
// GPIO controller
tcb_gpio #(
.GW (GW),
.CFG_RSP_MIN (1'b1),
.CHIP (CHIP)
) gpio (
// GPIO signals
.gpio_o (gpio_o),
.gpio_e (gpio_e),
.gpio_i (gpio_i),
// bus interface
.bus (bus_mem[1])
);
end: gen_gpio
else begin: gen_gpio_err
// error response
tcb_err gpio_err (.bus (bus_mem[1]));
// GPIO signals
assign gpio_o = '0;
assign gpio_e = '0;
// gpio_i
end: gen_gpio_err
endgenerate
////////////////////////////////////////////////////////////////////////////////
// UART
////////////////////////////////////////////////////////////////////////////////
generate
if (ENA_UART) begin: gen_uart
// baudrate parameters (divider and counter width)
localparam int unsigned BDR = 50_000_000 / 115_200; // 50MHz / 115200 = 434.0
localparam int unsigned BCW = $clog2(BDR); // a 9-bit counter is required
// UART controller
tcb_uart #(
// UART parameters
.CW (BCW),
// configuration register parameters (write enable, reset value)
.CFG_TX_BDR_WEN (1'b0), .CFG_TX_BDR_RST (BCW'(BDR)),
.CFG_TX_IRQ_WEN (1'b0), .CFG_TX_IRQ_RST ('x),
.CFG_RX_BDR_WEN (1'b0), .CFG_RX_BDR_RST (BCW'(BDR)),
.CFG_RX_SMP_WEN (1'b0), .CFG_RX_SMP_RST (BCW'(BDR/2)),
.CFG_RX_IRQ_WEN (1'b0), .CFG_RX_IRQ_RST ('x),
// TCB parameters
.CFG_RSP_MIN (1'b1)
) uart (
// UART signals
.uart_txd (uart_txd),
.uart_rxd (uart_rxd),
// system bus interface
.bus (bus_mem[2])
);
end: gen_uart
else begin: gen_uart_err
// error response
tcb_err uart_err (.bus (bus_mem[2]));
// GPIO signals
assign uart_txd = 1'b1;
// uart_rxd
end: gen_uart_err
endgenerate
endmodule: r5p_mouse_soc_top