blob: 55461305ff2f6eeebf2175a9cbf5c336029c8607 [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)(
Tim Edwards9eda80d2020-10-08 21:36:44 -040026 inout vdda1, // User area 1 3.3V supply
27 inout vdda2, // User area 2 3.3V supply
28 inout vssa1, // User area 1 analog ground
29 inout vssa2, // User area 2 analog ground
30 inout vccd1, // User area 1 1.8V supply
31 inout vccd2, // User area 2 1.8v supply
32 inout vssd1, // User area 1 digital ground
33 inout vssd2, // User area 2 digital ground
Tim Edwards9eda80d2020-10-08 21:36:44 -040034
shalan0d14e6e2020-08-31 16:50:48 +020035 // Wishbone Slave ports (WB MI A)
36 input wb_clk_i,
37 input wb_rst_i,
Tim Edwards05537512020-10-06 14:59:26 -040038 input wbs_stb_i,
39 input wbs_cyc_i,
shalan0d14e6e2020-08-31 16:50:48 +020040 input wbs_we_i,
Tim Edwards05537512020-10-06 14:59:26 -040041 input [3:0] wbs_sel_i,
shalan0d14e6e2020-08-31 16:50:48 +020042 input [31:0] wbs_dat_i,
43 input [31:0] wbs_adr_i,
44 output wbs_ack_o,
Tim Edwards05537512020-10-06 14:59:26 -040045 output [31:0] wbs_dat_o,
46
shalan0d14e6e2020-08-31 16:50:48 +020047 // Logic Analyzer Signals
48 input [127:0] la_data_in,
49 output [127:0] la_data_out,
50 input [127:0] la_oen,
Tim Edwards05537512020-10-06 14:59:26 -040051
shalan0d14e6e2020-08-31 16:50:48 +020052 // IOs
Ahmed Ghazy22d29d62020-10-28 03:42:02 +020053 input [`MPRJ_IO_PADS-1:0] io_in,
54 output [`MPRJ_IO_PADS-1:0] io_out,
55 output [`MPRJ_IO_PADS-1:0] io_oeb
shalan0d14e6e2020-08-31 16:50:48 +020056);
57 wire clk;
58 wire rst;
59
Ahmed Ghazy22d29d62020-10-28 03:42:02 +020060 wire [`MPRJ_IO_PADS-1:0] io_in;
61 wire [`MPRJ_IO_PADS-1:0] io_out;
62 wire [`MPRJ_IO_PADS-1:0] io_oeb;
Tim Edwardsef2b68d2020-10-11 17:00:44 -040063
shalan0d14e6e2020-08-31 16:50:48 +020064 wire [31:0] rdata;
65 wire [31:0] wdata;
66 wire [BITS-1:0] count;
67
68 wire valid;
69 wire [3:0] wstrb;
70 wire [31:0] la_write;
71
72 // WB MI A
73 assign valid = wbs_cyc_i && wbs_stb_i;
74 assign wstrb = wbs_sel_i & {4{wbs_we_i}};
75 assign wbs_dat_o = rdata;
76 assign wdata = wbs_dat_i;
77
78 // IO
79 assign io_out = count;
Ahmed Ghazy22d29d62020-10-28 03:42:02 +020080 assign io_oeb = {(`MPRJ_IO_PADS-1){rst}};
shalan0d14e6e2020-08-31 16:50:48 +020081
82 // LA
83 assign la_data_out = {{(127-BITS){1'b0}}, count};
84 // Assuming LA probes [63:32] are for controlling the count register
85 assign la_write = ~la_oen[63:32] & ~{BITS{valid}};
86 // Assuming LA probes [65:64] are for controlling the count clk & reset
87 assign clk = (~la_oen[64]) ? la_data_in[64]: wb_clk_i;
88 assign rst = (~la_oen[65]) ? la_data_in[65]: wb_rst_i;
89
90 counter #(
91 .BITS(BITS)
92 ) counter(
93 .clk(clk),
94 .reset(rst),
Dan Rodriguesb9a8c912020-11-08 13:15:21 +110095 .ready(wbs_ack_o),
shalan0d14e6e2020-08-31 16:50:48 +020096 .valid(valid),
97 .rdata(rdata),
98 .wdata(wbs_dat_i),
99 .wstrb(wstrb),
100 .la_write(la_write),
101 .la_input(la_data_in[63:32]),
102 .count(count)
103 );
104
105endmodule
106
107module counter #(
108 parameter BITS = 32
109)(
110 input clk,
111 input reset,
112 input valid,
113 input [3:0] wstrb,
114 input [BITS-1:0] wdata,
115 input [BITS-1:0] la_write,
116 input [BITS-1:0] la_input,
117 output ready,
118 output [BITS-1:0] rdata,
119 output [BITS-1:0] count
120);
121 reg ready;
122 reg [BITS-1:0] count;
123 reg [BITS-1:0] rdata;
124
125 always @(posedge clk) begin
126 if (reset) begin
127 count <= 0;
128 ready <= 0;
129 end else begin
130 ready <= 1'b0;
131 if (~|la_write) begin
132 count <= count + 1;
133 end
134 if (valid && !ready) begin
135 ready <= 1'b1;
136 rdata <= count;
137 if (wstrb[0]) count[7:0] <= wdata[7:0];
138 if (wstrb[1]) count[15:8] <= wdata[15:8];
139 if (wstrb[2]) count[23:16] <= wdata[23:16];
140 if (wstrb[3]) count[31:24] <= wdata[31:24];
141 end
142 end
143 end
144
145 genvar i;
146 generate
147 for(i=0; i<BITS; i=i+1) begin
148 always @(posedge clk) begin
149 if (la_write[i]) count[i] <= la_input[i];
150 end
151 end
152 endgenerate
153
Tim Edwards05537512020-10-06 14:59:26 -0400154endmodule
Tim Edwards581068f2020-11-19 12:45:25 -0500155`default_nettype wire