Annotation of nono/vm/sram.cpp, revision 1.1.1.13

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: //
                      8: // SRAM
                      9: //
1.1.1.9   root       10: 
1.1       root       11: // SRAM はビッグエンディアンのままメモリに置かれるので、
                     12: // 必要ならアクセス時に都度変換すること。
                     13: 
1.1.1.9   root       14: #include "sram.h"
1.1.1.10  root       15: #include "config.h"
1.1.1.9   root       16: #include "mainapp.h"
1.1.1.11  root       17: #include "mainram.h"
1.1.1.13! root       18: #include <sys/mman.h>
1.1.1.9   root       19: 
                     20: // コンストラクタ
1.1       root       21: SRAMDevice::SRAMDevice()
1.1.1.11  root       22:        : inherited(OBJ_SRAM)
1.1       root       23: {
                     24: }
                     25: 
1.1.1.9   root       26: // デストラクタ
1.1       root       27: SRAMDevice::~SRAMDevice()
                     28: {
1.1.1.13! root       29:        if ((bool)file) {
        !            30:                // この場合は mem は file が管理している。
        !            31:                file.reset();
        !            32:        } else {
        !            33:                if (mem) {
        !            34:                        munmap(mem, sramsize);
        !            35:                }
        !            36:        }
1.1       root       37: }
                     38: 
1.1.1.11  root       39: // 初期化
1.1       root       40: bool
                     41: SRAMDevice::Init()
                     42: {
1.1.1.11  root       43:        if (inherited::Init() == false) {
                     44:                return false;
                     45:        }
                     46: 
1.1.1.13! root       47:        // SRAM ファイルをオープン。
        !            48:        if (gMainApp.GetVMDir().empty()) {
        !            49:                // Human68k モードで VM ディレクトリ指定がなければ、ダミーを用意。
        !            50:                void *m = mmap(NULL, sramsize, PROT_READ | PROT_WRITE,
        !            51:                        MAP_ANON | MAP_SHARED, -1, 0);
        !            52:                if (m == MAP_FAILED) {
        !            53:                        warn("mmap");
        !            54:                        return false;
        !            55:                }
        !            56:                mem = (uint8 *)m;
        !            57:                memcpy(mem, &InitialData[0], InitialData.size());
        !            58:        } else {
        !            59:                filename = gMainApp.GetVMDir() + "SRAM.DAT";
        !            60:                file.reset(new MappedFile());
        !            61:                file->SetFilename(filename);
        !            62:                file->SetDispname(string_format("SRAM \"%s\"", filename.c_str()));
        !            63:                mem = file->OpenCreate(sramsize);
        !            64:                if (mem == NULL) {
        !            65:                        return false;
        !            66:                }
1.1       root       67:        }
                     68: 
1.1.1.10  root       69:        // SRAM の RAM 容量欄を設定値に同期させる。
                     70:        // この処理はアプリケーション起動時に一回行われるのみで、
                     71:        // その後の VM 内のリセット等では動作しない。
                     72:        bool sync = gConfig->Find("sram-sync-ramsize").AsInt();
                     73:        if (sync) {
                     74:                SyncRAMSize();
                     75:        }
                     76: 
1.1       root       77:        return true;
                     78: }
                     79: 
1.1.1.9   root       80: // リセット
1.1.1.6   root       81: void
1.1.1.9   root       82: SRAMDevice::ResetHard(bool poweron)
1.1.1.6   root       83: {
                     84:        // XXX 未調査
                     85:        writeable = false;
                     86: }
                     87: 
1.1.1.10  root       88: // SRAM の RAM 容量欄を設定値に同期させる。
                     89: void
                     90: SRAMDevice::SyncRAMSize()
                     91: {
                     92:        // SRAM の内容が無効なら何もしない。
                     93:        static const std::array<uint8, 7> magic {
                     94:                0x82, 0x77, '6', '8', '0', '0', '0'
                     95:        };
                     96:        for (int i = 0; i < magic.size(); i++) {
                     97:                if (mem[i] != magic[i]) {
                     98:                        return;
                     99:                }
                    100:        }
                    101: 
1.1.1.11  root      102:        uint32 ramsize = GetMainRAMDevice()->GetSize();
1.1.1.10  root      103: 
                    104:        // 値が違う時だけ表示したいので一旦読み出して比較
                    105:        uint32 cursize;
                    106:        cursize  = mem[0x08] << 24;
                    107:        cursize |= mem[0x09] << 16;
                    108:        cursize |= mem[0x0a] << 8;
                    109:        cursize |= mem[0x0b];
                    110:        if (cursize != ramsize) {
1.1.1.13! root      111:                if (gMainApp.GetVMDir().empty()) {
        !           112:                        // Human68k モードで -c のない時は表示不要。ダミー SRAM なので。
        !           113:                } else {
        !           114:                        // ここは Init() なので putmsg() は使えない
        !           115:                        warnx("SRAM: Update ramsize %uMB", ramsize / 1024 / 1024);
        !           116:                }
1.1.1.10  root      117: 
                    118:                mem[0x08] = ramsize >> 24;
                    119:                mem[0x09] = ramsize >> 16;
                    120:                mem[0x0a] = ramsize >> 8;
                    121:                mem[0x0b] = ramsize;
                    122:        }
                    123: }
                    124: 
