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

am29f.h (1697B)


      1 #pragma once
      2 
      3 #include <cstdint>
      4 #include <cstddef>
      5 #include <vector>
      6 
      7 namespace hwLib
      8 {
      9 	class Am29f
     10 	{
     11 	public:
     12 		struct BusCycle
     13 		{
     14 			uint16_t addr;
     15 			uint8_t data;
     16 		};
     17 
     18 		struct Command
     19 		{
     20 			std::vector<BusCycle> cycles;
     21 		};
     22 
     23 		enum class CommandType
     24 		{
     25 			Invalid = -1,
     26 			ChipErase,
     27 			SectorErase,
     28 			Program,
     29 		};
     30 
     31 		explicit Am29f(uint8_t* _buffer, size_t _size, bool _useWriteEnable, bool _bitreversedCmdAddr);
     32 		virtual ~Am29f() = default;
     33 
     34 		void writeEnable(bool _writeEnable)
     35 		{
     36 			m_writeEnable = _writeEnable;
     37 		}
     38 
     39 		void write(uint32_t _addr, uint16_t _data);
     40 
     41 		bool eraseSector(uint32_t _addr, size_t _sizeInKb) const;
     42 
     43 		virtual bool eraseSector(const uint32_t _addr) const
     44 		{
     45 			return eraseSector4MbitTopBoot(_addr);
     46 		}
     47 
     48 		bool eraseSector1Mbit(uint32_t _addr) const;
     49 		bool eraseSector2MbitTopBoot(uint32_t _addr) const;
     50 		bool eraseSector4MbitTopBoot(uint32_t _addr) const;
     51 
     52 	private:
     53 		bool writeEnabled() const
     54 		{
     55 			return !m_useWriteEnable || m_writeEnable;
     56 		}
     57 
     58 		static constexpr uint16_t bitreverse(uint16_t _x)
     59 		{
     60 			_x = ((_x & 0xaaaau) >> 1) | static_cast<uint16_t>((_x & 0x5555u) << 1);
     61 			_x = ((_x & 0xccccu) >> 2) | static_cast<uint16_t>((_x & 0x3333u) << 2);
     62 			_x = ((_x & 0xf0f0u) >> 4) | static_cast<uint16_t>((_x & 0x0f0fu) << 4);
     63 
     64 			return ((_x & 0xff00) >> 8) | static_cast<uint16_t>((_x & 0x00ff) << 8);
     65 		}
     66 
     67 		void execCommand(CommandType _command, uint32_t _addr, uint16_t _data) const;
     68 
     69 		uint8_t* m_buffer;
     70 		const size_t m_size;
     71 		const bool m_useWriteEnable;
     72 		const bool m_bitreverseCmdAddr;
     73 
     74 		std::vector<Command> m_commands;
     75 		bool m_writeEnable = false;
     76 		uint32_t m_currentBusCycle = 0;
     77 		int32_t m_currentCommand = -1;
     78 	};
     79 }