|
|
1.1 ! root 1: // ! 2: // nono ! 3: // Copyright (C) 2023 nono project ! 4: // Licensed under nono-license.txt ! 5: // ! 6: ! 7: // 指定のプログラムを XP 側で走らせ、送られてくる結果を表示する。 ! 8: // このプログラム自身は m88k でもビルドするため C99 くらいで書くこと。 ! 9: ! 10: // XP 側から文字の出力は RINGADDR から 256 バイトの循環バッファを使う。 ! 11: // RINGADDR としてここでは 7F00H を使う。実際にはその手前3バイトもワークと ! 12: // して使う。 ! 13: // (RINGADDR - 2) からの2バイトが XP の次の書き込み位置 (下位バイトだけを ! 14: // カウンタとして使ったりもする)、(RINGADDR - 3) からの1バイトがメイン ! 15: // プロセッサ側の次の読み込み位置で、XP 側に対する (TCP の) ACK のようなもの。 ! 16: // ! 17: // 1. XP 側は (RINGADDR + 0) に1文字目を書き込んで (RINGADDR - 2).b の値を ! 18: // 01H に更新。 ! 19: // 2. ホスト側は (RINGADDR - 2) の値が (00H から) 変わったことをポーリングで ! 20: // 把握し、文字を読み出してから ACK として (RINGADDR - 3) を 01H に更新。 ! 21: // 3. カウント 0FFH (メモリでいうと RINGADDR + 0FFH) まで書き込むと次は ! 22: // カウント 00H (メモリでいうと RINGADDR + 0) に戻る。以降繰り返し。 ! 23: // 4. 読み込み位置と書き出し位置をそれぞれ持っている循環バッファでは ! 24: // エンプティとフルの区別がつかないためと、メインプロセッサ側の読み出し ! 25: // 速度が遅い (UNIX プロセスに処理が回ってくる頻度が低い) ため、 ! 26: // カウント 00H と 80H で同期をとる。 ! 27: // 5. '^Z' 文字の出力で正常終了、とする。 ! 28: ! 29: #include <sys/types.h> ! 30: #include <err.h> ! 31: #include <fcntl.h> ! 32: #include <stdio.h> ! 33: #include <stdlib.h> ! 34: #include <string.h> ! 35: #include <unistd.h> ! 36: #include <sys/ioctl.h> ! 37: #include <sys/mman.h> ! 38: #include <sys/stat.h> ! 39: #if defined(__m68k__) || defined(__m88k__) ! 40: #include <machine/xpio.h> ! 41: #else ! 42: // dummy ! 43: #define XPIOCDOWNLD 0 ! 44: struct xp_download { ! 45: int size; ! 46: uint8_t *data; ! 47: }; ! 48: #endif ! 49: ! 50: // 共有メモリ先頭からのオフセット ! 51: #define RINGADDR (0x7f00) ! 52: ! 53: extern int system_datalen; ! 54: extern unsigned char system_data[]; ! 55: ! 56: static struct xp_download make_world(const char *filename); ! 57: static void mainloop(volatile uint8_t *); ! 58: static struct xp_download load_binary(const char *filename); ! 59: ! 60: int ! 61: main(int ac, char *av[]) ! 62: { ! 63: const char *filename; ! 64: struct xp_download data; ! 65: uint8_t *mem; ! 66: int fd; ! 67: int r; ! 68: ! 69: if (ac < 2) { ! 70: errx(1, "usage: <z80-asm object>"); ! 71: } ! 72: filename = av[1]; ! 73: ! 74: // 転送する全体を用意。 ! 75: data = make_world(filename); ! 76: ! 77: fd = open("/dev/xp", O_RDWR); ! 78: if (fd < 0) { ! 79: err(1, "open: /dev/xp"); ! 80: } ! 81: r = ioctl(fd, XPIOCDOWNLD, &data); ! 82: if (r < 0) { ! 83: err(1, "XPIOCDOWNLD"); ! 84: } ! 85: ! 86: mem = mmap(NULL, 65536, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); ! 87: if (mem == MAP_FAILED) { ! 88: err(1, "mmap /dev/xp"); ! 89: } ! 90: ! 91: mainloop((volatile uint8_t *)mem); ! 92: ! 93: munmap(mem, 65536); ! 94: close(fd); ! 95: return 0; ! 96: } ! 97: ! 98: // 転送するプログラム全体を用意。 ! 99: // 0000H〜 リセットベクタ ! 100: // 0100H〜 ユーザプログラム ! 101: // 7000H〜 OS に相当する部分 ! 102: static struct xp_download ! 103: make_world(const char *filename) ! 104: { ! 105: struct xp_download user; ! 106: struct xp_download rv; ! 107: int len; ! 108: ! 109: rv.size = 0x8000; ! 110: rv.data = calloc(rv.size, 1); ! 111: if (rv.data == NULL) { ! 112: err(1, "malloc failed"); ! 113: } ! 114: ! 115: // プログラムをロードして 0100H からにコピー ! 116: user = load_binary(filename); ! 117: len = 0x7000 - 0x100; ! 118: if (user.size < len) { ! 119: len = user.size; ! 120: } ! 121: memcpy(rv.data + 0x100, user.data, len); ! 122: ! 123: // OS 領域をコピー ! 124: memcpy(rv.data + 0x7000, system_data, system_datalen); ! 125: ! 126: // リセット時に最初に実行されるジャンプ命令を用意。 ! 127: rv.data[0x00] = 0xc3; ! 128: rv.data[0x01] = 0x00; ! 129: rv.data[0x02] = 0x70; ! 130: ! 131: return rv; ! 132: } ! 133: ! 134: static void ! 135: mainloop(volatile uint8_t *mem) ! 136: { ! 137: uint8_t cur; ! 138: ! 139: cur = 0x00; ! 140: for (;;) { ! 141: // XP 側の書き込みカーソルは (RINGADDR - 2) からリトル ! 142: // エンディアンでワードだけど実際には1バイト変数なので、 ! 143: // バイトで読み込む。 ! 144: uint8_t newcur = mem[RINGADDR - 2]; ! 145: if (newcur == cur) { ! 146: usleep(10); ! 147: continue; ! 148: } ! 149: ! 150: for (; cur != newcur;) { ! 151: uint8_t ch = mem[RINGADDR + cur]; ! 152: if (ch == '\x1a') { ! 153: return; ! 154: } ! 155: printf("%c", ch); ! 156: fflush(stdout); ! 157: ! 158: cur++; ! 159: ! 160: // 読み込んだところまでポインタを進める(同期用) ! 161: mem[(RINGADDR - 3)] = cur; ! 162: } ! 163: } ! 164: } ! 165: ! 166: // filename からバイナリをロードする。サポートしているのは以下の形式。 ! 167: // o MSX-DOS の .COM 形式 ! 168: // o z80-asm が出力する .z80 バイナリ形式 ! 169: static struct xp_download ! 170: load_binary(const char *filename) ! 171: { ! 172: struct xp_download dl; ! 173: struct stat st; ! 174: char header[10]; ! 175: int fd; ! 176: int r; ! 177: ! 178: fd = open(filename, O_RDONLY); ! 179: if (fd < 0) { ! 180: err(1, "open: %s", filename); ! 181: } ! 182: ! 183: r = fstat(fd, &st); ! 184: if (r < 0) { ! 185: err(1, "stat: %s", filename); ! 186: } ! 187: ! 188: r = read(fd, header, sizeof(header)); ! 189: if (r < 0) { ! 190: err(1, "read header"); ! 191: } ! 192: if (r < sizeof(header)) { ! 193: errx(1, "read header: %d: too short", r); ! 194: } ! 195: ! 196: if (strncmp(header, "Z80ASM\x1a\x0a", 8) == 0) { ! 197: // .z80 形式。先頭 10 バイトがヘッダ。 ! 198: dl.size = st.st_size - 10; ! 199: } else { ! 200: // そうでなければ今の所 .COM 形式。ヘッダなしの生バイナリ。 ! 201: dl.size = st.st_size; ! 202: if (lseek(fd, 0, SEEK_SET) < 0) { ! 203: err(1, "lseek(0) failed"); ! 204: } ! 205: } ! 206: ! 207: dl.data = malloc(dl.size); ! 208: if (dl.data == NULL) { ! 209: err(1, "malloc failed"); ! 210: } ! 211: ! 212: r = read(fd, dl.data, dl.size); ! 213: if (r < 0) { ! 214: err(1, "read"); ! 215: } ! 216: if (r < dl.size) { ! 217: errx(1, "read: %d < %d: too short", r, dl.size); ! 218: } ! 219: ! 220: close(fd); ! 221: ! 222: return dl; ! 223: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.