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

i2cFlash.h (1372B)


      1 #pragma once
      2 
      3 #include <array>
      4 #include <optional>
      5 #include <string>
      6 #include <vector>
      7 
      8 #include "i2c.h"
      9 
     10 namespace hwLib
     11 {
     12 	// M24512 64kx 8 i2c flash chip emulation
     13 	class I2cFlash : public I2c
     14 	{
     15 	public:
     16 		static constexpr uint32_t Size = 0x10000;
     17 		using Data = std::array<uint8_t, Size>;
     18 
     19 		I2cFlash()
     20 		{
     21 			m_data.fill(0xff);
     22 		}
     23 
     24 		explicit I2cFlash(const Data& _data) : m_data(_data)
     25 		{
     26 		}
     27 
     28 		void saveAs(const std::string& _filename) const;
     29 
     30 		bool setData(std::vector<uint8_t>& _data);
     31 
     32 		const auto& getAddress() const { return m_address; }
     33 
     34 	protected:
     35 		void onStartCondition() override;
     36 		void onStopCondition() override;
     37 		void onByteWritten() override;
     38 		std::optional<bool> onAck() override;
     39 		uint8_t onReadByte() override;
     40 
     41 	private:
     42 		enum class State
     43 		{
     44 			ReadDeviceSelect,	AckDeviceSelect,
     45 			ReadAddressMSB,		AckAddressMSB,
     46 			ReadAddressLSB,		AckAddressLSB,
     47 			ReadWriteData,
     48 		};
     49 
     50 		enum DeviceSelectMask
     51 		{
     52 			Area       = 0b1111'000'0,
     53 			ChipEnable = 0b0000'111'0,
     54 			Rw         = 0b0000'000'1,
     55 		};
     56 
     57 		enum DeviceSelectValues
     58 		{
     59 			AreaMemory = 0b1010'000'0,
     60 			AreaId     = 0b1011'000'0,
     61 			Read       = 0b0000'000'1,
     62 			Write      = 0b0000'000'0,
     63 		};
     64 
     65 		void writeByte(uint8_t _byte);
     66 		void advanceAddress();
     67 
     68 		State m_state = State::ReadDeviceSelect;
     69 		uint8_t m_deviceSelect = 0;
     70 		uint32_t m_address = 0;
     71 
     72 		Data m_data;
     73 	};
     74 }