Software APIs
mock_csr.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_BASE_MOCK_CSR_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_BASE_MOCK_CSR_H_
7 
8 #include "gmock/gmock.h"
9 #include "gtest/gtest.h"
10 #include "sw/device/lib/base/csr.h"
11 #include "sw/device/lib/base/global_mock.h"
12 
13 namespace mock_csr {
14 
15 namespace internal {
16 
17 class MockCsr : public ::global_mock::GlobalMock<MockCsr> {
18  public:
19  MOCK_METHOD(uint32_t, Read, (uint32_t csr));
20  MOCK_METHOD(void, Write, (uint32_t csr, uint32_t value));
21  MOCK_METHOD(void, SetBits, (uint32_t csr, uint32_t mask));
22  MOCK_METHOD(void, ClearBits, (uint32_t csr, uint32_t mask));
23 };
24 } // namespace internal
25 
26 using MockCsr = testing::StrictMock<internal::MockCsr>;
27 
28 } // namespace mock_csr
29 
30 /**
31  * Expect a read of the provided CSR.
32  *
33  * The tested code must use `CSR_READ(csr, dest)`.
34  *
35  * @param csr The CSR that will be read. Note: does
36  * not need to be constant.
37  * @param value The value to return from the read operation.
38  */
39 #define EXPECT_CSR_READ(csr, value) \
40  EXPECT_CALL(::mock_csr::MockCsr::Instance(), Read(csr)) \
41  .WillOnce(::testing::Return(value))
42 
43 /**
44  * Expect a write to the provided CSR.
45  *
46  * The tested code must use `CSR_WRITE(csr, value)`.
47  *
48  * @param csr The CSR that will be written to.
49  * Note: does not need to be constant.
50  * @param value The value that is expected to be written to the CSR.
51  */
52 #define EXPECT_CSR_WRITE(csr, value) \
53  EXPECT_CALL(::mock_csr::MockCsr::Instance(), Write(csr, value))
54 
55 /**
56  * Expect a set masked bits operation on the provided CSR.
57  *
58  * The tested code must use `CSR_SET_BITS(csr, mask)`
59  *
60  * @param csr The CSR that will be targeted. Note:
61  * does not need to be constant.
62  * @param mask The expected mask containing the bits to set.
63  */
64 #define EXPECT_CSR_SET_BITS(csr, mask) \
65  EXPECT_CALL(::mock_csr::MockCsr::Instance(), SetBits(csr, mask))
66 
67 /**
68  * Expect a clear masked bits operation on the provided CSR.
69  *
70  * The tested code must use `CSR_CLEAR_BITS(csr, mask)`.
71  *
72  * @param csr The CSR that will be targeted. Note:
73  * does not need to be constant.
74  * @param mask The expected mask containing the bits to clear.
75  */
76 #define EXPECT_CSR_CLEAR_BITS(csr, mask) \
77  EXPECT_CALL(::mock_csr::MockCsr::Instance(), ClearBits(csr, mask))
78 
79 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_BASE_MOCK_CSR_H_