blob: b33e0324fe1a30327fe4115c985100b60754d9eb [file] [log] [blame]
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +02001// SPDX-FileCopyrightText: 2020 Efabless Corporation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// SPDX-License-Identifier: Apache-2.0
15
16`default_nettype none
17/*
18 *-------------------------------------------------------------
19 *
20 * user_proj_example
21 *
22 * This is an example of a (trivially simple) user project,
23 * showing how the user project can connect to the logic
24 * analyzer, the wishbone bus, and the I/O pads.
25 *
26 * This project generates an integer count, which is output
27 * on the user area GPIO pads (digital output only). The
28 * wishbone connection allows the project to be controlled
29 * (start and stop) from the management SoC program.
30 *
31 * See the testbenches in directory "mprj_counter" for the
32 * example programs that drive this user project. The three
33 * testbenches are "io_ports", "la_test1", and "la_test2".
34 *
35 *-------------------------------------------------------------
36 */
37
38module user_proj_example #(
39 parameter BITS = 32
40)(
41`ifdef USE_POWER_PINS
42 inout vdda1, // User area 1 3.3V supply
43 inout vdda2, // User area 2 3.3V supply
44 inout vssa1, // User area 1 analog ground
45 inout vssa2, // User area 2 analog ground
46 inout vccd1, // User area 1 1.8V supply
47 inout vccd2, // User area 2 1.8v supply
48 inout vssd1, // User area 1 digital ground
49 inout vssd2, // User area 2 digital ground
50`endif
51
52 // Wishbone Slave ports (WB MI A)
53 input wb_clk_i,
54 input wb_rst_i,
55 input wbs_stb_i,
56 input wbs_cyc_i,
57 input wbs_we_i,
58 input [3:0] wbs_sel_i,
59 input [31:0] wbs_dat_i,
60 input [31:0] wbs_adr_i,
61 output wbs_ack_o,
62 output [31:0] wbs_dat_o,
63
64 // Logic Analyzer Signals
65 input [127:0] la_data_in,
66 output [127:0] la_data_out,
Tim Edwardsc89cfac2021-04-23 15:16:09 -040067 input [127:0] la_oenb,
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +020068
69 // IOs
70 input [`MPRJ_IO_PADS-1:0] io_in,
71 output [`MPRJ_IO_PADS-1:0] io_out,
Tim Edwards694bfd32021-04-23 10:55:41 -040072 output [`MPRJ_IO_PADS-1:0] io_oeb,
73
74 // IRQ
75 output [2:0] irq
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +020076);
77 wire clk;
78 wire rst;
79
80 wire [`MPRJ_IO_PADS-1:0] io_in;
81 wire [`MPRJ_IO_PADS-1:0] io_out;
82 wire [`MPRJ_IO_PADS-1:0] io_oeb;
83
84 wire [31:0] rdata;
85 wire [31:0] wdata;
86 wire [BITS-1:0] count;
87
88 wire valid;
89 wire [3:0] wstrb;
90 wire [31:0] la_write;
91
92 // WB MI A
93 assign valid = wbs_cyc_i && wbs_stb_i;
94 assign wstrb = wbs_sel_i & {4{wbs_we_i}};
95 assign wbs_dat_o = rdata;
96 assign wdata = wbs_dat_i;
97
98 // IO
99 assign io_out = count;
100 assign io_oeb = {(`MPRJ_IO_PADS-1){rst}};
101
Tim Edwards694bfd32021-04-23 10:55:41 -0400102 // IRQ
manarabdelaty401a14d2021-04-23 17:20:56 +0200103 assign irq = 3'b000; // Unused
Tim Edwards694bfd32021-04-23 10:55:41 -0400104
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +0200105 // LA
106 assign la_data_out = {{(127-BITS){1'b0}}, count};
107 // Assuming LA probes [63:32] are for controlling the count register
Tim Edwardsc89cfac2021-04-23 15:16:09 -0400108 assign la_write = ~la_oenb[63:32] & ~{BITS{valid}};
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +0200109 // Assuming LA probes [65:64] are for controlling the count clk & reset
Tim Edwardsc89cfac2021-04-23 15:16:09 -0400110 assign clk = (~la_oenb[64]) ? la_data_in[64]: wb_clk_i;
111 assign rst = (~la_oenb[65]) ? la_data_in[65]: wb_rst_i;
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +0200112
113 counter #(
114 .BITS(BITS)
115 ) counter(
116 .clk(clk),
117 .reset(rst),
118 .ready(wbs_ack_o),
119 .valid(valid),
120 .rdata(rdata),
121 .wdata(wbs_dat_i),
122 .wstrb(wstrb),
123 .la_write(la_write),
124 .la_input(la_data_in[63:32]),
125 .count(count)
126 );
127
128endmodule
129
130module counter #(
131 parameter BITS = 32
132)(
133 input clk,
134 input reset,
135 input valid,
136 input [3:0] wstrb,
137 input [BITS-1:0] wdata,
138 input [BITS-1:0] la_write,
139 input [BITS-1:0] la_input,
140 output ready,
141 output [BITS-1:0] rdata,
142 output [BITS-1:0] count
143);
144 reg ready;
145 reg [BITS-1:0] count;
146 reg [BITS-1:0] rdata;
147
148 always @(posedge clk) begin
149 if (reset) begin
150 count <= 0;
151 ready <= 0;
152 end else begin
153 ready <= 1'b0;
154 if (~|la_write) begin
155 count <= count + 1;
156 end
157 if (valid && !ready) begin
158 ready <= 1'b1;
159 rdata <= count;
160 if (wstrb[0]) count[7:0] <= wdata[7:0];
161 if (wstrb[1]) count[15:8] <= wdata[15:8];
162 if (wstrb[2]) count[23:16] <= wdata[23:16];
163 if (wstrb[3]) count[31:24] <= wdata[31:24];
manarabdelatya63e2e62021-04-08 20:32:40 +0200164 end else if (|la_write) begin
165 count <= la_write & la_input;
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +0200166 end
167 end
168 end
169
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +0200170endmodule
171`default_nettype wire