|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2023 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // MSX-DOS コマンドラインエミュレータ
9: //
10:
11: #include "msxdos.h"
12: #include "autofd.h"
13: #include "human68k.h"
14: #include "iodevstream.h"
15: #include "mainapp.h"
16: #include "memorystream.h"
17: #include "mpu64180.h"
18: #include "subram.h"
19: #include <fcntl.h>
20: #include <string.h>
21: #include <unistd.h>
22: #include <sys/stat.h>
23:
24: static void msxdos_callback(void *arg);
25:
26: // コンストラクタ
27: MSXDOSDevice::MSXDOSDevice()
28: : inherited()
29: {
30: // 大きさは適当
31: uint devlen = 128 * 1024;
32: imagebuf.reset(new uint8 [devlen]);
33: memset(&imagebuf[0], 0, devlen);
34: mem = &imagebuf[0];
35:
36: mask = devlen - 1;
37: }
38:
39: // デストラクタ
40: MSXDOSDevice::~MSXDOSDevice()
41: {
42: }
43:
44: // 初期化
45: bool
46: MSXDOSDevice::Init()
47: {
48: if (inherited::Init() == false) {
49: return false;
50: }
51:
52: xp = GetMPU64180Device();
53:
54: // システムコール処理のコールバックを設定
55: xp->SetSYSCALLCallback(msxdos_callback, this);
56:
57: //
58: // ROM っぽいものを用意。
59: //
60: MemoryStreamBE ms(imagebuf.get());
61: ms.SetOffset(0);
1.1.1.4 ! root 62: ms.Write4(0x00002000); // +00: リセットベクタ SP
! 63: ms.Write4(0x41000402); // +04: リセットベクタ PC
1.1 root 64: // 全ベクタを RTE に向ける
65: for (int i = 8; i < 0x400; i += 4) {
1.1.1.4 ! root 66: ms.Write4(0x41000000);
1.1 root 67: }
68: ms.SetOffset(0x400); //all_handler:
1.1.1.4 ! root 69: ms.Write2(0x4e73); // rte
1.1 root 70: // ここからリセット時に実行する命令
1.1.1.4 ! root 71: ms.Write2(0x41f9); // lea.l $49000000,a0
! 72: ms.Write4(0x49000000);
! 73: ms.Write2(0x117c); // move.b #$b6,3(a0) ; SetMode
! 74: ms.Write4(0x00b60003);
! 75: ms.Write2(0x117c); // move.b #$0f,3(a0) ; XPRESET <- %1
! 76: ms.Write4(0x000f0003);
! 77: ms.Write2(0x117c); // move.b #$0e,3(a0) ; XPRESET <- %0
! 78: ms.Write4(0x000e0003);
! 79: ms.Write4(0x4e722700); //@:stop #0x2700
! 80: ms.Write2(0x60fa); // bra @b
1.1 root 81:
82: //
83: // SubRAM に XP 用ファームウェアを書き込む。
84: // (このモードではこの後の電源オンで SubRAM を初期化しないようになってる)
85: //
86: auto subram = GetSubRAMDevice();
87: IODeviceStream xs(subram);
88: const uint16 _doscall = 0x7000;
89: const uint16 _term = 0x7010;
90: const uint16 _trap_handler = 0x7020;
91: const uint16 _rominit = 0x7030;
92: const uint16 _msg_trap = 0x7050;
93:
94: // リセットベクタ
1.1.1.4 ! root 95: xs.SetOffset(0x0000);
! 96: xs.Write1(0xc3); // 0000: JP _rominit
! 97: xs.Write2LE(_rominit);
! 98: xs.Write2(0x0000); // 0003: 00,00
! 99: xs.Write1(0xc3); // 0005: JP _doscall
! 100: xs.Write2LE(_doscall);
1.1 root 101:
102: // ここから ROM。
103: // CP/M は 0005H の飛び先がワークの先頭らしく、ここより前を
104: // SP として使っているらしいので、先頭に DOSCALL ハンドラを用意。
105:
106: // DOSCALL ハンドラ。
1.1.1.4 ! root 107: xs.SetOffset(_doscall);
! 108: xs.Write1(0x79); // 7000: LD A,C
! 109: xs.Write1(0xb7); // 7001: OR A
! 110: xs.Write1(0xca); // 7002: JP Z,_term + 2
! 111: xs.Write2LE(_term + 2);
! 112: xs.Write2(0xedff); // 7005: SYSCALL
! 113: xs.Write1(0xc9); // 7007: RET
1.1 root 114:
115: // TERM
1.1.1.4 ! root 116: xs.SetOffset(_term);
! 117: xs.Write2(0x0e00); // 7010: LD C,00H
! 118: xs.Write2(0xedff); // 7012: SYSCALL
! 119: xs.Write1(0x76); // 7014: HALT
! 120: xs.Write2(0x18fd); // 7015: JR 7014H ; 念のため
1.1 root 121:
122: // トラップハンドラ。メッセージを出して終了。
1.1.1.4 ! root 123: xs.SetOffset(_trap_handler);
! 124: xs.Write1(0x11); // 7020: LD DE,msg_trap
! 125: xs.Write2LE(_msg_trap);
! 126: xs.Write2(0x0e09); // 7023: LD C,09H
! 127: xs.Write2(0xedff); // 7025: SYSCALL
! 128: xs.Write1(0xc3); // 7026: JP _term
! 129: xs.Write2LE(_term);
1.1 root 130:
131: // XP 側の初期化ルーチン。
132: // 0000H 番地をトラップハンドラに向け変えて、
133: // 0100H にジャンプ。戻ってきたら終了。
1.1.1.4 ! root 134: xs.SetOffset(_rominit);
! 135: xs.Write2(0x3e3c); // 7030: LD A,3CH
! 136: xs.Write2(0xed39); // 7032: OUT0 (36H),A
! 137: xs.Write1(0x36);
! 138: xs.Write1(0x21); // 7035: LD HL,_trap_handler
! 139: xs.Write2LE(_trap_handler);
! 140: xs.Write1(0x22); // 7038: LD (0001H),HL
! 141: xs.Write2LE(0x0001);
! 142: xs.Write1(0xcd); // 703b: CALL 0100H
! 143: xs.Write2LE(0x0100);
! 144: xs.Write1(0xc3); // 703e: JP _term
! 145: xs.Write2LE(_term);
1.1 root 146:
147: // msg_trap
1.1.1.4 ! root 148: xs.SetOffset(_msg_trap);
1.1 root 149: xs.WriteString("trap occured!\x0d\x0a$");
150:
151: // 実行ファイルオープン
1.1.1.3 root 152: std::vector<uint8> src;
153: if (LoadBinary(gMainApp.exec_file, src) == false) {
1.1 root 154: return false;
155: }
156:
157: // コピー
1.1.1.4 ! root 158: xs.SetOffset(0x100);
1.1.1.3 root 159: for (auto s : src) {
1.1.1.4 ! root 160: xs.Write1(s);
1.1 root 161: }
162:
163: return true;
164: }
165:
1.1.1.3 root 166: // filename で示されるファイルをロードする。
167: // 成功すれば data に格納して true を返す。
168: // 失敗すればエラーメッセージを表示し、false を返す。
1.1 root 169: // サポートしているのは
170: // o z80-asm の出力する .z80 形式
171: // o MSX-DOS の .COM 形式 (生バイナリ)
1.1.1.3 root 172: bool
173: MSXDOSDevice::LoadBinary(const char *filename, std::vector<uint8>& dst)
1.1 root 174: {
175: struct stat st;
1.1.1.3 root 176: size_t dlsize;
1.1 root 177: char header[10];
178: autofd fd;
179: int r;
180:
181: fd = open(filename, O_RDONLY);
182: if (fd < 0) {
183: warn("\"%s\" open failed", filename);
1.1.1.3 root 184: return false;
1.1 root 185: }
186:
187: r = fstat(fd, &st);
188: if (r < 0) {
189: warn("\"%s\" fstat failed", filename);
1.1.1.3 root 190: return false;
1.1 root 191: }
192:
193: r = read(fd, header, sizeof(header));
194: if (r < 0) {
195: warn("\"%s\" read(header) failed", filename);
1.1.1.3 root 196: return false;
1.1 root 197: }
198: if (r < sizeof(header)) {
199: warnx("\"%s\" read(header): %d: too short", filename, r);
200: }
201:
202: if (strncmp(header, "Z80ASM\x1a\x0a", 8) == 0) {
203: // .z80 形式。先頭 10 バイトがヘッダ。
1.1.1.3 root 204: dlsize = st.st_size - 10;
1.1 root 205: } else {
206: // そうでなければ今の所 .COM 形式。ヘッダなしの生バイナリ。
1.1.1.3 root 207: dlsize = st.st_size;
1.1 root 208: if (lseek(fd, 0, SEEK_SET) < 0) {
209: warn("\"%s\" lseek(0) failed", filename);
1.1.1.3 root 210: return false;
1.1 root 211: }
212: }
213:
1.1.1.2 root 214: // サイズ制限
1.1.1.3 root 215: if (dlsize >= 0x7000 - 0x100) {
1.1.1.2 root 216: errno = EFBIG;
217: warn("%s", filename);
1.1.1.3 root 218: return false;
1.1 root 219: }
220:
1.1.1.3 root 221: dst.resize(dlsize);
222: r = read(fd, dst.data(), dst.size());
1.1 root 223: if (r < 0) {
224: warn("\%s\" read failed", filename);
1.1.1.3 root 225: return false;
1.1 root 226: }
1.1.1.3 root 227: if (r < dlsize) {
1.1 root 228: warn("\%s\" read: %d: too short", filename, r);
1.1.1.3 root 229: return false;
1.1 root 230: }
231:
1.1.1.3 root 232: return true;
1.1 root 233: }
234:
235: // コールバック入口のグローバル関数
236: void
237: msxdos_callback(void *arg)
238: {
1.1.1.4 ! root 239: auto *msxdosdev = reinterpret_cast<MSXDOSDevice *>(arg);
1.1 root 240: msxdosdev->Syscall();
241: }
242:
243: // DOS コールの処理
244: void
245: MSXDOSDevice::Syscall()
246: {
247: switch (xp->reg.c) {
248: case 0x00: // _TERM
249: putmsg(1, "DOS _TERM");
1.1.1.3 root 250: Human68kDevice::RequestExit(0);
1.1 root 251: break;
252:
253: case 0x02: // _PUTC
254: putchar(xp->reg.e);
255: fflush(stdout);
256: break;
257:
258: case 0x09: // _PUTS
259: {
260: uint16 de = xp->reg.GetDE();
261: uint8 ch;
1.1.1.4 ! root 262: for (; (ch = xp->Read1(de)) != '$'; de++) {
1.1 root 263: putchar(ch);
264: }
265: fflush(stdout);
266: break;
267: }
268:
269: default:
270: putmsg(0, "Syscall: unsupported DOSCALL C=%02XH", xp->reg.c);
271: break;
272: }
273: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.