|
|
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>
10:
11: //
12: // SRAM
13: //
14: // SRAM はビッグエンディアンのままメモリに置かれるので、
15: // 必要ならアクセス時に都度変換すること。
16: //
17:
18: SRAMDevice *gSRAM;
19:
20: SRAMDevice::SRAMDevice()
21: {
22: logname = "sram";
23: devname = "SRAM";
24: devaddr = baseaddr;
25: devlen = 0x10000; // 通常実際は16KBだが64KB分占有しておく
26:
27: fd = -1;
28: }
29:
30: SRAMDevice::~SRAMDevice()
31: {
32: if (mem) {
33: munmap(mem, memlen);
34: mem = NULL;
35: memlen = 0;
36: }
37: if (fd >= 0) {
38: close(fd);
39: fd = -1;
40: }
41: }
42:
43: bool
44: SRAMDevice::Init()
45: {
46: filename = gConfig->GetConfigDir() + "SRAM030.DAT";
47:
48: fd = open(filename.c_str(), O_RDWR);
49: if (fd == -1) {
50: warn("%s: cannot open %s", devname, filename.c_str());
51: return false;
52: }
53:
54: memlen = 16 * 1024;
55: mem = (uint8 *)mmap(NULL, memlen, PROT_READ | PROT_WRITE,
56: MAP_SHARED, fd, 0);
57: if (mem == MAP_FAILED) {
58: warn("%s: cannot mmap %s", devname, filename.c_str());
59: close(fd);
60: return false;
61: }
62:
63: return true;
64: }
65:
66: uint64
67: SRAMDevice::Read8(uint32 addr)
68: {
69: return mem[addr - baseaddr];
70: }
71:
72: uint64
73: SRAMDevice::Read16(uint32 addr)
74: {
75: return be16toh(*(uint16 *)&mem[addr - baseaddr]);
76: }
77:
78: uint64
79: SRAMDevice::Write8(uint32 addr, uint32 data)
80: {
81: if (writeable) {
82: if (addr - baseaddr < 0x100) {
83: putlog(1, "更新 $%06x <- $%02x", addr, data);
84: }
85: mem[addr - baseaddr] = data;
86: } else {
87: putlog(1, "書き込み禁止 $%06x <- $%02x", addr, data);
88: }
89: return 0;
90: }
91:
92: uint64
93: SRAMDevice::Write16(uint32 addr, uint32 data)
94: {
95: if (writeable) {
96: if (addr - baseaddr < 0x100) {
97: putlog(1, "更新 $%06x <- $%04x", addr, data);
98: }
99: *(uint16 *)&mem[addr - baseaddr] = htobe16(data);
100: } else {
101: putlog(1, "書き込み禁止 $%06x <- $%04x", addr, data);
102: }
103: return 0;
104: }
105:
106: uint64
107: SRAMDevice::Peek8(uint32 addr)
108: {
109: return mem[addr - baseaddr];
110: }
111:
112: void
113: SRAMDevice::write_enable(bool value)
114: {
115: if (writeable == false && value == true) {
116: putlog(1, "書き込み許可");
117: } else if (writeable == true && value == false) {
118: putlog(1, "書き込み禁止");
119: }
120:
121: writeable = value;
122: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.