1.1.1.3   root      125: // アドレスデコーダ
1.1.1.12  root      126: inline uint32
1.1.1.3   root      127: SRAMDevice::Decoder(uint32 addr) const
                    128: {
1.1.1.4   root      129:        uint32 offset = addr - baseaddr;
1.1.1.13! root      130:        assertmsg(offset < file->GetMemSize(),
        !           131:                "offset=%x memlen=%x", offset, (uint)file->GetMemSize());
1.1.1.4   root      132:        return offset;
1.1.1.3   root      133: }
                    134: 
1.1.1.12  root      135: busdata
1.1.1.13! root      136: SRAMDevice::Read(busaddr addr)
1.1       root      137: {
1.1.1.13! root      138:        uint32 offset = Decoder(addr.Addr());
        !           139:        busdata data;
1.1.1.4   root      140: 
1.1.1.13! root      141:        data  = Get16(offset & ~1U);
        !           142:        data |= BusData::Size2;
        !           143:        putlog(2, "$%06x -> $%04x", addr.Addr() & ~1U, data.Data());
1.1.1.4   root      144:        return data;
1.1       root      145: }
                    146: 
1.1.1.12  root      147: busdata
1.1.1.13! root      148: SRAMDevice::Write(busaddr addr, uint32 data)
1.1       root      149: {
1.1.1.13! root      150:        uint32 offset = Decoder(addr.Addr());
        !           151:        uint32 reqsize = addr.GetSize();
        !           152:        uint32 datasize = std::min(2 - (offset & 1), reqsize);
        !           153:        data >>= (reqsize - datasize) * 8;
1.1.1.3   root      154: 
1.1       root      155:        if (writeable) {
1.1.1.3   root      156:                if (offset < 0x100) {
1.1.1.13! root      157:                        putlog(1, "$%06x.%c <- $%0*x", addr.Addr(),
        !           158:                                (datasize == 1 ? 'B' : 'W'), datasize * 2, data);
        !           159:                }
        !           160:                if (datasize == 1) {
        !           161:                        mem[offset] = data;
        !           162:                } else {
        !           163:                        *(uint16 *)&mem[offset] = htobe16(data);
1.1       root      164:                }
                    165:        } else {
1.1.1.13! root      166:                putlog(1, "Write protected $%06x.%c <- $%0*x", addr.Addr(),
        !           167:                        (datasize == 1 ? 'B' : 'W'), datasize * 2, data);
1.1       root      168:        }
1.1.1.13! root      169: 
        !           170:        busdata r = busdata::Size(datasize);
        !           171:        return r;
1.1       root      172: }
                    173: 
1.1.1.12  root      174: busdata
1.1.1.13! root      175: SRAMDevice::Peek1(uint32 addr)
1.1       root      176: {
1.1.1.3   root      177:        uint32 offset = Decoder(addr);
1.1.1.13! root      178:        uint32 data = Get16(offset & ~1U);
1.1.1.3   root      179: 
1.1.1.13! root      180:        if ((offset & 1) == 0) {
        !           181:                return data >> 8;
1.1       root      182:        } else {
1.1.1.13! root      183:                return data & 0xff;
1.1       root      184:        }
1.1.1.11  root      185: }
1.1.1.4   root      186: 
1.1.1.12  root      187: bool
1.1.1.13! root      188: SRAMDevice::Poke1(uint32 addr, uint32 data)
1.1.1.11  root      189: {
                    190:        // WriteEnable() に関わらず変更する。
                    191:        // また Get8()、Get16() はこの変更の影響を受けないことに注意。
                    192: 
                    193:        if ((int32)data >= 0) {
                    194:                uint32 offset = Decoder(addr);
                    195:                mem[offset] = data;
                    196:        }
1.1.1.12  root      197:        return true;
1.1       root      198: }
                    199: 
                    200: void
