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

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2017 [email protected]
                      4: //
                      5: 
                      6: #include "configfile.h"
                      7: #include "sram.h"
                      8: #include <sys/fcntl.h>
                      9: #include <sys/mman.h>
1.1.1.2 ! root       10: #include <sys/stat.h>
1.1       root       11: 
                     12: //
                     13: // SRAM
                     14: //
                     15: // SRAM はビッグエンディアンのままメモリに置かれるので、
                     16: // 必要ならアクセス時に都度変換すること。
                     17: //
                     18: 
                     19: SRAMDevice *gSRAM;
                     20: 
                     21: SRAMDevice::SRAMDevice()
                     22: {
                     23:        logname = "sram";
                     24:        devname = "SRAM";
                     25:        devaddr = baseaddr;
                     26:        devlen  = 0x10000;      // 通常実際は16KBだが64KB分占有しておく
                     27: 
                     28:        fd = -1;
                     29: }
                     30: 
                     31: SRAMDevice::~SRAMDevice()
                     32: {
                     33:        if (mem) {
                     34:                munmap(mem, memlen);
                     35:                mem = NULL;
                     36:                memlen = 0;
                     37:        }
                     38:        if (fd >= 0) {
                     39:                close(fd);
                     40:                fd = -1;
                     41:        }
                     42: }
                     43: 
                     44: bool
                     45: SRAMDevice::Init()
                     46: {
1.1.1.2 ! root       47:        struct stat st;
        !            48: 
1.1       root       49:        filename = gConfig->GetConfigDir() + "SRAM030.DAT";
1.1.1.2 ! root       50:        memlen = 16 * 1024;
1.1       root       51: 
                     52:        fd = open(filename.c_str(), O_RDWR);
                     53:        if (fd == -1) {
1.1.1.2 ! root       54:                warn("SRAM \"%s\" open failed", filename.c_str());
1.1       root       55:                return false;
                     56:        }
                     57: 
1.1.1.2 ! root       58:        // ファイルサイズをチェック。
        !            59:        // ファイルが短くても mmap はしれっと成功してしまうようだ。
        !            60:        if (fstat(fd, &st) == -1) {
        !            61:                warn("SRAM \"%s\" fstat failed", filename.c_str());
        !            62:                close(fd);
        !            63:                return false;
        !            64:        }
        !            65:        if (st.st_size < memlen) {
        !            66:                warnx("SRAM \"%s\" is too short (filesize is expected %d but %d)",
        !            67:                        filename.c_str(), memlen, (int)st.st_size);
        !            68:                close(fd);
        !            69:                return false;
        !            70:        }
        !            71:        if (st.st_size > memlen) {
        !            72:                // 大きすぎるほうはワーニングだけでいいか
        !            73:                warnx("SRAM \"%s\" is too large (ignore %d bytes)",
        !            74:                        filename.c_str(), (int)st.st_size - memlen);
        !            75:        }
        !            76: 
1.1       root       77:        mem = (uint8 *)mmap(NULL, memlen, PROT_READ | PROT_WRITE,
                     78:                MAP_SHARED, fd, 0);
                     79:        if (mem == MAP_FAILED) {
1.1.1.2 ! root       80:                warn("SRAM \"%s\" mmap failed", filename.c_str());
1.1       root       81:                close(fd);
                     82:                return false;
                     83:        }
                     84: 
                     85:        return true;
                     86: }
                     87: 
                     88: uint64
                     89: SRAMDevice::Read8(uint32 addr)
                     90: {
                     91:        return mem[addr - baseaddr];
                     92: }
                     93: 
                     94: uint64
                     95: SRAMDevice::Read16(uint32 addr)
                     96: {
                     97:        return be16toh(*(uint16 *)&mem[addr - baseaddr]);
                     98: }
                     99: 
                    100: uint64
                    101: SRAMDevice::Write8(uint32 addr, uint32 data)
                    102: {
                    103:        if (writeable) {
                    104:                if (addr - baseaddr < 0x100) {
                    105:                        putlog(1, "更新 $%06x <- $%02x", addr, data);
                    106:                }
                    107:                mem[addr - baseaddr] = data;
                    108:        } else {
                    109:                putlog(1, "書き込み禁止 $%06x <- $%02x", addr, data);
                    110:        }
                    111:        return 0;
                    112: }
                    113: 
                    114: uint64
                    115: SRAMDevice::Write16(uint32 addr, uint32 data)
                    116: {
                    117:        if (writeable) {
                    118:                if (addr - baseaddr < 0x100) {
                    119:                        putlog(1, "更新 $%06x <- $%04x", addr, data);
                    120:                }
                    121:                *(uint16 *)&mem[addr - baseaddr] = htobe16(data);
                    122:        } else {
                    123:                putlog(1, "書き込み禁止 $%06x <- $%04x", addr, data);
                    124:        }
                    125:        return 0;
                    126: }
                    127: 
                    128: uint64
                    129: SRAMDevice::Peek8(uint32 addr)
                    130: {
                    131:        return mem[addr - baseaddr];
                    132: }
                    133: 
                    134: void
                    135: SRAMDevice::write_enable(bool value)
                    136: {
                    137:        if (writeable == false && value == true) {
                    138:                putlog(1, "書き込み許可");
                    139:        } else if (writeable == true && value == false) {
                    140:                putlog(1, "書き込み禁止");
                    141:        }
                    142: 
                    143:        writeable = value;
                    144: }

unix.superglobalmegacorp.com

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