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

xtRomWaves.cpp (2004B)


      1 #include "xtRomWaves.h"
      2 
      3 #include <cstdint>
      4 
      5 #include "xtState.h"
      6 
      7 namespace xt
      8 {
      9 	constexpr uint32_t g_rwRomWaveStartAddr = 0x26501;
     10 
     11 	namespace
     12 	{
     13 		bool isValidRwRomWave(const WaveId _id)
     14 		{
     15 			return _id.rawId() >= wave::g_firstRwRomWaveIndex && _id.rawId() < wave::g_romWaveCount;
     16 		}
     17 		uint32_t getWaveAddr(const WaveId _id)
     18 		{
     19 			return g_rwRomWaveStartAddr + (_id.rawId() - wave::g_firstRwRomWaveIndex) * 128;
     20 		}
     21 	}
     22 
     23 	bool RomWaves::receiveSysEx(std::vector<SysEx>& _results, const SysEx& _data) const
     24 	{
     25 		switch (State::getCommand(_data))
     26 		{
     27 		case SysexCommand::WaveRequest:
     28 			{
     29 				auto waveId = State::getWaveId(_data);
     30 				if (!isValidRwRomWave(waveId))
     31 					return false;
     32 				WaveData waveData;
     33 				if (!readFromRom(waveData, waveId))
     34 					return false;
     35 				auto response = State::createWaveData(waveData, waveId.rawId(), false);
     36 				_results.emplace_back(std::move(response));
     37 			}
     38 			return true;
     39 		case SysexCommand::WaveDump:
     40 			{
     41 				auto waveId = State::getWaveId(_data);
     42 				if (!isValidRwRomWave(waveId))
     43 					return false;
     44 
     45 				WaveData d;
     46 				if (!State::parseWaveData(d, _data))
     47 					return false;
     48 
     49 				writeToRom(waveId, d);
     50 			}
     51 			return true;
     52 		default:
     53 			return false;
     54 		}
     55 	}
     56 
     57 	bool RomWaves::writeToRom(const WaveId _id, const WaveData& _waveData) const
     58 	{
     59 		if (!isValidRwRomWave(_id))
     60 			return false;
     61 
     62 		const auto addr = getWaveAddr(_id);
     63 
     64 		for (size_t i=0; i<64; ++i)
     65 		{
     66 			const auto idx = i << 1;
     67 			const auto sample = static_cast<uint8_t>(_waveData[i] ^ 0x80);
     68 
     69 			m_uc.getRomRuntimeData()[addr + idx] = sample;
     70 		}
     71 
     72 		return true;
     73 	}
     74 
     75 	bool RomWaves::readFromRom(WaveData& _waveData, const WaveId _id) const
     76 	{
     77 		if (!isValidRwRomWave(_id))
     78 			return false;
     79 
     80 		const auto addr = getWaveAddr(_id);
     81 		for (size_t i = 0; i < 64; ++i)
     82 		{
     83 			const auto idx = i << 1;
     84 			const auto sample = m_uc.getRomRuntimeData()[addr + idx] ^ 0x80;
     85 			_waveData[i] = static_cast<int8_t>(sample);
     86 			_waveData[127-i] = static_cast<int8_t>(-sample);
     87 		}
     88 		return true;
     89 	}
     90 }