カテゴリー
Books

相対性理論

電気力学の部

 運動学の部と同じように解るところまで数式を勉強します。

$$
\frac{1}{c^2}\frac{\partial E_x}{\partial t}=\frac{\partial B_z}{\partial y}-\frac{\partial B_y}{\partial z},
$$

$$
\frac{\partial B_x}{\partial t}=-\left(\frac{\partial E_z}{\partial y}-\frac{\partial E_y}{\partial z}\right),
$$

$$
\frac{1}{c^2}\frac{\partial E_y}{\partial t}=\frac{\partial B_x}{\partial z}-\frac{\partial B_z}{\partial x},
$$

$$
\frac{\partial B_y}{\partial t}=-\left(\frac{\partial E_x}{\partial z}-\frac{\partial E_z}{\partial x}\right),
$$

$$
\frac{1}{c^2}\frac{\partial E_z}{\partial t}=\frac{\partial B_y}{\partial x}-\frac{\partial B_x}{\partial y},
$$

$$
\frac{\partial B_z}{\partial t}=-\left(\frac{\partial E_y}{\partial x}-\frac{\partial E_x}{\partial y}\right),
$$

$$\left.\begin{array}{c}
\frac{1}{c^2}\frac{\partial E_x}{\partial \tau}=\frac{\partial}{\partial\eta}\left\{\beta\left(E_z-\frac{v}{c^2}E_y\right)\right\}-\frac{\partial}{\partial\zeta}\left\{\beta\left(B_y+\frac{v}{c^2}E_z\right)\right\},
\\
\frac{1}{c^2}\frac{\partial}{\partial \tau}\{\beta(E_y-vB_z)\}=\frac{\partial B_x}{\partial\zeta}-\frac{\partial}{\partial\xi}\left\{\beta\left(B_z-\frac{v}{c^2}E_y\right)\right\},
\\
\frac{1}{c^2}\frac{\partial}{\partial \tau}\{\beta(E_z+vB_y)\}=\frac{\partial}{\partial\xi}\left\{\beta\left(B_y+\frac{v}{c^2}E_z\right)\right\}-\frac{\partial}{\partial\eta}B_x,
\end{array}\right\}
$$

$$
\left.\begin{array}{c}
\frac{\partial B_x}{\partial\tau}=-\frac{\partial}{\partial\eta}\{\beta(E_z+vB_y)\}+\frac{\partial}{\partial\zeta}\{\beta(E_y-vB_z)\},
\\
\frac{\partial}{\partial\tau}\left\{\beta\left(B_y+\frac{v}{c^2}E_z\right)\right\}=-\frac{\partial}{\partial\zeta}E_x+\frac{\partial}{\partial\xi}\{\beta(E_z+vB_y)\},
\\
\frac{\partial}{\partial\tau}\left\{\beta\left(B_z-\frac{v}{c^2}E_y\right)\right\}=-\frac{\partial}{\partial\xi}\{\beta(E_y-vB_z)\}+\frac{\partial}{\partial\eta}E_x.
\end{array}\right\}
$$

$$\beta=\frac{1}{\sqrt{1-(v/c)^2}}$$

$$
\frac{1}{c^2}\frac{\partial E’_{\xi}}{\partial\tau}=\frac{\partial B’_{\zeta}}{\partial\eta}-\frac{\partial B’_{\eta}}{\partial\zeta},
$$

$$
\frac{\partial B’_{\xi}}{\partial\tau}=-\left(\frac{\partial E’_{\zeta}}{\partial\eta}-\frac{\partial E’_{\eta}}{\partial\zeta}\right),
$$

$$
\frac{1}{c^2}\frac{\partial E’_{\eta}}{\partial\tau}=\frac{\partial B’_{\xi}}{\partial\zeta}-\frac{\partial B’_{\zeta}}{\partial\xi},
$$

$$
\frac{\partial B’_{\eta}}{\partial\tau}=-\left(\frac{\partial E’_{\xi}}{\partial\zeta}-\frac{\partial E’_{\zeta}}{\partial\xi}\right),
$$

$$
\frac{1}{c^2}\frac{\partial E’_{\zeta}}{\partial\tau}=\frac{\partial B’_{\eta}}{\partial\xi}-\frac{\partial B’_{\xi}}{\partial\eta},
$$

$$
\frac{\partial B’_{\zeta}}{\partial\tau}=-\left(\frac{\partial E’_{\eta}}{\partial\xi}-\frac{\partial E’_{\xi}}{\partial\eta}\right).
$$

$$
E’_{\xi}=\psi(v)E_x,
\space
B’_{\xi}=\psi(v)B_x,
$$

$$
E’_{\eta}=\psi(v)\beta(E_y-vB_z),
\space
B’_{\eta}=\psi(v)\beta\{B_y+(v/c^2)E_z\},
$$

$$
E’_{\zeta}=\psi(v)\beta(E_z+vB_y),
\space
B’_{\zeta}=\psi(v)\beta\{B_z-(v/c^2)E_y\}.
$$

$$\psi(v)\cdot\psi(-v)=1.$$

$$\psi(v)=\psi(-v).$$

$$\psi(v)=1$$

$$E’_{\xi}=E_x,\space B’_{\xi}=B_x,$$

$$
E’_{\eta}=\beta(E_y-vB_z),\space B’_{\eta}=\beta\left(B_y+\frac{v}{c^2}E_z\right),
$$

$$
E’_{\zeta}=\beta(E_z-vB_y),\space B’_{\zeta}=\beta\left(B_z+\frac{v}{c^2}E_y\right).
$$

