STAR-future commited on
Commit
f37eac2
·
verified ·
1 Parent(s): bc2da72

add chips/openroad_flow_scripts (part 4)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +4 -0
  2. chips/openroad_flow_scripts/flow/designs/src/cva6/core/csr_regfile.sv +0 -0
  3. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6.sv +1817 -0
  4. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_accel_first_pass_decoder_stub.sv +34 -0
  5. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_fifo_v3.sv +231 -0
  6. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_mmu.sv +764 -0
  7. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_ptw.sv +654 -0
  8. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_shared_tlb.sv +502 -0
  9. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_tlb.sv +439 -0
  10. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_rvfi_probes.sv +133 -0
  11. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/common_cells/include/common_cells/registers.svh +224 -0
  12. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_cast_multi.sv +794 -0
  13. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_classifier.sv +74 -0
  14. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_divsqrt_multi.sv +366 -0
  15. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_fma.sv +690 -0
  16. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_fma_multi.sv +839 -0
  17. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_noncomp.sv +415 -0
  18. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_opgroup_block.sv +244 -0
  19. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_opgroup_fmt_slice.sv +292 -0
  20. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_opgroup_multifmt_slice.sv +449 -0
  21. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_pkg.sv +495 -0
  22. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_rounding.sv +76 -0
  23. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_top.sv +185 -0
  24. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/control_mvp.sv +0 -0
  25. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/defs_div_sqrt_mvp.sv +83 -0
  26. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/div_sqrt_top_mvp.sv +180 -0
  27. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/iteration_div_sqrt_mvp.sv +61 -0
  28. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/norm_div_sqrt_mvp.sv +470 -0
  29. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/nrbd_nrsc_mvp.sv +104 -0
  30. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/preprocess_mvp.sv +425 -0
  31. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_compressed_if_driver.sv +66 -0
  32. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/compressed_instr_decoder.sv +49 -0
  33. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/copro_alu.sv +160 -0
  34. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/include/cvxif_instr_pkg.sv +154 -0
  35. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/instr_decoder.sv +89 -0
  36. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_fu.sv +74 -0
  37. chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_issue_register_commit_if_driver.sv +66 -0
  38. chips/openroad_flow_scripts/flow/designs/src/cva6/core/decoder.sv +1722 -0
  39. chips/openroad_flow_scripts/flow/designs/src/cva6/core/ex_stage.sv +725 -0
  40. chips/openroad_flow_scripts/flow/designs/src/cva6/core/fpu_wrap.sv +571 -0
  41. chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/bht.sv +313 -0
  42. chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/bht2lvl.sv +133 -0
  43. chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/btb.sv +193 -0
  44. chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/frontend.sv +590 -0
  45. chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/instr_queue.sv +565 -0
  46. chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/instr_scan.sv +110 -0
  47. chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/ras.sv +79 -0
  48. chips/openroad_flow_scripts/flow/designs/src/cva6/core/id_stage.sv +452 -0
  49. chips/openroad_flow_scripts/flow/designs/src/cva6/core/include/ariane_pkg.sv +818 -0
  50. chips/openroad_flow_scripts/flow/designs/src/cva6/core/include/build_config_pkg.sv +196 -0
.gitattributes CHANGED
@@ -3034,3 +3034,7 @@ chips/skywater_sky130_pdk/libraries/sky130_fd_pr/latest/cells/rf_pfet_20v0_withp
3034
  chips/openroad_flow_scripts/flow/designs/nangate45/bp_quad/bsg_chip_block.sv2v.v filter=lfs diff=lfs merge=lfs -text
3035
  chips/openroad_flow_scripts/flow/designs/src/bp_quad/bsg_chip_block.sv2v.v filter=lfs diff=lfs merge=lfs -text
3036
  chips/openroad_flow_scripts/flow/designs/src/coyote/coyote.sv2v.v filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
3034
  chips/openroad_flow_scripts/flow/designs/nangate45/bp_quad/bsg_chip_block.sv2v.v filter=lfs diff=lfs merge=lfs -text
3035
  chips/openroad_flow_scripts/flow/designs/src/bp_quad/bsg_chip_block.sv2v.v filter=lfs diff=lfs merge=lfs -text
3036
  chips/openroad_flow_scripts/flow/designs/src/coyote/coyote.sv2v.v filter=lfs diff=lfs merge=lfs -text
3037
+ chips/openroad_flow_scripts/flow/platforms/asap7/gds/asap7sc7p5t_28_L_220121a.gds filter=lfs diff=lfs merge=lfs -text
3038
+ chips/openroad_flow_scripts/flow/platforms/asap7/gds/asap7sc7p5t_28_R_220121a.gds filter=lfs diff=lfs merge=lfs -text
3039
+ chips/openroad_flow_scripts/flow/platforms/asap7/gds/asap7sc7p5t_28_SL_220121a.gds filter=lfs diff=lfs merge=lfs -text
3040
+ chips/openroad_flow_scripts/flow/platforms/asap7/gds/asap7sc7p5t_28_SRAM_220121a.gds filter=lfs diff=lfs merge=lfs -text
chips/openroad_flow_scripts/flow/designs/src/cva6/core/csr_regfile.sv ADDED
The diff for this file is too large to render. See raw diff
 
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6.sv ADDED
@@ -0,0 +1,1817 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2017-2019 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Author: Florian Zaruba, ETH Zurich
12
+ // Date: 19.03.2017
13
+ // Description: CVA6 Top-level module
14
+
15
+ `include "rvfi_types.svh"
16
+ `include "cvxif_types.svh"
17
+
18
+ module cva6
19
+ import ariane_pkg::*;
20
+ #(
21
+ // CVA6 config
22
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = build_config_pkg::build_config(
23
+ cva6_config_pkg::cva6_cfg
24
+ ),
25
+
26
+ // RVFI PROBES
27
+ parameter type rvfi_probes_instr_t = `RVFI_PROBES_INSTR_T(CVA6Cfg),
28
+ parameter type rvfi_probes_csr_t = `RVFI_PROBES_CSR_T(CVA6Cfg),
29
+ parameter type rvfi_probes_t = struct packed {
30
+ rvfi_probes_csr_t csr;
31
+ rvfi_probes_instr_t instr;
32
+ },
33
+
34
+ // branchpredict scoreboard entry
35
+ // this is the struct which we will inject into the pipeline to guide the various
36
+ // units towards the correct branch decision and resolve
37
+ localparam type branchpredict_sbe_t = struct packed {
38
+ cf_t cf; // type of control flow prediction
39
+ logic [CVA6Cfg.VLEN-1:0] predict_address; // target address at which to jump, or not
40
+ },
41
+
42
+ parameter type exception_t = struct packed {
43
+ logic [CVA6Cfg.XLEN-1:0] cause; // cause of exception
44
+ logic [CVA6Cfg.XLEN-1:0] tval; // additional information of causing exception (e.g.: instruction causing it),
45
+ // address of LD/ST fault
46
+ logic [CVA6Cfg.GPLEN-1:0] tval2; // additional information when the causing exception in a guest exception
47
+ logic [31:0] tinst; // transformed instruction information
48
+ logic gva; // signals when a guest virtual address is written to tval
49
+ logic valid;
50
+ },
51
+
52
+ // cache request ports
53
+ // I$ address translation requests
54
+ localparam type icache_areq_t = struct packed {
55
+ logic fetch_valid; // address translation valid
56
+ logic [CVA6Cfg.PLEN-1:0] fetch_paddr; // physical address in
57
+ exception_t fetch_exception; // exception occurred during fetch
58
+ },
59
+ localparam type icache_arsp_t = struct packed {
60
+ logic fetch_req; // address translation request
61
+ logic [CVA6Cfg.VLEN-1:0] fetch_vaddr; // virtual address out
62
+ },
63
+
64
+ // I$ data requests
65
+ localparam type icache_dreq_t = struct packed {
66
+ logic req; // we request a new word
67
+ logic kill_s1; // kill the current request
68
+ logic kill_s2; // kill the last request
69
+ logic spec; // request is speculative
70
+ logic [CVA6Cfg.VLEN-1:0] vaddr; // 1st cycle: 12 bit index is taken for lookup
71
+ },
72
+ localparam type icache_drsp_t = struct packed {
73
+ logic ready; // icache is ready
74
+ logic valid; // signals a valid read
75
+ logic [CVA6Cfg.FETCH_WIDTH-1:0] data; // 2+ cycle out: tag
76
+ logic [CVA6Cfg.FETCH_USER_WIDTH-1:0] user; // User bits
77
+ logic [CVA6Cfg.VLEN-1:0] vaddr; // virtual address out
78
+ exception_t ex; // we've encountered an exception
79
+ },
80
+
81
+ // IF/ID Stage
82
+ // store the decompressed instruction
83
+ localparam type fetch_entry_t = struct packed {
84
+ logic [CVA6Cfg.VLEN-1:0] address; // the address of the instructions from below
85
+ logic [31:0] instruction; // instruction word
86
+ branchpredict_sbe_t branch_predict; // this field contains branch prediction information regarding the forward branch path
87
+ exception_t ex; // this field contains exceptions which might have happened earlier, e.g.: fetch exceptions
88
+ },
89
+ //JVT struct{base,mode}
90
+ localparam type jvt_t = struct packed {
91
+ logic [CVA6Cfg.XLEN-7:0] base;
92
+ logic [5:0] mode;
93
+ },
94
+
95
+ // ID/EX/WB Stage
96
+ localparam type scoreboard_entry_t = struct packed {
97
+ logic [CVA6Cfg.VLEN-1:0] pc; // PC of instruction
98
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] trans_id; // this can potentially be simplified, we could index the scoreboard entry
99
+ // with the transaction id in any case make the width more generic
100
+ fu_t fu; // functional unit to use
101
+ fu_op op; // operation to perform in each functional unit
102
+ logic [REG_ADDR_SIZE-1:0] rs1; // register source address 1
103
+ logic [REG_ADDR_SIZE-1:0] rs2; // register source address 2
104
+ logic [REG_ADDR_SIZE-1:0] rd; // register destination address
105
+ logic [CVA6Cfg.XLEN-1:0] result; // for unfinished instructions this field also holds the immediate,
106
+ // for unfinished floating-point that are partly encoded in rs2, this field also holds rs2
107
+ // for unfinished floating-point fused operations (FMADD, FMSUB, FNMADD, FNMSUB)
108
+ // this field holds the address of the third operand from the floating-point register file
109
+ logic valid; // is the result valid
110
+ logic use_imm; // should we use the immediate as operand b?
111
+ logic use_zimm; // use zimm as operand a
112
+ logic use_pc; // set if we need to use the PC as operand a, PC from exception
113
+ exception_t ex; // exception has occurred
114
+ branchpredict_sbe_t bp; // branch predict scoreboard data structure
115
+ logic is_compressed; // signals a compressed instructions, we need this information at the commit stage if
116
+ // we want jump accordingly e.g.: +4, +2
117
+ logic is_macro_instr; // is an instruction executed as predefined sequence of instructions called macro definition
118
+ logic is_last_macro_instr; // is last decoded 32bit instruction of macro definition
119
+ logic is_double_rd_macro_instr; // is double move decoded 32bit instruction of macro definition
120
+ logic vfp; // is this a vector floating-point instruction?
121
+ logic is_zcmt; //is a zcmt instruction
122
+ },
123
+ localparam type writeback_t = struct packed {
124
+ logic valid; // wb data is valid
125
+ logic [CVA6Cfg.XLEN-1:0] data; //wb data
126
+ logic ex_valid; // exception from WB
127
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] trans_id; //transaction ID
128
+ },
129
+
130
+ // branch-predict
131
+ // this is the struct we get back from ex stage and we will use it to update
132
+ // all the necessary data structures
133
+ // bp_resolve_t
134
+ localparam type bp_resolve_t = struct packed {
135
+ logic valid; // prediction with all its values is valid
136
+ logic [CVA6Cfg.VLEN-1:0] pc; // PC of predict or mis-predict
137
+ logic [CVA6Cfg.VLEN-1:0] target_address; // target address at which to jump, or not
138
+ logic is_mispredict; // set if this was a mis-predict
139
+ logic is_taken; // branch is taken
140
+ cf_t cf_type; // Type of control flow change
141
+ },
142
+
143
+ // All information needed to determine whether we need to associate an interrupt
144
+ // with the corresponding instruction or not.
145
+ localparam type irq_ctrl_t = struct packed {
146
+ logic [CVA6Cfg.XLEN-1:0] mie;
147
+ logic [CVA6Cfg.XLEN-1:0] mip;
148
+ logic [CVA6Cfg.XLEN-1:0] mideleg;
149
+ logic [CVA6Cfg.XLEN-1:0] hideleg;
150
+ logic sie;
151
+ logic global_enable;
152
+ },
153
+
154
+ localparam type lsu_ctrl_t = struct packed {
155
+ logic valid;
156
+ logic [CVA6Cfg.VLEN-1:0] vaddr;
157
+ logic [31:0] tinst;
158
+ logic hs_ld_st_inst;
159
+ logic hlvx_inst;
160
+ logic overflow;
161
+ logic g_overflow;
162
+ logic [CVA6Cfg.XLEN-1:0] data;
163
+ logic [(CVA6Cfg.XLEN/8)-1:0] be;
164
+ fu_t fu;
165
+ fu_op operation;
166
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] trans_id;
167
+ },
168
+
169
+ localparam type fu_data_t = struct packed {
170
+ fu_t fu;
171
+ fu_op operation;
172
+ logic [CVA6Cfg.XLEN-1:0] operand_a;
173
+ logic [CVA6Cfg.XLEN-1:0] operand_b;
174
+ logic [CVA6Cfg.XLEN-1:0] imm;
175
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] trans_id;
176
+ },
177
+
178
+ localparam type icache_req_t = struct packed {
179
+ logic [CVA6Cfg.ICACHE_SET_ASSOC_WIDTH-1:0] way; // way to replace
180
+ logic [CVA6Cfg.PLEN-1:0] paddr; // physical address
181
+ logic nc; // noncacheable
182
+ logic [CVA6Cfg.MEM_TID_WIDTH-1:0] tid; // threadi id (used as transaction id in Ariane)
183
+ },
184
+ localparam type icache_rtrn_t = struct packed {
185
+ wt_cache_pkg::icache_in_t rtype; // see definitions above
186
+ logic [CVA6Cfg.ICACHE_LINE_WIDTH-1:0] data; // full cache line width
187
+ logic [CVA6Cfg.ICACHE_USER_LINE_WIDTH-1:0] user; // user bits
188
+ struct packed {
189
+ logic vld; // invalidate only affected way
190
+ logic all; // invalidate all ways
191
+ logic [CVA6Cfg.ICACHE_INDEX_WIDTH-1:0] idx; // physical address to invalidate
192
+ logic [CVA6Cfg.ICACHE_SET_ASSOC_WIDTH-1:0] way; // way to invalidate
193
+ } inv; // invalidation vector
194
+ logic [CVA6Cfg.MEM_TID_WIDTH-1:0] tid; // threadi id (used as transaction id in Ariane)
195
+ },
196
+
197
+ // D$ data requests
198
+ localparam type dcache_req_i_t = struct packed {
199
+ logic [CVA6Cfg.DCACHE_INDEX_WIDTH-1:0] address_index;
200
+ logic [CVA6Cfg.DCACHE_TAG_WIDTH-1:0] address_tag;
201
+ logic [CVA6Cfg.XLEN-1:0] data_wdata;
202
+ logic [CVA6Cfg.DCACHE_USER_WIDTH-1:0] data_wuser;
203
+ logic data_req;
204
+ logic data_we;
205
+ logic [(CVA6Cfg.XLEN/8)-1:0] data_be;
206
+ logic [1:0] data_size;
207
+ logic [CVA6Cfg.DcacheIdWidth-1:0] data_id;
208
+ logic kill_req;
209
+ logic tag_valid;
210
+ },
211
+
212
+ localparam type dcache_req_o_t = struct packed {
213
+ logic data_gnt;
214
+ logic data_rvalid;
215
+ logic [CVA6Cfg.DcacheIdWidth-1:0] data_rid;
216
+ logic [CVA6Cfg.XLEN-1:0] data_rdata;
217
+ logic [CVA6Cfg.DCACHE_USER_WIDTH-1:0] data_ruser;
218
+ },
219
+
220
+ // Accelerator - CVA6
221
+ parameter type accelerator_req_t = logic,
222
+ parameter type accelerator_resp_t = logic,
223
+
224
+ // Accelerator - CVA6's MMU
225
+ parameter type acc_mmu_req_t = logic,
226
+ parameter type acc_mmu_resp_t = logic,
227
+
228
+ // AXI types
229
+ parameter type axi_ar_chan_t = struct packed {
230
+ logic [CVA6Cfg.AxiIdWidth-1:0] id;
231
+ logic [CVA6Cfg.AxiAddrWidth-1:0] addr;
232
+ axi_pkg::len_t len;
233
+ axi_pkg::size_t size;
234
+ axi_pkg::burst_t burst;
235
+ logic lock;
236
+ axi_pkg::cache_t cache;
237
+ axi_pkg::prot_t prot;
238
+ axi_pkg::qos_t qos;
239
+ axi_pkg::region_t region;
240
+ logic [CVA6Cfg.AxiUserWidth-1:0] user;
241
+ },
242
+ parameter type axi_aw_chan_t = struct packed {
243
+ logic [CVA6Cfg.AxiIdWidth-1:0] id;
244
+ logic [CVA6Cfg.AxiAddrWidth-1:0] addr;
245
+ axi_pkg::len_t len;
246
+ axi_pkg::size_t size;
247
+ axi_pkg::burst_t burst;
248
+ logic lock;
249
+ axi_pkg::cache_t cache;
250
+ axi_pkg::prot_t prot;
251
+ axi_pkg::qos_t qos;
252
+ axi_pkg::region_t region;
253
+ axi_pkg::atop_t atop;
254
+ logic [CVA6Cfg.AxiUserWidth-1:0] user;
255
+ },
256
+ parameter type axi_w_chan_t = struct packed {
257
+ logic [CVA6Cfg.AxiDataWidth-1:0] data;
258
+ logic [(CVA6Cfg.AxiDataWidth/8)-1:0] strb;
259
+ logic last;
260
+ logic [CVA6Cfg.AxiUserWidth-1:0] user;
261
+ },
262
+ parameter type b_chan_t = struct packed {
263
+ logic [CVA6Cfg.AxiIdWidth-1:0] id;
264
+ axi_pkg::resp_t resp;
265
+ logic [CVA6Cfg.AxiUserWidth-1:0] user;
266
+ },
267
+ parameter type r_chan_t = struct packed {
268
+ logic [CVA6Cfg.AxiIdWidth-1:0] id;
269
+ logic [CVA6Cfg.AxiDataWidth-1:0] data;
270
+ axi_pkg::resp_t resp;
271
+ logic last;
272
+ logic [CVA6Cfg.AxiUserWidth-1:0] user;
273
+ },
274
+ parameter type noc_req_t = struct packed {
275
+ axi_aw_chan_t aw;
276
+ logic aw_valid;
277
+ axi_w_chan_t w;
278
+ logic w_valid;
279
+ logic b_ready;
280
+ axi_ar_chan_t ar;
281
+ logic ar_valid;
282
+ logic r_ready;
283
+ },
284
+ parameter type noc_resp_t = struct packed {
285
+ logic aw_ready;
286
+ logic ar_ready;
287
+ logic w_ready;
288
+ logic b_valid;
289
+ b_chan_t b;
290
+ logic r_valid;
291
+ r_chan_t r;
292
+ },
293
+ //
294
+ parameter type acc_cfg_t = logic,
295
+ parameter acc_cfg_t AccCfg = '0,
296
+ // CVXIF Types
297
+ parameter type readregflags_t = `READREGFLAGS_T(CVA6Cfg),
298
+ parameter type writeregflags_t = `WRITEREGFLAGS_T(CVA6Cfg),
299
+ parameter type id_t = `ID_T(CVA6Cfg),
300
+ parameter type hartid_t = `HARTID_T(CVA6Cfg),
301
+ parameter type x_compressed_req_t = `X_COMPRESSED_REQ_T(CVA6Cfg, hartid_t),
302
+ parameter type x_compressed_resp_t = `X_COMPRESSED_RESP_T(CVA6Cfg),
303
+ parameter type x_issue_req_t = `X_ISSUE_REQ_T(CVA6Cfg, hartit_t, id_t),
304
+ parameter type x_issue_resp_t = `X_ISSUE_RESP_T(CVA6Cfg, writeregflags_t, readregflags_t),
305
+ parameter type x_register_t = `X_REGISTER_T(CVA6Cfg, hartid_t, id_t, readregflags_t),
306
+ parameter type x_commit_t = `X_COMMIT_T(CVA6Cfg, hartid_t, id_t),
307
+ parameter type x_result_t = `X_RESULT_T(CVA6Cfg, hartid_t, id_t, writeregflags_t),
308
+ parameter type cvxif_req_t =
309
+ `CVXIF_REQ_T(CVA6Cfg, x_compressed_req_t, x_issue_req_t, x_register_req_t, x_commit_t),
310
+ parameter type cvxif_resp_t =
311
+ `CVXIF_RESP_T(CVA6Cfg, x_compressed_resp_t, x_issue_resp_t, x_result_t)
312
+ ) (
313
+ // Subsystem Clock - SUBSYSTEM
314
+ input logic clk_i,
315
+ // Asynchronous reset active low - SUBSYSTEM
316
+ input logic rst_ni,
317
+ // Reset boot address - SUBSYSTEM
318
+ input logic [CVA6Cfg.VLEN-1:0] boot_addr_i,
319
+ // Hard ID reflected as CSR - SUBSYSTEM
320
+ input logic [CVA6Cfg.XLEN-1:0] hart_id_i,
321
+ // Level sensitive (async) interrupts - SUBSYSTEM
322
+ input logic [1:0] irq_i,
323
+ // Inter-processor (async) interrupt - SUBSYSTEM
324
+ input logic ipi_i,
325
+ // Timer (async) interrupt - SUBSYSTEM
326
+ input logic time_irq_i,
327
+ // Debug (async) request - SUBSYSTEM
328
+ input logic debug_req_i,
329
+ // Probes to build RVFI, can be left open when not used - RVFI
330
+ output rvfi_probes_t rvfi_probes_o,
331
+ // CVXIF request - SUBSYSTEM
332
+ output cvxif_req_t cvxif_req_o,
333
+ // CVXIF response - SUBSYSTEM
334
+ input cvxif_resp_t cvxif_resp_i,
335
+ // noc request, can be AXI or OpenPiton - SUBSYSTEM
336
+ output noc_req_t noc_req_o,
337
+ // noc response, can be AXI or OpenPiton - SUBSYSTEM
338
+ input noc_resp_t noc_resp_i
339
+ );
340
+
341
+ localparam type interrupts_t = struct packed {
342
+ logic [CVA6Cfg.XLEN-1:0] S_SW;
343
+ logic [CVA6Cfg.XLEN-1:0] VS_SW;
344
+ logic [CVA6Cfg.XLEN-1:0] M_SW;
345
+ logic [CVA6Cfg.XLEN-1:0] S_TIMER;
346
+ logic [CVA6Cfg.XLEN-1:0] VS_TIMER;
347
+ logic [CVA6Cfg.XLEN-1:0] M_TIMER;
348
+ logic [CVA6Cfg.XLEN-1:0] S_EXT;
349
+ logic [CVA6Cfg.XLEN-1:0] VS_EXT;
350
+ logic [CVA6Cfg.XLEN-1:0] M_EXT;
351
+ logic [CVA6Cfg.XLEN-1:0] HS_EXT;
352
+ };
353
+
354
+ localparam interrupts_t INTERRUPTS = '{
355
+ S_SW: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_S_SOFT),
356
+ VS_SW: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_VS_SOFT),
357
+ M_SW: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_M_SOFT),
358
+ S_TIMER: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_S_TIMER),
359
+ VS_TIMER: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_VS_TIMER),
360
+ M_TIMER: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_M_TIMER),
361
+ S_EXT: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_S_EXT),
362
+ VS_EXT: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_VS_EXT),
363
+ M_EXT: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_M_EXT),
364
+ HS_EXT: (1 << (CVA6Cfg.XLEN - 1)) | CVA6Cfg.XLEN'(riscv::IRQ_HS_EXT)
365
+ };
366
+
367
+ // ------------------------------------------
368
+ // Global Signals
369
+ // Signals connecting more than one module
370
+ // ------------------------------------------
371
+ riscv::priv_lvl_t priv_lvl;
372
+ logic v;
373
+ exception_t ex_commit; // exception from commit stage
374
+ bp_resolve_t resolved_branch;
375
+ logic [ CVA6Cfg.VLEN-1:0] pc_commit;
376
+ logic eret;
377
+ logic [CVA6Cfg.NrCommitPorts-1:0] commit_ack;
378
+ logic [CVA6Cfg.NrCommitPorts-1:0] commit_macro_ack;
379
+
380
+ localparam NumPorts = 4;
381
+
382
+ // CVXIF
383
+ cvxif_req_t cvxif_req;
384
+ // CVXIF OUTPUTS
385
+ logic x_compressed_valid;
386
+ x_compressed_req_t x_compressed_req;
387
+ logic x_issue_valid;
388
+ x_issue_req_t x_issue_req;
389
+ logic x_register_valid;
390
+ x_register_t x_register;
391
+ logic x_commit_valid;
392
+ x_commit_t x_commit;
393
+ logic x_result_ready;
394
+ // CVXIF INPUTS
395
+ logic x_compressed_ready;
396
+ x_compressed_resp_t x_compressed_resp;
397
+ logic x_issue_ready;
398
+ x_issue_resp_t x_issue_resp;
399
+ logic x_register_ready;
400
+ logic x_result_valid;
401
+ x_result_t x_result;
402
+
403
+ // --------------
404
+ // PCGEN <-> CSR
405
+ // --------------
406
+ logic [CVA6Cfg.VLEN-1:0] trap_vector_base_commit_pcgen;
407
+ logic [CVA6Cfg.VLEN-1:0] epc_commit_pcgen;
408
+ // --------------
409
+ // IF <-> ID
410
+ // --------------
411
+ fetch_entry_t [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_if_id;
412
+ logic [CVA6Cfg.NrIssuePorts-1:0] fetch_valid_if_id;
413
+ logic [CVA6Cfg.NrIssuePorts-1:0] fetch_ready_id_if;
414
+
415
+ // --------------
416
+ // ID <-> ISSUE
417
+ // --------------
418
+ scoreboard_entry_t [CVA6Cfg.NrIssuePorts-1:0] issue_entry_id_issue, issue_entry_id_issue_prev;
419
+ logic [CVA6Cfg.NrIssuePorts-1:0][31:0] orig_instr_id_issue;
420
+ logic [CVA6Cfg.NrIssuePorts-1:0] issue_entry_valid_id_issue;
421
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_ctrl_fow_id_issue;
422
+ logic [CVA6Cfg.NrIssuePorts-1:0] issue_instr_issue_id;
423
+
424
+ // --------------
425
+ // ISSUE <-> EX
426
+ // --------------
427
+ logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.VLEN-1:0] rs1_forwarding_id_ex; // unregistered version of fu_data_o.operanda
428
+ logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.VLEN-1:0] rs2_forwarding_id_ex; // unregistered version of fu_data_o.operandb
429
+ logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.XLEN-1:0] rvfi_rs1;
430
+ logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.XLEN-1:0] rvfi_rs2;
431
+
432
+ fu_data_t [CVA6Cfg.NrIssuePorts-1:0] fu_data_id_ex;
433
+ logic [CVA6Cfg.VLEN-1:0] pc_id_ex;
434
+ logic zcmt_id_ex;
435
+ logic is_compressed_instr_id_ex;
436
+ logic [CVA6Cfg.NrIssuePorts-1:0][31:0] tinst_ex;
437
+ // fixed latency units
438
+ logic flu_ready_ex_id;
439
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] flu_trans_id_ex_id;
440
+ logic flu_valid_ex_id;
441
+ logic [CVA6Cfg.XLEN-1:0] flu_result_ex_id;
442
+ exception_t flu_exception_ex_id;
443
+ // ALU
444
+ logic [CVA6Cfg.NrIssuePorts-1:0] alu_valid_id_ex;
445
+ // Branches and Jumps
446
+ logic [CVA6Cfg.NrIssuePorts-1:0] branch_valid_id_ex;
447
+
448
+ branchpredict_sbe_t branch_predict_id_ex;
449
+ logic resolve_branch_ex_id;
450
+ // LSU
451
+ logic [CVA6Cfg.NrIssuePorts-1:0] lsu_valid_id_ex;
452
+ logic lsu_ready_ex_id;
453
+
454
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] load_trans_id_ex_id;
455
+ logic [CVA6Cfg.XLEN-1:0] load_result_ex_id;
456
+ logic load_valid_ex_id;
457
+ exception_t load_exception_ex_id;
458
+
459
+ logic [CVA6Cfg.XLEN-1:0] store_result_ex_id;
460
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] store_trans_id_ex_id;
461
+ logic store_valid_ex_id;
462
+ exception_t store_exception_ex_id;
463
+ // MULT
464
+ logic [CVA6Cfg.NrIssuePorts-1:0] mult_valid_id_ex;
465
+ // FPU
466
+ logic fpu_ready_ex_id;
467
+ logic [CVA6Cfg.NrIssuePorts-1:0] fpu_valid_id_ex;
468
+ logic [1:0] fpu_fmt_id_ex;
469
+ logic [2:0] fpu_rm_id_ex;
470
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] fpu_trans_id_ex_id;
471
+ logic [CVA6Cfg.XLEN-1:0] fpu_result_ex_id;
472
+ logic fpu_valid_ex_id;
473
+ exception_t fpu_exception_ex_id;
474
+ // ALU2
475
+ logic [CVA6Cfg.NrIssuePorts-1:0] alu2_valid_id_ex;
476
+ // Accelerator
477
+ logic stall_acc_id;
478
+ scoreboard_entry_t issue_instr_id_acc;
479
+ logic issue_instr_hs_id_acc;
480
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] acc_trans_id_ex_id;
481
+ logic [CVA6Cfg.XLEN-1:0] acc_result_ex_id;
482
+ logic acc_valid_ex_id;
483
+ exception_t acc_exception_ex_id;
484
+ logic halt_acc_ctrl;
485
+ logic [4:0] acc_resp_fflags;
486
+ logic acc_resp_fflags_valid;
487
+ logic single_step_acc_commit;
488
+ // CSR
489
+ logic [CVA6Cfg.NrIssuePorts-1:0] csr_valid_id_ex;
490
+ logic csr_hs_ld_st_inst_ex;
491
+ // CVXIF
492
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] x_trans_id_ex_id;
493
+ logic [CVA6Cfg.XLEN-1:0] x_result_ex_id;
494
+ logic x_valid_ex_id;
495
+ exception_t x_exception_ex_id;
496
+ logic x_we_ex_id;
497
+ logic [4:0] x_rd_ex_id;
498
+ logic [CVA6Cfg.NrIssuePorts-1:0] x_issue_valid_id_ex;
499
+ logic x_issue_ready_ex_id;
500
+ logic [31:0] x_off_instr_id_ex;
501
+ logic x_transaction_rejected;
502
+ // --------------
503
+ // EX <-> COMMIT
504
+ // --------------
505
+ // CSR Commit
506
+ logic csr_commit_commit_ex;
507
+ logic dirty_fp_state;
508
+ logic dirty_v_state;
509
+ // LSU Commit
510
+ logic lsu_commit_commit_ex;
511
+ logic lsu_commit_ready_ex_commit;
512
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] lsu_commit_trans_id;
513
+ logic stall_st_pending_ex;
514
+ logic no_st_pending_ex;
515
+ logic no_st_pending_commit;
516
+ logic amo_valid_commit;
517
+ // ACCEL Commit
518
+ logic acc_valid_acc_ex;
519
+ // --------------
520
+ // EX <-> ACC_DISP
521
+ // --------------
522
+ acc_mmu_req_t acc_mmu_req;
523
+ acc_mmu_resp_t acc_mmu_resp;
524
+ // --------------
525
+ // ID <-> COMMIT
526
+ // --------------
527
+ scoreboard_entry_t [CVA6Cfg.NrCommitPorts-1:0] commit_instr_id_commit;
528
+ logic [CVA6Cfg.NrCommitPorts-1:0] commit_drop_id_commit;
529
+ logic [CVA6Cfg.NrCommitPorts-1:0] commit_ack_commit_id;
530
+
531
+ // --------------
532
+ // RVFI
533
+ // --------------
534
+ logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.TRANS_ID_BITS-1:0] rvfi_issue_pointer;
535
+ logic [CVA6Cfg.NrCommitPorts-1:0][CVA6Cfg.TRANS_ID_BITS-1:0] rvfi_commit_pointer;
536
+ // --------------
537
+ // COMMIT <-> ID
538
+ // --------------
539
+ logic [CVA6Cfg.NrCommitPorts-1:0][4:0] waddr_commit_id;
540
+ logic [CVA6Cfg.NrCommitPorts-1:0][CVA6Cfg.XLEN-1:0] wdata_commit_id;
541
+ logic [CVA6Cfg.NrCommitPorts-1:0] we_gpr_commit_id;
542
+ logic [CVA6Cfg.NrCommitPorts-1:0] we_fpr_commit_id;
543
+ // --------------
544
+ // CSR <-> *
545
+ // --------------
546
+ logic [4:0] fflags_csr_commit;
547
+ riscv::xs_t fs;
548
+ riscv::xs_t vfs;
549
+ logic [2:0] frm_csr_id_issue_ex;
550
+ logic [6:0] fprec_csr_ex;
551
+ riscv::xs_t vs;
552
+ logic enable_translation_csr_ex;
553
+ logic enable_g_translation_csr_ex;
554
+ logic en_ld_st_translation_csr_ex;
555
+ logic en_ld_st_g_translation_csr_ex;
556
+ riscv::priv_lvl_t ld_st_priv_lvl_csr_ex;
557
+ logic ld_st_v_csr_ex;
558
+ logic sum_csr_ex;
559
+ logic vs_sum_csr_ex;
560
+ logic mxr_csr_ex;
561
+ logic vmxr_csr_ex;
562
+ logic [CVA6Cfg.PPNW-1:0] satp_ppn_csr_ex;
563
+ logic [CVA6Cfg.ASID_WIDTH-1:0] asid_csr_ex;
564
+ logic [CVA6Cfg.PPNW-1:0] vsatp_ppn_csr_ex;
565
+ logic [CVA6Cfg.ASID_WIDTH-1:0] vs_asid_csr_ex;
566
+ logic [CVA6Cfg.PPNW-1:0] hgatp_ppn_csr_ex;
567
+ logic [CVA6Cfg.VMID_WIDTH-1:0] vmid_csr_ex;
568
+ logic [11:0] csr_addr_ex_csr;
569
+ fu_op csr_op_commit_csr;
570
+ logic [CVA6Cfg.XLEN-1:0] csr_wdata_commit_csr;
571
+ logic [CVA6Cfg.XLEN-1:0] csr_rdata_csr_commit;
572
+ exception_t csr_exception_csr_commit;
573
+ logic tvm_csr_id;
574
+ logic tw_csr_id;
575
+ logic vtw_csr_id;
576
+ logic tsr_csr_id;
577
+ logic hu;
578
+ irq_ctrl_t irq_ctrl_csr_id;
579
+ logic dcache_en_csr_nbdcache;
580
+ logic csr_write_fflags_commit_cs;
581
+ logic icache_en_csr;
582
+ logic acc_cons_en_csr;
583
+ logic debug_mode;
584
+ logic single_step_csr_commit;
585
+ riscv::pmpcfg_t [avoid_neg(CVA6Cfg.NrPMPEntries-1):0] pmpcfg;
586
+ logic [avoid_neg(CVA6Cfg.NrPMPEntries-1):0][CVA6Cfg.PLEN-3:0] pmpaddr;
587
+ logic [31:0] mcountinhibit_csr_perf;
588
+ //jvt
589
+ jvt_t jvt;
590
+ // ----------------------------
591
+ // Performance Counters <-> *
592
+ // ----------------------------
593
+ logic [11:0] addr_csr_perf;
594
+ logic [CVA6Cfg.XLEN-1:0] data_csr_perf, data_perf_csr;
595
+ logic we_csr_perf;
596
+
597
+ logic icache_flush_ctrl_cache;
598
+ logic itlb_miss_ex_perf;
599
+ logic dtlb_miss_ex_perf;
600
+ logic dcache_miss_cache_perf;
601
+ logic icache_miss_cache_perf;
602
+ logic [NumPorts-1:0][CVA6Cfg.DCACHE_SET_ASSOC-1:0] miss_vld_bits;
603
+ logic stall_issue;
604
+ // --------------
605
+ // CTRL <-> *
606
+ // --------------
607
+ logic set_pc_ctrl_pcgen;
608
+ logic flush_csr_ctrl;
609
+ logic flush_unissued_instr_ctrl_id;
610
+ logic flush_ctrl_if;
611
+ logic flush_ctrl_id;
612
+ logic flush_ctrl_ex;
613
+ logic flush_ctrl_bp;
614
+ logic flush_tlb_ctrl_ex;
615
+ logic flush_tlb_vvma_ctrl_ex;
616
+ logic flush_tlb_gvma_ctrl_ex;
617
+ logic fence_i_commit_controller;
618
+ logic fence_commit_controller;
619
+ logic sfence_vma_commit_controller;
620
+ logic hfence_vvma_commit_controller;
621
+ logic hfence_gvma_commit_controller;
622
+ logic halt_ctrl;
623
+ logic halt_csr_ctrl;
624
+ logic dcache_flush_ctrl_cache;
625
+ logic dcache_flush_ack_cache_ctrl;
626
+ logic set_debug_pc;
627
+ logic flush_commit;
628
+ logic flush_acc;
629
+
630
+ icache_areq_t icache_areq_ex_cache;
631
+ icache_arsp_t icache_areq_cache_ex;
632
+ icache_dreq_t icache_dreq_if_cache;
633
+ icache_drsp_t icache_dreq_cache_if;
634
+
635
+ amo_req_t amo_req;
636
+ amo_resp_t amo_resp;
637
+ logic sb_full;
638
+
639
+ // ----------------
640
+ // DCache <-> *
641
+ // ----------------
642
+ dcache_req_i_t [2:0] dcache_req_ports_ex_cache;
643
+ dcache_req_o_t [2:0] dcache_req_ports_cache_ex;
644
+ dcache_req_i_t dcache_req_ports_id_cache;
645
+ dcache_req_o_t dcache_req_ports_cache_id;
646
+ dcache_req_i_t [1:0] dcache_req_ports_acc_cache;
647
+ dcache_req_o_t [1:0] dcache_req_ports_cache_acc;
648
+ logic dcache_commit_wbuffer_empty;
649
+ logic dcache_commit_wbuffer_not_ni;
650
+
651
+ //RVFI
652
+ lsu_ctrl_t rvfi_lsu_ctrl;
653
+ logic [CVA6Cfg.PLEN-1:0] rvfi_mem_paddr;
654
+ logic [CVA6Cfg.NrIssuePorts-1:0] rvfi_is_compressed;
655
+ rvfi_probes_csr_t rvfi_csr;
656
+
657
+ // Accelerator port
658
+ logic [63:0] inval_addr;
659
+ logic inval_valid;
660
+ logic inval_ready;
661
+
662
+ // --------------
663
+ // Frontend
664
+ // --------------
665
+ frontend #(
666
+ .CVA6Cfg(CVA6Cfg),
667
+ .bp_resolve_t(bp_resolve_t),
668
+ .fetch_entry_t(fetch_entry_t),
669
+ .icache_dreq_t(icache_dreq_t),
670
+ .icache_drsp_t(icache_drsp_t)
671
+ ) i_frontend (
672
+ .clk_i,
673
+ .rst_ni,
674
+ .boot_addr_i (boot_addr_i[CVA6Cfg.VLEN-1:0]),
675
+ .flush_bp_i (1'b0),
676
+ .flush_i (flush_ctrl_if), // not entirely correct
677
+ .halt_i (halt_ctrl),
678
+ .set_pc_commit_i (set_pc_ctrl_pcgen),
679
+ .pc_commit_i (pc_commit),
680
+ .ex_valid_i (ex_commit.valid),
681
+ .resolved_branch_i (resolved_branch),
682
+ .eret_i (eret),
683
+ .epc_i (epc_commit_pcgen),
684
+ .trap_vector_base_i (trap_vector_base_commit_pcgen),
685
+ .set_debug_pc_i (set_debug_pc),
686
+ .debug_mode_i (debug_mode),
687
+ .icache_dreq_o (icache_dreq_if_cache),
688
+ .icache_dreq_i (icache_dreq_cache_if),
689
+ .fetch_entry_o (fetch_entry_if_id),
690
+ .fetch_entry_valid_o(fetch_valid_if_id),
691
+ .fetch_entry_ready_i(fetch_ready_id_if)
692
+ );
693
+
694
+ // ---------
695
+ // ID
696
+ // ---------
697
+ id_stage #(
698
+ .CVA6Cfg(CVA6Cfg),
699
+ .branchpredict_sbe_t(branchpredict_sbe_t),
700
+ .dcache_req_i_t(dcache_req_i_t),
701
+ .dcache_req_o_t(dcache_req_o_t),
702
+ .exception_t(exception_t),
703
+ .fetch_entry_t(fetch_entry_t),
704
+ .jvt_t(jvt_t),
705
+ .irq_ctrl_t(irq_ctrl_t),
706
+ .scoreboard_entry_t(scoreboard_entry_t),
707
+ .interrupts_t(interrupts_t),
708
+ .INTERRUPTS(INTERRUPTS),
709
+ .x_compressed_req_t(x_compressed_req_t),
710
+ .x_compressed_resp_t(x_compressed_resp_t)
711
+ ) id_stage_i (
712
+ .clk_i,
713
+ .rst_ni,
714
+ .flush_i(flush_ctrl_if),
715
+ .debug_req_i,
716
+
717
+ .fetch_entry_i (fetch_entry_if_id),
718
+ .fetch_entry_valid_i(fetch_valid_if_id),
719
+ .fetch_entry_ready_o(fetch_ready_id_if),
720
+
721
+ .issue_entry_o (issue_entry_id_issue),
722
+ .issue_entry_o_prev (issue_entry_id_issue_prev),
723
+ .orig_instr_o (orig_instr_id_issue),
724
+ .issue_entry_valid_o(issue_entry_valid_id_issue),
725
+ .is_ctrl_flow_o (is_ctrl_fow_id_issue),
726
+ .issue_instr_ack_i (issue_instr_issue_id),
727
+
728
+ .rvfi_is_compressed_o(rvfi_is_compressed),
729
+
730
+ .priv_lvl_i (priv_lvl),
731
+ .v_i (v),
732
+ .fs_i (fs),
733
+ .vfs_i (vfs),
734
+ .frm_i (frm_csr_id_issue_ex),
735
+ .vs_i (vs),
736
+ .irq_i (irq_i),
737
+ .irq_ctrl_i (irq_ctrl_csr_id),
738
+ .debug_mode_i (debug_mode),
739
+ .tvm_i (tvm_csr_id),
740
+ .tw_i (tw_csr_id),
741
+ .vtw_i (vtw_csr_id),
742
+ .tsr_i (tsr_csr_id),
743
+ .hu_i (hu),
744
+ .hart_id_i (hart_id_i),
745
+ .compressed_ready_i(x_compressed_ready),
746
+ .compressed_resp_i (x_compressed_resp),
747
+ .compressed_valid_o(x_compressed_valid),
748
+ .compressed_req_o (x_compressed_req),
749
+ .jvt_i (jvt),
750
+ // DCACHE interfaces
751
+ .dcache_req_ports_i(dcache_req_ports_cache_id),
752
+ .dcache_req_ports_o(dcache_req_ports_id_cache)
753
+ );
754
+
755
+ logic [CVA6Cfg.NrWbPorts-1:0][CVA6Cfg.TRANS_ID_BITS-1:0] trans_id_ex_id;
756
+ logic [CVA6Cfg.NrWbPorts-1:0][CVA6Cfg.XLEN-1:0] wbdata_ex_id;
757
+ exception_t [CVA6Cfg.NrWbPorts-1:0] ex_ex_ex_id; // exception from execute, ex_stage to id_stage
758
+ logic [CVA6Cfg.NrWbPorts-1:0] wt_valid_ex_id;
759
+
760
+ assign trans_id_ex_id[FLU_WB] = flu_trans_id_ex_id;
761
+ assign wbdata_ex_id[FLU_WB] = flu_result_ex_id;
762
+ assign ex_ex_ex_id[FLU_WB] = flu_exception_ex_id;
763
+ assign wt_valid_ex_id[FLU_WB] = flu_valid_ex_id;
764
+
765
+ assign trans_id_ex_id[STORE_WB] = store_trans_id_ex_id;
766
+ assign wbdata_ex_id[STORE_WB] = store_result_ex_id;
767
+ assign ex_ex_ex_id[STORE_WB] = store_exception_ex_id;
768
+ assign wt_valid_ex_id[STORE_WB] = store_valid_ex_id;
769
+
770
+ assign trans_id_ex_id[LOAD_WB] = load_trans_id_ex_id;
771
+ assign wbdata_ex_id[LOAD_WB] = load_result_ex_id;
772
+ assign ex_ex_ex_id[LOAD_WB] = load_exception_ex_id;
773
+ assign wt_valid_ex_id[LOAD_WB] = load_valid_ex_id;
774
+
775
+ assign trans_id_ex_id[FPU_WB] = fpu_trans_id_ex_id;
776
+ assign wbdata_ex_id[FPU_WB] = fpu_result_ex_id;
777
+ assign ex_ex_ex_id[FPU_WB] = fpu_exception_ex_id;
778
+ assign wt_valid_ex_id[FPU_WB] = fpu_valid_ex_id;
779
+
780
+ if (CVA6Cfg.CvxifEn) begin
781
+ always_comb begin : gen_cvxif_input_assignement
782
+ x_compressed_ready = cvxif_resp_i.compressed_ready;
783
+ x_compressed_resp = cvxif_resp_i.compressed_resp;
784
+ x_issue_ready = cvxif_resp_i.issue_ready;
785
+ x_issue_resp = cvxif_resp_i.issue_resp;
786
+ x_register_ready = cvxif_resp_i.register_ready;
787
+ x_result_valid = cvxif_resp_i.result_valid;
788
+ x_result = cvxif_resp_i.result;
789
+ end
790
+
791
+ always_comb begin : gen_cvxif_output_assignement
792
+ cvxif_req.compressed_valid = x_compressed_valid;
793
+ cvxif_req.compressed_req = x_compressed_req;
794
+ cvxif_req.issue_valid = x_issue_valid;
795
+ cvxif_req.issue_req = x_issue_req;
796
+ cvxif_req.register_valid = x_register_valid;
797
+ cvxif_req.register = x_register;
798
+ cvxif_req.commit_valid = x_commit_valid;
799
+ cvxif_req.commit = x_commit;
800
+ cvxif_req.result_ready = x_result_ready;
801
+ end
802
+ assign trans_id_ex_id[X_WB] = x_trans_id_ex_id;
803
+ assign wbdata_ex_id[X_WB] = x_result_ex_id;
804
+ assign ex_ex_ex_id[X_WB] = x_exception_ex_id;
805
+ assign wt_valid_ex_id[X_WB] = x_valid_ex_id;
806
+ end else if (CVA6Cfg.EnableAccelerator) begin
807
+ assign cvxif_req = '0;
808
+ assign trans_id_ex_id[ACC_WB] = acc_trans_id_ex_id;
809
+ assign wbdata_ex_id[ACC_WB] = acc_result_ex_id;
810
+ assign ex_ex_ex_id[ACC_WB] = acc_exception_ex_id;
811
+ assign wt_valid_ex_id[ACC_WB] = acc_valid_ex_id;
812
+ end else begin
813
+ assign cvxif_req = '0;
814
+ end
815
+
816
+ if (CVA6Cfg.CvxifEn && CVA6Cfg.EnableAccelerator) begin : gen_err_xif_and_acc
817
+ $error("X-interface and accelerator port cannot be enabled at the same time.");
818
+ end
819
+
820
+ // ---------
821
+ // Issue
822
+ // ---------
823
+ issue_stage #(
824
+ .CVA6Cfg(CVA6Cfg),
825
+ .bp_resolve_t(bp_resolve_t),
826
+ .branchpredict_sbe_t(branchpredict_sbe_t),
827
+ .exception_t(exception_t),
828
+ .fu_data_t(fu_data_t),
829
+ .scoreboard_entry_t(scoreboard_entry_t),
830
+ .writeback_t(writeback_t),
831
+ .x_issue_req_t(x_issue_req_t),
832
+ .x_issue_resp_t(x_issue_resp_t),
833
+ .x_register_t(x_register_t),
834
+ .x_commit_t(x_commit_t)
835
+ ) issue_stage_i (
836
+ .clk_i,
837
+ .rst_ni,
838
+ .sb_full_o (sb_full),
839
+ .flush_unissued_instr_i (flush_unissued_instr_ctrl_id),
840
+ .flush_i (flush_ctrl_id),
841
+ .stall_i (stall_acc_id),
842
+ // ID Stage
843
+ .decoded_instr_i (issue_entry_id_issue),
844
+ .decoded_instr_i_prev (issue_entry_id_issue_prev),
845
+ .orig_instr_i (orig_instr_id_issue),
846
+ .decoded_instr_valid_i (issue_entry_valid_id_issue),
847
+ .is_ctrl_flow_i (is_ctrl_fow_id_issue),
848
+ .decoded_instr_ack_o (issue_instr_issue_id),
849
+ // Functional Units
850
+ .rs1_forwarding_o (rs1_forwarding_id_ex),
851
+ .rs2_forwarding_o (rs2_forwarding_id_ex),
852
+ .fu_data_o (fu_data_id_ex),
853
+ .pc_o (pc_id_ex),
854
+ .is_zcmt_o (zcmt_id_ex),
855
+ .is_compressed_instr_o (is_compressed_instr_id_ex),
856
+ .tinst_o (tinst_ex),
857
+ // fixed latency unit ready
858
+ .flu_ready_i (flu_ready_ex_id),
859
+ // ALU
860
+ .alu_valid_o (alu_valid_id_ex),
861
+ // Branches and Jumps
862
+ .branch_valid_o (branch_valid_id_ex), // branch is valid
863
+ .branch_predict_o (branch_predict_id_ex), // branch predict to ex
864
+ .resolve_branch_i (resolve_branch_ex_id), // in order to resolve the branch
865
+ // LSU
866
+ .lsu_ready_i (lsu_ready_ex_id),
867
+ .lsu_valid_o (lsu_valid_id_ex),
868
+ // Multiplier
869
+ .mult_valid_o (mult_valid_id_ex),
870
+ // FPU
871
+ .fpu_ready_i (fpu_ready_ex_id),
872
+ .fpu_valid_o (fpu_valid_id_ex),
873
+ .fpu_fmt_o (fpu_fmt_id_ex),
874
+ .fpu_rm_o (fpu_rm_id_ex),
875
+ // ALU2
876
+ .alu2_valid_o (alu2_valid_id_ex),
877
+ // CSR
878
+ .csr_valid_o (csr_valid_id_ex),
879
+ // CVXIF
880
+ .xfu_valid_o (x_issue_valid_id_ex),
881
+ .xfu_ready_i (x_issue_ready_ex_id),
882
+ .x_off_instr_o (x_off_instr_id_ex),
883
+ .hart_id_i (hart_id_i),
884
+ .x_issue_ready_i (x_issue_ready),
885
+ .x_issue_resp_i (x_issue_resp),
886
+ .x_issue_valid_o (x_issue_valid),
887
+ .x_issue_req_o (x_issue_req),
888
+ .x_register_ready_i (x_register_ready),
889
+ .x_register_valid_o (x_register_valid),
890
+ .x_register_o (x_register),
891
+ .x_commit_valid_o (x_commit_valid),
892
+ .x_commit_o (x_commit),
893
+ .x_transaction_rejected_o(x_transaction_rejected),
894
+ // Accelerator
895
+ .issue_instr_o (issue_instr_id_acc),
896
+ .issue_instr_hs_o (issue_instr_hs_id_acc),
897
+ // Commit
898
+ .trans_id_i (trans_id_ex_id),
899
+ .resolved_branch_i (resolved_branch),
900
+ .wbdata_i (wbdata_ex_id),
901
+ .ex_ex_i (ex_ex_ex_id),
902
+ .wt_valid_i (wt_valid_ex_id),
903
+ .x_we_i (x_we_ex_id),
904
+ .x_rd_i (x_rd_ex_id),
905
+
906
+ .waddr_i (waddr_commit_id),
907
+ .wdata_i (wdata_commit_id),
908
+ .we_gpr_i (we_gpr_commit_id),
909
+ .we_fpr_i (we_fpr_commit_id),
910
+ .commit_instr_o (commit_instr_id_commit),
911
+ .commit_drop_o (commit_drop_id_commit),
912
+ .commit_ack_i (commit_ack_commit_id),
913
+ // Performance Counters
914
+ .stall_issue_o (stall_issue),
915
+ //RVFI
916
+ .rvfi_issue_pointer_o (rvfi_issue_pointer),
917
+ .rvfi_commit_pointer_o(rvfi_commit_pointer),
918
+ .rvfi_rs1_o (rvfi_rs1),
919
+ .rvfi_rs2_o (rvfi_rs2)
920
+ );
921
+
922
+ // ---------
923
+ // EX
924
+ // ---------
925
+ ex_stage #(
926
+ .CVA6Cfg (CVA6Cfg),
927
+ .bp_resolve_t(bp_resolve_t),
928
+ .branchpredict_sbe_t(branchpredict_sbe_t),
929
+ .dcache_req_i_t(dcache_req_i_t),
930
+ .dcache_req_o_t(dcache_req_o_t),
931
+ .exception_t(exception_t),
932
+ .fu_data_t(fu_data_t),
933
+ .icache_areq_t(icache_areq_t),
934
+ .icache_arsp_t(icache_arsp_t),
935
+ .icache_dreq_t(icache_dreq_t),
936
+ .icache_drsp_t(icache_drsp_t),
937
+ .lsu_ctrl_t(lsu_ctrl_t),
938
+ .x_result_t(x_result_t),
939
+ .acc_mmu_req_t(acc_mmu_req_t),
940
+ .acc_mmu_resp_t(acc_mmu_resp_t)
941
+ ) ex_stage_i (
942
+ .clk_i(clk_i),
943
+ .rst_ni(rst_ni),
944
+ .debug_mode_i(debug_mode),
945
+ .flush_i(flush_ctrl_ex),
946
+ .rs1_forwarding_i(rs1_forwarding_id_ex),
947
+ .rs2_forwarding_i(rs2_forwarding_id_ex),
948
+ .fu_data_i(fu_data_id_ex),
949
+ .pc_i(pc_id_ex),
950
+ .is_zcmt_i(zcmt_id_ex),
951
+ .is_compressed_instr_i(is_compressed_instr_id_ex),
952
+ .tinst_i(tinst_ex),
953
+ // fixed latency units
954
+ .flu_result_o(flu_result_ex_id),
955
+ .flu_trans_id_o(flu_trans_id_ex_id),
956
+ .flu_valid_o(flu_valid_ex_id),
957
+ .flu_exception_o(flu_exception_ex_id),
958
+ .flu_ready_o(flu_ready_ex_id),
959
+ // ALU
960
+ .alu_valid_i(alu_valid_id_ex),
961
+ // Branches and Jumps
962
+ .branch_valid_i(branch_valid_id_ex),
963
+ .branch_predict_i(branch_predict_id_ex), // branch predict to ex
964
+ .resolved_branch_o(resolved_branch),
965
+ .resolve_branch_o(resolve_branch_ex_id),
966
+ // CSR
967
+ .csr_valid_i(csr_valid_id_ex),
968
+ .csr_addr_o(csr_addr_ex_csr),
969
+ .csr_commit_i(csr_commit_commit_ex), // from commit
970
+ .csr_hs_ld_st_inst_o(csr_hs_ld_st_inst_ex), // signals a Hypervisor Load/Store Instruction
971
+ // MULT
972
+ .mult_valid_i(mult_valid_id_ex),
973
+ // LSU
974
+ .lsu_ready_o(lsu_ready_ex_id),
975
+ .lsu_valid_i(lsu_valid_id_ex),
976
+
977
+ .load_result_o (load_result_ex_id),
978
+ .load_trans_id_o (load_trans_id_ex_id),
979
+ .load_valid_o (load_valid_ex_id),
980
+ .load_exception_o(load_exception_ex_id),
981
+
982
+ .store_result_o (store_result_ex_id),
983
+ .store_trans_id_o (store_trans_id_ex_id),
984
+ .store_valid_o (store_valid_ex_id),
985
+ .store_exception_o(store_exception_ex_id),
986
+
987
+ .lsu_commit_i (lsu_commit_commit_ex), // from commit
988
+ .lsu_commit_ready_o (lsu_commit_ready_ex_commit), // to commit
989
+ .commit_tran_id_i (lsu_commit_trans_id), // from commit
990
+ .stall_st_pending_i (stall_st_pending_ex),
991
+ .no_st_pending_o (no_st_pending_ex),
992
+ // FPU
993
+ .fpu_ready_o (fpu_ready_ex_id),
994
+ .fpu_valid_i (fpu_valid_id_ex),
995
+ .fpu_fmt_i (fpu_fmt_id_ex),
996
+ .fpu_rm_i (fpu_rm_id_ex),
997
+ .fpu_frm_i (frm_csr_id_issue_ex),
998
+ .fpu_prec_i (fprec_csr_ex),
999
+ .fpu_trans_id_o (fpu_trans_id_ex_id),
1000
+ .fpu_result_o (fpu_result_ex_id),
1001
+ .fpu_valid_o (fpu_valid_ex_id),
1002
+ .fpu_exception_o (fpu_exception_ex_id),
1003
+ // ALU2
1004
+ .alu2_valid_i (alu2_valid_id_ex),
1005
+ .amo_valid_commit_i (amo_valid_commit),
1006
+ .amo_req_o (amo_req),
1007
+ .amo_resp_i (amo_resp),
1008
+ // CoreV-X-Interface
1009
+ .x_valid_i (x_issue_valid_id_ex),
1010
+ .x_ready_o (x_issue_ready_ex_id),
1011
+ .x_off_instr_i (x_off_instr_id_ex),
1012
+ .x_transaction_rejected_i(x_transaction_rejected),
1013
+ .x_trans_id_o (x_trans_id_ex_id),
1014
+ .x_exception_o (x_exception_ex_id),
1015
+ .x_result_o (x_result_ex_id),
1016
+ .x_valid_o (x_valid_ex_id),
1017
+ .x_we_o (x_we_ex_id),
1018
+ .x_rd_o (x_rd_ex_id),
1019
+ .x_result_valid_i (x_result_valid),
1020
+ .x_result_i (x_result),
1021
+ .x_result_ready_o (x_result_ready),
1022
+ // Accelerator
1023
+ .acc_valid_i (acc_valid_acc_ex),
1024
+ // Accelerator MMU access
1025
+ .acc_mmu_req_i (acc_mmu_req),
1026
+ .acc_mmu_resp_o (acc_mmu_resp),
1027
+ // Performance counters
1028
+ .itlb_miss_o (itlb_miss_ex_perf),
1029
+ .dtlb_miss_o (dtlb_miss_ex_perf),
1030
+ // Memory Management
1031
+ .enable_translation_i (enable_translation_csr_ex), // from CSR
1032
+ .enable_g_translation_i (enable_g_translation_csr_ex), // from CSR
1033
+ .en_ld_st_translation_i (en_ld_st_translation_csr_ex),
1034
+ .en_ld_st_g_translation_i(en_ld_st_g_translation_csr_ex),
1035
+ .flush_tlb_i (flush_tlb_ctrl_ex),
1036
+ .flush_tlb_vvma_i (flush_tlb_vvma_ctrl_ex),
1037
+ .flush_tlb_gvma_i (flush_tlb_gvma_ctrl_ex),
1038
+ .priv_lvl_i (priv_lvl), // from CSR
1039
+ .v_i (v), // from CSR
1040
+ .ld_st_priv_lvl_i (ld_st_priv_lvl_csr_ex), // from CSR
1041
+ .ld_st_v_i (ld_st_v_csr_ex), // from CSR
1042
+ .sum_i (sum_csr_ex), // from CSR
1043
+ .vs_sum_i (vs_sum_csr_ex), // from CSR
1044
+ .mxr_i (mxr_csr_ex), // from CSR
1045
+ .vmxr_i (vmxr_csr_ex), // from CSR
1046
+ .satp_ppn_i (satp_ppn_csr_ex), // from CSR
1047
+ .asid_i (asid_csr_ex), // from CSR
1048
+ .vsatp_ppn_i (vsatp_ppn_csr_ex), // from CSR
1049
+ .vs_asid_i (vs_asid_csr_ex), // from CSR
1050
+ .hgatp_ppn_i (hgatp_ppn_csr_ex), // from CSR
1051
+ .vmid_i (vmid_csr_ex), // from CSR
1052
+ .icache_areq_i (icache_areq_cache_ex),
1053
+ .icache_areq_o (icache_areq_ex_cache),
1054
+ // DCACHE interfaces
1055
+ .dcache_req_ports_i (dcache_req_ports_cache_ex),
1056
+ .dcache_req_ports_o (dcache_req_ports_ex_cache),
1057
+ .dcache_wbuffer_empty_i (dcache_commit_wbuffer_empty),
1058
+ .dcache_wbuffer_not_ni_i (dcache_commit_wbuffer_not_ni),
1059
+ // PMP
1060
+ .pmpcfg_i (pmpcfg),
1061
+ .pmpaddr_i (pmpaddr),
1062
+ //RVFI
1063
+ .rvfi_lsu_ctrl_o (rvfi_lsu_ctrl),
1064
+ .rvfi_mem_paddr_o (rvfi_mem_paddr)
1065
+ );
1066
+
1067
+ // ---------
1068
+ // Commit
1069
+ // ---------
1070
+
1071
+ // we have to make sure that the whole write buffer path is empty before
1072
+ // used e.g. for fence instructions.
1073
+ assign no_st_pending_commit = no_st_pending_ex & dcache_commit_wbuffer_empty;
1074
+
1075
+ commit_stage #(
1076
+ .CVA6Cfg(CVA6Cfg),
1077
+ .exception_t(exception_t),
1078
+ .scoreboard_entry_t(scoreboard_entry_t)
1079
+ ) commit_stage_i (
1080
+ .clk_i,
1081
+ .rst_ni,
1082
+ .halt_i (halt_ctrl),
1083
+ .flush_dcache_i (dcache_flush_ctrl_cache),
1084
+ .exception_o (ex_commit),
1085
+ .dirty_fp_state_o (dirty_fp_state),
1086
+ .single_step_i (single_step_csr_commit || single_step_acc_commit),
1087
+ .commit_instr_i (commit_instr_id_commit),
1088
+ .commit_drop_i (commit_drop_id_commit),
1089
+ .commit_ack_o (commit_ack_commit_id),
1090
+ .commit_macro_ack_o(commit_macro_ack),
1091
+ .waddr_o (waddr_commit_id),
1092
+ .wdata_o (wdata_commit_id),
1093
+ .we_gpr_o (we_gpr_commit_id),
1094
+ .we_fpr_o (we_fpr_commit_id),
1095
+ .amo_resp_i (amo_resp),
1096
+ .pc_o (pc_commit),
1097
+ .csr_op_o (csr_op_commit_csr),
1098
+ .csr_wdata_o (csr_wdata_commit_csr),
1099
+ .csr_rdata_i (csr_rdata_csr_commit),
1100
+ .csr_write_fflags_o(csr_write_fflags_commit_cs),
1101
+ .csr_exception_i (csr_exception_csr_commit),
1102
+ .commit_lsu_o (lsu_commit_commit_ex),
1103
+ .commit_lsu_ready_i(lsu_commit_ready_ex_commit),
1104
+ .commit_tran_id_o (lsu_commit_trans_id),
1105
+ .amo_valid_commit_o(amo_valid_commit),
1106
+ .no_st_pending_i (no_st_pending_commit),
1107
+ .commit_csr_o (csr_commit_commit_ex),
1108
+ .fence_i_o (fence_i_commit_controller),
1109
+ .fence_o (fence_commit_controller),
1110
+ .flush_commit_o (flush_commit),
1111
+ .sfence_vma_o (sfence_vma_commit_controller),
1112
+ .hfence_vvma_o (hfence_vvma_commit_controller),
1113
+ .hfence_gvma_o (hfence_gvma_commit_controller)
1114
+ );
1115
+
1116
+ assign commit_ack = commit_macro_ack & ~commit_drop_id_commit;
1117
+
1118
+ // ---------
1119
+ // CSR
1120
+ // ---------
1121
+ csr_regfile #(
1122
+ .CVA6Cfg (CVA6Cfg),
1123
+ .exception_t (exception_t),
1124
+ .jvt_t (jvt_t),
1125
+ .irq_ctrl_t (irq_ctrl_t),
1126
+ .scoreboard_entry_t(scoreboard_entry_t),
1127
+ .rvfi_probes_csr_t (rvfi_probes_csr_t),
1128
+ .MHPMCounterNum (MHPMCounterNum)
1129
+ ) csr_regfile_i (
1130
+ .clk_i,
1131
+ .rst_ni,
1132
+ .time_irq_i,
1133
+ .flush_o (flush_csr_ctrl),
1134
+ .halt_csr_o (halt_csr_ctrl),
1135
+ .commit_instr_i (commit_instr_id_commit[0]),
1136
+ .commit_ack_i (commit_ack),
1137
+ .boot_addr_i (boot_addr_i[CVA6Cfg.VLEN-1:0]),
1138
+ .hart_id_i (hart_id_i[CVA6Cfg.XLEN-1:0]),
1139
+ .ex_i (ex_commit),
1140
+ .csr_op_i (csr_op_commit_csr),
1141
+ .csr_addr_i (csr_addr_ex_csr),
1142
+ .csr_wdata_i (csr_wdata_commit_csr),
1143
+ .csr_rdata_o (csr_rdata_csr_commit),
1144
+ .dirty_fp_state_i (dirty_fp_state),
1145
+ .csr_write_fflags_i (csr_write_fflags_commit_cs),
1146
+ .dirty_v_state_i (dirty_v_state),
1147
+ .pc_i (pc_commit),
1148
+ .csr_exception_o (csr_exception_csr_commit),
1149
+ .epc_o (epc_commit_pcgen),
1150
+ .eret_o (eret),
1151
+ .trap_vector_base_o (trap_vector_base_commit_pcgen),
1152
+ .priv_lvl_o (priv_lvl),
1153
+ .v_o (v),
1154
+ .acc_fflags_ex_i (acc_resp_fflags),
1155
+ .acc_fflags_ex_valid_i (acc_resp_fflags_valid),
1156
+ .fs_o (fs),
1157
+ .vfs_o (vfs),
1158
+ .fflags_o (fflags_csr_commit),
1159
+ .frm_o (frm_csr_id_issue_ex),
1160
+ .fprec_o (fprec_csr_ex),
1161
+ .vs_o (vs),
1162
+ .irq_ctrl_o (irq_ctrl_csr_id),
1163
+ .en_translation_o (enable_translation_csr_ex),
1164
+ .en_g_translation_o (enable_g_translation_csr_ex),
1165
+ .en_ld_st_translation_o (en_ld_st_translation_csr_ex),
1166
+ .en_ld_st_g_translation_o(en_ld_st_g_translation_csr_ex),
1167
+ .ld_st_priv_lvl_o (ld_st_priv_lvl_csr_ex),
1168
+ .ld_st_v_o (ld_st_v_csr_ex),
1169
+ .csr_hs_ld_st_inst_i (csr_hs_ld_st_inst_ex),
1170
+ .sum_o (sum_csr_ex),
1171
+ .vs_sum_o (vs_sum_csr_ex),
1172
+ .mxr_o (mxr_csr_ex),
1173
+ .vmxr_o (vmxr_csr_ex),
1174
+ .satp_ppn_o (satp_ppn_csr_ex),
1175
+ .asid_o (asid_csr_ex),
1176
+ .vsatp_ppn_o (vsatp_ppn_csr_ex),
1177
+ .vs_asid_o (vs_asid_csr_ex),
1178
+ .hgatp_ppn_o (hgatp_ppn_csr_ex),
1179
+ .vmid_o (vmid_csr_ex),
1180
+ .irq_i,
1181
+ .ipi_i,
1182
+ .debug_req_i,
1183
+ .set_debug_pc_o (set_debug_pc),
1184
+ .tvm_o (tvm_csr_id),
1185
+ .tw_o (tw_csr_id),
1186
+ .vtw_o (vtw_csr_id),
1187
+ .tsr_o (tsr_csr_id),
1188
+ .hu_o (hu),
1189
+ .debug_mode_o (debug_mode),
1190
+ .single_step_o (single_step_csr_commit),
1191
+ .icache_en_o (icache_en_csr),
1192
+ .dcache_en_o (dcache_en_csr_nbdcache),
1193
+ .acc_cons_en_o (acc_cons_en_csr),
1194
+ .perf_addr_o (addr_csr_perf),
1195
+ .perf_data_o (data_csr_perf),
1196
+ .perf_data_i (data_perf_csr),
1197
+ .perf_we_o (we_csr_perf),
1198
+ .pmpcfg_o (pmpcfg),
1199
+ .pmpaddr_o (pmpaddr),
1200
+ .mcountinhibit_o (mcountinhibit_csr_perf),
1201
+ .jvt_o (jvt),
1202
+ //RVFI
1203
+ .rvfi_csr_o (rvfi_csr)
1204
+ );
1205
+
1206
+ // ------------------------
1207
+ // Performance Counters
1208
+ // ------------------------
1209
+ if (CVA6Cfg.PerfCounterEn) begin : gen_perf_counter
1210
+ perf_counters #(
1211
+ .CVA6Cfg(CVA6Cfg),
1212
+ .bp_resolve_t(bp_resolve_t),
1213
+ .exception_t(exception_t),
1214
+ .scoreboard_entry_t(scoreboard_entry_t),
1215
+ .icache_dreq_t(icache_dreq_t),
1216
+ .dcache_req_i_t(dcache_req_i_t),
1217
+ .dcache_req_o_t(dcache_req_o_t),
1218
+ .NumPorts(NumPorts)
1219
+ ) perf_counters_i (
1220
+ .clk_i (clk_i),
1221
+ .rst_ni (rst_ni),
1222
+ .debug_mode_i (debug_mode),
1223
+ .addr_i (addr_csr_perf),
1224
+ .we_i (we_csr_perf),
1225
+ .data_i (data_csr_perf),
1226
+ .data_o (data_perf_csr),
1227
+ .commit_instr_i(commit_instr_id_commit),
1228
+ .commit_ack_i (commit_ack),
1229
+
1230
+ .l1_icache_miss_i (icache_miss_cache_perf),
1231
+ .l1_dcache_miss_i (dcache_miss_cache_perf),
1232
+ .itlb_miss_i (itlb_miss_ex_perf),
1233
+ .dtlb_miss_i (dtlb_miss_ex_perf),
1234
+ .sb_full_i (sb_full),
1235
+ // TODO this is more complex that that
1236
+ // If superscalar then we additionally have to check [1] when transaction 0 succeeded
1237
+ .if_empty_i (~fetch_valid_if_id[0]),
1238
+ .ex_i (ex_commit),
1239
+ .eret_i (eret),
1240
+ .resolved_branch_i (resolved_branch),
1241
+ .branch_exceptions_i(flu_exception_ex_id),
1242
+ .l1_icache_access_i (icache_dreq_if_cache),
1243
+ .l1_dcache_access_i (dcache_req_ports_ex_cache),
1244
+ .miss_vld_bits_i (miss_vld_bits),
1245
+ .i_tlb_flush_i (flush_tlb_ctrl_ex),
1246
+ .stall_issue_i (stall_issue),
1247
+ .mcountinhibit_i (mcountinhibit_csr_perf)
1248
+ );
1249
+ end : gen_perf_counter
1250
+ else begin : gen_no_perf_counter
1251
+ assign data_perf_csr = '0;
1252
+ end : gen_no_perf_counter
1253
+
1254
+ // ------------
1255
+ // Controller
1256
+ // ------------
1257
+ controller #(
1258
+ .CVA6Cfg(CVA6Cfg),
1259
+ .bp_resolve_t(bp_resolve_t)
1260
+ ) controller_i (
1261
+ .clk_i,
1262
+ .rst_ni,
1263
+ // virtualization mode
1264
+ .v_i (v),
1265
+ // flush ports
1266
+ .set_pc_commit_o (set_pc_ctrl_pcgen),
1267
+ .flush_if_o (flush_ctrl_if),
1268
+ .flush_unissued_instr_o(flush_unissued_instr_ctrl_id),
1269
+ .flush_id_o (flush_ctrl_id),
1270
+ .flush_ex_o (flush_ctrl_ex),
1271
+ .flush_bp_o (flush_ctrl_bp),
1272
+ .flush_icache_o (icache_flush_ctrl_cache),
1273
+ .flush_dcache_o (dcache_flush_ctrl_cache),
1274
+ .flush_dcache_ack_i (dcache_flush_ack_cache_ctrl),
1275
+ .flush_tlb_o (flush_tlb_ctrl_ex),
1276
+ .flush_tlb_vvma_o (flush_tlb_vvma_ctrl_ex),
1277
+ .flush_tlb_gvma_o (flush_tlb_gvma_ctrl_ex),
1278
+ .halt_csr_i (halt_csr_ctrl),
1279
+ .halt_acc_i (halt_acc_ctrl),
1280
+ .halt_o (halt_ctrl),
1281
+ // control ports
1282
+ .eret_i (eret),
1283
+ .ex_valid_i (ex_commit.valid),
1284
+ .set_debug_pc_i (set_debug_pc),
1285
+ .resolved_branch_i (resolved_branch),
1286
+ .flush_csr_i (flush_csr_ctrl),
1287
+ .fence_i_i (fence_i_commit_controller),
1288
+ .fence_i (fence_commit_controller),
1289
+ .sfence_vma_i (sfence_vma_commit_controller),
1290
+ .hfence_vvma_i (hfence_vvma_commit_controller),
1291
+ .hfence_gvma_i (hfence_gvma_commit_controller),
1292
+ .flush_commit_i (flush_commit),
1293
+ .flush_acc_i (flush_acc)
1294
+ );
1295
+
1296
+ // -------------------
1297
+ // Cache Subsystem
1298
+ // -------------------
1299
+
1300
+ // Acc dispatcher and store buffer share a dcache request port.
1301
+ // Store buffer always has priority access over acc dipsatcher.
1302
+ dcache_req_i_t [NumPorts-1:0] dcache_req_to_cache;
1303
+ dcache_req_o_t [NumPorts-1:0] dcache_req_from_cache;
1304
+
1305
+ // D$ request
1306
+ // Since ZCMT is only enable for embdeed class so MMU should be disable.
1307
+ // Cache port 0 is being ultilize in implicit read access in ZCMT extension.
1308
+ if (CVA6Cfg.RVZCMT & ~(CVA6Cfg.MmuPresent)) begin
1309
+ assign dcache_req_to_cache[0] = dcache_req_ports_id_cache;
1310
+ end else begin
1311
+ assign dcache_req_to_cache[0] = dcache_req_ports_ex_cache[0];
1312
+ end
1313
+ assign dcache_req_to_cache[1] = dcache_req_ports_ex_cache[1];
1314
+ assign dcache_req_to_cache[2] = dcache_req_ports_acc_cache[0];
1315
+ assign dcache_req_to_cache[3] = dcache_req_ports_ex_cache[2].data_req ? dcache_req_ports_ex_cache [2] :
1316
+ dcache_req_ports_acc_cache[1];
1317
+
1318
+ // D$ response
1319
+ // Since ZCMT is only enable for embdeed class so MMU should be disable.
1320
+ // Cache port 0 is being ultilized in implicit read access in ZCMT extension.
1321
+ if (CVA6Cfg.RVZCMT & ~(CVA6Cfg.MmuPresent)) begin
1322
+ assign dcache_req_ports_cache_id = dcache_req_from_cache[0];
1323
+ assign dcache_req_ports_cache_ex[0] = '0;
1324
+ end else begin
1325
+ assign dcache_req_ports_cache_ex[0] = dcache_req_from_cache[0];
1326
+ assign dcache_req_ports_cache_id = '0;
1327
+ end
1328
+ assign dcache_req_ports_cache_ex[1] = dcache_req_from_cache[1];
1329
+ assign dcache_req_ports_cache_acc[0] = dcache_req_from_cache[2];
1330
+ always_comb begin : gen_dcache_req_store_data_gnt
1331
+ dcache_req_ports_cache_ex[2] = dcache_req_from_cache[3];
1332
+ dcache_req_ports_cache_acc[1] = dcache_req_from_cache[3];
1333
+
1334
+ // Set gnt signal
1335
+ dcache_req_ports_cache_ex[2].data_gnt &= dcache_req_ports_ex_cache[2].data_req;
1336
+ dcache_req_ports_cache_acc[1].data_gnt &= !dcache_req_ports_ex_cache[2].data_req;
1337
+ end
1338
+
1339
+ if (CVA6Cfg.DCacheType == config_pkg::WT) begin : gen_cache_wt
1340
+ // this is a cache subsystem that is compatible with OpenPiton
1341
+ wt_cache_subsystem #(
1342
+ .CVA6Cfg (CVA6Cfg),
1343
+ .icache_areq_t(icache_areq_t),
1344
+ .icache_arsp_t(icache_arsp_t),
1345
+ .icache_dreq_t(icache_dreq_t),
1346
+ .icache_drsp_t(icache_drsp_t),
1347
+ .icache_req_t(icache_req_t),
1348
+ .icache_rtrn_t(icache_rtrn_t),
1349
+ .dcache_req_i_t(dcache_req_i_t),
1350
+ .dcache_req_o_t(dcache_req_o_t),
1351
+ .NumPorts (NumPorts),
1352
+ .noc_req_t (noc_req_t),
1353
+ .noc_resp_t(noc_resp_t)
1354
+ ) i_cache_subsystem (
1355
+ // to D$
1356
+ .clk_i (clk_i),
1357
+ .rst_ni (rst_ni),
1358
+ // I$
1359
+ .icache_en_i (icache_en_csr),
1360
+ .icache_flush_i (icache_flush_ctrl_cache),
1361
+ .icache_miss_o (icache_miss_cache_perf),
1362
+ .icache_areq_i (icache_areq_ex_cache),
1363
+ .icache_areq_o (icache_areq_cache_ex),
1364
+ .icache_dreq_i (icache_dreq_if_cache),
1365
+ .icache_dreq_o (icache_dreq_cache_if),
1366
+ // D$
1367
+ .dcache_enable_i (dcache_en_csr_nbdcache),
1368
+ .dcache_flush_i (dcache_flush_ctrl_cache),
1369
+ .dcache_flush_ack_o(dcache_flush_ack_cache_ctrl),
1370
+ // to commit stage
1371
+ .dcache_amo_req_i (amo_req),
1372
+ .dcache_amo_resp_o (amo_resp),
1373
+ // from PTW, Load Unit and Store Unit
1374
+ .dcache_miss_o (dcache_miss_cache_perf),
1375
+ .miss_vld_bits_o (miss_vld_bits),
1376
+ .dcache_req_ports_i(dcache_req_to_cache),
1377
+ .dcache_req_ports_o(dcache_req_from_cache),
1378
+ // write buffer status
1379
+ .wbuffer_empty_o (dcache_commit_wbuffer_empty),
1380
+ .wbuffer_not_ni_o (dcache_commit_wbuffer_not_ni),
1381
+ // memory side
1382
+ .noc_req_o (noc_req_o),
1383
+ .noc_resp_i (noc_resp_i),
1384
+ .inval_addr_i (inval_addr),
1385
+ .inval_valid_i (inval_valid),
1386
+ .inval_ready_o (inval_ready)
1387
+ );
1388
+ end else if (
1389
+ CVA6Cfg.DCacheType == config_pkg::HPDCACHE_WT ||
1390
+ CVA6Cfg.DCacheType == config_pkg::HPDCACHE_WB ||
1391
+ CVA6Cfg.DCacheType == config_pkg::HPDCACHE_WT_WB
1392
+ )
1393
+ begin : gen_cache_hpd
1394
+ cva6_hpdcache_subsystem #(
1395
+ .CVA6Cfg (CVA6Cfg),
1396
+ .icache_areq_t(icache_areq_t),
1397
+ .icache_arsp_t(icache_arsp_t),
1398
+ .icache_dreq_t(icache_dreq_t),
1399
+ .icache_drsp_t(icache_drsp_t),
1400
+ .icache_req_t(icache_req_t),
1401
+ .icache_rtrn_t(icache_rtrn_t),
1402
+ .dcache_req_i_t(dcache_req_i_t),
1403
+ .dcache_req_o_t(dcache_req_o_t),
1404
+ .NumPorts (NumPorts),
1405
+ .axi_ar_chan_t(axi_ar_chan_t),
1406
+ .axi_aw_chan_t(axi_aw_chan_t),
1407
+ .axi_w_chan_t (axi_w_chan_t),
1408
+ .axi_b_chan_t (b_chan_t),
1409
+ .axi_r_chan_t (r_chan_t),
1410
+ .noc_req_t (noc_req_t),
1411
+ .noc_resp_t(noc_resp_t),
1412
+ .cmo_req_t (logic /*FIXME*/),
1413
+ .cmo_rsp_t (logic /*FIXME*/)
1414
+ ) i_cache_subsystem (
1415
+ .clk_i (clk_i),
1416
+ .rst_ni(rst_ni),
1417
+
1418
+ .icache_en_i (icache_en_csr),
1419
+ .icache_flush_i(icache_flush_ctrl_cache),
1420
+ .icache_miss_o (icache_miss_cache_perf),
1421
+ .icache_areq_i (icache_areq_ex_cache),
1422
+ .icache_areq_o (icache_areq_cache_ex),
1423
+ .icache_dreq_i (icache_dreq_if_cache),
1424
+ .icache_dreq_o (icache_dreq_cache_if),
1425
+
1426
+ .dcache_enable_i (dcache_en_csr_nbdcache),
1427
+ .dcache_flush_i (dcache_flush_ctrl_cache),
1428
+ .dcache_flush_ack_o(dcache_flush_ack_cache_ctrl),
1429
+ .dcache_miss_o (dcache_miss_cache_perf),
1430
+
1431
+ .dcache_amo_req_i (amo_req),
1432
+ .dcache_amo_resp_o(amo_resp),
1433
+
1434
+ .dcache_cmo_req_i ('0 /*FIXME*/),
1435
+ .dcache_cmo_resp_o( /*FIXME*/),
1436
+
1437
+ .dcache_req_ports_i(dcache_req_to_cache),
1438
+ .dcache_req_ports_o(dcache_req_from_cache),
1439
+
1440
+ .wbuffer_empty_o (dcache_commit_wbuffer_empty),
1441
+ .wbuffer_not_ni_o(dcache_commit_wbuffer_not_ni),
1442
+
1443
+ .hwpf_base_set_i ('0 /*FIXME*/),
1444
+ .hwpf_base_i ('0 /*FIXME*/),
1445
+ .hwpf_base_o ( /*FIXME*/),
1446
+ .hwpf_param_set_i ('0 /*FIXME*/),
1447
+ .hwpf_param_i ('0 /*FIXME*/),
1448
+ .hwpf_param_o ( /*FIXME*/),
1449
+ .hwpf_throttle_set_i('0 /*FIXME*/),
1450
+ .hwpf_throttle_i ('0 /*FIXME*/),
1451
+ .hwpf_throttle_o ( /*FIXME*/),
1452
+ .hwpf_status_o ( /*FIXME*/),
1453
+
1454
+ .noc_req_o (noc_req_o),
1455
+ .noc_resp_i(noc_resp_i)
1456
+ );
1457
+ assign inval_ready = 1'b1;
1458
+ end else begin : gen_cache_wb
1459
+ std_cache_subsystem #(
1460
+ // note: this only works with one cacheable region
1461
+ // not as important since this cache subsystem is about to be
1462
+ // deprecated
1463
+ .CVA6Cfg (CVA6Cfg),
1464
+ .icache_areq_t (icache_areq_t),
1465
+ .icache_arsp_t (icache_arsp_t),
1466
+ .icache_dreq_t (icache_dreq_t),
1467
+ .icache_drsp_t (icache_drsp_t),
1468
+ .icache_req_t (icache_req_t),
1469
+ .icache_rtrn_t (icache_rtrn_t),
1470
+ .dcache_req_i_t(dcache_req_i_t),
1471
+ .dcache_req_o_t(dcache_req_o_t),
1472
+ .NumPorts (NumPorts),
1473
+ .axi_ar_chan_t (axi_ar_chan_t),
1474
+ .axi_aw_chan_t (axi_aw_chan_t),
1475
+ .axi_w_chan_t (axi_w_chan_t),
1476
+ .axi_req_t (noc_req_t),
1477
+ .axi_rsp_t (noc_resp_t)
1478
+ ) i_cache_subsystem (
1479
+ // to D$
1480
+ .clk_i (clk_i),
1481
+ .rst_ni (rst_ni),
1482
+ .priv_lvl_i (priv_lvl),
1483
+ // I$
1484
+ .icache_en_i (icache_en_csr),
1485
+ .icache_flush_i (icache_flush_ctrl_cache),
1486
+ .icache_miss_o (icache_miss_cache_perf),
1487
+ .icache_areq_i (icache_areq_ex_cache),
1488
+ .icache_areq_o (icache_areq_cache_ex),
1489
+ .icache_dreq_i (icache_dreq_if_cache),
1490
+ .icache_dreq_o (icache_dreq_cache_if),
1491
+ // D$
1492
+ .dcache_enable_i (dcache_en_csr_nbdcache),
1493
+ .dcache_flush_i (dcache_flush_ctrl_cache),
1494
+ .dcache_flush_ack_o(dcache_flush_ack_cache_ctrl),
1495
+ // to commit stage
1496
+ .amo_req_i (amo_req),
1497
+ .amo_resp_o (amo_resp),
1498
+ .dcache_miss_o (dcache_miss_cache_perf),
1499
+ // this is statically set to 1 as the std_cache does not have a wbuffer
1500
+ .wbuffer_empty_o (dcache_commit_wbuffer_empty),
1501
+ // from PTW, Load Unit and Store Unit
1502
+ .dcache_req_ports_i(dcache_req_to_cache),
1503
+ .dcache_req_ports_o(dcache_req_from_cache),
1504
+ // memory side
1505
+ .axi_req_o (noc_req_o),
1506
+ .axi_resp_i (noc_resp_i)
1507
+ );
1508
+ assign dcache_commit_wbuffer_not_ni = 1'b1;
1509
+ assign inval_ready = 1'b1;
1510
+ end
1511
+
1512
+ // ----------------
1513
+ // Accelerator
1514
+ // ----------------
1515
+
1516
+ if (CVA6Cfg.EnableAccelerator) begin : gen_accelerator
1517
+ acc_dispatcher #(
1518
+ .CVA6Cfg (CVA6Cfg),
1519
+ .fu_data_t (fu_data_t),
1520
+ .dcache_req_i_t (dcache_req_i_t),
1521
+ .dcache_req_o_t (dcache_req_o_t),
1522
+ .exception_t (exception_t),
1523
+ .scoreboard_entry_t(scoreboard_entry_t),
1524
+ .acc_cfg_t (acc_cfg_t),
1525
+ .AccCfg (AccCfg),
1526
+ .acc_req_t (cvxif_req_t),
1527
+ .acc_resp_t (cvxif_resp_t),
1528
+ .accelerator_req_t (accelerator_req_t),
1529
+ .accelerator_resp_t(accelerator_resp_t),
1530
+ .acc_mmu_req_t (acc_mmu_req_t),
1531
+ .acc_mmu_resp_t (acc_mmu_resp_t)
1532
+ ) i_acc_dispatcher (
1533
+ .clk_i (clk_i),
1534
+ .rst_ni (rst_ni),
1535
+ .flush_unissued_instr_i(flush_unissued_instr_ctrl_id),
1536
+ .flush_ex_i (flush_ctrl_ex),
1537
+ .flush_pipeline_o (flush_acc),
1538
+ .single_step_o (single_step_acc_commit),
1539
+ .acc_cons_en_i (acc_cons_en_csr),
1540
+ .acc_fflags_valid_o (acc_resp_fflags_valid),
1541
+ .acc_fflags_o (acc_resp_fflags),
1542
+ .ld_st_priv_lvl_i (ld_st_priv_lvl_csr_ex),
1543
+ .sum_i (sum_csr_ex),
1544
+ .pmpcfg_i (pmpcfg),
1545
+ .pmpaddr_i (pmpaddr),
1546
+ .fcsr_frm_i (frm_csr_id_issue_ex),
1547
+ .acc_mmu_en_i (enable_translation_csr_ex),
1548
+ .dirty_v_state_o (dirty_v_state),
1549
+ .issue_instr_i (issue_instr_id_acc),
1550
+ .issue_instr_hs_i (issue_instr_hs_id_acc),
1551
+ .issue_stall_o (stall_acc_id),
1552
+ .fu_data_i (fu_data_id_ex[0]),
1553
+ .commit_instr_i (commit_instr_id_commit),
1554
+ .commit_st_barrier_i (fence_i_commit_controller | fence_commit_controller),
1555
+ .acc_trans_id_o (acc_trans_id_ex_id),
1556
+ .acc_result_o (acc_result_ex_id),
1557
+ .acc_valid_o (acc_valid_ex_id),
1558
+ .acc_exception_o (acc_exception_ex_id),
1559
+ .acc_valid_ex_o (acc_valid_acc_ex),
1560
+ .commit_ack_i (commit_ack),
1561
+ .acc_stall_st_pending_o(stall_st_pending_ex),
1562
+ .acc_no_st_pending_i (no_st_pending_commit),
1563
+ .dcache_req_ports_i (dcache_req_ports_ex_cache),
1564
+ .acc_mmu_req_o (acc_mmu_req),
1565
+ .acc_mmu_resp_i (acc_mmu_resp),
1566
+ .ctrl_halt_o (halt_acc_ctrl),
1567
+ .csr_addr_i (csr_addr_ex_csr),
1568
+ .acc_dcache_req_ports_o(dcache_req_ports_acc_cache),
1569
+ .acc_dcache_req_ports_i(dcache_req_ports_cache_acc),
1570
+ .inval_ready_i (inval_ready),
1571
+ .inval_valid_o (inval_valid),
1572
+ .inval_addr_o (inval_addr),
1573
+ .acc_req_o (cvxif_req_o),
1574
+ .acc_resp_i (cvxif_resp_i)
1575
+ );
1576
+ end : gen_accelerator
1577
+ else begin : gen_no_accelerator
1578
+ assign acc_trans_id_ex_id = '0;
1579
+ assign acc_result_ex_id = '0;
1580
+ assign acc_valid_ex_id = '0;
1581
+ assign acc_exception_ex_id = '0;
1582
+ assign acc_resp_fflags = '0;
1583
+ assign acc_resp_fflags_valid = '0;
1584
+ assign stall_acc_id = '0;
1585
+ assign dirty_v_state = '0;
1586
+ assign acc_valid_acc_ex = '0;
1587
+ assign halt_acc_ctrl = '0;
1588
+ assign stall_st_pending_ex = '0;
1589
+ assign flush_acc = '0;
1590
+ assign single_step_acc_commit = '0;
1591
+
1592
+ // D$ connection is unused
1593
+ assign dcache_req_ports_acc_cache = '0;
1594
+
1595
+ // MMU access is unused
1596
+ assign acc_mmu_req = '0;
1597
+
1598
+ // No invalidation interface
1599
+ assign inval_valid = '0;
1600
+ assign inval_addr = '0;
1601
+
1602
+ // Feed through cvxif
1603
+ assign cvxif_req_o = cvxif_req;
1604
+ end : gen_no_accelerator
1605
+
1606
+ // -------------------
1607
+ // Parameter Check
1608
+ // -------------------
1609
+ // pragma translate_off
1610
+ initial config_pkg::check_cfg(CVA6Cfg);
1611
+ // pragma translate_on
1612
+
1613
+ // -------------------
1614
+ // Instruction Tracer
1615
+ // -------------------
1616
+
1617
+ //pragma translate_off
1618
+ `ifdef PITON_ARIANE
1619
+ localparam PC_QUEUE_DEPTH = 16;
1620
+
1621
+ logic piton_pc_vld;
1622
+ logic [ CVA6Cfg.VLEN-1:0] piton_pc;
1623
+ logic [CVA6Cfg.NrCommitPorts-1:0][CVA6Cfg.VLEN-1:0] pc_data;
1624
+ logic [CVA6Cfg.NrCommitPorts-1:0] pc_pop, pc_empty;
1625
+
1626
+ for (genvar i = 0; i < CVA6Cfg.NrCommitPorts; i++) begin : gen_pc_fifo
1627
+ fifo_v3 #(
1628
+ .DATA_WIDTH(64),
1629
+ .DEPTH(PC_QUEUE_DEPTH),
1630
+ .FPGA_EN(CVA6Cfg.FpgaEn)
1631
+ ) i_pc_fifo (
1632
+ .clk_i (clk_i),
1633
+ .rst_ni (rst_ni),
1634
+ .flush_i ('0),
1635
+ .testmode_i('0),
1636
+ .full_o (),
1637
+ .empty_o (pc_empty[i]),
1638
+ .usage_o (),
1639
+ .data_i (commit_instr_id_commit[i].pc),
1640
+ .push_i (commit_ack[i] & ~commit_instr_id_commit[i].ex.valid),
1641
+ .data_o (pc_data[i]),
1642
+ .pop_i (pc_pop[i])
1643
+ );
1644
+ end
1645
+
1646
+ rr_arb_tree #(
1647
+ .NumIn(CVA6Cfg.NrCommitPorts),
1648
+ .DataWidth(64)
1649
+ ) i_rr_arb_tree (
1650
+ .clk_i (clk_i),
1651
+ .rst_ni (rst_ni),
1652
+ .flush_i('0),
1653
+ .rr_i ('0),
1654
+ .req_i (~pc_empty),
1655
+ .gnt_o (pc_pop),
1656
+ .data_i (pc_data),
1657
+ .gnt_i (piton_pc_vld),
1658
+ .req_o (piton_pc_vld),
1659
+ .data_o (piton_pc),
1660
+ .idx_o ()
1661
+ );
1662
+ `endif // PITON_ARIANE
1663
+
1664
+ `ifndef VERILATOR
1665
+ instr_tracer #(
1666
+ .CVA6Cfg(CVA6Cfg),
1667
+ .bp_resolve_t(bp_resolve_t),
1668
+ .scoreboard_entry_t(scoreboard_entry_t),
1669
+ .interrupts_t(interrupts_t),
1670
+ .exception_t(exception_t),
1671
+ .INTERRUPTS(INTERRUPTS)
1672
+ ) instr_tracer_i (
1673
+ // .tracer_if(tracer_if),
1674
+ .pck(clk_i),
1675
+ .rstn(rst_ni),
1676
+ .flush_unissued(flush_unissued_instr_ctrl_id),
1677
+ .flush_all(flush_ctrl_ex),
1678
+ .instruction(id_stage_i.fetch_entry_i[0].instruction),
1679
+ .fetch_valid(id_stage_i.fetch_entry_valid_i[0]),
1680
+ .fetch_ack(id_stage_i.fetch_entry_ready_o[0]),
1681
+ .issue_ack(issue_stage_i.i_scoreboard.issue_ack_i),
1682
+ .issue_sbe(issue_stage_i.i_scoreboard.issue_instr_o),
1683
+ .waddr(waddr_commit_id),
1684
+ .wdata(wdata_commit_id),
1685
+ .we_gpr(we_gpr_commit_id),
1686
+ .we_fpr(we_fpr_commit_id),
1687
+ .commit_instr(commit_instr_id_commit),
1688
+ .commit_ack(commit_ack),
1689
+ .st_valid(ex_stage_i.lsu_i.i_store_unit.store_buffer_i.valid_i),
1690
+ .st_paddr(ex_stage_i.lsu_i.i_store_unit.store_buffer_i.paddr_i),
1691
+ .ld_valid(ex_stage_i.lsu_i.i_load_unit.req_port_o.tag_valid),
1692
+ .ld_kill(ex_stage_i.lsu_i.i_load_unit.req_port_o.kill_req),
1693
+ .ld_paddr(ex_stage_i.lsu_i.i_load_unit.paddr_i),
1694
+ .resolve_branch(resolved_branch),
1695
+ .commit_exception(commit_stage_i.exception_o),
1696
+ .priv_lvl(priv_lvl),
1697
+ .debug_mode(debug_mode),
1698
+ .hart_id_i(hart_id_i)
1699
+ );
1700
+
1701
+ // mock tracer for Verilator, to be used with spike-dasm
1702
+ `else
1703
+
1704
+ int f;
1705
+ logic [63:0] cycles;
1706
+
1707
+ initial begin
1708
+ string fn;
1709
+ $sformat(fn, "trace_hart_%0.0f.dasm", hart_id_i);
1710
+ f = $fopen(fn, "w");
1711
+ end
1712
+
1713
+ always_ff @(posedge clk_i or negedge rst_ni) begin
1714
+ if (~rst_ni) begin
1715
+ cycles <= 0;
1716
+ end else begin
1717
+ byte mode = "";
1718
+ if (CVA6Cfg.DebugEn && debug_mode) mode = "D";
1719
+ else begin
1720
+ case (priv_lvl)
1721
+ riscv::PRIV_LVL_M: mode = "M";
1722
+ riscv::PRIV_LVL_S: if (CVA6Cfg.RVS) mode = "S";
1723
+ riscv::PRIV_LVL_U: mode = "U";
1724
+ default: ; // Do nothing
1725
+ endcase
1726
+ end
1727
+ for (int i = 0; i < CVA6Cfg.NrCommitPorts; i++) begin
1728
+ if (commit_ack[i] && !commit_instr_id_commit[i].ex.valid) begin
1729
+ $fwrite(f, "%d 0x%0h %s (0x%h) DASM(%h)\n", cycles, commit_instr_id_commit[i].pc, mode,
1730
+ commit_instr_id_commit[i].ex.tval[31:0], commit_instr_id_commit[i].ex.tval[31:0]);
1731
+ end else if (commit_ack[i] && commit_instr_id_commit[i].ex.valid) begin
1732
+ if (commit_instr_id_commit[i].ex.cause == 2) begin
1733
+ $fwrite(f, "Exception Cause: Illegal Instructions, DASM(%h) PC=%h\n",
1734
+ commit_instr_id_commit[i].ex.tval[31:0], commit_instr_id_commit[i].pc);
1735
+ end else begin
1736
+ if (CVA6Cfg.DebugEn && debug_mode) begin
1737
+ $fwrite(f, "%d 0x%0h %s (0x%h) DASM(%h)\n", cycles, commit_instr_id_commit[i].pc,
1738
+ mode, commit_instr_id_commit[i].ex.tval[31:0],
1739
+ commit_instr_id_commit[i].ex.tval[31:0]);
1740
+ end else begin
1741
+ $fwrite(f, "Exception Cause: %5d, DASM(%h) PC=%h\n",
1742
+ commit_instr_id_commit[i].ex.cause, commit_instr_id_commit[i].ex.tval[31:0],
1743
+ commit_instr_id_commit[i].pc);
1744
+ end
1745
+ end
1746
+ end
1747
+ end
1748
+ cycles <= cycles + 1;
1749
+ end
1750
+ end
1751
+
1752
+ final begin
1753
+ $fclose(f);
1754
+ end
1755
+ `endif // VERILATOR
1756
+ //pragma translate_on
1757
+
1758
+
1759
+ //RVFI INSTR
1760
+ logic [CVA6Cfg.NrIssuePorts-1:0][31:0] rvfi_fetch_instr;
1761
+ for (genvar i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
1762
+ assign rvfi_fetch_instr[i] = fetch_entry_if_id[i].instruction;
1763
+ end
1764
+
1765
+ cva6_rvfi_probes #(
1766
+ .CVA6Cfg (CVA6Cfg),
1767
+ .exception_t (exception_t),
1768
+ .scoreboard_entry_t (scoreboard_entry_t),
1769
+ .lsu_ctrl_t (lsu_ctrl_t),
1770
+ .rvfi_probes_instr_t(rvfi_probes_instr_t),
1771
+ .rvfi_probes_csr_t (rvfi_probes_csr_t),
1772
+ .rvfi_probes_t (rvfi_probes_t)
1773
+ ) i_cva6_rvfi_probes (
1774
+
1775
+ .flush_i (flush_ctrl_if),
1776
+ .issue_instr_ack_i (issue_instr_issue_id),
1777
+ .fetch_entry_valid_i(fetch_valid_if_id),
1778
+ .instruction_i (rvfi_fetch_instr),
1779
+ .is_compressed_i (rvfi_is_compressed),
1780
+
1781
+ .issue_pointer_i (rvfi_issue_pointer),
1782
+ .commit_pointer_i(rvfi_commit_pointer),
1783
+
1784
+ .flush_unissued_instr_i(flush_unissued_instr_ctrl_id),
1785
+ .decoded_instr_valid_i (issue_entry_valid_id_issue),
1786
+ .decoded_instr_ack_i (issue_instr_issue_id),
1787
+
1788
+ .rs1_i(rvfi_rs1),
1789
+ .rs2_i(rvfi_rs2),
1790
+
1791
+ .commit_instr_i(commit_instr_id_commit),
1792
+ .commit_drop_i (commit_drop_id_commit),
1793
+ .ex_commit_i (ex_commit),
1794
+ .priv_lvl_i (priv_lvl),
1795
+
1796
+ .lsu_ctrl_i (rvfi_lsu_ctrl),
1797
+ .wbdata_i (wbdata_ex_id),
1798
+ .commit_ack_i(commit_ack),
1799
+ .mem_paddr_i (rvfi_mem_paddr),
1800
+ .debug_mode_i(debug_mode),
1801
+ .wdata_i (wdata_commit_id),
1802
+
1803
+ .csr_i(rvfi_csr),
1804
+ .irq_i(irq_i),
1805
+
1806
+ .rvfi_probes_o(rvfi_probes_o)
1807
+
1808
+ );
1809
+
1810
+ //pragma translate_off
1811
+ initial begin
1812
+ assert (!(CVA6Cfg.SuperscalarEn && CVA6Cfg.EnableAccelerator))
1813
+ else $fatal(1, "Accelerator is not supported by superscalar pipeline");
1814
+ end
1815
+ //pragma translate_on
1816
+
1817
+ endmodule // ariane
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_accel_first_pass_decoder_stub.sv ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 ETH Zurich and University of Bologna.
2
+ // Solderpad Hardware License, Version 0.51, see LICENSE for details.
3
+ // SPDX-License-Identifier: SHL-0.51
4
+
5
+ // Author: Nils Wistoff <nwistoff@iis.ee.ethz.ch>
6
+
7
+ // Module stub for the cva6_accel_first_pass_decoder. Replace this with your accelerator's
8
+ // first pass decoder.
9
+
10
+ module cva6_accel_first_pass_decoder
11
+ import ariane_pkg::*;
12
+ #(
13
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = '0,
14
+ parameter type scoreboard_entry_t = logic
15
+ ) (
16
+ input logic [31:0] instruction_i, // instruction from IF
17
+ input riscv::xs_t fs_i, // floating point extension status
18
+ input riscv::xs_t vs_i, // vector extension status
19
+ output logic is_accel_o, // is an accelerator instruction
20
+ output scoreboard_entry_t instruction_o, // predecoded instruction
21
+ output logic illegal_instr_o, // is an illegal instruction
22
+ output logic is_control_flow_instr_o // is a control flow instruction
23
+ );
24
+
25
+ assign is_accel_o = 1'b0;
26
+ assign instruction_o = '0;
27
+ assign illegal_instr_o = 1'b0;
28
+ assign is_control_flow_instr_o = 1'b0;
29
+
30
+ $error("cva6_accel_first_pass_decoder: instantiated non-functional module stub.\
31
+ Please replace this with your accelerator's first pass decoder \
32
+ (or unset ENABLE_ACCELERATOR).");
33
+
34
+ endmodule : cva6_accel_first_pass_decoder
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_fifo_v3.sv ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright 2024 - PlanV Technologies for additionnal contribution.
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+
12
+ // Author: Florian Zaruba <zarubaf@iis.ee.ethz.ch>
13
+ // Additional contributions by:
14
+ // Angela Gonzalez - PlanV Technologies
15
+
16
+ module cva6_fifo_v3 #(
17
+ parameter bit FALL_THROUGH = 1'b0, // fifo is in fall-through mode
18
+ parameter bit FPGA_ALTERA = 1'b0, // FPGA Altera optimizations enabled
19
+ parameter int unsigned DATA_WIDTH = 32, // default data width if the fifo is of type logic
20
+ parameter int unsigned DEPTH = 8, // depth can be arbitrary from 0 to 2**32
21
+ parameter type dtype = logic [DATA_WIDTH-1:0],
22
+ parameter bit FPGA_EN = 1'b0,
23
+ // DO NOT OVERWRITE THIS PARAMETER
24
+ parameter int unsigned ADDR_DEPTH = (DEPTH > 1) ? $clog2(DEPTH) : 1
25
+ ) (
26
+ input logic clk_i, // Clock
27
+ input logic rst_ni, // Asynchronous reset active low
28
+ input logic flush_i, // flush the queue
29
+ input logic testmode_i, // test_mode to bypass clock gating
30
+ // status flags
31
+ output logic full_o, // queue is full
32
+ output logic empty_o, // queue is empty
33
+ output logic [ADDR_DEPTH-1:0] usage_o, // fill pointer
34
+ // as long as the queue is not full we can push new data
35
+ input dtype data_i, // data to push into the queue
36
+ input logic push_i, // data is valid and can be pushed to the queue
37
+ // as long as the queue is not empty we can pop new elements
38
+ output dtype data_o, // output data
39
+ input logic pop_i // pop head from queue
40
+ );
41
+ // local parameter
42
+ // FIFO depth - handle the case of pass-through, synthesizer will do constant propagation
43
+ localparam int unsigned FifoDepth = (DEPTH > 0) ? DEPTH : 1;
44
+ // clock gating control
45
+ logic gate_clock;
46
+ // pointer to the read and write section of the queue
47
+ logic [ADDR_DEPTH - 1:0] read_pointer_n, read_pointer_q, write_pointer_n, write_pointer_q;
48
+ // keep a counter to keep track of the current queue status
49
+ // this integer will be truncated by the synthesis tool
50
+ logic [ADDR_DEPTH:0] status_cnt_n, status_cnt_q;
51
+ // actual memory
52
+ dtype [FifoDepth - 1:0] mem_n, mem_q;
53
+ dtype data_ft_n, data_ft_q;
54
+ logic first_word_n, first_word_q;
55
+
56
+ // fifo ram signals for fpga target
57
+ logic fifo_ram_we;
58
+ logic [ADDR_DEPTH-1:0] fifo_ram_read_address;
59
+ logic [ADDR_DEPTH-1:0] fifo_ram_write_address;
60
+ logic [$bits(dtype)-1:0] fifo_ram_wdata;
61
+ logic [$bits(dtype)-1:0] fifo_ram_rdata;
62
+
63
+ assign usage_o = status_cnt_q[ADDR_DEPTH-1:0];
64
+
65
+ if (DEPTH == 0) begin : gen_pass_through
66
+ assign empty_o = ~push_i;
67
+ assign full_o = ~pop_i;
68
+ end else begin : gen_fifo
69
+ assign full_o = (status_cnt_q == FifoDepth[ADDR_DEPTH:0]);
70
+ assign empty_o = (status_cnt_q == 0) & ~(FALL_THROUGH & push_i);
71
+ end
72
+ // status flags
73
+
74
+ // read and write queue logic
75
+ always_comb begin : read_write_comb
76
+ // default assignment
77
+ read_pointer_n = read_pointer_q;
78
+ write_pointer_n = write_pointer_q;
79
+ status_cnt_n = status_cnt_q;
80
+ if (FPGA_EN && FPGA_ALTERA) data_ft_n = data_ft_q;
81
+ if (FPGA_EN && FPGA_ALTERA) first_word_n = first_word_q;
82
+ if (FPGA_EN) begin
83
+ fifo_ram_we = '0;
84
+ fifo_ram_write_address = '0;
85
+ fifo_ram_wdata = '0;
86
+ if (DEPTH == 0) begin
87
+ data_o = data_i;
88
+ end else begin
89
+ if (FPGA_ALTERA) data_o = first_word_q ? data_ft_q : fifo_ram_rdata;
90
+ else data_o = fifo_ram_rdata;
91
+ end
92
+ end else begin
93
+ data_o = (DEPTH == 0) ? data_i : mem_q[read_pointer_q];
94
+ mem_n = mem_q;
95
+ gate_clock = 1'b1;
96
+ end
97
+
98
+ // push a new element to the queue
99
+ if (push_i && ~full_o) begin
100
+ if (FPGA_EN) begin
101
+ fifo_ram_we = 1'b1;
102
+ fifo_ram_write_address = write_pointer_q;
103
+ fifo_ram_wdata = data_i;
104
+ if (FPGA_ALTERA) first_word_n = first_word_q && pop_i;
105
+ end else begin
106
+ // push the data onto the queue
107
+ mem_n[write_pointer_q] = data_i;
108
+ // un-gate the clock, we want to write something
109
+ gate_clock = 1'b0;
110
+ end
111
+
112
+ // increment the write counter
113
+ if (write_pointer_q == FifoDepth[ADDR_DEPTH-1:0] - 1) write_pointer_n = '0;
114
+ else write_pointer_n = write_pointer_q + 1;
115
+ // increment the overall counter
116
+ status_cnt_n = status_cnt_q + 1;
117
+ end
118
+
119
+ if (pop_i && ~empty_o) begin
120
+ data_ft_n = data_i;
121
+ if (FPGA_EN && FPGA_ALTERA) first_word_n = first_word_q && push_i;
122
+ // read from the queue is a default assignment
123
+ // but increment the read pointer...
124
+ if (read_pointer_n == FifoDepth[ADDR_DEPTH-1:0] - 1) read_pointer_n = '0;
125
+ else read_pointer_n = read_pointer_q + 1;
126
+ // ... and decrement the overall count
127
+ status_cnt_n = status_cnt_q - 1;
128
+ end
129
+
130
+ // keep the count pointer stable if we push and pop at the same time
131
+ if (push_i && pop_i && ~full_o && ~empty_o) status_cnt_n = status_cnt_q;
132
+
133
+ // FIFO is in pass through mode -> do not change the pointers
134
+ if ((FALL_THROUGH || (FPGA_EN && FPGA_ALTERA)) && (status_cnt_q == 0) && push_i) begin
135
+ if (FALL_THROUGH) data_o = data_i;
136
+ if (FPGA_EN && FPGA_ALTERA) begin
137
+ data_ft_n = data_i;
138
+ first_word_n = '1;
139
+ end
140
+ if (pop_i) begin
141
+ first_word_n = '0;
142
+ status_cnt_n = status_cnt_q;
143
+ read_pointer_n = read_pointer_q;
144
+ write_pointer_n = write_pointer_q;
145
+ end
146
+ end
147
+
148
+ if (FPGA_EN) fifo_ram_read_address = (FPGA_ALTERA == 1) ? read_pointer_n : read_pointer_q;
149
+ else fifo_ram_read_address = '0;
150
+
151
+ end
152
+
153
+ // sequential process
154
+ always_ff @(posedge clk_i or negedge rst_ni) begin
155
+ if (~rst_ni) begin
156
+ read_pointer_q <= '0;
157
+ write_pointer_q <= '0;
158
+ status_cnt_q <= '0;
159
+ if (FPGA_ALTERA) first_word_q <= '0;
160
+ if (FPGA_ALTERA) data_ft_q <= '0;
161
+ end else begin
162
+ if (flush_i) begin
163
+ read_pointer_q <= '0;
164
+ write_pointer_q <= '0;
165
+ status_cnt_q <= '0;
166
+ if (FPGA_ALTERA) first_word_q <= '0;
167
+ if (FPGA_ALTERA) data_ft_q <= '0;
168
+ end else begin
169
+ read_pointer_q <= read_pointer_n;
170
+ write_pointer_q <= write_pointer_n;
171
+ status_cnt_q <= status_cnt_n;
172
+ if (FPGA_ALTERA) data_ft_q <= data_ft_n;
173
+ if (FPGA_ALTERA) first_word_q <= first_word_n;
174
+ end
175
+ end
176
+ end
177
+
178
+ if (FPGA_EN) begin : gen_fpga_queue
179
+ if (FPGA_ALTERA) begin
180
+ SyncDpRam_ind_r_w #(
181
+ .ADDR_WIDTH(ADDR_DEPTH),
182
+ .DATA_DEPTH(DEPTH),
183
+ .DATA_WIDTH($bits(dtype))
184
+ ) fifo_ram (
185
+ .Clk_CI (clk_i),
186
+ .WrEn_SI (fifo_ram_we),
187
+ .RdAddr_DI(fifo_ram_read_address),
188
+ .WrAddr_DI(fifo_ram_write_address),
189
+ .WrData_DI(fifo_ram_wdata),
190
+ .RdData_DO(fifo_ram_rdata)
191
+ );
192
+ end else begin
193
+ AsyncDpRam #(
194
+ .ADDR_WIDTH(ADDR_DEPTH),
195
+ .DATA_DEPTH(DEPTH),
196
+ .DATA_WIDTH($bits(dtype))
197
+ ) fifo_ram (
198
+ .Clk_CI (clk_i),
199
+ .WrEn_SI (fifo_ram_we),
200
+ .RdAddr_DI(fifo_ram_read_address),
201
+ .WrAddr_DI(fifo_ram_write_address),
202
+ .WrData_DI(fifo_ram_wdata),
203
+ .RdData_DO(fifo_ram_rdata)
204
+ );
205
+ end
206
+ end else begin : gen_asic_queue
207
+ always_ff @(posedge clk_i or negedge rst_ni) begin
208
+ if (~rst_ni) begin
209
+ mem_q <= '0;
210
+ end else if (!gate_clock) begin
211
+ mem_q <= mem_n;
212
+ end
213
+ end
214
+ end
215
+
216
+ // pragma translate_off
217
+ initial begin
218
+ assert (DEPTH > 0)
219
+ else $error("DEPTH must be greater than 0.");
220
+ end
221
+
222
+ full_write :
223
+ assert property (@(posedge clk_i) disable iff (~rst_ni) (full_o |-> ~push_i))
224
+ else $fatal(1, "Trying to push new data although the FIFO is full.");
225
+
226
+ empty_read :
227
+ assert property (@(posedge clk_i) disable iff (~rst_ni) (empty_o |-> ~pop_i))
228
+ else $fatal(1, "Trying to pop data although the FIFO is empty.");
229
+ // pragma translate_on
230
+
231
+ endmodule // fifo_v3
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_mmu.sv ADDED
@@ -0,0 +1,764 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright (c) 2018 ETH Zurich and University of Bologna.
2
+ // Copyright (c) 2021 Thales.
3
+ // Copyright (c) 2022 Bruno Sá and Zero-Day Labs.
4
+ // Copyright (c) 2024 PlanV Technology
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
6
+ // Copyright and related rights are licensed under the Solderpad Hardware
7
+ // License, Version 0.51 (the "License"); you may not use this file except in
8
+ // compliance with the License. You may obtain a copy of the License at
9
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
10
+ // or agreed to in writing, software, hardware and materials distributed under
11
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
+ // specific language governing permissions and limitations under the License.
14
+ //
15
+ // Author: Angela Gonzalez, PlanV Technology
16
+ // Date: 26/02/2024
17
+ //
18
+ // Description: Memory Management Unit for CVA6, contains TLB and
19
+ // address translation unit. SV32 SV39 and SV39x4 as defined in RISC-V
20
+ // privilege specification 1.11-WIP.
21
+ // This module is an merge of the MMU Sv39 developed
22
+ // by Florian Zaruba, the MMU Sv32 developed by Sebastien Jacq and the MMU Sv39x4 developed by Bruno Sá.
23
+
24
+
25
+ module cva6_mmu
26
+ import ariane_pkg::*;
27
+ #(
28
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
29
+ parameter type icache_areq_t = logic,
30
+ parameter type icache_arsp_t = logic,
31
+ parameter type icache_dreq_t = logic,
32
+ parameter type icache_drsp_t = logic,
33
+ parameter type dcache_req_i_t = logic,
34
+ parameter type dcache_req_o_t = logic,
35
+ parameter type exception_t = logic,
36
+ parameter int unsigned HYP_EXT = 0
37
+
38
+ ) (
39
+ input logic clk_i,
40
+ input logic rst_ni,
41
+ input logic flush_i,
42
+ input logic enable_translation_i,
43
+ input logic enable_g_translation_i,
44
+ input logic en_ld_st_translation_i, // enable virtual memory translation for load/stores
45
+ input logic en_ld_st_g_translation_i, // enable G-Stage translation for load/stores
46
+ // IF interface
47
+ input icache_arsp_t icache_areq_i,
48
+ output icache_areq_t icache_areq_o,
49
+ // LSU interface
50
+ // this is a more minimalistic interface because the actual addressing logic is handled
51
+ // in the LSU as we distinguish load and stores, what we do here is simple address translation
52
+ input exception_t misaligned_ex_i,
53
+ input logic lsu_req_i, // request address translation
54
+ input logic [CVA6Cfg.VLEN-1:0] lsu_vaddr_i, // virtual address in
55
+ input logic [31:0] lsu_tinst_i, // transformed instruction in
56
+ input logic lsu_is_store_i, // the translation is requested by a store
57
+ output logic csr_hs_ld_st_inst_o, // hyp load store instruction
58
+ // if we need to walk the page table we can't grant in the same cycle
59
+ // Cycle 0
60
+ output logic lsu_dtlb_hit_o, // sent in same cycle as the request if translation hits in DTLB
61
+ output logic [CVA6Cfg.PPNW-1:0] lsu_dtlb_ppn_o, // ppn (send same cycle as hit)
62
+ // Cycle 1
63
+ output logic lsu_valid_o, // translation is valid
64
+ output logic [CVA6Cfg.PLEN-1:0] lsu_paddr_o, // translated address
65
+ output exception_t lsu_exception_o, // address translation threw an exception
66
+ // General control signals
67
+ input riscv::priv_lvl_t priv_lvl_i,
68
+ input logic v_i,
69
+ input riscv::priv_lvl_t ld_st_priv_lvl_i,
70
+ input logic ld_st_v_i,
71
+ input logic sum_i,
72
+ input logic vs_sum_i,
73
+ input logic mxr_i,
74
+ input logic vmxr_i,
75
+ input logic hlvx_inst_i,
76
+ input logic hs_ld_st_inst_i,
77
+ // input logic flag_mprv_i,
78
+ input logic [CVA6Cfg.PPNW-1:0] satp_ppn_i,
79
+ input logic [CVA6Cfg.PPNW-1:0] vsatp_ppn_i,
80
+ input logic [CVA6Cfg.PPNW-1:0] hgatp_ppn_i,
81
+
82
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] asid_i,
83
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] vs_asid_i,
84
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] asid_to_be_flushed_i,
85
+ input logic [CVA6Cfg.VMID_WIDTH-1:0] vmid_i,
86
+ input logic [CVA6Cfg.VMID_WIDTH-1:0] vmid_to_be_flushed_i,
87
+ input logic [CVA6Cfg.VLEN-1:0] vaddr_to_be_flushed_i,
88
+ input logic [CVA6Cfg.GPLEN-1:0] gpaddr_to_be_flushed_i,
89
+
90
+ input logic flush_tlb_i,
91
+ input logic flush_tlb_vvma_i,
92
+ input logic flush_tlb_gvma_i,
93
+
94
+ // Performance counters
95
+ output logic itlb_miss_o,
96
+ output logic dtlb_miss_o,
97
+ // PTW memory interface
98
+ input dcache_req_o_t req_port_i,
99
+ output dcache_req_i_t req_port_o,
100
+
101
+ // PMP
102
+
103
+ input riscv::pmpcfg_t [avoid_neg(CVA6Cfg.NrPMPEntries-1):0] pmpcfg_i,
104
+ input logic [avoid_neg(CVA6Cfg.NrPMPEntries-1):0][CVA6Cfg.PLEN-3:0] pmpaddr_i
105
+ );
106
+
107
+ // memory management, pte for cva6
108
+ localparam type pte_cva6_t = struct packed {
109
+ logic [9:0] reserved;
110
+ logic [CVA6Cfg.PPNW-1:0] ppn; // PPN length for
111
+ logic [1:0] rsw;
112
+ logic d;
113
+ logic a;
114
+ logic g;
115
+ logic u;
116
+ logic x;
117
+ logic w;
118
+ logic r;
119
+ logic v;
120
+ };
121
+
122
+ localparam type tlb_update_cva6_t = struct packed {
123
+ logic valid;
124
+ logic [CVA6Cfg.PtLevels-2:0][HYP_EXT:0] is_page;
125
+ logic [CVA6Cfg.VpnLen-1:0] vpn;
126
+ logic [CVA6Cfg.ASID_WIDTH-1:0] asid;
127
+ logic [CVA6Cfg.VMID_WIDTH-1:0] vmid;
128
+ logic [HYP_EXT*2:0] v_st_enbl; // v_i,g-stage enabled, s-stage enabled
129
+ pte_cva6_t content;
130
+ pte_cva6_t g_content;
131
+ };
132
+
133
+ logic iaccess_err; // insufficient privilege to access this instruction page
134
+ logic i_g_st_access_err; // insufficient privilege at g stage to access this instruction page
135
+ logic daccess_err; // insufficient privilege to access this data page
136
+ logic canonical_addr_check; // canonical check on the virtual address for SV39
137
+ logic d_g_st_access_err; // insufficient privilege to access this data page
138
+ logic ptw_active; // PTW is currently walking a page table
139
+ logic walking_instr; // PTW is walking because of an ITLB miss
140
+ logic ptw_error; // PTW threw an exception
141
+ logic ptw_error_at_g_st; // PTW threw an exception at the G-Stage
142
+ logic ptw_err_at_g_int_st; // PTW threw an exception at the G-Stage during S-Stage translation
143
+ logic ptw_access_exception; // PTW threw an access exception (PMPs)
144
+ logic [CVA6Cfg.PLEN-1:0] ptw_bad_paddr; // PTW page fault bad physical addr
145
+ logic [CVA6Cfg.GPLEN-1:0] ptw_bad_gpaddr; // PTW guest page fault bad guest physical addr
146
+
147
+ logic [CVA6Cfg.VLEN-1:0] update_vaddr, shared_tlb_vaddr;
148
+
149
+ tlb_update_cva6_t update_itlb, update_dtlb, update_shared_tlb;
150
+
151
+ logic itlb_lu_access;
152
+ pte_cva6_t itlb_content;
153
+ pte_cva6_t itlb_g_content;
154
+ logic [ CVA6Cfg.PtLevels-2:0] itlb_is_page;
155
+ logic itlb_lu_hit;
156
+ logic [ CVA6Cfg.GPLEN-1:0] itlb_gpaddr;
157
+ logic [CVA6Cfg.ASID_WIDTH-1:0] itlb_lu_asid;
158
+
159
+ logic dtlb_lu_access;
160
+ pte_cva6_t dtlb_content;
161
+ pte_cva6_t dtlb_g_content;
162
+ logic [ CVA6Cfg.PtLevels-2:0] dtlb_is_page;
163
+ logic [CVA6Cfg.ASID_WIDTH-1:0] dtlb_lu_asid;
164
+ logic dtlb_lu_hit;
165
+ logic [ CVA6Cfg.GPLEN-1:0] dtlb_gpaddr;
166
+
167
+ logic shared_tlb_access, shared_tlb_miss;
168
+ logic shared_tlb_hit, itlb_req;
169
+
170
+ // Assignments
171
+
172
+ assign itlb_lu_access = icache_areq_i.fetch_req;
173
+ assign dtlb_lu_access = lsu_req_i;
174
+ assign itlb_lu_asid = v_i ? vs_asid_i : asid_i;
175
+ assign dtlb_lu_asid = (ld_st_v_i || flush_tlb_vvma_i) ? vs_asid_i : asid_i;
176
+
177
+
178
+ cva6_tlb #(
179
+ .CVA6Cfg (CVA6Cfg),
180
+ .pte_cva6_t (pte_cva6_t),
181
+ .tlb_update_cva6_t(tlb_update_cva6_t),
182
+ .TLB_ENTRIES (CVA6Cfg.InstrTlbEntries),
183
+ .HYP_EXT (HYP_EXT)
184
+ ) i_itlb (
185
+ .clk_i (clk_i),
186
+ .rst_ni (rst_ni),
187
+ .flush_i (flush_tlb_i),
188
+ .flush_vvma_i (flush_tlb_vvma_i),
189
+ .flush_gvma_i (flush_tlb_gvma_i),
190
+ .s_st_enbl_i (enable_translation_i),
191
+ .g_st_enbl_i (enable_g_translation_i),
192
+ .v_i (v_i),
193
+ .update_i (update_itlb),
194
+ .lu_access_i (itlb_lu_access),
195
+ .lu_asid_i (itlb_lu_asid),
196
+ .lu_vmid_i (vmid_i),
197
+ .lu_vaddr_i (icache_areq_i.fetch_vaddr),
198
+ .lu_gpaddr_o (itlb_gpaddr),
199
+ .lu_content_o (itlb_content),
200
+ .lu_g_content_o(itlb_g_content),
201
+ .asid_to_be_flushed_i,
202
+ .vmid_to_be_flushed_i,
203
+ .vaddr_to_be_flushed_i,
204
+ .gpaddr_to_be_flushed_i,
205
+ .lu_is_page_o (itlb_is_page),
206
+ .lu_hit_o (itlb_lu_hit)
207
+ );
208
+
209
+ cva6_tlb #(
210
+ .CVA6Cfg (CVA6Cfg),
211
+ .pte_cva6_t (pte_cva6_t),
212
+ .tlb_update_cva6_t(tlb_update_cva6_t),
213
+ .TLB_ENTRIES (CVA6Cfg.DataTlbEntries),
214
+ .HYP_EXT (HYP_EXT)
215
+ ) i_dtlb (
216
+ .clk_i (clk_i),
217
+ .rst_ni (rst_ni),
218
+ .flush_i (flush_tlb_i),
219
+ .flush_vvma_i (flush_tlb_vvma_i),
220
+ .flush_gvma_i (flush_tlb_gvma_i),
221
+ .s_st_enbl_i (en_ld_st_translation_i),
222
+ .g_st_enbl_i (en_ld_st_g_translation_i),
223
+ .v_i (ld_st_v_i),
224
+ .update_i (update_dtlb),
225
+ .lu_access_i (dtlb_lu_access),
226
+ .lu_asid_i (itlb_lu_asid),
227
+ .lu_vmid_i (vmid_i),
228
+ .lu_vaddr_i (lsu_vaddr_i),
229
+ .lu_gpaddr_o (dtlb_gpaddr),
230
+ .lu_content_o (dtlb_content),
231
+ .lu_g_content_o(dtlb_g_content),
232
+ .asid_to_be_flushed_i,
233
+ .vmid_to_be_flushed_i,
234
+ .vaddr_to_be_flushed_i,
235
+ .gpaddr_to_be_flushed_i,
236
+ .lu_is_page_o (dtlb_is_page),
237
+ .lu_hit_o (dtlb_lu_hit)
238
+ );
239
+
240
+
241
+ cva6_shared_tlb #(
242
+ .CVA6Cfg (CVA6Cfg),
243
+ .SHARED_TLB_WAYS (2),
244
+ .HYP_EXT (HYP_EXT),
245
+ .pte_cva6_t (pte_cva6_t),
246
+ .tlb_update_cva6_t(tlb_update_cva6_t)
247
+ ) i_shared_tlb (
248
+ .clk_i (clk_i),
249
+ .rst_ni (rst_ni),
250
+ .flush_i (flush_tlb_i),
251
+ .flush_vvma_i (flush_tlb_vvma_i),
252
+ .flush_gvma_i (flush_tlb_gvma_i),
253
+ .s_st_enbl_i (enable_translation_i),
254
+ .g_st_enbl_i (enable_g_translation_i),
255
+ .v_i (v_i),
256
+ .s_ld_st_enbl_i(en_ld_st_translation_i),
257
+ .g_ld_st_enbl_i(en_ld_st_g_translation_i),
258
+ .ld_st_v_i (ld_st_v_i),
259
+
260
+ .dtlb_asid_i (dtlb_lu_asid),
261
+ .itlb_asid_i (itlb_lu_asid),
262
+ .lu_vmid_i (vmid_i),
263
+ // from TLBs
264
+ // did we miss?
265
+ .itlb_access_i(itlb_lu_access),
266
+ .itlb_hit_i (itlb_lu_hit),
267
+ .itlb_vaddr_i (icache_areq_i.fetch_vaddr),
268
+
269
+ .dtlb_access_i(dtlb_lu_access),
270
+ .dtlb_hit_i (dtlb_lu_hit),
271
+ .dtlb_vaddr_i (lsu_vaddr_i),
272
+
273
+ // to TLBs, update logic
274
+ .itlb_update_o(update_itlb),
275
+ .dtlb_update_o(update_dtlb),
276
+
277
+ // Performance counters
278
+ .itlb_miss_o(itlb_miss_o),
279
+ .dtlb_miss_o(dtlb_miss_o),
280
+ .shared_tlb_miss_i(shared_tlb_miss),
281
+
282
+ .shared_tlb_access_o(shared_tlb_access),
283
+ .shared_tlb_hit_o (shared_tlb_hit),
284
+ .shared_tlb_vaddr_o (shared_tlb_vaddr),
285
+
286
+ .itlb_req_o (itlb_req),
287
+ // to update shared tlb
288
+ .shared_tlb_update_i(update_shared_tlb)
289
+ );
290
+
291
+ cva6_ptw #(
292
+ .CVA6Cfg (CVA6Cfg),
293
+ .pte_cva6_t (pte_cva6_t),
294
+ .tlb_update_cva6_t(tlb_update_cva6_t),
295
+ .dcache_req_i_t (dcache_req_i_t),
296
+ .dcache_req_o_t (dcache_req_o_t),
297
+ .HYP_EXT (HYP_EXT)
298
+ ) i_ptw (
299
+ .clk_i (clk_i),
300
+ .rst_ni(rst_ni),
301
+ .flush_i,
302
+
303
+ .ptw_active_o (ptw_active),
304
+ .walking_instr_o (walking_instr),
305
+ .ptw_error_o (ptw_error),
306
+ .ptw_error_at_g_st_o (ptw_error_at_g_st),
307
+ .ptw_err_at_g_int_st_o (ptw_err_at_g_int_st),
308
+ .ptw_access_exception_o(ptw_access_exception),
309
+ .enable_translation_i,
310
+ .enable_g_translation_i,
311
+ .en_ld_st_translation_i,
312
+ .en_ld_st_g_translation_i,
313
+ .v_i,
314
+ .ld_st_v_i,
315
+ .hlvx_inst_i (hlvx_inst_i),
316
+
317
+ .lsu_is_store_i(lsu_is_store_i),
318
+ // PTW memory interface
319
+ .req_port_i (req_port_i),
320
+ .req_port_o (req_port_o),
321
+
322
+ // to Shared TLB, update logic
323
+ .shared_tlb_update_o(update_shared_tlb),
324
+
325
+ .update_vaddr_o(update_vaddr),
326
+
327
+ .asid_i,
328
+ .vs_asid_i,
329
+ .vmid_i,
330
+
331
+ // from shared TLB
332
+ // did we miss?
333
+ .shared_tlb_access_i(shared_tlb_access),
334
+ .shared_tlb_hit_i (shared_tlb_hit),
335
+ .shared_tlb_vaddr_i (shared_tlb_vaddr),
336
+
337
+ .itlb_req_i(itlb_req),
338
+
339
+ .satp_ppn_i,
340
+ .vsatp_ppn_i,
341
+ .hgatp_ppn_i,
342
+ .mxr_i,
343
+ .vmxr_i,
344
+
345
+ // Performance counters
346
+ .shared_tlb_miss_o(shared_tlb_miss), //open for now
347
+
348
+ // PMP
349
+ .pmpcfg_i (pmpcfg_i),
350
+ .pmpaddr_i (pmpaddr_i),
351
+ .bad_paddr_o(ptw_bad_paddr),
352
+ .bad_gpaddr_o(ptw_bad_gpaddr)
353
+ );
354
+
355
+ //-----------------------
356
+ // Instruction Interface
357
+ //-----------------------
358
+ localparam int PPNWMin = (CVA6Cfg.PPNW - 1 > 29) ? 29 : CVA6Cfg.PPNW - 1;
359
+
360
+ // The instruction interface is a simple request response interface
361
+ always_comb begin : instr_interface
362
+ // MMU disabled: just pass through
363
+ icache_areq_o.fetch_valid = icache_areq_i.fetch_req;
364
+ icache_areq_o.fetch_paddr = CVA6Cfg.PLEN'(icache_areq_i.fetch_vaddr[((CVA6Cfg.PLEN > CVA6Cfg.VLEN) ? CVA6Cfg.VLEN -1: CVA6Cfg.PLEN -1 ):0]);
365
+ // two potential exception sources:
366
+ // 1. HPTW threw an exception -> signal with a page fault exception
367
+ // 2. We got an access error because of insufficient permissions -> throw an access exception
368
+ icache_areq_o.fetch_exception = '0;
369
+ // Check whether we are allowed to access this memory region from a fetch perspective
370
+ iaccess_err = icache_areq_i.fetch_req && enable_translation_i && //
371
+ (((priv_lvl_i == riscv::PRIV_LVL_U) && ~itlb_content.u) //
372
+ || ((priv_lvl_i == riscv::PRIV_LVL_S) && itlb_content.u));
373
+
374
+ if (CVA6Cfg.RVH)
375
+ i_g_st_access_err = icache_areq_i.fetch_req && enable_g_translation_i && !itlb_g_content.u;
376
+ // MMU enabled: address from TLB, request delayed until hit. Error when TLB
377
+ // hit and no access right or TLB hit and translated address not valid (e.g.
378
+ // AXI decode error), or when PTW performs walk due to ITLB miss and raises
379
+ // an error.
380
+ if ((enable_translation_i || enable_g_translation_i)) begin
381
+ // we work with SV39 or SV32, so if VM is enabled, check that all bits [CVA6Cfg.VLEN-1:CVA6Cfg.SV-1] are equal
382
+ if (icache_areq_i.fetch_req && !((&icache_areq_i.fetch_vaddr[CVA6Cfg.VLEN-1:CVA6Cfg.SV-1]) == 1'b1 || (|icache_areq_i.fetch_vaddr[CVA6Cfg.VLEN-1:CVA6Cfg.SV-1]) == 1'b0)) begin
383
+
384
+ icache_areq_o.fetch_exception.cause = riscv::INSTR_PAGE_FAULT;
385
+ icache_areq_o.fetch_exception.valid = 1'b1;
386
+ if (CVA6Cfg.TvalEn)
387
+ icache_areq_o.fetch_exception.tval = CVA6Cfg.XLEN'(icache_areq_i.fetch_vaddr);
388
+ if (CVA6Cfg.RVH) begin
389
+ icache_areq_o.fetch_exception.tval2 = '0;
390
+ icache_areq_o.fetch_exception.tinst = '0;
391
+ icache_areq_o.fetch_exception.gva = v_i;
392
+ end
393
+ end
394
+
395
+ icache_areq_o.fetch_valid = 1'b0;
396
+
397
+ icache_areq_o.fetch_paddr = {
398
+ (enable_g_translation_i && CVA6Cfg.RVH) ? itlb_g_content.ppn : itlb_content.ppn,
399
+ icache_areq_i.fetch_vaddr[11:0]
400
+ };
401
+
402
+ if (CVA6Cfg.PtLevels == 3 && itlb_is_page[CVA6Cfg.PtLevels-2]) begin
403
+
404
+ icache_areq_o.fetch_paddr[PPNWMin-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):9+CVA6Cfg.PtLevels] = icache_areq_i.fetch_vaddr[PPNWMin-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):9+CVA6Cfg.PtLevels];
405
+
406
+ end
407
+
408
+ if (itlb_is_page[0]) begin
409
+
410
+ icache_areq_o.fetch_paddr[PPNWMin:12] = icache_areq_i.fetch_vaddr[PPNWMin:12];
411
+
412
+ end
413
+ // ---------//
414
+ // ITLB Hit
415
+ // --------//
416
+ // if we hit the ITLB output the request signal immediately
417
+ if (itlb_lu_hit) begin
418
+ icache_areq_o.fetch_valid = icache_areq_i.fetch_req;
419
+ if (CVA6Cfg.RVH && i_g_st_access_err) begin
420
+ icache_areq_o.fetch_exception.cause = riscv::INSTR_GUEST_PAGE_FAULT;
421
+ icache_areq_o.fetch_exception.valid = 1'b1;
422
+ if (CVA6Cfg.TvalEn)
423
+ icache_areq_o.fetch_exception.tval = CVA6Cfg.XLEN'(icache_areq_i.fetch_vaddr);
424
+ if (CVA6Cfg.RVH) begin
425
+ icache_areq_o.fetch_exception.tval2 = itlb_gpaddr[CVA6Cfg.GPLEN-1:0];
426
+ icache_areq_o.fetch_exception.tinst = '0;
427
+ icache_areq_o.fetch_exception.gva = v_i;
428
+ end
429
+
430
+ // we got an access error
431
+ end else if (iaccess_err) begin
432
+ // throw a page fault
433
+ icache_areq_o.fetch_exception.cause = riscv::INSTR_PAGE_FAULT;
434
+ icache_areq_o.fetch_exception.valid = 1'b1;
435
+ if (CVA6Cfg.TvalEn)
436
+ icache_areq_o.fetch_exception.tval = CVA6Cfg.XLEN'(icache_areq_i.fetch_vaddr);
437
+ if (CVA6Cfg.RVH) begin
438
+ icache_areq_o.fetch_exception.tval2 = '0;
439
+ icache_areq_o.fetch_exception.tinst = '0;
440
+ icache_areq_o.fetch_exception.gva = v_i;
441
+ end
442
+ end
443
+ end else if (ptw_active && walking_instr) begin
444
+ // ---------//
445
+ // ITLB Miss
446
+ // ---------//
447
+ // watch out for exceptions happening during walking the page table
448
+ icache_areq_o.fetch_valid = ptw_error | ptw_access_exception;
449
+ if (ptw_error) begin
450
+ if (CVA6Cfg.RVH && ptw_error_at_g_st) begin
451
+ icache_areq_o.fetch_exception.cause = riscv::INSTR_GUEST_PAGE_FAULT;
452
+ icache_areq_o.fetch_exception.valid = 1'b1;
453
+ if (CVA6Cfg.TvalEn) icache_areq_o.fetch_exception.tval = CVA6Cfg.XLEN'(update_vaddr);
454
+ if (CVA6Cfg.RVH) begin
455
+ icache_areq_o.fetch_exception.tval2 = ptw_bad_gpaddr[CVA6Cfg.GPLEN-1:0];
456
+ icache_areq_o.fetch_exception.tinst=(ptw_err_at_g_int_st ? (CVA6Cfg.IS_XLEN64 ? riscv::READ_64_PSEUDOINSTRUCTION : riscv::READ_32_PSEUDOINSTRUCTION) : '0);
457
+ icache_areq_o.fetch_exception.gva = v_i;
458
+ end
459
+ end else begin
460
+ icache_areq_o.fetch_exception.cause = riscv::INSTR_PAGE_FAULT;
461
+ icache_areq_o.fetch_exception.valid = 1'b1;
462
+ if (CVA6Cfg.TvalEn) icache_areq_o.fetch_exception.tval = CVA6Cfg.XLEN'(update_vaddr);
463
+ if (CVA6Cfg.RVH) begin
464
+ icache_areq_o.fetch_exception.tval2 = '0;
465
+ icache_areq_o.fetch_exception.tinst = '0;
466
+ icache_areq_o.fetch_exception.gva = v_i;
467
+ end
468
+ end
469
+ end else begin
470
+ icache_areq_o.fetch_exception.cause = riscv::INSTR_ACCESS_FAULT;
471
+ icache_areq_o.fetch_exception.valid = 1'b1;
472
+ if (CVA6Cfg.TvalEn) //To confirm this is the right TVAL
473
+ icache_areq_o.fetch_exception.tval = CVA6Cfg.XLEN'(update_vaddr);
474
+ if (CVA6Cfg.RVH) begin
475
+ icache_areq_o.fetch_exception.tval2 = '0;
476
+ icache_areq_o.fetch_exception.tinst = '0;
477
+ icache_areq_o.fetch_exception.gva = v_i;
478
+ end
479
+ end
480
+ end
481
+ end
482
+ end
483
+
484
+
485
+ //-----------------------
486
+ // Data Interface
487
+ //-----------------------
488
+ logic [CVA6Cfg.VLEN-1:0] lsu_vaddr_n, lsu_vaddr_q;
489
+ logic [CVA6Cfg.GPLEN-1:0] lsu_gpaddr_n, lsu_gpaddr_q;
490
+ logic [31:0] lsu_tinst_n, lsu_tinst_q;
491
+ logic hs_ld_st_inst_n, hs_ld_st_inst_q;
492
+ pte_cva6_t dtlb_pte_n, dtlb_pte_q;
493
+ pte_cva6_t dtlb_gpte_n, dtlb_gpte_q;
494
+ logic lsu_req_n, lsu_req_q;
495
+ logic lsu_is_store_n, lsu_is_store_q;
496
+ logic dtlb_hit_n, dtlb_hit_q;
497
+ logic [CVA6Cfg.PtLevels-2:0] dtlb_is_page_n, dtlb_is_page_q;
498
+ exception_t misaligned_ex_n, misaligned_ex_q;
499
+
500
+ // check if we need to do translation or if we are always ready (e.g.: we are not translating anything)
501
+ assign lsu_dtlb_hit_o = (en_ld_st_translation_i || en_ld_st_g_translation_i) ? dtlb_lu_hit : 1'b1;
502
+
503
+
504
+ // The data interface is simpler and only consists of a request/response interface
505
+ always_comb begin : data_interface
506
+ // save request and DTLB response
507
+ lsu_vaddr_n = lsu_vaddr_i;
508
+ lsu_req_n = lsu_req_i;
509
+ dtlb_pte_n = dtlb_content;
510
+ dtlb_hit_n = dtlb_lu_hit;
511
+ lsu_is_store_n = lsu_is_store_i;
512
+ dtlb_is_page_n = dtlb_is_page;
513
+ misaligned_ex_n = misaligned_ex_i;
514
+
515
+ lsu_valid_o = lsu_req_q;
516
+ lsu_exception_o = misaligned_ex_q;
517
+
518
+ // mute misaligned exceptions if there is no request otherwise they will throw accidental exceptions
519
+ misaligned_ex_n.valid = misaligned_ex_i.valid & lsu_req_i;
520
+
521
+ // we work with SV39 or SV32, so if VM is enabled, check that all bits [CVA6Cfg.VLEN-1:CVA6Cfg.SV-1] are equal to bit [CVA6Cfg.SV]
522
+ canonical_addr_check = (lsu_req_i && en_ld_st_translation_i &&
523
+ !((&lsu_vaddr_i[CVA6Cfg.VLEN-1:CVA6Cfg.SV-1]) == 1'b1 || (|lsu_vaddr_i[CVA6Cfg.VLEN-1:CVA6Cfg.SV-1]) == 1'b0));
524
+
525
+ // Check if the User flag is set, then we may only access it in supervisor mode
526
+ // if SUM is enabled
527
+ daccess_err = en_ld_st_translation_i &&
528
+ ((ld_st_priv_lvl_i == riscv::PRIV_LVL_S && (ld_st_v_i ? !vs_sum_i : !sum_i ) && dtlb_pte_q.u) || // SUM is not set and we are trying to access a user page in supervisor mode
529
+ (ld_st_priv_lvl_i == riscv::PRIV_LVL_U && !dtlb_pte_q.u));
530
+
531
+ csr_hs_ld_st_inst_o = 1'b0;
532
+
533
+ if (CVA6Cfg.RVH) begin
534
+ lsu_tinst_n = lsu_tinst_i;
535
+ hs_ld_st_inst_n = hs_ld_st_inst_i;
536
+ lsu_gpaddr_n[(CVA6Cfg.XLEN == 32 ? CVA6Cfg.VLEN: CVA6Cfg.GPLEN)-1:0] = dtlb_gpaddr[(CVA6Cfg.XLEN == 32 ? CVA6Cfg.VLEN: CVA6Cfg.GPLEN)-1:0];
537
+ csr_hs_ld_st_inst_o = hs_ld_st_inst_i || hs_ld_st_inst_q;
538
+ d_g_st_access_err = en_ld_st_g_translation_i && !dtlb_gpte_q.u;
539
+ dtlb_gpte_n = dtlb_g_content;
540
+ end
541
+
542
+ lsu_paddr_o = (CVA6Cfg.PLEN)'(lsu_vaddr_q[((CVA6Cfg.PLEN > CVA6Cfg.VLEN) ? CVA6Cfg.VLEN -1: CVA6Cfg.PLEN -1 ):0]);
543
+ lsu_dtlb_ppn_o = (CVA6Cfg.PPNW)'(lsu_vaddr_n[((CVA6Cfg.PLEN > CVA6Cfg.VLEN) ? CVA6Cfg.VLEN -1: CVA6Cfg.PLEN -1 ):12]);
544
+
545
+ // translation is enabled and no misaligned exception occurred
546
+ if ((en_ld_st_translation_i || en_ld_st_g_translation_i) && !misaligned_ex_q.valid) begin
547
+ lsu_valid_o = 1'b0;
548
+
549
+ lsu_dtlb_ppn_o = (en_ld_st_g_translation_i && CVA6Cfg.RVH)? dtlb_g_content.ppn :dtlb_content.ppn;
550
+ lsu_paddr_o = {
551
+ (en_ld_st_g_translation_i && CVA6Cfg.RVH) ? dtlb_gpte_q.ppn : dtlb_pte_q.ppn,
552
+ lsu_vaddr_q[11:0]
553
+ };
554
+
555
+ if (CVA6Cfg.PtLevels == 3 && dtlb_is_page_q[CVA6Cfg.PtLevels-2]) begin
556
+ lsu_paddr_o[PPNWMin-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):9+CVA6Cfg.PtLevels] = lsu_vaddr_q[PPNWMin-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):9+CVA6Cfg.PtLevels];
557
+ lsu_dtlb_ppn_o[PPNWMin-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):9+CVA6Cfg.PtLevels] = lsu_vaddr_n[PPNWMin-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):9+CVA6Cfg.PtLevels];
558
+ end
559
+
560
+ if (dtlb_is_page_q[0]) begin
561
+ lsu_dtlb_ppn_o[PPNWMin:12] = lsu_vaddr_n[PPNWMin:12];
562
+ lsu_paddr_o[PPNWMin:12] = lsu_vaddr_q[PPNWMin:12];
563
+ end
564
+
565
+
566
+
567
+ // ---------
568
+ // DTLB Hit
569
+ // --------
570
+ if (dtlb_hit_q && lsu_req_q) begin
571
+ lsu_valid_o = 1'b1;
572
+ // exception priority:
573
+ // PAGE_FAULTS have higher priority than ACCESS_FAULTS
574
+ // virtual memory based exceptions are PAGE_FAULTS
575
+ // physical memory based exceptions are ACCESS_FAULTS (PMA/PMP)
576
+
577
+ // this is a store
578
+ if (lsu_is_store_q) begin
579
+ // check if the page is write-able and we are not violating privileges
580
+ // also check if the dirty flag is set
581
+ if(CVA6Cfg.RVH && en_ld_st_g_translation_i && (!dtlb_gpte_q.w || d_g_st_access_err || !dtlb_gpte_q.d)) begin
582
+ lsu_exception_o.cause = riscv::STORE_GUEST_PAGE_FAULT;
583
+ lsu_exception_o.valid = 1'b1;
584
+ if (CVA6Cfg.TvalEn)
585
+ lsu_exception_o.tval = {
586
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, lsu_vaddr_q
587
+ };
588
+ if (CVA6Cfg.RVH) begin
589
+ lsu_exception_o.tval2= CVA6Cfg.GPLEN'(lsu_gpaddr_q[(CVA6Cfg.XLEN==32?CVA6Cfg.VLEN : CVA6Cfg.GPLEN)-1:0]);
590
+ lsu_exception_o.tinst = '0;
591
+ lsu_exception_o.gva = ld_st_v_i;
592
+ end
593
+ end else if ((en_ld_st_translation_i || !CVA6Cfg.RVH) && (!dtlb_pte_q.w || daccess_err || canonical_addr_check || !dtlb_pte_q.d)) begin
594
+ lsu_exception_o.cause = riscv::STORE_PAGE_FAULT;
595
+ lsu_exception_o.valid = 1'b1;
596
+ if (CVA6Cfg.TvalEn)
597
+ lsu_exception_o.tval = {
598
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, lsu_vaddr_q
599
+ };
600
+ if (CVA6Cfg.RVH) begin
601
+ lsu_exception_o.tval2 = '0;
602
+ lsu_exception_o.tinst = lsu_tinst_q;
603
+ lsu_exception_o.gva = ld_st_v_i;
604
+ end
605
+ end
606
+ // this is a load
607
+ end else begin
608
+ if (CVA6Cfg.RVH && d_g_st_access_err) begin
609
+ lsu_exception_o.cause = riscv::LOAD_GUEST_PAGE_FAULT;
610
+ lsu_exception_o.valid = 1'b1;
611
+ if (CVA6Cfg.TvalEn)
612
+ lsu_exception_o.tval = {
613
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, lsu_vaddr_q
614
+ };
615
+ if (CVA6Cfg.RVH) begin
616
+ lsu_exception_o.tval2= CVA6Cfg.GPLEN'(lsu_gpaddr_q[(CVA6Cfg.XLEN==32?CVA6Cfg.VLEN : CVA6Cfg.GPLEN)-1:0]);
617
+ lsu_exception_o.tinst = '0;
618
+ lsu_exception_o.gva = ld_st_v_i;
619
+ end
620
+ // check for sufficient access privileges - throw a page fault if necessary
621
+ end else if (daccess_err || canonical_addr_check) begin
622
+ lsu_exception_o.cause = riscv::LOAD_PAGE_FAULT;
623
+ lsu_exception_o.valid = 1'b1;
624
+ if (CVA6Cfg.TvalEn)
625
+ lsu_exception_o.tval = {
626
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, lsu_vaddr_q
627
+ };
628
+ if (CVA6Cfg.RVH) begin
629
+ lsu_exception_o.tval2 = '0;
630
+ lsu_exception_o.tinst = lsu_tinst_q;
631
+ lsu_exception_o.gva = ld_st_v_i;
632
+ end
633
+ end
634
+ end
635
+ end else
636
+
637
+ // ---------
638
+ // DTLB Miss
639
+ // ---------
640
+ // watch out for exceptions
641
+ if (ptw_active && !walking_instr) begin
642
+ // page table walker threw an exception
643
+ if (ptw_error) begin
644
+ // an error makes the translation valid
645
+ lsu_valid_o = 1'b1;
646
+ // the page table walker can only throw page faults
647
+ if (lsu_is_store_q) begin
648
+ if (CVA6Cfg.RVH && ptw_error_at_g_st) begin
649
+ lsu_exception_o.cause = riscv::STORE_GUEST_PAGE_FAULT;
650
+ lsu_exception_o.valid = 1'b1;
651
+ if (CVA6Cfg.TvalEn)
652
+ lsu_exception_o.tval = {
653
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, update_vaddr
654
+ };
655
+ if (CVA6Cfg.RVH) begin
656
+ lsu_exception_o.tval2 = ptw_bad_gpaddr[CVA6Cfg.GPLEN-1:0];
657
+ lsu_exception_o.tinst= (ptw_err_at_g_int_st ? (CVA6Cfg.IS_XLEN64 ? riscv::READ_64_PSEUDOINSTRUCTION : riscv::READ_32_PSEUDOINSTRUCTION) : '0);
658
+ lsu_exception_o.gva = ld_st_v_i;
659
+ end
660
+ end else begin
661
+ lsu_exception_o.cause = riscv::STORE_PAGE_FAULT;
662
+ lsu_exception_o.valid = 1'b1;
663
+ if (CVA6Cfg.TvalEn)
664
+ lsu_exception_o.tval = {
665
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, update_vaddr
666
+ };
667
+ if (CVA6Cfg.RVH) begin
668
+ lsu_exception_o.tval2 = '0;
669
+ lsu_exception_o.tinst = lsu_tinst_q;
670
+ lsu_exception_o.gva = ld_st_v_i;
671
+ end
672
+ end
673
+ end else begin
674
+ if (CVA6Cfg.RVH && ptw_error_at_g_st) begin
675
+ lsu_exception_o.cause = riscv::LOAD_GUEST_PAGE_FAULT;
676
+ lsu_exception_o.valid = 1'b1;
677
+ if (CVA6Cfg.TvalEn)
678
+ lsu_exception_o.tval = {
679
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, update_vaddr
680
+ };
681
+ if (CVA6Cfg.RVH) begin
682
+ lsu_exception_o.tval2 = ptw_bad_gpaddr[CVA6Cfg.GPLEN-1:0];
683
+ lsu_exception_o.tinst= (ptw_err_at_g_int_st ? (CVA6Cfg.IS_XLEN64 ? riscv::READ_64_PSEUDOINSTRUCTION : riscv::READ_32_PSEUDOINSTRUCTION) : '0);
684
+ lsu_exception_o.gva = ld_st_v_i;
685
+ end
686
+ end else begin
687
+ lsu_exception_o.cause = riscv::LOAD_PAGE_FAULT;
688
+ lsu_exception_o.valid = 1'b1;
689
+ if (CVA6Cfg.TvalEn)
690
+ lsu_exception_o.tval = {
691
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, update_vaddr
692
+ };
693
+ if (CVA6Cfg.RVH) begin
694
+ lsu_exception_o.tval2 = '0;
695
+ lsu_exception_o.tinst = lsu_tinst_q;
696
+ lsu_exception_o.gva = ld_st_v_i;
697
+ end
698
+ end
699
+ end
700
+ end
701
+ if (ptw_access_exception) begin
702
+ // an error makes the translation valid
703
+ lsu_valid_o = 1'b1;
704
+ // Any fault of the page table walk should be based of the original access type
705
+ if (lsu_is_store_q && !CVA6Cfg.RVH && CVA6Cfg.PtLevels == 3) begin
706
+ lsu_exception_o.cause = riscv::ST_ACCESS_FAULT;
707
+ lsu_exception_o.valid = 1'b1;
708
+ if (CVA6Cfg.TvalEn)
709
+ lsu_exception_o.tval = {
710
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, update_vaddr
711
+ };
712
+ end else begin
713
+ // the page table walker can only throw page faults
714
+ lsu_exception_o.cause = riscv::LD_ACCESS_FAULT;
715
+ lsu_exception_o.valid = 1'b1;
716
+ if (CVA6Cfg.TvalEn)
717
+ lsu_exception_o.tval = {
718
+ {CVA6Cfg.XLEN - CVA6Cfg.VLEN{lsu_vaddr_q[CVA6Cfg.VLEN-1]}}, update_vaddr
719
+ };
720
+ if (CVA6Cfg.RVH) begin
721
+ lsu_exception_o.tval2 = '0;
722
+ lsu_exception_o.tinst = lsu_tinst_q;
723
+ lsu_exception_o.gva = ld_st_v_i;
724
+ end
725
+ end
726
+ end
727
+ end
728
+ end
729
+ end
730
+
731
+ // ----------
732
+ // Registers
733
+ // ----------
734
+ always_ff @(posedge clk_i or negedge rst_ni) begin
735
+ if (~rst_ni) begin
736
+ lsu_vaddr_q <= '0;
737
+ lsu_gpaddr_q <= '0;
738
+ lsu_req_q <= '0;
739
+ dtlb_pte_q <= '0;
740
+ dtlb_gpte_q <= '0;
741
+ dtlb_hit_q <= '0;
742
+ lsu_is_store_q <= '0;
743
+ dtlb_is_page_q <= '0;
744
+ lsu_tinst_q <= '0;
745
+ hs_ld_st_inst_q <= '0;
746
+ misaligned_ex_q <= '0;
747
+ end else begin
748
+ lsu_vaddr_q <= lsu_vaddr_n;
749
+ lsu_req_q <= lsu_req_n;
750
+ dtlb_pte_q <= dtlb_pte_n;
751
+ dtlb_hit_q <= dtlb_hit_n;
752
+ lsu_is_store_q <= lsu_is_store_n;
753
+ dtlb_is_page_q <= dtlb_is_page_n;
754
+
755
+ if (CVA6Cfg.RVH) begin
756
+ lsu_tinst_q <= lsu_tinst_n;
757
+ hs_ld_st_inst_q <= hs_ld_st_inst_n;
758
+ dtlb_gpte_q <= dtlb_gpte_n;
759
+ lsu_gpaddr_q <= lsu_gpaddr_n;
760
+ misaligned_ex_q <= misaligned_ex_n;
761
+ end
762
+ end
763
+ end
764
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_ptw.sv ADDED
@@ -0,0 +1,654 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright (c) 2018 ETH Zurich and University of Bologna.
2
+ // Copyright (c) 2021 Thales.
3
+ // Copyright (c) 2022 Bruno Sá and Zero-Day Labs.
4
+ // Copyright (c) 2024 PlanV Technology
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
6
+ // Copyright and related rights are licensed under the Solderpad Hardware
7
+ // License, Version 0.51 (the "License"); you may not use this file except in
8
+ // compliance with the License. You may obtain a copy of the License at
9
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
10
+ // or agreed to in writing, software, hardware and materials distributed under
11
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
+ // specific language governing permissions and limitations under the License.
14
+ //
15
+ // Author: Angela Gonzalez, PlanV Technology
16
+ // Date: 26/02/2024
17
+ // Description: Hardware-PTW (Page-Table-Walker) for CVA6 supporting sv32, sv39 and sv39x4.
18
+ // This module is an merge of the PTW Sv39 developed by Florian Zaruba,
19
+ // the PTW Sv32 developed by Sebastien Jacq and the PTW Sv39x4 by Bruno Sá.
20
+
21
+ /* verilator lint_off WIDTH */
22
+
23
+ module cva6_ptw
24
+ import ariane_pkg::*;
25
+ #(
26
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
27
+ parameter type pte_cva6_t = logic,
28
+ parameter type tlb_update_cva6_t = logic,
29
+ parameter type dcache_req_i_t = logic,
30
+ parameter type dcache_req_o_t = logic,
31
+ parameter int unsigned HYP_EXT = 0
32
+ ) (
33
+ input logic clk_i, // Clock
34
+ input logic rst_ni, // Asynchronous reset active low
35
+ input logic flush_i, // flush everything, we need to do this because
36
+ // actually everything we do is speculative at this stage
37
+ // e.g.: there could be a CSR instruction that changes everything
38
+ output logic ptw_active_o,
39
+ output logic walking_instr_o, // set when walking for TLB
40
+ output logic ptw_error_o, // set when an error occurred
41
+ output logic ptw_error_at_g_st_o, // set when an error occurred at the G-Stage
42
+ output logic ptw_err_at_g_int_st_o, // set when an error occurred at the G-Stage during S-Stage translation
43
+ output logic ptw_access_exception_o, // set when an PMP access exception occured
44
+ input logic enable_translation_i, // CSRs indicate to enable SV39 VS-Stage translation
45
+ input logic enable_g_translation_i, // CSRs indicate to enable SV39 G-Stage translation
46
+ input logic en_ld_st_translation_i, // enable virtual memory translation for load/stores
47
+ input logic en_ld_st_g_translation_i, // enable G-Stage translation for load/stores
48
+ input logic v_i, // current virtualization mode bit
49
+ input logic ld_st_v_i, // load/store virtualization mode bit
50
+ input logic hlvx_inst_i, // is a HLVX load/store instruction
51
+
52
+ input logic lsu_is_store_i, // this translation was triggered by a store
53
+ // PTW memory interface
54
+ input dcache_req_o_t req_port_i,
55
+ output dcache_req_i_t req_port_o,
56
+
57
+
58
+ // to TLBs, update logic
59
+ output tlb_update_cva6_t shared_tlb_update_o,
60
+
61
+ output logic [CVA6Cfg.VLEN-1:0] update_vaddr_o,
62
+
63
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] asid_i,
64
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] vs_asid_i,
65
+ input logic [CVA6Cfg.VMID_WIDTH-1:0] vmid_i,
66
+
67
+ // from TLBs
68
+ // did we miss?
69
+ input logic shared_tlb_access_i,
70
+ input logic shared_tlb_hit_i,
71
+ input logic [CVA6Cfg.VLEN-1:0] shared_tlb_vaddr_i,
72
+
73
+ input logic itlb_req_i,
74
+
75
+ // from CSR file
76
+ input logic [CVA6Cfg.PPNW-1:0] satp_ppn_i, // ppn from satp
77
+ input logic [CVA6Cfg.PPNW-1:0] vsatp_ppn_i, // ppn from satp
78
+ input logic [CVA6Cfg.PPNW-1:0] hgatp_ppn_i, // ppn from hgatp
79
+ input logic mxr_i,
80
+ input logic vmxr_i,
81
+
82
+ // Performance counters
83
+ output logic shared_tlb_miss_o,
84
+
85
+ // PMP
86
+ input riscv::pmpcfg_t [avoid_neg(CVA6Cfg.NrPMPEntries-1):0] pmpcfg_i,
87
+ input logic [avoid_neg(CVA6Cfg.NrPMPEntries-1):0][CVA6Cfg.PLEN-3:0] pmpaddr_i,
88
+ output logic [CVA6Cfg.PLEN-1:0] bad_paddr_o,
89
+ output logic [CVA6Cfg.GPLEN-1:0] bad_gpaddr_o
90
+ );
91
+
92
+ // input registers
93
+ logic data_rvalid_q;
94
+ logic [CVA6Cfg.XLEN-1:0] data_rdata_q;
95
+
96
+ pte_cva6_t pte;
97
+ // register to perform context switch between stages
98
+ pte_cva6_t gpte_q, gpte_d;
99
+ assign pte = pte_cva6_t'(data_rdata_q);
100
+
101
+ enum logic [2:0] {
102
+ IDLE,
103
+ WAIT_GRANT,
104
+ PTE_LOOKUP,
105
+ WAIT_RVALID,
106
+ PROPAGATE_ERROR,
107
+ PROPAGATE_ACCESS_ERROR,
108
+ LATENCY
109
+ }
110
+ state_q, state_d;
111
+
112
+ logic [CVA6Cfg.PtLevels-2:0] misaligned_page;
113
+ logic [HYP_EXT:0][CVA6Cfg.PtLevels-2:0] ptw_lvl_n, ptw_lvl_q;
114
+
115
+ // define 3 PTW stages to be used in sv39x4. sv32 and sv39 are always in S_STAGE
116
+ // S_STAGE -> S/VS-stage normal translation controlled by the satp/vsatp CSRs
117
+ // G_INTERMED_STAGE -> Converts the S/VS-stage non-leaf GPA pointers to HPA (controlled by hgatp)
118
+ // G_FINAL_STAGE -> Converts the S/VS-stage final GPA to HPA (controlled by hgatp)
119
+ enum logic [1:0] {
120
+ S_STAGE,
121
+ G_INTERMED_STAGE,
122
+ G_FINAL_STAGE
123
+ }
124
+ ptw_stage_q, ptw_stage_d;
125
+
126
+ // is this an instruction page table walk?
127
+ logic is_instr_ptw_q, is_instr_ptw_n;
128
+ logic global_mapping_q, global_mapping_n;
129
+ // latched tag signal
130
+ logic tag_valid_n, tag_valid_q;
131
+ // register the ASID
132
+ logic [CVA6Cfg.ASID_WIDTH-1:0] tlb_update_asid_q, tlb_update_asid_n;
133
+ // register the VMID
134
+ logic [CVA6Cfg.VMID_WIDTH-1:0] tlb_update_vmid_q, tlb_update_vmid_n;
135
+ // register the VPN we need to walk, SV39 defines a 39 bit virtual address
136
+ logic [CVA6Cfg.VLEN-1:0] vaddr_q, vaddr_n;
137
+ logic [HYP_EXT*2:0][CVA6Cfg.PtLevels-2:0][(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)-1:0] vaddr_lvl;
138
+ // register the VPN we need to walk, SV39x4 defines a 41 bit virtual address for the G-Stage
139
+ logic [CVA6Cfg.GPLEN-1:0] gpaddr_q, gpaddr_n, gpaddr_base;
140
+ logic [CVA6Cfg.PtLevels-1:0][CVA6Cfg.GPLEN-1:0] gpaddr;
141
+ // 4 byte aligned physical pointer
142
+ logic [CVA6Cfg.PLEN-1:0] ptw_pptr_q, ptw_pptr_n;
143
+ logic [CVA6Cfg.PLEN-1:0] gptw_pptr_q, gptw_pptr_n;
144
+
145
+ // Assignments
146
+ assign update_vaddr_o = vaddr_q;
147
+
148
+ assign ptw_active_o = (state_q != IDLE);
149
+ assign walking_instr_o = is_instr_ptw_q;
150
+ // directly output the correct physical address
151
+ assign req_port_o.address_index = ptw_pptr_q[CVA6Cfg.DCACHE_INDEX_WIDTH-1:0];
152
+ assign req_port_o.address_tag = ptw_pptr_q[CVA6Cfg.DCACHE_INDEX_WIDTH+CVA6Cfg.DCACHE_TAG_WIDTH-1:CVA6Cfg.DCACHE_INDEX_WIDTH];
153
+ // we are never going to kill this request
154
+ assign req_port_o.kill_req = '0;
155
+ // we are never going to write with the HPTW
156
+ assign req_port_o.data_wdata = '0;
157
+ // we only issue one single request at a time
158
+ assign req_port_o.data_id = '0;
159
+
160
+ // -----------
161
+ // TLB Update
162
+ // -----------
163
+
164
+ assign gpaddr_base = {pte.ppn[CVA6Cfg.GPPNW-1:0], vaddr_q[11:0]};
165
+ assign gpaddr[CVA6Cfg.PtLevels-1] = gpaddr_base;
166
+ assign shared_tlb_update_o.vpn = CVA6Cfg.VpnLen'(vaddr_q[CVA6Cfg.SV+HYP_EXT*2-1:12]);
167
+
168
+ genvar z, w;
169
+ generate
170
+ for (z = 0; z < CVA6Cfg.PtLevels - 1; z++) begin
171
+
172
+ // check if the ppn is correctly aligned:
173
+ // 6. If i > 0 and pa.ppn[i − 1 : 0] != 0, this is a misaligned superpage; stop and raise a page-fault
174
+ // exception.
175
+ assign misaligned_page[z] = (ptw_lvl_q[0] == (z)) && (pte.ppn[(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(CVA6Cfg.PtLevels-1-z)-1:0] != '0);
176
+
177
+ //record the vaddr corresponding to each level
178
+ for (w = 0; w < HYP_EXT * 2 + 1; w++) begin
179
+ assign vaddr_lvl[w][z] = w==0 ? vaddr_q[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(CVA6Cfg.PtLevels-z-1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(CVA6Cfg.PtLevels-z-2))] :
180
+ w==1 ? gptw_pptr_q[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(CVA6Cfg.PtLevels-z-1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(CVA6Cfg.PtLevels-z-2))]:
181
+ gpaddr_q[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(CVA6Cfg.PtLevels-z-1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(CVA6Cfg.PtLevels-z-2))];
182
+ end
183
+
184
+ if (CVA6Cfg.RVH) begin
185
+ assign gpaddr[z][CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):0]= (ptw_lvl_q[0] == z) ? vaddr_q[CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):0] : gpaddr_base[CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels):0];
186
+ assign gpaddr[z][CVA6Cfg.VpnLen:CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)+1]= (ptw_lvl_q[0] == 0) ? vaddr_q[CVA6Cfg.VpnLen:CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)+1] : gpaddr_base[CVA6Cfg.VpnLen:CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)+1];
187
+ assign gpaddr[z][CVA6Cfg.GPLEN-1:CVA6Cfg.VpnLen+1] = gpaddr_base[CVA6Cfg.GPLEN-1:CVA6Cfg.VpnLen+1];
188
+ end
189
+
190
+
191
+ end
192
+ endgenerate
193
+
194
+ always_comb begin : tlb_update
195
+ // update the correct page table level
196
+ for (int unsigned y = 0; y < HYP_EXT + 1; y++) begin
197
+ for (int unsigned x = 0; x < CVA6Cfg.PtLevels - 1; x++) begin
198
+ if((enable_g_translation_i && enable_translation_i) || (en_ld_st_g_translation_i && en_ld_st_translation_i) && CVA6Cfg.RVH) begin
199
+ shared_tlb_update_o.is_page[x][y] = (ptw_lvl_q[y==HYP_EXT?0 : 1] == x);
200
+ end else if (enable_translation_i || en_ld_st_translation_i || !CVA6Cfg.RVH) begin
201
+ shared_tlb_update_o.is_page[x][y] = y == 0 ? (ptw_lvl_q[0] == x) : 1'b0;
202
+ end else begin
203
+ shared_tlb_update_o.is_page[x][y] = y != 0 ? (ptw_lvl_q[0] == x) : 1'b0;
204
+ end
205
+ end
206
+ end
207
+
208
+ // set the global mapping bit
209
+ if ((enable_g_translation_i || en_ld_st_g_translation_i) && CVA6Cfg.RVH) begin
210
+ shared_tlb_update_o.content = gpte_q | (global_mapping_q << 5);
211
+ shared_tlb_update_o.g_content = pte;
212
+ end else begin
213
+ shared_tlb_update_o.content = (pte | (global_mapping_q << 5));
214
+ shared_tlb_update_o.g_content = '0;
215
+ end
216
+
217
+ // output the correct ASIDs
218
+ shared_tlb_update_o.asid = tlb_update_asid_q;
219
+ shared_tlb_update_o.vmid = CVA6Cfg.RVH ? tlb_update_vmid_q : '0;
220
+
221
+ bad_paddr_o = ptw_access_exception_o ? ptw_pptr_q : 'b0;
222
+ if (CVA6Cfg.RVH)
223
+ bad_gpaddr_o[CVA6Cfg.GPLEN-1:0] = ptw_error_at_g_st_o ? ((ptw_stage_q == G_INTERMED_STAGE) ? gptw_pptr_q[CVA6Cfg.GPLEN-1:0] : gpaddr_q) : 'b0;
224
+ end
225
+
226
+ assign req_port_o.tag_valid = tag_valid_q;
227
+
228
+ logic allow_access;
229
+
230
+
231
+
232
+ pmp #(
233
+ .CVA6Cfg(CVA6Cfg)
234
+ ) i_pmp_ptw (
235
+ .addr_i (ptw_pptr_q),
236
+ // PTW access are always checked as if in S-Mode...
237
+ .priv_lvl_i (riscv::PRIV_LVL_S),
238
+ // ...and they are always loads
239
+ .access_type_i(riscv::ACCESS_READ),
240
+ // Configuration
241
+ .conf_addr_i (pmpaddr_i),
242
+ .conf_i (pmpcfg_i),
243
+ .allow_o (allow_access)
244
+ );
245
+
246
+
247
+ assign req_port_o.data_be = CVA6Cfg.XLEN == 32 ? be_gen_32(
248
+ req_port_o.address_index[1:0], req_port_o.data_size
249
+ ) : '1;
250
+
251
+
252
+
253
+ //-------------------
254
+ // Page table walker
255
+ //-------------------
256
+ // A virtual address va is translated into a physical address pa as follows:
257
+ // 1. Let a be sptbr.ppn × PAGESIZE, and let i = LEVELS-1. (For Sv39,
258
+ // PAGESIZE=2^12 and LEVELS=3.)
259
+ // 2. Let pte be the value of the PTE at address a+va.vpn[i]×PTESIZE. (For
260
+ // Sv32, PTESIZE=4.)
261
+ // 3. If pte.v = 0, or if pte.r = 0 and pte.w = 1, or if any bits or encodings
262
+ // that are reserved for future standard use are set within pte, stop and raise
263
+ // a page-fault exception corresponding to the original access type.
264
+ // 4. Otherwise, the PTE is valid. If pte.r = 1 or pte.x = 1, go to step 5.
265
+ // Otherwise, this PTE is a pointer to the next level of the page table.
266
+ // Let i=i-1. If i < 0, stop and raise an access exception. Otherwise, let
267
+ // a = pte.ppn × PAGESIZE and go to step 2.
268
+ // 5. A leaf PTE has been found. Determine if the requested memory access
269
+ // is allowed by the pte.r, pte.w, and pte.x bits. If not, stop and
270
+ // raise an access exception. Otherwise, the translation is successful.
271
+ // Set pte.a to 1, and, if the memory access is a store, set pte.d to 1.
272
+ // The translated physical address is given as follows:
273
+ // - pa.pgoff = va.pgoff.
274
+ // - If i > 0, then this is a superpage translation and
275
+ // pa.ppn[i-1:0] = va.vpn[i-1:0].
276
+ // - pa.ppn[LEVELS-1:i] = pte.ppn[LEVELS-1:i].
277
+ always_comb begin : ptw
278
+ automatic logic [CVA6Cfg.PLEN-1:0] pptr;
279
+ // automatic logic [CVA6Cfg.GPLEN-1:0] gpaddr;
280
+ // default assignments
281
+ // PTW memory interface
282
+ tag_valid_n = 1'b0;
283
+ req_port_o.data_req = 1'b0;
284
+ req_port_o.data_size = 2'(CVA6Cfg.PtLevels);
285
+ req_port_o.data_we = 1'b0;
286
+ ptw_error_o = 1'b0;
287
+ ptw_error_at_g_st_o = 1'b0;
288
+ ptw_err_at_g_int_st_o = 1'b0;
289
+ ptw_access_exception_o = 1'b0;
290
+ shared_tlb_update_o.valid = 1'b0;
291
+ is_instr_ptw_n = is_instr_ptw_q;
292
+ ptw_lvl_n = ptw_lvl_q;
293
+ ptw_pptr_n = ptw_pptr_q;
294
+ state_d = state_q;
295
+ ptw_stage_d = ptw_stage_q;
296
+ global_mapping_n = global_mapping_q;
297
+ // input registers
298
+ tlb_update_asid_n = tlb_update_asid_q;
299
+ vaddr_n = vaddr_q;
300
+ pptr = ptw_pptr_q;
301
+
302
+ if (CVA6Cfg.RVH) begin
303
+ gpaddr_n = gpaddr_q;
304
+ gptw_pptr_n = gptw_pptr_q;
305
+ gpte_d = gpte_q;
306
+ tlb_update_vmid_n = tlb_update_vmid_q;
307
+ end
308
+
309
+ shared_tlb_miss_o = 1'b0;
310
+
311
+
312
+ case (state_q)
313
+
314
+ IDLE: begin
315
+ // by default we start with the top-most page table
316
+ ptw_lvl_n = '0;
317
+ global_mapping_n = 1'b0;
318
+ is_instr_ptw_n = 1'b0;
319
+
320
+
321
+ if (CVA6Cfg.RVH) begin
322
+ gpte_d = '0;
323
+ gpaddr_n = '0;
324
+ end
325
+
326
+
327
+ // if we got an ITLB miss
328
+ if (((enable_translation_i | enable_g_translation_i) || (en_ld_st_translation_i || en_ld_st_g_translation_i) || !CVA6Cfg.RVH) && shared_tlb_access_i && ~shared_tlb_hit_i) begin
329
+ if (((enable_translation_i && enable_g_translation_i) || (en_ld_st_translation_i && en_ld_st_g_translation_i)) && CVA6Cfg.RVH) begin
330
+ ptw_stage_d = G_INTERMED_STAGE;
331
+ pptr = {
332
+ vsatp_ppn_i,
333
+ shared_tlb_vaddr_i[CVA6Cfg.SV-1:CVA6Cfg.SV-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)],
334
+ (CVA6Cfg.PtLevels)'(0)
335
+ };
336
+ gptw_pptr_n = pptr;
337
+ ptw_pptr_n = {
338
+ hgatp_ppn_i[CVA6Cfg.PPNW-1:2],
339
+ pptr[CVA6Cfg.SV+HYP_EXT*2-1:CVA6Cfg.SV-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)],
340
+ (CVA6Cfg.PtLevels)'(0)
341
+ };
342
+ end else if ((((enable_translation_i | enable_g_translation_i) && !enable_translation_i) || ((en_ld_st_translation_i || en_ld_st_g_translation_i) && !en_ld_st_translation_i)) && CVA6Cfg.RVH) begin
343
+ ptw_stage_d = G_FINAL_STAGE;
344
+ gpaddr_n = shared_tlb_vaddr_i[CVA6Cfg.SV+HYP_EXT*2-1:0];
345
+ ptw_pptr_n = {
346
+ hgatp_ppn_i[CVA6Cfg.PPNW-1:2],
347
+ shared_tlb_vaddr_i[CVA6Cfg.SV+HYP_EXT*2-1:CVA6Cfg.SV-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)],
348
+ (CVA6Cfg.PtLevels)'(0)
349
+ };
350
+ end else begin
351
+ ptw_stage_d = S_STAGE;
352
+ if ((v_i || ld_st_v_i) && CVA6Cfg.RVH)
353
+ ptw_pptr_n = {
354
+ vsatp_ppn_i,
355
+ shared_tlb_vaddr_i[CVA6Cfg.SV-1:CVA6Cfg.SV-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)],
356
+ (CVA6Cfg.PtLevels)'(0)
357
+ };
358
+ else
359
+ ptw_pptr_n = {
360
+ satp_ppn_i,
361
+ shared_tlb_vaddr_i[CVA6Cfg.SV-1:CVA6Cfg.SV-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)],
362
+ (CVA6Cfg.PtLevels)'(0)
363
+ };
364
+ end
365
+
366
+ is_instr_ptw_n = itlb_req_i;
367
+ vaddr_n = shared_tlb_vaddr_i;
368
+ state_d = WAIT_GRANT;
369
+ shared_tlb_miss_o = 1'b1;
370
+
371
+ if (itlb_req_i) begin
372
+ tlb_update_asid_n = v_i ? vs_asid_i : asid_i;
373
+ if (CVA6Cfg.RVH) tlb_update_vmid_n = vmid_i;
374
+ end else begin
375
+ tlb_update_asid_n = ld_st_v_i ? vs_asid_i : asid_i;
376
+ if (CVA6Cfg.RVH) tlb_update_vmid_n = vmid_i;
377
+ end
378
+ end
379
+ end
380
+
381
+ WAIT_GRANT: begin
382
+ // send a request out
383
+ req_port_o.data_req = 1'b1;
384
+ // wait for the WAIT_GRANT
385
+ if (req_port_i.data_gnt) begin
386
+ // send the tag valid signal one cycle later
387
+ tag_valid_n = 1'b1;
388
+ state_d = PTE_LOOKUP;
389
+ end
390
+ end
391
+
392
+ PTE_LOOKUP: begin
393
+ // we wait for the valid signal
394
+ if (data_rvalid_q) begin
395
+
396
+ // check if the global mapping bit is set
397
+ if (pte.g && (ptw_stage_q == S_STAGE || !CVA6Cfg.RVH)) global_mapping_n = 1'b1;
398
+
399
+ // -------------
400
+ // Invalid PTE
401
+ // -------------
402
+ // If pte.v = 0, or if pte.r = 0 and pte.w = 1, or if pte.reserved !=0 in sv39 and sv39x4, stop and raise a page-fault exception.
403
+ if (!pte.v || (!pte.r && pte.w) || (|pte.reserved && CVA6Cfg.XLEN == 64))
404
+ state_d = PROPAGATE_ERROR;
405
+ // -----------
406
+ // Valid PTE
407
+ // -----------
408
+ else begin
409
+ state_d = LATENCY;
410
+ // it is a valid PTE
411
+ // if pte.r = 1 or pte.x = 1 it is a valid PTE
412
+ if (pte.r || pte.x) begin
413
+ if (CVA6Cfg.RVH) begin
414
+ case (ptw_stage_q)
415
+ S_STAGE: begin
416
+ if ((is_instr_ptw_q && enable_g_translation_i) || (!is_instr_ptw_q && en_ld_st_g_translation_i)) begin
417
+ state_d = WAIT_GRANT;
418
+ ptw_stage_d = G_FINAL_STAGE;
419
+ if (CVA6Cfg.RVH) gpte_d = pte;
420
+ ptw_lvl_n[HYP_EXT] = ptw_lvl_q[0];
421
+ gpaddr_n = gpaddr[ptw_lvl_q[0]];
422
+ ptw_pptr_n = {
423
+ hgatp_ppn_i[CVA6Cfg.PPNW-1:2],
424
+ gpaddr[ptw_lvl_q[0]][CVA6Cfg.SV+HYP_EXT*2-1:CVA6Cfg.SV-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)],
425
+ (CVA6Cfg.PtLevels)'(0)
426
+ };
427
+ ptw_lvl_n[0] = '0;
428
+ end
429
+ end
430
+ G_INTERMED_STAGE: begin
431
+ state_d = WAIT_GRANT;
432
+ ptw_stage_d = S_STAGE;
433
+ ptw_lvl_n[0] = ptw_lvl_q[HYP_EXT];
434
+ pptr = {pte.ppn[CVA6Cfg.GPPNW-1:0], gptw_pptr_q[11:0]};
435
+ if (ptw_lvl_q[0] == 1) pptr[20:0] = gptw_pptr_q[20:0];
436
+ if (ptw_lvl_q[0] == 0) pptr[29:0] = gptw_pptr_q[29:0];
437
+ ptw_pptr_n = pptr;
438
+ end
439
+ default: ;
440
+ endcase
441
+ end
442
+ // Valid translation found (either 1G, 2M or 4K entry)
443
+ if (is_instr_ptw_q) begin
444
+ // ------------
445
+ // Update ITLB
446
+ // ------------
447
+ // If page is not executable, we can directly raise an error. This
448
+ // doesn't put a useless entry into the TLB. The same idea applies
449
+ // to the access flag since we let the access flag be managed by SW.
450
+ if (!pte.x || !pte.a) begin
451
+ state_d = PROPAGATE_ERROR;
452
+ if (CVA6Cfg.RVH) ptw_stage_d = ptw_stage_q;
453
+ end else if (CVA6Cfg.RVH && ((ptw_stage_q == G_FINAL_STAGE) || !enable_g_translation_i) || !CVA6Cfg.RVH)
454
+ shared_tlb_update_o.valid = 1'b1;
455
+
456
+ end else begin
457
+ // ------------
458
+ // Update DTLB
459
+ // ------------
460
+ // Check if the access flag has been set, otherwise throw a page-fault
461
+ // and let the software handle those bits.
462
+ // If page is not readable (there are no write-only pages)
463
+ // we can directly raise an error. This doesn't put a useless
464
+ // entry into the TLB.
465
+ if (pte.a && ((pte.r && !hlvx_inst_i) || (pte.x && (mxr_i || hlvx_inst_i || (ptw_stage_q == S_STAGE && vmxr_i && ld_st_v_i && CVA6Cfg.RVH))))) begin
466
+ if (CVA6Cfg.RVH && ((ptw_stage_q == G_FINAL_STAGE) || !en_ld_st_g_translation_i) || !CVA6Cfg.RVH)
467
+ shared_tlb_update_o.valid = 1'b1;
468
+ end else begin
469
+ state_d = PROPAGATE_ERROR;
470
+ if (CVA6Cfg.RVH) ptw_stage_d = ptw_stage_q;
471
+ end
472
+ // Request is a store: perform some additional checks
473
+ // If the request was a store and the page is not write-able, raise an error
474
+ // the same applies if the dirty flag is not set
475
+ if (lsu_is_store_i && (!pte.w || !pte.d)) begin
476
+ shared_tlb_update_o.valid = 1'b0;
477
+ state_d = PROPAGATE_ERROR;
478
+ if (CVA6Cfg.RVH) ptw_stage_d = ptw_stage_q;
479
+ end
480
+ end
481
+
482
+ //if there is a misaligned page, propagate error
483
+ if (|misaligned_page) begin
484
+ state_d = PROPAGATE_ERROR;
485
+ if (CVA6Cfg.RVH) ptw_stage_d = ptw_stage_q;
486
+ shared_tlb_update_o.valid = 1'b0;
487
+ end
488
+
489
+ // check if 63:41 are all zeros
490
+ if (CVA6Cfg.RVH) begin
491
+ if (((v_i && is_instr_ptw_q) || (ld_st_v_i && !is_instr_ptw_q)) && ptw_stage_q == S_STAGE && !((|pte.ppn[CVA6Cfg.PPNW-1:CVA6Cfg.GPPNW-1+HYP_EXT]) == 1'b0)) begin
492
+ state_d = PROPAGATE_ERROR;
493
+ ptw_stage_d = G_FINAL_STAGE;
494
+ end
495
+ end
496
+ // this is a pointer to the next TLB level
497
+ end else begin
498
+ // pointer to next level of page table
499
+
500
+ if (ptw_lvl_q[0] == CVA6Cfg.PtLevels - 1) begin
501
+ // Should already be the last level page table => Error
502
+ ptw_lvl_n[0] = ptw_lvl_q[0];
503
+ state_d = PROPAGATE_ERROR;
504
+ if (CVA6Cfg.RVH) ptw_stage_d = ptw_stage_q;
505
+
506
+
507
+ end else begin
508
+ ptw_lvl_n[0] = ptw_lvl_q[0] + 1'b1;
509
+ state_d = WAIT_GRANT;
510
+
511
+ if (CVA6Cfg.RVH) begin
512
+ case (ptw_stage_q)
513
+ S_STAGE: begin
514
+ if (CVA6Cfg.RVH && ((is_instr_ptw_q && enable_g_translation_i) || (!is_instr_ptw_q && en_ld_st_g_translation_i))) begin
515
+ ptw_stage_d = G_INTERMED_STAGE;
516
+ if (CVA6Cfg.RVH) gpte_d = pte;
517
+ ptw_lvl_n[HYP_EXT] = ptw_lvl_q[0] + 1;
518
+ pptr = {pte.ppn, vaddr_lvl[0][ptw_lvl_q[0]], (CVA6Cfg.PtLevels)'(0)};
519
+ gptw_pptr_n = pptr;
520
+ ptw_pptr_n = {
521
+ hgatp_ppn_i[CVA6Cfg.PPNW-1:2],
522
+ pptr[CVA6Cfg.SV+HYP_EXT*2-1:CVA6Cfg.SV-(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)],
523
+ (CVA6Cfg.PtLevels)'(0)
524
+ };
525
+ ptw_lvl_n[0] = '0;
526
+ end else begin
527
+ ptw_pptr_n = {pte.ppn, vaddr_lvl[0][ptw_lvl_q[0]], (CVA6Cfg.PtLevels)'(0)};
528
+ end
529
+ end
530
+ G_INTERMED_STAGE: begin
531
+ ptw_pptr_n = {
532
+ pte.ppn, vaddr_lvl[HYP_EXT][ptw_lvl_q[0]], (CVA6Cfg.PtLevels)'(0)
533
+ };
534
+ end
535
+ G_FINAL_STAGE: begin
536
+ ptw_pptr_n = {
537
+ pte.ppn, vaddr_lvl[HYP_EXT*2][ptw_lvl_q[0]], (CVA6Cfg.PtLevels)'(0)
538
+ };
539
+ end
540
+ endcase
541
+ end else ptw_pptr_n = {pte.ppn, vaddr_lvl[0][ptw_lvl_q[0]], (CVA6Cfg.PtLevels)'(0)};
542
+
543
+ if (CVA6Cfg.RVH && (pte.a || pte.d || pte.u)) begin
544
+ state_d = PROPAGATE_ERROR;
545
+ ptw_stage_d = ptw_stage_q;
546
+ end
547
+
548
+ end
549
+
550
+ // check if 63:41 are all zeros
551
+ if (CVA6Cfg.RVH) begin
552
+ if (((v_i && is_instr_ptw_q) || (ld_st_v_i && !is_instr_ptw_q)) && ptw_stage_q == S_STAGE && !((|pte.ppn[CVA6Cfg.PPNW-1:CVA6Cfg.GPPNW-1+HYP_EXT]) == 1'b0)) begin
553
+ state_d = PROPAGATE_ERROR;
554
+ ptw_stage_d = ptw_stage_q;
555
+ end
556
+ end
557
+ end
558
+ end
559
+
560
+ // Check if this access was actually allowed from a PMP perspective
561
+ if (!allow_access) begin
562
+ shared_tlb_update_o.valid = 1'b0;
563
+ // we have to return the failed address in bad_addr
564
+ ptw_pptr_n = ptw_pptr_q;
565
+ if (CVA6Cfg.RVH) ptw_stage_d = ptw_stage_q;
566
+ state_d = PROPAGATE_ACCESS_ERROR;
567
+ end
568
+ end
569
+ // we've got a data WAIT_GRANT so tell the cache that the tag is valid
570
+ end
571
+ // Propagate error to MMU/LSU
572
+ PROPAGATE_ERROR: begin
573
+ state_d = LATENCY;
574
+ ptw_error_o = 1'b1;
575
+ if (CVA6Cfg.RVH) begin
576
+ ptw_error_at_g_st_o = (ptw_stage_q != S_STAGE) ? 1'b1 : 1'b0;
577
+ ptw_err_at_g_int_st_o = (ptw_stage_q == G_INTERMED_STAGE) ? 1'b1 : 1'b0;
578
+ end
579
+ end
580
+ PROPAGATE_ACCESS_ERROR: begin
581
+ state_d = LATENCY;
582
+ ptw_access_exception_o = 1'b1;
583
+ end
584
+ // wait for the rvalid before going back to IDLE
585
+ WAIT_RVALID: begin
586
+ if (data_rvalid_q) state_d = IDLE;
587
+ end
588
+ LATENCY: begin
589
+ state_d = IDLE;
590
+ end
591
+ default: begin
592
+ state_d = IDLE;
593
+ end
594
+ endcase
595
+
596
+ // -------
597
+ // Flush
598
+ // -------
599
+ // should we have flushed before we got an rvalid, wait for it until going back to IDLE
600
+ if (flush_i) begin
601
+ // on a flush check whether we are
602
+ // 1. in the PTE Lookup check whether we still need to wait for an rvalid
603
+ // 2. waiting for a grant, if so: wait for it
604
+ // if not, go back to idle
605
+ if (((state_q inside {PTE_LOOKUP, WAIT_RVALID}) && !data_rvalid_q) || ((state_q == WAIT_GRANT) && req_port_i.data_gnt))
606
+ state_d = WAIT_RVALID;
607
+ else state_d = LATENCY;
608
+ end
609
+ end
610
+
611
+ // sequential process
612
+ always_ff @(posedge clk_i or negedge rst_ni) begin
613
+ if (~rst_ni) begin
614
+ state_q <= IDLE;
615
+ is_instr_ptw_q <= 1'b0;
616
+ ptw_lvl_q <= '0;
617
+ tag_valid_q <= 1'b0;
618
+ tlb_update_asid_q <= '0;
619
+ vaddr_q <= '0;
620
+ ptw_pptr_q <= '0;
621
+ global_mapping_q <= 1'b0;
622
+ data_rdata_q <= '0;
623
+ data_rvalid_q <= 1'b0;
624
+ if (CVA6Cfg.RVH) begin
625
+ gpaddr_q <= '0;
626
+ gptw_pptr_q <= '0;
627
+ ptw_stage_q <= S_STAGE;
628
+ gpte_q <= '0;
629
+ tlb_update_vmid_q <= '0;
630
+ end
631
+ end else begin
632
+ state_q <= state_d;
633
+ ptw_pptr_q <= ptw_pptr_n;
634
+ is_instr_ptw_q <= is_instr_ptw_n;
635
+ ptw_lvl_q <= ptw_lvl_n;
636
+ tag_valid_q <= tag_valid_n;
637
+ tlb_update_asid_q <= tlb_update_asid_n;
638
+ vaddr_q <= vaddr_n;
639
+ global_mapping_q <= global_mapping_n;
640
+ data_rdata_q <= req_port_i.data_rdata;
641
+ data_rvalid_q <= req_port_i.data_rvalid;
642
+
643
+ if (CVA6Cfg.RVH) begin
644
+ gpaddr_q <= gpaddr_n;
645
+ gptw_pptr_q <= gptw_pptr_n;
646
+ ptw_stage_q <= ptw_stage_d;
647
+ gpte_q <= gpte_d;
648
+ tlb_update_vmid_q <= tlb_update_vmid_n;
649
+ end
650
+ end
651
+ end
652
+
653
+ endmodule
654
+ /* verilator lint_on WIDTH */
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_shared_tlb.sv ADDED
@@ -0,0 +1,502 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright (c) 2023 Thales.
2
+ // Copyright (c) 2024, PlanV Technology
3
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
4
+ // Copyright and related rights are licensed under the Solderpad Hardware
5
+ // License, Version 0.51 (the "License"); you may not use this file except in
6
+ // compliance with the License. You may obtain a copy of the License at
7
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
8
+ // or agreed to in writing, software, hardware and materials distributed under
9
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
11
+ // specific language governing permissions and limitations under the License.
12
+ //
13
+ // Author: Angela Gonzalez PlanV Technology
14
+ // Date: 26/02/2024
15
+ //
16
+ // Description: N-way associative shared TLB, it allows to reduce the number
17
+ // of ITLB and DTLB entries. This module is an update of the
18
+ // shared TLB sv32 developed by Sebastien Jacq (Thales Research & Technology)
19
+ // to be used with sv32, sv39 and sv39x4.
20
+
21
+ /* verilator lint_off WIDTH */
22
+
23
+ module cva6_shared_tlb #(
24
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
25
+ parameter type pte_cva6_t = logic,
26
+ parameter type tlb_update_cva6_t = logic,
27
+ parameter int SHARED_TLB_WAYS = 2,
28
+ parameter int unsigned HYP_EXT = 0
29
+
30
+ ) (
31
+ input logic clk_i, // Clock
32
+ input logic rst_ni, // Asynchronous reset active low
33
+ input logic flush_i, // Flush normal translations signal
34
+ input logic flush_vvma_i, // Flush vs stage signal
35
+ input logic flush_gvma_i, // Flush g stage signal
36
+ input logic s_st_enbl_i, // s-stage enabled
37
+ input logic g_st_enbl_i, // g-stage enabled
38
+ input logic v_i, // virtualization mode
39
+ input logic s_ld_st_enbl_i, // s-stage enabled for load stores
40
+ input logic g_ld_st_enbl_i, // g-stage enabled for load stores
41
+ input logic ld_st_v_i, // virtualization mode for load stores
42
+
43
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] dtlb_asid_i,
44
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] itlb_asid_i,
45
+ input logic [CVA6Cfg.VMID_WIDTH-1:0] lu_vmid_i,
46
+
47
+ // from TLBs
48
+ // did we miss?
49
+ input logic itlb_access_i,
50
+ input logic itlb_hit_i,
51
+ input logic [CVA6Cfg.VLEN-1:0] itlb_vaddr_i,
52
+
53
+ input logic dtlb_access_i,
54
+ input logic dtlb_hit_i,
55
+ input logic [CVA6Cfg.VLEN-1:0] dtlb_vaddr_i,
56
+
57
+ input logic shared_tlb_miss_i,
58
+
59
+ // to TLBs, update logic
60
+ output tlb_update_cva6_t itlb_update_o,
61
+ output tlb_update_cva6_t dtlb_update_o,
62
+
63
+ // Performance counters
64
+ output logic itlb_miss_o,
65
+ output logic dtlb_miss_o,
66
+
67
+ output logic shared_tlb_access_o,
68
+ output logic shared_tlb_hit_o,
69
+ output logic [CVA6Cfg.VLEN-1:0] shared_tlb_vaddr_o,
70
+
71
+ output logic itlb_req_o,
72
+
73
+ // Update shared TLB in case of miss
74
+ input tlb_update_cva6_t shared_tlb_update_i
75
+
76
+ );
77
+
78
+ function logic [SHARED_TLB_WAYS-1:0] shared_tlb_way_bin2oh(input logic [$clog2(SHARED_TLB_WAYS
79
+ )-1:0] in);
80
+ logic [SHARED_TLB_WAYS-1:0] out;
81
+ out = '0;
82
+ out[in] = 1'b1;
83
+ return out;
84
+ endfunction
85
+
86
+ typedef struct packed {
87
+ logic [CVA6Cfg.ASID_WIDTH-1:0] asid;
88
+ logic [CVA6Cfg.VMID_WIDTH-1:0] vmid;
89
+ logic [CVA6Cfg.PtLevels+HYP_EXT-1:0][(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)-1:0] vpn;
90
+ logic [CVA6Cfg.PtLevels-2:0][HYP_EXT:0] is_page;
91
+ logic [HYP_EXT*2:0] v_st_enbl; // v_i,g-stage enabled, s-stage enabled
92
+ } shared_tag_t;
93
+
94
+ shared_tag_t shared_tag_wr;
95
+ shared_tag_t [SHARED_TLB_WAYS-1:0] shared_tag_rd;
96
+
97
+ logic [CVA6Cfg.SharedTlbDepth-1:0][SHARED_TLB_WAYS-1:0] shared_tag_valid_q, shared_tag_valid_d;
98
+
99
+ logic [ SHARED_TLB_WAYS-1:0] shared_tag_valid;
100
+
101
+ logic [ SHARED_TLB_WAYS-1:0] tag_wr_en;
102
+ logic [$clog2(CVA6Cfg.SharedTlbDepth)-1:0] tag_wr_addr;
103
+ logic [ $bits(shared_tag_t)-1:0] tag_wr_data;
104
+
105
+ logic [ SHARED_TLB_WAYS-1:0] tag_rd_en;
106
+ logic [$clog2(CVA6Cfg.SharedTlbDepth)-1:0] tag_rd_addr;
107
+ logic [ $bits(shared_tag_t)-1:0] tag_rd_data [SHARED_TLB_WAYS-1:0];
108
+
109
+ logic [ SHARED_TLB_WAYS-1:0] tag_req;
110
+ logic [ SHARED_TLB_WAYS-1:0] tag_we;
111
+ logic [$clog2(CVA6Cfg.SharedTlbDepth)-1:0] tag_addr;
112
+
113
+ logic [ SHARED_TLB_WAYS-1:0] pte_wr_en;
114
+ logic [$clog2(CVA6Cfg.SharedTlbDepth)-1:0] pte_wr_addr;
115
+ logic [ CVA6Cfg.XLEN-1:0] pte_wr_data [ 1:0];
116
+
117
+ logic [ SHARED_TLB_WAYS-1:0] pte_rd_en;
118
+ logic [$clog2(CVA6Cfg.SharedTlbDepth)-1:0] pte_rd_addr;
119
+ logic [ CVA6Cfg.XLEN-1:0] pte_rd_data [SHARED_TLB_WAYS-1:0] [HYP_EXT:0];
120
+
121
+ logic [ SHARED_TLB_WAYS-1:0] pte_req;
122
+ logic [ SHARED_TLB_WAYS-1:0] pte_we;
123
+ logic [$clog2(CVA6Cfg.SharedTlbDepth)-1:0] pte_addr;
124
+
125
+ logic [CVA6Cfg.PtLevels+HYP_EXT-1:0][(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)-1:0] vpn_d, vpn_q;
126
+ logic [SHARED_TLB_WAYS-1:0][CVA6Cfg.PtLevels-1:0] vpn_match;
127
+ logic [SHARED_TLB_WAYS-1:0][CVA6Cfg.PtLevels-1:0] page_match;
128
+ logic [SHARED_TLB_WAYS-1:0][CVA6Cfg.PtLevels-1:0] level_match;
129
+
130
+ logic [SHARED_TLB_WAYS-1:0] match_asid;
131
+ logic [SHARED_TLB_WAYS-1:0] match_vmid;
132
+ logic [SHARED_TLB_WAYS-1:0] match_stage;
133
+
134
+ pte_cva6_t [SHARED_TLB_WAYS-1:0][HYP_EXT:0] pte;
135
+
136
+ logic [CVA6Cfg.VLEN-1-12:0] itlb_vpn_q;
137
+ logic [CVA6Cfg.VLEN-1-12:0] dtlb_vpn_q;
138
+
139
+ logic [CVA6Cfg.ASID_WIDTH-1:0] tlb_update_asid_q, tlb_update_asid_d;
140
+ logic [CVA6Cfg.VMID_WIDTH-1:0] tlb_update_vmid_q, tlb_update_vmid_d;
141
+
142
+ logic shared_tlb_access_q, shared_tlb_access_d;
143
+ logic shared_tlb_hit_d;
144
+ logic [CVA6Cfg.VLEN-1:0] shared_tlb_vaddr_q, shared_tlb_vaddr_d;
145
+
146
+ logic itlb_req_d, itlb_req_q;
147
+ logic dtlb_req_d, dtlb_req_q;
148
+
149
+ int i_req_d, i_req_q;
150
+
151
+ logic [1:0][2:0] v_st_enbl;
152
+
153
+ // replacement strategy
154
+ logic [SHARED_TLB_WAYS-1:0] way_valid;
155
+ logic update_lfsr; // shift the LFSR
156
+ logic [$clog2(SHARED_TLB_WAYS)-1:0] inv_way; // first non-valid encountered
157
+ logic [$clog2(SHARED_TLB_WAYS)-1:0] rnd_way; // random index for replacement
158
+ logic [$clog2(SHARED_TLB_WAYS)-1:0] repl_way; // way to replace
159
+ logic [SHARED_TLB_WAYS-1:0] repl_way_oh_d; // way to replace (onehot)
160
+ logic all_ways_valid; // we need to switch repl strategy since all are valid
161
+
162
+ assign shared_tlb_access_o = shared_tlb_access_q;
163
+ assign shared_tlb_hit_o = shared_tlb_hit_d;
164
+ assign shared_tlb_vaddr_o = shared_tlb_vaddr_q;
165
+ assign itlb_req_o = itlb_req_q;
166
+ assign v_st_enbl = {{v_i, g_st_enbl_i, s_st_enbl_i}, {ld_st_v_i, g_ld_st_enbl_i, s_ld_st_enbl_i}};
167
+
168
+ genvar i, x;
169
+ generate
170
+ for (i = 0; i < SHARED_TLB_WAYS; i++) begin : gen_match_tlb_ways
171
+ //identify page_match for all TLB Entries
172
+
173
+ for (x = 0; x < CVA6Cfg.PtLevels; x++) begin : gen_match
174
+ assign page_match[i][x] = x==0 ? 1 :((HYP_EXT==0 || x==(CVA6Cfg.PtLevels-1)) ? // PAGE_MATCH CONTAINS THE MATCH INFORMATION FOR EACH TAG OF is_1G and is_2M in sv39x4. HIGHER LEVEL (Giga page), THEN THERE IS THE Mega page AND AT THE LOWER LEVEL IS ALWAYS 1
175
+ &(shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-1-x] | (~v_st_enbl[i_req_q][HYP_EXT:0])):
176
+ ((&v_st_enbl[i_req_q][HYP_EXT:0]) ?
177
+ ((shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-1-x][0] && (shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-2-x][HYP_EXT] || shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-1-x][HYP_EXT]))
178
+ || (shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-1-x][HYP_EXT] && (shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-2-x][0] || shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-1-x][0]))):
179
+ shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-1-x][0] && v_st_enbl[i_req_q][0] || shared_tag_rd[i].is_page[CVA6Cfg.PtLevels-1-x][HYP_EXT] && v_st_enbl[i_req_q][HYP_EXT]));
180
+
181
+ //identify if vpn matches at all PT levels for all TLB entries
182
+ assign vpn_match[i][x] = (HYP_EXT==1 && x==(CVA6Cfg.PtLevels-1) && ~v_st_enbl[i_req_q][0]) ? //
183
+ vpn_q[x] == shared_tag_rd[i].vpn[x] && vpn_q[x+HYP_EXT][(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)-HYP_EXT:0] == shared_tag_rd[i].vpn[x+HYP_EXT][(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)-HYP_EXT:0]: //
184
+ vpn_q[x] == shared_tag_rd[i].vpn[x];
185
+
186
+ //identify if there is a hit at each PT level for all TLB entries
187
+ assign level_match[i][x] = &vpn_match[i][CVA6Cfg.PtLevels-1:x] && page_match[i][x];
188
+
189
+ end
190
+ end
191
+ endgenerate
192
+
193
+ genvar w;
194
+ generate
195
+ for (w = 0; w < CVA6Cfg.PtLevels; w++) begin
196
+ assign vpn_d[w] = ((|v_st_enbl[1][HYP_EXT:0]) && itlb_access_i && ~itlb_hit_i && ~dtlb_access_i) ? //
197
+ itlb_vaddr_i[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(w+1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*w)] : //
198
+ (((|v_st_enbl[0][HYP_EXT:0]) && dtlb_access_i && ~dtlb_hit_i) ? //
199
+ dtlb_vaddr_i[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(w+1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*w)] : vpn_q[w]);
200
+ end
201
+ endgenerate
202
+
203
+ if (CVA6Cfg.RVH) //THIS UPDATES THE EXTRA BITS OF VPN IN SV39x4
204
+ assign vpn_d[CVA6Cfg.PtLevels][(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)-1:0] = ((|v_st_enbl[1][HYP_EXT:0]) && itlb_access_i && ~itlb_hit_i && ~dtlb_access_i) ? //
205
+ itlb_vaddr_i[CVA6Cfg.VpnLen-1:CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)] : //
206
+ (((|v_st_enbl[0][HYP_EXT:0]) && dtlb_access_i && ~dtlb_hit_i) ? //
207
+ dtlb_vaddr_i[CVA6Cfg.VpnLen-1: CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)] : vpn_q[CVA6Cfg.PtLevels][(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)-1:0]);
208
+
209
+ ///////////////////////////////////////////////////////
210
+ // tag comparison, hit generation
211
+ ///////////////////////////////////////////////////////
212
+ always_comb begin : itlb_dtlb_miss
213
+ itlb_miss_o = 1'b0;
214
+ dtlb_miss_o = 1'b0;
215
+
216
+ tag_rd_en = '0;
217
+ pte_rd_en = '0;
218
+
219
+ itlb_req_d = 1'b0;
220
+ dtlb_req_d = 1'b0;
221
+
222
+ tlb_update_asid_d = tlb_update_asid_q;
223
+ tlb_update_vmid_d = tlb_update_vmid_q;
224
+
225
+ shared_tlb_access_d = '0;
226
+ shared_tlb_vaddr_d = shared_tlb_vaddr_q;
227
+
228
+ tag_rd_addr = '0;
229
+ pte_rd_addr = '0;
230
+ i_req_d = i_req_q;
231
+
232
+ // if we got an ITLB miss
233
+ if ((|v_st_enbl[1][HYP_EXT:0]) & itlb_access_i & ~itlb_hit_i & ~dtlb_access_i) begin
234
+ tag_rd_en = '1;
235
+ tag_rd_addr = itlb_vaddr_i[12+:$clog2(CVA6Cfg.SharedTlbDepth)];
236
+ pte_rd_en = '1;
237
+ pte_rd_addr = itlb_vaddr_i[12+:$clog2(CVA6Cfg.SharedTlbDepth)];
238
+
239
+ itlb_miss_o = shared_tlb_miss_i;
240
+ itlb_req_d = 1'b1;
241
+ tlb_update_asid_d = itlb_asid_i;
242
+ tlb_update_vmid_d = lu_vmid_i;
243
+
244
+ shared_tlb_access_d = '1;
245
+ shared_tlb_vaddr_d = itlb_vaddr_i;
246
+ i_req_d = 1;
247
+
248
+ // we got an DTLB miss
249
+ end else if ((|v_st_enbl[0][HYP_EXT:0]) & dtlb_access_i & ~dtlb_hit_i) begin
250
+ tag_rd_en = '1;
251
+ tag_rd_addr = dtlb_vaddr_i[12+:$clog2(CVA6Cfg.SharedTlbDepth)];
252
+ pte_rd_en = '1;
253
+ pte_rd_addr = dtlb_vaddr_i[12+:$clog2(CVA6Cfg.SharedTlbDepth)];
254
+
255
+ dtlb_miss_o = shared_tlb_miss_i;
256
+ dtlb_req_d = 1'b1;
257
+ tlb_update_asid_d = dtlb_asid_i;
258
+ tlb_update_vmid_d = lu_vmid_i;
259
+
260
+ shared_tlb_access_d = '1;
261
+ shared_tlb_vaddr_d = dtlb_vaddr_i;
262
+ i_req_d = 0;
263
+ end
264
+ end //itlb_dtlb_miss
265
+
266
+ always_comb begin : tag_comparison
267
+ shared_tlb_hit_d = 1'b0;
268
+ dtlb_update_o = '0;
269
+ itlb_update_o = '0;
270
+ match_asid = '{default: 0};
271
+ match_vmid = CVA6Cfg.RVH ? '{default: 0} : '{default: 1};
272
+
273
+
274
+ if (!CVA6Cfg.UseSharedTlb) begin
275
+ if (shared_tlb_update_i.valid) begin
276
+ shared_tlb_hit_d = 1'b1;
277
+ if (itlb_req_q) begin
278
+ itlb_update_o.valid = 1'b1;
279
+ itlb_update_o.vpn = shared_tlb_update_i.vpn;
280
+ itlb_update_o.is_page = shared_tlb_update_i.is_page;
281
+ itlb_update_o.content = shared_tlb_update_i.content;
282
+ itlb_update_o.g_content = shared_tlb_update_i.g_content;
283
+ itlb_update_o.v_st_enbl = v_st_enbl[i_req_q][HYP_EXT*2:0];
284
+ itlb_update_o.asid = shared_tlb_update_i.asid;
285
+ itlb_update_o.vmid = shared_tlb_update_i.vmid;
286
+
287
+ end else if (dtlb_req_q) begin
288
+ dtlb_update_o.valid = 1'b1;
289
+ dtlb_update_o.vpn = shared_tlb_update_i.vpn;
290
+ dtlb_update_o.is_page = shared_tlb_update_i.is_page;
291
+ dtlb_update_o.content = shared_tlb_update_i.content;
292
+ dtlb_update_o.g_content = shared_tlb_update_i.g_content;
293
+ dtlb_update_o.v_st_enbl = v_st_enbl[i_req_q][HYP_EXT*2:0];
294
+ dtlb_update_o.asid = shared_tlb_update_i.asid;
295
+ dtlb_update_o.vmid = shared_tlb_update_i.vmid;
296
+ end
297
+ end
298
+ end else begin
299
+
300
+ //number of ways
301
+ for (int unsigned i = 0; i < SHARED_TLB_WAYS; i++) begin
302
+ // first level match, this may be a giga page, check the ASID flags as well
303
+ // if the entry is associated to a global address, don't match the ASID (ASID is don't care)
304
+ match_asid[i] = (((tlb_update_asid_q == shared_tag_rd[i].asid) || pte[i][0].g) && v_st_enbl[i_req_q][0]) || !v_st_enbl[i_req_q][0];
305
+
306
+ if (CVA6Cfg.RVH) begin
307
+ match_vmid[i] = (tlb_update_vmid_q == shared_tag_rd[i].vmid && v_st_enbl[i_req_q][HYP_EXT]) || !v_st_enbl[i_req_q][HYP_EXT];
308
+ end
309
+
310
+ // check if translation is a: S-Stage and G-Stage, S-Stage only or G-Stage only translation and virtualization mode is on/off
311
+ match_stage[i] = shared_tag_rd[i].v_st_enbl == v_st_enbl[i_req_q][HYP_EXT*2:0];
312
+
313
+ if (shared_tag_valid[i] && match_asid && match_vmid && match_stage[i]) begin
314
+ if (|level_match[i]) begin
315
+ shared_tlb_hit_d = 1'b1;
316
+ if (itlb_req_q) begin
317
+ itlb_update_o.valid = 1'b1;
318
+ itlb_update_o.vpn = itlb_vpn_q;
319
+ itlb_update_o.is_page = shared_tag_rd[i].is_page;
320
+ itlb_update_o.content = pte[i][0];
321
+ itlb_update_o.g_content = pte[i][HYP_EXT];
322
+ itlb_update_o.v_st_enbl = shared_tag_rd[i].v_st_enbl;
323
+ itlb_update_o.asid = tlb_update_asid_q;
324
+ itlb_update_o.vmid = tlb_update_vmid_q;
325
+ end else if (dtlb_req_q) begin
326
+ dtlb_update_o.valid = 1'b1;
327
+ dtlb_update_o.vpn = dtlb_vpn_q;
328
+ dtlb_update_o.is_page = shared_tag_rd[i].is_page;
329
+ dtlb_update_o.content = pte[i][0];
330
+ dtlb_update_o.g_content = pte[i][HYP_EXT];
331
+ dtlb_update_o.v_st_enbl = shared_tag_rd[i].v_st_enbl;
332
+ dtlb_update_o.asid = tlb_update_asid_q;
333
+ dtlb_update_o.vmid = tlb_update_vmid_q;
334
+ end
335
+ end
336
+ end
337
+ end
338
+ end
339
+ end //tag_comparison
340
+
341
+ // sequential process
342
+ always_ff @(posedge clk_i or negedge rst_ni) begin
343
+ if (~rst_ni) begin
344
+ itlb_vpn_q <= '0;
345
+ dtlb_vpn_q <= '0;
346
+ tlb_update_asid_q <= '{default: 0};
347
+ tlb_update_vmid_q <= '{default: 0};
348
+ shared_tlb_access_q <= '0;
349
+ shared_tlb_vaddr_q <= '0;
350
+ shared_tag_valid_q <= '0;
351
+ vpn_q <= 0;
352
+ itlb_req_q <= '0;
353
+ dtlb_req_q <= '0;
354
+ i_req_q <= 0;
355
+ shared_tag_valid <= '0;
356
+ end else begin
357
+ itlb_vpn_q <= itlb_vaddr_i[CVA6Cfg.SV-1:12];
358
+ dtlb_vpn_q <= dtlb_vaddr_i[CVA6Cfg.SV-1:12];
359
+ tlb_update_asid_q <= tlb_update_asid_d;
360
+ shared_tlb_access_q <= shared_tlb_access_d;
361
+ shared_tlb_vaddr_q <= shared_tlb_vaddr_d;
362
+ shared_tag_valid_q <= shared_tag_valid_d;
363
+ vpn_q <= vpn_d;
364
+ itlb_req_q <= itlb_req_d;
365
+ dtlb_req_q <= dtlb_req_d;
366
+ i_req_q <= i_req_d;
367
+ shared_tag_valid <= shared_tag_valid_q[tag_rd_addr];
368
+
369
+ if (CVA6Cfg.RVH) tlb_update_vmid_q <= tlb_update_vmid_d;
370
+ end
371
+ end
372
+
373
+ // ------------------
374
+ // Update and Flush
375
+ // ------------------
376
+ always_comb begin : update_flush
377
+ shared_tag_valid_d = shared_tag_valid_q;
378
+ tag_wr_en = '0;
379
+ pte_wr_en = '0;
380
+
381
+ if (flush_i || flush_vvma_i || flush_gvma_i) begin
382
+ shared_tag_valid_d = '0;
383
+ end else if (shared_tlb_update_i.valid) begin
384
+ for (int unsigned i = 0; i < SHARED_TLB_WAYS; i++) begin
385
+ if (repl_way_oh_d[i]) begin
386
+ shared_tag_valid_d[shared_tlb_update_i.vpn[$clog2(CVA6Cfg.SharedTlbDepth)-1:0]][i] = 1'b1;
387
+ tag_wr_en[i] = 1'b1;
388
+ pte_wr_en[i] = 1'b1;
389
+ end
390
+ end
391
+ end
392
+ end //update_flush
393
+
394
+ assign shared_tag_wr.asid = shared_tlb_update_i.asid;
395
+ assign shared_tag_wr.vmid = shared_tlb_update_i.vmid;
396
+ assign shared_tag_wr.is_page = shared_tlb_update_i.is_page;
397
+ assign shared_tag_wr.v_st_enbl = v_st_enbl[i_req_q][HYP_EXT*2:0];
398
+
399
+ genvar z;
400
+ generate
401
+ for (z = 0; z < CVA6Cfg.PtLevels; z++) begin : gen_shared_tag
402
+ assign shared_tag_wr.vpn[z] = shared_tlb_update_i.vpn[((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(z+1))-1:((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*z)];
403
+ end
404
+ if (CVA6Cfg.RVH) begin : gen_shared_tag_hyp
405
+ //THIS UPDATES THE EXTRA BITS OF VPN IN SV39x4
406
+ assign shared_tag_wr.vpn[CVA6Cfg.PtLevels][(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)-1:0] = shared_tlb_update_i.vpn[CVA6Cfg.VpnLen-1: CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)];
407
+ end
408
+ endgenerate
409
+
410
+
411
+ assign tag_wr_addr = shared_tlb_update_i.vpn[$clog2(CVA6Cfg.SharedTlbDepth)-1:0];
412
+ assign tag_wr_data = shared_tag_wr;
413
+
414
+ assign pte_wr_addr = shared_tlb_update_i.vpn[$clog2(CVA6Cfg.SharedTlbDepth)-1:0];
415
+
416
+ assign pte_wr_data[0] = shared_tlb_update_i.content[CVA6Cfg.XLEN-1:0];
417
+ assign pte_wr_data[1] = shared_tlb_update_i.g_content[CVA6Cfg.XLEN-1:0];
418
+
419
+
420
+
421
+ assign way_valid = shared_tag_valid_q[shared_tlb_update_i.vpn[$clog2(
422
+ CVA6Cfg.SharedTlbDepth
423
+ )-1:0]];
424
+ assign repl_way = (all_ways_valid) ? rnd_way : inv_way;
425
+ assign update_lfsr = shared_tlb_update_i.valid & all_ways_valid;
426
+ assign repl_way_oh_d = (shared_tlb_update_i.valid) ? shared_tlb_way_bin2oh(repl_way) : '0;
427
+
428
+ lzc #(
429
+ .WIDTH(SHARED_TLB_WAYS)
430
+ ) i_lzc (
431
+ .in_i (~way_valid),
432
+ .cnt_o (inv_way),
433
+ .empty_o(all_ways_valid)
434
+ );
435
+
436
+ lfsr #(
437
+ .LfsrWidth(8),
438
+ .OutWidth ($clog2(SHARED_TLB_WAYS))
439
+ ) i_lfsr (
440
+ .clk_i (clk_i),
441
+ .rst_ni(rst_ni),
442
+ .en_i (update_lfsr),
443
+ .out_o (rnd_way)
444
+ );
445
+
446
+ ///////////////////////////////////////////////////////
447
+ // memory arrays and regs
448
+ ///////////////////////////////////////////////////////
449
+
450
+ assign tag_req = tag_wr_en | tag_rd_en;
451
+ assign tag_we = tag_wr_en;
452
+ assign tag_addr = tag_wr_en ? tag_wr_addr : tag_rd_addr;
453
+
454
+ assign pte_req = pte_wr_en | pte_rd_en;
455
+ assign pte_we = pte_wr_en;
456
+ assign pte_addr = pte_wr_en ? pte_wr_addr : pte_rd_addr;
457
+
458
+ for (genvar i = 0; i < SHARED_TLB_WAYS; i++) begin : gen_sram
459
+ if (CVA6Cfg.UseSharedTlb) begin
460
+ // Tag RAM
461
+ sram #(
462
+ .DATA_WIDTH($bits(shared_tag_t)),
463
+ .NUM_WORDS (CVA6Cfg.SharedTlbDepth)
464
+ ) tag_sram (
465
+ .clk_i (clk_i),
466
+ .rst_ni (rst_ni),
467
+ .req_i (tag_req[i]),
468
+ .we_i (tag_we[i]),
469
+ .addr_i (tag_addr),
470
+ .wuser_i('0),
471
+ .wdata_i(tag_wr_data),
472
+ .be_i ('1),
473
+ .ruser_o(),
474
+ .rdata_o(tag_rd_data[i])
475
+ );
476
+
477
+ assign shared_tag_rd[i] = shared_tag_t'(tag_rd_data[i]);
478
+
479
+ for (genvar a = 0; a < HYP_EXT + 1; a++) begin : g_content_sram
480
+ // PTE RAM
481
+ sram #(
482
+ .DATA_WIDTH(CVA6Cfg.XLEN),
483
+ .NUM_WORDS (CVA6Cfg.SharedTlbDepth)
484
+ ) pte_sram (
485
+ .clk_i (clk_i),
486
+ .rst_ni (rst_ni),
487
+ .req_i (pte_req[i]),
488
+ .we_i (pte_we[i]),
489
+ .addr_i (pte_addr),
490
+ .wuser_i('0),
491
+ .wdata_i(pte_wr_data[a]),
492
+ .be_i ('1),
493
+ .ruser_o(),
494
+ .rdata_o(pte_rd_data[i][a])
495
+ );
496
+ assign pte[i][a] = pte_cva6_t'(pte_rd_data[i][a]);
497
+ end
498
+ end
499
+ end
500
+ endmodule
501
+
502
+ /* verilator lint_on WIDTH */
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_mmu/cva6_tlb.sv ADDED
@@ -0,0 +1,439 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright (c) 2018 ETH Zurich and University of Bologna.
2
+ // Copyright (c) 2021 Thales.
3
+ // Copyright (c) 2022 Bruno Sá and Zero-Day Labs.
4
+ // Copyright (c) 2024 PlanV Technology
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
6
+ // Copyright and related rights are licensed under the Solderpad Hardware
7
+ // License, Version 0.51 (the "License"); you may not use this file except in
8
+ // compliance with the License. You may obtain a copy of the License at
9
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
10
+ // or agreed to in writing, software, hardware and materials distributed under
11
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
+ // specific language governing permissions and limitations under the License.
14
+ //
15
+ // Author: Angela Gonzalez PlanV Technology
16
+ // Date: 26/02/2024
17
+ //
18
+ // Description: Translation Lookaside Buffer, parameterizable to Sv32 or Sv39 ,
19
+ // or sv39x4 fully set-associative
20
+ // This module is an merge of the Sv32 TLB developed by Sebastien
21
+ // Jacq (Thales Research & Technology), the Sv39 TLB developed
22
+ // by Florian Zaruba and David Schaffenrath and the Sv39x4 by Bruno Sá.
23
+
24
+ module cva6_tlb
25
+ import ariane_pkg::*;
26
+ #(
27
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
28
+ parameter type pte_cva6_t = logic,
29
+ parameter type tlb_update_cva6_t = logic,
30
+ parameter int unsigned TLB_ENTRIES = 4,
31
+ parameter int unsigned HYP_EXT = 0
32
+ ) (
33
+ input logic clk_i, // Clock
34
+ input logic rst_ni, // Asynchronous reset active low
35
+ input logic flush_i, // Flush normal translations signal
36
+ input logic flush_vvma_i, // Flush vs stage signal
37
+ input logic flush_gvma_i, // Flush g stage signal
38
+ input logic s_st_enbl_i, // s-stage enabled
39
+ input logic g_st_enbl_i, // g-stage enabled
40
+ input logic v_i, // virtualization mode
41
+ // Update TLB
42
+ input tlb_update_cva6_t update_i,
43
+ // Lookup signals
44
+ input logic lu_access_i,
45
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] lu_asid_i,
46
+ input logic [CVA6Cfg.VMID_WIDTH-1:0] lu_vmid_i,
47
+ input logic [CVA6Cfg.VLEN-1:0] lu_vaddr_i,
48
+ output logic [CVA6Cfg.GPLEN-1:0] lu_gpaddr_o,
49
+ output pte_cva6_t lu_content_o,
50
+ output pte_cva6_t lu_g_content_o,
51
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] asid_to_be_flushed_i,
52
+ input logic [CVA6Cfg.VMID_WIDTH-1:0] vmid_to_be_flushed_i,
53
+ input logic [CVA6Cfg.VLEN-1:0] vaddr_to_be_flushed_i,
54
+ input logic [CVA6Cfg.GPLEN-1:0] gpaddr_to_be_flushed_i,
55
+ output logic [CVA6Cfg.PtLevels-2:0] lu_is_page_o,
56
+ output logic lu_hit_o
57
+ );
58
+ localparam GPPN2 = (CVA6Cfg.XLEN == 32) ? CVA6Cfg.VLEN - 33 : 10;
59
+ // SV39 defines three levels of page tables
60
+ struct packed {
61
+ logic [CVA6Cfg.ASID_WIDTH-1:0] asid;
62
+ logic [CVA6Cfg.VMID_WIDTH-1:0] vmid;
63
+ logic [CVA6Cfg.PtLevels+HYP_EXT-1:0][(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)-1:0] vpn;
64
+ logic [CVA6Cfg.PtLevels-2:0][HYP_EXT:0] is_page;
65
+ logic [HYP_EXT*2:0] v_st_enbl; // v_i,g-stage enabled, s-stage enabled
66
+ logic valid;
67
+ } [TLB_ENTRIES-1:0]
68
+ tags_q, tags_n;
69
+
70
+ struct packed {
71
+ pte_cva6_t pte;
72
+ pte_cva6_t gpte;
73
+ } [TLB_ENTRIES-1:0]
74
+ content_q, content_n;
75
+
76
+ logic [TLB_ENTRIES-1:0][CVA6Cfg.PtLevels-1:0] vpn_match;
77
+ logic [TLB_ENTRIES-1:0][CVA6Cfg.PtLevels-1:0] level_match;
78
+ logic [TLB_ENTRIES-1:0][HYP_EXT:0][CVA6Cfg.PtLevels-1:0] vaddr_vpn_match;
79
+ logic [TLB_ENTRIES-1:0][HYP_EXT:0][CVA6Cfg.PtLevels-1:0] vaddr_level_match;
80
+ logic [TLB_ENTRIES-1:0] lu_hit; // to replacement logic
81
+ logic [TLB_ENTRIES-1:0] replace_en; // replace the following entry, set by replacement strategy
82
+ logic [TLB_ENTRIES-1:0] match_asid;
83
+ logic [TLB_ENTRIES-1:0] match_vmid;
84
+ logic [TLB_ENTRIES-1:0][CVA6Cfg.PtLevels-1:0] page_match;
85
+ logic [TLB_ENTRIES-1:0][HYP_EXT:0][CVA6Cfg.PtLevels-1:0] vpage_match;
86
+ logic [TLB_ENTRIES-1:0][CVA6Cfg.PtLevels-2:0] is_page_o;
87
+ logic [TLB_ENTRIES-1:0] match_stage;
88
+ pte_cva6_t g_content;
89
+ logic [TLB_ENTRIES-1:0][(CVA6Cfg.GPPNW-1):0] gppn;
90
+ logic [2:0] v_st_enbl;
91
+
92
+ assign v_st_enbl = (CVA6Cfg.RVH) ? {v_i, g_st_enbl_i, s_st_enbl_i} : '1;
93
+ //-------------
94
+ // Translation
95
+ //-------------
96
+
97
+ genvar i, x, z, w;
98
+ generate
99
+ for (i = 0; i < TLB_ENTRIES; i++) begin
100
+ for (x = 0; x < CVA6Cfg.PtLevels; x++) begin
101
+ //identify page_match for all TLB Entries
102
+ assign page_match[i][x] = x==0 ? 1 :((HYP_EXT==0 || x==(CVA6Cfg.PtLevels-1)) ? // PAGE_MATCH CONTAINS THE MATCH INFORMATION FOR EACH TAG OF is_1G and is_2M in sv39x4. HIGHER LEVEL (Giga page), THEN THERE IS THE Mega page AND AT THE LOWER LEVEL IS ALWAYS 1
103
+ &(tags_q[i].is_page[CVA6Cfg.PtLevels-1-x] | (~v_st_enbl[HYP_EXT:0])):
104
+ ((&v_st_enbl[HYP_EXT:0]) ?
105
+ ((tags_q[i].is_page[CVA6Cfg.PtLevels-1-x][0] && (tags_q[i].is_page[CVA6Cfg.PtLevels-2-x][HYP_EXT] || tags_q[i].is_page[CVA6Cfg.PtLevels-1-x][HYP_EXT]))
106
+ || (tags_q[i].is_page[CVA6Cfg.PtLevels-1-x][HYP_EXT] && (tags_q[i].is_page[CVA6Cfg.PtLevels-2-x][0] || tags_q[i].is_page[CVA6Cfg.PtLevels-1-x][0]))):
107
+ tags_q[i].is_page[CVA6Cfg.PtLevels-1-x][0] && s_st_enbl_i || tags_q[i].is_page[CVA6Cfg.PtLevels-1-x][HYP_EXT] && g_st_enbl_i));
108
+
109
+ //identify if vpn matches at all PT levels for all TLB entries
110
+ assign vpn_match[i][x] = (CVA6Cfg.RVH && x == (CVA6Cfg.PtLevels - 1) && ~s_st_enbl_i) ? //
111
+ lu_vaddr_i[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(x+1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*x)] == tags_q[i].vpn[x] && lu_vaddr_i[12+HYP_EXT*(CVA6Cfg.VpnLen-1): 12+HYP_EXT*(CVA6Cfg.VpnLen-(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels))] == tags_q[i].vpn[x+HYP_EXT][(CVA6Cfg.VpnLen%CVA6Cfg.PtLevels)-HYP_EXT:0]: //
112
+ lu_vaddr_i[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(x+1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*x)] == tags_q[i].vpn[x];
113
+
114
+ //identify if there is a hit at each PT level for all TLB entries
115
+ assign level_match[i][x] = &vpn_match[i][CVA6Cfg.PtLevels-1:x] && page_match[i][x];
116
+
117
+ //identify vpage_match for all TLB Entries and vaddr_level match (if there is a hit at each PT level for all TLB entries on the vaddr)
118
+ for (z = 0; z < HYP_EXT + 1; z++) begin
119
+ assign vpage_match[i][z][x] = x == 0 ? 1 : tags_q[i].is_page[CVA6Cfg.PtLevels-1-x][z];
120
+ assign vaddr_level_match[i][z][x]= &vaddr_vpn_match[i][z][CVA6Cfg.PtLevels-1:x] && vpage_match[i][z][x];
121
+
122
+ end
123
+ //identify if virtual address vpn matches at all PT levels for all TLB entries
124
+ assign vaddr_vpn_match[i][0][x] = vaddr_to_be_flushed_i[12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*(x+1))-1:12+((CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)*x)] == tags_q[i].vpn[x];
125
+
126
+ end
127
+
128
+
129
+
130
+ if (CVA6Cfg.RVH) begin
131
+ //identify if GPADDR matches the GPPN
132
+ assign vaddr_vpn_match[i][HYP_EXT][0] = (gpaddr_to_be_flushed_i[20:12] == gppn[i][8:0]);
133
+ assign vaddr_vpn_match[i][HYP_EXT][HYP_EXT] = (gpaddr_to_be_flushed_i[29:21] == gppn[i][17:9]);
134
+ assign vaddr_vpn_match[i][HYP_EXT][HYP_EXT*2] = (gpaddr_to_be_flushed_i[30+GPPN2:30] == gppn[i][18+GPPN2:18]);
135
+
136
+ end
137
+
138
+ for (w = 0; w < CVA6Cfg.PtLevels - 1; w++) begin
139
+ assign is_page_o[i][w] = page_match[i][CVA6Cfg.PtLevels - 1 - w]; //THIS REORGANIZES THE PAGES TO MATCH THE OUTPUT STRUCTURE (2M,1G)
140
+ end
141
+ end
142
+ endgenerate
143
+
144
+ always_comb begin : translation
145
+
146
+ // default assignment
147
+ lu_hit = '{default: 0};
148
+ lu_hit_o = 1'b0;
149
+ lu_content_o = '{default: 0};
150
+ lu_g_content_o = '{default: 0};
151
+ lu_is_page_o = '{default: 0};
152
+ match_asid = '{default: 0};
153
+ match_vmid = CVA6Cfg.RVH ? '{default: 0} : '{default: 1};
154
+ match_stage = '{default: 0};
155
+ g_content = '{default: 0};
156
+ lu_gpaddr_o = '{default: 0};
157
+
158
+ for (int unsigned i = 0; i < TLB_ENTRIES; i++) begin
159
+ // first level match, this may be a giga page, check the ASID flags as well
160
+ // if the entry is associated to a global address, don't match the ASID (ASID is don't care)
161
+ match_asid[i] = ((lu_asid_i == tags_q[i].asid || content_q[i].pte.g) && s_st_enbl_i) || !s_st_enbl_i;
162
+
163
+ if (CVA6Cfg.RVH) begin
164
+ match_vmid[i] = (lu_vmid_i == tags_q[i].vmid && g_st_enbl_i) || !g_st_enbl_i;
165
+ end
166
+
167
+ // check if translation is a: S-Stage and G-Stage, S-Stage only or G-Stage only translation and virtualization mode is on/off
168
+ match_stage[i] = tags_q[i].v_st_enbl[HYP_EXT*2:0] == v_st_enbl[HYP_EXT*2:0];
169
+
170
+ if (tags_q[i].valid && match_asid[i] && match_vmid[i] && match_stage[i]) begin
171
+
172
+ if (CVA6Cfg.RVH && vpn_match[i][HYP_EXT*2]) begin
173
+ if (s_st_enbl_i) begin
174
+ lu_gpaddr_o = {content_q[i].pte.ppn[(CVA6Cfg.GPPNW-1):0], lu_vaddr_i[11:0]};
175
+ // Giga page
176
+ if (tags_q[i].is_page[0][0])
177
+ lu_gpaddr_o[12+2*CVA6Cfg.VpnLen/CVA6Cfg.PtLevels-1:12] = lu_vaddr_i[12+2*CVA6Cfg.VpnLen/CVA6Cfg.PtLevels-1:12];
178
+ // Mega page
179
+ if (tags_q[i].is_page[HYP_EXT][0])
180
+ lu_gpaddr_o[12+CVA6Cfg.VpnLen/CVA6Cfg.PtLevels-1:12] = lu_vaddr_i[12+CVA6Cfg.VpnLen/CVA6Cfg.PtLevels-1:12];
181
+ end else begin
182
+ lu_gpaddr_o =CVA6Cfg.GPLEN'(lu_vaddr_i[(CVA6Cfg.XLEN == 32 ? CVA6Cfg.VLEN: CVA6Cfg.GPLEN)-1:0]);
183
+ end
184
+ end
185
+
186
+ if (|level_match[i]) begin
187
+ lu_is_page_o = is_page_o[i];
188
+ lu_content_o = content_q[i].pte;
189
+ lu_hit_o = 1'b1;
190
+ lu_hit[i] = 1'b1;
191
+
192
+ if (CVA6Cfg.RVH) begin
193
+ // Compute G-Stage PPN based on the gpaddr
194
+ g_content = content_q[i].gpte;
195
+ if (tags_q[i].is_page[HYP_EXT][HYP_EXT]) g_content.ppn[8:0] = lu_gpaddr_o[20:12];
196
+ if (tags_q[i].is_page[0][HYP_EXT]) g_content.ppn[17:0] = lu_gpaddr_o[29:12];
197
+ // Output G-stage and S-stage content
198
+ lu_g_content_o = level_match[i][CVA6Cfg.PtLevels-1] ? content_q[i].gpte : g_content;
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ logic [HYP_EXT:0]asid_to_be_flushed_is0; // indicates that the ASID provided by SFENCE.VMA (rs2)is 0, active high
206
+ logic [HYP_EXT:0] vaddr_to_be_flushed_is0; // indicates that the VADDR provided by SFENCE.VMA (rs1)is 0, active high
207
+ logic vmid_to_be_flushed_is0; // indicates that the VMID provided is 0, active high
208
+ logic gpaddr_to_be_flushed_is0; // indicates that the GPADDR provided is 0, active high
209
+
210
+ assign asid_to_be_flushed_is0 = ~(|asid_to_be_flushed_i);
211
+ assign vaddr_to_be_flushed_is0 = ~(|vaddr_to_be_flushed_i);
212
+ assign vmid_to_be_flushed_is0 = ~(|vmid_to_be_flushed_i);
213
+ assign gpaddr_to_be_flushed_is0 = ~(|gpaddr_to_be_flushed_i);
214
+
215
+ // ------------------
216
+ // Update and Flush
217
+ // ------------------
218
+ always_comb begin : update_flush
219
+ tags_n = tags_q;
220
+ content_n = content_q;
221
+
222
+ for (int unsigned i = 0; i < TLB_ENTRIES; i++) begin
223
+
224
+
225
+ if (CVA6Cfg.RVH) begin
226
+
227
+ if (tags_q[i].v_st_enbl[0]) begin
228
+ gppn[i] = content_q[i].pte.ppn[(CVA6Cfg.GPPNW-1):0];
229
+ if (tags_q[i].is_page[HYP_EXT][0])
230
+ gppn[i][CVA6Cfg.VpnLen/CVA6Cfg.PtLevels-1:0] = tags_q[i].vpn[0];
231
+ if (tags_q[i].is_page[0][0])
232
+ gppn[i][2*(CVA6Cfg.VpnLen/CVA6Cfg.PtLevels)-1:0] = {
233
+ tags_q[i].vpn[HYP_EXT], tags_q[i].vpn[0]
234
+ };
235
+ end else begin
236
+ gppn[i][CVA6Cfg.VpnLen-1:0] = CVA6Cfg.VpnLen'(tags_q[i].vpn);
237
+ end
238
+ end
239
+
240
+
241
+ if (flush_i) begin
242
+ if (!tags_q[i].v_st_enbl[HYP_EXT*2] || HYP_EXT == 0) begin
243
+ // invalidate logic
244
+ // flush everything if ASID is 0 and vaddr is 0 ("SFENCE.VMA x0 x0" case)
245
+ if (asid_to_be_flushed_is0 && vaddr_to_be_flushed_is0) tags_n[i].valid = 1'b0;
246
+ // flush vaddr in all addressing space ("SFENCE.VMA vaddr x0" case), it should happen only for leaf pages
247
+ else if (asid_to_be_flushed_is0 && (|vaddr_level_match[i][0] ) && (~vaddr_to_be_flushed_is0))
248
+ tags_n[i].valid = 1'b0;
249
+ // the entry is flushed if it's not global and asid and vaddr both matches with the entry to be flushed ("SFENCE.VMA vaddr asid" case)
250
+ else if ((!content_q[i].pte.g) && (|vaddr_level_match[i][0]) && (asid_to_be_flushed_i == tags_q[i].asid ) && (!vaddr_to_be_flushed_is0) && (!asid_to_be_flushed_is0))
251
+ tags_n[i].valid = 1'b0;
252
+ // the entry is flushed if it's not global, and the asid matches and vaddr is 0. ("SFENCE.VMA 0 asid" case)
253
+ else if ((!content_q[i].pte.g) && (vaddr_to_be_flushed_is0) && (asid_to_be_flushed_i == tags_q[i].asid ) && (!asid_to_be_flushed_is0))
254
+ tags_n[i].valid = 1'b0;
255
+ end
256
+ end else if (flush_vvma_i && CVA6Cfg.RVH) begin
257
+ if (tags_q[i].v_st_enbl[HYP_EXT*2] && tags_q[i].v_st_enbl[0]) begin
258
+ // invalidate logic
259
+ // flush everything if current VMID matches and ASID is 0 and vaddr is 0 ("SFENCE.VMA/HFENCE.VVMA x0 x0" case)
260
+ if (asid_to_be_flushed_is0 && vaddr_to_be_flushed_is0 && ((tags_q[i].v_st_enbl[HYP_EXT] && lu_vmid_i == tags_q[i].vmid) || !tags_q[i].v_st_enbl[HYP_EXT]))
261
+ tags_n[i].valid = 1'b0;
262
+ // flush vaddr in all addressing space if current VMID matches ("SFENCE.VMA/HFENCE.VVMA vaddr x0" case), it should happen only for leaf pages
263
+ else if (asid_to_be_flushed_is0 && (|vaddr_level_match[i][0]) && (~vaddr_to_be_flushed_is0) && ((tags_q[i].v_st_enbl[HYP_EXT] && lu_vmid_i == tags_q[i].vmid) || !tags_q[i].v_st_enbl[HYP_EXT]))
264
+ tags_n[i].valid = 1'b0;
265
+ // the entry is flushed if it's not global and asid and vaddr and current VMID matches with the entry to be flushed ("SFENCE.VMA/HFENCE.VVMA vaddr asid" case)
266
+ else if ((!content_q[i].pte.g) && (|vaddr_level_match[i][0]) && (asid_to_be_flushed_i == tags_q[i].asid && ((tags_q[i].v_st_enbl[HYP_EXT] && lu_vmid_i == tags_q[i].vmid) || !tags_q[i].v_st_enbl[HYP_EXT])) && (!vaddr_to_be_flushed_is0) && (!asid_to_be_flushed_is0))
267
+ tags_n[i].valid = 1'b0;
268
+ // the entry is flushed if it's not global, and the asid and the current VMID matches and vaddr is 0. ("SFENCE.VMA/HFENCE.VVMA 0 asid" case)
269
+ else if ((!content_q[i].pte.g) && (vaddr_to_be_flushed_is0) && (asid_to_be_flushed_i == tags_q[i].asid && ((tags_q[i].v_st_enbl[HYP_EXT] && lu_vmid_i == tags_q[i].vmid) || !tags_q[i].v_st_enbl[HYP_EXT])) && (!asid_to_be_flushed_is0))
270
+ tags_n[i].valid = 1'b0;
271
+ end
272
+ end else if (flush_gvma_i && CVA6Cfg.RVH) begin
273
+ if (tags_q[i].v_st_enbl[HYP_EXT]) begin
274
+ // invalidate logic
275
+ // flush everything if vmid is 0 and addr is 0 ("HFENCE.GVMA x0 x0" case)
276
+ if (vmid_to_be_flushed_is0 && gpaddr_to_be_flushed_is0) tags_n[i].valid = 1'b0;
277
+ // flush gpaddr in all addressing space ("HFENCE.GVMA gpaddr x0" case), it should happen only for leaf pages
278
+ else if (vmid_to_be_flushed_is0 && (|vaddr_level_match[i][HYP_EXT] ) && (~gpaddr_to_be_flushed_is0))
279
+ tags_n[i].valid = 1'b0;
280
+ // the entry vmid and gpaddr both matches with the entry to be flushed ("HFENCE.GVMA gpaddr vmid" case)
281
+ else if ((|vaddr_level_match[i][HYP_EXT]) && (vmid_to_be_flushed_i == tags_q[i].vmid) && (~gpaddr_to_be_flushed_is0) && (~vmid_to_be_flushed_is0))
282
+ tags_n[i].valid = 1'b0;
283
+ // the entry is flushed if the vmid matches and gpaddr is 0. ("HFENCE.GVMA 0 vmid" case)
284
+ else if ((gpaddr_to_be_flushed_is0) && (vmid_to_be_flushed_i == tags_q[i].vmid) && (!vmid_to_be_flushed_is0))
285
+ tags_n[i].valid = 1'b0;
286
+ end
287
+ // normal replacement
288
+ end else if (update_i.valid & replace_en[i] & !lu_hit_o) begin
289
+ //update tag
290
+ tags_n[i] = {
291
+ update_i.asid,
292
+ update_i.vmid,
293
+ ((CVA6Cfg.PtLevels + HYP_EXT) * (CVA6Cfg.VpnLen / CVA6Cfg.PtLevels))'(update_i.vpn),
294
+ update_i.is_page,
295
+ update_i.v_st_enbl,
296
+ 1'b1
297
+ };
298
+ // update content as well
299
+ content_n[i].pte = update_i.content;
300
+ if (CVA6Cfg.RVH) content_n[i].gpte = update_i.g_content;
301
+ end
302
+ end
303
+ end
304
+
305
+ // -----------------------------------------------
306
+ // PLRU - Pseudo Least Recently Used Replacement
307
+ // -----------------------------------------------
308
+ logic [2*(TLB_ENTRIES-1)-1:0] plru_tree_q, plru_tree_n;
309
+ always_comb begin : plru_replacement
310
+ plru_tree_n = plru_tree_q;
311
+ // The PLRU-tree indexing:
312
+ // lvl0 0
313
+ // / \
314
+ // / \
315
+ // lvl1 1 2
316
+ // / \ / \
317
+ // lvl2 3 4 5 6
318
+ // / \ /\/\ /\
319
+ // ... ... ... ...
320
+ // Just predefine which nodes will be set/cleared
321
+ // E.g. for a TLB with 8 entries, the for-loop is semantically
322
+ // equivalent to the following pseudo-code:
323
+ // unique case (1'b1)
324
+ // lu_hit[7]: plru_tree_n[0, 2, 6] = {1, 1, 1};
325
+ // lu_hit[6]: plru_tree_n[0, 2, 6] = {1, 1, 0};
326
+ // lu_hit[5]: plru_tree_n[0, 2, 5] = {1, 0, 1};
327
+ // lu_hit[4]: plru_tree_n[0, 2, 5] = {1, 0, 0};
328
+ // lu_hit[3]: plru_tree_n[0, 1, 4] = {0, 1, 1};
329
+ // lu_hit[2]: plru_tree_n[0, 1, 4] = {0, 1, 0};
330
+ // lu_hit[1]: plru_tree_n[0, 1, 3] = {0, 0, 1};
331
+ // lu_hit[0]: plru_tree_n[0, 1, 3] = {0, 0, 0};
332
+ // default: begin /* No hit */ end
333
+ // endcase
334
+ for (
335
+ int unsigned i = 0; i < TLB_ENTRIES; i++
336
+ ) begin
337
+ automatic int unsigned idx_base, shift, new_index;
338
+ // we got a hit so update the pointer as it was least recently used
339
+ if (lu_hit[i] & lu_access_i) begin
340
+ // Set the nodes to the values we would expect
341
+ for (int unsigned lvl = 0; lvl < $clog2(TLB_ENTRIES); lvl++) begin
342
+ idx_base = $unsigned((2 ** lvl) - 1);
343
+ // lvl0 <=> MSB, lvl1 <=> MSB-1, ...
344
+ shift = $clog2(TLB_ENTRIES) - lvl;
345
+ // to circumvent the 32 bit integer arithmetic assignment
346
+ new_index = ~((i >> (shift - 1)) & 32'b1);
347
+ plru_tree_n[idx_base+(i>>shift)] = new_index[0];
348
+ end
349
+ end
350
+ end
351
+ // Decode tree to write enable signals
352
+ // Next for-loop basically creates the following logic for e.g. an 8 entry
353
+ // TLB (note: pseudo-code obviously):
354
+ // replace_en[7] = &plru_tree_q[ 6, 2, 0]; //plru_tree_q[0,2,6]=={1,1,1}
355
+ // replace_en[6] = &plru_tree_q[~6, 2, 0]; //plru_tree_q[0,2,6]=={1,1,0}
356
+ // replace_en[5] = &plru_tree_q[ 5,~2, 0]; //plru_tree_q[0,2,5]=={1,0,1}
357
+ // replace_en[4] = &plru_tree_q[~5,~2, 0]; //plru_tree_q[0,2,5]=={1,0,0}
358
+ // replace_en[3] = &plru_tree_q[ 4, 1,~0]; //plru_tree_q[0,1,4]=={0,1,1}
359
+ // replace_en[2] = &plru_tree_q[~4, 1,~0]; //plru_tree_q[0,1,4]=={0,1,0}
360
+ // replace_en[1] = &plru_tree_q[ 3,~1,~0]; //plru_tree_q[0,1,3]=={0,0,1}
361
+ // replace_en[0] = &plru_tree_q[~3,~1,~0]; //plru_tree_q[0,1,3]=={0,0,0}
362
+ // For each entry traverse the tree. If every tree-node matches,
363
+ // the corresponding bit of the entry's index, this is
364
+ // the next entry to replace.
365
+ for (int unsigned i = 0; i < TLB_ENTRIES; i += 1) begin
366
+ automatic logic en;
367
+ automatic int unsigned idx_base, shift, new_index;
368
+ en = 1'b1;
369
+ for (int unsigned lvl = 0; lvl < $clog2(TLB_ENTRIES); lvl++) begin
370
+ idx_base = $unsigned((2 ** lvl) - 1);
371
+ // lvl0 <=> MSB, lvl1 <=> MSB-1, ...
372
+ shift = $clog2(TLB_ENTRIES) - lvl;
373
+
374
+ // en &= plru_tree_q[idx_base + (i>>shift)] == ((i >> (shift-1)) & 1'b1);
375
+ new_index = (i >> (shift - 1)) & 32'b1;
376
+ if (new_index[0]) begin
377
+ en &= plru_tree_q[idx_base+(i>>shift)];
378
+ end else begin
379
+ en &= ~plru_tree_q[idx_base+(i>>shift)];
380
+ end
381
+ end
382
+ replace_en[i] = en;
383
+ end
384
+ end
385
+
386
+ // sequential process
387
+ always_ff @(posedge clk_i or negedge rst_ni) begin
388
+ if (~rst_ni) begin
389
+ tags_q <= '{default: 0};
390
+ content_q <= '{default: 0};
391
+ plru_tree_q <= '{default: 0};
392
+ end else begin
393
+ tags_q <= tags_n;
394
+ content_q <= content_n;
395
+ plru_tree_q <= plru_tree_n;
396
+ end
397
+ end
398
+ //--------------
399
+ // Sanity checks
400
+ //--------------
401
+
402
+ //pragma translate_off
403
+
404
+ initial begin : p_assertions
405
+ assert ((TLB_ENTRIES % 2 == 0) && (TLB_ENTRIES > 1))
406
+ else begin
407
+ $error("TLB size must be a multiple of 2 and greater than 1");
408
+ $stop();
409
+ end
410
+ assert (CVA6Cfg.ASID_WIDTH >= 1)
411
+ else begin
412
+ $error("ASID width must be at least 1");
413
+ $stop();
414
+ end
415
+ end
416
+
417
+ // Just for checking
418
+ function int countSetBits(logic [TLB_ENTRIES-1:0] vector);
419
+ automatic int count = 0;
420
+ foreach (vector[idx]) begin
421
+ count += vector[idx];
422
+ end
423
+ return count;
424
+ endfunction
425
+
426
+ assert property (@(posedge clk_i) (countSetBits(lu_hit) <= 1))
427
+ else begin
428
+ $error("More then one hit in TLB!");
429
+ $stop();
430
+ end
431
+ assert property (@(posedge clk_i) (countSetBits(replace_en) <= 1))
432
+ else begin
433
+ $error("More then one TLB entry selected for next replace!");
434
+ $stop();
435
+ end
436
+
437
+ //pragma translate_on
438
+
439
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cva6_rvfi_probes.sv ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 Thales DIS France SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Yannick Casamatta - Thales
9
+ // Date: 09/01/2024
10
+
11
+
12
+ module cva6_rvfi_probes
13
+ import ariane_pkg::*;
14
+ #(
15
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
16
+ parameter type exception_t = logic,
17
+ parameter type scoreboard_entry_t = logic,
18
+ parameter type lsu_ctrl_t = logic,
19
+ parameter type rvfi_probes_instr_t = logic,
20
+ parameter type rvfi_probes_csr_t = logic,
21
+ parameter type rvfi_probes_t = logic
22
+
23
+ ) (
24
+
25
+ input logic flush_i,
26
+ input logic [CVA6Cfg.NrIssuePorts-1:0] issue_instr_ack_i,
27
+ input logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_valid_i,
28
+ input logic [CVA6Cfg.NrIssuePorts-1:0][31:0] instruction_i,
29
+ input logic [CVA6Cfg.NrIssuePorts-1:0] is_compressed_i,
30
+
31
+ input logic [CVA6Cfg.NrIssuePorts-1 : 0][CVA6Cfg.TRANS_ID_BITS-1:0] issue_pointer_i,
32
+ input logic [ CVA6Cfg.NrCommitPorts-1:0][CVA6Cfg.TRANS_ID_BITS-1:0] commit_pointer_i,
33
+
34
+ input logic flush_unissued_instr_i,
35
+ input logic [CVA6Cfg.NrIssuePorts-1:0] decoded_instr_valid_i,
36
+ input logic [CVA6Cfg.NrIssuePorts-1:0] decoded_instr_ack_i,
37
+
38
+ input logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.XLEN-1:0] rs1_i,
39
+ input logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.XLEN-1:0] rs2_i,
40
+
41
+ input scoreboard_entry_t [CVA6Cfg.NrCommitPorts-1:0] commit_instr_i,
42
+ input logic [CVA6Cfg.NrCommitPorts-1:0] commit_drop_i,
43
+ input exception_t ex_commit_i,
44
+ input riscv::priv_lvl_t priv_lvl_i,
45
+
46
+ input lsu_ctrl_t lsu_ctrl_i,
47
+ input logic [ CVA6Cfg.NrWbPorts-1:0][CVA6Cfg.XLEN-1:0] wbdata_i,
48
+ input logic [CVA6Cfg.NrCommitPorts-1:0] commit_ack_i,
49
+ input logic [ CVA6Cfg.PLEN-1:0] mem_paddr_i,
50
+ input logic debug_mode_i,
51
+ input logic [CVA6Cfg.NrCommitPorts-1:0][CVA6Cfg.XLEN-1:0] wdata_i,
52
+
53
+ input rvfi_probes_csr_t csr_i,
54
+ input logic [1:0] irq_i,
55
+
56
+ output rvfi_probes_t rvfi_probes_o
57
+ );
58
+
59
+
60
+ rvfi_probes_csr_t csr;
61
+ rvfi_probes_instr_t instr;
62
+
63
+ always_comb begin
64
+ csr = '0;
65
+ instr = '0;
66
+
67
+ instr.flush = flush_i;
68
+ instr.issue_instr_ack = issue_instr_ack_i;
69
+ instr.fetch_entry_valid = fetch_entry_valid_i;
70
+ instr.instruction = instruction_i;
71
+ instr.is_compressed = is_compressed_i;
72
+
73
+ instr.issue_pointer = issue_pointer_i;
74
+
75
+ instr.flush_unissued_instr = flush_unissued_instr_i;
76
+ instr.decoded_instr_valid = decoded_instr_valid_i;
77
+ instr.decoded_instr_ack = decoded_instr_ack_i;
78
+
79
+ instr.rs1 = rs1_i;
80
+ instr.rs2 = rs2_i;
81
+
82
+ instr.ex_commit_cause = ex_commit_i.cause;
83
+ instr.ex_commit_valid = ex_commit_i.valid;
84
+
85
+ instr.priv_lvl = priv_lvl_i;
86
+
87
+ instr.lsu_ctrl_vaddr = lsu_ctrl_i.vaddr;
88
+ instr.lsu_ctrl_fu = lsu_ctrl_i.fu;
89
+ instr.lsu_ctrl_be = lsu_ctrl_i.be;
90
+ instr.lsu_ctrl_trans_id = lsu_ctrl_i.trans_id;
91
+
92
+ instr.wbdata = wbdata_i;
93
+ instr.mem_paddr = mem_paddr_i;
94
+ instr.debug_mode = debug_mode_i;
95
+
96
+ instr.commit_pointer = commit_pointer_i;
97
+
98
+ for (int i = 0; i < CVA6Cfg.NrCommitPorts; i++) begin
99
+ instr.commit_instr_pc[i] = commit_instr_i[i].pc;
100
+ instr.commit_instr_op[i] = commit_instr_i[i].op;
101
+ instr.commit_instr_rs1[i] = commit_instr_i[i].rs1;
102
+ instr.commit_instr_rs2[i] = commit_instr_i[i].rs2;
103
+ instr.commit_instr_rd[i] = commit_instr_i[i].rd;
104
+ instr.commit_instr_result[i] = commit_instr_i[i].result;
105
+ instr.commit_instr_valid[i] = commit_instr_i[i].valid;
106
+ end
107
+
108
+ instr.commit_drop = commit_drop_i;
109
+ instr.commit_ack = commit_ack_i;
110
+ instr.wdata = wdata_i;
111
+
112
+ csr = csr_i;
113
+ csr.mip_q = csr_i.mip_q | ({{CVA6Cfg.XLEN - 1{1'b0}}, CVA6Cfg.RVS && irq_i[1]} << riscv::IRQ_S_EXT);
114
+
115
+ end
116
+
117
+
118
+ always_comb begin
119
+ rvfi_probes_o = '0;
120
+
121
+ if ($bits(rvfi_probes_o.instr) == $bits(instr)) begin
122
+ rvfi_probes_o.instr = instr;
123
+ end
124
+
125
+ if ($bits(rvfi_probes_o.csr) == $bits(csr)) begin
126
+ rvfi_probes_o.csr = csr;
127
+ end
128
+
129
+ end
130
+
131
+
132
+ endmodule
133
+
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/common_cells/include/common_cells/registers.svh ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+
12
+ // Common register defines for RTL designs
13
+ `ifndef COMMON_CELLS_REGISTERS_SVH_
14
+ `define COMMON_CELLS_REGISTERS_SVH_
15
+
16
+ // Abridged Summary of available FF macros:
17
+ // `FF: asynchronous active-low reset (implicit clock and reset)
18
+ // `FFAR: asynchronous active-high reset
19
+ // `FFARN: asynchronous active-low reset
20
+ // `FFSR: synchronous active-high reset
21
+ // `FFSRN: synchronous active-low reset
22
+ // `FFNR: without reset
23
+ // `FFL: load-enable and asynchronous active-low reset (implicit clock and reset)
24
+ // `FFLAR: load-enable and asynchronous active-high reset
25
+ // `FFLARN: load-enable and asynchronous active-low reset
26
+ // `FFLARNC: load-enable and asynchronous active-low reset and synchronous active-high clear
27
+ // `FFLSR: load-enable and synchronous active-high reset
28
+ // `FFLSRN: load-enable and synchronous active-low reset
29
+ // `FFLNR: load-enable without reset
30
+
31
+
32
+ // Flip-Flop with asynchronous active-low reset (implicit clock and reset)
33
+ // __q: Q output of FF
34
+ // __d: D input of FF
35
+ // __reset_value: value assigned upon reset
36
+ // Implicit:
37
+ // clk_i: clock input
38
+ // rst_ni: reset input (asynchronous, active low)
39
+ `define FF(__q, __d, __reset_value) \
40
+ always_ff @(posedge clk_i or negedge rst_ni) begin \
41
+ if (!rst_ni) begin \
42
+ __q <= (__reset_value); \
43
+ end else begin \
44
+ __q <= (__d); \
45
+ end \
46
+ end
47
+
48
+ // Flip-Flop with asynchronous active-high reset
49
+ // __q: Q output of FF
50
+ // __d: D input of FF
51
+ // __reset_value: value assigned upon reset
52
+ // __clk: clock input
53
+ // __arst: asynchronous reset
54
+ `define FFAR(__q, __d, __reset_value, __clk, __arst) \
55
+ always_ff @(posedge (__clk) or posedge (__arst)) begin \
56
+ if (__arst) begin \
57
+ __q <= (__reset_value); \
58
+ end else begin \
59
+ __q <= (__d); \
60
+ end \
61
+ end
62
+
63
+ // Flip-Flop with asynchronous active-low reset
64
+ // __q: Q output of FF
65
+ // __d: D input of FF
66
+ // __reset_value: value assigned upon reset
67
+ // __clk: clock input
68
+ // __arst_n: asynchronous reset
69
+ `define FFARN(__q, __d, __reset_value, __clk, __arst_n) \
70
+ always_ff @(posedge (__clk) or negedge (__arst_n)) begin \
71
+ if (!__arst_n) begin \
72
+ __q <= (__reset_value); \
73
+ end else begin \
74
+ __q <= (__d); \
75
+ end \
76
+ end
77
+
78
+ // Flip-Flop with synchronous active-high reset
79
+ // __q: Q output of FF
80
+ // __d: D input of FF
81
+ // __reset_value: value assigned upon reset
82
+ // __clk: clock input
83
+ // __reset_clk: reset input
84
+ `define FFSR(__q, __d, __reset_value, __clk, __reset_clk) \
85
+ `ifndef VERILATOR \
86
+ /``* synopsys sync_set_reset `"__reset_clk`" *``/ \
87
+ `endif \
88
+ always_ff @(posedge (__clk)) begin \
89
+ __q <= (__reset_clk) ? (__reset_value) : (__d); \
90
+ end
91
+
92
+ // Flip-Flop with synchronous active-low reset
93
+ // __q: Q output of FF
94
+ // __d: D input of FF
95
+ // __reset_value: value assigned upon reset
96
+ // __clk: clock input
97
+ // __reset_n_clk: reset input
98
+ `define FFSRN(__q, __d, __reset_value, __clk, __reset_n_clk) \
99
+ `ifndef VERILATOR \
100
+ /``* synopsys sync_set_reset `"__reset_n_clk`" *``/ \
101
+ `endif \
102
+ always_ff @(posedge (__clk)) begin \
103
+ __q <= (!__reset_n_clk) ? (__reset_value) : (__d); \
104
+ end
105
+
106
+ // Always-enable Flip-Flop without reset
107
+ // __q: Q output of FF
108
+ // __d: D input of FF
109
+ // __clk: clock input
110
+ `define FFNR(__q, __d, __clk) \
111
+ always_ff @(posedge (__clk)) begin \
112
+ __q <= (__d); \
113
+ end
114
+
115
+ // Flip-Flop with load-enable and asynchronous active-low reset (implicit clock and reset)
116
+ // __q: Q output of FF
117
+ // __d: D input of FF
118
+ // __load: load d value into FF
119
+ // __reset_value: value assigned upon reset
120
+ // Implicit:
121
+ // clk_i: clock input
122
+ // rst_ni: reset input (asynchronous, active low)
123
+ `define FFL(__q, __d, __load, __reset_value) \
124
+ always_ff @(posedge clk_i or negedge rst_ni) begin \
125
+ if (!rst_ni) begin \
126
+ __q <= (__reset_value); \
127
+ end else begin \
128
+ __q <= (__load) ? (__d) : (__q); \
129
+ end \
130
+ end
131
+
132
+ // Flip-Flop with load-enable and asynchronous active-high reset
133
+ // __q: Q output of FF
134
+ // __d: D input of FF
135
+ // __load: load d value into FF
136
+ // __reset_value: value assigned upon reset
137
+ // __clk: clock input
138
+ // __arst: asynchronous reset
139
+ `define FFLAR(__q, __d, __load, __reset_value, __clk, __arst) \
140
+ always_ff @(posedge (__clk) or posedge (__arst)) begin \
141
+ if (__arst) begin \
142
+ __q <= (__reset_value); \
143
+ end else begin \
144
+ __q <= (__load) ? (__d) : (__q); \
145
+ end \
146
+ end
147
+
148
+ // Flip-Flop with load-enable and asynchronous active-low reset
149
+ // __q: Q output of FF
150
+ // __d: D input of FF
151
+ // __load: load d value into FF
152
+ // __reset_value: value assigned upon reset
153
+ // __clk: clock input
154
+ // __arst_n: asynchronous reset
155
+ `define FFLARN(__q, __d, __load, __reset_value, __clk, __arst_n) \
156
+ always_ff @(posedge (__clk) or negedge (__arst_n)) begin \
157
+ if (!__arst_n) begin \
158
+ __q <= (__reset_value); \
159
+ end else begin \
160
+ __q <= (__load) ? (__d) : (__q); \
161
+ end \
162
+ end
163
+
164
+ // Flip-Flop with load-enable and synchronous active-high reset
165
+ // __q: Q output of FF
166
+ // __d: D input of FF
167
+ // __load: load d value into FF
168
+ // __reset_value: value assigned upon reset
169
+ // __clk: clock input
170
+ // __reset_clk: reset input
171
+ `define FFLSR(__q, __d, __load, __reset_value, __clk, __reset_clk) \
172
+ `ifndef VERILATOR \
173
+ /``* synopsys sync_set_reset `"__reset_clk`" *``/ \
174
+ `endif \
175
+ always_ff @(posedge (__clk)) begin \
176
+ __q <= (__reset_clk) ? (__reset_value) : ((__load) ? (__d) : (__q)); \
177
+ end
178
+
179
+ // Flip-Flop with load-enable and synchronous active-low reset
180
+ // __q: Q output of FF
181
+ // __d: D input of FF
182
+ // __load: load d value into FF
183
+ // __reset_value: value assigned upon reset
184
+ // __clk: clock input
185
+ // __reset_n_clk: reset input
186
+ `define FFLSRN(__q, __d, __load, __reset_value, __clk, __reset_n_clk) \
187
+ `ifndef VERILATOR \
188
+ /``* synopsys sync_set_reset `"__reset_n_clk`" *``/ \
189
+ `endif \
190
+ always_ff @(posedge (__clk)) begin \
191
+ __q <= (!__reset_n_clk) ? (__reset_value) : ((__load) ? (__d) : (__q)); \
192
+ end
193
+
194
+ // Flip-Flop with load-enable and asynchronous active-low reset and synchronous clear
195
+ // __q: Q output of FF
196
+ // __d: D input of FF
197
+ // __load: load d value into FF
198
+ // __clear: assign reset value into FF
199
+ // __reset_value: value assigned upon reset
200
+ // __clk: clock input
201
+ // __arst_n: asynchronous reset
202
+ `define FFLARNC(__q, __d, __load, __clear, __reset_value, __clk, __arst_n) \
203
+ `ifndef VERILATOR \
204
+ /``* synopsys sync_set_reset `"__clear`" *``/ \
205
+ `endif \
206
+ always_ff @(posedge (__clk) or negedge (__arst_n)) begin \
207
+ if (!__arst_n) begin \
208
+ __q <= (__reset_value); \
209
+ end else begin \
210
+ __q <= (__clear) ? (__reset_value) : (__load) ? (__d) : (__q); \
211
+ end \
212
+ end
213
+
214
+ // Load-enable Flip-Flop without reset
215
+ // __q: Q output of FF
216
+ // __d: D input of FF
217
+ // __load: load d value into FF
218
+ // __clk: clock input
219
+ `define FFLNR(__q, __d, __load, __clk) \
220
+ always_ff @(posedge (__clk)) begin \
221
+ __q <= (__load) ? (__d) : (__q); \
222
+ end
223
+
224
+ `endif
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_cast_multi.sv ADDED
@@ -0,0 +1,794 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ `include "common_cells/registers.svh"
17
+
18
+ module fpnew_cast_multi #(
19
+ parameter fpnew_pkg::fmt_logic_t FpFmtConfig = '1,
20
+ parameter fpnew_pkg::ifmt_logic_t IntFmtConfig = '1,
21
+ // FPU configuration
22
+ parameter int unsigned NumPipeRegs = 0,
23
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::BEFORE,
24
+ parameter type TagType = logic,
25
+ parameter type AuxType = logic,
26
+ // Do not change
27
+ localparam int unsigned WIDTH = fpnew_pkg::maximum(fpnew_pkg::max_fp_width(FpFmtConfig),
28
+ fpnew_pkg::max_int_width(IntFmtConfig)),
29
+ localparam int unsigned NUM_FORMATS = fpnew_pkg::NUM_FP_FORMATS
30
+ ) (
31
+ input logic clk_i,
32
+ input logic rst_ni,
33
+ // Input signals
34
+ input logic [WIDTH-1:0] operands_i, // 1 operand
35
+ input logic [NUM_FORMATS-1:0] is_boxed_i, // 1 operand
36
+ input fpnew_pkg::roundmode_e rnd_mode_i,
37
+ input fpnew_pkg::operation_e op_i,
38
+ input logic op_mod_i,
39
+ input fpnew_pkg::fp_format_e src_fmt_i,
40
+ input fpnew_pkg::fp_format_e dst_fmt_i,
41
+ input fpnew_pkg::int_format_e int_fmt_i,
42
+ input TagType tag_i,
43
+ input logic mask_i,
44
+ input AuxType aux_i,
45
+ // Input Handshake
46
+ input logic in_valid_i,
47
+ output logic in_ready_o,
48
+ input logic flush_i,
49
+ // Output signals
50
+ output logic [WIDTH-1:0] result_o,
51
+ output fpnew_pkg::status_t status_o,
52
+ output logic extension_bit_o,
53
+ output TagType tag_o,
54
+ output logic mask_o,
55
+ output AuxType aux_o,
56
+ // Output handshake
57
+ output logic out_valid_o,
58
+ input logic out_ready_i,
59
+ // Indication of valid data in flight
60
+ output logic busy_o
61
+ );
62
+
63
+ // ----------
64
+ // Constants
65
+ // ----------
66
+ localparam int unsigned NUM_INT_FORMATS = fpnew_pkg::NUM_INT_FORMATS;
67
+ localparam int unsigned MAX_INT_WIDTH = fpnew_pkg::max_int_width(IntFmtConfig);
68
+
69
+ localparam fpnew_pkg::fp_encoding_t SUPER_FORMAT = fpnew_pkg::super_format(FpFmtConfig);
70
+
71
+ localparam int unsigned SUPER_EXP_BITS = SUPER_FORMAT.exp_bits;
72
+ localparam int unsigned SUPER_MAN_BITS = SUPER_FORMAT.man_bits;
73
+ localparam int unsigned SUPER_BIAS = 2**(SUPER_EXP_BITS - 1) - 1;
74
+
75
+ // The internal mantissa includes normal bit or an entire integer
76
+ localparam int unsigned INT_MAN_WIDTH = fpnew_pkg::maximum(SUPER_MAN_BITS + 1, MAX_INT_WIDTH);
77
+ // If needed, there will be a LZC for renormalization
78
+ localparam int unsigned LZC_RESULT_WIDTH = $clog2(INT_MAN_WIDTH);
79
+ // The internal exponent must be able to represent the smallest denormal input value as signed
80
+ // or the number of bits in an integer
81
+ localparam int unsigned INT_EXP_WIDTH = fpnew_pkg::maximum($clog2(MAX_INT_WIDTH),
82
+ fpnew_pkg::maximum(SUPER_EXP_BITS, $clog2(SUPER_BIAS + SUPER_MAN_BITS))) + 1;
83
+ // Pipelines
84
+ localparam NUM_INP_REGS = PipeConfig == fpnew_pkg::BEFORE
85
+ ? NumPipeRegs
86
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
87
+ ? ((NumPipeRegs + 1) / 3) // Second to get distributed regs
88
+ : 0); // no regs here otherwise
89
+ localparam NUM_MID_REGS = PipeConfig == fpnew_pkg::INSIDE
90
+ ? NumPipeRegs
91
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
92
+ ? ((NumPipeRegs + 2) / 3) // First to get distributed regs
93
+ : 0); // no regs here otherwise
94
+ localparam NUM_OUT_REGS = PipeConfig == fpnew_pkg::AFTER
95
+ ? NumPipeRegs
96
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
97
+ ? (NumPipeRegs / 3) // Last to get distributed regs
98
+ : 0); // no regs here otherwise
99
+
100
+ // ---------------
101
+ // Input pipeline
102
+ // ---------------
103
+ // Selected pipeline output signals as non-arrays
104
+ logic [WIDTH-1:0] operands_q;
105
+ logic [NUM_FORMATS-1:0] is_boxed_q;
106
+ logic op_mod_q;
107
+ fpnew_pkg::fp_format_e src_fmt_q;
108
+ fpnew_pkg::fp_format_e dst_fmt_q;
109
+ fpnew_pkg::int_format_e int_fmt_q;
110
+
111
+ // Input pipeline signals, index i holds signal after i register stages
112
+ logic [0:NUM_INP_REGS][WIDTH-1:0] inp_pipe_operands_q;
113
+ logic [0:NUM_INP_REGS][NUM_FORMATS-1:0] inp_pipe_is_boxed_q;
114
+ fpnew_pkg::roundmode_e [0:NUM_INP_REGS] inp_pipe_rnd_mode_q;
115
+ fpnew_pkg::operation_e [0:NUM_INP_REGS] inp_pipe_op_q;
116
+ logic [0:NUM_INP_REGS] inp_pipe_op_mod_q;
117
+ fpnew_pkg::fp_format_e [0:NUM_INP_REGS] inp_pipe_src_fmt_q;
118
+ fpnew_pkg::fp_format_e [0:NUM_INP_REGS] inp_pipe_dst_fmt_q;
119
+ fpnew_pkg::int_format_e [0:NUM_INP_REGS] inp_pipe_int_fmt_q;
120
+ TagType [0:NUM_INP_REGS] inp_pipe_tag_q;
121
+ logic [0:NUM_INP_REGS] inp_pipe_mask_q;
122
+ AuxType [0:NUM_INP_REGS] inp_pipe_aux_q;
123
+ logic [0:NUM_INP_REGS] inp_pipe_valid_q;
124
+ // Ready signal is combinatorial for all stages
125
+ logic [0:NUM_INP_REGS] inp_pipe_ready;
126
+
127
+ // Input stage: First element of pipeline is taken from inputs
128
+ assign inp_pipe_operands_q[0] = operands_i;
129
+ assign inp_pipe_is_boxed_q[0] = is_boxed_i;
130
+ assign inp_pipe_rnd_mode_q[0] = rnd_mode_i;
131
+ assign inp_pipe_op_q[0] = op_i;
132
+ assign inp_pipe_op_mod_q[0] = op_mod_i;
133
+ assign inp_pipe_src_fmt_q[0] = src_fmt_i;
134
+ assign inp_pipe_dst_fmt_q[0] = dst_fmt_i;
135
+ assign inp_pipe_int_fmt_q[0] = int_fmt_i;
136
+ assign inp_pipe_tag_q[0] = tag_i;
137
+ assign inp_pipe_mask_q[0] = mask_i;
138
+ assign inp_pipe_aux_q[0] = aux_i;
139
+ assign inp_pipe_valid_q[0] = in_valid_i;
140
+ // Input stage: Propagate pipeline ready signal to updtream circuitry
141
+ assign in_ready_o = inp_pipe_ready[0];
142
+ // Generate the register stages
143
+ for (genvar i = 0; i < NUM_INP_REGS; i++) begin : gen_input_pipeline
144
+ // Internal register enable for this stage
145
+ logic reg_ena;
146
+ // Determine the ready signal of the current stage - advance the pipeline:
147
+ // 1. if the next stage is ready for our data
148
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
149
+ assign inp_pipe_ready[i] = inp_pipe_ready[i+1] | ~inp_pipe_valid_q[i+1];
150
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
151
+ `FFLARNC(inp_pipe_valid_q[i+1], inp_pipe_valid_q[i], inp_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
152
+ // Enable register if pipleine ready and a valid data item is present
153
+ assign reg_ena = inp_pipe_ready[i] & inp_pipe_valid_q[i];
154
+ // Generate the pipeline registers within the stages, use enable-registers
155
+ `FFL(inp_pipe_operands_q[i+1], inp_pipe_operands_q[i], reg_ena, '0)
156
+ `FFL(inp_pipe_is_boxed_q[i+1], inp_pipe_is_boxed_q[i], reg_ena, '0)
157
+ `FFL(inp_pipe_rnd_mode_q[i+1], inp_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
158
+ `FFL(inp_pipe_op_q[i+1], inp_pipe_op_q[i], reg_ena, fpnew_pkg::FMADD)
159
+ `FFL(inp_pipe_op_mod_q[i+1], inp_pipe_op_mod_q[i], reg_ena, '0)
160
+ `FFL(inp_pipe_src_fmt_q[i+1], inp_pipe_src_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
161
+ `FFL(inp_pipe_dst_fmt_q[i+1], inp_pipe_dst_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
162
+ `FFL(inp_pipe_int_fmt_q[i+1], inp_pipe_int_fmt_q[i], reg_ena, fpnew_pkg::int_format_e'(0))
163
+ `FFL(inp_pipe_tag_q[i+1], inp_pipe_tag_q[i], reg_ena, TagType'('0))
164
+ `FFL(inp_pipe_mask_q[i+1], inp_pipe_mask_q[i], reg_ena, '0)
165
+ `FFL(inp_pipe_aux_q[i+1], inp_pipe_aux_q[i], reg_ena, AuxType'('0))
166
+ end
167
+ // Output stage: assign selected pipe outputs to signals for later use
168
+ assign operands_q = inp_pipe_operands_q[NUM_INP_REGS];
169
+ assign is_boxed_q = inp_pipe_is_boxed_q[NUM_INP_REGS];
170
+ assign op_mod_q = inp_pipe_op_mod_q[NUM_INP_REGS];
171
+ assign src_fmt_q = inp_pipe_src_fmt_q[NUM_INP_REGS];
172
+ assign dst_fmt_q = inp_pipe_dst_fmt_q[NUM_INP_REGS];
173
+ assign int_fmt_q = inp_pipe_int_fmt_q[NUM_INP_REGS];
174
+
175
+ // -----------------
176
+ // Input processing
177
+ // -----------------
178
+ logic src_is_int, dst_is_int; // if 0, it's a float
179
+
180
+ assign src_is_int = (inp_pipe_op_q[NUM_INP_REGS] == fpnew_pkg::I2F);
181
+ assign dst_is_int = (inp_pipe_op_q[NUM_INP_REGS] == fpnew_pkg::F2I);
182
+
183
+ logic [INT_MAN_WIDTH-1:0] encoded_mant; // input mantissa with implicit bit
184
+
185
+ logic [NUM_FORMATS-1:0] fmt_sign;
186
+ logic signed [NUM_FORMATS-1:0][INT_EXP_WIDTH-1:0] fmt_exponent;
187
+ logic [NUM_FORMATS-1:0][INT_MAN_WIDTH-1:0] fmt_mantissa;
188
+ logic signed [NUM_FORMATS-1:0][INT_EXP_WIDTH-1:0] fmt_shift_compensation; // for LZC
189
+
190
+ fpnew_pkg::fp_info_t [NUM_FORMATS-1:0] info;
191
+
192
+ logic [NUM_INT_FORMATS-1:0][INT_MAN_WIDTH-1:0] ifmt_input_val;
193
+ logic int_sign;
194
+ logic [INT_MAN_WIDTH-1:0] int_value, int_mantissa;
195
+
196
+ // FP Input initialization
197
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : fmt_init_inputs
198
+ // Set up some constants
199
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
200
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
201
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
202
+
203
+ if (FpFmtConfig[fmt]) begin : active_format
204
+ // Classify input
205
+ fpnew_classifier #(
206
+ .FpFormat ( fpnew_pkg::fp_format_e'(fmt) ),
207
+ .NumOperands ( 1 )
208
+ ) i_fpnew_classifier (
209
+ .operands_i ( operands_q[FP_WIDTH-1:0] ),
210
+ .is_boxed_i ( is_boxed_q[fmt] ),
211
+ .info_o ( info[fmt] )
212
+ );
213
+
214
+ assign fmt_sign[fmt] = operands_q[FP_WIDTH-1];
215
+ assign fmt_exponent[fmt] = signed'({1'b0, operands_q[MAN_BITS+:EXP_BITS]});
216
+ assign fmt_mantissa[fmt] = {info[fmt].is_normal, operands_q[MAN_BITS-1:0]}; // zero pad
217
+ // Compensation for the difference in mantissa widths used for leading-zero count
218
+ assign fmt_shift_compensation[fmt] = signed'(INT_MAN_WIDTH - 1 - MAN_BITS);
219
+ end else begin : inactive_format
220
+ assign info[fmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
221
+ assign fmt_sign[fmt] = fpnew_pkg::DONT_CARE; // format disabled
222
+ assign fmt_exponent[fmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
223
+ assign fmt_mantissa[fmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
224
+ assign fmt_shift_compensation[fmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
225
+ end
226
+ end
227
+
228
+ // Sign-extend INT input
229
+ for (genvar ifmt = 0; ifmt < int'(NUM_INT_FORMATS); ifmt++) begin : gen_sign_extend_int
230
+ // Set up some constants
231
+ localparam int unsigned INT_WIDTH = fpnew_pkg::int_width(fpnew_pkg::int_format_e'(ifmt));
232
+
233
+ if (IntFmtConfig[ifmt]) begin : active_format // only active formats
234
+ always_comb begin : sign_ext_input
235
+ // sign-extend value only if it's signed
236
+ ifmt_input_val[ifmt] = '{default: operands_q[INT_WIDTH-1] & ~op_mod_q};
237
+ ifmt_input_val[ifmt][INT_WIDTH-1:0] = operands_q[INT_WIDTH-1:0];
238
+ end
239
+ end else begin : inactive_format
240
+ assign ifmt_input_val[ifmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
241
+ end
242
+ end
243
+
244
+ // Construct input mantissa from integer
245
+ assign int_value = ifmt_input_val[int_fmt_q];
246
+ assign int_sign = int_value[INT_MAN_WIDTH-1] & ~op_mod_q; // only signed ints are negative
247
+ assign int_mantissa = int_sign ? unsigned'(-int_value) : int_value; // get magnitude of negative
248
+
249
+ // select mantissa with source format
250
+ assign encoded_mant = src_is_int ? int_mantissa : fmt_mantissa[src_fmt_q];
251
+
252
+ // --------------
253
+ // Normalization
254
+ // --------------
255
+ logic signed [INT_EXP_WIDTH-1:0] src_bias; // src format bias
256
+ logic signed [INT_EXP_WIDTH-1:0] src_exp; // src format exponent (biased)
257
+ logic signed [INT_EXP_WIDTH-1:0] src_subnormal; // src is subnormal
258
+ logic signed [INT_EXP_WIDTH-1:0] src_offset; // src offset within mantissa
259
+
260
+ assign src_bias = signed'(fpnew_pkg::bias(src_fmt_q));
261
+ assign src_exp = fmt_exponent[src_fmt_q];
262
+ assign src_subnormal = signed'({1'b0, info[src_fmt_q].is_subnormal});
263
+ assign src_offset = fmt_shift_compensation[src_fmt_q];
264
+
265
+ logic input_sign; // input sign
266
+ logic signed [INT_EXP_WIDTH-1:0] input_exp; // unbiased true exponent
267
+ logic [INT_MAN_WIDTH-1:0] input_mant; // normalized input mantissa
268
+ logic mant_is_zero; // for integer zeroes
269
+
270
+ logic signed [INT_EXP_WIDTH-1:0] fp_input_exp;
271
+ logic signed [INT_EXP_WIDTH-1:0] int_input_exp;
272
+
273
+ // Input mantissa needs to be normalized
274
+ logic [LZC_RESULT_WIDTH-1:0] renorm_shamt; // renormalization shift amount
275
+ logic [LZC_RESULT_WIDTH:0] renorm_shamt_sgn; // signed form for calculations
276
+
277
+ // Leading-zero counter is needed for renormalization
278
+ lzc #(
279
+ .WIDTH ( INT_MAN_WIDTH ),
280
+ .MODE ( 1 ) // MODE = 1 counts leading zeroes
281
+ ) i_lzc (
282
+ .in_i ( encoded_mant ),
283
+ .cnt_o ( renorm_shamt ),
284
+ .empty_o ( mant_is_zero )
285
+ );
286
+ assign renorm_shamt_sgn = signed'({1'b0, renorm_shamt});
287
+
288
+ // Get the sign from the proper source
289
+ assign input_sign = src_is_int ? int_sign : fmt_sign[src_fmt_q];
290
+ // Realign input mantissa, append zeroes if destination is wider
291
+ assign input_mant = encoded_mant << renorm_shamt;
292
+ // Unbias exponent and compensate for shift
293
+ assign fp_input_exp = signed'(src_exp + src_subnormal - src_bias -
294
+ renorm_shamt_sgn + src_offset); // compensate for shift
295
+ assign int_input_exp = signed'(INT_MAN_WIDTH - 1 - renorm_shamt_sgn);
296
+
297
+ assign input_exp = src_is_int ? int_input_exp : fp_input_exp;
298
+
299
+ logic signed [INT_EXP_WIDTH-1:0] destination_exp; // re-biased exponent for destination
300
+
301
+ // Rebias the exponent
302
+ assign destination_exp = input_exp + signed'(fpnew_pkg::bias(dst_fmt_q));
303
+
304
+ // ---------------
305
+ // Internal pipeline
306
+ // ---------------
307
+ // Pipeline output signals as non-arrays
308
+ logic input_sign_q;
309
+ logic signed [INT_EXP_WIDTH-1:0] input_exp_q;
310
+ logic [INT_MAN_WIDTH-1:0] input_mant_q;
311
+ logic signed [INT_EXP_WIDTH-1:0] destination_exp_q;
312
+ logic src_is_int_q;
313
+ logic dst_is_int_q;
314
+ fpnew_pkg::fp_info_t info_q;
315
+ logic mant_is_zero_q;
316
+ logic op_mod_q2;
317
+ fpnew_pkg::roundmode_e rnd_mode_q;
318
+ fpnew_pkg::fp_format_e src_fmt_q2;
319
+ fpnew_pkg::fp_format_e dst_fmt_q2;
320
+ fpnew_pkg::int_format_e int_fmt_q2;
321
+ // Internal pipeline signals, index i holds signal after i register stages
322
+
323
+
324
+ logic [0:NUM_MID_REGS] mid_pipe_input_sign_q;
325
+ logic signed [0:NUM_MID_REGS][INT_EXP_WIDTH-1:0] mid_pipe_input_exp_q;
326
+ logic [0:NUM_MID_REGS][INT_MAN_WIDTH-1:0] mid_pipe_input_mant_q;
327
+ logic signed [0:NUM_MID_REGS][INT_EXP_WIDTH-1:0] mid_pipe_dest_exp_q;
328
+ logic [0:NUM_MID_REGS] mid_pipe_src_is_int_q;
329
+ logic [0:NUM_MID_REGS] mid_pipe_dst_is_int_q;
330
+ fpnew_pkg::fp_info_t [0:NUM_MID_REGS] mid_pipe_info_q;
331
+ logic [0:NUM_MID_REGS] mid_pipe_mant_zero_q;
332
+ logic [0:NUM_MID_REGS] mid_pipe_op_mod_q;
333
+ fpnew_pkg::roundmode_e [0:NUM_MID_REGS] mid_pipe_rnd_mode_q;
334
+ fpnew_pkg::fp_format_e [0:NUM_MID_REGS] mid_pipe_src_fmt_q;
335
+ fpnew_pkg::fp_format_e [0:NUM_MID_REGS] mid_pipe_dst_fmt_q;
336
+ fpnew_pkg::int_format_e [0:NUM_MID_REGS] mid_pipe_int_fmt_q;
337
+ TagType [0:NUM_MID_REGS] mid_pipe_tag_q;
338
+ logic [0:NUM_MID_REGS] mid_pipe_mask_q;
339
+ AuxType [0:NUM_MID_REGS] mid_pipe_aux_q;
340
+ logic [0:NUM_MID_REGS] mid_pipe_valid_q;
341
+ // Ready signal is combinatorial for all stages
342
+ logic [0:NUM_MID_REGS] mid_pipe_ready;
343
+
344
+ // Input stage: First element of pipeline is taken from upstream logic
345
+ assign mid_pipe_input_sign_q[0] = input_sign;
346
+ assign mid_pipe_input_exp_q[0] = input_exp;
347
+ assign mid_pipe_input_mant_q[0] = input_mant;
348
+ assign mid_pipe_dest_exp_q[0] = destination_exp;
349
+ assign mid_pipe_src_is_int_q[0] = src_is_int;
350
+ assign mid_pipe_dst_is_int_q[0] = dst_is_int;
351
+ assign mid_pipe_info_q[0] = info[src_fmt_q];
352
+ assign mid_pipe_mant_zero_q[0] = mant_is_zero;
353
+ assign mid_pipe_op_mod_q[0] = op_mod_q;
354
+ assign mid_pipe_rnd_mode_q[0] = inp_pipe_rnd_mode_q[NUM_INP_REGS];
355
+ assign mid_pipe_src_fmt_q[0] = src_fmt_q;
356
+ assign mid_pipe_dst_fmt_q[0] = dst_fmt_q;
357
+ assign mid_pipe_int_fmt_q[0] = int_fmt_q;
358
+ assign mid_pipe_tag_q[0] = inp_pipe_tag_q[NUM_INP_REGS];
359
+ assign mid_pipe_mask_q[0] = inp_pipe_mask_q[NUM_INP_REGS];
360
+ assign mid_pipe_aux_q[0] = inp_pipe_aux_q[NUM_INP_REGS];
361
+ assign mid_pipe_valid_q[0] = inp_pipe_valid_q[NUM_INP_REGS];
362
+ // Input stage: Propagate pipeline ready signal to input pipe
363
+ assign inp_pipe_ready[NUM_INP_REGS] = mid_pipe_ready[0];
364
+
365
+ // Generate the register stages
366
+ for (genvar i = 0; i < NUM_MID_REGS; i++) begin : gen_inside_pipeline
367
+ // Internal register enable for this stage
368
+ logic reg_ena;
369
+ // Determine the ready signal of the current stage - advance the pipeline:
370
+ // 1. if the next stage is ready for our data
371
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
372
+ assign mid_pipe_ready[i] = mid_pipe_ready[i+1] | ~mid_pipe_valid_q[i+1];
373
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
374
+ `FFLARNC(mid_pipe_valid_q[i+1], mid_pipe_valid_q[i], mid_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
375
+ // Enable register if pipleine ready and a valid data item is present
376
+ assign reg_ena = mid_pipe_ready[i] & mid_pipe_valid_q[i];
377
+ // Generate the pipeline registers within the stages, use enable-registers
378
+ `FFL(mid_pipe_input_sign_q[i+1], mid_pipe_input_sign_q[i], reg_ena, '0)
379
+ `FFL(mid_pipe_input_exp_q[i+1], mid_pipe_input_exp_q[i], reg_ena, '0)
380
+ `FFL(mid_pipe_input_mant_q[i+1], mid_pipe_input_mant_q[i], reg_ena, '0)
381
+ `FFL(mid_pipe_dest_exp_q[i+1], mid_pipe_dest_exp_q[i], reg_ena, '0)
382
+ `FFL(mid_pipe_src_is_int_q[i+1], mid_pipe_src_is_int_q[i], reg_ena, '0)
383
+ `FFL(mid_pipe_dst_is_int_q[i+1], mid_pipe_dst_is_int_q[i], reg_ena, '0)
384
+ `FFL(mid_pipe_info_q[i+1], mid_pipe_info_q[i], reg_ena, '0)
385
+ `FFL(mid_pipe_mant_zero_q[i+1], mid_pipe_mant_zero_q[i], reg_ena, '0)
386
+ `FFL(mid_pipe_op_mod_q[i+1], mid_pipe_op_mod_q[i], reg_ena, '0)
387
+ `FFL(mid_pipe_rnd_mode_q[i+1], mid_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
388
+ `FFL(mid_pipe_src_fmt_q[i+1], mid_pipe_src_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
389
+ `FFL(mid_pipe_dst_fmt_q[i+1], mid_pipe_dst_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
390
+ `FFL(mid_pipe_int_fmt_q[i+1], mid_pipe_int_fmt_q[i], reg_ena, fpnew_pkg::int_format_e'(0))
391
+ `FFL(mid_pipe_tag_q[i+1], mid_pipe_tag_q[i], reg_ena, TagType'('0))
392
+ `FFL(mid_pipe_mask_q[i+1], mid_pipe_mask_q[i], reg_ena, '0)
393
+ `FFL(mid_pipe_aux_q[i+1], mid_pipe_aux_q[i], reg_ena, AuxType'('0))
394
+ end
395
+ // Output stage: assign selected pipe outputs to signals for later use
396
+ assign input_sign_q = mid_pipe_input_sign_q[NUM_MID_REGS];
397
+ assign input_exp_q = mid_pipe_input_exp_q[NUM_MID_REGS];
398
+ assign input_mant_q = mid_pipe_input_mant_q[NUM_MID_REGS];
399
+ assign destination_exp_q = mid_pipe_dest_exp_q[NUM_MID_REGS];
400
+ assign src_is_int_q = mid_pipe_src_is_int_q[NUM_MID_REGS];
401
+ assign dst_is_int_q = mid_pipe_dst_is_int_q[NUM_MID_REGS];
402
+ assign info_q = mid_pipe_info_q[NUM_MID_REGS];
403
+ assign mant_is_zero_q = mid_pipe_mant_zero_q[NUM_MID_REGS];
404
+ assign op_mod_q2 = mid_pipe_op_mod_q[NUM_MID_REGS];
405
+ assign rnd_mode_q = mid_pipe_rnd_mode_q[NUM_MID_REGS];
406
+ assign src_fmt_q2 = mid_pipe_src_fmt_q[NUM_MID_REGS];
407
+ assign dst_fmt_q2 = mid_pipe_dst_fmt_q[NUM_MID_REGS];
408
+ assign int_fmt_q2 = mid_pipe_int_fmt_q[NUM_MID_REGS];
409
+
410
+ // --------
411
+ // Casting
412
+ // --------
413
+ logic [INT_EXP_WIDTH-1:0] final_exp; // after eventual adjustments
414
+
415
+ logic [2*INT_MAN_WIDTH:0] preshift_mant; // mantissa before final shift
416
+ logic [2*INT_MAN_WIDTH:0] destination_mant; // mantissa from shifter, with rnd bit
417
+ logic [SUPER_MAN_BITS-1:0] final_mant; // mantissa after adjustments
418
+ logic [MAX_INT_WIDTH-1:0] final_int; // integer shifted in position
419
+
420
+ logic [$clog2(INT_MAN_WIDTH+1)-1:0] denorm_shamt; // shift amount for denormalization
421
+
422
+ logic [1:0] fp_round_sticky_bits, int_round_sticky_bits, round_sticky_bits;
423
+ logic of_before_round, uf_before_round;
424
+
425
+
426
+ // Perform adjustments to mantissa and exponent
427
+ always_comb begin : cast_value
428
+ // Default assignment
429
+ final_exp = unsigned'(destination_exp_q); // take exponent as is, only look at lower bits
430
+ preshift_mant = '0; // initialize mantissa container with zeroes
431
+ denorm_shamt = SUPER_MAN_BITS - fpnew_pkg::man_bits(dst_fmt_q2); // right of mantissa
432
+ of_before_round = 1'b0;
433
+ uf_before_round = 1'b0;
434
+
435
+ // Place mantissa to the left of the shifter
436
+ preshift_mant = input_mant_q << (INT_MAN_WIDTH + 1);
437
+
438
+ // Handle INT casts
439
+ if (dst_is_int_q) begin
440
+ // By default right shift mantissa to be an integer
441
+ denorm_shamt = unsigned'(MAX_INT_WIDTH - 1 - input_exp_q);
442
+ // overflow: when converting to unsigned the range is larger by one
443
+ if (input_exp_q >= signed'(fpnew_pkg::int_width(int_fmt_q2) - 1 + op_mod_q2)) begin
444
+ denorm_shamt = '0; // prevent shifting
445
+ of_before_round = 1'b1;
446
+ // underflow
447
+ end else if (input_exp_q < -1) begin
448
+ denorm_shamt = MAX_INT_WIDTH + 1; // all bits go to the sticky
449
+ uf_before_round = 1'b1;
450
+ end
451
+ // Handle FP over-/underflows
452
+ end else begin
453
+ // Overflow or infinities (for proper rounding)
454
+ if ((destination_exp_q >= signed'(2**fpnew_pkg::exp_bits(dst_fmt_q2))-1) ||
455
+ (~src_is_int_q && info_q.is_inf)) begin
456
+ final_exp = unsigned'(2**fpnew_pkg::exp_bits(dst_fmt_q2)-2); // largest normal value
457
+ preshift_mant = '1; // largest normal value and RS bits set
458
+ of_before_round = 1'b1;
459
+ // Denormalize underflowing values
460
+ end else if (destination_exp_q < 1 &&
461
+ destination_exp_q >= -signed'(fpnew_pkg::man_bits(dst_fmt_q2))) begin
462
+ final_exp = '0; // denormal result
463
+ denorm_shamt = unsigned'(denorm_shamt + 1 - destination_exp_q); // adjust right shifting
464
+ uf_before_round = 1'b1;
465
+ // Limit the shift to retain sticky bits
466
+ end else if (destination_exp_q < -signed'(fpnew_pkg::man_bits(dst_fmt_q2))) begin
467
+ final_exp = '0; // denormal result
468
+ denorm_shamt = unsigned'(denorm_shamt + 2 + fpnew_pkg::man_bits(dst_fmt_q2)); // to sticky
469
+ uf_before_round = 1'b1;
470
+ end
471
+ end
472
+ end
473
+
474
+ localparam NUM_FP_STICKY = 2 * INT_MAN_WIDTH - SUPER_MAN_BITS - 1; // removed mantissa, 1. and R
475
+ localparam NUM_INT_STICKY = 2 * INT_MAN_WIDTH - MAX_INT_WIDTH; // removed int and R
476
+
477
+ // Mantissa adjustment shift
478
+ assign destination_mant = preshift_mant >> denorm_shamt;
479
+ // Extract final mantissa and round bit, discard the normal bit (for FP)
480
+ assign {final_mant, fp_round_sticky_bits[1]} =
481
+ destination_mant[2*INT_MAN_WIDTH-1-:SUPER_MAN_BITS+1];
482
+ assign {final_int, int_round_sticky_bits[1]} = destination_mant[2*INT_MAN_WIDTH-:MAX_INT_WIDTH+1];
483
+ // Collapse sticky bits
484
+ assign fp_round_sticky_bits[0] = (| {destination_mant[NUM_FP_STICKY-1:0]});
485
+ assign int_round_sticky_bits[0] = (| {destination_mant[NUM_INT_STICKY-1:0]});
486
+
487
+ // select RS bits for destination operation
488
+ assign round_sticky_bits = dst_is_int_q ? int_round_sticky_bits : fp_round_sticky_bits;
489
+
490
+ // ----------------------------
491
+ // Rounding and classification
492
+ // ----------------------------
493
+ logic [WIDTH-1:0] pre_round_abs; // absolute value of result before rnd
494
+ logic of_after_round; // overflow
495
+ logic uf_after_round; // underflow
496
+
497
+ logic [NUM_FORMATS-1:0][WIDTH-1:0] fmt_pre_round_abs; // per format
498
+ logic [NUM_FORMATS-1:0] fmt_of_after_round;
499
+ logic [NUM_FORMATS-1:0] fmt_uf_after_round;
500
+
501
+ logic [NUM_INT_FORMATS-1:0][WIDTH-1:0] ifmt_pre_round_abs; // per format
502
+ logic [NUM_INT_FORMATS-1:0] ifmt_of_after_round;
503
+
504
+ logic rounded_sign;
505
+ logic [WIDTH-1:0] rounded_abs; // absolute value of result after rounding
506
+ logic result_true_zero;
507
+
508
+ logic [WIDTH-1:0] rounded_int_res; // after possible inversion
509
+ logic rounded_int_res_zero; // after rounding
510
+
511
+
512
+ // Pack exponent and mantissa into proper rounding form
513
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_res_assemble
514
+ // Set up some constants
515
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
516
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
517
+
518
+ if (FpFmtConfig[fmt]) begin : active_format
519
+ always_comb begin : assemble_result
520
+ fmt_pre_round_abs[fmt] = {final_exp[EXP_BITS-1:0], final_mant[MAN_BITS-1:0]}; // 0-extend
521
+ end
522
+ end else begin : inactive_format
523
+ assign fmt_pre_round_abs[fmt] = '{default: fpnew_pkg::DONT_CARE};
524
+ end
525
+ end
526
+
527
+ // Sign-extend integer result
528
+ for (genvar ifmt = 0; ifmt < int'(NUM_INT_FORMATS); ifmt++) begin : gen_int_res_sign_ext
529
+ // Set up some constants
530
+ localparam int unsigned INT_WIDTH = fpnew_pkg::int_width(fpnew_pkg::int_format_e'(ifmt));
531
+
532
+ if (IntFmtConfig[ifmt]) begin : active_format
533
+ always_comb begin : assemble_result
534
+ // sign-extend reusult
535
+ ifmt_pre_round_abs[ifmt] = '{default: final_int[INT_WIDTH-1]};
536
+ ifmt_pre_round_abs[ifmt][INT_WIDTH-1:0] = final_int[INT_WIDTH-1:0];
537
+ end
538
+ end else begin : inactive_format
539
+ assign ifmt_pre_round_abs[ifmt] = '{default: fpnew_pkg::DONT_CARE};
540
+ end
541
+ end
542
+
543
+ // Select output with destination format and operation
544
+ assign pre_round_abs = dst_is_int_q ? ifmt_pre_round_abs[int_fmt_q2] : fmt_pre_round_abs[dst_fmt_q2];
545
+
546
+ fpnew_rounding #(
547
+ .AbsWidth ( WIDTH )
548
+ ) i_fpnew_rounding (
549
+ .abs_value_i ( pre_round_abs ),
550
+ .sign_i ( input_sign_q ), // source format
551
+ .round_sticky_bits_i ( round_sticky_bits ),
552
+ .rnd_mode_i ( rnd_mode_q ),
553
+ .effective_subtraction_i ( 1'b0 ), // no operation happened
554
+ .abs_rounded_o ( rounded_abs ),
555
+ .sign_o ( rounded_sign ),
556
+ .exact_zero_o ( result_true_zero )
557
+ );
558
+
559
+ logic [NUM_FORMATS-1:0][WIDTH-1:0] fmt_result;
560
+
561
+ // Detect overflows and inject sign
562
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_sign_inject
563
+ // Set up some constants
564
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
565
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
566
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
567
+
568
+ if (FpFmtConfig[fmt]) begin : active_format
569
+ always_comb begin : post_process
570
+ // detect of / uf
571
+ fmt_uf_after_round[fmt] = rounded_abs[EXP_BITS+MAN_BITS-1:MAN_BITS] == '0; // denormal
572
+ fmt_of_after_round[fmt] = rounded_abs[EXP_BITS+MAN_BITS-1:MAN_BITS] == '1; // inf exp.
573
+
574
+ // Assemble regular result, nan box short ones. Int zeroes need to be detected`
575
+ fmt_result[fmt] = '1;
576
+ fmt_result[fmt][FP_WIDTH-1:0] = src_is_int_q & mant_is_zero_q
577
+ ? '0
578
+ : {rounded_sign, rounded_abs[EXP_BITS+MAN_BITS-1:0]};
579
+ end
580
+ end else begin : inactive_format
581
+ assign fmt_uf_after_round[fmt] = fpnew_pkg::DONT_CARE;
582
+ assign fmt_of_after_round[fmt] = fpnew_pkg::DONT_CARE;
583
+ assign fmt_result[fmt] = '{default: fpnew_pkg::DONT_CARE};
584
+ end
585
+ end
586
+
587
+ // Negative integer result needs to be brought into two's complement
588
+ assign rounded_int_res = rounded_sign ? unsigned'(-rounded_abs) : rounded_abs;
589
+ assign rounded_int_res_zero = (rounded_int_res == '0);
590
+
591
+ // Detect integer overflows after rounding (only positives)
592
+ for (genvar ifmt = 0; ifmt < int'(NUM_INT_FORMATS); ifmt++) begin : gen_int_overflow
593
+ // Set up some constants
594
+ localparam int unsigned INT_WIDTH = fpnew_pkg::int_width(fpnew_pkg::int_format_e'(ifmt));
595
+
596
+ if (IntFmtConfig[ifmt]) begin : active_format
597
+ always_comb begin : detect_overflow
598
+ ifmt_of_after_round[ifmt] = 1'b0;
599
+ // Int result can overflow if we're at the max exponent
600
+ if (!rounded_sign && input_exp_q == signed'(INT_WIDTH - 2 + op_mod_q2)) begin
601
+ // Check whether the rounded MSB differs from unrounded MSB
602
+ ifmt_of_after_round[ifmt] = ~rounded_int_res[INT_WIDTH-2+op_mod_q2];
603
+ end
604
+ end
605
+ end else begin : inactive_format
606
+ assign ifmt_of_after_round[ifmt] = fpnew_pkg::DONT_CARE;
607
+ end
608
+ end
609
+
610
+ // Classification after rounding select by destination format
611
+ assign uf_after_round = fmt_uf_after_round[dst_fmt_q2];
612
+ assign of_after_round = dst_is_int_q ? ifmt_of_after_round[int_fmt_q2] : fmt_of_after_round[dst_fmt_q2];
613
+
614
+ // -------------------------
615
+ // FP Special case handling
616
+ // -------------------------
617
+ logic [WIDTH-1:0] fp_special_result;
618
+ fpnew_pkg::status_t fp_special_status;
619
+ logic fp_result_is_special;
620
+
621
+ logic [NUM_FORMATS-1:0][WIDTH-1:0] fmt_special_result;
622
+
623
+ // Special result construction
624
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_special_results
625
+ // Set up some constants
626
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
627
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
628
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
629
+
630
+ localparam logic [EXP_BITS-1:0] QNAN_EXPONENT = '1;
631
+ localparam logic [MAN_BITS-1:0] QNAN_MANTISSA = 2**(MAN_BITS-1);
632
+
633
+ if (FpFmtConfig[fmt]) begin : active_format
634
+ always_comb begin : special_results
635
+ logic [FP_WIDTH-1:0] special_res;
636
+ special_res = info_q.is_zero
637
+ ? input_sign_q << FP_WIDTH-1 // signed zero
638
+ : {1'b0, QNAN_EXPONENT, QNAN_MANTISSA}; // qNaN
639
+
640
+ // Initialize special result with ones (NaN-box)
641
+ fmt_special_result[fmt] = '1;
642
+ fmt_special_result[fmt][FP_WIDTH-1:0] = special_res;
643
+ end
644
+ end else begin : inactive_format
645
+ assign fmt_special_result[fmt] = '{default: fpnew_pkg::DONT_CARE};
646
+ end
647
+ end
648
+
649
+ // Detect special case from source format, I2F casts don't produce a special result
650
+ assign fp_result_is_special = ~src_is_int_q & (info_q.is_zero |
651
+ info_q.is_nan |
652
+ ~info_q.is_boxed);
653
+
654
+ // Signalling input NaNs raise invalid flag, otherwise no flags set
655
+ assign fp_special_status = '{NV: info_q.is_signalling, default: 1'b0};
656
+
657
+ // Assemble result according to destination format
658
+ assign fp_special_result = fmt_special_result[dst_fmt_q2]; // destination format
659
+
660
+ // --------------------------
661
+ // INT Special case handling
662
+ // --------------------------
663
+ logic [WIDTH-1:0] int_special_result;
664
+ fpnew_pkg::status_t int_special_status;
665
+ logic int_result_is_special;
666
+
667
+ logic [NUM_INT_FORMATS-1:0][WIDTH-1:0] ifmt_special_result;
668
+
669
+ // Special result construction
670
+ for (genvar ifmt = 0; ifmt < int'(NUM_INT_FORMATS); ifmt++) begin : gen_special_results_int
671
+ // Set up some constants
672
+ localparam int unsigned INT_WIDTH = fpnew_pkg::int_width(fpnew_pkg::int_format_e'(ifmt));
673
+
674
+ if (IntFmtConfig[ifmt]) begin : active_format
675
+ always_comb begin : special_results
676
+ automatic logic [INT_WIDTH-1:0] special_res;
677
+
678
+ // Default is overflow to positive max, which is 2**INT_WIDTH-1 or 2**(INT_WIDTH-1)-1
679
+ special_res[INT_WIDTH-2:0] = '1; // alone yields 2**(INT_WIDTH-1)-1
680
+ special_res[INT_WIDTH-1] = op_mod_q2; // for unsigned casts yields 2**INT_WIDTH-1
681
+
682
+ // Negative special case (except for nans) tie to -max or 0
683
+ if (input_sign_q && !info_q.is_nan)
684
+ special_res = ~special_res;
685
+
686
+ // Initialize special result with sign-extension
687
+ ifmt_special_result[ifmt] = '{default: special_res[INT_WIDTH-1]};
688
+ ifmt_special_result[ifmt][INT_WIDTH-1:0] = special_res;
689
+ end
690
+ end else begin : inactive_format
691
+ assign ifmt_special_result[ifmt] = '{default: fpnew_pkg::DONT_CARE};
692
+ end
693
+ end
694
+
695
+ // Detect special case from source format (inf, nan, overflow, nan-boxing or negative unsigned)
696
+ assign int_result_is_special = info_q.is_nan | info_q.is_inf |
697
+ of_before_round | of_after_round | ~info_q.is_boxed |
698
+ (input_sign_q & op_mod_q2 & ~rounded_int_res_zero);
699
+
700
+ // All integer special cases are invalid
701
+ assign int_special_status = '{NV: 1'b1, default: 1'b0};
702
+
703
+ // Assemble result according to destination format
704
+ assign int_special_result = ifmt_special_result[int_fmt_q2]; // destination format
705
+
706
+ // -----------------
707
+ // Result selection
708
+ // -----------------
709
+ fpnew_pkg::status_t int_regular_status, fp_regular_status;
710
+
711
+ logic [WIDTH-1:0] fp_result, int_result;
712
+ fpnew_pkg::status_t fp_status, int_status;
713
+
714
+ assign fp_regular_status.NV = src_is_int_q & (of_before_round | of_after_round); // overflow is invalid for I2F casts
715
+ assign fp_regular_status.DZ = 1'b0; // no divisions
716
+ assign fp_regular_status.OF = ~src_is_int_q & (~info_q.is_inf & (of_before_round | of_after_round)); // inf casts no OF
717
+ assign fp_regular_status.UF = uf_after_round & fp_regular_status.NX;
718
+ assign fp_regular_status.NX = src_is_int_q ? (| fp_round_sticky_bits) // overflow is invalid in i2f
719
+ : (| fp_round_sticky_bits) | (~info_q.is_inf & (of_before_round | of_after_round));
720
+ assign int_regular_status = '{NX: (| int_round_sticky_bits), default: 1'b0};
721
+
722
+ assign fp_result = fp_result_is_special ? fp_special_result : fmt_result[dst_fmt_q2];
723
+ assign fp_status = fp_result_is_special ? fp_special_status : fp_regular_status;
724
+ assign int_result = int_result_is_special ? int_special_result : rounded_int_res;
725
+ assign int_status = int_result_is_special ? int_special_status : int_regular_status;
726
+
727
+ // Final results for output pipeline
728
+ logic [WIDTH-1:0] result_d;
729
+ fpnew_pkg::status_t status_d;
730
+ logic extension_bit;
731
+
732
+ // Select output depending on special case detection
733
+ assign result_d = dst_is_int_q ? int_result : fp_result;
734
+ assign status_d = dst_is_int_q ? int_status : fp_status;
735
+
736
+ // MSB of int result decides extension, otherwise NaN box
737
+ assign extension_bit = dst_is_int_q ? int_result[WIDTH-1] : 1'b1;
738
+
739
+ // ----------------
740
+ // Output Pipeline
741
+ // ----------------
742
+ // Output pipeline signals, index i holds signal after i register stages
743
+ logic [0:NUM_OUT_REGS][WIDTH-1:0] out_pipe_result_q;
744
+ fpnew_pkg::status_t [0:NUM_OUT_REGS] out_pipe_status_q;
745
+ logic [0:NUM_OUT_REGS] out_pipe_ext_bit_q;
746
+ TagType [0:NUM_OUT_REGS] out_pipe_tag_q;
747
+ logic [0:NUM_OUT_REGS] out_pipe_mask_q;
748
+ AuxType [0:NUM_OUT_REGS] out_pipe_aux_q;
749
+ logic [0:NUM_OUT_REGS] out_pipe_valid_q;
750
+ // Ready signal is combinatorial for all stages
751
+ logic [0:NUM_OUT_REGS] out_pipe_ready;
752
+
753
+ // Input stage: First element of pipeline is taken from inputs
754
+ assign out_pipe_result_q[0] = result_d;
755
+ assign out_pipe_status_q[0] = status_d;
756
+ assign out_pipe_ext_bit_q[0] = extension_bit;
757
+ assign out_pipe_tag_q[0] = mid_pipe_tag_q[NUM_MID_REGS];
758
+ assign out_pipe_mask_q[0] = mid_pipe_mask_q[NUM_MID_REGS];
759
+ assign out_pipe_aux_q[0] = mid_pipe_aux_q[NUM_MID_REGS];
760
+ assign out_pipe_valid_q[0] = mid_pipe_valid_q[NUM_MID_REGS];
761
+ // Input stage: Propagate pipeline ready signal to inside pipe
762
+ assign mid_pipe_ready[NUM_MID_REGS] = out_pipe_ready[0];
763
+ // Generate the register stages
764
+ for (genvar i = 0; i < NUM_OUT_REGS; i++) begin : gen_output_pipeline
765
+ // Internal register enable for this stage
766
+ logic reg_ena;
767
+ // Determine the ready signal of the current stage - advance the pipeline:
768
+ // 1. if the next stage is ready for our data
769
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
770
+ assign out_pipe_ready[i] = out_pipe_ready[i+1] | ~out_pipe_valid_q[i+1];
771
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
772
+ `FFLARNC(out_pipe_valid_q[i+1], out_pipe_valid_q[i], out_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
773
+ // Enable register if pipleine ready and a valid data item is present
774
+ assign reg_ena = out_pipe_ready[i] & out_pipe_valid_q[i];
775
+ // Generate the pipeline registers within the stages, use enable-registers
776
+ `FFL(out_pipe_result_q[i+1], out_pipe_result_q[i], reg_ena, '0)
777
+ `FFL(out_pipe_status_q[i+1], out_pipe_status_q[i], reg_ena, '0)
778
+ `FFL(out_pipe_ext_bit_q[i+1], out_pipe_ext_bit_q[i], reg_ena, '0)
779
+ `FFL(out_pipe_tag_q[i+1], out_pipe_tag_q[i], reg_ena, TagType'('0))
780
+ `FFL(out_pipe_mask_q[i+1], out_pipe_mask_q[i], reg_ena, '0)
781
+ `FFL(out_pipe_aux_q[i+1], out_pipe_aux_q[i], reg_ena, AuxType'('0))
782
+ end
783
+ // Output stage: Ready travels backwards from output side, driven by downstream circuitry
784
+ assign out_pipe_ready[NUM_OUT_REGS] = out_ready_i;
785
+ // Output stage: assign module outputs
786
+ assign result_o = out_pipe_result_q[NUM_OUT_REGS];
787
+ assign status_o = out_pipe_status_q[NUM_OUT_REGS];
788
+ assign extension_bit_o = out_pipe_ext_bit_q[NUM_OUT_REGS];
789
+ assign tag_o = out_pipe_tag_q[NUM_OUT_REGS];
790
+ assign mask_o = out_pipe_mask_q[NUM_OUT_REGS];
791
+ assign aux_o = out_pipe_aux_q[NUM_OUT_REGS];
792
+ assign out_valid_o = out_pipe_valid_q[NUM_OUT_REGS];
793
+ assign busy_o = (| {inp_pipe_valid_q, mid_pipe_valid_q, out_pipe_valid_q});
794
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_classifier.sv ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ module fpnew_classifier #(
17
+ parameter fpnew_pkg::fp_format_e FpFormat = fpnew_pkg::fp_format_e'(0),
18
+ parameter int unsigned NumOperands = 1,
19
+ // Do not change
20
+ localparam int unsigned WIDTH = fpnew_pkg::fp_width(FpFormat)
21
+ ) (
22
+ input logic [NumOperands-1:0][WIDTH-1:0] operands_i,
23
+ input logic [NumOperands-1:0] is_boxed_i,
24
+ output fpnew_pkg::fp_info_t [NumOperands-1:0] info_o
25
+ );
26
+
27
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(FpFormat);
28
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(FpFormat);
29
+
30
+ // Type definition
31
+ typedef struct packed {
32
+ logic sign;
33
+ logic [EXP_BITS-1:0] exponent;
34
+ logic [MAN_BITS-1:0] mantissa;
35
+ } fp_t;
36
+
37
+ // Iterate through all operands
38
+ for (genvar op = 0; op < int'(NumOperands); op++) begin : gen_num_values
39
+
40
+ fp_t value;
41
+ logic is_boxed;
42
+ logic is_normal;
43
+ logic is_inf;
44
+ logic is_nan;
45
+ logic is_signalling;
46
+ logic is_quiet;
47
+ logic is_zero;
48
+ logic is_subnormal;
49
+
50
+ // ---------------
51
+ // Classify Input
52
+ // ---------------
53
+ always_comb begin : classify_input
54
+ value = operands_i[op];
55
+ is_boxed = is_boxed_i[op];
56
+ is_normal = is_boxed && (value.exponent != '0) && (value.exponent != '1);
57
+ is_zero = is_boxed && (value.exponent == '0) && (value.mantissa == '0);
58
+ is_subnormal = is_boxed && (value.exponent == '0) && !is_zero;
59
+ is_inf = is_boxed && ((value.exponent == '1) && (value.mantissa == '0));
60
+ is_nan = !is_boxed || ((value.exponent == '1) && (value.mantissa != '0));
61
+ is_signalling = is_boxed && is_nan && (value.mantissa[MAN_BITS-1] == 1'b0);
62
+ is_quiet = is_nan && !is_signalling;
63
+ // Assign output for current input
64
+ info_o[op].is_normal = is_normal;
65
+ info_o[op].is_subnormal = is_subnormal;
66
+ info_o[op].is_zero = is_zero;
67
+ info_o[op].is_inf = is_inf;
68
+ info_o[op].is_nan = is_nan;
69
+ info_o[op].is_signalling = is_signalling;
70
+ info_o[op].is_quiet = is_quiet;
71
+ info_o[op].is_boxed = is_boxed;
72
+ end
73
+ end
74
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_divsqrt_multi.sv ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ `include "common_cells/registers.svh"
17
+
18
+ module fpnew_divsqrt_multi #(
19
+ parameter fpnew_pkg::fmt_logic_t FpFmtConfig = '1,
20
+ // FPU configuration
21
+ parameter int unsigned NumPipeRegs = 0,
22
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::AFTER,
23
+ parameter type TagType = logic,
24
+ parameter type AuxType = logic,
25
+ // Do not change
26
+ localparam int unsigned WIDTH = fpnew_pkg::max_fp_width(FpFmtConfig),
27
+ localparam int unsigned NUM_FORMATS = fpnew_pkg::NUM_FP_FORMATS
28
+ ) (
29
+ input logic clk_i,
30
+ input logic rst_ni,
31
+ // Input signals
32
+ input logic [1:0][WIDTH-1:0] operands_i, // 2 operands
33
+ input logic [NUM_FORMATS-1:0][1:0] is_boxed_i, // 2 operands
34
+ input fpnew_pkg::roundmode_e rnd_mode_i,
35
+ input fpnew_pkg::operation_e op_i,
36
+ input fpnew_pkg::fp_format_e dst_fmt_i,
37
+ input TagType tag_i,
38
+ input logic mask_i,
39
+ input AuxType aux_i,
40
+ // Input Handshake
41
+ input logic in_valid_i,
42
+ output logic in_ready_o,
43
+ output logic divsqrt_done_o,
44
+ input logic simd_synch_done_i,
45
+ output logic divsqrt_ready_o,
46
+ input logic simd_synch_rdy_i,
47
+ input logic flush_i,
48
+ // Output signals
49
+ output logic [WIDTH-1:0] result_o,
50
+ output fpnew_pkg::status_t status_o,
51
+ output logic extension_bit_o,
52
+ output TagType tag_o,
53
+ output logic mask_o,
54
+ output AuxType aux_o,
55
+ // Output handshake
56
+ output logic out_valid_o,
57
+ input logic out_ready_i,
58
+ // Indication of valid data in flight
59
+ output logic busy_o
60
+ );
61
+
62
+ // ----------
63
+ // Constants
64
+ // ----------
65
+ // Pipelines
66
+ localparam NUM_INP_REGS = (PipeConfig == fpnew_pkg::BEFORE)
67
+ ? NumPipeRegs
68
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
69
+ ? (NumPipeRegs / 2) // Last to get distributed regs
70
+ : 0); // no regs here otherwise
71
+ localparam NUM_OUT_REGS = (PipeConfig == fpnew_pkg::AFTER || PipeConfig == fpnew_pkg::INSIDE)
72
+ ? NumPipeRegs
73
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
74
+ ? ((NumPipeRegs + 1) / 2) // First to get distributed regs
75
+ : 0); // no regs here otherwise
76
+
77
+ // ---------------
78
+ // Input pipeline
79
+ // ---------------
80
+ // Selected pipeline output signals as non-arrays
81
+ logic [1:0][WIDTH-1:0] operands_q;
82
+ fpnew_pkg::roundmode_e rnd_mode_q;
83
+ fpnew_pkg::operation_e op_q;
84
+ fpnew_pkg::fp_format_e dst_fmt_q;
85
+ logic in_valid_q;
86
+
87
+ // Input pipeline signals, index i holds signal after i register stages
88
+ logic [0:NUM_INP_REGS][1:0][WIDTH-1:0] inp_pipe_operands_q;
89
+ fpnew_pkg::roundmode_e [0:NUM_INP_REGS] inp_pipe_rnd_mode_q;
90
+ fpnew_pkg::operation_e [0:NUM_INP_REGS] inp_pipe_op_q;
91
+ fpnew_pkg::fp_format_e [0:NUM_INP_REGS] inp_pipe_dst_fmt_q;
92
+ TagType [0:NUM_INP_REGS] inp_pipe_tag_q;
93
+ logic [0:NUM_INP_REGS] inp_pipe_mask_q;
94
+ AuxType [0:NUM_INP_REGS] inp_pipe_aux_q;
95
+ logic [0:NUM_INP_REGS] inp_pipe_valid_q;
96
+ // Ready signal is combinatorial for all stages
97
+ logic [0:NUM_INP_REGS] inp_pipe_ready;
98
+
99
+ // Input stage: First element of pipeline is taken from inputs
100
+ assign inp_pipe_operands_q[0] = operands_i;
101
+ assign inp_pipe_rnd_mode_q[0] = rnd_mode_i;
102
+ assign inp_pipe_op_q[0] = op_i;
103
+ assign inp_pipe_dst_fmt_q[0] = dst_fmt_i;
104
+ assign inp_pipe_tag_q[0] = tag_i;
105
+ assign inp_pipe_mask_q[0] = mask_i;
106
+ assign inp_pipe_aux_q[0] = aux_i;
107
+ assign inp_pipe_valid_q[0] = in_valid_i;
108
+ // Input stage: Propagate pipeline ready signal to updtream circuitry
109
+ assign in_ready_o = inp_pipe_ready[0];
110
+ // Generate the register stages
111
+ for (genvar i = 0; i < NUM_INP_REGS; i++) begin : gen_input_pipeline
112
+ // Internal register enable for this stage
113
+ logic reg_ena;
114
+ // Determine the ready signal of the current stage - advance the pipeline:
115
+ // 1. if the next stage is ready for our data
116
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
117
+ assign inp_pipe_ready[i] = inp_pipe_ready[i+1] | ~inp_pipe_valid_q[i+1];
118
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
119
+ `FFLARNC(inp_pipe_valid_q[i+1], inp_pipe_valid_q[i], inp_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
120
+ // Enable register if pipleine ready and a valid data item is present
121
+ assign reg_ena = inp_pipe_ready[i] & inp_pipe_valid_q[i];
122
+ // Generate the pipeline registers within the stages, use enable-registers
123
+ `FFL(inp_pipe_operands_q[i+1], inp_pipe_operands_q[i], reg_ena, '0)
124
+ `FFL(inp_pipe_rnd_mode_q[i+1], inp_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
125
+ `FFL(inp_pipe_op_q[i+1], inp_pipe_op_q[i], reg_ena, fpnew_pkg::FMADD)
126
+ `FFL(inp_pipe_dst_fmt_q[i+1], inp_pipe_dst_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
127
+ `FFL(inp_pipe_tag_q[i+1], inp_pipe_tag_q[i], reg_ena, TagType'('0))
128
+ `FFL(inp_pipe_mask_q[i+1], inp_pipe_mask_q[i], reg_ena, '0)
129
+ `FFL(inp_pipe_aux_q[i+1], inp_pipe_aux_q[i], reg_ena, AuxType'('0))
130
+ end
131
+ // Output stage: assign selected pipe outputs to signals for later use
132
+ assign operands_q = inp_pipe_operands_q[NUM_INP_REGS];
133
+ assign rnd_mode_q = inp_pipe_rnd_mode_q[NUM_INP_REGS];
134
+ assign op_q = inp_pipe_op_q[NUM_INP_REGS];
135
+ assign dst_fmt_q = inp_pipe_dst_fmt_q[NUM_INP_REGS];
136
+ assign in_valid_q = inp_pipe_valid_q[NUM_INP_REGS];
137
+
138
+ // -----------------
139
+ // Input processing
140
+ // -----------------
141
+ logic [1:0] divsqrt_fmt;
142
+ logic [1:0][63:0] divsqrt_operands; // those are fixed to 64bit
143
+ logic input_is_fp8;
144
+
145
+ // Translate fpnew formats into divsqrt formats
146
+ always_comb begin : translate_fmt
147
+ unique case (dst_fmt_q)
148
+ fpnew_pkg::FP32: divsqrt_fmt = 2'b00;
149
+ fpnew_pkg::FP64: divsqrt_fmt = 2'b01;
150
+ fpnew_pkg::FP16: divsqrt_fmt = 2'b10;
151
+ fpnew_pkg::FP16ALT: divsqrt_fmt = 2'b11;
152
+ default: divsqrt_fmt = 2'b10; // maps also FP8 to FP16
153
+ endcase
154
+
155
+ // Only if FP8 is enabled
156
+ input_is_fp8 = FpFmtConfig[fpnew_pkg::FP8] & (dst_fmt_q == fpnew_pkg::FP8);
157
+
158
+ // If FP8 is supported, map it to an FP16 value
159
+ divsqrt_operands[0] = input_is_fp8 ? operands_q[0] << 8 : operands_q[0];
160
+ divsqrt_operands[1] = input_is_fp8 ? operands_q[1] << 8 : operands_q[1];
161
+ end
162
+
163
+ // ------------
164
+ // Control FSM
165
+ // ------------
166
+
167
+ logic in_ready; // input handshake with upstream
168
+ logic div_valid, sqrt_valid; // input signalling with unit
169
+ logic unit_ready, unit_done, unit_done_q; // status signals from unit instance
170
+ logic op_starting; // high in the cycle a new operation starts
171
+ logic out_valid, out_ready; // output handshake with downstream
172
+ logic unit_busy; // valid data in flight
173
+ // FSM states
174
+ typedef enum logic [1:0] {IDLE, BUSY, HOLD} fsm_state_e;
175
+ fsm_state_e state_q, state_d;
176
+
177
+ // Ready synch with other lanes
178
+ // Bring the FSM-generated ready outside the unit, to synchronize it with the other lanes
179
+ assign divsqrt_ready_o = in_ready;
180
+ // Upstream ready comes from sanitization FSM, and it is synched among all the lanes
181
+ assign inp_pipe_ready[NUM_INP_REGS] = simd_synch_rdy_i;
182
+
183
+ // Valid synch with other lanes
184
+ // When one divsqrt unit completes an operation, keep its done high, waiting for the other lanes
185
+ // As soon as all the lanes are over, we can clear this FF and start with a new operation
186
+ `FFLARNC(unit_done_q, unit_done, unit_done, simd_synch_done_i, 1'b0, clk_i, rst_ni);
187
+ // Tell the other units that this unit has finished now or in the past
188
+ assign divsqrt_done_o = unit_done_q | unit_done;
189
+
190
+ // Valids are gated by the FSM ready. Invalid input ops run a sqrt to not lose illegal instr.
191
+ assign div_valid = in_valid_q & (op_q == fpnew_pkg::DIV) & in_ready & ~flush_i;
192
+ assign sqrt_valid = in_valid_q & (op_q != fpnew_pkg::DIV) & in_ready & ~flush_i;
193
+ assign op_starting = div_valid | sqrt_valid;
194
+
195
+ // FSM to safely apply and receive data from DIVSQRT unit
196
+ always_comb begin : flag_fsm
197
+ // Default assignments
198
+ in_ready = 1'b0;
199
+ out_valid = 1'b0;
200
+ unit_busy = 1'b0;
201
+ state_d = state_q;
202
+
203
+ unique case (state_q)
204
+ // Waiting for work
205
+ IDLE: begin
206
+ in_ready = 1'b1; // we're ready
207
+ if (in_valid_q && unit_ready) begin // New work arrives
208
+ state_d = BUSY; // go into processing state
209
+ end
210
+ end
211
+ // Operation in progress
212
+ BUSY: begin
213
+ unit_busy = 1'b1; // data in flight
214
+ // If all the lanes are done with processing
215
+ if (simd_synch_done_i) begin
216
+ out_valid = 1'b1; // try to commit result downstream
217
+ // If downstream accepts our result
218
+ if (out_ready) begin
219
+ state_d = IDLE; // we anticipate going back to idling..
220
+ if (in_valid_q && unit_ready) begin // ..unless new work comes in
221
+ in_ready = 1'b1; // we acknowledge the instruction
222
+ state_d = BUSY; // and stay busy with it
223
+ end
224
+ // Otherwise if downstream is not ready for the result
225
+ end else begin
226
+ state_d = HOLD; // wait for the pipeline to take the data
227
+ end
228
+ end
229
+ end
230
+ // Waiting with valid result for downstream
231
+ HOLD: begin
232
+ unit_busy = 1'b1; // data in flight
233
+ out_valid = 1'b1; // try to commit result downstream
234
+ // If the result is accepted by downstream
235
+ if (out_ready) begin
236
+ state_d = IDLE; // go back to idle..
237
+ if (in_valid_q && unit_ready) begin // ..unless new work comes in
238
+ in_ready = 1'b1; // acknowledge the new transaction
239
+ state_d = BUSY; // will be busy with the next instruction
240
+ end
241
+ end
242
+ end
243
+ // fall into idle state otherwise
244
+ default: state_d = IDLE;
245
+ endcase
246
+
247
+ // Flushing overrides the other actions
248
+ if (flush_i) begin
249
+ unit_busy = 1'b0; // data is invalidated
250
+ out_valid = 1'b0; // cancel any valid data
251
+ state_d = IDLE; // go to default state
252
+ end
253
+ end
254
+
255
+ // FSM status register (asynch active low reset)
256
+ `FF(state_q, state_d, IDLE)
257
+
258
+ // Hold additional information while the operation is in progress
259
+ logic result_is_fp8_q;
260
+ TagType result_tag_q;
261
+ logic result_mask_q;
262
+ AuxType result_aux_q;
263
+
264
+ // Fill the registers everytime a valid operation arrives (load FF, active low asynch rst)
265
+ `FFL(result_is_fp8_q, input_is_fp8, op_starting, '0)
266
+ `FFL(result_tag_q, inp_pipe_tag_q[NUM_INP_REGS], op_starting, '0)
267
+ `FFL(result_mask_q, inp_pipe_mask_q[NUM_INP_REGS],op_starting, '0)
268
+ `FFL(result_aux_q, inp_pipe_aux_q[NUM_INP_REGS], op_starting, '0)
269
+
270
+ // -----------------
271
+ // DIVSQRT instance
272
+ // -----------------
273
+ logic [63:0] unit_result;
274
+ logic [WIDTH-1:0] adjusted_result, held_result_q;
275
+ fpnew_pkg::status_t unit_status, held_status_q;
276
+ logic hold_en;
277
+
278
+ div_sqrt_top_mvp i_divsqrt_lei (
279
+ .Clk_CI ( clk_i ),
280
+ .Rst_RBI ( rst_ni ),
281
+ .Div_start_SI ( div_valid ),
282
+ .Sqrt_start_SI ( sqrt_valid ),
283
+ .Operand_a_DI ( divsqrt_operands[0] ),
284
+ .Operand_b_DI ( divsqrt_operands[1] ),
285
+ .RM_SI ( rnd_mode_q ),
286
+ .Precision_ctl_SI ( '0 ),
287
+ .Format_sel_SI ( divsqrt_fmt ),
288
+ .Kill_SI ( flush_i ),
289
+ .Result_DO ( unit_result ),
290
+ .Fflags_SO ( unit_status ),
291
+ .Ready_SO ( unit_ready ),
292
+ .Done_SO ( unit_done )
293
+ );
294
+
295
+ // Adjust result width and fix FP8
296
+ assign adjusted_result = result_is_fp8_q ? unit_result >> 8 : unit_result;
297
+
298
+ // Hold the result when one lane has finished execution, except when all the lanes finish together
299
+ // and the result can be accepted downstream
300
+ assign hold_en = unit_done & (~simd_synch_done_i | ~out_ready);
301
+ // The Hold register (load, no reset)
302
+ `FFLNR(held_result_q, adjusted_result, hold_en, clk_i)
303
+ `FFLNR(held_status_q, unit_status, hold_en, clk_i)
304
+
305
+ // --------------
306
+ // Output Select
307
+ // --------------
308
+ logic [WIDTH-1:0] result_d;
309
+ fpnew_pkg::status_t status_d;
310
+ // Prioritize hold register data
311
+ assign result_d = unit_done_q ? held_result_q : adjusted_result;
312
+ assign status_d = unit_done_q ? held_status_q : unit_status;
313
+
314
+ // ----------------
315
+ // Output Pipeline
316
+ // ----------------
317
+ // Output pipeline signals, index i holds signal after i register stages
318
+ logic [0:NUM_OUT_REGS][WIDTH-1:0] out_pipe_result_q;
319
+ fpnew_pkg::status_t [0:NUM_OUT_REGS] out_pipe_status_q;
320
+ TagType [0:NUM_OUT_REGS] out_pipe_tag_q;
321
+ logic [0:NUM_OUT_REGS] out_pipe_mask_q;
322
+ AuxType [0:NUM_OUT_REGS] out_pipe_aux_q;
323
+ logic [0:NUM_OUT_REGS] out_pipe_valid_q;
324
+ // Ready signal is combinatorial for all stages
325
+ logic [0:NUM_OUT_REGS] out_pipe_ready;
326
+
327
+ // Input stage: First element of pipeline is taken from inputs
328
+ assign out_pipe_result_q[0] = result_d;
329
+ assign out_pipe_status_q[0] = status_d;
330
+ assign out_pipe_tag_q[0] = result_tag_q;
331
+ assign out_pipe_mask_q[0] = result_mask_q;
332
+ assign out_pipe_aux_q[0] = result_aux_q;
333
+ assign out_pipe_valid_q[0] = out_valid;
334
+ // Input stage: Propagate pipeline ready signal to inside pipe
335
+ assign out_ready = out_pipe_ready[0];
336
+ // Generate the register stages
337
+ for (genvar i = 0; i < NUM_OUT_REGS; i++) begin : gen_output_pipeline
338
+ // Internal register enable for this stage
339
+ logic reg_ena;
340
+ // Determine the ready signal of the current stage - advance the pipeline:
341
+ // 1. if the next stage is ready for our data
342
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
343
+ assign out_pipe_ready[i] = out_pipe_ready[i+1] | ~out_pipe_valid_q[i+1];
344
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
345
+ `FFLARNC(out_pipe_valid_q[i+1], out_pipe_valid_q[i], out_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
346
+ // Enable register if pipleine ready and a valid data item is present
347
+ assign reg_ena = out_pipe_ready[i] & out_pipe_valid_q[i];
348
+ // Generate the pipeline registers within the stages, use enable-registers
349
+ `FFL(out_pipe_result_q[i+1], out_pipe_result_q[i], reg_ena, '0)
350
+ `FFL(out_pipe_status_q[i+1], out_pipe_status_q[i], reg_ena, '0)
351
+ `FFL(out_pipe_tag_q[i+1], out_pipe_tag_q[i], reg_ena, TagType'('0))
352
+ `FFL(out_pipe_mask_q[i+1], out_pipe_mask_q[i], reg_ena, '0)
353
+ `FFL(out_pipe_aux_q[i+1], out_pipe_aux_q[i], reg_ena, AuxType'('0))
354
+ end
355
+ // Output stage: Ready travels backwards from output side, driven by downstream circuitry
356
+ assign out_pipe_ready[NUM_OUT_REGS] = out_ready_i;
357
+ // Output stage: assign module outputs
358
+ assign result_o = out_pipe_result_q[NUM_OUT_REGS];
359
+ assign status_o = out_pipe_status_q[NUM_OUT_REGS];
360
+ assign extension_bit_o = 1'b1; // always NaN-Box result
361
+ assign tag_o = out_pipe_tag_q[NUM_OUT_REGS];
362
+ assign mask_o = out_pipe_mask_q[NUM_OUT_REGS];
363
+ assign aux_o = out_pipe_aux_q[NUM_OUT_REGS];
364
+ assign out_valid_o = out_pipe_valid_q[NUM_OUT_REGS];
365
+ assign busy_o = (| {inp_pipe_valid_q, unit_busy, out_pipe_valid_q});
366
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_fma.sv ADDED
@@ -0,0 +1,690 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ `include "common_cells/registers.svh"
17
+
18
+ module fpnew_fma #(
19
+ parameter fpnew_pkg::fp_format_e FpFormat = fpnew_pkg::fp_format_e'(0),
20
+ parameter int unsigned NumPipeRegs = 0,
21
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::BEFORE,
22
+ parameter type TagType = logic,
23
+ parameter type AuxType = logic,
24
+
25
+ localparam int unsigned WIDTH = fpnew_pkg::fp_width(FpFormat) // do not change
26
+ ) (
27
+ input logic clk_i,
28
+ input logic rst_ni,
29
+ // Input signals
30
+ input logic [2:0][WIDTH-1:0] operands_i, // 3 operands
31
+ input logic [2:0] is_boxed_i, // 3 operands
32
+ input fpnew_pkg::roundmode_e rnd_mode_i,
33
+ input fpnew_pkg::operation_e op_i,
34
+ input logic op_mod_i,
35
+ input TagType tag_i,
36
+ input logic mask_i,
37
+ input AuxType aux_i,
38
+ // Input Handshake
39
+ input logic in_valid_i,
40
+ output logic in_ready_o,
41
+ input logic flush_i,
42
+ // Output signals
43
+ output logic [WIDTH-1:0] result_o,
44
+ output fpnew_pkg::status_t status_o,
45
+ output logic extension_bit_o,
46
+ output TagType tag_o,
47
+ output logic mask_o,
48
+ output AuxType aux_o,
49
+ // Output handshake
50
+ output logic out_valid_o,
51
+ input logic out_ready_i,
52
+ // Indication of valid data in flight
53
+ output logic busy_o
54
+ );
55
+
56
+ // ----------
57
+ // Constants
58
+ // ----------
59
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(FpFormat);
60
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(FpFormat);
61
+ localparam int unsigned BIAS = fpnew_pkg::bias(FpFormat);
62
+ // Precision bits 'p' include the implicit bit
63
+ localparam int unsigned PRECISION_BITS = MAN_BITS + 1;
64
+ // The lower 2p+3 bits of the internal FMA result will be needed for leading-zero detection
65
+ localparam int unsigned LOWER_SUM_WIDTH = 2 * PRECISION_BITS + 3;
66
+ localparam int unsigned LZC_RESULT_WIDTH = $clog2(LOWER_SUM_WIDTH);
67
+ // Internal exponent width of FMA must accomodate all meaningful exponent values in order to avoid
68
+ // datapath leakage. This is either given by the exponent bits or the width of the LZC result.
69
+ // In most reasonable FP formats the internal exponent will be wider than the LZC result.
70
+ localparam int unsigned EXP_WIDTH = unsigned'(fpnew_pkg::maximum(EXP_BITS + 2, LZC_RESULT_WIDTH));
71
+ // Shift amount width: maximum internal mantissa size is 3p+4 bits
72
+ localparam int unsigned SHIFT_AMOUNT_WIDTH = $clog2(3 * PRECISION_BITS + 5);
73
+ // Pipelines
74
+ localparam NUM_INP_REGS = PipeConfig == fpnew_pkg::BEFORE
75
+ ? NumPipeRegs
76
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
77
+ ? ((NumPipeRegs + 1) / 3) // Second to get distributed regs
78
+ : 0); // no regs here otherwise
79
+ localparam NUM_MID_REGS = PipeConfig == fpnew_pkg::INSIDE
80
+ ? NumPipeRegs
81
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
82
+ ? ((NumPipeRegs + 2) / 3) // First to get distributed regs
83
+ : 0); // no regs here otherwise
84
+ localparam NUM_OUT_REGS = PipeConfig == fpnew_pkg::AFTER
85
+ ? NumPipeRegs
86
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
87
+ ? (NumPipeRegs / 3) // Last to get distributed regs
88
+ : 0); // no regs here otherwise
89
+
90
+ // ----------------
91
+ // Type definition
92
+ // ----------------
93
+ typedef struct packed {
94
+ logic sign;
95
+ logic [EXP_BITS-1:0] exponent;
96
+ logic [MAN_BITS-1:0] mantissa;
97
+ } fp_t;
98
+
99
+ // ---------------
100
+ // Input pipeline
101
+ // ---------------
102
+ // Input pipeline signals, index i holds signal after i register stages
103
+ logic [0:NUM_INP_REGS][2:0][WIDTH-1:0] inp_pipe_operands_q;
104
+ logic [0:NUM_INP_REGS][2:0] inp_pipe_is_boxed_q;
105
+ fpnew_pkg::roundmode_e [0:NUM_INP_REGS] inp_pipe_rnd_mode_q;
106
+ fpnew_pkg::operation_e [0:NUM_INP_REGS] inp_pipe_op_q;
107
+ logic [0:NUM_INP_REGS] inp_pipe_op_mod_q;
108
+ TagType [0:NUM_INP_REGS] inp_pipe_tag_q;
109
+ logic [0:NUM_INP_REGS] inp_pipe_mask_q;
110
+ AuxType [0:NUM_INP_REGS] inp_pipe_aux_q;
111
+ logic [0:NUM_INP_REGS] inp_pipe_valid_q;
112
+ // Ready signal is combinatorial for all stages
113
+ logic [0:NUM_INP_REGS] inp_pipe_ready;
114
+
115
+ // Input stage: First element of pipeline is taken from inputs
116
+ assign inp_pipe_operands_q[0] = operands_i;
117
+ assign inp_pipe_is_boxed_q[0] = is_boxed_i;
118
+ assign inp_pipe_rnd_mode_q[0] = rnd_mode_i;
119
+ assign inp_pipe_op_q[0] = op_i;
120
+ assign inp_pipe_op_mod_q[0] = op_mod_i;
121
+ assign inp_pipe_tag_q[0] = tag_i;
122
+ assign inp_pipe_mask_q[0] = mask_i;
123
+ assign inp_pipe_aux_q[0] = aux_i;
124
+ assign inp_pipe_valid_q[0] = in_valid_i;
125
+ // Input stage: Propagate pipeline ready signal to updtream circuitry
126
+ assign in_ready_o = inp_pipe_ready[0];
127
+ // Generate the register stages
128
+ for (genvar i = 0; i < NUM_INP_REGS; i++) begin : gen_input_pipeline
129
+ // Internal register enable for this stage
130
+ logic reg_ena;
131
+ // Determine the ready signal of the current stage - advance the pipeline:
132
+ // 1. if the next stage is ready for our data
133
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
134
+ assign inp_pipe_ready[i] = inp_pipe_ready[i+1] | ~inp_pipe_valid_q[i+1];
135
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
136
+ `FFLARNC(inp_pipe_valid_q[i+1], inp_pipe_valid_q[i], inp_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
137
+ // Enable register if pipleine ready and a valid data item is present
138
+ assign reg_ena = inp_pipe_ready[i] & inp_pipe_valid_q[i];
139
+ // Generate the pipeline registers within the stages, use enable-registers
140
+ `FFL(inp_pipe_operands_q[i+1], inp_pipe_operands_q[i], reg_ena, '0)
141
+ `FFL(inp_pipe_is_boxed_q[i+1], inp_pipe_is_boxed_q[i], reg_ena, '0)
142
+ `FFL(inp_pipe_rnd_mode_q[i+1], inp_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
143
+ `FFL(inp_pipe_op_q[i+1], inp_pipe_op_q[i], reg_ena, fpnew_pkg::FMADD)
144
+ `FFL(inp_pipe_op_mod_q[i+1], inp_pipe_op_mod_q[i], reg_ena, '0)
145
+ `FFL(inp_pipe_tag_q[i+1], inp_pipe_tag_q[i], reg_ena, TagType'('0))
146
+ `FFL(inp_pipe_mask_q[i+1], inp_pipe_mask_q[i], reg_ena, '0)
147
+ `FFL(inp_pipe_aux_q[i+1], inp_pipe_aux_q[i], reg_ena, AuxType'('0))
148
+ end
149
+
150
+ // -----------------
151
+ // Input processing
152
+ // -----------------
153
+ fpnew_pkg::fp_info_t [2:0] info_q;
154
+
155
+ // Classify input
156
+ fpnew_classifier #(
157
+ .FpFormat ( FpFormat ),
158
+ .NumOperands ( 3 )
159
+ ) i_class_inputs (
160
+ .operands_i ( inp_pipe_operands_q[NUM_INP_REGS] ),
161
+ .is_boxed_i ( inp_pipe_is_boxed_q[NUM_INP_REGS] ),
162
+ .info_o ( info_q )
163
+ );
164
+
165
+ fp_t operand_a, operand_b, operand_c;
166
+ fpnew_pkg::fp_info_t info_a, info_b, info_c;
167
+
168
+ // Operation selection and operand adjustment
169
+ // | \c op_q | \c op_mod_q | Operation Adjustment
170
+ // |:--------:|:-----------:|---------------------
171
+ // | FMADD | \c 0 | FMADD: none
172
+ // | FMADD | \c 1 | FMSUB: Invert sign of operand C
173
+ // | FNMSUB | \c 0 | FNMSUB: Invert sign of operand A
174
+ // | FNMSUB | \c 1 | FNMADD: Invert sign of operands A and C
175
+ // | ADD | \c 0 | ADD: Set operand A to +1.0
176
+ // | ADD | \c 1 | SUB: Set operand A to +1.0, invert sign of operand C
177
+ // | MUL | \c 0 | MUL: Set operand C to +0.0 or -0.0 depending on the rounding mode
178
+ // | *others* | \c - | *invalid*
179
+ // \note \c op_mod_q always inverts the sign of the addend.
180
+ always_comb begin : op_select
181
+
182
+ // Default assignments - packing-order-agnostic
183
+ operand_a = inp_pipe_operands_q[NUM_INP_REGS][0];
184
+ operand_b = inp_pipe_operands_q[NUM_INP_REGS][1];
185
+ operand_c = inp_pipe_operands_q[NUM_INP_REGS][2];
186
+ info_a = info_q[0];
187
+ info_b = info_q[1];
188
+ info_c = info_q[2];
189
+
190
+ // op_mod_q inverts sign of operand C
191
+ operand_c.sign = operand_c.sign ^ inp_pipe_op_mod_q[NUM_INP_REGS];
192
+
193
+ unique case (inp_pipe_op_q[NUM_INP_REGS])
194
+ fpnew_pkg::FMADD: ; // do nothing
195
+ fpnew_pkg::FNMSUB: operand_a.sign = ~operand_a.sign; // invert sign of product
196
+ fpnew_pkg::ADD: begin // Set multiplicand to +1
197
+ operand_a = '{sign: 1'b0, exponent: BIAS, mantissa: '0};
198
+ info_a = '{is_normal: 1'b1, is_boxed: 1'b1, default: 1'b0}; //normal, boxed value.
199
+ end
200
+ fpnew_pkg::MUL: begin // Set addend to +0 or -0, depending whether the rounding mode is RDN
201
+ if (inp_pipe_rnd_mode_q[NUM_INP_REGS] == fpnew_pkg::RDN)
202
+ operand_c = '{sign: 1'b0, exponent: '0, mantissa: '0};
203
+ else
204
+ operand_c = '{sign: 1'b1, exponent: '0, mantissa: '0};
205
+ info_c = '{is_zero: 1'b1, is_boxed: 1'b1, default: 1'b0}; //zero, boxed value.
206
+ end
207
+ default: begin // propagate don't cares
208
+ operand_a = '{default: fpnew_pkg::DONT_CARE};
209
+ operand_b = '{default: fpnew_pkg::DONT_CARE};
210
+ operand_c = '{default: fpnew_pkg::DONT_CARE};
211
+ info_a = '{default: fpnew_pkg::DONT_CARE};
212
+ info_b = '{default: fpnew_pkg::DONT_CARE};
213
+ info_c = '{default: fpnew_pkg::DONT_CARE};
214
+ end
215
+ endcase
216
+ end
217
+
218
+ // ---------------------
219
+ // Input classification
220
+ // ---------------------
221
+ logic any_operand_inf;
222
+ logic any_operand_nan;
223
+ logic signalling_nan;
224
+ logic effective_subtraction;
225
+ logic tentative_sign;
226
+
227
+ // Reduction for special case handling
228
+ assign any_operand_inf = (| {info_a.is_inf, info_b.is_inf, info_c.is_inf});
229
+ assign any_operand_nan = (| {info_a.is_nan, info_b.is_nan, info_c.is_nan});
230
+ assign signalling_nan = (| {info_a.is_signalling, info_b.is_signalling, info_c.is_signalling});
231
+ // Effective subtraction in FMA occurs when product and addend signs differ
232
+ assign effective_subtraction = operand_a.sign ^ operand_b.sign ^ operand_c.sign;
233
+ // The tentative sign of the FMA shall be the sign of the product
234
+ assign tentative_sign = operand_a.sign ^ operand_b.sign;
235
+
236
+ // ----------------------
237
+ // Special case handling
238
+ // ----------------------
239
+ fp_t special_result;
240
+ fpnew_pkg::status_t special_status;
241
+ logic result_is_special;
242
+
243
+ always_comb begin : special_cases
244
+ // Default assignments
245
+ special_result = '{sign: 1'b0, exponent: '1, mantissa: 2**(MAN_BITS-1)}; // canonical qNaN
246
+ special_status = '0;
247
+ result_is_special = 1'b0;
248
+
249
+ // Handle potentially mixed nan & infinity input => important for the case where infinity and
250
+ // zero are multiplied and added to a qnan.
251
+ // RISC-V mandates raising the NV exception in these cases:
252
+ // (inf * 0) + c or (0 * inf) + c INVALID, no matter c (even quiet NaNs)
253
+ if ((info_a.is_inf && info_b.is_zero) || (info_a.is_zero && info_b.is_inf)) begin
254
+ result_is_special = 1'b1; // bypass FMA, output is the canonical qNaN
255
+ special_status.NV = 1'b1; // invalid operation
256
+ // NaN Inputs cause canonical quiet NaN at the output and maybe invalid OP
257
+ end else if (any_operand_nan) begin
258
+ result_is_special = 1'b1; // bypass FMA, output is the canonical qNaN
259
+ special_status.NV = signalling_nan; // raise the invalid operation flag if signalling
260
+ // Special cases involving infinity
261
+ end else if (any_operand_inf) begin
262
+ result_is_special = 1'b1; // bypass FMA
263
+ // Effective addition of opposite infinities (±inf - ±inf) is invalid!
264
+ if ((info_a.is_inf || info_b.is_inf) && info_c.is_inf && effective_subtraction)
265
+ special_status.NV = 1'b1; // invalid operation
266
+ // Handle cases where output will be inf because of inf product input
267
+ else if (info_a.is_inf || info_b.is_inf) begin
268
+ // Result is infinity with the sign of the product
269
+ special_result = '{sign: operand_a.sign ^ operand_b.sign, exponent: '1, mantissa: '0};
270
+ // Handle cases where the addend is inf
271
+ end else if (info_c.is_inf) begin
272
+ // Result is inifinity with sign of the addend (= operand_c)
273
+ special_result = '{sign: operand_c.sign, exponent: '1, mantissa: '0};
274
+ end
275
+ end
276
+ end
277
+
278
+ // ---------------------------
279
+ // Initial exponent data path
280
+ // ---------------------------
281
+ logic signed [EXP_WIDTH-1:0] exponent_a, exponent_b, exponent_c;
282
+ logic signed [EXP_WIDTH-1:0] exponent_addend, exponent_product, exponent_difference;
283
+ logic signed [EXP_WIDTH-1:0] tentative_exponent;
284
+
285
+ // Zero-extend exponents into signed container - implicit width extension
286
+ assign exponent_a = signed'({1'b0, operand_a.exponent});
287
+ assign exponent_b = signed'({1'b0, operand_b.exponent});
288
+ assign exponent_c = signed'({1'b0, operand_c.exponent});
289
+
290
+ // Calculate internal exponents from encoded values. Real exponents are (ex = Ex - bias + 1 - nx)
291
+ // with Ex the encoded exponent and nx the implicit bit. Internal exponents stay biased.
292
+ assign exponent_addend = signed'(exponent_c + $signed({1'b0, ~info_c.is_normal})); // 0 as subnorm
293
+ // Biased product exponent is the sum of encoded exponents minus the bias.
294
+ assign exponent_product = (info_a.is_zero || info_b.is_zero)
295
+ ? 2 - signed'(BIAS) // in case the product is zero, set minimum exp.
296
+ : signed'(exponent_a + info_a.is_subnormal
297
+ + exponent_b + info_b.is_subnormal
298
+ - signed'(BIAS));
299
+ // Exponent difference is the addend exponent minus the product exponent
300
+ assign exponent_difference = exponent_addend - exponent_product;
301
+ // The tentative exponent will be the larger of the product or addend exponent
302
+ assign tentative_exponent = (exponent_difference > 0) ? exponent_addend : exponent_product;
303
+
304
+ // Shift amount for addend based on exponents (unsigned as only right shifts)
305
+ logic [SHIFT_AMOUNT_WIDTH-1:0] addend_shamt;
306
+
307
+ always_comb begin : addend_shift_amount
308
+ // Product-anchored case, saturated shift (addend is only in the sticky bit)
309
+ if (exponent_difference <= signed'(-2 * PRECISION_BITS - 1))
310
+ addend_shamt = 3 * PRECISION_BITS + 4;
311
+ // Addend and product will have mutual bits to add
312
+ else if (exponent_difference <= signed'(PRECISION_BITS + 2))
313
+ addend_shamt = unsigned'(signed'(PRECISION_BITS) + 3 - exponent_difference);
314
+ // Addend-anchored case, saturated shift (product is only in the sticky bit)
315
+ else
316
+ addend_shamt = 0;
317
+ end
318
+
319
+ // ------------------
320
+ // Product data path
321
+ // ------------------
322
+ logic [PRECISION_BITS-1:0] mantissa_a, mantissa_b, mantissa_c;
323
+ logic [2*PRECISION_BITS-1:0] product; // the p*p product is 2p bits wide
324
+ logic [3*PRECISION_BITS+3:0] product_shifted; // addends are 3p+4 bit wide (including G/R)
325
+
326
+ // Add implicit bits to mantissae
327
+ assign mantissa_a = {info_a.is_normal, operand_a.mantissa};
328
+ assign mantissa_b = {info_b.is_normal, operand_b.mantissa};
329
+ assign mantissa_c = {info_c.is_normal, operand_c.mantissa};
330
+
331
+ // Mantissa multiplier (a*b)
332
+ assign product = mantissa_a * mantissa_b;
333
+
334
+ // Product is placed into a 3p+4 bit wide vector, padded with 2 bits for round and sticky:
335
+ // | 000...000 | product | RS |
336
+ // <- p+2 -> <- 2p -> < 2>
337
+ assign product_shifted = product << 2; // constant shift
338
+
339
+ // -----------------
340
+ // Addend data path
341
+ // -----------------
342
+ logic [3*PRECISION_BITS+3:0] addend_after_shift; // upper 3p+4 bits are needed to go on
343
+ logic [PRECISION_BITS-1:0] addend_sticky_bits; // up to p bit of shifted addend are sticky
344
+ logic sticky_before_add; // they are compressed into a single sticky bit
345
+ logic [3*PRECISION_BITS+3:0] addend_shifted; // addends are 3p+4 bit wide (including G/R)
346
+ logic inject_carry_in; // inject carry for subtractions if needed
347
+
348
+ // In parallel, the addend is right-shifted according to the exponent difference. Up to p bits
349
+ // are shifted out and compressed into a sticky bit.
350
+ // BEFORE THE SHIFT:
351
+ // | mantissa_c | 000..000 |
352
+ // <- p -> <- 3p+4 ->
353
+ // AFTER THE SHIFT:
354
+ // | 000..........000 | mantissa_c | 000...............0GR | sticky bits |
355
+ // <- addend_shamt -> <- p -> <- 2p+4-addend_shamt -> <- up to p ->
356
+ assign {addend_after_shift, addend_sticky_bits} =
357
+ (mantissa_c << (3 * PRECISION_BITS + 4)) >> addend_shamt;
358
+
359
+ assign sticky_before_add = (| addend_sticky_bits);
360
+ // assign addend_after_shift[0] = sticky_before_add;
361
+
362
+ // In case of a subtraction, the addend is inverted
363
+ assign addend_shifted = (effective_subtraction) ? ~addend_after_shift : addend_after_shift;
364
+ assign inject_carry_in = effective_subtraction & ~sticky_before_add;
365
+
366
+ // ------
367
+ // Adder
368
+ // ------
369
+ logic [3*PRECISION_BITS+4:0] sum_raw; // added one bit for the carry
370
+ logic sum_carry; // observe carry bit from sum for sign fixing
371
+ logic [3*PRECISION_BITS+3:0] sum; // discard carry as sum won't overflow
372
+ logic final_sign;
373
+
374
+ //Mantissa adder (ab+c). In normal addition, it cannot overflow.
375
+ assign sum_raw = product_shifted + addend_shifted + inject_carry_in;
376
+ assign sum_carry = sum_raw[3*PRECISION_BITS+4];
377
+
378
+ // Complement negative sum (can only happen in subtraction -> overflows for positive results)
379
+ assign sum = (effective_subtraction && ~sum_carry) ? -sum_raw : sum_raw;
380
+
381
+ // In case of a mispredicted subtraction result, do a sign flip
382
+ assign final_sign = (effective_subtraction && (sum_carry == tentative_sign))
383
+ ? 1'b1
384
+ : (effective_subtraction ? 1'b0 : tentative_sign);
385
+
386
+ // ---------------
387
+ // Internal pipeline
388
+ // ---------------
389
+ // Pipeline output signals as non-arrays
390
+ logic effective_subtraction_q;
391
+ logic signed [EXP_WIDTH-1:0] exponent_product_q;
392
+ logic signed [EXP_WIDTH-1:0] exponent_difference_q;
393
+ logic signed [EXP_WIDTH-1:0] tentative_exponent_q;
394
+ logic [SHIFT_AMOUNT_WIDTH-1:0] addend_shamt_q;
395
+ logic sticky_before_add_q;
396
+ logic [3*PRECISION_BITS+3:0] sum_q;
397
+ logic final_sign_q;
398
+ fpnew_pkg::roundmode_e rnd_mode_q;
399
+ logic result_is_special_q;
400
+ fp_t special_result_q;
401
+ fpnew_pkg::status_t special_status_q;
402
+ // Internal pipeline signals, index i holds signal after i register stages
403
+ logic [0:NUM_MID_REGS] mid_pipe_eff_sub_q;
404
+ logic signed [0:NUM_MID_REGS][EXP_WIDTH-1:0] mid_pipe_exp_prod_q;
405
+ logic signed [0:NUM_MID_REGS][EXP_WIDTH-1:0] mid_pipe_exp_diff_q;
406
+ logic signed [0:NUM_MID_REGS][EXP_WIDTH-1:0] mid_pipe_tent_exp_q;
407
+ logic [0:NUM_MID_REGS][SHIFT_AMOUNT_WIDTH-1:0] mid_pipe_add_shamt_q;
408
+ logic [0:NUM_MID_REGS] mid_pipe_sticky_q;
409
+ logic [0:NUM_MID_REGS][3*PRECISION_BITS+3:0] mid_pipe_sum_q;
410
+ logic [0:NUM_MID_REGS] mid_pipe_final_sign_q;
411
+ fpnew_pkg::roundmode_e [0:NUM_MID_REGS] mid_pipe_rnd_mode_q;
412
+ logic [0:NUM_MID_REGS] mid_pipe_res_is_spec_q;
413
+ fp_t [0:NUM_MID_REGS] mid_pipe_spec_res_q;
414
+ fpnew_pkg::status_t [0:NUM_MID_REGS] mid_pipe_spec_stat_q;
415
+ TagType [0:NUM_MID_REGS] mid_pipe_tag_q;
416
+ logic [0:NUM_MID_REGS] mid_pipe_mask_q;
417
+ AuxType [0:NUM_MID_REGS] mid_pipe_aux_q;
418
+ logic [0:NUM_MID_REGS] mid_pipe_valid_q;
419
+ // Ready signal is combinatorial for all stages
420
+ logic [0:NUM_MID_REGS] mid_pipe_ready;
421
+
422
+ // Input stage: First element of pipeline is taken from upstream logic
423
+ assign mid_pipe_eff_sub_q[0] = effective_subtraction;
424
+ assign mid_pipe_exp_prod_q[0] = exponent_product;
425
+ assign mid_pipe_exp_diff_q[0] = exponent_difference;
426
+ assign mid_pipe_tent_exp_q[0] = tentative_exponent;
427
+ assign mid_pipe_add_shamt_q[0] = addend_shamt;
428
+ assign mid_pipe_sticky_q[0] = sticky_before_add;
429
+ assign mid_pipe_sum_q[0] = sum;
430
+ assign mid_pipe_final_sign_q[0] = final_sign;
431
+ assign mid_pipe_rnd_mode_q[0] = inp_pipe_rnd_mode_q[NUM_INP_REGS];
432
+ assign mid_pipe_res_is_spec_q[0] = result_is_special;
433
+ assign mid_pipe_spec_res_q[0] = special_result;
434
+ assign mid_pipe_spec_stat_q[0] = special_status;
435
+ assign mid_pipe_tag_q[0] = inp_pipe_tag_q[NUM_INP_REGS];
436
+ assign mid_pipe_mask_q[0] = inp_pipe_mask_q[NUM_INP_REGS];
437
+ assign mid_pipe_aux_q[0] = inp_pipe_aux_q[NUM_INP_REGS];
438
+ assign mid_pipe_valid_q[0] = inp_pipe_valid_q[NUM_INP_REGS];
439
+ // Input stage: Propagate pipeline ready signal to input pipe
440
+ assign inp_pipe_ready[NUM_INP_REGS] = mid_pipe_ready[0];
441
+
442
+ // Generate the register stages
443
+ for (genvar i = 0; i < NUM_MID_REGS; i++) begin : gen_inside_pipeline
444
+ // Internal register enable for this stage
445
+ logic reg_ena;
446
+ // Determine the ready signal of the current stage - advance the pipeline:
447
+ // 1. if the next stage is ready for our data
448
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
449
+ assign mid_pipe_ready[i] = mid_pipe_ready[i+1] | ~mid_pipe_valid_q[i+1];
450
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
451
+ `FFLARNC(mid_pipe_valid_q[i+1], mid_pipe_valid_q[i], mid_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
452
+ // Enable register if pipleine ready and a valid data item is present
453
+ assign reg_ena = mid_pipe_ready[i] & mid_pipe_valid_q[i];
454
+ // Generate the pipeline registers within the stages, use enable-registers
455
+ `FFL(mid_pipe_eff_sub_q[i+1], mid_pipe_eff_sub_q[i], reg_ena, '0)
456
+ `FFL(mid_pipe_exp_prod_q[i+1], mid_pipe_exp_prod_q[i], reg_ena, '0)
457
+ `FFL(mid_pipe_exp_diff_q[i+1], mid_pipe_exp_diff_q[i], reg_ena, '0)
458
+ `FFL(mid_pipe_tent_exp_q[i+1], mid_pipe_tent_exp_q[i], reg_ena, '0)
459
+ `FFL(mid_pipe_add_shamt_q[i+1], mid_pipe_add_shamt_q[i], reg_ena, '0)
460
+ `FFL(mid_pipe_sticky_q[i+1], mid_pipe_sticky_q[i], reg_ena, '0)
461
+ `FFL(mid_pipe_sum_q[i+1], mid_pipe_sum_q[i], reg_ena, '0)
462
+ `FFL(mid_pipe_final_sign_q[i+1], mid_pipe_final_sign_q[i], reg_ena, '0)
463
+ `FFL(mid_pipe_rnd_mode_q[i+1], mid_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
464
+ `FFL(mid_pipe_res_is_spec_q[i+1], mid_pipe_res_is_spec_q[i], reg_ena, '0)
465
+ `FFL(mid_pipe_spec_res_q[i+1], mid_pipe_spec_res_q[i], reg_ena, '0)
466
+ `FFL(mid_pipe_spec_stat_q[i+1], mid_pipe_spec_stat_q[i], reg_ena, '0)
467
+ `FFL(mid_pipe_tag_q[i+1], mid_pipe_tag_q[i], reg_ena, TagType'('0))
468
+ `FFL(mid_pipe_mask_q[i+1], mid_pipe_mask_q[i], reg_ena, '0)
469
+ `FFL(mid_pipe_aux_q[i+1], mid_pipe_aux_q[i], reg_ena, AuxType'('0))
470
+ end
471
+ // Output stage: assign selected pipe outputs to signals for later use
472
+ assign effective_subtraction_q = mid_pipe_eff_sub_q[NUM_MID_REGS];
473
+ assign exponent_product_q = mid_pipe_exp_prod_q[NUM_MID_REGS];
474
+ assign exponent_difference_q = mid_pipe_exp_diff_q[NUM_MID_REGS];
475
+ assign tentative_exponent_q = mid_pipe_tent_exp_q[NUM_MID_REGS];
476
+ assign addend_shamt_q = mid_pipe_add_shamt_q[NUM_MID_REGS];
477
+ assign sticky_before_add_q = mid_pipe_sticky_q[NUM_MID_REGS];
478
+ assign sum_q = mid_pipe_sum_q[NUM_MID_REGS];
479
+ assign final_sign_q = mid_pipe_final_sign_q[NUM_MID_REGS];
480
+ assign rnd_mode_q = mid_pipe_rnd_mode_q[NUM_MID_REGS];
481
+ assign result_is_special_q = mid_pipe_res_is_spec_q[NUM_MID_REGS];
482
+ assign special_result_q = mid_pipe_spec_res_q[NUM_MID_REGS];
483
+ assign special_status_q = mid_pipe_spec_stat_q[NUM_MID_REGS];
484
+
485
+ // --------------
486
+ // Normalization
487
+ // --------------
488
+ logic [LOWER_SUM_WIDTH-1:0] sum_lower; // lower 2p+3 bits of sum are searched
489
+ logic [LZC_RESULT_WIDTH-1:0] leading_zero_count; // the number of leading zeroes
490
+ logic signed [LZC_RESULT_WIDTH:0] leading_zero_count_sgn; // signed leading-zero count
491
+ logic lzc_zeroes; // in case only zeroes found
492
+
493
+ logic [SHIFT_AMOUNT_WIDTH-1:0] norm_shamt; // Normalization shift amount
494
+ logic signed [EXP_WIDTH-1:0] normalized_exponent;
495
+
496
+ logic [3*PRECISION_BITS+4:0] sum_shifted; // result after first normalization shift
497
+ logic [PRECISION_BITS:0] final_mantissa; // final mantissa before rounding with round bit
498
+ logic [2*PRECISION_BITS+2:0] sum_sticky_bits; // remaining 2p+3 sticky bits after normalization
499
+ logic sticky_after_norm; // sticky bit after normalization
500
+
501
+ logic signed [EXP_WIDTH-1:0] final_exponent;
502
+
503
+ assign sum_lower = sum_q[LOWER_SUM_WIDTH-1:0];
504
+
505
+ // Leading zero counter for cancellations
506
+ lzc #(
507
+ .WIDTH ( LOWER_SUM_WIDTH ),
508
+ .MODE ( 1 ) // MODE = 1 counts leading zeroes
509
+ ) i_lzc (
510
+ .in_i ( sum_lower ),
511
+ .cnt_o ( leading_zero_count ),
512
+ .empty_o ( lzc_zeroes )
513
+ );
514
+
515
+ assign leading_zero_count_sgn = signed'({1'b0, leading_zero_count});
516
+
517
+ // Normalization shift amount based on exponents and LZC (unsigned as only left shifts)
518
+ always_comb begin : norm_shift_amount
519
+ // Product-anchored case or cancellations require LZC
520
+ if ((exponent_difference_q <= 0) || (effective_subtraction_q && (exponent_difference_q <= 2))) begin
521
+ // Normal result (biased exponent > 0 and not a zero)
522
+ if ((exponent_product_q - leading_zero_count_sgn + 1 >= 0) && !lzc_zeroes) begin
523
+ // Undo initial product shift, remove the counted zeroes
524
+ norm_shamt = PRECISION_BITS + 2 + leading_zero_count;
525
+ normalized_exponent = exponent_product_q - leading_zero_count_sgn + 1; // account for shift
526
+ // Subnormal result
527
+ end else begin
528
+ // Cap the shift distance to align mantissa with minimum exponent
529
+ norm_shamt = unsigned'(signed'(PRECISION_BITS) + 2 + exponent_product_q);
530
+ normalized_exponent = 0; // subnormals encoded as 0
531
+ end
532
+ // Addend-anchored case
533
+ end else begin
534
+ norm_shamt = addend_shamt_q; // Undo the initial shift
535
+ normalized_exponent = tentative_exponent_q;
536
+ end
537
+ end
538
+
539
+ // Do the large normalization shift
540
+ assign sum_shifted = sum_q << norm_shamt;
541
+
542
+ // The addend-anchored case needs a 1-bit normalization since the leading-one can be to the left
543
+ // or right of the (non-carry) MSB of the sum.
544
+ always_comb begin : small_norm
545
+ // Default assignment, discarding carry bit
546
+ {final_mantissa, sum_sticky_bits} = sum_shifted;
547
+ final_exponent = normalized_exponent;
548
+
549
+ // The normalized sum has overflown, align right and fix exponent
550
+ if (sum_shifted[3*PRECISION_BITS+4]) begin // check the carry bit
551
+ {final_mantissa, sum_sticky_bits} = sum_shifted >> 1;
552
+ final_exponent = normalized_exponent + 1;
553
+ // The normalized sum is normal, nothing to do
554
+ end else if (sum_shifted[3*PRECISION_BITS+3]) begin // check the sum MSB
555
+ // do nothing
556
+ // The normalized sum is still denormal, align left - unless the result is not already subnormal
557
+ end else if (normalized_exponent > 1) begin
558
+ {final_mantissa, sum_sticky_bits} = sum_shifted << 1;
559
+ final_exponent = normalized_exponent - 1;
560
+ // Otherwise we're denormal
561
+ end else begin
562
+ final_exponent = '0;
563
+ end
564
+ end
565
+
566
+ // Update the sticky bit with the shifted-out bits
567
+ assign sticky_after_norm = (| {sum_sticky_bits}) | sticky_before_add_q;
568
+
569
+ // ----------------------------
570
+ // Rounding and classification
571
+ // ----------------------------
572
+ logic pre_round_sign;
573
+ logic [EXP_BITS-1:0] pre_round_exponent;
574
+ logic [MAN_BITS-1:0] pre_round_mantissa;
575
+ logic [EXP_BITS+MAN_BITS-1:0] pre_round_abs; // absolute value of result before rounding
576
+ logic [1:0] round_sticky_bits;
577
+
578
+ logic of_before_round, of_after_round; // overflow
579
+ logic uf_before_round, uf_after_round; // underflow
580
+ logic result_zero;
581
+
582
+ logic rounded_sign;
583
+ logic [EXP_BITS+MAN_BITS-1:0] rounded_abs; // absolute value of result after rounding
584
+
585
+ // Classification before round. RISC-V mandates checking underflow AFTER rounding!
586
+ assign of_before_round = final_exponent >= 2**(EXP_BITS)-1; // infinity exponent is all ones
587
+ assign uf_before_round = final_exponent == 0; // exponent for subnormals capped to 0
588
+
589
+ // Assemble result before rounding. In case of overflow, the largest normal value is set.
590
+ assign pre_round_sign = final_sign_q;
591
+ assign pre_round_exponent = (of_before_round) ? 2**EXP_BITS-2 : unsigned'(final_exponent[EXP_BITS-1:0]);
592
+ assign pre_round_mantissa = (of_before_round) ? '1 : final_mantissa[MAN_BITS:1]; // bit 0 is R bit
593
+ assign pre_round_abs = {pre_round_exponent, pre_round_mantissa};
594
+
595
+ // In case of overflow, the round and sticky bits are set for proper rounding
596
+ assign round_sticky_bits = (of_before_round) ? 2'b11 : {final_mantissa[0], sticky_after_norm};
597
+
598
+ // Perform the rounding
599
+ fpnew_rounding #(
600
+ .AbsWidth ( EXP_BITS + MAN_BITS )
601
+ ) i_fpnew_rounding (
602
+ .abs_value_i ( pre_round_abs ),
603
+ .sign_i ( pre_round_sign ),
604
+ .round_sticky_bits_i ( round_sticky_bits ),
605
+ .rnd_mode_i ( rnd_mode_q ),
606
+ .effective_subtraction_i ( effective_subtraction_q ),
607
+ .abs_rounded_o ( rounded_abs ),
608
+ .sign_o ( rounded_sign ),
609
+ .exact_zero_o ( result_zero )
610
+ );
611
+
612
+ // Classification after rounding
613
+ assign uf_after_round = rounded_abs[EXP_BITS+MAN_BITS-1:MAN_BITS] == '0; // exponent = 0
614
+ assign of_after_round = rounded_abs[EXP_BITS+MAN_BITS-1:MAN_BITS] == '1; // exponent all ones
615
+
616
+ // -----------------
617
+ // Result selection
618
+ // -----------------
619
+ logic [WIDTH-1:0] regular_result;
620
+ fpnew_pkg::status_t regular_status;
621
+
622
+ // Assemble regular result
623
+ assign regular_result = {rounded_sign, rounded_abs};
624
+ assign regular_status.NV = 1'b0; // only valid cases are handled in regular path
625
+ assign regular_status.DZ = 1'b0; // no divisions
626
+ assign regular_status.OF = of_before_round | of_after_round; // rounding can introduce overflow
627
+ assign regular_status.UF = uf_after_round & regular_status.NX; // only inexact results raise UF
628
+ assign regular_status.NX = (| round_sticky_bits) | of_before_round | of_after_round;
629
+
630
+ // Final results for output pipeline
631
+ fp_t result_d;
632
+ fpnew_pkg::status_t status_d;
633
+
634
+ // Select output depending on special case detection
635
+ assign result_d = result_is_special_q ? special_result_q : regular_result;
636
+ assign status_d = result_is_special_q ? special_status_q : regular_status;
637
+
638
+ // ----------------
639
+ // Output Pipeline
640
+ // ----------------
641
+ // Output pipeline signals, index i holds signal after i register stages
642
+ fp_t [0:NUM_OUT_REGS] out_pipe_result_q;
643
+ fpnew_pkg::status_t [0:NUM_OUT_REGS] out_pipe_status_q;
644
+ TagType [0:NUM_OUT_REGS] out_pipe_tag_q;
645
+ logic [0:NUM_OUT_REGS] out_pipe_mask_q;
646
+ AuxType [0:NUM_OUT_REGS] out_pipe_aux_q;
647
+ logic [0:NUM_OUT_REGS] out_pipe_valid_q;
648
+ // Ready signal is combinatorial for all stages
649
+ logic [0:NUM_OUT_REGS] out_pipe_ready;
650
+
651
+ // Input stage: First element of pipeline is taken from inputs
652
+ assign out_pipe_result_q[0] = result_d;
653
+ assign out_pipe_status_q[0] = status_d;
654
+ assign out_pipe_tag_q[0] = mid_pipe_tag_q[NUM_MID_REGS];
655
+ assign out_pipe_mask_q[0] = mid_pipe_mask_q[NUM_MID_REGS];
656
+ assign out_pipe_aux_q[0] = mid_pipe_aux_q[NUM_MID_REGS];
657
+ assign out_pipe_valid_q[0] = mid_pipe_valid_q[NUM_MID_REGS];
658
+ // Input stage: Propagate pipeline ready signal to inside pipe
659
+ assign mid_pipe_ready[NUM_MID_REGS] = out_pipe_ready[0];
660
+ // Generate the register stages
661
+ for (genvar i = 0; i < NUM_OUT_REGS; i++) begin : gen_output_pipeline
662
+ // Internal register enable for this stage
663
+ logic reg_ena;
664
+ // Determine the ready signal of the current stage - advance the pipeline:
665
+ // 1. if the next stage is ready for our data
666
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
667
+ assign out_pipe_ready[i] = out_pipe_ready[i+1] | ~out_pipe_valid_q[i+1];
668
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
669
+ `FFLARNC(out_pipe_valid_q[i+1], out_pipe_valid_q[i], out_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
670
+ // Enable register if pipleine ready and a valid data item is present
671
+ assign reg_ena = out_pipe_ready[i] & out_pipe_valid_q[i];
672
+ // Generate the pipeline registers within the stages, use enable-registers
673
+ `FFL(out_pipe_result_q[i+1], out_pipe_result_q[i], reg_ena, '0)
674
+ `FFL(out_pipe_status_q[i+1], out_pipe_status_q[i], reg_ena, '0)
675
+ `FFL(out_pipe_tag_q[i+1], out_pipe_tag_q[i], reg_ena, TagType'('0))
676
+ `FFL(out_pipe_mask_q[i+1], out_pipe_mask_q[i], reg_ena, '0)
677
+ `FFL(out_pipe_aux_q[i+1], out_pipe_aux_q[i], reg_ena, AuxType'('0))
678
+ end
679
+ // Output stage: Ready travels backwards from output side, driven by downstream circuitry
680
+ assign out_pipe_ready[NUM_OUT_REGS] = out_ready_i;
681
+ // Output stage: assign module outputs
682
+ assign result_o = out_pipe_result_q[NUM_OUT_REGS];
683
+ assign status_o = out_pipe_status_q[NUM_OUT_REGS];
684
+ assign extension_bit_o = 1'b1; // always NaN-Box result
685
+ assign tag_o = out_pipe_tag_q[NUM_OUT_REGS];
686
+ assign mask_o = out_pipe_mask_q[NUM_OUT_REGS];
687
+ assign aux_o = out_pipe_aux_q[NUM_OUT_REGS];
688
+ assign out_valid_o = out_pipe_valid_q[NUM_OUT_REGS];
689
+ assign busy_o = (| {inp_pipe_valid_q, mid_pipe_valid_q, out_pipe_valid_q});
690
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_fma_multi.sv ADDED
@@ -0,0 +1,839 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ `include "common_cells/registers.svh"
17
+
18
+ module fpnew_fma_multi #(
19
+ parameter fpnew_pkg::fmt_logic_t FpFmtConfig = '1,
20
+ parameter int unsigned NumPipeRegs = 0,
21
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::BEFORE,
22
+ parameter type TagType = logic,
23
+ parameter type AuxType = logic,
24
+ // Do not change
25
+ localparam int unsigned WIDTH = fpnew_pkg::max_fp_width(FpFmtConfig),
26
+ localparam int unsigned NUM_FORMATS = fpnew_pkg::NUM_FP_FORMATS
27
+ ) (
28
+ input logic clk_i,
29
+ input logic rst_ni,
30
+ // Input signals
31
+ input logic [2:0][WIDTH-1:0] operands_i, // 3 operands
32
+ input logic [NUM_FORMATS-1:0][2:0] is_boxed_i, // 3 operands
33
+ input fpnew_pkg::roundmode_e rnd_mode_i,
34
+ input fpnew_pkg::operation_e op_i,
35
+ input logic op_mod_i,
36
+ input fpnew_pkg::fp_format_e src_fmt_i, // format of the multiplicands
37
+ input fpnew_pkg::fp_format_e dst_fmt_i, // format of the addend and result
38
+ input TagType tag_i,
39
+ input logic mask_i,
40
+ input AuxType aux_i,
41
+ // Input Handshake
42
+ input logic in_valid_i,
43
+ output logic in_ready_o,
44
+ input logic flush_i,
45
+ // Output signals
46
+ output logic [WIDTH-1:0] result_o,
47
+ output fpnew_pkg::status_t status_o,
48
+ output logic extension_bit_o,
49
+ output TagType tag_o,
50
+ output logic mask_o,
51
+ output AuxType aux_o,
52
+ // Output handshake
53
+ output logic out_valid_o,
54
+ input logic out_ready_i,
55
+ // Indication of valid data in flight
56
+ output logic busy_o
57
+ );
58
+
59
+ // ----------
60
+ // Constants
61
+ // ----------
62
+ // The super-format that can hold all formats
63
+ localparam fpnew_pkg::fp_encoding_t SUPER_FORMAT = fpnew_pkg::super_format(FpFmtConfig);
64
+
65
+ localparam int unsigned SUPER_EXP_BITS = SUPER_FORMAT.exp_bits;
66
+ localparam int unsigned SUPER_MAN_BITS = SUPER_FORMAT.man_bits;
67
+
68
+ // Precision bits 'p' include the implicit bit
69
+ localparam int unsigned PRECISION_BITS = SUPER_MAN_BITS + 1;
70
+ // The lower 2p+3 bits of the internal FMA result will be needed for leading-zero detection
71
+ localparam int unsigned LOWER_SUM_WIDTH = 2 * PRECISION_BITS + 3;
72
+ localparam int unsigned LZC_RESULT_WIDTH = $clog2(LOWER_SUM_WIDTH);
73
+ // Internal exponent width of FMA must accomodate all meaningful exponent values in order to avoid
74
+ // datapath leakage. This is either given by the exponent bits or the width of the LZC result.
75
+ // In most reasonable FP formats the internal exponent will be wider than the LZC result.
76
+ localparam int unsigned EXP_WIDTH = fpnew_pkg::maximum(SUPER_EXP_BITS + 2, LZC_RESULT_WIDTH);
77
+ // Shift amount width: maximum internal mantissa size is 3p+4 bits
78
+ localparam int unsigned SHIFT_AMOUNT_WIDTH = $clog2(3 * PRECISION_BITS + 5);
79
+ // Pipelines
80
+ localparam NUM_INP_REGS = PipeConfig == fpnew_pkg::BEFORE
81
+ ? NumPipeRegs
82
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
83
+ ? ((NumPipeRegs + 1) / 3) // Second to get distributed regs
84
+ : 0); // no regs here otherwise
85
+ localparam NUM_MID_REGS = PipeConfig == fpnew_pkg::INSIDE
86
+ ? NumPipeRegs
87
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
88
+ ? ((NumPipeRegs + 2) / 3) // First to get distributed regs
89
+ : 0); // no regs here otherwise
90
+ localparam NUM_OUT_REGS = PipeConfig == fpnew_pkg::AFTER
91
+ ? NumPipeRegs
92
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
93
+ ? (NumPipeRegs / 3) // Last to get distributed regs
94
+ : 0); // no regs here otherwise
95
+
96
+ // ----------------
97
+ // Type definition
98
+ // ----------------
99
+ typedef struct packed {
100
+ logic sign;
101
+ logic [SUPER_EXP_BITS-1:0] exponent;
102
+ logic [SUPER_MAN_BITS-1:0] mantissa;
103
+ } fp_t;
104
+
105
+ // ---------------
106
+ // Input pipeline
107
+ // ---------------
108
+ // Selected pipeline output signals as non-arrays
109
+ logic [2:0][WIDTH-1:0] operands_q;
110
+ fpnew_pkg::fp_format_e src_fmt_q;
111
+ fpnew_pkg::fp_format_e dst_fmt_q;
112
+
113
+ // Input pipeline signals, index i holds signal after i register stages
114
+ logic [0:NUM_INP_REGS][2:0][WIDTH-1:0] inp_pipe_operands_q;
115
+ logic [0:NUM_INP_REGS][NUM_FORMATS-1:0][2:0] inp_pipe_is_boxed_q;
116
+ fpnew_pkg::roundmode_e [0:NUM_INP_REGS] inp_pipe_rnd_mode_q;
117
+ fpnew_pkg::operation_e [0:NUM_INP_REGS] inp_pipe_op_q;
118
+ logic [0:NUM_INP_REGS] inp_pipe_op_mod_q;
119
+ fpnew_pkg::fp_format_e [0:NUM_INP_REGS] inp_pipe_src_fmt_q;
120
+ fpnew_pkg::fp_format_e [0:NUM_INP_REGS] inp_pipe_dst_fmt_q;
121
+ TagType [0:NUM_INP_REGS] inp_pipe_tag_q;
122
+ logic [0:NUM_INP_REGS] inp_pipe_mask_q;
123
+ AuxType [0:NUM_INP_REGS] inp_pipe_aux_q;
124
+ logic [0:NUM_INP_REGS] inp_pipe_valid_q;
125
+ // Ready signal is combinatorial for all stages
126
+ logic [0:NUM_INP_REGS] inp_pipe_ready;
127
+
128
+ // Input stage: First element of pipeline is taken from inputs
129
+ assign inp_pipe_operands_q[0] = operands_i;
130
+ assign inp_pipe_is_boxed_q[0] = is_boxed_i;
131
+ assign inp_pipe_rnd_mode_q[0] = rnd_mode_i;
132
+ assign inp_pipe_op_q[0] = op_i;
133
+ assign inp_pipe_op_mod_q[0] = op_mod_i;
134
+ assign inp_pipe_src_fmt_q[0] = src_fmt_i;
135
+ assign inp_pipe_dst_fmt_q[0] = dst_fmt_i;
136
+ assign inp_pipe_tag_q[0] = tag_i;
137
+ assign inp_pipe_mask_q[0] = mask_i;
138
+ assign inp_pipe_aux_q[0] = aux_i;
139
+ assign inp_pipe_valid_q[0] = in_valid_i;
140
+ // Input stage: Propagate pipeline ready signal to updtream circuitry
141
+ assign in_ready_o = inp_pipe_ready[0];
142
+ // Generate the register stages
143
+ for (genvar i = 0; i < NUM_INP_REGS; i++) begin : gen_input_pipeline
144
+ // Internal register enable for this stage
145
+ logic reg_ena;
146
+ // Determine the ready signal of the current stage - advance the pipeline:
147
+ // 1. if the next stage is ready for our data
148
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
149
+ assign inp_pipe_ready[i] = inp_pipe_ready[i+1] | ~inp_pipe_valid_q[i+1];
150
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
151
+ `FFLARNC(inp_pipe_valid_q[i+1], inp_pipe_valid_q[i], inp_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
152
+ // Enable register if pipleine ready and a valid data item is present
153
+ assign reg_ena = inp_pipe_ready[i] & inp_pipe_valid_q[i];
154
+ // Generate the pipeline registers within the stages, use enable-registers
155
+ `FFL(inp_pipe_operands_q[i+1], inp_pipe_operands_q[i], reg_ena, '0)
156
+ `FFL(inp_pipe_is_boxed_q[i+1], inp_pipe_is_boxed_q[i], reg_ena, '0)
157
+ `FFL(inp_pipe_rnd_mode_q[i+1], inp_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
158
+ `FFL(inp_pipe_op_q[i+1], inp_pipe_op_q[i], reg_ena, fpnew_pkg::FMADD)
159
+ `FFL(inp_pipe_op_mod_q[i+1], inp_pipe_op_mod_q[i], reg_ena, '0)
160
+ `FFL(inp_pipe_src_fmt_q[i+1], inp_pipe_src_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
161
+ `FFL(inp_pipe_dst_fmt_q[i+1], inp_pipe_dst_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
162
+ `FFL(inp_pipe_tag_q[i+1], inp_pipe_tag_q[i], reg_ena, TagType'('0))
163
+ `FFL(inp_pipe_mask_q[i+1], inp_pipe_mask_q[i], reg_ena, '0)
164
+ `FFL(inp_pipe_aux_q[i+1], inp_pipe_aux_q[i], reg_ena, AuxType'('0))
165
+ end
166
+ // Output stage: assign selected pipe outputs to signals for later use
167
+ assign operands_q = inp_pipe_operands_q[NUM_INP_REGS];
168
+ assign src_fmt_q = inp_pipe_src_fmt_q[NUM_INP_REGS];
169
+ assign dst_fmt_q = inp_pipe_dst_fmt_q[NUM_INP_REGS];
170
+
171
+ // -----------------
172
+ // Input processing
173
+ // -----------------
174
+ logic [NUM_FORMATS-1:0][2:0] fmt_sign;
175
+ logic signed [NUM_FORMATS-1:0][2:0][SUPER_EXP_BITS-1:0] fmt_exponent;
176
+ logic [NUM_FORMATS-1:0][2:0][SUPER_MAN_BITS-1:0] fmt_mantissa;
177
+
178
+ fpnew_pkg::fp_info_t [NUM_FORMATS-1:0][2:0] info_q;
179
+
180
+ // FP Input initialization
181
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : fmt_init_inputs
182
+ // Set up some constants
183
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
184
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
185
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
186
+
187
+ if (FpFmtConfig[fmt]) begin : active_format
188
+ logic [2:0][FP_WIDTH-1:0] trimmed_ops;
189
+
190
+ // Classify input
191
+ fpnew_classifier #(
192
+ .FpFormat ( fpnew_pkg::fp_format_e'(fmt) ),
193
+ .NumOperands ( 3 )
194
+ ) i_fpnew_classifier (
195
+ .operands_i ( trimmed_ops ),
196
+ .is_boxed_i ( inp_pipe_is_boxed_q[NUM_INP_REGS][fmt] ),
197
+ .info_o ( info_q[fmt] )
198
+ );
199
+ for (genvar op = 0; op < 3; op++) begin : gen_operands
200
+ assign trimmed_ops[op] = operands_q[op][FP_WIDTH-1:0];
201
+ assign fmt_sign[fmt][op] = operands_q[op][FP_WIDTH-1];
202
+ assign fmt_exponent[fmt][op] = signed'({1'b0, operands_q[op][MAN_BITS+:EXP_BITS]});
203
+ assign fmt_mantissa[fmt][op] = {info_q[fmt][op].is_normal, operands_q[op][MAN_BITS-1:0]} <<
204
+ (SUPER_MAN_BITS - MAN_BITS); // move to left of mantissa
205
+ end
206
+ end else begin : inactive_format
207
+ assign info_q[fmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
208
+ assign fmt_sign[fmt] = fpnew_pkg::DONT_CARE; // format disabled
209
+ assign fmt_exponent[fmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
210
+ assign fmt_mantissa[fmt] = '{default: fpnew_pkg::DONT_CARE}; // format disabled
211
+ end
212
+ end
213
+
214
+ fp_t operand_a, operand_b, operand_c;
215
+ fpnew_pkg::fp_info_t info_a, info_b, info_c;
216
+
217
+ // Operation selection and operand adjustment
218
+ // | \c op_q | \c op_mod_q | Operation Adjustment
219
+ // |:--------:|:-----------:|---------------------
220
+ // | FMADD | \c 0 | FMADD: none
221
+ // | FMADD | \c 1 | FMSUB: Invert sign of operand C
222
+ // | FNMSUB | \c 0 | FNMSUB: Invert sign of operand A
223
+ // | FNMSUB | \c 1 | FNMADD: Invert sign of operands A and C
224
+ // | ADD | \c 0 | ADD: Set operand A to +1.0
225
+ // | ADD | \c 1 | SUB: Set operand A to +1.0, invert sign of operand C
226
+ // | MUL | \c 0 | MUL: Set operand C to +0.0 or -0.0 depending on the rounding mode
227
+ // | *others* | \c - | *invalid*
228
+ // \note \c op_mod_q always inverts the sign of the addend.
229
+ always_comb begin : op_select
230
+
231
+ // Default assignments - packing-order-agnostic
232
+ operand_a = {fmt_sign[src_fmt_q][0], fmt_exponent[src_fmt_q][0], fmt_mantissa[src_fmt_q][0]};
233
+ operand_b = {fmt_sign[src_fmt_q][1], fmt_exponent[src_fmt_q][1], fmt_mantissa[src_fmt_q][1]};
234
+ operand_c = {fmt_sign[dst_fmt_q][2], fmt_exponent[dst_fmt_q][2], fmt_mantissa[dst_fmt_q][2]};
235
+ info_a = info_q[src_fmt_q][0];
236
+ info_b = info_q[src_fmt_q][1];
237
+ info_c = info_q[dst_fmt_q][2];
238
+
239
+ // op_mod_q inverts sign of operand C
240
+ operand_c.sign = operand_c.sign ^ inp_pipe_op_mod_q[NUM_INP_REGS];
241
+
242
+ unique case (inp_pipe_op_q[NUM_INP_REGS])
243
+ fpnew_pkg::FMADD: ; // do nothing
244
+ fpnew_pkg::FNMSUB: operand_a.sign = ~operand_a.sign; // invert sign of product
245
+ fpnew_pkg::ADD: begin // Set multiplicand to +1
246
+ operand_a = '{sign: 1'b0, exponent: fpnew_pkg::bias(src_fmt_q), mantissa: '0};
247
+ info_a = '{is_normal: 1'b1, is_boxed: 1'b1, default: 1'b0}; //normal, boxed value.
248
+ end
249
+ fpnew_pkg::MUL: begin // Set addend to +0 or -0, depending whether the rounding mode is RDN
250
+ if (inp_pipe_rnd_mode_q[NUM_INP_REGS] == fpnew_pkg::RDN)
251
+ operand_c = '{sign: 1'b0, exponent: '0, mantissa: '0};
252
+ else
253
+ operand_c = '{sign: 1'b1, exponent: '0, mantissa: '0};
254
+ info_c = '{is_zero: 1'b1, is_boxed: 1'b1, default: 1'b0}; //zero, boxed value.
255
+ end
256
+ default: begin // propagate don't cares
257
+ operand_a = '{default: fpnew_pkg::DONT_CARE};
258
+ operand_b = '{default: fpnew_pkg::DONT_CARE};
259
+ operand_c = '{default: fpnew_pkg::DONT_CARE};
260
+ info_a = '{default: fpnew_pkg::DONT_CARE};
261
+ info_b = '{default: fpnew_pkg::DONT_CARE};
262
+ info_c = '{default: fpnew_pkg::DONT_CARE};
263
+ end
264
+ endcase
265
+ end
266
+
267
+ // ---------------------
268
+ // Input classification
269
+ // ---------------------
270
+ logic any_operand_inf;
271
+ logic any_operand_nan;
272
+ logic signalling_nan;
273
+ logic effective_subtraction;
274
+ logic tentative_sign;
275
+
276
+ // Reduction for special case handling
277
+ assign any_operand_inf = (| {info_a.is_inf, info_b.is_inf, info_c.is_inf});
278
+ assign any_operand_nan = (| {info_a.is_nan, info_b.is_nan, info_c.is_nan});
279
+ assign signalling_nan = (| {info_a.is_signalling, info_b.is_signalling, info_c.is_signalling});
280
+ // Effective subtraction in FMA occurs when product and addend signs differ
281
+ assign effective_subtraction = operand_a.sign ^ operand_b.sign ^ operand_c.sign;
282
+ // The tentative sign of the FMA shall be the sign of the product
283
+ assign tentative_sign = operand_a.sign ^ operand_b.sign;
284
+
285
+ // ----------------------
286
+ // Special case handling
287
+ // ----------------------
288
+ logic [WIDTH-1:0] special_result;
289
+ fpnew_pkg::status_t special_status;
290
+ logic result_is_special;
291
+
292
+ logic [NUM_FORMATS-1:0][WIDTH-1:0] fmt_special_result;
293
+ fpnew_pkg::status_t [NUM_FORMATS-1:0] fmt_special_status;
294
+ logic [NUM_FORMATS-1:0] fmt_result_is_special;
295
+
296
+
297
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_special_results
298
+ // Set up some constants
299
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
300
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
301
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
302
+
303
+ localparam logic [EXP_BITS-1:0] QNAN_EXPONENT = '1;
304
+ localparam logic [MAN_BITS-1:0] QNAN_MANTISSA = 2**(MAN_BITS-1);
305
+ localparam logic [MAN_BITS-1:0] ZERO_MANTISSA = '0;
306
+
307
+ if (FpFmtConfig[fmt]) begin : active_format
308
+ always_comb begin : special_results
309
+ logic [FP_WIDTH-1:0] special_res;
310
+
311
+ // Default assignment
312
+ special_res = {1'b0, QNAN_EXPONENT, QNAN_MANTISSA}; // qNaN
313
+ fmt_special_status[fmt] = '0;
314
+ fmt_result_is_special[fmt] = 1'b0;
315
+
316
+ // Handle potentially mixed nan & infinity input => important for the case where infinity and
317
+ // zero are multiplied and added to a qnan.
318
+ // RISC-V mandates raising the NV exception in these cases:
319
+ // (inf * 0) + c or (0 * inf) + c INVALID, no matter c (even quiet NaNs)
320
+ if ((info_a.is_inf && info_b.is_zero) || (info_a.is_zero && info_b.is_inf)) begin
321
+ fmt_result_is_special[fmt] = 1'b1; // bypass FMA, output is the canonical qNaN
322
+ fmt_special_status[fmt].NV = 1'b1; // invalid operation
323
+ // NaN Inputs cause canonical quiet NaN at the output and maybe invalid OP
324
+ end else if (any_operand_nan) begin
325
+ fmt_result_is_special[fmt] = 1'b1; // bypass FMA, output is the canonical qNaN
326
+ fmt_special_status[fmt].NV = signalling_nan; // raise the invalid operation flag if signalling
327
+ // Special cases involving infinity
328
+ end else if (any_operand_inf) begin
329
+ fmt_result_is_special[fmt] = 1'b1; // bypass FMA
330
+ // Effective addition of opposite infinities (±inf - ±inf) is invalid!
331
+ if ((info_a.is_inf || info_b.is_inf) && info_c.is_inf && effective_subtraction)
332
+ fmt_special_status[fmt].NV = 1'b1; // invalid operation
333
+ // Handle cases where output will be inf because of inf product input
334
+ else if (info_a.is_inf || info_b.is_inf) begin
335
+ // Result is infinity with the sign of the product
336
+ special_res = {operand_a.sign ^ operand_b.sign, QNAN_EXPONENT, ZERO_MANTISSA};
337
+ // Handle cases where the addend is inf
338
+ end else if (info_c.is_inf) begin
339
+ // Result is inifinity with sign of the addend (= operand_c)
340
+ special_res = {operand_c.sign, QNAN_EXPONENT, ZERO_MANTISSA};
341
+ end
342
+ end
343
+ // Initialize special result with ones (NaN-box)
344
+ fmt_special_result[fmt] = '1;
345
+ fmt_special_result[fmt][FP_WIDTH-1:0] = special_res;
346
+ end
347
+ end else begin : inactive_format
348
+ assign fmt_special_result[fmt] = '{default: fpnew_pkg::DONT_CARE};
349
+ assign fmt_special_status[fmt] = '0;
350
+ assign fmt_result_is_special[fmt] = 1'b0;
351
+ end
352
+ end
353
+
354
+ // Detect special case from source format, I2F casts don't produce a special result
355
+ assign result_is_special = fmt_result_is_special[dst_fmt_q]; // they're all the same
356
+ // Signalling input NaNs raise invalid flag, otherwise no flags set
357
+ assign special_status = fmt_special_status[dst_fmt_q];
358
+ // Assemble result according to destination format
359
+ assign special_result = fmt_special_result[dst_fmt_q]; // destination format
360
+
361
+ // ---------------------------
362
+ // Initial exponent data path
363
+ // ---------------------------
364
+ logic signed [EXP_WIDTH-1:0] exponent_a, exponent_b, exponent_c;
365
+ logic signed [EXP_WIDTH-1:0] exponent_addend, exponent_product, exponent_difference;
366
+ logic signed [EXP_WIDTH-1:0] tentative_exponent;
367
+
368
+ // Zero-extend exponents into signed container - implicit width extension
369
+ assign exponent_a = signed'({1'b0, operand_a.exponent});
370
+ assign exponent_b = signed'({1'b0, operand_b.exponent});
371
+ assign exponent_c = signed'({1'b0, operand_c.exponent});
372
+
373
+ // Calculate internal exponents from encoded values. Real exponents are (ex = Ex - bias + 1 - nx)
374
+ // with Ex the encoded exponent and nx the implicit bit. Internal exponents are biased to dst fmt.
375
+ assign exponent_addend = signed'(exponent_c + $signed({1'b0, ~info_c.is_normal})); // 0 as subnorm
376
+ // Biased product exponent is the sum of encoded exponents minus the bias.
377
+ assign exponent_product = (info_a.is_zero || info_b.is_zero) // in case the product is zero, set minimum exp.
378
+ ? 2 - signed'(fpnew_pkg::bias(dst_fmt_q))
379
+ : signed'(exponent_a + info_a.is_subnormal
380
+ + exponent_b + info_b.is_subnormal
381
+ - 2*signed'(fpnew_pkg::bias(src_fmt_q))
382
+ + signed'(fpnew_pkg::bias(dst_fmt_q))); // rebias for dst fmt
383
+ // Exponent difference is the addend exponent minus the product exponent
384
+ assign exponent_difference = exponent_addend - exponent_product;
385
+ // The tentative exponent will be the larger of the product or addend exponent
386
+ assign tentative_exponent = (exponent_difference > 0) ? exponent_addend : exponent_product;
387
+
388
+ // Shift amount for addend based on exponents (unsigned as only right shifts)
389
+ logic [SHIFT_AMOUNT_WIDTH-1:0] addend_shamt;
390
+
391
+ always_comb begin : addend_shift_amount
392
+ // Product-anchored case, saturated shift (addend is only in the sticky bit)
393
+ if (exponent_difference <= signed'(-2 * PRECISION_BITS - 1))
394
+ addend_shamt = 3 * PRECISION_BITS + 4;
395
+ // Addend and product will have mutual bits to add
396
+ else if (exponent_difference <= signed'(PRECISION_BITS + 2))
397
+ addend_shamt = unsigned'(signed'(PRECISION_BITS) + 3 - exponent_difference);
398
+ // Addend-anchored case, saturated shift (product is only in the sticky bit)
399
+ else
400
+ addend_shamt = 0;
401
+ end
402
+
403
+ // ------------------
404
+ // Product data path
405
+ // ------------------
406
+ logic [PRECISION_BITS-1:0] mantissa_a, mantissa_b, mantissa_c;
407
+ logic [2*PRECISION_BITS-1:0] product; // the p*p product is 2p bits wide
408
+ logic [3*PRECISION_BITS+3:0] product_shifted; // addends are 3p+4 bit wide (including G/R)
409
+
410
+ // Add implicit bits to mantissae
411
+ assign mantissa_a = {info_a.is_normal, operand_a.mantissa};
412
+ assign mantissa_b = {info_b.is_normal, operand_b.mantissa};
413
+ assign mantissa_c = {info_c.is_normal, operand_c.mantissa};
414
+
415
+ // Mantissa multiplier (a*b)
416
+ assign product = mantissa_a * mantissa_b;
417
+
418
+ // Product is placed into a 3p+4 bit wide vector, padded with 2 bits for round and sticky:
419
+ // | 000...000 | product | RS |
420
+ // <- p+2 -> <- 2p -> < 2>
421
+ assign product_shifted = product << 2; // constant shift
422
+
423
+ // -----------------
424
+ // Addend data path
425
+ // -----------------
426
+ logic [3*PRECISION_BITS+3:0] addend_after_shift; // upper 3p+4 bits are needed to go on
427
+ logic [PRECISION_BITS-1:0] addend_sticky_bits; // up to p bit of shifted addend are sticky
428
+ logic sticky_before_add; // they are compressed into a single sticky bit
429
+ logic [3*PRECISION_BITS+3:0] addend_shifted; // addends are 3p+4 bit wide (including G/R)
430
+ logic inject_carry_in; // inject carry for subtractions if needed
431
+
432
+ // In parallel, the addend is right-shifted according to the exponent difference. Up to p bits are
433
+ // shifted out and compressed into a sticky bit.
434
+ // BEFORE THE SHIFT:
435
+ // | mantissa_c | 000..000 |
436
+ // <- p -> <- 3p+4 ->
437
+ // AFTER THE SHIFT:
438
+ // | 000..........000 | mantissa_c | 000...............0GR | sticky bits |
439
+ // <- addend_shamt -> <- p -> <- 2p+4-addend_shamt -> <- up to p ->
440
+ assign {addend_after_shift, addend_sticky_bits} =
441
+ (mantissa_c << (3 * PRECISION_BITS + 4)) >> addend_shamt;
442
+
443
+ assign sticky_before_add = (| addend_sticky_bits);
444
+
445
+ // In case of a subtraction, the addend is inverted
446
+ assign addend_shifted = (effective_subtraction) ? ~addend_after_shift : addend_after_shift;
447
+ assign inject_carry_in = effective_subtraction & ~sticky_before_add;
448
+
449
+ // ------
450
+ // Adder
451
+ // ------
452
+ logic [3*PRECISION_BITS+4:0] sum_raw; // added one bit for the carry
453
+ logic sum_carry; // observe carry bit from sum for sign fixing
454
+ logic [3*PRECISION_BITS+3:0] sum; // discard carry as sum won't overflow
455
+ logic final_sign;
456
+
457
+ //Mantissa adder (ab+c). In normal addition, it cannot overflow.
458
+ assign sum_raw = product_shifted + addend_shifted + inject_carry_in;
459
+ assign sum_carry = sum_raw[3*PRECISION_BITS+4];
460
+
461
+ // Complement negative sum (can only happen in subtraction -> overflows for positive results)
462
+ assign sum = (effective_subtraction && ~sum_carry) ? -sum_raw : sum_raw;
463
+
464
+ // In case of a mispredicted subtraction result, do a sign flip
465
+ assign final_sign = (effective_subtraction && (sum_carry == tentative_sign))
466
+ ? 1'b1
467
+ : (effective_subtraction ? 1'b0 : tentative_sign);
468
+
469
+ // ---------------
470
+ // Internal pipeline
471
+ // ---------------
472
+ // Pipeline output signals as non-arrays
473
+ logic effective_subtraction_q;
474
+ logic signed [EXP_WIDTH-1:0] exponent_product_q;
475
+ logic signed [EXP_WIDTH-1:0] exponent_difference_q;
476
+ logic signed [EXP_WIDTH-1:0] tentative_exponent_q;
477
+ logic [SHIFT_AMOUNT_WIDTH-1:0] addend_shamt_q;
478
+ logic sticky_before_add_q;
479
+ logic [3*PRECISION_BITS+3:0] sum_q;
480
+ logic final_sign_q;
481
+ fpnew_pkg::fp_format_e dst_fmt_q2;
482
+ fpnew_pkg::roundmode_e rnd_mode_q;
483
+ logic result_is_special_q;
484
+ fp_t special_result_q;
485
+ fpnew_pkg::status_t special_status_q;
486
+ // Internal pipeline signals, index i holds signal after i register stages
487
+ logic [0:NUM_MID_REGS] mid_pipe_eff_sub_q;
488
+ logic signed [0:NUM_MID_REGS][EXP_WIDTH-1:0] mid_pipe_exp_prod_q;
489
+ logic signed [0:NUM_MID_REGS][EXP_WIDTH-1:0] mid_pipe_exp_diff_q;
490
+ logic signed [0:NUM_MID_REGS][EXP_WIDTH-1:0] mid_pipe_tent_exp_q;
491
+ logic [0:NUM_MID_REGS][SHIFT_AMOUNT_WIDTH-1:0] mid_pipe_add_shamt_q;
492
+ logic [0:NUM_MID_REGS] mid_pipe_sticky_q;
493
+ logic [0:NUM_MID_REGS][3*PRECISION_BITS+3:0] mid_pipe_sum_q;
494
+ logic [0:NUM_MID_REGS] mid_pipe_final_sign_q;
495
+ fpnew_pkg::roundmode_e [0:NUM_MID_REGS] mid_pipe_rnd_mode_q;
496
+ fpnew_pkg::fp_format_e [0:NUM_MID_REGS] mid_pipe_dst_fmt_q;
497
+ logic [0:NUM_MID_REGS] mid_pipe_res_is_spec_q;
498
+ fp_t [0:NUM_MID_REGS] mid_pipe_spec_res_q;
499
+ fpnew_pkg::status_t [0:NUM_MID_REGS] mid_pipe_spec_stat_q;
500
+ TagType [0:NUM_MID_REGS] mid_pipe_tag_q;
501
+ logic [0:NUM_MID_REGS] mid_pipe_mask_q;
502
+ AuxType [0:NUM_MID_REGS] mid_pipe_aux_q;
503
+ logic [0:NUM_MID_REGS] mid_pipe_valid_q;
504
+ // Ready signal is combinatorial for all stages
505
+ logic [0:NUM_MID_REGS] mid_pipe_ready;
506
+
507
+ // Input stage: First element of pipeline is taken from upstream logic
508
+ assign mid_pipe_eff_sub_q[0] = effective_subtraction;
509
+ assign mid_pipe_exp_prod_q[0] = exponent_product;
510
+ assign mid_pipe_exp_diff_q[0] = exponent_difference;
511
+ assign mid_pipe_tent_exp_q[0] = tentative_exponent;
512
+ assign mid_pipe_add_shamt_q[0] = addend_shamt;
513
+ assign mid_pipe_sticky_q[0] = sticky_before_add;
514
+ assign mid_pipe_sum_q[0] = sum;
515
+ assign mid_pipe_final_sign_q[0] = final_sign;
516
+ assign mid_pipe_rnd_mode_q[0] = inp_pipe_rnd_mode_q[NUM_INP_REGS];
517
+ assign mid_pipe_dst_fmt_q[0] = dst_fmt_q;
518
+ assign mid_pipe_res_is_spec_q[0] = result_is_special;
519
+ assign mid_pipe_spec_res_q[0] = special_result;
520
+ assign mid_pipe_spec_stat_q[0] = special_status;
521
+ assign mid_pipe_tag_q[0] = inp_pipe_tag_q[NUM_INP_REGS];
522
+ assign mid_pipe_mask_q[0] = inp_pipe_mask_q[NUM_INP_REGS];
523
+ assign mid_pipe_aux_q[0] = inp_pipe_aux_q[NUM_INP_REGS];
524
+ assign mid_pipe_valid_q[0] = inp_pipe_valid_q[NUM_INP_REGS];
525
+ // Input stage: Propagate pipeline ready signal to input pipe
526
+ assign inp_pipe_ready[NUM_INP_REGS] = mid_pipe_ready[0];
527
+
528
+ // Generate the register stages
529
+ for (genvar i = 0; i < NUM_MID_REGS; i++) begin : gen_inside_pipeline
530
+ // Internal register enable for this stage
531
+ logic reg_ena;
532
+ // Determine the ready signal of the current stage - advance the pipeline:
533
+ // 1. if the next stage is ready for our data
534
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
535
+ assign mid_pipe_ready[i] = mid_pipe_ready[i+1] | ~mid_pipe_valid_q[i+1];
536
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
537
+ `FFLARNC(mid_pipe_valid_q[i+1], mid_pipe_valid_q[i], mid_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
538
+ // Enable register if pipleine ready and a valid data item is present
539
+ assign reg_ena = mid_pipe_ready[i] & mid_pipe_valid_q[i];
540
+ // Generate the pipeline registers within the stages, use enable-registers
541
+ `FFL(mid_pipe_eff_sub_q[i+1], mid_pipe_eff_sub_q[i], reg_ena, '0)
542
+ `FFL(mid_pipe_exp_prod_q[i+1], mid_pipe_exp_prod_q[i], reg_ena, '0)
543
+ `FFL(mid_pipe_exp_diff_q[i+1], mid_pipe_exp_diff_q[i], reg_ena, '0)
544
+ `FFL(mid_pipe_tent_exp_q[i+1], mid_pipe_tent_exp_q[i], reg_ena, '0)
545
+ `FFL(mid_pipe_add_shamt_q[i+1], mid_pipe_add_shamt_q[i], reg_ena, '0)
546
+ `FFL(mid_pipe_sticky_q[i+1], mid_pipe_sticky_q[i], reg_ena, '0)
547
+ `FFL(mid_pipe_sum_q[i+1], mid_pipe_sum_q[i], reg_ena, '0)
548
+ `FFL(mid_pipe_final_sign_q[i+1], mid_pipe_final_sign_q[i], reg_ena, '0)
549
+ `FFL(mid_pipe_rnd_mode_q[i+1], mid_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
550
+ `FFL(mid_pipe_dst_fmt_q[i+1], mid_pipe_dst_fmt_q[i], reg_ena, fpnew_pkg::fp_format_e'(0))
551
+ `FFL(mid_pipe_res_is_spec_q[i+1], mid_pipe_res_is_spec_q[i], reg_ena, '0)
552
+ `FFL(mid_pipe_spec_res_q[i+1], mid_pipe_spec_res_q[i], reg_ena, '0)
553
+ `FFL(mid_pipe_spec_stat_q[i+1], mid_pipe_spec_stat_q[i], reg_ena, '0)
554
+ `FFL(mid_pipe_tag_q[i+1], mid_pipe_tag_q[i], reg_ena, TagType'('0))
555
+ `FFL(mid_pipe_mask_q[i+1], mid_pipe_mask_q[i], reg_ena, '0)
556
+ `FFL(mid_pipe_aux_q[i+1], mid_pipe_aux_q[i], reg_ena, AuxType'('0))
557
+ end
558
+ // Output stage: assign selected pipe outputs to signals for later use
559
+ assign effective_subtraction_q = mid_pipe_eff_sub_q[NUM_MID_REGS];
560
+ assign exponent_product_q = mid_pipe_exp_prod_q[NUM_MID_REGS];
561
+ assign exponent_difference_q = mid_pipe_exp_diff_q[NUM_MID_REGS];
562
+ assign tentative_exponent_q = mid_pipe_tent_exp_q[NUM_MID_REGS];
563
+ assign addend_shamt_q = mid_pipe_add_shamt_q[NUM_MID_REGS];
564
+ assign sticky_before_add_q = mid_pipe_sticky_q[NUM_MID_REGS];
565
+ assign sum_q = mid_pipe_sum_q[NUM_MID_REGS];
566
+ assign final_sign_q = mid_pipe_final_sign_q[NUM_MID_REGS];
567
+ assign rnd_mode_q = mid_pipe_rnd_mode_q[NUM_MID_REGS];
568
+ assign dst_fmt_q2 = mid_pipe_dst_fmt_q[NUM_MID_REGS];
569
+ assign result_is_special_q = mid_pipe_res_is_spec_q[NUM_MID_REGS];
570
+ assign special_result_q = mid_pipe_spec_res_q[NUM_MID_REGS];
571
+ assign special_status_q = mid_pipe_spec_stat_q[NUM_MID_REGS];
572
+
573
+ // --------------
574
+ // Normalization
575
+ // --------------
576
+ logic [LOWER_SUM_WIDTH-1:0] sum_lower; // lower 2p+3 bits of sum are searched
577
+ logic [LZC_RESULT_WIDTH-1:0] leading_zero_count; // the number of leading zeroes
578
+ logic signed [LZC_RESULT_WIDTH:0] leading_zero_count_sgn; // signed leading-zero count
579
+ logic lzc_zeroes; // in case only zeroes found
580
+
581
+ logic [SHIFT_AMOUNT_WIDTH-1:0] norm_shamt; // Normalization shift amount
582
+ logic signed [EXP_WIDTH-1:0] normalized_exponent;
583
+
584
+ logic [3*PRECISION_BITS+4:0] sum_shifted; // result after first normalization shift
585
+ logic [PRECISION_BITS:0] final_mantissa; // final mantissa before rounding with round bit
586
+ logic [2*PRECISION_BITS+2:0] sum_sticky_bits; // remaining 2p+3 sticky bits after normalization
587
+ logic sticky_after_norm; // sticky bit after normalization
588
+
589
+ logic signed [EXP_WIDTH-1:0] final_exponent;
590
+
591
+ assign sum_lower = sum_q[LOWER_SUM_WIDTH-1:0];
592
+
593
+ // Leading zero counter for cancellations
594
+ lzc #(
595
+ .WIDTH ( LOWER_SUM_WIDTH ),
596
+ .MODE ( 1 ) // MODE = 1 counts leading zeroes
597
+ ) i_lzc (
598
+ .in_i ( sum_lower ),
599
+ .cnt_o ( leading_zero_count ),
600
+ .empty_o ( lzc_zeroes )
601
+ );
602
+
603
+ assign leading_zero_count_sgn = signed'({1'b0, leading_zero_count});
604
+
605
+ // Normalization shift amount based on exponents and LZC (unsigned as only left shifts)
606
+ always_comb begin : norm_shift_amount
607
+ // Product-anchored case or cancellations require LZC
608
+ if ((exponent_difference_q <= 0) || (effective_subtraction_q && (exponent_difference_q <= 2))) begin
609
+ // Normal result (biased exponent > 0 and not a zero)
610
+ if ((exponent_product_q - leading_zero_count_sgn + 1 >= 0) && !lzc_zeroes) begin
611
+ // Undo initial product shift, remove the counted zeroes
612
+ norm_shamt = PRECISION_BITS + 2 + leading_zero_count;
613
+ normalized_exponent = exponent_product_q - leading_zero_count_sgn + 1; // account for shift
614
+ // Subnormal result
615
+ end else begin
616
+ // Cap the shift distance to align mantissa with minimum exponent
617
+ norm_shamt = unsigned'(signed'(PRECISION_BITS + 2 + exponent_product_q));
618
+ normalized_exponent = 0; // subnormals encoded as 0
619
+ end
620
+ // Addend-anchored case
621
+ end else begin
622
+ norm_shamt = addend_shamt_q; // Undo the initial shift
623
+ normalized_exponent = tentative_exponent_q;
624
+ end
625
+ end
626
+
627
+ // Do the large normalization shift
628
+ assign sum_shifted = sum_q << norm_shamt;
629
+
630
+ // The addend-anchored case needs a 1-bit normalization since the leading-one can be to the left
631
+ // or right of the (non-carry) MSB of the sum.
632
+ always_comb begin : small_norm
633
+ // Default assignment, discarding carry bit
634
+ {final_mantissa, sum_sticky_bits} = sum_shifted;
635
+ final_exponent = normalized_exponent;
636
+
637
+ // The normalized sum has overflown, align right and fix exponent
638
+ if (sum_shifted[3*PRECISION_BITS+4]) begin // check the carry bit
639
+ {final_mantissa, sum_sticky_bits} = sum_shifted >> 1;
640
+ final_exponent = normalized_exponent + 1;
641
+ // The normalized sum is normal, nothing to do
642
+ end else if (sum_shifted[3*PRECISION_BITS+3]) begin // check the sum MSB
643
+ // do nothing
644
+ // The normalized sum is still denormal, align left - unless the result is not already subnormal
645
+ end else if (normalized_exponent > 1) begin
646
+ {final_mantissa, sum_sticky_bits} = sum_shifted << 1;
647
+ final_exponent = normalized_exponent - 1;
648
+ // Otherwise we're denormal
649
+ end else begin
650
+ final_exponent = '0;
651
+ end
652
+ end
653
+
654
+ // Update the sticky bit with the shifted-out bits
655
+ assign sticky_after_norm = (| {sum_sticky_bits}) | sticky_before_add_q;
656
+
657
+ // ----------------------------
658
+ // Rounding and classification
659
+ // ----------------------------
660
+ logic pre_round_sign;
661
+ logic [SUPER_EXP_BITS+SUPER_MAN_BITS-1:0] pre_round_abs; // absolute value of result before rounding
662
+ logic [1:0] round_sticky_bits;
663
+
664
+ logic of_before_round, of_after_round; // overflow
665
+ logic uf_before_round, uf_after_round; // underflow
666
+
667
+ logic [NUM_FORMATS-1:0][SUPER_EXP_BITS+SUPER_MAN_BITS-1:0] fmt_pre_round_abs; // per format
668
+ logic [NUM_FORMATS-1:0][1:0] fmt_round_sticky_bits;
669
+
670
+ logic [NUM_FORMATS-1:0] fmt_of_after_round;
671
+ logic [NUM_FORMATS-1:0] fmt_uf_after_round;
672
+
673
+ logic rounded_sign;
674
+ logic [SUPER_EXP_BITS+SUPER_MAN_BITS-1:0] rounded_abs; // absolute value of result after rounding
675
+ logic result_zero;
676
+
677
+ // Classification before round. RISC-V mandates checking underflow AFTER rounding!
678
+ assign of_before_round = final_exponent >= 2**(fpnew_pkg::exp_bits(dst_fmt_q2))-1; // infinity exponent is all ones
679
+ assign uf_before_round = final_exponent == 0; // exponent for subnormals capped to 0
680
+
681
+ // Pack exponent and mantissa into proper rounding form
682
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_res_assemble
683
+ // Set up some constants
684
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
685
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
686
+
687
+ logic [EXP_BITS-1:0] pre_round_exponent;
688
+ logic [MAN_BITS-1:0] pre_round_mantissa;
689
+
690
+ if (FpFmtConfig[fmt]) begin : active_format
691
+
692
+ assign pre_round_exponent = (of_before_round) ? 2**EXP_BITS-2 : final_exponent[EXP_BITS-1:0];
693
+ assign pre_round_mantissa = (of_before_round) ? '1 : final_mantissa[SUPER_MAN_BITS-:MAN_BITS];
694
+ // Assemble result before rounding. In case of overflow, the largest normal value is set.
695
+ assign fmt_pre_round_abs[fmt] = {pre_round_exponent, pre_round_mantissa}; // 0-extend
696
+
697
+ // Round bit is after mantissa (1 in case of overflow for rounding)
698
+ assign fmt_round_sticky_bits[fmt][1] = final_mantissa[SUPER_MAN_BITS-MAN_BITS] |
699
+ of_before_round;
700
+
701
+ // remaining bits in mantissa to sticky (1 in case of overflow for rounding)
702
+ if (MAN_BITS < SUPER_MAN_BITS) begin : narrow_sticky
703
+ assign fmt_round_sticky_bits[fmt][0] = (| final_mantissa[SUPER_MAN_BITS-MAN_BITS-1:0]) |
704
+ sticky_after_norm | of_before_round;
705
+ end else begin : normal_sticky
706
+ assign fmt_round_sticky_bits[fmt][0] = sticky_after_norm | of_before_round;
707
+ end
708
+ end else begin : inactive_format
709
+ assign fmt_pre_round_abs[fmt] = '{default: fpnew_pkg::DONT_CARE};
710
+ assign fmt_round_sticky_bits[fmt] = '{default: fpnew_pkg::DONT_CARE};
711
+ end
712
+ end
713
+
714
+ // Assemble result before rounding. In case of overflow, the largest normal value is set.
715
+ assign pre_round_sign = final_sign_q;
716
+ assign pre_round_abs = fmt_pre_round_abs[dst_fmt_q2];
717
+
718
+ // In case of overflow, the round and sticky bits are set for proper rounding
719
+ assign round_sticky_bits = fmt_round_sticky_bits[dst_fmt_q2];
720
+
721
+ // Perform the rounding
722
+ fpnew_rounding #(
723
+ .AbsWidth ( SUPER_EXP_BITS + SUPER_MAN_BITS )
724
+ ) i_fpnew_rounding (
725
+ .abs_value_i ( pre_round_abs ),
726
+ .sign_i ( pre_round_sign ),
727
+ .round_sticky_bits_i ( round_sticky_bits ),
728
+ .rnd_mode_i ( rnd_mode_q ),
729
+ .effective_subtraction_i ( effective_subtraction_q ),
730
+ .abs_rounded_o ( rounded_abs ),
731
+ .sign_o ( rounded_sign ),
732
+ .exact_zero_o ( result_zero )
733
+ );
734
+
735
+ logic [NUM_FORMATS-1:0][WIDTH-1:0] fmt_result;
736
+
737
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_sign_inject
738
+ // Set up some constants
739
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
740
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(fpnew_pkg::fp_format_e'(fmt));
741
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(fpnew_pkg::fp_format_e'(fmt));
742
+
743
+ if (FpFmtConfig[fmt]) begin : active_format
744
+ always_comb begin : post_process
745
+ // detect of / uf
746
+ fmt_uf_after_round[fmt] = rounded_abs[EXP_BITS+MAN_BITS-1:MAN_BITS] == '0; // denormal
747
+ fmt_of_after_round[fmt] = rounded_abs[EXP_BITS+MAN_BITS-1:MAN_BITS] == '1; // inf exp.
748
+
749
+ // Assemble regular result, nan box short ones.
750
+ fmt_result[fmt] = '1;
751
+ fmt_result[fmt][FP_WIDTH-1:0] = {rounded_sign, rounded_abs[EXP_BITS+MAN_BITS-1:0]};
752
+ end
753
+ end else begin : inactive_format
754
+ assign fmt_uf_after_round[fmt] = fpnew_pkg::DONT_CARE;
755
+ assign fmt_of_after_round[fmt] = fpnew_pkg::DONT_CARE;
756
+ assign fmt_result[fmt] = '{default: fpnew_pkg::DONT_CARE};
757
+ end
758
+ end
759
+
760
+ // Classification after rounding select by destination format
761
+ assign uf_after_round = fmt_uf_after_round[dst_fmt_q2];
762
+ assign of_after_round = fmt_of_after_round[dst_fmt_q2];
763
+
764
+
765
+ // -----------------
766
+ // Result selection
767
+ // -----------------
768
+ logic [WIDTH-1:0] regular_result;
769
+ fpnew_pkg::status_t regular_status;
770
+
771
+ // Assemble regular result
772
+ assign regular_result = fmt_result[dst_fmt_q2];
773
+ assign regular_status.NV = 1'b0; // only valid cases are handled in regular path
774
+ assign regular_status.DZ = 1'b0; // no divisions
775
+ assign regular_status.OF = of_before_round | of_after_round; // rounding can introduce overflow
776
+ assign regular_status.UF = uf_after_round & regular_status.NX; // only inexact results raise UF
777
+ assign regular_status.NX = (| round_sticky_bits) | of_before_round | of_after_round;
778
+
779
+ // Final results for output pipeline
780
+ logic [WIDTH-1:0] result_d;
781
+ fpnew_pkg::status_t status_d;
782
+
783
+ // Select output depending on special case detection
784
+ assign result_d = result_is_special_q ? special_result_q : regular_result;
785
+ assign status_d = result_is_special_q ? special_status_q : regular_status;
786
+
787
+ // ----------------
788
+ // Output Pipeline
789
+ // ----------------
790
+ // Output pipeline signals, index i holds signal after i register stages
791
+ logic [0:NUM_OUT_REGS][WIDTH-1:0] out_pipe_result_q;
792
+ fpnew_pkg::status_t [0:NUM_OUT_REGS] out_pipe_status_q;
793
+ TagType [0:NUM_OUT_REGS] out_pipe_tag_q;
794
+ logic [0:NUM_OUT_REGS] out_pipe_mask_q;
795
+ AuxType [0:NUM_OUT_REGS] out_pipe_aux_q;
796
+ logic [0:NUM_OUT_REGS] out_pipe_valid_q;
797
+ // Ready signal is combinatorial for all stages
798
+ logic [0:NUM_OUT_REGS] out_pipe_ready;
799
+
800
+ // Input stage: First element of pipeline is taken from inputs
801
+ assign out_pipe_result_q[0] = result_d;
802
+ assign out_pipe_status_q[0] = status_d;
803
+ assign out_pipe_tag_q[0] = mid_pipe_tag_q[NUM_MID_REGS];
804
+ assign out_pipe_mask_q[0] = mid_pipe_mask_q[NUM_MID_REGS];
805
+ assign out_pipe_aux_q[0] = mid_pipe_aux_q[NUM_MID_REGS];
806
+ assign out_pipe_valid_q[0] = mid_pipe_valid_q[NUM_MID_REGS];
807
+ // Input stage: Propagate pipeline ready signal to inside pipe
808
+ assign mid_pipe_ready[NUM_MID_REGS] = out_pipe_ready[0];
809
+ // Generate the register stages
810
+ for (genvar i = 0; i < NUM_OUT_REGS; i++) begin : gen_output_pipeline
811
+ // Internal register enable for this stage
812
+ logic reg_ena;
813
+ // Determine the ready signal of the current stage - advance the pipeline:
814
+ // 1. if the next stage is ready for our data
815
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
816
+ assign out_pipe_ready[i] = out_pipe_ready[i+1] | ~out_pipe_valid_q[i+1];
817
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
818
+ `FFLARNC(out_pipe_valid_q[i+1], out_pipe_valid_q[i], out_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
819
+ // Enable register if pipleine ready and a valid data item is present
820
+ assign reg_ena = out_pipe_ready[i] & out_pipe_valid_q[i];
821
+ // Generate the pipeline registers within the stages, use enable-registers
822
+ `FFL(out_pipe_result_q[i+1], out_pipe_result_q[i], reg_ena, '0)
823
+ `FFL(out_pipe_status_q[i+1], out_pipe_status_q[i], reg_ena, '0)
824
+ `FFL(out_pipe_tag_q[i+1], out_pipe_tag_q[i], reg_ena, TagType'('0))
825
+ `FFL(out_pipe_mask_q[i+1], out_pipe_mask_q[i], reg_ena, '0)
826
+ `FFL(out_pipe_aux_q[i+1], out_pipe_aux_q[i], reg_ena, AuxType'('0))
827
+ end
828
+ // Output stage: Ready travels backwards from output side, driven by downstream circuitry
829
+ assign out_pipe_ready[NUM_OUT_REGS] = out_ready_i;
830
+ // Output stage: assign module outputs
831
+ assign result_o = out_pipe_result_q[NUM_OUT_REGS];
832
+ assign status_o = out_pipe_status_q[NUM_OUT_REGS];
833
+ assign extension_bit_o = 1'b1; // always NaN-Box result
834
+ assign tag_o = out_pipe_tag_q[NUM_OUT_REGS];
835
+ assign mask_o = out_pipe_mask_q[NUM_OUT_REGS];
836
+ assign aux_o = out_pipe_aux_q[NUM_OUT_REGS];
837
+ assign out_valid_o = out_pipe_valid_q[NUM_OUT_REGS];
838
+ assign busy_o = (| {inp_pipe_valid_q, mid_pipe_valid_q, out_pipe_valid_q});
839
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_noncomp.sv ADDED
@@ -0,0 +1,415 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ `include "common_cells/registers.svh"
17
+
18
+ module fpnew_noncomp #(
19
+ parameter fpnew_pkg::fp_format_e FpFormat = fpnew_pkg::fp_format_e'(0),
20
+ parameter int unsigned NumPipeRegs = 0,
21
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::BEFORE,
22
+ parameter type TagType = logic,
23
+ parameter type AuxType = logic,
24
+
25
+ localparam int unsigned WIDTH = fpnew_pkg::fp_width(FpFormat) // do not change
26
+ ) (
27
+ input logic clk_i,
28
+ input logic rst_ni,
29
+ // Input signals
30
+ input logic [1:0][WIDTH-1:0] operands_i, // 2 operands
31
+ input logic [1:0] is_boxed_i, // 2 operands
32
+ input fpnew_pkg::roundmode_e rnd_mode_i,
33
+ input fpnew_pkg::operation_e op_i,
34
+ input logic op_mod_i,
35
+ input TagType tag_i,
36
+ input logic mask_i,
37
+ input AuxType aux_i,
38
+ // Input Handshake
39
+ input logic in_valid_i,
40
+ output logic in_ready_o,
41
+ input logic flush_i,
42
+ // Output signals
43
+ output logic [WIDTH-1:0] result_o,
44
+ output fpnew_pkg::status_t status_o,
45
+ output logic extension_bit_o,
46
+ output fpnew_pkg::classmask_e class_mask_o,
47
+ output logic is_class_o,
48
+ output TagType tag_o,
49
+ output logic mask_o,
50
+ output AuxType aux_o,
51
+ // Output handshake
52
+ output logic out_valid_o,
53
+ input logic out_ready_i,
54
+ // Indication of valid data in flight
55
+ output logic busy_o
56
+ );
57
+
58
+ // ----------
59
+ // Constants
60
+ // ----------
61
+ localparam int unsigned EXP_BITS = fpnew_pkg::exp_bits(FpFormat);
62
+ localparam int unsigned MAN_BITS = fpnew_pkg::man_bits(FpFormat);
63
+ // Pipelines
64
+ localparam NUM_INP_REGS = (PipeConfig == fpnew_pkg::BEFORE || PipeConfig == fpnew_pkg::INSIDE)
65
+ ? NumPipeRegs
66
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
67
+ ? ((NumPipeRegs + 1) / 2) // First to get distributed regs
68
+ : 0); // no regs here otherwise
69
+ localparam NUM_OUT_REGS = PipeConfig == fpnew_pkg::AFTER
70
+ ? NumPipeRegs
71
+ : (PipeConfig == fpnew_pkg::DISTRIBUTED
72
+ ? (NumPipeRegs / 2) // Last to get distributed regs
73
+ : 0); // no regs here otherwise
74
+
75
+ // ----------------
76
+ // Type definition
77
+ // ----------------
78
+ typedef struct packed {
79
+ logic sign;
80
+ logic [EXP_BITS-1:0] exponent;
81
+ logic [MAN_BITS-1:0] mantissa;
82
+ } fp_t;
83
+
84
+ // ---------------
85
+ // Input pipeline
86
+ // ---------------
87
+ // Input pipeline signals, index i holds signal after i register stages
88
+ logic [0:NUM_INP_REGS][1:0][WIDTH-1:0] inp_pipe_operands_q;
89
+ logic [0:NUM_INP_REGS][1:0] inp_pipe_is_boxed_q;
90
+ fpnew_pkg::roundmode_e [0:NUM_INP_REGS] inp_pipe_rnd_mode_q;
91
+ fpnew_pkg::operation_e [0:NUM_INP_REGS] inp_pipe_op_q;
92
+ logic [0:NUM_INP_REGS] inp_pipe_op_mod_q;
93
+ TagType [0:NUM_INP_REGS] inp_pipe_tag_q;
94
+ logic [0:NUM_INP_REGS] inp_pipe_mask_q;
95
+ AuxType [0:NUM_INP_REGS] inp_pipe_aux_q;
96
+ logic [0:NUM_INP_REGS] inp_pipe_valid_q;
97
+ // Ready signal is combinatorial for all stages
98
+ logic [0:NUM_INP_REGS] inp_pipe_ready;
99
+
100
+ // Input stage: First element of pipeline is taken from inputs
101
+ assign inp_pipe_operands_q[0] = operands_i;
102
+ assign inp_pipe_is_boxed_q[0] = is_boxed_i;
103
+ assign inp_pipe_rnd_mode_q[0] = rnd_mode_i;
104
+ assign inp_pipe_op_q[0] = op_i;
105
+ assign inp_pipe_op_mod_q[0] = op_mod_i;
106
+ assign inp_pipe_tag_q[0] = tag_i;
107
+ assign inp_pipe_mask_q[0] = mask_i;
108
+ assign inp_pipe_aux_q[0] = aux_i;
109
+ assign inp_pipe_valid_q[0] = in_valid_i;
110
+ // Input stage: Propagate pipeline ready signal to updtream circuitry
111
+ assign in_ready_o = inp_pipe_ready[0];
112
+ // Generate the register stages
113
+ for (genvar i = 0; i < NUM_INP_REGS; i++) begin : gen_input_pipeline
114
+ // Internal register enable for this stage
115
+ logic reg_ena;
116
+ // Determine the ready signal of the current stage - advance the pipeline:
117
+ // 1. if the next stage is ready for our data
118
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
119
+ assign inp_pipe_ready[i] = inp_pipe_ready[i+1] | ~inp_pipe_valid_q[i+1];
120
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
121
+ `FFLARNC(inp_pipe_valid_q[i+1], inp_pipe_valid_q[i], inp_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
122
+ // Enable register if pipleine ready and a valid data item is present
123
+ assign reg_ena = inp_pipe_ready[i] & inp_pipe_valid_q[i];
124
+ // Generate the pipeline registers within the stages, use enable-registers
125
+ `FFL(inp_pipe_operands_q[i+1], inp_pipe_operands_q[i], reg_ena, '0)
126
+ `FFL(inp_pipe_is_boxed_q[i+1], inp_pipe_is_boxed_q[i], reg_ena, '0)
127
+ `FFL(inp_pipe_rnd_mode_q[i+1], inp_pipe_rnd_mode_q[i], reg_ena, fpnew_pkg::RNE)
128
+ `FFL(inp_pipe_op_q[i+1], inp_pipe_op_q[i], reg_ena, fpnew_pkg::FMADD)
129
+ `FFL(inp_pipe_op_mod_q[i+1], inp_pipe_op_mod_q[i], reg_ena, '0)
130
+ `FFL(inp_pipe_tag_q[i+1], inp_pipe_tag_q[i], reg_ena, TagType'('0))
131
+ `FFL(inp_pipe_mask_q[i+1], inp_pipe_mask_q[i], reg_ena, '0)
132
+ `FFL(inp_pipe_aux_q[i+1], inp_pipe_aux_q[i], reg_ena, AuxType'('0))
133
+ end
134
+
135
+ // ---------------------
136
+ // Input classification
137
+ // ---------------------
138
+ fpnew_pkg::fp_info_t [1:0] info_q;
139
+
140
+ // Classify input
141
+ fpnew_classifier #(
142
+ .FpFormat ( FpFormat ),
143
+ .NumOperands ( 2 )
144
+ ) i_class_a (
145
+ .operands_i ( inp_pipe_operands_q[NUM_INP_REGS] ),
146
+ .is_boxed_i ( inp_pipe_is_boxed_q[NUM_INP_REGS] ),
147
+ .info_o ( info_q )
148
+ );
149
+
150
+ fp_t operand_a, operand_b;
151
+ fpnew_pkg::fp_info_t info_a, info_b;
152
+
153
+ // Packing-order-agnostic assignments
154
+ assign operand_a = inp_pipe_operands_q[NUM_INP_REGS][0];
155
+ assign operand_b = inp_pipe_operands_q[NUM_INP_REGS][1];
156
+ assign info_a = info_q[0];
157
+ assign info_b = info_q[1];
158
+
159
+ logic any_operand_inf;
160
+ logic any_operand_nan;
161
+ logic signalling_nan;
162
+
163
+ // Reduction for special case handling
164
+ assign any_operand_inf = (| {info_a.is_inf, info_b.is_inf});
165
+ assign any_operand_nan = (| {info_a.is_nan, info_b.is_nan});
166
+ assign signalling_nan = (| {info_a.is_signalling, info_b.is_signalling});
167
+
168
+ logic operands_equal, operand_a_smaller;
169
+
170
+ // Equality checks for zeroes too
171
+ assign operands_equal = (operand_a == operand_b) || (info_a.is_zero && info_b.is_zero);
172
+ // Invert result if non-zero signs involved (unsigned comparison)
173
+ assign operand_a_smaller = (operand_a < operand_b) ^ (operand_a.sign || operand_b.sign);
174
+
175
+ // ---------------
176
+ // Sign Injection
177
+ // ---------------
178
+ fp_t sgnj_result;
179
+ fpnew_pkg::status_t sgnj_status;
180
+ logic sgnj_extension_bit;
181
+
182
+ // Sign Injection - operation is encoded in rnd_mode_q:
183
+ // RNE = SGNJ, RTZ = SGNJN, RDN = SGNJX, RUP = Passthrough (no NaN-box check)
184
+ always_comb begin : sign_injections
185
+ logic sign_a, sign_b; // internal signs
186
+ // Default assignment
187
+ sgnj_result = operand_a; // result based on operand a
188
+
189
+ // NaN-boxing check will treat invalid inputs as canonical NaNs
190
+ if (!info_a.is_boxed) sgnj_result = '{sign: 1'b0, exponent: '1, mantissa: 2**(MAN_BITS-1)};
191
+
192
+ // Internal signs are treated as positive in case of non-NaN-boxed values
193
+ sign_a = operand_a.sign & info_a.is_boxed;
194
+ sign_b = operand_b.sign & info_b.is_boxed;
195
+
196
+ // Do the sign injection based on rm field
197
+ unique case (inp_pipe_rnd_mode_q[NUM_INP_REGS])
198
+ fpnew_pkg::RNE: sgnj_result.sign = sign_b; // SGNJ
199
+ fpnew_pkg::RTZ: sgnj_result.sign = ~sign_b; // SGNJN
200
+ fpnew_pkg::RDN: sgnj_result.sign = sign_a ^ sign_b; // SGNJX
201
+ fpnew_pkg::RUP: sgnj_result = operand_a; // passthrough
202
+ default: sgnj_result = '{default: fpnew_pkg::DONT_CARE}; // don't care
203
+ endcase
204
+ end
205
+
206
+ assign sgnj_status = '0; // sign injections never raise exceptions
207
+
208
+ // op_mod_q enables integer sign-extension of result (for storing to integer regfile)
209
+ assign sgnj_extension_bit = inp_pipe_op_mod_q[NUM_INP_REGS] ? sgnj_result.sign : 1'b1;
210
+
211
+ // ------------------
212
+ // Minimum / Maximum
213
+ // ------------------
214
+ fp_t minmax_result;
215
+ fpnew_pkg::status_t minmax_status;
216
+ logic minmax_extension_bit;
217
+
218
+ // Minimum/Maximum - operation is encoded in rnd_mode_q:
219
+ // RNE = MIN, RTZ = MAX
220
+ always_comb begin : min_max
221
+ // Default assignment
222
+ minmax_status = '0;
223
+
224
+ // Min/Max use quiet comparisons - only sNaN are invalid
225
+ minmax_status.NV = signalling_nan;
226
+
227
+ // Both NaN inputs cause a NaN output
228
+ if (info_a.is_nan && info_b.is_nan)
229
+ minmax_result = '{sign: 1'b0, exponent: '1, mantissa: 2**(MAN_BITS-1)}; // canonical qNaN
230
+ // If one operand is NaN, the non-NaN operand is returned
231
+ else if (info_a.is_nan) minmax_result = operand_b;
232
+ else if (info_b.is_nan) minmax_result = operand_a;
233
+ // Otherwise decide according to the operation
234
+ else begin
235
+ unique case (inp_pipe_rnd_mode_q[NUM_INP_REGS])
236
+ fpnew_pkg::RNE: minmax_result = operand_a_smaller ? operand_a : operand_b; // MIN
237
+ fpnew_pkg::RTZ: minmax_result = operand_a_smaller ? operand_b : operand_a; // MAX
238
+ default: minmax_result = '{default: fpnew_pkg::DONT_CARE}; // don't care
239
+ endcase
240
+ end
241
+ end
242
+
243
+ assign minmax_extension_bit = 1'b1; // NaN-box as result is always a float value
244
+
245
+ // ------------
246
+ // Comparisons
247
+ // ------------
248
+ fp_t cmp_result;
249
+ fpnew_pkg::status_t cmp_status;
250
+ logic cmp_extension_bit;
251
+
252
+ // Comparisons - operation is encoded in rnd_mode_q:
253
+ // RNE = LE, RTZ = LT, RDN = EQ
254
+ // op_mod_q inverts boolean outputs
255
+ always_comb begin : comparisons
256
+ // Default assignment
257
+ cmp_result = '0; // false
258
+ cmp_status = '0; // no flags
259
+
260
+ // Signalling NaNs always compare as false and are illegal
261
+ if (signalling_nan) cmp_status.NV = 1'b1; // invalid operation
262
+ // Otherwise do comparisons
263
+ else begin
264
+ unique case (inp_pipe_rnd_mode_q[NUM_INP_REGS])
265
+ fpnew_pkg::RNE: begin // Less than or equal
266
+ if (any_operand_nan) cmp_status.NV = 1'b1; // Signalling comparison: NaNs are invalid
267
+ else cmp_result = (operand_a_smaller | operands_equal) ^ inp_pipe_op_mod_q[NUM_INP_REGS];
268
+ end
269
+ fpnew_pkg::RTZ: begin // Less than
270
+ if (any_operand_nan) cmp_status.NV = 1'b1; // Signalling comparison: NaNs are invalid
271
+ else cmp_result = (operand_a_smaller & ~operands_equal) ^ inp_pipe_op_mod_q[NUM_INP_REGS];
272
+ end
273
+ fpnew_pkg::RDN: begin // Equal
274
+ if (any_operand_nan) cmp_result = inp_pipe_op_mod_q[NUM_INP_REGS]; // NaN always not equal
275
+ else cmp_result = operands_equal ^ inp_pipe_op_mod_q[NUM_INP_REGS];
276
+ end
277
+ default: cmp_result = '{default: fpnew_pkg::DONT_CARE}; // don't care
278
+ endcase
279
+ end
280
+ end
281
+
282
+ assign cmp_extension_bit = 1'b0; // Comparisons always produce booleans in integer registers
283
+
284
+ // ---------------
285
+ // Classification
286
+ // ---------------
287
+ fpnew_pkg::status_t class_status;
288
+ logic class_extension_bit;
289
+ fpnew_pkg::classmask_e class_mask_d; // the result is actually here
290
+
291
+ // Classification - always return the classification mask on the dedicated port
292
+ always_comb begin : classify
293
+ if (info_a.is_normal) begin
294
+ class_mask_d = operand_a.sign ? fpnew_pkg::NEGNORM : fpnew_pkg::POSNORM;
295
+ end else if (info_a.is_subnormal) begin
296
+ class_mask_d = operand_a.sign ? fpnew_pkg::NEGSUBNORM : fpnew_pkg::POSSUBNORM;
297
+ end else if (info_a.is_zero) begin
298
+ class_mask_d = operand_a.sign ? fpnew_pkg::NEGZERO : fpnew_pkg::POSZERO;
299
+ end else if (info_a.is_inf) begin
300
+ class_mask_d = operand_a.sign ? fpnew_pkg::NEGINF : fpnew_pkg::POSINF;
301
+ end else if (info_a.is_nan) begin
302
+ class_mask_d = info_a.is_signalling ? fpnew_pkg::SNAN : fpnew_pkg::QNAN;
303
+ end else begin
304
+ class_mask_d = fpnew_pkg::QNAN; // default value
305
+ end
306
+ end
307
+
308
+ assign class_status = '0; // classification does not set flags
309
+ assign class_extension_bit = 1'b0; // classification always produces results in integer registers
310
+
311
+ // -----------------
312
+ // Result selection
313
+ // -----------------
314
+ fp_t result_d;
315
+ fpnew_pkg::status_t status_d;
316
+ logic extension_bit_d;
317
+ logic is_class_d;
318
+
319
+ // Select result
320
+ always_comb begin : select_result
321
+ unique case (inp_pipe_op_q[NUM_INP_REGS])
322
+ fpnew_pkg::SGNJ: begin
323
+ result_d = sgnj_result;
324
+ status_d = sgnj_status;
325
+ extension_bit_d = sgnj_extension_bit;
326
+ end
327
+ fpnew_pkg::MINMAX: begin
328
+ result_d = minmax_result;
329
+ status_d = minmax_status;
330
+ extension_bit_d = minmax_extension_bit;
331
+ end
332
+ fpnew_pkg::CMP: begin
333
+ result_d = cmp_result;
334
+ status_d = cmp_status;
335
+ extension_bit_d = cmp_extension_bit;
336
+ end
337
+ fpnew_pkg::CLASSIFY: begin
338
+ result_d = '{default: fpnew_pkg::DONT_CARE}; // unused
339
+ status_d = class_status;
340
+ extension_bit_d = class_extension_bit;
341
+ end
342
+ default: begin
343
+ result_d = '{default: fpnew_pkg::DONT_CARE}; // dont care
344
+ status_d = '{default: fpnew_pkg::DONT_CARE}; // dont care
345
+ extension_bit_d = fpnew_pkg::DONT_CARE; // dont care
346
+ end
347
+ endcase
348
+ end
349
+
350
+ assign is_class_d = (inp_pipe_op_q[NUM_INP_REGS] == fpnew_pkg::CLASSIFY);
351
+
352
+ // ----------------
353
+ // Output Pipeline
354
+ // ----------------
355
+ // Output pipeline signals, index i holds signal after i register stages
356
+ fp_t [0:NUM_OUT_REGS] out_pipe_result_q;
357
+ fpnew_pkg::status_t [0:NUM_OUT_REGS] out_pipe_status_q;
358
+ logic [0:NUM_OUT_REGS] out_pipe_extension_bit_q;
359
+ fpnew_pkg::classmask_e [0:NUM_OUT_REGS] out_pipe_class_mask_q;
360
+ logic [0:NUM_OUT_REGS] out_pipe_is_class_q;
361
+ TagType [0:NUM_OUT_REGS] out_pipe_tag_q;
362
+ logic [0:NUM_OUT_REGS] out_pipe_mask_q;
363
+ AuxType [0:NUM_OUT_REGS] out_pipe_aux_q;
364
+ logic [0:NUM_OUT_REGS] out_pipe_valid_q;
365
+ // Ready signal is combinatorial for all stages
366
+ logic [0:NUM_OUT_REGS] out_pipe_ready;
367
+
368
+ // Input stage: First element of pipeline is taken from inputs
369
+ assign out_pipe_result_q[0] = result_d;
370
+ assign out_pipe_status_q[0] = status_d;
371
+ assign out_pipe_extension_bit_q[0] = extension_bit_d;
372
+ assign out_pipe_class_mask_q[0] = class_mask_d;
373
+ assign out_pipe_is_class_q[0] = is_class_d;
374
+ assign out_pipe_tag_q[0] = inp_pipe_tag_q[NUM_INP_REGS];
375
+ assign out_pipe_mask_q[0] = inp_pipe_mask_q[NUM_INP_REGS];
376
+ assign out_pipe_aux_q[0] = inp_pipe_aux_q[NUM_INP_REGS];
377
+ assign out_pipe_valid_q[0] = inp_pipe_valid_q[NUM_INP_REGS];
378
+ // Input stage: Propagate pipeline ready signal to inside pipe
379
+ assign inp_pipe_ready[NUM_INP_REGS] = out_pipe_ready[0];
380
+ // Generate the register stages
381
+ for (genvar i = 0; i < NUM_OUT_REGS; i++) begin : gen_output_pipeline
382
+ // Internal register enable for this stage
383
+ logic reg_ena;
384
+ // Determine the ready signal of the current stage - advance the pipeline:
385
+ // 1. if the next stage is ready for our data
386
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
387
+ assign out_pipe_ready[i] = out_pipe_ready[i+1] | ~out_pipe_valid_q[i+1];
388
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
389
+ `FFLARNC(out_pipe_valid_q[i+1], out_pipe_valid_q[i], out_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
390
+ // Enable register if pipleine ready and a valid data item is present
391
+ assign reg_ena = out_pipe_ready[i] & out_pipe_valid_q[i];
392
+ // Generate the pipeline registers within the stages, use enable-registers
393
+ `FFL(out_pipe_result_q[i+1], out_pipe_result_q[i], reg_ena, '0)
394
+ `FFL(out_pipe_status_q[i+1], out_pipe_status_q[i], reg_ena, '0)
395
+ `FFL(out_pipe_extension_bit_q[i+1], out_pipe_extension_bit_q[i], reg_ena, '0)
396
+ `FFL(out_pipe_class_mask_q[i+1], out_pipe_class_mask_q[i], reg_ena, fpnew_pkg::QNAN)
397
+ `FFL(out_pipe_is_class_q[i+1], out_pipe_is_class_q[i], reg_ena, '0)
398
+ `FFL(out_pipe_tag_q[i+1], out_pipe_tag_q[i], reg_ena, TagType'('0))
399
+ `FFL(out_pipe_mask_q[i+1], out_pipe_mask_q[i], reg_ena, '0)
400
+ `FFL(out_pipe_aux_q[i+1], out_pipe_aux_q[i], reg_ena, AuxType'('0))
401
+ end
402
+ // Output stage: Ready travels backwards from output side, driven by downstream circuitry
403
+ assign out_pipe_ready[NUM_OUT_REGS] = out_ready_i;
404
+ // Output stage: assign module outputs
405
+ assign result_o = out_pipe_result_q[NUM_OUT_REGS];
406
+ assign status_o = out_pipe_status_q[NUM_OUT_REGS];
407
+ assign extension_bit_o = out_pipe_extension_bit_q[NUM_OUT_REGS];
408
+ assign class_mask_o = out_pipe_class_mask_q[NUM_OUT_REGS];
409
+ assign is_class_o = out_pipe_is_class_q[NUM_OUT_REGS];
410
+ assign tag_o = out_pipe_tag_q[NUM_OUT_REGS];
411
+ assign mask_o = out_pipe_mask_q[NUM_OUT_REGS];
412
+ assign aux_o = out_pipe_aux_q[NUM_OUT_REGS];
413
+ assign out_valid_o = out_pipe_valid_q[NUM_OUT_REGS];
414
+ assign busy_o = (| {inp_pipe_valid_q, out_pipe_valid_q});
415
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_opgroup_block.sv ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ module fpnew_opgroup_block #(
17
+ parameter fpnew_pkg::opgroup_e OpGroup = fpnew_pkg::ADDMUL,
18
+ // FPU configuration
19
+ parameter int unsigned Width = 32,
20
+ parameter logic EnableVectors = 1'b1,
21
+ parameter fpnew_pkg::fmt_logic_t FpFmtMask = '1,
22
+ parameter fpnew_pkg::ifmt_logic_t IntFmtMask = '1,
23
+ parameter fpnew_pkg::fmt_unsigned_t FmtPipeRegs = '{default: 0},
24
+ parameter fpnew_pkg::fmt_unit_types_t FmtUnitTypes = '{default: fpnew_pkg::PARALLEL},
25
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::BEFORE,
26
+ parameter type TagType = logic,
27
+ parameter int unsigned TrueSIMDClass = 0,
28
+ // Do not change
29
+ localparam int unsigned NUM_FORMATS = fpnew_pkg::NUM_FP_FORMATS,
30
+ localparam int unsigned NUM_OPERANDS = fpnew_pkg::num_operands(OpGroup),
31
+ localparam int unsigned NUM_LANES = fpnew_pkg::max_num_lanes(Width, FpFmtMask, EnableVectors),
32
+ localparam type MaskType = logic [NUM_LANES-1:0]
33
+ ) (
34
+ input logic clk_i,
35
+ input logic rst_ni,
36
+ // Input signals
37
+ input logic [NUM_OPERANDS-1:0][Width-1:0] operands_i,
38
+ input logic [NUM_FORMATS-1:0][NUM_OPERANDS-1:0] is_boxed_i,
39
+ input fpnew_pkg::roundmode_e rnd_mode_i,
40
+ input fpnew_pkg::operation_e op_i,
41
+ input logic op_mod_i,
42
+ input fpnew_pkg::fp_format_e src_fmt_i,
43
+ input fpnew_pkg::fp_format_e dst_fmt_i,
44
+ input fpnew_pkg::int_format_e int_fmt_i,
45
+ input logic vectorial_op_i,
46
+ input TagType tag_i,
47
+ input MaskType simd_mask_i,
48
+ // Input Handshake
49
+ input logic in_valid_i,
50
+ output logic in_ready_o,
51
+ input logic flush_i,
52
+ // Output signals
53
+ output logic [Width-1:0] result_o,
54
+ output fpnew_pkg::status_t status_o,
55
+ output logic extension_bit_o,
56
+ output TagType tag_o,
57
+ // Output handshake
58
+ output logic out_valid_o,
59
+ input logic out_ready_i,
60
+ // Indication of valid data in flight
61
+ output logic busy_o
62
+ );
63
+
64
+ // ----------------
65
+ // Type Definition
66
+ // ----------------
67
+ typedef struct packed {
68
+ logic [Width-1:0] result;
69
+ fpnew_pkg::status_t status;
70
+ logic ext_bit;
71
+ TagType tag;
72
+ } output_t;
73
+
74
+ // Handshake signals for the slices
75
+ logic [NUM_FORMATS-1:0] fmt_in_ready, fmt_out_valid, fmt_out_ready, fmt_busy;
76
+ output_t [NUM_FORMATS-1:0] fmt_outputs;
77
+
78
+ // -----------
79
+ // Input Side
80
+ // -----------
81
+ assign in_ready_o = in_valid_i & fmt_in_ready[dst_fmt_i]; // Ready is given by selected format
82
+
83
+ // -------------------------
84
+ // Generate Parallel Slices
85
+ // -------------------------
86
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_parallel_slices
87
+ // Some constants for this format
88
+ localparam logic ANY_MERGED = fpnew_pkg::any_enabled_multi(FmtUnitTypes, FpFmtMask);
89
+ localparam logic IS_FIRST_MERGED =
90
+ fpnew_pkg::is_first_enabled_multi(fpnew_pkg::fp_format_e'(fmt), FmtUnitTypes, FpFmtMask);
91
+
92
+ // Generate slice only if format enabled
93
+ if (FpFmtMask[fmt] && (FmtUnitTypes[fmt] == fpnew_pkg::PARALLEL)) begin : active_format
94
+
95
+ logic in_valid;
96
+
97
+ assign in_valid = in_valid_i & (dst_fmt_i == fmt); // enable selected format
98
+
99
+ // Forward masks related to the right SIMD lane
100
+ localparam int unsigned INTERNAL_LANES = fpnew_pkg::num_lanes(Width, fpnew_pkg::fp_format_e'(fmt), EnableVectors);
101
+ logic [INTERNAL_LANES-1:0] mask_slice;
102
+ always_comb for (int b = 0; b < INTERNAL_LANES; b++) mask_slice[b] = simd_mask_i[(NUM_LANES/INTERNAL_LANES)*b];
103
+
104
+ fpnew_opgroup_fmt_slice #(
105
+ .OpGroup ( OpGroup ),
106
+ .FpFormat ( fpnew_pkg::fp_format_e'(fmt) ),
107
+ .Width ( Width ),
108
+ .EnableVectors ( EnableVectors ),
109
+ .NumPipeRegs ( FmtPipeRegs[fmt] ),
110
+ .PipeConfig ( PipeConfig ),
111
+ .TagType ( TagType ),
112
+ .TrueSIMDClass ( TrueSIMDClass )
113
+ ) i_fmt_slice (
114
+ .clk_i,
115
+ .rst_ni,
116
+ .operands_i ( operands_i ),
117
+ .is_boxed_i ( is_boxed_i[fmt] ),
118
+ .rnd_mode_i,
119
+ .op_i,
120
+ .op_mod_i,
121
+ .vectorial_op_i,
122
+ .tag_i,
123
+ .simd_mask_i ( mask_slice ),
124
+ .in_valid_i ( in_valid ),
125
+ .in_ready_o ( fmt_in_ready[fmt] ),
126
+ .flush_i,
127
+ .result_o ( fmt_outputs[fmt].result ),
128
+ .status_o ( fmt_outputs[fmt].status ),
129
+ .extension_bit_o( fmt_outputs[fmt].ext_bit ),
130
+ .tag_o ( fmt_outputs[fmt].tag ),
131
+ .out_valid_o ( fmt_out_valid[fmt] ),
132
+ .out_ready_i ( fmt_out_ready[fmt] ),
133
+ .busy_o ( fmt_busy[fmt] )
134
+ );
135
+ // If the format wants to use merged ops, tie off the dangling ones not used here
136
+ end else if (FpFmtMask[fmt] && ANY_MERGED && !IS_FIRST_MERGED) begin : merged_unused
137
+
138
+ localparam FMT = fpnew_pkg::get_first_enabled_multi(FmtUnitTypes, FpFmtMask);
139
+ // Ready is split up into formats
140
+ assign fmt_in_ready[fmt] = fmt_in_ready[int'(FMT)];
141
+
142
+ assign fmt_out_valid[fmt] = 1'b0; // don't emit values
143
+ assign fmt_busy[fmt] = 1'b0; // never busy
144
+ // Outputs are don't care
145
+ assign fmt_outputs[fmt].result = '{default: fpnew_pkg::DONT_CARE};
146
+ assign fmt_outputs[fmt].status = '{default: fpnew_pkg::DONT_CARE};
147
+ assign fmt_outputs[fmt].ext_bit = fpnew_pkg::DONT_CARE;
148
+ assign fmt_outputs[fmt].tag = TagType'(fpnew_pkg::DONT_CARE);
149
+
150
+ // Tie off disabled formats
151
+ end else if (!FpFmtMask[fmt] || (FmtUnitTypes[fmt] == fpnew_pkg::DISABLED)) begin : disable_fmt
152
+ assign fmt_in_ready[fmt] = 1'b0; // don't accept operations
153
+ assign fmt_out_valid[fmt] = 1'b0; // don't emit values
154
+ assign fmt_busy[fmt] = 1'b0; // never busy
155
+ // Outputs are don't care
156
+ assign fmt_outputs[fmt].result = '{default: fpnew_pkg::DONT_CARE};
157
+ assign fmt_outputs[fmt].status = '{default: fpnew_pkg::DONT_CARE};
158
+ assign fmt_outputs[fmt].ext_bit = fpnew_pkg::DONT_CARE;
159
+ assign fmt_outputs[fmt].tag = TagType'(fpnew_pkg::DONT_CARE);
160
+ end
161
+ end
162
+
163
+ // ----------------------
164
+ // Generate Merged Slice
165
+ // ----------------------
166
+ if (fpnew_pkg::any_enabled_multi(FmtUnitTypes, FpFmtMask)) begin : gen_merged_slice
167
+
168
+ localparam FMT = fpnew_pkg::get_first_enabled_multi(FmtUnitTypes, FpFmtMask);
169
+ localparam REG = fpnew_pkg::get_num_regs_multi(FmtPipeRegs, FmtUnitTypes, FpFmtMask);
170
+
171
+ logic in_valid;
172
+
173
+ assign in_valid = in_valid_i & (FmtUnitTypes[dst_fmt_i] == fpnew_pkg::MERGED);
174
+
175
+ fpnew_opgroup_multifmt_slice #(
176
+ .OpGroup ( OpGroup ),
177
+ .Width ( Width ),
178
+ .FpFmtConfig ( FpFmtMask ),
179
+ .IntFmtConfig ( IntFmtMask ),
180
+ .EnableVectors ( EnableVectors ),
181
+ .NumPipeRegs ( REG ),
182
+ .PipeConfig ( PipeConfig ),
183
+ .TagType ( TagType )
184
+ ) i_multifmt_slice (
185
+ .clk_i,
186
+ .rst_ni,
187
+ .operands_i,
188
+ .is_boxed_i,
189
+ .rnd_mode_i,
190
+ .op_i,
191
+ .op_mod_i,
192
+ .src_fmt_i,
193
+ .dst_fmt_i,
194
+ .int_fmt_i,
195
+ .vectorial_op_i,
196
+ .tag_i,
197
+ .simd_mask_i ( simd_mask_i ),
198
+ .in_valid_i ( in_valid ),
199
+ .in_ready_o ( fmt_in_ready[FMT] ),
200
+ .flush_i,
201
+ .result_o ( fmt_outputs[FMT].result ),
202
+ .status_o ( fmt_outputs[FMT].status ),
203
+ .extension_bit_o ( fmt_outputs[FMT].ext_bit ),
204
+ .tag_o ( fmt_outputs[FMT].tag ),
205
+ .out_valid_o ( fmt_out_valid[FMT] ),
206
+ .out_ready_i ( fmt_out_ready[FMT] ),
207
+ .busy_o ( fmt_busy[FMT] )
208
+ );
209
+
210
+ end
211
+
212
+ // ------------------
213
+ // Arbitrate Outputs
214
+ // ------------------
215
+ output_t arbiter_output;
216
+
217
+ // Round-Robin arbiter to decide which result to use
218
+ rr_arb_tree #(
219
+ .NumIn ( NUM_FORMATS ),
220
+ .DataType ( output_t ),
221
+ .AxiVldRdy ( 1'b1 )
222
+ ) i_arbiter (
223
+ .clk_i,
224
+ .rst_ni,
225
+ .flush_i,
226
+ .rr_i ( '0 ),
227
+ .req_i ( fmt_out_valid ),
228
+ .gnt_o ( fmt_out_ready ),
229
+ .data_i ( fmt_outputs ),
230
+ .gnt_i ( out_ready_i ),
231
+ .req_o ( out_valid_o ),
232
+ .data_o ( arbiter_output ),
233
+ .idx_o ( /* unused */ )
234
+ );
235
+
236
+ // Unpack output
237
+ assign result_o = arbiter_output.result;
238
+ assign status_o = arbiter_output.status;
239
+ assign extension_bit_o = arbiter_output.ext_bit;
240
+ assign tag_o = arbiter_output.tag;
241
+
242
+ assign busy_o = (| fmt_busy);
243
+
244
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_opgroup_fmt_slice.sv ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ module fpnew_opgroup_fmt_slice #(
17
+ parameter fpnew_pkg::opgroup_e OpGroup = fpnew_pkg::ADDMUL,
18
+ parameter fpnew_pkg::fp_format_e FpFormat = fpnew_pkg::fp_format_e'(0),
19
+ // FPU configuration
20
+ parameter int unsigned Width = 32,
21
+ parameter logic EnableVectors = 1'b1,
22
+ parameter int unsigned NumPipeRegs = 0,
23
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::BEFORE,
24
+ parameter type TagType = logic,
25
+ parameter int unsigned TrueSIMDClass = 0,
26
+ // Do not change
27
+ localparam int unsigned NUM_OPERANDS = fpnew_pkg::num_operands(OpGroup),
28
+ localparam int unsigned NUM_LANES = fpnew_pkg::num_lanes(Width, FpFormat, EnableVectors),
29
+ localparam type MaskType = logic [NUM_LANES-1:0]
30
+ ) (
31
+ input logic clk_i,
32
+ input logic rst_ni,
33
+ // Input signals
34
+ input logic [NUM_OPERANDS-1:0][Width-1:0] operands_i,
35
+ input logic [NUM_OPERANDS-1:0] is_boxed_i,
36
+ input fpnew_pkg::roundmode_e rnd_mode_i,
37
+ input fpnew_pkg::operation_e op_i,
38
+ input logic op_mod_i,
39
+ input logic vectorial_op_i,
40
+ input TagType tag_i,
41
+ input MaskType simd_mask_i,
42
+ // Input Handshake
43
+ input logic in_valid_i,
44
+ output logic in_ready_o,
45
+ input logic flush_i,
46
+ // Output signals
47
+ output logic [Width-1:0] result_o,
48
+ output fpnew_pkg::status_t status_o,
49
+ output logic extension_bit_o,
50
+ output TagType tag_o,
51
+ // Output handshake
52
+ output logic out_valid_o,
53
+ input logic out_ready_i,
54
+ // Indication of valid data in flight
55
+ output logic busy_o
56
+ );
57
+
58
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(FpFormat);
59
+ localparam int unsigned SIMD_WIDTH = unsigned'(Width/NUM_LANES);
60
+
61
+
62
+ logic [NUM_LANES-1:0] lane_in_ready, lane_out_valid; // Handshake signals for the lanes
63
+ logic vectorial_op;
64
+
65
+ logic [NUM_LANES*FP_WIDTH-1:0] slice_result;
66
+ logic [Width-1:0] slice_regular_result, slice_class_result, slice_vec_class_result;
67
+
68
+ fpnew_pkg::status_t [NUM_LANES-1:0] lane_status;
69
+ logic [NUM_LANES-1:0] lane_ext_bit; // only the first one is actually used
70
+ fpnew_pkg::classmask_e [NUM_LANES-1:0] lane_class_mask;
71
+ TagType [NUM_LANES-1:0] lane_tags; // only the first one is actually used
72
+ logic [NUM_LANES-1:0] lane_masks;
73
+ logic [NUM_LANES-1:0] lane_vectorial, lane_busy, lane_is_class; // dito
74
+
75
+ logic result_is_vector, result_is_class;
76
+
77
+ // -----------
78
+ // Input Side
79
+ // -----------
80
+ assign in_ready_o = lane_in_ready[0]; // Upstream ready is given by first lane
81
+ assign vectorial_op = vectorial_op_i & EnableVectors; // only do vectorial stuff if enabled
82
+
83
+ // ---------------
84
+ // Generate Lanes
85
+ // ---------------
86
+ for (genvar lane = 0; lane < int'(NUM_LANES); lane++) begin : gen_num_lanes
87
+ logic [FP_WIDTH-1:0] local_result; // lane-local results
88
+ logic local_sign;
89
+
90
+ // Generate instances only if needed, lane 0 always generated
91
+ if ((lane == 0) || EnableVectors) begin : active_lane
92
+ logic in_valid, out_valid, out_ready; // lane-local handshake
93
+
94
+ logic [NUM_OPERANDS-1:0][FP_WIDTH-1:0] local_operands; // lane-local operands
95
+ logic [FP_WIDTH-1:0] op_result; // lane-local results
96
+ fpnew_pkg::status_t op_status;
97
+
98
+ assign in_valid = in_valid_i & ((lane == 0) | vectorial_op); // upper lanes only for vectors
99
+ // Slice out the operands for this lane
100
+ always_comb begin : prepare_input
101
+ for (int i = 0; i < int'(NUM_OPERANDS); i++) begin
102
+ local_operands[i] = operands_i[i][(unsigned'(lane)+1)*FP_WIDTH-1:unsigned'(lane)*FP_WIDTH];
103
+ end
104
+ end
105
+
106
+ // Instantiate the operation from the selected opgroup
107
+ if (OpGroup == fpnew_pkg::ADDMUL) begin : lane_instance
108
+ fpnew_fma #(
109
+ .FpFormat ( FpFormat ),
110
+ .NumPipeRegs ( NumPipeRegs ),
111
+ .PipeConfig ( PipeConfig ),
112
+ .TagType ( TagType ),
113
+ .AuxType ( logic )
114
+ ) i_fma (
115
+ .clk_i,
116
+ .rst_ni,
117
+ .operands_i ( local_operands ),
118
+ .is_boxed_i ( is_boxed_i[NUM_OPERANDS-1:0] ),
119
+ .rnd_mode_i,
120
+ .op_i,
121
+ .op_mod_i,
122
+ .tag_i,
123
+ .mask_i ( simd_mask_i[lane] ),
124
+ .aux_i ( vectorial_op ), // Remember whether operation was vectorial
125
+ .in_valid_i ( in_valid ),
126
+ .in_ready_o ( lane_in_ready[lane] ),
127
+ .flush_i,
128
+ .result_o ( op_result ),
129
+ .status_o ( op_status ),
130
+ .extension_bit_o ( lane_ext_bit[lane] ),
131
+ .tag_o ( lane_tags[lane] ),
132
+ .mask_o ( lane_masks[lane] ),
133
+ .aux_o ( lane_vectorial[lane] ),
134
+ .out_valid_o ( out_valid ),
135
+ .out_ready_i ( out_ready ),
136
+ .busy_o ( lane_busy[lane] )
137
+ );
138
+ assign lane_is_class[lane] = 1'b0;
139
+ assign lane_class_mask[lane] = fpnew_pkg::NEGINF;
140
+ end else if (OpGroup == fpnew_pkg::DIVSQRT) begin : lane_instance
141
+ // fpnew_divsqrt #(
142
+ // .FpFormat (FpFormat),
143
+ // .NumPipeRegs(NumPipeRegs),
144
+ // .PipeConfig (PipeConfig),
145
+ // .TagType (TagType),
146
+ // .AuxType (logic)
147
+ // ) i_divsqrt (
148
+ // .clk_i,
149
+ // .rst_ni,
150
+ // .operands_i ( local_operands ),
151
+ // .is_boxed_i ( is_boxed_i[NUM_OPERANDS-1:0] ),
152
+ // .rnd_mode_i,
153
+ // .op_i,
154
+ // .op_mod_i,
155
+ // .tag_i,
156
+ // .aux_i ( vectorial_op ), // Remember whether operation was vectorial
157
+ // .in_valid_i ( in_valid ),
158
+ // .in_ready_o ( lane_in_ready[lane] ),
159
+ // .flush_i,
160
+ // .result_o ( op_result ),
161
+ // .status_o ( op_status ),
162
+ // .extension_bit_o ( lane_ext_bit[lane] ),
163
+ // .tag_o ( lane_tags[lane] ),
164
+ // .aux_o ( lane_vectorial[lane] ),
165
+ // .out_valid_o ( out_valid ),
166
+ // .out_ready_i ( out_ready ),
167
+ // .busy_o ( lane_busy[lane] )
168
+ // );
169
+ // assign lane_is_class[lane] = 1'b0;
170
+ end else if (OpGroup == fpnew_pkg::NONCOMP) begin : lane_instance
171
+ fpnew_noncomp #(
172
+ .FpFormat (FpFormat),
173
+ .NumPipeRegs(NumPipeRegs),
174
+ .PipeConfig (PipeConfig),
175
+ .TagType (TagType),
176
+ .AuxType (logic)
177
+ ) i_noncomp (
178
+ .clk_i,
179
+ .rst_ni,
180
+ .operands_i ( local_operands ),
181
+ .is_boxed_i ( is_boxed_i[NUM_OPERANDS-1:0] ),
182
+ .rnd_mode_i,
183
+ .op_i,
184
+ .op_mod_i,
185
+ .tag_i,
186
+ .mask_i ( simd_mask_i[lane] ),
187
+ .aux_i ( vectorial_op ), // Remember whether operation was vectorial
188
+ .in_valid_i ( in_valid ),
189
+ .in_ready_o ( lane_in_ready[lane] ),
190
+ .flush_i,
191
+ .result_o ( op_result ),
192
+ .status_o ( op_status ),
193
+ .extension_bit_o ( lane_ext_bit[lane] ),
194
+ .class_mask_o ( lane_class_mask[lane] ),
195
+ .is_class_o ( lane_is_class[lane] ),
196
+ .tag_o ( lane_tags[lane] ),
197
+ .mask_o ( lane_masks[lane] ),
198
+ .aux_o ( lane_vectorial[lane] ),
199
+ .out_valid_o ( out_valid ),
200
+ .out_ready_i ( out_ready ),
201
+ .busy_o ( lane_busy[lane] )
202
+ );
203
+ end // ADD OTHER OPTIONS HERE
204
+
205
+ // Handshakes are only done if the lane is actually used
206
+ assign out_ready = out_ready_i & ((lane == 0) | result_is_vector);
207
+ assign lane_out_valid[lane] = out_valid & ((lane == 0) | result_is_vector);
208
+
209
+ // Properly NaN-box or sign-extend the slice result if not in use
210
+ assign local_result = lane_out_valid[lane] ? op_result : '{default: lane_ext_bit[0]};
211
+ assign lane_status[lane] = lane_out_valid[lane] ? op_status : '0;
212
+
213
+ // Otherwise generate constant sign-extension
214
+ end else begin
215
+ assign lane_out_valid[lane] = 1'b0; // unused lane
216
+ assign lane_in_ready[lane] = 1'b0; // unused lane
217
+ assign local_result = '{default: lane_ext_bit[0]}; // sign-extend/nan box
218
+ assign lane_status[lane] = '0;
219
+ assign lane_busy[lane] = 1'b0;
220
+ assign lane_is_class[lane] = 1'b0;
221
+ end
222
+
223
+ // Insert lane result into slice result
224
+ assign slice_result[(unsigned'(lane)+1)*FP_WIDTH-1:unsigned'(lane)*FP_WIDTH] = local_result;
225
+
226
+ // Create Classification results
227
+ if (TrueSIMDClass && SIMD_WIDTH >= 10) begin : vectorial_true_class // true vectorial class blocks are 10bits in size
228
+ assign slice_vec_class_result[lane*SIMD_WIDTH +: 10] = lane_class_mask[lane];
229
+ assign slice_vec_class_result[(lane+1)*SIMD_WIDTH-1 -: SIMD_WIDTH-10] = '0;
230
+ end else if ((lane+1)*8 <= Width) begin : vectorial_class // vectorial class blocks are 8bits in size
231
+ assign local_sign = (lane_class_mask[lane] == fpnew_pkg::NEGINF ||
232
+ lane_class_mask[lane] == fpnew_pkg::NEGNORM ||
233
+ lane_class_mask[lane] == fpnew_pkg::NEGSUBNORM ||
234
+ lane_class_mask[lane] == fpnew_pkg::NEGZERO);
235
+ // Write the current block segment
236
+ assign slice_vec_class_result[(lane+1)*8-1:lane*8] = {
237
+ local_sign, // BIT 7
238
+ ~local_sign, // BIT 6
239
+ lane_class_mask[lane] == fpnew_pkg::QNAN, // BIT 5
240
+ lane_class_mask[lane] == fpnew_pkg::SNAN, // BIT 4
241
+ lane_class_mask[lane] == fpnew_pkg::POSZERO
242
+ || lane_class_mask[lane] == fpnew_pkg::NEGZERO, // BIT 3
243
+ lane_class_mask[lane] == fpnew_pkg::POSSUBNORM
244
+ || lane_class_mask[lane] == fpnew_pkg::NEGSUBNORM, // BIT 2
245
+ lane_class_mask[lane] == fpnew_pkg::POSNORM
246
+ || lane_class_mask[lane] == fpnew_pkg::NEGNORM, // BIT 1
247
+ lane_class_mask[lane] == fpnew_pkg::POSINF
248
+ || lane_class_mask[lane] == fpnew_pkg::NEGINF // BIT 0
249
+ };
250
+ end
251
+ end
252
+
253
+ // ------------
254
+ // Output Side
255
+ // ------------
256
+ assign result_is_vector = lane_vectorial[0];
257
+ assign result_is_class = lane_is_class[0];
258
+
259
+ assign slice_regular_result = $signed({extension_bit_o, slice_result});
260
+
261
+ localparam int unsigned CLASS_VEC_BITS = (NUM_LANES*8 > Width) ? 8 * (Width / 8) : NUM_LANES*8;
262
+
263
+ // Pad out unused vec_class bits if each classify result is on 8 bits
264
+ if (!(TrueSIMDClass && SIMD_WIDTH >= 10)) begin
265
+ if (CLASS_VEC_BITS < Width) begin : pad_vectorial_class
266
+ assign slice_vec_class_result[Width-1:CLASS_VEC_BITS] = '0;
267
+ end
268
+ end
269
+
270
+ // localparam logic [Width-1:0] CLASS_VEC_MASK = 2**CLASS_VEC_BITS - 1;
271
+
272
+ assign slice_class_result = result_is_vector ? slice_vec_class_result : lane_class_mask[0];
273
+
274
+ // Select the proper result
275
+ assign result_o = result_is_class ? slice_class_result : slice_regular_result;
276
+
277
+ assign extension_bit_o = lane_ext_bit[0]; // upper lanes unused
278
+ assign tag_o = lane_tags[0]; // upper lanes unused
279
+ assign busy_o = (| lane_busy);
280
+ assign out_valid_o = lane_out_valid[0]; // upper lanes unused
281
+
282
+
283
+ // Collapse the lane status
284
+ always_comb begin : output_processing
285
+ // Collapse the status
286
+ automatic fpnew_pkg::status_t temp_status;
287
+ temp_status = '0;
288
+ for (int i = 0; i < int'(NUM_LANES); i++)
289
+ temp_status |= lane_status[i] & {5{lane_masks[i]}};
290
+ status_o = temp_status;
291
+ end
292
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_opgroup_multifmt_slice.sv ADDED
@@ -0,0 +1,449 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ `include "common_cells/registers.svh"
17
+
18
+ module fpnew_opgroup_multifmt_slice #(
19
+ parameter fpnew_pkg::opgroup_e OpGroup = fpnew_pkg::CONV,
20
+ parameter int unsigned Width = 64,
21
+ // FPU configuration
22
+ parameter fpnew_pkg::fmt_logic_t FpFmtConfig = '1,
23
+ parameter fpnew_pkg::ifmt_logic_t IntFmtConfig = '1,
24
+ parameter logic EnableVectors = 1'b1,
25
+ parameter int unsigned NumPipeRegs = 0,
26
+ parameter fpnew_pkg::pipe_config_t PipeConfig = fpnew_pkg::BEFORE,
27
+ parameter type TagType = logic,
28
+ // Do not change
29
+ localparam int unsigned NUM_OPERANDS = fpnew_pkg::num_operands(OpGroup),
30
+ localparam int unsigned NUM_FORMATS = fpnew_pkg::NUM_FP_FORMATS,
31
+ localparam int unsigned NUM_SIMD_LANES = fpnew_pkg::max_num_lanes(Width, FpFmtConfig, EnableVectors),
32
+ localparam type MaskType = logic [NUM_SIMD_LANES-1:0]
33
+ ) (
34
+ input logic clk_i,
35
+ input logic rst_ni,
36
+ // Input signals
37
+ input logic [NUM_OPERANDS-1:0][Width-1:0] operands_i,
38
+ input logic [NUM_FORMATS-1:0][NUM_OPERANDS-1:0] is_boxed_i,
39
+ input fpnew_pkg::roundmode_e rnd_mode_i,
40
+ input fpnew_pkg::operation_e op_i,
41
+ input logic op_mod_i,
42
+ input fpnew_pkg::fp_format_e src_fmt_i,
43
+ input fpnew_pkg::fp_format_e dst_fmt_i,
44
+ input fpnew_pkg::int_format_e int_fmt_i,
45
+ input logic vectorial_op_i,
46
+ input TagType tag_i,
47
+ input MaskType simd_mask_i,
48
+ // Input Handshake
49
+ input logic in_valid_i,
50
+ output logic in_ready_o,
51
+ input logic flush_i,
52
+ // Output signals
53
+ output logic [Width-1:0] result_o,
54
+ output fpnew_pkg::status_t status_o,
55
+ output logic extension_bit_o,
56
+ output TagType tag_o,
57
+ // Output handshake
58
+ output logic out_valid_o,
59
+ input logic out_ready_i,
60
+ // Indication of valid data in flight
61
+ output logic busy_o
62
+ );
63
+
64
+ localparam int unsigned MAX_FP_WIDTH = fpnew_pkg::max_fp_width(FpFmtConfig);
65
+ localparam int unsigned MAX_INT_WIDTH = fpnew_pkg::max_int_width(IntFmtConfig);
66
+ localparam int unsigned NUM_LANES = fpnew_pkg::max_num_lanes(Width, FpFmtConfig, 1'b1);
67
+ localparam int unsigned NUM_INT_FORMATS = fpnew_pkg::NUM_INT_FORMATS;
68
+ // We will send the format information along with the data
69
+ localparam int unsigned FMT_BITS =
70
+ fpnew_pkg::maximum($clog2(NUM_FORMATS), $clog2(NUM_INT_FORMATS));
71
+ localparam int unsigned AUX_BITS = FMT_BITS + 2; // also add vectorial and integer flags
72
+
73
+ logic [NUM_LANES-1:0] lane_in_ready, lane_out_valid, divsqrt_done, divsqrt_ready; // Handshake signals for the lanes
74
+ logic vectorial_op;
75
+ logic [FMT_BITS-1:0] dst_fmt; // destination format to pass along with operation
76
+ logic [AUX_BITS-1:0] aux_data;
77
+
78
+ // additional flags for CONV
79
+ logic dst_fmt_is_int, dst_is_cpk;
80
+ logic [1:0] dst_vec_op; // info for vectorial results (for packing)
81
+ logic [2:0] target_aux_d, target_aux_q;
82
+ logic is_up_cast, is_down_cast;
83
+
84
+ logic [NUM_FORMATS-1:0][Width-1:0] fmt_slice_result;
85
+ logic [NUM_INT_FORMATS-1:0][Width-1:0] ifmt_slice_result;
86
+ logic [Width-1:0] conv_slice_result;
87
+
88
+
89
+ logic [Width-1:0] conv_target_d, conv_target_q; // vectorial conversions update a register
90
+
91
+ fpnew_pkg::status_t [NUM_LANES-1:0] lane_status;
92
+ logic [NUM_LANES-1:0] lane_ext_bit; // only the first one is actually used
93
+ TagType [NUM_LANES-1:0] lane_tags; // only the first one is actually used
94
+ logic [NUM_LANES-1:0] lane_masks;
95
+ logic [NUM_LANES-1:0][AUX_BITS-1:0] lane_aux; // only the first one is actually used
96
+ logic [NUM_LANES-1:0] lane_busy; // dito
97
+
98
+ logic result_is_vector;
99
+ logic [FMT_BITS-1:0] result_fmt;
100
+ logic result_fmt_is_int, result_is_cpk;
101
+ logic [1:0] result_vec_op; // info for vectorial results (for packing)
102
+
103
+ logic simd_synch_rdy, simd_synch_done;
104
+
105
+ // -----------
106
+ // Input Side
107
+ // -----------
108
+ assign in_ready_o = lane_in_ready[0]; // Upstream ready is given by first lane
109
+ assign vectorial_op = vectorial_op_i & EnableVectors; // only do vectorial stuff if enabled
110
+
111
+ // Cast-and-Pack ops are encoded in operation and modifier
112
+ assign dst_fmt_is_int = (OpGroup == fpnew_pkg::CONV) & (op_i == fpnew_pkg::F2I);
113
+ assign dst_is_cpk = (OpGroup == fpnew_pkg::CONV) & (op_i == fpnew_pkg::CPKAB ||
114
+ op_i == fpnew_pkg::CPKCD);
115
+ assign dst_vec_op = (OpGroup == fpnew_pkg::CONV) & {(op_i == fpnew_pkg::CPKCD), op_mod_i};
116
+
117
+ assign is_up_cast = (fpnew_pkg::fp_width(dst_fmt_i) > fpnew_pkg::fp_width(src_fmt_i));
118
+ assign is_down_cast = (fpnew_pkg::fp_width(dst_fmt_i) < fpnew_pkg::fp_width(src_fmt_i));
119
+
120
+ // The destination format is the int format for F2I casts
121
+ assign dst_fmt = dst_fmt_is_int ? int_fmt_i : dst_fmt_i;
122
+
123
+ // The data sent along consists of the vectorial flag and format bits
124
+ assign aux_data = {dst_fmt_is_int, vectorial_op, dst_fmt};
125
+ assign target_aux_d = {dst_vec_op, dst_is_cpk};
126
+
127
+ // CONV passes one operand for assembly after the unit: opC for cpk, opB for others
128
+ if (OpGroup == fpnew_pkg::CONV) begin : conv_target
129
+ assign conv_target_d = dst_is_cpk ? operands_i[2] : operands_i[1];
130
+ end
131
+
132
+ // For 2-operand units, prepare boxing info
133
+ logic [NUM_FORMATS-1:0] is_boxed_1op;
134
+ logic [NUM_FORMATS-1:0][1:0] is_boxed_2op;
135
+
136
+ always_comb begin : boxed_2op
137
+ for (int fmt = 0; fmt < NUM_FORMATS; fmt++) begin
138
+ is_boxed_1op[fmt] = is_boxed_i[fmt][0];
139
+ is_boxed_2op[fmt] = is_boxed_i[fmt][1:0];
140
+ end
141
+ end
142
+
143
+ // ---------------
144
+ // Generate Lanes
145
+ // ---------------
146
+ for (genvar lane = 0; lane < int'(NUM_LANES); lane++) begin : gen_num_lanes
147
+ localparam int unsigned LANE = unsigned'(lane); // unsigned to please the linter
148
+ // Get a mask of active formats for this lane
149
+ localparam fpnew_pkg::fmt_logic_t ACTIVE_FORMATS =
150
+ fpnew_pkg::get_lane_formats(Width, FpFmtConfig, LANE);
151
+ localparam fpnew_pkg::ifmt_logic_t ACTIVE_INT_FORMATS =
152
+ fpnew_pkg::get_lane_int_formats(Width, FpFmtConfig, IntFmtConfig, LANE);
153
+ localparam int unsigned MAX_WIDTH = fpnew_pkg::max_fp_width(ACTIVE_FORMATS);
154
+
155
+ // Cast-specific parameters
156
+ localparam fpnew_pkg::fmt_logic_t CONV_FORMATS =
157
+ fpnew_pkg::get_conv_lane_formats(Width, FpFmtConfig, LANE);
158
+ localparam fpnew_pkg::ifmt_logic_t CONV_INT_FORMATS =
159
+ fpnew_pkg::get_conv_lane_int_formats(Width, FpFmtConfig, IntFmtConfig, LANE);
160
+ localparam int unsigned CONV_WIDTH = fpnew_pkg::max_fp_width(CONV_FORMATS);
161
+
162
+ // Lane parameters from Opgroup
163
+ localparam fpnew_pkg::fmt_logic_t LANE_FORMATS = (OpGroup == fpnew_pkg::CONV)
164
+ ? CONV_FORMATS : ACTIVE_FORMATS;
165
+ localparam int unsigned LANE_WIDTH = (OpGroup == fpnew_pkg::CONV) ? CONV_WIDTH : MAX_WIDTH;
166
+
167
+ logic [LANE_WIDTH-1:0] local_result; // lane-local results
168
+
169
+ // Generate instances only if needed, lane 0 always generated
170
+ if ((lane == 0) || EnableVectors) begin : active_lane
171
+ logic in_valid, out_valid, out_ready; // lane-local handshake
172
+
173
+ logic [NUM_OPERANDS-1:0][LANE_WIDTH-1:0] local_operands; // lane-local oprands
174
+ logic [LANE_WIDTH-1:0] op_result; // lane-local results
175
+ fpnew_pkg::status_t op_status;
176
+
177
+ assign in_valid = in_valid_i & ((lane == 0) | vectorial_op); // upper lanes only for vectors
178
+
179
+ // Slice out the operands for this lane, upper bits are ignored in the unit
180
+ always_comb begin : prepare_input
181
+ for (int unsigned i = 0; i < NUM_OPERANDS; i++) begin
182
+ local_operands[i] = operands_i[i] >> LANE*fpnew_pkg::fp_width(src_fmt_i);
183
+ end
184
+
185
+ // override operand 0 for some conversions
186
+ if (OpGroup == fpnew_pkg::CONV) begin
187
+ // Source is an integer
188
+ if (op_i == fpnew_pkg::I2F) begin
189
+ local_operands[0] = operands_i[0] >> LANE*fpnew_pkg::int_width(int_fmt_i);
190
+ // vectorial F2F up casts
191
+ end else if (op_i == fpnew_pkg::F2F) begin
192
+ if (vectorial_op && op_mod_i && is_up_cast) begin // up cast with upper half
193
+ local_operands[0] = operands_i[0] >> LANE*fpnew_pkg::fp_width(src_fmt_i) +
194
+ MAX_FP_WIDTH/2;
195
+ end
196
+ // CPK
197
+ end else if (dst_is_cpk) begin
198
+ if (lane == 1) begin
199
+ local_operands[0] = operands_i[1][LANE_WIDTH-1:0]; // using opB as second argument
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ // Instantiate the operation from the selected opgroup
206
+ if (OpGroup == fpnew_pkg::ADDMUL) begin : lane_instance
207
+ fpnew_fma_multi #(
208
+ .FpFmtConfig ( LANE_FORMATS ),
209
+ .NumPipeRegs ( NumPipeRegs ),
210
+ .PipeConfig ( PipeConfig ),
211
+ .TagType ( TagType ),
212
+ .AuxType ( logic [AUX_BITS-1:0] )
213
+ ) i_fpnew_fma_multi (
214
+ .clk_i,
215
+ .rst_ni,
216
+ .operands_i ( local_operands ),
217
+ .is_boxed_i,
218
+ .rnd_mode_i,
219
+ .op_i,
220
+ .op_mod_i,
221
+ .src_fmt_i,
222
+ .dst_fmt_i,
223
+ .tag_i,
224
+ .mask_i ( simd_mask_i[lane] ),
225
+ .aux_i ( aux_data ),
226
+ .in_valid_i ( in_valid ),
227
+ .in_ready_o ( lane_in_ready[lane] ),
228
+ .flush_i,
229
+ .result_o ( op_result ),
230
+ .status_o ( op_status ),
231
+ .extension_bit_o ( lane_ext_bit[lane] ),
232
+ .tag_o ( lane_tags[lane] ),
233
+ .mask_o ( lane_masks[lane] ),
234
+ .aux_o ( lane_aux[lane] ),
235
+ .out_valid_o ( out_valid ),
236
+ .out_ready_i ( out_ready ),
237
+ .busy_o ( lane_busy[lane] )
238
+ );
239
+
240
+ end else if (OpGroup == fpnew_pkg::DIVSQRT) begin : lane_instance
241
+ fpnew_divsqrt_multi #(
242
+ .FpFmtConfig ( LANE_FORMATS ),
243
+ .NumPipeRegs ( NumPipeRegs ),
244
+ .PipeConfig ( PipeConfig ),
245
+ .TagType ( TagType ),
246
+ .AuxType ( logic [AUX_BITS-1:0] )
247
+ ) i_fpnew_divsqrt_multi (
248
+ .clk_i,
249
+ .rst_ni,
250
+ .operands_i ( local_operands[1:0] ), // 2 operands
251
+ .is_boxed_i ( is_boxed_2op ), // 2 operands
252
+ .rnd_mode_i,
253
+ .op_i,
254
+ .dst_fmt_i,
255
+ .tag_i,
256
+ .mask_i ( simd_mask_i[lane] ),
257
+ .aux_i ( aux_data ),
258
+ .in_valid_i ( in_valid ),
259
+ .in_ready_o ( lane_in_ready[lane] ),
260
+ .divsqrt_done_o ( divsqrt_done[lane] ),
261
+ .simd_synch_done_i( simd_synch_done ),
262
+ .divsqrt_ready_o ( divsqrt_ready[lane]),
263
+ .simd_synch_rdy_i( simd_synch_rdy ),
264
+ .flush_i,
265
+ .result_o ( op_result ),
266
+ .status_o ( op_status ),
267
+ .extension_bit_o ( lane_ext_bit[lane] ),
268
+ .tag_o ( lane_tags[lane] ),
269
+ .mask_o ( lane_masks[lane] ),
270
+ .aux_o ( lane_aux[lane] ),
271
+ .out_valid_o ( out_valid ),
272
+ .out_ready_i ( out_ready ),
273
+ .busy_o ( lane_busy[lane] )
274
+ );
275
+ end else if (OpGroup == fpnew_pkg::NONCOMP) begin : lane_instance
276
+
277
+ end else if (OpGroup == fpnew_pkg::CONV) begin : lane_instance
278
+ fpnew_cast_multi #(
279
+ .FpFmtConfig ( LANE_FORMATS ),
280
+ .IntFmtConfig ( CONV_INT_FORMATS ),
281
+ .NumPipeRegs ( NumPipeRegs ),
282
+ .PipeConfig ( PipeConfig ),
283
+ .TagType ( TagType ),
284
+ .AuxType ( logic [AUX_BITS-1:0] )
285
+ ) i_fpnew_cast_multi (
286
+ .clk_i,
287
+ .rst_ni,
288
+ .operands_i ( local_operands[0] ),
289
+ .is_boxed_i ( is_boxed_1op ),
290
+ .rnd_mode_i,
291
+ .op_i,
292
+ .op_mod_i,
293
+ .src_fmt_i,
294
+ .dst_fmt_i,
295
+ .int_fmt_i,
296
+ .tag_i,
297
+ .mask_i ( simd_mask_i[lane] ),
298
+ .aux_i ( aux_data ),
299
+ .in_valid_i ( in_valid ),
300
+ .in_ready_o ( lane_in_ready[lane] ),
301
+ .flush_i,
302
+ .result_o ( op_result ),
303
+ .status_o ( op_status ),
304
+ .extension_bit_o ( lane_ext_bit[lane] ),
305
+ .tag_o ( lane_tags[lane] ),
306
+ .mask_o ( lane_masks[lane] ),
307
+ .aux_o ( lane_aux[lane] ),
308
+ .out_valid_o ( out_valid ),
309
+ .out_ready_i ( out_ready ),
310
+ .busy_o ( lane_busy[lane] )
311
+ );
312
+ end // ADD OTHER OPTIONS HERE
313
+
314
+ // Handshakes are only done if the lane is actually used
315
+ assign out_ready = out_ready_i & ((lane == 0) | result_is_vector);
316
+ assign lane_out_valid[lane] = out_valid & ((lane == 0) | result_is_vector);
317
+
318
+ // Properly NaN-box or sign-extend the slice result if not in use
319
+ assign local_result = lane_out_valid[lane] ? op_result : '{default: lane_ext_bit[0]};
320
+ assign lane_status[lane] = lane_out_valid[lane] ? op_status : '0;
321
+
322
+ // Otherwise generate constant sign-extension
323
+ end else begin : inactive_lane
324
+ assign lane_out_valid[lane] = 1'b0; // unused lane
325
+ assign lane_in_ready[lane] = 1'b0; // unused lane
326
+ assign local_result = '{default: lane_ext_bit[0]}; // sign-extend/nan box
327
+ assign lane_status[lane] = '0;
328
+ assign lane_busy[lane] = 1'b0;
329
+ end
330
+
331
+ // Generate result packing depending on float format
332
+ for (genvar fmt = 0; fmt < NUM_FORMATS; fmt++) begin : pack_fp_result
333
+ // Set up some constants
334
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
335
+ // only for active formats within the lane
336
+ if (ACTIVE_FORMATS[fmt]) begin
337
+ assign fmt_slice_result[fmt][(LANE+1)*FP_WIDTH-1:LANE*FP_WIDTH] =
338
+ local_result[FP_WIDTH-1:0];
339
+ end else if ((LANE+1)*FP_WIDTH <= Width) begin
340
+ assign fmt_slice_result[fmt][(LANE+1)*FP_WIDTH-1:LANE*FP_WIDTH] =
341
+ '{default: lane_ext_bit[LANE]};
342
+ end else if (LANE*FP_WIDTH < Width) begin
343
+ assign fmt_slice_result[fmt][Width-1:LANE*FP_WIDTH] =
344
+ '{default: lane_ext_bit[LANE]};
345
+ end
346
+ end
347
+
348
+ // Generate result packing depending on integer format
349
+ if (OpGroup == fpnew_pkg::CONV) begin : int_results_enabled
350
+ for (genvar ifmt = 0; ifmt < NUM_INT_FORMATS; ifmt++) begin : pack_int_result
351
+ // Set up some constants
352
+ localparam int unsigned INT_WIDTH = fpnew_pkg::int_width(fpnew_pkg::int_format_e'(ifmt));
353
+ if (ACTIVE_INT_FORMATS[ifmt]) begin
354
+ assign ifmt_slice_result[ifmt][(LANE+1)*INT_WIDTH-1:LANE*INT_WIDTH] =
355
+ local_result[INT_WIDTH-1:0];
356
+ end else if ((LANE+1)*INT_WIDTH <= Width) begin
357
+ assign ifmt_slice_result[ifmt][(LANE+1)*INT_WIDTH-1:LANE*INT_WIDTH] = '0;
358
+ end else if (LANE*INT_WIDTH < Width) begin
359
+ assign ifmt_slice_result[ifmt][Width-1:LANE*INT_WIDTH] = '0;
360
+ end
361
+ end
362
+ end
363
+ end
364
+
365
+ // Extend slice result if needed
366
+ for (genvar fmt = 0; fmt < NUM_FORMATS; fmt++) begin : extend_fp_result
367
+ // Set up some constants
368
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
369
+ if (NUM_LANES*FP_WIDTH < Width)
370
+ assign fmt_slice_result[fmt][Width-1:NUM_LANES*FP_WIDTH] = '{default: lane_ext_bit[0]};
371
+ end
372
+
373
+ // Mute int results if unused
374
+ for (genvar ifmt = 0; ifmt < NUM_INT_FORMATS; ifmt++) begin : int_results_disabled
375
+ if (OpGroup != fpnew_pkg::CONV) begin : mute_int_result
376
+ assign ifmt_slice_result[ifmt] = '0;
377
+ end
378
+ end
379
+
380
+ // Bypass lanes with target operand for vectorial casts
381
+ if (OpGroup == fpnew_pkg::CONV) begin : target_regs
382
+ // Bypass pipeline signals, index i holds signal after i register stages
383
+ logic [0:NumPipeRegs][Width-1:0] byp_pipe_target_q;
384
+ logic [0:NumPipeRegs][2:0] byp_pipe_aux_q;
385
+ logic [0:NumPipeRegs] byp_pipe_valid_q;
386
+ // Ready signal is combinatorial for all stages
387
+ logic [0:NumPipeRegs] byp_pipe_ready;
388
+
389
+ // Input stage: First element of pipeline is taken from inputs
390
+ assign byp_pipe_target_q[0] = conv_target_d;
391
+ assign byp_pipe_aux_q[0] = target_aux_d;
392
+ assign byp_pipe_valid_q[0] = in_valid_i & vectorial_op;
393
+ // Generate the register stages
394
+ for (genvar i = 0; i < NumPipeRegs; i++) begin : gen_bypass_pipeline
395
+ // Internal register enable for this stage
396
+ logic reg_ena;
397
+ // Determine the ready signal of the current stage - advance the pipeline:
398
+ // 1. if the next stage is ready for our data
399
+ // 2. if the next stage only holds a bubble (not valid) -> we can pop it
400
+ assign byp_pipe_ready[i] = byp_pipe_ready[i+1] | ~byp_pipe_valid_q[i+1];
401
+ // Valid: enabled by ready signal, synchronous clear with the flush signal
402
+ `FFLARNC(byp_pipe_valid_q[i+1], byp_pipe_valid_q[i], byp_pipe_ready[i], flush_i, 1'b0, clk_i, rst_ni)
403
+ // Enable register if pipleine ready and a valid data item is present
404
+ assign reg_ena = byp_pipe_ready[i] & byp_pipe_valid_q[i];
405
+ // Generate the pipeline registers within the stages, use enable-registers
406
+ `FFL(byp_pipe_target_q[i+1], byp_pipe_target_q[i], reg_ena, '0)
407
+ `FFL(byp_pipe_aux_q[i+1], byp_pipe_aux_q[i], reg_ena, '0)
408
+ end
409
+ // Output stage: Ready travels backwards from output side, driven by downstream circuitry
410
+ assign byp_pipe_ready[NumPipeRegs] = out_ready_i & result_is_vector;
411
+ // Output stage: assign module outputs
412
+ assign conv_target_q = byp_pipe_target_q[NumPipeRegs];
413
+
414
+ // decode the aux data
415
+ assign {result_vec_op, result_is_cpk} = byp_pipe_aux_q[NumPipeRegs];
416
+ end else begin : no_conv
417
+ assign {result_vec_op, result_is_cpk} = '0;
418
+ end
419
+
420
+ // Synch lanes if there is more than one
421
+ assign simd_synch_rdy = EnableVectors ? &divsqrt_ready : divsqrt_ready[0];
422
+ assign simd_synch_done = EnableVectors ? &divsqrt_done : divsqrt_done[0];
423
+
424
+ // ------------
425
+ // Output Side
426
+ // ------------
427
+ assign {result_fmt_is_int, result_is_vector, result_fmt} = lane_aux[0];
428
+
429
+ assign result_o = result_fmt_is_int
430
+ ? ifmt_slice_result[result_fmt]
431
+ : fmt_slice_result[result_fmt];
432
+
433
+ assign extension_bit_o = lane_ext_bit[0]; // don't care about upper ones
434
+ assign tag_o = lane_tags[0]; // don't care about upper ones
435
+ assign busy_o = (| lane_busy);
436
+
437
+ assign out_valid_o = lane_out_valid[0]; // don't care about upper ones
438
+
439
+ // Collapse the status
440
+ always_comb begin : output_processing
441
+ // Collapse the status
442
+ automatic fpnew_pkg::status_t temp_status;
443
+ temp_status = '0;
444
+ for (int i = 0; i < int'(NUM_LANES); i++)
445
+ temp_status |= lane_status[i] & {5{lane_masks[i]}};
446
+ status_o = temp_status;
447
+ end
448
+
449
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_pkg.sv ADDED
@@ -0,0 +1,495 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ package fpnew_pkg;
17
+
18
+ // ---------
19
+ // FP TYPES
20
+ // ---------
21
+ // | Enumerator | Format | Width | EXP_BITS | MAN_BITS
22
+ // |:----------:|------------------|-------:|:--------:|:--------:
23
+ // | FP32 | IEEE binary32 | 32 bit | 8 | 23
24
+ // | FP64 | IEEE binary64 | 64 bit | 11 | 52
25
+ // | FP16 | IEEE binary16 | 16 bit | 5 | 10
26
+ // | FP8 | binary8 | 8 bit | 5 | 2
27
+ // | FP16ALT | binary16alt | 16 bit | 8 | 7
28
+ // *NOTE:* Add new formats only at the end of the enumeration for backwards compatibilty!
29
+
30
+ // Encoding for a format
31
+ typedef struct packed {
32
+ int unsigned exp_bits;
33
+ int unsigned man_bits;
34
+ } fp_encoding_t;
35
+
36
+ localparam int unsigned NUM_FP_FORMATS = 5; // change me to add formats
37
+ localparam int unsigned FP_FORMAT_BITS = $clog2(NUM_FP_FORMATS);
38
+
39
+ // FP formats
40
+ typedef enum logic [FP_FORMAT_BITS-1:0] {
41
+ FP32 = 'd0,
42
+ FP64 = 'd1,
43
+ FP16 = 'd2,
44
+ FP8 = 'd3,
45
+ FP16ALT = 'd4
46
+ // add new formats here
47
+ } fp_format_e;
48
+
49
+ // Encodings for supported FP formats
50
+ localparam fp_encoding_t [0:NUM_FP_FORMATS-1] FP_ENCODINGS = '{
51
+ '{8, 23}, // IEEE binary32 (single)
52
+ '{11, 52}, // IEEE binary64 (double)
53
+ '{5, 10}, // IEEE binary16 (half)
54
+ '{5, 2}, // custom binary8
55
+ '{8, 7} // custom binary16alt
56
+ // add new formats here
57
+ };
58
+
59
+ typedef logic [0:NUM_FP_FORMATS-1] fmt_logic_t; // Logic indexed by FP format (for masks)
60
+ typedef logic [0:NUM_FP_FORMATS-1][31:0] fmt_unsigned_t; // Unsigned indexed by FP format
61
+
62
+ localparam fmt_logic_t CPK_FORMATS = 5'b11000; // FP32 and FP64 can provide CPK only
63
+
64
+ // ---------
65
+ // INT TYPES
66
+ // ---------
67
+ // | Enumerator | Width |
68
+ // |:----------:|-------:|
69
+ // | INT8 | 8 bit |
70
+ // | INT16 | 16 bit |
71
+ // | INT32 | 32 bit |
72
+ // | INT64 | 64 bit |
73
+ // *NOTE:* Add new formats only at the end of the enumeration for backwards compatibilty!
74
+
75
+ localparam int unsigned NUM_INT_FORMATS = 4; // change me to add formats
76
+ localparam int unsigned INT_FORMAT_BITS = $clog2(NUM_INT_FORMATS);
77
+
78
+ // Int formats
79
+ typedef enum logic [INT_FORMAT_BITS-1:0] {
80
+ INT8,
81
+ INT16,
82
+ INT32,
83
+ INT64
84
+ // add new formats here
85
+ } int_format_e;
86
+
87
+ // Returns the width of an INT format by index
88
+ function automatic int unsigned int_width(int_format_e ifmt);
89
+ unique case (ifmt)
90
+ INT8: return 8;
91
+ INT16: return 16;
92
+ INT32: return 32;
93
+ INT64: return 64;
94
+ default: begin
95
+ // pragma translate_off
96
+ $fatal(1, "Invalid INT format supplied");
97
+ // pragma translate_on
98
+ // just return any integer to avoid any latches
99
+ // hopefully this error is caught by simulation
100
+ return INT8;
101
+ end
102
+ endcase
103
+ endfunction
104
+
105
+ typedef logic [0:NUM_INT_FORMATS-1] ifmt_logic_t; // Logic indexed by INT format (for masks)
106
+
107
+ // --------------
108
+ // FP OPERATIONS
109
+ // --------------
110
+ localparam int unsigned NUM_OPGROUPS = 4;
111
+
112
+ // Each FP operation belongs to an operation group
113
+ typedef enum logic [1:0] {
114
+ ADDMUL, DIVSQRT, NONCOMP, CONV
115
+ } opgroup_e;
116
+
117
+ localparam int unsigned OP_BITS = 4;
118
+
119
+ typedef enum logic [OP_BITS-1:0] {
120
+ FMADD, FNMSUB, ADD, MUL, // ADDMUL operation group
121
+ DIV, SQRT, // DIVSQRT operation group
122
+ SGNJ, MINMAX, CMP, CLASSIFY, // NONCOMP operation group
123
+ F2F, F2I, I2F, CPKAB, CPKCD // CONV operation group
124
+ } operation_e;
125
+
126
+ // -------------------
127
+ // RISC-V FP-SPECIFIC
128
+ // -------------------
129
+ // Rounding modes
130
+ typedef enum logic [2:0] {
131
+ RNE = 3'b000,
132
+ RTZ = 3'b001,
133
+ RDN = 3'b010,
134
+ RUP = 3'b011,
135
+ RMM = 3'b100,
136
+ ROD = 3'b101, // This mode is not defined in RISC-V FP-SPEC
137
+ DYN = 3'b111
138
+ } roundmode_e;
139
+
140
+ // Status flags
141
+ typedef struct packed {
142
+ logic NV; // Invalid
143
+ logic DZ; // Divide by zero
144
+ logic OF; // Overflow
145
+ logic UF; // Underflow
146
+ logic NX; // Inexact
147
+ } status_t;
148
+
149
+ // Information about a floating point value
150
+ typedef struct packed {
151
+ logic is_normal; // is the value normal
152
+ logic is_subnormal; // is the value subnormal
153
+ logic is_zero; // is the value zero
154
+ logic is_inf; // is the value infinity
155
+ logic is_nan; // is the value NaN
156
+ logic is_signalling; // is the value a signalling NaN
157
+ logic is_quiet; // is the value a quiet NaN
158
+ logic is_boxed; // is the value properly NaN-boxed (RISC-V specific)
159
+ } fp_info_t;
160
+
161
+ // Classification mask
162
+ typedef enum logic [9:0] {
163
+ NEGINF = 10'b00_0000_0001,
164
+ NEGNORM = 10'b00_0000_0010,
165
+ NEGSUBNORM = 10'b00_0000_0100,
166
+ NEGZERO = 10'b00_0000_1000,
167
+ POSZERO = 10'b00_0001_0000,
168
+ POSSUBNORM = 10'b00_0010_0000,
169
+ POSNORM = 10'b00_0100_0000,
170
+ POSINF = 10'b00_1000_0000,
171
+ SNAN = 10'b01_0000_0000,
172
+ QNAN = 10'b10_0000_0000
173
+ } classmask_e;
174
+
175
+ // ------------------
176
+ // FPU configuration
177
+ // ------------------
178
+ // Pipelining registers can be inserted (at elaboration time) into operational units
179
+ typedef enum logic [1:0] {
180
+ BEFORE, // registers are inserted at the inputs of the unit
181
+ AFTER, // registers are inserted at the outputs of the unit
182
+ INSIDE, // registers are inserted at predetermined (suboptimal) locations in the unit
183
+ DISTRIBUTED // registers are evenly distributed, INSIDE >= AFTER >= BEFORE
184
+ } pipe_config_t;
185
+
186
+ // Arithmetic units can be arranged in parallel (per format), merged (multi-format) or not at all.
187
+ typedef enum logic [1:0] {
188
+ DISABLED, // arithmetic units are not generated
189
+ PARALLEL, // arithmetic units are generated in prallel slices, one for each format
190
+ MERGED // arithmetic units are contained within a merged unit holding multiple formats
191
+ } unit_type_t;
192
+
193
+ // Array of unit types indexed by format
194
+ typedef unit_type_t [0:NUM_FP_FORMATS-1] fmt_unit_types_t;
195
+
196
+ // Array of format-specific unit types by opgroup
197
+ typedef fmt_unit_types_t [0:NUM_OPGROUPS-1] opgrp_fmt_unit_types_t;
198
+ // same with unsigned
199
+ typedef fmt_unsigned_t [0:NUM_OPGROUPS-1] opgrp_fmt_unsigned_t;
200
+
201
+ // FPU configuration: features
202
+ typedef struct packed {
203
+ int unsigned Width;
204
+ logic EnableVectors;
205
+ logic EnableNanBox;
206
+ fmt_logic_t FpFmtMask;
207
+ ifmt_logic_t IntFmtMask;
208
+ } fpu_features_t;
209
+
210
+ localparam fpu_features_t RV64D = '{
211
+ Width: 64,
212
+ EnableVectors: 1'b0,
213
+ EnableNanBox: 1'b1,
214
+ FpFmtMask: 5'b11000,
215
+ IntFmtMask: 4'b0011
216
+ };
217
+
218
+ localparam fpu_features_t RV32D = '{
219
+ Width: 64,
220
+ EnableVectors: 1'b1,
221
+ EnableNanBox: 1'b1,
222
+ FpFmtMask: 5'b11000,
223
+ IntFmtMask: 4'b0010
224
+ };
225
+
226
+ localparam fpu_features_t RV32F = '{
227
+ Width: 32,
228
+ EnableVectors: 1'b0,
229
+ EnableNanBox: 1'b1,
230
+ FpFmtMask: 5'b10000,
231
+ IntFmtMask: 4'b0010
232
+ };
233
+
234
+ localparam fpu_features_t RV64D_Xsflt = '{
235
+ Width: 64,
236
+ EnableVectors: 1'b1,
237
+ EnableNanBox: 1'b1,
238
+ FpFmtMask: 5'b11111,
239
+ IntFmtMask: 4'b1111
240
+ };
241
+
242
+ localparam fpu_features_t RV32F_Xsflt = '{
243
+ Width: 32,
244
+ EnableVectors: 1'b1,
245
+ EnableNanBox: 1'b1,
246
+ FpFmtMask: 5'b10111,
247
+ IntFmtMask: 4'b1110
248
+ };
249
+
250
+ localparam fpu_features_t RV32F_Xf16alt_Xfvec = '{
251
+ Width: 32,
252
+ EnableVectors: 1'b1,
253
+ EnableNanBox: 1'b1,
254
+ FpFmtMask: 5'b10001,
255
+ IntFmtMask: 4'b0110
256
+ };
257
+
258
+
259
+ // FPU configuraion: implementation
260
+ typedef struct packed {
261
+ opgrp_fmt_unsigned_t PipeRegs;
262
+ opgrp_fmt_unit_types_t UnitTypes;
263
+ pipe_config_t PipeConfig;
264
+ } fpu_implementation_t;
265
+
266
+ localparam fpu_implementation_t DEFAULT_NOREGS = '{
267
+ PipeRegs: '{default: 0},
268
+ UnitTypes: '{'{default: PARALLEL}, // ADDMUL
269
+ '{default: MERGED}, // DIVSQRT
270
+ '{default: PARALLEL}, // NONCOMP
271
+ '{default: MERGED}}, // CONV
272
+ PipeConfig: BEFORE
273
+ };
274
+
275
+ localparam fpu_implementation_t DEFAULT_SNITCH = '{
276
+ PipeRegs: '{default: 1},
277
+ UnitTypes: '{'{default: PARALLEL}, // ADDMUL
278
+ '{default: DISABLED}, // DIVSQRT
279
+ '{default: PARALLEL}, // NONCOMP
280
+ '{default: MERGED}}, // CONV
281
+ PipeConfig: BEFORE
282
+ };
283
+
284
+ // -----------------------
285
+ // Synthesis optimization
286
+ // -----------------------
287
+ localparam logic DONT_CARE = 1'b1; // the value to assign as don't care
288
+
289
+ // -------------------------
290
+ // General helper functions
291
+ // -------------------------
292
+ function automatic int minimum(int a, int b);
293
+ return (a < b) ? a : b;
294
+ endfunction
295
+
296
+ function automatic int maximum(int a, int b);
297
+ return (a > b) ? a : b;
298
+ endfunction
299
+
300
+ // -------------------------------------------
301
+ // Helper functions for FP formats and values
302
+ // -------------------------------------------
303
+ // Returns the width of a FP format
304
+ function automatic int unsigned fp_width(fp_format_e fmt);
305
+ return FP_ENCODINGS[fmt].exp_bits + FP_ENCODINGS[fmt].man_bits + 1;
306
+ endfunction
307
+
308
+ // Returns the widest FP format present
309
+ function automatic int unsigned max_fp_width(fmt_logic_t cfg);
310
+ automatic int unsigned res = 0;
311
+ for (int unsigned i = 0; i < NUM_FP_FORMATS; i++)
312
+ if (cfg[i])
313
+ res = unsigned'(maximum(res, fp_width(fp_format_e'(i))));
314
+ return res;
315
+ endfunction
316
+
317
+ // Returns the narrowest FP format present
318
+ function automatic int unsigned min_fp_width(fmt_logic_t cfg);
319
+ automatic int unsigned res = max_fp_width(cfg);
320
+ for (int unsigned i = 0; i < NUM_FP_FORMATS; i++)
321
+ if (cfg[i])
322
+ res = unsigned'(minimum(res, fp_width(fp_format_e'(i))));
323
+ return res;
324
+ endfunction
325
+
326
+ // Returns the number of expoent bits for a format
327
+ function automatic int unsigned exp_bits(fp_format_e fmt);
328
+ return FP_ENCODINGS[fmt].exp_bits;
329
+ endfunction
330
+
331
+ // Returns the number of mantissa bits for a format
332
+ function automatic int unsigned man_bits(fp_format_e fmt);
333
+ return FP_ENCODINGS[fmt].man_bits;
334
+ endfunction
335
+
336
+ // Returns the bias value for a given format (as per IEEE 754-2008)
337
+ function automatic int unsigned bias(fp_format_e fmt);
338
+ return unsigned'(2**(FP_ENCODINGS[fmt].exp_bits-1)-1); // symmetrical bias
339
+ endfunction
340
+
341
+ function automatic fp_encoding_t super_format(fmt_logic_t cfg);
342
+ automatic fp_encoding_t res;
343
+ res = '0;
344
+ for (int unsigned fmt = 0; fmt < NUM_FP_FORMATS; fmt++)
345
+ if (cfg[fmt]) begin // only active format
346
+ res.exp_bits = unsigned'(maximum(res.exp_bits, exp_bits(fp_format_e'(fmt))));
347
+ res.man_bits = unsigned'(maximum(res.man_bits, man_bits(fp_format_e'(fmt))));
348
+ end
349
+ return res;
350
+ endfunction
351
+
352
+ // -------------------------------------------
353
+ // Helper functions for INT formats and values
354
+ // -------------------------------------------
355
+ // Returns the widest INT format present
356
+ function automatic int unsigned max_int_width(ifmt_logic_t cfg);
357
+ automatic int unsigned res = 0;
358
+ for (int ifmt = 0; ifmt < NUM_INT_FORMATS; ifmt++) begin
359
+ if (cfg[ifmt]) res = maximum(res, int_width(int_format_e'(ifmt)));
360
+ end
361
+ return res;
362
+ endfunction
363
+
364
+ // --------------------------------------------------
365
+ // Helper functions for operations and FPU structure
366
+ // --------------------------------------------------
367
+ // Returns the operation group of the given operation
368
+ function automatic opgroup_e get_opgroup(operation_e op);
369
+ unique case (op)
370
+ FMADD, FNMSUB, ADD, MUL: return ADDMUL;
371
+ DIV, SQRT: return DIVSQRT;
372
+ SGNJ, MINMAX, CMP, CLASSIFY: return NONCOMP;
373
+ F2F, F2I, I2F, CPKAB, CPKCD: return CONV;
374
+ default: return NONCOMP;
375
+ endcase
376
+ endfunction
377
+
378
+ // Returns the number of operands by operation group
379
+ function automatic int unsigned num_operands(opgroup_e grp);
380
+ unique case (grp)
381
+ ADDMUL: return 3;
382
+ DIVSQRT: return 2;
383
+ NONCOMP: return 2;
384
+ CONV: return 3; // vectorial casts use 3 operands
385
+ default: return 0;
386
+ endcase
387
+ endfunction
388
+
389
+ // Returns the number of lanes according to width, format and vectors
390
+ function automatic int unsigned num_lanes(int unsigned width, fp_format_e fmt, logic vec);
391
+ return vec ? width / fp_width(fmt) : 1; // if no vectors, only one lane
392
+ endfunction
393
+
394
+ // Returns the maximum number of lanes in the FPU according to width, format config and vectors
395
+ function automatic int unsigned max_num_lanes(int unsigned width, fmt_logic_t cfg, logic vec);
396
+ return vec ? width / min_fp_width(cfg) : 1; // if no vectors, only one lane
397
+ endfunction
398
+
399
+ // Returns a mask of active FP formats that are present in lane lane_no of a multiformat slice
400
+ function automatic fmt_logic_t get_lane_formats(int unsigned width,
401
+ fmt_logic_t cfg,
402
+ int unsigned lane_no);
403
+ automatic fmt_logic_t res;
404
+ for (int unsigned fmt = 0; fmt < NUM_FP_FORMATS; fmt++)
405
+ // Mask active formats with the number of lanes for that format
406
+ res[fmt] = cfg[fmt] & (width / fp_width(fp_format_e'(fmt)) > lane_no);
407
+ return res;
408
+ endfunction
409
+
410
+ // Returns a mask of active INT formats that are present in lane lane_no of a multiformat slice
411
+ function automatic ifmt_logic_t get_lane_int_formats(int unsigned width,
412
+ fmt_logic_t cfg,
413
+ ifmt_logic_t icfg,
414
+ int unsigned lane_no);
415
+ automatic ifmt_logic_t res;
416
+ automatic fmt_logic_t lanefmts;
417
+ res = '0;
418
+ lanefmts = get_lane_formats(width, cfg, lane_no);
419
+
420
+ for (int unsigned ifmt = 0; ifmt < NUM_INT_FORMATS; ifmt++)
421
+ for (int unsigned fmt = 0; fmt < NUM_FP_FORMATS; fmt++)
422
+ // Mask active int formats with the width of the float formats
423
+ if ((fp_width(fp_format_e'(fmt)) == int_width(int_format_e'(ifmt))))
424
+ res[ifmt] |= icfg[ifmt] && lanefmts[fmt];
425
+ return res;
426
+ endfunction
427
+
428
+ // Returns a mask of active FP formats that are present in lane lane_no of a CONV slice
429
+ function automatic fmt_logic_t get_conv_lane_formats(int unsigned width,
430
+ fmt_logic_t cfg,
431
+ int unsigned lane_no);
432
+ automatic fmt_logic_t res;
433
+ for (int unsigned fmt = 0; fmt < NUM_FP_FORMATS; fmt++)
434
+ // Mask active formats with the number of lanes for that format, CPK at least twice
435
+ res[fmt] = cfg[fmt] && ((width / fp_width(fp_format_e'(fmt)) > lane_no) ||
436
+ (CPK_FORMATS[fmt] && (lane_no < 2)));
437
+ return res;
438
+ endfunction
439
+
440
+ // Returns a mask of active INT formats that are present in lane lane_no of a CONV slice
441
+ function automatic ifmt_logic_t get_conv_lane_int_formats(int unsigned width,
442
+ fmt_logic_t cfg,
443
+ ifmt_logic_t icfg,
444
+ int unsigned lane_no);
445
+ automatic ifmt_logic_t res;
446
+ automatic fmt_logic_t lanefmts;
447
+ res = '0;
448
+ lanefmts = get_conv_lane_formats(width, cfg, lane_no);
449
+
450
+ for (int unsigned ifmt = 0; ifmt < NUM_INT_FORMATS; ifmt++)
451
+ for (int unsigned fmt = 0; fmt < NUM_FP_FORMATS; fmt++)
452
+ // Mask active int formats with the width of the float formats
453
+ res[ifmt] |= icfg[ifmt] && lanefmts[fmt] &&
454
+ (fp_width(fp_format_e'(fmt)) == int_width(int_format_e'(ifmt)));
455
+ return res;
456
+ endfunction
457
+
458
+ // Return whether any active format is set as MERGED
459
+ function automatic logic any_enabled_multi(fmt_unit_types_t types, fmt_logic_t cfg);
460
+ for (int unsigned i = 0; i < NUM_FP_FORMATS; i++)
461
+ if (cfg[i] && types[i] == MERGED)
462
+ return 1'b1;
463
+ return 1'b0;
464
+ endfunction
465
+
466
+ // Return whether the given format is the first active one set as MERGED
467
+ function automatic logic is_first_enabled_multi(fp_format_e fmt,
468
+ fmt_unit_types_t types,
469
+ fmt_logic_t cfg);
470
+ for (int unsigned i = 0; i < NUM_FP_FORMATS; i++) begin
471
+ if (cfg[i] && types[i] == MERGED) return (fp_format_e'(i) == fmt);
472
+ end
473
+ return 1'b0;
474
+ endfunction
475
+
476
+ // Returns the first format that is active and is set as MERGED
477
+ function automatic fp_format_e get_first_enabled_multi(fmt_unit_types_t types, fmt_logic_t cfg);
478
+ for (int unsigned i = 0; i < NUM_FP_FORMATS; i++)
479
+ if (cfg[i] && types[i] == MERGED)
480
+ return fp_format_e'(i);
481
+ return fp_format_e'(0);
482
+ endfunction
483
+
484
+ // Returns the largest number of regs that is active and is set as MERGED
485
+ function automatic int unsigned get_num_regs_multi(fmt_unsigned_t regs,
486
+ fmt_unit_types_t types,
487
+ fmt_logic_t cfg);
488
+ automatic int unsigned res = 0;
489
+ for (int unsigned i = 0; i < NUM_FP_FORMATS; i++) begin
490
+ if (cfg[i] && types[i] == MERGED) res = maximum(res, regs[i]);
491
+ end
492
+ return res;
493
+ endfunction
494
+
495
+ endpackage
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_rounding.sv ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ module fpnew_rounding #(
17
+ parameter int unsigned AbsWidth=2 // Width of the abolute value, without sign bit
18
+ ) (
19
+ // Input value
20
+ input logic [AbsWidth-1:0] abs_value_i, // absolute value without sign
21
+ input logic sign_i,
22
+ // Rounding information
23
+ input logic [1:0] round_sticky_bits_i, // round and sticky bits {RS}
24
+ input fpnew_pkg::roundmode_e rnd_mode_i,
25
+ input logic effective_subtraction_i, // sign of inputs affects rounding of zeroes
26
+ // Output value
27
+ output logic [AbsWidth-1:0] abs_rounded_o, // absolute value without sign
28
+ output logic sign_o,
29
+ // Output classification
30
+ output logic exact_zero_o // output is an exact zero
31
+ );
32
+
33
+ logic round_up; // Rounding decision
34
+
35
+ // Take the rounding decision according to RISC-V spec
36
+ // RoundMode | Mnemonic | Meaning
37
+ // :--------:|:--------:|:-------
38
+ // 000 | RNE | Round to Nearest, ties to Even
39
+ // 001 | RTZ | Round towards Zero
40
+ // 010 | RDN | Round Down (towards -\infty)
41
+ // 011 | RUP | Round Up (towards \infty)
42
+ // 100 | RMM | Round to Nearest, ties to Max Magnitude
43
+ // 101 | ROD | Round towards odd (this mode is not define in RISC-V FP-SPEC)
44
+ // others | | *invalid*
45
+ always_comb begin : rounding_decision
46
+ unique case (rnd_mode_i)
47
+ fpnew_pkg::RNE: // Decide accoring to round/sticky bits
48
+ unique case (round_sticky_bits_i)
49
+ 2'b00,
50
+ 2'b01: round_up = 1'b0; // < ulp/2 away, round down
51
+ 2'b10: round_up = abs_value_i[0]; // = ulp/2 away, round towards even result
52
+ 2'b11: round_up = 1'b1; // > ulp/2 away, round up
53
+ default: round_up = fpnew_pkg::DONT_CARE;
54
+ endcase
55
+ fpnew_pkg::RTZ: round_up = 1'b0; // always round down
56
+ fpnew_pkg::RDN: round_up = (| round_sticky_bits_i) ? sign_i : 1'b0; // to 0 if +, away if -
57
+ fpnew_pkg::RUP: round_up = (| round_sticky_bits_i) ? ~sign_i : 1'b0; // to 0 if -, away if +
58
+ fpnew_pkg::RMM: round_up = round_sticky_bits_i[1]; // round down if < ulp/2 away, else up
59
+ fpnew_pkg::ROD: round_up = ~abs_value_i[0] & (| round_sticky_bits_i);
60
+ default: round_up = fpnew_pkg::DONT_CARE; // propagate x
61
+ endcase
62
+ end
63
+
64
+ // Perform the rounding, exponent change and overflow to inf happens automagically
65
+ assign abs_rounded_o = abs_value_i + round_up;
66
+
67
+ // True zero result is a zero result without dirty round/sticky bits
68
+ assign exact_zero_o = (abs_value_i == '0) && (round_sticky_bits_i == '0);
69
+
70
+ // In case of effective subtraction (thus signs of addition operands must have differed) and a
71
+ // true zero result, the result sign is '-' in case of RDN and '+' for other modes.
72
+ assign sign_o = (exact_zero_o && effective_subtraction_i)
73
+ ? (rnd_mode_i == fpnew_pkg::RDN)
74
+ : sign_i;
75
+
76
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpnew_top.sv ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 ETH Zurich and University of Bologna.
2
+ //
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // SPDX-License-Identifier: SHL-0.51
13
+
14
+ // Author: Stefan Mach <smach@iis.ee.ethz.ch>
15
+
16
+ module fpnew_top #(
17
+ // FPU configuration
18
+ parameter fpnew_pkg::fpu_features_t Features = fpnew_pkg::RV64D_Xsflt,
19
+ parameter fpnew_pkg::fpu_implementation_t Implementation = fpnew_pkg::DEFAULT_NOREGS,
20
+ parameter type TagType = logic,
21
+ parameter int unsigned TrueSIMDClass = 0,
22
+ parameter int unsigned EnableSIMDMask = 0,
23
+ // Do not change
24
+ localparam int unsigned NumLanes = fpnew_pkg::max_num_lanes(Features.Width, Features.FpFmtMask, Features.EnableVectors),
25
+ localparam type MaskType = logic [NumLanes-1:0],
26
+ localparam int unsigned WIDTH = Features.Width,
27
+ localparam int unsigned NUM_OPERANDS = 3
28
+ ) (
29
+ input logic clk_i,
30
+ input logic rst_ni,
31
+ // Input signals
32
+ input logic [NUM_OPERANDS-1:0][WIDTH-1:0] operands_i,
33
+ input fpnew_pkg::roundmode_e rnd_mode_i,
34
+ input fpnew_pkg::operation_e op_i,
35
+ input logic op_mod_i,
36
+ input fpnew_pkg::fp_format_e src_fmt_i,
37
+ input fpnew_pkg::fp_format_e dst_fmt_i,
38
+ input fpnew_pkg::int_format_e int_fmt_i,
39
+ input logic vectorial_op_i,
40
+ input TagType tag_i,
41
+ input MaskType simd_mask_i,
42
+ // Input Handshake
43
+ input logic in_valid_i,
44
+ output logic in_ready_o,
45
+ input logic flush_i,
46
+ // Output signals
47
+ output logic [WIDTH-1:0] result_o,
48
+ output fpnew_pkg::status_t status_o,
49
+ output TagType tag_o,
50
+ // Output handshake
51
+ output logic out_valid_o,
52
+ input logic out_ready_i,
53
+ // Indication of valid data in flight
54
+ output logic busy_o
55
+ );
56
+
57
+ localparam int unsigned NUM_OPGROUPS = fpnew_pkg::NUM_OPGROUPS;
58
+ localparam int unsigned NUM_FORMATS = fpnew_pkg::NUM_FP_FORMATS;
59
+
60
+ // ----------------
61
+ // Type Definition
62
+ // ----------------
63
+ typedef struct packed {
64
+ logic [WIDTH-1:0] result;
65
+ fpnew_pkg::status_t status;
66
+ TagType tag;
67
+ } output_t;
68
+
69
+ // Handshake signals for the blocks
70
+ logic [NUM_OPGROUPS-1:0] opgrp_in_ready, opgrp_out_valid, opgrp_out_ready, opgrp_ext, opgrp_busy;
71
+ output_t [NUM_OPGROUPS-1:0] opgrp_outputs;
72
+
73
+ logic [NUM_FORMATS-1:0][NUM_OPERANDS-1:0] is_boxed;
74
+
75
+ // -----------
76
+ // Input Side
77
+ // -----------
78
+ assign in_ready_o = in_valid_i & opgrp_in_ready[fpnew_pkg::get_opgroup(op_i)];
79
+
80
+ // NaN-boxing check
81
+ for (genvar fmt = 0; fmt < int'(NUM_FORMATS); fmt++) begin : gen_nanbox_check
82
+ localparam int unsigned FP_WIDTH = fpnew_pkg::fp_width(fpnew_pkg::fp_format_e'(fmt));
83
+ // NaN boxing is only generated if it's enabled and needed
84
+ if (Features.EnableNanBox && (FP_WIDTH < WIDTH)) begin : check
85
+ for (genvar op = 0; op < int'(NUM_OPERANDS); op++) begin : operands
86
+ assign is_boxed[fmt][op] = (!vectorial_op_i)
87
+ ? operands_i[op][WIDTH-1:FP_WIDTH] == '1
88
+ : 1'b1;
89
+ end
90
+ end else begin : no_check
91
+ assign is_boxed[fmt] = '1;
92
+ end
93
+ end
94
+
95
+ // Filter out the mask if not used
96
+ MaskType simd_mask;
97
+ assign simd_mask = simd_mask_i | ~{NumLanes{logic'(EnableSIMDMask)}};
98
+
99
+ // -------------------------
100
+ // Generate Operation Blocks
101
+ // -------------------------
102
+ for (genvar opgrp = 0; opgrp < int'(NUM_OPGROUPS); opgrp++) begin : gen_operation_groups
103
+ localparam int unsigned NUM_OPS = fpnew_pkg::num_operands(fpnew_pkg::opgroup_e'(opgrp));
104
+
105
+ logic in_valid;
106
+ logic [NUM_FORMATS-1:0][NUM_OPS-1:0] input_boxed;
107
+
108
+ assign in_valid = in_valid_i & (fpnew_pkg::get_opgroup(op_i) == fpnew_pkg::opgroup_e'(opgrp));
109
+
110
+ // slice out input boxing
111
+ always_comb begin : slice_inputs
112
+ for (int unsigned fmt = 0; fmt < NUM_FORMATS; fmt++)
113
+ input_boxed[fmt] = is_boxed[fmt][NUM_OPS-1:0];
114
+ end
115
+
116
+ fpnew_opgroup_block #(
117
+ .OpGroup ( fpnew_pkg::opgroup_e'(opgrp) ),
118
+ .Width ( WIDTH ),
119
+ .EnableVectors ( Features.EnableVectors ),
120
+ .FpFmtMask ( Features.FpFmtMask ),
121
+ .IntFmtMask ( Features.IntFmtMask ),
122
+ .FmtPipeRegs ( Implementation.PipeRegs[opgrp] ),
123
+ .FmtUnitTypes ( Implementation.UnitTypes[opgrp] ),
124
+ .PipeConfig ( Implementation.PipeConfig ),
125
+ .TagType ( TagType ),
126
+ .TrueSIMDClass ( TrueSIMDClass )
127
+ ) i_opgroup_block (
128
+ .clk_i,
129
+ .rst_ni,
130
+ .operands_i ( operands_i[NUM_OPS-1:0] ),
131
+ .is_boxed_i ( input_boxed ),
132
+ .rnd_mode_i,
133
+ .op_i,
134
+ .op_mod_i,
135
+ .src_fmt_i,
136
+ .dst_fmt_i,
137
+ .int_fmt_i,
138
+ .vectorial_op_i,
139
+ .tag_i,
140
+ .simd_mask_i ( simd_mask ),
141
+ .in_valid_i ( in_valid ),
142
+ .in_ready_o ( opgrp_in_ready[opgrp] ),
143
+ .flush_i,
144
+ .result_o ( opgrp_outputs[opgrp].result ),
145
+ .status_o ( opgrp_outputs[opgrp].status ),
146
+ .extension_bit_o ( opgrp_ext[opgrp] ),
147
+ .tag_o ( opgrp_outputs[opgrp].tag ),
148
+ .out_valid_o ( opgrp_out_valid[opgrp] ),
149
+ .out_ready_i ( opgrp_out_ready[opgrp] ),
150
+ .busy_o ( opgrp_busy[opgrp] )
151
+ );
152
+ end
153
+
154
+ // ------------------
155
+ // Arbitrate Outputs
156
+ // ------------------
157
+ output_t arbiter_output;
158
+
159
+ // Round-Robin arbiter to decide which result to use
160
+ rr_arb_tree #(
161
+ .NumIn ( NUM_OPGROUPS ),
162
+ .DataType ( output_t ),
163
+ .AxiVldRdy ( 1'b1 )
164
+ ) i_arbiter (
165
+ .clk_i,
166
+ .rst_ni,
167
+ .flush_i,
168
+ .rr_i ( '0 ),
169
+ .req_i ( opgrp_out_valid ),
170
+ .gnt_o ( opgrp_out_ready ),
171
+ .data_i ( opgrp_outputs ),
172
+ .gnt_i ( out_ready_i ),
173
+ .req_o ( out_valid_o ),
174
+ .data_o ( arbiter_output ),
175
+ .idx_o ( /* unused */ )
176
+ );
177
+
178
+ // Unpack output
179
+ assign result_o = arbiter_output.result;
180
+ assign status_o = arbiter_output.status;
181
+ assign tag_o = arbiter_output.tag;
182
+
183
+ assign busy_o = (| opgrp_busy);
184
+
185
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/control_mvp.sv ADDED
The diff for this file is too large to render. See raw diff
 
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/defs_div_sqrt_mvp.sv ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the “License”); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // This file contains all div_sqrt_top_mvp parameters
12
+ // Authors : Lei Li (lile@iis.ee.ethz.ch)
13
+
14
+ package defs_div_sqrt_mvp;
15
+
16
+ // op command
17
+ localparam C_RM = 3;
18
+ localparam C_RM_NEAREST = 3'h0;
19
+ localparam C_RM_TRUNC = 3'h1;
20
+ localparam C_RM_PLUSINF = 3'h2;
21
+ localparam C_RM_MINUSINF = 3'h3;
22
+ localparam C_PC = 6; // Precision Control
23
+ localparam C_FS = 2; // Format Selection
24
+ localparam C_IUNC = 2; // Iteration Unit Number Control
25
+ localparam Iteration_unit_num_S = 2'b10;
26
+
27
+ // FP64
28
+ localparam C_OP_FP64 = 64;
29
+ localparam C_MANT_FP64 = 52;
30
+ localparam C_EXP_FP64 = 11;
31
+ localparam C_BIAS_FP64 = 1023;
32
+ localparam C_BIAS_AONE_FP64 = 11'h400;
33
+ localparam C_HALF_BIAS_FP64 = 511;
34
+ localparam C_EXP_ZERO_FP64 = 11'h000;
35
+ localparam C_EXP_ONE_FP64 = 13'h001; // Bit width is in agreement with in norm
36
+ localparam C_EXP_INF_FP64 = 11'h7FF;
37
+ localparam C_MANT_ZERO_FP64 = 52'h0;
38
+ localparam C_MANT_NAN_FP64 = 52'h8_0000_0000_0000;
39
+ localparam C_PZERO_FP64 = 64'h0000_0000_0000_0000;
40
+ localparam C_MZERO_FP64 = 64'h8000_0000_0000_0000;
41
+ localparam C_QNAN_FP64 = 64'h7FF8_0000_0000_0000;
42
+
43
+ // FP32
44
+ localparam C_OP_FP32 = 32;
45
+ localparam C_MANT_FP32 = 23;
46
+ localparam C_EXP_FP32 = 8;
47
+ localparam C_BIAS_FP32 = 127;
48
+ localparam C_BIAS_AONE_FP32 = 8'h80;
49
+ localparam C_HALF_BIAS_FP32 = 63;
50
+ localparam C_EXP_ZERO_FP32 = 8'h00;
51
+ localparam C_EXP_INF_FP32 = 8'hFF;
52
+ localparam C_MANT_ZERO_FP32 = 23'h0;
53
+ localparam C_PZERO_FP32 = 32'h0000_0000;
54
+ localparam C_MZERO_FP32 = 32'h8000_0000;
55
+ localparam C_QNAN_FP32 = 32'h7FC0_0000;
56
+
57
+ // FP16
58
+ localparam C_OP_FP16 = 16;
59
+ localparam C_MANT_FP16 = 10;
60
+ localparam C_EXP_FP16 = 5;
61
+ localparam C_BIAS_FP16 = 15;
62
+ localparam C_BIAS_AONE_FP16 = 5'h10;
63
+ localparam C_HALF_BIAS_FP16 = 7;
64
+ localparam C_EXP_ZERO_FP16 = 5'h00;
65
+ localparam C_EXP_INF_FP16 = 5'h1F;
66
+ localparam C_MANT_ZERO_FP16 = 10'h0;
67
+ localparam C_PZERO_FP16 = 16'h0000;
68
+ localparam C_MZERO_FP16 = 16'h8000;
69
+ localparam C_QNAN_FP16 = 16'h7E00;
70
+
71
+ // FP16alt
72
+ localparam C_OP_FP16ALT = 16;
73
+ localparam C_MANT_FP16ALT = 7;
74
+ localparam C_EXP_FP16ALT = 8;
75
+ localparam C_BIAS_FP16ALT = 127;
76
+ localparam C_BIAS_AONE_FP16ALT = 8'h80;
77
+ localparam C_HALF_BIAS_FP16ALT = 63;
78
+ localparam C_EXP_ZERO_FP16ALT = 8'h00;
79
+ localparam C_EXP_INF_FP16ALT = 8'hFF;
80
+ localparam C_MANT_ZERO_FP16ALT = 7'h0;
81
+ localparam C_QNAN_FP16ALT = 16'h7FC0;
82
+
83
+ endpackage : defs_div_sqrt_mvp
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/div_sqrt_top_mvp.sv ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the “License”); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////
12
+ // Company: IIS @ ETHZ - Federal Institute of Technology //
13
+ // //
14
+ // Engineers: Lei Li -- lile@iis.ee.ethz.ch //
15
+ // //
16
+ // Additional contributions by: //
17
+ // //
18
+ // //
19
+ // //
20
+ // Create Date: 03/03/2018 //
21
+ // Design Name: div_sqrt_top_mvp //
22
+ // Module Name: div_sqrt_top_mvp.sv //
23
+ // Project Name: The shared divisor and square root //
24
+ // Language: SystemVerilog //
25
+ // //
26
+ // Description: The top of div and sqrt //
27
+ // //
28
+ // //
29
+ // Revision Date: 12/04/2018 //
30
+ // Lei Li //
31
+ // To address some requirements by Stefan and add low power //
32
+ // control for special cases //
33
+ ////////////////////////////////////////////////////////////////////////////////
34
+
35
+ import defs_div_sqrt_mvp::*;
36
+
37
+ module div_sqrt_top_mvp
38
+
39
+ (//Input
40
+ input logic Clk_CI,
41
+ input logic Rst_RBI,
42
+ input logic Div_start_SI,
43
+ input logic Sqrt_start_SI,
44
+
45
+ //Input Operands
46
+ input logic [C_OP_FP64-1:0] Operand_a_DI,
47
+ input logic [C_OP_FP64-1:0] Operand_b_DI,
48
+
49
+ // Input Control
50
+ input logic [C_RM-1:0] RM_SI, //Rounding Mode
51
+ input logic [C_PC-1:0] Precision_ctl_SI, // Precision Control
52
+ input logic [C_FS-1:0] Format_sel_SI, // Format Selection,
53
+ input logic Kill_SI,
54
+
55
+ //Output Result
56
+ output logic [C_OP_FP64-1:0] Result_DO,
57
+
58
+ //Output-Flags
59
+ output logic [4:0] Fflags_SO,
60
+ output logic Ready_SO,
61
+ output logic Done_SO
62
+ );
63
+
64
+
65
+
66
+
67
+
68
+ //Operand components
69
+ logic [C_EXP_FP64:0] Exp_a_D;
70
+ logic [C_EXP_FP64:0] Exp_b_D;
71
+ logic [C_MANT_FP64:0] Mant_a_D;
72
+ logic [C_MANT_FP64:0] Mant_b_D;
73
+
74
+ logic [C_EXP_FP64+1:0] Exp_z_D;
75
+ logic [C_MANT_FP64+4:0] Mant_z_D;
76
+ logic Sign_z_D;
77
+ logic Start_S;
78
+ logic [C_RM-1:0] RM_dly_S;
79
+ logic Div_enable_S;
80
+ logic Sqrt_enable_S;
81
+ logic Inf_a_S;
82
+ logic Inf_b_S;
83
+ logic Zero_a_S;
84
+ logic Zero_b_S;
85
+ logic NaN_a_S;
86
+ logic NaN_b_S;
87
+ logic SNaN_S;
88
+ logic Special_case_SB,Special_case_dly_SB;
89
+
90
+ logic Full_precision_S;
91
+ logic FP32_S;
92
+ logic FP64_S;
93
+ logic FP16_S;
94
+ logic FP16ALT_S;
95
+
96
+
97
+ preprocess_mvp preprocess_U0
98
+ (
99
+ .Clk_CI (Clk_CI ),
100
+ .Rst_RBI (Rst_RBI ),
101
+ .Div_start_SI (Div_start_SI ),
102
+ .Sqrt_start_SI (Sqrt_start_SI ),
103
+ .Ready_SI (Ready_SO ),
104
+ .Operand_a_DI (Operand_a_DI ),
105
+ .Operand_b_DI (Operand_b_DI ),
106
+ .RM_SI (RM_SI ),
107
+ .Format_sel_SI (Format_sel_SI ),
108
+ .Start_SO (Start_S ),
109
+ .Exp_a_DO_norm (Exp_a_D ),
110
+ .Exp_b_DO_norm (Exp_b_D ),
111
+ .Mant_a_DO_norm (Mant_a_D ),
112
+ .Mant_b_DO_norm (Mant_b_D ),
113
+ .RM_dly_SO (RM_dly_S ),
114
+ .Sign_z_DO (Sign_z_D ),
115
+ .Inf_a_SO (Inf_a_S ),
116
+ .Inf_b_SO (Inf_b_S ),
117
+ .Zero_a_SO (Zero_a_S ),
118
+ .Zero_b_SO (Zero_b_S ),
119
+ .NaN_a_SO (NaN_a_S ),
120
+ .NaN_b_SO (NaN_b_S ),
121
+ .SNaN_SO (SNaN_S ),
122
+ .Special_case_SBO (Special_case_SB ),
123
+ .Special_case_dly_SBO (Special_case_dly_SB)
124
+ );
125
+
126
+ nrbd_nrsc_mvp nrbd_nrsc_U0
127
+ (
128
+ .Clk_CI (Clk_CI ),
129
+ .Rst_RBI (Rst_RBI ),
130
+ .Div_start_SI (Div_start_SI ) ,
131
+ .Sqrt_start_SI (Sqrt_start_SI ),
132
+ .Start_SI (Start_S ),
133
+ .Kill_SI (Kill_SI ),
134
+ .Special_case_SBI (Special_case_SB ),
135
+ .Special_case_dly_SBI (Special_case_dly_SB),
136
+ .Div_enable_SO (Div_enable_S ),
137
+ .Sqrt_enable_SO (Sqrt_enable_S ),
138
+ .Precision_ctl_SI (Precision_ctl_SI ),
139
+ .Format_sel_SI (Format_sel_SI ),
140
+ .Exp_a_DI (Exp_a_D ),
141
+ .Exp_b_DI (Exp_b_D ),
142
+ .Mant_a_DI (Mant_a_D ),
143
+ .Mant_b_DI (Mant_b_D ),
144
+ .Full_precision_SO (Full_precision_S ),
145
+ .FP32_SO (FP32_S ),
146
+ .FP64_SO (FP64_S ),
147
+ .FP16_SO (FP16_S ),
148
+ .FP16ALT_SO (FP16ALT_S ),
149
+ .Ready_SO (Ready_SO ),
150
+ .Done_SO (Done_SO ),
151
+ .Exp_z_DO (Exp_z_D ),
152
+ .Mant_z_DO (Mant_z_D )
153
+ );
154
+
155
+
156
+ norm_div_sqrt_mvp fpu_norm_U0
157
+ (
158
+ .Mant_in_DI (Mant_z_D ),
159
+ .Exp_in_DI (Exp_z_D ),
160
+ .Sign_in_DI (Sign_z_D ),
161
+ .Div_enable_SI (Div_enable_S ),
162
+ .Sqrt_enable_SI (Sqrt_enable_S ),
163
+ .Inf_a_SI (Inf_a_S ),
164
+ .Inf_b_SI (Inf_b_S ),
165
+ .Zero_a_SI (Zero_a_S ),
166
+ .Zero_b_SI (Zero_b_S ),
167
+ .NaN_a_SI (NaN_a_S ),
168
+ .NaN_b_SI (NaN_b_S ),
169
+ .SNaN_SI (SNaN_S ),
170
+ .RM_SI (RM_dly_S ),
171
+ .Full_precision_SI (Full_precision_S ),
172
+ .FP32_SI (FP32_S ),
173
+ .FP64_SI (FP64_S ),
174
+ .FP16_SI (FP16_S ),
175
+ .FP16ALT_SI (FP16ALT_S ),
176
+ .Result_DO (Result_DO ),
177
+ .Fflags_SO (Fflags_SO ) //{NV,DZ,OF,UF,NX}
178
+ );
179
+
180
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/iteration_div_sqrt_mvp.sv ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the “License”); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ ////////////////////////////////////////////////////////////////////////////////
11
+ // Company: IIS @ ETHZ - Federal Institute of Technology //
12
+ // //
13
+ // Engineers: Lei Li lile@iis.ee.ethz.ch //
14
+ // //
15
+ // Additional contributions by: //
16
+ // //
17
+ // //
18
+ // //
19
+ // Create Date: 12/01/2017 //
20
+ // Design Name: FPU //
21
+ // Module Name: iteration_div_sqrt_mvp //
22
+ // Project Name: Private FPU //
23
+ // Language: SystemVerilog //
24
+ // //
25
+ // Description: iteration unit for div and sqrt //
26
+ // //
27
+ // //
28
+ // Revision: 03/14/2018 //
29
+ // For div_sqrt_mvp //
30
+ ////////////////////////////////////////////////////////////////////////////////
31
+
32
+ module iteration_div_sqrt_mvp
33
+ #(
34
+ parameter WIDTH=25
35
+ )
36
+ (//Input
37
+
38
+ input logic [WIDTH-1:0] A_DI,
39
+ input logic [WIDTH-1:0] B_DI,
40
+ input logic Div_enable_SI,
41
+ input logic Div_start_dly_SI,
42
+ input logic Sqrt_enable_SI,
43
+ input logic [1:0] D_DI,
44
+
45
+ output logic [1:0] D_DO,
46
+ output logic [WIDTH-1:0] Sum_DO,
47
+ output logic Carry_out_DO
48
+ );
49
+
50
+ logic D_carry_D;
51
+ logic Sqrt_cin_D;
52
+ logic Cin_D;
53
+
54
+ assign D_DO[0]=~D_DI[0];
55
+ assign D_DO[1]=~(D_DI[1] ^ D_DI[0]);
56
+ assign D_carry_D=D_DI[1] | D_DI[0];
57
+ assign Sqrt_cin_D=Sqrt_enable_SI&&D_carry_D;
58
+ assign Cin_D=Div_enable_SI?1'b0:Sqrt_cin_D;
59
+ assign {Carry_out_DO,Sum_DO}=A_DI+B_DI+Cin_D;
60
+
61
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/norm_div_sqrt_mvp.sv ADDED
@@ -0,0 +1,470 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the “License”); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+
11
+ ////////////////////////////////////////////////////////////////////////////////
12
+ // Company: IIS @ ETHZ - Federal Institute of Technology //
13
+ // //
14
+ // Engineers: Lei Li lile@iis.ee.ethz.ch //
15
+ // //
16
+ // Additional contributions by: //
17
+ // //
18
+ // //
19
+ // //
20
+ // Create Date: 09/03/2018 //
21
+ // Design Name: FPU //
22
+ // Module Name: norm_div_sqrt_mvp.sv //
23
+ // Project Name: //
24
+ // Language: SystemVerilog //
25
+ // //
26
+ // Description: Floating point Normalizer/Rounding unit //
27
+ // Since this module is design as a combinatinal logic, it can//
28
+ // be added arbinary register stages for different frequency //
29
+ // in the wrapper module. //
30
+ // //
31
+ // //
32
+ // //
33
+ // Revision Date: 12/04/2018 //
34
+ // Lei Li //
35
+ // To address some requirements by Stefan //
36
+ // //
37
+ // //
38
+ // //
39
+ // //
40
+ // //
41
+ // //
42
+ ////////////////////////////////////////////////////////////////////////////////
43
+
44
+ import defs_div_sqrt_mvp::*;
45
+
46
+ module norm_div_sqrt_mvp
47
+ (//Inputs
48
+ input logic [C_MANT_FP64+4:0] Mant_in_DI, // Include the needed 4-bit for rounding and hidden bit
49
+ input logic signed [C_EXP_FP64+1:0] Exp_in_DI,
50
+ input logic Sign_in_DI,
51
+ input logic Div_enable_SI,
52
+ input logic Sqrt_enable_SI,
53
+ input logic Inf_a_SI,
54
+ input logic Inf_b_SI,
55
+ input logic Zero_a_SI,
56
+ input logic Zero_b_SI,
57
+ input logic NaN_a_SI,
58
+ input logic NaN_b_SI,
59
+ input logic SNaN_SI,
60
+ input logic [C_RM-1:0] RM_SI,
61
+ input logic Full_precision_SI,
62
+ input logic FP32_SI,
63
+ input logic FP64_SI,
64
+ input logic FP16_SI,
65
+ input logic FP16ALT_SI,
66
+ //Outputs
67
+ output logic [C_EXP_FP64+C_MANT_FP64:0] Result_DO,
68
+ output logic [4:0] Fflags_SO //{NV,DZ,OF,UF,NX}
69
+ );
70
+
71
+
72
+ logic Sign_res_D;
73
+
74
+ logic NV_OP_S;
75
+ logic Exp_OF_S;
76
+ logic Exp_UF_S;
77
+ logic Div_Zero_S;
78
+ logic In_Exact_S;
79
+
80
+ /////////////////////////////////////////////////////////////////////////////
81
+ // Normalization //
82
+ /////////////////////////////////////////////////////////////////////////////
83
+ logic [C_MANT_FP64:0] Mant_res_norm_D;
84
+ logic [C_EXP_FP64-1:0] Exp_res_norm_D;
85
+
86
+ /////////////////////////////////////////////////////////////////////////////
87
+ // Right shift operations for negtive exponents //
88
+ /////////////////////////////////////////////////////////////////////////////
89
+
90
+ logic [C_EXP_FP64+1:0] Exp_Max_RS_FP64_D;
91
+ logic [C_EXP_FP32+1:0] Exp_Max_RS_FP32_D;
92
+ logic [C_EXP_FP16+1:0] Exp_Max_RS_FP16_D;
93
+ logic [C_EXP_FP16ALT+1:0] Exp_Max_RS_FP16ALT_D;
94
+ //
95
+ assign Exp_Max_RS_FP64_D=Exp_in_DI[C_EXP_FP64:0]+C_MANT_FP64+1; // to check exponent after (C_MANT_FP64+1)-bit >> when Exp_in_DI is negative
96
+ assign Exp_Max_RS_FP32_D=Exp_in_DI[C_EXP_FP32:0]+C_MANT_FP32+1; // to check exponent after (C_MANT_FP32+1)-bit >> when Exp_in_DI is negative
97
+ assign Exp_Max_RS_FP16_D=Exp_in_DI[C_EXP_FP16:0]+C_MANT_FP16+1; // to check exponent after (C_MANT_FP16+1)-bit >> when Exp_in_DI is negative
98
+ assign Exp_Max_RS_FP16ALT_D=Exp_in_DI[C_EXP_FP16ALT:0]+C_MANT_FP16ALT+1; // to check exponent after (C_MANT_FP16ALT+1)-bit >> when Exp_in_DI is negative
99
+ logic [C_EXP_FP64+1:0] Num_RS_D;
100
+ assign Num_RS_D=~Exp_in_DI+1+1; // How many right shifts(RS) are needed to generate a denormal number? >> is need only when Exp_in_DI is negative
101
+ logic [C_MANT_FP64:0] Mant_RS_D;
102
+ logic [C_MANT_FP64+4:0] Mant_forsticky_D;
103
+ assign {Mant_RS_D,Mant_forsticky_D} ={Mant_in_DI,{(C_MANT_FP64+1){1'b0}} } >>(Num_RS_D); //
104
+ //
105
+ logic [C_EXP_FP64+1:0] Exp_subOne_D;
106
+ assign Exp_subOne_D = Exp_in_DI -1;
107
+
108
+ //normalization
109
+ logic [1:0] Mant_lower_D;
110
+ logic Mant_sticky_bit_D;
111
+ logic [C_MANT_FP64+4:0] Mant_forround_D;
112
+
113
+ always_comb
114
+ begin
115
+
116
+ if(NaN_a_SI) // if a is NaN, return NaN
117
+ begin
118
+ Div_Zero_S=1'b0;
119
+ Exp_OF_S=1'b0;
120
+ Exp_UF_S=1'b0;
121
+ Mant_res_norm_D={1'b0,C_MANT_NAN_FP64};
122
+ Exp_res_norm_D='1;
123
+ Mant_forround_D='0;
124
+ Sign_res_D=1'b0;
125
+ NV_OP_S = SNaN_SI;
126
+ end
127
+
128
+ else if(NaN_b_SI) //if b is NaN, return NaN
129
+ begin
130
+ Div_Zero_S=1'b0;
131
+ Exp_OF_S=1'b0;
132
+ Exp_UF_S=1'b0;
133
+ Mant_res_norm_D={1'b0,C_MANT_NAN_FP64};
134
+ Exp_res_norm_D='1;
135
+ Mant_forround_D='0;
136
+ Sign_res_D=1'b0;
137
+ NV_OP_S = SNaN_SI;
138
+ end
139
+
140
+ else if(Inf_a_SI)
141
+ begin
142
+ if(Div_enable_SI&&Inf_b_SI) //Inf/Inf, retrurn NaN
143
+ begin
144
+ Div_Zero_S=1'b0;
145
+ Exp_OF_S=1'b0;
146
+ Exp_UF_S=1'b0;
147
+ Mant_res_norm_D={1'b0,C_MANT_NAN_FP64};
148
+ Exp_res_norm_D='1;
149
+ Mant_forround_D='0;
150
+ Sign_res_D=1'b0;
151
+ NV_OP_S = 1'b1;
152
+ end
153
+ else if (Sqrt_enable_SI && Sign_in_DI) begin // catch sqrt(-inf)
154
+ Div_Zero_S=1'b0;
155
+ Exp_OF_S=1'b0;
156
+ Exp_UF_S=1'b0;
157
+ Mant_res_norm_D={1'b0,C_MANT_NAN_FP64};
158
+ Exp_res_norm_D='1;
159
+ Mant_forround_D='0;
160
+ Sign_res_D=1'b0;
161
+ NV_OP_S = 1'b1;
162
+ end else begin
163
+ Div_Zero_S=1'b0;
164
+ Exp_OF_S=1'b1;
165
+ Exp_UF_S=1'b0;
166
+ Mant_res_norm_D= '0;
167
+ Exp_res_norm_D='1;
168
+ Mant_forround_D='0;
169
+ Sign_res_D=Sign_in_DI;
170
+ NV_OP_S = 1'b0;
171
+ end
172
+ end
173
+
174
+ else if(Div_enable_SI&&Inf_b_SI)
175
+ begin
176
+ Div_Zero_S=1'b0;
177
+ Exp_OF_S=1'b1;
178
+ Exp_UF_S=1'b0;
179
+ Mant_res_norm_D= '0;
180
+ Exp_res_norm_D='0;
181
+ Mant_forround_D='0;
182
+ Sign_res_D=Sign_in_DI;
183
+ NV_OP_S = 1'b0;
184
+ end
185
+
186
+ else if(Zero_a_SI)
187
+ begin
188
+ if(Div_enable_SI&&Zero_b_SI)
189
+ begin
190
+ Div_Zero_S=1'b1;
191
+ Exp_OF_S=1'b0;
192
+ Exp_UF_S=1'b0;
193
+ Mant_res_norm_D={1'b0,C_MANT_NAN_FP64};
194
+ Exp_res_norm_D='1;
195
+ Mant_forround_D='0;
196
+ Sign_res_D=1'b0;
197
+ NV_OP_S = 1'b1;
198
+ end
199
+ else
200
+ begin
201
+ Div_Zero_S=1'b0;
202
+ Exp_OF_S=1'b0;
203
+ Exp_UF_S=1'b0;
204
+ Mant_res_norm_D='0;
205
+ Exp_res_norm_D='0;
206
+ Mant_forround_D='0;
207
+ Sign_res_D=Sign_in_DI;
208
+ NV_OP_S = 1'b0;
209
+ end
210
+ end
211
+
212
+ else if(Div_enable_SI&&(Zero_b_SI)) //div Zero
213
+ begin
214
+ Div_Zero_S=1'b1;
215
+ Exp_OF_S=1'b0;
216
+ Exp_UF_S=1'b0;
217
+ Mant_res_norm_D='0;
218
+ Exp_res_norm_D='1;
219
+ Mant_forround_D='0;
220
+ Sign_res_D=Sign_in_DI;
221
+ NV_OP_S = 1'b0;
222
+ end
223
+
224
+ else if(Sign_in_DI&&Sqrt_enable_SI) //sqrt(-a)
225
+ begin
226
+ Div_Zero_S=1'b0;
227
+ Exp_OF_S=1'b0;
228
+ Exp_UF_S=1'b0;
229
+ Mant_res_norm_D={1'b0,C_MANT_NAN_FP64};
230
+ Exp_res_norm_D='1;
231
+ Mant_forround_D='0;
232
+ Sign_res_D=1'b0;
233
+ NV_OP_S = 1'b1;
234
+ end
235
+
236
+ else if((Exp_in_DI[C_EXP_FP64:0]=='0))
237
+ begin
238
+ if(Mant_in_DI!='0) //Exp=0, Mant!=0, it is denormal
239
+ begin
240
+ Div_Zero_S=1'b0;
241
+ Exp_OF_S=1'b0;
242
+ Exp_UF_S=1'b1;
243
+ Mant_res_norm_D={1'b0,Mant_in_DI[C_MANT_FP64+4:5]};
244
+ Exp_res_norm_D='0;
245
+ Mant_forround_D={Mant_in_DI[4:0],{(C_MANT_FP64){1'b0}} };
246
+ Sign_res_D=Sign_in_DI;
247
+ NV_OP_S = 1'b0;
248
+ end
249
+ else // Zero
250
+ begin
251
+ Div_Zero_S=1'b0;
252
+ Exp_OF_S=1'b0;
253
+ Exp_UF_S=1'b0;
254
+ Mant_res_norm_D='0;
255
+ Exp_res_norm_D='0;
256
+ Mant_forround_D='0;
257
+ Sign_res_D=Sign_in_DI;
258
+ NV_OP_S = 1'b0;
259
+ end
260
+ end
261
+
262
+ else if((Exp_in_DI[C_EXP_FP64:0]==C_EXP_ONE_FP64)&&(~Mant_in_DI[C_MANT_FP64+4])) //denormal
263
+ begin
264
+ Div_Zero_S=1'b0;
265
+ Exp_OF_S=1'b0;
266
+ Exp_UF_S=1'b1;
267
+ Mant_res_norm_D=Mant_in_DI[C_MANT_FP64+4:4];
268
+ Exp_res_norm_D='0;
269
+ Mant_forround_D={Mant_in_DI[3:0],{(C_MANT_FP64+1){1'b0}}};
270
+ Sign_res_D=Sign_in_DI;
271
+ NV_OP_S = 1'b0;
272
+ end
273
+
274
+ else if(Exp_in_DI[C_EXP_FP64+1]) //minus //consider format
275
+ begin
276
+ Div_Zero_S=1'b0;
277
+ Exp_OF_S=1'b0;
278
+ Exp_UF_S=1'b1;
279
+ Mant_res_norm_D={Mant_RS_D[C_MANT_FP64:0]};
280
+ Exp_res_norm_D='0;
281
+ Mant_forround_D={Mant_forsticky_D[C_MANT_FP64+4:0]}; //??
282
+ Sign_res_D=Sign_in_DI;
283
+ NV_OP_S = 1'b0;
284
+ end
285
+
286
+ else if( (Exp_in_DI[C_EXP_FP32]&&FP32_SI) | (Exp_in_DI[C_EXP_FP64]&&FP64_SI) | (Exp_in_DI[C_EXP_FP16]&&FP16_SI) | (Exp_in_DI[C_EXP_FP16ALT]&&FP16ALT_SI) ) //OF
287
+ begin
288
+ Div_Zero_S=1'b0;
289
+ Exp_OF_S=1'b1;
290
+ Exp_UF_S=1'b0;
291
+ Mant_res_norm_D='0;
292
+ Exp_res_norm_D='1;
293
+ Mant_forround_D='0;
294
+ Sign_res_D=Sign_in_DI;
295
+ NV_OP_S = 1'b0;
296
+ end
297
+
298
+ else if( ((Exp_in_DI[C_EXP_FP32-1:0]=='1)&&FP32_SI) | ((Exp_in_DI[C_EXP_FP64-1:0]=='1)&&FP64_SI) | ((Exp_in_DI[C_EXP_FP16-1:0]=='1)&&FP16_SI) | ((Exp_in_DI[C_EXP_FP16ALT-1:0]=='1)&&FP16ALT_SI) )//255
299
+ begin
300
+ if(~Mant_in_DI[C_MANT_FP64+4]) // MSB=0
301
+ begin
302
+ Div_Zero_S=1'b0;
303
+ Exp_OF_S=1'b0;
304
+ Exp_UF_S=1'b0;
305
+ Mant_res_norm_D=Mant_in_DI[C_MANT_FP64+3:3];
306
+ Exp_res_norm_D=Exp_subOne_D;
307
+ Mant_forround_D={Mant_in_DI[2:0],{(C_MANT_FP64+2){1'b0}}};
308
+ Sign_res_D=Sign_in_DI;
309
+ NV_OP_S = 1'b0;
310
+ end
311
+ else if(Mant_in_DI!='0) //NaN
312
+ begin
313
+ Div_Zero_S=1'b0;
314
+ Exp_OF_S=1'b1;
315
+ Exp_UF_S=1'b0;
316
+ Mant_res_norm_D= '0;
317
+ Exp_res_norm_D='1;
318
+ Mant_forround_D='0;
319
+ Sign_res_D=Sign_in_DI;
320
+ NV_OP_S = 1'b0;
321
+ end
322
+ else //infinity
323
+ begin
324
+ Div_Zero_S=1'b0;
325
+ Exp_OF_S=1'b1;
326
+ Exp_UF_S=1'b0;
327
+ Mant_res_norm_D= '0;
328
+ Exp_res_norm_D='1;
329
+ Mant_forround_D='0;
330
+ Sign_res_D=Sign_in_DI;
331
+ NV_OP_S = 1'b0;
332
+ end
333
+ end
334
+
335
+ else if(Mant_in_DI[C_MANT_FP64+4]) //normal numbers with 1.XXX
336
+ begin
337
+ Div_Zero_S=1'b0;
338
+ Exp_OF_S=1'b0;
339
+ Exp_UF_S=1'b0;
340
+ Mant_res_norm_D= Mant_in_DI[C_MANT_FP64+4:4];
341
+ Exp_res_norm_D=Exp_in_DI[C_EXP_FP64-1:0];
342
+ Mant_forround_D={Mant_in_DI[3:0],{(C_MANT_FP64+1){1'b0}}};
343
+ Sign_res_D=Sign_in_DI;
344
+ NV_OP_S = 1'b0;
345
+ end
346
+
347
+ else //normal numbers with 0.1XX
348
+ begin
349
+ Div_Zero_S=1'b0;
350
+ Exp_OF_S=1'b0;
351
+ Exp_UF_S=1'b0;
352
+ Mant_res_norm_D=Mant_in_DI[C_MANT_FP64+3:3];
353
+ Exp_res_norm_D=Exp_subOne_D;
354
+ Mant_forround_D={Mant_in_DI[2:0],{(C_MANT_FP64+2){1'b0}}};
355
+ Sign_res_D=Sign_in_DI;
356
+ NV_OP_S = 1'b0;
357
+ end
358
+
359
+ end
360
+
361
+ /////////////////////////////////////////////////////////////////////////////
362
+ // Rounding enable only for full precision (Full_precision_SI==1'b1) //
363
+ /////////////////////////////////////////////////////////////////////////////
364
+
365
+ logic [C_MANT_FP64:0] Mant_upper_D;
366
+ logic [C_MANT_FP64+1:0] Mant_upperRounded_D;
367
+ logic Mant_roundUp_S;
368
+ logic Mant_rounded_S;
369
+
370
+ always_comb //determine which bits for Mant_lower_D and Mant_sticky_bit_D
371
+ begin
372
+ if(FP32_SI)
373
+ begin
374
+ Mant_upper_D = {Mant_res_norm_D[C_MANT_FP64:C_MANT_FP64-C_MANT_FP32], {(C_MANT_FP64-C_MANT_FP32){1'b0}} };
375
+ Mant_lower_D = Mant_res_norm_D[C_MANT_FP64-C_MANT_FP32-1:C_MANT_FP64-C_MANT_FP32-2];
376
+ Mant_sticky_bit_D = | Mant_res_norm_D[C_MANT_FP64-C_MANT_FP32-3:0];
377
+ end
378
+ else if(FP64_SI)
379
+ begin
380
+ Mant_upper_D = Mant_res_norm_D[C_MANT_FP64:0];
381
+ Mant_lower_D = Mant_forround_D[C_MANT_FP64+4:C_MANT_FP64+3];
382
+ Mant_sticky_bit_D = | Mant_forround_D[C_MANT_FP64+3:0];
383
+ end
384
+ else if(FP16_SI)
385
+ begin
386
+ Mant_upper_D = {Mant_res_norm_D[C_MANT_FP64:C_MANT_FP64-C_MANT_FP16], {(C_MANT_FP64-C_MANT_FP16){1'b0}} };
387
+ Mant_lower_D = Mant_res_norm_D[C_MANT_FP64-C_MANT_FP16-1:C_MANT_FP64-C_MANT_FP16-2];
388
+ Mant_sticky_bit_D = | Mant_res_norm_D[C_MANT_FP64-C_MANT_FP16-3:30];
389
+ end
390
+ else //FP16ALT
391
+ begin
392
+ Mant_upper_D = {Mant_res_norm_D[C_MANT_FP64:C_MANT_FP64-C_MANT_FP16ALT], {(C_MANT_FP64-C_MANT_FP16ALT){1'b0}} };
393
+ Mant_lower_D = Mant_res_norm_D[C_MANT_FP64-C_MANT_FP16ALT-1:C_MANT_FP64-C_MANT_FP16ALT-2];
394
+ Mant_sticky_bit_D = | Mant_res_norm_D[C_MANT_FP64-C_MANT_FP16ALT-3:30];
395
+ end
396
+ end
397
+
398
+ assign Mant_rounded_S = (|(Mant_lower_D))| Mant_sticky_bit_D;
399
+
400
+
401
+
402
+
403
+ always_comb //determine whether to round up or not
404
+ begin
405
+ Mant_roundUp_S = 1'b0;
406
+ case (RM_SI)
407
+ C_RM_NEAREST :
408
+ Mant_roundUp_S = Mant_lower_D[1] && ((Mant_lower_D[0] | Mant_sticky_bit_D )| ( (FP32_SI&&Mant_upper_D[C_MANT_FP64-C_MANT_FP32]) | (FP64_SI&&Mant_upper_D[0]) | (FP16_SI&&Mant_upper_D[C_MANT_FP64-C_MANT_FP16]) | (FP16ALT_SI&&Mant_upper_D[C_MANT_FP64-C_MANT_FP16ALT]) ) );
409
+ C_RM_TRUNC :
410
+ Mant_roundUp_S = 0;
411
+ C_RM_PLUSINF :
412
+ Mant_roundUp_S = Mant_rounded_S & ~Sign_in_DI;
413
+ C_RM_MINUSINF:
414
+ Mant_roundUp_S = Mant_rounded_S & Sign_in_DI;
415
+ default :
416
+ Mant_roundUp_S = 0;
417
+ endcase // case (RM_DI)
418
+ end // always_comb begin
419
+
420
+ logic Mant_renorm_S;
421
+ logic [C_MANT_FP64:0] Mant_roundUp_Vector_S; // for all the formats
422
+
423
+ assign Mant_roundUp_Vector_S={7'h0,(FP16ALT_SI&&Mant_roundUp_S),2'h0,(FP16_SI&&Mant_roundUp_S),12'h0,(FP32_SI&&Mant_roundUp_S),28'h0,(FP64_SI&&Mant_roundUp_S)};
424
+
425
+
426
+ assign Mant_upperRounded_D = Mant_upper_D + Mant_roundUp_Vector_S;
427
+ assign Mant_renorm_S = Mant_upperRounded_D[C_MANT_FP64+1];
428
+
429
+ /////////////////////////////////////////////////////////////////////////////
430
+ // Renormalization for Rounding //
431
+ /////////////////////////////////////////////////////////////////////////////
432
+ logic [C_MANT_FP64-1:0] Mant_res_round_D;
433
+ logic [C_EXP_FP64-1:0] Exp_res_round_D;
434
+
435
+
436
+ assign Mant_res_round_D = (Mant_renorm_S)?Mant_upperRounded_D[C_MANT_FP64:1]:Mant_upperRounded_D[C_MANT_FP64-1:0]; // including the process of the hidden bit
437
+ assign Exp_res_round_D = Exp_res_norm_D+Mant_renorm_S;
438
+
439
+ /////////////////////////////////////////////////////////////////////////////
440
+ // Output Assignments //
441
+ /////////////////////////////////////////////////////////////////////////////
442
+ logic [C_MANT_FP64-1:0] Mant_before_format_ctl_D;
443
+ logic [C_EXP_FP64-1:0] Exp_before_format_ctl_D;
444
+ assign Mant_before_format_ctl_D = Full_precision_SI ? Mant_res_round_D : Mant_res_norm_D;
445
+ assign Exp_before_format_ctl_D = Full_precision_SI ? Exp_res_round_D : Exp_res_norm_D;
446
+
447
+ always_comb //NaN Boxing
448
+ begin //
449
+ if(FP32_SI)
450
+ begin
451
+ Result_DO ={32'hffff_ffff,Sign_res_D,Exp_before_format_ctl_D[C_EXP_FP32-1:0],Mant_before_format_ctl_D[C_MANT_FP64-1:C_MANT_FP64-C_MANT_FP32]};
452
+ end
453
+ else if(FP64_SI)
454
+ begin
455
+ Result_DO ={Sign_res_D,Exp_before_format_ctl_D[C_EXP_FP64-1:0],Mant_before_format_ctl_D[C_MANT_FP64-1:0]};
456
+ end
457
+ else if(FP16_SI)
458
+ begin
459
+ Result_DO ={48'hffff_ffff_ffff,Sign_res_D,Exp_before_format_ctl_D[C_EXP_FP16-1:0],Mant_before_format_ctl_D[C_MANT_FP64-1:C_MANT_FP64-C_MANT_FP16]};
460
+ end
461
+ else
462
+ begin
463
+ Result_DO ={48'hffff_ffff_ffff,Sign_res_D,Exp_before_format_ctl_D[C_EXP_FP16ALT-1:0],Mant_before_format_ctl_D[C_MANT_FP64-1:C_MANT_FP64-C_MANT_FP16ALT]};
464
+ end
465
+ end
466
+
467
+ assign In_Exact_S = (~Full_precision_SI) | Mant_rounded_S;
468
+ assign Fflags_SO = {NV_OP_S,Div_Zero_S,Exp_OF_S,Exp_UF_S,In_Exact_S}; //{NV,DZ,OF,UF,NX}
469
+
470
+ endmodule // norm_div_sqrt_mvp
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/nrbd_nrsc_mvp.sv ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the “License”); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ ////////////////////////////////////////////////////////////////////////////////
11
+ // Company: IIS @ ETHZ - Federal Institute of Technology //
12
+ // //
13
+ // Engineers: Lei Li lile@iis.ee.ethz.ch //
14
+ // //
15
+ // Additional contributions by: //
16
+ // //
17
+ // //
18
+ // //
19
+ // Create Date: 10/04/2018 //
20
+ // Design Name: FPU //
21
+ // Module Name: nrbd_nrsc_mvp.sv //
22
+ // Project Name: Private FPU //
23
+ // Language: SystemVerilog //
24
+ // //
25
+ // Description: non restroring binary divisior/ square root //
26
+ // //
27
+ // Revision Date: 12/04/2018 //
28
+ // Lei Li //
29
+ // To address some requirements by Stefan and add low power //
30
+ // control for special cases //
31
+ // //
32
+ ////////////////////////////////////////////////////////////////////////////////
33
+
34
+ import defs_div_sqrt_mvp::*;
35
+
36
+ module nrbd_nrsc_mvp
37
+
38
+ (//Input
39
+ input logic Clk_CI,
40
+ input logic Rst_RBI,
41
+ input logic Div_start_SI,
42
+ input logic Sqrt_start_SI,
43
+ input logic Start_SI,
44
+ input logic Kill_SI,
45
+ input logic Special_case_SBI,
46
+ input logic Special_case_dly_SBI,
47
+ input logic [C_PC-1:0] Precision_ctl_SI,
48
+ input logic [1:0] Format_sel_SI,
49
+ input logic [C_MANT_FP64:0] Mant_a_DI,
50
+ input logic [C_MANT_FP64:0] Mant_b_DI,
51
+ input logic [C_EXP_FP64:0] Exp_a_DI,
52
+ input logic [C_EXP_FP64:0] Exp_b_DI,
53
+ //output
54
+ output logic Div_enable_SO,
55
+ output logic Sqrt_enable_SO,
56
+
57
+ output logic Full_precision_SO,
58
+ output logic FP32_SO,
59
+ output logic FP64_SO,
60
+ output logic FP16_SO,
61
+ output logic FP16ALT_SO,
62
+ output logic Ready_SO,
63
+ output logic Done_SO,
64
+ output logic [C_MANT_FP64+4:0] Mant_z_DO,
65
+ output logic [C_EXP_FP64+1:0] Exp_z_DO
66
+ );
67
+
68
+
69
+ logic Div_start_dly_S,Sqrt_start_dly_S;
70
+
71
+
72
+ control_mvp control_U0
73
+ ( .Clk_CI (Clk_CI ),
74
+ .Rst_RBI (Rst_RBI ),
75
+ .Div_start_SI (Div_start_SI ),
76
+ .Sqrt_start_SI (Sqrt_start_SI ),
77
+ .Start_SI (Start_SI ),
78
+ .Kill_SI (Kill_SI ),
79
+ .Special_case_SBI (Special_case_SBI ),
80
+ .Special_case_dly_SBI (Special_case_dly_SBI ),
81
+ .Precision_ctl_SI (Precision_ctl_SI ),
82
+ .Format_sel_SI (Format_sel_SI ),
83
+ .Numerator_DI (Mant_a_DI ),
84
+ .Exp_num_DI (Exp_a_DI ),
85
+ .Denominator_DI (Mant_b_DI ),
86
+ .Exp_den_DI (Exp_b_DI ),
87
+ .Div_start_dly_SO (Div_start_dly_S ),
88
+ .Sqrt_start_dly_SO (Sqrt_start_dly_S ),
89
+ .Div_enable_SO (Div_enable_SO ),
90
+ .Sqrt_enable_SO (Sqrt_enable_SO ),
91
+ .Full_precision_SO (Full_precision_SO ),
92
+ .FP32_SO (FP32_SO ),
93
+ .FP64_SO (FP64_SO ),
94
+ .FP16_SO (FP16_SO ),
95
+ .FP16ALT_SO (FP16ALT_SO ),
96
+ .Ready_SO (Ready_SO ),
97
+ .Done_SO (Done_SO ),
98
+ .Mant_result_prenorm_DO (Mant_z_DO ),
99
+ .Exp_result_prenorm_DO (Exp_z_DO )
100
+ );
101
+
102
+
103
+
104
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvfpu/src/fpu_div_sqrt_mvp/hdl/preprocess_mvp.sv ADDED
@@ -0,0 +1,425 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the “License”); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ ////////////////////////////////////////////////////////////////////////////////
11
+ // Company: IIS @ ETHZ - Federal Institute of Technology //
12
+ // //
13
+ // Engineers: Lei Li //lile@iis.ee.ethz.ch //
14
+ // //
15
+ // Additional contributions by: //
16
+ // //
17
+ // //
18
+ // //
19
+ // Create Date: 01/03/2018 //
20
+ // Design Name: FPU //
21
+ // Module Name: preprocess_mvp.sv //
22
+ // Project Name: Private FPU //
23
+ // Language: SystemVerilog //
24
+ // //
25
+ // Description: decode and data preparation //
26
+ // //
27
+ // Revision Date: 12/04/2018 //
28
+ // Lei Li //
29
+ // To address some requirements by Stefan and add low power //
30
+ // control for special cases //
31
+ // //
32
+ // //
33
+ ////////////////////////////////////////////////////////////////////////////////
34
+
35
+ import defs_div_sqrt_mvp::*;
36
+
37
+ module preprocess_mvp
38
+ (
39
+ input logic Clk_CI,
40
+ input logic Rst_RBI,
41
+ input logic Div_start_SI,
42
+ input logic Sqrt_start_SI,
43
+ input logic Ready_SI,
44
+ //Input Operands
45
+ input logic [C_OP_FP64-1:0] Operand_a_DI,
46
+ input logic [C_OP_FP64-1:0] Operand_b_DI,
47
+ input logic [C_RM-1:0] RM_SI, //Rounding Mode
48
+ input logic [C_FS-1:0] Format_sel_SI, // Format Selection
49
+
50
+ // to control
51
+ output logic Start_SO,
52
+ output logic [C_EXP_FP64:0] Exp_a_DO_norm,
53
+ output logic [C_EXP_FP64:0] Exp_b_DO_norm,
54
+ output logic [C_MANT_FP64:0] Mant_a_DO_norm,
55
+ output logic [C_MANT_FP64:0] Mant_b_DO_norm,
56
+
57
+ output logic [C_RM-1:0] RM_dly_SO,
58
+
59
+ output logic Sign_z_DO,
60
+ output logic Inf_a_SO,
61
+ output logic Inf_b_SO,
62
+ output logic Zero_a_SO,
63
+ output logic Zero_b_SO,
64
+ output logic NaN_a_SO,
65
+ output logic NaN_b_SO,
66
+ output logic SNaN_SO,
67
+ output logic Special_case_SBO,
68
+ output logic Special_case_dly_SBO
69
+ );
70
+
71
+ //Hidden Bits
72
+ logic Hb_a_D;
73
+ logic Hb_b_D;
74
+
75
+ logic [C_EXP_FP64-1:0] Exp_a_D;
76
+ logic [C_EXP_FP64-1:0] Exp_b_D;
77
+ logic [C_MANT_FP64-1:0] Mant_a_NonH_D;
78
+ logic [C_MANT_FP64-1:0] Mant_b_NonH_D;
79
+ logic [C_MANT_FP64:0] Mant_a_D;
80
+ logic [C_MANT_FP64:0] Mant_b_D;
81
+
82
+ /////////////////////////////////////////////////////////////////////////////
83
+ // Disassemble operands
84
+ /////////////////////////////////////////////////////////////////////////////
85
+ logic Sign_a_D,Sign_b_D;
86
+ logic Start_S;
87
+
88
+ always_comb
89
+ begin
90
+ case(Format_sel_SI)
91
+ 2'b00:
92
+ begin
93
+ Sign_a_D = Operand_a_DI[C_OP_FP32-1];
94
+ Sign_b_D = Operand_b_DI[C_OP_FP32-1];
95
+ Exp_a_D = {3'h0, Operand_a_DI[C_OP_FP32-2:C_MANT_FP32]};
96
+ Exp_b_D = {3'h0, Operand_b_DI[C_OP_FP32-2:C_MANT_FP32]};
97
+ Mant_a_NonH_D = {Operand_a_DI[C_MANT_FP32-1:0],29'h0};
98
+ Mant_b_NonH_D = {Operand_b_DI[C_MANT_FP32-1:0],29'h0};
99
+ end
100
+ 2'b01:
101
+ begin
102
+ Sign_a_D = Operand_a_DI[C_OP_FP64-1];
103
+ Sign_b_D = Operand_b_DI[C_OP_FP64-1];
104
+ Exp_a_D = Operand_a_DI[C_OP_FP64-2:C_MANT_FP64];
105
+ Exp_b_D = Operand_b_DI[C_OP_FP64-2:C_MANT_FP64];
106
+ Mant_a_NonH_D = Operand_a_DI[C_MANT_FP64-1:0];
107
+ Mant_b_NonH_D = Operand_b_DI[C_MANT_FP64-1:0];
108
+ end
109
+ 2'b10:
110
+ begin
111
+ Sign_a_D = Operand_a_DI[C_OP_FP16-1];
112
+ Sign_b_D = Operand_b_DI[C_OP_FP16-1];
113
+ Exp_a_D = {6'h00, Operand_a_DI[C_OP_FP16-2:C_MANT_FP16]};
114
+ Exp_b_D = {6'h00, Operand_b_DI[C_OP_FP16-2:C_MANT_FP16]};
115
+ Mant_a_NonH_D = {Operand_a_DI[C_MANT_FP16-1:0],42'h0};
116
+ Mant_b_NonH_D = {Operand_b_DI[C_MANT_FP16-1:0],42'h0};
117
+ end
118
+ 2'b11:
119
+ begin
120
+ Sign_a_D = Operand_a_DI[C_OP_FP16ALT-1];
121
+ Sign_b_D = Operand_b_DI[C_OP_FP16ALT-1];
122
+ Exp_a_D = {3'h0, Operand_a_DI[C_OP_FP16ALT-2:C_MANT_FP16ALT]};
123
+ Exp_b_D = {3'h0, Operand_b_DI[C_OP_FP16ALT-2:C_MANT_FP16ALT]};
124
+ Mant_a_NonH_D = {Operand_a_DI[C_MANT_FP16ALT-1:0],45'h0};
125
+ Mant_b_NonH_D = {Operand_b_DI[C_MANT_FP16ALT-1:0],45'h0};
126
+ end
127
+ endcase
128
+ end
129
+
130
+
131
+ assign Mant_a_D = {Hb_a_D,Mant_a_NonH_D};
132
+ assign Mant_b_D = {Hb_b_D,Mant_b_NonH_D};
133
+
134
+ assign Hb_a_D = | Exp_a_D; // hidden bit
135
+ assign Hb_b_D = | Exp_b_D; // hidden bit
136
+
137
+ assign Start_S= Div_start_SI | Sqrt_start_SI;
138
+
139
+
140
+
141
+ /////////////////////////////////////////////////////////////////////////////
142
+ // preliminary checks for infinite/zero/NaN operands //
143
+ /////////////////////////////////////////////////////////////////////////////
144
+
145
+ logic Mant_a_prenorm_zero_S;
146
+ logic Mant_b_prenorm_zero_S;
147
+
148
+ logic Exp_a_prenorm_zero_S;
149
+ logic Exp_b_prenorm_zero_S;
150
+ assign Exp_a_prenorm_zero_S = ~Hb_a_D;
151
+ assign Exp_b_prenorm_zero_S = ~Hb_b_D;
152
+
153
+ logic Exp_a_prenorm_Inf_NaN_S;
154
+ logic Exp_b_prenorm_Inf_NaN_S;
155
+
156
+ logic Mant_a_prenorm_QNaN_S;
157
+ logic Mant_a_prenorm_SNaN_S;
158
+ logic Mant_b_prenorm_QNaN_S;
159
+ logic Mant_b_prenorm_SNaN_S;
160
+
161
+ assign Mant_a_prenorm_QNaN_S=Mant_a_NonH_D[C_MANT_FP64-1]&&(~(|Mant_a_NonH_D[C_MANT_FP64-2:0]));
162
+ assign Mant_a_prenorm_SNaN_S=(~Mant_a_NonH_D[C_MANT_FP64-1])&&((|Mant_a_NonH_D[C_MANT_FP64-2:0]));
163
+ assign Mant_b_prenorm_QNaN_S=Mant_b_NonH_D[C_MANT_FP64-1]&&(~(|Mant_b_NonH_D[C_MANT_FP64-2:0]));
164
+ assign Mant_b_prenorm_SNaN_S=(~Mant_b_NonH_D[C_MANT_FP64-1])&&((|Mant_b_NonH_D[C_MANT_FP64-2:0]));
165
+
166
+ always_comb
167
+ begin
168
+ case(Format_sel_SI)
169
+ 2'b00:
170
+ begin
171
+ Mant_a_prenorm_zero_S=(Operand_a_DI[C_MANT_FP32-1:0] == C_MANT_ZERO_FP32);
172
+ Mant_b_prenorm_zero_S=(Operand_b_DI[C_MANT_FP32-1:0] == C_MANT_ZERO_FP32);
173
+ Exp_a_prenorm_Inf_NaN_S=(Operand_a_DI[C_OP_FP32-2:C_MANT_FP32] == C_EXP_INF_FP32);
174
+ Exp_b_prenorm_Inf_NaN_S=(Operand_b_DI[C_OP_FP32-2:C_MANT_FP32] == C_EXP_INF_FP32);
175
+ end
176
+ 2'b01:
177
+ begin
178
+ Mant_a_prenorm_zero_S=(Operand_a_DI[C_MANT_FP64-1:0] == C_MANT_ZERO_FP64);
179
+ Mant_b_prenorm_zero_S=(Operand_b_DI[C_MANT_FP64-1:0] == C_MANT_ZERO_FP64);
180
+ Exp_a_prenorm_Inf_NaN_S=(Operand_a_DI[C_OP_FP64-2:C_MANT_FP64] == C_EXP_INF_FP64);
181
+ Exp_b_prenorm_Inf_NaN_S=(Operand_b_DI[C_OP_FP64-2:C_MANT_FP64] == C_EXP_INF_FP64);
182
+ end
183
+ 2'b10:
184
+ begin
185
+ Mant_a_prenorm_zero_S=(Operand_a_DI[C_MANT_FP16-1:0] == C_MANT_ZERO_FP16);
186
+ Mant_b_prenorm_zero_S=(Operand_b_DI[C_MANT_FP16-1:0] == C_MANT_ZERO_FP16);
187
+ Exp_a_prenorm_Inf_NaN_S=(Operand_a_DI[C_OP_FP16-2:C_MANT_FP16] == C_EXP_INF_FP16);
188
+ Exp_b_prenorm_Inf_NaN_S=(Operand_b_DI[C_OP_FP16-2:C_MANT_FP16] == C_EXP_INF_FP16);
189
+ end
190
+ 2'b11:
191
+ begin
192
+ Mant_a_prenorm_zero_S=(Operand_a_DI[C_MANT_FP16ALT-1:0] == C_MANT_ZERO_FP16ALT);
193
+ Mant_b_prenorm_zero_S=(Operand_b_DI[C_MANT_FP16ALT-1:0] == C_MANT_ZERO_FP16ALT);
194
+ Exp_a_prenorm_Inf_NaN_S=(Operand_a_DI[C_OP_FP16ALT-2:C_MANT_FP16ALT] == C_EXP_INF_FP16ALT);
195
+ Exp_b_prenorm_Inf_NaN_S=(Operand_b_DI[C_OP_FP16ALT-2:C_MANT_FP16ALT] == C_EXP_INF_FP16ALT);
196
+ end
197
+ endcase
198
+ end
199
+
200
+
201
+
202
+
203
+ logic Zero_a_SN,Zero_a_SP;
204
+ logic Zero_b_SN,Zero_b_SP;
205
+ logic Inf_a_SN,Inf_a_SP;
206
+ logic Inf_b_SN,Inf_b_SP;
207
+ logic NaN_a_SN,NaN_a_SP;
208
+ logic NaN_b_SN,NaN_b_SP;
209
+ logic SNaN_SN,SNaN_SP;
210
+
211
+ assign Zero_a_SN = (Start_S&&Ready_SI)?(Exp_a_prenorm_zero_S&&Mant_a_prenorm_zero_S):Zero_a_SP;
212
+ assign Zero_b_SN = (Start_S&&Ready_SI)?(Exp_b_prenorm_zero_S&&Mant_b_prenorm_zero_S):Zero_b_SP;
213
+ assign Inf_a_SN = (Start_S&&Ready_SI)?(Exp_a_prenorm_Inf_NaN_S&&Mant_a_prenorm_zero_S):Inf_a_SP;
214
+ assign Inf_b_SN = (Start_S&&Ready_SI)?(Exp_b_prenorm_Inf_NaN_S&&Mant_b_prenorm_zero_S):Inf_b_SP;
215
+ assign NaN_a_SN = (Start_S&&Ready_SI)?(Exp_a_prenorm_Inf_NaN_S&&(~Mant_a_prenorm_zero_S)):NaN_a_SP;
216
+ assign NaN_b_SN = (Start_S&&Ready_SI)?(Exp_b_prenorm_Inf_NaN_S&&(~Mant_b_prenorm_zero_S)):NaN_b_SP;
217
+ assign SNaN_SN = (Start_S&&Ready_SI) ? ((Mant_a_prenorm_SNaN_S&&NaN_a_SN) | (Mant_b_prenorm_SNaN_S&&NaN_b_SN)) : SNaN_SP;
218
+
219
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
220
+ begin
221
+ if(~Rst_RBI)
222
+ begin
223
+ Zero_a_SP <='0;
224
+ Zero_b_SP <='0;
225
+ Inf_a_SP <='0;
226
+ Inf_b_SP <='0;
227
+ NaN_a_SP <='0;
228
+ NaN_b_SP <='0;
229
+ SNaN_SP <= '0;
230
+ end
231
+ else
232
+ begin
233
+ Inf_a_SP <=Inf_a_SN;
234
+ Inf_b_SP <=Inf_b_SN;
235
+ Zero_a_SP <=Zero_a_SN;
236
+ Zero_b_SP <=Zero_b_SN;
237
+ NaN_a_SP <=NaN_a_SN;
238
+ NaN_b_SP <=NaN_b_SN;
239
+ SNaN_SP <= SNaN_SN;
240
+ end
241
+ end
242
+
243
+ /////////////////////////////////////////////////////////////////////////////
244
+ // Low power control
245
+ /////////////////////////////////////////////////////////////////////////////
246
+
247
+ assign Special_case_SBO=(~{(Div_start_SI)?(Zero_a_SN | Zero_b_SN | Inf_a_SN | Inf_b_SN | NaN_a_SN | NaN_b_SN): (Zero_a_SN | Inf_a_SN | NaN_a_SN | Sign_a_D) })&&(Start_S&&Ready_SI);
248
+
249
+
250
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
251
+ begin
252
+ if(~Rst_RBI)
253
+ begin
254
+ Special_case_dly_SBO <= '0;
255
+ end
256
+ else if((Start_S&&Ready_SI))
257
+ begin
258
+ Special_case_dly_SBO <= Special_case_SBO;
259
+ end
260
+ else if(Special_case_dly_SBO)
261
+ begin
262
+ Special_case_dly_SBO <= 1'b1;
263
+ end
264
+ else
265
+ begin
266
+ Special_case_dly_SBO <= '0;
267
+ end
268
+ end
269
+
270
+ /////////////////////////////////////////////////////////////////////////////
271
+ // Delay sign for normalization and round //
272
+ /////////////////////////////////////////////////////////////////////////////
273
+
274
+ logic Sign_z_DN;
275
+ logic Sign_z_DP;
276
+
277
+ always_comb
278
+ begin
279
+ if(Div_start_SI&&Ready_SI)
280
+ Sign_z_DN = Sign_a_D ^ Sign_b_D;
281
+ else if(Sqrt_start_SI&&Ready_SI)
282
+ Sign_z_DN = Sign_a_D;
283
+ else
284
+ Sign_z_DN = Sign_z_DP;
285
+ end
286
+
287
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
288
+ begin
289
+ if(~Rst_RBI)
290
+ begin
291
+ Sign_z_DP <= '0;
292
+ end
293
+ else
294
+ begin
295
+ Sign_z_DP <= Sign_z_DN;
296
+ end
297
+ end
298
+
299
+ logic [C_RM-1:0] RM_DN;
300
+ logic [C_RM-1:0] RM_DP;
301
+
302
+ always_comb
303
+ begin
304
+ if(Start_S&&Ready_SI)
305
+ RM_DN = RM_SI;
306
+ else
307
+ RM_DN = RM_DP;
308
+ end
309
+
310
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
311
+ begin
312
+ if(~Rst_RBI)
313
+ begin
314
+ RM_DP <= '0;
315
+ end
316
+ else
317
+ begin
318
+ RM_DP <= RM_DN;
319
+ end
320
+ end
321
+ assign RM_dly_SO = RM_DP;
322
+
323
+ logic [5:0] Mant_leadingOne_a, Mant_leadingOne_b;
324
+ logic Mant_zero_S_a,Mant_zero_S_b;
325
+
326
+ lzc #(
327
+ .WIDTH ( C_MANT_FP64+1 ),
328
+ .MODE ( 1 )
329
+ ) LOD_Ua (
330
+ .in_i ( Mant_a_D ),
331
+ .cnt_o ( Mant_leadingOne_a ),
332
+ .empty_o ( Mant_zero_S_a )
333
+ );
334
+
335
+ logic [C_MANT_FP64:0] Mant_a_norm_DN,Mant_a_norm_DP;
336
+
337
+ assign Mant_a_norm_DN = ((Start_S&&Ready_SI))?(Mant_a_D<<(Mant_leadingOne_a)):Mant_a_norm_DP;
338
+
339
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
340
+ begin
341
+ if(~Rst_RBI)
342
+ begin
343
+ Mant_a_norm_DP <= '0;
344
+ end
345
+ else
346
+ begin
347
+ Mant_a_norm_DP<=Mant_a_norm_DN;
348
+ end
349
+ end
350
+
351
+ logic [C_EXP_FP64:0] Exp_a_norm_DN,Exp_a_norm_DP;
352
+ assign Exp_a_norm_DN = ((Start_S&&Ready_SI))?(Exp_a_D-Mant_leadingOne_a+(|Mant_leadingOne_a)):Exp_a_norm_DP; //Covering the process of denormal numbers
353
+
354
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
355
+ begin
356
+ if(~Rst_RBI)
357
+ begin
358
+ Exp_a_norm_DP <= '0;
359
+ end
360
+ else
361
+ begin
362
+ Exp_a_norm_DP<=Exp_a_norm_DN;
363
+ end
364
+ end
365
+
366
+ lzc #(
367
+ .WIDTH ( C_MANT_FP64+1 ),
368
+ .MODE ( 1 )
369
+ ) LOD_Ub (
370
+ .in_i ( Mant_b_D ),
371
+ .cnt_o ( Mant_leadingOne_b ),
372
+ .empty_o ( Mant_zero_S_b )
373
+ );
374
+
375
+
376
+ logic [C_MANT_FP64:0] Mant_b_norm_DN,Mant_b_norm_DP;
377
+
378
+ assign Mant_b_norm_DN = ((Start_S&&Ready_SI))?(Mant_b_D<<(Mant_leadingOne_b)):Mant_b_norm_DP;
379
+
380
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
381
+ begin
382
+ if(~Rst_RBI)
383
+ begin
384
+ Mant_b_norm_DP <= '0;
385
+ end
386
+ else
387
+ begin
388
+ Mant_b_norm_DP<=Mant_b_norm_DN;
389
+ end
390
+ end
391
+
392
+ logic [C_EXP_FP64:0] Exp_b_norm_DN,Exp_b_norm_DP;
393
+ assign Exp_b_norm_DN = ((Start_S&&Ready_SI))?(Exp_b_D-Mant_leadingOne_b+(|Mant_leadingOne_b)):Exp_b_norm_DP; //Covering the process of denormal numbers
394
+
395
+ always_ff @(posedge Clk_CI, negedge Rst_RBI)
396
+ begin
397
+ if(~Rst_RBI)
398
+ begin
399
+ Exp_b_norm_DP <= '0;
400
+ end
401
+ else
402
+ begin
403
+ Exp_b_norm_DP<=Exp_b_norm_DN;
404
+ end
405
+ end
406
+
407
+ /////////////////////////////////////////////////////////////////////////////
408
+ // Output assignments //
409
+ /////////////////////////////////////////////////////////////////////////////
410
+
411
+ assign Start_SO=Start_S;
412
+ assign Exp_a_DO_norm=Exp_a_norm_DP;
413
+ assign Exp_b_DO_norm=Exp_b_norm_DP;
414
+ assign Mant_a_DO_norm=Mant_a_norm_DP;
415
+ assign Mant_b_DO_norm=Mant_b_norm_DP;
416
+ assign Sign_z_DO=Sign_z_DP;
417
+ assign Inf_a_SO=Inf_a_SP;
418
+ assign Inf_b_SO=Inf_b_SP;
419
+ assign Zero_a_SO=Zero_a_SP;
420
+ assign Zero_b_SO=Zero_b_SP;
421
+ assign NaN_a_SO=NaN_a_SP;
422
+ assign NaN_b_SO=NaN_b_SP;
423
+ assign SNaN_SO=SNaN_SP;
424
+
425
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_compressed_if_driver.sv ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 Thales DIS France SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Guillaume Chauvon
9
+
10
+ module cvxif_compressed_if_driver #(
11
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
12
+ parameter type x_compressed_req_t = logic,
13
+ parameter type x_compressed_resp_t = logic
14
+ ) (
15
+ // Subsystem Clock - SUBSYSTEM
16
+ input logic clk_i,
17
+ // Asynchronous reset active low - SUBSYSTEM
18
+ input logic rst_ni,
19
+ input logic flush_i,
20
+ // CVA6 Hart id
21
+ input logic [CVA6Cfg.XLEN-1:0] hart_id_i,
22
+
23
+ input logic is_compressed_i,
24
+ input logic is_illegal_i,
25
+ input logic [31:0] instruction_i,
26
+
27
+ output logic is_compressed_o,
28
+ output logic is_illegal_o,
29
+ output logic [31:0] instruction_o,
30
+ input logic stall_i,
31
+ output logic stall_o,
32
+ // CVXIF Compressed interface
33
+ input logic compressed_ready_i,
34
+ input x_compressed_resp_t compressed_resp_i,
35
+ output logic compressed_valid_o,
36
+ output x_compressed_req_t compressed_req_o
37
+ );
38
+
39
+
40
+ always_comb begin
41
+ is_illegal_o = is_illegal_i;
42
+ instruction_o = instruction_i;
43
+ is_compressed_o = is_compressed_i;
44
+ compressed_valid_o = 1'b0;
45
+ compressed_req_o.instr = '0;
46
+ compressed_req_o.hartid = hart_id_i;
47
+ stall_o = stall_i;
48
+ if (is_illegal_i) begin
49
+ compressed_valid_o = is_illegal_i;
50
+ compressed_req_o.instr = instruction_i[15:0];
51
+ is_illegal_o = ~compressed_resp_i.accept;
52
+ instruction_o = compressed_resp_i.accept ? compressed_resp_i.instr : instruction_i;
53
+ is_compressed_o = compressed_resp_i.accept ? 1'b0 : is_compressed_i;
54
+ if (~stall_i) begin
55
+ // Propagate stall from macro decoder or wait for compressed ready if compressed transaction is happening.
56
+ stall_o = (compressed_valid_o && ~compressed_ready_i);
57
+ end
58
+ end
59
+ if (flush_i) begin
60
+ compressed_valid_o = 1'b0;
61
+ compressed_req_o.instr = '0;
62
+ compressed_req_o.hartid = hart_id_i;
63
+ end
64
+ end
65
+
66
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/compressed_instr_decoder.sv ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 Thales DIS France SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Guillaume Chauvon
9
+
10
+ module compressed_instr_decoder #(
11
+ parameter type copro_compressed_resp_t = logic,
12
+ parameter int NbInstr = 1,
13
+ parameter copro_compressed_resp_t CoproInstr [NbInstr] = {0},
14
+ parameter type x_compressed_req_t = logic,
15
+ parameter type x_compressed_resp_t = logic
16
+ ) (
17
+ input logic clk_i,
18
+ input logic rst_ni,
19
+ input logic compressed_valid_i,
20
+ input x_compressed_req_t compressed_req_i,
21
+ output logic compressed_ready_o,
22
+ output x_compressed_resp_t compressed_resp_o
23
+ );
24
+
25
+ logic [NbInstr-1:0] sel;
26
+
27
+ for (genvar i = 0; i < NbInstr; i++) begin : gen_predecoder_selector
28
+ assign sel[i] = ((CoproInstr[i].mask & compressed_req_i.instr) == CoproInstr[i].instr);
29
+ end
30
+
31
+ always_comb begin
32
+ compressed_ready_o = '1;
33
+ compressed_resp_o.accept = '0;
34
+ compressed_resp_o.instr = '0;
35
+ for (int unsigned i = 0; i < NbInstr; i++) begin
36
+ if (sel[i] && compressed_valid_i) begin
37
+ compressed_resp_o.accept = CoproInstr[i].resp.accept;
38
+ compressed_resp_o.instr = CoproInstr[i].resp.instr;
39
+ // Remap rs1 and rs2
40
+ compressed_resp_o.instr[19:15] = compressed_req_i.instr[11:7];
41
+ compressed_resp_o.instr[24:20] = compressed_req_i.instr[6:2];
42
+ end
43
+ end
44
+ end
45
+
46
+ assert property (@(posedge clk_i) $onehot0(sel))
47
+ else $warning("This offloaded instruction is valid for multiple coprocessor instructions !");
48
+
49
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/copro_alu.sv ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 Thales DIS France SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Guillaume Chauvon
9
+
10
+ module copro_alu
11
+ import cvxif_instr_pkg::*;
12
+ #(
13
+ parameter int unsigned NrRgprPorts = 2,
14
+ parameter int unsigned XLEN = 32,
15
+ parameter type hartid_t = logic,
16
+ parameter type id_t = logic,
17
+ parameter type registers_t = logic
18
+
19
+ ) (
20
+ input logic clk_i,
21
+ input logic rst_ni,
22
+ input registers_t registers_i,
23
+ input opcode_t opcode_i,
24
+ input hartid_t hartid_i,
25
+ input id_t id_i,
26
+ input logic [ 4:0] rd_i,
27
+ output logic [XLEN-1:0] result_o,
28
+ output hartid_t hartid_o,
29
+ output id_t id_o,
30
+ output logic [ 4:0] rd_o,
31
+ output logic valid_o,
32
+ output logic we_o
33
+ );
34
+
35
+ logic [XLEN-1:0] result_n, result_q;
36
+ hartid_t hartid_n, hartid_q;
37
+ id_t id_n, id_q;
38
+ logic valid_n, valid_q;
39
+ logic [4:0] rd_n, rd_q;
40
+ logic we_n, we_q;
41
+
42
+ assign result_o = result_q;
43
+ assign hartid_o = hartid_q;
44
+ assign id_o = id_q;
45
+ assign valid_o = valid_q;
46
+ assign rd_o = rd_q;
47
+ assign we_o = we_q;
48
+
49
+ always_comb begin
50
+ case (opcode_i)
51
+ cvxif_instr_pkg::NOP: begin
52
+ result_n = '0;
53
+ hartid_n = hartid_i;
54
+ id_n = id_i;
55
+ valid_n = 1'b1;
56
+ rd_n = '0;
57
+ we_n = '0;
58
+ end
59
+ cvxif_instr_pkg::ADD: begin
60
+ result_n = registers_i[1] + registers_i[0];
61
+ hartid_n = hartid_i;
62
+ id_n = id_i;
63
+ valid_n = 1'b1;
64
+ rd_n = rd_i;
65
+ we_n = 1'b1;
66
+ end
67
+ cvxif_instr_pkg::DOUBLE_RS1: begin
68
+ result_n = registers_i[0] + registers_i[0];
69
+ hartid_n = hartid_i;
70
+ id_n = id_i;
71
+ valid_n = 1'b1;
72
+ rd_n = rd_i;
73
+ we_n = 1'b1;
74
+ end
75
+ cvxif_instr_pkg::DOUBLE_RS2: begin
76
+ result_n = registers_i[1] + registers_i[1];
77
+ hartid_n = hartid_i;
78
+ id_n = id_i;
79
+ valid_n = 1'b1;
80
+ rd_n = rd_i;
81
+ we_n = 1'b1;
82
+ end
83
+ cvxif_instr_pkg::ADD_MULTI: begin
84
+ result_n = registers_i[1] + registers_i[0];
85
+ hartid_n = hartid_i;
86
+ id_n = id_i;
87
+ valid_n = 1'b1;
88
+ rd_n = rd_i;
89
+ we_n = 1'b1;
90
+ end
91
+ cvxif_instr_pkg::MADD_RS3_R4: begin
92
+ result_n = NrRgprPorts == 3 ? (registers_i[0] + registers_i[1] + registers_i[2]) : (registers_i[0] + registers_i[1]);
93
+ hartid_n = hartid_i;
94
+ id_n = id_i;
95
+ valid_n = 1'b1;
96
+ rd_n = rd_i;
97
+ we_n = 1'b1;
98
+ end
99
+ cvxif_instr_pkg::MSUB_RS3_R4: begin
100
+ result_n = NrRgprPorts == 3 ? (registers_i[0] - registers_i[1] - registers_i[2]) : (registers_i[0] - registers_i[1]);
101
+ hartid_n = hartid_i;
102
+ id_n = id_i;
103
+ valid_n = 1'b1;
104
+ rd_n = rd_i;
105
+ we_n = 1'b1;
106
+ end
107
+ cvxif_instr_pkg::NMADD_RS3_R4: begin
108
+ result_n = NrRgprPorts == 3 ? ~(registers_i[0] + registers_i[1] + registers_i[2]) : ~(registers_i[0] + registers_i[1]);
109
+ hartid_n = hartid_i;
110
+ id_n = id_i;
111
+ valid_n = 1'b1;
112
+ rd_n = rd_i;
113
+ we_n = 1'b1;
114
+ end
115
+ cvxif_instr_pkg::NMSUB_RS3_R4: begin
116
+ result_n = NrRgprPorts == 3 ? ~(registers_i[0] - registers_i[1] - registers_i[2]) : ~(registers_i[0] - registers_i[1]);
117
+ hartid_n = hartid_i;
118
+ id_n = id_i;
119
+ valid_n = 1'b1;
120
+ rd_n = rd_i;
121
+ we_n = 1'b1;
122
+ end
123
+ cvxif_instr_pkg::ADD_RS3_R: begin
124
+ result_n = NrRgprPorts == 3 ? registers_i[2] + registers_i[1] + registers_i[0] : registers_i[1] + registers_i[0];
125
+ hartid_n = hartid_i;
126
+ id_n = id_i;
127
+ valid_n = 1'b1;
128
+ rd_n = 5'b01010;
129
+ we_n = 1'b1;
130
+ end
131
+ default: begin
132
+ result_n = '0;
133
+ hartid_n = '0;
134
+ id_n = '0;
135
+ valid_n = '0;
136
+ rd_n = '0;
137
+ we_n = '0;
138
+ end
139
+ endcase
140
+ end
141
+
142
+ always_ff @(posedge clk_i, negedge rst_ni) begin
143
+ if (~rst_ni) begin
144
+ result_q <= '0;
145
+ hartid_q <= '0;
146
+ id_q <= '0;
147
+ valid_q <= '0;
148
+ rd_q <= '0;
149
+ we_q <= '0;
150
+ end else begin
151
+ result_q <= result_n;
152
+ hartid_q <= hartid_n;
153
+ id_q <= id_n;
154
+ valid_q <= valid_n;
155
+ rd_q <= rd_n;
156
+ we_q <= we_n;
157
+ end
158
+ end
159
+
160
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/include/cvxif_instr_pkg.sv ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2021 Thales DIS design services SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Guillaume Chauvon (guillaume.chauvon@thalesgroup.com)
9
+
10
+
11
+
12
+ package cvxif_instr_pkg;
13
+
14
+ typedef enum logic [3:0] {
15
+ ILLEGAL = 4'b0000,
16
+ NOP = 4'b0001,
17
+ ADD = 4'b0010,
18
+ DOUBLE_RS1 = 4'b0011,
19
+ DOUBLE_RS2 = 4'b0100,
20
+ ADD_MULTI = 4'b0101,
21
+ MADD_RS3_R4 = 4'b0110,
22
+ MSUB_RS3_R4 = 4'b0111,
23
+ NMADD_RS3_R4 = 4'b1000,
24
+ NMSUB_RS3_R4 = 4'b1001,
25
+ ADD_RS3_R = 4'b1111
26
+ } opcode_t;
27
+
28
+
29
+ typedef struct packed {
30
+ logic accept;
31
+ logic writeback; // TODO depends on dualwrite
32
+ logic [2:0] register_read; // TODO Nr read ports
33
+ } issue_resp_t;
34
+
35
+ typedef struct packed {
36
+ logic accept;
37
+ logic [31:0] instr;
38
+ } compressed_resp_t;
39
+
40
+ typedef struct packed {
41
+ logic [31:0] instr;
42
+ logic [31:0] mask;
43
+ issue_resp_t resp;
44
+ opcode_t opcode;
45
+ } copro_issue_resp_t;
46
+
47
+
48
+ typedef struct packed {
49
+ logic [15:0] instr;
50
+ logic [15:0] mask;
51
+ compressed_resp_t resp;
52
+ } copro_compressed_resp_t;
53
+
54
+ // 4 Possible RISCV instructions for Coprocessor
55
+ parameter int unsigned NbInstr = 10;
56
+ parameter copro_issue_resp_t CoproInstr[NbInstr] = '{
57
+ '{
58
+ // Custom Nop
59
+ instr:
60
+ 32'b00000_00_00000_00000_0_00_00000_1111011, // custom3 opcode
61
+ mask: 32'b11111_11_00000_00000_1_11_00000_1111111,
62
+ resp : '{accept : 1'b1, writeback : 1'b0, register_read : {1'b0, 1'b0, 1'b0}},
63
+ opcode : NOP
64
+ },
65
+ '{
66
+ // Custom Add : cus_add rd, rs1, rs2
67
+ instr:
68
+ 32'b00000_00_00000_00000_0_01_00000_1111011, // custom3 opcode
69
+ mask: 32'b11111_11_00000_00000_1_11_00000_1111111,
70
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b0, 1'b1, 1'b1}},
71
+ opcode : ADD
72
+ },
73
+ '{
74
+ // Custom Add rs1 : cus_add rd, rs1, rs1
75
+ instr:
76
+ 32'b00000_01_00000_00000_0_01_00000_1111011, // custom3 opcode
77
+ mask: 32'b11111_11_00000_00000_1_11_00000_1111111,
78
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b0, 1'b0, 1'b1}},
79
+ opcode : DOUBLE_RS1
80
+ },
81
+ '{
82
+ // Custom Add rs2 : cus_add rd, rs2, rs2
83
+ instr:
84
+ 32'b00000_10_00000_00000_0_01_00000_1111011, // custom3 opcode
85
+ mask: 32'b11111_11_00000_00000_1_11_00000_1111111,
86
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b0, 1'b1, 1'b0}},
87
+ opcode : DOUBLE_RS2
88
+ },
89
+ '{
90
+ // Custom Add Multi rs1 : cus_add rd, rs1, rs1
91
+ instr:
92
+ 32'b00000_11_00000_00000_0_01_00000_1111011, // custom3 opcode
93
+ mask: 32'b11111_11_00000_00000_1_11_00000_1111111,
94
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b0, 1'b1, 1'b1}},
95
+ opcode : ADD_MULTI
96
+ },
97
+ '{
98
+ // Custom Add Multi rs1 : cus_add rd, rs1, rs1
99
+ instr:
100
+ 32'b00001_00_00000_00000_0_01_00000_1111011, // custom3 opcode
101
+ mask: 32'b11111_11_00000_00000_1_11_00000_1111111,
102
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b1, 1'b1, 1'b1}},
103
+ opcode : ADD_RS3_R
104
+ },
105
+ '{
106
+ // Custom Add Multi rs1 : cus_add rd, rs1, rs1
107
+ instr:
108
+ 32'b00000_00_00000_00000_0_00_00000_1000011, // MADD opcode
109
+ mask: 32'b00000_11_00000_00000_1_11_00000_1111111,
110
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b1, 1'b1, 1'b1}},
111
+ opcode : MADD_RS3_R4
112
+ },
113
+ '{
114
+ // Custom Add Multi rs1 : cus_add rd, rs1, rs1
115
+ instr:
116
+ 32'b00000_00_00000_00000_0_00_00000_1000111, // MSUB opcode
117
+ mask: 32'b00000_11_00000_00000_1_11_00000_1111111,
118
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b1, 1'b1, 1'b1}},
119
+ opcode : MSUB_RS3_R4
120
+ },
121
+ '{
122
+ // Custom Add Multi rs1 : cus_add rd, rs1, rs1
123
+ instr:
124
+ 32'b00000_00_00000_00000_0_00_00000_1001011, // NMSUB opcode
125
+ mask: 32'b00000_11_00000_00000_1_11_00000_1111111,
126
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b1, 1'b1, 1'b1}},
127
+ opcode : NMSUB_RS3_R4
128
+ },
129
+ '{
130
+ // Custom Add Multi rs1 : cus_add rd, rs1, rs1
131
+ instr:
132
+ 32'b00000_00_00000_00000_0_00_00000_1001111, // NMADD opcode
133
+ mask: 32'b00000_11_00000_00000_1_11_00000_1111111,
134
+ resp : '{accept : 1'b1, writeback : 1'b1, register_read : {1'b1, 1'b1, 1'b1}},
135
+ opcode : NMADD_RS3_R4
136
+ }
137
+ };
138
+
139
+ parameter int unsigned NbCompInstr = 2;
140
+ parameter copro_compressed_resp_t CoproCompInstr[NbCompInstr] = '{
141
+ // C_NOP
142
+ '{
143
+ instr : 16'b111_0_00000_00000_00,
144
+ mask : 16'b111_1_00000_00000_11,
145
+ resp : '{accept : 1'b1, instr : 32'b00000_00_00000_00000_0_00_00000_1111011}
146
+ },
147
+ '{
148
+ instr : 16'b111_1_00000_00000_00,
149
+ mask : 16'b111_1_00000_00000_11,
150
+ resp : '{accept : 1'b1, instr : 32'b00000_00_00000_00000_0_01_01010_1111011}
151
+ }
152
+ };
153
+
154
+ endpackage
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_example/instr_decoder.sv ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 Thales DIS France SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Guillaume Chauvon
9
+
10
+ module instr_decoder #(
11
+ parameter type copro_issue_resp_t = logic,
12
+ parameter type opcode_t = logic,
13
+ parameter int NbInstr = 1,
14
+ parameter copro_issue_resp_t CoproInstr [NbInstr] = {0},
15
+ parameter int unsigned NrRgprPorts = 2,
16
+ parameter type hartid_t = logic,
17
+ parameter type id_t = logic,
18
+ parameter type x_issue_req_t = logic,
19
+ parameter type x_issue_resp_t = logic,
20
+ parameter type x_register_t = logic,
21
+ parameter type registers_t = logic
22
+ ) (
23
+ input logic clk_i,
24
+ input logic rst_ni,
25
+ input logic issue_valid_i,
26
+ input x_issue_req_t issue_req_i,
27
+ output logic issue_ready_o,
28
+ output x_issue_resp_t issue_resp_o,
29
+ input logic register_valid_i,
30
+ input x_register_t register_i,
31
+ output registers_t registers_o,
32
+ output opcode_t opcode_o,
33
+ output hartid_t hartid_o,
34
+ output id_t id_o,
35
+ output logic [4:0] rd_o
36
+ );
37
+
38
+ logic [NbInstr-1:0] sel;
39
+ logic rs1_ready;
40
+ logic rs2_ready;
41
+ logic rs3_ready;
42
+
43
+ for (genvar i = 0; i < NbInstr; i++) begin : gen_predecoder_selector
44
+ assign sel[i] = ((CoproInstr[i].mask & issue_req_i.instr) == CoproInstr[i].instr);
45
+ end
46
+
47
+ always_comb begin
48
+ rs1_ready = '0;
49
+ rs2_ready = '0;
50
+ rs3_ready = '0;
51
+ issue_ready_o = '0;
52
+ issue_resp_o.accept = '0;
53
+ issue_resp_o.writeback = '0;
54
+ issue_resp_o.register_read = '0;
55
+ registers_o = '0;
56
+ opcode_o = opcode_t'(0); // == ILLEGAL see cvxif_instr_pkg.sv
57
+ hartid_o = '0;
58
+ id_o = '0;
59
+ rd_o = '0;
60
+ for (int unsigned i = 0; i < NbInstr; i++) begin
61
+ if (sel[i] && issue_valid_i) begin
62
+ issue_resp_o.accept = CoproInstr[i].resp.accept;
63
+ issue_resp_o.writeback = CoproInstr[i].resp.writeback;
64
+ issue_resp_o.register_read = CoproInstr[i].resp.register_read; // Warning : potential 3 bits vector into 2 bits one
65
+ if (issue_resp_o.accept) begin
66
+ rs1_ready = (~CoproInstr[i].resp.register_read[0] || register_i.rs_valid[0]);
67
+ rs2_ready = (~CoproInstr[i].resp.register_read[1] || register_i.rs_valid[1]);
68
+ rs3_ready = NrRgprPorts == 3 ? (~CoproInstr[i].resp.register_read[2] || register_i.rs_valid[2]) : 1'b1;
69
+ issue_ready_o = rs1_ready && rs2_ready && rs3_ready;
70
+ end
71
+ opcode_o = CoproInstr[i].opcode;
72
+ id_o = issue_req_i.id;
73
+ hartid_o = issue_req_i.hartid;
74
+ rd_o = issue_req_i.instr[11:7];
75
+ for (int unsigned j = 0; j < NrRgprPorts; j++) begin
76
+ registers_o[j] = issue_resp_o.register_read[j] ? register_i.rs[j] : '0;
77
+ end
78
+ end
79
+ end
80
+ // Coprocessor could not decode offloaded instruction -> instruction is not accepted
81
+ if (issue_valid_i && ~(|sel)) begin
82
+ issue_ready_o = 1'b1;
83
+ end
84
+ end
85
+
86
+ assert property (@(posedge clk_i) $onehot0(sel))
87
+ else $warning("This offloaded instruction is valid for multiple coprocessor instructions !");
88
+
89
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_fu.sv ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 Thales DIS France SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Guillaume Chauvon
9
+
10
+ // Functional Unit for the CoreV-X-Interface
11
+ // Handles Result interface and exception forwarding to next stages.
12
+
13
+
14
+ module cvxif_fu
15
+ import ariane_pkg::*;
16
+ #(
17
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
18
+ parameter type exception_t = logic,
19
+ parameter type x_result_t = logic
20
+ ) (
21
+ // Subsystem Clock - SUBSYSTEM
22
+ input logic clk_i,
23
+ // Asynchronous reset active low - SUBSYSTEM
24
+ input logic rst_ni,
25
+ // CVXIF instruction is valid - ISSUE_STAGE
26
+ input logic x_valid_i,
27
+ // Transaction ID - ISSUE_STAGE
28
+ input logic [CVA6Cfg.TRANS_ID_BITS-1:0] x_trans_id_i,
29
+ // Instruction is illegal, determined during CVXIF issue transaction - ISSUE_STAGE
30
+ input logic x_illegal_i,
31
+ // Offloaded instruction - ISSUE_STAGE
32
+ input logic [ 31:0] x_off_instr_i,
33
+ // CVXIF is ready - ISSUE_STAGE
34
+ output logic x_ready_o,
35
+ // CVXIF result transaction ID - ISSUE_STAGE
36
+ output logic [CVA6Cfg.TRANS_ID_BITS-1:0] x_trans_id_o,
37
+ // CVXIF exception - ISSUE_STAGE
38
+ output exception_t x_exception_o,
39
+ // CVXIF FU result - ISSUE_STAGE
40
+ output logic [ CVA6Cfg.XLEN-1:0] x_result_o,
41
+ // CVXIF result valid - ISSUE_STAGE
42
+ output logic x_valid_o,
43
+ // CVXIF write enable - ISSUE_STAGE
44
+ output logic x_we_o,
45
+ // CVXIF destination register - ISSUE_STAGE
46
+ output logic [ 4:0] x_rd_o,
47
+ // CVXIF result interface
48
+ input logic result_valid_i,
49
+ input x_result_t result_i,
50
+ output logic result_ready_o
51
+ );
52
+
53
+
54
+
55
+ assign result_ready_o = 1'b1;
56
+
57
+ assign x_ready_o = 1'b1; // Readyness of cvxif_fu is determined in issue stage by CVXIF issue interface
58
+ // Result signals
59
+ assign x_valid_o = x_illegal_i || result_valid_i;
60
+ assign x_result_o = result_i.data;
61
+ assign x_trans_id_o = x_illegal_i ? x_trans_id_i : result_i.id;
62
+ assign x_we_o = result_i.we;
63
+ assign x_rd_o = result_i.rd;
64
+
65
+ // Handling of illegal instruction exception
66
+ always_comb begin
67
+ x_exception_o = '0;
68
+ x_exception_o.valid = x_illegal_i;
69
+ x_exception_o.cause = x_illegal_i ? riscv::ILLEGAL_INSTR : '0;
70
+ if (CVA6Cfg.TvalEn)
71
+ x_exception_o.tval = x_off_instr_i; // TODO Optimization : Set exception in IRO.
72
+ end
73
+
74
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/cvxif_issue_register_commit_if_driver.sv ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 Thales DIS France SAS
2
+ //
3
+ // Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH SHL-2.0
6
+ // You may obtain a copy of the License at https://solderpad.org/licenses/
7
+ //
8
+ // Original Author: Guillaume Chauvon
9
+
10
+ module cvxif_issue_register_commit_if_driver #(
11
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
12
+ parameter type x_issue_req_t = logic,
13
+ parameter type x_issue_resp_t = logic,
14
+ parameter type x_register_t = logic,
15
+ parameter type x_commit_t = logic
16
+ ) (
17
+ // CVA6 inputs
18
+ input logic clk_i,
19
+ input logic rst_ni,
20
+ input logic flush_i,
21
+ input logic [CVA6Cfg.XLEN-1:0] hart_id_i,
22
+ // CVXIF Issue interface
23
+ input logic issue_ready_i,
24
+ input x_issue_resp_t issue_resp_i,
25
+ output logic issue_valid_o,
26
+ output x_issue_req_t issue_req_o,
27
+ // CVXIF Register interface
28
+ input logic register_ready_i,
29
+ output logic register_valid_o,
30
+ output x_register_t register_o,
31
+ // CVXIF Commit interface
32
+ output logic commit_valid_o,
33
+ output x_commit_t commit_o,
34
+ // IRO in/out
35
+ input logic valid_i,
36
+ input logic [31:0] x_off_instr_i,
37
+ input logic [CVA6Cfg.TRANS_ID_BITS-1:0] x_trans_id_i,
38
+ input [(CVA6Cfg.NrRgprPorts/CVA6Cfg.NrIssuePorts)-1:0][CVA6Cfg.XLEN-1:0] register_i,
39
+ input logic [(CVA6Cfg.NrRgprPorts/CVA6Cfg.NrIssuePorts)-1:0] rs_valid_i
40
+ );
41
+ // X_ISSUE_REGISTER_SPLIT = 0 : Issue and register transactions are synchrone
42
+ // Mandatory assignement
43
+ assign register_valid_o = issue_valid_o;
44
+ assign register_o.hartid = issue_req_o.hartid;
45
+ assign register_o.id = issue_req_o.id;
46
+
47
+ always_comb begin
48
+ issue_valid_o = valid_i && ~flush_i;
49
+ issue_req_o.instr = x_off_instr_i;
50
+ issue_req_o.hartid = hart_id_i;
51
+ issue_req_o.id = x_trans_id_i;
52
+ register_o.rs = register_i;
53
+ register_o.rs_valid = rs_valid_i;
54
+ end
55
+
56
+ /* WARNING */
57
+ // Always commit since speculation in execute in not possible : TODO to be verified
58
+
59
+ // Always do commit transaction with issue
60
+ // If instruction goes to execute then it is not speculative
61
+ assign commit_valid_o = issue_valid_o && issue_ready_i;
62
+ assign commit_o.hartid = issue_req_o.hartid;
63
+ assign commit_o.id = issue_req_o.id;
64
+ assign commit_o.commit_kill = 1'b0;
65
+
66
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/decoder.sv ADDED
@@ -0,0 +1,1722 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // File: issue_read_operands.sv
12
+ // Author: Florian Zaruba <zarubaf@ethz.ch>
13
+ // Date: 8.4.2017
14
+ //
15
+ // Copyright (C) 2017 ETH Zurich, University of Bologna
16
+ // All rights reserved.
17
+ //
18
+ // Description: Issues instruction from the scoreboard and fetches the operands
19
+ // This also includes all the forwarding logic
20
+ //
21
+
22
+ module decoder
23
+ import ariane_pkg::*;
24
+ #(
25
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
26
+ parameter type branchpredict_sbe_t = logic,
27
+ parameter type exception_t = logic,
28
+ parameter type irq_ctrl_t = logic,
29
+ parameter type scoreboard_entry_t = logic,
30
+ parameter type interrupts_t = logic,
31
+ parameter interrupts_t INTERRUPTS = '0
32
+ ) (
33
+ // Debug (async) request - SUBSYSTEM
34
+ input logic debug_req_i,
35
+ // PC from fetch stage - FRONTEND
36
+ input logic [CVA6Cfg.VLEN-1:0] pc_i,
37
+ // Is a compressed instruction - compressed_decoder
38
+ input logic is_compressed_i,
39
+ // Compressed form of instruction - FRONTEND
40
+ input logic [15:0] compressed_instr_i,
41
+ // Illegal compressed instruction - compressed_decoder
42
+ input logic is_illegal_i,
43
+ // Instruction from fetch stage - FRONTEND
44
+ input logic [31:0] instruction_i,
45
+ // Is a macro instruction - macro_decoder
46
+ input logic is_macro_instr_i,
47
+ // Is a last macro instruction - macro_decoder
48
+ input logic is_last_macro_instr_i,
49
+ // Is mvsa01/mva01s macro instruction - macro_decoder
50
+ input logic is_double_rd_macro_instr_i,
51
+ // Zcmt instruction - FRONTEND
52
+ input logic is_zcmt_i,
53
+ // Jump address - zcmt_decoder
54
+ input logic [CVA6Cfg.XLEN-1:0] jump_address_i,
55
+ // Is a branch predict instruction - FRONTEND
56
+ input branchpredict_sbe_t branch_predict_i,
57
+ // If an exception occured in fetch stage - FRONTEND
58
+ input exception_t ex_i,
59
+ // Level sensitive (async) interrupts - SUBSYSTEM
60
+ input logic [1:0] irq_i,
61
+ // Interrupt control status - CSR_REGFILE
62
+ input irq_ctrl_t irq_ctrl_i,
63
+ // Current privilege level - CSR_REGFILE
64
+ input riscv::priv_lvl_t priv_lvl_i,
65
+ // Current virtualization mode - CSR_REGFILE
66
+ input logic v_i,
67
+ // Is debug mode - CSR_REGFILE
68
+ input logic debug_mode_i,
69
+ // Floating point extension status - CSR_REGFILE
70
+ input riscv::xs_t fs_i,
71
+ // Virtual floating point extension status - CSR_REGFILE
72
+ input riscv::xs_t vfs_i,
73
+ // Floating-point dynamic rounding mode - CSR_REGFILE
74
+ input logic [2:0] frm_i,
75
+ // Vector extension status - CSR_REGFILE
76
+ input riscv::xs_t vs_i,
77
+ // Trap virtual memory - CSR_REGFILE
78
+ input logic tvm_i,
79
+ // Timeout wait - CSR_REGFILE
80
+ input logic tw_i,
81
+ // Virtual timeout wait - CSR_REGFILE
82
+ input logic vtw_i,
83
+ // Trap sret - CSR_REGFILE
84
+ input logic tsr_i,
85
+ // Hypervisor user mode - CSR_REGFILE
86
+ input logic hu_i,
87
+ // Instruction to be added to scoreboard entry - ISSUE_STAGE
88
+ output scoreboard_entry_t instruction_o,
89
+ // Instruction - ISSUE_STAGE
90
+ output logic [31:0] orig_instr_o,
91
+ // Is a control flow instruction - ISSUE_STAGE
92
+ output logic is_control_flow_instr_o
93
+ );
94
+ logic illegal_instr;
95
+ logic illegal_instr_bm;
96
+ logic illegal_instr_zic;
97
+ logic illegal_instr_non_bm;
98
+ logic virtual_illegal_instr;
99
+ // this instruction is an environment call (ecall), it is handled like an exception
100
+ logic ecall;
101
+ // this instruction is a software break-point
102
+ logic ebreak;
103
+ // this instruction needs floating-point rounding-mode verification
104
+ logic check_fprm;
105
+ riscv::instruction_t instr;
106
+ assign instr = riscv::instruction_t'(instruction_i);
107
+ // transformed instruction
108
+ logic [31:0] tinst;
109
+ // --------------------
110
+ // Immediate select
111
+ // --------------------
112
+ enum logic [3:0] {
113
+ NOIMM,
114
+ IIMM,
115
+ SIMM,
116
+ SBIMM,
117
+ UIMM,
118
+ JIMM,
119
+ RS3,
120
+ MUX_RD_RS3
121
+ } imm_select;
122
+
123
+ logic [CVA6Cfg.XLEN-1:0] imm_i_type;
124
+ logic [CVA6Cfg.XLEN-1:0] imm_s_type;
125
+ logic [CVA6Cfg.XLEN-1:0] imm_sb_type;
126
+ logic [CVA6Cfg.XLEN-1:0] imm_u_type;
127
+ logic [CVA6Cfg.XLEN-1:0] imm_uj_type;
128
+
129
+ // ---------------------------------------
130
+ // Accelerator instructions' first-pass decoder
131
+ // ---------------------------------------
132
+ logic is_accel;
133
+ scoreboard_entry_t acc_instruction;
134
+ logic acc_illegal_instr;
135
+ logic acc_is_control_flow_instr;
136
+
137
+ if (CVA6Cfg.EnableAccelerator) begin : gen_accel_decoder
138
+ // This module is responsible for a light-weight decoding of accelerator instructions,
139
+ // identifying them, but also whether they read/write scalar registers.
140
+ // Accelerators are supposed to define this module.
141
+ cva6_accel_first_pass_decoder #(
142
+ .CVA6Cfg(CVA6Cfg),
143
+ .scoreboard_entry_t(scoreboard_entry_t)
144
+ ) i_accel_decoder (
145
+ .instruction_i(instruction_i),
146
+ .fs_i(fs_i),
147
+ .vs_i(vs_i),
148
+ .is_accel_o(is_accel),
149
+ .instruction_o(acc_instruction),
150
+ .illegal_instr_o(acc_illegal_instr),
151
+ .is_control_flow_instr_o(acc_is_control_flow_instr)
152
+ );
153
+ end : gen_accel_decoder
154
+ else begin
155
+ assign is_accel = 1'b0;
156
+ assign acc_instruction = '0;
157
+ assign acc_illegal_instr = 1'b1; // this should never propagate
158
+ assign acc_is_control_flow_instr = 1'b0;
159
+ end
160
+
161
+ always_comb begin : decoder
162
+
163
+ imm_select = NOIMM;
164
+ is_control_flow_instr_o = 1'b0;
165
+ illegal_instr = 1'b0;
166
+ illegal_instr_non_bm = 1'b0;
167
+ illegal_instr_bm = 1'b0;
168
+ illegal_instr_zic = 1'b0;
169
+ virtual_illegal_instr = 1'b0;
170
+ instruction_o.pc = pc_i;
171
+ instruction_o.trans_id = '0;
172
+ instruction_o.fu = NONE;
173
+ instruction_o.op = ariane_pkg::ADD;
174
+ instruction_o.rs1 = '0;
175
+ instruction_o.rs2 = '0;
176
+ instruction_o.rd = '0;
177
+ instruction_o.use_pc = 1'b0;
178
+ instruction_o.is_compressed = is_compressed_i;
179
+ instruction_o.is_macro_instr = is_macro_instr_i;
180
+ instruction_o.is_last_macro_instr = is_last_macro_instr_i;
181
+ instruction_o.is_double_rd_macro_instr = is_double_rd_macro_instr_i;
182
+ instruction_o.use_zimm = 1'b0;
183
+ instruction_o.bp = branch_predict_i;
184
+ instruction_o.vfp = 1'b0;
185
+ instruction_o.is_zcmt = is_zcmt_i;
186
+ ecall = 1'b0;
187
+ ebreak = 1'b0;
188
+ check_fprm = 1'b0;
189
+
190
+ if (~ex_i.valid) begin
191
+ case (instr.rtype.opcode)
192
+ riscv::OpcodeSystem: begin
193
+ instruction_o.fu = CSR;
194
+ instruction_o.rs1 = instr.itype.rs1;
195
+ instruction_o.rs2 = instr.rtype.rs2; //TODO: needs to be checked if better way is available
196
+ instruction_o.rd = instr.itype.rd;
197
+
198
+ unique case (instr.itype.funct3)
199
+ 3'b000: begin
200
+ // check if the RD and and RS1 fields are zero, this may be reset for the SENCE.VMA instruction
201
+ if (instr.itype.rs1 != '0 || instr.itype.rd != '0) begin
202
+ if (CVA6Cfg.RVH && v_i) begin
203
+ virtual_illegal_instr = 1'b1;
204
+ end else begin
205
+ illegal_instr = 1'b1;
206
+ end
207
+ end
208
+ // decode the immiediate field
209
+ case (instr.itype.imm)
210
+ // ECALL -> inject exception
211
+ 12'b0: ecall = 1'b1;
212
+ // EBREAK -> inject exception
213
+ 12'b1: ebreak = 1'b1;
214
+ // SRET
215
+ 12'b1_0000_0010: begin
216
+ if (CVA6Cfg.RVS) begin
217
+ instruction_o.op = ariane_pkg::SRET;
218
+ // check privilege level, SRET can only be executed in S and M mode
219
+ // we'll just decode an illegal instruction if we are in the wrong privilege level
220
+ if (CVA6Cfg.RVU && priv_lvl_i == riscv::PRIV_LVL_U) begin
221
+ if (CVA6Cfg.RVH && v_i) begin
222
+ virtual_illegal_instr = 1'b1;
223
+ end else begin
224
+ illegal_instr = 1'b1;
225
+ end
226
+ // do not change privilege level if this is an illegal instruction
227
+ instruction_o.op = ariane_pkg::ADD;
228
+ end
229
+ // if we are in S-Mode and Trap SRET (tsr) is set -> trap on illegal instruction
230
+ if (priv_lvl_i == riscv::PRIV_LVL_S && tsr_i) begin
231
+ if (CVA6Cfg.RVH && v_i) begin
232
+ virtual_illegal_instr = 1'b1;
233
+ end else begin
234
+ illegal_instr = 1'b1;
235
+ end
236
+ // do not change privilege level if this is an illegal instruction
237
+ instruction_o.op = ariane_pkg::ADD;
238
+ end
239
+ end else begin
240
+ illegal_instr = 1'b1;
241
+ instruction_o.op = ariane_pkg::ADD;
242
+ end
243
+ end
244
+ // MRET
245
+ 12'b11_0000_0010: begin
246
+ instruction_o.op = ariane_pkg::MRET;
247
+ // check privilege level, MRET can only be executed in M mode
248
+ // otherwise we decode an illegal instruction
249
+ if ((CVA6Cfg.RVS && priv_lvl_i == riscv::PRIV_LVL_S) || (CVA6Cfg.RVU && priv_lvl_i == riscv::PRIV_LVL_U))
250
+ illegal_instr = 1'b1;
251
+ end
252
+ // DRET
253
+ 12'b111_1011_0010: begin
254
+ instruction_o.op = ariane_pkg::DRET;
255
+ if (CVA6Cfg.DebugEn) begin
256
+ // check that we are in debug mode when executing this instruction
257
+ illegal_instr = (!debug_mode_i) ? 1'b1 : illegal_instr;
258
+ end else begin
259
+ illegal_instr = 1'b1;
260
+ end
261
+ end
262
+ // WFI
263
+ 12'b1_0000_0101: begin
264
+ instruction_o.op = ariane_pkg::WFI;
265
+ // if timeout wait is set, trap on an illegal instruction in S Mode
266
+ // (after 0 cycles timeout)
267
+ if (CVA6Cfg.RVS && priv_lvl_i == riscv::PRIV_LVL_S && tw_i) begin
268
+ illegal_instr = 1'b1;
269
+ instruction_o.op = ariane_pkg::ADD;
270
+ end
271
+ if (CVA6Cfg.RVH && priv_lvl_i == riscv::PRIV_LVL_S && v_i && vtw_i && !tw_i) begin
272
+ virtual_illegal_instr = 1'b1;
273
+ instruction_o.op = ariane_pkg::ADD;
274
+ end
275
+ // we don't support U mode interrupts so WFI is illegal in this context
276
+ if (CVA6Cfg.RVU && priv_lvl_i == riscv::PRIV_LVL_U) begin
277
+ if (CVA6Cfg.RVH && v_i) virtual_illegal_instr = 1'b1;
278
+ else illegal_instr = 1'b1;
279
+ instruction_o.op = ariane_pkg::ADD;
280
+ end
281
+ end
282
+ // SFENCE.VMA
283
+ default: begin
284
+ if (instr.instr[31:25] == 7'b1001) begin
285
+ // check privilege level, SFENCE.VMA can only be executed in M/S mode
286
+ // only if S mode is supported
287
+ // otherwise decode an illegal instruction
288
+ if (CVA6Cfg.RVH && v_i) begin
289
+ virtual_illegal_instr = (priv_lvl_i == riscv::PRIV_LVL_S) ? 1'b0 : 1'b1;
290
+ end else begin
291
+ illegal_instr = (CVA6Cfg.RVS && (priv_lvl_i inside {riscv::PRIV_LVL_M, riscv::PRIV_LVL_S}) && instr.itype.rd == '0) ? 1'b0 : 1'b1;
292
+ end
293
+ instruction_o.op = ariane_pkg::SFENCE_VMA;
294
+ // check TVM flag and intercept SFENCE.VMA call if necessary
295
+ if (CVA6Cfg.RVS && priv_lvl_i == riscv::PRIV_LVL_S && tvm_i) begin
296
+ if (CVA6Cfg.RVH && v_i) virtual_illegal_instr = 1'b1;
297
+ else illegal_instr = 1'b1;
298
+ end
299
+ end else if (CVA6Cfg.RVH) begin
300
+ if (instr.instr[31:25] == 7'b10001) begin
301
+ // check privilege level, HFENCE.VVMA can only be executed in M/S mode
302
+ // otherwise decode an illegal instruction or virtual illegal instruction
303
+ if (v_i) begin
304
+ virtual_illegal_instr = 1'b1;
305
+ end else begin
306
+ illegal_instr = (priv_lvl_i inside {riscv::PRIV_LVL_M, riscv::PRIV_LVL_S}) ? 1'b0 : 1'b1;
307
+ end
308
+ instruction_o.op = ariane_pkg::HFENCE_VVMA;
309
+ end else if (instr.instr[31:25] == 7'b110001) begin
310
+ // check privilege level, HFENCE.GVMA can only be executed in M/S mode
311
+ // otherwise decode an illegal instruction or virtual illegal instruction
312
+ if (v_i) begin
313
+ virtual_illegal_instr = 1'b1;
314
+ end else begin
315
+ illegal_instr = (priv_lvl_i inside {riscv::PRIV_LVL_M, riscv::PRIV_LVL_S}) ? 1'b0 : 1'b1;
316
+ end
317
+ instruction_o.op = ariane_pkg::HFENCE_GVMA;
318
+ // check TVM flag and intercept HFENCE.GVMA call if necessary
319
+ if (priv_lvl_i == riscv::PRIV_LVL_S && !v_i && tvm_i) illegal_instr = 1'b1;
320
+ end else begin
321
+ illegal_instr = 1'b1;
322
+ end
323
+ end else begin
324
+ illegal_instr = 1'b1;
325
+ end
326
+ end
327
+ endcase
328
+ end
329
+ 3'b100: begin
330
+ // Hypervisor load/store instructions
331
+ if (CVA6Cfg.RVH) begin
332
+ if (instr.instr[25] != 1'b0) begin
333
+ instruction_o.fu = STORE;
334
+ imm_select = NOIMM;
335
+ instruction_o.rs1 = instr.stype.rs1;
336
+ instruction_o.rs2 = instr.stype.rs2;
337
+ end else begin
338
+ instruction_o.fu = LOAD;
339
+ imm_select = NOIMM;
340
+ instruction_o.rs1 = instr.itype.rs1;
341
+ instruction_o.rd = instr.itype.rd;
342
+ end
343
+
344
+ // Hypervisor load/store instructions when V=1 cause virtual instruction
345
+ if (v_i) virtual_illegal_instr = 1'b1;
346
+ // Hypervisor load/store instructions in U-mode when hstatus.HU=0 cause an illegal instruction trap.
347
+ else if (!hu_i && priv_lvl_i == riscv::PRIV_LVL_U) illegal_instr = 1'b1;
348
+ unique case (instr.rtype.funct7)
349
+ 7'b011_0000: begin
350
+ if (instr.rtype.rs2 == 5'b0) begin
351
+ instruction_o.op = ariane_pkg::HLV_B;
352
+ end
353
+ if (instr.rtype.rs2 == 5'b1) begin
354
+ instruction_o.op = ariane_pkg::HLV_BU;
355
+ end
356
+ end
357
+ 7'b011_0010: begin
358
+ if (instr.rtype.rs2 == 5'b0) begin
359
+ instruction_o.op = ariane_pkg::HLV_H;
360
+ end
361
+ if (instr.rtype.rs2 == 5'b1) begin
362
+ instruction_o.op = ariane_pkg::HLV_HU;
363
+ end
364
+ if (instr.rtype.rs2 == 5'b11) begin
365
+ instruction_o.op = ariane_pkg::HLVX_HU;
366
+ end
367
+ end
368
+ 7'b011_0100: begin
369
+ if (instr.rtype.rs2 == 5'b0) begin
370
+ instruction_o.op = ariane_pkg::HLV_W;
371
+ end
372
+ if (instr.rtype.rs2 == 5'b1) begin
373
+ instruction_o.op = ariane_pkg::HLV_WU;
374
+ end
375
+ if (instr.rtype.rs2 == 5'b11) begin
376
+ instruction_o.op = ariane_pkg::HLVX_WU;
377
+ end
378
+ end
379
+ 7'b011_0001: instruction_o.op = ariane_pkg::HSV_B;
380
+ 7'b011_0011: instruction_o.op = ariane_pkg::HSV_H;
381
+ 7'b011_0101: instruction_o.op = ariane_pkg::HSV_W;
382
+ 7'b011_0110: instruction_o.op = ariane_pkg::HLV_D;
383
+ 7'b011_0111: instruction_o.op = ariane_pkg::HSV_D;
384
+ default: illegal_instr = 1'b1;
385
+
386
+ endcase
387
+ tinst = {
388
+ instr.rtype.funct7,
389
+ instr.rtype.rs2,
390
+ 5'b0,
391
+ instr.rtype.funct3,
392
+ instr.rtype.rd,
393
+ instr.rtype.opcode
394
+ };
395
+ end else begin
396
+ illegal_instr = 1'b1;
397
+ end
398
+ end
399
+ // atomically swaps values in the CSR and integer register
400
+ 3'b001: begin // CSRRW
401
+ imm_select = IIMM;
402
+ instruction_o.op = ariane_pkg::CSR_WRITE;
403
+ end
404
+ // atomically set values in the CSR and write back to rd
405
+ 3'b010: begin // CSRRS
406
+ imm_select = IIMM;
407
+ // this is just a read
408
+ if (instr.itype.rs1 == '0) instruction_o.op = ariane_pkg::CSR_READ;
409
+ else instruction_o.op = ariane_pkg::CSR_SET;
410
+ end
411
+ // atomically clear values in the CSR and write back to rd
412
+ 3'b011: begin // CSRRC
413
+ imm_select = IIMM;
414
+ // this is just a read
415
+ if (instr.itype.rs1 == '0) instruction_o.op = ariane_pkg::CSR_READ;
416
+ else instruction_o.op = ariane_pkg::CSR_CLEAR;
417
+ end
418
+ // use zimm and iimm
419
+ 3'b101: begin // CSRRWI
420
+ instruction_o.rs1 = instr.itype.rs1;
421
+ imm_select = IIMM;
422
+ instruction_o.use_zimm = 1'b1;
423
+ instruction_o.op = ariane_pkg::CSR_WRITE;
424
+ end
425
+ 3'b110: begin // CSRRSI
426
+ instruction_o.rs1 = instr.itype.rs1;
427
+ imm_select = IIMM;
428
+ instruction_o.use_zimm = 1'b1;
429
+ // this is just a read
430
+ if (instr.itype.rs1 == 5'b0) instruction_o.op = ariane_pkg::CSR_READ;
431
+ else instruction_o.op = ariane_pkg::CSR_SET;
432
+ end
433
+ 3'b111: begin // CSRRCI
434
+ instruction_o.rs1 = instr.itype.rs1;
435
+ imm_select = IIMM;
436
+ instruction_o.use_zimm = 1'b1;
437
+ // this is just a read
438
+ if (instr.itype.rs1 == '0) instruction_o.op = ariane_pkg::CSR_READ;
439
+ else instruction_o.op = ariane_pkg::CSR_CLEAR;
440
+ end
441
+ default: illegal_instr = 1'b1;
442
+ endcase
443
+ end
444
+ // Memory ordering instructions
445
+ riscv::OpcodeMiscMem: begin
446
+ instruction_o.fu = CSR;
447
+ instruction_o.rs1 = '0;
448
+ instruction_o.rs2 = '0;
449
+ instruction_o.rd = '0;
450
+
451
+ case (instr.stype.funct3)
452
+ // FENCE
453
+ // Currently implemented as a whole DCache flush boldly ignoring other things
454
+ 3'b000: instruction_o.op = ariane_pkg::FENCE;
455
+ // FENCE.I
456
+ 3'b001: instruction_o.op = ariane_pkg::FENCE_I;
457
+
458
+ default: illegal_instr = 1'b1;
459
+ endcase
460
+ end
461
+
462
+ // --------------------------
463
+ // Reg-Reg Operations
464
+ // --------------------------
465
+ riscv::OpcodeOp: begin
466
+ // --------------------------------------------
467
+ // Vectorial Floating-Point Reg-Reg Operations
468
+ // --------------------------------------------
469
+ if (instr.rvftype.funct2 == 2'b10) begin // Prefix 10 for all Xfvec ops
470
+ // only generate decoder if FP extensions are enabled (static)
471
+ if (CVA6Cfg.FpPresent && CVA6Cfg.XFVec && fs_i != riscv::Off && ((CVA6Cfg.RVH && (!v_i || vfs_i != riscv::Off)) || !CVA6Cfg.RVH)) begin
472
+ automatic logic allow_replication; // control honoring of replication flag
473
+
474
+ instruction_o.fu = FPU_VEC; // Same unit, but sets 'vectorial' signal
475
+ instruction_o.rs1 = instr.rvftype.rs1;
476
+ instruction_o.rs2 = instr.rvftype.rs2;
477
+ instruction_o.rd = instr.rvftype.rd;
478
+ check_fprm = 1'b1;
479
+ allow_replication = 1'b1;
480
+ // decode vectorial FP instruction
481
+ unique case (instr.rvftype.vecfltop)
482
+ 5'b00001: begin
483
+ instruction_o.op = ariane_pkg::FADD; // vfadd.vfmt - Vectorial FP Addition
484
+ instruction_o.rs1 = '0; // Operand A is set to 0
485
+ instruction_o.rs2 = instr.rvftype.rs1; // Operand B is set to rs1
486
+ imm_select = IIMM; // Operand C is set to rs2
487
+ end
488
+ 5'b00010: begin
489
+ instruction_o.op = ariane_pkg::FSUB; // vfsub.vfmt - Vectorial FP Subtraction
490
+ instruction_o.rs1 = '0; // Operand A is set to 0
491
+ instruction_o.rs2 = instr.rvftype.rs1; // Operand B is set to rs1
492
+ imm_select = IIMM; // Operand C is set to rs2
493
+ end
494
+ 5'b00011:
495
+ instruction_o.op = ariane_pkg::FMUL; // vfmul.vfmt - Vectorial FP Multiplication
496
+ 5'b00100:
497
+ instruction_o.op = ariane_pkg::FDIV; // vfdiv.vfmt - Vectorial FP Division
498
+ 5'b00101: begin
499
+ instruction_o.op = ariane_pkg::VFMIN; // vfmin.vfmt - Vectorial FP Minimum
500
+ check_fprm = 1'b0; // rounding mode irrelevant
501
+ end
502
+ 5'b00110: begin
503
+ instruction_o.op = ariane_pkg::VFMAX; // vfmax.vfmt - Vectorial FP Maximum
504
+ check_fprm = 1'b0; // rounding mode irrelevant
505
+ end
506
+ 5'b00111: begin
507
+ instruction_o.op = ariane_pkg::FSQRT; // vfsqrt.vfmt - Vectorial FP Square Root
508
+ allow_replication = 1'b0; // only one operand
509
+ if (instr.rvftype.rs2 != 5'b00000) illegal_instr = 1'b1; // rs2 must be 0
510
+ end
511
+ 5'b01000: begin
512
+ instruction_o.op = ariane_pkg::FMADD; // vfmac.vfmt - Vectorial FP Multiply-Accumulate
513
+ imm_select = SIMM; // rd into result field (upper bits don't matter)
514
+ end
515
+ 5'b01001: begin
516
+ instruction_o.op = ariane_pkg::FMSUB; // vfmre.vfmt - Vectorial FP Multiply-Reduce
517
+ imm_select = SIMM; // rd into result field (upper bits don't matter)
518
+ end
519
+ 5'b01100: begin
520
+ unique case (instr.rvftype.rs2) inside // operation encoded in rs2, `inside` for matching ?
521
+ 5'b00000: begin
522
+ instruction_o.rs2 = instr.rvftype.rs1; // set rs2 = rs1 so we can map FMV to SGNJ in the unit
523
+ if (instr.rvftype.repl)
524
+ instruction_o.op = ariane_pkg::FMV_X2F; // vfmv.vfmt.x - GPR to FPR Move
525
+ else instruction_o.op = ariane_pkg::FMV_F2X; // vfmv.x.vfmt - FPR to GPR Move
526
+ check_fprm = 1'b0; // no rounding for moves
527
+ end
528
+ 5'b00001: begin
529
+ instruction_o.op = ariane_pkg::FCLASS; // vfclass.vfmt - Vectorial FP Classify
530
+ check_fprm = 1'b0; // no rounding for classification
531
+ allow_replication = 1'b0; // R must not be set
532
+ end
533
+ 5'b00010:
534
+ instruction_o.op = ariane_pkg::FCVT_F2I; // vfcvt.x.vfmt - Vectorial FP to Int Conversion
535
+ 5'b00011:
536
+ instruction_o.op = ariane_pkg::FCVT_I2F; // vfcvt.vfmt.x - Vectorial Int to FP Conversion
537
+ 5'b001??: begin
538
+ instruction_o.op = ariane_pkg::FCVT_F2F; // vfcvt.vfmt.vfmt - Vectorial FP to FP Conversion
539
+ instruction_o.rs2 = instr.rvftype.rd; // set rs2 = rd as target vector for conversion
540
+ imm_select = IIMM; // rs2 holds part of the intruction
541
+ // TODO CHECK R bit for valid fmt combinations
542
+ // determine source format
543
+ unique case (instr.rvftype.rs2[21:20])
544
+ // Only process instruction if corresponding extension is active (static)
545
+ 2'b00: if (~CVA6Cfg.RVFVec) illegal_instr = 1'b1;
546
+ 2'b01: if (~CVA6Cfg.XF16ALTVec) illegal_instr = 1'b1;
547
+ 2'b10: if (~CVA6Cfg.XF16Vec) illegal_instr = 1'b1;
548
+ 2'b11: if (~CVA6Cfg.XF8Vec) illegal_instr = 1'b1;
549
+ default: illegal_instr = 1'b1;
550
+ endcase
551
+ end
552
+ default: illegal_instr = 1'b1;
553
+ endcase
554
+ end
555
+ 5'b01101: begin
556
+ check_fprm = 1'b0; // no rounding for sign-injection
557
+ instruction_o.op = ariane_pkg::VFSGNJ; // vfsgnj.vfmt - Vectorial FP Sign Injection
558
+ end
559
+ 5'b01110: begin
560
+ check_fprm = 1'b0; // no rounding for sign-injection
561
+ instruction_o.op = ariane_pkg::VFSGNJN; // vfsgnjn.vfmt - Vectorial FP Negated Sign Injection
562
+ end
563
+ 5'b01111: begin
564
+ check_fprm = 1'b0; // no rounding for sign-injection
565
+ instruction_o.op = ariane_pkg::VFSGNJX; // vfsgnjx.vfmt - Vectorial FP XORed Sign Injection
566
+ end
567
+ 5'b10000: begin
568
+ check_fprm = 1'b0; // no rounding for comparisons
569
+ instruction_o.op = ariane_pkg::VFEQ; // vfeq.vfmt - Vectorial FP Equality
570
+ end
571
+ 5'b10001: begin
572
+ check_fprm = 1'b0; // no rounding for comparisons
573
+ instruction_o.op = ariane_pkg::VFNE; // vfne.vfmt - Vectorial FP Non-Equality
574
+ end
575
+ 5'b10010: begin
576
+ check_fprm = 1'b0; // no rounding for comparisons
577
+ instruction_o.op = ariane_pkg::VFLT; // vfle.vfmt - Vectorial FP Less Than
578
+ end
579
+ 5'b10011: begin
580
+ check_fprm = 1'b0; // no rounding for comparisons
581
+ instruction_o.op = ariane_pkg::VFGE; // vfge.vfmt - Vectorial FP Greater or Equal
582
+ end
583
+ 5'b10100: begin
584
+ check_fprm = 1'b0; // no rounding for comparisons
585
+ instruction_o.op = ariane_pkg::VFLE; // vfle.vfmt - Vectorial FP Less or Equal
586
+ end
587
+ 5'b10101: begin
588
+ check_fprm = 1'b0; // no rounding for comparisons
589
+ instruction_o.op = ariane_pkg::VFGT; // vfgt.vfmt - Vectorial FP Greater Than
590
+ end
591
+ 5'b11000: begin
592
+ instruction_o.op = ariane_pkg::VFCPKAB_S; // vfcpka/b.vfmt.s - Vectorial FP Cast-and-Pack from 2x FP32, lowest 4 entries
593
+ imm_select = SIMM; // rd into result field (upper bits don't matter)
594
+ if (~CVA6Cfg.RVF)
595
+ illegal_instr = 1'b1; // if we don't support RVF, we can't cast from FP32
596
+ // check destination format
597
+ unique case (instr.rvftype.vfmt)
598
+ // Only process instruction if corresponding extension is active and FLEN suffices (static)
599
+ 2'b00: begin
600
+ if (~CVA6Cfg.RVFVec)
601
+ illegal_instr = 1'b1; // destination vector not supported
602
+ if (instr.rvftype.repl)
603
+ illegal_instr = 1'b1; // no entries 2/3 in vector of 2 fp32
604
+ end
605
+ 2'b01: begin
606
+ if (~CVA6Cfg.XF16ALTVec)
607
+ illegal_instr = 1'b1; // destination vector not supported
608
+ end
609
+ 2'b10: begin
610
+ if (~CVA6Cfg.XF16Vec)
611
+ illegal_instr = 1'b1; // destination vector not supported
612
+ end
613
+ 2'b11: begin
614
+ if (~CVA6Cfg.XF8Vec)
615
+ illegal_instr = 1'b1; // destination vector not supported
616
+ end
617
+ default: illegal_instr = 1'b1;
618
+ endcase
619
+ end
620
+ 5'b11001: begin
621
+ instruction_o.op = ariane_pkg::VFCPKCD_S; // vfcpkc/d.vfmt.s - Vectorial FP Cast-and-Pack from 2x FP32, second 4 entries
622
+ imm_select = SIMM; // rd into result field (upper bits don't matter)
623
+ if (~CVA6Cfg.RVF)
624
+ illegal_instr = 1'b1; // if we don't support RVF, we can't cast from FP32
625
+ // check destination format
626
+ unique case (instr.rvftype.vfmt)
627
+ // Only process instruction if corresponding extension is active and FLEN suffices (static)
628
+ 2'b00: illegal_instr = 1'b1; // no entries 4-7 in vector of 2 FP32
629
+ 2'b01: illegal_instr = 1'b1; // no entries 4-7 in vector of 4 FP16ALT
630
+ 2'b10: illegal_instr = 1'b1; // no entries 4-7 in vector of 4 FP16
631
+ 2'b11: begin
632
+ if (~CVA6Cfg.XF8Vec)
633
+ illegal_instr = 1'b1; // destination vector not supported
634
+ end
635
+ default: illegal_instr = 1'b1;
636
+ endcase
637
+ end
638
+ 5'b11010: begin
639
+ instruction_o.op = ariane_pkg::VFCPKAB_D; // vfcpka/b.vfmt.d - Vectorial FP Cast-and-Pack from 2x FP64, lowest 4 entries
640
+ imm_select = SIMM; // rd into result field (upper bits don't matter)
641
+ if (~CVA6Cfg.RVD)
642
+ illegal_instr = 1'b1; // if we don't support RVD, we can't cast from FP64
643
+ // check destination format
644
+ unique case (instr.rvftype.vfmt)
645
+ // Only process instruction if corresponding extension is active and FLEN suffices (static)
646
+ 2'b00: begin
647
+ if (~CVA6Cfg.RVFVec)
648
+ illegal_instr = 1'b1; // destination vector not supported
649
+ if (instr.rvftype.repl)
650
+ illegal_instr = 1'b1; // no entries 2/3 in vector of 2 fp32
651
+ end
652
+ 2'b01: begin
653
+ if (~CVA6Cfg.XF16ALTVec)
654
+ illegal_instr = 1'b1; // destination vector not supported
655
+ end
656
+ 2'b10: begin
657
+ if (~CVA6Cfg.XF16Vec)
658
+ illegal_instr = 1'b1; // destination vector not supported
659
+ end
660
+ 2'b11: begin
661
+ if (~CVA6Cfg.XF8Vec)
662
+ illegal_instr = 1'b1; // destination vector not supported
663
+ end
664
+ default: illegal_instr = 1'b1;
665
+ endcase
666
+ end
667
+ 5'b11011: begin
668
+ instruction_o.op = ariane_pkg::VFCPKCD_D; // vfcpka/b.vfmt.d - Vectorial FP Cast-and-Pack from 2x FP64, second 4 entries
669
+ imm_select = SIMM; // rd into result field (upper bits don't matter)
670
+ if (~CVA6Cfg.RVD)
671
+ illegal_instr = 1'b1; // if we don't support RVD, we can't cast from FP64
672
+ // check destination format
673
+ unique case (instr.rvftype.vfmt)
674
+ // Only process instruction if corresponding extension is active and FLEN suffices (static)
675
+ 2'b00: illegal_instr = 1'b1; // no entries 4-7 in vector of 2 FP32
676
+ 2'b01: illegal_instr = 1'b1; // no entries 4-7 in vector of 4 FP16ALT
677
+ 2'b10: illegal_instr = 1'b1; // no entries 4-7 in vector of 4 FP16
678
+ 2'b11: begin
679
+ if (~CVA6Cfg.XF8Vec)
680
+ illegal_instr = 1'b1; // destination vector not supported
681
+ end
682
+ default: illegal_instr = 1'b1;
683
+ endcase
684
+ end
685
+ default: illegal_instr = 1'b1;
686
+ endcase
687
+
688
+ // check format
689
+ unique case (instr.rvftype.vfmt)
690
+ // Only process instruction if corresponding extension is active (static)
691
+ 2'b00: if (~CVA6Cfg.RVFVec) illegal_instr = 1'b1;
692
+ 2'b01: if (~CVA6Cfg.XF16ALTVec) illegal_instr = 1'b1;
693
+ 2'b10: if (~CVA6Cfg.XF16Vec) illegal_instr = 1'b1;
694
+ 2'b11: if (~CVA6Cfg.XF8Vec) illegal_instr = 1'b1;
695
+ default: illegal_instr = 1'b1;
696
+ endcase
697
+
698
+ // check disallowed replication
699
+ if (~allow_replication & instr.rvftype.repl) illegal_instr = 1'b1;
700
+
701
+ // check rounding mode
702
+ if (check_fprm) begin
703
+ unique case (frm_i) inside // actual rounding mode from frm csr
704
+ [3'b000 : 3'b100]: ; //legal rounding modes
705
+ default: illegal_instr = 1'b1;
706
+ endcase
707
+ end
708
+
709
+ end else begin // No vectorial FP enabled (static)
710
+ illegal_instr = 1'b1;
711
+ end
712
+
713
+ // ---------------------------
714
+ // Integer Reg-Reg Operations
715
+ // ---------------------------
716
+ end else begin
717
+ if (CVA6Cfg.RVB) begin
718
+ instruction_o.fu = (instr.rtype.funct7 == 7'b000_0001 || ((instr.rtype.funct7 == 7'b000_0101) && !(instr.rtype.funct3[14]))) ? MULT : ALU;
719
+ end else begin
720
+ instruction_o.fu = (instr.rtype.funct7 == 7'b000_0001) ? MULT : ALU;
721
+ end
722
+ instruction_o.rs1 = instr.rtype.rs1;
723
+ instruction_o.rs2 = instr.rtype.rs2;
724
+ instruction_o.rd = instr.rtype.rd;
725
+
726
+ unique case ({
727
+ instr.rtype.funct7, instr.rtype.funct3
728
+ })
729
+ {7'b000_0000, 3'b000} : instruction_o.op = ariane_pkg::ADD; // Add
730
+ {7'b010_0000, 3'b000} : instruction_o.op = ariane_pkg::SUB; // Sub
731
+ {7'b000_0000, 3'b010} : instruction_o.op = ariane_pkg::SLTS; // Set Lower Than
732
+ {
733
+ 7'b000_0000, 3'b011
734
+ } :
735
+ instruction_o.op = ariane_pkg::SLTU; // Set Lower Than Unsigned
736
+ {7'b000_0000, 3'b100} : instruction_o.op = ariane_pkg::XORL; // Xor
737
+ {7'b000_0000, 3'b110} : instruction_o.op = ariane_pkg::ORL; // Or
738
+ {7'b000_0000, 3'b111} : instruction_o.op = ariane_pkg::ANDL; // And
739
+ {7'b000_0000, 3'b001} : instruction_o.op = ariane_pkg::SLL; // Shift Left Logical
740
+ {7'b000_0000, 3'b101} : instruction_o.op = ariane_pkg::SRL; // Shift Right Logical
741
+ {7'b010_0000, 3'b101} : instruction_o.op = ariane_pkg::SRA; // Shift Right Arithmetic
742
+ // Multiplications
743
+ {7'b000_0001, 3'b000} : instruction_o.op = ariane_pkg::MUL;
744
+ {7'b000_0001, 3'b001} : instruction_o.op = ariane_pkg::MULH;
745
+ {7'b000_0001, 3'b010} : instruction_o.op = ariane_pkg::MULHSU;
746
+ {7'b000_0001, 3'b011} : instruction_o.op = ariane_pkg::MULHU;
747
+ {7'b000_0001, 3'b100} : instruction_o.op = ariane_pkg::DIV;
748
+ {7'b000_0001, 3'b101} : instruction_o.op = ariane_pkg::DIVU;
749
+ {7'b000_0001, 3'b110} : instruction_o.op = ariane_pkg::REM;
750
+ {7'b000_0001, 3'b111} : instruction_o.op = ariane_pkg::REMU;
751
+ default: begin
752
+ illegal_instr_non_bm = 1'b1;
753
+ end
754
+ endcase
755
+ if (CVA6Cfg.RVB) begin
756
+ unique case ({
757
+ instr.rtype.funct7, instr.rtype.funct3
758
+ })
759
+ //Logical with Negate
760
+ {7'b010_0000, 3'b111} : instruction_o.op = ariane_pkg::ANDN; // Andn
761
+ {7'b010_0000, 3'b110} : instruction_o.op = ariane_pkg::ORN; // Orn
762
+ {7'b010_0000, 3'b100} : instruction_o.op = ariane_pkg::XNOR; // Xnor
763
+ //Shift and Add (Bitmanip)
764
+ {7'b001_0000, 3'b010} : instruction_o.op = ariane_pkg::SH1ADD; // Sh1add
765
+ {7'b001_0000, 3'b100} : instruction_o.op = ariane_pkg::SH2ADD; // Sh2add
766
+ {7'b001_0000, 3'b110} : instruction_o.op = ariane_pkg::SH3ADD; // Sh3add
767
+ // Integer maximum/minimum
768
+ {7'b000_0101, 3'b110} : instruction_o.op = ariane_pkg::MAX; // max
769
+ {7'b000_0101, 3'b111} : instruction_o.op = ariane_pkg::MAXU; // maxu
770
+ {7'b000_0101, 3'b100} : instruction_o.op = ariane_pkg::MIN; // min
771
+ {7'b000_0101, 3'b101} : instruction_o.op = ariane_pkg::MINU; // minu
772
+ // Single bit instructions
773
+ {7'b010_0100, 3'b001} : instruction_o.op = ariane_pkg::BCLR; // bclr
774
+ {7'b010_0100, 3'b101} : instruction_o.op = ariane_pkg::BEXT; // bext
775
+ {7'b011_0100, 3'b001} : instruction_o.op = ariane_pkg::BINV; // binv
776
+ {7'b001_0100, 3'b001} : instruction_o.op = ariane_pkg::BSET; // bset
777
+ // Carry-Less-Multiplication (clmul, clmulh, clmulr)
778
+ {7'b000_0101, 3'b001} : instruction_o.op = ariane_pkg::CLMUL; // clmul
779
+ {7'b000_0101, 3'b011} : instruction_o.op = ariane_pkg::CLMULH; // clmulh
780
+ {7'b000_0101, 3'b010} : instruction_o.op = ariane_pkg::CLMULR; // clmulr
781
+ // Bitwise Shifting
782
+ {7'b011_0000, 3'b001} : instruction_o.op = ariane_pkg::ROL; // rol
783
+ {7'b011_0000, 3'b101} : instruction_o.op = ariane_pkg::ROR; // ror
784
+ {
785
+ 7'b000_0100, 3'b111
786
+ } : begin
787
+ if (CVA6Cfg.ZKN) instruction_o.op = ariane_pkg::PACK_H; //packh
788
+ else illegal_instr_bm = 1'b1;
789
+ end
790
+ // Zero Extend Op RV32 encoding
791
+ {
792
+ 7'b000_0100, 3'b100
793
+ } : begin
794
+ if (!CVA6Cfg.IS_XLEN64 && instr.instr[24:20] == 5'b00000)
795
+ instruction_o.op = ariane_pkg::ZEXTH; // Zero Extend Op RV32 encoding
796
+ else if (CVA6Cfg.ZKN) instruction_o.op = ariane_pkg::PACK; // pack
797
+ else illegal_instr_bm = 1'b1;
798
+ end
799
+ default: begin
800
+ illegal_instr_bm = 1'b1;
801
+ end
802
+ endcase
803
+ end
804
+ if (CVA6Cfg.RVZiCond) begin
805
+ unique case ({
806
+ instr.rtype.funct7, instr.rtype.funct3
807
+ })
808
+ //Conditional move
809
+ {7'b000_0111, 3'b101} : instruction_o.op = ariane_pkg::CZERO_EQZ; // czero.eqz
810
+ {7'b000_0111, 3'b111} : instruction_o.op = ariane_pkg::CZERO_NEZ; // czero.nez
811
+ default: begin
812
+ illegal_instr_zic = 1'b1;
813
+ end
814
+ endcase
815
+ end
816
+ //VCS coverage on
817
+ unique case ({
818
+ CVA6Cfg.RVB, CVA6Cfg.RVZiCond
819
+ })
820
+ 2'b00: illegal_instr = illegal_instr_non_bm;
821
+ 2'b01: illegal_instr = illegal_instr_non_bm & illegal_instr_zic;
822
+ 2'b10: illegal_instr = illegal_instr_non_bm & illegal_instr_bm;
823
+ 2'b11: illegal_instr = illegal_instr_non_bm & illegal_instr_bm & illegal_instr_zic;
824
+ default: ; // TODO: Check that default case is not synthesized.
825
+ endcase
826
+ end
827
+ end
828
+
829
+ // --------------------------
830
+ // 32bit Reg-Reg Operations
831
+ // --------------------------
832
+ riscv::OpcodeOp32: begin
833
+ instruction_o.fu = (instr.rtype.funct7 == 7'b000_0001) ? MULT : ALU;
834
+ instruction_o.rs1 = instr.rtype.rs1;
835
+ instruction_o.rs2 = instr.rtype.rs2;
836
+ instruction_o.rd = instr.rtype.rd;
837
+ if (CVA6Cfg.IS_XLEN64) begin
838
+ unique case ({
839
+ instr.rtype.funct7, instr.rtype.funct3
840
+ })
841
+ {7'b000_0000, 3'b000} : instruction_o.op = ariane_pkg::ADDW; // addw
842
+ {7'b010_0000, 3'b000} : instruction_o.op = ariane_pkg::SUBW; // subw
843
+ {7'b000_0000, 3'b001} : instruction_o.op = ariane_pkg::SLLW; // sllw
844
+ {7'b000_0000, 3'b101} : instruction_o.op = ariane_pkg::SRLW; // srlw
845
+ {7'b010_0000, 3'b101} : instruction_o.op = ariane_pkg::SRAW; // sraw
846
+ // Multiplications
847
+ {7'b000_0001, 3'b000} : instruction_o.op = ariane_pkg::MULW;
848
+ {7'b000_0001, 3'b100} : instruction_o.op = ariane_pkg::DIVW;
849
+ {7'b000_0001, 3'b101} : instruction_o.op = ariane_pkg::DIVUW;
850
+ {7'b000_0001, 3'b110} : instruction_o.op = ariane_pkg::REMW;
851
+ {7'b000_0001, 3'b111} : instruction_o.op = ariane_pkg::REMUW;
852
+ default: illegal_instr_non_bm = 1'b1;
853
+ endcase
854
+ if (CVA6Cfg.RVB) begin
855
+ unique case ({
856
+ instr.rtype.funct7, instr.rtype.funct3
857
+ })
858
+ // Shift with Add (Unsigned Word)
859
+ {7'b001_0000, 3'b010} : instruction_o.op = ariane_pkg::SH1ADDUW; // sh1add.uw
860
+ {7'b001_0000, 3'b100} : instruction_o.op = ariane_pkg::SH2ADDUW; // sh2add.uw
861
+ {7'b001_0000, 3'b110} : instruction_o.op = ariane_pkg::SH3ADDUW; // sh3add.uw
862
+ // Unsigned word Op's
863
+ {7'b000_0100, 3'b000} : instruction_o.op = ariane_pkg::ADDUW; // add.uw
864
+ // Bitwise Shifting
865
+ {7'b011_0000, 3'b001} : instruction_o.op = ariane_pkg::ROLW; // rolw
866
+ {7'b011_0000, 3'b101} : instruction_o.op = ariane_pkg::RORW; // rorw
867
+ {
868
+ 7'b000_0100, 3'b100
869
+ } : begin
870
+ if (instr.instr[24:20] == 5'b00000)
871
+ instruction_o.op = ariane_pkg::ZEXTH; // Zero Extend Op RV64 encoding
872
+ else if (CVA6Cfg.ZKN) instruction_o.op = ariane_pkg::PACK_W; // packw
873
+ else illegal_instr_bm = 1'b1;
874
+ end
875
+ default: illegal_instr_bm = 1'b1;
876
+ endcase
877
+ illegal_instr = illegal_instr_non_bm & illegal_instr_bm;
878
+ end else begin
879
+ illegal_instr = illegal_instr_non_bm;
880
+ end
881
+ end else illegal_instr = 1'b1;
882
+ end
883
+ // --------------------------------
884
+ // Reg-Immediate Operations
885
+ // --------------------------------
886
+ riscv::OpcodeOpImm: begin
887
+ instruction_o.fu = ALU;
888
+ imm_select = IIMM;
889
+ instruction_o.rs1 = instr.itype.rs1;
890
+ instruction_o.rd = instr.itype.rd;
891
+ unique case (instr.itype.funct3)
892
+ 3'b000: instruction_o.op = ariane_pkg::ADD; // Add Immediate
893
+ 3'b010: instruction_o.op = ariane_pkg::SLTS; // Set to one if Lower Than Immediate
894
+ 3'b011:
895
+ instruction_o.op = ariane_pkg::SLTU; // Set to one if Lower Than Immediate Unsigned
896
+ 3'b100: instruction_o.op = ariane_pkg::XORL; // Exclusive Or with Immediate
897
+ 3'b110: instruction_o.op = ariane_pkg::ORL; // Or with Immediate
898
+ 3'b111: instruction_o.op = ariane_pkg::ANDL; // And with Immediate
899
+
900
+ 3'b001: begin
901
+ instruction_o.op = ariane_pkg::SLL; // Shift Left Logical by Immediate
902
+ if (instr.instr[31:26] != 6'b0) illegal_instr_non_bm = 1'b1;
903
+ if (instr.instr[25] != 1'b0 && CVA6Cfg.XLEN == 32) illegal_instr_non_bm = 1'b1;
904
+ end
905
+
906
+ 3'b101: begin
907
+ if (instr.instr[31:26] == 6'b0)
908
+ instruction_o.op = ariane_pkg::SRL; // Shift Right Logical by Immediate
909
+ else if (instr.instr[31:26] == 6'b010_000)
910
+ instruction_o.op = ariane_pkg::SRA; // Shift Right Arithmetically by Immediate
911
+ else illegal_instr_non_bm = 1'b1;
912
+ if (instr.instr[25] != 1'b0 && CVA6Cfg.XLEN == 32) illegal_instr_non_bm = 1'b1;
913
+ end
914
+ endcase
915
+ if (CVA6Cfg.RVB) begin
916
+ unique case (instr.itype.funct3)
917
+ 3'b001: begin
918
+ if (instr.instr[31:25] == 7'b0110000) begin
919
+ if (instr.instr[24:20] == 5'b00100) instruction_o.op = ariane_pkg::SEXTB;
920
+ else if (instr.instr[24:20] == 5'b00101) instruction_o.op = ariane_pkg::SEXTH;
921
+ else if (instr.instr[24:20] == 5'b00010) instruction_o.op = ariane_pkg::CPOP;
922
+ else if (instr.instr[24:20] == 5'b00000) instruction_o.op = ariane_pkg::CLZ;
923
+ else if (instr.instr[24:20] == 5'b00001) instruction_o.op = ariane_pkg::CTZ;
924
+ else illegal_instr_bm = 1'b1;
925
+ end else if (CVA6Cfg.IS_XLEN64 && instr.instr[31:26] == 6'b010010)
926
+ instruction_o.op = ariane_pkg::BCLRI;
927
+ else if (CVA6Cfg.IS_XLEN32 && instr.instr[31:25] == 7'b0100100)
928
+ instruction_o.op = ariane_pkg::BCLRI;
929
+ else if (CVA6Cfg.IS_XLEN64 && instr.instr[31:26] == 6'b011010)
930
+ instruction_o.op = ariane_pkg::BINVI;
931
+ else if (CVA6Cfg.IS_XLEN32 && instr.instr[31:25] == 7'b0110100)
932
+ instruction_o.op = ariane_pkg::BINVI;
933
+ else if (CVA6Cfg.IS_XLEN64 && instr.instr[31:26] == 6'b001010)
934
+ instruction_o.op = ariane_pkg::BSETI;
935
+ else if (CVA6Cfg.IS_XLEN32 && instr.instr[31:25] == 7'b0010100)
936
+ instruction_o.op = ariane_pkg::BSETI;
937
+ else if (CVA6Cfg.ZKN && instr.instr[31:20] == 12'b000010001111)
938
+ instruction_o.op = ariane_pkg::ZIP;
939
+ else illegal_instr_bm = 1'b1;
940
+ end
941
+ 3'b101: begin
942
+ if (instr.instr[31:20] == 12'b001010000111) instruction_o.op = ariane_pkg::ORCB;
943
+ else if (CVA6Cfg.IS_XLEN64 && instr.instr[31:20] == 12'b011010111000)
944
+ instruction_o.op = ariane_pkg::REV8;
945
+ else if (instr.instr[31:20] == 12'b011010011000)
946
+ instruction_o.op = ariane_pkg::REV8;
947
+ else if (CVA6Cfg.IS_XLEN64 && instr.instr[31:26] == 6'b010_010)
948
+ instruction_o.op = ariane_pkg::BEXTI;
949
+ else if (CVA6Cfg.IS_XLEN32 && instr.instr[31:25] == 7'b010_0100)
950
+ instruction_o.op = ariane_pkg::BEXTI;
951
+ else if (CVA6Cfg.IS_XLEN64 && instr.instr[31:26] == 6'b011_000)
952
+ instruction_o.op = ariane_pkg::RORI;
953
+ else if (CVA6Cfg.IS_XLEN32 && instr.instr[31:25] == 7'b011_0000)
954
+ instruction_o.op = ariane_pkg::RORI;
955
+ else if (CVA6Cfg.ZKN && instr.instr[31:20] == 12'b011010000111)
956
+ instruction_o.op = ariane_pkg::BREV8;
957
+ else if (CVA6Cfg.ZKN && instr.instr[31:20] == 12'b000010001111)
958
+ instruction_o.op = ariane_pkg::UNZIP;
959
+ else illegal_instr_bm = 1'b1;
960
+ end
961
+ default: illegal_instr_bm = 1'b1;
962
+ endcase
963
+ illegal_instr = illegal_instr_non_bm & illegal_instr_bm;
964
+ end else begin
965
+ illegal_instr = illegal_instr_non_bm;
966
+ end
967
+ end
968
+
969
+ // --------------------------------
970
+ // 32 bit Reg-Immediate Operations
971
+ // --------------------------------
972
+ riscv::OpcodeOpImm32: begin
973
+ instruction_o.fu = ALU;
974
+ imm_select = IIMM;
975
+ instruction_o.rs1 = instr.itype.rs1;
976
+ instruction_o.rd = instr.itype.rd;
977
+ if (CVA6Cfg.IS_XLEN64) begin
978
+ unique case (instr.itype.funct3)
979
+ 3'b000: instruction_o.op = ariane_pkg::ADDW; // Add Immediate
980
+ 3'b001: begin
981
+ instruction_o.op = ariane_pkg::SLLW; // Shift Left Logical by Immediate
982
+ if (instr.instr[31:25] != 7'b0) illegal_instr_non_bm = 1'b1;
983
+ end
984
+ 3'b101: begin
985
+ if (instr.instr[31:25] == 7'b0)
986
+ instruction_o.op = ariane_pkg::SRLW; // Shift Right Logical by Immediate
987
+ else if (instr.instr[31:25] == 7'b010_0000)
988
+ instruction_o.op = ariane_pkg::SRAW; // Shift Right Arithmetically by Immediate
989
+ else illegal_instr_non_bm = 1'b1;
990
+ end
991
+ default: illegal_instr_non_bm = 1'b1;
992
+ endcase
993
+ if (CVA6Cfg.RVB) begin
994
+ unique case (instr.itype.funct3)
995
+ 3'b001: begin
996
+ if (instr.instr[31:25] == 7'b0110000) begin
997
+ if (instr.instr[21:20] == 2'b10) instruction_o.op = ariane_pkg::CPOPW;
998
+ else if (instr.instr[21:20] == 2'b00) instruction_o.op = ariane_pkg::CLZW;
999
+ else if (instr.instr[21:20] == 2'b01) instruction_o.op = ariane_pkg::CTZW;
1000
+ else illegal_instr_bm = 1'b1;
1001
+ end else if (instr.instr[31:26] == 6'b000010) begin
1002
+ instruction_o.op = ariane_pkg::SLLIUW; // Shift Left Logic by Immediate (Unsigned Word)
1003
+ end else illegal_instr_bm = 1'b1;
1004
+ end
1005
+ 3'b101: begin
1006
+ if (instr.instr[31:25] == 7'b011_0000) instruction_o.op = ariane_pkg::RORIW;
1007
+ else illegal_instr_bm = 1'b1;
1008
+ end
1009
+ default: illegal_instr_bm = 1'b1;
1010
+ endcase
1011
+ illegal_instr = illegal_instr_non_bm & illegal_instr_bm;
1012
+ end else begin
1013
+ illegal_instr = illegal_instr_non_bm;
1014
+ end
1015
+
1016
+ end else illegal_instr = 1'b1;
1017
+ end
1018
+ // --------------------------------
1019
+ // LSU
1020
+ // --------------------------------
1021
+ riscv::OpcodeStore: begin
1022
+ instruction_o.fu = STORE;
1023
+ imm_select = SIMM;
1024
+ instruction_o.rs1 = instr.stype.rs1;
1025
+ instruction_o.rs2 = instr.stype.rs2;
1026
+ // determine store size
1027
+ unique case (instr.stype.funct3)
1028
+ 3'b000: instruction_o.op = ariane_pkg::SB;
1029
+ 3'b001: instruction_o.op = ariane_pkg::SH;
1030
+ 3'b010: instruction_o.op = ariane_pkg::SW;
1031
+ 3'b011:
1032
+ if (CVA6Cfg.XLEN == 64) instruction_o.op = ariane_pkg::SD;
1033
+ else illegal_instr = 1'b1;
1034
+ default: illegal_instr = 1'b1;
1035
+ endcase
1036
+ if (CVA6Cfg.RVH) begin
1037
+ tinst = {7'b0, instr.stype.rs2, 5'b0, instr.stype.funct3, 5'b0, instr.stype.opcode};
1038
+ tinst[1] = is_compressed_i ? 1'b0 : 'b1;
1039
+ end
1040
+ end
1041
+
1042
+ riscv::OpcodeLoad: begin
1043
+ instruction_o.fu = LOAD;
1044
+ imm_select = IIMM;
1045
+ instruction_o.rs1 = instr.itype.rs1;
1046
+ instruction_o.rd = instr.itype.rd;
1047
+ // determine load size and signed type
1048
+ unique case (instr.itype.funct3)
1049
+ 3'b000: instruction_o.op = ariane_pkg::LB;
1050
+ 3'b001: instruction_o.op = ariane_pkg::LH;
1051
+ 3'b010: instruction_o.op = ariane_pkg::LW;
1052
+ 3'b100: instruction_o.op = ariane_pkg::LBU;
1053
+ 3'b101: instruction_o.op = ariane_pkg::LHU;
1054
+ 3'b110:
1055
+ if (CVA6Cfg.XLEN == 64) instruction_o.op = ariane_pkg::LWU;
1056
+ else illegal_instr = 1'b1;
1057
+ 3'b011:
1058
+ if (CVA6Cfg.XLEN == 64) instruction_o.op = ariane_pkg::LD;
1059
+ else illegal_instr = 1'b1;
1060
+ default: illegal_instr = 1'b1;
1061
+ endcase
1062
+ if (CVA6Cfg.RVH) begin
1063
+ tinst = {17'b0, instr.itype.funct3, instr.itype.rd, instr.itype.opcode};
1064
+ tinst[1] = is_compressed_i ? 1'b0 : 'b1;
1065
+ end
1066
+ end
1067
+
1068
+ // --------------------------------
1069
+ // Floating-Point Load/store
1070
+ // --------------------------------
1071
+ riscv::OpcodeStoreFp: begin
1072
+ if (CVA6Cfg.FpPresent && fs_i != riscv::Off && ((CVA6Cfg.RVH && (!v_i || vfs_i != riscv::Off)) || !CVA6Cfg.RVH)) begin // only generate decoder if FP extensions are enabled (static)
1073
+ instruction_o.fu = STORE;
1074
+ imm_select = SIMM;
1075
+ instruction_o.rs1 = instr.stype.rs1;
1076
+ instruction_o.rs2 = instr.stype.rs2;
1077
+ // determine store size
1078
+ unique case (instr.stype.funct3)
1079
+ // Only process instruction if corresponding extension is active (static)
1080
+ 3'b000:
1081
+ if (CVA6Cfg.XF8) instruction_o.op = ariane_pkg::FSB;
1082
+ else illegal_instr = 1'b1;
1083
+ 3'b001:
1084
+ if (CVA6Cfg.XF16 | CVA6Cfg.XF16ALT) instruction_o.op = ariane_pkg::FSH;
1085
+ else illegal_instr = 1'b1;
1086
+ 3'b010:
1087
+ if (CVA6Cfg.RVF) instruction_o.op = ariane_pkg::FSW;
1088
+ else illegal_instr = 1'b1;
1089
+ 3'b011:
1090
+ if (CVA6Cfg.RVD) instruction_o.op = ariane_pkg::FSD;
1091
+ else illegal_instr = 1'b1;
1092
+ default: illegal_instr = 1'b1;
1093
+ endcase
1094
+ if (CVA6Cfg.RVH) begin
1095
+ tinst = {7'b0, instr.stype.rs2, 5'b0, instr.stype.funct3, 5'b0, instr.stype.opcode};
1096
+ tinst[1] = is_compressed_i ? 1'b0 : 'b1;
1097
+ end
1098
+ end else illegal_instr = 1'b1;
1099
+ end
1100
+
1101
+ riscv::OpcodeLoadFp: begin
1102
+ if (CVA6Cfg.FpPresent && fs_i != riscv::Off && ((CVA6Cfg.RVH && (!v_i || vfs_i != riscv::Off)) || !CVA6Cfg.RVH)) begin // only generate decoder if FP extensions are enabled (static)
1103
+ instruction_o.fu = LOAD;
1104
+ imm_select = IIMM;
1105
+ instruction_o.rs1 = instr.itype.rs1;
1106
+ instruction_o.rd = instr.itype.rd;
1107
+ // determine load size
1108
+ unique case (instr.itype.funct3)
1109
+ // Only process instruction if corresponding extension is active (static)
1110
+ 3'b000:
1111
+ if (CVA6Cfg.XF8) instruction_o.op = ariane_pkg::FLB;
1112
+ else illegal_instr = 1'b1;
1113
+ 3'b001:
1114
+ if (CVA6Cfg.XF16 | CVA6Cfg.XF16ALT) instruction_o.op = ariane_pkg::FLH;
1115
+ else illegal_instr = 1'b1;
1116
+ 3'b010:
1117
+ if (CVA6Cfg.RVF) instruction_o.op = ariane_pkg::FLW;
1118
+ else illegal_instr = 1'b1;
1119
+ 3'b011:
1120
+ if (CVA6Cfg.RVD) instruction_o.op = ariane_pkg::FLD;
1121
+ else illegal_instr = 1'b1;
1122
+ default: illegal_instr = 1'b1;
1123
+ endcase
1124
+ if (CVA6Cfg.RVH) begin
1125
+ tinst = {17'b0, instr.itype.funct3, instr.itype.rd, instr.itype.opcode};
1126
+ tinst[1] = is_compressed_i ? 1'b0 : 'b1;
1127
+ end
1128
+ end else illegal_instr = 1'b1;
1129
+ end
1130
+
1131
+ // ----------------------------------
1132
+ // Floating-Point Reg-Reg Operations
1133
+ // ----------------------------------
1134
+ riscv::OpcodeMadd, riscv::OpcodeMsub, riscv::OpcodeNmsub, riscv::OpcodeNmadd: begin
1135
+ if (CVA6Cfg.FpPresent && fs_i != riscv::Off && ((CVA6Cfg.RVH && (!v_i || vfs_i != riscv::Off)) || !CVA6Cfg.RVH)) begin // only generate decoder if FP extensions are enabled (static)
1136
+ instruction_o.fu = FPU;
1137
+ instruction_o.rs1 = instr.r4type.rs1;
1138
+ instruction_o.rs2 = instr.r4type.rs2;
1139
+ instruction_o.rd = instr.r4type.rd;
1140
+ imm_select = RS3; // rs3 into result field
1141
+ check_fprm = 1'b1;
1142
+ // select the correct fused operation
1143
+ unique case (instr.r4type.opcode)
1144
+ default: instruction_o.op = ariane_pkg::FMADD; // fmadd.fmt - FP Fused multiply-add
1145
+ riscv::OpcodeMsub:
1146
+ instruction_o.op = ariane_pkg::FMSUB; // fmsub.fmt - FP Fused multiply-subtract
1147
+ riscv::OpcodeNmsub:
1148
+ instruction_o.op = ariane_pkg::FNMSUB; // fnmsub.fmt - FP Negated fused multiply-subtract
1149
+ riscv::OpcodeNmadd:
1150
+ instruction_o.op = ariane_pkg::FNMADD; // fnmadd.fmt - FP Negated fused multiply-add
1151
+ endcase
1152
+
1153
+ // determine fp format
1154
+ unique case (instr.r4type.funct2)
1155
+ // Only process instruction if corresponding extension is active (static)
1156
+ 2'b00: if (~CVA6Cfg.RVF) illegal_instr = 1'b1;
1157
+ 2'b01: if (~CVA6Cfg.RVD) illegal_instr = 1'b1;
1158
+ 2'b10: if (~CVA6Cfg.XF16 & ~CVA6Cfg.XF16ALT) illegal_instr = 1'b1;
1159
+ 2'b11: if (~CVA6Cfg.XF8) illegal_instr = 1'b1;
1160
+ default: illegal_instr = 1'b1;
1161
+ endcase
1162
+
1163
+ // check rounding mode
1164
+ if (check_fprm) begin
1165
+ unique case (instr.rftype.rm) inside
1166
+ [3'b000 : 3'b100]: ; //legal rounding modes
1167
+ 3'b101: begin // Alternative Half-Precsision encded as fmt=10 and rm=101
1168
+ if (~CVA6Cfg.XF16ALT || instr.rftype.fmt != 2'b10) illegal_instr = 1'b1;
1169
+ unique case (frm_i) inside // actual rounding mode from frm csr
1170
+ [3'b000 : 3'b100]: ; //legal rounding modes
1171
+ default: illegal_instr = 1'b1;
1172
+ endcase
1173
+ end
1174
+ 3'b111: begin
1175
+ // rounding mode from frm csr
1176
+ unique case (frm_i) inside
1177
+ [3'b000 : 3'b100]: ; //legal rounding modes
1178
+ default: illegal_instr = 1'b1;
1179
+ endcase
1180
+ end
1181
+ default: illegal_instr = 1'b1;
1182
+ endcase
1183
+ end
1184
+ end else begin
1185
+ illegal_instr = 1'b1;
1186
+ end
1187
+ end
1188
+
1189
+ riscv::OpcodeOpFp: begin
1190
+ if (CVA6Cfg.FpPresent && fs_i != riscv::Off && ((CVA6Cfg.RVH && (!v_i || vfs_i != riscv::Off)) || !CVA6Cfg.RVH)) begin // only generate decoder if FP extensions are enabled (static)
1191
+ instruction_o.fu = FPU;
1192
+ instruction_o.rs1 = instr.rftype.rs1;
1193
+ instruction_o.rs2 = instr.rftype.rs2;
1194
+ instruction_o.rd = instr.rftype.rd;
1195
+ check_fprm = 1'b1;
1196
+ // decode FP instruction
1197
+ unique case (instr.rftype.funct5)
1198
+ 5'b00000: begin
1199
+ instruction_o.op = ariane_pkg::FADD; // fadd.fmt - FP Addition
1200
+ instruction_o.rs1 = '0; // Operand A is set to 0
1201
+ instruction_o.rs2 = instr.rftype.rs1; // Operand B is set to rs1
1202
+ imm_select = IIMM; // Operand C is set to rs2
1203
+ end
1204
+ 5'b00001: begin
1205
+ instruction_o.op = ariane_pkg::FSUB; // fsub.fmt - FP Subtraction
1206
+ instruction_o.rs1 = '0; // Operand A is set to 0
1207
+ instruction_o.rs2 = instr.rftype.rs1; // Operand B is set to rs1
1208
+ imm_select = IIMM; // Operand C is set to rs2
1209
+ end
1210
+ 5'b00010: instruction_o.op = ariane_pkg::FMUL; // fmul.fmt - FP Multiplication
1211
+ 5'b00011: instruction_o.op = ariane_pkg::FDIV; // fdiv.fmt - FP Division
1212
+ 5'b01011: begin
1213
+ instruction_o.op = ariane_pkg::FSQRT; // fsqrt.fmt - FP Square Root
1214
+ // rs2 must be zero
1215
+ if (instr.rftype.rs2 != 5'b00000) illegal_instr = 1'b1;
1216
+ end
1217
+ 5'b00100: begin
1218
+ instruction_o.op = ariane_pkg::FSGNJ; // fsgn{j[n]/jx}.fmt - FP Sign Injection
1219
+ check_fprm = 1'b0; // instruction encoded in rm, do the check here
1220
+ if (CVA6Cfg.XF16ALT) begin // FP16ALT instructions encoded in rm separately (static)
1221
+ if (!(instr.rftype.rm inside {[3'b000 : 3'b010], [3'b100 : 3'b110]}))
1222
+ illegal_instr = 1'b1;
1223
+ end else begin
1224
+ if (!(instr.rftype.rm inside {[3'b000 : 3'b010]})) illegal_instr = 1'b1;
1225
+ end
1226
+ end
1227
+ 5'b00101: begin
1228
+ instruction_o.op = ariane_pkg::FMIN_MAX; // fmin/fmax.fmt - FP Minimum / Maximum
1229
+ check_fprm = 1'b0; // instruction encoded in rm, do the check here
1230
+ if (CVA6Cfg.XF16ALT) begin // FP16ALT instructions encoded in rm separately (static)
1231
+ if (!(instr.rftype.rm inside {[3'b000 : 3'b001], [3'b100 : 3'b101]}))
1232
+ illegal_instr = 1'b1;
1233
+ end else begin
1234
+ if (!(instr.rftype.rm inside {[3'b000 : 3'b001]})) illegal_instr = 1'b1;
1235
+ end
1236
+ end
1237
+ 5'b01000: begin
1238
+ instruction_o.op = ariane_pkg::FCVT_F2F; // fcvt.fmt.fmt - FP to FP Conversion
1239
+ instruction_o.rs2 = instr.rvftype.rs1; // tie rs2 to rs1 to be safe (vectors use rs2)
1240
+ imm_select = IIMM; // rs2 holds part of the intruction
1241
+ if (|instr.rftype.rs2[24:23])
1242
+ illegal_instr = 1'b1; // bits [22:20] used, other bits must be 0
1243
+ // check source format
1244
+ unique case (instr.rftype.rs2[22:20])
1245
+ // Only process instruction if corresponding extension is active (static)
1246
+ 3'b000: if (~CVA6Cfg.RVF) illegal_instr = 1'b1;
1247
+ 3'b001: if (~CVA6Cfg.RVD) illegal_instr = 1'b1;
1248
+ 3'b010: if (~CVA6Cfg.XF16) illegal_instr = 1'b1;
1249
+ 3'b110: if (~CVA6Cfg.XF16ALT) illegal_instr = 1'b1;
1250
+ 3'b011: if (~CVA6Cfg.XF8) illegal_instr = 1'b1;
1251
+ default: illegal_instr = 1'b1;
1252
+ endcase
1253
+ end
1254
+ 5'b10100: begin
1255
+ instruction_o.op = ariane_pkg::FCMP; // feq/flt/fle.fmt - FP Comparisons
1256
+ check_fprm = 1'b0; // instruction encoded in rm, do the check here
1257
+ if (CVA6Cfg.XF16ALT) begin // FP16ALT instructions encoded in rm separately (static)
1258
+ if (!(instr.rftype.rm inside {[3'b000 : 3'b010], [3'b100 : 3'b110]}))
1259
+ illegal_instr = 1'b1;
1260
+ end else begin
1261
+ if (!(instr.rftype.rm inside {[3'b000 : 3'b010]})) illegal_instr = 1'b1;
1262
+ end
1263
+ end
1264
+ 5'b11000: begin
1265
+ instruction_o.op = ariane_pkg::FCVT_F2I; // fcvt.ifmt.fmt - FP to Int Conversion
1266
+ imm_select = IIMM; // rs2 holds part of the instruction
1267
+ if (|instr.rftype.rs2[24:22])
1268
+ illegal_instr = 1'b1; // bits [21:20] used, other bits must be 0
1269
+ end
1270
+ 5'b11010: begin
1271
+ instruction_o.op = ariane_pkg::FCVT_I2F; // fcvt.fmt.ifmt - Int to FP Conversion
1272
+ imm_select = IIMM; // rs2 holds part of the instruction
1273
+ if (|instr.rftype.rs2[24:22])
1274
+ illegal_instr = 1'b1; // bits [21:20] used, other bits must be 0
1275
+ end
1276
+ 5'b11100: begin
1277
+ instruction_o.rs2 = instr.rftype.rs1; // set rs2 = rs1 so we can map FMV to SGNJ in the unit
1278
+ check_fprm = 1'b0; // instruction encoded in rm, do the check here
1279
+ if (instr.rftype.rm == 3'b000 || (CVA6Cfg.XF16ALT && instr.rftype.rm == 3'b100)) // FP16ALT has separate encoding
1280
+ instruction_o.op = ariane_pkg::FMV_F2X; // fmv.ifmt.fmt - FPR to GPR Move
1281
+ else if (instr.rftype.rm == 3'b001 || (CVA6Cfg.XF16ALT && instr.rftype.rm == 3'b101)) // FP16ALT has separate encoding
1282
+ instruction_o.op = ariane_pkg::FCLASS; // fclass.fmt - FP Classify
1283
+ else illegal_instr = 1'b1;
1284
+ // rs2 must be zero
1285
+ if (instr.rftype.rs2 != 5'b00000) illegal_instr = 1'b1;
1286
+ end
1287
+ 5'b11110: begin
1288
+ instruction_o.op = ariane_pkg::FMV_X2F; // fmv.fmt.ifmt - GPR to FPR Move
1289
+ instruction_o.rs2 = instr.rftype.rs1; // set rs2 = rs1 so we can map FMV to SGNJ in the unit
1290
+ check_fprm = 1'b0; // instruction encoded in rm, do the check here
1291
+ if (!(instr.rftype.rm == 3'b000 || (CVA6Cfg.XF16ALT && instr.rftype.rm == 3'b100)))
1292
+ illegal_instr = 1'b1;
1293
+ // rs2 must be zero
1294
+ if (instr.rftype.rs2 != 5'b00000) illegal_instr = 1'b1;
1295
+ end
1296
+ default: illegal_instr = 1'b1;
1297
+ endcase
1298
+
1299
+ // check format
1300
+ unique case (instr.rftype.fmt)
1301
+ // Only process instruction if corresponding extension is active (static)
1302
+ 2'b00: if (~CVA6Cfg.RVF) illegal_instr = 1'b1;
1303
+ 2'b01: if (~CVA6Cfg.RVD) illegal_instr = 1'b1;
1304
+ 2'b10: if (~CVA6Cfg.XF16 & ~CVA6Cfg.XF16ALT) illegal_instr = 1'b1;
1305
+ 2'b11: if (~CVA6Cfg.XF8) illegal_instr = 1'b1;
1306
+ default: illegal_instr = 1'b1;
1307
+ endcase
1308
+
1309
+ // check rounding mode
1310
+ if (check_fprm) begin
1311
+ unique case (instr.rftype.rm) inside
1312
+ [3'b000 : 3'b100]: ; //legal rounding modes
1313
+ 3'b101: begin // Alternative Half-Precsision encded as fmt=10 and rm=101
1314
+ if (~CVA6Cfg.XF16ALT || instr.rftype.fmt != 2'b10) illegal_instr = 1'b1;
1315
+ unique case (frm_i) inside // actual rounding mode from frm csr
1316
+ [3'b000 : 3'b100]: ; //legal rounding modes
1317
+ default: illegal_instr = 1'b1;
1318
+ endcase
1319
+ end
1320
+ 3'b111: begin
1321
+ // rounding mode from frm csr
1322
+ unique case (frm_i) inside
1323
+ [3'b000 : 3'b100]: ; //legal rounding modes
1324
+ default: illegal_instr = 1'b1;
1325
+ endcase
1326
+ end
1327
+ default: illegal_instr = 1'b1;
1328
+ endcase
1329
+ end
1330
+ end else begin
1331
+ illegal_instr = 1'b1;
1332
+ end
1333
+ end
1334
+
1335
+ // ----------------------------------
1336
+ // Atomic Operations
1337
+ // ----------------------------------
1338
+ riscv::OpcodeAmo: begin
1339
+ // we are going to use the load unit for AMOs
1340
+ instruction_o.fu = STORE;
1341
+ instruction_o.rs1 = instr.atype.rs1;
1342
+ instruction_o.rs2 = instr.atype.rs2;
1343
+ instruction_o.rd = instr.atype.rd;
1344
+ // TODO(zarubaf): Ordering
1345
+ // words
1346
+ if (CVA6Cfg.RVA && instr.stype.funct3 == 3'h2) begin
1347
+ unique case (instr.instr[31:27])
1348
+ 5'h0: instruction_o.op = ariane_pkg::AMO_ADDW;
1349
+ 5'h1: instruction_o.op = ariane_pkg::AMO_SWAPW;
1350
+ 5'h2: begin
1351
+ instruction_o.op = ariane_pkg::AMO_LRW;
1352
+ if (instr.atype.rs2 != 0) illegal_instr = 1'b1;
1353
+ end
1354
+ 5'h3: instruction_o.op = ariane_pkg::AMO_SCW;
1355
+ 5'h4: instruction_o.op = ariane_pkg::AMO_XORW;
1356
+ 5'h8: instruction_o.op = ariane_pkg::AMO_ORW;
1357
+ 5'hC: instruction_o.op = ariane_pkg::AMO_ANDW;
1358
+ 5'h10: instruction_o.op = ariane_pkg::AMO_MINW;
1359
+ 5'h14: instruction_o.op = ariane_pkg::AMO_MAXW;
1360
+ 5'h18: instruction_o.op = ariane_pkg::AMO_MINWU;
1361
+ 5'h1C: instruction_o.op = ariane_pkg::AMO_MAXWU;
1362
+ default: illegal_instr = 1'b1;
1363
+ endcase
1364
+ // double words
1365
+ end else if (CVA6Cfg.IS_XLEN64 && CVA6Cfg.RVA && instr.stype.funct3 == 3'h3) begin
1366
+ unique case (instr.instr[31:27])
1367
+ 5'h0: instruction_o.op = ariane_pkg::AMO_ADDD;
1368
+ 5'h1: instruction_o.op = ariane_pkg::AMO_SWAPD;
1369
+ 5'h2: begin
1370
+ instruction_o.op = ariane_pkg::AMO_LRD;
1371
+ if (instr.atype.rs2 != 0) illegal_instr = 1'b1;
1372
+ end
1373
+ 5'h3: instruction_o.op = ariane_pkg::AMO_SCD;
1374
+ 5'h4: instruction_o.op = ariane_pkg::AMO_XORD;
1375
+ 5'h8: instruction_o.op = ariane_pkg::AMO_ORD;
1376
+ 5'hC: instruction_o.op = ariane_pkg::AMO_ANDD;
1377
+ 5'h10: instruction_o.op = ariane_pkg::AMO_MIND;
1378
+ 5'h14: instruction_o.op = ariane_pkg::AMO_MAXD;
1379
+ 5'h18: instruction_o.op = ariane_pkg::AMO_MINDU;
1380
+ 5'h1C: instruction_o.op = ariane_pkg::AMO_MAXDU;
1381
+ default: illegal_instr = 1'b1;
1382
+ endcase
1383
+ end else begin
1384
+ illegal_instr = 1'b1;
1385
+ end
1386
+ if (CVA6Cfg.RVH) begin
1387
+ tinst = {
1388
+ instr.atype.funct5,
1389
+ instr.atype.aq,
1390
+ instr.atype.rl,
1391
+ instr.atype.rs2,
1392
+ 5'b0,
1393
+ instr.atype.funct3,
1394
+ instr.atype.rd,
1395
+ instr.atype.opcode
1396
+ };
1397
+ end
1398
+ end
1399
+
1400
+ // --------------------------------
1401
+ // Control Flow Instructions
1402
+ // --------------------------------
1403
+ riscv::OpcodeBranch: begin
1404
+ imm_select = SBIMM;
1405
+ instruction_o.fu = CTRL_FLOW;
1406
+ instruction_o.rs1 = instr.stype.rs1;
1407
+ instruction_o.rs2 = instr.stype.rs2;
1408
+
1409
+ is_control_flow_instr_o = 1'b1;
1410
+
1411
+ case (instr.stype.funct3)
1412
+ 3'b000: instruction_o.op = ariane_pkg::EQ;
1413
+ 3'b001: instruction_o.op = ariane_pkg::NE;
1414
+ 3'b100: instruction_o.op = ariane_pkg::LTS;
1415
+ 3'b101: instruction_o.op = ariane_pkg::GES;
1416
+ 3'b110: instruction_o.op = ariane_pkg::LTU;
1417
+ 3'b111: instruction_o.op = ariane_pkg::GEU;
1418
+ default: begin
1419
+ is_control_flow_instr_o = 1'b0;
1420
+ illegal_instr = 1'b1;
1421
+ end
1422
+ endcase
1423
+ end
1424
+ // Jump and link register
1425
+ riscv::OpcodeJalr: begin
1426
+ instruction_o.fu = CTRL_FLOW;
1427
+ instruction_o.op = ariane_pkg::JALR;
1428
+ instruction_o.rs1 = instr.itype.rs1;
1429
+ imm_select = IIMM;
1430
+ instruction_o.rd = instr.itype.rd;
1431
+ is_control_flow_instr_o = 1'b1;
1432
+ // invalid jump and link register -> reserved for vector encoding
1433
+ if (instr.itype.funct3 != 3'b0) illegal_instr = 1'b1;
1434
+ end
1435
+ // Jump and link
1436
+ riscv::OpcodeJal: begin
1437
+ instruction_o.fu = CTRL_FLOW;
1438
+ imm_select = JIMM;
1439
+ instruction_o.rd = instr.utype.rd;
1440
+ is_control_flow_instr_o = 1'b1;
1441
+ end
1442
+
1443
+ riscv::OpcodeAuipc: begin
1444
+ instruction_o.fu = ALU;
1445
+ imm_select = UIMM;
1446
+ instruction_o.use_pc = 1'b1;
1447
+ instruction_o.rd = instr.utype.rd;
1448
+ end
1449
+
1450
+ riscv::OpcodeLui: begin
1451
+ imm_select = UIMM;
1452
+ instruction_o.fu = ALU;
1453
+ instruction_o.rd = instr.utype.rd;
1454
+ end
1455
+
1456
+ default: illegal_instr = 1'b1;
1457
+ endcase
1458
+ end
1459
+ if (CVA6Cfg.CvxifEn) begin
1460
+ if (~ex_i.valid && (is_illegal_i || illegal_instr)) begin
1461
+ instruction_o.fu = CVXIF;
1462
+ instruction_o.rs1 = instr.r4type.rs1;
1463
+ instruction_o.rs2 = instr.r4type.rs2;
1464
+ instruction_o.rd = instr.r4type.rd;
1465
+ instruction_o.op = ariane_pkg::OFFLOAD;
1466
+ imm_select = instr.rtype.opcode == riscv::OpcodeMadd ||
1467
+ instr.rtype.opcode == riscv::OpcodeMsub ||
1468
+ instr.rtype.opcode == riscv::OpcodeNmadd ||
1469
+ instr.rtype.opcode == riscv::OpcodeNmsub ? RS3 : MUX_RD_RS3;
1470
+ end
1471
+ end
1472
+
1473
+ // Accelerator instructions.
1474
+ // These can overwrite the previous decoding entirely.
1475
+ if (CVA6Cfg.EnableAccelerator) begin // only generate decoder if accelerators are enabled (static)
1476
+ if (is_accel) begin
1477
+ instruction_o.fu = acc_instruction.fu;
1478
+ instruction_o.vfp = acc_instruction.vfp;
1479
+ instruction_o.rs1 = acc_instruction.rs1;
1480
+ instruction_o.rs2 = acc_instruction.rs2;
1481
+ instruction_o.rd = acc_instruction.rd;
1482
+ instruction_o.op = acc_instruction.op;
1483
+ illegal_instr = acc_illegal_instr;
1484
+ is_control_flow_instr_o = acc_is_control_flow_instr;
1485
+ end
1486
+ end
1487
+ end
1488
+
1489
+ // --------------------------------
1490
+ // Sign extend immediate
1491
+ // --------------------------------
1492
+ always_comb begin : sign_extend
1493
+ imm_i_type = {{CVA6Cfg.XLEN - 12{instruction_i[31]}}, instruction_i[31:20]};
1494
+ imm_s_type = {
1495
+ {CVA6Cfg.XLEN - 12{instruction_i[31]}}, instruction_i[31:25], instruction_i[11:7]
1496
+ };
1497
+ imm_sb_type = {
1498
+ {CVA6Cfg.XLEN - 13{instruction_i[31]}},
1499
+ instruction_i[31],
1500
+ instruction_i[7],
1501
+ instruction_i[30:25],
1502
+ instruction_i[11:8],
1503
+ 1'b0
1504
+ };
1505
+ imm_u_type = {
1506
+ {CVA6Cfg.XLEN - 32{instruction_i[31]}}, instruction_i[31:12], 12'b0
1507
+ }; // JAL, AUIPC, sign extended to 64 bit
1508
+ // if zcmt then xlen jump address assign to immidiate
1509
+ if (CVA6Cfg.RVZCMT && is_zcmt_i) begin
1510
+ imm_uj_type = {{CVA6Cfg.XLEN - 32{jump_address_i[31]}}, jump_address_i[31:0]};
1511
+ end else begin
1512
+ imm_uj_type = {
1513
+ {CVA6Cfg.XLEN - 20{instruction_i[31]}},
1514
+ instruction_i[19:12],
1515
+ instruction_i[20],
1516
+ instruction_i[30:21],
1517
+ 1'b0
1518
+ };
1519
+ end
1520
+
1521
+ // NOIMM, IIMM, SIMM, SBIMM, UIMM, JIMM, RS3
1522
+ // select immediate
1523
+ case (imm_select)
1524
+ IIMM: begin
1525
+ instruction_o.result = imm_i_type;
1526
+ instruction_o.use_imm = 1'b1;
1527
+ end
1528
+ SIMM: begin
1529
+ instruction_o.result = imm_s_type;
1530
+ instruction_o.use_imm = 1'b1;
1531
+ end
1532
+ SBIMM: begin
1533
+ instruction_o.result = imm_sb_type;
1534
+ instruction_o.use_imm = 1'b1;
1535
+ end
1536
+ UIMM: begin
1537
+ instruction_o.result = imm_u_type;
1538
+ instruction_o.use_imm = 1'b1;
1539
+ end
1540
+ JIMM: begin
1541
+ instruction_o.result = imm_uj_type;
1542
+ instruction_o.use_imm = 1'b1;
1543
+ end
1544
+ RS3: begin
1545
+ // result holds address of fp operand rs3
1546
+ instruction_o.result = {{CVA6Cfg.XLEN - 5{1'b0}}, instr.r4type.rs3};
1547
+ instruction_o.use_imm = 1'b0;
1548
+ end
1549
+ MUX_RD_RS3: begin
1550
+ // result holds address of operand rs3 which is in rd field
1551
+ instruction_o.result = {{CVA6Cfg.XLEN - 5{1'b0}}, instr.rtype.rd};
1552
+ instruction_o.use_imm = 1'b0;
1553
+ end
1554
+ default: begin
1555
+ instruction_o.result = {CVA6Cfg.XLEN{1'b0}};
1556
+ instruction_o.use_imm = 1'b0;
1557
+ end
1558
+ endcase
1559
+
1560
+ if (CVA6Cfg.EnableAccelerator) begin
1561
+ if (is_accel) begin
1562
+ instruction_o.result = acc_instruction.result;
1563
+ instruction_o.use_imm = acc_instruction.use_imm;
1564
+ end
1565
+ end
1566
+ end
1567
+
1568
+ // ---------------------
1569
+ // Exception handling
1570
+ // ---------------------
1571
+ logic [CVA6Cfg.XLEN-1:0] interrupt_cause;
1572
+
1573
+ // this instruction has already executed if the exception is valid
1574
+ assign instruction_o.valid = instruction_o.ex.valid;
1575
+
1576
+ always_comb begin : exception_handling
1577
+ interrupt_cause = '0;
1578
+ instruction_o.ex = ex_i;
1579
+ orig_instr_o = '0;
1580
+ // look if we didn't already get an exception in any previous
1581
+ // stage - we should not overwrite it as we retain order regarding the exception
1582
+ if (~ex_i.valid) begin
1583
+ // if we didn't already get an exception save the instruction here as we may need it
1584
+ // in the commit stage if we got a access exception to one of the CSR registers
1585
+ if (CVA6Cfg.CvxifEn || CVA6Cfg.RVF)
1586
+ orig_instr_o = (is_compressed_i) ? {{CVA6Cfg.XLEN-16{1'b0}}, compressed_instr_i} : {{CVA6Cfg.XLEN-32{1'b0}}, instruction_i};
1587
+ if (CVA6Cfg.TvalEn)
1588
+ instruction_o.ex.tval = (is_compressed_i) ? {{CVA6Cfg.XLEN-16{1'b0}}, compressed_instr_i} : {{CVA6Cfg.XLEN-32{1'b0}}, instruction_i};
1589
+ else instruction_o.ex.tval = '0;
1590
+ if (CVA6Cfg.RVH) instruction_o.ex.tinst = tinst;
1591
+ else instruction_o.ex.tinst = '0;
1592
+ // instructions which will throw an exception are marked as valid
1593
+ // e.g.: they can be committed anytime and do not need to wait for any functional unit
1594
+ // check here if we decoded an invalid instruction or if the compressed decoder already decoded
1595
+ // a invalid instruction
1596
+ if (illegal_instr || is_illegal_i) begin
1597
+ if (!CVA6Cfg.CvxifEn) instruction_o.ex.valid = 1'b1;
1598
+ // we decoded an illegal exception here
1599
+ instruction_o.ex.cause = riscv::ILLEGAL_INSTR;
1600
+ end else if (CVA6Cfg.RVH && virtual_illegal_instr) begin
1601
+ instruction_o.ex.valid = 1'b1;
1602
+ // we decoded an virtual illegal exception here
1603
+ instruction_o.ex.cause = riscv::VIRTUAL_INSTRUCTION;
1604
+ // we got an ecall, set the correct cause depending on the current privilege level
1605
+ end else if (ecall) begin
1606
+ // this exception is valid
1607
+ instruction_o.ex.valid = 1'b1;
1608
+ // depending on the privilege mode, set the appropriate cause
1609
+ if (priv_lvl_i == riscv::PRIV_LVL_S && CVA6Cfg.RVS) begin
1610
+ instruction_o.ex.cause = (CVA6Cfg.RVH && v_i) ? riscv::ENV_CALL_VSMODE : riscv::ENV_CALL_SMODE;
1611
+ end else if (priv_lvl_i == riscv::PRIV_LVL_U && CVA6Cfg.RVU) begin
1612
+ instruction_o.ex.cause = riscv::ENV_CALL_UMODE;
1613
+ end else if (priv_lvl_i == riscv::PRIV_LVL_M) begin
1614
+ instruction_o.ex.cause = riscv::ENV_CALL_MMODE;
1615
+ end
1616
+ end else if (ebreak) begin
1617
+ // this exception is valid
1618
+ instruction_o.ex.valid = 1'b1;
1619
+ // set breakpoint cause
1620
+ instruction_o.ex.cause = riscv::BREAKPOINT;
1621
+ // set gva bit
1622
+ if (CVA6Cfg.RVH) instruction_o.ex.gva = v_i;
1623
+ else instruction_o.ex.gva = 1'b0;
1624
+ end
1625
+ // -----------------
1626
+ // Interrupt Control
1627
+ // -----------------
1628
+ // we decode an interrupt the same as an exception, hence it will be taken if the instruction did not
1629
+ // throw any previous exception.
1630
+ // we have three interrupt sources: external interrupts, software interrupts, timer interrupts (order of precedence)
1631
+ // for two privilege levels: Supervisor and Machine Mode
1632
+ // Virtual Supervisor Timer Interrupt
1633
+ if (CVA6Cfg.RVH) begin
1634
+ if (irq_ctrl_i.mie[riscv::IRQ_VS_TIMER] && irq_ctrl_i.mip[riscv::IRQ_VS_TIMER]) begin
1635
+ interrupt_cause = INTERRUPTS.VS_TIMER;
1636
+ end
1637
+ // Virtual Supervisor Software Interrupt
1638
+ if (irq_ctrl_i.mie[riscv::IRQ_VS_SOFT] && irq_ctrl_i.mip[riscv::IRQ_VS_SOFT]) begin
1639
+ interrupt_cause = INTERRUPTS.VS_SW;
1640
+ end
1641
+ // Virtual Supervisor External Interrupt
1642
+ if (irq_ctrl_i.mie[riscv::IRQ_VS_EXT] && (irq_ctrl_i.mip[riscv::IRQ_VS_EXT])) begin
1643
+ interrupt_cause = INTERRUPTS.VS_EXT;
1644
+ end
1645
+ // Hypervisor Guest External Interrupts
1646
+ if (irq_ctrl_i.mie[riscv::IRQ_HS_EXT] && irq_ctrl_i.mip[riscv::IRQ_HS_EXT]) begin
1647
+ interrupt_cause = INTERRUPTS.HS_EXT;
1648
+ end
1649
+ end
1650
+ if (CVA6Cfg.RVS) begin
1651
+ // Supervisor Timer Interrupt
1652
+ if (irq_ctrl_i.mie[riscv::IRQ_S_TIMER] && irq_ctrl_i.mip[riscv::IRQ_S_TIMER]) begin
1653
+ interrupt_cause = INTERRUPTS.S_TIMER;
1654
+ end
1655
+ // Supervisor Software Interrupt
1656
+ if (irq_ctrl_i.mie[riscv::IRQ_S_SOFT] && irq_ctrl_i.mip[riscv::IRQ_S_SOFT]) begin
1657
+ interrupt_cause = INTERRUPTS.S_SW;
1658
+ end
1659
+ // Supervisor External Interrupt
1660
+ // The logical-OR of the software-writable bit and the signal from the external interrupt controller is
1661
+ // used to generate external interrupts to the supervisor
1662
+ if (irq_ctrl_i.mie[riscv::IRQ_S_EXT] && (irq_ctrl_i.mip[riscv::IRQ_S_EXT] | irq_i[ariane_pkg::SupervisorIrq])) begin
1663
+ interrupt_cause = INTERRUPTS.S_EXT;
1664
+ end
1665
+ end
1666
+ // Machine Timer Interrupt
1667
+ if (irq_ctrl_i.mip[riscv::IRQ_M_TIMER] && irq_ctrl_i.mie[riscv::IRQ_M_TIMER]) begin
1668
+ interrupt_cause = INTERRUPTS.M_TIMER;
1669
+ end
1670
+ if (CVA6Cfg.SoftwareInterruptEn) begin
1671
+ // Machine Mode Software Interrupt
1672
+ if (irq_ctrl_i.mip[riscv::IRQ_M_SOFT] && irq_ctrl_i.mie[riscv::IRQ_M_SOFT]) begin
1673
+ interrupt_cause = INTERRUPTS.M_SW;
1674
+ end
1675
+ end
1676
+ // Machine Mode External Interrupt
1677
+ if (irq_ctrl_i.mip[riscv::IRQ_M_EXT] && irq_ctrl_i.mie[riscv::IRQ_M_EXT]) begin
1678
+ interrupt_cause = INTERRUPTS.M_EXT;
1679
+ end
1680
+
1681
+ if (interrupt_cause[CVA6Cfg.XLEN-1] && irq_ctrl_i.global_enable) begin
1682
+ // However, if bit i in mideleg is set, interrupts are considered to be globally enabled if the hart’s current privilege
1683
+ // mode equals the delegated privilege mode (S or U) and that mode’s interrupt enable bit
1684
+ // (SIE or UIE in mstatus) is set, or if the current privilege mode is less than the delegated privilege mode.
1685
+ if (irq_ctrl_i.mideleg[interrupt_cause[$clog2(CVA6Cfg.XLEN)-1:0]]) begin
1686
+ if (CVA6Cfg.RVH) begin : hyp_int_gen
1687
+ if (v_i && irq_ctrl_i.hideleg[interrupt_cause[$clog2(CVA6Cfg.XLEN)-1:0]]) begin
1688
+ if ((irq_ctrl_i.sie && priv_lvl_i == riscv::PRIV_LVL_S) || priv_lvl_i == riscv::PRIV_LVL_U) begin
1689
+ instruction_o.ex.valid = 1'b1;
1690
+ instruction_o.ex.cause = interrupt_cause;
1691
+ end
1692
+ end else if (v_i && ~irq_ctrl_i.hideleg[interrupt_cause[$clog2(
1693
+ CVA6Cfg.XLEN
1694
+ )-1:0]]) begin
1695
+ instruction_o.ex.valid = 1'b1;
1696
+ instruction_o.ex.cause = interrupt_cause;
1697
+ end else if (!v_i && ((irq_ctrl_i.sie && priv_lvl_i == riscv::PRIV_LVL_S) || priv_lvl_i == riscv::PRIV_LVL_U) && ~irq_ctrl_i.hideleg[interrupt_cause[$clog2(
1698
+ CVA6Cfg.XLEN
1699
+ )-1:0]]) begin
1700
+ instruction_o.ex.valid = 1'b1;
1701
+ instruction_o.ex.cause = interrupt_cause;
1702
+ end
1703
+ end else begin
1704
+ if ((CVA6Cfg.RVS && irq_ctrl_i.sie && priv_lvl_i == riscv::PRIV_LVL_S) || (CVA6Cfg.RVU && priv_lvl_i == riscv::PRIV_LVL_U)) begin
1705
+ instruction_o.ex.valid = 1'b1;
1706
+ instruction_o.ex.cause = interrupt_cause;
1707
+ end
1708
+ end
1709
+ end else begin
1710
+ instruction_o.ex.valid = 1'b1;
1711
+ instruction_o.ex.cause = interrupt_cause;
1712
+ end
1713
+ end
1714
+ end
1715
+
1716
+ // a debug request has precendece over everything else
1717
+ if (CVA6Cfg.DebugEn && debug_req_i && !debug_mode_i) begin
1718
+ instruction_o.ex.valid = 1'b1;
1719
+ instruction_o.ex.cause = riscv::DEBUG_REQUEST;
1720
+ end
1721
+ end
1722
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/ex_stage.sv ADDED
@@ -0,0 +1,725 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ // Copyright 2018 ETH Zurich and University of Bologna.
3
+ // Copyright and related rights are licensed under the Solderpad Hardware
4
+ // License, Version 0.51 (the "License"); you may not use this file except in
5
+ // compliance with the License. You may obtain a copy of the License at
6
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
7
+ // or agreed to in writing, software, hardware and materials distributed under
8
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ // specific language governing permissions and limitations under the License.
11
+ //
12
+ // Author: Florian Zaruba, ETH Zurich
13
+ // Date: 19.04.2017
14
+ // Description: Instantiation of all functional units residing in the execute stage
15
+
16
+
17
+ module ex_stage
18
+ import ariane_pkg::*;
19
+ #(
20
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
21
+ parameter type bp_resolve_t = logic,
22
+ parameter type branchpredict_sbe_t = logic,
23
+ parameter type dcache_req_i_t = logic,
24
+ parameter type dcache_req_o_t = logic,
25
+ parameter type exception_t = logic,
26
+ parameter type fu_data_t = logic,
27
+ parameter type icache_areq_t = logic,
28
+ parameter type icache_arsp_t = logic,
29
+ parameter type icache_dreq_t = logic,
30
+ parameter type icache_drsp_t = logic,
31
+ parameter type lsu_ctrl_t = logic,
32
+ parameter type x_result_t = logic,
33
+ parameter type acc_mmu_req_t = logic,
34
+ parameter type acc_mmu_resp_t = logic
35
+ ) (
36
+ // Subsystem Clock - SUBSYSTEM
37
+ input logic clk_i,
38
+ // Asynchronous reset active low - SUBSYSTEM
39
+ input logic rst_ni,
40
+ // Fetch flush request - CONTROLLER
41
+ input logic flush_i,
42
+ // Debug mode is enabled - CSR_REGFILE
43
+ input logic debug_mode_i,
44
+ // rs1 forwarding - ISSUE_STAGE
45
+ input logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.VLEN-1:0] rs1_forwarding_i,
46
+ // rs2 forwarding - ISSUE_STAGE
47
+ input logic [CVA6Cfg.NrIssuePorts-1:0][CVA6Cfg.VLEN-1:0] rs2_forwarding_i,
48
+ // FU data useful to execute instruction - ISSUE_STAGE
49
+ input fu_data_t [CVA6Cfg.NrIssuePorts-1:0] fu_data_i,
50
+ // PC of the current instruction - ISSUE_STAGE
51
+ input logic [CVA6Cfg.VLEN-1:0] pc_i,
52
+ // Is_zcmt instruction - ISSUE_STAGE
53
+ input logic is_zcmt_i,
54
+ // Report whether instruction is compressed - ISSUE_STAGE
55
+ input logic is_compressed_instr_i,
56
+ // Report instruction encoding - ISSUE_STAGE
57
+ input logic [CVA6Cfg.NrIssuePorts-1:0][31:0] tinst_i,
58
+ // Fixed Latency Unit result - ISSUE_STAGE
59
+ output logic [CVA6Cfg.XLEN-1:0] flu_result_o,
60
+ // ID of the scoreboard entry at which a=to write back - ISSUE_STAGE
61
+ output logic [CVA6Cfg.TRANS_ID_BITS-1:0] flu_trans_id_o,
62
+ // Fixed Latency Unit exception - ISSUE_STAGE
63
+ output exception_t flu_exception_o,
64
+ // FLU is ready - ISSUE_STAGE
65
+ output logic flu_ready_o,
66
+ // FLU result is valid - ISSUE_STAGE
67
+ output logic flu_valid_o,
68
+ // ALU instruction is valid - ISSUE_STAGE
69
+ input logic [CVA6Cfg.NrIssuePorts-1:0] alu_valid_i,
70
+ // Branch unit instruction is valid - ISSUE_STAGE
71
+ input logic [CVA6Cfg.NrIssuePorts-1:0] branch_valid_i,
72
+ // Information of branch prediction - ISSUE_STAGE
73
+ input branchpredict_sbe_t branch_predict_i,
74
+ // The branch engine uses the write back from the ALU - several_modules
75
+ output bp_resolve_t resolved_branch_o,
76
+ // Signaling that we resolved the branch - ISSUE_STAGE
77
+ output logic resolve_branch_o,
78
+ // CSR instruction is valid - ISSUE_STAGE
79
+ input logic [CVA6Cfg.NrIssuePorts-1:0] csr_valid_i,
80
+ // CSR address to write - COMMIT_STAGE
81
+ output logic [11:0] csr_addr_o,
82
+ // CSR commit - COMMIT_STAGE
83
+ input logic csr_commit_i,
84
+ // MULT instruction is valid - ISSUE_STAGE
85
+ input logic [CVA6Cfg.NrIssuePorts-1:0] mult_valid_i,
86
+ // LSU is ready - ISSUE_STAGE
87
+ output logic lsu_ready_o,
88
+ // LSU instruction is valid - ISSUE_STAGE
89
+ input logic [CVA6Cfg.NrIssuePorts-1:0] lsu_valid_i,
90
+ // Load result is valid - ISSUE_STAGE
91
+ output logic load_valid_o,
92
+ // Load result valid - ISSUE_STAGE
93
+ output logic [CVA6Cfg.XLEN-1:0] load_result_o,
94
+ // Load instruction ID - ISSUE_STAGE
95
+ output logic [CVA6Cfg.TRANS_ID_BITS-1:0] load_trans_id_o,
96
+ // Exception generated by load instruction - ISSUE_STAGE
97
+ output exception_t load_exception_o,
98
+ // Store result is valid - ISSUe_STAGE
99
+ output logic store_valid_o,
100
+ // Store result - ISSUE_STAGE
101
+ output logic [CVA6Cfg.XLEN-1:0] store_result_o,
102
+ // Store instruction ID - ISSUE_STAGE
103
+ output logic [CVA6Cfg.TRANS_ID_BITS-1:0] store_trans_id_o,
104
+ // Exception generated by store instruction - ISSUE_STAGE
105
+ output exception_t store_exception_o,
106
+ // LSU commit - COMMIT_STAGE
107
+ input logic lsu_commit_i,
108
+ // Commit queue ready to accept another commit request - COMMIT_STAGE
109
+ output logic lsu_commit_ready_o,
110
+ // Commit transaction ID - COMMIT_STAGE
111
+ input logic [CVA6Cfg.TRANS_ID_BITS-1:0] commit_tran_id_i,
112
+ // TO_BE_COMPLETED - ACC_DISPATCHER
113
+ input logic stall_st_pending_i,
114
+ // TO_BE_COMPLETED - COMMIT_STAGE
115
+ output logic no_st_pending_o,
116
+ // Atomic result is valid - COMMIT_STAGE
117
+ input logic amo_valid_commit_i,
118
+ // FU is ready - ISSUE_STAGE
119
+ output logic fpu_ready_o,
120
+ // FPU instruction is ready - ISSUE_STAGE
121
+ input logic [CVA6Cfg.NrIssuePorts-1:0] fpu_valid_i,
122
+ // FPU format - ISSUE_STAGE
123
+ input logic [1:0] fpu_fmt_i,
124
+ // FPU rm - ISSUE_STAGE
125
+ input logic [2:0] fpu_rm_i,
126
+ // FPU frm - ISSUE_STAGE
127
+ input logic [2:0] fpu_frm_i,
128
+ // FPU precision control - CSR_REGFILE
129
+ input logic [6:0] fpu_prec_i,
130
+ // FPU transaction ID - ISSUE_STAGE
131
+ output logic [CVA6Cfg.TRANS_ID_BITS-1:0] fpu_trans_id_o,
132
+ // FPU result - ISSUE_STAGE
133
+ output logic [CVA6Cfg.XLEN-1:0] fpu_result_o,
134
+ // FPU valid - ISSUE_STAGE
135
+ output logic fpu_valid_o,
136
+ // FPU exception - ISSUE_STAGE
137
+ output exception_t fpu_exception_o,
138
+ // ALU2 instruction is valid - ISSUE_STAGE
139
+ input logic [CVA6Cfg.NrIssuePorts-1:0] alu2_valid_i,
140
+ // CVXIF instruction is valid - ISSUE_STAGE
141
+ input logic [CVA6Cfg.NrIssuePorts-1:0] x_valid_i,
142
+ // CVXIF is ready - ISSUE_STAGE
143
+ output logic x_ready_o,
144
+ // CVXIF undecoded instruction
145
+ input logic [31:0] x_off_instr_i,
146
+ // CVXIF transaction ID - ISSUE_STAGE
147
+ output logic [CVA6Cfg.TRANS_ID_BITS-1:0] x_trans_id_o,
148
+ // CVXIF exception - ISSUE_STAGE
149
+ output exception_t x_exception_o,
150
+ // CVXIF result - ISSUE_STAGE
151
+ output logic [CVA6Cfg.XLEN-1:0] x_result_o,
152
+ // CVXIF result valid - ISSUE_STAGE
153
+ output logic x_valid_o,
154
+ // CVXIF write enable - ISSUE_STAGE
155
+ output logic x_we_o,
156
+ // CVXIF destination register - ISSUE_STAGE
157
+ output logic [4:0] x_rd_o,
158
+ // CVXIF Result interface - SUBSYSTEM
159
+ input logic x_result_valid_i,
160
+ input x_result_t x_result_i,
161
+ output logic x_result_ready_o,
162
+ // CVXIF Issue transaction rejected -> illegal instruction - ISSUE_STAGE
163
+ input logic x_transaction_rejected_i,
164
+ // accelerate port result is valid - ACC_DISPATCHER
165
+ input logic acc_valid_i,
166
+ // Accelerator MMU access
167
+ input acc_mmu_req_t acc_mmu_req_i,
168
+ output acc_mmu_resp_t acc_mmu_resp_o,
169
+ // Enable virtual memory translation - CSR_REGFILE
170
+ input logic enable_translation_i,
171
+ // Enable G-Stage memory translation - CSR_REGFILE
172
+ input logic enable_g_translation_i,
173
+ // Enable virtual memory translation for load/stores - CSR_REGFILE
174
+ input logic en_ld_st_translation_i,
175
+ // Enable G-Stage memory translation for load/stores - CSR_REGFILE
176
+ input logic en_ld_st_g_translation_i,
177
+ // Flush TLB - CONTROLLER
178
+ input logic flush_tlb_i,
179
+ input logic flush_tlb_vvma_i,
180
+ input logic flush_tlb_gvma_i,
181
+ // Privilege mode - CSR_REGFILE
182
+ input riscv::priv_lvl_t priv_lvl_i,
183
+ // Virtualization mode - CSR_REGFILE
184
+ input logic v_i,
185
+ // Privilege level at which load and stores should happen - CSR_REGFILE
186
+ input riscv::priv_lvl_t ld_st_priv_lvl_i,
187
+ // Virtualization mode at which load and stores should happen - CSR_REGFILE
188
+ input logic ld_st_v_i,
189
+ // Instruction is hypervisor load/store - CSR_REGFILE
190
+ output logic csr_hs_ld_st_inst_o,
191
+ // Supervisor user memory - CSR_REGFILE
192
+ input logic sum_i,
193
+ // Virtual Supervisor user memory - CSR_REGFILE
194
+ input logic vs_sum_i,
195
+ // Make executable readable - CSR_REGFILE
196
+ input logic mxr_i,
197
+ // Make executable readable Virtual Supervisor - CSR_REGFILE
198
+ input logic vmxr_i,
199
+ // TO_BE_COMPLETED - CSR_REGFILE
200
+ input logic [CVA6Cfg.PPNW-1:0] satp_ppn_i,
201
+ // TO_BE_COMPLETED - CSR_REGFILE
202
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] asid_i,
203
+ // TO_BE_COMPLETED - CSR_REGFILE
204
+ input logic [CVA6Cfg.PPNW-1:0] vsatp_ppn_i,
205
+ // TO_BE_COMPLETED - CSR_REGFILE
206
+ input logic [CVA6Cfg.ASID_WIDTH-1:0] vs_asid_i,
207
+ // TO_BE_COMPLETED - CSR_REGFILE
208
+ input logic [CVA6Cfg.PPNW-1:0] hgatp_ppn_i,
209
+ // TO_BE_COMPLETED - CSR_REGFILE
210
+ input logic [CVA6Cfg.VMID_WIDTH-1:0] vmid_i,
211
+ // icache translation response - CACHE
212
+ input icache_arsp_t icache_areq_i,
213
+ // icache translation request - CACHE
214
+ output icache_areq_t icache_areq_o,
215
+ // Data cache request ouput - CACHE
216
+ input dcache_req_o_t [2:0] dcache_req_ports_i,
217
+ // Data cache request input - CACHE
218
+ output dcache_req_i_t [2:0] dcache_req_ports_o,
219
+ // Write buffer is empty - CACHE
220
+ input logic dcache_wbuffer_empty_i,
221
+ // TO_BE_COMPLETED - CACHE
222
+ input logic dcache_wbuffer_not_ni_i,
223
+ // AMO request - CACHE
224
+ output amo_req_t amo_req_o,
225
+ // AMO response - CACHE
226
+ input amo_resp_t amo_resp_i,
227
+ // To count the instruction TLB misses - PERF_COUNTERS
228
+ output logic itlb_miss_o,
229
+ // To count the data TLB misses - PERF_COUNTERS
230
+ output logic dtlb_miss_o,
231
+ // Report the PMP configuration - CSR_REGFILE
232
+ input riscv::pmpcfg_t [avoid_neg(CVA6Cfg.NrPMPEntries-1):0] pmpcfg_i,
233
+ // Report the PMP addresses - CSR_REGFILE
234
+ input logic [avoid_neg(CVA6Cfg.NrPMPEntries-1):0][CVA6Cfg.PLEN-3:0] pmpaddr_i,
235
+ // Information dedicated to RVFI - RVFI
236
+ output lsu_ctrl_t rvfi_lsu_ctrl_o,
237
+ // Information dedicated to RVFI - RVFI
238
+ output [CVA6Cfg.PLEN-1:0] rvfi_mem_paddr_o
239
+ );
240
+
241
+ // -------------------------
242
+ // Fixed Latency Units
243
+ // -------------------------
244
+ // all fixed latency units share a single issue port and a sing write
245
+ // port into the scoreboard. At the moment those are:
246
+ // 1. ALU - all operations are single cycle
247
+ // 2. Branch unit: operation is single cycle, the ALU is needed
248
+ // for comparison
249
+ // 3. CSR: This is a small buffer which saves the address of the CSR.
250
+ // The value is then re-fetched once the instruction retires. The buffer
251
+ // is only a single entry deep, hence this operation will block all
252
+ // other operations once this buffer is full. This should not be a major
253
+ // concern though as CSRs are infrequent.
254
+ // 4. Multiplier/Divider: The multiplier has a fixed latency of 1 cycle.
255
+ // The issue logic will take care of not issuing
256
+ // another instruction if it will collide on the
257
+ // output port. Divisions are arbitrary in length
258
+ // they will simply block the issue of all other
259
+ // instructions.
260
+
261
+
262
+ logic current_instruction_is_sfence_vma;
263
+ logic current_instruction_is_hfence_vvma;
264
+ logic current_instruction_is_hfence_gvma;
265
+ // These two register store the rs1 and rs2 parameters in case of `SFENCE_VMA`
266
+ // instruction to be used for TLB flush in the next clock cycle.
267
+ logic [CVA6Cfg.VMID_WIDTH-1:0] vmid_to_be_flushed;
268
+ logic [CVA6Cfg.ASID_WIDTH-1:0] asid_to_be_flushed;
269
+ logic [CVA6Cfg.VLEN-1:0] vaddr_to_be_flushed;
270
+ logic [CVA6Cfg.GPLEN-1:0] gpaddr_to_be_flushed;
271
+
272
+ // from ALU to branch unit
273
+ logic alu_branch_res; // branch comparison result
274
+ logic [CVA6Cfg.XLEN-1:0] alu_result, csr_result, mult_result;
275
+ logic [CVA6Cfg.VLEN-1:0] branch_result;
276
+ logic csr_ready, mult_ready;
277
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] mult_trans_id;
278
+ logic mult_valid;
279
+
280
+ logic [CVA6Cfg.NrIssuePorts-1:0] one_cycle_select;
281
+ assign one_cycle_select = alu_valid_i | branch_valid_i | csr_valid_i;
282
+
283
+ fu_data_t one_cycle_data;
284
+ logic [CVA6Cfg.VLEN-1:0] rs1_forwarding;
285
+ logic [CVA6Cfg.VLEN-1:0] rs2_forwarding;
286
+ always_comb begin
287
+ // data silence operation
288
+ one_cycle_data = one_cycle_select[0] ? fu_data_i[0] : '0;
289
+ rs1_forwarding = rs1_forwarding_i[0];
290
+ rs2_forwarding = rs2_forwarding_i[0];
291
+
292
+ if (CVA6Cfg.SuperscalarEn) begin
293
+ if (one_cycle_select[1]) begin
294
+ one_cycle_data = fu_data_i[1];
295
+ rs1_forwarding = rs1_forwarding_i[1];
296
+ rs2_forwarding = rs2_forwarding_i[1];
297
+ end
298
+ end
299
+ end
300
+
301
+ // 1. ALU (combinatorial)
302
+ alu #(
303
+ .CVA6Cfg (CVA6Cfg),
304
+ .HasBranch(1'b1),
305
+ .fu_data_t(fu_data_t)
306
+ ) alu_i (
307
+ .clk_i,
308
+ .rst_ni,
309
+ .fu_data_i (one_cycle_data),
310
+ .result_o (alu_result),
311
+ .alu_branch_res_o(alu_branch_res)
312
+ );
313
+
314
+ // 2. Branch Unit (combinatorial)
315
+ // we don't silence the branch unit as this is already critical and we do
316
+ // not want to add another layer of logic
317
+ branch_unit #(
318
+ .CVA6Cfg(CVA6Cfg),
319
+ .bp_resolve_t(bp_resolve_t),
320
+ .branchpredict_sbe_t(branchpredict_sbe_t),
321
+ .exception_t(exception_t),
322
+ .fu_data_t(fu_data_t)
323
+ ) branch_unit_i (
324
+ .clk_i,
325
+ .rst_ni,
326
+ .v_i,
327
+ .debug_mode_i,
328
+ .fu_data_i (one_cycle_data),
329
+ .pc_i,
330
+ .is_zcmt_i,
331
+ .is_compressed_instr_i,
332
+ .branch_valid_i (|branch_valid_i),
333
+ .branch_comp_res_i (alu_branch_res),
334
+ .branch_result_o (branch_result),
335
+ .branch_predict_i,
336
+ .resolved_branch_o,
337
+ .resolve_branch_o,
338
+ .branch_exception_o(flu_exception_o)
339
+ );
340
+
341
+ // 3. CSR (sequential)
342
+ csr_buffer #(
343
+ .CVA6Cfg (CVA6Cfg),
344
+ .fu_data_t(fu_data_t)
345
+ ) csr_buffer_i (
346
+ .clk_i,
347
+ .rst_ni,
348
+ .flush_i,
349
+ .fu_data_i (one_cycle_data),
350
+ .csr_valid_i (|csr_valid_i),
351
+ .csr_ready_o (csr_ready),
352
+ .csr_result_o(csr_result),
353
+ .csr_commit_i,
354
+ .csr_addr_o
355
+ );
356
+
357
+ assign flu_valid_o = |one_cycle_select | mult_valid;
358
+
359
+ // result MUX
360
+ always_comb begin
361
+ // Branch result as default case
362
+ flu_result_o = {{CVA6Cfg.XLEN - CVA6Cfg.VLEN{1'b0}}, branch_result};
363
+ flu_trans_id_o = one_cycle_data.trans_id;
364
+ // ALU result
365
+ if (|alu_valid_i) begin
366
+ flu_result_o = alu_result;
367
+ // CSR result
368
+ end else if (|csr_valid_i) begin
369
+ flu_result_o = csr_result;
370
+ end else if (mult_valid) begin
371
+ flu_result_o = mult_result;
372
+ flu_trans_id_o = mult_trans_id;
373
+ end
374
+ end
375
+
376
+ // ready flags for FLU
377
+ always_comb begin
378
+ flu_ready_o = csr_ready & mult_ready;
379
+ end
380
+
381
+ // 4. Multiplication (Sequential)
382
+ fu_data_t mult_data;
383
+ // input silencing of multiplier
384
+ always_comb begin
385
+ mult_data = mult_valid_i[0] ? fu_data_i[0] : '0;
386
+ if (CVA6Cfg.SuperscalarEn) begin
387
+ if (mult_valid_i[1]) begin
388
+ mult_data = fu_data_i[1];
389
+ end
390
+ end
391
+ end
392
+
393
+ mult #(
394
+ .CVA6Cfg (CVA6Cfg),
395
+ .fu_data_t(fu_data_t)
396
+ ) i_mult (
397
+ .clk_i,
398
+ .rst_ni,
399
+ .flush_i,
400
+ .mult_valid_i (|mult_valid_i),
401
+ .fu_data_i (mult_data),
402
+ .result_o (mult_result),
403
+ .mult_valid_o (mult_valid),
404
+ .mult_ready_o (mult_ready),
405
+ .mult_trans_id_o(mult_trans_id)
406
+ );
407
+
408
+ // ----------------
409
+ // FPU
410
+ // ----------------
411
+ logic fpu_valid;
412
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] fpu_trans_id;
413
+ logic [CVA6Cfg.XLEN-1:0] fpu_result;
414
+ logic alu2_valid;
415
+ logic [CVA6Cfg.XLEN-1:0] alu2_result;
416
+
417
+ generate
418
+ if (CVA6Cfg.FpPresent) begin : fpu_gen
419
+ fu_data_t fpu_data;
420
+ always_comb begin
421
+ fpu_data = fpu_valid_i[0] ? fu_data_i[0] : '0;
422
+ if (CVA6Cfg.SuperscalarEn) begin
423
+ if (fpu_valid_i[1]) begin
424
+ fpu_data = fu_data_i[1];
425
+ end
426
+ end
427
+ end
428
+
429
+ fpu_wrap #(
430
+ .CVA6Cfg(CVA6Cfg),
431
+ .exception_t(exception_t),
432
+ .fu_data_t(fu_data_t)
433
+ ) fpu_i (
434
+ .clk_i,
435
+ .rst_ni,
436
+ .flush_i,
437
+ .fpu_valid_i(|fpu_valid_i),
438
+ .fpu_ready_o,
439
+ .fu_data_i(fpu_data),
440
+ .fpu_fmt_i,
441
+ .fpu_rm_i,
442
+ .fpu_frm_i,
443
+ .fpu_prec_i,
444
+ .fpu_trans_id_o(fpu_trans_id),
445
+ .result_o(fpu_result),
446
+ .fpu_valid_o(fpu_valid),
447
+ .fpu_exception_o
448
+ );
449
+ end else begin : no_fpu_gen
450
+ assign fpu_ready_o = '0;
451
+ assign fpu_trans_id = '0;
452
+ assign fpu_result = '0;
453
+ assign fpu_valid = '0;
454
+ assign fpu_exception_o = '0;
455
+ end
456
+ endgenerate
457
+
458
+ // ----------------
459
+ // ALU2
460
+ // ----------------
461
+ fu_data_t alu2_data;
462
+ if (CVA6Cfg.SuperscalarEn) begin : alu2_gen
463
+ always_comb begin
464
+ alu2_data = alu2_valid_i[0] ? fu_data_i[0] : '0;
465
+ if (alu2_valid_i[1]) begin
466
+ alu2_data = fu_data_i[1];
467
+ end
468
+ end
469
+
470
+ alu #(
471
+ .CVA6Cfg (CVA6Cfg),
472
+ .HasBranch(1'b0),
473
+ .fu_data_t(fu_data_t)
474
+ ) alu2_i (
475
+ .clk_i,
476
+ .rst_ni,
477
+ .fu_data_i (alu2_data),
478
+ .result_o (alu2_result),
479
+ .alu_branch_res_o( /* this ALU does not handle branching */)
480
+ );
481
+ end else begin
482
+ assign alu2_data = '0;
483
+ assign alu2_result = '0;
484
+ end
485
+
486
+ // result MUX
487
+ // This is really explicit so that synthesis tools can elide unused signals
488
+ if (CVA6Cfg.SuperscalarEn) begin
489
+ if (CVA6Cfg.FpPresent) begin
490
+ assign fpu_valid_o = fpu_valid || |alu2_valid_i;
491
+ assign fpu_result_o = fpu_valid ? fpu_result : alu2_result;
492
+ assign fpu_trans_id_o = fpu_valid ? fpu_trans_id : alu2_data.trans_id;
493
+ end else begin
494
+ assign fpu_valid_o = |alu2_valid_i;
495
+ assign fpu_result_o = alu2_result;
496
+ assign fpu_trans_id_o = alu2_data.trans_id;
497
+ end
498
+ end else begin
499
+ if (CVA6Cfg.FpPresent) begin
500
+ assign fpu_valid_o = fpu_valid;
501
+ assign fpu_result_o = fpu_result;
502
+ assign fpu_trans_id_o = fpu_trans_id;
503
+ end else begin
504
+ assign fpu_valid_o = '0;
505
+ assign fpu_result_o = '0;
506
+ assign fpu_trans_id_o = '0;
507
+ end
508
+ end
509
+
510
+ // ----------------
511
+ // Load-Store Unit
512
+ // ----------------
513
+ fu_data_t lsu_data;
514
+ logic [31:0] lsu_tinst;
515
+ always_comb begin
516
+ lsu_data = lsu_valid_i[0] ? fu_data_i[0] : '0;
517
+ lsu_tinst = tinst_i[0];
518
+
519
+ if (CVA6Cfg.SuperscalarEn) begin
520
+ if (lsu_valid_i[1]) begin
521
+ lsu_data = fu_data_i[1];
522
+ lsu_tinst = tinst_i[1];
523
+ end
524
+ end
525
+ end
526
+
527
+ load_store_unit #(
528
+ .CVA6Cfg (CVA6Cfg),
529
+ .dcache_req_i_t(dcache_req_i_t),
530
+ .dcache_req_o_t(dcache_req_o_t),
531
+ .exception_t(exception_t),
532
+ .fu_data_t (fu_data_t),
533
+ .icache_areq_t(icache_areq_t),
534
+ .icache_arsp_t(icache_arsp_t),
535
+ .icache_dreq_t(icache_dreq_t),
536
+ .icache_drsp_t(icache_drsp_t),
537
+ .lsu_ctrl_t(lsu_ctrl_t),
538
+ .acc_mmu_req_t(acc_mmu_req_t),
539
+ .acc_mmu_resp_t(acc_mmu_resp_t)
540
+ ) lsu_i (
541
+ .clk_i,
542
+ .rst_ni,
543
+ .flush_i,
544
+ .stall_st_pending_i,
545
+ .no_st_pending_o,
546
+ .fu_data_i (lsu_data),
547
+ .lsu_ready_o,
548
+ .lsu_valid_i (|lsu_valid_i),
549
+ .load_trans_id_o,
550
+ .load_result_o,
551
+ .load_valid_o,
552
+ .load_exception_o,
553
+ .store_trans_id_o,
554
+ .store_result_o,
555
+ .store_valid_o,
556
+ .store_exception_o,
557
+ .commit_i (lsu_commit_i),
558
+ .commit_ready_o (lsu_commit_ready_o),
559
+ .commit_tran_id_i,
560
+ .enable_translation_i,
561
+ .enable_g_translation_i,
562
+ .en_ld_st_translation_i,
563
+ .en_ld_st_g_translation_i,
564
+ .acc_mmu_req_i,
565
+ .acc_mmu_resp_o,
566
+ .icache_areq_i,
567
+ .icache_areq_o,
568
+ .priv_lvl_i,
569
+ .v_i,
570
+ .ld_st_priv_lvl_i,
571
+ .ld_st_v_i,
572
+ .csr_hs_ld_st_inst_o,
573
+ .sum_i,
574
+ .vs_sum_i,
575
+ .mxr_i,
576
+ .vmxr_i,
577
+ .satp_ppn_i,
578
+ .vsatp_ppn_i,
579
+ .hgatp_ppn_i,
580
+ .asid_i,
581
+ .vs_asid_i,
582
+ .asid_to_be_flushed_i (asid_to_be_flushed),
583
+ .vmid_i,
584
+ .vmid_to_be_flushed_i (vmid_to_be_flushed),
585
+ .vaddr_to_be_flushed_i (vaddr_to_be_flushed),
586
+ .gpaddr_to_be_flushed_i(gpaddr_to_be_flushed),
587
+ .flush_tlb_i,
588
+ .flush_tlb_vvma_i,
589
+ .flush_tlb_gvma_i,
590
+ .itlb_miss_o,
591
+ .dtlb_miss_o,
592
+ .dcache_req_ports_i,
593
+ .dcache_req_ports_o,
594
+ .dcache_wbuffer_empty_i,
595
+ .dcache_wbuffer_not_ni_i,
596
+ .amo_valid_commit_i,
597
+ .amo_req_o,
598
+ .amo_resp_i,
599
+ .tinst_i (lsu_tinst),
600
+ .pmpcfg_i,
601
+ .pmpaddr_i,
602
+ .rvfi_lsu_ctrl_o,
603
+ .rvfi_mem_paddr_o
604
+ );
605
+
606
+ if (CVA6Cfg.CvxifEn) begin : gen_cvxif
607
+ fu_data_t cvxif_data;
608
+ always_comb begin
609
+ cvxif_data = x_valid_i[0] ? fu_data_i[0] : '0;
610
+ if (CVA6Cfg.SuperscalarEn) begin
611
+ if (x_valid_i[1]) begin
612
+ cvxif_data = fu_data_i[1];
613
+ end
614
+ end
615
+ end
616
+
617
+ cvxif_fu #(
618
+ .CVA6Cfg(CVA6Cfg),
619
+ .exception_t(exception_t),
620
+ .x_result_t(x_result_t)
621
+ ) cvxif_fu_i (
622
+ .clk_i,
623
+ .rst_ni,
624
+ .x_valid_i(|x_valid_i),
625
+ .x_trans_id_i(cvxif_data.trans_id),
626
+ .x_illegal_i(x_transaction_rejected_i),
627
+ .x_off_instr_i,
628
+ .x_ready_o,
629
+ .x_trans_id_o,
630
+ .x_exception_o,
631
+ .x_result_o,
632
+ .x_valid_o,
633
+ .x_we_o,
634
+ .x_rd_o,
635
+ .result_valid_i(x_result_valid_i),
636
+ .result_i(x_result_i),
637
+ .result_ready_o(x_result_ready_o)
638
+ );
639
+ end else begin : gen_no_cvxif
640
+ assign x_result_ready_o = '0;
641
+ assign x_trans_id_o = '0;
642
+ assign x_exception_o = '0;
643
+ assign x_result_o = '0;
644
+ assign x_valid_o = '0;
645
+ end
646
+
647
+ if (CVA6Cfg.RVS) begin
648
+ if (CVA6Cfg.RVH) begin
649
+ always_ff @(posedge clk_i or negedge rst_ni) begin
650
+ if (~rst_ni) begin
651
+ current_instruction_is_sfence_vma <= 1'b0;
652
+ current_instruction_is_hfence_vvma <= 1'b0;
653
+ current_instruction_is_hfence_gvma <= 1'b0;
654
+ end else begin
655
+ // TODO handle this with superscalar (issue only one instruction in this case?)
656
+ if (flush_i) begin
657
+ current_instruction_is_sfence_vma <= 1'b0;
658
+ current_instruction_is_hfence_vvma <= 1'b0;
659
+ current_instruction_is_hfence_gvma <= 1'b0;
660
+ end else if ((fu_data_i[0].operation == SFENCE_VMA && !v_i) && |csr_valid_i) begin
661
+ current_instruction_is_sfence_vma <= 1'b1;
662
+ end else if (((fu_data_i[0].operation == SFENCE_VMA && v_i) || fu_data_i[0].operation == HFENCE_VVMA) && |csr_valid_i) begin
663
+ current_instruction_is_hfence_vvma <= 1'b1;
664
+ end else if ((fu_data_i[0].operation == HFENCE_GVMA) && |csr_valid_i) begin
665
+ current_instruction_is_hfence_gvma <= 1'b1;
666
+ end
667
+ end
668
+ end
669
+ end else begin
670
+ assign current_instruction_is_hfence_vvma = 1'b0;
671
+ assign current_instruction_is_hfence_gvma = 1'b0;
672
+ always_ff @(posedge clk_i or negedge rst_ni) begin
673
+ if (~rst_ni) begin
674
+ current_instruction_is_sfence_vma <= 1'b0;
675
+ end else begin
676
+ if (flush_i) begin
677
+ current_instruction_is_sfence_vma <= 1'b0;
678
+ end else if (fu_data_i[0].operation == SFENCE_VMA && |csr_valid_i) begin
679
+ current_instruction_is_sfence_vma <= 1'b1;
680
+ end
681
+ end
682
+ end
683
+ end
684
+ if (CVA6Cfg.RVH) begin
685
+ // This process stores the rs1 and rs2 parameters of a SFENCE_VMA instruction.
686
+ always_ff @(posedge clk_i or negedge rst_ni) begin
687
+ if (~rst_ni) begin
688
+ vmid_to_be_flushed <= '0;
689
+ asid_to_be_flushed <= '0;
690
+ vaddr_to_be_flushed <= '0;
691
+ gpaddr_to_be_flushed <= '0;
692
+ // if the current instruction in EX_STAGE is a sfence.vma, in the next cycle no writes will happen
693
+ end else if ((~(current_instruction_is_sfence_vma || current_instruction_is_hfence_vvma || current_instruction_is_hfence_gvma)) && (~((fu_data_i[0].operation == SFENCE_VMA || fu_data_i[0].operation == HFENCE_VVMA || fu_data_i[0].operation == HFENCE_GVMA ) && |csr_valid_i))) begin
694
+ vaddr_to_be_flushed <= rs1_forwarding;
695
+ gpaddr_to_be_flushed <= {2'b00, rs1_forwarding[CVA6Cfg.GPLEN-1:2]};
696
+ asid_to_be_flushed <= rs2_forwarding[CVA6Cfg.ASID_WIDTH-1:0];
697
+ vmid_to_be_flushed <= rs2_forwarding[CVA6Cfg.VMID_WIDTH-1:0];
698
+ end
699
+ end
700
+ end else begin
701
+ assign vmid_to_be_flushed = '0;
702
+ assign gpaddr_to_be_flushed = '0;
703
+ // This process stores the rs1 and rs2 parameters of a SFENCE_VMA instruction.
704
+ always_ff @(posedge clk_i or negedge rst_ni) begin
705
+ if (~rst_ni) begin
706
+ asid_to_be_flushed <= '0;
707
+ vaddr_to_be_flushed <= '0;
708
+ // if the current instruction in EX_STAGE is a sfence.vma, in the next cycle no writes will happen
709
+ end else if ((~current_instruction_is_sfence_vma) && (~((fu_data_i[0].operation == SFENCE_VMA) && |csr_valid_i))) begin
710
+ vaddr_to_be_flushed <= rs1_forwarding;
711
+ asid_to_be_flushed <= rs2_forwarding[CVA6Cfg.ASID_WIDTH-1:0];
712
+ end
713
+ end
714
+ end
715
+ end else begin
716
+ assign current_instruction_is_sfence_vma = 1'b0;
717
+ assign current_instruction_is_hfence_vvma = 1'b0;
718
+ assign current_instruction_is_hfence_gvma = 1'b0;
719
+ assign asid_to_be_flushed = '0;
720
+ assign vaddr_to_be_flushed = '0;
721
+ assign vmid_to_be_flushed = '0;
722
+ assign gpaddr_to_be_flushed = '0;
723
+ end
724
+
725
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/fpu_wrap.sv ADDED
@@ -0,0 +1,571 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Author: Stefan Mach, ETH Zurich
12
+ // Date: 12.04.2018
13
+ // Description: Wrapper for the floating-point unit
14
+
15
+
16
+ module fpu_wrap
17
+ import ariane_pkg::*;
18
+ #(
19
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
20
+ parameter type exception_t = logic,
21
+ parameter type fu_data_t = logic
22
+ ) (
23
+ input logic clk_i,
24
+ input logic rst_ni,
25
+ input logic flush_i,
26
+ input logic fpu_valid_i,
27
+ output logic fpu_ready_o,
28
+ input fu_data_t fu_data_i,
29
+
30
+ input logic [ 1:0] fpu_fmt_i,
31
+ input logic [ 2:0] fpu_rm_i,
32
+ input logic [ 2:0] fpu_frm_i,
33
+ input logic [ 6:0] fpu_prec_i,
34
+ output logic [CVA6Cfg.TRANS_ID_BITS-1:0] fpu_trans_id_o,
35
+ output logic [ CVA6Cfg.FLen-1:0] result_o,
36
+ output logic fpu_valid_o,
37
+ output exception_t fpu_exception_o
38
+ );
39
+
40
+ // this is a workaround
41
+ // otherwise compilation might issue an error if FLEN=0
42
+ enum logic {
43
+ READY,
44
+ STALL
45
+ }
46
+ state_q, state_d;
47
+ if (CVA6Cfg.FpPresent) begin : fpu_gen
48
+ logic [CVA6Cfg.FLen-1:0] operand_a_i;
49
+ logic [CVA6Cfg.FLen-1:0] operand_b_i;
50
+ logic [CVA6Cfg.FLen-1:0] operand_c_i;
51
+ assign operand_a_i = fu_data_i.operand_a[CVA6Cfg.FLen-1:0];
52
+ assign operand_b_i = fu_data_i.operand_b[CVA6Cfg.FLen-1:0];
53
+ assign operand_c_i = fu_data_i.imm[CVA6Cfg.FLen-1:0];
54
+
55
+ //-----------------------------------
56
+ // FPnew config from FPnew package
57
+ //-----------------------------------
58
+ localparam OPBITS = fpnew_pkg::OP_BITS;
59
+ localparam FMTBITS = $clog2(fpnew_pkg::NUM_FP_FORMATS);
60
+ localparam IFMTBITS = $clog2(fpnew_pkg::NUM_INT_FORMATS);
61
+
62
+ // Features (enabled formats, vectors etc.)
63
+ localparam fpnew_pkg::fpu_features_t FPU_FEATURES = '{
64
+ Width: unsigned'(CVA6Cfg.FLen), // parameterized using CVA6Cfg.FLen
65
+ EnableVectors: CVA6Cfg.XFVec,
66
+ EnableNanBox: 1'b1,
67
+ FpFmtMask: {CVA6Cfg.RVF, CVA6Cfg.RVD, CVA6Cfg.XF16, CVA6Cfg.XF8, CVA6Cfg.XF16ALT},
68
+ IntFmtMask: {
69
+ CVA6Cfg.XFVec && CVA6Cfg.XF8,
70
+ CVA6Cfg.XFVec && (CVA6Cfg.XF16 || CVA6Cfg.XF16ALT),
71
+ 1'b1,
72
+ 1'b1
73
+ }
74
+ };
75
+
76
+ // Implementation (number of registers etc)
77
+ localparam fpnew_pkg::fpu_implementation_t FPU_IMPLEMENTATION = '{
78
+ PipeRegs: '{ // FP32, FP64, FP16, FP8, FP16alt
79
+ '{
80
+ unsigned'(LAT_COMP_FP32),
81
+ unsigned'(LAT_COMP_FP64),
82
+ unsigned'(LAT_COMP_FP16),
83
+ unsigned'(LAT_COMP_FP8),
84
+ unsigned'(LAT_COMP_FP16ALT)
85
+ }, // ADDMUL
86
+ '{default: unsigned'(LAT_DIVSQRT)}, // DIVSQRT
87
+ '{default: unsigned'(LAT_NONCOMP)}, // NONCOMP
88
+ '{default: unsigned'(LAT_CONV)}
89
+ }, // CONV
90
+ UnitTypes: '{
91
+ '{default: fpnew_pkg::PARALLEL}, // ADDMUL
92
+ '{default: fpnew_pkg::MERGED}, // DIVSQRT
93
+ '{default: fpnew_pkg::PARALLEL}, // NONCOMP
94
+ '{default: fpnew_pkg::MERGED}
95
+ }, // CONV
96
+ PipeConfig: fpnew_pkg::DISTRIBUTED
97
+ };
98
+
99
+ //-------------------------------------------------
100
+ // Inputs to the FPU and protocol inversion buffer
101
+ //-------------------------------------------------
102
+ logic [CVA6Cfg.FLen-1:0] operand_a_d, operand_a_q, operand_a;
103
+ logic [CVA6Cfg.FLen-1:0] operand_b_d, operand_b_q, operand_b;
104
+ logic [CVA6Cfg.FLen-1:0] operand_c_d, operand_c_q, operand_c;
105
+ logic [OPBITS-1:0] fpu_op_d, fpu_op_q, fpu_op;
106
+ logic fpu_op_mod_d, fpu_op_mod_q, fpu_op_mod;
107
+ logic [FMTBITS-1:0] fpu_srcfmt_d, fpu_srcfmt_q, fpu_srcfmt;
108
+ logic [FMTBITS-1:0] fpu_dstfmt_d, fpu_dstfmt_q, fpu_dstfmt;
109
+ logic [IFMTBITS-1:0] fpu_ifmt_d, fpu_ifmt_q, fpu_ifmt;
110
+ logic [2:0] fpu_rm_d, fpu_rm_q, fpu_rm;
111
+ logic fpu_vec_op_d, fpu_vec_op_q, fpu_vec_op;
112
+
113
+ logic [CVA6Cfg.TRANS_ID_BITS-1:0] fpu_tag_d, fpu_tag_q, fpu_tag;
114
+
115
+ logic fpu_in_ready, fpu_in_valid;
116
+ logic fpu_out_ready, fpu_out_valid;
117
+
118
+ logic [4:0] fpu_status;
119
+
120
+ // FSM to handle protocol inversion
121
+ logic hold_inputs;
122
+ logic use_hold;
123
+
124
+ //-----------------------------
125
+ // Translate inputs
126
+ //-----------------------------
127
+
128
+ always_comb begin : input_translation
129
+
130
+ automatic logic vec_replication; // control honoring of replication flag
131
+ automatic logic replicate_c; // replicate operand C instead of B (for ADD/SUB)
132
+ automatic logic check_ah; // Decide for AH from RM field encoding
133
+
134
+ // Default Values
135
+ operand_a_d = operand_a_i;
136
+ operand_b_d = operand_b_i; // immediates come through this port unless used as operand
137
+ operand_c_d = operand_c_i; // immediates come through this port unless used as operand
138
+ fpu_op_d = fpnew_pkg::SGNJ; // sign injection by default
139
+ fpu_op_mod_d = 1'b0;
140
+ fpu_dstfmt_d = fpnew_pkg::FP32;
141
+ fpu_ifmt_d = fpnew_pkg::INT32;
142
+ fpu_rm_d = fpu_rm_i;
143
+ fpu_vec_op_d = fu_data_i.fu == FPU_VEC;
144
+ fpu_tag_d = fu_data_i.trans_id;
145
+ vec_replication = fpu_rm_i[0]; // replication bit is sent via rm field
146
+ replicate_c = 1'b0;
147
+ check_ah = 1'b0; // whether set scalar AH encoding from MSB of rm_i
148
+
149
+ // Scalar Rounding Modes - some ops encode inside RM but use smaller range
150
+ if (!(fpu_rm_i inside {[3'b000 : 3'b100]})) fpu_rm_d = fpu_frm_i;
151
+
152
+ // Vectorial ops always consult FRM
153
+ if (fpu_vec_op_d) fpu_rm_d = fpu_frm_i;
154
+
155
+ // Formats
156
+ unique case (fpu_fmt_i)
157
+ // FP32
158
+ 2'b00: fpu_dstfmt_d = fpnew_pkg::FP32;
159
+ // FP64 or FP16ALT (vectorial)
160
+ 2'b01: fpu_dstfmt_d = fpu_vec_op_d ? fpnew_pkg::FP16ALT : fpnew_pkg::FP64;
161
+ // FP16 or FP16ALT (scalar)
162
+ 2'b10: begin
163
+ if (!fpu_vec_op_d && fpu_rm_i == 3'b101) fpu_dstfmt_d = fpnew_pkg::FP16ALT;
164
+ else fpu_dstfmt_d = fpnew_pkg::FP16;
165
+ end
166
+ // FP8
167
+ default: fpu_dstfmt_d = fpnew_pkg::FP8;
168
+ endcase
169
+
170
+ // By default, set src=dst
171
+ fpu_srcfmt_d = fpu_dstfmt_d;
172
+
173
+ // Operations (this can modify the rounding mode field and format!)
174
+ unique case (fu_data_i.operation)
175
+ // Addition
176
+ FADD: begin
177
+ fpu_op_d = fpnew_pkg::ADD;
178
+ replicate_c = 1'b1; // second operand is in C
179
+ end
180
+ // Subtraction is modified ADD
181
+ FSUB: begin
182
+ fpu_op_d = fpnew_pkg::ADD;
183
+ fpu_op_mod_d = 1'b1;
184
+ replicate_c = 1'b1; // second operand is in C
185
+ end
186
+ // Multiplication
187
+ FMUL: fpu_op_d = fpnew_pkg::MUL;
188
+ // Division
189
+ FDIV: fpu_op_d = fpnew_pkg::DIV;
190
+ // Min/Max - OP is encoded in rm (000-001)
191
+ FMIN_MAX: begin
192
+ fpu_op_d = fpnew_pkg::MINMAX;
193
+ fpu_rm_d = {1'b0, fpu_rm_i[1:0]}; // mask out AH encoding bit
194
+ check_ah = 1'b1; // AH has RM MSB encoding
195
+ end
196
+ // Square Root
197
+ FSQRT: fpu_op_d = fpnew_pkg::SQRT;
198
+ // Fused Multiply Add
199
+ FMADD: fpu_op_d = fpnew_pkg::FMADD;
200
+ // Fused Multiply Subtract is modified FMADD
201
+ FMSUB: begin
202
+ fpu_op_d = fpnew_pkg::FMADD;
203
+ fpu_op_mod_d = 1'b1;
204
+ end
205
+ // Fused Negated Multiply Subtract
206
+ FNMSUB: fpu_op_d = fpnew_pkg::FNMSUB;
207
+ // Fused Negated Multiply Add is modified FNMSUB
208
+ FNMADD: begin
209
+ fpu_op_d = fpnew_pkg::FNMSUB;
210
+ fpu_op_mod_d = 1'b1;
211
+ end
212
+ // Float to Int Cast - Op encoded in lowest two imm bits or rm
213
+ FCVT_F2I: begin
214
+ fpu_op_d = fpnew_pkg::F2I;
215
+ // Vectorial Ops encoded in R bit
216
+ if (fpu_vec_op_d) begin
217
+ fpu_op_mod_d = fpu_rm_i[0];
218
+ vec_replication = 1'b0; // no replication, R bit used for op
219
+ unique case (fpu_fmt_i)
220
+ 2'b00: fpu_ifmt_d = fpnew_pkg::INT32;
221
+ 2'b01, 2'b10: fpu_ifmt_d = fpnew_pkg::INT16;
222
+ 2'b11: fpu_ifmt_d = fpnew_pkg::INT8;
223
+ endcase
224
+ // Scalar casts encoded in imm
225
+ end else begin
226
+ fpu_op_mod_d = operand_c_i[0];
227
+ if (operand_c_i[1]) fpu_ifmt_d = fpnew_pkg::INT64;
228
+ else fpu_ifmt_d = fpnew_pkg::INT32;
229
+ end
230
+ end
231
+ // Int to Float Cast - Op encoded in lowest two imm bits or rm
232
+ FCVT_I2F: begin
233
+ fpu_op_d = fpnew_pkg::I2F;
234
+ // Vectorial Ops encoded in R bit
235
+ if (fpu_vec_op_d) begin
236
+ fpu_op_mod_d = fpu_rm_i[0];
237
+ vec_replication = 1'b0; // no replication, R bit used for op
238
+ unique case (fpu_fmt_i)
239
+ 2'b00: fpu_ifmt_d = fpnew_pkg::INT32;
240
+ 2'b01, 2'b10: fpu_ifmt_d = fpnew_pkg::INT16;
241
+ 2'b11: fpu_ifmt_d = fpnew_pkg::INT8;
242
+ endcase
243
+ // Scalar casts encoded in imm
244
+ end else begin
245
+ fpu_op_mod_d = operand_c_i[0];
246
+ if (operand_c_i[1]) fpu_ifmt_d = fpnew_pkg::INT64;
247
+ else fpu_ifmt_d = fpnew_pkg::INT32;
248
+ end
249
+ end
250
+ // Float to Float Cast - Source format encoded in lowest two/three imm bits
251
+ FCVT_F2F: begin
252
+ fpu_op_d = fpnew_pkg::F2F;
253
+ // Vectorial ops encoded in lowest two imm bits
254
+ if (fpu_vec_op_d) begin
255
+ vec_replication = 1'b0; // no replication for casts (not needed)
256
+ unique case (operand_c_i[1:0])
257
+ 2'b00: fpu_srcfmt_d = fpnew_pkg::FP32;
258
+ 2'b01: fpu_srcfmt_d = fpnew_pkg::FP16ALT;
259
+ 2'b10: fpu_srcfmt_d = fpnew_pkg::FP16;
260
+ 2'b11: fpu_srcfmt_d = fpnew_pkg::FP8;
261
+ endcase
262
+ // Scalar ops encoded in lowest three imm bits
263
+ end else begin
264
+ unique case (operand_c_i[2:0])
265
+ 3'b000: fpu_srcfmt_d = fpnew_pkg::FP32;
266
+ 3'b001: fpu_srcfmt_d = fpnew_pkg::FP64;
267
+ 3'b010: fpu_srcfmt_d = fpnew_pkg::FP16;
268
+ 3'b110: fpu_srcfmt_d = fpnew_pkg::FP16ALT;
269
+ 3'b011: fpu_srcfmt_d = fpnew_pkg::FP8;
270
+ default: ; // Do nothing
271
+ endcase
272
+ end
273
+ end
274
+ // Scalar Sign Injection - op encoded in rm (000-010)
275
+ FSGNJ: begin
276
+ fpu_op_d = fpnew_pkg::SGNJ;
277
+ fpu_rm_d = {1'b0, fpu_rm_i[1:0]}; // mask out AH encoding bit
278
+ check_ah = 1'b1; // AH has RM MSB encoding
279
+ end
280
+ // Move from FPR to GPR - mapped to SGNJ-passthrough since no recoding
281
+ FMV_F2X: begin
282
+ fpu_op_d = fpnew_pkg::SGNJ;
283
+ fpu_rm_d = 3'b011; // passthrough without checking nan-box
284
+ fpu_op_mod_d = 1'b1; // no NaN-Boxing
285
+ check_ah = 1'b1; // AH has RM MSB encoding
286
+ vec_replication = 1'b0; // no replication, we set second operand
287
+ end
288
+ // Move from GPR to FPR - mapped to NOP since no recoding
289
+ FMV_X2F: begin
290
+ fpu_op_d = fpnew_pkg::SGNJ;
291
+ fpu_rm_d = 3'b011; // passthrough without checking nan-box
292
+ check_ah = 1'b1; // AH has RM MSB encoding
293
+ vec_replication = 1'b0; // no replication, we set second operand
294
+ end
295
+ // Scalar Comparisons - op encoded in rm (000-010)
296
+ FCMP: begin
297
+ fpu_op_d = fpnew_pkg::CMP;
298
+ fpu_rm_d = {1'b0, fpu_rm_i[1:0]}; // mask out AH encoding bit
299
+ check_ah = 1'b1; // AH has RM MSB encoding
300
+ end
301
+ // Classification
302
+ FCLASS: begin
303
+ fpu_op_d = fpnew_pkg::CLASSIFY;
304
+ fpu_rm_d = {
305
+ 1'b0, fpu_rm_i[1:0]
306
+ }; // mask out AH encoding bit - CLASS doesn't care anyways
307
+ check_ah = 1'b1; // AH has RM MSB encoding
308
+ end
309
+ // Vectorial Minimum - set up scalar encoding in rm
310
+ VFMIN: begin
311
+ fpu_op_d = fpnew_pkg::MINMAX;
312
+ fpu_rm_d = 3'b000; // min
313
+ end
314
+ // Vectorial Maximum - set up scalar encoding in rm
315
+ VFMAX: begin
316
+ fpu_op_d = fpnew_pkg::MINMAX;
317
+ fpu_rm_d = 3'b001; // max
318
+ end
319
+ // Vectorial Sign Injection - set up scalar encoding in rm
320
+ VFSGNJ: begin
321
+ fpu_op_d = fpnew_pkg::SGNJ;
322
+ fpu_rm_d = 3'b000; // sgnj
323
+ end
324
+ // Vectorial Negated Sign Injection - set up scalar encoding in rm
325
+ VFSGNJN: begin
326
+ fpu_op_d = fpnew_pkg::SGNJ;
327
+ fpu_rm_d = 3'b001; // sgnjn
328
+ end
329
+ // Vectorial Xored Sign Injection - set up scalar encoding in rm
330
+ VFSGNJX: begin
331
+ fpu_op_d = fpnew_pkg::SGNJ;
332
+ fpu_rm_d = 3'b010; // sgnjx
333
+ end
334
+ // Vectorial Equals - set up scalar encoding in rm
335
+ VFEQ: begin
336
+ fpu_op_d = fpnew_pkg::CMP;
337
+ fpu_rm_d = 3'b010; // eq
338
+ end
339
+ // Vectorial Not Equals - set up scalar encoding in rm
340
+ VFNE: begin
341
+ fpu_op_d = fpnew_pkg::CMP;
342
+ fpu_op_mod_d = 1'b1; // invert output
343
+ fpu_rm_d = 3'b010; // eq
344
+ end
345
+ // Vectorial Less Than - set up scalar encoding in rm
346
+ VFLT: begin
347
+ fpu_op_d = fpnew_pkg::CMP;
348
+ fpu_rm_d = 3'b001; // lt
349
+ end
350
+ // Vectorial Greater or Equal - set up scalar encoding in rm
351
+ VFGE: begin
352
+ fpu_op_d = fpnew_pkg::CMP;
353
+ fpu_op_mod_d = 1'b1; // invert output
354
+ fpu_rm_d = 3'b001; // lt
355
+ end
356
+ // Vectorial Less or Equal - set up scalar encoding in rm
357
+ VFLE: begin
358
+ fpu_op_d = fpnew_pkg::CMP;
359
+ fpu_rm_d = 3'b000; // le
360
+ end
361
+ // Vectorial Greater Than - set up scalar encoding in rm
362
+ VFGT: begin
363
+ fpu_op_d = fpnew_pkg::CMP;
364
+ fpu_op_mod_d = 1'b1; // invert output
365
+ fpu_rm_d = 3'b000; // le
366
+ end
367
+ // Vectorial Convert-and-Pack from FP32, lower 4 entries
368
+ VFCPKAB_S: begin
369
+ fpu_op_d = fpnew_pkg::CPKAB;
370
+ fpu_op_mod_d = fpu_rm_i[0]; // A/B selection from R bit
371
+ vec_replication = 1'b0; // no replication, R bit used for op
372
+ fpu_srcfmt_d = fpnew_pkg::FP32; // Cast from FP32
373
+ end
374
+ // Vectorial Convert-and-Pack from FP32, upper 4 entries
375
+ VFCPKCD_S: begin
376
+ fpu_op_d = fpnew_pkg::CPKCD;
377
+ fpu_op_mod_d = fpu_rm_i[0]; // C/D selection from R bit
378
+ vec_replication = 1'b0; // no replication, R bit used for op
379
+ fpu_srcfmt_d = fpnew_pkg::FP32; // Cast from FP32
380
+ end
381
+ // Vectorial Convert-and-Pack from FP64, lower 4 entries
382
+ VFCPKAB_D: begin
383
+ fpu_op_d = fpnew_pkg::CPKAB;
384
+ fpu_op_mod_d = fpu_rm_i[0]; // A/B selection from R bit
385
+ vec_replication = 1'b0; // no replication, R bit used for op
386
+ fpu_srcfmt_d = fpnew_pkg::FP64; // Cast from FP64
387
+ end
388
+ // Vectorial Convert-and-Pack from FP64, upper 4 entries
389
+ VFCPKCD_D: begin
390
+ fpu_op_d = fpnew_pkg::CPKCD;
391
+ fpu_op_mod_d = fpu_rm_i[0]; // C/D selection from R bit
392
+ vec_replication = 1'b0; // no replication, R bit used for op
393
+ fpu_srcfmt_d = fpnew_pkg::FP64; // Cast from FP64
394
+ end
395
+ // No changes per default
396
+ default: ; //nothing
397
+ endcase
398
+
399
+ // Scalar AH encoding fixing
400
+ if (!fpu_vec_op_d && check_ah) if (fpu_rm_i[2]) fpu_dstfmt_d = fpnew_pkg::FP16ALT;
401
+
402
+ // Replication
403
+ if (fpu_vec_op_d && vec_replication) begin
404
+ if (replicate_c) begin
405
+ unique case (fpu_dstfmt_d)
406
+ fpnew_pkg::FP32: operand_c_d = CVA6Cfg.RVD ? {2{operand_c_i[31:0]}} : operand_c_i;
407
+ fpnew_pkg::FP16, fpnew_pkg::FP16ALT:
408
+ operand_c_d = CVA6Cfg.RVD ? {4{operand_c_i[15:0]}} : {2{operand_c_i[15:0]}};
409
+ fpnew_pkg::FP8:
410
+ operand_c_d = CVA6Cfg.RVD ? {8{operand_c_i[7:0]}} : {4{operand_c_i[7:0]}};
411
+ default: ; // Do nothing
412
+ endcase // fpu_dstfmt_d
413
+ end else begin
414
+ unique case (fpu_dstfmt_d)
415
+ fpnew_pkg::FP32: operand_b_d = CVA6Cfg.RVD ? {2{operand_b_i[31:0]}} : operand_b_i;
416
+ fpnew_pkg::FP16, fpnew_pkg::FP16ALT:
417
+ operand_b_d = CVA6Cfg.RVD ? {4{operand_b_i[15:0]}} : {2{operand_b_i[15:0]}};
418
+ fpnew_pkg::FP8:
419
+ operand_b_d = CVA6Cfg.RVD ? {8{operand_b_i[7:0]}} : {4{operand_b_i[7:0]}};
420
+ default: ; // Do nothing
421
+ endcase // fpu_dstfmt_d
422
+ end
423
+ end
424
+ end
425
+
426
+
427
+ //---------------------------------------------------------
428
+ // Upstream protocol inversion: InValid depends on InReady
429
+ //---------------------------------------------------------
430
+
431
+ always_comb begin : p_inputFSM
432
+ // Default Values
433
+ fpu_ready_o = 1'b0;
434
+ fpu_in_valid = 1'b0;
435
+ hold_inputs = 1'b0; // hold register disabled
436
+ use_hold = 1'b0; // inputs go directly to unit
437
+ state_d = state_q; // stay in the same state
438
+
439
+ // FSM
440
+ unique case (state_q)
441
+ // Default state, ready for instructions
442
+ READY: begin
443
+ fpu_ready_o = 1'b1; // Act as if FPU ready
444
+ fpu_in_valid = fpu_valid_i; // Forward input valid to FPU
445
+ // There is a transaction but the FPU can't handle it
446
+ if (fpu_valid_i & ~fpu_in_ready) begin
447
+ fpu_ready_o = 1'b0; // No token given to Issue
448
+ hold_inputs = 1'b1; // save inputs to the holding register
449
+ state_d = STALL; // stall future incoming requests
450
+ end
451
+ end
452
+ // We're stalling the upstream (ready=0)
453
+ STALL: begin
454
+ fpu_in_valid = 1'b1; // we have data for the FPU
455
+ use_hold = 1'b1; // the data comes from the hold reg
456
+ // Wait until it's consumed
457
+ if (fpu_in_ready) begin
458
+ fpu_ready_o = 1'b1; // Give a token to issue
459
+ state_d = READY; // accept future requests
460
+ end
461
+ end
462
+ // Default: emit default values
463
+ default: ;
464
+ endcase
465
+
466
+ // Flushing will override issue and go back to idle
467
+ if (flush_i) begin
468
+ state_d = READY;
469
+ end
470
+
471
+ end
472
+
473
+ // Buffer register and FSM state holding
474
+ always_ff @(posedge clk_i or negedge rst_ni) begin : fp_hold_reg
475
+ if (~rst_ni) begin
476
+ state_q <= READY;
477
+ operand_a_q <= '0;
478
+ operand_b_q <= '0;
479
+ operand_c_q <= '0;
480
+ fpu_op_q <= '0;
481
+ fpu_op_mod_q <= '0;
482
+ fpu_srcfmt_q <= '0;
483
+ fpu_dstfmt_q <= '0;
484
+ fpu_ifmt_q <= '0;
485
+ fpu_rm_q <= '0;
486
+ fpu_vec_op_q <= '0;
487
+ fpu_tag_q <= '0;
488
+ end else begin
489
+ state_q <= state_d;
490
+ // Hold register is [TRIGGERED] by FSM
491
+ if (hold_inputs) begin
492
+ operand_a_q <= operand_a_d;
493
+ operand_b_q <= operand_b_d;
494
+ operand_c_q <= operand_c_d;
495
+ fpu_op_q <= fpu_op_d;
496
+ fpu_op_mod_q <= fpu_op_mod_d;
497
+ fpu_srcfmt_q <= fpu_srcfmt_d;
498
+ fpu_dstfmt_q <= fpu_dstfmt_d;
499
+ fpu_ifmt_q <= fpu_ifmt_d;
500
+ fpu_rm_q <= fpu_rm_d;
501
+ fpu_vec_op_q <= fpu_vec_op_d;
502
+ fpu_tag_q <= fpu_tag_d;
503
+ end
504
+ end
505
+ end
506
+
507
+ // Select FPU input data: from register if valid data in register, else directly from input
508
+ assign operand_a = use_hold ? operand_a_q : operand_a_d;
509
+ assign operand_b = use_hold ? operand_b_q : operand_b_d;
510
+ assign operand_c = use_hold ? operand_c_q : operand_c_d;
511
+ assign fpu_op = use_hold ? fpu_op_q : fpu_op_d;
512
+ assign fpu_op_mod = use_hold ? fpu_op_mod_q : fpu_op_mod_d;
513
+ assign fpu_srcfmt = use_hold ? fpu_srcfmt_q : fpu_srcfmt_d;
514
+ assign fpu_dstfmt = use_hold ? fpu_dstfmt_q : fpu_dstfmt_d;
515
+ assign fpu_ifmt = use_hold ? fpu_ifmt_q : fpu_ifmt_d;
516
+ assign fpu_rm = use_hold ? fpu_rm_q : fpu_rm_d;
517
+ assign fpu_vec_op = use_hold ? fpu_vec_op_q : fpu_vec_op_d;
518
+ assign fpu_tag = use_hold ? fpu_tag_q : fpu_tag_d;
519
+
520
+ // Consolidate operands
521
+ logic [2:0][CVA6Cfg.FLen-1:0] fpu_operands;
522
+
523
+ assign fpu_operands[0] = operand_a;
524
+ assign fpu_operands[1] = operand_b;
525
+ assign fpu_operands[2] = operand_c;
526
+
527
+ //---------------
528
+ // FPU instance
529
+ //---------------
530
+
531
+ fpnew_top #(
532
+ .Features (FPU_FEATURES),
533
+ .Implementation(FPU_IMPLEMENTATION),
534
+ .TagType (logic [CVA6Cfg.TRANS_ID_BITS-1:0])
535
+ ) i_fpnew_bulk (
536
+ .clk_i,
537
+ .rst_ni,
538
+ .operands_i (fpu_operands),
539
+ .rnd_mode_i (fpnew_pkg::roundmode_e'(fpu_rm)),
540
+ .op_i (fpnew_pkg::operation_e'(fpu_op)),
541
+ .op_mod_i (fpu_op_mod),
542
+ .src_fmt_i (fpnew_pkg::fp_format_e'(fpu_srcfmt)),
543
+ .dst_fmt_i (fpnew_pkg::fp_format_e'(fpu_dstfmt)),
544
+ .int_fmt_i (fpnew_pkg::int_format_e'(fpu_ifmt)),
545
+ .vectorial_op_i(fpu_vec_op),
546
+ .tag_i (fpu_tag),
547
+ .simd_mask_i (1'b1),
548
+ .in_valid_i (fpu_in_valid),
549
+ .in_ready_o (fpu_in_ready),
550
+ .flush_i,
551
+ .result_o,
552
+ .status_o (fpu_status),
553
+ .tag_o (fpu_trans_id_o),
554
+ .out_valid_o (fpu_out_valid),
555
+ .out_ready_i (fpu_out_ready),
556
+ .busy_o ( /* unused */)
557
+ );
558
+
559
+ // Pack status flag into exception cause, tval ignored in wb, exception is always invalid
560
+ assign fpu_exception_o.cause = {59'h0, fpu_status};
561
+ assign fpu_exception_o.valid = 1'b0;
562
+ assign fpu_exception_o.tval = '0;
563
+
564
+ // Donwstream write port is dedicated to FPU and always ready
565
+ assign fpu_out_ready = 1'b1;
566
+
567
+ // Downstream valid from unit
568
+ assign fpu_valid_o = fpu_out_valid;
569
+
570
+ end
571
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/bht.sv ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 - 2019 ETH Zurich and University of Bologna.
2
+ // Copyright 2023 - Thales for additionnal contribution.
3
+ // Copyright 2024 - PlanV Technologies for additionnal contribution.
4
+ // Copyright and related rights are licensed under the Solderpad Hardware
5
+ // License, Version 2.0 (the "License"); you may not use this file except in
6
+ // compliance with the License. You may obtain a copy of the License at
7
+ // http://solderpad.org/licenses/SHL-2.0. Unless required by applicable law
8
+ // or agreed to in writing, software, hardware and materials distributed under
9
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
11
+ // specific language governing permissions and limitations under the License.
12
+ //
13
+ // Author: Florian Zaruba, ETH Zurich
14
+ // Date: 08.02.2018
15
+ // Migrated: Luis Vitorio Cargnini, IEEE
16
+ // Date: 09.06.2018
17
+ // FPGA optimization: Sebastien Jacq, Thales
18
+ // Date: 2023-01-30
19
+ // FPGA optimization for Altera: Angela Gonzalez, PlanV Technolgies
20
+ // Date: 2024-10-16
21
+
22
+ // branch history table - 2 bit saturation counter
23
+
24
+ module bht #(
25
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
26
+ parameter type bht_update_t = logic,
27
+ parameter int unsigned NR_ENTRIES = 1024
28
+ ) (
29
+ // Subsystem Clock - SUBSYSTEM
30
+ input logic clk_i,
31
+ // Asynchronous reset active low - SUBSYSTEM
32
+ input logic rst_ni,
33
+ // Branch prediction flush request - zero
34
+ input logic flush_bp_i,
35
+ // Debug mode state - CSR
36
+ input logic debug_mode_i,
37
+ // Virtual PC - CACHE
38
+ input logic [CVA6Cfg.VLEN-1:0] vpc_i,
39
+ // Update bht with resolved address - EXECUTE
40
+ input bht_update_t bht_update_i,
41
+ // Prediction from bht - FRONTEND
42
+ output ariane_pkg::bht_prediction_t [CVA6Cfg.INSTR_PER_FETCH-1:0] bht_prediction_o
43
+ );
44
+ // the last bit is always zero, we don't need it for indexing
45
+ localparam OFFSET = CVA6Cfg.RVC == 1'b1 ? 1 : 2;
46
+ // re-shape the branch history table
47
+ localparam NR_ROWS = NR_ENTRIES / CVA6Cfg.INSTR_PER_FETCH;
48
+ // number of bits needed to index the row
49
+ localparam ROW_ADDR_BITS = $clog2(CVA6Cfg.INSTR_PER_FETCH);
50
+ localparam ROW_INDEX_BITS = CVA6Cfg.RVC == 1'b1 ? $clog2(CVA6Cfg.INSTR_PER_FETCH) : 1;
51
+ // number of bits we should use for prediction
52
+ localparam PREDICTION_BITS = $clog2(NR_ROWS) + OFFSET + ROW_ADDR_BITS;
53
+
54
+ struct packed {
55
+ logic valid;
56
+ logic [1:0] saturation_counter;
57
+ }
58
+ bht_d[NR_ROWS-1:0][CVA6Cfg.INSTR_PER_FETCH-1:0],
59
+ bht_q[NR_ROWS-1:0][CVA6Cfg.INSTR_PER_FETCH-1:0];
60
+
61
+ logic [$clog2(NR_ROWS)-1:0] index, update_pc;
62
+ logic [ROW_INDEX_BITS-1:0] update_row_index, update_row_index_q, check_update_row_index;
63
+
64
+ assign index = vpc_i[PREDICTION_BITS-1:ROW_ADDR_BITS+OFFSET];
65
+ assign update_pc = bht_update_i.pc[PREDICTION_BITS-1:ROW_ADDR_BITS+OFFSET];
66
+ if (CVA6Cfg.RVC) begin : gen_update_row_index
67
+ assign update_row_index = bht_update_i.pc[ROW_ADDR_BITS+OFFSET-1:OFFSET];
68
+ end else begin
69
+ assign update_row_index = '0;
70
+ end
71
+
72
+ if (!CVA6Cfg.FpgaEn) begin : gen_asic_bht // ASIC TARGET
73
+
74
+ logic [1:0] saturation_counter;
75
+ // prediction assignment
76
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_bht_output
77
+ assign bht_prediction_o[i].valid = bht_q[index][i].valid;
78
+ assign bht_prediction_o[i].taken = bht_q[index][i].saturation_counter[1] == 1'b1;
79
+ end
80
+
81
+ always_comb begin : update_bht
82
+ bht_d = bht_q;
83
+ saturation_counter = bht_q[update_pc][update_row_index].saturation_counter;
84
+
85
+ if ((bht_update_i.valid && CVA6Cfg.DebugEn && !debug_mode_i) || (bht_update_i.valid && !CVA6Cfg.DebugEn)) begin
86
+ bht_d[update_pc][update_row_index].valid = 1'b1;
87
+
88
+ if (saturation_counter == 2'b11) begin
89
+ // we can safely decrease it
90
+ if (!bht_update_i.taken)
91
+ bht_d[update_pc][update_row_index].saturation_counter = saturation_counter - 1;
92
+ // then check if it saturated in the negative regime e.g.: branch not taken
93
+ end else if (saturation_counter == 2'b00) begin
94
+ // we can safely increase it
95
+ if (bht_update_i.taken)
96
+ bht_d[update_pc][update_row_index].saturation_counter = saturation_counter + 1;
97
+ end else begin // otherwise we are not in any boundaries and can decrease or increase it
98
+ if (bht_update_i.taken)
99
+ bht_d[update_pc][update_row_index].saturation_counter = saturation_counter + 1;
100
+ else bht_d[update_pc][update_row_index].saturation_counter = saturation_counter - 1;
101
+ end
102
+ end
103
+ end
104
+
105
+ always_ff @(posedge clk_i or negedge rst_ni) begin
106
+ if (!rst_ni) begin
107
+ for (int unsigned i = 0; i < NR_ROWS; i++) begin
108
+ for (int j = 0; j < CVA6Cfg.INSTR_PER_FETCH; j++) begin
109
+ bht_q[i][j] <= '0;
110
+ end
111
+ end
112
+ end else begin
113
+ // evict all entries
114
+ if (flush_bp_i) begin
115
+ for (int i = 0; i < NR_ROWS; i++) begin
116
+ for (int j = 0; j < CVA6Cfg.INSTR_PER_FETCH; j++) begin
117
+ bht_q[i][j].valid <= 1'b0;
118
+ bht_q[i][j].saturation_counter <= 2'b10;
119
+ end
120
+ end
121
+ end else begin
122
+ bht_q <= bht_d;
123
+ end
124
+ end
125
+ end
126
+
127
+ end else begin : gen_fpga_bht //FPGA TARGETS
128
+
129
+ // number of bits par word in the bram
130
+ localparam BRAM_WORD_BITS = $bits(ariane_pkg::bht_t);
131
+ logic [ROW_INDEX_BITS-1:0] row_index, row_index_q, check_row_index;
132
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] bht_ram_we, bht_ram_we_q;
133
+ logic [CVA6Cfg.INSTR_PER_FETCH*$clog2(NR_ROWS)-1:0] bht_ram_read_address_0;
134
+ logic [CVA6Cfg.INSTR_PER_FETCH*$clog2(NR_ROWS)-1:0] bht_ram_read_address_1;
135
+ logic [CVA6Cfg.INSTR_PER_FETCH*$clog2(NR_ROWS)-1:0]
136
+ bht_ram_write_address, bht_ram_write_address_q;
137
+ logic [CVA6Cfg.INSTR_PER_FETCH*BRAM_WORD_BITS-1:0] bht_ram_wdata, bht_ram_wdata_q;
138
+ logic [CVA6Cfg.INSTR_PER_FETCH*BRAM_WORD_BITS-1:0] bht_ram_rdata_0;
139
+ logic [CVA6Cfg.INSTR_PER_FETCH*BRAM_WORD_BITS-1:0] bht_ram_rdata_1;
140
+
141
+ ariane_pkg::bht_t [CVA6Cfg.INSTR_PER_FETCH-1:0] bht;
142
+ ariane_pkg::bht_t [CVA6Cfg.INSTR_PER_FETCH-1:0] bht_updated;
143
+
144
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0][1:0] bht_updated_valid;
145
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0][1:0][CVA6Cfg.VLEN-1:0] bht_updated_pc;
146
+ logic bht_update_taken, check_bht_update_taken;
147
+ logic [CVA6Cfg.VLEN-1:0] vpc_q;
148
+
149
+ if (CVA6Cfg.RVC) begin : gen_row_index
150
+ assign row_index = vpc_i[ROW_ADDR_BITS+OFFSET-1:OFFSET];
151
+ end else begin
152
+ assign row_index = '0;
153
+ end
154
+
155
+ // -------------------------
156
+ // prediction assignment & update Branch History Table
157
+ // -------------------------
158
+ always_comb begin : prediction_update_bht
159
+ bht_ram_we = '0;
160
+ bht_ram_read_address_0 = '0;
161
+ bht_ram_read_address_1 = '0;
162
+ bht_ram_write_address = '0;
163
+ bht_ram_wdata = '0;
164
+ bht_updated = '0;
165
+ bht = '0;
166
+
167
+ //Write to RAM
168
+ if (bht_update_i.valid && !debug_mode_i) begin
169
+ for (int i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
170
+ if (update_row_index == i) begin
171
+ bht_updated[i].valid = 1'b1;
172
+ bht_ram_we[i] = 1'b1;
173
+ bht_ram_write_address[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)] = update_pc;
174
+ end
175
+ end
176
+ end
177
+
178
+ for (int i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
179
+
180
+ //When synchronous RAM is used, addresses are needed as soon as available
181
+ if (CVA6Cfg.FpgaAlteraEn)
182
+ bht_ram_read_address_0[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)] = index;
183
+ if (CVA6Cfg.FpgaAlteraEn)
184
+ bht_ram_read_address_1[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)] = update_pc;
185
+
186
+ if (check_update_row_index == i) begin
187
+ //When asynchronous RAM is used, the address can be updated on the cycle when data is read
188
+ if (!CVA6Cfg.FpgaAlteraEn)
189
+ bht_ram_read_address_1[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)] = update_pc;
190
+ bht[i].saturation_counter = bht_ram_rdata_1[i*BRAM_WORD_BITS+:2];
191
+
192
+ if (bht[i].saturation_counter == 2'b11) begin
193
+ // we can safely decrease it
194
+ if (!check_bht_update_taken)
195
+ bht_updated[i].saturation_counter = bht[i].saturation_counter - 1;
196
+ else bht_updated[i].saturation_counter = 2'b11;
197
+ // then check if it saturated in the negative regime e.g.: branch not taken
198
+ end else if (bht[i].saturation_counter == 2'b00) begin
199
+ // we can safely increase it
200
+ if (check_bht_update_taken)
201
+ bht_updated[i].saturation_counter = bht[i].saturation_counter + 1;
202
+ else bht_updated[i].saturation_counter = 2'b00;
203
+ end else begin // otherwise we are not in any boundaries and can decrease or increase it
204
+ if (check_bht_update_taken)
205
+ bht_updated[i].saturation_counter = bht[i].saturation_counter + 1;
206
+ else bht_updated[i].saturation_counter = bht[i].saturation_counter - 1;
207
+ end
208
+
209
+ //The data written in the RAM will have the valid bit from current input (async RAM) or the one from one clock cycle before (sync RAM)
210
+ bht_ram_wdata[i*BRAM_WORD_BITS+:BRAM_WORD_BITS] = CVA6Cfg.FpgaAlteraEn ? {bht_updated_valid[i][0], bht_updated[i].saturation_counter} :
211
+ {bht_updated[i].valid, bht_updated[i].saturation_counter};
212
+ end
213
+
214
+
215
+ if (!rst_ni) begin
216
+ //initialize output
217
+ bht_prediction_o[i] = '0;
218
+ end else begin
219
+ //When asynchronous RAM is used, addresses can be calculated on the same cycle as data is read
220
+ if (!CVA6Cfg.FpgaAlteraEn)
221
+ bht_ram_read_address_0[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)] = index;
222
+ //When synchronous RAM is used and data is read right after writing, we need some buffering
223
+ // This is one cycle of buffering
224
+ if (CVA6Cfg.FpgaAlteraEn && bht_updated_valid[i][0] && vpc_q == bht_updated_pc[i][0]) begin
225
+ bht_prediction_o[i].valid = bht_ram_wdata[i*BRAM_WORD_BITS+2];
226
+ bht_prediction_o[i].taken = bht_ram_wdata[i*BRAM_WORD_BITS+1];
227
+ //This is two cycles of buffering
228
+ end else if (CVA6Cfg.FpgaAlteraEn && bht_updated_valid[i][1] && vpc_q == bht_updated_pc[i][1]) begin
229
+ bht_prediction_o[i].valid = bht_ram_wdata_q[i*BRAM_WORD_BITS+2];
230
+ bht_prediction_o[i].taken = bht_ram_wdata_q[i*BRAM_WORD_BITS+1];
231
+ //In any other case we can safely read from the RAM as data is available
232
+ end else begin
233
+ bht_prediction_o[i].valid = bht_ram_rdata_0[i*BRAM_WORD_BITS+2];
234
+ bht_prediction_o[i].taken = bht_ram_rdata_0[i*BRAM_WORD_BITS+1];
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_bht_ram
241
+ if (CVA6Cfg.FpgaAlteraEn) begin
242
+ SyncThreePortRam #(
243
+ .ADDR_WIDTH($clog2(NR_ROWS)),
244
+ .DATA_DEPTH(NR_ROWS),
245
+ .DATA_WIDTH(BRAM_WORD_BITS)
246
+ ) i_bht_ram (
247
+ .Clk_CI (clk_i),
248
+ .WrEn_SI (bht_ram_we_q[i]),
249
+ .WrAddr_DI (bht_ram_write_address_q[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
250
+ .WrData_DI (bht_ram_wdata[i*BRAM_WORD_BITS+:BRAM_WORD_BITS]),
251
+ .RdAddr_DI_0(bht_ram_read_address_0[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
252
+ .RdAddr_DI_1(bht_ram_read_address_1[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
253
+ .RdData_DO_0(bht_ram_rdata_0[i*BRAM_WORD_BITS+:BRAM_WORD_BITS]),
254
+ .RdData_DO_1(bht_ram_rdata_1[i*BRAM_WORD_BITS+:BRAM_WORD_BITS])
255
+ );
256
+
257
+ end else begin
258
+ AsyncThreePortRam #(
259
+ .ADDR_WIDTH($clog2(NR_ROWS)),
260
+ .DATA_DEPTH(NR_ROWS),
261
+ .DATA_WIDTH(BRAM_WORD_BITS)
262
+ ) i_bht_ram (
263
+ .Clk_CI (clk_i),
264
+ .WrEn_SI (bht_ram_we[i]),
265
+ .WrAddr_DI (bht_ram_write_address[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
266
+ .WrData_DI (bht_ram_wdata[i*BRAM_WORD_BITS+:BRAM_WORD_BITS]),
267
+ .RdAddr_DI_0(bht_ram_read_address_0[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
268
+ .RdAddr_DI_1(bht_ram_read_address_1[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
269
+ .RdData_DO_0(bht_ram_rdata_0[i*BRAM_WORD_BITS+:BRAM_WORD_BITS]),
270
+ .RdData_DO_1(bht_ram_rdata_1[i*BRAM_WORD_BITS+:BRAM_WORD_BITS])
271
+ );
272
+ end
273
+ end
274
+
275
+ // Extra buffering signals needed when synchronous RAM is used
276
+
277
+ always_ff @(posedge clk_i or negedge rst_ni) begin
278
+ if (CVA6Cfg.FpgaAlteraEn) begin
279
+ if (!rst_ni) begin
280
+ bht_updated_valid <= '0;
281
+ bht_update_taken <= '0;
282
+ bht_ram_wdata_q <= '0;
283
+ row_index_q <= '0;
284
+ bht_ram_we_q <= '0;
285
+ bht_ram_write_address_q <= '0;
286
+ update_row_index_q <= '0;
287
+ end else begin
288
+ for (int i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
289
+ bht_updated_valid[i][1] <= bht_updated_valid[i][0];
290
+ bht_updated_valid[i][0] <= bht_updated[i].valid;
291
+ bht_updated_pc[i][1] <= bht_updated_pc[i][0];
292
+ bht_updated_pc[i][0] <= bht_update_i.pc;
293
+
294
+ end
295
+ vpc_q <= vpc_i;
296
+ bht_update_taken <= bht_update_i.taken;
297
+ bht_ram_wdata_q <= bht_ram_wdata;
298
+ bht_ram_we_q <= bht_ram_we;
299
+ bht_ram_write_address_q <= bht_ram_write_address;
300
+ update_row_index_q <= update_row_index;
301
+
302
+ row_index_q <= row_index;
303
+ end
304
+ end
305
+ end
306
+
307
+ // Assignment of indexes checked to generate data written in the RAM. When synchronous RAM is used these signals need to be delayed
308
+ assign check_update_row_index = CVA6Cfg.FpgaAlteraEn ? update_row_index_q : update_row_index;
309
+ assign check_bht_update_taken = CVA6Cfg.FpgaAlteraEn ? bht_update_taken : bht_update_i.taken;
310
+ assign check_row_index = CVA6Cfg.FpgaAlteraEn ? row_index_q : row_index;
311
+
312
+ end
313
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/bht2lvl.sv ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2025 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Original author: Gianmarco Ottavi, University of Bologna
12
+ // Description: Private history BHT
13
+
14
+ module bht2lvl #(
15
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
16
+ parameter type bht_update_t = logic
17
+ ) (
18
+ input logic clk_i,
19
+ input logic rst_ni,
20
+ input logic flush_i,
21
+ input logic [ CVA6Cfg.VLEN-1:0] vpc_i,
22
+ input bht_update_t bht_update_i,
23
+ // we potentially need INSTR_PER_FETCH predictions/cycle
24
+ output ariane_pkg::bht_prediction_t [CVA6Cfg.INSTR_PER_FETCH-1:0] bht_prediction_o
25
+ );
26
+
27
+ // the last bit is always zero, we don't need it for indexing
28
+ localparam OFFSET = CVA6Cfg.RVC == 1'b1 ? 1 : 2;
29
+ // re-shape the branch history table
30
+ localparam NR_ROWS = CVA6Cfg.BHTEntries / CVA6Cfg.INSTR_PER_FETCH;
31
+ // number of bits needed to index the row
32
+ localparam ROW_ADDR_BITS = $clog2(CVA6Cfg.INSTR_PER_FETCH);
33
+ localparam ROW_INDEX_BITS = CVA6Cfg.RVC == 1'b1 ? $clog2(CVA6Cfg.INSTR_PER_FETCH) : 1;
34
+ // number of bits we should use for prediction
35
+ localparam PREDICTION_BITS = $clog2(NR_ROWS) + OFFSET + ROW_ADDR_BITS;
36
+
37
+ struct packed {
38
+ logic valid;
39
+ logic [CVA6Cfg.BHTHist-1:0] hist;
40
+ logic [2**CVA6Cfg.BHTHist-1:0][1:0] saturation_counter;
41
+ }
42
+ bht_d[NR_ROWS-1:0][CVA6Cfg.INSTR_PER_FETCH-1:0],
43
+ bht_q[NR_ROWS-1:0][CVA6Cfg.INSTR_PER_FETCH-1:0];
44
+
45
+
46
+ logic [$clog2(NR_ROWS)-1:0] index, update_pc;
47
+ logic [CVA6Cfg.BHTHist-1:0] update_hist;
48
+ logic [ ROW_INDEX_BITS-1:0] update_row_index;
49
+
50
+ assign index = vpc_i[PREDICTION_BITS-1:ROW_ADDR_BITS+OFFSET];
51
+ assign update_pc = bht_update_i.pc[PREDICTION_BITS-1:ROW_ADDR_BITS+OFFSET];
52
+ assign update_hist = bht_q[update_pc][update_row_index].hist;
53
+
54
+ if (CVA6Cfg.RVC) begin : gen_update_row_index
55
+ assign update_row_index = bht_update_i.pc[ROW_ADDR_BITS+OFFSET-1:OFFSET];
56
+ end else begin
57
+ assign update_row_index = '0;
58
+ end
59
+
60
+
61
+ logic [1:0] saturation_counter;
62
+
63
+ // Get the current history of the entry
64
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0][CVA6Cfg.BHTHist-1:0] read_history;
65
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
66
+ assign read_history[i] = bht_q[index][i].hist;
67
+ end
68
+
69
+ // prediction assignment
70
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_bht_output
71
+ assign bht_prediction_o[i].valid = bht_q[index][i].valid;
72
+ assign bht_prediction_o[i].taken = bht_q[index][i].saturation_counter[read_history[i]][1] == 1'b1;
73
+ end
74
+
75
+ always_comb begin : update_bht
76
+ bht_d = bht_q;
77
+ saturation_counter = bht_q[update_pc][update_row_index].saturation_counter[update_hist];
78
+
79
+ if (bht_update_i.valid) begin
80
+ bht_d[update_pc][update_row_index].valid = 1'b1;
81
+
82
+ if (saturation_counter == 2'b11) begin
83
+ // we can safely decrease it
84
+ if (!bht_update_i.taken)
85
+ bht_d[update_pc][update_row_index].saturation_counter[update_hist] = saturation_counter - 1;
86
+ // then check if it saturated in the negative regime e.g.: branch not taken
87
+ end else if (saturation_counter == 2'b00) begin
88
+ // we can safely increase it
89
+ if (bht_update_i.taken)
90
+ bht_d[update_pc][update_row_index].saturation_counter[update_hist] = saturation_counter + 1;
91
+ end else begin // otherwise we are not in any boundaries and can decrease or increase it
92
+ if (bht_update_i.taken)
93
+ bht_d[update_pc][update_row_index].saturation_counter[update_hist] = saturation_counter + 1;
94
+ else
95
+ bht_d[update_pc][update_row_index].saturation_counter[update_hist] = saturation_counter - 1;
96
+ end
97
+
98
+ bht_d[update_pc][update_row_index].hist = {
99
+ update_hist[CVA6Cfg.BHTHist-2:0], bht_update_i.taken
100
+ };
101
+ end
102
+ end
103
+
104
+ always_ff @(posedge clk_i or negedge rst_ni) begin
105
+ if (!rst_ni) begin
106
+ for (int unsigned i = 0; i < NR_ROWS; i++) begin
107
+ for (int j = 0; j < CVA6Cfg.INSTR_PER_FETCH; j++) begin
108
+ bht_q[i][j] <= '0;
109
+ for (int k = 0; k < 2 ** CVA6Cfg.BHTHist; k++) begin
110
+ bht_q[i][j].saturation_counter[k] <= 2'b10;
111
+ end
112
+ end
113
+ end
114
+ end else begin
115
+ // evict all entries
116
+ if (flush_i) begin
117
+ for (int i = 0; i < NR_ROWS; i++) begin
118
+ for (int j = 0; j < CVA6Cfg.INSTR_PER_FETCH; j++) begin
119
+ bht_q[i][j].valid <= 1'b0;
120
+ bht_q[i][j].hist <= '0;
121
+ for (int k = 0; k < 2 ** CVA6Cfg.BHTHist; k++) begin
122
+ bht_q[i][j].saturation_counter[k] <= 2'b10;
123
+ end
124
+ end
125
+ end
126
+ end else begin
127
+ bht_q <= bht_d;
128
+ end
129
+ end
130
+ end
131
+
132
+
133
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/btb.sv ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 - 2019 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 2.0 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-2.0. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Author: Florian Zaruba, ETH Zurich
12
+ // Date: 08.02.2018
13
+ // Migrated: Luis Vitorio Cargnini, IEEE
14
+ // Date: 09.06.2018
15
+ //
16
+ // Additional contributions by:
17
+ // Sebastien Jacq, Thales - sjthales on github.com
18
+ // Date: 2022-12-01
19
+ //
20
+ // Description: This module is an adaptation of the BTB (Branch Target Buffer)
21
+ // module both FPGA and ASIC targets.
22
+ // Prediction target address is stored in BRAM on FPGA while for
23
+ // original module, target address is stored in D flip-flop.
24
+ // For FPGA flushing is not supported because the frontend module
25
+ // flushing signal is not connected.
26
+ //
27
+ // branch target buffer
28
+ module btb #(
29
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
30
+ parameter type btb_update_t = logic,
31
+ parameter type btb_prediction_t = logic,
32
+ parameter int NR_ENTRIES = 8
33
+ ) (
34
+ // Subsystem Clock - SUBSYSTEM
35
+ input logic clk_i,
36
+ // Asynchronous reset active low - SUBSYSTEM
37
+ input logic rst_ni,
38
+ // Branch prediction flush request - zero
39
+ input logic flush_bp_i,
40
+ // Debug mode state - CSR
41
+ input logic debug_mode_i,
42
+ // Virtual PC - CACHE
43
+ input logic [CVA6Cfg.VLEN-1:0] vpc_i,
44
+ // Update BTB with resolved address - EXECUTE
45
+ input btb_update_t btb_update_i,
46
+ // BTB Prediction - FRONTEND
47
+ output btb_prediction_t [CVA6Cfg.INSTR_PER_FETCH-1:0] btb_prediction_o
48
+ );
49
+ // the last bit is always zero, we don't need it for indexing
50
+ localparam OFFSET = CVA6Cfg.RVC == 1'b1 ? 1 : 2;
51
+ // re-shape the branch history table
52
+ localparam NR_ROWS = NR_ENTRIES / CVA6Cfg.INSTR_PER_FETCH;
53
+ // number of bits needed to index the row
54
+ localparam ROW_ADDR_BITS = $clog2(CVA6Cfg.INSTR_PER_FETCH);
55
+ localparam ROW_INDEX_BITS = CVA6Cfg.RVC == 1'b1 ? $clog2(CVA6Cfg.INSTR_PER_FETCH) : 1;
56
+ // number of bits we should use for prediction
57
+ localparam PREDICTION_BITS = $clog2(NR_ROWS) + OFFSET + ROW_ADDR_BITS;
58
+ // prevent aliasing to degrade performance
59
+ localparam ANTIALIAS_BITS = 8;
60
+ // number of bits par word in the bram
61
+ localparam BRAM_WORD_BITS = $bits(btb_prediction_t);
62
+ // we are not interested in all bits of the address
63
+ unread i_unread (.d_i(|vpc_i));
64
+
65
+
66
+ logic [$clog2(NR_ROWS)-1:0] index, update_pc;
67
+ logic [ROW_INDEX_BITS-1:0] update_row_index;
68
+
69
+ assign index = vpc_i[PREDICTION_BITS-1:ROW_ADDR_BITS+OFFSET];
70
+ assign update_pc = btb_update_i.pc[PREDICTION_BITS-1:ROW_ADDR_BITS+OFFSET];
71
+ if (CVA6Cfg.RVC) begin : gen_update_row_index
72
+ assign update_row_index = btb_update_i.pc[ROW_ADDR_BITS+OFFSET-1:OFFSET];
73
+ end else begin
74
+ assign update_row_index = '0;
75
+ end
76
+
77
+ if (CVA6Cfg.FpgaEn) begin : gen_fpga_btb //FPGA TARGETS
78
+ logic [ CVA6Cfg.INSTR_PER_FETCH-1:0] btb_ram_csel_prediction;
79
+ logic [ CVA6Cfg.INSTR_PER_FETCH-1:0] btb_ram_we_prediction;
80
+ logic [CVA6Cfg.INSTR_PER_FETCH*$clog2(NR_ROWS)-1:0] btb_ram_addr_prediction;
81
+ logic [ CVA6Cfg.INSTR_PER_FETCH*BRAM_WORD_BITS-1:0] btb_ram_wdata_prediction;
82
+ logic [ CVA6Cfg.INSTR_PER_FETCH*BRAM_WORD_BITS-1:0] btb_ram_rdata_prediction;
83
+
84
+ logic [ CVA6Cfg.INSTR_PER_FETCH-1:0] btb_ram_csel_update;
85
+ logic [ CVA6Cfg.INSTR_PER_FETCH-1:0] btb_ram_we_update;
86
+ logic [CVA6Cfg.INSTR_PER_FETCH*$clog2(NR_ROWS)-1:0] btb_ram_addr_update;
87
+ logic [ CVA6Cfg.INSTR_PER_FETCH*BRAM_WORD_BITS-1:0] btb_ram_wdata_update;
88
+
89
+ // output matching prediction
90
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_btb_output
91
+ assign btb_ram_csel_prediction[i] = 1'b1;
92
+ assign btb_ram_we_prediction[i] = 1'b0;
93
+ assign btb_ram_wdata_prediction[i*BRAM_WORD_BITS+:BRAM_WORD_BITS] = '0;
94
+ assign btb_ram_addr_prediction[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)] = index;
95
+ assign btb_prediction_o[i] = btb_ram_rdata_prediction[i*BRAM_WORD_BITS+:BRAM_WORD_BITS];
96
+ end
97
+
98
+ // -------------------------
99
+ // Update Branch Prediction
100
+ // -------------------------
101
+ // update on a mis-predict
102
+ always_comb begin : update_branch_predict
103
+ btb_ram_csel_update = '0;
104
+ btb_ram_we_update = '0;
105
+ btb_ram_addr_update = '0;
106
+ btb_ram_wdata_update = '0;
107
+
108
+ if (btb_update_i.valid && !debug_mode_i) begin
109
+ for (int i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
110
+ if (update_row_index == i) begin
111
+ btb_ram_csel_update[i] = 1'b1;
112
+ btb_ram_we_update[i] = 1'b1;
113
+ btb_ram_addr_update[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)] = update_pc;
114
+ btb_ram_wdata_update[i*BRAM_WORD_BITS+:BRAM_WORD_BITS] = {
115
+ 1'b1, btb_update_i.target_address
116
+ };
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_btb_ram
123
+ SyncDpRam #(
124
+ .ADDR_WIDTH($clog2(NR_ROWS)),
125
+ .DATA_DEPTH(NR_ROWS),
126
+ .DATA_WIDTH(BRAM_WORD_BITS),
127
+ .OUT_REGS (0),
128
+ .SIM_INIT (1)
129
+ ) i_btb_ram (
130
+ .Clk_CI (clk_i),
131
+ .Rst_RBI (rst_ni),
132
+ //----------------------------
133
+ .CSelA_SI (btb_ram_csel_update[i]),
134
+ .WrEnA_SI (btb_ram_we_update[i]),
135
+ .AddrA_DI (btb_ram_addr_update[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
136
+ .WrDataA_DI(btb_ram_wdata_update[i*BRAM_WORD_BITS+:BRAM_WORD_BITS]),
137
+ .RdDataA_DO(),
138
+ //-----------------------------
139
+ .CSelB_SI (btb_ram_csel_prediction[i]),
140
+ .WrEnB_SI (btb_ram_we_prediction[i]),
141
+ .AddrB_DI (btb_ram_addr_prediction[i*$clog2(NR_ROWS)+:$clog2(NR_ROWS)]),
142
+ .WrDataB_DI(btb_ram_wdata_prediction[i*BRAM_WORD_BITS+:BRAM_WORD_BITS]),
143
+ .RdDataB_DO(btb_ram_rdata_prediction[i*BRAM_WORD_BITS+:BRAM_WORD_BITS])
144
+ );
145
+ end
146
+
147
+ end else begin : gen_asic_btb // ASIC TARGET
148
+
149
+ // typedef for all branch target entries
150
+ // we may want to try to put a tag field that fills the rest of the PC in-order to mitigate aliasing effects
151
+ btb_prediction_t
152
+ btb_d[NR_ROWS-1:0][CVA6Cfg.INSTR_PER_FETCH-1:0],
153
+ btb_q[NR_ROWS-1:0][CVA6Cfg.INSTR_PER_FETCH-1:0];
154
+
155
+ // output matching prediction
156
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_btb_output
157
+ assign btb_prediction_o[i] = btb_q[index][i]; // workaround
158
+ end
159
+
160
+ // -------------------------
161
+ // Update Branch Prediction
162
+ // -------------------------
163
+ // update on a mis-predict
164
+ always_comb begin : update_branch_predict
165
+ btb_d = btb_q;
166
+
167
+ if (btb_update_i.valid && !debug_mode_i) begin
168
+ btb_d[update_pc][update_row_index].valid = 1'b1;
169
+ // the target address is simply updated
170
+ btb_d[update_pc][update_row_index].target_address = btb_update_i.target_address;
171
+ end
172
+ end
173
+
174
+ // sequential process
175
+ always_ff @(posedge clk_i or negedge rst_ni) begin
176
+ if (!rst_ni) begin
177
+ // Bias the branches to be taken upon first arrival
178
+ for (int i = 0; i < NR_ROWS; i++) btb_q[i] <= '{default: 0};
179
+ end else begin
180
+ // evict all entries
181
+ if (flush_bp_i) begin
182
+ for (int i = 0; i < NR_ROWS; i++) begin
183
+ for (int j = 0; j < CVA6Cfg.INSTR_PER_FETCH; j++) begin
184
+ btb_q[i][j].valid <= 1'b0;
185
+ end
186
+ end
187
+ end else begin
188
+ btb_q <= btb_d;
189
+ end
190
+ end
191
+ end
192
+ end
193
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/frontend.sv ADDED
@@ -0,0 +1,590 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Author: Florian Zaruba, ETH Zurich
12
+ // Date: 08.02.2018
13
+ // Description: Ariane Instruction Fetch Frontend
14
+ //
15
+ // This module interfaces with the instruction cache, handles control
16
+ // change request from the back-end and does branch prediction.
17
+
18
+ module frontend
19
+ import ariane_pkg::*;
20
+ #(
21
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
22
+ parameter type bp_resolve_t = logic,
23
+ parameter type fetch_entry_t = logic,
24
+ parameter type icache_dreq_t = logic,
25
+ parameter type icache_drsp_t = logic
26
+ ) (
27
+ // Subsystem Clock - SUBSYSTEM
28
+ input logic clk_i,
29
+ // Asynchronous reset active low - SUBSYSTEM
30
+ input logic rst_ni,
31
+ // Next PC when reset - SUBSYSTEM
32
+ input logic [CVA6Cfg.VLEN-1:0] boot_addr_i,
33
+ // Flush branch prediction - zero
34
+ input logic flush_bp_i,
35
+ // Flush requested by FENCE, mis-predict and exception - CONTROLLER
36
+ input logic flush_i,
37
+ // Halt requested by WFI and Accelerate port - CONTROLLER
38
+ input logic halt_i,
39
+ // Set COMMIT PC as next PC requested by FENCE, CSR side-effect and Accelerate port - CONTROLLER
40
+ input logic set_pc_commit_i,
41
+ // COMMIT PC - COMMIT
42
+ input logic [CVA6Cfg.VLEN-1:0] pc_commit_i,
43
+ // Exception event - COMMIT
44
+ input logic ex_valid_i,
45
+ // Mispredict event and next PC - EXECUTE
46
+ input bp_resolve_t resolved_branch_i,
47
+ // Return from exception event - CSR
48
+ input logic eret_i,
49
+ // Next PC when returning from exception - CSR
50
+ input logic [CVA6Cfg.VLEN-1:0] epc_i,
51
+ // Next PC when jumping into exception - CSR
52
+ input logic [CVA6Cfg.VLEN-1:0] trap_vector_base_i,
53
+ // Debug event - CSR
54
+ input logic set_debug_pc_i,
55
+ // Debug mode state - CSR
56
+ input logic debug_mode_i,
57
+ // Handshake between CACHE and FRONTEND (fetch) - CACHES
58
+ output icache_dreq_t icache_dreq_o,
59
+ // Handshake between CACHE and FRONTEND (fetch) - CACHES
60
+ input icache_drsp_t icache_dreq_i,
61
+ // Handshake's data between fetch and decode - ID_STAGE
62
+ output fetch_entry_t [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_o,
63
+ // Handshake's valid between fetch and decode - ID_STAGE
64
+ output logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_valid_o,
65
+ // Handshake's ready between fetch and decode - ID_STAGE
66
+ input logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_ready_i
67
+ );
68
+
69
+ localparam type bht_update_t = struct packed {
70
+ logic valid;
71
+ logic [CVA6Cfg.VLEN-1:0] pc; // update at PC
72
+ logic taken;
73
+ };
74
+
75
+ localparam type btb_prediction_t = struct packed {
76
+ logic valid;
77
+ logic [CVA6Cfg.VLEN-1:0] target_address;
78
+ };
79
+
80
+ localparam type btb_update_t = struct packed {
81
+ logic valid;
82
+ logic [CVA6Cfg.VLEN-1:0] pc; // update at PC
83
+ logic [CVA6Cfg.VLEN-1:0] target_address;
84
+ };
85
+
86
+ localparam type ras_t = struct packed {
87
+ logic valid;
88
+ logic [CVA6Cfg.VLEN-1:0] ra;
89
+ };
90
+
91
+ // Instruction Cache Registers, from I$
92
+ logic [ CVA6Cfg.FETCH_WIDTH-1:0] icache_data_q;
93
+ logic icache_valid_q;
94
+ ariane_pkg::frontend_exception_t icache_ex_valid_q;
95
+ logic [ CVA6Cfg.VLEN-1:0] icache_vaddr_q;
96
+ logic [ CVA6Cfg.GPLEN-1:0] icache_gpaddr_q;
97
+ logic [ 31:0] icache_tinst_q;
98
+ logic icache_gva_q;
99
+ logic instr_queue_ready;
100
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] instr_queue_consumed;
101
+ // upper-most branch-prediction from last cycle
102
+ btb_prediction_t btb_q;
103
+ bht_prediction_t bht_q;
104
+ // instruction fetch is ready
105
+ logic if_ready;
106
+ logic [CVA6Cfg.VLEN-1:0] npc_d, npc_q; // next PC
107
+
108
+ // indicates whether we come out of reset (then we need to load boot_addr_i)
109
+ logic npc_rst_load_q;
110
+
111
+ logic replay;
112
+ logic [ CVA6Cfg.VLEN-1:0] replay_addr;
113
+
114
+ // shift amount
115
+ logic [$clog2(CVA6Cfg.INSTR_PER_FETCH)-1:0] shamt;
116
+ // address will always be 16 bit aligned, make this explicit here
117
+ if (CVA6Cfg.RVC) begin : gen_shamt
118
+ assign shamt = icache_dreq_i.vaddr[$clog2(CVA6Cfg.INSTR_PER_FETCH):1];
119
+ end else begin
120
+ assign shamt = 1'b0;
121
+ end
122
+
123
+ // -----------------------
124
+ // Ctrl Flow Speculation
125
+ // -----------------------
126
+ // RVI ctrl flow prediction
127
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] rvi_return, rvi_call, rvi_branch, rvi_jalr, rvi_jump;
128
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0][CVA6Cfg.VLEN-1:0] rvi_imm;
129
+ // RVC branching
130
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] rvc_branch, rvc_jump, rvc_jr, rvc_return, rvc_jalr, rvc_call;
131
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0][CVA6Cfg.VLEN-1:0] rvc_imm;
132
+ // re-aligned instruction and address (coming from cache - combinationally)
133
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0][ 31:0] instr;
134
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0][CVA6Cfg.VLEN-1:0] addr;
135
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] instruction_valid;
136
+ // BHT, BTB and RAS prediction
137
+ bht_prediction_t [CVA6Cfg.INSTR_PER_FETCH-1:0] bht_prediction;
138
+ btb_prediction_t [CVA6Cfg.INSTR_PER_FETCH-1:0] btb_prediction;
139
+ bht_prediction_t [CVA6Cfg.INSTR_PER_FETCH-1:0] bht_prediction_shifted;
140
+ btb_prediction_t [CVA6Cfg.INSTR_PER_FETCH-1:0] btb_prediction_shifted;
141
+ ras_t ras_predict;
142
+ logic [ CVA6Cfg.VLEN-1:0] vpc_btb;
143
+ logic [ CVA6Cfg.VLEN-1:0] vpc_bht;
144
+
145
+ // branch-predict update
146
+ logic is_mispredict;
147
+ logic ras_push, ras_pop;
148
+ logic [ CVA6Cfg.VLEN-1:0] ras_update;
149
+
150
+ // Instruction FIFO
151
+ logic [ CVA6Cfg.VLEN-1:0] predict_address;
152
+ cf_t [CVA6Cfg.INSTR_PER_FETCH-1:0] cf_type;
153
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] taken_rvi_cf;
154
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] taken_rvc_cf;
155
+
156
+ logic serving_unaligned;
157
+ // Re-align instructions
158
+ instr_realign #(
159
+ .CVA6Cfg(CVA6Cfg)
160
+ ) i_instr_realign (
161
+ .clk_i (clk_i),
162
+ .rst_ni (rst_ni),
163
+ .flush_i (icache_dreq_o.kill_s2),
164
+ .valid_i (icache_valid_q),
165
+ .serving_unaligned_o(serving_unaligned),
166
+ .address_i (icache_vaddr_q),
167
+ .data_i (icache_data_q),
168
+ .valid_o (instruction_valid),
169
+ .addr_o (addr),
170
+ .instr_o (instr)
171
+ );
172
+ // --------------------
173
+ // Branch Prediction
174
+ // --------------------
175
+ // select the right branch prediction result
176
+ // in case we are serving an unaligned instruction in instr[0] we need to take
177
+ // the prediction we saved from the previous fetch
178
+ if (CVA6Cfg.RVC) begin : gen_btb_prediction_shifted
179
+ assign bht_prediction_shifted[0] = (serving_unaligned) ? bht_q : bht_prediction[addr[0][$clog2(
180
+ CVA6Cfg.INSTR_PER_FETCH
181
+ ):1]];
182
+ assign btb_prediction_shifted[0] = (serving_unaligned) ? btb_q : btb_prediction[addr[0][$clog2(
183
+ CVA6Cfg.INSTR_PER_FETCH
184
+ ):1]];
185
+
186
+ // for all other predictions we can use the generated address to index
187
+ // into the branch prediction data structures
188
+ for (genvar i = 1; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_prediction_address
189
+ assign bht_prediction_shifted[i] = bht_prediction[addr[i][$clog2(CVA6Cfg.INSTR_PER_FETCH):1]];
190
+ assign btb_prediction_shifted[i] = btb_prediction[addr[i][$clog2(CVA6Cfg.INSTR_PER_FETCH):1]];
191
+ end
192
+ end else begin
193
+ assign bht_prediction_shifted[0] = (serving_unaligned) ? bht_q : bht_prediction[addr[0][1]];
194
+ assign btb_prediction_shifted[0] = (serving_unaligned) ? btb_q : btb_prediction[addr[0][1]];
195
+ end
196
+ ;
197
+
198
+ // for the return address stack it doens't matter as we have the
199
+ // address of the call/return already
200
+ logic bp_valid;
201
+
202
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] is_branch;
203
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] is_call;
204
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] is_jump;
205
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] is_return;
206
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] is_jalr;
207
+
208
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
209
+ // branch history table -> BHT
210
+ assign is_branch[i] = instruction_valid[i] & (rvi_branch[i] | rvc_branch[i]);
211
+ // function calls -> RAS
212
+ assign is_call[i] = instruction_valid[i] & (rvi_call[i] | rvc_call[i]);
213
+ // function return -> RAS
214
+ assign is_return[i] = instruction_valid[i] & (rvi_return[i] | rvc_return[i]);
215
+ // unconditional jumps with known target -> immediately resolved
216
+ assign is_jump[i] = instruction_valid[i] & (rvi_jump[i] | rvc_jump[i]);
217
+ // unconditional jumps with unknown target -> BTB
218
+ assign is_jalr[i] = instruction_valid[i] & ~is_return[i] & (rvi_jalr[i] | rvc_jalr[i] | rvc_jr[i]);
219
+ end
220
+
221
+ // taken/not taken
222
+ always_comb begin
223
+ taken_rvi_cf = '0;
224
+ taken_rvc_cf = '0;
225
+ predict_address = '0;
226
+
227
+ for (int i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) cf_type[i] = ariane_pkg::NoCF;
228
+
229
+ ras_push = 1'b0;
230
+ ras_pop = 1'b0;
231
+ ras_update = '0;
232
+
233
+ // lower most prediction gets precedence
234
+ for (int i = CVA6Cfg.INSTR_PER_FETCH - 1; i >= 0; i--) begin
235
+ unique case ({
236
+ is_branch[i], is_return[i], is_jump[i], is_jalr[i]
237
+ })
238
+ 4'b0000: ; // regular instruction e.g.: no branch
239
+ // unconditional jump to register, we need the BTB to resolve this
240
+ 4'b0001: begin
241
+ ras_pop = 1'b0;
242
+ ras_push = 1'b0;
243
+ if (CVA6Cfg.BTBEntries != 0 && btb_prediction_shifted[i].valid) begin
244
+ predict_address = btb_prediction_shifted[i].target_address;
245
+ cf_type[i] = ariane_pkg::JumpR;
246
+ end
247
+ end
248
+ // its an unconditional jump to an immediate
249
+ 4'b0010: begin
250
+ ras_pop = 1'b0;
251
+ ras_push = 1'b0;
252
+ taken_rvi_cf[i] = rvi_jump[i];
253
+ taken_rvc_cf[i] = rvc_jump[i];
254
+ cf_type[i] = ariane_pkg::Jump;
255
+ end
256
+ // return
257
+ 4'b0100: begin
258
+ // make sure to only alter the RAS if we actually consumed the instruction
259
+ ras_pop = ras_predict.valid & instr_queue_consumed[i];
260
+ ras_push = 1'b0;
261
+ predict_address = ras_predict.ra;
262
+ cf_type[i] = ariane_pkg::Return;
263
+ end
264
+ // branch prediction
265
+ 4'b1000: begin
266
+ ras_pop = 1'b0;
267
+ ras_push = 1'b0;
268
+ // if we have a valid dynamic prediction use it
269
+ if (bht_prediction_shifted[i].valid) begin
270
+ taken_rvi_cf[i] = rvi_branch[i] & bht_prediction_shifted[i].taken;
271
+ taken_rvc_cf[i] = rvc_branch[i] & bht_prediction_shifted[i].taken;
272
+ // otherwise default to static prediction
273
+ end else begin
274
+ // set if immediate is negative - static prediction
275
+ taken_rvi_cf[i] = rvi_branch[i] & rvi_imm[i][CVA6Cfg.VLEN-1];
276
+ taken_rvc_cf[i] = rvc_branch[i] & rvc_imm[i][CVA6Cfg.VLEN-1];
277
+ end
278
+ if (taken_rvi_cf[i] || taken_rvc_cf[i]) begin
279
+ cf_type[i] = ariane_pkg::Branch;
280
+ end
281
+ end
282
+ default: ;
283
+ // default: $error("Decoded more than one control flow");
284
+ endcase
285
+ // if this instruction, in addition, is a call, save the resulting address
286
+ // but only if we actually consumed the address
287
+ if (is_call[i]) begin
288
+ ras_push = instr_queue_consumed[i];
289
+ ras_update = addr[i] + (rvc_call[i] ? 2 : 4);
290
+ end
291
+ // calculate the jump target address
292
+ if (taken_rvc_cf[i] || taken_rvi_cf[i]) begin
293
+ predict_address = addr[i] + (taken_rvc_cf[i] ? rvc_imm[i] : rvi_imm[i]);
294
+ end
295
+ end
296
+ end
297
+ // or reduce struct
298
+ always_comb begin
299
+ bp_valid = 1'b0;
300
+ // BP cannot be valid if we have a return instruction and the RAS is not giving a valid address
301
+ // Check that we encountered a control flow and that for a return the RAS
302
+ // contains a valid prediction.
303
+ for (int i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++)
304
+ bp_valid |= ((cf_type[i] != NoCF & cf_type[i] != Return) | ((cf_type[i] == Return) & ras_predict.valid));
305
+ end
306
+ assign is_mispredict = resolved_branch_i.valid & resolved_branch_i.is_mispredict;
307
+
308
+ // Cache interface
309
+ assign icache_dreq_o.req = instr_queue_ready;
310
+ assign if_ready = icache_dreq_i.ready & instr_queue_ready;
311
+ // We need to flush the cache pipeline if:
312
+ // 1. We mispredicted
313
+ // 2. Want to flush the whole processor front-end
314
+ // 3. Need to replay an instruction because the fetch-fifo was full
315
+ assign icache_dreq_o.kill_s1 = is_mispredict | flush_i | replay;
316
+ // if we have a valid branch-prediction we need to only kill the last cache request
317
+ // also if we killed the first stage we also need to kill the second stage (inclusive flush)
318
+ assign icache_dreq_o.kill_s2 = icache_dreq_o.kill_s1 | bp_valid;
319
+
320
+ // Update Control Flow Predictions
321
+ bht_update_t bht_update;
322
+ btb_update_t btb_update;
323
+
324
+ // assert on branch, deassert when resolved
325
+ logic speculative_q, speculative_d;
326
+ assign speculative_d = (speculative_q && !resolved_branch_i.valid || |is_branch || |is_return || |is_jalr) && !flush_i;
327
+ assign icache_dreq_o.spec = speculative_d;
328
+
329
+ assign bht_update.valid = resolved_branch_i.valid
330
+ & (resolved_branch_i.cf_type == ariane_pkg::Branch);
331
+ assign bht_update.pc = resolved_branch_i.pc;
332
+ assign bht_update.taken = resolved_branch_i.is_taken;
333
+ // only update mispredicted branches e.g. no returns from the RAS
334
+ assign btb_update.valid = resolved_branch_i.valid
335
+ & resolved_branch_i.is_mispredict
336
+ & (resolved_branch_i.cf_type == ariane_pkg::JumpR);
337
+ assign btb_update.pc = resolved_branch_i.pc;
338
+ assign btb_update.target_address = resolved_branch_i.target_address;
339
+
340
+ // -------------------
341
+ // Next PC
342
+ // -------------------
343
+ // next PC (NPC) can come from (in order of precedence):
344
+ // 0. Default assignment/replay instruction
345
+ // 1. Branch Predict taken
346
+ // 2. Control flow change request (misprediction)
347
+ // 3. Return from environment call
348
+ // 4. Exception/Interrupt
349
+ // 5. Pipeline Flush because of CSR side effects
350
+ // Mis-predict handling is a little bit different
351
+ // select PC a.k.a PC Gen
352
+ always_comb begin : npc_select
353
+ automatic logic [CVA6Cfg.VLEN-1:0] fetch_address;
354
+ // check whether we come out of reset
355
+ // this is a workaround. some tools have issues
356
+ // having boot_addr_i in the asynchronous
357
+ // reset assignment to npc_q, even though
358
+ // boot_addr_i will be assigned a constant
359
+ // on the top-level.
360
+ if (npc_rst_load_q) begin
361
+ npc_d = boot_addr_i;
362
+ fetch_address = boot_addr_i;
363
+ end else begin
364
+ fetch_address = npc_q;
365
+ // keep stable by default
366
+ npc_d = npc_q;
367
+ end
368
+ // 0. Branch Prediction
369
+ if (bp_valid) begin
370
+ fetch_address = predict_address;
371
+ npc_d = predict_address;
372
+ end
373
+ // 1. Default assignment
374
+ if (if_ready) begin
375
+ npc_d = {
376
+ fetch_address[CVA6Cfg.VLEN-1:CVA6Cfg.FETCH_ALIGN_BITS] + 1, {CVA6Cfg.FETCH_ALIGN_BITS{1'b0}}
377
+ };
378
+ end
379
+ // 2. Replay instruction fetch
380
+ if (replay) begin
381
+ npc_d = replay_addr;
382
+ end
383
+ // 3. Control flow change request
384
+ if (is_mispredict) begin
385
+ npc_d = resolved_branch_i.target_address;
386
+ end
387
+ // 4. Return from environment call
388
+ if (eret_i) begin
389
+ npc_d = epc_i;
390
+ end
391
+ // 5. Exception/Interrupt
392
+ if (ex_valid_i) begin
393
+ npc_d = trap_vector_base_i;
394
+ end
395
+ // 6. Pipeline Flush because of CSR side effects
396
+ // On a pipeline flush start fetching from the next address
397
+ // of the instruction in the commit stage
398
+ // we either came here from a flush request of a CSR instruction or AMO,
399
+ // so as CSR or AMO instructions do not exist in a compressed form
400
+ // we can unconditionally do PC + 4 here
401
+ // or if the commit stage is halted, just take the current pc of the
402
+ // instruction in the commit stage
403
+ // TODO(zarubaf) This adder can at least be merged with the one in the csr_regfile stage
404
+ if (set_pc_commit_i) begin
405
+ npc_d = pc_commit_i + (halt_i ? '0 : {{CVA6Cfg.VLEN - 3{1'b0}}, 3'b100});
406
+ end
407
+ // 7. Debug
408
+ // enter debug on a hard-coded base-address
409
+ if (CVA6Cfg.DebugEn && set_debug_pc_i)
410
+ npc_d = CVA6Cfg.DmBaseAddress[CVA6Cfg.VLEN-1:0] + CVA6Cfg.HaltAddress[CVA6Cfg.VLEN-1:0];
411
+ icache_dreq_o.vaddr = fetch_address;
412
+ end
413
+
414
+ logic [CVA6Cfg.FETCH_WIDTH-1:0] icache_data;
415
+ // re-align the cache line
416
+ assign icache_data = icache_dreq_i.data >> {shamt, 4'b0};
417
+
418
+ always_ff @(posedge clk_i or negedge rst_ni) begin
419
+ if (!rst_ni) begin
420
+ npc_rst_load_q <= 1'b1;
421
+ npc_q <= '0;
422
+ speculative_q <= '0;
423
+ icache_data_q <= '0;
424
+ icache_valid_q <= 1'b0;
425
+ icache_vaddr_q <= 'b0;
426
+ icache_gpaddr_q <= 'b0;
427
+ icache_tinst_q <= 'b0;
428
+ icache_gva_q <= 1'b0;
429
+ icache_ex_valid_q <= ariane_pkg::FE_NONE;
430
+ btb_q <= '0;
431
+ bht_q <= '0;
432
+ end else begin
433
+ npc_rst_load_q <= 1'b0;
434
+ npc_q <= npc_d;
435
+ speculative_q <= speculative_d;
436
+ icache_valid_q <= icache_dreq_i.valid;
437
+ if (icache_dreq_i.valid) begin
438
+ icache_data_q <= icache_data;
439
+ icache_vaddr_q <= icache_dreq_i.vaddr;
440
+ if (CVA6Cfg.RVH) begin
441
+ icache_gpaddr_q <= icache_dreq_i.ex.tval2[CVA6Cfg.GPLEN-1:0];
442
+ icache_tinst_q <= icache_dreq_i.ex.tinst;
443
+ icache_gva_q <= icache_dreq_i.ex.gva;
444
+ end else begin
445
+ icache_gpaddr_q <= 'b0;
446
+ icache_tinst_q <= 'b0;
447
+ icache_gva_q <= 1'b0;
448
+ end
449
+
450
+ // Map the only three exceptions which can occur in the frontend to a two bit enum
451
+ if (CVA6Cfg.MmuPresent && icache_dreq_i.ex.cause == riscv::INSTR_GUEST_PAGE_FAULT) begin
452
+ icache_ex_valid_q <= ariane_pkg::FE_INSTR_GUEST_PAGE_FAULT;
453
+ end else if (CVA6Cfg.MmuPresent && icache_dreq_i.ex.cause == riscv::INSTR_PAGE_FAULT) begin
454
+ icache_ex_valid_q <= ariane_pkg::FE_INSTR_PAGE_FAULT;
455
+ end else if (icache_dreq_i.ex.cause == riscv::INSTR_ACCESS_FAULT) begin
456
+ icache_ex_valid_q <= ariane_pkg::FE_INSTR_ACCESS_FAULT;
457
+ end else begin
458
+ icache_ex_valid_q <= ariane_pkg::FE_NONE;
459
+ end
460
+ // save the uppermost prediction
461
+ btb_q <= btb_prediction[CVA6Cfg.INSTR_PER_FETCH-1];
462
+ bht_q <= bht_prediction[CVA6Cfg.INSTR_PER_FETCH-1];
463
+ end
464
+ end
465
+ end
466
+
467
+ if (CVA6Cfg.RASDepth == 0) begin
468
+ assign ras_predict = '0;
469
+ end else begin : ras_gen
470
+ ras #(
471
+ .CVA6Cfg(CVA6Cfg),
472
+ .ras_t (ras_t),
473
+ .DEPTH (CVA6Cfg.RASDepth)
474
+ ) i_ras (
475
+ .clk_i,
476
+ .rst_ni,
477
+ .flush_bp_i(flush_bp_i),
478
+ .push_i(ras_push),
479
+ .pop_i(ras_pop),
480
+ .data_i(ras_update),
481
+ .data_o(ras_predict)
482
+ );
483
+ end
484
+
485
+ //For FPGA, BTB is implemented in read synchronous BRAM
486
+ //while for ASIC, BTB is implemented in D flip-flop
487
+ //and can be read at the same cycle.
488
+ //Same for BHT
489
+ assign vpc_btb = (CVA6Cfg.FpgaEn) ? icache_dreq_i.vaddr : icache_vaddr_q;
490
+ assign vpc_bht = (CVA6Cfg.FpgaEn && CVA6Cfg.FpgaAlteraEn && icache_dreq_i.valid) ? icache_dreq_i.vaddr : icache_vaddr_q;
491
+
492
+ if (CVA6Cfg.BTBEntries == 0) begin
493
+ assign btb_prediction = '0;
494
+ end else begin : btb_gen
495
+ btb #(
496
+ .CVA6Cfg (CVA6Cfg),
497
+ .btb_update_t(btb_update_t),
498
+ .btb_prediction_t(btb_prediction_t),
499
+ .NR_ENTRIES(CVA6Cfg.BTBEntries)
500
+ ) i_btb (
501
+ .clk_i,
502
+ .rst_ni,
503
+ .flush_bp_i (flush_bp_i),
504
+ .debug_mode_i,
505
+ .vpc_i (vpc_btb),
506
+ .btb_update_i (btb_update),
507
+ .btb_prediction_o(btb_prediction)
508
+ );
509
+ end
510
+
511
+ if (CVA6Cfg.BHTEntries == 0) begin
512
+ assign bht_prediction = '0;
513
+ end else if (CVA6Cfg.BPType == config_pkg::BHT) begin : bht_gen
514
+ bht #(
515
+ .CVA6Cfg (CVA6Cfg),
516
+ .bht_update_t(bht_update_t),
517
+ .NR_ENTRIES(CVA6Cfg.BHTEntries)
518
+ ) i_bht (
519
+ .clk_i,
520
+ .rst_ni,
521
+ .flush_bp_i (flush_bp_i),
522
+ .debug_mode_i,
523
+ .vpc_i (vpc_bht),
524
+ .bht_update_i (bht_update),
525
+ .bht_prediction_o(bht_prediction)
526
+ );
527
+ end else if (CVA6Cfg.BPType == config_pkg::PH_BHT) begin : bht2lvl_gen
528
+ bht2lvl #(
529
+ .CVA6Cfg (CVA6Cfg),
530
+ .bht_update_t(bht_update_t)
531
+ ) i_bht (
532
+ .clk_i,
533
+ .rst_ni,
534
+ .flush_i (flush_bp_i),
535
+ .vpc_i (icache_vaddr_q),
536
+ .bht_update_i (bht_update),
537
+ .bht_prediction_o(bht_prediction)
538
+ );
539
+ end
540
+
541
+ // we need to inspect up to CVA6Cfg.INSTR_PER_FETCH instructions for branches
542
+ // and jumps
543
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_instr_scan
544
+ instr_scan #(
545
+ .CVA6Cfg(CVA6Cfg)
546
+ ) i_instr_scan (
547
+ .instr_i (instr[i]),
548
+ .rvi_return_o(rvi_return[i]),
549
+ .rvi_call_o (rvi_call[i]),
550
+ .rvi_branch_o(rvi_branch[i]),
551
+ .rvi_jalr_o (rvi_jalr[i]),
552
+ .rvi_jump_o (rvi_jump[i]),
553
+ .rvi_imm_o (rvi_imm[i]),
554
+ .rvc_branch_o(rvc_branch[i]),
555
+ .rvc_jump_o (rvc_jump[i]),
556
+ .rvc_jr_o (rvc_jr[i]),
557
+ .rvc_return_o(rvc_return[i]),
558
+ .rvc_jalr_o (rvc_jalr[i]),
559
+ .rvc_call_o (rvc_call[i]),
560
+ .rvc_imm_o (rvc_imm[i])
561
+ );
562
+ end
563
+
564
+ instr_queue #(
565
+ .CVA6Cfg(CVA6Cfg),
566
+ .fetch_entry_t(fetch_entry_t)
567
+ ) i_instr_queue (
568
+ .clk_i (clk_i),
569
+ .rst_ni (rst_ni),
570
+ .flush_i (flush_i),
571
+ .instr_i (instr), // from re-aligner
572
+ .addr_i (addr), // from re-aligner
573
+ .exception_i (icache_ex_valid_q), // from I$
574
+ .exception_addr_i (icache_vaddr_q),
575
+ .exception_gpaddr_i (icache_gpaddr_q),
576
+ .exception_tinst_i (icache_tinst_q),
577
+ .exception_gva_i (icache_gva_q),
578
+ .predict_address_i (predict_address),
579
+ .cf_type_i (cf_type),
580
+ .valid_i (instruction_valid), // from re-aligner
581
+ .consumed_o (instr_queue_consumed),
582
+ .ready_o (instr_queue_ready),
583
+ .replay_o (replay),
584
+ .replay_addr_o (replay_addr),
585
+ .fetch_entry_o (fetch_entry_o), // to back-end
586
+ .fetch_entry_valid_o(fetch_entry_valid_o), // to back-end
587
+ .fetch_entry_ready_i(fetch_entry_ready_i) // to back-end
588
+ );
589
+
590
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/instr_queue.sv ADDED
@@ -0,0 +1,565 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 - 2019 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Author: Florian Zaruba, ETH Zurich
12
+ // Date: 26.10.2018sim:/ariane_tb/dut/i_ariane/i_frontend/icache_ex_valid_q
13
+
14
+ // Description: Instruction Queue, separates instruction front-end from processor
15
+ // back-end.
16
+ //
17
+ // This is an optimized instruction queue which supports the handling of
18
+ // compressed instructions (16 bit instructions). Internally it is organized as
19
+ // FETCH_ENTRY x 32 bit queues which are filled in a consecutive manner. Two pointers
20
+ // point into (`idx_is_q` and `idx_ds_q`) the fill port and the read port. The read port
21
+ // is designed so that it will easily allow for multiple issue implementation.
22
+ // The input supports arbitrary power of two instruction fetch widths.
23
+ //
24
+ // The queue supports handling of branch prediction and will take care of
25
+ // only saving a valid instruction stream.
26
+ //
27
+ // Furthermore it contains a replay interface in case the instruction queue
28
+ // is already full. As instructions are in general easily replayed this should
29
+ // increase the efficiency as I$ misses are potentially hidden. This stands in
30
+ // contrast to pessimistic actions (early stalling) or credit based approaches.
31
+ // Credit based systems might be difficult to implement with the current system
32
+ // as we do not exactly know how much space we are going to need in the fifos
33
+ // as each instruction can take either one or two slots.
34
+ //
35
+ // So the consumed/valid interface degenerates to a `information` interface. If the
36
+ // upstream circuits keeps pushing the queue will discard the information
37
+ // and start replaying from the point were it could last manage to accept instructions.
38
+ //
39
+ // The instruction front-end will stop issuing instructions as soon as the
40
+ // fifo is full. This will gate the logic if the processor is e.g.: halted
41
+ //
42
+ // TODO(zarubaf): The instruction queues can be reduced to 16 bit. Potentially
43
+ // the replay mechanism gets more complicated as it can be that a 32 bit instruction
44
+ // can not be pushed at once.
45
+
46
+ module instr_queue
47
+ import ariane_pkg::*;
48
+ #(
49
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
50
+ parameter type fetch_entry_t = logic
51
+ ) (
52
+ // Subsystem Clock - SUBSYSTEM
53
+ input logic clk_i,
54
+ // Asynchronous reset active low - SUBSYSTEM
55
+ input logic rst_ni,
56
+ // Fetch flush request - CONTROLLER
57
+ input logic flush_i,
58
+ // Instruction - instr_realign
59
+ input logic [CVA6Cfg.INSTR_PER_FETCH-1:0][31:0] instr_i,
60
+ // Instruction address - instr_realign
61
+ input logic [CVA6Cfg.INSTR_PER_FETCH-1:0][CVA6Cfg.VLEN-1:0] addr_i,
62
+ // Instruction is valid - instr_realign
63
+ input logic [CVA6Cfg.INSTR_PER_FETCH-1:0] valid_i,
64
+ // Handshake’s ready with CACHE - CACHE
65
+ output logic ready_o,
66
+ // Indicates instructions consummed, or popped by ID_STAGE - FRONTEND
67
+ output logic [CVA6Cfg.INSTR_PER_FETCH-1:0] consumed_o,
68
+ // Exception (which is page-table fault) - CACHE
69
+ input ariane_pkg::frontend_exception_t exception_i,
70
+ // Exception address - CACHE
71
+ input logic [CVA6Cfg.VLEN-1:0] exception_addr_i,
72
+ input logic [CVA6Cfg.GPLEN-1:0] exception_gpaddr_i,
73
+ input logic [31:0] exception_tinst_i,
74
+ input logic exception_gva_i,
75
+ // Branch predict - FRONTEND
76
+ input logic [CVA6Cfg.VLEN-1:0] predict_address_i,
77
+ // Instruction predict address - FRONTEND
78
+ input ariane_pkg::cf_t [CVA6Cfg.INSTR_PER_FETCH-1:0] cf_type_i,
79
+ // Replay instruction because one of the FIFO was full - FRONTEND
80
+ output logic replay_o,
81
+ // Address at which to replay the fetch - FRONTEND
82
+ output logic [CVA6Cfg.VLEN-1:0] replay_addr_o,
83
+ // Handshake’s data with ID_STAGE - ID_STAGE
84
+ output fetch_entry_t [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_o,
85
+ // Handshake’s valid with ID_STAGE - ID_STAGE
86
+ output logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_valid_o,
87
+ // Handshake’s ready with ID_STAGE - ID_STAGE
88
+ input logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_ready_i
89
+ );
90
+
91
+ // Calculate next index based on whether superscalar is enabled or not.
92
+ localparam NID = CVA6Cfg.SuperscalarEn ? 1 : 0;
93
+
94
+ typedef struct packed {
95
+ logic [31:0] instr; // instruction word
96
+ ariane_pkg::cf_t cf; // branch was taken
97
+ ariane_pkg::frontend_exception_t ex; // exception happened
98
+ logic [CVA6Cfg.VLEN-1:0] ex_vaddr; // lower VLEN bits of tval for exception
99
+ logic [CVA6Cfg.GPLEN-1:0] ex_gpaddr; // lower GPLEN bits of tval2 for exception
100
+ logic [31:0] ex_tinst; // tinst of exception
101
+ logic ex_gva;
102
+ } instr_data_t;
103
+
104
+ logic [CVA6Cfg.LOG2_INSTR_PER_FETCH-1:0] branch_index;
105
+ // instruction queues
106
+ instr_data_t [CVA6Cfg.INSTR_PER_FETCH-1:0] instr_data_in, instr_data_out;
107
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] push_instr, push_instr_fifo;
108
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] pop_instr;
109
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] instr_queue_full;
110
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] instr_queue_empty;
111
+ logic instr_overflow;
112
+ // address queue
113
+ logic [ CVA6Cfg.VLEN-1:0] address_out;
114
+ logic pop_address;
115
+ logic push_address;
116
+ logic full_address;
117
+ logic address_overflow;
118
+ // input stream counter
119
+ logic [CVA6Cfg.LOG2_INSTR_PER_FETCH-1:0] idx_is_d, idx_is_q;
120
+
121
+ // Registers
122
+ // output FIFO select, one-hot
123
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] idx_ds_d, idx_ds_q;
124
+ // rotated by N
125
+ logic [CVA6Cfg.NrIssuePorts:0][CVA6Cfg.INSTR_PER_FETCH-1:0] idx_ds;
126
+
127
+ logic [CVA6Cfg.VLEN-1:0] pc_d, pc_q; // current PC
128
+ logic [CVA6Cfg.NrIssuePorts:0][CVA6Cfg.VLEN-1:0] pc_j;
129
+ logic reset_address_d, reset_address_q; // we need to re-set the address because of a flush
130
+
131
+ logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_is_cf, fetch_entry_fire;
132
+
133
+ logic [CVA6Cfg.INSTR_PER_FETCH*2-2:0] branch_mask_extended;
134
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] branch_mask;
135
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] taken;
136
+ // shift amount, e.g.: instructions we want to retire
137
+ logic [CVA6Cfg.LOG2_INSTR_PER_FETCH:0] popcount;
138
+ logic [CVA6Cfg.LOG2_INSTR_PER_FETCH-1:0] shamt;
139
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] valid;
140
+ logic [CVA6Cfg.INSTR_PER_FETCH*2-1:0] consumed_extended;
141
+ // FIFO mask
142
+ logic [CVA6Cfg.INSTR_PER_FETCH*2-1:0] fifo_pos_extended;
143
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] fifo_pos;
144
+ logic [CVA6Cfg.INSTR_PER_FETCH*2-1:0][31:0] instr;
145
+ ariane_pkg::cf_t [CVA6Cfg.INSTR_PER_FETCH*2-1:0] cf;
146
+ // replay interface
147
+ logic [CVA6Cfg.INSTR_PER_FETCH-1:0] instr_overflow_fifo;
148
+
149
+ assign ready_o = ~(|instr_queue_full) & ~full_address;
150
+
151
+ if (CVA6Cfg.RVC) begin : gen_multiple_instr_per_fetch_with_C
152
+
153
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_unpack_taken
154
+ assign taken[i] = cf_type_i[i] != ariane_pkg::NoCF;
155
+ end
156
+
157
+ // calculate a branch mask, e.g.: get the first taken branch
158
+ lzc #(
159
+ .WIDTH(CVA6Cfg.INSTR_PER_FETCH),
160
+ .MODE (0) // count trailing zeros
161
+ ) i_lzc_branch_index (
162
+ .in_i (taken), // we want to count trailing zeros
163
+ .cnt_o (branch_index), // first branch on branch_index
164
+ .empty_o()
165
+ );
166
+
167
+
168
+ // the first index is for sure valid
169
+ // for example (64 bit fetch):
170
+ // taken mask: 0 1 1 0
171
+ // leading zero count = 1
172
+ // 0 0 0 1, 1 1 1 << 1 = 0 0 1 1, 1 1 0
173
+ // take the upper 4 bits: 0 0 1 1
174
+ assign branch_mask_extended = {{{CVA6Cfg.INSTR_PER_FETCH-1}{1'b0}}, {{CVA6Cfg.INSTR_PER_FETCH}{1'b1}}} << branch_index;
175
+ assign branch_mask = branch_mask_extended[CVA6Cfg.INSTR_PER_FETCH * 2 - 2:CVA6Cfg.INSTR_PER_FETCH - 1];
176
+
177
+ // mask with taken branches to get the actual amount of instructions we want to push
178
+ assign valid = valid_i & branch_mask;
179
+ // rotate right again
180
+ assign consumed_extended = {push_instr_fifo, push_instr_fifo} >> idx_is_q;
181
+ assign consumed_o = consumed_extended[CVA6Cfg.INSTR_PER_FETCH-1:0];
182
+ // count the numbers of valid instructions we've pushed from this package
183
+ popcount #(
184
+ .INPUT_WIDTH(CVA6Cfg.INSTR_PER_FETCH)
185
+ ) i_popcount (
186
+ .data_i (push_instr_fifo),
187
+ .popcount_o(popcount)
188
+ );
189
+ assign shamt = popcount[$bits(shamt)-1:0];
190
+
191
+ // save the shift amount for next cycle
192
+ assign idx_is_d = idx_is_q + shamt;
193
+
194
+ // ----------------------
195
+ // Input interface
196
+ // ----------------------
197
+ // rotate left by the current position
198
+ assign fifo_pos_extended = {valid, valid} << idx_is_q;
199
+ // we just care about the upper bits
200
+ assign fifo_pos = fifo_pos_extended[CVA6Cfg.INSTR_PER_FETCH*2-1:CVA6Cfg.INSTR_PER_FETCH];
201
+ // the fifo_position signal can directly be used to guide the push signal of each FIFO
202
+ // make sure it is not full
203
+ assign push_instr = fifo_pos & ~instr_queue_full;
204
+
205
+ // duplicate the entries for easier selection e.g.: 3 2 1 0 3 2 1 0
206
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_duplicate_instr_input
207
+ assign instr[i] = instr_i[i];
208
+ assign instr[i+CVA6Cfg.INSTR_PER_FETCH] = instr_i[i];
209
+ assign cf[i] = cf_type_i[i];
210
+ assign cf[i+CVA6Cfg.INSTR_PER_FETCH] = cf_type_i[i];
211
+ end
212
+
213
+ // shift the inputs
214
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_fifo_input_select
215
+ /* verilator lint_off WIDTH */
216
+ assign instr_data_in[i].instr = instr[CVA6Cfg.INSTR_PER_FETCH+i-idx_is_q];
217
+ assign instr_data_in[i].cf = cf[CVA6Cfg.INSTR_PER_FETCH+i-idx_is_q];
218
+ assign instr_data_in[i].ex = exception_i; // exceptions hold for the whole fetch packet
219
+ assign instr_data_in[i].ex_vaddr = exception_addr_i;
220
+ if (CVA6Cfg.RVH) begin : gen_hyp_ex_with_C
221
+ assign instr_data_in[i].ex_gpaddr = exception_gpaddr_i;
222
+ assign instr_data_in[i].ex_tinst = exception_tinst_i;
223
+ assign instr_data_in[i].ex_gva = exception_gva_i;
224
+ end else begin : gen_no_hyp_ex_with_C
225
+ assign instr_data_in[i].ex_gpaddr = '0;
226
+ assign instr_data_in[i].ex_tinst = '0;
227
+ assign instr_data_in[i].ex_gva = 1'b0;
228
+ end
229
+ /* verilator lint_on WIDTH */
230
+ end
231
+ end else begin : gen_multiple_instr_per_fetch_without_C
232
+
233
+ assign taken = '0;
234
+ assign branch_index = '0;
235
+ assign branch_mask_extended = '0;
236
+ assign branch_mask = '0;
237
+ assign consumed_extended = '0;
238
+ assign fifo_pos_extended = '0;
239
+ assign fifo_pos = '0;
240
+ assign instr = '0;
241
+ assign popcount = '0;
242
+ assign shamt = '0;
243
+ assign valid = '0;
244
+
245
+
246
+ assign consumed_o = push_instr_fifo[0];
247
+ // ----------------------
248
+ // Input interface
249
+ // ----------------------
250
+ assign push_instr = valid_i & ~instr_queue_full;
251
+
252
+ /* verilator lint_off WIDTH */
253
+ assign instr_data_in[0].instr = instr_i[0];
254
+ assign instr_data_in[0].cf = cf_type_i[0];
255
+ assign instr_data_in[0].ex = exception_i; // exceptions hold for the whole fetch packet
256
+ assign instr_data_in[0].ex_vaddr = exception_addr_i;
257
+ if (CVA6Cfg.RVH) begin : gen_hyp_ex_without_C
258
+ assign instr_data_in[0].ex_gpaddr = exception_gpaddr_i;
259
+ assign instr_data_in[0].ex_tinst = exception_tinst_i;
260
+ assign instr_data_in[0].ex_gva = exception_gva_i;
261
+ end else begin : gen_no_hyp_ex_without_C
262
+ assign instr_data_in[0].ex_gpaddr = '0;
263
+ assign instr_data_in[0].ex_tinst = '0;
264
+ assign instr_data_in[0].ex_gva = 1'b0;
265
+ end
266
+ /* verilator lint_on WIDTH */
267
+ end
268
+
269
+ // ----------------------
270
+ // Replay Logic
271
+ // ----------------------
272
+ // We need to replay a instruction fetch iff:
273
+ // 1. One of the instruction data FIFOs was full and we needed it
274
+ // (e.g.: we pushed and it was full)
275
+ // 2. The address/branch predict FIFO was full
276
+ // if one of the FIFOs was full we need to replay the faulting instruction
277
+ if (CVA6Cfg.RVC == 1'b1) begin : gen_instr_overflow_fifo_with_C
278
+ assign instr_overflow_fifo = instr_queue_full & fifo_pos;
279
+ end else begin : gen_instr_overflow_fifo_without_C
280
+ assign instr_overflow_fifo = instr_queue_full & valid_i;
281
+ end
282
+ assign instr_overflow = |instr_overflow_fifo; // at least one instruction overflowed
283
+ assign address_overflow = full_address & push_address;
284
+ assign replay_o = instr_overflow | address_overflow;
285
+
286
+ if (CVA6Cfg.RVC) begin : gen_replay_addr_o_with_c
287
+ // select the address, in the case of an address fifo overflow just
288
+ // use the base of this package
289
+ // if we successfully pushed some instructions we can output the next instruction
290
+ // which we didn't manage to push
291
+ assign replay_addr_o = (address_overflow) ? addr_i[0] : addr_i[shamt];
292
+ end else begin : gen_replay_addr_o_without_C
293
+ assign replay_addr_o = addr_i[0];
294
+ end
295
+
296
+ // ----------------------
297
+ // Downstream interface
298
+ // ----------------------
299
+ // as long as there is at least one queue which can take the value we have a valid instruction
300
+ assign fetch_entry_valid_o[0] = ~(&instr_queue_empty);
301
+ if (CVA6Cfg.SuperscalarEn) begin : gen_fetch_entry_valid_1
302
+ // TODO Maybe this additional fetch_entry_is_cf check is useless as issue-stage already performs it?
303
+ assign fetch_entry_valid_o[NID] = ~|(instr_queue_empty & idx_ds[1]) & ~(&fetch_entry_is_cf);
304
+ end
305
+
306
+ assign idx_ds[0] = idx_ds_q;
307
+ for (genvar i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
308
+ if (CVA6Cfg.INSTR_PER_FETCH > 1) begin
309
+ assign idx_ds[i+1] = {
310
+ idx_ds[i][CVA6Cfg.INSTR_PER_FETCH-2:0], idx_ds[i][CVA6Cfg.INSTR_PER_FETCH-1]
311
+ };
312
+ end else begin
313
+ assign idx_ds[i+1] = idx_ds[i];
314
+ end
315
+ end
316
+
317
+ if (CVA6Cfg.RVC) begin : gen_downstream_itf_with_c
318
+ always_comb begin
319
+ idx_ds_d = idx_ds_q;
320
+
321
+ pop_instr = '0;
322
+ // assemble fetch entry
323
+ for (int unsigned i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
324
+ fetch_entry_o[i].instruction = '0;
325
+ fetch_entry_o[i].address = pc_j[i];
326
+ fetch_entry_o[i].ex.valid = 1'b0;
327
+ fetch_entry_o[i].ex.cause = '0;
328
+
329
+ fetch_entry_o[i].ex.tval = '0;
330
+ fetch_entry_o[i].ex.tval2 = '0;
331
+ fetch_entry_o[i].ex.gva = 1'b0;
332
+ fetch_entry_o[i].ex.tinst = '0;
333
+ fetch_entry_o[i].branch_predict.predict_address = address_out;
334
+ fetch_entry_o[i].branch_predict.cf = ariane_pkg::NoCF;
335
+ end
336
+
337
+ // output mux select
338
+ for (int unsigned i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
339
+ // TODO handle fetch_entry_o[1] if superscalar
340
+ if (idx_ds[0][i]) begin
341
+ if (instr_data_out[i].ex == ariane_pkg::FE_INSTR_ACCESS_FAULT) begin
342
+ fetch_entry_o[0].ex.cause = riscv::INSTR_ACCESS_FAULT;
343
+ end else if (CVA6Cfg.RVH && instr_data_out[i].ex == ariane_pkg::FE_INSTR_GUEST_PAGE_FAULT) begin
344
+ fetch_entry_o[0].ex.cause = riscv::INSTR_GUEST_PAGE_FAULT;
345
+ end else begin
346
+ fetch_entry_o[0].ex.cause = riscv::INSTR_PAGE_FAULT;
347
+ end
348
+ fetch_entry_o[0].instruction = instr_data_out[i].instr;
349
+ fetch_entry_o[0].ex.valid = instr_data_out[i].ex != ariane_pkg::FE_NONE;
350
+ if (CVA6Cfg.TvalEn)
351
+ fetch_entry_o[0].ex.tval = {
352
+ {(CVA6Cfg.XLEN - CVA6Cfg.VLEN) {1'b0}}, instr_data_out[i].ex_vaddr
353
+ };
354
+ if (CVA6Cfg.RVH) begin
355
+ fetch_entry_o[0].ex.tval2 = instr_data_out[i].ex_gpaddr;
356
+ fetch_entry_o[0].ex.tinst = instr_data_out[i].ex_tinst;
357
+ fetch_entry_o[0].ex.gva = instr_data_out[i].ex_gva;
358
+ end
359
+ fetch_entry_o[0].branch_predict.cf = instr_data_out[i].cf;
360
+ pop_instr[i] = fetch_entry_fire[0];
361
+ end
362
+
363
+ if (CVA6Cfg.SuperscalarEn) begin
364
+ if (idx_ds[1][i]) begin
365
+ if (instr_data_out[i].ex == ariane_pkg::FE_INSTR_ACCESS_FAULT) begin
366
+ fetch_entry_o[NID].ex.cause = riscv::INSTR_ACCESS_FAULT;
367
+ end else begin
368
+ fetch_entry_o[NID].ex.cause = riscv::INSTR_PAGE_FAULT;
369
+ end
370
+ fetch_entry_o[NID].instruction = instr_data_out[i].instr;
371
+ fetch_entry_o[NID].ex.valid = instr_data_out[i].ex != ariane_pkg::FE_NONE;
372
+ fetch_entry_o[NID].ex.tval = {{64 - CVA6Cfg.VLEN{1'b0}}, instr_data_out[i].ex_vaddr};
373
+ fetch_entry_o[NID].branch_predict.cf = instr_data_out[i].cf;
374
+ // Cannot output two CF the same cycle.
375
+ pop_instr[i] = fetch_entry_fire[NID];
376
+ end
377
+ end
378
+ end
379
+ // rotate the pointer left
380
+ if (fetch_entry_fire[0]) begin
381
+ if (CVA6Cfg.SuperscalarEn) begin
382
+ idx_ds_d = fetch_entry_fire[NID] ? idx_ds[2] : idx_ds[1];
383
+ end else begin
384
+ idx_ds_d = idx_ds[1];
385
+ end
386
+ end
387
+ end
388
+ end else begin : gen_downstream_itf_without_c
389
+ always_comb begin
390
+ idx_ds_d = '0;
391
+ idx_is_d = '0;
392
+ fetch_entry_o[0].instruction = instr_data_out[0].instr;
393
+ fetch_entry_o[0].address = pc_q;
394
+
395
+ fetch_entry_o[0].ex.valid = instr_data_out[0].ex != ariane_pkg::FE_NONE;
396
+ if (instr_data_out[0].ex == ariane_pkg::FE_INSTR_ACCESS_FAULT) begin
397
+ fetch_entry_o[0].ex.cause = riscv::INSTR_ACCESS_FAULT;
398
+ end else begin
399
+ fetch_entry_o[0].ex.cause = riscv::INSTR_PAGE_FAULT;
400
+ end
401
+ if (CVA6Cfg.TvalEn)
402
+ fetch_entry_o[0].ex.tval = {{64 - CVA6Cfg.VLEN{1'b0}}, instr_data_out[0].ex_vaddr};
403
+ else fetch_entry_o[0].ex.tval = '0;
404
+ if (CVA6Cfg.RVH) begin
405
+ fetch_entry_o[0].ex.tval2 = instr_data_out[0].ex_gpaddr;
406
+ fetch_entry_o[0].ex.tinst = instr_data_out[0].ex_tinst;
407
+ fetch_entry_o[0].ex.gva = instr_data_out[0].ex_gva;
408
+ end else begin
409
+ fetch_entry_o[0].ex.tval2 = '0;
410
+ fetch_entry_o[0].ex.tinst = '0;
411
+ fetch_entry_o[0].ex.gva = 1'b0;
412
+ end
413
+
414
+ fetch_entry_o[0].branch_predict.predict_address = address_out;
415
+ fetch_entry_o[0].branch_predict.cf = instr_data_out[0].cf;
416
+
417
+ pop_instr[0] = fetch_entry_valid_o[0] & fetch_entry_ready_i[0];
418
+ end
419
+ end
420
+
421
+ for (genvar i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
422
+ assign fetch_entry_is_cf[i] = fetch_entry_o[i].branch_predict.cf != ariane_pkg::NoCF;
423
+ assign fetch_entry_fire[i] = fetch_entry_valid_o[i] & fetch_entry_ready_i[i];
424
+ end
425
+
426
+ assign pop_address = |(fetch_entry_is_cf & fetch_entry_fire);
427
+
428
+ // ----------------------
429
+ // Calculate (Next) PC
430
+ // ----------------------
431
+ assign pc_j[0] = pc_q;
432
+ for (genvar i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
433
+ assign pc_j[i+1] = fetch_entry_is_cf[i] ? address_out : (
434
+ pc_j[i] + ((fetch_entry_o[i].instruction[1:0] != 2'b11) ? 'd2 : 'd4)
435
+ );
436
+ end
437
+
438
+ always_comb begin
439
+ pc_d = pc_q;
440
+ reset_address_d = flush_i ? 1'b1 : reset_address_q;
441
+
442
+ if (fetch_entry_fire[0]) begin
443
+ pc_d = pc_j[1];
444
+ if (CVA6Cfg.SuperscalarEn) begin
445
+ if (fetch_entry_fire[NID]) begin
446
+ pc_d = pc_j[2];
447
+ end
448
+ end
449
+ end
450
+
451
+ // we previously flushed so we need to reset the address
452
+ if (valid_i[0] && reset_address_q) begin
453
+ // this is the base of the first instruction
454
+ pc_d = addr_i[0];
455
+ reset_address_d = 1'b0;
456
+ end
457
+ end
458
+
459
+ // FIFOs
460
+ for (genvar i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin : gen_instr_fifo
461
+ // Make sure we don't save any instructions if we couldn't save the address
462
+ assign push_instr_fifo[i] = push_instr[i] & ~address_overflow;
463
+ cva6_fifo_v3 #(
464
+ .FPGA_ALTERA(CVA6Cfg.FpgaAlteraEn),
465
+ .DEPTH(ariane_pkg::FETCH_FIFO_DEPTH),
466
+ .dtype(instr_data_t),
467
+ .FPGA_EN(CVA6Cfg.FpgaEn)
468
+ ) i_fifo_instr_data (
469
+ .clk_i (clk_i),
470
+ .rst_ni (rst_ni),
471
+ .flush_i (flush_i),
472
+ .testmode_i(1'b0),
473
+ .full_o (instr_queue_full[i]),
474
+ .empty_o (instr_queue_empty[i]),
475
+ .usage_o (),
476
+ .data_i (instr_data_in[i]),
477
+ .push_i (push_instr_fifo[i]),
478
+ .data_o (instr_data_out[i]),
479
+ .pop_i (pop_instr[i])
480
+ );
481
+ end
482
+ // or reduce and check whether we are retiring a taken branch (might be that the corresponding)
483
+ // fifo is full.
484
+ always_comb begin
485
+ push_address = 1'b0;
486
+ // check if we are pushing a ctrl flow change, if so save the address
487
+ for (int i = 0; i < CVA6Cfg.INSTR_PER_FETCH; i++) begin
488
+ push_address |= push_instr[i] & (instr_data_in[i].cf != ariane_pkg::NoCF);
489
+ end
490
+ end
491
+
492
+ cva6_fifo_v3 #(
493
+ .FPGA_ALTERA(CVA6Cfg.FpgaAlteraEn),
494
+ .DEPTH (ariane_pkg::FETCH_ADDR_FIFO_DEPTH),
495
+ .DATA_WIDTH (CVA6Cfg.VLEN),
496
+ .FPGA_EN (CVA6Cfg.FpgaEn)
497
+ ) i_fifo_address (
498
+ .clk_i (clk_i),
499
+ .rst_ni (rst_ni),
500
+ .flush_i (flush_i),
501
+ .testmode_i(1'b0),
502
+ .full_o (full_address),
503
+ .empty_o (),
504
+ .usage_o (),
505
+ .data_i (predict_address_i),
506
+ .push_i (push_address & ~full_address),
507
+ .data_o (address_out),
508
+ .pop_i (pop_address)
509
+ );
510
+
511
+ unread i_unread_branch_mask (.d_i(|branch_mask_extended));
512
+ unread i_unread_fifo_pos (.d_i(|fifo_pos_extended)); // we don't care about the lower signals
513
+
514
+ if (CVA6Cfg.RVC) begin : gen_pc_q_with_c
515
+ always_ff @(posedge clk_i or negedge rst_ni) begin
516
+ if (!rst_ni) begin
517
+ idx_ds_q <= 'b1;
518
+ idx_is_q <= '0;
519
+ pc_q <= '0;
520
+ reset_address_q <= 1'b1;
521
+ end else begin
522
+ pc_q <= pc_d;
523
+ reset_address_q <= reset_address_d;
524
+ if (flush_i) begin
525
+ // one-hot encoded
526
+ idx_ds_q <= 'b1;
527
+ // binary encoded
528
+ idx_is_q <= '0;
529
+ reset_address_q <= 1'b1;
530
+ end else begin
531
+ idx_ds_q <= idx_ds_d;
532
+ idx_is_q <= idx_is_d;
533
+ end
534
+ end
535
+ end
536
+ end else begin : gen_pc_q_without_C
537
+ assign idx_ds_q = '0;
538
+ assign idx_is_q = '0;
539
+ always_ff @(posedge clk_i or negedge rst_ni) begin
540
+ if (!rst_ni) begin
541
+ pc_q <= '0;
542
+ reset_address_q <= 1'b1;
543
+ end else begin
544
+ pc_q <= pc_d;
545
+ reset_address_q <= reset_address_d;
546
+ if (flush_i) begin
547
+ reset_address_q <= 1'b1;
548
+ end
549
+ end
550
+ end
551
+ end
552
+
553
+ // pragma translate_off
554
+ replay_address_fifo :
555
+ assert property (@(posedge clk_i) disable iff (!rst_ni) replay_o |-> !i_fifo_address.push_i)
556
+ else $fatal(1, "[instr_queue] Pushing address although replay asserted");
557
+
558
+ output_select_onehot :
559
+ assert property (@(posedge clk_i) $onehot0(idx_ds_q))
560
+ else begin
561
+ $error("Output select should be one-hot encoded");
562
+ $stop();
563
+ end
564
+ // pragma translate_on
565
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/instr_scan.sv ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 - 2019 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 2.0 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-2.0. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ // Author: Florian Zaruba, ETH Zurich
11
+ // Date: 08.02.2018
12
+ // Migrated: Luis Vitorio Cargnini, IEEE
13
+ // Date: 09.06.2018
14
+
15
+ // ------------------------------
16
+ // Instruction Scanner
17
+ // ------------------------------
18
+ module instr_scan #(
19
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty
20
+ ) (
21
+ // Instruction to be predecoded - instr_realign
22
+ input logic [31:0] instr_i,
23
+ // Return instruction - FRONTEND
24
+ output logic rvi_return_o,
25
+ // JAL instruction - FRONTEND
26
+ output logic rvi_call_o,
27
+ // Branch instruction - FRONTEND
28
+ output logic rvi_branch_o,
29
+ // JALR instruction - FRONTEND
30
+ output logic rvi_jalr_o,
31
+ // Unconditional jump instruction - FRONTEND
32
+ output logic rvi_jump_o,
33
+ // Instruction immediate - FRONTEND
34
+ output logic [CVA6Cfg.VLEN-1:0] rvi_imm_o,
35
+ // Branch compressed instruction - FRONTEND
36
+ output logic rvc_branch_o,
37
+ // Unconditional jump compressed instruction - FRONTEND
38
+ output logic rvc_jump_o,
39
+ // JR compressed instruction - FRONTEND
40
+ output logic rvc_jr_o,
41
+ // Return compressed instruction - FRONTEND
42
+ output logic rvc_return_o,
43
+ // JALR compressed instruction - FRONTEND
44
+ output logic rvc_jalr_o,
45
+ // JAL compressed instruction - FRONTEND
46
+ output logic rvc_call_o,
47
+ // Instruction compressed immediate - FRONTEND
48
+ output logic [CVA6Cfg.VLEN-1:0] rvc_imm_o
49
+ );
50
+
51
+ function automatic logic [CVA6Cfg.VLEN-1:0] uj_imm(logic [31:0] instruction_i);
52
+ return {
53
+ {44 + CVA6Cfg.VLEN - 64{instruction_i[31]}},
54
+ instruction_i[19:12],
55
+ instruction_i[20],
56
+ instruction_i[30:21],
57
+ 1'b0
58
+ };
59
+ endfunction
60
+
61
+ function automatic logic [CVA6Cfg.VLEN-1:0] sb_imm(logic [31:0] instruction_i);
62
+ return {
63
+ {51 + CVA6Cfg.VLEN - 64{instruction_i[31]}},
64
+ instruction_i[31],
65
+ instruction_i[7],
66
+ instruction_i[30:25],
67
+ instruction_i[11:8],
68
+ 1'b0
69
+ };
70
+ endfunction
71
+
72
+ logic rv32_rvc_jal;
73
+ assign rv32_rvc_jal = (CVA6Cfg.XLEN == 32) & ((instr_i[15:13] == riscv::OpcodeC1Jal) & (instr_i[1:0] == riscv::OpcodeC1));
74
+
75
+ logic is_xret;
76
+ assign is_xret = logic'(instr_i[31:30] == 2'b00) & logic'(instr_i[28:0] == 29'b10000001000000000000001110011);
77
+
78
+ // check that rs1 is either x1 or x5 and that rd is not rs1
79
+ assign rvi_return_o = rvi_jalr_o & ((instr_i[19:15] == 5'd1) | instr_i[19:15] == 5'd5)
80
+ & (instr_i[19:15] != instr_i[11:7]);
81
+ // Opocde is JAL[R] and destination register is either x1 or x5
82
+ assign rvi_call_o = (rvi_jalr_o | rvi_jump_o) & ((instr_i[11:7] == 5'd1) | instr_i[11:7] == 5'd5);
83
+ // differentiates between JAL and BRANCH opcode, JALR comes from BHT
84
+ assign rvi_imm_o = is_xret ? '0 : (instr_i[3]) ? uj_imm(instr_i) : sb_imm(instr_i);
85
+ assign rvi_branch_o = (instr_i[6:0] == riscv::OpcodeBranch);
86
+ assign rvi_jalr_o = (instr_i[6:0] == riscv::OpcodeJalr);
87
+ assign rvi_jump_o = logic'(instr_i[6:0] == riscv::OpcodeJal) | is_xret;
88
+
89
+ // opcode JAL
90
+ assign rvc_jump_o = ((instr_i[15:13] == riscv::OpcodeC1J) & (instr_i[1:0] == riscv::OpcodeC1)) | rv32_rvc_jal;
91
+
92
+ // always links to register 0
93
+ logic is_jal_r;
94
+ assign is_jal_r = (instr_i[15:13] == riscv::OpcodeC2JalrMvAdd)
95
+ & (instr_i[6:2] == 5'b00000)
96
+ & (instr_i[1:0] == riscv::OpcodeC2);
97
+ assign rvc_jr_o = is_jal_r & ~instr_i[12];
98
+ // always links to register 1 e.g.: it is a jump
99
+ assign rvc_jalr_o = is_jal_r & instr_i[12];
100
+ assign rvc_call_o = rvc_jalr_o | rv32_rvc_jal;
101
+
102
+ assign rvc_branch_o = ((instr_i[15:13] == riscv::OpcodeC1Beqz) | (instr_i[15:13] == riscv::OpcodeC1Bnez))
103
+ & (instr_i[1:0] == riscv::OpcodeC1);
104
+ // check that rs1 is x1 or x5
105
+ assign rvc_return_o = ((instr_i[11:7] == 5'd1) | (instr_i[11:7] == 5'd5)) & rvc_jr_o;
106
+
107
+ // differentiates between JAL and BRANCH opcode, JALR comes from BHT
108
+ assign rvc_imm_o = (instr_i[14]) ? {{56+CVA6Cfg.VLEN-64{instr_i[12]}}, instr_i[6:5], instr_i[2], instr_i[11:10], instr_i[4:3], 1'b0}
109
+ : {{53+CVA6Cfg.VLEN-64{instr_i[12]}}, instr_i[8], instr_i[10:9], instr_i[6], instr_i[7], instr_i[2], instr_i[11], instr_i[5:3], 1'b0};
110
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/frontend/ras.sv ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //Copyright (C) 2018 to present,
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 2.0 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-2.0. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Author: Florian Zaruba, ETH Zurich
12
+ // Date: 08.02.2018
13
+ // Migrated: Luis Vitorio Cargnini, IEEE
14
+ // Date: 09.06.2018
15
+
16
+ // return address stack
17
+ module ras #(
18
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
19
+ parameter type ras_t = logic,
20
+ parameter int unsigned DEPTH = 2
21
+ ) (
22
+ // Subsystem Clock - SUBSYSTEM
23
+ input logic clk_i,
24
+ // Asynchronous reset active low - SUBSYSTEM
25
+ input logic rst_ni,
26
+ // Branch prediction flush request - zero
27
+ input logic flush_bp_i,
28
+ // Push address in RAS - FRONTEND
29
+ input logic push_i,
30
+ // Pop address from RAS - FRONTEND
31
+ input logic pop_i,
32
+ // Data to be pushed - FRONTEND
33
+ input logic [CVA6Cfg.VLEN-1:0] data_i,
34
+ // Popped data - FRONTEND
35
+ output ras_t data_o
36
+ );
37
+
38
+ ras_t [DEPTH-1:0] stack_d, stack_q;
39
+
40
+ assign data_o = stack_q[0];
41
+
42
+ always_comb begin
43
+ stack_d = stack_q;
44
+
45
+ // push on the stack
46
+ if (push_i) begin
47
+ stack_d[0].ra = data_i;
48
+ // mark the new return address as valid
49
+ stack_d[0].valid = 1'b1;
50
+ stack_d[DEPTH-1:1] = stack_q[DEPTH-2:0];
51
+ end
52
+
53
+ if (pop_i) begin
54
+ stack_d[DEPTH-2:0] = stack_q[DEPTH-1:1];
55
+ // we popped the value so invalidate the end of the stack
56
+ stack_d[DEPTH-1].valid = 1'b0;
57
+ stack_d[DEPTH-1].ra = 'b0;
58
+ end
59
+ // leave everything untouched and just push the latest value to the
60
+ // top of the stack
61
+ if (pop_i && push_i) begin
62
+ stack_d = stack_q;
63
+ stack_d[0].ra = data_i;
64
+ stack_d[0].valid = 1'b1;
65
+ end
66
+
67
+ if (flush_bp_i) begin
68
+ stack_d = '0;
69
+ end
70
+ end
71
+
72
+ always_ff @(posedge clk_i or negedge rst_ni) begin
73
+ if (~rst_ni) begin
74
+ stack_q <= '0;
75
+ end else begin
76
+ stack_q <= stack_d;
77
+ end
78
+ end
79
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/id_stage.sv ADDED
@@ -0,0 +1,452 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 ETH Zurich and University of Bologna.
2
+ // Copyright and related rights are licensed under the Solderpad Hardware
3
+ // License, Version 0.51 (the "License"); you may not use this file except in
4
+ // compliance with the License. You may obtain a copy of the License at
5
+ // http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ // or agreed to in writing, software, hardware and materials distributed under
7
+ // this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ // specific language governing permissions and limitations under the License.
10
+ //
11
+ // Author: Florian Zaruba, ETH Zurich
12
+ // Date: 15.04.2017
13
+ // Description: Instruction decode, contains the logic for decode,
14
+ // issue and read operands.
15
+
16
+ module id_stage #(
17
+ parameter config_pkg::cva6_cfg_t CVA6Cfg = config_pkg::cva6_cfg_empty,
18
+ parameter type branchpredict_sbe_t = logic,
19
+ parameter type dcache_req_i_t = logic,
20
+ parameter type dcache_req_o_t = logic,
21
+ parameter type exception_t = logic,
22
+ parameter type fetch_entry_t = logic,
23
+ parameter type jvt_t = logic,
24
+ parameter type irq_ctrl_t = logic,
25
+ parameter type scoreboard_entry_t = logic,
26
+ parameter type interrupts_t = logic,
27
+ parameter interrupts_t INTERRUPTS = '0,
28
+ parameter type x_compressed_req_t = logic,
29
+ parameter type x_compressed_resp_t = logic
30
+ ) (
31
+ // Subsystem Clock - SUBSYSTEM
32
+ input logic clk_i,
33
+ // Asynchronous reset active low - SUBSYSTEM
34
+ input logic rst_ni,
35
+ // Fetch flush request - CONTROLLER
36
+ input logic flush_i,
37
+ // Debug (async) request - SUBSYSTEM
38
+ input logic debug_req_i,
39
+ // Handshake's data between fetch and decode - FRONTEND
40
+ input fetch_entry_t [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_i,
41
+ // Handshake's valid between fetch and decode - FRONTEND
42
+ input logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_valid_i,
43
+ // Handshake's ready between fetch and decode - FRONTEND
44
+ output logic [CVA6Cfg.NrIssuePorts-1:0] fetch_entry_ready_o,
45
+ // Handshake's data between decode and issue - ISSUE
46
+ output scoreboard_entry_t [CVA6Cfg.NrIssuePorts-1:0] issue_entry_o,
47
+ output scoreboard_entry_t [CVA6Cfg.NrIssuePorts-1:0] issue_entry_o_prev,
48
+ // Instruction value - ISSUE
49
+ output logic [CVA6Cfg.NrIssuePorts-1:0][31:0] orig_instr_o,
50
+ // Handshake's valid between decode and issue - ISSUE
51
+ output logic [CVA6Cfg.NrIssuePorts-1:0] issue_entry_valid_o,
52
+ // Report if instruction is a control flow instruction - ISSUE
53
+ output logic [CVA6Cfg.NrIssuePorts-1:0] is_ctrl_flow_o,
54
+ // Handshake's acknowlege between decode and issue - ISSUE
55
+ input logic [CVA6Cfg.NrIssuePorts-1:0] issue_instr_ack_i,
56
+ // Information dedicated to RVFI - RVFI
57
+ output logic [CVA6Cfg.NrIssuePorts-1:0] rvfi_is_compressed_o,
58
+ // Current privilege level - CSR_REGFILE
59
+ input riscv::priv_lvl_t priv_lvl_i,
60
+ // Current virtualization mode - CSR_REGFILE
61
+ input logic v_i,
62
+ // Floating point extension status - CSR_REGFILE
63
+ input riscv::xs_t fs_i,
64
+ // Floating point extension virtual status - CSR_REGFILE
65
+ input riscv::xs_t vfs_i,
66
+ // Floating point dynamic rounding mode - CSR_REGFILE
67
+ input logic [2:0] frm_i,
68
+ // Vector extension status - CSR_REGFILE
69
+ input riscv::xs_t vs_i,
70
+ // Level sensitive (async) interrupts - SUBSYSTEM
71
+ input logic [1:0] irq_i,
72
+ // Interrupt control status - CSR_REGFILE
73
+ input irq_ctrl_t irq_ctrl_i,
74
+ // Is current mode debug ? - CSR_REGFILE
75
+ input logic debug_mode_i,
76
+ // Trap virtual memory - CSR_REGFILE
77
+ input logic tvm_i,
78
+ // Timeout wait - CSR_REGFILE
79
+ input logic tw_i,
80
+ // Virtual timeout wait - CSR_REGFILE
81
+ input logic vtw_i,
82
+ // Trap sret - CSR_REGFILE
83
+ input logic tsr_i,
84
+ // Hypervisor user mode - CSR_REGFILE
85
+ input logic hu_i,
86
+ // CVXIF Compressed interface
87
+ input logic [CVA6Cfg.XLEN-1:0] hart_id_i,
88
+ input logic compressed_ready_i,
89
+ //JVT
90
+ input jvt_t jvt_i,
91
+ input x_compressed_resp_t compressed_resp_i,
92
+ output logic compressed_valid_o,
93
+ output x_compressed_req_t compressed_req_o,
94
+ // Data cache request ouput - CACHE
95
+ input dcache_req_o_t dcache_req_ports_i,
96
+ // Data cache request input - CACHE
97
+ output dcache_req_i_t dcache_req_ports_o
98
+ );
99
+ // ID/ISSUE register stage
100
+ typedef struct packed {
101
+ logic valid;
102
+ scoreboard_entry_t sbe;
103
+ logic [31:0] orig_instr;
104
+ logic is_ctrl_flow;
105
+ } issue_struct_t;
106
+ issue_struct_t [CVA6Cfg.NrIssuePorts-1:0] issue_n, issue_q;
107
+ // stall required for ZCMP ZCMT CVXIF
108
+ logic [CVA6Cfg.NrIssuePorts-1:0] stall_instr_fetch;
109
+
110
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_control_flow_instr;
111
+ scoreboard_entry_t [CVA6Cfg.NrIssuePorts-1:0] decoded_instruction;
112
+ logic [CVA6Cfg.NrIssuePorts-1:0] decoded_instruction_valid;
113
+ logic [CVA6Cfg.NrIssuePorts-1:0][31:0] orig_instr;
114
+
115
+ // Compressed decoder signals
116
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_illegal_rvc;
117
+ logic [CVA6Cfg.NrIssuePorts-1:0][31:0] instruction_rvc;
118
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_compressed_rvc;
119
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_zcmt_instr;
120
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_macro_instr;
121
+
122
+ // CVXIF compressed interface driver signals
123
+ // Inputs
124
+ logic is_illegal_cvxif_i;
125
+ logic [ 31:0] instruction_cvxif_i;
126
+ logic is_compressed_cvxif_i;
127
+ logic stall_macro_deco;
128
+ // Outputs
129
+ logic is_illegal_cvxif_o;
130
+ logic [ 31:0] instruction_cvxif_o;
131
+ logic is_compressed_cvxif_o;
132
+
133
+ // ZCMP decoder signals
134
+ logic is_illegal_zcmp;
135
+ logic [ 31:0] instruction_zcmp;
136
+ logic is_compressed_zcmp;
137
+ logic stall_macro_deco_zcmp;
138
+ logic is_last_macro_instr;
139
+ logic is_double_rd_macro_instr;
140
+
141
+ // ZCMT decoder signals
142
+ logic is_illegal_zcmt;
143
+ logic [ 31:0] instruction_zcmt;
144
+ logic is_compressed_zcmt;
145
+ logic stall_macro_deco_zcmt;
146
+ logic [ CVA6Cfg.XLEN-1:0] jump_address;
147
+
148
+ // Decoder signals
149
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_illegal_deco;
150
+ logic [CVA6Cfg.NrIssuePorts-1:0][31:0] instruction_deco;
151
+ logic [CVA6Cfg.NrIssuePorts-1:0] is_compressed_deco;
152
+
153
+
154
+ if (CVA6Cfg.RVC) begin
155
+ // ---------------------------------------------------------
156
+ // 1. Check if they are compressed and expand in case they are
157
+ // ---------------------------------------------------------
158
+ for (genvar i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
159
+ compressed_decoder #(
160
+ .CVA6Cfg(CVA6Cfg)
161
+ ) compressed_decoder_i (
162
+ .instr_i (fetch_entry_i[i].instruction),
163
+ .instr_o (instruction_rvc[i]),
164
+ .illegal_instr_o (is_illegal_rvc[i]),
165
+ .is_compressed_o (is_compressed_rvc[i]),
166
+ .is_macro_instr_o(is_macro_instr[i]),
167
+ .is_zcmt_instr_o (is_zcmt_instr[i])
168
+ );
169
+ end
170
+
171
+ if (CVA6Cfg.SuperscalarEn) begin
172
+ assign stall_instr_fetch[1] = is_illegal_rvc[1] || is_macro_instr[1] || is_zcmt_instr[1];
173
+ end
174
+
175
+ if (CVA6Cfg.RVZCMP) begin
176
+ macro_decoder #(
177
+ .CVA6Cfg(CVA6Cfg)
178
+ ) macro_decoder_i (
179
+ .instr_i (instruction_rvc[0]),
180
+ .is_macro_instr_i (is_macro_instr[0]),
181
+ .clk_i (clk_i),
182
+ .rst_ni (rst_ni),
183
+ .instr_o (instruction_zcmp),
184
+ .illegal_instr_i (is_illegal_rvc[0]),
185
+ .is_compressed_i (is_compressed_rvc[0]),
186
+ .issue_ack_i (issue_instr_ack_i[0]),
187
+ .illegal_instr_o (is_illegal_zcmp),
188
+ .is_compressed_o (is_compressed_zcmp),
189
+ .fetch_stall_o (stall_macro_deco_zcmp),
190
+ .is_last_macro_instr_o (is_last_macro_instr),
191
+ .is_double_rd_macro_instr_o(is_double_rd_macro_instr)
192
+ );
193
+ end else begin
194
+ assign instruction_zcmp = instruction_rvc;
195
+ assign is_illegal_zcmp = is_illegal_rvc;
196
+ assign is_compressed_zcmp = is_compressed_rvc;
197
+ assign stall_macro_deco_zcmp = '0;
198
+ assign is_last_macro_instr = '0;
199
+ assign is_double_rd_macro_instr = '0;
200
+ end
201
+
202
+ if (CVA6Cfg.RVZCMT) begin
203
+ zcmt_decoder #(
204
+ .CVA6Cfg(CVA6Cfg),
205
+ .dcache_req_i_t(dcache_req_i_t),
206
+ .dcache_req_o_t(dcache_req_o_t),
207
+ .jvt_t(jvt_t),
208
+ .branchpredict_sbe_t(branchpredict_sbe_t)
209
+ ) zcmt_decoder_i (
210
+ .instr_i (instruction_rvc[0]),
211
+ .pc_i (fetch_entry_i[0].address),
212
+ .is_zcmt_instr_i(is_zcmt_instr[0]),
213
+ .clk_i (clk_i),
214
+ .rst_ni (rst_ni),
215
+ .instr_o (instruction_zcmt),
216
+ .illegal_instr_i(is_illegal_rvc[0]),
217
+ .is_compressed_i(is_compressed_rvc[0]),
218
+ .illegal_instr_o(is_illegal_zcmt),
219
+ .is_compressed_o(is_compressed_zcmt),
220
+ .fetch_stall_o (stall_macro_deco_zcmt),
221
+ .jvt_i (jvt_i),
222
+ .req_port_i (dcache_req_ports_i),
223
+ .req_port_o (dcache_req_ports_o),
224
+ .jump_address_o (jump_address)
225
+ );
226
+ end else begin
227
+ assign instruction_zcmt = instruction_rvc;
228
+ assign is_illegal_zcmt = is_illegal_rvc;
229
+ assign is_compressed_zcmt = is_compressed_rvc;
230
+ assign stall_macro_deco_zcmt = '0;
231
+ assign jump_address = '0;
232
+ assign dcache_req_ports_o = '0;
233
+ end
234
+
235
+ if (CVA6Cfg.RVZCMT) begin
236
+ assign instruction_cvxif_i = is_zcmt_instr[0] ? instruction_zcmt : instruction_zcmp;
237
+ assign is_illegal_cvxif_i = is_zcmt_instr[0] ? is_illegal_zcmt : is_illegal_zcmp;
238
+ assign is_compressed_cvxif_i = is_zcmt_instr[0] ? is_compressed_zcmt : is_compressed_zcmp;
239
+ assign stall_macro_deco = is_zcmt_instr[0] ? stall_macro_deco_zcmt : stall_macro_deco_zcmp;
240
+ end else begin // Do not instantiate the mux which is not optimized cross-bondaries
241
+ assign instruction_cvxif_i = instruction_zcmp;
242
+ assign is_illegal_cvxif_i = is_illegal_zcmp;
243
+ assign is_compressed_cvxif_i = is_compressed_zcmp;
244
+ assign stall_macro_deco = stall_macro_deco_zcmp;
245
+ end
246
+
247
+ if (CVA6Cfg.CvxifEn) begin
248
+ cvxif_compressed_if_driver #(
249
+ .CVA6Cfg(CVA6Cfg),
250
+ .x_compressed_req_t(x_compressed_req_t),
251
+ .x_compressed_resp_t(x_compressed_resp_t)
252
+ ) i_cvxif_compressed_if_driver_i (
253
+ .clk_i (clk_i),
254
+ .rst_ni (rst_ni),
255
+ .flush_i (flush_i),
256
+ .hart_id_i (hart_id_i),
257
+ .is_compressed_i (is_compressed_cvxif_i),
258
+ .is_illegal_i (is_illegal_cvxif_i),
259
+ .instruction_i (instruction_cvxif_i),
260
+ .is_compressed_o (is_compressed_cvxif_o),
261
+ .is_illegal_o (is_illegal_cvxif_o),
262
+ .instruction_o (instruction_cvxif_o),
263
+ .stall_i (stall_macro_deco),
264
+ .stall_o (stall_instr_fetch[0]),
265
+ .compressed_ready_i(compressed_ready_i),
266
+ .compressed_resp_i (compressed_resp_i),
267
+ .compressed_valid_o(compressed_valid_o),
268
+ .compressed_req_o (compressed_req_o)
269
+ );
270
+ end else begin
271
+ assign stall_instr_fetch[0] = stall_macro_deco;
272
+ end
273
+ end
274
+
275
+ // ---------------------------------------------------------
276
+ // 2. Decode and emit instruction to issue stage
277
+ // ---------------------------------------------------------
278
+
279
+ always_comb begin
280
+ // No CVXIF, No ZCMP, No ZCMT => Connect directly compressed decoder to decoder
281
+ is_illegal_deco = is_illegal_rvc;
282
+ instruction_deco = instruction_rvc;
283
+ is_compressed_deco = is_compressed_rvc;
284
+ if (CVA6Cfg.CvxifEn) begin
285
+ is_illegal_deco[0] = is_illegal_cvxif_o;
286
+ instruction_deco[0] = instruction_cvxif_o;
287
+ is_compressed_deco[0] = is_compressed_cvxif_o;
288
+ end else if (!CVA6Cfg.CvxifEn && (CVA6Cfg.RVZCMP || CVA6Cfg.RVZCMT)) begin
289
+ is_illegal_deco[0] = is_illegal_cvxif_i;
290
+ instruction_deco[0] = instruction_cvxif_i;
291
+ is_compressed_deco[0] = is_compressed_cvxif_i;
292
+ end
293
+ end
294
+
295
+ assign rvfi_is_compressed_o = is_compressed_rvc;
296
+
297
+ for (genvar i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
298
+ decoder #(
299
+ .CVA6Cfg(CVA6Cfg),
300
+ .branchpredict_sbe_t(branchpredict_sbe_t),
301
+ .exception_t(exception_t),
302
+ .irq_ctrl_t(irq_ctrl_t),
303
+ .scoreboard_entry_t(scoreboard_entry_t),
304
+ .interrupts_t(interrupts_t),
305
+ .INTERRUPTS(INTERRUPTS)
306
+ ) decoder_i (
307
+ .debug_req_i,
308
+ .irq_ctrl_i,
309
+ .irq_i,
310
+ .pc_i (fetch_entry_i[i].address),
311
+ .is_compressed_i (is_compressed_deco[i]),
312
+ .is_macro_instr_i (is_macro_instr[i]),
313
+ .is_zcmt_i (is_zcmt_instr[i]),
314
+ .is_last_macro_instr_i (is_last_macro_instr),
315
+ .is_double_rd_macro_instr_i(is_double_rd_macro_instr),
316
+ .jump_address_i (jump_address),
317
+ .is_illegal_i (is_illegal_deco[i]),
318
+ .instruction_i (instruction_deco[i]),
319
+ .compressed_instr_i (fetch_entry_i[i].instruction[15:0]),
320
+ .branch_predict_i (fetch_entry_i[i].branch_predict),
321
+ .ex_i (fetch_entry_i[i].ex),
322
+ .priv_lvl_i (priv_lvl_i),
323
+ .v_i (v_i),
324
+ .debug_mode_i (debug_mode_i),
325
+ .fs_i,
326
+ .vfs_i,
327
+ .frm_i,
328
+ .vs_i,
329
+ .tvm_i,
330
+ .tw_i,
331
+ .vtw_i,
332
+ .tsr_i,
333
+ .hu_i,
334
+ .instruction_o (decoded_instruction[i]),
335
+ .orig_instr_o (orig_instr[i]),
336
+ .is_control_flow_instr_o (is_control_flow_instr[i])
337
+ );
338
+ end
339
+
340
+ // ------------------
341
+ // 3. Pipeline Register
342
+ // ------------------
343
+ for (genvar i = 0; i < CVA6Cfg.NrIssuePorts; i++) begin
344
+ assign issue_entry_o[i] = issue_q[i].sbe;
345
+ assign issue_entry_o_prev[i] = CVA6Cfg.FpgaAlteraEn ? issue_n[i].sbe : '0;
346
+ assign issue_entry_valid_o[i] = issue_q[i].valid;
347
+ assign is_ctrl_flow_o[i] = issue_q[i].is_ctrl_flow;
348
+ assign orig_instr_o[i] = issue_q[i].orig_instr;
349
+ end
350
+
351
+ if (CVA6Cfg.SuperscalarEn) begin
352
+ always_comb begin
353
+ issue_n = issue_q;
354
+ fetch_entry_ready_o = '0;
355
+ // instruction is not valid if we stall due to ZCMT or CVXIF
356
+ decoded_instruction_valid[0] = (CVA6Cfg.RVZCMT && is_zcmt_instr[0] && stall_macro_deco_zcmt) ||
357
+ (CVA6Cfg.CvxifEn && is_illegal_cvxif_i && ~stall_macro_deco) && stall_instr_fetch[0]
358
+ ? 1'b0 : 1'b1;
359
+ // Instruction on port 1 are always valid. It is either 32bits or legal 16bits.
360
+ decoded_instruction_valid[1] = ~stall_instr_fetch[1];
361
+
362
+ // Clear the valid flag if issue has acknowledged the instruction
363
+ if (issue_instr_ack_i[0]) begin
364
+ issue_n[0].valid = 1'b0;
365
+ end
366
+ if (issue_instr_ack_i[1]) begin
367
+ issue_n[1].valid = 1'b0;
368
+ end
369
+
370
+ if (!issue_n[0].valid) begin
371
+ if (issue_n[1].valid) begin
372
+ issue_n[0] = issue_n[1];
373
+ issue_n[1].valid = 1'b0;
374
+ end else if (fetch_entry_valid_i[0]) begin
375
+ fetch_entry_ready_o[0] = ~stall_instr_fetch[0];
376
+ issue_n[0] = '{
377
+ decoded_instruction_valid[0],
378
+ decoded_instruction[0],
379
+ orig_instr[0],
380
+ is_control_flow_instr[0]
381
+ };
382
+ end
383
+ end
384
+
385
+ if (!issue_n[1].valid) begin
386
+ if (fetch_entry_ready_o[0]) begin
387
+ if (fetch_entry_valid_i[1]) begin
388
+ fetch_entry_ready_o[1] = ~stall_instr_fetch[1];
389
+ issue_n[1] = '{
390
+ decoded_instruction_valid[1],
391
+ decoded_instruction[1],
392
+ orig_instr[1],
393
+ is_control_flow_instr[1]
394
+ };
395
+ end
396
+ end else if (fetch_entry_valid_i[0]) begin
397
+ fetch_entry_ready_o[0] = ~stall_instr_fetch[0];
398
+ issue_n[1] = '{
399
+ decoded_instruction_valid[0],
400
+ decoded_instruction[0],
401
+ orig_instr[0],
402
+ is_control_flow_instr[0]
403
+ };
404
+ end
405
+ end
406
+
407
+ if (flush_i) begin
408
+ issue_n[0].valid = 1'b0;
409
+ issue_n[1].valid = 1'b0;
410
+ end
411
+ end
412
+ end else begin
413
+ always_comb begin
414
+ issue_n = issue_q;
415
+ fetch_entry_ready_o = '0;
416
+ // instruction is not valid if we stall due to ZCMT or CVXIF
417
+ decoded_instruction_valid[0] = (CVA6Cfg.RVZCMT && is_zcmt_instr[0] && stall_macro_deco_zcmt) ||
418
+ (CVA6Cfg.CvxifEn && is_illegal_cvxif_i && ~stall_macro_deco && stall_instr_fetch[0])
419
+ ? 1'b0 : 1'b1;
420
+ // Clear the valid flag if issue has acknowledged the instruction
421
+ if (issue_instr_ack_i[0]) issue_n[0].valid = 1'b0;
422
+
423
+ // TODO: refaire
424
+ // if we have a space in the register and the fetch is valid, go get it
425
+ // or the issue stage is currently acknowledging an instruction, which means that we will have space
426
+ // for a new instruction
427
+ if (!issue_n[0].valid && fetch_entry_valid_i[0]) begin
428
+ fetch_entry_ready_o[0] = ~stall_instr_fetch[0];
429
+ issue_n[0] = '{
430
+ decoded_instruction_valid[0],
431
+ decoded_instruction[0],
432
+ orig_instr[0],
433
+ is_control_flow_instr[0]
434
+ };
435
+ end
436
+
437
+ // invalidate the pipeline register on a flush
438
+ if (flush_i) issue_n[0].valid = 1'b0;
439
+ end
440
+ end
441
+ // -------------------------
442
+ // Registers (ID <-> Issue)
443
+ // -------------------------
444
+ always_ff @(posedge clk_i or negedge rst_ni) begin
445
+ if (~rst_ni) begin
446
+ issue_q <= '0;
447
+ end else begin
448
+ issue_q <= issue_n;
449
+ end
450
+ end
451
+
452
+ endmodule
chips/openroad_flow_scripts/flow/designs/src/cva6/core/include/ariane_pkg.sv ADDED
@@ -0,0 +1,818 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2018 ETH Zurich and University of Bologna.
2
+ * Copyright and related rights are licensed under the Solderpad Hardware
3
+ * License, Version 0.51 (the “License”); you may not use this file except in
4
+ * compliance with the License. You may obtain a copy of the License at
5
+ * http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law
6
+ * or agreed to in writing, software, hardware and materials distributed under
7
+ * this License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
8
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9
+ * specific language governing permissions and limitations under the License.
10
+ *
11
+ * File: ariane_pkg.sv
12
+ * Author: Florian Zaruba <zarubaf@iis.ee.ethz.ch>
13
+ * Date: 8.4.2017
14
+ *
15
+ * Description: Contains all the necessary defines for Ariane
16
+ * in one package.
17
+ */
18
+
19
+ // this is needed to propagate the
20
+ // configuration in case Ariane is
21
+ // instantiated in OpenPiton
22
+ `ifdef PITON_ARIANE
23
+ `include "l15.tmp.h"
24
+ `endif
25
+
26
+ /// This package contains `functions` and global defines for CVA6.
27
+ /// *Note*: There are some parameters here as well which will eventually be
28
+ /// moved out to favour a fully parameterizable core.
29
+ package ariane_pkg;
30
+
31
+ // TODO: Slowly move those parameters to the new system.
32
+ localparam BITS_SATURATION_COUNTER = 2;
33
+
34
+ // depth of store-buffers, this needs to be a power of two
35
+ localparam logic [2:0] DEPTH_SPEC = 'd4;
36
+
37
+ // if CVA6Cfg.DCacheType = cva6_config_pkg::WT
38
+ // we can use a small commit queue since we have a write buffer in the dcache
39
+ // we could in principle do without the commit queue in this case, but the timing degrades if we do that due
40
+ // to longer paths into the commit stage
41
+ // if CVA6Cfg.DCacheType = cva6_config_pkg::WB
42
+ // allocate more space for the commit buffer to be on the save side, this needs to be a power of two
43
+ localparam logic [2:0] DEPTH_COMMIT = 'd4;
44
+
45
+ // Transprecision float unit
46
+ localparam int unsigned LAT_COMP_FP32 = 'd2;
47
+ localparam int unsigned LAT_COMP_FP64 = 'd3;
48
+ localparam int unsigned LAT_COMP_FP16 = 'd1;
49
+ localparam int unsigned LAT_COMP_FP16ALT = 'd1;
50
+ localparam int unsigned LAT_COMP_FP8 = 'd1;
51
+ localparam int unsigned LAT_DIVSQRT = 'd2;
52
+ localparam int unsigned LAT_NONCOMP = 'd1;
53
+ localparam int unsigned LAT_CONV = 'd2;
54
+
55
+ localparam logic [31:0] OPENHWGROUP_MVENDORID = 32'h0602;
56
+ localparam logic [31:0] ARIANE_MARCHID = 32'd3;
57
+
58
+ // 32 registers
59
+ localparam REG_ADDR_SIZE = 5;
60
+
61
+ // Read ports for general purpose register files
62
+ localparam NR_RGPR_PORTS = 2;
63
+
64
+ // static debug hartinfo
65
+ // debug causes
66
+ localparam logic [2:0] CauseBreakpoint = 3'h1;
67
+ localparam logic [2:0] CauseTrigger = 3'h2;
68
+ localparam logic [2:0] CauseRequest = 3'h3;
69
+ localparam logic [2:0] CauseSingleStep = 3'h4;
70
+ // amount of data count registers implemented
71
+ localparam logic [3:0] DataCount = 4'h2;
72
+
73
+ // address where data0-15 is shadowed or if shadowed in a CSR
74
+ // address of the first CSR used for shadowing the data
75
+ localparam logic [11:0] DataAddr = 12'h380; // we are aligned with Rocket here
76
+ typedef struct packed {
77
+ logic [31:24] zero1;
78
+ logic [23:20] nscratch;
79
+ logic [19:17] zero0;
80
+ logic dataaccess;
81
+ logic [15:12] datasize;
82
+ logic [11:0] dataaddr;
83
+ } hartinfo_t;
84
+
85
+ localparam hartinfo_t DebugHartInfo = '{
86
+ zero1: '0,
87
+ nscratch: 2, // Debug module needs at least two scratch regs
88
+ zero0: '0,
89
+ dataaccess: 1'b1, // data registers are memory mapped in the debugger
90
+ datasize: DataCount,
91
+ dataaddr: DataAddr
92
+ };
93
+
94
+ // enables a commit log which matches spikes commit log format for easier trace comparison
95
+ localparam bit ENABLE_SPIKE_COMMIT_LOG = 1'b1;
96
+
97
+ // ------------- Dangerous -------------
98
+ // if set to zero a flush will not invalidate the cache-lines, in a single core environment
99
+ // where coherence is not necessary this can improve performance. This needs to be switched on
100
+ // when more than one core is in a system
101
+ localparam logic INVALIDATE_ON_FLUSH = 1'b1;
102
+
103
+ `ifdef SPIKE_TANDEM
104
+ // Spike still places 0 in TVAL for ENV_CALL_* exceptions.
105
+ // This may eventually go away when Spike starts to handle TVAL for *all* exceptions.
106
+ localparam bit ZERO_TVAL = 1'b1;
107
+ `else
108
+ localparam bit ZERO_TVAL = 1'b0;
109
+ `endif
110
+
111
+ // read mask for SSTATUS over MMSTATUS
112
+ function automatic logic [63:0] smode_status_read_mask(config_pkg::cva6_cfg_t Cfg);
113
+ return riscv::SSTATUS_UIE
114
+ | riscv::SSTATUS_SIE
115
+ | riscv::SSTATUS_SPIE
116
+ | riscv::SSTATUS_SPP
117
+ | riscv::SSTATUS_FS
118
+ | riscv::SSTATUS_XS
119
+ | riscv::SSTATUS_SUM
120
+ | riscv::SSTATUS_MXR
121
+ | riscv::SSTATUS_UPIE
122
+ | riscv::SSTATUS_SPIE
123
+ | riscv::SSTATUS_UXL
124
+ | riscv::sstatus_sd(Cfg.IS_XLEN64);
125
+ endfunction
126
+
127
+ localparam logic [63:0] SMODE_STATUS_WRITE_MASK = riscv::SSTATUS_SIE
128
+ | riscv::SSTATUS_SPIE
129
+ | riscv::SSTATUS_SPP
130
+ | riscv::SSTATUS_FS
131
+ | riscv::SSTATUS_SUM
132
+ | riscv::SSTATUS_MXR;
133
+
134
+ localparam logic [63:0] HSTATUS_WRITE_MASK = riscv::HSTATUS_VSBE
135
+ | riscv::HSTATUS_GVA
136
+ | riscv::HSTATUS_SPV
137
+ | riscv::HSTATUS_SPVP
138
+ | riscv::HSTATUS_HU
139
+ | riscv::HSTATUS_VTVM
140
+ | riscv::HSTATUS_VTW
141
+ | riscv::HSTATUS_VTSR;
142
+
143
+ // hypervisor delegable interrupts
144
+ function automatic logic [31:0] hs_deleg_interrupts(config_pkg::cva6_cfg_t Cfg);
145
+ return riscv::MIP_VSSIP | riscv::MIP_VSTIP | riscv::MIP_VSEIP;
146
+ endfunction
147
+
148
+ // virtual supervisor delegable interrupts
149
+ function automatic logic [31:0] vs_deleg_interrupts(config_pkg::cva6_cfg_t Cfg);
150
+ return riscv::MIP_VSSIP | riscv::MIP_VSTIP | riscv::MIP_VSEIP;
151
+ endfunction
152
+
153
+ // ---------------
154
+ // AXI
155
+ // ---------------
156
+
157
+ typedef enum logic {
158
+ SINGLE_REQ,
159
+ CACHE_LINE_REQ
160
+ } ad_req_t;
161
+
162
+ // ---------------
163
+ // Fetch Stage
164
+ // ---------------
165
+
166
+ // leave as is (fails with >8 entries and wider fetch width)
167
+ localparam int unsigned FETCH_FIFO_DEPTH = 4;
168
+ localparam int unsigned FETCH_ADDR_FIFO_DEPTH = 2;
169
+
170
+ typedef enum logic [2:0] {
171
+ NoCF, // No control flow prediction
172
+ Branch, // Branch
173
+ Jump, // Jump to address from immediate
174
+ JumpR, // Jump to address from registers
175
+ Return // Return Address Prediction
176
+ } cf_t;
177
+
178
+ typedef struct packed {
179
+ logic valid;
180
+ logic taken;
181
+ } bht_prediction_t;
182
+
183
+ typedef struct packed {
184
+ logic valid;
185
+ logic [1:0] saturation_counter;
186
+ } bht_t;
187
+
188
+ typedef enum logic [3:0] {
189
+ NONE, // 0
190
+ LOAD, // 1
191
+ STORE, // 2
192
+ ALU, // 3
193
+ CTRL_FLOW, // 4
194
+ MULT, // 5
195
+ CSR, // 6
196
+ FPU, // 7
197
+ FPU_VEC, // 8
198
+ CVXIF, // 9
199
+ ACCEL // 10
200
+ } fu_t;
201
+
202
+ // Index of writeback ports
203
+ localparam FLU_WB = 0;
204
+ localparam STORE_WB = 1;
205
+ localparam LOAD_WB = 2;
206
+ localparam FPU_WB = 3;
207
+ localparam ACC_WB = 4;
208
+ localparam X_WB = 4;
209
+
210
+ localparam EXC_OFF_RST = 8'h80;
211
+
212
+ localparam SupervisorIrq = 1;
213
+ localparam MachineIrq = 0;
214
+
215
+ // ---------------
216
+ // Cache config
217
+ // ---------------
218
+
219
+ // for usage in OpenPiton we have to propagate the openpiton L15 configuration from l15.h
220
+ `ifdef PITON_ARIANE
221
+
222
+ `ifndef CONFIG_L1I_CACHELINE_WIDTH
223
+ `define CONFIG_L1I_CACHELINE_WIDTH 128
224
+ `endif
225
+
226
+ `ifndef CONFIG_L1I_ASSOCIATIVITY
227
+ `define CONFIG_L1I_ASSOCIATIVITY 4
228
+ `endif
229
+
230
+ `ifndef CONFIG_L1I_SIZE
231
+ `define CONFIG_L1I_SIZE 16*1024
232
+ `endif
233
+
234
+ `ifndef CONFIG_L1D_CACHELINE_WIDTH
235
+ `define CONFIG_L1D_CACHELINE_WIDTH 128
236
+ `endif
237
+
238
+ `ifndef CONFIG_L1D_ASSOCIATIVITY
239
+ `define CONFIG_L1D_ASSOCIATIVITY 8
240
+ `endif
241
+
242
+ `ifndef CONFIG_L1D_SIZE
243
+ `define CONFIG_L1D_SIZE 32*1024
244
+ `endif
245
+
246
+ `ifndef L15_THREADID_WIDTH
247
+ `define L15_THREADID_WIDTH 3
248
+ `endif
249
+
250
+ // I$
251
+ localparam int unsigned ICACHE_LINE_WIDTH = `CONFIG_L1I_CACHELINE_WIDTH;
252
+ localparam int unsigned ICACHE_SET_ASSOC = `CONFIG_L1I_ASSOCIATIVITY;
253
+ localparam int unsigned ICACHE_INDEX_WIDTH = $clog2(`CONFIG_L1I_SIZE / ICACHE_SET_ASSOC);
254
+ localparam int unsigned ICACHE_TAG_WIDTH = riscv::PLEN - ICACHE_INDEX_WIDTH;
255
+ localparam int unsigned ICACHE_USER_LINE_WIDTH = (AXI_USER_WIDTH == 1) ? 4 : 128; // in bit
256
+ // D$
257
+ localparam int unsigned DCACHE_LINE_WIDTH = `CONFIG_L1D_CACHELINE_WIDTH;
258
+ localparam int unsigned DCACHE_SET_ASSOC = `CONFIG_L1D_ASSOCIATIVITY;
259
+ localparam int unsigned DCACHE_INDEX_WIDTH = $clog2(`CONFIG_L1D_SIZE / DCACHE_SET_ASSOC);
260
+ localparam int unsigned DCACHE_TAG_WIDTH = riscv::PLEN - DCACHE_INDEX_WIDTH;
261
+ localparam int unsigned DCACHE_USER_LINE_WIDTH = (AXI_USER_WIDTH == 1) ? 4 : 128; // in bit
262
+ localparam int unsigned DCACHE_USER_WIDTH = cva6_config_pkg::CVA6ConfigDataUserWidth;
263
+
264
+ localparam int unsigned MEM_TID_WIDTH = `L15_THREADID_WIDTH;
265
+ `endif
266
+
267
+ // ---------------
268
+ // EX Stage
269
+ // ---------------
270
+
271
+ typedef enum logic [7:0] { // basic ALU op
272
+ ADD,
273
+ SUB,
274
+ ADDW,
275
+ SUBW,
276
+ // logic operations
277
+ XORL,
278
+ ORL,
279
+ ANDL,
280
+ // shifts
281
+ SRA,
282
+ SRL,
283
+ SLL,
284
+ SRLW,
285
+ SLLW,
286
+ SRAW,
287
+ // comparisons
288
+ LTS,
289
+ LTU,
290
+ GES,
291
+ GEU,
292
+ EQ,
293
+ NE,
294
+ // jumps
295
+ JALR,
296
+ BRANCH,
297
+ // set lower than operations
298
+ SLTS,
299
+ SLTU,
300
+ // CSR functions
301
+ MRET,
302
+ SRET,
303
+ DRET,
304
+ ECALL,
305
+ WFI,
306
+ FENCE,
307
+ FENCE_I,
308
+ SFENCE_VMA,
309
+ HFENCE_VVMA,
310
+ HFENCE_GVMA,
311
+ CSR_WRITE,
312
+ CSR_READ,
313
+ CSR_SET,
314
+ CSR_CLEAR,
315
+ // LSU functions
316
+ LD,
317
+ SD,
318
+ LW,
319
+ LWU,
320
+ SW,
321
+ LH,
322
+ LHU,
323
+ SH,
324
+ LB,
325
+ SB,
326
+ LBU,
327
+ // Hypervisor Virtual-Machine Load and Store Instructions
328
+ HLV_B,
329
+ HLV_BU,
330
+ HLV_H,
331
+ HLV_HU,
332
+ HLVX_HU,
333
+ HLV_W,
334
+ HLVX_WU,
335
+ HSV_B,
336
+ HSV_H,
337
+ HSV_W,
338
+ HLV_WU,
339
+ HLV_D,
340
+ HSV_D,
341
+ // Atomic Memory Operations
342
+ AMO_LRW,
343
+ AMO_LRD,
344
+ AMO_SCW,
345
+ AMO_SCD,
346
+ AMO_SWAPW,
347
+ AMO_ADDW,
348
+ AMO_ANDW,
349
+ AMO_ORW,
350
+ AMO_XORW,
351
+ AMO_MAXW,
352
+ AMO_MAXWU,
353
+ AMO_MINW,
354
+ AMO_MINWU,
355
+ AMO_SWAPD,
356
+ AMO_ADDD,
357
+ AMO_ANDD,
358
+ AMO_ORD,
359
+ AMO_XORD,
360
+ AMO_MAXD,
361
+ AMO_MAXDU,
362
+ AMO_MIND,
363
+ AMO_MINDU,
364
+ // Multiplications
365
+ MUL,
366
+ MULH,
367
+ MULHU,
368
+ MULHSU,
369
+ MULW,
370
+ // Divisions
371
+ DIV,
372
+ DIVU,
373
+ DIVW,
374
+ DIVUW,
375
+ REM,
376
+ REMU,
377
+ REMW,
378
+ REMUW,
379
+ // Floating-Point Load and Store Instructions
380
+ FLD,
381
+ FLW,
382
+ FLH,
383
+ FLB,
384
+ FSD,
385
+ FSW,
386
+ FSH,
387
+ FSB,
388
+ // Floating-Point Computational Instructions
389
+ FADD,
390
+ FSUB,
391
+ FMUL,
392
+ FDIV,
393
+ FMIN_MAX,
394
+ FSQRT,
395
+ FMADD,
396
+ FMSUB,
397
+ FNMSUB,
398
+ FNMADD,
399
+ // Floating-Point Conversion and Move Instructions
400
+ FCVT_F2I,
401
+ FCVT_I2F,
402
+ FCVT_F2F,
403
+ FSGNJ,
404
+ FMV_F2X,
405
+ FMV_X2F,
406
+ // Floating-Point Compare Instructions
407
+ FCMP,
408
+ // Floating-Point Classify Instruction
409
+ FCLASS,
410
+ // Vectorial Floating-Point Instructions that don't directly map onto the scalar ones
411
+ VFMIN,
412
+ VFMAX,
413
+ VFSGNJ,
414
+ VFSGNJN,
415
+ VFSGNJX,
416
+ VFEQ,
417
+ VFNE,
418
+ VFLT,
419
+ VFGE,
420
+ VFLE,
421
+ VFGT,
422
+ VFCPKAB_S,
423
+ VFCPKCD_S,
424
+ VFCPKAB_D,
425
+ VFCPKCD_D,
426
+ // Offload Instructions to be directed into cv_x_if
427
+ OFFLOAD,
428
+ // Or-Combine and REV8
429
+ ORCB,
430
+ REV8,
431
+ // Bitwise Rotation
432
+ ROL,
433
+ ROLW,
434
+ ROR,
435
+ RORI,
436
+ RORIW,
437
+ RORW,
438
+ // Sign and Zero Extend
439
+ SEXTB,
440
+ SEXTH,
441
+ ZEXTH,
442
+ // Count population
443
+ CPOP,
444
+ CPOPW,
445
+ // Count Leading/Training Zeros
446
+ CLZ,
447
+ CLZW,
448
+ CTZ,
449
+ CTZW,
450
+ // Carry less multiplication Op's
451
+ CLMUL,
452
+ CLMULH,
453
+ CLMULR,
454
+ // Single bit instructions Op's
455
+ BCLR,
456
+ BCLRI,
457
+ BEXT,
458
+ BEXTI,
459
+ BINV,
460
+ BINVI,
461
+ BSET,
462
+ BSETI,
463
+ // Integer minimum/maximum
464
+ MAX,
465
+ MAXU,
466
+ MIN,
467
+ MINU,
468
+ // Shift with Add Unsigned Word and Unsigned Word Op's (Bitmanip)
469
+ SH1ADDUW,
470
+ SH2ADDUW,
471
+ SH3ADDUW,
472
+ ADDUW,
473
+ SLLIUW,
474
+ // Shift with Add (Bitmanip)
475
+ SH1ADD,
476
+ SH2ADD,
477
+ SH3ADD,
478
+ // Bitmanip Logical with negate op (Bitmanip)
479
+ ANDN,
480
+ ORN,
481
+ XNOR,
482
+ // Accelerator operations
483
+ ACCEL_OP,
484
+ ACCEL_OP_FS1,
485
+ ACCEL_OP_FD,
486
+ ACCEL_OP_LOAD,
487
+ ACCEL_OP_STORE,
488
+ // Zicond instruction
489
+ CZERO_EQZ,
490
+ CZERO_NEZ,
491
+ // Pack instructions
492
+ PACK,
493
+ PACK_H,
494
+ PACK_W,
495
+ // Brev8 instruction
496
+ BREV8,
497
+ // Zip instructions
498
+ UNZIP,
499
+ ZIP
500
+ } fu_op;
501
+
502
+ function automatic logic op_is_branch(input fu_op op);
503
+ unique case (op) inside
504
+ EQ, NE, LTS, GES, LTU, GEU: return 1'b1;
505
+ default: return 1'b0; // all other ops
506
+ endcase
507
+ endfunction
508
+
509
+ // -------------------------------
510
+ // Extract Src/Dst FP Reg from Op
511
+ // -------------------------------
512
+ // function used in instr_trace svh
513
+ // is_rs1_fpr function is kept to allow cva6 compilation with instr_trace feature
514
+ function automatic logic is_rs1_fpr(input fu_op op);
515
+ unique case (op) inside
516
+ [FMUL : FNMADD], // Computational Operations (except ADD/SUB)
517
+ FCVT_F2I, // Float-Int Casts
518
+ FCVT_F2F, // Float-Float Casts
519
+ FSGNJ, // Sign Injections
520
+ FMV_F2X, // FPR-GPR Moves
521
+ FCMP, // Comparisons
522
+ FCLASS, // Classifications
523
+ [VFMIN : VFCPKCD_D], // Additional Vectorial FP ops
524
+ ACCEL_OP_FS1:
525
+ return 1'b1; // Accelerator instructions
526
+ default: return 1'b0; // all other ops
527
+ endcase
528
+ endfunction
529
+
530
+ // function used in instr_trace svh
531
+ // is_rs2_fpr function is kept to allow cva6 compilation with instr_trace feature
532
+ function automatic logic is_rs2_fpr(input fu_op op);
533
+ unique case (op) inside
534
+ [FSD : FSB], // FP Stores
535
+ [FADD : FMIN_MAX], // Computational Operations (no sqrt)
536
+ [FMADD : FNMADD], // Fused Computational Operations
537
+ FCVT_F2F, // Vectorial F2F Conversions requrie target
538
+ [FSGNJ : FMV_F2X], // Sign Injections and moves mapped to SGNJ
539
+ FCMP, // Comparisons
540
+ [VFMIN : VFCPKCD_D]:
541
+ return 1'b1; // Additional Vectorial FP ops
542
+ default: return 1'b0; // all other ops
543
+ endcase
544
+ endfunction
545
+
546
+ // function used in instr_trace svh
547
+ // is_imm_fpr function is kept to allow cva6 compilation with instr_trace feature
548
+ // ternary operations encode the rs3 address in the imm field, also add/sub
549
+ function automatic logic is_imm_fpr(input fu_op op);
550
+ unique case (op) inside
551
+ [FADD : FSUB], // ADD/SUB need inputs as Operand B/C
552
+ [FMADD : FNMADD], // Fused Computational Operations
553
+ [VFCPKAB_S : VFCPKCD_D]:
554
+ return 1'b1; // Vectorial FP cast and pack ops
555
+ default: return 1'b0; // all other ops
556
+ endcase
557
+ endfunction
558
+
559
+ // function used in instr_trace svh
560
+ // is_rd_fpr function is kept to allow cva6 compilation with instr_trace feature
561
+ function automatic logic is_rd_fpr(input fu_op op);
562
+ unique case (op) inside
563
+ [FLD : FLB], // FP Loads
564
+ [FADD : FNMADD], // Computational Operations
565
+ FCVT_I2F, // Int-Float Casts
566
+ FCVT_F2F, // Float-Float Casts
567
+ FSGNJ, // Sign Injections
568
+ FMV_X2F, // GPR-FPR Moves
569
+ [VFMIN : VFSGNJX], // Vectorial MIN/MAX and SGNJ
570
+ [VFCPKAB_S : VFCPKCD_D], // Vectorial FP cast and pack ops
571
+ ACCEL_OP_FD:
572
+ return 1'b1; // Accelerator instructions
573
+ default: return 1'b0; // all other ops
574
+ endcase
575
+ endfunction
576
+
577
+ function automatic logic is_amo(fu_op op);
578
+ case (op) inside
579
+ [AMO_LRW : AMO_MINDU]: begin
580
+ return 1'b1;
581
+ end
582
+ default: return 1'b0;
583
+ endcase
584
+ endfunction
585
+
586
+ // -------------------
587
+ // Performance counter
588
+ // -------------------
589
+ localparam int unsigned MHPMCounterNum = 6;
590
+
591
+ // --------------------
592
+ // Atomics
593
+ // --------------------
594
+ typedef enum logic [3:0] {
595
+ AMO_NONE = 4'b0000,
596
+ AMO_LR = 4'b0001,
597
+ AMO_SC = 4'b0010,
598
+ AMO_SWAP = 4'b0011,
599
+ AMO_ADD = 4'b0100,
600
+ AMO_AND = 4'b0101,
601
+ AMO_OR = 4'b0110,
602
+ AMO_XOR = 4'b0111,
603
+ AMO_MAX = 4'b1000,
604
+ AMO_MAXU = 4'b1001,
605
+ AMO_MIN = 4'b1010,
606
+ AMO_MINU = 4'b1011,
607
+ AMO_CAS1 = 4'b1100, // unused, not part of riscv spec, but provided in OpenPiton
608
+ AMO_CAS2 = 4'b1101 // unused, not part of riscv spec, but provided in OpenPiton
609
+ } amo_t;
610
+
611
+ // Bits required for representation of physical address space as 4K pages
612
+ // (e.g. 27*4K == 39bit address space).
613
+ localparam PPN4K_WIDTH = 38;
614
+
615
+ typedef struct packed {
616
+ logic valid; // valid flag
617
+ logic is_4M; //
618
+ logic [20-1:0] vpn; //VPN (32bits) = 20bits + 12bits offset
619
+ logic [9-1:0] asid; //ASID length = 9 for Sv32 mmu
620
+ riscv::pte_sv32_t content;
621
+ } tlb_update_sv32_t;
622
+
623
+ typedef enum logic [1:0] {
624
+ FE_NONE,
625
+ FE_INSTR_ACCESS_FAULT,
626
+ FE_INSTR_PAGE_FAULT,
627
+ FE_INSTR_GUEST_PAGE_FAULT
628
+ } frontend_exception_t;
629
+
630
+ // AMO request going to cache. this request is unconditionally valid as soon
631
+ // as request goes high.
632
+ // Furthermore, those signals are kept stable until the response indicates
633
+ // completion by asserting ack.
634
+ typedef struct packed {
635
+ logic req; // this request is valid
636
+ amo_t amo_op; // atomic memory operation to perform
637
+ logic [1:0] size; // 2'b10 --> word operation, 2'b11 --> double word operation
638
+ logic [63:0] operand_a; // address
639
+ logic [63:0] operand_b; // data as layouted in the register
640
+ } amo_req_t;
641
+
642
+ // AMO response coming from cache.
643
+ typedef struct packed {
644
+ logic ack; // response is valid
645
+ logic [63:0] result; // sign-extended, result
646
+ } amo_resp_t;
647
+
648
+ localparam RVFI = cva6_config_pkg::CVA6ConfigRvfiTrace;
649
+
650
+ // ----------------------
651
+ // Arithmetic Functions
652
+ // ----------------------
653
+ function automatic logic [63:0] sext32to64(logic [31:0] operand);
654
+ return {{32{operand[31]}}, operand[31:0]};
655
+ endfunction
656
+
657
+ // ----------------------
658
+ // LSU Functions
659
+ // ----------------------
660
+ // generate byte enable mask
661
+ function automatic logic [7:0] be_gen(logic [2:0] addr, logic [1:0] size);
662
+ case (size)
663
+ 2'b11: begin
664
+ return 8'b1111_1111;
665
+ end
666
+ 2'b10: begin
667
+ case (addr[2:0])
668
+ 3'b000: return 8'b0000_1111;
669
+ 3'b001: return 8'b0001_1110;
670
+ 3'b010: return 8'b0011_1100;
671
+ 3'b011: return 8'b0111_1000;
672
+ 3'b100: return 8'b1111_0000;
673
+ default: ; // Do nothing
674
+ endcase
675
+ end
676
+ 2'b01: begin
677
+ case (addr[2:0])
678
+ 3'b000: return 8'b0000_0011;
679
+ 3'b001: return 8'b0000_0110;
680
+ 3'b010: return 8'b0000_1100;
681
+ 3'b011: return 8'b0001_1000;
682
+ 3'b100: return 8'b0011_0000;
683
+ 3'b101: return 8'b0110_0000;
684
+ 3'b110: return 8'b1100_0000;
685
+ default: ; // Do nothing
686
+ endcase
687
+ end
688
+ 2'b00: begin
689
+ case (addr[2:0])
690
+ 3'b000: return 8'b0000_0001;
691
+ 3'b001: return 8'b0000_0010;
692
+ 3'b010: return 8'b0000_0100;
693
+ 3'b011: return 8'b0000_1000;
694
+ 3'b100: return 8'b0001_0000;
695
+ 3'b101: return 8'b0010_0000;
696
+ 3'b110: return 8'b0100_0000;
697
+ 3'b111: return 8'b1000_0000;
698
+ endcase
699
+ end
700
+ endcase
701
+ return 8'b0;
702
+ endfunction
703
+
704
+ function automatic logic [3:0] be_gen_32(logic [1:0] addr, logic [1:0] size);
705
+ case (size)
706
+ 2'b10: begin
707
+ return 4'b1111;
708
+ end
709
+ 2'b01: begin
710
+ case (addr[1:0])
711
+ 2'b00: return 4'b0011;
712
+ 2'b01: return 4'b0110;
713
+ 2'b10: return 4'b1100;
714
+ default: ; // Do nothing
715
+ endcase
716
+ end
717
+ 2'b00: begin
718
+ case (addr[1:0])
719
+ 2'b00: return 4'b0001;
720
+ 2'b01: return 4'b0010;
721
+ 2'b10: return 4'b0100;
722
+ 2'b11: return 4'b1000;
723
+ endcase
724
+ end
725
+ default: return 4'b0;
726
+ endcase
727
+ return 4'b0;
728
+ endfunction
729
+
730
+ // ----------------------
731
+ // Extract Bytes from Op
732
+ // ----------------------
733
+ function automatic logic [1:0] extract_transfer_size(fu_op op);
734
+ case (op)
735
+ LD, HLV_D, SD, HSV_D, FLD, FSD,
736
+ AMO_LRD, AMO_SCD,
737
+ AMO_SWAPD, AMO_ADDD,
738
+ AMO_ANDD, AMO_ORD,
739
+ AMO_XORD, AMO_MAXD,
740
+ AMO_MAXDU, AMO_MIND,
741
+ AMO_MINDU: begin
742
+ return 2'b11;
743
+ end
744
+ LW, LWU, HLV_W, HLV_WU, HLVX_WU,
745
+ SW, HSV_W, FLW, FSW,
746
+ AMO_LRW, AMO_SCW,
747
+ AMO_SWAPW, AMO_ADDW,
748
+ AMO_ANDW, AMO_ORW,
749
+ AMO_XORW, AMO_MAXW,
750
+ AMO_MAXWU, AMO_MINW,
751
+ AMO_MINWU: begin
752
+ return 2'b10;
753
+ end
754
+ LH, LHU, HLV_H, HLV_HU, HLVX_HU, SH, HSV_H, FLH, FSH: return 2'b01;
755
+ LB, LBU, HLV_B, HLV_BU, SB, HSV_B, FLB, FSB: return 2'b00;
756
+ default: return 2'b11;
757
+ endcase
758
+ endfunction
759
+ // ----------------------
760
+ // MMU Functions
761
+ // ----------------------
762
+
763
+ // checks if final translation page size is 1G when H-extension is enabled
764
+ function automatic logic is_trans_1G(input logic s_st_enbl, input logic g_st_enbl,
765
+ input logic is_s_1G, input logic is_g_1G);
766
+ return (((is_s_1G && s_st_enbl) || !s_st_enbl) && ((is_g_1G && g_st_enbl) || !g_st_enbl));
767
+ endfunction : is_trans_1G
768
+
769
+ // checks if final translation page size is 2M when H-extension is enabled
770
+ function automatic logic is_trans_2M(input logic s_st_enbl, input logic g_st_enbl,
771
+ input logic is_s_1G, input logic is_s_2M,
772
+ input logic is_g_1G, input logic is_g_2M);
773
+ return (s_st_enbl && g_st_enbl) ?
774
+ ((is_s_2M && (is_g_1G || is_g_2M)) || (is_g_2M && (is_s_1G || is_s_2M))) :
775
+ ((is_s_2M && s_st_enbl) || (is_g_2M && g_st_enbl));
776
+ endfunction : is_trans_2M
777
+
778
+ // computes the paddr based on the page size, ppn and offset
779
+ function automatic logic [40:0] make_gpaddr(input logic s_st_enbl, input logic is_1G,
780
+ input logic is_2M, input logic [63:0] vaddr,
781
+ input riscv::pte_t pte);
782
+ logic [40:0] gpaddr;
783
+ if (s_st_enbl) begin
784
+ gpaddr = {pte.ppn[28:0], vaddr[11:0]};
785
+ // Giga page
786
+ if (is_1G) gpaddr[29:12] = vaddr[29:12];
787
+ // Mega page
788
+ if (is_2M) gpaddr[20:12] = vaddr[20:12];
789
+ end else begin
790
+ gpaddr = vaddr[40:0];
791
+ end
792
+ return gpaddr;
793
+ endfunction : make_gpaddr
794
+
795
+ // computes the final gppn based on the guest physical address
796
+ function automatic logic [28:0] make_gppn(input logic s_st_enbl, input logic is_1G,
797
+ input logic is_2M, input logic [28:0] vpn,
798
+ input riscv::pte_t pte);
799
+ logic [28:0] gppn;
800
+ if (s_st_enbl) begin
801
+ gppn = pte.ppn[28:0];
802
+ if (is_2M) gppn[8:0] = vpn[8:0];
803
+ if (is_1G) gppn[17:0] = vpn[17:0];
804
+ end else begin
805
+ gppn = vpn;
806
+ end
807
+ return gppn;
808
+ endfunction : make_gppn
809
+
810
+ // ----------------------
811
+ // Helper functions
812
+ // ----------------------
813
+ // Avoid negative array slices when defining parametrized sizes
814
+ function automatic int unsigned avoid_neg(int n);
815
+ return (n < 0) ? 0 : n;
816
+ endfunction : avoid_neg
817
+
818
+ endpackage
chips/openroad_flow_scripts/flow/designs/src/cva6/core/include/build_config_pkg.sv ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package build_config_pkg;
2
+
3
+ function automatic config_pkg::cva6_cfg_t build_config(config_pkg::cva6_user_cfg_t CVA6Cfg);
4
+ bit IS_XLEN32 = (CVA6Cfg.XLEN == 32) ? 1'b1 : 1'b0;
5
+ bit IS_XLEN64 = (CVA6Cfg.XLEN == 32) ? 1'b0 : 1'b1;
6
+ bit FpPresent = CVA6Cfg.RVF | CVA6Cfg.RVD | CVA6Cfg.XF16 | CVA6Cfg.XF16ALT | CVA6Cfg.XF8;
7
+ bit NSX = CVA6Cfg.XF16 | CVA6Cfg.XF16ALT | CVA6Cfg.XF8 | CVA6Cfg.XFVec; // Are non-standard extensions present?
8
+ int unsigned FLen = CVA6Cfg.RVD ? 64 : // D ext.
9
+ CVA6Cfg.RVF ? 32 : // F ext.
10
+ CVA6Cfg.XF16 ? 16 : // Xf16 ext.
11
+ CVA6Cfg.XF16ALT ? 16 : // Xf16alt ext.
12
+ CVA6Cfg.XF8 ? 8 : // Xf8 ext.
13
+ 1; // Unused in case of no FP
14
+
15
+ // Transprecision floating-point extensions configuration
16
+ bit RVFVec = CVA6Cfg.RVF & CVA6Cfg.XFVec & FLen>32; // FP32 vectors available if vectors and larger fmt enabled
17
+ bit XF16Vec = CVA6Cfg.XF16 & CVA6Cfg.XFVec & FLen>16; // FP16 vectors available if vectors and larger fmt enabled
18
+ bit XF16ALTVec = CVA6Cfg.XF16ALT & CVA6Cfg.XFVec & FLen>16; // FP16ALT vectors available if vectors and larger fmt enabled
19
+ bit XF8Vec = CVA6Cfg.XF8 & CVA6Cfg.XFVec & FLen>8; // FP8 vectors available if vectors and larger fmt enabled
20
+
21
+ bit EnableAccelerator = CVA6Cfg.RVV; // Currently only used by V extension (Ara)
22
+ int unsigned NrWbPorts = (CVA6Cfg.CvxifEn || EnableAccelerator) ? 5 : 4;
23
+
24
+ int unsigned ICACHE_INDEX_WIDTH = $clog2(CVA6Cfg.IcacheByteSize / CVA6Cfg.IcacheSetAssoc);
25
+ int unsigned DCACHE_INDEX_WIDTH = $clog2(CVA6Cfg.DcacheByteSize / CVA6Cfg.DcacheSetAssoc);
26
+ int unsigned DCACHE_OFFSET_WIDTH = $clog2(CVA6Cfg.DcacheLineWidth / 8);
27
+
28
+ // MMU
29
+ int unsigned VpnLen = (CVA6Cfg.XLEN == 64) ? (CVA6Cfg.RVH ? 29 : 27) : 20;
30
+ int unsigned PtLevels = (CVA6Cfg.XLEN == 64) ? 3 : 2;
31
+
32
+ config_pkg::cva6_cfg_t cfg;
33
+
34
+ cfg.XLEN = CVA6Cfg.XLEN;
35
+ cfg.VLEN = CVA6Cfg.VLEN;
36
+ cfg.PLEN = (CVA6Cfg.XLEN == 32) ? 34 : 56;
37
+ cfg.GPLEN = (CVA6Cfg.XLEN == 32) ? 34 : 41;
38
+ cfg.IS_XLEN32 = IS_XLEN32;
39
+ cfg.IS_XLEN64 = IS_XLEN64;
40
+ cfg.XLEN_ALIGN_BYTES = $clog2(CVA6Cfg.XLEN / 8);
41
+ cfg.ASID_WIDTH = (CVA6Cfg.XLEN == 64) ? 16 : 1;
42
+ cfg.VMID_WIDTH = (CVA6Cfg.XLEN == 64) ? 14 : 1;
43
+
44
+ cfg.FpgaEn = CVA6Cfg.FpgaEn;
45
+ cfg.FpgaAlteraEn = CVA6Cfg.FpgaAlteraEn;
46
+ cfg.TechnoCut = CVA6Cfg.TechnoCut;
47
+
48
+ cfg.SuperscalarEn = CVA6Cfg.SuperscalarEn;
49
+ cfg.NrCommitPorts = CVA6Cfg.SuperscalarEn ? unsigned'(2) : CVA6Cfg.NrCommitPorts;
50
+ cfg.NrIssuePorts = unsigned'(CVA6Cfg.SuperscalarEn ? 2 : 1);
51
+ cfg.SpeculativeSb = CVA6Cfg.SuperscalarEn;
52
+
53
+ cfg.NrLoadPipeRegs = CVA6Cfg.NrLoadPipeRegs;
54
+ cfg.NrStorePipeRegs = CVA6Cfg.NrStorePipeRegs;
55
+ cfg.AxiAddrWidth = CVA6Cfg.AxiAddrWidth;
56
+ cfg.AxiDataWidth = CVA6Cfg.AxiDataWidth;
57
+ cfg.AxiIdWidth = CVA6Cfg.AxiIdWidth;
58
+ cfg.AxiUserWidth = CVA6Cfg.AxiUserWidth;
59
+ cfg.MEM_TID_WIDTH = CVA6Cfg.MemTidWidth;
60
+ cfg.NrLoadBufEntries = CVA6Cfg.NrLoadBufEntries;
61
+ cfg.RVF = CVA6Cfg.RVF;
62
+ cfg.RVD = CVA6Cfg.RVD;
63
+ cfg.XF16 = CVA6Cfg.XF16;
64
+ cfg.XF16ALT = CVA6Cfg.XF16ALT;
65
+ cfg.XF8 = CVA6Cfg.XF8;
66
+ cfg.RVA = CVA6Cfg.RVA;
67
+ cfg.RVB = CVA6Cfg.RVB;
68
+ cfg.ZKN = CVA6Cfg.ZKN;
69
+ cfg.RVV = CVA6Cfg.RVV;
70
+ cfg.RVC = CVA6Cfg.RVC;
71
+ cfg.RVH = CVA6Cfg.RVH;
72
+ cfg.RVZCB = CVA6Cfg.RVZCB;
73
+ cfg.RVZCMT = CVA6Cfg.RVZCMT;
74
+ cfg.RVZCMP = CVA6Cfg.RVZCMP;
75
+ cfg.XFVec = CVA6Cfg.XFVec;
76
+ cfg.CvxifEn = CVA6Cfg.CvxifEn;
77
+ cfg.CoproType = CVA6Cfg.CoproType;
78
+ cfg.RVZiCond = CVA6Cfg.RVZiCond;
79
+ cfg.RVZicntr = CVA6Cfg.RVZicntr;
80
+ cfg.RVZihpm = CVA6Cfg.RVZihpm;
81
+ cfg.NR_SB_ENTRIES = CVA6Cfg.NrScoreboardEntries;
82
+ cfg.TRANS_ID_BITS = $clog2(CVA6Cfg.NrScoreboardEntries);
83
+
84
+ cfg.FpPresent = bit'(FpPresent);
85
+ cfg.NSX = bit'(NSX);
86
+ cfg.FLen = unsigned'(FLen);
87
+ cfg.RVFVec = bit'(RVFVec);
88
+ cfg.XF16Vec = bit'(XF16Vec);
89
+ cfg.XF16ALTVec = bit'(XF16ALTVec);
90
+ cfg.XF8Vec = bit'(XF8Vec);
91
+ // Can take 2 or 3 in single issue. 4 or 6 in dual issue.
92
+ cfg.NrRgprPorts = unsigned'(CVA6Cfg.SuperscalarEn ? 4 : 2);
93
+ // cfg.NrRgprPorts = unsigned'(CVA6Cfg.SuperscalarEn ? 6 : 3);
94
+ cfg.NrWbPorts = unsigned'(NrWbPorts);
95
+ cfg.EnableAccelerator = bit'(EnableAccelerator);
96
+ cfg.PerfCounterEn = CVA6Cfg.PerfCounterEn;
97
+ cfg.MmuPresent = CVA6Cfg.MmuPresent;
98
+ cfg.RVS = CVA6Cfg.RVS;
99
+ cfg.RVU = CVA6Cfg.RVU;
100
+ cfg.SoftwareInterruptEn = CVA6Cfg.SoftwareInterruptEn;
101
+
102
+ cfg.HaltAddress = CVA6Cfg.HaltAddress;
103
+ cfg.ExceptionAddress = CVA6Cfg.ExceptionAddress;
104
+ cfg.RASDepth = CVA6Cfg.RASDepth;
105
+ cfg.BTBEntries = CVA6Cfg.BTBEntries;
106
+ cfg.BPType = CVA6Cfg.BPType;
107
+ cfg.BHTEntries = CVA6Cfg.BHTEntries;
108
+ cfg.BHTHist = CVA6Cfg.BHTHist;
109
+ cfg.DmBaseAddress = CVA6Cfg.DmBaseAddress;
110
+ cfg.TvalEn = CVA6Cfg.TvalEn;
111
+ cfg.DirectVecOnly = CVA6Cfg.DirectVecOnly;
112
+ cfg.NrPMPEntries = CVA6Cfg.NrPMPEntries;
113
+ cfg.PMPCfgRstVal = CVA6Cfg.PMPCfgRstVal;
114
+ cfg.PMPAddrRstVal = CVA6Cfg.PMPAddrRstVal;
115
+ cfg.PMPEntryReadOnly = CVA6Cfg.PMPEntryReadOnly;
116
+ cfg.PMPNapotEn = CVA6Cfg.PMPNapotEn;
117
+ cfg.NOCType = CVA6Cfg.NOCType;
118
+ cfg.NrNonIdempotentRules = CVA6Cfg.NrNonIdempotentRules;
119
+ cfg.NonIdempotentAddrBase = CVA6Cfg.NonIdempotentAddrBase;
120
+ cfg.NonIdempotentLength = CVA6Cfg.NonIdempotentLength;
121
+ cfg.NrExecuteRegionRules = CVA6Cfg.NrExecuteRegionRules;
122
+ cfg.ExecuteRegionAddrBase = CVA6Cfg.ExecuteRegionAddrBase;
123
+ cfg.ExecuteRegionLength = CVA6Cfg.ExecuteRegionLength;
124
+ cfg.NrCachedRegionRules = CVA6Cfg.NrCachedRegionRules;
125
+ cfg.CachedRegionAddrBase = CVA6Cfg.CachedRegionAddrBase;
126
+ cfg.CachedRegionLength = CVA6Cfg.CachedRegionLength;
127
+ cfg.MaxOutstandingStores = CVA6Cfg.MaxOutstandingStores;
128
+ cfg.DebugEn = CVA6Cfg.DebugEn;
129
+ cfg.NonIdemPotenceEn = (CVA6Cfg.NrNonIdempotentRules > 0) && (CVA6Cfg.NonIdempotentLength > 0);
130
+ cfg.AxiBurstWriteEn = CVA6Cfg.AxiBurstWriteEn;
131
+
132
+ cfg.ICACHE_SET_ASSOC = CVA6Cfg.IcacheSetAssoc;
133
+ cfg.ICACHE_SET_ASSOC_WIDTH = CVA6Cfg.IcacheSetAssoc > 1 ? $clog2(CVA6Cfg.IcacheSetAssoc) :
134
+ CVA6Cfg.IcacheSetAssoc;
135
+ cfg.ICACHE_INDEX_WIDTH = ICACHE_INDEX_WIDTH;
136
+ cfg.ICACHE_TAG_WIDTH = cfg.PLEN - ICACHE_INDEX_WIDTH;
137
+ cfg.ICACHE_LINE_WIDTH = CVA6Cfg.IcacheLineWidth;
138
+ cfg.ICACHE_USER_LINE_WIDTH = (CVA6Cfg.AxiUserWidth == 1) ? 4 : CVA6Cfg.IcacheLineWidth;
139
+ cfg.DCacheType = CVA6Cfg.DCacheType;
140
+ cfg.DcacheIdWidth = CVA6Cfg.DcacheIdWidth;
141
+ cfg.DCACHE_SET_ASSOC = CVA6Cfg.DcacheSetAssoc;
142
+ cfg.DCACHE_SET_ASSOC_WIDTH = CVA6Cfg.DcacheSetAssoc > 1 ? $clog2(CVA6Cfg.DcacheSetAssoc) :
143
+ CVA6Cfg.DcacheSetAssoc;
144
+ cfg.DCACHE_INDEX_WIDTH = DCACHE_INDEX_WIDTH;
145
+ cfg.DCACHE_TAG_WIDTH = cfg.PLEN - DCACHE_INDEX_WIDTH;
146
+ cfg.DCACHE_LINE_WIDTH = CVA6Cfg.DcacheLineWidth;
147
+ cfg.DCACHE_USER_LINE_WIDTH = (CVA6Cfg.AxiUserWidth == 1) ? 4 : CVA6Cfg.DcacheLineWidth;
148
+ cfg.DCACHE_USER_WIDTH = CVA6Cfg.AxiUserWidth;
149
+ cfg.DCACHE_OFFSET_WIDTH = DCACHE_OFFSET_WIDTH;
150
+ cfg.DCACHE_NUM_WORDS = 2 ** (DCACHE_INDEX_WIDTH - DCACHE_OFFSET_WIDTH);
151
+
152
+ cfg.DCACHE_MAX_TX = unsigned'(2 ** CVA6Cfg.MemTidWidth);
153
+
154
+ cfg.DcacheFlushOnFence = CVA6Cfg.DcacheFlushOnFence;
155
+ cfg.DcacheInvalidateOnFlush = CVA6Cfg.DcacheInvalidateOnFlush;
156
+
157
+ cfg.DATA_USER_EN = CVA6Cfg.DataUserEn;
158
+ cfg.WtDcacheWbufDepth = CVA6Cfg.WtDcacheWbufDepth;
159
+ cfg.FETCH_USER_WIDTH = CVA6Cfg.FetchUserWidth;
160
+ cfg.FETCH_USER_EN = CVA6Cfg.FetchUserEn;
161
+ cfg.AXI_USER_EN = CVA6Cfg.DataUserEn | CVA6Cfg.FetchUserEn;
162
+
163
+ cfg.FETCH_WIDTH = unsigned'(CVA6Cfg.SuperscalarEn ? 64 : 32);
164
+ cfg.FETCH_ALIGN_BITS = $clog2(cfg.FETCH_WIDTH / 8);
165
+ cfg.INSTR_PER_FETCH = cfg.FETCH_WIDTH / (CVA6Cfg.RVC ? 16 : 32);
166
+ cfg.LOG2_INSTR_PER_FETCH = cfg.INSTR_PER_FETCH > 1 ? $clog2(cfg.INSTR_PER_FETCH) : 1;
167
+
168
+ cfg.ModeW = (CVA6Cfg.XLEN == 32) ? 1 : 4;
169
+ cfg.ASIDW = (CVA6Cfg.XLEN == 32) ? 9 : 16;
170
+ cfg.VMIDW = (CVA6Cfg.XLEN == 32) ? 7 : 14;
171
+ cfg.PPNW = (CVA6Cfg.XLEN == 32) ? 22 : 44;
172
+ cfg.GPPNW = (CVA6Cfg.XLEN == 32) ? 22 : 29;
173
+ cfg.MODE_SV = (CVA6Cfg.XLEN == 32) ? config_pkg::ModeSv32 : config_pkg::ModeSv39;
174
+ cfg.SV = (cfg.MODE_SV == config_pkg::ModeSv32) ? 32 : 39;
175
+ cfg.SVX = (cfg.MODE_SV == config_pkg::ModeSv32) ? 34 : 41;
176
+ cfg.InstrTlbEntries = CVA6Cfg.InstrTlbEntries;
177
+ cfg.DataTlbEntries = CVA6Cfg.DataTlbEntries;
178
+ cfg.UseSharedTlb = CVA6Cfg.UseSharedTlb;
179
+ cfg.SharedTlbDepth = CVA6Cfg.SharedTlbDepth;
180
+ cfg.VpnLen = VpnLen;
181
+ cfg.PtLevels = PtLevels;
182
+
183
+ cfg.X_NUM_RS = cfg.NrRgprPorts / cfg.NrIssuePorts;
184
+ cfg.X_ID_WIDTH = cfg.TRANS_ID_BITS;
185
+ cfg.X_RFR_WIDTH = cfg.XLEN;
186
+ cfg.X_RFW_WIDTH = cfg.XLEN;
187
+ cfg.X_NUM_HARTS = 1;
188
+ cfg.X_HARTID_WIDTH = cfg.XLEN;
189
+ cfg.X_DUALREAD = 0;
190
+ cfg.X_DUALWRITE = 0;
191
+ cfg.X_ISSUE_REGISTER_SPLIT = 0;
192
+
193
+ return cfg;
194
+ endfunction
195
+
196
+ endpackage