DataHoarder
6d3760c96e
All checks were successful
continuous-integration/drone/push Build is passing
207 lines
7.9 KiB
C++
207 lines
7.9 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 "CalcImm.h"
|
|
#include "../Registers.h"
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
#include "../OutputContext.h"
|
|
|
|
void Instruction::CalcImm::fromBytes(uint32_t offset, const std::vector<uint8_t> &bytes) {
|
|
_address = offset;
|
|
|
|
uint32_t command = bytes[offset++];
|
|
|
|
operation = static_cast<Calc::Operation>((command >> 2) & 0b111);
|
|
addressOffset = command & 0b11;
|
|
|
|
addressDestination = bytes[offset++] << 16;
|
|
addressDestination |= bytes[offset++] << 8;
|
|
addressDestination |= bytes[offset++];
|
|
|
|
offset++;
|
|
|
|
addressSource = bytes[offset++] << 16;
|
|
addressSource |= bytes[offset++] << 8;
|
|
addressSource |= bytes[offset++];
|
|
|
|
|
|
value = bytes[offset++] << 24;
|
|
value |= bytes[offset++] << 16;
|
|
value |= bytes[offset++] << 8;
|
|
value |= bytes[offset++];
|
|
|
|
|
|
_endAddress = offset;
|
|
}
|
|
|
|
std::string Instruction::CalcImm::toString() const {
|
|
std::stringstream op;
|
|
op << "CALC_IMM " << getAddressRegisterName(addressDestination, addressOffset) << " = "
|
|
<< getAddressRegisterName(addressSource, addressOffset);
|
|
switch (operation) {
|
|
case Calc::Operation::AND:
|
|
op << " & ";
|
|
break;
|
|
case Calc::Operation::OR:
|
|
op << " | ";
|
|
break;
|
|
case Calc::Operation::ADD:
|
|
op << " + ";
|
|
break;
|
|
case Calc::Operation::SUB:
|
|
op << " - ";
|
|
break;
|
|
case Calc::Operation::LSHIFT:
|
|
op << " << ";
|
|
break;
|
|
case Calc::Operation::RSHIFT:
|
|
op << " >> ";
|
|
break;
|
|
case Calc::Operation::ARSHIFT:
|
|
op << " >>> ";
|
|
break;
|
|
case Calc::Operation::UNDEFINED:
|
|
op << " UNDEFINED ";
|
|
break;
|
|
}
|
|
op << "0x" << std::hex << std::setw(8) << std::setfill('0') << value;
|
|
return op.str();
|
|
}
|
|
|
|
std::vector<Instruction::OutputFormat> Instruction::CalcImm::toOutputFormat(const OutputContext &context) const {
|
|
std::stringstream opname;
|
|
switch (operation) {
|
|
case Calc::Operation::AND:
|
|
opname << "AND";
|
|
break;
|
|
case Calc::Operation::OR:
|
|
opname << "OR";
|
|
break;
|
|
case Calc::Operation::ADD:
|
|
opname << "ADD";
|
|
break;
|
|
case Calc::Operation::SUB:
|
|
opname << "SUB";
|
|
break;
|
|
case Calc::Operation::LSHIFT:
|
|
opname << "LSHIFT";
|
|
break;
|
|
case Calc::Operation::RSHIFT:
|
|
opname << "RSHIFT";
|
|
break;
|
|
case Calc::Operation::ARSHIFT:
|
|
opname << "ARSHIFT";
|
|
break;
|
|
case Calc::Operation::UNDEFINED:
|
|
opname << "UNDEFINED";
|
|
break;
|
|
}
|
|
|
|
return std::vector<OutputFormat>{
|
|
OutputFormat{getAddress(), dynamic_cast<std::stringstream &>(opname << "_IMM "
|
|
<< context.getAddressRegisterName(
|
|
addressDestination,
|
|
addressOffset) << " = ").str()},
|
|
OutputFormat{getAddress() + 4, dynamic_cast<std::stringstream &>(std::stringstream("") << " SOURCE "
|
|
<< context.getAddressRegisterName(
|
|
addressSource,
|
|
addressOffset)).str()},
|
|
OutputFormat{getAddress() + 8,
|
|
dynamic_cast<std::stringstream &>(std::stringstream("") << " TARGET 0x" << std::hex
|
|
<< std::setw(8) << std::setfill('0')
|
|
<< value).str()},
|
|
};
|
|
}
|
|
|
|
std::vector<uint32_t> Instruction::CalcImm::getPossibleBranches() const {
|
|
return std::vector<uint32_t>{_endAddress};
|
|
}
|
|
|
|
std::vector<std::pair<uint32_t, std::unordered_map<uint32_t, uint32_t>>>
|
|
Instruction::CalcImm::execute(AnalysisState &state) const {
|
|
|
|
uint32_t memoryOffset = state.getAddressOffset(addressOffset);
|
|
|
|
uint32_t v_a = state.getRegister(memoryOffset + addressSource);
|
|
|
|
switch (operation) {
|
|
case Calc::Operation::AND:
|
|
state.setRegister(addressDestination, v_a & value);
|
|
break;
|
|
case Calc::Operation::OR:
|
|
state.setRegister(addressDestination, v_a | value);
|
|
break;
|
|
case Calc::Operation::ADD:
|
|
state.setRegister(addressDestination, v_a + value);
|
|
break;
|
|
case Calc::Operation::SUB:
|
|
state.setRegister(addressDestination, v_a - value);
|
|
break;
|
|
case Calc::Operation::LSHIFT:
|
|
state.setRegister(addressDestination, v_a << value);
|
|
break;
|
|
case Calc::Operation::RSHIFT:
|
|
state.setRegister(addressDestination, v_a >> value);
|
|
break;
|
|
case Calc::Operation::ARSHIFT:
|
|
state.setRegister(addressDestination, (uint32_t) ((int32_t) v_a >> value));
|
|
break;
|
|
case Calc::Operation::UNDEFINED:
|
|
break;
|
|
}
|
|
|
|
state.current = _endAddress;
|
|
|
|
state.addKnownJump(getEndAddress(), getAddress(), JumpKind::Continue);
|
|
|
|
return {};
|
|
}
|
|
|
|
std::vector<uint8_t> Instruction::CalcImm::toBytes() const {
|
|
std::vector<uint8_t> bytes;
|
|
|
|
bytes.emplace_back((uint8_t) getCommand() | ((uint8_t) operation << 2) | addressOffset);
|
|
|
|
bytes.emplace_back((addressDestination >> 16) & 0xFF);
|
|
bytes.emplace_back((addressDestination >> 8) & 0xFF);
|
|
bytes.emplace_back(addressDestination & 0xFF);
|
|
|
|
bytes.emplace_back(0x00);
|
|
|
|
bytes.emplace_back((addressSource >> 16) & 0xFF);
|
|
bytes.emplace_back((addressSource >> 8) & 0xFF);
|
|
bytes.emplace_back(addressSource & 0xFF);
|
|
|
|
bytes.emplace_back((value >> 24) & 0xFF);
|
|
bytes.emplace_back((value >> 16) & 0xFF);
|
|
bytes.emplace_back((value >> 8) & 0xFF);
|
|
bytes.emplace_back(value & 0xFF);
|
|
|
|
return bytes;
|
|
}
|