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

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

unix.superglobalmegacorp.com

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