$$
E_x=E_{x0}\space sin\space\varPhi,\space B_x=B_{x0}\space sin\space\varPhi,
$$

$$
E_y=E_{y0}\space sin\space\varPhi,\space B_y=B_{y0}\space sin\space\varPhi,
$$

$$
E_z=E_{z0}\space sin\space\varPhi,\space B_z=B_{z0}\space sin\space\varPhi,
$$

$$
\varPhi=\omega\{t-(lx+my+nz)/c\}.
$$

$$
E’_{\xi}=E_{x0}\space sin\space\varPhi’,\space B’_{\xi}=B_{x0}\space sin\space\varPhi’,
$$

$$
E’_{\eta}=\beta(E_{y0}-vB_{z0})\space sin\space\varPhi’,\space B’_{\eta}=\beta\left(B_{y0}+\frac{v}{c^2}E_{z0}\right)\space sin\space\varPhi’,
$$

$$
E’_{\zeta}=\beta(E_{z0}-vB_{y0})\space sin\space\varPhi’,\space B’_{\zeta}=\beta\left(B_{z0}+\frac{v}{c^2}E_{y0}\right)\space sin\space\varPhi’,
$$

$$\varPhi’=\omega’\{\tau-(l’\xi+m’\eta+n’\zeta)/c\}.$$

$$\omega’=\omega\beta\left(1-l\frac{v}{c}\right),$$

$$l’=\left.\left(l-\frac{v}{c}\right)\middle/\left(1-l\frac{v}{c}\right)\right.,$$

$$m’=\left. m\middle/\beta\left(1-l\frac{v}{c}\right)\right.,$$

$$n’=\left. n\middle/\beta\left(1-l\frac{v}{c}\right)\right. .$$

$$\nu’=\left.\nu\left(1-\frac{v}{c}cos\space\varphi\right)\middle/\sqrt{1-\left(\frac{v}{c}\right)^2}\right.$$

$$\nu’=\nu\sqrt{\left.\left(1-\frac{v}{c}\right)\middle/\left(1+\frac{v}{c}\right)\right.}.$$

$$cos\space\varphi’=\left.\left(cos\space\varphi-\frac{v}{c}\right)\middle/\left(1-\frac{v}{c}cos\space\varphi\right)\right..$$

$$cos\space\varphi’=-\frac{v}{c}.$$

$$
(A’)^2=A^2\left.\left(1-\frac{v}{c}cos\space\varphi\right)^2\middle/\left\{1-\left(\frac{v}{c}\right)^2\right\}\right..
$$

$$\varphi=0$$

$$
(A’)^2=A^2\left.\left(1-\frac{v}{c}\right)\middle/\left(1+\frac{v}{c}\right)\right..
$$

 この式のところで「観測者が速さcで光源に向かって走ればこの人から見たとき光源は無限に強い明るさに輝いて見えることになる」とあったのですが直ぐに理解できなかったので補足しておきます。

 光源に向かっているのでvはマイナスの値になります。vがcに近づくほど分母は1+v/cなので0に近づきます。分子は1-v/cなので2に近づきます。従って限り無く2に近い値を限り無く0に近い値で割るので無限に大きい振幅になるということです。

$$w=(\vec E\cdot\vec D+\vec H\cdot\vec B)/2$$

$$(x-lct)^2+(y-mct)^2+(z-nct)^2=R^2$$

$$\left(\beta\xi-l\frac{v}{c}\beta\xi\right)^2+\left(\eta-m\frac{v}{c}\beta\xi\right)^2+\left(\zeta-n\frac{v}{c}\beta\xi\right)^2=R^2$$

$$
\frac{S’}{S}=\left.\sqrt{1-\left(\frac{v}{c}\right)^2}\middle/\left(1-\frac{v}{c}cos\space\varphi\right)\right.
$$

$$
\frac{E’}{E}=\frac{w’S’}{wS}=\frac{(A’)^2S’}{(A)^2S}=\frac{1-(v/c)cos\space\varphi}{\sqrt{1-(v/c)^2}}
$$

$$\varphi=0$$

$$\frac{E’}{E}=\sqrt{\frac{1-(v/c)}{1+(v/c)}}.$$

$$
A’=A\{1-(v/c)cos\space\varphi\}/\sqrt{1-(v/c)^2},
$$

$$
cos\space\varphi’=\{cos\space\varphi-(v/c)\}/\{1-(v/c)cos\space\varphi\},
$$

$$
\nu’=\nu\cdot\{1-(v/c)cos\space\varphi\}/\sqrt{1-(v/c)^2}
$$

$$
A^{\prime\prime}=A’,\space cos\space\varphi^{\prime\prime}=-cos\space\varphi’,\space\nu^{\prime\prime}=\nu’
$$

$$
A^{\prime\prime\prime}=A^{\prime\prime}\frac{1+(v/c)cos\space\varphi^{\prime\prime}}{\sqrt{1-(v/c)^2}}=A\frac{1-2(v/c)cos\space\varphi+(v/c)^2}{1-(v/c)^2},
$$

$$
cos\space\varphi^{\prime\prime\prime}=\frac{cos\space\varphi^{\prime\prime}+(v/c)}{1+(v/c)cos\space\varphi^{\prime\prime}}=-\frac{\{1+(v/c)^2\}cos\space\varphi-2(v/c)}{1-2(v/c)cos\space\varphi+(v/c)^2},
$$

