Annotation of nono/vm/vm.cpp, revision 1.1.1.4

1.1       root        1: //
                      2: // nono
1.1.1.4 ! root        3: // Copyright (C) 2020 nono project
        !             4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
                      7: #include "vm.h"
                      8: #include "bus.h"
1.1.1.3   root        9: #include "mainapp.h"
1.1       root       10: #include "mpu.h"
1.1.1.4 ! root       11: #include "mythread.h"
1.1       root       12: #include "ram.h"
                     13: #include "renderer.h"
                     14: #include "scheduler.h"
                     15: 
1.1.1.3   root       16: std::unique_ptr<VM> gVM;
1.1       root       17: 
                     18: // コンストラクタ
1.1.1.3   root       19: VM::VM()
1.1       root       20: {
                     21:        // どこでやるのがいいかわからんけど、このコンストラクタはメインスレッドで
                     22:        // 作られるので、ここでメインスレッドの名前を設定。
1.1.1.4 ! root       23:        PTHREAD_SETNAME("Main");
1.1       root       24: 
1.1.1.4 ! root       25:        // バスエラーデバイスだけここで作成。
1.1.1.3   root       26:        buserr.reset(new BusErrDevice());
1.1       root       27: 
                     28:        // まず全域をバスエラーで初期化。
                     29:        // この後継承先で必要に応じて上書きする。
                     30:        for (int i = 0; i < countof(::devtable); i++) {
1.1.1.3   root       31:                ::devtable[i] = buserr.get();
1.1       root       32:        }
                     33: }
                     34: 
                     35: // デストラクタ
                     36: VM::~VM()
                     37: {
1.1.1.4 ! root       38:        Dispose();
        !            39: }
        !            40: 
        !            41: // 終了
        !            42: void
        !            43: VM::Dispose()
        !            44: {
        !            45:        if ((bool)gScheduler) {
        !            46:                // スケジューラを停止。
        !            47:                // スケジューラ(VMスレッド)からのアクセスさえなくなれば
        !            48:                // 後の有象無象はグローバルデストラクタにお任せでもいいか。
        !            49:                // だめだったらもっと頑張る。
        !            50:                gScheduler->Terminate();
        !            51:                gScheduler.reset();
        !            52:        }
1.1       root       53: }
                     54: 
                     55: // コンストラクタ後の初期化
                     56: bool
                     57: VM::Create()
                     58: {
                     59:        for (const auto& d : gDevices) {
                     60:                if (!d->Create()) {
                     61:                        return false;
                     62:                }
                     63:        }
                     64: 
                     65:        return true;
                     66: }
                     67: 
                     68: // 初期化
                     69: bool
                     70: VM::Init()
                     71: {
                     72:        for (const auto& d : gDevices) {
                     73:                if (!d->Init()) {
                     74:                        return false;
                     75:                }
                     76:        }
                     77: 
                     78:        return true;
                     79: }
                     80: 
                     81: // 設定の適用
                     82: bool
                     83: VM::Apply()
                     84: {
                     85:        for (const auto& d : gDevices) {
                     86:                if (!d->Apply()) {
                     87:                        return false;
                     88:                }
                     89:        }
                     90: 
                     91:        return true;
                     92: }
                     93: 
                     94: // VM の全デバイスの電源をオン
                     95: bool
                     96: VM::DevicePowerOn()
                     97: {
                     98:        for (const auto& d : gDevices) {
                     99:                if (!d->PowerOn()) {
                    100:                        return false;
                    101:                }
                    102:        }
                    103: 
                    104:        // ハードウェアリセット
                    105:        ResetHard();
                    106: 
                    107:        // スケジューラを開始
1.1.1.3   root      108:        // (MPU の ResetHard() より後で起動時に1回だけ呼び出す)
1.1       root      109:        gScheduler->Run();
                    110: 
                    111:        return true;
                    112: }
                    113: 
                    114: // VM の全デバイスの電源をオフ
                    115: // (Scheduler から呼ばれる)
                    116: bool
                    117: VM::DevicePowerOff()
                    118: {
                    119:        for (const auto& d : gDevices) {
                    120:                if (!d->PowerOff()) {
                    121:                        return false;
                    122:                }
                    123:        }
                    124:        // GUI に通知
                    125:        if (poweroff_callback) {
                    126:                poweroff_callback();
                    127:        }
                    128:        return true;
                    129: }
                    130: 
                    131: // VM 電源オフ時のコールバックを設定する
                    132: void
                    133: VM::SetPowerOffCallback(void (*callback)())
                    134: {
                    135:        poweroff_callback = callback;
                    136: }
                    137: 
                    138: // ハードウェアリセット
                    139: void
                    140: VM::ResetHard()
                    141: {
                    142:        for (const auto& d : gDevices) {
                    143:                d->ResetHard();
                    144:        }
                    145: }
                    146: 
                    147: // MPU の RESET 命令によるリセット
                    148: void
                    149: VM::ResetSoft()
                    150: {
                    151:        for (const auto& d : gDevices) {
                    152:                d->ResetSoft();
                    153:        }
                    154: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.