blob: 4c2a6b99f7b1dc27d7d218557b6cab9d82edcf2f [file] [log] [blame]
Tim Edwards05537512020-10-06 14:59:26 -04001/*
2 *-------------------------------------------------------------
3 *
4 * user_proj_example
5 *
6 * This is an example of a (trivially simple) user project,
7 * showing how the user project can connect to the logic
8 * analyzer, the wishbone bus, and the I/O pads.
9 *
10 * This project generates an integer count, which is output
11 * on the user area GPIO pads (digital output only). The
12 * wishbone connection allows the project to be controlled
13 * (start and stop) from the management SoC program.
14 *
15 * See the testbenches in directory "mprj_counter" for the
16 * example programs that drive this user project. The three
17 * testbenches are "io_ports", "la_test1", and "la_test2".
18 *
19 *-------------------------------------------------------------
20 */
21
22module user_proj_example #(
Tim Edwards9eda80d2020-10-08 21:36:44 -040023 parameter IO_PADS = 37,
24 parameter PWR_PADS = 4,
shalan0d14e6e2020-08-31 16:50:48 +020025 parameter BITS = 32
26)(
Tim Edwards9eda80d2020-10-08 21:36:44 -040027`ifdef LVS
28 inout vdda1, // User area 1 3.3V supply
29 inout vdda2, // User area 2 3.3V supply
30 inout vssa1, // User area 1 analog ground
31 inout vssa2, // User area 2 analog ground
32 inout vccd1, // User area 1 1.8V supply
33 inout vccd2, // User area 2 1.8v supply
34 inout vssd1, // User area 1 digital ground
35 inout vssd2, // User area 2 digital ground
36`endif
37
shalan0d14e6e2020-08-31 16:50:48 +020038 // Wishbone Slave ports (WB MI A)
39 input wb_clk_i,
40 input wb_rst_i,
Tim Edwards05537512020-10-06 14:59:26 -040041 input wbs_stb_i,
42 input wbs_cyc_i,
shalan0d14e6e2020-08-31 16:50:48 +020043 input wbs_we_i,
Tim Edwards05537512020-10-06 14:59:26 -040044 input [3:0] wbs_sel_i,
shalan0d14e6e2020-08-31 16:50:48 +020045 input [31:0] wbs_dat_i,
46 input [31:0] wbs_adr_i,
47 output wbs_ack_o,
Tim Edwards05537512020-10-06 14:59:26 -040048 output [31:0] wbs_dat_o,
49
shalan0d14e6e2020-08-31 16:50:48 +020050 // Logic Analyzer Signals
51 input [127:0] la_data_in,
52 output [127:0] la_data_out,
53 input [127:0] la_oen,
Tim Edwards05537512020-10-06 14:59:26 -040054
shalan0d14e6e2020-08-31 16:50:48 +020055 // IOs
56 input [IO_PADS-1:0] io_in,
Tim Edwardsef2b68d2020-10-11 17:00:44 -040057 output [IO_PADS-1:0] io_out,
58 output [IO_PADS-1:0] io_oeb
shalan0d14e6e2020-08-31 16:50:48 +020059);
60 wire clk;
61 wire rst;
62
Tim Edwardsef2b68d2020-10-11 17:00:44 -040063 wire [IO_PADS-1:0] io_in;
64 wire [IO_PADS-1:0] io_out;
65 wire [IO_PADS-1:0] io_oeb;
66
shalan0d14e6e2020-08-31 16:50:48 +020067 wire [31:0] rdata;
68 wire [31:0] wdata;
69 wire [BITS-1:0] count;
70
71 wire valid;
72 wire [3:0] wstrb;
73 wire [31:0] la_write;
74
75 // WB MI A
76 assign valid = wbs_cyc_i && wbs_stb_i;
77 assign wstrb = wbs_sel_i & {4{wbs_we_i}};
78 assign wbs_dat_o = rdata;
79 assign wdata = wbs_dat_i;
80
81 // IO
82 assign io_out = count;
Tim Edwardsef2b68d2020-10-11 17:00:44 -040083 assign io_oeb = {(IO_PADS-1){rst}};
shalan0d14e6e2020-08-31 16:50:48 +020084
85 // LA
86 assign la_data_out = {{(127-BITS){1'b0}}, count};
87 // Assuming LA probes [63:32] are for controlling the count register
88 assign la_write = ~la_oen[63:32] & ~{BITS{valid}};
89 // Assuming LA probes [65:64] are for controlling the count clk & reset
90 assign clk = (~la_oen[64]) ? la_data_in[64]: wb_clk_i;
91 assign rst = (~la_oen[65]) ? la_data_in[65]: wb_rst_i;
92
93 counter #(
94 .BITS(BITS)
95 ) counter(
96 .clk(clk),
97 .reset(rst),
98 .ready(wbs_ack_i),
99 .valid(valid),
100 .rdata(rdata),
101 .wdata(wbs_dat_i),
102 .wstrb(wstrb),
103 .la_write(la_write),
104 .la_input(la_data_in[63:32]),
105 .count(count)
106 );
107
108endmodule
109
110module counter #(
111 parameter BITS = 32
112)(
113 input clk,
114 input reset,
115 input valid,
116 input [3:0] wstrb,
117 input [BITS-1:0] wdata,
118 input [BITS-1:0] la_write,
119 input [BITS-1:0] la_input,
120 output ready,
121 output [BITS-1:0] rdata,
122 output [BITS-1:0] count
123);
124 reg ready;
125 reg [BITS-1:0] count;
126 reg [BITS-1:0] rdata;
127
128 always @(posedge clk) begin
129 if (reset) begin
130 count <= 0;
131 ready <= 0;
132 end else begin
133 ready <= 1'b0;
134 if (~|la_write) begin
135 count <= count + 1;
136 end
137 if (valid && !ready) begin
138 ready <= 1'b1;
139 rdata <= count;
140 if (wstrb[0]) count[7:0] <= wdata[7:0];
141 if (wstrb[1]) count[15:8] <= wdata[15:8];
142 if (wstrb[2]) count[23:16] <= wdata[23:16];
143 if (wstrb[3]) count[31:24] <= wdata[31:24];
144 end
145 end
146 end
147
148 genvar i;
149 generate
150 for(i=0; i<BITS; i=i+1) begin
151 always @(posedge clk) begin
152 if (la_write[i]) count[i] <= la_input[i];
153 end
154 end
155 endgenerate
156
Tim Edwards05537512020-10-06 14:59:26 -0400157endmodule