| 1 | /* Simple program to display a binary card-image file in ASCII.\r |
| 2 | * We assume the deck was written with one card per 16-bit word, left-justified,\r |
| 3 | * and written in PC little-endian order\r |
| 4 | *\r |
| 5 | * (C) Copyright 2002, Brian Knittel.\r |
| 6 | * You may freely use this program, but: it offered strictly on an AS-IS, AT YOUR OWN\r |
| 7 | * RISK basis, there is no warranty of fitness for any purpose, and the rest of the\r |
| 8 | * usual yada-yada. Please keep this notice and the copyright in any distributions\r |
| 9 | * or modifications.\r |
| 10 | *\r |
| 11 | * This is not a supported product, but I welcome bug reports and fixes.\r |
| 12 | * Mail to sim@ibm1130.org\r |
| 13 | */\r |
| 14 | \r |
| 15 | #include <stdio.h>\r |
| 16 | #include <stdlib.h>\r |
| 17 | #include "util_io.h"\r |
| 18 | \r |
| 19 | #define TRUE 1\r |
| 20 | #define FALSE 0\r |
| 21 | typedef int BOOL;\r |
| 22 | \r |
| 23 | int hollerith_to_ascii (unsigned short h);\r |
| 24 | void bail (char *msg);\r |
| 25 | void format_coldstart (unsigned short *buf);\r |
| 26 | \r |
| 27 | int main (int argc, char **argv)\r |
| 28 | {\r |
| 29 | FILE *fd;\r |
| 30 | char *fname = NULL, line[82], *arg;\r |
| 31 | BOOL coldstart = FALSE;\r |
| 32 | unsigned short buf[80];\r |
| 33 | int i, lastnb;\r |
| 34 | static char usestr[] =\r |
| 35 | "Usage: viewdeck [-c] deckfile\n"\r |
| 36 | "\n"\r |
| 37 | "-c: convert cold start card to 16-bit format as a C array initializer\n";\r |
| 38 | \r |
| 39 | for (i = 1; i < argc; i++) { // process command line arguments\r |
| 40 | arg = argv[i];\r |
| 41 | \r |
| 42 | if (*arg == '-') {\r |
| 43 | arg++;\r |
| 44 | while (*arg) {\r |
| 45 | switch (*arg++) {\r |
| 46 | case 'c':\r |
| 47 | coldstart = TRUE;\r |
| 48 | break;\r |
| 49 | default:\r |
| 50 | bail(usestr);\r |
| 51 | }\r |
| 52 | }\r |
| 53 | }\r |
| 54 | else if (fname == NULL) // first non-switch arg is file name\r |
| 55 | fname = arg;\r |
| 56 | else\r |
| 57 | bail(usestr); // there can be only one name\r |
| 58 | }\r |
| 59 | \r |
| 60 | if (fname == NULL) // there must be a name\r |
| 61 | bail(usestr);\r |
| 62 | \r |
| 63 | if ((fd = fopen(fname, "rb")) == NULL) {\r |
| 64 | perror(fname);\r |
| 65 | return 1;\r |
| 66 | }\r |
| 67 | \r |
| 68 | while (fxread(buf, sizeof(short), 80, fd) == 80) {\r |
| 69 | if (coldstart) {\r |
| 70 | format_coldstart(buf);\r |
| 71 | break;\r |
| 72 | }\r |
| 73 | \r |
| 74 | lastnb = -1;\r |
| 75 | for (i = 0; i < 80; i++) {\r |
| 76 | line[i] = hollerith_to_ascii(buf[i]);\r |
| 77 | if (line[i] > ' ')\r |
| 78 | lastnb = i;\r |
| 79 | }\r |
| 80 | line[++lastnb] = '\n';\r |
| 81 | line[++lastnb] = '\0';\r |
| 82 | fputs(line, stdout);\r |
| 83 | }\r |
| 84 | \r |
| 85 | if (coldstart) {\r |
| 86 | if (fxread(buf, sizeof(short), 1, fd) == 1)\r |
| 87 | bail("Coldstart deck has more than one card");\r |
| 88 | }\r |
| 89 | \r |
| 90 | fclose(fd);\r |
| 91 | \r |
| 92 | return 0;\r |
| 93 | }\r |
| 94 | \r |
| 95 | void format_coldstart (unsigned short *buf)\r |
| 96 | {\r |
| 97 | int i, nout = 0;\r |
| 98 | unsigned short word;\r |
| 99 | \r |
| 100 | for (i = 0; i < 80; i++) {\r |
| 101 | word = buf[i]; // expand 12-bit card data to 16-bit instruction\r |
| 102 | word = (word & 0xF800) | ((word & 0x0400) ? 0x00C0 : 0x0000) | ((word & 0x03F0) >> 4);\r |
| 103 | \r |
| 104 | if (nout >= 8) {\r |
| 105 | fputs(",\n", stdout);\r |
| 106 | nout = 0;\r |
| 107 | }\r |
| 108 | else if (i > 0)\r |
| 109 | fputs(", ", stdout);\r |
| 110 | \r |
| 111 | printf("0x%04x", word);\r |
| 112 | nout++;\r |
| 113 | }\r |
| 114 | \r |
| 115 | putchar('\n');\r |
| 116 | }\r |
| 117 | \r |
| 118 | typedef struct {\r |
| 119 | unsigned short hollerith;\r |
| 120 | char ascii;\r |
| 121 | } CPCODE;\r |
| 122 | \r |
| 123 | static CPCODE cardcode_029[] =\r |
| 124 | {\r |
| 125 | 0x0000, ' ',\r |
| 126 | 0x8000, '&', // + in 026 Fortran\r |
| 127 | 0x4000, '-',\r |
| 128 | 0x2000, '0',\r |
| 129 | 0x1000, '1',\r |
| 130 | 0x0800, '2',\r |
| 131 | 0x0400, '3',\r |
| 132 | 0x0200, '4',\r |
| 133 | 0x0100, '5',\r |
| 134 | 0x0080, '6',\r |
| 135 | 0x0040, '7',\r |
| 136 | 0x0020, '8',\r |
| 137 | 0x0010, '9',\r |
| 138 | 0x9000, 'A',\r |
| 139 | 0x8800, 'B',\r |
| 140 | 0x8400, 'C',\r |
| 141 | 0x8200, 'D',\r |
| 142 | 0x8100, 'E',\r |
| 143 | 0x8080, 'F',\r |
| 144 | 0x8040, 'G',\r |
| 145 | 0x8020, 'H',\r |
| 146 | 0x8010, 'I',\r |
| 147 | 0x5000, 'J',\r |
| 148 | 0x4800, 'K',\r |
| 149 | 0x4400, 'L',\r |
| 150 | 0x4200, 'M',\r |
| 151 | 0x4100, 'N',\r |
| 152 | 0x4080, 'O',\r |
| 153 | 0x4040, 'P',\r |
| 154 | 0x4020, 'Q',\r |
| 155 | 0x4010, 'R',\r |
| 156 | 0x3000, '/',\r |
| 157 | 0x2800, 'S',\r |
| 158 | 0x2400, 'T',\r |
| 159 | 0x2200, 'U',\r |
| 160 | 0x2100, 'V',\r |
| 161 | 0x2080, 'W',\r |
| 162 | 0x2040, 'X',\r |
| 163 | 0x2020, 'Y',\r |
| 164 | 0x2010, 'Z',\r |
| 165 | 0x0820, ':',\r |
| 166 | 0x0420, '#', // = in 026 Fortran\r |
| 167 | 0x0220, '@', // ' in 026 Fortran\r |
| 168 | 0x0120, '\'',\r |
| 169 | 0x00A0, '=',\r |
| 170 | 0x0060, '"',\r |
| 171 | 0x8820, '\xA2', // cent, in MS-DOS encoding\r |
| 172 | 0x8420, '.',\r |
| 173 | 0x8220, '<', // ) in 026 Fortran\r |
| 174 | 0x8120, '(',\r |
| 175 | 0x80A0, '+',\r |
| 176 | 0x8060, '|',\r |
| 177 | 0x4820, '!',\r |
| 178 | 0x4420, '$',\r |
| 179 | 0x4220, '*',\r |
| 180 | 0x4120, ')',\r |
| 181 | 0x40A0, ';',\r |
| 182 | 0x4060, '\xAC', // not, in MS-DOS encoding\r |
| 183 | 0x2420, ',',\r |
| 184 | 0x2220, '%', // ( in 026 Fortran\r |
| 185 | 0x2120, '_',\r |
| 186 | 0x20A0, '>',\r |
| 187 | 0xB000, 'a',\r |
| 188 | 0xA800, 'b',\r |
| 189 | 0xA400, 'c',\r |
| 190 | 0xA200, 'd',\r |
| 191 | 0xA100, 'e',\r |
| 192 | 0xA080, 'f',\r |
| 193 | 0xA040, 'g',\r |
| 194 | 0xA020, 'h',\r |
| 195 | 0xA010, 'i',\r |
| 196 | 0xD000, 'j',\r |
| 197 | 0xC800, 'k',\r |
| 198 | 0xC400, 'l',\r |
| 199 | 0xC200, 'm',\r |
| 200 | 0xC100, 'n',\r |
| 201 | 0xC080, 'o',\r |
| 202 | 0xC040, 'p',\r |
| 203 | 0xC020, 'q',\r |
| 204 | 0xC010, 'r',\r |
| 205 | 0x6800, 's',\r |
| 206 | 0x6400, 't',\r |
| 207 | 0x6200, 'u',\r |
| 208 | 0x6100, 'v',\r |
| 209 | 0x6080, 'w',\r |
| 210 | 0x6040, 'x',\r |
| 211 | 0x6020, 'y',\r |
| 212 | 0x6010, 'z', // these odd punch codes are used by APL:\r |
| 213 | 0x1010, '\001', // no corresponding ASCII using ^A\r |
| 214 | 0x0810, '\002', // SYN using ^B\r |
| 215 | 0x0410, '\003', // no corresponding ASCII using ^C\r |
| 216 | 0x0210, '\004', // PUNCH ON using ^D\r |
| 217 | 0x0110, '\005', // READER STOP using ^E\r |
| 218 | 0x0090, '\006', // UPPER CASE using ^F\r |
| 219 | 0x0050, '\013', // EOT using ^K\r |
| 220 | 0x0030, '\016', // no corresponding ASCII using ^N\r |
| 221 | 0x1030, '\017', // no corresponding ASCII using ^O\r |
| 222 | 0x0830, '\020', // no corresponding ASCII using ^P\r |
| 223 | };\r |
| 224 | \r |
| 225 | int hollerith_to_ascii (unsigned short h)\r |
| 226 | {\r |
| 227 | int i;\r |
| 228 | \r |
| 229 | h &= 0xFFF0;\r |
| 230 | \r |
| 231 | for (i = 0; i < sizeof(cardcode_029) / sizeof(CPCODE); i++)\r |
| 232 | if (cardcode_029[i].hollerith == h)\r |
| 233 | return cardcode_029[i].ascii;\r |
| 234 | \r |
| 235 | return '?';\r |
| 236 | }\r |
| 237 | \r |
| 238 | void bail (char *msg)\r |
| 239 | {\r |
| 240 | fprintf(stderr, "%s\n", msg);\r |
| 241 | exit(1);\r |
| 242 | }\r |
| 243 | \r |