-
Notifications
You must be signed in to change notification settings - Fork 0
/
cas
executable file
·726 lines (629 loc) · 24 KB
/
cas
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# CSCVon8 assembler, (C) 2019 Warren Toomey, GPL3
my $debug = 0; # Print debugging information
my $listing = 0; # List program with line numbers
my $ramimage = 1; # Assemble to RAM instead of ROM
my $linenum = 0; # Line number being parsed
my $PC; # Program counter
my $StartPC = 0x8000;# Start PC at 0x0000, or 0x8000 for $ramimage
my @Line; # Array of whole lines from input file
my @Linenum; # Line number for each line in @Line
my @MEM = (0) x 65536; # Contents of memory
my %Label; # Hash of labels
my %Export; # Hash of export symbols
my @Numlabel; # Sparse 2D array of numeric labels and at what location
# they were defined.
my $RUN; # Run program from
my $PROGNAME; # Name of program - to use in filesystem
# Hash of instruction names, their opcode values and length in bytes.
# Also, hash of known keywords that occur in instructions.
my %Inst;
my %Keyword;
# Hash of RAM return address locations for each function, and
# next available RAM location if none yet defined for a function
# For programs in the RAM start the return addresses from $FF00 and grow downwards
my %ReturnAddr;
my $nextreturnaddr = 0xFE00;
# Set a memory location to a specific value. We use this so that
# we can track the lowest and highest locations used. This is needed
# when we write out the code assembled for loading into RAM
my $lomemused = 1000000;
my $himemused = -1;
sub setMEM {
my ( $addr, $val ) = @_;
$MEM[$addr] = $val;
$lomemused = $addr if ( $addr < $lomemused );
$himemused = $addr if ( $addr > $himemused );
}
# Get the return address for a function, given an int function address
sub get_return_address {
my $funcaddr = shift;
if ( !defined( $ReturnAddr{$funcaddr} ) ) {
$ReturnAddr{$funcaddr} = $nextreturnaddr;
$nextreturnaddr -= 2;
}
return ( $ReturnAddr{$funcaddr} );
}
# Get the return address for a function, given an int function address if it already exists
sub get_existing_return_address {
my $funcaddr = shift;
if ( !defined( $ReturnAddr{$funcaddr} ) ) {
return undef;
}
return ( $ReturnAddr{$funcaddr} );
}
# Load the opcodes file to populate the %Inst hash
sub load_opcodes {
my $lnum = 0;
my %Opcode;
# Insert pseudo-ops by hand
$Inst{EQU} = [ 0x100, 0 ];
$Keyword{EQU} = 1;
$Inst{ORG} = [ 0x101, 0 ];
$Keyword{ORG} = 1;
$Inst{STR} = [ 0x102, 0 ];
$Keyword{STR} = 1;
$Inst{LHB} = [ 0x103, 2 ];
$Keyword{LHB} = 1;
$Inst{LHA} = [ 0x104, 2 ];
$Keyword{LHA} = 1;
$Inst{HEX} = [ 0x105, 0 ];
$Keyword{HEX} = 1;
$Inst{PAG} = [ 0x106, 0 ];
$Keyword{PAG} = 1;
$Inst{LLB} = [ 0x107, 2 ];
$Keyword{LLB} = 1;
$Inst{LLA} = [ 0x108, 2 ];
$Keyword{LLA} = 1;
$Inst{BYTE} = [ 0x109, 0 ];
$Keyword{BYTE} = 1;
$Inst{WORD} = [ 0x10a, 0 ];
$Keyword{WORD} = 1;
$Inst{EXPORT} = [ 0x10b, 0 ];
$Keyword{EXPORT} = 1;
$Inst{RUN} = [ 0x10c, 0 ];
$Keyword{RUN} = 1;
$Inst{NAME} = [ 0x10d, 0 ];
$Keyword{NAME} = 1;
open( my $IN, "<", "opcodes" ) || die("Cannot read opcodes: $!\n");
while (<$IN>) {
chomp;
$lnum++;
s{#.*}{}; # Lose comments
s{//.*}{}; # Lose comments
s{^\s+}{}; # Lose leading whitespace
s{\s+$}{}; # Lose trailing whitespace
next if (m{^$}); # Ignore empty lines
my ( $opcode, $oplen, $name ) = split( m{\s+}, $_ );
die("Missing opcode on line $lnum\n") if ( !defined($opcode) );
die("Missing oplen on line $lnum\n") if ( !defined($oplen) );
die("Missing name on line $lnum\n") if ( !defined($name) );
die("Instruction $name redefined on line $lnum\n")
if ( defined( $Inst{$name} ) );
die("Opcode $opcode redefined on line $lnum\n")
if ( defined( $Opcode{$opcode} ) );
$Inst{$name} = [ hex($opcode), $oplen ];
$Opcode{$opcode} = 1;
# Now split the instruction name on underscores and add the elements
# to the list of keywords
foreach my $key ( split( m{_}, $name ) ) {
$Keyword{$key} = 1;
}
}
close($IN);
}
# Given a numeric label value and either 'b' or 'f', return the value of
# the matching numeric label, or die if none is found
sub get_numeric_label_value {
my ( $n, $direction ) = @_;
# Get the reference to the array of addresses for the given label
my $lref = $Numlabel[$n];
die("Numeric label $n never used, cannot be referenced on line $linenum\n")
if ( !defined($lref) );
# Walk the list getting the next value. Keep the previous value.
my $bval = undef;
foreach my $fval ( @{$lref} ) {
# Return the first value after the PC if direction is forward
return ($fval) if ( ( $direction eq "f" ) && ( $fval > $PC ) );
$bval = $fval;
}
# Return the last back value if the direction is backward
return ($bval) if ( ( $direction eq "b" ) && defined($bval) );
# We didn't find a reference for the direction
die("No value for $n$direction on line $linenum\n");
}
# Get the value of a label or a $hex constant. Also support . for PC,
# .+num, .-num and label/HI to get the top byte of a label.
sub get_label_value {
my ( $word, $firstpass ) = @_;
my $address = 0;
my $offset = 0;
# If it's a reference to a numeric label, use another function
if ( $word =~ m{^(\d+)([bf])$} ) {
return ( get_numeric_label_value( $1, $2 ) );
}
# Find any decimal offset and separate the word from the offset
if ( $word =~ m{^(.*)\+(\d+)$} ) { $word = $1; $offset = $2; }
if ( $word =~ m{^(.*)\-(\d+)$} ) { $word = $1; $offset = -$2; }
if ( defined( $Label{$word} ) ) {
$address = $Label{$word};
}
elsif ( ( $word =~ m{^(.*)/HI} ) && defined( $Label{$1} ) ) {
$address = $Label{$1} >> 8; # Get high byte of the address
}
elsif ( $word eq "." ) { # . means current PC value
$address = $PC;
}
elsif ( $word =~ m{^\.\+(\d+)} ) { # .+num
$address = $PC + $1;
}
elsif ( $word =~ m{^\.\-(\d+)} ) { # .-num
$address = $PC - $1;
}
elsif ( !$firstpass ) {
die("Expression $word on line $linenum unrecognised\n");
}
return ( $address + $offset );
}
# Parse a single line from the assembly input. Also get a boolean
# to indicate this is the first pass, so we can ignore unknown labels.
sub parse2 {
my ( $wholeline, $firstpass ) = @_;
# Split the whole line up into lines that are semicolon separated
foreach my $line ( split( m{\s*;\s*}, $wholeline ) ) {
my $label = "";
my @constant;
my $string = "";
my $index = "";
my $word = "";
my $isindexed = 0;
# Divide the words up into keywords and non-keywords
my @keylist;
my @otherlist;
# Loop while there is something left in the line
while (1) {
# Single character
if ( $line =~ m{^\s*'(.)'(.*)} ) {
push( @constant, ord($1) );
$line = $2;
next;
}
# Escaped single character
if ( $line =~ m{^\s*'\\(.)'(.*)} ) {
# Evaluate escaped characters
my $c = $1;
$c =~ y{tnr}{\t\n\r};
push( @constant, ord($c) );
$line = $2;
next;
}
# String
if ( $line =~ m{^\s*"(.*)"(.*)} ) {
$string = $1;
$line = $2;
# Evaluate escaped characters
$string =~ s{\\n}{\n}g;
$string =~ s{\\t}{\t}g;
$string =~ s{\\"}{"}g;
next;
}
# Two byte hex value
if ( $line =~
m{^\s*\$([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])(.*)} )
{
push( @constant, hex($1) );
$line = $2;
next;
}
# Decimal value
if ( $line =~ m{^\s*@(\d+)(.*)} )
{
push( @constant, $1 );
$line = $2;
next;
}
# One byte hex value
if ( $line =~ m{^\s*\$([0-9A-Fa-f][0-9A-Fa-f])(.*)} ) {
push( @constant, hex($1) );
$line = $2;
next;
}
# Numbered label: push the PC to define the current position
# of this temporary label
if ( $line =~ m{^\s*(\d+):(.*)} ) {
my $nlabel = $1;
$line = $2;
push( @{ $Numlabel[$nlabel] }, $PC );
next;
}
# Label
if ( $line =~ m{^\s*(\S+):(.*)} ) {
$label = $1;
$line = $2;
next;
}
# Word, either a label, keyword or operation. May have an index
if ( $line =~ m{^\s*(\S+)(.*)} ) {
$word = $1;
$line = $2;
# Find any index
if ( $word =~ m{(.*)(,[AB])$} ) {
$word = $1;
$index = $2;
$isindexed = 1;
}
if ( defined( $Keyword{$word} ) ) {
push( @keylist, $word );
} else {
push( @otherlist, $word ) if ($word);
}
push( @keylist, $index ) if ($isindexed);
next;
}
# Stop now if nothing is left
last if ( $line =~ m{^\s*$} );
# Otherwise we don't recognise it
die("Unrecognised input on line $linenum: $wholeline\n");
}
# We have parsed the line, now implement it
# Build the instruction from the keywords
my $inst = join( '_', @keylist );
# ORG instruction: set the PC. Do this here so
# we can put the label on the same line
if ( $inst eq "ORG" ) {
die("No ORG address on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
$PC = $constant[0];
}
# Save location of label on pass one
if ( $label && $firstpass ) {
# An EQU defines the value of this label.
if ( $inst eq "EQU" ) {
die("No EQU value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
$Label{$label} = $constant[0];
} else {
# Otherwise the label's value is the PC's value
$Label{$label} = $PC;
}
}
# Deal with the unrecognised words on the line
if ( !$firstpass ) {
foreach my $word (@otherlist) {
# Convert a label into an address.
# Die if it isn't a known label on pass two
push( @constant, get_label_value( $word, $firstpass ) );
}
}
# No instruction, skip the line
return if ( !defined($inst) || $inst eq "" );
die("Unrecognised instruction >$inst< on line $linenum\n")
if ( !defined( $Inst{$inst} ) );
my ( $opcode, $oplen ) = @{ $Inst{$inst} };
printf( "PC %04x, Recognised $inst => $opcode $oplen\n", $PC )
if ($debug);
# Special instruction handling code goes here
# JSR instruction
if ( !$firstpass && $inst eq "JSR" ) {
# Store the function's start address in the instruction stream
die("No JSR value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
setMEM( $PC + 5, $constant[0] >> 8 );
setMEM( $PC + 6, $constant[0] & 0xff );
# Use the $constant[0], which is the function's start address, to get
# the location of the return address in RAM for the function
printf( "JSR: for function at 0x%04x, ", $constant[0] )
if ($debug);
if ( defined $constant[1] ) {
$constant[0] = $constant[1];
printf( "override return address 0x%04x\n", $constant[1] )
if ($debug);
} else {
$constant[0] = get_return_address( $constant[0] );
printf( "store return address at 0x%04x\n", $constant[0] )
if ($debug);
}
# Now put the return address into the instruction stream
setMEM( $PC + 3, ( $PC + 7 ) >> 8 );
setMEM( $PC + 4, ( $PC + 7 ) & 0xff );
}
# RTS instruction
if ( !$firstpass && $inst eq "RTS" ) {
# Use the $constant[0], which is the function's start address, to get
# the location of the return address in RAM for the function
die("No RTS value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
printf( "RTS: for function at 0x%04x, ", $constant[0] ) if ($debug);
$constant[0] = get_return_address( $constant[0] );
printf( "return address is 0x%04x\n", $constant[0] ) if ($debug);
}
# LHB instruction: load the high byte of a label in B
if ( !$firstpass && $inst eq "LHB" ) {
# Shift down the top byte, then change instruction to LCB
die("No LHB value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
$constant[0] = $constant[0] >> 8;
( $opcode, $oplen ) = @{ $Inst{"LCB"} };
}
# LHA instruction: load the high byte of a label in A
if ( !$firstpass && $inst eq "LHA" ) {
# Shift down the top byte, then change instruction to LCA
die("No LHA value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
$constant[0] = $constant[0] >> 8;
( $opcode, $oplen ) = @{ $Inst{"LCA"} };
}
# LLB instruction: load the low byte of a label in B
if ( !$firstpass && $inst eq "LLB" ) {
# Shift down the low byte, then change instruction to LCB
die("No LLB value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
$constant[0] = $constant[0] & 0xFF;
( $opcode, $oplen ) = @{ $Inst{"LCB"} };
}
# LLA instruction: load the low byte of a label in A
if ( !$firstpass && $inst eq "LLA" ) {
# Shift down the low byte, then change instruction to LCA
die("No LLA value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
$constant[0] = $constant[0] & 0xFF;
( $opcode, $oplen ) = @{ $Inst{"LCA"} };
}
# SIA/SIB instruction: also insert an incremented low pointer byte
if ( !$firstpass && $inst =~ m{^SI[AB]$} ) {
die("No SIA/SIB value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
setMEM( $PC + 3, ( $constant[0] + 1 ) & 0xff );
die("Pointer $constant[0] straddles page!\n")
if ( $MEM[ $PC + 3 ] == 0 );
}
# Insert a string
if ( $inst eq "STR" ) {
my $oldPC = $PC;
# Evaluate escaped characters in the string
$string =~ s{\\n}{\n}g;
$string =~ s{\\t}{\t}g;
$string =~ s{\\"}{"}g;
foreach my $ch ( split( "", $string ) ) {
setMEM( $PC++, ord($ch) );
}
setMEM( $PC++, 0 );
warn("String $string straddles pages\n")
if ( ( $oldPC >> 8 ) != ( $PC >> 8 ) );
}
# Insert a sequence of HEX bytes, given as a string, e.g. "12 34 56 AB"
if ( $inst eq "HEX" ) {
my $oldPC = $PC;
foreach my $hexpair ( split( " ", $string ) ) {
setMEM( $PC++, hex($hexpair) );
}
warn("Warning: Hex $string straddles pages\n")
if ( $firstpass && ( $oldPC >> 8 ) != ( $PC >> 8 ) );
}
# Reserve number of bytes
if ( $inst eq "BYTE" ) {
my $oldPC = $PC;
my $num_bytes = 1;
$num_bytes = $constant[0]
if (length $constant[0]);
$PC += $num_bytes;
warn("Warning: $num_bytes bytes straddles pages\n")
if ( $firstpass && ( $oldPC >> 8 ) != ( $PC >> 8 ) );
}
# Reserve number of words
if ( $inst eq "WORD" ) {
my $oldPC = $PC;
my $num_bytes = 2;
$num_bytes = $constant[0]*2
if (length $constant[0]);
$PC += $num_bytes;
warn("Warning: $num_bytes words straddles pages\n")
if ( $firstpass && ( $oldPC >> 8 ) != ( $PC >> 8 ) );
}
# Run
if ( $inst eq "RUN" ) {
$RUN = $PC;
}
if ( $inst eq "NAME" ) {
$PROGNAME = $string;
}
# Increment PC to the start of the next page
if ( $inst eq "PAG" ) {
my $oldPC = $PC;
$PC = ( $PC + 0xFF ) & 0xFF00;
printf( "Page aligned PC %04x -> %04x\n", $oldPC, $PC ) if ($debug);
}
if ( $inst eq "EXPORT" && !$firstpass) {
my $lab = $otherlist[0];
my $lab_val = get_label_value($lab);
my $lab_ret = get_existing_return_address($lab_val);
$Export{$lab} = $lab_val;
if ( $lab_ret ) {
my $lab_return = "${lab}_ret";
$Export{$lab_return} = $lab_ret;
}
printf( "Exporting label %s: address %04x \n", $lab, $lab_val ) if ($debug && !$lab_ret);
printf( "Exporting subroutine %s: address %04x, return %04x\n", $lab, $lab_val, $lab_ret ) if ($debug && $lab_ret);
}
# Back to general instruction handling
# Save the instruction on pass 2 if there was an instruction.
if ( !$firstpass ) {
# Shift the constant if indexed
# die("No indexed value on line $linenum: $wholeline\n")
# if ( $isindexed && !defined( $constant[0] ) );
printf( "\tShifting addr %04x as indexed\n", $constant[0] )
if ( $debug && $isindexed && $oplen == 2 );
$constant[0] = $constant[0] >> 8 if ($isindexed && $oplen == 2);
if ( $oplen > 0 ) {
setMEM( $PC, $opcode );
printf( " %02x", $opcode ) if ( $debug && !$firstpass );
}
if ( $oplen == 2 ) {
die("No constant/label value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
setMEM( $PC + 1, $constant[0] & 0xff );
printf( " %02x", $constant[0] & 0xff )
if ( $debug && !$firstpass );
}
if ( $oplen > 2 ) {
die("No constant/label value on line $linenum: $wholeline\n")
if ( !defined( $constant[0] ) );
setMEM( $PC + 1, $constant[0] >> 8 );
setMEM( $PC + 2, $constant[0] & 0xff );
printf( " %02x %02x", $constant[0] >> 8, $constant[0] & 0xff )
if ( $debug && !$firstpass );
}
if ( $oplen == 5 ) {
die("No constant/label value on line $linenum: $wholeline\n")
if ( !defined( $constant[1] ) );
setMEM( $PC + 3, $constant[1] >> 8 );
setMEM( $PC + 4, $constant[1] & 0xff );
printf( " %02x %02x", $constant[1] >> 8, $constant[1] & 0xff )
if ( $debug && !$firstpass );
}
print("\n") if ( $debug && !$firstpass && $oplen > 0 );
}
# Increment the PC
die("Out of instruction space!\n") if ( $PC > 0xFFFF );
$PC += $oplen;
}
}
#### MAIN PROGRAM ####
# Enable debugging
while ( @ARGV > 1 ) {
if ( $ARGV[0] eq "-d" ) {
$debug++;
shift(@ARGV);
next;
}
if ( $ARGV[0] eq "-l" ) {
$listing++;
shift(@ARGV);
next;
}
# If we are writing code for the monitor then start code at $0000
# Return addresses start at $FEFE and grow downwards
if ( $ARGV[0] eq "-m" ) {
$StartPC = 0x0000;
$nextreturnaddr = 0xFEFE;
$ramimage = 0;
shift(@ARGV);
next;
}
die("Usage: $0 [-d] [-m] infile\n");
}
# Give usage
die("Usage: $0 [-d] [-m] infile\n") if ( @ARGV != 1 );
# Load the instruction names, opcodes and lengths
load_opcodes();
# Read in the instructions and store in the @Line array. Use the
# C preprocessor as well.
open( my $IN, "-|", "cpp -nostdinc $ARGV[0] | grep -v '^# [0-9]+ \"(.+)\"'" )
|| die("Cannot open $ARGV[0]: $!");
while (<$IN>) {
chomp;
$linenum++;
if (!m{'(.*)#(.*)'}) {
s{#.*}{}; # Lose comments
}
s{^\s+}{}; # Lose leading whitespace
s{\s+$}{}; # Lose trailing whitespace
next if (m{^$}); # Ignore empty lines
push( @Line, $_ ); # Save the completed line and its line number
push( @Linenum, $linenum );
}
close($IN);
# First pass: find the labels
$PC = $StartPC;
foreach my $i ( 0 .. ( @Line - 1 ) ) {
my $line = $Line[$i];
$linenum = $Linenum[$i];
printf("[%d] %s\n", $linenum, $line)
if ($listing);
parse2( $line, 1 );
}
if ($debug) {
print("Labels:\n");
foreach my $k ( sort( keys(%Label) ) ) {
printf( "%s:\t%04x\n", $k, $Label{$k} );
}
print("Numeric labels:\n");
foreach my $k ( 0 .. scalar(@Numlabel) - 1 ) {
next if ( !defined( $Numlabel[$k] ) );
print( "$k: ", join( ", ", @{ $Numlabel[$k] } ), "\n" );
}
}
# Second pass: assemble
$PC = $StartPC;
foreach my $i ( 0 .. ( @Line - 1 ) ) {
my $line = $Line[$i];
$linenum = $Linenum[$i];
parse2( $line, 0 );
}
# Show exports
if ($debug) {
printf("Exports: %d\n", scalar(%Export));
foreach my $k ( sort( keys(%Export) ) ) {
printf( "%s: %04x\n", $k, $Export{$k} );
}
}
# Output exports as .h header file
if (scalar(%Export)) {
my $exportfile = $ARGV[0];
if ($ramimage) {
$exportfile =~ s{\.s$}{};
$exportfile = $exportfile . ".h";
} else {
$exportfile = "Examples/monitor.h"
}
open( my $OUT, ">", $exportfile) || die("Can't write to $exportfile: $!\n");
foreach my $k ( sort( keys(%Export) ) ) {
printf( $OUT "%s: EQU \$%04x\n", $k, $Export{$k} );
}
close($OUT);
}
# Dump a RAM image ready to load into the monitor if $ramimage is set
if ($ramimage) {
my $hexfile = $ARGV[0];
$hexfile =~ s{\.s$}{};
my $binfile = $hexfile . ".bin";
$hexfile = $hexfile . ".hex";
open( my $OUT, ">", $hexfile ) || die("Can't write to $hexfile: $!\n");
open( my $BIN, ">:raw", $binfile ) || die("Can't write to $binfile: $!\n");
printf( $OUT "C%04x\n", $lomemused );
for my $i ( $lomemused .. $himemused ) {
printf( $OUT "%02x ", $MEM[$i] ? $MEM[$i] : 0 );
print( $BIN pack("C", $MEM[$i] ? $MEM[$i] : 0 ));
print( $OUT "\n" ) if ( ( $i % 16 ) == 15 );
}
print( $OUT "Z\n" );
# output start address and length in $F002 and $F004
printf( $OUT "CF002\n%04x Z\n", $lomemused);
my $length = $himemused - $lomemused + 1;
printf( $OUT "CF004\n%04x Z\n", $length);
# output name as hex & \0 (max 20 chars)
if ($PROGNAME) {
print( $OUT "CF006\n");
my @chars = split("", $PROGNAME);
foreach ( @chars ) {
printf( $OUT "%02x ", ord($_));
}
print( $OUT "00 Z\n" );
}
printf( $OUT "R%04x\n", $RUN ) if ($RUN);
close($OUT);
exit(0);
}
# Otherwise, Write 32K ROM out in hex
open( my $OUT, ">", "instr.rom" ) || die("Can't write to instr.rom: $!\n");
open( my $BIN, ">:raw", "instr.bin" ) || die("Can't write to instr.bin: $!\n");
for my $i ( 0 .. ( 2**15 - 1 ) ) {
printf( $OUT "%02x ", $MEM[$i] ? $MEM[$i] : 0 );
print( $BIN pack("C", $MEM[$i] ? $MEM[$i] : 0 ));
print( $OUT "\n" ) if ( ( $i % 16 ) == 15 );
}
close($OUT);
exit(0);