# HG changeset patch # User Mychaela Falconia # Date 1727235196 0 # Node ID 4681ad8483d69482343018c6b5b1f2e603c045d0 # Parent 4189abdfeaa47f49c9a40b363806ff3fa2de106a d144: edata-input-compile program written diff -r 4189abdfeaa4 -r 4681ad8483d6 .hgignore --- a/.hgignore Wed Sep 25 02:36:08 2024 +0000 +++ b/.hgignore Wed Sep 25 03:33:16 2024 +0000 @@ -2,6 +2,8 @@ \.[oa]$ +^d144/edata-input-compile$ + ^tfo/find-is-hdr$ ^tfo/tfo-trace-msg$ diff -r 4189abdfeaa4 -r 4681ad8483d6 d144/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/d144/Makefile Wed Sep 25 03:33:16 2024 +0000 @@ -0,0 +1,11 @@ +CC= gcc +CFLAGS= -O2 +PROGS= edata-input-compile + +all: ${PROGS} + +edata-input-compile: edata-input-compile.c + ${CC} ${CFLAGS} -o $@ $@.c + +clean: + rm -f *.o ${PROGS} diff -r 4189abdfeaa4 -r 4681ad8483d6 d144/edata-input-compile.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/d144/edata-input-compile.c Wed Sep 25 03:33:16 2024 +0000 @@ -0,0 +1,210 @@ +/* + * This program compiles E-data input from our handcrafting format + * into the binary format that will be read by itt-ater-16. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define MAX_FIELDS 8 + +static char *infname, *outfname; +static FILE *inf, *outf; + +static int lineno; +static char linebuf[256]; +static char *fields[MAX_FIELDS]; +static unsigned nfields; + +static uint8_t frame_nibbles[72]; +static unsigned subframe_count; + +static int +get_line() +{ + char *cp; + + if (!fgets(linebuf, sizeof linebuf, inf)) + return 1; + lineno++; + if (!index(linebuf, '\n')) { + fprintf(stderr, "%s line %d: too long or missing newline\n", + infname, lineno); + exit(1); + } + nfields = 0; + for (cp = linebuf; ; ) { + while (isspace(*cp)) + cp++; + if (*cp == '\0' || *cp == '#') + break; + if (nfields >= MAX_FIELDS) { + fprintf(stderr, "%s line %d: too many fields\n", + infname, lineno); + exit(1); + } + fields[nfields++] = cp; + while (*cp && !isspace(*cp)) + cp++; + if (*cp) + *cp++ = '\0'; + } + return 0; +} + +static int +get_line_nonempty() +{ + int rc; + + for (;;) { + rc = get_line(); + if (rc) + return rc; + if (nfields) + return 0; + } +} + +static int +decode_hex_digit(c) +{ + if (isdigit(c)) + return c - '0'; + else if (isupper(c)) + return c - 'A' + 10; + else + return c - 'a' + 10; +} + +static void +bits_line() +{ + uint8_t *op; + char *cp; + unsigned n; + + if (nfields != 2) { + fprintf(stderr, "%s line %d: bits command takes one argument\n", + infname, lineno); + exit(1); + } + if (strlen(fields[1]) != 9) { + fprintf(stderr, "%s line %d: bits argument has wrong length\n", + infname, lineno); + exit(1); + } + if (subframe_count >= 8) { + fprintf(stderr, + "%s line %d: 8 subframes already given, can't have more\n", + infname, lineno); + exit(1); + } + op = frame_nibbles + subframe_count * 9; + cp = fields[1]; + for (n = 0; n < 9; n++) { + if (!isxdigit(*cp)) { + fprintf(stderr, + "%s line %d: bits argument is not valid hex\n", + infname, lineno); + exit(1); + } + *op++ = decode_hex_digit(*cp++); + } + subframe_count++; +} + +static void +frame_line() +{ + uint8_t bin[38], m_bits[2]; + unsigned n, i; + + if (nfields != 2) { + fprintf(stderr, + "%s line %d: frame command takes one argument\n", + infname, lineno); + exit(1); + } + for (n = 0; n < 2; n++) { + switch (fields[1][n]) { + case '0': + m_bits[n] = 0; + break; + case '1': + m_bits[n] = 1; + break; + default: + bad_arg: + fprintf(stderr, + "%s line %d: invalid M-bits argument \"%s\"\n", + infname, lineno, fields[1]); + exit(1); + } + } + if (fields[1][2]) + goto bad_arg; + + if (subframe_count != 8) { + fprintf(stderr, + "%s line %d: not preceded by exactly 8 subframes\n", + infname, lineno); + exit(1); + } + bin[0] = 0xD4; + bin[1] = (m_bits[0] << 1) | m_bits[1]; + i = 0; + for (n = 0; n < 36; n++) { + bin[n+2] = (frame_nibbles[i] << 4) | frame_nibbles[i+1]; + i += 2; + } + fwrite(bin, 1, 38, outf); + + subframe_count = 0; +} + +main(argc, argv) + char **argv; +{ + int rc; + + if (argc != 3) { + fprintf(stderr, "usage: %s input.src output.bin\n", argv[0]); + exit(1); + } + infname = argv[1]; + outfname = argv[2]; + + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + outf = fopen(outfname, "w"); + if (!outf) { + perror(outfname); + exit(1); + } + + for (;;) { + rc = get_line_nonempty(); + if (rc) + break; + if (!strcasecmp(fields[0], "bits")) + bits_line(); + else if (!strcasecmp(fields[0], "frame")) + frame_line(); + else { + fprintf(stderr, + "%s line %d: non-understood keyword \"%s\"\n", + infname, lineno, fields[0]); + exit(1); + } + } + exit(0); +}