blob: af198c48c89cd87b1989305c74338cf26db13d4a [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 #(
shalan0d14e6e2020-08-31 16:50:48 +020023 parameter IO_PADS = 32,
24 parameter BITS = 32
25)(
26 // Wishbone Slave ports (WB MI A)
27 input wb_clk_i,
28 input wb_rst_i,
Tim Edwards05537512020-10-06 14:59:26 -040029 input wbs_stb_i,
30 input wbs_cyc_i,
shalan0d14e6e2020-08-31 16:50:48 +020031 input wbs_we_i,
Tim Edwards05537512020-10-06 14:59:26 -040032 input [3:0] wbs_sel_i,
shalan0d14e6e2020-08-31 16:50:48 +020033 input [31:0] wbs_dat_i,
34 input [31:0] wbs_adr_i,
35 output wbs_ack_o,
Tim Edwards05537512020-10-06 14:59:26 -040036 output [31:0] wbs_dat_o,
37
shalan0d14e6e2020-08-31 16:50:48 +020038 // Logic Analyzer Signals
39 input [127:0] la_data_in,
40 output [127:0] la_data_out,
41 input [127:0] la_oen,
Tim Edwards05537512020-10-06 14:59:26 -040042
shalan0d14e6e2020-08-31 16:50:48 +020043 // IOs
44 input [IO_PADS-1:0] io_in,
45 output [IO_PADS-1:0] io_out
46);
47 wire clk;
48 wire rst;
49
50 wire [31:0] rdata;
51 wire [31:0] wdata;
52 wire [BITS-1:0] count;
53
54 wire valid;
55 wire [3:0] wstrb;
56 wire [31:0] la_write;
57
58 // WB MI A
59 assign valid = wbs_cyc_i && wbs_stb_i;
60 assign wstrb = wbs_sel_i & {4{wbs_we_i}};
61 assign wbs_dat_o = rdata;
62 assign wdata = wbs_dat_i;
63
64 // IO
65 assign io_out = count;
66
67 // LA
68 assign la_data_out = {{(127-BITS){1'b0}}, count};
69 // Assuming LA probes [63:32] are for controlling the count register
70 assign la_write = ~la_oen[63:32] & ~{BITS{valid}};
71 // Assuming LA probes [65:64] are for controlling the count clk & reset
72 assign clk = (~la_oen[64]) ? la_data_in[64]: wb_clk_i;
73 assign rst = (~la_oen[65]) ? la_data_in[65]: wb_rst_i;
74
75 counter #(
76 .BITS(BITS)
77 ) counter(
78 .clk(clk),
79 .reset(rst),
80 .ready(wbs_ack_i),
81 .valid(valid),
82 .rdata(rdata),
83 .wdata(wbs_dat_i),
84 .wstrb(wstrb),
85 .la_write(la_write),
86 .la_input(la_data_in[63:32]),
87 .count(count)
88 );
89
90endmodule
91
92module counter #(
93 parameter BITS = 32
94)(
95 input clk,
96 input reset,
97 input valid,
98 input [3:0] wstrb,
99 input [BITS-1:0] wdata,
100 input [BITS-1:0] la_write,
101 input [BITS-1:0] la_input,
102 output ready,
103 output [BITS-1:0] rdata,
104 output [BITS-1:0] count
105);
106 reg ready;
107 reg [BITS-1:0] count;
108 reg [BITS-1:0] rdata;
109
110 always @(posedge clk) begin
111 if (reset) begin
112 count <= 0;
113 ready <= 0;
114 end else begin
115 ready <= 1'b0;
116 if (~|la_write) begin
117 count <= count + 1;
118 end
119 if (valid && !ready) begin
120 ready <= 1'b1;
121 rdata <= count;
122 if (wstrb[0]) count[7:0] <= wdata[7:0];
123 if (wstrb[1]) count[15:8] <= wdata[15:8];
124 if (wstrb[2]) count[23:16] <= wdata[23:16];
125 if (wstrb[3]) count[31:24] <= wdata[31:24];
126 end
127 end
128 end
129
130 genvar i;
131 generate
132 for(i=0; i<BITS; i=i+1) begin
133 always @(posedge clk) begin
134 if (la_write[i]) count[i] <= la_input[i];
135 end
136 end
137 endgenerate
138
Tim Edwards05537512020-10-06 14:59:26 -0400139endmodule