--- nono/vm/videoctlr.cpp 2026/04/29 17:04:36 1.1 +++ nono/vm/videoctlr.cpp 2026/04/29 17:05:24 1.1.1.9 @@ -4,7 +4,9 @@ // Licensed under nono-license.txt // -#include "videoctlr.h" +// +// ビデオコントローラ +// // e82000..e823ff パレット // e82400..e824ff R0(e82400.w) のミラー @@ -13,112 +15,463 @@ // e82700..e82fff $00 が読めるようだ // 以上の 4KB をミラー。 -std::unique_ptr gVideoCtlr; +#include "videoctlr.h" +#include "mpu.h" +#include "renderer.h" +#include "scheduler.h" +#include "uimessage.h" +#include + +// InsideOut p.135 +static const busdata read_wait = busdata::Wait( 9 * 40_nsec); +static const busdata write_wait = busdata::Wait(11 * 40_nsec); +// コンストラクタ VideoCtlrDevice::VideoCtlrDevice() + : inherited(OBJ_VIDEOCTLR) { - logname = "videoctlr"; - devname = "VideoCtlr"; - devaddr = baseaddr; - devlen = 0x2000; + textbmp.Create(768, 512); + hostcolor.resize(16); + +#if defined(HAVE_AVX2) + enable_avx2 = gMainApp.enable_avx2; +#endif + + // 約30fps。これ以上速くても分からない。 + contrast_event.func = ToEventCallback(&VideoCtlrDevice::ContrastCallback); + contrast_event.time = 30_msec; + contrast_event.Regist("VideoCtlr Analog Contrast"); } +// デストラクタ VideoCtlrDevice::~VideoCtlrDevice() { } -// addr は何ワード目のポートかという意味になるので -// $E82002 が addr=1 (番目のワード)。 -uint64 -VideoCtlrDevice::Read(uint32 addr) +// 初期化 +bool +VideoCtlrDevice::Init() { - uint32 data; + if (inherited::Init() == false) { + return false; + } - if (addr < 0x400 / 2) { // パレット - data = palette.w[addr]; + planevram = GetPlaneVRAMDevice(); + renderer = GetRenderer(); - } else if (addr < 0x500 / 2) { // $E82400 (R0) - data = videoctlr.r0; + return true; +} - } else if (addr < 0x600 / 2) { // $E82500 (R1) - data = videoctlr.r1; +// リセット +void +VideoCtlrDevice::ResetHard(bool poweron) +{ + if (poweron) { + // 本当かどうかは知らないが、とりあえず。 + // 初期状態は放電されていれば 0 のはずで、 + // ビデオコントローラ側の出力の初期値はおそらく High のはず。 + running_contrast = 0; + SetContrast(15); + } +} - } else if (addr < 0x700 / 2) { // $E82600 (R2) - data = videoctlr.r2; +inline uint32 +VideoCtlrDevice::Decoder(uint32 addr) const +{ + return addr & 0xfff; +} + +busdata +VideoCtlrDevice::Read8(uint32 addr) +{ + busdata data; + + uint32 offset = Decoder(addr); + if (offset < 0x400) { // パレット + data = palette[HB(offset)]; + putlog(3, "Palette $%06x.B -> $%02x", addr, data.Data()); + + } else if (offset < 0x700) { // R0,R1,R2 + uint nr = Offset2NR(offset); + data = GetNR(nr); + putlog(3, "R%d.%c -> $%02x", + (nr / 2), + (nr % 2) == 0 ? 'H' : 'L', + data.Data()); - } else { // それ以降は $00 が読めるようだ + } else { data = 0; } + data |= read_wait; return data; } -uint64 -VideoCtlrDevice::Write(uint32 addr, uint32 data) +busdata +VideoCtlrDevice::Read16(uint32 addr) { - if (addr < 0x400 / 2) { // パレット - putlog(3, "パレット $%06x.W <- $%04x", baseaddr + addr * 2, data); - palette.w[addr] = data; + busdata data; - } else if (addr < 0x500 / 2) { // $E82400 (R0) - SetR0(data); + uint32 offset = Decoder(addr); + if (offset < 0x400) { // パレット + data = *(uint16 *)&palette[offset]; + putlog(3, "Palette $%06x.W -> $%04x", addr, data.Data()); + + } else if (offset < 0x700) { // R0,R1,R2 + uint nr = Offset2NR(offset); + data = GetNR(nr) << 8; + data |= GetNR(nr + 1); + putlog(3, "R%d -> $%04x", (nr / 2), data.Data()); - } else if (addr < 0x600 / 2) { // $E82500 (R1) - SetR1(data); + } else { + data = 0; + } - } else if (addr < 0x700 / 2) { // $E82600 (R2) - SetR2(data); + data |= read_wait; + return data; +} - } else { // それ以降はたぶん何も起きない? +busdata +VideoCtlrDevice::Write8(uint32 addr, uint32 data) +{ + uint32 offset = Decoder(addr); + if (offset < 0x400) { // パレット + putlog(2, "Palette $%06x.B <- $%02x", addr, data); + palette[HB(offset)] = data; + MakePalette(); + + } else if (offset < 0x700) { // R0,R1,R2 + uint nr = Offset2NR(offset); + putlog(2, "R%d.%c <- $%02x (NOT IMPLEMENTED)", + (nr / 2), + (nr % 2) == 0 ? 'H' : 'L', + data); + SetNR(nr, data); + + } else { + // たぶん何も起きない? } - return 0; + return write_wait; } -uint64 -VideoCtlrDevice::Peek(uint32 addr) +busdata +VideoCtlrDevice::Write16(uint32 addr, uint32 data) { - uint32 data; + uint32 offset = Decoder(addr); + if (offset < 0x400) { // パレット + putlog(2, "Palette $%06x.W <- $%04x", addr, data); + *(uint16 *)&palette[offset] = data; + MakePalette(); + + } else if (offset < 0x700) { // R0,R1,R2 + uint nr = Offset2NR(offset); + putlog(3, "R%d <- $%04x (NOT IMPLEMENTED)", (nr / 2), data); + SetNR(nr, data >> 8); + SetNR(nr + 1, data & 0xff); - if (addr < 0x400 / 2) { // パレット - data = palette.w[addr]; + } else { + // たぶん何も起きない? + } - } else if (addr < 0x500 / 2) { // $E82400 (R0) - data = videoctlr.r0; + return write_wait; +} - } else if (addr < 0x600 / 2) { // $E82500 (R1) - data = videoctlr.r1; +busdata +VideoCtlrDevice::Peek8(uint32 addr) +{ + uint32 offset = Decoder(addr); + if (offset < 0x400) { // パレット + return palette[HB(offset)]; + + } else if (offset < 0x700) { // R0,R1,R2 + uint nr = Offset2NR(offset); + return GetNR(nr); - } else if (addr < 0x700 / 2) { // $E82600 (R2) - data = videoctlr.r2; + } else { + return 0x00; + } +} - } else { // それ以降は $00 が読めるようだ - data = 0; +bool +VideoCtlrDevice::Poke8(uint32 addr, uint32 data) +{ + uint32 offset = Decoder(addr); + if (offset < 0x400) { + if ((int32)data >= 0) { + palette[HB(offset)] = data; + // Poke による編集でもパレットに反映させる + MakePalette(); + } + return true; } - return data; + return false; +} + +// R0, R1, R2 のオフセットから内部仮想レジスタ番号 NR0-NR5 に変換する。 +// offset は R0, R1, R2 の領域を指していること。 +inline uint +VideoCtlrDevice::Offset2NR(uint32 offset) const +{ + assert(0x400 <= offset && offset < 0x700); + return ((offset / 0x100) - 4) * 2 + (offset & 1); +} + +// 内部仮想レジスタ NR0-NR5 を読み出す。 +uint32 +VideoCtlrDevice::GetNR(uint reg) const +{ + return nreg[reg]; +} + +// 内部仮想レジスタ NR0-NR5 にバイトデータを書き込む。 +void +VideoCtlrDevice::SetNR(uint reg, uint32 data) +{ + // XXX まだ値を保存する以外何もしていない + assert(reg < 6); + switch (reg) { + case 0: // R0.H + nreg[reg] = 0; + break; + case 1: // R0.L + nreg[reg] = data & 0x03; + break; + + case 2: // R1.H + nreg[reg] = data & 0x3f; + break; + case 3: // R1.L + nreg[reg] = data & 0xff; + break; + + case 4: // R2.H + nreg[reg] = data & 0xff; + break; + case 5: // R2.L + nreg[reg] = data & 0x7f; + break; + + default: + __unreachable(); + } } -// R0 への書き込み +// コントラストを設定する。 +// 指定値は 0-15 だが、内部では 0-xff で保持。 void -VideoCtlrDevice::SetR0(uint32 data) +VideoCtlrDevice::SetContrast(int contrast_) { - putlog(0, "R0 <- $%04x 未実装書き込み", data); - videoctlr.r0 = data & 3; + target_contrast = contrast_ * 0x11; + + if (running_contrast != target_contrast) { + initial_contrast = running_contrast; + contrast_time0 = scheduler->GetVirtTime(); + scheduler->RestartEvent(contrast_event); + } } -// R1 への書き込み +// コントラスト変化イベント。 +// +// Human68k 電源オフ時などの画面フェードアウトは ROM や Human68k がソフト +// ウェアで時間をかけてコントラストを変えているのではなく、ハードウェアで +// 実装してある。(わざわざ?) RC 回路が入っているのでおそらく意図していると +// 思われ。電源オフ時は ROM ルーチンがシステムポートにコントラスト 0 を1回 +// だけ書き込んでいる。 +// +// 細かいことは無視して RC 回路の過渡現象の式だけ持ってきて使う。 +// +// 下がる時は E * exp(-t / RC) で、 +// E は開始時の値なので initial_contrast、原点は target_contrast とする。 +// +// E |* +// |* +// | * +// | *** +// | **** +// 0 +--------- +// +// 上がる時は E * (1 - exp(-t / RC)) で、 +// この場合 E は定常値なので target_contrast、原点が initial_contrast となる。 +// +// E | **** +// | *** +// | * +// |* +// |* +// 0 +--------- +// +// 途中まで充電されてる状態から始まる過渡現象がこれと同じかどうかは知らない +// けど、とりあえず毎回 initial と target の差だけで動作する。 void -VideoCtlrDevice::SetR1(uint32 data) +VideoCtlrDevice::ContrastCallback(Event& ev) { - putlog(0, "R1 <- $%04x 未実装書き込み", data); - videoctlr.r1 = data & 0x3fff; + const float RC = 0.18; // 時定数 1.8[KΩ]*100[uF] + + if (running_contrast != target_contrast) { + // 下がる時は (initial - target) * exp(-t / RC) + // 上がる時は (target - initial) * (1 - exp(-t / RC)) + uint64 now = scheduler->GetVirtTime(); + float t = (float)(now - contrast_time0) / 1e9; + float e = std::exp(-t / RC); + float n; + if (initial_contrast > target_contrast) { + float v = (float)(initial_contrast - target_contrast); + n = target_contrast + v * e; + } else { + float v = (float)(target_contrast - initial_contrast); + n = initial_contrast + v * (1 - e); + } + uint32 new_contrast = (uint32)(n + 0.5); + putlog(2, "Contrast %x->%x: t=%g e=%g new=%x", + initial_contrast, target_contrast, t, e, new_contrast); + + if (new_contrast != running_contrast) { + running_contrast = new_contrast; + + // 強制再描画をレンダラに通知 + renderer->Invalidate2(); + } + + scheduler->RestartEvent(ev); + } } -// R2 への書き込み void -VideoCtlrDevice::SetR2(uint32 data) +VideoCtlrDevice::MakePalette() +{ + const uint16 *p; + + // パレット前半 256 ワードはグラフィックパレット、 + // テキストパレットは後半。 + p = (uint16 *)&palette[512]; + + // パレット情報から Color 形式の色データを作成 + // X680x0 のパレットは %GGGGG'RRRRR'BBBBB'I で並んでいる。 + for (int i = 0; i < 16; i++) { + int I = (p[i] & 1) << 2; // 輝度ビット + int R = (((p[i] >> 6) & 0x1f) << 3) + I; + int G = (((p[i] >> 11)) << 3) + I; + int B = (((p[i] >> 1) & 0x1f) << 3) + I; + hostcolor[i].r = R; + hostcolor[i].g = G; + hostcolor[i].b = B; + } + + // パレット変更が起きたことをレンダラに通知 + renderer->Invalidate2(); + + // UI にも通知 + UIMessage::Post(UIMessage::PALETTE); +} + +// ゲストパレットを uint32 形式にしたものの一覧を返す。(GUI 用) +// XXX 今の所テキスト16色しか考えてない +const std::vector +VideoCtlrDevice::GetIntPalette() const { - putlog(0, "R2 <- $%04x 未実装書き込み", data); - videoctlr.r2 = (data & 0xff7f); + std::vector ipal; + const uint16 *p = (const uint16 *)&palette[0]; + + for (int i = 0; i < 16; i++) { + ipal.push_back(p[256 + i]); + } + return ipal; +} + +// 画面合成。 +// レンダリングスレッドから呼ばれる。 +bool +VideoCtlrDevice::Render(BitmapRGBX& dst, ModifyInfo& modify) +{ + // 今はテキスト画面しかない。 + bool updated = planevram->Render(textbmp, modify); + + // 最後にコントラスト適用。 + // アトミックじゃなくても大丈夫のはず。 + if (__predict_false(rendering_contrast != running_contrast)) { + rendering_contrast = running_contrast; + updated = true; + } + if (__predict_false(updated)) { + RenderContrast(dst, textbmp); + updated = true; + } + return updated; +} + +#if 0 +#define PERFCONT +#include "stopwatch.h" +static uint64 perf_total; +static int perf_count; +#endif + +// コントラストを適用。 +void +VideoCtlrDevice::RenderContrast(BitmapRGBX& dst, const BitmapRGBX& src) +{ + assert(dst.GetWidth() == src.GetWidth()); + assert(dst.GetHeight() == src.GetHeight()); + assert(src.GetWidth() % 4 == 0); + + uint32 contrast = rendering_contrast; + if (contrast == 255) { + dst.CopyFrom(&src); + } else { +#if defined(PERFCONT) + Stopwatch sw; + sw.Start(); +#endif + +#if defined(HAVE_AVX2) + if (__predict_true(enable_avx2)) { + RenderContrast_avx2(dst, src, contrast); + } else +#endif + { + RenderContrast_gen(dst, src, contrast); + } + +#if defined(PERFCONT) + sw.Stop(); + perf_total += sw.Elapsed(); + if (++perf_count % 100 == 0) { + printf("%" PRIu64 "\n", perf_total / perf_count); + } +#endif + } +} + +// コントラスト (0-254) を適用。C++ 版 +/*static*/ void +VideoCtlrDevice::RenderContrast_gen(BitmapRGBX& dst, const BitmapRGBX& src, + uint32 contrast) +{ + std::array lut; + + for (int i = 0; i < lut.size(); i++) { + lut[i] = (i * contrast) >> 8; + } + + for (int y = 0, yend = src.GetHeight(); y < yend; y++) { + const uint32 *s = (const uint32 *)src.GetRowPtr(y); + uint32 *d = (uint32 *)dst.GetRowPtr(y); + + const uint32 *send = s + src.GetWidth(); + for (; s < send; ) { + // 1ピクセルずつ処理する + // 445.1 usec + + uint32 c = htole32(*s++); + + uint32 r = lut[c & 0xff]; + uint32 g = lut[(c >> 8) & 0xff]; + uint32 b = lut[(c >> 16) & 0xff]; + + c = r | (g << 8 ) | (b << 16); + *d++ = le32toh(c); + } + } }