Skip to content

Commit

Permalink
dev: add method to set initial register value out of constructor.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Gabe Black <[email protected]>
Maintainer: Gabe Black <[email protected]>
  • Loading branch information
hungweihsuG committed Feb 15, 2023
1 parent ea9239a commit e10be09
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/dev/reg_bank.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/dev/reg_bank.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit e10be09

Please sign in to comment.