$$
\nu^{\prime\prime\prime}=\nu^{\prime\prime}\frac{1+(v/c)cos\space\varphi^{\prime\prime}}{\sqrt{1-(v/c)^2}}=\nu\frac{1-2(v/c)cos\space\varphi+(v/c)^2}{\{1-(v/c)\}^2},
$$

$$
P=2w\frac{\{cos\space\varphi-(v/c)\}^2}{1-(v/c)^2}.
$$

$$P=2w\space cos^2\varphi$$

 ようやく「力」が出てきました。

$$
\frac{\partial D_x}{\partial t}+\rho u_x=\frac{\partial H_z}{\partial y}-\frac{\partial H_y}{\partial z},\space\frac{\partial B_x}{\partial t}=-\frac{\partial E_z}{\partial y}+\frac{\partial E_y}{\partial z},
$$

$$
\frac{\partial D_y}{\partial t}+\rho u_y=\frac{\partial H_x}{\partial z}-\frac{\partial H_z}{\partial x},\space\frac{\partial B_y}{\partial t}=-\frac{\partial E_x}{\partial z}+\frac{\partial E_z}{\partial x},
$$

$$
\frac{\partial D_z}{\partial t}+\rho u_z=\frac{\partial H_y}{\partial x}-\frac{\partial H_x}{\partial y},\space\frac{\partial B_z}{\partial t}=-\frac{\partial E_y}{\partial x}+\frac{\partial E_x}{\partial y}.
$$

$$
\rho=\frac{\partial D_x}{\partial x}+\frac{\partial D_y}{\partial y}+\frac{\partial D_z}{\partial z}
$$

$$
\frac{\partial D’_\xi}{\partial\tau}+\rho’ u_\xi=\frac{\partial H’_\zeta}{\partial \eta}-\frac{\partial H’_\eta}{\partial \zeta},\space\frac{\partial B’_\xi}{\partial\tau}=-\frac{\partial E’_\zeta}{\partial \eta}+\frac{\partial E’_\eta}{\partial \zeta},
$$

$$
\frac{\partial D’_\eta}{\partial\tau}+\rho’ u_\eta=\frac{\partial H’_\xi}{\partial \zeta}-\frac{\partial H’_\zeta}{\partial \xi},\space\frac{\partial B’_\eta}{\partial\tau}=-\frac{\partial E’_\xi}{\partial \zeta}+\frac{\partial E’_\zeta}{\partial \xi},
$$

$$
\frac{\partial D’_\zeta}{\partial\tau}+\rho’ u_\zeta=\frac{\partial H’_\eta}{\partial \xi}-\frac{\partial H’_\xi}{\partial \eta},\space\frac{\partial B’_\zeta}{\partial\tau}=-\frac{\partial E’_\eta}{\partial \xi}+\frac{\partial E’_\xi}{\partial \eta}.
$$

$$u_\xi=\frac{u_x-v}{1-(u_xv/c^2)},\space u_\eta=\frac{u_y}{\beta\{1-(u_xv/c^2)\}},\space u_\zeta=\frac{u_z}{\beta\{1-(u_xv/c^2)\}},$$

$$\rho’=\frac{\partial D’_\xi}{\partial\xi}+\frac{\partial D’_\eta}{\partial\eta}+\frac{\partial D’_\zeta}{\partial\zeta}=\beta\left(1-\frac{u_xv}{c^2}\right)\rho.$$

$$
\mu\frac{d^2x}{dt^2}=\varepsilon E_x,
\space
\mu\frac{d^2y}{dt^2}=\varepsilon E_y,
\space
\mu\frac{d^2z}{dt^2}=\varepsilon E_z
$$

ここの説明に \(\mu\) は「その質量を表す」とあります。「力」に続き「質量」も登場してきました。

$$t=x=y=z=0$$

$$\tau=\xi=\eta=\zeta=0$$

$$\tau=\beta\{t-(v/c^2)x\},$$

$$\xi=\beta(x-vy),\space E’_\xi=E_x,$$

$$\eta=y,\space E’_\eta=\beta(E_y-vB_z),$$

$$\zeta=z,\space E’_\zeta=\beta(E_z+vB_y).$$

$$\left.\begin{array}{c}
\frac{d^2x}{dt^2}=\frac{\varepsilon}{\mu}\frac{1}{\beta^3}E_x,
\\
\frac{d^2y}{dt^2}=\frac{\varepsilon}{\mu}\frac{1}{\beta}(E_y-vB_z),
\\
\frac{d^2z}{dt^2}=\frac{\varepsilon}{\mu}\frac{1}{\beta}(E_z+vB_y).
\end{array}\right\}
$$

$$\mu\beta^3\frac{d^2x}{dt^2}=\varepsilon E_x =\varepsilon E’\xi,$$

$$\mu\beta^2\frac{d^2y}{dt^2}=\varepsilon\beta(E_y-vB_z) =\varepsilon E’\eta,$$

$$\mu\beta^2\frac{d^2z}{dt^2}=\varepsilon\beta(E_z+vB_y) =\varepsilon E’\zeta,$$

$$mass \times acceleration = power$$

$$longitudinal\space mass=\left.\mu\middle/\left\{\sqrt{1-\left(\frac{v}{c}\right)^2}\right\}^3\right.,$$

$$transverse\space mass=\left.\mu\middle/\left\{1-\left(\frac{v}{c}\right)^2\right\}\right.$$

$$W=\int\varepsilon E_xdx=\mu\int_0^v\beta^3vdv=\mu c^2\left\{\frac{1}{\sqrt{1-(v/c)^2}}-1\right\}.$$

