-
Notifications
You must be signed in to change notification settings - Fork 13
/
cross-file.c
106 lines (85 loc) · 2.41 KB
/
cross-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
/* 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"
/* The binary output from CROSS is formatted into blocks, with a
header of three words describing its type, length, and address. After
the data bytes comes a checksum byte. */
static unsigned char buf[4];
static int n = 0;
static int checksum;
static void refill (FILE *f)
{
word_t word = get_word (f);
if (word == -1)
exit (0);
buf[3] = (word >> 18) & 0377;
buf[2] = (word >> 26) & 0377;
buf[1] = (word >> 0) & 0377;
buf[0] = (word >> 8) & 0377;
n = 4;
}
static int get_8 (FILE *f)
{
int data;
if (n == 0)
refill (f);
data = buf[--n];
checksum += data;
return data;
}
static int get_16 (FILE *f)
{
int word;
word = get_8 (f);
word |= get_8 (f) << 8;
return word;
}
static void
read_cross (FILE *f, struct pdp10_memory *memory, int cpu_model)
{
int i, data, length, address;
word_t *core;
(void)cpu_model;
for (;;)
{
checksum = 0;
do
data = get_8 (f);
while (data == 0);
data |= get_8 (f) << 8;
length = get_16 (f) - 6; /* Subtract header length. */
address = get_16 (f);
if (length == 0)
return;
core = malloc (length * sizeof (word_t));
if (core == NULL)
{
fprintf (stderr, "Out of memory.\n");
exit (1);
}
fprintf (stderr, "Type %d, length %d, address %04x\n",
data, length, address);
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: %04X.\n", data);
}
}
struct file_format cross_file_format = {
"cross",
read_cross,
NULL
};