#include "model/hardware/device/SonoDevice.h"

#include <QDebug>
#include <QFile>

SonoDevice::SonoDevice()
{
}

SonoDevice::~SonoDevice()
{
    for(auto i = 0; i < SW_BUFFER_NUM; i++)
    {
        delete[] _buffers[i];
    }
}

void SonoDevice::init()
{
    device.init();

    for(auto i = 0; i < SW_BUFFER_NUM; i++)
	{
        _buffers[i] = new char[BUFFER_SIZE];
		memset(_buffers[i], 0, BUFFER_SIZE);
	}
}

void SonoDevice::startDma()
{
    device.writeWord(0, 0x8,1);
}

void SonoDevice::stopDma()
{
    device.writeWord(0, 0x8,0);
}

int SonoDevice::getCounter()
{
    auto temp = device.readWord(BAR, COUNTER_REG);

	return temp & COUNTER_MASK;
}

void SonoDevice::copy(int srcIndex, int dstIndex)
{
    auto src = device.getBufferPtr(srcIndex);
    auto dst = _buffers[dstIndex];
    memcpy(dst, src, BUFFER_SIZE);
}

//void SonoDevice::show(int i)
//{
//	auto tmp = _buffers[i];
//    for(auto j = 0; j < 80; j += 8)
//    {

//        quint32 res = 0;
//        quint32 res2 = 0;
//        res = tmp[j] | (tmp[j + 1] << 8) | (tmp[j + 2] << 16) | (tmp[j + 3] << 24);
//        res2 = (tmp[j + 4] << 32) | (tmp[j + 5] << 40) | (tmp[j + 6] << 48) | (tmp[j + 7] << 56);
//        qDebug() << QString("%1 %2").arg(QString::number(res2,
//                                                         10).rightJustified(
//                                             6,
//                                             '0')).arg(QString::number(res, 10));
//    }
//}

const char* SonoDevice::getBufferPtr(int index)
{
    return _buffers[index];
}