-
Notifications
You must be signed in to change notification settings - Fork 13
/
simh-file.c
171 lines (146 loc) · 3.73 KB
/
simh-file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Copyright (C) 2024 Lars Brinkhoff <lars@nocrew.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/*
File format to output SIMH deposit commands.
Converts a core image to file to a series of SIMH deposit commands.
If there is a start address, there will also be a GO command at the end.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dis.h"
#include "memory.h"
static void
write_location (FILE *f, struct pdp10_memory *memory, int address)
{
word_t data = get_word_at (memory, address);
if (data >= 0)
{
data &= 0777777777777LL;
fprintf (f, "d %06o %012llo\n", address, data);
}
}
static void
write_simh (FILE *f, struct pdp10_memory *memory)
{
int i;
/* Memory contents, as deposit commands. */
for (i = 0; i <= 0777777; i++)
write_location (f, memory, i);
/* Start. */
if (start_instruction <= 0)
;
else if (((start_instruction & 0777000000000LL) == JRST) ||
start_instruction <= 0777777)
fprintf (f, "go %06llo\n", start_instruction & 0777777);
else
fprintf (f, ";execute %012llo\n", start_instruction);
}
static int
whitespace(char c)
{
switch (c)
{
case ' ':
case '\t':
case '\r':
case '\n':
return 1;
default:
return 0;
}
}
static int
whitespace_or_nul(char c)
{
return whitespace (c) || c == 0;
}
static void
fatal (char *format, char *line)
{
fprintf (stderr, format, line);
exit (1);
}
static void
deposit (char *line, struct pdp10_memory *memory)
{
char *p;
word_t *data;
unsigned long x, address = strtoul (line, &p, 8);
if (!whitespace (*p))
fatal ("Invalid DEPOSIT arguments: \"%s\"\n", line);
while (whitespace(*p))
p++;
if (*p == 0)
fatal ("Invalid DEPOSIT arguments: \"%s\"\n", line);
data = malloc (sizeof (word_t));
if (data == NULL)
{
fprintf (stderr, "Out of memory\n");
exit (1);
}
x = strtoul (p, &p, 8);
if (!whitespace_or_nul (*p))
fatal ("Invalid DEPOSIT arguments: \"%s\"\n", line);
*data = x;
add_memory (memory, address, 1, data);
}
static void
start (char *line)
{
char *p;
unsigned long address = strtoul (line, &p, 8);
if (p == line || !whitespace_or_nul (*p))
fatal ("Invalid GO argument: \"%s\"\n", line);
start_instruction = JRST | address;
}
static void
read_line (char *line, struct pdp10_memory *memory)
{
char *p = line;
size_t n;
while (whitespace(*p))
p++;
if (*p == 0 || *p == ';' || *p == '#')
return;
line = p++;
while (!whitespace_or_nul(*p))
p++;
if (*p == 0)
fatal ("SIMH command has no argument: %s\n", line);
*p++ = 0;
n = strlen(line);
if (strncasecmp (line, "deposit", n) == 0)
deposit (p, memory);
else if (strncasecmp (line, "go", n) == 0)
start (p);
else
fatal ("Unsupported SIMH command: %s\n", line);
}
static void
read_simh (FILE *f, struct pdp10_memory *memory, int cpu_model)
{
static char line[100];
(void)cpu_model;
fprintf (output_file, ";SIMH script\n\n");
for (;;)
{
if (fgets (line, sizeof line, f) == NULL)
return;
read_line (line, memory);
}
}
struct file_format simh_file_format = {
"simh",
read_simh,
write_simh
};