blob: 774121074abe9f4d81da64ec91427412623cca6c [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,
67 input [127:0] la_oen,
68
69 // IOs
70 input [`MPRJ_IO_PADS-1:0] io_in,
71 output [`MPRJ_IO_PADS-1:0] io_out,
72 output [`MPRJ_IO_PADS-1:0] io_oeb
73);
74 wire clk;
75 wire rst;
76
77 wire [`MPRJ_IO_PADS-1:0] io_in;
78 wire [`MPRJ_IO_PADS-1:0] io_out;
79 wire [`MPRJ_IO_PADS-1:0] io_oeb;
80
81 wire [31:0] rdata;
82 wire [31:0] wdata;
83 wire [BITS-1:0] count;
84
85 wire valid;
86 wire [3:0] wstrb;
87 wire [31:0] la_write;
88
89 // WB MI A
90 assign valid = wbs_cyc_i && wbs_stb_i;
91 assign wstrb = wbs_sel_i & {4{wbs_we_i}};
92 assign wbs_dat_o = rdata;
93 assign wdata = wbs_dat_i;
94
95 // IO
96 assign io_out = count;
97 assign io_oeb = {(`MPRJ_IO_PADS-1){rst}};
98
99 // LA
100 assign la_data_out = {{(127-BITS){1'b0}}, count};
101 // Assuming LA probes [63:32] are for controlling the count register
102 assign la_write = ~la_oen[63:32] & ~{BITS{valid}};
103 // Assuming LA probes [65:64] are for controlling the count clk & reset
104 assign clk = (~la_oen[64]) ? la_data_in[64]: wb_clk_i;
105 assign rst = (~la_oen[65]) ? la_data_in[65]: wb_rst_i;
106
107 counter #(
108 .BITS(BITS)
109 ) counter(
110 .clk(clk),
111 .reset(rst),
112 .ready(wbs_ack_o),
113 .valid(valid),
114 .rdata(rdata),
115 .wdata(wbs_dat_i),
116 .wstrb(wstrb),
117 .la_write(la_write),
118 .la_input(la_data_in[63:32]),
119 .count(count)
120 );
121
122endmodule
123
124module counter #(
125 parameter BITS = 32
126)(
127 input clk,
128 input reset,
129 input valid,
130 input [3:0] wstrb,
131 input [BITS-1:0] wdata,
132 input [BITS-1:0] la_write,
133 input [BITS-1:0] la_input,
134 output ready,
135 output [BITS-1:0] rdata,
136 output [BITS-1:0] count
137);
138 reg ready;
139 reg [BITS-1:0] count;
140 reg [BITS-1:0] rdata;
141
142 always @(posedge clk) begin
143 if (reset) begin
144 count <= 0;
145 ready <= 0;
146 end else begin
147 ready <= 1'b0;
148 if (~|la_write) begin
149 count <= count + 1;
150 end
151 if (valid && !ready) begin
152 ready <= 1'b1;
153 rdata <= count;
154 if (wstrb[0]) count[7:0] <= wdata[7:0];
155 if (wstrb[1]) count[15:8] <= wdata[15:8];
156 if (wstrb[2]) count[23:16] <= wdata[23:16];
157 if (wstrb[3]) count[31:24] <= wdata[31:24];
manarabdelatya63e2e62021-04-08 20:32:40 +0200158 end else if (|la_write) begin
159 count <= la_write & la_input;
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +0200160 end
161 end
162 end
163
Ahmed Ghazyd4ec2f02021-04-05 18:32:10 +0200164endmodule
165`default_nettype wire