|
| 1 | +/* Copyright (C) 2021 Lars Brinkhoff <lars@nocrew.org> |
| 2 | +
|
| 3 | + This program is free software: you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation, either version 2 of the License, or |
| 6 | + (at your option) any later version. |
| 7 | +
|
| 8 | + This program is distributed in the hope that it will be useful, |
| 9 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + GNU General Public License for more details. |
| 12 | +
|
| 13 | + You should have received a copy of the GNU General Public License |
| 14 | + along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 15 | + |
| 16 | +#include <stdio.h> |
| 17 | + |
| 18 | +#include "dis.h" |
| 19 | +#include "memory.h" |
| 20 | + |
| 21 | +/* Imlac papertape boostrap loader. */ |
| 22 | +static char loader[124] = |
| 23 | + "\002\032\027\340\047\277\077\360\204\001\027\303\200\006\047\377" |
| 24 | + "\077\350\047\376\177\330\027\315\000\000\077\350\247\376\077\331" |
| 25 | + "\067\376\067\377\027\315\077\350\167\277\204\001\027\346\000\000" |
| 26 | + "\377\377\037\320\200\010\157\277\004\004\200\004\047\277\227\331" |
| 27 | + "\002\061\147\374\047\361\012\032\377\375\047\361\200\011\027\302" |
| 28 | + "\037\311\200\011\077\360\006\003\006\003\006\002\077\360\227\350" |
| 29 | + "\037\357\002\032\205\000\027\362\005\000\027\364\002\051\227\360" |
| 30 | + "\004\040\027\370\002\033\227\360\027\370\000\044"; |
| 31 | + |
| 32 | +static int checksum = 0; |
| 33 | + |
| 34 | +static void |
| 35 | +update_sum (int word) |
| 36 | +{ |
| 37 | + checksum += word; |
| 38 | + if (checksum & 0200000) |
| 39 | + { |
| 40 | + checksum++; |
| 41 | + checksum &= 0177777; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +static int |
| 46 | +get_8 (FILE *f) |
| 47 | +{ |
| 48 | + int c = fgetc (f); |
| 49 | + if (c == EOF) |
| 50 | + { |
| 51 | + fprintf (stderr, "Unexpected end of input file.\n"); |
| 52 | + exit (1); |
| 53 | + } |
| 54 | + return c; |
| 55 | +} |
| 56 | + |
| 57 | +static int |
| 58 | +get_8_not_0 (FILE *f) |
| 59 | +{ |
| 60 | + for (;;) |
| 61 | + { |
| 62 | + int c = get_8 (f); |
| 63 | + if (c != 0) |
| 64 | + return c; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static int |
| 69 | +get_16 (FILE *f) |
| 70 | +{ |
| 71 | + int data = (get_8 (f) << 8) | get_8 (f); |
| 72 | + update_sum (data); |
| 73 | + return data; |
| 74 | +} |
| 75 | + |
| 76 | +static void |
| 77 | +read_imlac_pt (FILE *f, struct pdp10_memory *memory, int cpu_model) |
| 78 | +{ |
| 79 | + int length, address, data; |
| 80 | + word_t *core; |
| 81 | + int c, i; |
| 82 | + |
| 83 | + (void)cpu_model; |
| 84 | + start_instruction = 0; |
| 85 | + |
| 86 | + /* Discard block loader. */ |
| 87 | + for (c = 0; c != 2;) |
| 88 | + c = get_8_not_0 (f); |
| 89 | + for (i = 0; i < 123; i++) |
| 90 | + c = get_8 (f); |
| 91 | + |
| 92 | + for (;;) |
| 93 | + { |
| 94 | + length = get_8_not_0 (f); |
| 95 | + address = get_16 (f); |
| 96 | + |
| 97 | + /* All ones signals end of data. */ |
| 98 | + if (address == 0177777) |
| 99 | + { |
| 100 | + /* Search for a start address at 037714 modulo 4K. */ |
| 101 | + address = 037714; |
| 102 | + while (address >= 07714) |
| 103 | + { |
| 104 | + start_instruction = get_word_at (memory, address); |
| 105 | + if (start_instruction == -1) |
| 106 | + start_instruction = 0; |
| 107 | + else |
| 108 | + break; |
| 109 | + address -= 4096; |
| 110 | + } |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + core = malloc (length * sizeof (word_t)); |
| 115 | + if (core == NULL) |
| 116 | + { |
| 117 | + fprintf (stderr, "Out of memory.\n"); |
| 118 | + exit (1); |
| 119 | + } |
| 120 | + |
| 121 | + checksum = 0; |
| 122 | + for (i = address; i < address + length; i++) |
| 123 | + core[i] = get_16 (f); |
| 124 | + add_memory (memory, address, length, core); |
| 125 | + |
| 126 | + data = checksum; |
| 127 | + if (data != get_16 (f)) |
| 128 | + fprintf (stderr, "Bad checksum: %04X.\n", data); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +static void |
| 133 | +out_8 (FILE *f, int word) |
| 134 | +{ |
| 135 | + fputc (word & 0377, f); |
| 136 | +} |
| 137 | + |
| 138 | +static void |
| 139 | +out_16 (FILE *f, int word) |
| 140 | +{ |
| 141 | + update_sum (word); |
| 142 | + out_8 (f, word >> 8); |
| 143 | + out_8 (f, word); |
| 144 | +} |
| 145 | + |
| 146 | +static void |
| 147 | +blank (FILE *f, int n) |
| 148 | +{ |
| 149 | + int i; |
| 150 | + for (i = 0; i < n; i++) |
| 151 | + fputc (0, f); |
| 152 | +} |
| 153 | + |
| 154 | +static void |
| 155 | +write_imlac_pt (FILE *f, struct pdp10_memory *memory) |
| 156 | +{ |
| 157 | + int start, length; |
| 158 | + int i, j; |
| 159 | + |
| 160 | + blank (f, 32); |
| 161 | + |
| 162 | + fprintf (stderr, "Loader: %lu\n", sizeof loader); |
| 163 | + for (i = 0; i < (int)sizeof loader; i++) |
| 164 | + fputc (loader[i], f); |
| 165 | + |
| 166 | + for (i = 0; i < memory->areas; i++) |
| 167 | + { |
| 168 | + start = memory->area[i].start; |
| 169 | + length = memory->area[i].end - start; |
| 170 | + fprintf (stderr, "Area: %04o %06o\n", start, length); |
| 171 | + |
| 172 | + while (length > 0) |
| 173 | + { |
| 174 | + int n = length; |
| 175 | + if (n > 0377) { |
| 176 | + if (n < 0777) |
| 177 | + n /= 2; |
| 178 | + else |
| 179 | + n = 0377; |
| 180 | + } |
| 181 | + length -= n; |
| 182 | + blank (f, 10); |
| 183 | + out_8 (f, n); |
| 184 | + out_16 (f, start); |
| 185 | + fprintf (stderr, "PT: %04o %06o\n", n, start); |
| 186 | + checksum = 0; |
| 187 | + for (j = 0; j < n; j++) |
| 188 | + out_16 (f, get_word_at (memory, start + j)); |
| 189 | + out_16 (f, checksum); |
| 190 | + start += n; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + if (start_instruction != 0) |
| 195 | + { |
| 196 | + blank (f, 10); |
| 197 | + out_8 (f, 2); |
| 198 | + out_16 (f, 037713); |
| 199 | + checksum = 0; |
| 200 | + out_16 (f, 0113714); |
| 201 | + out_16 (f, start_instruction & 037777); |
| 202 | + out_16 (f, checksum); |
| 203 | + } |
| 204 | + |
| 205 | + blank (f, 10); |
| 206 | + out_8 (f, 0377); |
| 207 | + out_16 (f, 0177777); |
| 208 | + blank (f, 10); |
| 209 | +} |
| 210 | + |
| 211 | +struct file_format imlac_pt_file_format = { |
| 212 | + "imlac-pt", |
| 213 | + read_imlac_pt, |
| 214 | + write_imlac_pt |
| 215 | +}; |
0 commit comments