$$A_m/A_e=v/c$$

$$P=\int E_xdx=\frac{\mu}{\varepsilon}c^2\left\{\frac{1}{\sqrt{1-(v/c)^2}}-1\right\}.$$

$$-\frac{d^2y}{dt^2}=\frac{v^2}{R}=\frac{\varepsilon}{\mu}vB\sqrt{1-(v/c)^2},$$

$$R=\mu v/\varepsilon B\sqrt{1-(v/c)^2}.$$

カテゴリー
writer

自作ROMライター その6

 パソコンから3バイト単位(アドレス16ビット、データ8ビット)で信号を送ることができるようになりました。クロック43Hz、アドレス+データ24ビット、リセット信号をキーボドから入力した値に応じて送りLEDを点灯しています。最後のビットを送った後に書き込み信号を送ることができればROMに書き込みができるはずです。

アドレス・データとして3バイト単位で送出
アドレス・データとして3バイト送る
アドレス・データとして3バイト送る
#include <iostream>
#include <windows.h>
#include <math.h>
#include <MMSystem.h>

#pragma comment (lib, "winmm.lib")

constexpr auto SAMPLING = 192000;
constexpr auto CHANNEL = 2;
constexpr auto BITSPERSAMPLE = 16;
constexpr auto MAXAMP = 32767;
constexpr auto DIV = 4;
constexpr auto BUFFERING = 10;

void createWave(LPWORD lpData, size_t frequency, size_t sampling, WORD amplitude) {
    double wavelength = (double)sampling / (double)frequency;
    double d = 360.0 / wavelength;
    double pi = 3.14159265359;
    for (int i = 0; i < wavelength; i++) {
        lpData[i] = (WORD)(amplitude * sin(d * i / 180.0 * pi));
    }
}

BOOLEAN isON(unsigned char data, size_t bit, size_t bitLength, size_t value) {
    int oneByte = 8;
    for (int i = 0; i < oneByte; i++) {
        unsigned char mask = 0x80 >> i;
        if (((data & mask) == mask) &&
            (bitLength *((bit-oneByte )+i)) <= (value % (bitLength * bit)) &&
            (value % (bitLength * bit)) < (bitLength * ((bit - oneByte + 1) + i))) {
            return true;
        }
    }
    return false;
}

DWORD soundOut(
    LPWORD *lpWaveData,
    HWAVEOUT hWaveOut,
    unsigned char data,
    size_t bit,
    size_t cFrequency,
    BOOL reset
) {
    LPWORD lpSinWave;
    LPWORD lpWave;
    LPWORD lpWave1;
    LPWORD lpWave2;
    LPWORD lpWave3;
    LPWORD lpWave4;
    size_t i, j, end;
    size_t cWavelength = SAMPLING / cFrequency;
    size_t dataLength = (size_t)CHANNEL * (BITSPERSAMPLE / 8) * cWavelength * bit;
    size_t dWavelength;
    size_t dFrequency;
    WORD amplitude = MAXAMP / DIV;

    lpWave = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave1 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave2 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave3 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave4 = (LPWORD)calloc(sizeof(WORD), dataLength);

    end = dataLength/(BITSPERSAMPLE / 8);

    for (i = 0; i < end; i++) {
        lpWave[i] = 0;
        lpWave1[i] = 0;
        lpWave2[i] = 0;
        lpWave3[i] = 0;
        lpWave4[i] = 0;
    }

    /*  チャンネル1 ********************************/
    lpSinWave = (LPWORD)calloc(sizeof(WORD), cWavelength);
    createWave(lpSinWave, cFrequency, SAMPLING, MAXAMP);
    for (i = 0, j = 0; i < end; i += CHANNEL) {
        lpWave[i] = lpSinWave[j];
        ++j;
        if (j >= cWavelength) { j = 0; }
    }
    free(lpSinWave);

    /*  チャンネル2 ********************************/

    // クロック
    dFrequency = 3200;
    dWavelength = SAMPLING / dFrequency;
    lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
    createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += CHANNEL) {
        lpWave1[i] = lpSinWave[j];
        ++j;
        if (j >= dWavelength) { j = 0; }
    }
    for (i = 1, j = 0; i < end; i += CHANNEL, ++j) {
        if (j < cWavelength/2) { lpWave1[i] = 0; }
        if (j >= cWavelength) { j = 0; }
    }
    free(lpSinWave);

    // 制御データ
    dFrequency = 8000;
    dWavelength = SAMPLING / dFrequency;
    lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
    createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += CHANNEL) {
        lpWave2[i] = lpSinWave[j];
        ++j;
        if (j >= dWavelength) { j = 0; }
    }
    for (i = 1, j = 0; i < end; i += CHANNEL, ++j) {
        if (j < cWavelength/1.2) { lpWave2[i] = 0; }
        if (j >= cWavelength) { j = 0; }
    }
    free(lpSinWave);

    // シリアルデータ2
    dFrequency = 11300;
    dWavelength = SAMPLING / dFrequency;
    lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
    createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += CHANNEL, ++j) {
        lpWave3[i] = lpSinWave[j];
        if (j >= dWavelength) { j = 0; }
    }
    for (i = 1; i < end; i += CHANNEL) {
        if (isON(data, bit, cWavelength * CHANNEL, i)) {
            lpWave3[i] = 0;
        }
    }
    free(lpSinWave);

    // リセット
    if (reset) {
        dFrequency = 17000;
        dWavelength = SAMPLING / dFrequency;
        lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
        createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
        for (i = 1, j = 0; i < cWavelength; i += CHANNEL, ++j) {
            lpWave4[i] = lpSinWave[j];
            if (j >= dWavelength) { j = 0; }
        }
        free(lpSinWave);
    }

    // チャンネル2に合成
    for (i = 1; i < end; i += CHANNEL) {
        lpWave[i] = lpWave1[i] + lpWave2[i] + lpWave3[i] + lpWave4[i];
    }

    *lpWaveData = lpWave;
    return (DWORD)dataLength;
}

