gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

dspMemoryPatch.cpp (1987B)


      1 #include "dspMemoryPatch.h"
      2 
      3 #include "dsp56kEmu/dsp.h"
      4 
      5 namespace synthLib
      6 {
      7 	std::string DspMemoryPatch::toString() const
      8 	{
      9 		std::stringstream ss;
     10 		if(area >= dsp56k::MemArea_COUNT)
     11 			ss << '?';
     12 		else
     13 			ss << dsp56k::g_memAreaNames[area];
     14 
     15 		ss << ':' << HEX(address) << '=' << HEX(newValue);
     16 
     17 		if(newValue != expectedOldValue)
     18 			ss << ", expected old = " << HEX(expectedOldValue);
     19 		return ss.str();
     20 	}
     21 
     22 	bool DspMemoryPatches::apply(dsp56k::DSP& _dsp, const std::initializer_list<DspMemoryPatch>& _patches)
     23 	{
     24 		bool res = true;
     25 
     26 		for (const auto& patch : _patches)
     27 			res &= apply(_dsp, patch);
     28 
     29 		return res;
     30 	}
     31 
     32 	bool DspMemoryPatches::apply(dsp56k::DSP& _dsp, const DspMemoryPatch& _patch)
     33 	{
     34 		auto& mem = _dsp.memory();
     35 
     36 		if(_patch.area == dsp56k::MemArea_COUNT)
     37 		{
     38 			LOG("Failed to apply patch, memory area is not valid, for patch " << _patch.toString());
     39 			return false;
     40 		}
     41 
     42 		if(_patch.address >= mem.size(_patch.area))
     43 		{
     44 			LOG("Failed to apply patch, address " << HEX(_patch.address) << " is out of range, area " << dsp56k::g_memAreaNames[_patch.area] << " size is " << HEX(mem.size(_patch.area)) << ", for patch " << _patch.toString());
     45 			return false;
     46 		}
     47 
     48 		if(_patch.expectedOldValue != _patch.newValue)
     49 		{
     50 			const auto v = mem.get(_patch.area, _patch.address);
     51 			if(v != _patch.expectedOldValue)
     52 			{
     53 				LOG("Failed to apply patch, expected current value to be " << HEX(_patch.expectedOldValue) << " but current value is " << HEX(v) << ", for patch " << _patch.toString());
     54 				return false;
     55 			}
     56 		}
     57 
     58 		if(_patch.area == dsp56k::MemArea_P)
     59 			_dsp.memWriteP(_patch.address, _patch.newValue);
     60 		else
     61 			_dsp.memWrite(_patch.area, _patch.address, _patch.newValue);
     62 
     63 		LOG("Successfully applied patch " << _patch.toString());
     64 
     65 		return true;
     66 	}
     67 
     68 	bool DspMemoryPatches::apply(dsp56k::DSP& _dsp, const baseLib::MD5& _md5) const
     69 	{
     70 		for (const auto& e : allowedTargets)
     71 		{
     72 			if(e == _md5)
     73 				return apply(_dsp, patches);
     74 		}
     75 		return false;
     76 	}
     77 }