Annotation of nono/util/runcom/runcom.c, revision 1.1.1.2

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>
1.1.1.2 ! root       31: #include <errno.h>
1.1       root       32: #include <fcntl.h>
                     33: #include <stdio.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
                     36: #include <unistd.h>
                     37: #include <sys/ioctl.h>
                     38: #include <sys/mman.h>
                     39: #include <sys/stat.h>
                     40: #if defined(__m68k__) || defined(__m88k__)
                     41: #include <machine/xpio.h>
                     42: #else
                     43: // dummy
                     44: #define XPIOCDOWNLD 0
                     45: struct xp_download {
                     46:        int size;
                     47:        uint8_t *data;
                     48: };
                     49: #endif
                     50: 
                     51: // 共有メモリ先頭からのオフセット
                     52: #define RINGADDR (0x7f00)
                     53: 
1.1.1.2 ! root       54: // ユーザプログラムの最大サイズ
        !            55: #define MAX_PROGSIZE   (0x7000 - 0x100)
        !            56: 
1.1       root       57: extern int system_datalen;
                     58: extern unsigned char system_data[];
                     59: 
                     60: static struct xp_download make_world(const char *filename);
                     61: static void mainloop(volatile uint8_t *);
                     62: static struct xp_download load_binary(const char *filename);
                     63: 
                     64: int
                     65: main(int ac, char *av[])
                     66: {
                     67:        const char *filename;
                     68:        struct xp_download data;
                     69:        uint8_t *mem;
                     70:        int fd;
                     71:        int r;
                     72: 
                     73:        if (ac < 2) {
                     74:                errx(1, "usage: <z80-asm object>");
                     75:        }
                     76:        filename = av[1];
                     77: 
                     78:        // 転送する全体を用意。
                     79:        data = make_world(filename);
                     80: 
                     81:        fd = open("/dev/xp", O_RDWR);
                     82:        if (fd < 0) {
                     83:                err(1, "open: /dev/xp");
                     84:        }
                     85:        r = ioctl(fd, XPIOCDOWNLD, &data);
                     86:        if (r < 0) {
                     87:                err(1, "XPIOCDOWNLD");
                     88:        }
                     89: 
                     90:        mem = mmap(NULL, 65536, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
                     91:        if (mem == MAP_FAILED) {
                     92:                err(1, "mmap /dev/xp");
                     93:        }
                     94: 
                     95:        mainloop((volatile uint8_t *)mem);
                     96: 
                     97:        munmap(mem, 65536);
                     98:        close(fd);
                     99:        return 0;
                    100: }
                    101: 
                    102: // 転送するプログラム全体を用意。
                    103: // 0000H〜 リセットベクタ
                    104: // 0100H〜 ユーザプログラム
                    105: // 7000H〜 OS に相当する部分
                    106: static struct xp_download
                    107: make_world(const char *filename)
                    108: {
                    109:        struct xp_download user;
                    110:        struct xp_download rv;
                    111:        int len;
                    112: 
                    113:        rv.size = 0x8000;
                    114:        rv.data = calloc(rv.size, 1);
                    115:        if (rv.data == NULL) {
                    116:                err(1, "malloc failed");
                    117:        }
                    118: 
                    119:        // プログラムをロードして 0100H からにコピー
                    120:        user = load_binary(filename);
1.1.1.2 ! root      121:        memcpy(rv.data + 0x100, user.data, user.size);
1.1       root      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:        }
1.1.1.2 ! root      206:        if (dl.size > MAX_PROGSIZE) {
        !           207:                errno = EFBIG;
        !           208:                err(1, "%s", filename);
        !           209:        }
1.1       root      210: 
                    211:        dl.data = malloc(dl.size);
                    212:        if (dl.data == NULL) {
                    213:                err(1, "malloc failed");
                    214:        }
                    215: 
                    216:        r = read(fd, dl.data, dl.size);
                    217:        if (r < 0) {
                    218:                err(1, "read");
                    219:        }
                    220:        if (r < dl.size) {
                    221:                errx(1, "read: %d < %d: too short", r, dl.size);
                    222:        }
                    223: 
                    224:        close(fd);
                    225: 
                    226:        return dl;
                    227: }

unix.superglobalmegacorp.com

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