-
Notifications
You must be signed in to change notification settings - Fork 0
/
pasm.c
1361 lines (1215 loc) · 33.1 KB
/
pasm.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
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------------------------------------------
--
-- pasm.c
-- An assembler for the pumpkin-cpu
-- Version 1.3
--
--------------------------------------------------------------------------------------------------------
--
-- This file is part of the pumpkin-cpu Project
-- Copyright (C) 2020 Steve Teal
--
-- This source file may be used and distributed without restriction provided that this copyright
-- statement is not removed from the file and that any derivative work contains the original
-- copyright notice and the associated disclaimer.
--
-- This source file is free software; you can redistribute it and/or modify it under the terms
-- of the GNU Lesser General Public License as published by the Free Software Foundation,
-- either version 3 of the License, or (at your option) any later version.
--
-- This source 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 Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License along with this
-- source; if not, download it from http://www.gnu.org/licenses/lgpl-3.0.en.html
--
------------------------------------------------------------------------------------------------------*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#define VERSION_STRING "1.3"
#define MAX_MEMORY_SIZE (4096)
#define MAX_LABEL_NAME_LENGTH (64)
#define MAX_LABELS (500)
#define MAX_IMMEDIATES (500)
#define MAX_LINE_LENGTH (256)
#define MAX_WORDS_ON_LINE (5) /* eg: LABEL DB 0 DUP 125 */
#define NUMBER_OF_INSTRUCTIONS (16)
#define DB_DW_BUFFER_SIZE (256)
char *instructions[] = {"LOAD","STORE","ADD","SUB","OR","AND","XOR","ROR","SWAP","IN","OUT","BR","BNC","BNZ","CALL","RETURN"};
char VHDLFileStart[] =
"---------------------------------------------------------------------\n"
"--\n"
"-- Built with PASM version %s\n"
"-- File name: %s\n"
"-- %s\n"
"-- \n"
"---------------------------------------------------------------------\n"
"library ieee;\n"
"use ieee.std_logic_1164.all;\n"
"use ieee.numeric_std.all;\n\n"
"entity %s is\n"
"port (\n"
" clock : in std_logic;\n"
" clock_enable : in std_logic;\n"
" address : in std_logic_vector(%d downto 0);\n"
" data_out : out std_logic_vector(15 downto 0);\n"
" data_in : in std_logic_vector(15 downto 0);\n"
" write_enable : in std_logic);\n"
"end entity;\n\n"
"architecture rtl of %s is\n\n"
" type ram_type is array (0 to %d) of std_logic_vector(15 downto 0);\n"
" signal ram : ram_type := (\n";
char VHDLFileEnd[] =
"begin\n\n"
" process(clock)\n"
" begin\n"
" if rising_edge(clock) then\n"
" if clock_enable = '1' then\n"
" if write_enable = '1' then\n"
" ram(to_integer(unsigned(address))) <= data_in;\n"
" else\n"
" data_out <= ram(to_integer(unsigned(address)));\n"
" end if;\n"
" end if;\n"
" end if;\n"
" end process;\n\n"
"end rtl;\n\n"
"--- End of file ---\n";
typedef struct
{
char name[MAX_LABEL_NAME_LENGTH+1]; /* +1 to allow space for null */
int value;
}label_t;
typedef struct
{
int value;
int address;
}immediate_t;
int memorySize; /* Size of target memory */
int memoryImage[MAX_MEMORY_SIZE]; /* Assembled memory image */
int currentAddress;
int endAddress;
int currentLine;
int numImmediates;
immediate_t immediates[MAX_IMMEDIATES];
int numLabels;
label_t labels[MAX_LABELS];
int errorCount;
char line[MAX_LINE_LENGTH+1];
char *words[MAX_WORDS_ON_LINE];
int wordCount;
int buffer[DB_DW_BUFFER_SIZE]; /* Output storage for DB and DW parsers */
int bufferIndex;
int dup; /* DUP value used by DB/DW parsers */
int pass; /* Which pass we're on */
/*
Remove path from fileName
*/
void removePath(char *fileName, char *filePath, int size)
{
int i;
for(i=strlen(filePath);i>0;i--)
{
if(filePath[i-1]=='/' || filePath[i-1]=='\\')
{
break;
}
}
strncpy(fileName,&filePath[i],size);
}
/*
Remove extension from fileName
*/
void removeExtension(char *noExt, char *fileName, int size)
{
int i;
for(i=strlen(fileName);i>0;i--)
{
if(fileName[i]=='.')
{
break;
}
}
if(i == 0)
{
i = strlen(fileName);
}
if(i > size)
{
i = size;
}
strncpy(noExt,fileName,i);
noExt[i] = 0;
}
/*
Return pointer to filename extension
*/
char *getExtension(char *fileName)
{
char *ptr;
ptr = fileName;
if(strlen(fileName)>3)
{
ptr += strlen(fileName);
do
{
ptr--;
if(*ptr == '.')
{
return ptr+1;
}
}
while(ptr != fileName);
}
return NULL;
}
/*
Get current date and time and return as string
*/
void getTimeDate(char *str, int size)
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
snprintf(str,size,"%d-%d-%d %02d:%02d:%02d",tm.tm_mday,tm.tm_mon + 1,tm.tm_year + 1900,tm.tm_hour,tm.tm_min,tm.tm_sec);
}
int bit_width(int m)
{
int i;
i = 0;
while(m > 2)
{
i++;
m >>= 1;
}
return i;
}
/*
Create a VHDL file of a RAM model initialized with the assembled memory image
*/
void createVHDLFile(char *fileName)
{
FILE *fp;
int i;
char entity[40];
char file[40];
char dateTime[80];
fp = fopen(fileName,"w");
if(fp == NULL)
{
fclose(fp);
printf("Could not open output file %s\n",fileName);
}
else
{
removePath(file, fileName, sizeof(file));
removeExtension(entity, file, sizeof(entity));
getTimeDate(dateTime, sizeof(dateTime));
fprintf(fp, VHDLFileStart, VERSION_STRING, file, dateTime, entity, bit_width(memorySize), entity, memorySize-1);
fputs("\t\t\t",fp);
for(i=0;i<memorySize;i++)
{
fprintf(fp, "X\"%04X\"",memoryImage[i]);
if(i<memorySize-1)
{
fputc(',',fp);
if((i+1)%8 == 0)
{
fputs("\n\t\t\t",fp);
}
}
else
{
fputs(");\n",fp);
}
}
fprintf(fp,VHDLFileEnd);
fclose(fp);
printf("VHDL file '%s' created.\n",fileName);
}
}
/*
Create MIF file
*/
void createMIFFile(char *fileName)
{
FILE *fp;
int i;
char file[40];
char dateTime[80];
fp = fopen(fileName,"w");
if(fp == NULL)
{
fclose(fp);
printf("Could not open output file %s\n",fileName);
}
else
{
removePath(file, fileName, sizeof(file));
getTimeDate(dateTime, sizeof(dateTime));
fprintf(fp, "-- Built with PASM version %s\n",VERSION_STRING);
fprintf(fp, "-- File name: %s\n",file);
fprintf(fp, "-- %s\n\n",dateTime );
fprintf(fp, "DEPTH = %d;\n", memorySize);
fprintf(fp, "WIDTH = 16;\n");
fprintf(fp, "ADDRESS_RADIX = HEX;\n");
fprintf(fp, "DATA_RADIX = HEX;\n");
fprintf(fp, "CONTENT\nBEGIN\n");
for(i=0;i<memorySize;i++)
{
fprintf(fp, "%03X : %04X ;\n",i,memoryImage[i]);
}
fprintf(fp, "END;\n");
fclose(fp);
printf("MIF file '%s' created.\n",fileName);
}
}
/*
Create MEM file
*/
void createMEMFile(char *fileName)
{
FILE *fp;
int i;
char file[40];
char dateTime[80];
fp = fopen(fileName,"w");
if(fp == NULL)
{
fclose(fp);
printf("Could not open output file %s\n",fileName);
}
else
{
removePath(file, fileName, sizeof(file));
getTimeDate(dateTime, sizeof(dateTime));
fprintf(fp, "#Format=AddrHex\n");
fprintf(fp, "#Depth=%d\n",memorySize);
fprintf(fp, "#Width=16\n");
fprintf(fp, "#AddrRadix=3\n");
fprintf(fp, "#DataRadix=3\n");
fprintf(fp, "#Data\n");
fprintf(fp, "#Built with PASM version %s\n",VERSION_STRING);
fprintf(fp, "#File name: %s\n",file);
fprintf(fp, "#%s\n",dateTime );
for(i=0;i<memorySize;i++)
{
fprintf(fp, "%03X : %04X\n",i,memoryImage[i]);
}
fprintf(fp, "# The end\n");
fclose(fp);
printf("MEM file '%s' created.\n",fileName);
}
}
/*
Split line into words delimited by whitespace
*/
void splitLine(void)
{
int i;
int nextWord;
int lineLength;
int quote;
wordCount = 0;
nextWord = 1; /* Searching for start of next word */
quote = 0; /* Ignore spaces inside quotes */
lineLength = strlen(line);
for(i=0;i<lineLength;i++)
{
/* End of line or start of comment */
if(line[i] == 0 || line[i] == ';')
{
break;
}
/* Toggle quotes (used to ignore spaces inside quotes) */
if(line[i] == '\"')
{
quote ^= 1;
}
if(nextWord == 1 && !isspace(line[i]))
{
/* Start of next word */
words[wordCount++] = &line[i];
nextWord = 0;
if(wordCount > MAX_WORDS_ON_LINE)
{
printf("Error line %d: too many words\n",currentLine);
errorCount++;
wordCount = 0;
return;
}
continue;
}
if(nextWord == 0 && isspace(line[i]) && quote == 0)
{
/* End of current word - mark with null */
line[i] = 0;
nextWord = 1;
}
}
/* Check if quotes are still open */
if(quote)
{
printf("Error line %d: no closing \"\n",currentLine);
errorCount++;
wordCount = 0;
}
}
/*
Return instruction opcode (0 to 15) or -1 if word is not an instruction
*/
int getInstruction(char *word)
{
int i;
for(i=0;i<NUMBER_OF_INSTRUCTIONS;i++)
{
if(strcmp(instructions[i],word)==0)
{
return i;
}
}
return -1;
}
/*
Perform various checks on newLabel if all is OK add to label list
*/
int addLabel(char *newLabel)
{
int i;
/* Check label is not too long */
if(strlen(newLabel) > MAX_LABEL_NAME_LENGTH)
{
printf("Error line %d: label too long\n",currentLine);
return 0;
}
/* Check if label is a reserved word */
if(getInstruction(newLabel) >= 0 ||
strcmp(newLabel,"ORG") == 0 ||
strcmp(newLabel,"DUP") == 0 ||
strcmp(newLabel,"DW") == 0 ||
strcmp(newLabel,"DB") == 0 ||
strcmp(newLabel,"NOP") == 0)
{
printf("Error line %d: reserved word %s found in column 1\n",currentLine,newLabel);
return 0;
}
/* Check if label is already defined */
for(i=0;i<numLabels;i++)
{
if(strcmp(labels[i].name,newLabel)==0)
{
printf("Error line %d: label %s already defined\n",currentLine,newLabel);
return 0;
}
}
/* Check if we have too many labels */
if(numLabels >= MAX_LABELS)
{
printf("Error line %d: too many labels\n",currentLine);
return 0;
}
/* Add label to list */
strcpy(labels[numLabels].name,newLabel);
labels[numLabels].value = currentAddress;
numLabels++;
return 1;
}
/*
Check if the first word is a valid label then add to the label list checking it does not
already exist. A label must start at column 1 and start with a alpha character and
only contain alpha-numeric and underscore characters.
*/
int firstWordLabel(void)
{
int i;
char c;
if(isalpha(line[0]))
{
for(i=0;i<MAX_LABEL_NAME_LENGTH;i++)
{
/* Check character by character */
c = words[0][i];
if(c == 0)
{
/* End of word - try to add to label list */
if(!addLabel(words[0]))
{
/* Error adding label, increment error counter and clear word count to stop parsing the rest of the line */
errorCount++;
wordCount = 0;
return 0;
}
else
{
/* New label created */
return 1;
}
}
if((!isalnum(c)) && c != '_')
{
break; /* Not a valid label */
}
}
}
return 0; /* Not a valid label */
}
/*
Handle the ORG directive, change currentAddress to value following ORG
firstWord indexes 'ORG' in the words array
*/
void parseORG(int firstWord)
{
int orgValue;
char *ptr;
/* Check for correct number of words and parameter starts with a number */
if((wordCount != firstWord + 2) || (!isdigit(words[firstWord+1][0])))
{
printf("Error line %d: ORG expects a single numeric value\n",currentLine);
errorCount++;
return;
}
/* Convert string to int */
orgValue = (int)strtol(words[firstWord+1],&ptr,0);
/* Check for garbage after number */
if(*ptr != 0)
{
printf("Error line %d: syntax\n",currentLine);
errorCount++;
return;
}
/* Check if ORG is backwards */
if(orgValue < currentAddress)
{
printf("Error line %d: ORG preceeds current address\n",currentLine);
errorCount++;
return;
}
/* Check ORG is within memory range */
if(orgValue >= memorySize)
{
printf("Error line %d: ORG exceeds memory size\n",currentLine);
errorCount++;
return;
}
/* Change current address to ORG value */
currentAddress = orgValue;
}
/*
Check if label exists, return label value or -1 if it does not exist.
*/
int findLabel(char *word)
{
int i;
for(i=0;i<numLabels;i++)
{
if(strcmp(labels[i].name,word)==0)
{
return labels[i].value;
}
}
return -1;
}
/*
Check and extract DUP from DW / DB
*/
int parseDUP(int firstWord)
{
char *endStrol;
/* Nothing to do if wordCount = firstword + 2 */
if(wordCount == firstWord + 2)
{
dup = 0;
return 1;
}
/* Check if line includes DUP */
if(wordCount == firstWord + 4)
{
if(strcmp(words[firstWord+2],"DUP")==0)
{
dup = (int)strtol(words[firstWord+3],&endStrol,0);
if(*endStrol==0)
{
if(dup > sizeof(buffer))
{
printf("Error line %d: DUP exceeds maximum\n",currentLine);
errorCount++;
return 0;
}
return 1;
}
}
}
printf("Error line %d: syntax\n",currentLine);
errorCount++;
return 0;
}
/*
Lower level DB parsing
Returns bufferIndex value which will be 0 if an error was found
*/
int parseDB2(char *word)
{
char *ptr;
char *endStrol;
long value;
bufferIndex = 0;
ptr = word;
while(1)
{
/* Check for number */
if(isdigit(*ptr))
{
value = strtol(ptr,&endStrol,0);
/* Check valid delimiters */
if(*endStrol == 0 || *endStrol == ',')
{
/* Range check */
if(value < 256)
{
/* Store value and advance index if space in buffer */
if(bufferIndex < DB_DW_BUFFER_SIZE)
{
buffer[bufferIndex++] = (int)value;
}
if(*endStrol == 0)
{
break; /* Finished */
}
else
{
ptr = endStrol + 1; /* More data, update and advance pointer past comma */
continue;
}
}
else
{
printf("Error line %d: DB value exceeds 255\n",currentLine);
bufferIndex = 0;
errorCount++;
break;
}
}
}
/* Check for string in quotes */
if(*ptr = '\"')
{
do /* Copy string to buffer */
{
ptr++;
if(*ptr == 0 || *ptr == '\"')
{
break;
}
if(bufferIndex < DB_DW_BUFFER_SIZE)
{
buffer[bufferIndex++] = (int)*ptr;
}
}while(1);
/* Check string terminated correctly */
if(*ptr == '\"')
{
ptr++;
if(*ptr == ',')
{
ptr++; /* More data */
continue;
}
if(*ptr == 0)
{
break; /* No more data */
}
}
}
printf("Error line %d: syntax\n",currentLine);
bufferIndex = 0;
errorCount++;
break;
}
return bufferIndex;
}
/*
Handle NOP - just a branch to the next instruction
*/
void parseNOP(int firstWord)
{
/* Make sure there is nothing else on ths line */
if(firstWord + 1 != wordCount)
{
printf("Error line %d: NOP does not take parameters\n",currentLine);
errorCount++;
return;
}
memoryImage[currentAddress] = 0xB000 + currentAddress + 1;
currentAddress++;
}
/*
Handle DB
*/
void parseDB(int firstWord)
{
int i;
/* Check if there is something else on this line */
if(wordCount < firstWord + 2)
{
printf("Error line %d: DB expects one or more values\n",currentLine);
errorCount++;
return;
}
/* Check for DUP */
if(!parseDUP(firstWord))
{
return; /* An error was found */
}
/* Parse the line */
if(parseDB2(words[firstWord+1]) == 0)
{
return; /* Error */
}
/* Duplicate */
if(dup > 0)
{
if(bufferIndex > 1)
{
printf("Error line %d: can only duplicate a single byte\n",currentLine);
errorCount++;
return;
}
for(i=1;i<dup;i++)
{
if(bufferIndex < DB_DW_BUFFER_SIZE)
{
buffer[bufferIndex] = buffer[0];
}
bufferIndex++;
}
}
/* Check for data overflow */
if(bufferIndex > DB_DW_BUFFER_SIZE)
{
printf("Error line %d: too much data for DB\n",currentLine);
errorCount++;
return;
}
/* Add pading byte if odd number of bytes */
if(bufferIndex%2)
{
buffer[bufferIndex++] = 0;
}
/* Check data fits into memory */
if(currentAddress + (bufferIndex/2) > memorySize)
{
printf("Error line %d: DB exceeds remaining memory\n",currentLine);
errorCount++;
return;
}
/* Copy to memory image */
for(i=0;i<bufferIndex;i+=2)
{
memoryImage[currentAddress++] = (buffer[i] << 8)|(buffer[i+1]);
}
}
/*
Lower level DW parsing
Returns bufferIndex value which will be 0 if an error was found
*/
int parseDW2(char *word)
{
char *ptr;
char *endStrol;
long value;
char string[MAX_LABEL_NAME_LENGTH];
int stringIndex;
bufferIndex = 0;
ptr = word;
while(1)
{
/* Check for number */
if(isdigit(*ptr))
{
value = strtol(ptr,&endStrol,0);
/* Check valid delimiters */
if(*endStrol == 0 || *endStrol == ',')
{
/* Range check */
if(value < 65536L)
{
/* Store value and advance index if space in buffer */
if(bufferIndex < DB_DW_BUFFER_SIZE)
{
buffer[bufferIndex] = (int)value;
}
bufferIndex++;
if(*endStrol == 0)
{
break; /* Finished */
}
else
{
ptr = endStrol + 1; /* More data, update and advance pointer past comma */
continue;
}
}
else
{
printf("Error line %d: DW value exceeds 65536\n",currentLine);
bufferIndex = 0;
errorCount++;
break;
}
}
}
/* Check for leter - start of label */
if(isalpha(*ptr))
{
/* Copy string */
stringIndex = 0;
while(1)
{
if(stringIndex < MAX_LABEL_NAME_LENGTH)
{
if(*ptr == 0 || *ptr == ',')
{
string[stringIndex] = 0;
break;
}
string[stringIndex++] = *ptr;
ptr++;
}
else
{
/* If string is too long, clear and break so error will be detected trying to resolve label */
string[0] = 0;
*ptr = 0;
break;
}
}
/* String copied ptr points to termination character */
if(pass==1)
{
/* Pass 1 - just increment index */
bufferIndex++;
}
else
{
/* Pass 2 - resolve label */
if(bufferIndex < DB_DW_BUFFER_SIZE)
{
value = (long)findLabel(string);
if(value < 0)
{
/* Could not find label */
printf("Error line %d: failed to resolve label\n",currentLine);
bufferIndex = 0;
errorCount++;
break;
}
else
{
buffer[bufferIndex] = (int)value;
}
}
bufferIndex++;
}
/* Check for more data on this line */
if(*ptr == 0)
{
break; /* Finished */
}
else
{
ptr++; /* More data move past comma */
continue;
}
}
/* If not a letter or number then syntax error */
printf("Error line %d: syntax\n",currentLine);
bufferIndex = 0;
errorCount++;
break;
}
return bufferIndex;
}
/*
Handle DW
*/
void parseDW(int firstWord)
{
int i;
/* Check if there is something else on this line */
if(wordCount < firstWord + 2)
{
printf("Error line %d: DW expects one or more values\n",currentLine);
errorCount++;
return;
}
/* Check for DUP */
if(!parseDUP(firstWord))
{
return; /* An error was found */
}
/* Parse the line */
if(parseDW2(words[firstWord+1]) == 0)
{
return; /* Error */
}
/* Duplicate */
if(dup > 0)
{
if(bufferIndex > 1)
{
printf("Error line %d: can only duplicate a single word\n",currentLine);
errorCount++;
return;
}
for(i=1;i<dup;i++)
{
if(bufferIndex < DB_DW_BUFFER_SIZE)
{
buffer[bufferIndex] = buffer[0];
}
bufferIndex++;
}
}
/* Check for data overflow */
if(bufferIndex > DB_DW_BUFFER_SIZE)
{
printf("Error line %d: too much data for DW\n",currentLine);
errorCount++;
return;
}
/* Check data fits into memory */
if(currentAddress + bufferIndex > memorySize)
{
printf("Error line %d: DW exceeds remaining memory\n",currentLine);
errorCount++;
return;
}
/* Copy to memory image */