blob: d11cde195253e49b03659cdb2de244704194d840 [file] [log] [blame]
Philipp Wagner31441082020-07-14 11:17:21 +01001// Copyright lowRISC contributors.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5`include "prim_assert.sv"
6
7/**
8 * OTBN Controller
9 */
10module otbn_controller
11 import otbn_pkg::*;
12#(
13 // Size of the instruction memory, in bytes
14 parameter int ImemSizeByte = 4096,
15 // Size of the data memory, in bytes
16 parameter int DmemSizeByte = 4096,
17
18 localparam int ImemAddrWidth = prim_util_pkg::vbits(ImemSizeByte),
19 localparam int DmemAddrWidth = prim_util_pkg::vbits(DmemSizeByte)
20) (
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +000021 input logic clk_i,
22 input logic rst_ni,
Philipp Wagner31441082020-07-14 11:17:21 +010023
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +000024 input logic start_i, // start the processing at address zero
25 output logic locked_o, // OTBN in locked state and must be reset to perform any further actions
Greg Chadwickd3154ec2020-09-24 12:03:23 +010026
Andreas Kurth13d76852022-04-04 14:55:54 +020027 input prim_mubi_pkg::mubi4_t escalate_en_i,
Rupert Swarbrick75885e62022-03-07 14:54:45 +000028 output controller_err_bits_t err_bits_o,
29 output logic recoverable_err_o,
Greg Chadwickd3154ec2020-09-24 12:03:23 +010030
Philipp Wagner31441082020-07-14 11:17:21 +010031 // Next instruction selection (to instruction fetch)
32 output logic insn_fetch_req_valid_o,
33 output logic [ImemAddrWidth-1:0] insn_fetch_req_addr_o,
Greg Chadwick0ac448a2021-11-18 17:10:58 +000034 output logic insn_fetch_resp_clear_o,
Philipp Wagner31441082020-07-14 11:17:21 +010035
36 // Fetched/decoded instruction
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +000037 input logic insn_valid_i,
38 input logic insn_illegal_i,
39 input logic [ImemAddrWidth-1:0] insn_addr_i,
Philipp Wagner31441082020-07-14 11:17:21 +010040
Philipp Wagner56a64bd2021-05-08 14:31:24 +010041 // Decoded instruction data
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +000042 input insn_dec_base_t insn_dec_base_i,
43 input insn_dec_bignum_t insn_dec_bignum_i,
44 input insn_dec_shared_t insn_dec_shared_i,
Philipp Wagner31441082020-07-14 11:17:21 +010045
46 // Base register file
Greg Chadwickee060bd2021-05-15 17:33:22 +010047 output logic [4:0] rf_base_wr_addr_o,
48 output logic rf_base_wr_en_o,
49 output logic rf_base_wr_commit_o,
50 output logic [31:0] rf_base_wr_data_no_intg_o,
51 output logic [BaseIntgWidth-1:0] rf_base_wr_data_intg_o,
52 output logic rf_base_wr_data_intg_sel_o,
Philipp Wagner31441082020-07-14 11:17:21 +010053
Greg Chadwick009d9ee2021-04-26 16:25:51 +010054 output logic [4:0] rf_base_rd_addr_a_o,
55 output logic rf_base_rd_en_a_o,
56 input logic [BaseIntgWidth-1:0] rf_base_rd_data_a_intg_i,
57 output logic [4:0] rf_base_rd_addr_b_o,
58 output logic rf_base_rd_en_b_o,
59 input logic [BaseIntgWidth-1:0] rf_base_rd_data_b_intg_i,
60 output logic rf_base_rd_commit_o,
Philipp Wagner31441082020-07-14 11:17:21 +010061
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +000062 input logic rf_base_call_stack_err_i,
Greg Chadwickd3154ec2020-09-24 12:03:23 +010063
Greg Chadwickf7863442020-09-14 18:11:33 +010064 // Bignum register file (WDRs)
Greg Chadwick009d9ee2021-04-26 16:25:51 +010065 output logic [4:0] rf_bignum_wr_addr_o,
66 output logic [1:0] rf_bignum_wr_en_o,
67 output logic [WLEN-1:0] rf_bignum_wr_data_no_intg_o,
68 output logic [ExtWLEN-1:0] rf_bignum_wr_data_intg_o,
69 output logic rf_bignum_wr_data_intg_sel_o,
Greg Chadwickf7863442020-09-14 18:11:33 +010070
Greg Chadwick009d9ee2021-04-26 16:25:51 +010071 output logic [4:0] rf_bignum_rd_addr_a_o,
72 output logic rf_bignum_rd_en_a_o,
73 input logic [ExtWLEN-1:0] rf_bignum_rd_data_a_intg_i,
Greg Chadwickf7863442020-09-14 18:11:33 +010074
Greg Chadwick009d9ee2021-04-26 16:25:51 +010075 output logic [4:0] rf_bignum_rd_addr_b_o,
76 output logic rf_bignum_rd_en_b_o,
77 input logic [ExtWLEN-1:0] rf_bignum_rd_data_b_intg_i,
78
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +000079 input logic rf_bignum_rd_data_err_i,
Greg Chadwickf7863442020-09-14 18:11:33 +010080
Philipp Wagner31441082020-07-14 11:17:21 +010081 // Execution units
Greg Chadwickf7863442020-09-14 18:11:33 +010082
83 // Base ALU
Greg Chadwick9791eed2020-07-22 18:08:28 +010084 output alu_base_operation_t alu_base_operation_o,
85 output alu_base_comparison_t alu_base_comparison_o,
86 input logic [31:0] alu_base_operation_result_i,
Greg Chadwickc8cd4352020-08-14 16:45:23 +010087 input logic alu_base_comparison_result_i,
88
Greg Chadwickf7863442020-09-14 18:11:33 +010089 // Bignum ALU
90 output alu_bignum_operation_t alu_bignum_operation_o,
91 input logic [WLEN-1:0] alu_bignum_operation_result_i,
Greg Chadwick009d9ee2021-04-26 16:25:51 +010092 input logic alu_bignum_selection_flag_i,
Greg Chadwickf7863442020-09-14 18:11:33 +010093
Greg Chadwick94786452020-10-28 18:19:51 +000094 // Bignum MAC
95 output mac_bignum_operation_t mac_bignum_operation_o,
96 input logic [WLEN-1:0] mac_bignum_operation_result_i,
97 output logic mac_bignum_en_o,
98
Greg Chadwickf7863442020-09-14 18:11:33 +010099 // LSU
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100100 output logic lsu_load_req_o,
101 output logic lsu_store_req_o,
102 output insn_subset_e lsu_req_subset_o,
103 output logic [DmemAddrWidth-1:0] lsu_addr_o,
104
Greg Chadwickee060bd2021-05-15 17:33:22 +0100105 output logic [BaseIntgWidth-1:0] lsu_base_wdata_o,
106 output logic [ExtWLEN-1:0] lsu_bignum_wdata_o,
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100107
Greg Chadwickee060bd2021-05-15 17:33:22 +0100108 input logic [BaseIntgWidth-1:0] lsu_base_rdata_i,
109 input logic [ExtWLEN-1:0] lsu_bignum_rdata_i,
Greg Chadwickf7863442020-09-14 18:11:33 +0100110
111 // Internal Special-Purpose Registers (ISPRs)
112 output ispr_e ispr_addr_o,
113 output logic [31:0] ispr_base_wdata_o,
114 output logic [BaseWordsPerWLEN-1:0] ispr_base_wr_en_o,
115 output logic [WLEN-1:0] ispr_bignum_wdata_o,
116 output logic ispr_bignum_wr_en_o,
Greg Chadwickb168ae92021-04-14 16:04:03 +0100117 input logic [WLEN-1:0] ispr_rdata_i,
118
119 output logic rnd_req_o,
120 output logic rnd_prefetch_req_o,
Vladimir Rozicb8db07a2021-05-24 14:15:04 +0100121 input logic rnd_valid_i,
122
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100123 // Secure Wipe
124 input logic secure_wipe_running_i,
125 output logic start_secure_wipe_o,
126 input logic sec_wipe_zero_i,
127
Rupert Swarbrick13d50082021-07-13 14:14:03 +0100128 input logic state_reset_i,
Greg Chadwickf0a30192021-08-19 09:33:25 +0100129 output logic [31:0] insn_cnt_o,
Vladimir Rozic09e25ac2021-11-25 00:19:38 +0000130 input logic insn_cnt_clear_i,
Greg Chadwicke9452b52022-02-03 20:17:47 +0000131 output logic mems_sec_wipe_o,
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000132
Greg Chadwick4ca1caa2021-11-23 10:56:01 +0000133 input logic software_errs_fatal_i,
134
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000135 input logic [1:0] sideload_key_shares_valid_i,
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000136
137 // Prefetch stage control
138 output logic prefetch_en_o,
139 output logic prefetch_loop_active_o,
140 output logic [31:0] prefetch_loop_iterations_o,
Rupert Swarbrickfafeaf22022-01-04 14:42:44 +0000141 output logic [ImemAddrWidth:0] prefetch_loop_end_addr_o,
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000142 output logic [ImemAddrWidth-1:0] prefetch_loop_jump_addr_o
Philipp Wagner31441082020-07-14 11:17:21 +0100143);
Andreas Kurth13d76852022-04-04 14:55:54 +0200144 import prim_mubi_pkg::*;
145
Greg Chadwick529738c2021-09-29 18:08:11 +0100146 otbn_state_e state_q, state_d;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100147
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000148
149 controller_err_bits_t err_bits_q, err_bits_d;
150
151 // The specific error signals that go into err_bits_d
152 logic fatal_software_err, bad_internal_state_err, reg_intg_violation_err, key_invalid_err;
153 logic illegal_insn_err, bad_data_addr_err, call_stack_err, bad_insn_addr_err;
154
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100155 logic err;
Greg Chadwick84e01ac2021-10-04 17:51:07 +0100156 logic software_err;
Greg Chadwick3e25bcd2021-10-25 12:05:36 +0100157 logic non_insn_addr_software_err;
Greg Chadwick79738062021-09-15 18:09:14 +0100158 logic fatal_err;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100159 logic done_complete;
Rupert Swarbrick692db552021-09-24 16:26:31 +0100160 logic executing;
Vladimir Rozic76376ba2022-01-06 08:47:11 +0000161 logic state_error;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100162
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000163 logic insn_fetch_req_valid_raw;
164 logic [ImemAddrWidth-1:0] insn_fetch_req_addr_last;
Greg Chadwick28836af2020-07-23 14:35:52 +0100165
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100166 logic stall;
Greg Chadwickb168ae92021-04-14 16:04:03 +0100167 logic ispr_stall;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100168 logic mem_stall;
Rupert Swarbrickb2b784d2021-08-03 14:21:51 +0100169 logic jump_or_branch;
Greg Chadwick51f36232020-09-02 15:37:23 +0100170 logic branch_taken;
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000171 logic insn_executing;
Greg Chadwick51f36232020-09-02 15:37:23 +0100172 logic [ImemAddrWidth-1:0] branch_target;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100173 logic branch_target_overflow;
Rupert Swarbricke9ff47f2021-01-04 13:20:36 +0000174 logic [ImemAddrWidth:0] next_insn_addr_wide;
Greg Chadwick51f36232020-09-02 15:37:23 +0100175 logic [ImemAddrWidth-1:0] next_insn_addr;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100176
Greg Chadwickf7863442020-09-14 18:11:33 +0100177 csr_e csr_addr;
Philipp Wagner711d2262021-01-21 18:17:42 +0000178 logic [$clog2(BaseWordsPerWLEN)-1:0] csr_sub_addr;
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000179 logic [31:0] csr_rdata_raw;
Greg Chadwickf7863442020-09-14 18:11:33 +0100180 logic [31:0] csr_rdata;
181 logic [BaseWordsPerWLEN-1:0] csr_rdata_mux [32];
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000182 logic [31:0] csr_wdata_raw;
Greg Chadwickf7863442020-09-14 18:11:33 +0100183 logic [31:0] csr_wdata;
184
185 wsr_e wsr_addr;
186 logic [WLEN-1:0] wsr_wdata;
187
188 ispr_e ispr_addr_base;
189 logic [$clog2(BaseWordsPerWLEN)-1:0] ispr_word_addr_base;
190 logic [BaseWordsPerWLEN-1:0] ispr_word_sel_base;
191
192 ispr_e ispr_addr_bignum;
193
Greg Chadwickb168ae92021-04-14 16:04:03 +0100194 logic ispr_wr_insn, ispr_rd_insn;
Rupert Swarbrick514348e2021-02-03 09:04:59 +0000195 logic ispr_wr_base_insn;
196 logic ispr_wr_bignum_insn;
Prajwala Puttappa175c0d82021-12-17 11:23:16 +0000197 logic ispr_rd_bignum_insn;
Greg Chadwickf7863442020-09-14 18:11:33 +0100198
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000199 logic lsu_load_req_raw;
200 logic lsu_store_req_raw;
Rupert Swarbrick20429db2022-02-10 17:34:00 +0000201 logic rnd_req_raw;
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000202
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100203 // Register read data with integrity stripped off
204 logic [31:0] rf_base_rd_data_a_no_intg;
205 logic [31:0] rf_base_rd_data_b_no_intg;
206 logic [WLEN-1:0] rf_bignum_rd_data_a_no_intg;
207 logic [WLEN-1:0] rf_bignum_rd_data_b_no_intg;
208
209 logic [ExtWLEN-1:0] selection_result;
210
Greg Chadwickae8e6452020-10-02 12:04:15 +0100211 // Computed increments for indirect register index and memory address in BN.LID/BN.SID/BN.MOVR
212 // instructions.
Greg Chadwick496fd342021-03-05 18:08:39 +0000213 logic [5:0] rf_base_rd_data_a_inc;
214 logic [5:0] rf_base_rd_data_b_inc;
Rupert Swarbrick9a4f47b2021-01-04 15:48:24 +0000215 logic [26:0] rf_base_rd_data_a_wlen_word_inc;
Greg Chadwickae8e6452020-10-02 12:04:15 +0100216
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100217 // Read/Write enables for base register file before illegal instruction encoding are factored in
218 logic rf_base_rd_en_a_raw, rf_base_rd_en_b_raw, rf_base_wr_en_raw;
219
Greg Chadwickae8e6452020-10-02 12:04:15 +0100220 // Output of mux taking the above increments as inputs and choosing one to write back to base
221 // register file with appropriate zero extension and padding to give a 32-bit result.
222 logic [31:0] increment_out;
223
Greg Chadwick53c95862020-10-14 17:58:38 +0100224 // Loop control, used to start a new loop
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000225 logic loop_start_req;
226 logic loop_start_commit;
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100227 logic loop_reset;
Greg Chadwick53c95862020-10-14 17:58:38 +0100228 logic [11:0] loop_bodysize;
229 logic [31:0] loop_iterations;
230
231 // Loop generated jumps. The loop controller asks to jump when execution reaches the end of a loop
232 // body that hasn't completed all of its iterations.
233 logic loop_jump;
234 logic [ImemAddrWidth-1:0] loop_jump_addr;
235
Greg Chadwick94786452020-10-28 18:19:51 +0000236 logic [WLEN-1:0] mac_bignum_rf_wr_data;
237
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100238 logic csr_illegal_addr, wsr_illegal_addr, ispr_illegal_addr;
239 logic imem_addr_err, loop_err, ispr_err;
240 logic dmem_addr_err, dmem_addr_unaligned_base, dmem_addr_unaligned_bignum, dmem_addr_overflow;
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100241 logic illegal_insn_static;
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000242 logic key_invalid;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100243
Greg Chadwick496fd342021-03-05 18:08:39 +0000244 logic rf_a_indirect_err, rf_b_indirect_err, rf_d_indirect_err, rf_indirect_err;
245
Rupert Swarbrickdb4b4942022-01-10 12:35:14 +0000246 // If we are doing an indirect lookup from the bignum register file, it's possible that the
247 // address that we use for the lookup is architecturally unknown. This happens if it came from x1
248 // and we've underflowed the call stack. When this happens, we want to ignore any read data
249 // integrity errors since the read from the bignum register file didn't happen architecturally
250 // anyway.
251 logic ignore_bignum_rd_errs;
252 logic rf_bignum_rd_data_err;
253
Vladimir Rozicb8db07a2021-05-24 14:15:04 +0100254 logic [31:0] insn_cnt_d, insn_cnt_q;
Vladimir Rozic09e25ac2021-11-25 00:19:38 +0000255 logic insn_cnt_clear;
Vladimir Rozicb8db07a2021-05-24 14:15:04 +0100256
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100257 logic [4:0] ld_insn_bignum_wr_addr_q;
258
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100259 // Stall a cycle on loads to allow load data writeback to happen the following cycle. Stall not
260 // required on stores as there is no response to deal with.
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000261 assign mem_stall = lsu_load_req_raw;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100262
Greg Chadwickb168ae92021-04-14 16:04:03 +0100263 // Reads to RND must stall until data is available
Rupert Swarbrick20429db2022-02-10 17:34:00 +0000264 assign ispr_stall = rnd_req_raw & ~rnd_valid_i;
Greg Chadwickb168ae92021-04-14 16:04:03 +0100265
266 assign stall = mem_stall | ispr_stall;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100267
Rupert Swarbrick692db552021-09-24 16:26:31 +0100268 // OTBN is done when it was executing something (in state OtbnStateUrndRefresh, OtbnStateRun or
269 // OtbnStateStall) and either it executes an ecall or an error occurs. A pulse on the done signal
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000270 // raises the 'done' interrupt and also tells the top-level to update its ERR_BITS status
Rupert Swarbrick692db552021-09-24 16:26:31 +0100271 // register.
272 //
273 // The calculation that ecall triggered done is factored out as `done_complete` to avoid logic
274 // loops in the error handling logic.
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100275 assign done_complete = (insn_valid_i & insn_dec_shared_i.ecall_insn);
Rupert Swarbrick692db552021-09-24 16:26:31 +0100276 assign executing = (state_q == OtbnStateUrndRefresh) ||
277 (state_q == OtbnStateRun) ||
278 (state_q == OtbnStateStall);
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100279
Rupert Swarbrick5c72c962022-03-11 21:30:00 +0000280 assign locked_o = (state_q == OtbnStateLocked) & ~secure_wipe_running_i;
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100281 assign start_secure_wipe_o = executing & (done_complete | err) & ~secure_wipe_running_i;
Philipp Wagner31441082020-07-14 11:17:21 +0100282
Rupert Swarbrickb2b784d2021-08-03 14:21:51 +0100283 assign jump_or_branch = (insn_valid_i &
284 (insn_dec_shared_i.branch_insn | insn_dec_shared_i.jump_insn));
285
Greg Chadwick51f36232020-09-02 15:37:23 +0100286 // Branch taken when there is a valid branch instruction and comparison passes or a valid jump
287 // instruction (which is always taken)
Philipp Wagnerdc946522020-12-03 10:52:58 +0000288 assign branch_taken = insn_valid_i &
289 ((insn_dec_shared_i.branch_insn & alu_base_comparison_result_i) |
290 insn_dec_shared_i.jump_insn);
Greg Chadwick51f36232020-09-02 15:37:23 +0100291 // Branch target computed by base ALU (PC + imm)
Greg Chadwick51f36232020-09-02 15:37:23 +0100292 assign branch_target = alu_base_operation_result_i[ImemAddrWidth-1:0];
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100293 assign branch_target_overflow = |alu_base_operation_result_i[31:ImemAddrWidth];
Greg Chadwick51f36232020-09-02 15:37:23 +0100294
Rupert Swarbricke9ff47f2021-01-04 13:20:36 +0000295 assign next_insn_addr_wide = {1'b0, insn_addr_i} + 'd4;
296 assign next_insn_addr = next_insn_addr_wide[ImemAddrWidth-1:0];
Greg Chadwick51f36232020-09-02 15:37:23 +0100297
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000298 // Record address for fetch request so it can be retried when an invalid response is received
299 always_ff @(posedge clk_i) begin
300 if (insn_fetch_req_valid_raw) begin
301 insn_fetch_req_addr_last <= insn_fetch_req_addr_o;
302 end
303 end
304
Philipp Wagner31441082020-07-14 11:17:21 +0100305 always_comb begin
Greg Chadwick529738c2021-09-29 18:08:11 +0100306 state_d = state_q;
307 // `insn_fetch_req_valid_raw` is the value `insn_fetch_req_valid_o` before any errors are
308 // considered.
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100309 insn_fetch_req_valid_raw = 1'b0;
Rupert Swarbrick2fb857a2021-09-03 17:14:50 +0100310 insn_fetch_req_addr_o = '0;
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000311 insn_fetch_resp_clear_o = 1'b1;
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000312 prefetch_en_o = 1'b0;
Greg Chadwick28836af2020-07-23 14:35:52 +0100313
Vladimir Rozic76376ba2022-01-06 08:47:11 +0000314 state_error = 1'b0;
315
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100316 unique case (state_q)
317 OtbnStateHalt: begin
318 if (start_i) begin
Greg Chadwick529738c2021-09-29 18:08:11 +0100319 state_d = OtbnStateRun;
Greg Chadwickb5b86862021-04-09 15:49:43 +0100320
Rupert Swarbrick2fb857a2021-09-03 17:14:50 +0100321 insn_fetch_req_addr_o = '0;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100322 insn_fetch_req_valid_raw = 1'b1;
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000323 prefetch_en_o = 1'b1;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100324 end
325 end
326 OtbnStateRun: begin
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100327 insn_fetch_req_valid_raw = 1'b1;
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000328 prefetch_en_o = 1'b1;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100329
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000330 if (!insn_valid_i) begin
331 insn_fetch_req_addr_o = insn_fetch_req_addr_last;
332 end else if (done_complete) begin
Greg Chadwick529738c2021-09-29 18:08:11 +0100333 state_d = OtbnStateHalt;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100334 insn_fetch_req_valid_raw = 1'b0;
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000335 prefetch_en_o = 1'b0;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100336 end else begin
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100337 if (stall) begin
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000338 // When stalling don't request a new fetch and don't clear response either to keep
339 // current instruction.
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000340 state_d = OtbnStateStall;
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000341 insn_fetch_req_valid_raw = 1'b0;
342 insn_fetch_resp_clear_o = 1'b0;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100343 end else begin
Greg Chadwick51f36232020-09-02 15:37:23 +0100344 if (branch_taken) begin
345 insn_fetch_req_addr_o = branch_target;
Greg Chadwick53c95862020-10-14 17:58:38 +0100346 end else if (loop_jump) begin
347 insn_fetch_req_addr_o = loop_jump_addr;
Greg Chadwick51f36232020-09-02 15:37:23 +0100348 end else begin
349 insn_fetch_req_addr_o = next_insn_addr;
350 end
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100351 end
352 end
353 end
354 OtbnStateStall: begin
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000355 prefetch_en_o = 1'b1;
Greg Chadwicke6e8f152021-03-05 13:35:36 +0000356 // When stalling refetch the same instruction to keep decode inputs constant
357 if (stall) begin
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000358 state_d = OtbnStateStall;
359 //insn_fetch_req_addr_o = insn_addr_i;
360 insn_fetch_req_valid_raw = 1'b0;
361 insn_fetch_resp_clear_o = 1'b0;
Greg Chadwick9aef1c92021-01-25 18:24:58 +0000362 end else begin
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000363 insn_fetch_req_valid_raw = 1'b1;
364
Greg Chadwicke6e8f152021-03-05 13:35:36 +0000365 if (loop_jump) begin
366 insn_fetch_req_addr_o = loop_jump_addr;
367 end else begin
368 insn_fetch_req_addr_o = next_insn_addr;
369 end
Greg Chadwick9aef1c92021-01-25 18:24:58 +0000370
Greg Chadwick529738c2021-09-29 18:08:11 +0100371 state_d = OtbnStateRun;
Greg Chadwicke6e8f152021-03-05 13:35:36 +0000372 end
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100373 end
Greg Chadwick79738062021-09-15 18:09:14 +0100374 OtbnStateLocked: begin
375 insn_fetch_req_valid_raw = 1'b0;
Greg Chadwick529738c2021-09-29 18:08:11 +0100376 state_d = OtbnStateLocked;
Greg Chadwick79738062021-09-15 18:09:14 +0100377 end
Vladimir Rozic76376ba2022-01-06 08:47:11 +0000378 default: begin
379 state_error = 1'b1;
380 end
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100381 endcase
Greg Chadwick529738c2021-09-29 18:08:11 +0100382
383 // On any error immediately halt, either going to OtbnStateLocked or OtbnStateHalt depending on
384 // whether it was a fatal error.
Greg Chadwick987ee5a2021-10-25 11:59:57 +0100385 if (err) begin
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000386 prefetch_en_o = 1'b0;
387 insn_fetch_resp_clear_o = 1'b1;
388
Greg Chadwick987ee5a2021-10-25 11:59:57 +0100389 if (fatal_err) begin
390 state_d = OtbnStateLocked;
391 end else begin
392 state_d = OtbnStateHalt;
393 end
Greg Chadwick529738c2021-09-29 18:08:11 +0100394 end
395
396 // Regardless of what happens above enforce staying in OtnbStateLocked.
397 if (state_q == OtbnStateLocked) begin
398 state_d = OtbnStateLocked;
399 end
Philipp Wagner31441082020-07-14 11:17:21 +0100400 end
401
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000402 `ASSERT(InsnAlwaysValidInStall, state_q == OtbnStateStall |-> insn_valid_i)
403
Greg Chadwick9aef1c92021-01-25 18:24:58 +0000404 // Anything that moves us or keeps us in the stall state should cause `stall` to be asserted
Philipp Wagnerefa09012021-01-27 14:42:16 +0000405 `ASSERT(StallIfNextStateStall, insn_valid_i & (state_d == OtbnStateStall) |-> stall)
Greg Chadwick9aef1c92021-01-25 18:24:58 +0000406
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100407 assign insn_fetch_req_valid_o = err ? 1'b0 : insn_fetch_req_valid_raw;
408
409 // Determine if there are any errors related to the Imem fetch address.
410 always_comb begin
411 imem_addr_err = 1'b0;
412
413 if (insn_fetch_req_valid_raw) begin
414 if (|insn_fetch_req_addr_o[1:0]) begin
415 // Imem address is unaligned
416 imem_addr_err = 1'b1;
417 end else if (branch_taken) begin
418 imem_addr_err = branch_target_overflow;
419 end else begin
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000420 imem_addr_err = next_insn_addr_wide[ImemAddrWidth] & insn_valid_i;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100421 end
422 end
423 end
424
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100425 // Instruction is illegal based on the static properties of the instruction bits (illegal encoding
426 // or illegal WSR/CSR referenced).
427 assign illegal_insn_static = insn_illegal_i | ispr_err;
428
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000429 assign fatal_software_err = software_err & software_errs_fatal_i;
430 assign bad_internal_state_err = state_error;
431 assign reg_intg_violation_err = rf_bignum_rd_data_err;
432 assign key_invalid_err = ispr_rd_bignum_insn & insn_valid_i & key_invalid;
433 assign illegal_insn_err = illegal_insn_static | rf_indirect_err;
434 assign bad_data_addr_err = dmem_addr_err;
435 assign call_stack_err = rf_base_call_stack_err_i;
Greg Chadwick3e25bcd2021-10-25 12:05:36 +0100436
437 // All software errors that aren't bad_insn_addr. Factored into bad_insn_addr so it is only raised
438 // if other software errors haven't ocurred. As bad_insn_addr relates to the next instruction
439 // begin fetched it cannot occur if the current instruction has seen an error and failed to
440 // execute.
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000441 assign non_insn_addr_software_err = |{key_invalid_err,
442 loop_err,
443 illegal_insn_err,
444 call_stack_err,
445 bad_data_addr_err};
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100446
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000447 assign bad_insn_addr_err = imem_addr_err & ~non_insn_addr_software_err;
Greg Chadwick84e01ac2021-10-04 17:51:07 +0100448
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000449 assign err_bits_d = '{
450 fatal_software: fatal_software_err,
451 bad_internal_state: bad_internal_state_err,
452 reg_intg_violation: reg_intg_violation_err,
453 key_invalid: key_invalid_err,
454 loop: loop_err,
455 illegal_insn: illegal_insn_err,
456 call_stack: call_stack_err,
457 bad_data_addr: bad_data_addr_err,
458 bad_insn_addr: bad_insn_addr_err
459 };
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100460
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000461 always @(posedge clk_i or negedge rst_ni) begin
462 if (!rst_ni) begin
463 err_bits_q <= '0;
464 end else begin
465 if (start_i && !locked_o) begin
Vladimir Rozic09e25ac2021-11-25 00:19:38 +0000466 err_bits_q <= '0;
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000467 end else begin
468 err_bits_q <= err_bits_q | err_bits_d;
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100469 end
470 end
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100471 end
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000472 assign err_bits_o = err_bits_q;
473
474 assign software_err = non_insn_addr_software_err | bad_insn_addr_err;
475
476 assign fatal_err = |{fatal_software_err,
477 bad_internal_state_err,
478 reg_intg_violation_err,
Andreas Kurth13d76852022-04-04 14:55:54 +0200479 mubi4_test_true_loose(escalate_en_i)};
Rupert Swarbrick75885e62022-03-07 14:54:45 +0000480
481 assign recoverable_err_o = software_err & ~software_errs_fatal_i;
482 assign mems_sec_wipe_o = (state_d == OtbnStateLocked) & (state_q != OtbnStateLocked);
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100483
Greg Chadwick84e01ac2021-10-04 17:51:07 +0100484 assign err = software_err | fatal_err;
485
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000486 // Instructions must not execute if there is an error
487 assign insn_executing = insn_valid_i & ~err;
488
Andreas Kurth81f0c122022-04-01 19:19:47 +0200489 `ASSERT(ErrBitSetOnErr,
Andreas Kurth27e2b132022-04-04 15:03:27 +0200490 err & mubi4_test_false_strict(escalate_en_i) |=> err_bits_o)
Greg Chadwick79738062021-09-15 18:09:14 +0100491 `ASSERT(ErrSetOnFatalErr, fatal_err |-> err)
Greg Chadwick3e25bcd2021-10-25 12:05:36 +0100492 `ASSERT(SoftwareErrIfNonInsnAddrSoftwareErr, non_insn_addr_software_err |-> software_err)
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100493
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000494 `ASSERT(ControllerStateValid,
495 state_q inside {OtbnStateHalt, OtbnStateRun, OtbnStateStall, OtbnStateLocked})
Greg Chadwick51f36232020-09-02 15:37:23 +0100496 // Branch only takes effect in OtbnStateRun so must not go into stall state for branch
497 // instructions.
Philipp Wagnerdc946522020-12-03 10:52:58 +0000498 `ASSERT(NoStallOnBranch,
499 insn_valid_i & insn_dec_shared_i.branch_insn |-> state_q != OtbnStateStall)
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100500
Michael Schaffnerc5915742022-03-22 21:15:58 -0700501 `PRIM_FLOP_SPARSE_FSM(u_state_regs, state_d, state_q, otbn_state_e, OtbnStateHalt)
Greg Chadwick28836af2020-07-23 14:35:52 +0100502
Vladimir Rozic09e25ac2021-11-25 00:19:38 +0000503 assign insn_cnt_clear = state_reset_i | (state_q == OtbnStateLocked) | insn_cnt_clear_i;
504
Rupert Swarbrick3d1ca9e2021-10-25 11:18:02 +0100505 always_comb begin
Vladimir Rozic09e25ac2021-11-25 00:19:38 +0000506 if (insn_cnt_clear) begin
Rupert Swarbrick3d1ca9e2021-10-25 11:18:02 +0100507 insn_cnt_d = 32'd0;
508 end else if (insn_executing & ~stall & (insn_cnt_q != 32'hffffffff)) begin
509 insn_cnt_d = insn_cnt_q + 32'd1;
510 end else begin
511 insn_cnt_d = insn_cnt_q;
512 end
513 end
Vladimir Rozicb8db07a2021-05-24 14:15:04 +0100514
515 always_ff @(posedge clk_i or negedge rst_ni) begin
516 if (!rst_ni) begin
517 insn_cnt_q <= 32'd0;
Rupert Swarbrick3d1ca9e2021-10-25 11:18:02 +0100518 end else begin
Vladimir Rozicb8db07a2021-05-24 14:15:04 +0100519 insn_cnt_q <= insn_cnt_d;
520 end
521 end
522
Rupert Swarbrick3d1ca9e2021-10-25 11:18:02 +0100523 assign insn_cnt_o = insn_cnt_q;
524
Vladimir Rozic37c78bd2021-10-04 13:17:49 +0100525 assign loop_reset = state_reset_i | sec_wipe_zero_i;
526
Greg Chadwick53c95862020-10-14 17:58:38 +0100527 otbn_loop_controller #(
528 .ImemAddrWidth(ImemAddrWidth)
529 ) u_otbn_loop_controller (
530 .clk_i,
531 .rst_ni,
532
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000533 .state_reset_i(loop_reset),
Rupert Swarbrick13d50082021-07-13 14:14:03 +0100534
Greg Chadwickb5163fd2020-11-26 16:48:55 +0000535 .insn_valid_i,
Greg Chadwick53c95862020-10-14 17:58:38 +0100536 .insn_addr_i,
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000537 .next_insn_addr_i(next_insn_addr),
Greg Chadwick53c95862020-10-14 17:58:38 +0100538
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000539 .loop_start_req_i (loop_start_req),
540 .loop_start_commit_i(loop_start_commit),
541 .loop_bodysize_i (loop_bodysize),
542 .loop_iterations_i (loop_iterations),
Greg Chadwick53c95862020-10-14 17:58:38 +0100543
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000544 .loop_jump_o (loop_jump),
545 .loop_jump_addr_o(loop_jump_addr),
546 .loop_err_o (loop_err),
Greg Chadwickb5163fd2020-11-26 16:48:55 +0000547
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000548 .jump_or_branch_i(jump_or_branch),
549 .otbn_stall_i (stall),
Greg Chadwick0ac448a2021-11-18 17:10:58 +0000550
551 .prefetch_loop_active_o,
552 .prefetch_loop_iterations_o,
553 .prefetch_loop_end_addr_o,
554 .prefetch_loop_jump_addr_o
Greg Chadwick53c95862020-10-14 17:58:38 +0100555 );
556
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000557 // loop_start_req indicates the instruction wishes to start a loop, loop_start_commit confirms it
558 // should occur.
559 assign loop_start_req = insn_valid_i & insn_dec_shared_i.loop_insn;
560 assign loop_start_commit = insn_executing;
561 assign loop_bodysize = insn_dec_base_i.loop_bodysize;
Rupert Swarbrick514348e2021-02-03 09:04:59 +0000562 assign loop_iterations = insn_dec_base_i.loop_immediate ? insn_dec_base_i.i :
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100563 rf_base_rd_data_a_no_intg;
Greg Chadwick53c95862020-10-14 17:58:38 +0100564
Greg Chadwickae8e6452020-10-02 12:04:15 +0100565 // Compute increments which can be optionally applied to indirect register accesses and memory
566 // addresses in BN.LID/BN.SID/BN.MOVR instructions.
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100567 assign rf_base_rd_data_a_inc = rf_base_rd_data_a_no_intg[4:0] + 1'b1;
568 assign rf_base_rd_data_b_inc = rf_base_rd_data_b_no_intg[4:0] + 1'b1;
Rupert Swarbrick9a4f47b2021-01-04 15:48:24 +0000569 // We can avoid a full 32-bit adder here because the offset is 32-bit aligned, so we know the
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100570 // load/store address will only be valid if rf_base_rd_data_a_no_intg[4:0] is zero.
571 assign rf_base_rd_data_a_wlen_word_inc = rf_base_rd_data_a_no_intg[31:5] + 27'h1;
Greg Chadwickae8e6452020-10-02 12:04:15 +0100572
573 // Choose increment to write back to base register file, only one increment can be written as
574 // there is only one write port. Note that where an instruction is incrementing the indirect
575 // reference to its destination register (insn_dec_bignum_i.d_inc) that reference is read on the
576 // B read port so the B increment is written back.
577 always_comb begin
578 unique case (1'b1)
579 insn_dec_bignum_i.a_inc: begin
Greg Chadwick496fd342021-03-05 18:08:39 +0000580 increment_out = {26'b0, rf_base_rd_data_a_inc};
Greg Chadwickae8e6452020-10-02 12:04:15 +0100581 end
582 insn_dec_bignum_i.b_inc: begin
Greg Chadwick496fd342021-03-05 18:08:39 +0000583 increment_out = {26'b0, rf_base_rd_data_b_inc};
Greg Chadwickae8e6452020-10-02 12:04:15 +0100584 end
585 insn_dec_bignum_i.d_inc: begin
Greg Chadwick496fd342021-03-05 18:08:39 +0000586 increment_out = {26'b0, rf_base_rd_data_b_inc};
Greg Chadwickae8e6452020-10-02 12:04:15 +0100587 end
588 insn_dec_bignum_i.a_wlen_word_inc: begin
Rupert Swarbrick9a4f47b2021-01-04 15:48:24 +0000589 increment_out = {rf_base_rd_data_a_wlen_word_inc, 5'b0};
Greg Chadwickae8e6452020-10-02 12:04:15 +0100590 end
Pirmin Vogele97cac02020-11-02 12:28:50 +0100591 default: begin
592 // Whenever increment_out is written back to the register file, exactly one of the
593 // increment selector signals is high. To prevent the automatic inference of latches in
594 // case nothing is written back (rf_wdata_sel != RfWdSelIncr) and to save logic, we choose
595 // a valid output as default.
Greg Chadwick496fd342021-03-05 18:08:39 +0000596 increment_out = {26'b0, rf_base_rd_data_a_inc};
Pirmin Vogele97cac02020-11-02 12:28:50 +0100597 end
Greg Chadwickae8e6452020-10-02 12:04:15 +0100598 endcase
599 end
600
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100601 // Base RF read/write address, enable and commit control
Greg Chadwickae8e6452020-10-02 12:04:15 +0100602 always_comb begin
603 rf_base_rd_addr_a_o = insn_dec_base_i.a;
604 rf_base_rd_addr_b_o = insn_dec_base_i.b;
605 rf_base_wr_addr_o = insn_dec_base_i.d;
Greg Chadwicke6e8f152021-03-05 13:35:36 +0000606
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100607 // Only commit read or write if the instruction is executing (in particular a read commit pops
608 // the call stack so must not occur where a valid instruction sees an error and doesn't
609 // execute).
610 rf_base_rd_commit_o = insn_executing;
611 rf_base_wr_commit_o = insn_executing;
Greg Chadwick9f5b6382021-05-10 17:53:46 +0100612
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100613 rf_base_rd_en_a_raw = 1'b0;
614 rf_base_rd_en_b_raw = 1'b0;
615 rf_base_wr_en_raw = 1'b0;
Greg Chadwick9f5b6382021-05-10 17:53:46 +0100616
617 if (insn_valid_i) begin
Rupert Swarbrick46e11ba2021-07-13 12:11:43 +0100618 if (insn_dec_shared_i.st_insn) begin
619 // For stores, both base reads happen in the same cycle as the request because they give the
620 // address and data, which make up the request.
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100621 rf_base_rd_en_a_raw = insn_dec_base_i.rf_ren_a & lsu_store_req_raw;
622 rf_base_rd_en_b_raw = insn_dec_base_i.rf_ren_b & lsu_store_req_raw;
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100623
624 // Bignum stores can update the base register file where an increment is used.
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100625 rf_base_wr_en_raw = (insn_dec_shared_i.subset == InsnSubsetBignum) &
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100626 insn_dec_base_i.rf_we &
627 lsu_store_req_raw;
Rupert Swarbrick46e11ba2021-07-13 12:11:43 +0100628 end else if (insn_dec_shared_i.ld_insn) begin
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100629 // For loads, both base reads happen in the same cycle as the request. The address is
630 // required for the request and the indirect destination register (only used for Bignum
631 // loads) is flopped in ld_insn_bignum_wr_addr_q to correctly deal with the case where it's
632 // updated by an increment.
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100633 rf_base_rd_en_a_raw = insn_dec_base_i.rf_ren_a & lsu_load_req_raw;
634 rf_base_rd_en_b_raw = insn_dec_base_i.rf_ren_b & lsu_load_req_raw;
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100635
636 if (insn_dec_shared_i.subset == InsnSubsetBignum) begin
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100637 // Bignum loads can update the base register file where an increment is used. This must
638 // always happen in the same cycle as the request as this is where both registers are
639 // read.
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100640 rf_base_wr_en_raw = insn_dec_base_i.rf_we & lsu_load_req_raw;
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100641 end else begin
642 // For Base loads write the base register file when the instruction is unstalled (meaning
643 // the load data is available).
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100644 rf_base_wr_en_raw = insn_dec_base_i.rf_we & ~stall;
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100645 end
Greg Chadwick9f5b6382021-05-10 17:53:46 +0100646 end else begin
Greg Chadwick5c8579c2021-08-04 14:38:33 +0100647 // For all other instructions the read and write happen when the instruction is unstalled.
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100648 rf_base_rd_en_a_raw = insn_dec_base_i.rf_ren_a & ~stall;
649 rf_base_rd_en_b_raw = insn_dec_base_i.rf_ren_b & ~stall;
650 rf_base_wr_en_raw = insn_dec_base_i.rf_we & ~stall;
Greg Chadwick9f5b6382021-05-10 17:53:46 +0100651 end
Greg Chadwicke6e8f152021-03-05 13:35:36 +0000652 end
Greg Chadwickae8e6452020-10-02 12:04:15 +0100653
654 if (insn_dec_shared_i.subset == InsnSubsetBignum) begin
655 unique case (1'b1)
656 insn_dec_bignum_i.a_inc,
657 insn_dec_bignum_i.a_wlen_word_inc: begin
658 rf_base_wr_addr_o = insn_dec_base_i.a;
659 end
660
661 insn_dec_bignum_i.b_inc,
662 insn_dec_bignum_i.d_inc: begin
663 rf_base_wr_addr_o = insn_dec_base_i.b;
664 end
665 default: ;
666 endcase
667 end
Greg Chadwick1a1c0112021-09-14 16:27:31 +0100668
669 rf_base_rd_en_a_o = rf_base_rd_en_a_raw & ~illegal_insn_static;
670 rf_base_rd_en_b_o = rf_base_rd_en_b_raw & ~illegal_insn_static;
671 rf_base_wr_en_o = rf_base_wr_en_raw & ~illegal_insn_static;
Greg Chadwickae8e6452020-10-02 12:04:15 +0100672 end
Philipp Wagner31441082020-07-14 11:17:21 +0100673
674 // Base ALU Operand A MUX
675 always_comb begin
Greg Chadwickcf048242020-10-02 15:28:42 +0100676 unique case (insn_dec_base_i.op_a_sel)
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100677 OpASelRegister: alu_base_operation_o.operand_a = rf_base_rd_data_a_no_intg;
Greg Chadwick9e858fb2020-10-12 17:39:16 +0100678 OpASelZero: alu_base_operation_o.operand_a = '0;
679 OpASelCurrPc: alu_base_operation_o.operand_a = {{(32 - ImemAddrWidth){1'b0}}, insn_addr_i};
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100680 default: alu_base_operation_o.operand_a = rf_base_rd_data_a_no_intg;
Philipp Wagner31441082020-07-14 11:17:21 +0100681 endcase
682 end
683
684 // Base ALU Operand B MUX
685 always_comb begin
Greg Chadwickcf048242020-10-02 15:28:42 +0100686 unique case (insn_dec_base_i.op_b_sel)
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100687 OpBSelRegister: alu_base_operation_o.operand_b = rf_base_rd_data_b_no_intg;
Greg Chadwick9e858fb2020-10-12 17:39:16 +0100688 OpBSelImmediate: alu_base_operation_o.operand_b = insn_dec_base_i.i;
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100689 default: alu_base_operation_o.operand_b = rf_base_rd_data_b_no_intg;
Philipp Wagner31441082020-07-14 11:17:21 +0100690 endcase
691 end
692
Greg Chadwicke177f172020-09-09 14:46:03 +0100693 assign alu_base_operation_o.op = insn_dec_base_i.alu_op;
Philipp Wagner31441082020-07-14 11:17:21 +0100694
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100695 assign alu_base_comparison_o.operand_a = rf_base_rd_data_a_no_intg;
696 assign alu_base_comparison_o.operand_b = rf_base_rd_data_b_no_intg;
Greg Chadwicke177f172020-09-09 14:46:03 +0100697 assign alu_base_comparison_o.op = insn_dec_base_i.comparison_op;
Greg Chadwick9791eed2020-07-22 18:08:28 +0100698
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100699 assign rf_base_rd_data_a_no_intg = rf_base_rd_data_a_intg_i[31:0];
700 assign rf_base_rd_data_b_no_intg = rf_base_rd_data_b_intg_i[31:0];
701
702 // TODO: For now integrity bits from RF base are ignored in the controller, remove this when end
703 // to end integrity features that use them are implemented
704 logic unused_rf_base_rd_a_intg_bits;
705 logic unused_rf_base_rd_b_intg_bits;
706
707 assign unused_rf_base_rd_a_intg_bits = |rf_base_rd_data_a_intg_i[38:32];
708 assign unused_rf_base_rd_b_intg_bits = |rf_base_rd_data_b_intg_i[38:32];
709
Philipp Wagner31441082020-07-14 11:17:21 +0100710 // Register file write MUX
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100711 always_comb begin
Greg Chadwickee060bd2021-05-15 17:33:22 +0100712 // Write data mux for anything that needs integrity computing during register write
Greg Chadwickcf048242020-10-02 15:28:42 +0100713 unique case (insn_dec_base_i.rf_wdata_sel)
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100714 RfWdSelEx: rf_base_wr_data_no_intg_o = alu_base_operation_result_i;
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100715 RfWdSelNextPc: rf_base_wr_data_no_intg_o = {{(32-(ImemAddrWidth+1)){1'b0}},
716 next_insn_addr_wide};
717 RfWdSelIspr: rf_base_wr_data_no_intg_o = csr_rdata;
718 RfWdSelIncr: rf_base_wr_data_no_intg_o = increment_out;
719 default: rf_base_wr_data_no_intg_o = alu_base_operation_result_i;
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100720 endcase
Greg Chadwickc8cd4352020-08-14 16:45:23 +0100721
Greg Chadwickee060bd2021-05-15 17:33:22 +0100722 // Write data mux for anything that provides its own integrity
723 unique case (insn_dec_base_i.rf_wdata_sel)
724 RfWdSelLsu: begin
725 rf_base_wr_data_intg_sel_o = 1'b1;
726 rf_base_wr_data_intg_o = lsu_base_rdata_i;
727 end
728 default: begin
729 rf_base_wr_data_intg_sel_o = 1'b0;
730 rf_base_wr_data_intg_o = '0;
731 end
732 endcase
733 end
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100734
735 for (genvar i = 0; i < BaseWordsPerWLEN; ++i) begin : g_rf_bignum_rd_data
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000736 assign rf_bignum_rd_data_a_no_intg[i*32+:32] = rf_bignum_rd_data_a_intg_i[i*39+:32];
737 assign rf_bignum_rd_data_b_no_intg[i*32+:32] = rf_bignum_rd_data_b_intg_i[i*39+:32];
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100738 end
739
740 assign rf_bignum_rd_addr_a_o = insn_dec_bignum_i.rf_a_indirect ? rf_base_rd_data_a_no_intg[4:0] :
Greg Chadwickae8e6452020-10-02 12:04:15 +0100741 insn_dec_bignum_i.a;
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100742 assign rf_bignum_rd_en_a_o = insn_dec_bignum_i.rf_ren_a & insn_valid_i;
Greg Chadwickae8e6452020-10-02 12:04:15 +0100743
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100744 assign rf_bignum_rd_addr_b_o = insn_dec_bignum_i.rf_b_indirect ? rf_base_rd_data_b_no_intg[4:0] :
Greg Chadwickae8e6452020-10-02 12:04:15 +0100745 insn_dec_bignum_i.b;
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100746 assign rf_bignum_rd_en_b_o = insn_dec_bignum_i.rf_ren_b & insn_valid_i;
Greg Chadwickf7863442020-09-14 18:11:33 +0100747
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100748 assign alu_bignum_operation_o.operand_a = rf_bignum_rd_data_a_no_intg;
Greg Chadwickf7863442020-09-14 18:11:33 +0100749
750 // Base ALU Operand B MUX
751 always_comb begin
Greg Chadwick94786452020-10-28 18:19:51 +0000752 unique case (insn_dec_bignum_i.alu_op_b_sel)
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100753 OpBSelRegister: alu_bignum_operation_o.operand_b = rf_bignum_rd_data_b_no_intg;
Greg Chadwick9e858fb2020-10-12 17:39:16 +0100754 OpBSelImmediate: alu_bignum_operation_o.operand_b = insn_dec_bignum_i.i;
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100755 default: alu_bignum_operation_o.operand_b = rf_bignum_rd_data_b_no_intg;
Greg Chadwickf7863442020-09-14 18:11:33 +0100756 endcase
757 end
758
759 assign alu_bignum_operation_o.op = insn_dec_bignum_i.alu_op;
Greg Chadwick94786452020-10-28 18:19:51 +0000760 assign alu_bignum_operation_o.shift_right = insn_dec_bignum_i.alu_shift_right;
761 assign alu_bignum_operation_o.shift_amt = insn_dec_bignum_i.alu_shift_amt;
762 assign alu_bignum_operation_o.flag_group = insn_dec_bignum_i.alu_flag_group;
763 assign alu_bignum_operation_o.sel_flag = insn_dec_bignum_i.alu_sel_flag;
Greg Chadwick96fe7052021-03-18 15:15:05 +0000764 assign alu_bignum_operation_o.alu_flag_en = insn_dec_bignum_i.alu_flag_en & insn_executing;
765 assign alu_bignum_operation_o.mac_flag_en = insn_dec_bignum_i.mac_flag_en & insn_executing;
Greg Chadwickf7863442020-09-14 18:11:33 +0100766
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100767 assign mac_bignum_operation_o.operand_a = rf_bignum_rd_data_a_no_intg;
768 assign mac_bignum_operation_o.operand_b = rf_bignum_rd_data_b_no_intg;
Greg Chadwick94786452020-10-28 18:19:51 +0000769 assign mac_bignum_operation_o.operand_a_qw_sel = insn_dec_bignum_i.mac_op_a_qw_sel;
770 assign mac_bignum_operation_o.operand_b_qw_sel = insn_dec_bignum_i.mac_op_b_qw_sel;
Rupert Swarbrick8e016022020-11-19 16:59:02 +0000771 assign mac_bignum_operation_o.wr_hw_sel_upper = insn_dec_bignum_i.mac_wr_hw_sel_upper;
Greg Chadwick94786452020-10-28 18:19:51 +0000772 assign mac_bignum_operation_o.pre_acc_shift_imm = insn_dec_bignum_i.mac_pre_acc_shift;
773 assign mac_bignum_operation_o.zero_acc = insn_dec_bignum_i.mac_zero_acc;
774 assign mac_bignum_operation_o.shift_acc = insn_dec_bignum_i.mac_shift_out;
775
Greg Chadwick42a9f3b2021-01-28 13:54:14 +0000776 assign mac_bignum_en_o = insn_executing & insn_dec_bignum_i.mac_en;
Greg Chadwick94786452020-10-28 18:19:51 +0000777
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100778 // Move / Conditional Select. Only select B register data when a selection instruction is being
779 // executed and the selection flag isn't set.
780
781 `ASSERT(SelFlagValid, insn_valid_i & insn_dec_bignum_i.sel_insn |->
782 insn_dec_bignum_i.alu_sel_flag inside {FlagC, FlagL, FlagM, FlagZ})
783
784 assign selection_result =
785 ~insn_dec_bignum_i.sel_insn | alu_bignum_selection_flag_i ? rf_bignum_rd_data_a_intg_i :
786 rf_bignum_rd_data_b_intg_i;
Greg Chadwick94786452020-10-28 18:19:51 +0000787
788 // Bignum Register file write control
789
790 always_comb begin
791 // By default write nothing
792 rf_bignum_wr_en_o = 2'b00;
793
Greg Chadwicke6e8f152021-03-05 13:35:36 +0000794 // Only write if executing instruction wants a bignum rf write and it isn't stalled and there is
795 // no error
796 if (insn_executing && insn_dec_bignum_i.rf_we && !err && !stall) begin
Greg Chadwick94786452020-10-28 18:19:51 +0000797 if (insn_dec_bignum_i.mac_en && insn_dec_bignum_i.mac_shift_out) begin
798 // Special handling for BN.MULQACC.SO, only enable upper or lower half depending on
Rupert Swarbrick8e016022020-11-19 16:59:02 +0000799 // mac_wr_hw_sel_upper.
800 rf_bignum_wr_en_o = insn_dec_bignum_i.mac_wr_hw_sel_upper ? 2'b10 : 2'b01;
Greg Chadwick94786452020-10-28 18:19:51 +0000801 end else begin
802 // For everything else write both halves immediately.
803 rf_bignum_wr_en_o = 2'b11;
804 end
805 end
806 end
Greg Chadwickf7863442020-09-14 18:11:33 +0100807
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100808 // For BN.LID sample the indirect destination register index in first cycle as an increment might
809 // change it for the second cycle where the load data is written to the bignum register file.
810 always_ff @(posedge clk_i) begin
811 if (insn_dec_bignum_i.rf_d_indirect & lsu_load_req_raw) begin
812 ld_insn_bignum_wr_addr_q <= rf_base_rd_data_b_no_intg[4:0];
813 end
814 end
Greg Chadwickf7863442020-09-14 18:11:33 +0100815
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100816 always_comb begin
817 rf_bignum_wr_addr_o = insn_dec_bignum_i.d;
818
819 if (insn_dec_bignum_i.rf_d_indirect) begin
820 if (insn_dec_shared_i.ld_insn) begin
821 // Use sampled register index from first cycle of the load (in case the increment has
822 // changed the value in the mean-time).
823 rf_bignum_wr_addr_o = ld_insn_bignum_wr_addr_q;
824 end else begin
825 // Use read register index directly
826 rf_bignum_wr_addr_o = rf_base_rd_data_b_no_intg[4:0];
827 end
828 end
829 end
Greg Chadwick496fd342021-03-05 18:08:39 +0000830
Greg Chadwick94786452020-10-28 18:19:51 +0000831 // For the shift-out variant of BN.MULQACC the bottom half of the MAC result is written to one
Philipp Wagnerdc946522020-12-03 10:52:58 +0000832 // half of a desintation register specified by the instruction (mac_wr_hw_sel_upper). The bottom
833 // half of the MAC result must be placed in the appropriate half of the write data (the RF only
834 // accepts write data for the top half in the top half of the write data input). Otherwise
835 // (shift-out to bottom half and all other BN.MULQACC instructions) simply pass the MAC result
836 // through unchanged as write data.
Greg Chadwick94786452020-10-28 18:19:51 +0000837 assign mac_bignum_rf_wr_data[WLEN-1:WLEN/2] =
Philipp Wagnerdc946522020-12-03 10:52:58 +0000838 insn_dec_bignum_i.mac_wr_hw_sel_upper &&
839 insn_dec_bignum_i.mac_shift_out ? mac_bignum_operation_result_i[WLEN/2-1:0] :
840 mac_bignum_operation_result_i[WLEN-1:WLEN/2];
Greg Chadwick94786452020-10-28 18:19:51 +0000841
842 assign mac_bignum_rf_wr_data[WLEN/2-1:0] = mac_bignum_operation_result_i[WLEN/2-1:0];
843
Greg Chadwickf7863442020-09-14 18:11:33 +0100844 always_comb begin
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100845 // Write data mux for anything that needs integrity computing during register write
Greg Chadwickee060bd2021-05-15 17:33:22 +0100846 // TODO: ISPR data will go via direct mux below once integrity has been implemented for
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100847 // them.
Greg Chadwickcf048242020-10-02 15:28:42 +0100848 unique case (insn_dec_bignum_i.rf_wdata_sel)
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100849 RfWdSelEx: rf_bignum_wr_data_no_intg_o = alu_bignum_operation_result_i;
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100850 RfWdSelIspr: rf_bignum_wr_data_no_intg_o = ispr_rdata_i;
851 RfWdSelMac: rf_bignum_wr_data_no_intg_o = mac_bignum_rf_wr_data;
852 default: rf_bignum_wr_data_no_intg_o = alu_bignum_operation_result_i;
853 endcase
854
855 // Write data mux for anything that provides its own integrity
856 unique case (insn_dec_bignum_i.rf_wdata_sel)
857 RfWdSelMovSel: begin
858 rf_bignum_wr_data_intg_sel_o = 1'b1;
859 rf_bignum_wr_data_intg_o = selection_result;
860 end
Greg Chadwickee060bd2021-05-15 17:33:22 +0100861 RfWdSelLsu: begin
862 rf_bignum_wr_data_intg_sel_o = 1'b1;
863 rf_bignum_wr_data_intg_o = lsu_bignum_rdata_i;
864 end
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100865 default: begin
866 rf_bignum_wr_data_intg_sel_o = 1'b0;
867 rf_bignum_wr_data_intg_o = '0;
868 end
Greg Chadwickf7863442020-09-14 18:11:33 +0100869 endcase
870 end
871
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100872 assign rf_a_indirect_err = insn_dec_bignum_i.rf_a_indirect &
873 (|rf_base_rd_data_a_no_intg[31:5]) &
Rupert Swarbrickdb4b4942022-01-10 12:35:14 +0000874 ~rf_base_call_stack_err_i &
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100875 rf_base_rd_en_a_o;
876
877 assign rf_b_indirect_err = insn_dec_bignum_i.rf_b_indirect &
878 (|rf_base_rd_data_b_no_intg[31:5]) &
Rupert Swarbrickdb4b4942022-01-10 12:35:14 +0000879 ~rf_base_call_stack_err_i &
Greg Chadwick9a6fc462021-08-10 15:42:16 +0100880 rf_base_rd_en_b_o;
881
882 assign rf_d_indirect_err = insn_dec_bignum_i.rf_d_indirect &
883 (|rf_base_rd_data_b_no_intg[31:5]) &
884 rf_base_rd_en_b_o;
Greg Chadwick496fd342021-03-05 18:08:39 +0000885
886 assign rf_indirect_err =
887 insn_valid_i & (rf_a_indirect_err | rf_b_indirect_err | rf_d_indirect_err);
888
Rupert Swarbrickdb4b4942022-01-10 12:35:14 +0000889 assign ignore_bignum_rd_errs = (insn_dec_bignum_i.rf_a_indirect |
890 insn_dec_bignum_i.rf_b_indirect) &
891 rf_base_call_stack_err_i;
892
893 assign rf_bignum_rd_data_err = rf_bignum_rd_data_err_i & ~ignore_bignum_rd_errs;
894
Greg Chadwickf7863442020-09-14 18:11:33 +0100895 // CSR/WSR/ISPR handling
896 // ISPRs (Internal Special Purpose Registers) are the internal registers. CSRs and WSRs are the
897 // ISA visible versions of those registers in the base and bignum ISAs respectively.
898
Philipp Wagner711d2262021-01-21 18:17:42 +0000899 assign csr_addr = csr_e'(insn_dec_base_i.i[11:0]);
900 assign csr_sub_addr = insn_dec_base_i.i[$clog2(BaseWordsPerWLEN)-1:0];
Greg Chadwickf7863442020-09-14 18:11:33 +0100901
902 always_comb begin
903 ispr_addr_base = IsprMod;
904 ispr_word_addr_base = '0;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100905 csr_illegal_addr = 1'b0;
Greg Chadwickf7863442020-09-14 18:11:33 +0100906
907 unique case (csr_addr)
Rupert Swarbrickb9a1d312021-12-16 10:51:07 +0000908 CsrFlags, CsrFg0, CsrFg1: begin
Greg Chadwickf7863442020-09-14 18:11:33 +0100909 ispr_addr_base = IsprFlags;
910 ispr_word_addr_base = '0;
911 end
Philipp Wagner711d2262021-01-21 18:17:42 +0000912 CsrMod0, CsrMod1, CsrMod2, CsrMod3, CsrMod4, CsrMod5, CsrMod6, CsrMod7: begin
Greg Chadwickf7863442020-09-14 18:11:33 +0100913 ispr_addr_base = IsprMod;
Philipp Wagner711d2262021-01-21 18:17:42 +0000914 ispr_word_addr_base = csr_sub_addr;
Greg Chadwickf7863442020-09-14 18:11:33 +0100915 end
Greg Chadwickb168ae92021-04-14 16:04:03 +0100916 CsrRndPrefetch: begin
917 // Reading from RND_PREFETCH results in 0, there is no ISPR to read so no address is set.
918 // The csr_rdata mux logic takes care of producing the 0.
919 end
Philipp Wagner93877522021-07-16 10:49:25 +0100920 CsrRnd: begin
921 ispr_addr_base = IsprRnd;
922 ispr_word_addr_base = '0;
923 end
Greg Chadwickb168ae92021-04-14 16:04:03 +0100924 CsrUrnd: begin
925 ispr_addr_base = IsprUrnd;
926 ispr_word_addr_base = '0;
927 end
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100928 default: csr_illegal_addr = 1'b1;
Greg Chadwickf7863442020-09-14 18:11:33 +0100929 endcase
930 end
931
932 for (genvar i_word = 0; i_word < BaseWordsPerWLEN; i_word++) begin : g_ispr_word_sel_base
933 assign ispr_word_sel_base[i_word] = ispr_word_addr_base == i_word;
934 end
935
936 for (genvar i_bit = 0; i_bit < 32; i_bit++) begin : g_csr_rdata_mux
937 for (genvar i_word = 0; i_word < BaseWordsPerWLEN; i_word++) begin : g_csr_rdata_mux_inner
Philipp Wagnerdc946522020-12-03 10:52:58 +0000938 assign csr_rdata_mux[i_bit][i_word] =
939 ispr_rdata_i[i_word*32 + i_bit] & ispr_word_sel_base[i_word];
Greg Chadwickf7863442020-09-14 18:11:33 +0100940 end
941
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000942 assign csr_rdata_raw[i_bit] = |csr_rdata_mux[i_bit];
Greg Chadwickf7863442020-09-14 18:11:33 +0100943 end
944
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000945 // Specialised read data handling for CSR reads where raw read data needs modification.
946 always_comb begin
947 csr_rdata = csr_rdata_raw;
948
Greg Chadwickb168ae92021-04-14 16:04:03 +0100949 unique case (csr_addr)
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000950 // For FG0/FG1 select out appropriate bits from FLAGS ISPR and pad the rest with zeros.
Greg Chadwickb168ae92021-04-14 16:04:03 +0100951 CsrFg0: csr_rdata = {28'b0, csr_rdata_raw[3:0]};
952 CsrFg1: csr_rdata = {28'b0, csr_rdata_raw[7:4]};
953 CsrRndPrefetch: csr_rdata = '0;
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000954 default: ;
955 endcase
956 end
957
Greg Chadwick009d9ee2021-04-26 16:25:51 +0100958 assign csr_wdata_raw = insn_dec_shared_i.ispr_rs_insn ? csr_rdata | rf_base_rd_data_a_no_intg :
959 rf_base_rd_data_a_no_intg;
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000960
961 // Specialised write data handling for CSR writes where raw write data needs modification.
962 always_comb begin
963 csr_wdata = csr_wdata_raw;
964
Greg Chadwickb168ae92021-04-14 16:04:03 +0100965 unique case (csr_addr)
Greg Chadwickdbd655a2020-11-24 16:42:06 +0000966 // For FG0/FG1 only modify relevant part of FLAGS ISPR.
967 CsrFg0: csr_wdata = {24'b0, csr_rdata_raw[7:4], csr_wdata_raw[3:0]};
968 CsrFg1: csr_wdata = {24'b0, csr_wdata_raw[3:0], csr_rdata_raw[3:0]};
969 default: ;
970 endcase
971 end
Greg Chadwickf7863442020-09-14 18:11:33 +0100972
Rupert Swarbricka2c05e72020-11-20 08:46:25 +0000973 // ISPR RS (read and set) must not be combined with ISPR RD or WR (read or write). ISPR RD and
974 // WR (read and write) is allowed.
975 `ASSERT(NoIsprRorWAndRs, insn_valid_i |-> ~(insn_dec_shared_i.ispr_rs_insn &
976 (insn_dec_shared_i.ispr_rd_insn |
977 insn_dec_shared_i.ispr_wr_insn)))
978
979
Greg Chadwickf7863442020-09-14 18:11:33 +0100980 assign wsr_addr = wsr_e'(insn_dec_bignum_i.i[WsrNumWidth-1:0]);
981
982 always_comb begin
983 ispr_addr_bignum = IsprMod;
Greg Chadwickd3154ec2020-09-24 12:03:23 +0100984 wsr_illegal_addr = 1'b0;
Greg Chadwick4ca1caa2021-11-23 10:56:01 +0000985 key_invalid = 1'b0;
Greg Chadwickf7863442020-09-14 18:11:33 +0100986
987 unique case (wsr_addr)
Greg Chadwickb168ae92021-04-14 16:04:03 +0100988 WsrMod: ispr_addr_bignum = IsprMod;
989 WsrRnd: ispr_addr_bignum = IsprRnd;
Greg Chadwickb168ae92021-04-14 16:04:03 +0100990 WsrUrnd: ispr_addr_bignum = IsprUrnd;
Philipp Wagner19afa992021-07-16 10:56:23 +0100991 WsrAcc: ispr_addr_bignum = IsprAcc;
Greg Chadwick4ca1caa2021-11-23 10:56:01 +0000992 WsrKeyS0L: begin
993 ispr_addr_bignum = IsprKeyS0L;
994 key_invalid = ~sideload_key_shares_valid_i[0];
995 end
996 WsrKeyS0H: begin
997 ispr_addr_bignum = IsprKeyS0H;
998 key_invalid = ~sideload_key_shares_valid_i[0];
999 end
1000 WsrKeyS1L: begin
1001 ispr_addr_bignum = IsprKeyS1L;
1002 key_invalid = ~sideload_key_shares_valid_i[1];
1003 end
1004 WsrKeyS1H: begin
1005 ispr_addr_bignum = IsprKeyS1H;
1006 key_invalid = ~sideload_key_shares_valid_i[1];
1007 end
Greg Chadwickd3154ec2020-09-24 12:03:23 +01001008 default: wsr_illegal_addr = 1'b1;
Greg Chadwickf7863442020-09-14 18:11:33 +01001009 endcase
1010 end
1011
Greg Chadwick009d9ee2021-04-26 16:25:51 +01001012 assign wsr_wdata = insn_dec_shared_i.ispr_rs_insn ? ispr_rdata_i | rf_bignum_rd_data_a_no_intg :
1013 rf_bignum_rd_data_a_no_intg;
Greg Chadwickf7863442020-09-14 18:11:33 +01001014
Philipp Wagner711d2262021-01-21 18:17:42 +00001015 assign ispr_illegal_addr = insn_dec_shared_i.subset == InsnSubsetBase ? csr_illegal_addr :
1016 wsr_illegal_addr;
Greg Chadwickd3154ec2020-09-24 12:03:23 +01001017
1018 assign ispr_err = ispr_illegal_addr & insn_valid_i & (insn_dec_shared_i.ispr_rd_insn |
1019 insn_dec_shared_i.ispr_wr_insn |
1020 insn_dec_shared_i.ispr_rs_insn);
1021
Rupert Swarbricka2c05e72020-11-20 08:46:25 +00001022 assign ispr_wr_insn = insn_dec_shared_i.ispr_wr_insn | insn_dec_shared_i.ispr_rs_insn;
Greg Chadwickb168ae92021-04-14 16:04:03 +01001023 assign ispr_rd_insn = insn_dec_shared_i.ispr_rd_insn | insn_dec_shared_i.ispr_rs_insn;
1024
1025 // Write to RND_PREFETCH must not produce ISR write
1026 assign ispr_wr_base_insn =
1027 ispr_wr_insn & (insn_dec_shared_i.subset == InsnSubsetBase) & (csr_addr != CsrRndPrefetch);
1028
Rupert Swarbrick514348e2021-02-03 09:04:59 +00001029 assign ispr_wr_bignum_insn = ispr_wr_insn & (insn_dec_shared_i.subset == InsnSubsetBignum);
Prajwala Puttappa175c0d82021-12-17 11:23:16 +00001030 assign ispr_rd_bignum_insn = ispr_rd_insn & (insn_dec_shared_i.subset == InsnSubsetBignum);
Greg Chadwickf7863442020-09-14 18:11:33 +01001031
Philipp Wagnerdc946522020-12-03 10:52:58 +00001032 assign ispr_addr_o = insn_dec_shared_i.subset == InsnSubsetBase ? ispr_addr_base :
1033 ispr_addr_bignum;
Greg Chadwickf7863442020-09-14 18:11:33 +01001034 assign ispr_base_wdata_o = csr_wdata;
Rupert Swarbrick514348e2021-02-03 09:04:59 +00001035 assign ispr_base_wr_en_o = {BaseWordsPerWLEN{ispr_wr_base_insn & insn_executing}} &
1036 ispr_word_sel_base;
Greg Chadwickf6f35962020-11-02 17:32:08 +00001037
Greg Chadwickf7863442020-09-14 18:11:33 +01001038 assign ispr_bignum_wdata_o = wsr_wdata;
Rupert Swarbrick514348e2021-02-03 09:04:59 +00001039 assign ispr_bignum_wr_en_o = ispr_wr_bignum_insn & insn_executing;
Greg Chadwickf7863442020-09-14 18:11:33 +01001040
Greg Chadwick42a9f3b2021-01-28 13:54:14 +00001041 // lsu_load_req_raw/lsu_store_req_raw indicate an instruction wishes to perform a store or a load.
Rupert Swarbrick514348e2021-02-03 09:04:59 +00001042 // lsu_load_req_o/lsu_store_req_o factor in whether an instruction is actually executing (it may
1043 // be suppressed due an error) and command the load or store to happen when asserted.
Greg Chadwick42a9f3b2021-01-28 13:54:14 +00001044 assign lsu_load_req_raw = insn_valid_i & insn_dec_shared_i.ld_insn & (state_q == OtbnStateRun);
1045 assign lsu_load_req_o = insn_executing & lsu_load_req_raw;
1046
1047 assign lsu_store_req_raw = insn_valid_i & insn_dec_shared_i.st_insn & (state_q == OtbnStateRun);
1048 assign lsu_store_req_o = insn_executing & lsu_store_req_raw;
1049
Greg Chadwickf7863442020-09-14 18:11:33 +01001050 assign lsu_req_subset_o = insn_dec_shared_i.subset;
Greg Chadwickc8cd4352020-08-14 16:45:23 +01001051
Greg Chadwickae8e6452020-10-02 12:04:15 +01001052 assign lsu_addr_o = alu_base_operation_result_i[DmemAddrWidth-1:0];
Greg Chadwickee060bd2021-05-15 17:33:22 +01001053 assign lsu_base_wdata_o = rf_base_rd_data_b_intg_i;
1054 assign lsu_bignum_wdata_o = rf_bignum_rd_data_b_intg_i;
Greg Chadwick6ab8d952020-10-30 12:13:34 +00001055
Philipp Wagner711d2262021-01-21 18:17:42 +00001056 assign dmem_addr_unaligned_bignum =
1057 (lsu_req_subset_o == InsnSubsetBignum) & (|lsu_addr_o[$clog2(WLEN/8)-1:0]);
1058 assign dmem_addr_unaligned_base =
1059 (lsu_req_subset_o == InsnSubsetBase) & (|lsu_addr_o[1:0]);
Greg Chadwickd3154ec2020-09-24 12:03:23 +01001060 assign dmem_addr_overflow = |alu_base_operation_result_i[31:DmemAddrWidth];
1061
Greg Chadwick42a9f3b2021-01-28 13:54:14 +00001062 assign dmem_addr_err =
1063 insn_valid_i & (lsu_load_req_raw | lsu_store_req_raw) & (dmem_addr_overflow |
Greg Chadwickd3154ec2020-09-24 12:03:23 +01001064 dmem_addr_unaligned_bignum |
1065 dmem_addr_unaligned_base);
1066
Rupert Swarbrick20429db2022-02-10 17:34:00 +00001067 assign rnd_req_raw = insn_valid_i & ispr_rd_insn & (ispr_addr_o == IsprRnd);
1068 assign rnd_req_o = rnd_req_raw & insn_executing;
Greg Chadwickb168ae92021-04-14 16:04:03 +01001069
Rupert Swarbrick20429db2022-02-10 17:34:00 +00001070 assign rnd_prefetch_req_o = insn_executing & ispr_wr_insn &
Greg Chadwickb168ae92021-04-14 16:04:03 +01001071 (insn_dec_shared_i.subset == InsnSubsetBase) & (csr_addr == CsrRndPrefetch);
Philipp Wagner31441082020-07-14 11:17:21 +01001072endmodule