From e10be09dcf919d50f03547924dde0157692cc8f8 Mon Sep 17 00:00:00 2001 From: hungweihsu Date: Thu, 9 Feb 2023 05:39:15 +0000 Subject: [PATCH] dev: add method to set initial register value out of constructor. The initial value of register is set in constructor but there is no standard way to assign the initial value and default value at the same time out of that. So we decided to add an extra method to set the initialValue to current register value. The usecase would be: reg.get().field1 = val1; reg.get().field2 = val2; reg.resetInitialValue(); Change-Id: Ibc5454e2945cc6aff943e6599043edd8ca442f5f Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/67917 Tested-by: kokoro Reviewed-by: Gabe Black Maintainer: Gabe Black --- src/dev/reg_bank.hh | 3 +++ src/dev/reg_bank.test.cc | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/dev/reg_bank.hh b/src/dev/reg_bank.hh index 3d8dc576cb..3a89a00ab6 100644 --- a/src/dev/reg_bank.hh +++ b/src/dev/reg_bank.hh @@ -759,6 +759,9 @@ class RegisterBank : public RegisterBankBase // constructor. This is intended to be used in a resetter function. const Data &initialValue() const { return _resetData; } + // Reset the initial value, which is normally set in the constructor, + // to the register's current value. + void resetInitialValue() { _resetData = _data; } /* * Interface for accessing the register's state, for use by the diff --git a/src/dev/reg_bank.test.cc b/src/dev/reg_bank.test.cc index 4439526e35..c618ef16d4 100644 --- a/src/dev/reg_bank.test.cc +++ b/src/dev/reg_bank.test.cc @@ -881,6 +881,28 @@ TEST_F(TypedRegisterTest, DefaultResetter) EXPECT_EQ(reg.get(), initial_value); } +// Set initial value later than constructor +TEST_F(TypedRegisterTest, LateInitialValueAssignment) +{ + BackingType initial_value = reg.get(); + BackingType new_initial_value = initial_value + 1; + + reg.get() = new_initial_value; + reg.resetInitialValue(); + + EXPECT_EQ(reg.get(), new_initial_value); + EXPECT_EQ(reg.initialValue(), new_initial_value); + + reg.get() = new_initial_value + 1; + EXPECT_EQ(reg.get(), new_initial_value + 1); + EXPECT_EQ(reg.initialValue(), new_initial_value); + + reg.reset(); + + EXPECT_EQ(reg.get(), new_initial_value); + EXPECT_EQ(reg.initialValue(), new_initial_value); +} + // Set a custom resetter for a register. TEST_F(TypedRegisterTest, Resetter) {