--- nono/vm/device.cpp 2026/04/29 17:04:28 1.1.1.1 +++ nono/vm/device.cpp 2026/04/29 17:05:11 1.1.1.8 @@ -1,22 +1,25 @@ // // nono -// Copyright (C) 2017 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "device.h" -#include "vm.h" +// +// デバイスの基本クラスなど +// -// 全デバイスリスト -std::vector gDevices; +#include "device.h" +#include "mpu.h" +#include "scheduler.h" // // Device // // コンストラクタ -Device::Device() +Device::Device(const std::string& objname_) + : inherited(objname_) { - gDevices.push_back(this); } // デストラクタ @@ -42,28 +45,40 @@ Device::Apply() return true; } -bool -Device::PowerOn() +void +Device::ResetHard(bool poweron) { - return true; } -bool +void Device::PowerOff() { - return true; } -// 本体リセットスイッチによるリセット +// ログ表示。 +// VM デバイスでは、仮想時間、PC、オブジェクト名を表示する。 void -Device::ResetHard() +Device::putlogn(const char *fmt, ...) const { -} + char buf[1024]; + va_list ap; + uint64 vt; + int len; + + vt = gScheduler->GetVirtTime(); + len = snprintf(buf, sizeof(buf), "%4d.%03d'%03d'%03d %08x %s ", + (int)(vt / 1000 / 1000 / 1000), + (int)((vt / 1000 / 1000) % 1000), + (int)((vt / 1000) % 1000), + (int)(vt % 1000), + gMPU->GetPPC(), + GetName().c_str()); + + va_start(ap, fmt); + vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); + va_end(ap); -// MPU の RESET 命令によるリセット -void -Device::ResetSoft() -{ + WriteLog(buf); } @@ -72,7 +87,8 @@ Device::ResetSoft() // // コンストラクタ -IODevice::IODevice() +IODevice::IODevice(const std::string& objname_) + : inherited(objname_) { devaddr = 0; devlen = 0; @@ -112,7 +128,7 @@ IODevice::Write16(uint32 addr, uint32 da { int64 berr; - berr = Write8(addr, data >> 8) ; + berr = Write8(addr, data >> 8); if (berr < 0) return berr; berr = Write8(addr + 1, data & 0xff); @@ -138,7 +154,7 @@ IODevice::Write32(uint32 addr, uint32 da uint64 IODevice::Peek8(uint32 addr) { - return 0; + return 0xff; } @@ -146,50 +162,64 @@ IODevice::Peek8(uint32 addr) // バスエラー // +// グローバル参照用 +BusErrDevice *gBusErr; + // コンストラクタ BusErrDevice::BusErrDevice() + : inherited("(BusErr)") { - devname = "(BusErr)"; + ClearAlias(); + AddAlias("BusErr"); + devaddr = -1; // XXX どうしたもんか } +// デストラクタ BusErrDevice::~BusErrDevice() { + gBusErr = NULL; } uint64 BusErrDevice::Read8(uint32 addr) { + putlog(1, "$%08x.b", addr); return (uint64)-1; } uint64 BusErrDevice::Read16(uint32 addr) { + putlog(1, "$%08x.w", addr); return (uint64)-1; } uint64 BusErrDevice::Read32(uint32 addr) { + putlog(1, "$%08x.l", addr); return (uint64)-1; } uint64 BusErrDevice::Write8(uint32 addr, uint32 data) { + putlog(1, "$%08x.b <- %02X", addr, data); return (uint64)-1; } uint64 BusErrDevice::Write16(uint32 addr, uint32 data) { + putlog(1, "$%08x.w <- %04X", addr, data); return (uint64)-1; } uint64 BusErrDevice::Write32(uint32 addr, uint32 data) { + putlog(1, "$%08x.l <- %08X", addr, data); return (uint64)-1; } @@ -204,30 +234,67 @@ BusErrDevice::Peek8(uint32 addr) // 何もしないデバイス // +// グローバル参照用 +NopIODevice *gNopIO; + // コンストラクタ -NullDevice::NullDevice() +NopIODevice::NopIODevice() + : inherited("(NopIO)") { - devname = "(Null)"; + ClearAlias(); + AddAlias("NopIO"); } -NullDevice::~NullDevice() +// デストラクタ +NopIODevice::~NopIODevice() { + gNopIO = NULL; } uint64 -NullDevice::Read8(uint32 addr) +NopIODevice::Read8(uint32 addr) { + putlog(1, "$%08x.b", addr); return 0xff; } uint64 -NullDevice::Write8(uint32 addr, uint32 data) +NopIODevice::Read16(uint32 addr) +{ + putlog(1, "$%08x.w", addr); + return 0xffff; +} + +uint64 +NopIODevice::Read32(uint32 addr) +{ + putlog(1, "$%08x.l", addr); + return 0xffffffff; +} + +uint64 +NopIODevice::Write8(uint32 addr, uint32 data) +{ + putlog(1, "$%08x.b <- $%02x", addr, data); + return 0; +} + +uint64 +NopIODevice::Write16(uint32 addr, uint32 data) +{ + putlog(1, "$%08x.w <- $%04x", addr, data); + return 0; +} + +uint64 +NopIODevice::Write32(uint32 addr, uint32 data) { + putlog(1, "$%08x.l <- $%08x", addr, data); return 0; } uint64 -NullDevice::Peek8(uint32 addr) +NopIODevice::Peek8(uint32 addr) { return 0xff; }