blob: 3445aa06a6d74493ba3e51857515b41ce4493322 [file] [log] [blame]
Matt Venn08cd6eb2020-11-16 12:01:14 +01001`default_nettype none
Tim Edwards05537512020-10-06 14:59:26 -04002/*
3 *-------------------------------------------------------------
4 *
5 * user_proj_example
6 *
7 * This is an example of a (trivially simple) user project,
8 * showing how the user project can connect to the logic
9 * analyzer, the wishbone bus, and the I/O pads.
10 *
11 * This project generates an integer count, which is output
12 * on the user area GPIO pads (digital output only). The
13 * wishbone connection allows the project to be controlled
14 * (start and stop) from the management SoC program.
15 *
16 * See the testbenches in directory "mprj_counter" for the
17 * example programs that drive this user project. The three
18 * testbenches are "io_ports", "la_test1", and "la_test2".
19 *
20 *-------------------------------------------------------------
21 */
22
23module user_proj_example #(
shalan0d14e6e2020-08-31 16:50:48 +020024 parameter BITS = 32
25)(
Ahmed Ghazydf4dd882020-11-25 18:38:42 +020026`ifdef USE_POWER_PINS
Tim Edwards9eda80d2020-10-08 21:36:44 -040027 inout vdda1, // User area 1 3.3V supply
28 inout vdda2, // User area 2 3.3V supply
29 inout vssa1, // User area 1 analog ground
30 inout vssa2, // User area 2 analog ground
31 inout vccd1, // User area 1 1.8V supply
32 inout vccd2, // User area 2 1.8v supply
33 inout vssd1, // User area 1 digital ground
34 inout vssd2, // User area 2 digital ground
Ahmed Ghazydf4dd882020-11-25 18:38:42 +020035`endif
Tim Edwards9eda80d2020-10-08 21:36:44 -040036
shalan0d14e6e2020-08-31 16:50:48 +020037 // Wishbone Slave ports (WB MI A)
38 input wb_clk_i,
39 input wb_rst_i,
Tim Edwards05537512020-10-06 14:59:26 -040040 input wbs_stb_i,
41 input wbs_cyc_i,
shalan0d14e6e2020-08-31 16:50:48 +020042 input wbs_we_i,
Tim Edwards05537512020-10-06 14:59:26 -040043 input [3:0] wbs_sel_i,
shalan0d14e6e2020-08-31 16:50:48 +020044 input [31:0] wbs_dat_i,
45 input [31:0] wbs_adr_i,
46 output wbs_ack_o,
Tim Edwards05537512020-10-06 14:59:26 -040047 output [31:0] wbs_dat_o,
48
shalan0d14e6e2020-08-31 16:50:48 +020049 // Logic Analyzer Signals
50 input [127:0] la_data_in,
51 output [127:0] la_data_out,
52 input [127:0] la_oen,
Tim Edwards05537512020-10-06 14:59:26 -040053
shalan0d14e6e2020-08-31 16:50:48 +020054 // IOs
Ahmed Ghazy22d29d62020-10-28 03:42:02 +020055 input [`MPRJ_IO_PADS-1:0] io_in,
56 output [`MPRJ_IO_PADS-1:0] io_out,
57 output [`MPRJ_IO_PADS-1:0] io_oeb
shalan0d14e6e2020-08-31 16:50:48 +020058);
59 wire clk;
60 wire rst;
61
Ahmed Ghazy22d29d62020-10-28 03:42:02 +020062 wire [`MPRJ_IO_PADS-1:0] io_in;
63 wire [`MPRJ_IO_PADS-1:0] io_out;
64 wire [`MPRJ_IO_PADS-1:0] io_oeb;
Tim Edwardsef2b68d2020-10-11 17:00:44 -040065
shalan0d14e6e2020-08-31 16:50:48 +020066 wire [31:0] rdata;
67 wire [31:0] wdata;
68 wire [BITS-1:0] count;
69
70 wire valid;
71 wire [3:0] wstrb;
72 wire [31:0] la_write;
73
74 // WB MI A
75 assign valid = wbs_cyc_i && wbs_stb_i;
76 assign wstrb = wbs_sel_i & {4{wbs_we_i}};
77 assign wbs_dat_o = rdata;
78 assign wdata = wbs_dat_i;
79
80 // IO
81 assign io_out = count;
Ahmed Ghazy22d29d62020-10-28 03:42:02 +020082 assign io_oeb = {(`MPRJ_IO_PADS-1){rst}};
shalan0d14e6e2020-08-31 16:50:48 +020083
84 // LA
85 assign la_data_out = {{(127-BITS){1'b0}}, count};
86 // Assuming LA probes [63:32] are for controlling the count register
87 assign la_write = ~la_oen[63:32] & ~{BITS{valid}};
88 // Assuming LA probes [65:64] are for controlling the count clk & reset
89 assign clk = (~la_oen[64]) ? la_data_in[64]: wb_clk_i;
90 assign rst = (~la_oen[65]) ? la_data_in[65]: wb_rst_i;
91
92 counter #(
93 .BITS(BITS)
94 ) counter(
95 .clk(clk),
96 .reset(rst),
Dan Rodriguesb9a8c912020-11-08 13:15:21 +110097 .ready(wbs_ack_o),
shalan0d14e6e2020-08-31 16:50:48 +020098 .valid(valid),
99 .rdata(rdata),
100 .wdata(wbs_dat_i),
101 .wstrb(wstrb),
102 .la_write(la_write),
103 .la_input(la_data_in[63:32]),
104 .count(count)
105 );
106
107endmodule
108
109module counter #(
110 parameter BITS = 32
111)(
112 input clk,
113 input reset,
114 input valid,
115 input [3:0] wstrb,
116 input [BITS-1:0] wdata,
117 input [BITS-1:0] la_write,
118 input [BITS-1:0] la_input,
119 output ready,
120 output [BITS-1:0] rdata,
121 output [BITS-1:0] count
122);
123 reg ready;
124 reg [BITS-1:0] count;
125 reg [BITS-1:0] rdata;
126
127 always @(posedge clk) begin
128 if (reset) begin
129 count <= 0;
130 ready <= 0;
131 end else begin
132 ready <= 1'b0;
133 if (~|la_write) begin
134 count <= count + 1;
135 end
136 if (valid && !ready) begin
137 ready <= 1'b1;
138 rdata <= count;
139 if (wstrb[0]) count[7:0] <= wdata[7:0];
140 if (wstrb[1]) count[15:8] <= wdata[15:8];
141 if (wstrb[2]) count[23:16] <= wdata[23:16];
142 if (wstrb[3]) count[31:24] <= wdata[31:24];
143 end
144 end
145 end
146
147 genvar i;
148 generate
149 for(i=0; i<BITS; i=i+1) begin
150 always @(posedge clk) begin
151 if (la_write[i]) count[i] <= la_input[i];
152 end
153 end
154 endgenerate
155
Tim Edwards05537512020-10-06 14:59:26 -0400156endmodule
Tim Edwards581068f2020-11-19 12:45:25 -0500157`default_nettype wire