int main() {

    LPWORD lpWave[BUFFERING];
    WAVEHDR whdr[BUFFERING];
    WAVEFORMATEX wfe;
    HWAVEOUT hWaveOut;

    wfe.wFormatTag = WAVE_FORMAT_PCM;
    wfe.nChannels = CHANNEL;
    wfe.wBitsPerSample = BITSPERSAMPLE;
    wfe.nBlockAlign = CHANNEL * BITSPERSAMPLE / 8;
    wfe.nSamplesPerSec = SAMPLING;
    wfe.nAvgBytesPerSec = wfe.nSamplesPerSec * wfe.nBlockAlign;
    waveOutOpen(&hWaveOut, 0, &wfe, 0, 0, CALLBACK_NULL);

    for (int i = 0; i < BUFFERING; i++) {
        whdr[i].dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
        whdr[i].dwLoops = 1;
        lpWave[i] = NULL;
    }

    std::string str;
    do {
        std::cout << "2桁毎の16進コードを入力しEnterを押して下さい。(00 ~ FF)" << std::endl;
        std::cout << "続けて入力する場合は間を空けないでください。" << std::endl;
        std::cout << "Ctrl+Zを入力しEnterで終了します。" << std::endl;
        std::cin >> str;

        // Ctrl+Zで終了
        if (std::cin.eof()) { break; }

        char strData[3];
        strData[2] = 0x00;
        size_t bit = 9;
        size_t frequency = 43;
        int freeBuffer = 0;
        for (int i = 0, j = 0, k = 0; i < str.size(); i += 2, j++, k++) {

            if (j >= BUFFERING) {
                j = 0;
            }

            freeBuffer = j + 1;
            if (freeBuffer >= BUFFERING) {
                freeBuffer = 0;
            }
            if (lpWave[freeBuffer] != NULL) {
                std::cout << "free=" << std::dec << freeBuffer << std::endl;
                free(lpWave[freeBuffer]);
                lpWave[freeBuffer] = NULL;
            }

            strData[0] = str[i];
            strData[1] = str[(size_t)i + 1];
            // 入力された文字列をデータに変換
            unsigned char data = (unsigned char)strtol(strData, NULL, 16);

            // サウンド出力
            if (i % 3 == 0) {
                whdr[j].dwBufferLength = soundOut(&lpWave[j], hWaveOut, data, bit, frequency, true);
            }
            else {
                whdr[j].dwBufferLength = soundOut(&lpWave[j], hWaveOut, data, bit-1, frequency, false);
            }
            whdr[j].lpData = (LPSTR)lpWave[j];
            waveOutPrepareHeader(hWaveOut, &whdr[j], sizeof(WAVEHDR));
            waveOutWrite(hWaveOut, &whdr[j], sizeof(WAVEHDR));

            std::cout << "no=" << std::dec << j << ", data=" << std::hex << (int)data << "H" << std::endl;

            if (k > BUFFERING-3) {
                Sleep((1000 * (DWORD)bit) / (DWORD)frequency);
            }
        }
        int num = (int)str.size() / 2;
        if (num == 0) {
            num = 1;
        }
        else if (num > BUFFERING) {
            num = BUFFERING;
        }
        Sleep((1000 * (DWORD)bit * (num +1)) / (DWORD)frequency);
        for (int i = 0; i < num; i++) {
            if (lpWave[i] != NULL) {
                std::cout << "free=" << std::dec << i << std::endl;
                free(lpWave[i]);
                lpWave[i] = NULL;
            }
        }
    } while (true);

    waveOutClose(hWaveOut);
}
カテゴリー
writer

自作ROMライター その5

 ソフトウェアを改良し複数バイトのデータを送り込めるようにしました。これでROMに書き込みたいデータとクロックや制御信号を混合してパソコンからサウンド出力し、受け側の装置で分離してロジックICを意図通りに動かすことができるようになりました。

複数バイトのデータを連続して74LS273に送る
複数バイトのデータを連続して74LS273に送る
#include <iostream>
#include <windows.h>
#include <math.h>
#include <MMSystem.h>

#pragma comment (lib, "winmm.lib")

constexpr auto SAMPLING = 192000;
constexpr auto CHANNEL = 2;
constexpr auto BITSPERSAMPLE = 16;
constexpr auto MAXAMP = 32767;
constexpr auto DIV = 4;
constexpr auto BUFFERING = 10;

void createWave(LPWORD lpData, size_t frequency, size_t sampling, WORD amplitude) {
    double wavelength = (double)sampling / (double)frequency;
    double d = 360.0 / wavelength;
    double pi = 3.14159265359;
    for (int i = 0; i < wavelength; i++) {
        lpData[i] = (WORD)(amplitude * sin(d * i / 180.0 * pi));
    }
}

