chore: Refactor Rom class to include ReadWritePreconditions
This commit is contained in:
@@ -206,8 +206,16 @@ class Rom : public core::ExperimentFlags {
|
|||||||
*/
|
*/
|
||||||
absl::Status SaveAllPalettes();
|
absl::Status SaveAllPalettes();
|
||||||
|
|
||||||
|
absl::Status ReadWritePreconditions() {
|
||||||
|
if (!is_loaded_) {
|
||||||
|
return absl::FailedPreconditionError("ROM not loaded");
|
||||||
|
}
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
// Read functions
|
// Read functions
|
||||||
absl::StatusOr<uint8_t> ReadByte(int offset) {
|
absl::StatusOr<uint8_t> ReadByte(int offset) {
|
||||||
|
RETURN_IF_ERROR(ReadWritePreconditions());
|
||||||
if (offset >= rom_data_.size()) {
|
if (offset >= rom_data_.size()) {
|
||||||
return absl::FailedPreconditionError("Offset out of range");
|
return absl::FailedPreconditionError("Offset out of range");
|
||||||
}
|
}
|
||||||
@@ -215,6 +223,7 @@ class Rom : public core::ExperimentFlags {
|
|||||||
}
|
}
|
||||||
|
|
||||||
absl::StatusOr<uint16_t> ReadWord(int offset) {
|
absl::StatusOr<uint16_t> ReadWord(int offset) {
|
||||||
|
RETURN_IF_ERROR(ReadWritePreconditions());
|
||||||
if (offset + 1 >= rom_data_.size()) {
|
if (offset + 1 >= rom_data_.size()) {
|
||||||
return absl::FailedPreconditionError("Offset out of range");
|
return absl::FailedPreconditionError("Offset out of range");
|
||||||
}
|
}
|
||||||
@@ -227,8 +236,9 @@ class Rom : public core::ExperimentFlags {
|
|||||||
}
|
}
|
||||||
|
|
||||||
absl::StatusOr<uint32_t> ReadLong(int offset) {
|
absl::StatusOr<uint32_t> ReadLong(int offset) {
|
||||||
|
RETURN_IF_ERROR(ReadWritePreconditions());
|
||||||
if (offset + 2 >= rom_data_.size()) {
|
if (offset + 2 >= rom_data_.size()) {
|
||||||
return absl::FailedPreconditionError("Offset out of range");
|
return absl::OutOfRangeError("Offset out of range");
|
||||||
}
|
}
|
||||||
auto result = (uint32_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8) |
|
auto result = (uint32_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8) |
|
||||||
(rom_data_[offset + 2] << 16));
|
(rom_data_[offset + 2] << 16));
|
||||||
@@ -237,8 +247,9 @@ class Rom : public core::ExperimentFlags {
|
|||||||
|
|
||||||
absl::StatusOr<std::vector<uint8_t>> ReadByteVector(uint32_t offset,
|
absl::StatusOr<std::vector<uint8_t>> ReadByteVector(uint32_t offset,
|
||||||
uint32_t length) {
|
uint32_t length) {
|
||||||
|
RETURN_IF_ERROR(ReadWritePreconditions());
|
||||||
if (offset + length > rom_data_.size()) {
|
if (offset + length > rom_data_.size()) {
|
||||||
return absl::FailedPreconditionError("Offset and length out of range");
|
return absl::OutOfRangeError("Offset and length out of range");
|
||||||
}
|
}
|
||||||
std::vector<uint8_t> result;
|
std::vector<uint8_t> result;
|
||||||
for (int i = offset; i < offset + length; i++) {
|
for (int i = offset; i < offset + length; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user