rrcSmall/src/instructions/Init.cpp
DataHoarder 875f6588d9
All checks were successful
continuous-integration/drone/push Build is passing
Implement proper Init OutputContext
2021-08-05 16:29:07 +02:00

148 lines
5.7 KiB
C++

/*****************************************************************************
* Copyright (c) 2020, rrcSmall FM10K-Documentation Contributors
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include "Init.h"
#include "../Registers.h"
#include <iostream>
#include <iomanip>
#include "../OutputContext.h"
void Instruction::Init::fromBytes(uint32_t offset, const std::vector<uint8_t> &bytes) {
_address = offset;
uint32_t command = bytes[offset++];
increment = (command >> 2) & 0b1;
addressOffset = command & 0b11;
address = bytes[offset++] << 16;
address |= bytes[offset++] << 8;
address |= bytes[offset++];
offset++;
count = bytes[offset++] << 16;
count |= bytes[offset++] << 8;
count |= bytes[offset++];
data = bytes[offset++] << 24;
data |= bytes[offset++] << 16;
data |= bytes[offset++] << 8;
data |= bytes[offset++];
data_add = bytes[offset++] << 24;
data_add |= bytes[offset++] << 16;
data_add |= bytes[offset++] << 8;
data_add |= bytes[offset++];
_endAddress = offset;
}
std::string Instruction::Init::toString() const {
std::stringstream op;
op << "INIT INTO " << getAddressRegisterName(address, addressOffset);
if (count > 1 && increment) {
op << " UNTIL " << getAddressRegisterName(address + count - 1, addressOffset);
}
op << " INCREMENT " << std::dec << (uint32_t) increment;
op << " COUNT " << std::hex << std::setw(6) << std::setfill('0') << count;
op << " DATA 0x" << std::hex << std::setw(8) << std::setfill('0') << data;
op << " ADD 0x" << std::hex << std::setw(8) << std::setfill('0') << data_add;
return op.str();
}
std::vector<Instruction::OutputFormat> Instruction::Init::toOutputFormat(const OutputContext &context) const {
std::vector<OutputFormat> f{
OutputFormat{getAddress() + 0 * 4,
dynamic_cast<std::stringstream &>(std::stringstream("") << "INIT" << " "
<< context.getAddressRegisterName(
address, addressOffset)
<< ", INC " << std::dec
<< (uint32_t) increment).str()}
};
f.emplace_back(OutputFormat{getAddress() + 1 * 4,
dynamic_cast<std::stringstream &>(std::stringstream("") << " COUNT " << std::dec
<< count).str()});
f.emplace_back(OutputFormat{getAddress() + 2 * 4,
dynamic_cast<std::stringstream &>(std::stringstream("") << " DATA 0x" << std::hex << std::setw(8) << std::setfill('0') << data).str()});
f.emplace_back(OutputFormat{getAddress() + 3 * 4,
dynamic_cast<std::stringstream &>(std::stringstream("") << " ADD 0x" << std::hex << std::setw(8) << std::setfill('0') << data_add).str()});
return f;
}
std::vector<uint32_t> Instruction::Init::getPossibleBranches() const {
return std::vector<uint32_t>{_endAddress};
}
std::vector<std::pair<uint32_t, std::unordered_map<uint32_t, uint32_t>>>
Instruction::Init::execute(AnalysisState &state) const {
for (uint32_t i = 0; i < count; ++i) {
state.setRegister(address + increment, data + data_add * i);
}
state.current = _endAddress;
state.addKnownJump(getEndAddress(), getAddress(), JumpKind::Continue);
return {};
}
std::vector<uint8_t> Instruction::Init::toBytes() const {
std::vector<uint8_t> bytes;
bytes.emplace_back((uint8_t) getCommand() | (increment << 2) | addressOffset);
bytes.emplace_back((address >> 16) & 0xFF);
bytes.emplace_back((address >> 8) & 0xFF);
bytes.emplace_back(address & 0xFF);
bytes.emplace_back(0x00);
bytes.emplace_back((count >> 16) & 0xFF);
bytes.emplace_back((count >> 8) & 0xFF);
bytes.emplace_back(count & 0xFF);
bytes.emplace_back((data >> 24) & 0xFF);
bytes.emplace_back((data >> 16) & 0xFF);
bytes.emplace_back((data >> 8) & 0xFF);
bytes.emplace_back(data & 0xFF);
bytes.emplace_back((data_add >> 24) & 0xFF);
bytes.emplace_back((data_add >> 16) & 0xFF);
bytes.emplace_back((data_add >> 8) & 0xFF);
bytes.emplace_back(data_add & 0xFF);
return bytes;
}