BOOLEAN isON(unsigned char data, size_t bit, size_t bitLength, size_t value) {
    int oneByte = 8;
    for (int i = 0; i < oneByte; i++) {
        unsigned char mask = 0x80 >> i;
        if (((data & mask) == mask) &&
            (bitLength *((bit-oneByte )+i)) <= (value % (bitLength * bit)) &&
            (value % (bitLength * bit)) < (bitLength * ((bit - oneByte + 1) + i))) {
            return true;
        }
    }
    return false;
}

DWORD soundOut(LPWORD *lpWaveData, HWAVEOUT hWaveOut, unsigned char data, size_t bit, size_t cFrequency) {
    LPWORD lpSinWave;
    LPWORD lpWave;
    LPWORD lpWave1;
    LPWORD lpWave2;
    LPWORD lpWave3;
    LPWORD lpWave4;
    size_t i, j, end;
    size_t cWavelength = SAMPLING / cFrequency;
    size_t dataLength = (size_t)CHANNEL * (BITSPERSAMPLE / 8) * cWavelength * bit;
    size_t dWavelength;
    size_t dFrequency;
    WORD amplitude = MAXAMP / DIV;

    lpWave = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave1 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave2 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave3 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave4 = (LPWORD)calloc(sizeof(WORD), dataLength);

    end = dataLength/(BITSPERSAMPLE / 8);

    for (i = 0; i < end; i++) {
        lpWave[i] = 0;
        lpWave1[i] = 0;
        lpWave2[i] = 0;
        lpWave3[i] = 0;
        lpWave4[i] = 0;
    }

    /*  チャンネル1 ********************************/
    lpSinWave = (LPWORD)calloc(sizeof(WORD), cWavelength);
    createWave(lpSinWave, cFrequency, SAMPLING, MAXAMP);
    for (i = 0, j = 0; i < end; i += CHANNEL) {
        lpWave[i] = lpSinWave[j];
        ++j;
        if (j >= cWavelength) { j = 0; }
    }
    free(lpSinWave);

    /*  チャンネル2 ********************************/

    // クロック
    dFrequency = 3200;
    dWavelength = SAMPLING / dFrequency;
    lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
    createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += CHANNEL) {
        lpWave1[i] = lpSinWave[j];
        ++j;
        if (j >= dWavelength) { j = 0; }
    }
    for (i = 1, j = 0; i < end; i += CHANNEL, ++j) {
        if (j < cWavelength/2) { lpWave1[i] = 0; }
        if (j >= cWavelength) { j = 0; }
    }
    free(lpSinWave);

    // 制御データ
    dFrequency = 8000;
    dWavelength = SAMPLING / dFrequency;
    lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
    createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += CHANNEL) {
        lpWave2[i] = lpSinWave[j];
        ++j;
        if (j >= dWavelength) { j = 0; }
    }
    for (i = 1, j = 0; i < end; i += CHANNEL, ++j) {
        if (j < cWavelength/1.2) { lpWave2[i] = 0; }
        if (j >= cWavelength) { j = 0; }
    }
    free(lpSinWave);

    // シリアルデータ2
    dFrequency = 11300;
    dWavelength = SAMPLING / dFrequency;
    lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
    createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += CHANNEL, ++j) {
        lpWave3[i] = lpSinWave[j];
        if (j >= dWavelength) { j = 0; }
    }
    for (i = 1; i < end; i += CHANNEL) {
        if (isON(data, bit, cWavelength * CHANNEL, i)) {
            lpWave3[i] = 0;
        }
    }
    free(lpSinWave);

    // リセット
    dFrequency = 17000;
    dWavelength = SAMPLING / dFrequency;
    lpSinWave = (LPWORD)calloc(sizeof(WORD), dWavelength);
    createWave(lpSinWave, dFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < cWavelength; i += CHANNEL, ++j) {
        lpWave4[i] = lpSinWave[j];
        if (j >= dWavelength) { j = 0; }
    }
    free(lpSinWave);

    // チャンネル2に合成
    for (i = 1; i < end; i += CHANNEL) {
        lpWave[i] = lpWave1[i] + lpWave2[i] + lpWave3[i] + lpWave4[i];
    }

    *lpWaveData = lpWave;
    return (DWORD)dataLength;
}

