-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.c
243 lines (220 loc) · 5.98 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (C) 1992 Clarendon Hill Software.
*
* Permission is granted to any individual or institution to use, copy,
* or redistribute this software, provided this copyright notice is retained.
*
* This software is provided "as is" without any expressed or implied
* warranty. If this software brings on any sort of damage -- physical,
* monetary, emotional, or brain -- too bad. You've got no one to blame
* but yourself.
*
* The software may be modified for your own purposes, but modified versions
* must retain this notice.
*/
/*
* Portions copyright (c) 1996-2020, Timothy P. Mann
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "z80.h"
#include "trs.h"
#include "trs_disk.h"
#include "trs_hard.h"
#include "load_cmd.h"
int trs_model = 1;
int trs_paused = 1;
int trs_autodelay = 0;
char *program_name;
char *romfile1 = NULL;
char *romfile1x = NULL;
char *romfile3 = NULL;
char *romfile4p = NULL;
static void check_endian(void)
{
wordregister x;
x.byte.low = 1;
x.byte.high = 0;
if(x.word != 1)
{
fatal("Program compiled with wrong ENDIAN value -- adjust the Makefile.local, type \"rm *.o\", recompile, and try again.");
}
}
/*
* Note: If there are multiple ROMs, trs_rom_size is set to the ending
* address(+1) of the last ROM loaded. So ROMs must be loaded in
* ascending order by address.
*/
void trs_load_rom(int address, char *filename)
{
FILE *program;
int c;
if((program = fopen(filename, "r")) == NULL)
{
char message[100];
sprintf(message, "could not read %s", filename);
fatal(message);
}
c = getc(program);
if (c == ':') {
/* Assume Intel hex format */
rewind(program);
trs_rom_size = load_hex(program);
fclose(program);
return;
}
if (c == 1 || c == 5) {
/* Assume MODELA/III file */
int res;
extern Uchar *rom; /*!! fixme*/
Uchar loadmap[Z80_ADDRESS_LIMIT];
rewind(program);
res = load_cmd(program, rom, loadmap, 0, NULL, -1, NULL, NULL, 1);
if (res == LOAD_CMD_OK) {
trs_rom_size = Z80_ADDRESS_LIMIT;
while (trs_rom_size > 0) {
if (loadmap[--trs_rom_size] != 0) {
trs_rom_size++;
break;
}
}
fclose(program);
return;
} else {
/* Apparently it wasn't one */
rewind(program);
c = getc(program);
}
}
/* Assume raw binary */
trs_rom_size = address;
while (c != EOF) {
mem_write_rom(trs_rom_size++, c);
c = getc(program);
}
}
void trs_load_compiled_rom(int address, int size, unsigned char rom[])
{
int i;
trs_rom_size = size;
for (i = address; i < size; i++) {
mem_write_rom(i, rom[i]);
}
}
void
trs_load_romfiles(void)
{
struct stat statbuf;
switch (trs_model) {
case 1:
#ifdef DEFAULT_ROM1
if (!romfile1) {
romfile1 = DEFAULT_ROM1;
}
#endif
if (romfile1 != NULL && stat(romfile1, &statbuf) == 0) {
trs_load_rom(0, romfile1);
} else if (trs_rom1_size > 0) {
trs_load_compiled_rom(0, trs_rom1_size, trs_rom1);
} else {
fatal("ROM file not specified!");
}
#ifdef DEFAULT_ROM1X
if (!romfile1x) {
romfile1x = DEFAULT_ROM1X;
}
#endif
if (romfile1x != NULL && stat(romfile1x, &statbuf) == 0) {
trs_load_rom(0x3000, romfile1x);
} else if (trs_rom1x_size > 0) {
trs_load_compiled_rom(0x3000, trs_rom1x_size, trs_rom1x);
}
break;
case 3:
case 4:
#ifdef DEFAULT_ROM3
if (!romfile3) {
romfile3 = DEFAULT_ROM3;
}
#endif
if (romfile3 != NULL && stat(romfile3, &statbuf) == 0) {
trs_load_rom(0, romfile3);
} else if (trs_rom3_size > 0) {
trs_load_compiled_rom(0, trs_rom3_size, trs_rom3);
} else {
fatal("ROM file not specified!");
}
break;
default: /* 4P */
#ifdef DEFAULT_ROM4P
if (!romfile4p) {
romfile4p = DEFAULT_ROM4P;
}
#endif
if (romfile4p != NULL && stat(romfile4p, &statbuf) == 0) {
trs_load_rom(0, romfile4p);
} else if (trs_rom4p_size > 0) {
trs_load_compiled_rom(0, trs_rom4p_size, trs_rom4p);
} else {
fatal("ROM file not specified!");
}
break;
}
}
int main(int argc, char *argv[])
{
int debug = FALSE;
/* program_name must be set first because the error
* printing routines use it. */
program_name = strrchr(argv[0], '/');
if (program_name == NULL) {
program_name = argv[0];
} else {
program_name++;
}
check_endian();
argc = trs_parse_command_line(argc, argv, &debug);
if (argc > 1) {
fatal("erroneous argument %s", argv[1]);
}
mem_init();
trs_screen_init();
trs_timer_init();
trs_disk_init();
trs_hard_init();
stringy_init();
trs_load_romfiles();
trs_reset(1);
if (!debug) {
/* Run continuously until exit or request to enter debugger */
z80_run(TRUE);
}
printf("Entering debugger.\n");
debug_init();
debug_shell();
printf("Quitting.\n");
exit(0);
}