-
Notifications
You must be signed in to change notification settings - Fork 13
/
lda-file.c
138 lines (114 loc) · 2.87 KB
/
lda-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
/* Copyright (C) 2021 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/>. */
#include <stdio.h>
#include "dis.h"
#include "memory.h"
int checksum;
static int
get_8 (FILE *f)
{
int word = fgetc (f);
if (word == -1)
{
fprintf (stderr, "Unexpected end of LDA file.\n");
exit (1);
}
word &= 0377;
checksum += word;
return word;
}
static int
get_16 (FILE *f)
{
return get_8 (f) | (get_8 (f) << 8);
}
static void
read_lda (FILE *f, struct pdp10_memory *memory, int cpu_model)
{
int i, data, length, address;
word_t *core;
(void)cpu_model;
start_instruction = 1;
for (;;)
{
checksum = 0;
do
data = get_8 (f);
while (data == 0);
data |= get_8 (f) << 8;
if (data != 1)
{
fprintf (stderr, "Error looking for start of PALX block.\n");
exit (1);
}
length = get_16 (f);
address = get_16 (f);
length -= 6;
if (length == 0)
{
if ((address & 1) == 0)
start_instruction = address;
return;
}
core = malloc (length * sizeof (word_t));
if (core == NULL)
{
fprintf (stderr, "Out of memory.\n");
exit (1);
}
for (i = 0; i < length; i++)
core[i] = get_8 (f);
add_memory (memory, address, length, core);
data = get_8 (f);
if ((checksum & 0xFF) != 0)
fprintf (stderr, "Bad checksum %02X\n", data);
}
}
static void
out_8 (FILE *f, int word)
{
word &= 0377;
checksum += word;
fputc (word & 0377, f);
}
static void
out_16 (FILE *f, int word)
{
out_8 (f, word);
out_8 (f, word >> 8);
}
static void
write_lda (FILE *f, struct pdp10_memory *memory)
{
int start, length;
int i, j;
for (i = 0; i < memory->areas; i++)
{
start = memory->area[i].start;
length = memory->area[i].end - start;
checksum = 0;
out_16 (f, 1);
out_16 (f, length + 6);
out_16 (f, start);
for (j = start; j < start + length; j++)
out_8 (f, get_word_at (memory, j));
out_8 (f, -checksum & 0377);
}
out_16 (f, 1);
out_16 (f, length + 6);
out_16 (f, start_instruction & 1 ? 0 : start_instruction);
}
struct file_format lda_file_format = {
"lda",
read_lda,
write_lda
};