int main() {

    LPWORD lpWave[BUFFERING];
    WAVEHDR whdr[BUFFERING];
    WAVEFORMATEX wfe;
    HWAVEOUT hWaveOut;

    wfe.wFormatTag = WAVE_FORMAT_PCM;
    wfe.nChannels = CHANNEL;
    wfe.wBitsPerSample = BITSPERSAMPLE;
    wfe.nBlockAlign = CHANNEL * BITSPERSAMPLE / 8;
    wfe.nSamplesPerSec = SAMPLING;
    wfe.nAvgBytesPerSec = wfe.nSamplesPerSec * wfe.nBlockAlign;
    waveOutOpen(&hWaveOut, 0, &wfe, 0, 0, CALLBACK_NULL);

    for (int i = 0; i < BUFFERING; i++) {
        whdr[i].dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
        whdr[i].dwLoops = 1;
        lpWave[i] = NULL;
    }

    std::string str;
    do {
        std::cout << "2桁毎の16進コードを入力しEnterを押して下さい。(00 ~ FF)" << std::endl;
        std::cout << "続けて入力する場合は間を空けないでください。" << std::endl;
        std::cout << "Ctrl+Zを入力しEnterで終了します。" << std::endl;
        std::cin >> str;

        // Ctrl+Zで終了
        if (std::cin.eof()) { break; }

        char strData[3];
        strData[2] = 0x00;
        size_t bit = 9;
        size_t frequency = 43;
        int freeBuffer = 0;
        for (int i = 0, j = 0, k = 0; i < str.size(); i += 2, j++, k++) {

            if (j >= BUFFERING) {
                j = 0;
            }

            freeBuffer = j + 1;
            if (freeBuffer >= BUFFERING) {
                freeBuffer = 0;
            }
            if (lpWave[freeBuffer] != NULL) {
                std::cout << "free=" << std::dec << freeBuffer << std::endl;
                free(lpWave[freeBuffer]);
                lpWave[freeBuffer] = NULL;
            }

            strData[0] = str[i];
            strData[1] = str[(size_t)i + 1];
            // 入力された文字列をデータに変換
            unsigned char data = (unsigned char)strtol(strData, NULL, 16);

            // サウンド出力
            whdr[j].dwBufferLength = soundOut(&lpWave[j], hWaveOut, data, bit, frequency);
            whdr[j].lpData = (LPSTR)lpWave[j];
            waveOutPrepareHeader(hWaveOut, &whdr[j], sizeof(WAVEHDR));
            waveOutWrite(hWaveOut, &whdr[j], sizeof(WAVEHDR));

            std::cout << "no=" << std::dec << j << ", data=" << std::hex << (int)data << "H" << std::endl;

            if (k > BUFFERING-3) {
                Sleep((1000 * (DWORD)bit) / (DWORD)frequency);
            }
        }
        int num = (int)str.size() / 2;
        if (num == 0) {
            num = 1;
        }
        else if (num > BUFFERING) {
            num = BUFFERING;
        }
        Sleep((1000 * (DWORD)bit * (num +1)) / (DWORD)frequency);
        for (int i = 0; i < num; i++) {
            if (lpWave[i] != NULL) {
                std::cout << "free=" << std::dec << i << std::endl;
                free(lpWave[i]);
                lpWave[i] = NULL;
            }
        }
    } while (true);

    waveOutClose(hWaveOut);
}
カテゴリー
Future tasks

空間で演算

が究極のナチュラルコンピューティングかもしれない。導体に拘らなければ新しい世界が拓けるだろう。

カテゴリー
writer

自作ROMライター その4

 74HC164Nでシリアルパラレル変換した後74LS273PCでその値を保持できるか実験してみました。同時にソフトウェアを改良しキーボードからバイト単位に信号を送出できるようにしました。

 キーボードで16進数を1バイト分入力しEnterを押すとその値に応じてLEDが点灯しています。

フリップフロップICにバイト単位データを送る
フリップフロップICにバイト単位データを送る
フリップフロップICにバイト単位データを送る
フリップフロップICにバイト単位データを送る
#include <iostream>
#include <windows.h>
#include <math.h>
#include <MMSystem.h>

#pragma comment (lib, "winmm.lib")

constexpr auto SAMPLING = 192000;
constexpr auto CHANNEL = 2;
constexpr auto BITSPERSAMPLE = 16;

void createWave(LPWORD lpData, size_t frequency, size_t sampling, WORD amplitude) {
    size_t wavelength = SAMPLING / frequency;
    double d = 360.0 / wavelength;
    double pi = 3.14159265359;
    for (int i = 0; i < wavelength; i++) {
        lpData[i] = (WORD)(amplitude * sin(d * (i % wavelength) / 180.0 * pi));
    }
}

BOOLEAN isON(unsigned char data, size_t bit, size_t i) {
    size_t bit9 = bit * 9;
    size_t bit8 = bit * 8;
    size_t bit7 = bit * 7;
    size_t bit6 = bit * 6;
    size_t bit5 = bit * 5;
    size_t bit4 = bit * 4;
    size_t bit3 = bit * 3;
    size_t bit2 = bit * 2;

    if (((data & 0x80) == 0x80) && 0 <= (i % (bit9)) && (i % (bit9)) < (bit)) {
        return true;
    }
    if (((data & 0x40) == 0x40) && (bit) <= (i % (bit9)) && (i % (bit9)) < (bit2)) {
        return true;
    }
    if (((data & 0x20) == 0x20) && (bit2) <= (i % (bit9)) && (i % (bit9)) < (bit3)) {
        return true;
    }
    if (((data & 0x10) == 0x10) && (bit3) <= (i % (bit9)) && (i % (bit9)) < (bit4)) {
        return true;
    }
    if (((data & 0x08) == 0x08) && (bit4) <= (i % (bit9)) && (i % (bit9)) < (bit5)) {
        return true;
    }
    if (((data & 0x04) == 0x04) && (bit5) <= (i % (bit9)) && (i % (bit9)) < (bit6)) {
        return true;
    }
    if (((data & 0x02) == 0x02) && (bit6) <= (i % (bit9)) && (i % (bit9)) < (bit7)) {
        return true;
    }
    if (((data & 0x01) == 0x01) && (bit7) <= (i % (bit9)) && (i % (bit9)) < (bit8)) {
        return true;
    }
    return false;
}

static LPWORD lpWave;
static WAVEHDR whdr;

