Skip to content

Commit 11e53b8

Browse files
Fix: Correct open_cognitive_top.sv sub-module wiring and accumulator width
- Updated systolic array parameter wiring to use correct port names (inputs_a/inputs_b/outputs vs invented names) - Fixed accumulator width calculation to ACCUM_WIDTH = 2*DATA_WIDTH + $clog2(ARRAY_ROWS*ARRAY_COLS) + 1 - Implemented ReLU per-element instantiation in genvar loop for systolic row 0 - Connected softmax pipeline from ReLU output chain - Updated HDC n-gram encoder to use hdc_ngram_encoder_v4_pipelined with proper ready/valid handshake - Integrated AXI4-Lite control signals for global enable and array clear - Corrected effective enable logic combining external and AXI control paths
1 parent fc0593e commit 11e53b8

1 file changed

Lines changed: 150 additions & 109 deletions

File tree

‎rtl/top/open_cognitive_top.sv‎

Lines changed: 150 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
//
1818
// Licensed under the CERN Open Hardware Licence v2 - Weakly Reciprocal.
1919
// =============================================================================
20+
// Integration fixes:
21+
// - Re-wired every sub-module to its actual port list / parameter names.
22+
// The previous draft used invented interfaces (reg_write_en/reg_read_en,
23+
// data_in/data_out, inputs_A/inputs_B/result/done, in_vector/out_vector,
24+
// query_hv/ref_hv/hamming_dist, DIM) that matched no real module.
25+
// - syst_accum now reflects systolic_array_param's true accumulator width
26+
// (2*DATA_WIDTH + $clog2(ROWS*COLS) + 1) instead of 2*DATA_WIDTH.
27+
// - ReLU is instantiated per element (its real interface is 1-in/1-out).
28+
// - Softmax is fed from the systolic->ReLU row-0 chain (was a dead input).
29+
// - HDC n-gram uses the actual module name hdc_ngram_encoder_v4_pipelined
30+
// and its ready/valid handshake drives the distance core.
31+
// =============================================================================
2032

2133
`ifndef SYNTHESIS
2234
`timescale 1ns/1ps
@@ -28,20 +40,23 @@ module open_cognitive_top #(
2840
parameter VECTOR_SIZE = 4,
2941
parameter ARRAY_ROWS = 4,
3042
parameter ARRAY_COLS = 4,
31-
43+
// Systolic accumulator width (must match systolic_array_param)
44+
parameter EXTRA_BITS = $clog2(ARRAY_ROWS * ARRAY_COLS) + 1,
45+
parameter ACCUM_WIDTH = 2*DATA_WIDTH + EXTRA_BITS,
46+
3247
// HDC parameters
3348
parameter HV_DIM = 1024,
3449
parameter NGRAM_SIZE = 3,
3550
parameter TOKEN_WIDTH = 16,
36-
51+
3752
// AXI4-Lite parameters
3853
parameter AXI_ADDR_WIDTH = 32,
3954
parameter AXI_DATA_WIDTH = 32
4055
)(
4156
// Clock and reset
4257
input logic clk,
4358
input logic rst_n,
44-
59+
4560
// AXI4-Lite Slave Interface (Core Control)
4661
input logic [AXI_ADDR_WIDTH-1:0] axi_awaddr,
4762
input logic axi_awvalid,
@@ -60,7 +75,7 @@ module open_cognitive_top #(
6075
output logic [1:0] axi_rresp,
6176
output logic axi_rvalid,
6277
input logic axi_rready,
63-
78+
6479
// HDC Input Interface
6580
input logic hdc_en,
6681
input logic hdc_clear_context,
@@ -69,77 +84,91 @@ module open_cognitive_top #(
6984
output logic [HV_DIM-1:0] hdc_result_hv,
7085
output logic hdc_distance_valid,
7186
output logic [31:0] hdc_hamming_distance,
72-
87+
7388
// Systolic Array Interface
7489
input logic sa_en,
7590
input logic sa_start,
76-
input logic [ARRAY_ROWS-1:0][(2*DATA_WIDTH)-1:0] sa_matrix_a,
77-
input logic [ARRAY_COLS-1:0][(2*DATA_WIDTH)-1:0] sa_matrix_b,
78-
output logic [ARRAY_ROWS-1:0][(2*DATA_WIDTH)-1:0] sa_result,
91+
input logic [ARRAY_ROWS-1:0][DATA_WIDTH-1:0] sa_matrix_a,
92+
input logic [ARRAY_COLS-1:0][DATA_WIDTH-1:0] sa_matrix_b,
93+
output logic [ARRAY_ROWS-1:0][ARRAY_COLS-1:0][ACCUM_WIDTH-1:0] sa_result,
7994
output logic sa_done,
80-
95+
8196
// Softmax Interface
8297
input logic softmax_en,
8398
input logic softmax_start,
84-
input logic [VECTOR_SIZE-1:0][(2*DATA_WIDTH)-1:0] softmax_input,
8599
output logic [VECTOR_SIZE-1:0][DATA_WIDTH-1:0] softmax_probs,
86100
output logic softmax_done
87101
);
88102