1.1.1.11  root      201: SRAMDevice::WriteEnable(bool value)
1.1       root      202: {
                    203:        if (writeable == false && value == true) {
1.1.1.10  root      204:                putlog(1, "Write Enable");
1.1       root      205:        } else if (writeable == true && value == false) {
1.1.1.10  root      206:                putlog(1, "Write Disable");
1.1       root      207:        }
                    208: 
                    209:        writeable = value;
                    210: }
1.1.1.4   root      211: 
1.1.1.11  root      212: // SRAM から副作用なく読み出す。
                    213: // ただしホストファイル起動モードなら改変した値を返す。
1.1.1.4   root      214: uint32
1.1.1.13! root      215: SRAMDevice::Get16(uint32 offset) const
1.1.1.4   root      216: {
1.1.1.11  root      217:        if (gMainApp.exec_file) {
                    218:                switch (offset) {
                    219:                 case 0x0c:                     // ROM 起動アドレス(.L)
1.1.1.13! root      220:                        return (plutoaddr >> 16) & 0xffff;
1.1.1.11  root      221:                 case 0x0e:
1.1.1.13! root      222:                        return plutoaddr & 0xffff;
1.1.1.11  root      223: 
                    224:                 case 0x18:                     // 起動デバイス(.W) を ROM($a000) にする
1.1.1.13! root      225:                        return 0xa000;
1.1.1.11  root      226: 
                    227:                 default:
                    228:                        break;
                    229:                }
1.1.1.4   root      230:        }
1.1.1.11  root      231: 
                    232:        // 他は SRAM から
1.1.1.13! root      233:        uint32 data = *(uint16 *)&mem[offset];
        !           234:        return be16toh(data);
1.1.1.4   root      235: }
1.1.1.10  root      236: 
1.1.1.11  root      237: // SRAM から副作用なくロングワードで読み出す。
                    238: // ただしホストファイル起動モードなら改変した値を返す。
1.1.1.10  root      239: uint32
1.1.1.11  root      240: SRAMDevice::Get32(uint32 offset) const
1.1.1.10  root      241: {
                    242:        uint32 data;
                    243: 
1.1.1.11  root      244:        data  = Get16(offset) << 16;
                    245:        data |= Get16(offset + 2);
1.1.1.10  root      246: 
                    247:        return data;
                    248: }
1.1.1.11  root      249: 
                    250: // IPLROM30 が初期化した SRAM の初期値。
                    251: //
                    252: // 0000: 82 77 36 38 30 30 30 57  00 40 00 00 00 fc 00 00  |.w68000W.@......|
                    253: // 0010: 00 ed 01 00 ff ff ff ff  00 00 4e 07 00 10 00 00  |..........N.....|
                    254: // 0020: 00 00 ff ff 00 00 07 00  0e 00 0d 00 00 00 00 00  |................|
                    255: // 0030: f8 3e ff c0 ff fe de 6c  40 22 03 02 00 08 00 00  |.>.....l@"......|
                    256: // 0040: 00 00 00 00 00 00 00 00  00 ff f4 00 04 00 01 01  |................|
                    257: // 0050: 00 00 00 20 00 03 f9 01  00 00 00 00 00 00 00 00  |... ............|
                    258: /*static*/ std::array<uint8, 0x60>
                    259: SRAMDevice::InitialData {
                    260:        0x82, 0x77, 0x36, 0x38, 0x30, 0x30, 0x30, 0x57,
                    261:        0x00, 0x40, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00,
                    262:        0x00, 0xed, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff,
                    263:        0x00, 0x00, 0x4e, 0x07, 0x00, 0x10, 0x00, 0x00,
                    264:        0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x07, 0x00,
                    265:        0x0e, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
                    266:        0xf8, 0x3e, 0xff, 0xc0, 0xff, 0xfe, 0xde, 0x6c,
                    267:        0x40, 0x22, 0x03, 0x02, 0x00, 0x08, 0x00, 0x00,
                    268:        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    269:        0x00, 0xff, 0xf4, 0x00, 0x04, 0x00, 0x01, 0x01,
                    270:        0x00, 0x00, 0x00, 0x20, 0x00, 0x03, 0xf9, 0x01,
                    271:        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    272: };

unix.superglobalmegacorp.com

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