--- nono/vm/human68k.cpp 2026/04/29 17:04:32 1.1.1.3 +++ nono/vm/human68k.cpp 2026/04/29 17:04:50 1.1.1.6 @@ -1,20 +1,22 @@ // // nono -// Copyright (C) 2017 Y.Sugahara (moveccr) +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt +// + // // human68k .r .x .z console emulator // #include "human68k.h" +#include "autofd.h" +#include "bus.h" +#include "iodevstream.h" #include "mainapp.h" -#include "mpu.h" #include "m68030bus.h" #include "m68030core.h" -#include "bus.h" -#include "iodevstream.h" -#include "scheduler.h" +#include "mpu680x0.h" #include "vm.h" -#include #include #include #include @@ -44,11 +46,11 @@ Human68k::Human68k() human68k_arg = gMainApp.human68k_arg; Files[0].fd = 0; - Files[0].filename = strdup("|stdin"); + Files[0].filename = "|stdin"; Files[1].fd = 1; - Files[1].filename = strdup("|stdout"); + Files[1].filename = "|stdout"; Files[2].fd = 2; - Files[2].filename = strdup("|stderr"); + Files[2].filename = "|stderr"; } // デストラクタ @@ -79,7 +81,7 @@ Human68k::Init() putmsg(1, "arg=%s", human68k_arg); // 実行ファイルオープン - int file_fd = open(human68k_file, O_RDONLY); + autofd file_fd = open(human68k_file, O_RDONLY); if (file_fd == -1) { warn("Human68k executable \"%s\" open failed", human68k_file); return false; @@ -88,7 +90,6 @@ Human68k::Init() struct stat st; if (fstat(file_fd, &st) != 0) { warn("Human68k executable \"%s\" fstat failed", human68k_file); - close(file_fd); return false; } uint32 file_size; @@ -99,7 +100,6 @@ Human68k::Init() file = (uint8 *)mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, file_fd, 0); if (file == MAP_FAILED) { warn("Human68k executable \"%s\" mmap failed", human68k_file); - close(file_fd); return false; } @@ -119,21 +119,19 @@ Human68k::Init() warnx("Human68k executable \"%s\" cannot identify file type", human68k_file); munmap(file, file_size); - close(file_fd); return false; } } if (!r) { warnx("Human68k executable \"%s\" invalid file format", human68k_file); munmap(file, file_size); - close(file_fd); return false; } munmap(file, file_size); - close(file_fd); uint32 last_addr = load_addr + load_size + bss_size; + int ram_size = gRAM->GetSize(); IODeviceStream ds(ram); boot_addr = 0x200; @@ -306,7 +304,7 @@ Human68k::LoadZ(uint8 *file, uint size) bool Human68k::LoadMem(uint32 addr, uint8 *src, uint size) { - if (addr + size > ram_size) { + if (addr + size >= gRAM->GetSize()) { errx(EXIT_FAILURE, "file too large"); } @@ -343,7 +341,7 @@ Human68k::OpenFile(uint32 fileaddr, uint { // XXX: unix host only - int32 fileno; + int32 fileno {}; Human68k::File *f = NULL; // 開いているエントリを検索して Human fileno を取得 @@ -383,7 +381,7 @@ Human68k::OpenFile(uint32 fileaddr, uint } f->fd = fd; - f->filename = strdup(filename); + f->filename = filename; return fileno; } @@ -399,13 +397,13 @@ Human68k::CloseFile(int32 fileno) return -1; } f = &Files[fileno]; - putmsg(1, "CloseFile: %d %s", fileno, f->filename); + putmsg(1, "CloseFile: %d %s", fileno, f->filename.c_str()); if (f->fd > 2) { // stdin/out/err は閉じない close(f->fd); } f->fd = -1; - free(f->filename); + f->filename.clear(); return 0; } @@ -455,7 +453,7 @@ Human68k::FputsFile(int32 fileno, uint32 IODeviceStream ds(ram, dataaddr); int size = 1024; - uint8 buf[size]; + std::vector buf(size); bool eof = false; do { @@ -469,7 +467,7 @@ Human68k::FputsFile(int32 fileno, uint32 len++; } if (len > 0) { - write(f->fd, buf, len); + write(f->fd, &buf[0], len); } } while (!eof); } @@ -484,7 +482,7 @@ Human68k::IOCS(m68kcpu *cpu) case 0x7f: // _ONTIME { putmsg(1, "IOCS ONTIME"); - uint64 t = gScheduler->GetVirtTime(); + uint64 t = gMPU->GetVirtTime(); // nanosec to 10msec, in day RegD(0) = (t / 10_msec) % 8640000; // nanosec to day @@ -610,7 +608,7 @@ Human68k::FLineOp(m68kcpu *cpu) vecaddr += 0xd000; RegD(0) = m68030_read_32(cpu, vecaddr); m68030_write_32(cpu, vecaddr, addr); - } + } break; }