-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
547 lines (450 loc) · 15.6 KB
/
main.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
#include "main.h"
#if defined(_DEBUG) && defined(_MSC_VER)
#include <crtdbg.h>
#endif
/////////////////////////////////////////////////////////////////////////////
uint8_t* OutputBuffer = nullptr;
size_t OutputBufferSize = 0;
int OutputBlockCount = 0;
FILE* outfileobj = nullptr;
FILE* mapfileobj = nullptr;
FILE* stbfileobj = nullptr;
struct tagGlobals Globals;
char savfilename[64] = { 0 };
void println()
{
putchar('\n');
}
/////////////////////////////////////////////////////////////////////////////
const uint32_t RAD50_ABS = rad50x2(". ABS."); // ASECT
/////////////////////////////////////////////////////////////////////////////
// Initialize arrays and variables
void initialize()
{
memset(&Globals, 0, sizeof(Globals));
memset(SaveStatusArea, 0, SaveStatusAreaSize * sizeof(SaveStatusEntry));
SaveStatusCount = 0;
SymbolTable = (SymbolTableEntry*) ::calloc(SymbolTableSize, sizeof(SymbolTableEntry));
SymbolTableCount = 0;
memset(LibraryModuleList, 0, LibraryModuleListSize * sizeof(LibraryModuleEntry));
// Set globals defaults, see LINK1\START1
Globals.NUMCOL = 3; // 3-COLUMN MAP IS NORMAL
Globals.NUMBUF = 3; // NUMBER OF AVAILABLE CACHING BLOCKS (DEF=3)
Globals.BEGBLK.symbol = RAD50_ABS;
Globals.BEGBLK.code = 5;
Globals.BEGBLK.flags = 0100/*CS$GBL*/ | 04/*CS$ALO*/;
Globals.BEGBLK.value = 000001; // INITIAL TRANSFER ADR TO 1
//TODO
Globals.DBOTTM = 01000; // INITIAL BOTTOM ADR
Globals.BOTTOM = 01000; // INITIAL D-SPACE BOTTOM ADR
Globals.KSWVAL = 128/*RELSTK*/; // DEFAULT REL FILE STACK SIZE
Globals.MINADDR = 0xFFFF;
Globals.MODNAM = new string("");
Globals.IDENT = new string("");
}
// Free all memory, close all files
void finalize()
{
//printf("Finalization\n");
if (SymbolTable != nullptr)
{
free(SymbolTable); SymbolTable = nullptr;
}
for (int i = 0; i < SaveStatusCount; i++)
{
SaveStatusEntry* sscur = SaveStatusArea + i;
if (sscur->data != nullptr)
{
free(sscur->data); sscur->data = nullptr;
}
}
if (OutputBuffer != nullptr)
{
free(OutputBuffer); OutputBuffer = nullptr; OutputBufferSize = 0;
}
if (outfileobj != nullptr)
{
fclose(outfileobj); outfileobj = nullptr;
}
if (mapfileobj != nullptr)
{
fclose(mapfileobj); mapfileobj = nullptr;
}
if (stbfileobj != nullptr)
{
fclose(stbfileobj); stbfileobj = nullptr;
}
}
// Read all input files into memory
void prepare_read_files()
{
const size_t MAX_INPUTFILE_SIZE = 512 * 1024; // 512 KB max
for (int i = 0; i < SaveStatusCount; i++)
{
SaveStatusEntry* sscur = SaveStatusArea + i;
assert(sscur->filename[0] != 0);
FILE* file = fopen(sscur->filename, "rb");
if (file == nullptr)
fatal_error("ERR2: Failed to open input file: %s, error %d: %s.\n", sscur->filename, errno, strerror(errno));
fseek(file, 0L, SEEK_END);
size_t filesize = ftell(file);
if (filesize > MAX_INPUTFILE_SIZE)
fatal_error("Input file %s too long.\n", sscur->filename);
sscur->filesize = filesize;
sscur->data = (uint8_t*)malloc(filesize);
if (sscur->data == nullptr)
fatal_error("Failed to allocate memory for input file %s\n", sscur->filename);
fseek(file, 0L, SEEK_SET);
size_t bytesread = fread(sscur->data, 1, filesize, file);
if (bytesread != filesize)
fatal_error("ERR2: Failed to read input file %s\n", sscur->filename);
//printf(" File read %s, %d bytes.\n", sscur->filename, bytesread);
fclose(file);
}
}
void parse_commandline_option(const char* cur)
{
assert(cur != nullptr);
uint16_t param1, param2;
// /EXECUTE:filespec - Specifies the name of the memory image file
if (strncmp(cur, "EXECUTE:", 8) == 0) //TODO: or /SAV
{
strcpy(savfilename, cur + 8);
//TODO: Validate the name as a proper filename
return;
}
// /WIDE /W - SPECIFY WIDE MAP LISTING
if (strcmp(cur, "WIDE") == 0 || strcmp(cur, "W") == 0)
{
Globals.NUMCOL = 6; // 6 COLUMNS
//Globals.LSTFMT--; // WIDE CREF
return;
}
// /NOBITMAP /X - DO NOT EMIT BIT MAP
if (strcmp(cur, "NOBITMAP") == 0 || strcmp(cur, "X") == 0)
{
Globals.SWITCH |= SW_X;
return;
}
// /SYMBOLTABLE /STB - Generates a symbol table file
if (strcmp(cur, "SYMBOLTABLE") == 0 || strcmp(cur, "STB") == 0)
{
Globals.FlagSTB = true;
return;
}
// /MAP - Generates map file
if (strcmp(cur, "MAP") == 0)
{
Globals.FlagMAP = true;
return;
}
// /BIN - Generates binary file
if (strcmp(cur, "BIN") == 0)
{
Globals.FlagBIN = true;
return;
}
// /ALPHABETIZE /A - ALPHABETIZE MAP
if (strcmp(cur, "ALPHABETIZE") == 0 || strcmp(cur, "A") == 0)
{
Globals.SWITCH |= SW_A;
return;
}
// /FOREGROUND /R[:stacksize] - INDICATE FOREGROUND LINK
if (strcmp(cur, "FOREGROUND") == 0 || strcmp(cur, "R") == 0)
{
//result = sscanf(cur, ":%ho", ¶m1);
Globals.SWITCH |= SW_R;
//TODO: stacksize
return;
}
int result;
param1 = param2 = 0; result = 0;
int option = toupper(*cur++);
switch (option)
{
case 'T': // /T:addr - SPECIFY TRANSFER ADR
result = sscanf(cur, ":%ho", ¶m1);
if (result < 1)
fatal_error("Invalid /T option, use /T:addr\n");
Globals.SWITCH |= SW_T;
Globals.BEGBLK.value = param1;
break;
case 'M': // /M - MODIFY INITIAL STACK
//TODO: Ability to assign a symbol like /M:SYMBOL
result = sscanf(cur, ":%ho", ¶m1);
if (result < 1)
fatal_error("Invalid /M option, use /M:addr\n");
if (param1 & 1)
fatal_error("Invalid /M option value, use even address\n");
Globals.SWITCH |= SW_M;
// Globals.STKBLK[2] = param1;
Globals.STKBLK.value = param1;
break;
case 'B': // /B:addr - SPECIFY BOTTOM ADR FOR LINK
result = sscanf(cur, ":%ho", ¶m1);
if (result < 1)
fatal_error("Invalid /B option, use /B:addr\n");
if (param1 & 1)
fatal_error("Invalid /B option value, use even address\n");
Globals.SWITCH |= SW_B;
Globals.BOTTOM = param1;
Globals.DBOTTM = param1;
break;
case 'H': // /H:addr - SPECIFY TOP ADR FOR LINK
result = sscanf(cur, ":%ho", ¶m1);
if (result < 1)
fatal_error("Invalid /H option, use /H:addr\n");
if (param1 & 1)
fatal_error("Invalid /H option value, use even address\n");
Globals.SWITCH |= SW_H;
Globals.HSWVAL = param1;
Globals.DHSWVL = param1;
break;
//case 'U': // /U - ROUND SECTION
// result = sscanf(cur, ":%ho:%ho", ¶m1, ¶m2);
// Globals.SWITCH |= SW_U;
// //TMPIDD = D.SWU;
// //TMPIDI = I.SWU;
// //TODO
// break;
//case 'E': // /E - EXTEND SECTION
// result = sscanf(cur, ":%ho:%ho", ¶m1, ¶m2);
// Globals.SWITCH |= SW_E;
// //TMPIDD = D.SWE;
// //TMPIDI = I.SWE;
// //TODO
// break;
//case 'Y': // /Y - START SECTION ON MULTIPLE OF VALUE
// result = sscanf(cur, ":%ho:%ho", ¶m1, ¶m2);
// Globals.SWITCH |= SW_Y;
// //TMPIDD = D.SWY;
// //TMPIDI = I.SWY;
// //TODO
// break;
//case 'K': // /K - SPECIFY MINIMUM SIZE
// result = sscanf(cur, ":%ho", ¶m1);
// if (result < 1)
// fatal_error("Invalid /K option, use /K:value\n");
// if (param1 < 2 || param1 > 28)
// fatal_error("Invalid /K option value, should be: 2 < value < 28\n");
// Globals.SWITCH |= SW_K;
// //TODO: if (Globals.SWITCH & SW_R)
// Globals.KSWVAL = param1;
// break;
//case 'P': // /P:N SIZE OF LML TABLE
// result = sscanf(cur, ":%ho", ¶m1);
// //TODO
// break;
//case 'Z': // /Z - ZERO UNFILLED LOCATIONS
// result = sscanf(cur, ":%ho:%ho", ¶m1, ¶m2);
// //TMPIDD = D.SWZ;
// //TMPIDI = I.SWZ;
// //TODO
// break;
//case 'V': // /XM, OR /V ON 1ST LINE
// result = sscanf(cur, ":%ho", ¶m1);
// //TODO
// break;
//case 'L': // /L - INDICATE LDA OUTPUT
// //TODO
// break;
//case 'I': // /I - INCLUDE MODULES FROM LIBRARY
// Globals.SWITCH |= SW_I;
// break;
//case 'F': // /F - INCLUDE FORLIB.OBJ IN LINK
// Globals.SWITCH |= SW_F;
// break;
//case 'S': // /S - SYMBOL TABLE AS LARGE AS POSSIBLE
// //TODO
// break;
//case 'D': // /D - ALLOW DUPLICATE SYMBOLS
// Globals.SWIT1 |= SW_D;
// break;
//case 'N': // /N - GENERATE CROSS REFERENCE
// result = sscanf(cur, ":%ho", ¶m1);
// //TODO
// break;
//case 'G': // /G - CALC. EPT SIZE ON RT-11
// result = sscanf(cur, ":%ho", ¶m1);
// //TODO
// break;
//case 'Q': // /Q:addr - SET PSECTS TO ABSOLUTE ADDRESSES
// result = sscanf(cur, ":%ho", ¶m1);
// //TODO
// break;
//case 'J': // /J - USE SEPARATED I-D SPACE
// if (Globals.SWITCH & SW_R)
// fatal_error("Invalid option: /R illegal with /J\n"); //TODO: Should be warning only
// else
// Globals.SWIT1 |= SW_J;
// break;
default:
fatal_error("Unknown command line option '%c'\n", option);
}
}
bool g_okHelpRequested = false;
bool g_okVersionRequested = false;
// PROCESS COMMAND STRING SWITCHES, see LINK1\SWLOOP
void parse_commandline(int argc, char **argv)
{
assert(argv != nullptr);
//uint16_t TMPIDD = 0; // D BIT TO TEST FOR /J PROCESSING
//uint16_t TMPIDI = 0; // I BIT TO TEST FOR /J PROCESSING
for (int arg = 1; arg < argc; arg++)
{
const char* argvcur = argv[arg];
if (strcmp(argvcur, "--help") == 0)
{
g_okHelpRequested = true;
break;
}
if (strcmp(argvcur, "--version") == 0)
{
g_okVersionRequested = true;
break;
}
if (*argvcur == '/' || *argvcur == '-') // Parse global arguments
{
//TODO: Parse arguments like Command String Interpreter
const char* cur = argvcur + 1;
if (*cur != 0)
{
parse_commandline_option(cur);
}
}
else // Parse filename and arguments
{
if (SaveStatusCount == SaveStatusAreaSize)
fatal_error("Too many files specified.\n");
SaveStatusEntry* sscur = SaveStatusArea + SaveStatusCount;
// Parse filename
char* filenamecur = sscur->filename;
const char* cur = argvcur;
int filenamelen = 0;
while (*cur != 0 && (isalnum(*cur) || *cur == '.' || *cur == '_' || *cur == '-') || *cur == '/')
{
*filenamecur = *cur;
filenamecur++; cur++;
filenamelen++;
if (filenamelen >= sizeof(sscur->filename) - 1)
fatal_error("Too long filename: %s\n", argvcur);
}
SaveStatusCount++;
//TODO: Parse options associated with the file
}
}
if (g_okHelpRequested || g_okVersionRequested)
return;
// Validate command line params
if (SaveStatusCount == 0)
fatal_error("Input file not specified.\n");
}
void print_help()
{
printf("\n"
"Usage: pclink11 <input files and options, space-separated>\n"
"Options:\n"
" /EXECUTE:filespec Specifies the name of the memory image file\n"
" /T:addr Specifies the starting address of the linked program\n"
" /M:addr Specifies the stack address for the linked program\n"
" /B:addr Specifies the lowest address to be used by the linked program\n"
" /H:addr Specifies the highest address to be used by the linked program\n"
" /NOBITMAP /X Do not emit bit map\n"
" /WIDE /W Produces a load map that is 132 columns wide\n"
" /ALPHABETIZE /A Lists global symbols on the link map in alphabetical order\n"
" /SYMBOLTABLE /STB Generates a symbol table file (.STB file)\n"
" /MAP Generates map file\n"
" /BIN Genegate .BIN file instead of .SAV"
"\n");
//TODO
}
int main(int argc, char *argv[])
{
#if defined(_DEBUG) && defined(_MSC_VER)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF);
int n = 0;
_CrtSetBreakAlloc(n);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
#endif
printf("PCLINK11 %s %s\n", APP_VERSION_STRING, __DATE__);
assert(sizeof(uint8_t) == 1);
assert(sizeof(uint16_t) == 2);
assert(sizeof(uint32_t) == 4);
if (argc <= 1)
{
print_help();
exit(EXIT_FAILURE);
}
atexit(finalize);
initialize();
parse_commandline(argc, argv);
if (g_okHelpRequested)
{
print_help();
exit(EXIT_SUCCESS);
}
if (g_okVersionRequested)
{
printf(
"Cross-linker, porting PDP-11 LINK to C/C++, WIP\n"
"Ported in 2019-2020 by nzeemin\n"
"License LGPLv3: GNU Lesser General Public License v3.0 https://www.gnu.org/licenses/lgpl-3.0.html\n"
"Source code: https://github.com/nzeemin/pclink11\n");
exit(EXIT_SUCCESS);
}
prepare_read_files();
printf("PASS 1\n");
Globals.PAS1_5 = 0; // PASS 1 PHASE INDICATOR
process_pass1();
if (Globals.PAS1_5 & 1) // BIT 0 SET IF TO DO 1.5 (we have library files)
{
printf("PASS 1.5\n");
Globals.PAS1_5 = 128;
process_pass15(); // SCANS ONLY LIBRARIES
}
process_pass1_endp1();
//print_symbol_table();//DEBUG
printf("PASS MAP\n");
assert(mapfileobj == nullptr);
process_pass_map_init();
process_pass_map_output();
process_pass_map_done();
assert(mapfileobj == nullptr);
assert(stbfileobj == nullptr);
assert(outfileobj == nullptr);
//print_symbol_table();//DEBUG
printf("PASS 2\n");
process_pass2_init();
assert(outfileobj != nullptr);
Globals.PAS1_5 = 0;
process_pass2(); // Non-library pass
if (Globals.PAS1_5 & 1) // BIT 0 SET IF TO DO 1.5 (we have library files)
{
printf("PASS 2.5\n");
Globals.PAS1_5 |= 128; // MARK BEGINING OF 2ND HALF OF PASS
process_pass2(); // Library pass
}
process_pass2_done();
assert(outfileobj == nullptr);
printf("SUCCESS\n");
finalize();
#if defined(_DEBUG) && defined(_MSC_VER)
if (_CrtDumpMemoryLeaks())
printf("ERROR: Memory leak detected\n");
#endif
return EXIT_SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////