#include "M68000Instruction.h" #ifndef __M68000_NBCD_H__ #define __M68000_NBCD_H__ namespace M68000 { class NBCD :public M68000Instruction { public: virtual NBCD* Clone() const {return new NBCD();} virtual NBCD* ClonePlacement(void* buffer) const {return new(buffer) NBCD();} virtual size_t GetOpcodeClassByteSize() const {return sizeof(*this);} virtual bool RegisterOpcode(OpcodeTable& table) const { return table.AllocateRegionToOpcode(this, L"0100100000DDDDDD", L"DDDDDD=000000-000111,010000-110111,111000,111001"); } virtual std::wstring GetOpcodeName() const { return L"NBCD"; } virtual Disassembly M68000Disassemble(const M68000::LabelSubstitutionSettings& labelSettings) const { return Disassembly(GetOpcodeName(), _target.Disassemble(labelSettings)); } virtual void M68000Decode(M68000* cpu, const M68000Long& location, const M68000Word& data, bool transparent) { // ----------------------------------------------------------------- // |15 |14 |13 |12 |11 |10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | // |---|---|---|---|---|---|---|---|---|---|-----------|-----------| // | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | MODE | REGISTER | // ----------------------------------------========================= // |-------------------| _target.Decode(data.GetDataSegment(0, 3), data.GetDataSegment(3, 3), BITCOUNT_BYTE, location + GetInstructionSize(), cpu, transparent, GetInstructionRegister()); AddInstructionSize(_target.ExtensionSize()); if ((_target.GetAddressMode() == EffectiveAddress::Mode::DataRegDirect) || (_target.GetAddressMode() == EffectiveAddress::Mode::AddRegDirect)) { AddExecuteCycleCount(ExecuteTime(6, 1, 0)); } else { AddExecuteCycleCount(ExecuteTime(8, 1, 1)); AddExecuteCycleCount(_target.DecodeTime()); } } virtual ExecuteTime M68000Execute(M68000* cpu, const M68000Long& location) const { double additionalTime = 0; M68000Byte op1Base10; // Perform the operation additionalTime += _target.ReadWithoutAdjustingAddress(cpu, op1Base10, GetInstructionRegister()); // This code is based on information provided by flamewing from the following source: // http://gendev.spritesmind.net/forum/viewtopic.php?p=30943#p30943 // Correct behaviour of the result is tested and confirmed by "bcd-verifier-u1.bin" M68000Byte result = M68000Byte(0) - (op1Base10 + cpu->GetX()); M68000Byte binaryCarry = (op1Base10 | result) & 0x88; M68000Byte correctionFactor = binaryCarry - (binaryCarry >> 2); M68000Byte resultBase10 = result - correctionFactor; additionalTime += _target.Write(cpu, resultBase10, GetInstructionRegister()); // Set the flag results bool carry = (binaryCarry | (~result & resultBase10)).MSB(); cpu->SetX(carry); cpu->SetZ(cpu->GetZ() && resultBase10.Zero()); cpu->SetC(carry); cpu->SetN(resultBase10.Negative()); cpu->SetV(result.MSB() && !resultBase10.MSB()); // Adjust the PC and return the execution time cpu->SetPC(location + GetInstructionSize()); return GetExecuteCycleCount(additionalTime); } virtual void GetLabelTargetLocations(std::set& labelTargetLocations) const { _target.AddLabelTargetsToSet(labelTargetLocations); } private: EffectiveAddress _target; }; } // Close namespace M68000 #endif