89-
// Internal wires for AXI4-Lite controller
90-
logic axi_reg_write_en;
91-
logic axi_reg_read_en;
92-
logic [AXI_ADDR_WIDTH-1:0] axi_reg_addr;
93-
logic [AXI_DATA_WIDTH-1:0] axi_reg_wdata;
94-
logic [AXI_DATA_WIDTH-1:0] axi_reg_rdata;
95-
96-
// Internal wires for data path
97-
logic [ARRAY_ROWS-1:0][(2*DATA_WIDTH)-1:0] skewed_data;
98-
logic [ARRAY_ROWS-1:0][(2*DATA_WIDTH)-1:0] relu_output;
103+
// AXI4-Lite controller control outputs
104+
logic ctrl_global_en;
105+
logic ctrl_array_clr;
106+
logic ctrl_softmax_start;
107+
108+
// Effective enable signals (local AXI control OR external enables)
109+
logic sa_en_eff;
110+
logic softmax_start_eff;
111+
logic hdc_en_eff;
112+
113+
// Skew buffer
114+
logic [ARRAY_ROWS-1:0][DATA_WIDTH-1:0] skewed_data;
115+
116+
// ReLU outputs (applied to systolic row 0 that feeds the softmax)
117+
logic [ARRAY_COLS-1:0][DATA_WIDTH-1:0] relu_output;
118+
119+
// Softmax vector (zero-extended ReLU outputs)
99120
logic [VECTOR_SIZE-1:0][(2*DATA_WIDTH)-1:0] softmax_vector;
100-
121+
101122
// HDC internal signals
102123
logic [HV_DIM-1:0] ngram_hv;
103124
logic [HV_DIM-1:0] reference_hv [0:7]; // 8 reference vectors
104-
logic hdc_compute_en;
105-
125+
logic ngram_valid;
126+
logic hdc_s_ready;
127+
logic [$clog2(HV_DIM):0] hamming_dist;
128+
106129
// =========================================================================
107130
// AXI4-Lite Controller
108131
// =========================================================================
109132
axi4_lite_core_ctrl #(
110133
.ADDR_WIDTH(AXI_ADDR_WIDTH),
111134
.DATA_WIDTH(AXI_DATA_WIDTH)
112135
) u_axi_ctrl (
113-
.clk(clk),
114-
.rst_n(rst_n),
115-
136+
.S_AXI_ACLK(clk),
137+
.S_AXI_ARESETN(rst_n),
138+
116139
// AXI Slave
117-
.axi_awaddr(axi_awaddr),
118-
.axi_awvalid(axi_awvalid),
119-
.axi_awready(axi_awready),
120-
.axi_wdata(axi_wdata),
121-
.axi_wstrb(axi_wstrb),
122-
.axi_wvalid(axi_wvalid),
123-
.axi_wready(axi_wready),
124-
.axi_bresp(axi_bresp),
125-
.axi_bvalid(axi_bvalid),
126-
.axi_bready(axi_bready),
127-
.axi_araddr(axi_araddr),
128-
.axi_arvalid(axi_arvalid),
129-
.axi_arready(axi_arready),
130-
.axi_rdata(axi_rdata),
131-
.axi_rresp(axi_rresp),
132-
.axi_rvalid(axi_rvalid),
133-
.axi_rready(axi_rready),
134-
135-
// Register interface
136-
.reg_write_en(axi_reg_write_en),
137-
.reg_read_en(axi_reg_read_en),
138-
.reg_addr(axi_reg_addr),
139-
.reg_wdata(axi_reg_wdata),
140-
.reg_rdata(axi_reg_rdata)
140+
.S_AXI_AWADDR(axi_awaddr),
141+
.S_AXI_AWVALID(axi_awvalid),
142+
.S_AXI_AWREADY(axi_awready),
143+
.S_AXI_WDATA(axi_wdata),
144+
.S_AXI_WSTRB(axi_wstrb),
145+
.S_AXI_WVALID(axi_wvalid),
146+
.S_AXI_WREADY(axi_wready),
147+
.S_AXI_BRESP(axi_bresp),
148+
.S_AXI_BVALID(axi_bvalid),
149+
.S_AXI_BREADY(axi_bready),
150+
.S_AXI_ARADDR(axi_araddr),
151+
.S_AXI_ARVALID(axi_arvalid),
152+
.S_AXI_ARREADY(axi_arready),
153+
.S_AXI_RDATA(axi_rdata),
154+
.S_AXI_RRESP(axi_rresp),
155+
.S_AXI_RVALID(axi_rvalid),
156+
.S_AXI_RREADY(axi_rready),
157+
158+
// Control / status interface
159+
.ctrl_global_en(ctrl_global_en),
160+
.ctrl_array_clr(ctrl_array_clr),
161+
.ctrl_softmax_start(ctrl_softmax_start),
162+
.core_softmax_done(softmax_done)
141163
);
142-
164+
165+
// Effective enables: local AXI control OR external interface enables
166+
assign sa_en_eff = sa_en | ctrl_global_en;
167+
assign softmax_start_eff = (softmax_start | ctrl_softmax_start) &
168+
(softmax_en | ctrl_global_en);
169+
assign hdc_en_eff = (hdc_en | ctrl_global_en) &
170+
(hdc_s_ready | ~ngram_valid);
171+
143172
// =========================================================================
144173
// SRAM Skew Buffer
145174
// =========================================================================
@@ -149,11 +178,12 @@ module open_cognitive_top #(
149178
) u_skew (
150179
.clk(clk),
151180
.rst_n(rst_n),
152-
.en(sa_en),
153-
.data_in(sa_matrix_a),
154-
.data_out(skewed_data)
181+
.en(sa_en_eff),
182+
.wr_en(sa_start),
183+
.write_data(sa_matrix_a),
184+
.skewed_data(skewed_data)
155185
);
156-
186+
157187
// =========================================================================
158188
// Systolic Array
159189
// =========================================================================
@@ -164,94 +194,105 @@ module open_cognitive_top #(
164194
) u_systolic (
165195
.clk(clk),
166196
.rst_n(rst_n),
167-
.en(sa_en),
168-
.start(sa_start),
169-
.inputs_A(skewed_data),
170-
.inputs_B(sa_matrix_b),
171-
.result(sa_result),
172-
.done(sa_done)
197+
.en(sa_en_eff),
198+
.clr(ctrl_array_clr),
199+
.inputs_a(skewed_data),
200+
.inputs_b(sa_matrix_b),
201+
.outputs(sa_result)
173202
);
174-
203+
204+
// One-cycle completion pulse following a systolic start
205+
always_ff @(posedge clk or negedge rst_n) begin
206+
if (!rst_n) begin
207+
sa_done <= 1'b0;
208+
end else begin
209+
sa_done <= sa_start;
210+
end
211+
end
212+
175213
// =========================================================================
176-
// ReLU Activation
214+
// ReLU Activation (combinational, applied to systolic row 0)
177215
// =========================================================================
178-
relu_activation #(
179-
.DATA_WIDTH(DATA_WIDTH),
180-
.VECTOR_SIZE(VECTOR_SIZE)
181-
) u_relu (
182-
.clk(clk),
183-
.rst_n(rst_n),
184-
.en(sa_en),
185-
.in_vector({sa_result[0][2*DATA_WIDTH-1:DATA_WIDTH],
186-
sa_result[1][2*DATA_WIDTH-1:DATA_WIDTH],
187-
sa_result[2][2*DATA_WIDTH-1:DATA_WIDTH],
188-
sa_result[3][2*DATA_WIDTH-1:DATA_WIDTH]}),
189-
.out_vector(relu_output)
190-
);
191-
216+
genvar c;
217+
generate
218+
for (c = 0; c < ARRAY_COLS; c++) begin : relu_col
219+
relu_activation #(
220+
.DATA_WIDTH(DATA_WIDTH),
221+
.ACCUM_WIDTH(ACCUM_WIDTH)
222+
) u_relu (
223+
.accum_in(sa_result[0][c]),
224+
.relu_out(relu_output[c])
225+
);
226+
end
227+
endgenerate
228+
229+
// Build the softmax input vector from the ReLU outputs of systolic row 0
230+
genvar c2;
231+
generate
232+
for (c2 = 0; c2 < VECTOR_SIZE; c2++) begin : softmax_vec_gen
233+
assign softmax_vector[c2] = {{DATA_WIDTH{1'b0}}, relu_output[c2]};
234+
end
235+
endgenerate
236+
192237
// =========================================================================
193238
// Softmax
194239
// =========================================================================
195240
softmax_core #(
196241
.DATA_WIDTH(DATA_WIDTH),
197-
.VECTOR_SIZE(VECTOR_SIZE),
198-
.FRAC_WIDTH(DATA_WIDTH)
242+
.VECTOR_SIZE(VECTOR_SIZE)
199243
) u_softmax (
200244
.clk(clk),
201245
.rst_n(rst_n),
202-
.en(softmax_en),
203-
.start(softmax_start),
204-
.in_vector(softmax_input),
246+
.start(softmax_start_eff),
247+
.in_vector(softmax_vector),
205248
.out_probs(softmax_probs),
206249
.done(softmax_done)
207250
);
208-
251+
209252
// =========================================================================
210253
// HDC N-gram Encoder
211254
// =========================================================================
212-
hdc_ngram_encoder_v3 #(
255+
hdc_ngram_encoder_v4_pipelined #(
213256
.HV_DIM(HV_DIM),
214257
.NGRAM_SIZE(NGRAM_SIZE),
215258
.TOKEN_WIDTH(TOKEN_WIDTH)
216259
) u_ngram (
217260
.clk(clk),
218261
.rst_n(rst_n),
219-
.en(hdc_en),
262+
.en(hdc_en_eff),
220263
.clear_context(hdc_clear_context),
221264
.token_in(hdc_token_in),
222-
.ngram_hv(ngram_hv)
265+
.ngram_hv(ngram_hv),
266+
.ngram_valid(ngram_valid)
223267
);
224-
268+
225269
// Assign query HV from N-gram encoder
226270
assign hdc_query_hv = ngram_hv;
227-
271+
228272
// =========================================================================
229-
// HDC Distance Core (placeholder for reference HVs)
273+
// HDC Distance Core
230274
// =========================================================================
231-
// Note: Reference HVs should be loaded via AXI register interface
232-
// This is a simplified connection - full implementation needs memory
233-
275+
// Note: Reference HVs should be loaded via AXI register interface.
276+
// This is a simplified connection - full implementation needs memory.
234277
hdc_distance_core_v3 #(
235-
.DIM(HV_DIM)
278+
.VECTOR_SIZE(HV_DIM)
236279
) u_hdc_dist (
237280
.clk(clk),
238281
.rst_n(rst_n),
239-
.en(hdc_compute_en),
240-
.query_hv(hdc_query_hv),
241-
.ref_hv(reference_hv[0]), // Use first reference for demo
242-
.hamming_dist(hdc_hamming_distance),
243-
.distance_valid(hdc_distance_valid)
282+
.s_valid(ngram_valid),
283+
.s_ready(hdc_s_ready),
284+
.vector_a(hdc_query_hv),
285+
.vector_b(reference_hv[0]), // Use first reference for demo
286+
.m_valid(hdc_distance_valid),
287+
.m_ready(1'b1),
288+
.hamming_distance(hamming_dist)
244289
);
245-
246-
// Simple state machine to trigger HDC computation
247-
always_ff @(posedge clk or negedge rst_n) begin
248-
if (!rst_n) begin
249-
hdc_compute_en <= 1'b0;
250-
end else begin
251-
hdc_compute_en <= hdc_en;
252-
end
253-
end
254-
290+
291+
assign hdc_hamming_distance = {{(32 - ($clog2(HV_DIM) + 1)){1'b0}}, hamming_dist};
292+
293+
// Placeholder result HV (would come from a trained model memory)
294+
assign hdc_result_hv = reference_hv[0];
295+
255296
// Default initialization for reference HVs
256297
always_ff @(posedge clk or negedge rst_n) begin
257298
if (!rst_n) begin
@@ -261,5 +302,5 @@ module open_cognitive_top #(
261302
end
262303
// Reference HVs would be loaded via AXI in full implementation
263304
end
264-
305+
265306
endmodule

0 commit comments

Comments
 (0)