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

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

unix.superglobalmegacorp.com

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