Skip to content

Commit

Permalink
AmdFlash: Use static_vector to store command list.
Browse files Browse the repository at this point in the history
Also already add a dummy “status” field for use in a later commit.
  • Loading branch information
grauw committed May 19, 2024
1 parent f8a63a8 commit 52cace2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
41 changes: 26 additions & 15 deletions src/memory/AmdFlash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "one_of.hh"
#include "ranges.hh"
#include "serialize.hh"
#include "serialize_stl.hh"
#include "xrange.hh"
#include <bit>
#include <cassert>
Expand Down Expand Up @@ -198,7 +199,7 @@ AmdFlash::GetSectorInfoResult AmdFlash::getSectorInfo(size_t address) const

void AmdFlash::reset()
{
cmdIdx = 0;
cmd.clear();
setState(State::IDLE);
}

Expand Down Expand Up @@ -264,10 +265,8 @@ const uint8_t* AmdFlash::getReadCacheLine(size_t address) const

void AmdFlash::write(size_t address, uint8_t value)
{
assert(cmdIdx < MAX_CMD_SIZE);
cmd[cmdIdx].addr = address;
cmd[cmdIdx].value = value;
++cmdIdx;
cmd.push_back({address, value});

if (checkCommandManufacturer() ||
checkCommandEraseSector() ||
checkCommandProgram() ||
Expand Down Expand Up @@ -298,7 +297,7 @@ bool AmdFlash::checkCommandEraseSector()
{
static constexpr std::array<uint8_t, 5> cmdSeq = {0xaa, 0x55, 0x80, 0xaa, 0x55};
if (partialMatch(cmdSeq)) {
if (cmdIdx < 6) return true;
if (cmd.size() < 6) return true;
if (cmd[5].value == 0x30) {
auto addr = cmd[5].addr;
auto [sector, sectorSize, offset] = getSectorInfo(addr);
Expand All @@ -315,7 +314,7 @@ bool AmdFlash::checkCommandEraseChip()
{
static constexpr std::array<uint8_t, 5> cmdSeq = {0xaa, 0x55, 0x80, 0xaa, 0x55};
if (partialMatch(cmdSeq)) {
if (cmdIdx < 6) return true;
if (cmd.size() < 6) return true;
if (cmd[5].value == 0x10) {
if (ram) ram->memset(0, 0xff, ram->size());
}
Expand All @@ -326,7 +325,7 @@ bool AmdFlash::checkCommandEraseChip()
bool AmdFlash::checkCommandProgramHelper(size_t numBytes, std::span<const uint8_t> cmdSeq)
{
if (partialMatch(cmdSeq)) {
if (cmdIdx < (cmdSeq.size() + numBytes)) return true;
if (cmd.size() < (cmdSeq.size() + numBytes)) return true;
for (auto i : xrange(cmdSeq.size(), cmdSeq.size() + numBytes)) {
auto addr = cmd[i].addr;
auto [sector, sectorSize, offset] = getSectorInfo(addr);
Expand Down Expand Up @@ -361,10 +360,10 @@ bool AmdFlash::checkCommandManufacturer()
{
static constexpr std::array<uint8_t, 3> cmdSeq = {0xaa, 0x55, 0x90};
if (partialMatch(cmdSeq)) {
if (cmdIdx == 3) {
if (cmd.size() == 3) {
setState(State::IDENT);
}
if (cmdIdx < 4) return true;
if (cmd.size() < 4) return true;
}
return false;
}
Expand All @@ -376,7 +375,7 @@ bool AmdFlash::partialMatch(std::span<const uint8_t> dataSeq) const
(void)addrSeq; (void)cmdAddr; // suppress (invalid) gcc warning

assert(dataSeq.size() <= 5);
return ranges::all_of(xrange(std::min(unsigned(dataSeq.size()), cmdIdx)), [&](auto i) {
return ranges::all_of(xrange(std::min(dataSeq.size(), cmd.size())), [&](auto i) {
// convert the address to the '11 bit case'
auto addr = (addressing == Addressing::BITS_12) ? cmd[i].addr >> 1 : cmd[i].addr;
return ((addr & 0x7FF) == cmdAddr[addrSeq[i]]) &&
Expand All @@ -398,13 +397,25 @@ void AmdFlash::AmdCmd::serialize(Archive& ar, unsigned /*version*/)
"value", value);
}

// version 1: Initial version.
// version 2: Added vppWpPinLow.
// version 3: Changed cmd to static_vector, added status.
template<typename Archive>
void AmdFlash::serialize(Archive& ar, unsigned version)
{
ar.serialize("ram", *ram,
"cmd", cmd,
"cmdIdx", cmdIdx,
"state", state);
ar.serialize("ram", *ram);
if (ar.versionAtLeast(version, 3)) {
uint8_t status = 0x80;
ar.serialize("cmd", cmd,
"status", status);
} else {
std::array<AmdCmd, 8> cmdArray;
unsigned cmdSize = 0;
ar.serialize("cmd", cmdArray,
"cmdIdx", cmdSize);
cmd = {from_range, subspan(cmdArray, 0, cmdSize)};
}
ar.serialize("state", state);
if (ar.versionAtLeast(version, 2)) {
ar.serialize("vppWpPinLow", vppWpPinLow);
}
Expand Down
6 changes: 3 additions & 3 deletions src/memory/AmdFlash.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "MemBuffer.hh"
#include "serialize_meta.hh"
#include "static_vector.hh"
#include <array>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -113,12 +114,11 @@ private:
const Addressing addressing;

static constexpr unsigned MAX_CMD_SIZE = 8;
std::array<AmdCmd, MAX_CMD_SIZE> cmd;
unsigned cmdIdx;
static_vector<AmdCmd, MAX_CMD_SIZE> cmd;
State state = State::IDLE;
bool vppWpPinLow = false; // true = protection on
};
SERIALIZE_CLASS_VERSION(AmdFlash, 2);
SERIALIZE_CLASS_VERSION(AmdFlash, 3);

} // namespace openmsx

Expand Down

0 comments on commit 52cace2

Please sign in to comment.