// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
//
// Bound into the otbn_rf_base and used to help collect call stack information for coverage.

interface otbn_rf_base_if (
  input       clk_i,
  input       rst_ni,

  // Signal names from the otbn_rf_base module (where we are bound)
  input logic pop_stack_a, 
  input logic pop_stack_b,
  input logic pop_stack,
  input logic push_stack,
  input logic stack_full,
  input logic stack_data_valid 
);

  function automatic otbn_env_pkg::call_stack_flags_t get_call_stack_flags();
    return '{
              pop_a: pop_stack && pop_stack_a,
              pop_b: pop_stack && pop_stack_b,
              push: push_stack
            };
  endfunction

  function automatic otbn_env_pkg::stack_fullness_e get_call_stack_fullness();
    if (stack_full) begin
      return otbn_env_pkg::StackFull;
    end
    if (stack_data_valid) begin
      return otbn_env_pkg::StackPartial;
    end
    return otbn_env_pkg::StackEmpty;
  endfunction

endinterface

// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
//
// Bound into the otbn_rf_base and used to help collect call stack information for coverage.

interface otbn_rf_base_if (
  input       clk_i,
  input       rst_ni,

  // Signal names from the otbn_rf_base module (where we are bound)
  input logic pop_stack_a, 
  input logic pop_stack_b,
  input logic push_stack_reqd,
  input logic stack_full,
  input logic stack_data_valid 
);

  function automatic otbn_env_pkg::call_stack_flags_t get_call_stack_flags();
    return '{
              pop_a: pop_stack_a,
              pop_b: pop_stack_b,
              push: push_stack_reqd
            };
  endfunction

  function automatic otbn_env_pkg::stack_fullness_e get_call_stack_fullness();
    if (stack_full) begin
      return otbn_env_pkg::StackFull;
    end
    if (stack_data_valid) begin
      return otbn_env_pkg::StackPartial;
    end
    return otbn_env_pkg::StackEmpty;
  endfunction

endinterface

[otbn,dv] Fix callstack flags in coverage

pop_stack_a (resp. pop_stack_b) is signalled if the instruction in
question tries to read from x1 with operand a (resp b). pop_stack is
only true if, furthermore, rd_commit_i holds. This guarantees that the
instruction isn't stalled, but also implies that the instruction
didn't cause a fault.

We don't need to check for stalling instructions when calculating
these flags: they'll only be read in otbn_trace_monitor.sv if the
instruction isn't stalled. But we *do* need to make sure we get the
raw values for a faulting instruction. Without this fix, it would be
impossible to see e.g. how we caused an underflow with an instruction
like "add x0, x1, x1".

Similarly, the push_stack signal requires that the instruction didn't
cause a fault: we actually want the raw signal in push_stack_reqd.

Signed-off-by: Rupert Swarbrick <290093e2ad2515ed2f14d749293c8cf8e02ca274@lowrisc.org>