void soundOut(HWAVEOUT hWaveOut, unsigned char data) {
    LPWORD lpData;
    LPWORD lpWave1;
    LPWORD lpWave2;
    LPWORD lpWave3;
    LPWORD lpWave4;

    // 最初と最後の1sは出力しないので3以上とする
    DWORD terms = 3;

    size_t i, j, k, start, end;
    size_t frequency = 9;
    size_t wavelength = SAMPLING / frequency;
    size_t dataLength = (size_t)CHANNEL * (size_t)SAMPLING * terms;

    lpWave = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave1 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave2 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave3 = (LPWORD)calloc(sizeof(WORD), dataLength);
    lpWave4 = (LPWORD)calloc(sizeof(WORD), dataLength);

    end = SAMPLING * CHANNEL;
    WORD amplitude = 32767 / 4;
    for (i = 0; i < end; i++) {
        lpWave[i] = 0;
        lpWave1[i] = 0;
        lpWave2[i] = 0;
        lpWave3[i] = 0;
        lpWave4[i] = 0;
    }

    size_t bit = CHANNEL * SAMPLING / frequency;

    // 最初の1sは出力しない
    start = SAMPLING * CHANNEL;
    end = (size_t)CHANNEL * (size_t)SAMPLING * (size_t)terms;
    // 最後の1sは出力しない
    end -= SAMPLING * CHANNEL;

    size_t valWavelength;
    size_t valFrequency;

    /*  チャンネル1 ********************************/
    lpData = (LPWORD)calloc(sizeof(WORD), wavelength);
    createWave(lpData, frequency, SAMPLING, amplitude);
    for (i = start, j = 0; i < end; i += 2) {
        lpWave[i] = lpData[j];
        ++j;
        if (j >= wavelength) { j = 0; }
    }
    free(lpData);

    /*  チャンネル2 ********************************/

    // クロック
    valFrequency = 3200;
    valWavelength = SAMPLING / valFrequency;
    lpData = (LPWORD)calloc(sizeof(WORD), valWavelength);
    createWave(lpData, valFrequency, SAMPLING, amplitude);
    for (i = start + 1, j = 0, k = 0; i < end; i += 2, ++j, ++k) {
        if (k < SAMPLING / frequency / 2) { lpWave1[i] = lpData[j]; }
        if (j >= valWavelength) { j = 0; }
        if (k >= SAMPLING / frequency) { k = 0; }
    }
    free(lpData);

    // シリアルデータ1
    valFrequency = 8000;
    valWavelength = SAMPLING / valFrequency;
    lpData = (LPWORD)calloc(sizeof(WORD), valWavelength);
    createWave(lpData, valFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += 2, ++j) {
        lpWave2[start + i] = lpData[j];
        if (j >= valWavelength) { j = 0; }
    }
    free(lpData);

    // シリアルデータ2
    valFrequency = 11300;
    valWavelength = SAMPLING / valFrequency;
    lpData = (LPWORD)calloc(sizeof(WORD), valWavelength);
    createWave(lpData, valFrequency, SAMPLING, amplitude);
    for (i = 1, j = 0; i < end; i += 2, ++j) {
        lpWave3[start + i] = lpData[j];
        if (j >= valWavelength) { j = 0; }
    }
    for (i = 1, j = 0; i < end; i += 2, j += 2) {
        if (j >= bit * 9) { j = 0; }
        if (isON(data, bit, i)) {
            lpWave3[start + i] = 0;
        }
    }
    free(lpData);

    // リセット
    valFrequency = 17000;
    valWavelength = SAMPLING / valFrequency;
    lpData = (LPWORD)calloc(sizeof(WORD), valWavelength);
    createWave(lpData, valFrequency, SAMPLING, amplitude);
    for (i = 1; i < end; i += 2, ++j) {
        lpWave4[start + i] = 0;
    }
    for (i = 1, j = 0; i < (end - bit * 9); i += 2, ++j) {
        if ((i % (bit * 9)) < bit) {
            lpWave4[start + i - bit] = lpData[j];
        }
        if (j >= valWavelength) { j = 0; }
    }
    free(lpData);

    // チャンネル2に合成
    for (i = 1; i < end; i += 2) {
        lpWave[i] = lpWave1[i] + lpWave2[i] + lpWave3[i] + lpWave4[i];
    }

    // サウンド出力
    whdr.lpData = (LPSTR)lpWave;
    whdr.dwBufferLength = SAMPLING * (CHANNEL * BITSPERSAMPLE / 8) * terms;
    whdr.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
    whdr.dwLoops = 1;
    waveOutPrepareHeader(hWaveOut, &whdr, sizeof(WAVEHDR));
    waveOutWrite(hWaveOut, &whdr, sizeof(WAVEHDR));
}
int main() {

    WAVEFORMATEX wfe;
    HWAVEOUT hWaveOut;

    wfe.wFormatTag = WAVE_FORMAT_PCM;
    wfe.nChannels = CHANNEL;
    wfe.wBitsPerSample = BITSPERSAMPLE;
    wfe.nBlockAlign = CHANNEL * BITSPERSAMPLE / 8;
    wfe.nSamplesPerSec = SAMPLING;
    wfe.nAvgBytesPerSec = wfe.nSamplesPerSec * wfe.nBlockAlign;

    waveOutOpen(&hWaveOut, 0, &wfe, 0, 0, CALLBACK_NULL);

    char str[128];
    do {
        std::cout << "2桁の16進コードを入力しEnterを押して下さい。(00 ~ FF)" << std::endl;
        std::cout << "Ctrl+Zを入力しEnterを入力すると終了します。" << std::endl;
        std::cin >> str;

        // 入力された文字列をデータに変換
        unsigned char data = (unsigned char)strtol(str, NULL, 16);

        // Ctrl+Zで終了
        if (std::cin.eof()) { break; }

        // データをサウンド出力する
        soundOut(hWaveOut, data);
    } while (true);

    waveOutClose(hWaveOut);
}

 ここまででPCのサウンド出力を使ってデジタル素子を制御出来ることが解りました。