|
| 1 | +/* Copyright (C) 2025 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 | +/* |
| 17 | + File format to output ODT deposit commands. |
| 18 | +
|
| 19 | + Converts a core image to a series of ODT deposit commands. If there |
| 20 | + is a start address, there will also be a G command at the end. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdlib.h> |
| 25 | +#include <string.h> |
| 26 | + |
| 27 | +#include "dis.h" |
| 28 | +#include "memory.h" |
| 29 | + |
| 30 | +#define LF 012 |
| 31 | +#define CR 015 |
| 32 | +#define MASK 0177777 |
| 33 | +#define NO_ADDRESS -2 |
| 34 | +static int previous_address; |
| 35 | + |
| 36 | +static int quantity, address; |
| 37 | + |
| 38 | +static void |
| 39 | +write_location (FILE *f, struct pdp10_memory *memory, int address) |
| 40 | +{ |
| 41 | + word_t data = get_word_at (memory, address); |
| 42 | + if (data < 0) |
| 43 | + return; |
| 44 | + |
| 45 | + /* First, close the previous location (if there was one). If this |
| 46 | + location follows the previous, close with "^J" to open the next |
| 47 | + location. Otherwise, close with "^M" and open a new location with |
| 48 | + "/". Then enter one word of data, and leave the location open. */ |
| 49 | + |
| 50 | + data &= MASK; |
| 51 | + if (address == previous_address + 1) |
| 52 | + fputc (LF, f); |
| 53 | + else if (previous_address != NO_ADDRESS) |
| 54 | + fputc (CR, f); |
| 55 | + if (address != previous_address + 1) |
| 56 | + fprintf (f, "%06o/", address); |
| 57 | + fprintf (f, "%06llo", data); |
| 58 | + previous_address = address; |
| 59 | +} |
| 60 | + |
| 61 | +static void |
| 62 | +write_odt (FILE *f, struct pdp10_memory *memory) |
| 63 | +{ |
| 64 | + int i; |
| 65 | + |
| 66 | + previous_address = NO_ADDRESS; |
| 67 | + |
| 68 | + /* Memory contents, as deposit commands. */ |
| 69 | + for (i = 0; i <= MASK; i++) |
| 70 | + write_location (f, memory, i); |
| 71 | + |
| 72 | + /* Make sure to close last location. */ |
| 73 | + if (previous_address != NO_ADDRESS) |
| 74 | + fputc (CR, f); |
| 75 | + |
| 76 | + /* Start. */ |
| 77 | + if (start_instruction > 0) |
| 78 | + fprintf (f, "%06lloG", start_instruction & MASK); |
| 79 | +} |
| 80 | + |
| 81 | +static void |
| 82 | +deposit (struct pdp10_memory *memory) |
| 83 | +{ |
| 84 | + word_t *data = malloc (sizeof (word_t)); |
| 85 | + if (data == NULL) |
| 86 | + { |
| 87 | + fprintf (stderr, "Out of memory\n"); |
| 88 | + exit (1); |
| 89 | + } |
| 90 | + *data = quantity; |
| 91 | + add_memory (memory, address, 1, data); |
| 92 | +} |
| 93 | + |
| 94 | +static void |
| 95 | +read_char (char c, struct pdp10_memory *memory) |
| 96 | +{ |
| 97 | + switch (c) |
| 98 | + { |
| 99 | + case '0': case '1': case '2': case '3': |
| 100 | + case '4': case '5': case '6': case '7': |
| 101 | + if (quantity < 0) |
| 102 | + quantity = 0; |
| 103 | + quantity *= 010; |
| 104 | + quantity += c - '0'; |
| 105 | + break; |
| 106 | + case LF: |
| 107 | + case CR: |
| 108 | + if (address == -1) |
| 109 | + break; |
| 110 | + if (quantity < 0) |
| 111 | + quantity = -quantity; |
| 112 | + deposit (memory); |
| 113 | + quantity = 0; |
| 114 | + if (c == LF) |
| 115 | + address++; |
| 116 | + else |
| 117 | + address = -1; |
| 118 | + break; |
| 119 | + case '/': |
| 120 | + address = quantity; |
| 121 | + quantity = get_word_at (memory, address); |
| 122 | + if (quantity == -1) |
| 123 | + quantity = 0; |
| 124 | + else |
| 125 | + quantity = -quantity; |
| 126 | + break; |
| 127 | + case 'G': |
| 128 | + start_instruction = quantity; |
| 129 | + quantity = 0; |
| 130 | + break; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +static void |
| 135 | +read_odt (FILE *f, struct pdp10_memory *memory, int cpu_model) |
| 136 | +{ |
| 137 | + (void)cpu_model; |
| 138 | + |
| 139 | + fprintf (output_file, ";ODT commands\n\n"); |
| 140 | + |
| 141 | + quantity = 0; |
| 142 | + address = -1; |
| 143 | + |
| 144 | + for (;;) |
| 145 | + { |
| 146 | + int c = fgetc (f); |
| 147 | + if (c == EOF) |
| 148 | + return; |
| 149 | + read_char (c, memory); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +struct file_format odt_file_format = { |
| 154 | + "odt", |
| 155 | + read_odt, |
| 156 | + write_odt |
| 157 | +}; |
0 commit comments