// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
{
  testpoints: [
    {
      // this testplan should be imported by all IPs containing shadowed CSRs
      name: shadow_reg_update_error
      desc: '''Verify shadowed registers' update error.

            - Randomly pick a shadowed register in the DUT.
            - Write it twice with different values.
            - Verify that the update error alert is triggered and the register value remains
              unchanged.
            - Verify the update_error status register field is set to 1.
            - Repeat the above steps a bunch of times.
            '''
      stage: V2S
      tests: ["{name}_shadow_reg_errors"]
    }

    {
      name: shadow_reg_read_clear_staged_value
      desc: '''Verify reading a shadowed register will clear its staged value.

            - Randomly pick a shadowed register in the DUT.
            - Write it once and read it back to clear the staged value.
            - Then write it twice with the same new value (but different from the previous step).
            - Read it back to verify the new value and ensure that the update error alert did not
              trigger.
            - Verify the update_error status register field remains the same value.
            - Repeat the above steps a bunch of times.
            '''
      stage: V2S
      tests: ["{name}_shadow_reg_errors"]
    }

    {
      name: shadow_reg_storage_error
      desc: '''Verify shadowed registers' storage error.

            - Randomly pick a shadowed register in the DUT.
            - Backdoor write to shadowed or committed flops to create a storage fatal alert.
            - Check if fatal alert continuously fires until reset.
            - Verify that all other frontdoor write attempts are blocked during the storage error.
            - Verify that storage_error status register field is set to 1.
            - Reset the DUT.
            - Read all CSRs to ensure the DUT is properly reset.
            - Repeat the above steps a bunch of times.
            '''
      stage: V2S
      tests: ["{name}_shadow_reg_errors"]
    }

    {
      name: shadowed_reset_glitch
      desc: '''Verify toggle shadowed_rst_n pin can trigger storage error.

            - Randomly drive `shadowed_rst_n` pin to low or `rst_n` pin to low.
            - check if any registers have been written before the reset. If so check if storage
              error fatal alert is triggered.
            - Check status register.
            - Drive `shadowed_rst_n` pin or `rst_n` pin back to high.
            - If fatal alert is triggered, reset the DUT.
            - Read all CSRs to ensure the DUT is properly reset.
            - Repeat the above steps a bunch of times.
            '''
      stage: V2S
      tests: ["{name}_shadow_reg_errors"]
    }

    {
      // this testplan should be imported by all IPs containing shadowed CSRs
      name: shadow_reg_update_error_with_csr_rw
      desc: '''Run shadow_reg_update_error sequence in parallel with csr_rw sequence.

            - Randomly select one of the above sequences.
            - Apply csr_rw sequence in parallel but disable the `csr_access_abort` to ensure all
              shadowed registers' write/read to be executed without aborting.
            - Repeat the above steps a bunch of times.
            '''
      stage: V2S
      tests: ["{name}_shadow_reg_errors_with_csr_rw"]
    }
  ]

  covergroups: [
    {
      name: shadow_field_errs_cg
      desc: '''Cover all shadow register errors for each register field.

            For all register fields within the shadowed register, this coverpoint covers the
            following errors:
            - Update error
            - Storage error
            '''
    }
  ]
}
