-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1021 lines (879 loc) · 41.2 KB
/
index.js
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
export var assembler = {
// First Function : assembler.errorFunction
errorFunction: function (contents) {
/*const fileInput = document.getElementById("fileInput")
const file = fileInput.files[0]
const reader = new FileReader()*/
let i = 0;
let e,t;
let nbLigne = 0;
let nbError = 0;
let trouvOrg = false; // pour pouvoir detecter si on a écrit le ORG 2 fois : error
let trouvStart = false; // pour pouvoir detecter si on a écrit le START 2 fois / avant org ou bien on la pas éctit du tout : error
let trouvStop = false; // pour pouvoir detecter si on a écrit le STOP au milieu mais pas à la fin : error
let dirtybit = false; // Au cas où on a besoin d'arreter le traitement à cause d'une erreur qui va empecher ce dernier
let dirtystart = false;
let dirtyetiq = false;
let dirtyline = false;
let dirtyvar = false;
let dirtyspace=false;
let dirtyetiq2= false;
let tabNomVariable = [];
const messageDiv = document.getElementById("messageDiv");
messageDiv.innerHTML = "";
//const textDiv = document.getElementById("code"); elle est maintenent mise dans la fonction de click sur le button assembler
//reader.onload = (event) => {
//const contents = event.target.result;
//let contents = textDiv.innerHTML; il est devenue un paramètre à la fonction
//console.log(typeof(contents));
let lines = contents.split('\n');
console.log(contents);
lines = lines.filter(line => line.trim() !== ''); // remove the empty lines from my filecontents : the table lines which represents the lines of the file
let fileLength = lines.length; // file length without the empty lines
for (const line of lines) {
//textDiv.innerHTML += line;
nbLigne++;
//console.log("file length : "+fileLength);
if (line != "") {
let ligne_str = line.toString().trim(); // Enlever les espaces de debut et fin de line
ligne_str = ligne_str.replace(",", " "); // Remplacer les ',' par des blancs pour les traitements qui suivent
ligne_str = util.removeExtraSpaces(ligne_str); // Enlever plus d'un blanc ( remove extra spaces )
ligne_str = ligne_str.split(" "); // Diviser la ligne en mots séparés par des blans pour pouvoir les manipuler mot par mot . les mots sont mits dans un tableau
// Tester les conditions sur ORG
if (ligne_str[0].toUpperCase() == 'ORG')
{
if (trouvOrg == true) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut ecrire une seule fois ORG </span></p>";
nbError++;
}
if (i != 0 && !dirtystart && !trouvOrg) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color: #9ca3af; '> : Il faut commencer par ORG </span></p>";
nbError++;
}
if (ligne_str.length != 2 ) {
if(!trouvOrg){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color:#9ca3af; '> : Il faut specifier l'adresse debut dans ORG </span></p>";
nbError++;
dirtystart=true;
}
} else {
e = util.checkNumber(ligne_str[1], nbLigne);
nbError+=e;
}
trouvOrg=true;
} // fin conditions sur ORG
// tester les conditions sur START
else if (ligne_str[0].toUpperCase() == 'START')
{
if (trouvOrg == false) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il faut ecrire ORG au départ , avant d'arriver à START </span></p>";
nbError++;
}
if (trouvStop == true) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il ne faut pas que STOP soit écrit avant d'arriver à START </span></p>";
nbError++;
}
trouvStart = true;
} // fin conditions sur START
else if (ligne_str[0].toUpperCase() == 'SET') // tester les conditions sur SET
{
e = util.isVariableNameValid(ligne_str[1], nbLigne);
if (e != 0) {
nbError+=e;
t=nbLigne;
dirtybit = true;
}
if (trouvOrg == false) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut specifier l'adresse debut dans ORG avant de declarer les variables </span></p>";
nbError++;
}
if (trouvStart == true){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il faut declarer les variables avant START </span></p>";
nbError++;
}
if (ligne_str.length != 3 && (!dirtybit || t != nbLigne)) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut specifier le nom de la donnée et lui attribuer une valeur </span></p>";
nbError++;
dirtyvar= true;
t=nbLigne;
}
if (ligne_str.length == 3 ) {
e = util.checkNumber(ligne_str[2], nbLigne);
nbError+=e;
console.log("taille du tab var : ",tabNomVariable.length);
if (tabNomVariable.indexOf(ligne_str[1]) !== -1) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Variable deja declarée ! </span></p>";
nbError++;
} else {
tabNomVariable.push(ligne_str[1]);
}
}
}// fin conditions sur SET
else if (ligne_str[0].toUpperCase() == 'STOP') // tester les conditions sur STOP
{
if (trouvStop) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il faut ecrire STOP une seule fois </span></p>";
nbError++;
}
trouvStop = true;
}
else if (!trouvStop && (nbLigne == fileLength)) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut terminer par STOP </span></p>";
nbError++;
}
// fin condition sur stop
else // on est dans le cas d'une instruction
{
if (trouvStart==false){
console.log("edkhol hna !!!");
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut mettre START dans votre programme après la déclaration des variables </span></p>";
nbError++;
}
// traitement des étiquettes
if (line.indexOf(":") != -1) // On a trouvé les ':'
{
if (ligne_str[0][ligne_str[0].length - 1] == ":") // les ':' et l'etiquette sont collés
{
if (ligne_str.length == 1) // on a sauter de ligne après l'etiquette
{
dirtyetiq = true;
t=nbLigne;
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut mettre l'etiquette dans la meme ligne que l'instruction </span></p>";
nbError++;
} else { // l'etiq est correcte on a juste à décaler pour travailler avec la suite en tant que instruction normale
ligne_str.shift();
// début traitement ins normale
if (coding.getCop(ligne_str[0]) == -1 && !dirtyline) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut que ca soit une instruction parmi le jeu d'instructions </span></p>";
nbError++;
}
if (line.indexOf(",") == -1 && !dirtyline) {
if (!util.instUnSeulOp(ligne_str[0])) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il faut mettre ',' entre les operandes </span></p>";
nbError++;
}
}
if (line.indexOf("[") != -1 && !dirtyline) {
if ((line.slice(line.indexOf("[") + 1, line.indexOf("]"))).indexOf(" ") != -1) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut pas laisser des espaces entre '[' et ']' </span></p>";
nbError++;
dirtyspace=true;
t=nbLigne;
}
else if (coding.regexi(line.slice(line.indexOf("[") + 1, line.indexOf("]")))) {
if (!coding.regAdrExi(line.slice(line.indexOf("[") + 1, line.indexOf("]")))) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il faut mettre un registre d'adressage ' BX SI DI ' entre '[' et ']' </span></p>";
nbError++;
dirtyspace=true;
t=nbLigne;
}
}
}
if (!dirtyline){
if (!coding.regexi(ligne_str[1]) && ligne_str[1].indexOf("[") == -1 ){
if (tabNomVariable.indexOf(ligne_str[1]) == -1 && (!dirtyvar || t!=nbLigne) && !dirtyetiq2){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut declarer les variables </span></p>";
console.log("first herre the first param is not a reg");
nbError++;
}
}else if((!dirtyspace || t!=nbLigne) && !coding.regexi(ligne_str[2]) ){
if (tabNomVariable.indexOf(ligne_str[2]) == -1 && (!dirtyvar || t!=nbLigne) && !dirtyetiq2){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut declarer les variables </span></p>";
console.log("first herre the second param is not a reg");
nbError++;
}
}
}
// fin traitement ins normale
}
} else { // les ':' et l'etiquette ne sont pas collés
if (ligne_str[0].indexOf(":") != -1){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut laisser un espace entre l'etiquette et l'instruction </span></p>";
nbError++;
}else {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut pas laisser des espaces entre le nom de l'etiquette et ':' </span></p>";
nbError++;}
if (ligne_str.length == 2) // on a sauter de ligne après l'etiquette
{
dirtyetiq = true;
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il faut mettre l'etiquette dans la meme ligne que l'instruction </span></p>";
nbError++;
}else { //pas de saut de ligne
ligne_str.shift();
ligne_str.shift();
}
}
} // fin condition ':' trouvé
else if (!dirtyetiq ) { // on a pas trouvé une étiq avec ':'
if (ligne_str.length > 3 && line.indexOf("[") == -1) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut mettre ':' à la fin du nom de l'etiquette voulue </span></p>";
nbError++;
dirtyetiq2= true;
} else if (ligne_str.length==1){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut mettre ':' à la fin du nom de l'etiquette voulue et ne pas sauter de ligne après l'étiquette ! </span></p>";
dirtyline=true;
nbError++;
}
if ( coding.getCop(ligne_str[0]) == -1 && !dirtyline) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color:#9ca3af; '> : Il faut que ca soit une instruction parmi le jeu d'instructions </span></p>";
nbError++;
}
if (line.indexOf(",") == -1 && !dirtyline) {
if (!util.instUnSeulOp(ligne_str[0])) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut mettre ',' entre les operandes </span></p>";
nbError++;
}
}
if (line.indexOf("[") != -1 && !dirtyline) {
if ((line.slice(line.indexOf("[") + 1, line.indexOf("]"))).indexOf(" ") != -1) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut pas laisser des espaces entre '[' et ']' </span></p>";
nbError++;
dirtyspace=true;
t=nbLigne;
}
else if (coding.regexi(line.slice(line.indexOf("[") + 1, line.indexOf("]")))) {
if (!coding.regAdrExi(line.slice(line.indexOf("[") + 1, line.indexOf("]")))) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut mettre un registre d'adressage ' BX SI DI ' entre '[' et ']' </span></p>";
nbError++;
dirtyspace=true;
t=nbLigne;
}
}
}
if (!dirtyline){
if (!coding.regexi(ligne_str[1]) && ligne_str[1].indexOf("[") == -1 ){
if (tabNomVariable.indexOf(ligne_str[1]) == -1 && (!dirtyvar || t!=nbLigne) && !dirtyetiq2){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut declarer les variables </span></p>";
console.log("first herre the first param is not a reg");
nbError++;
}
}else if((!dirtyspace || t!=nbLigne) && !coding.regexi(ligne_str[2]) ){
if (tabNomVariable.indexOf(ligne_str[2]) == -1 && (!dirtyvar || t!=nbLigne) && !dirtyetiq2){
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span><span style='color: #9ca3af; '> : Il faut declarer les variables </span></p>";
console.log("first herre the second param is not a reg");
nbError++;
}
}
}
}
} // fin traitement instructions
i++;
}
};
console.log("taille du tab var : ",tabNomVariable.length);
if (nbError!=0){
messageDiv.innerHTML += "<p><span style='color: #9ca3af; '>Nombre d'erreur : <span style='color: red;'> " + nbError + "</span></span></p>";
} else {
messageDiv.innerHTML += "<p><span style='color: #9ca3af; '>Nombre d'erreur : <b><span style='color: green;'> " + nbError + "</span></b></span></p>";
}
console.log("Nombre d'erreur : ",nbError);
//};
// reader.readAsText(file);
},// fin fonction error
fromFileInputToTextZone: function (fileId, textZoneId) {
const fileInput = document.getElementById(fileId);
const file = fileInput.files[0];
const reader = new FileReader();
const textDiv = document.getElementById(textZoneId);
reader.onload = (event) => {
const contents = event.target.result;
let lines = contents.split('\n');
lines = lines.filter(line => line.trim() !== '');
const fileLength = lines.length;
let textContent = "";
for (const line of lines) {
textContent += line + "\n";
}
textDiv.textContent = textContent;
};
reader.readAsText(file);
},
fromTextZoneToPASSE_File: function(){
},
}
// coding.js no need to import it
export var coding = {
// debut coderInst "codage des instructions " return tableau des mots memoir apres codage de l'instruction
coderInst: function (strLigne, adr, dataTab) {
let str = "";
let instrTab = new Array();
if (strLigne[0][strLigne[0].length - 1] == ":") {
str = strLigne[0];
strLigne.shift();
}
instrTab.push(
new CaseMc(adr, util.binaryToHex(coding.getCode(strLigne)), str)
);
if (coding.getFormat(strLigne) == "0") {
for (let j = 0; j < instrTab.length; j++) instrTab[j].setVal(util.remplirZero(instrTab[j].getVal(), 4, 0));
return instrTab;
} else if (strLigne[0][strLigne[0].length - 1].toUpperCase() == "I") {
if (coding.modeAdr(strLigne) == "00" && coding.getDest(strLigne) == "0") {
if (strLigne[1].indexOf("[") != -1) {
adr = this.incrementHex(adr, 1);
instrTab.push(
new CaseMc(adr, strLigne[1].slice(1, strLigne[1].length - 1), "") // remplir 0 remplirZero
);
} else {
let indice = util.chercherDansTableau(dataTab, strLigne[1]);
adr = this.incrementHex(adr, 1);
instrTab.push(new CaseMc(adr, dataTab[indice].getVal(), ""));
}
adr = this.incrementHex(adr, 1);
if (strLigne[2].indexOf("H") != -1) {
instrTab.push(
new CaseMc(adr, strLigne[2].slice(0, strLigne[2].length - 1), "")
);
} else instrTab.push(new CaseMc(adr, strLigne[2], ""));
for (let j = 0; j < instrTab.length; j++) instrTab[j].setVal(util.remplirZero(instrTab[j].getVal(), 4, 0));
return instrTab;
}
if (coding.modeAdr(strLigne) == "10" && coding.getDest(strLigne) == "0") {
instrTab.push(
new CaseMc(
this.incrementHex(adr, 1),
parseInt(getSubstringBetweenChars(strLigne[1], "+", "]")).toString(
16
)
)
);
}
for (let j = 0; j < instrTab.length; j++) instrTab[j].setVal(util.remplirZero(instrTab[j].getVal(), 4, 0));
return instrTab;
} else if (coding.modeAdr(strLigne) == "00") {
if (coding.getDest(strLigne) == "0") {
if (strLigne[1].indexOf("[") != -1) {
adr = util.incrementHex(adr, 1);
instrTab.push(
new CaseMc(adr, strLigne[1].slice(1, strLigne[1].length - 2), "") // remplir 0 remplirZero
);
} else {
let indice = util.chercherDansTableau(dataTab, strLigne[1]);
adr = util.incrementHex(adr, 1);
//instrTab.push(new CaseMc(adr, dataTab[indice].getVal(), ""));
instrTab.push(new CaseMc(adr, dataTab[indice].getAdr(), ""));
}
} else {
if (strLigne[2].indexOf("[") != -1) {
adr = util.incrementHex(adr, 1);
instrTab.push(
new CaseMc(adr, strLigne[2].slice(1, strLigne[2].length - 2), "") // remplir 0 remplirZero
);
} else {
let indice = util.chercherDansTableau(dataTab, strLigne[2]);
adr = util.incrementHex(adr, 1);
instrTab.push(new CaseMc(adr, dataTab[indice].getAdr(), ""));
}
}
} else {
if (coding.modeAdr(strLigne) == "10") {
if (coding.getDest(strLigne) == "0") { //MOV [BX+3], AX
let regEtDepl = strLigne[1].slice(1, strLigne[1].length - 1);
let depl = "";
if (this.regexi(regEtDepl.substring(0, 2))) { depl = regEtDepl.substring(regEtDepl.lastIndexOf("+") + 1); }
else { depl = regEtDepl.substring(0, regEtDepl.length - 3); }
instrTab.push(new CaseMc(adr, depl.toString(16), ""));
}
else {
let regEtDepl = strLigne[2].slice(1, strLigne[2].length - 1);
let depl = "";
if (this.regexi(regEtDepl.substring(0, 2))) { depl = regEtDepl.substring(regEtDepl.lastIndexOf("+") + 1); }
else { depl = regEtDepl.substring(0, regEtDepl.length - 3); }
instrTab.push(new CaseMc(adr, depl.toString(16), ""));
}
} else if (coding.modeAdr(strLigne) == "10") {
}
}
for (let j = 0; j < instrTab.length; j++) { instrTab[j].setVal(util.remplirZero(instrTab[j].getVal(), 4, 0)) }
return instrTab;
}, // fin coder instruction
// return code binaire de l'instruction
getCode: function (str) {
let code;
if (str.length == 3) {
if (this.regexi(str[1]) && this.regexi(str[2])) {
code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[1]), this.getReg(str[2]));
}
if (!this.regexi(str[1]) && this.regexi(str[2])) {
if (this.regexi(str[1].slice(1, str[1].length - 1)))
code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[1].slice(1, str[1].length - 1)), this.getReg(str[2]));
else if ((str[1].indexOf("[") != -1) && (str[1].indexOf("+") != -1)) {
console.log("ttt");
let regEtDepl = str[1].slice(1, str[1].length - 1);
let reg = "";
if (this.regexi(regEtDepl.substring(0, 2))) { reg = regEtDepl.substring(0, 2); }
else { reg = regEtDepl.slice(-2); }
code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[2]), this.getReg(reg));
}
else { code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[2]), "000"); }
}
if (this.regexi(str[1]) && !this.regexi(str[2])) {
if (this.regexi(str[2].slice(1, str[2].length - 1))) { code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[1]), this.getReg(str[2].slice(1, str[2].length - 1))); }
else if ((str[2].indexOf("[") != -1) && (str[2].indexOf("+") != -1)) {
let reg = "";
let regEtDepl = str[2].slice(1, str[2].length - 1);
if (this.regexi(regEtDepl.substring(0, 2))) { reg = regEtDepl.substring(0, 2); }
else { reg = regEtDepl.slice(-2); }
code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[1]), this.getReg(reg));
}
else { code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[1]), "000"); }
}
}
else {
if (this.regexi(str[1])) {
code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[1]), "000");
}
else if (this.regexi(str[1].slice(1, str[1].length - 1))) {
code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), this.getReg(str[1].slice(1, str[1].length - 1)), "000");
} else {
code = this.getCop(str[0]).concat(this.modeAdr(str), this.getFormat(str), this.getDest(str), "000", "000");
}
}
return code;
}, // fin getcode
// return code operation de l'instruction
getCop: function (str) {
let ins;
switch (str.toUpperCase()) {
case "MOV": ins = "000000"; break;
case "ADD": ins = "000001"; break;
case "ADA": ins = "000010"; break;
case "SUB": ins = "000011"; break;
case "SUBA": ins = "000100"; break;
case "CMP": ins = "000101"; break;
case "OR": ins = "000110"; break;
case "AND": ins = "000111"; break;
case "SHR": ins = "001000"; break;
case "SHL": ins = "001001"; break;
case "ROL": ins = "001010"; break;
case "ROR": ins = "001011"; break;
case "JZ": ins = "001100"; break;
case "JNZ": ins = "001101"; break;
case "JC": ins = "001110"; break;
case "JS": ins = "010000"; break;
case "JNC": ins = "001111"; break;
case "JNS": ins = "010001"; break;
case "JO": ins = "010010"; break;
case "JNO": ins = "010011"; break;
case "JE": ins = "010100"; break;
case "JNE": ins = "010101"; break;
case "LOAD": ins = "010110"; break;
case "STORE": ins = "010111"; break;
case "INC": ins = "011000"; break;
case "DEC": ins = "011001"; break;
case "NOT": ins = "011010"; break;
case "JMP": ins = "011011"; break;
case "IN": ins = "011100"; break;
case "OUT": ins = "011101"; break;
case "START": ins = "011111"; break;
case "STOP": ins = "011110"; break;
case "MOVI": ins = "100000"; break;
case "ADDI": ins = "100001"; break;
case "ADAI": ins = "100010"; break;
case "SUBI": ins = "100011"; break;
case "SBAI": ins = "100100"; break;
case "CMPI": ins = "100101"; break;
case "ORI": ins = "100110"; break;
case "ANDI": ins = "100111"; break;
case "LOADI": ins = "110110"; break;
default: ins = "-1"; break;
}
return ins;
}, // fin get code operation
// return code de registre
getReg: function (reg) {
let code;
switch (reg.toUpperCase()) {
case "AX": code = "000"; break;
case "BX": code = "001"; break;
case "CX": code = "010"; break;
case "DX": code = "011"; break;
case "EX": code = "100"; break;
case "FX": code = "101"; break;
case "SI": code = "110"; break;
case "DI": code = "111"; break;
default: code = "000"; break;
}
return code;
}, // fin get code registre
// return true si reg est un registre false sinon
regexi: function (reg) {
reg = reg.toUpperCase();
if (reg == "AX" || reg == "BX" || reg == "CX" || reg == "DX" || reg == "EX" || reg == "FX" || reg == "DI" || reg == "SI")
return true;
else return false;
}, // fin regexiste
// return true si reg est un registre d'adressage
regAdrExi: function (reg) {
reg = reg.toUpperCase();
if (reg == "BX" || reg == "SI" || reg == "DI") {
return true;
} else return false;
}, // fin registre adressage existe
// Mode adressage
// return true si mode adressage direct
modeDirect: function (tabIns) {
if (this.regexi(tabIns[1]) && this.regexi(tabIns[2])) {
return true;
}
if (!(this.regAdrExi(tabIns[1].slice(1, tabIns[1].length - 1)) || this.regAdrExi(tabIns[2].slice(1, tabIns[2].length - 1))
) && !this.modeBaseIndx(tabIns)) {
return true;
}
if (tabIns[0][tabIns[0].length - 1] == "I" && (this.regexi(tabIns[1]) || (!this.regAdrExi(tabIns[1].slice(1, tabIns[1].length - 1)) &&
!this.modeBaseIndx(tabIns)))) {
return true;
}
}, // fin mode direct
// indirect
modeIndirct: function (tabIns) {
if ((this.regAdrExi(tabIns[1].slice(1, tabIns[1].length - 1)) && this.regexi(tabIns[2])) || (this.regAdrExi(tabIns[2].slice(1, tabIns[2].length - 1)) &&
this.regexi(tabIns[1])))
return true;
if (tabIns[0][tabIns[0].length - 1] == "I" && this.regAdrExi(tabIns[1].slice(1, tabIns[1].length - 1)))
return true;
else return false;
}, // fin mode indirect
// mode base ou indexé
modeBaseIndx: function (tabIns) {
if (this.regexi(tabIns[1]) && tabIns[2].slice(1, tabIns[2].length - 1).indexOf("+") != -1)
return true;
if (this.regexi(tabIns[2]) && tabIns[1].slice(1, tabIns[1].length - 1).indexOf("+") != -1)
return true;
if (tabIns[0][tabIns[0].length - 1] == "I" && tabIns[1].slice(1, tabIns[1].length - 1).indexOf("+") != -1)
return true;
else return false;
}, // fin mode base ou indexé
// return le code de mode d'adressage
modeAdr: function (tabIns) {
if (tabIns.length == 3) {
if (tabIns[0] == "SHR" || tabIns[0] == "SHL" || tabIns[0] == "ROL" || tabIns[0] == "ROR"
) {
console.log("Mode d'Adressage : 00 (direct) ");
return "00";
} else if ((util.getDel(tabIns[1], "[") != "" && tabIns[1].indexOf("[") != -1) ||
(util.getDel(tabIns[2], "[") != "" && tabIns[2].indexOf("[") != -1)) {
console.log("Mode d'Adressage : 11 (Direct Indexé) ");
return "11";
} else {
if (this.modeDirect(tabIns)) {
console.log("Mode d'Adressage : 00 (direct) ");
return "00";
} else if (this.modeIndirct(tabIns)) {
console.log("Mode d'Adressage : 01 (Indirect) ");
return "01";
} else if (this.modeBaseIndx(tabIns)) {
console.log("Mode d'Adressage : 10 (Basé Indexé) ");
return "10";
}
}
} else if (tabIns.length == 2) {
if (tabIns[0] == "LOAD") {
if (this.regAdrExi(tabIns[1].slice(1, tabIns[1].length - 1)))
return "01";
else if (this.getDel(tabIns[1], "[") != "" && tabIns[1].indexOf("[") != -1)
return "11";
else return "00";
} else return "00";
}
}, // fin modeAdr
// return destination
getDest: function (str) {
if (this.regexi(str[1].toUpperCase())) {
return "1";
} else {
return "0";
}
}, // fin get destination
// return Format
getFormat: function (ligne_str) {
let taille = ligne_str.length;
if (taille == 2) {
if (this.regexi(ligne_str[1]) || this.regAdrExi(ligne_str[1].slice(1, ligne_str[1].length - 1)))
return "0";
else return "1";
} else if (taille == 3) {
if ((this.regexi(ligne_str[1]) && this.regexi(ligne_str[2])) || (this.regAdrExi(ligne_str[1].slice(1, ligne_str[1].length - 1)) &&
this.regexi(ligne_str[2])) || (this.regAdrExi(ligne_str[2].slice(1, ligne_str[2].length - 1)) && this.regexi(ligne_str[1])))
return "0";
else return "1";
}
}, // fin getFormat
}
// class case mémoire
class CaseMc {
#val;
#adr;
#etiq;
constructor(adr, val, etiq) {
this.#adr = adr;
this.#val = val;
this.#etiq = etiq;
}
setVal(val) {
this.#val = val;
}
getVal() {
return this.#val;
}
getEtiq() {
return this.#etiq;
}
getAdr() {
return this.#adr;
}
afficher() {
console.log(`ETIQ: ${this.#etiq} @${this.#adr} ${this.#val}`);
}
/*getCOP() {
let val = this.hexToBinary(this.getVal());
return code.substring(0, 6);
}*/
hexToBinary(hex) {
return parseInt(hex, 16).toString(2);
}
}
// util.js no need to import it
export var util = {
chercherDansTableau: function (tableau, valeur) {
// change the name to chercherEtiq
let i = 0;
while (i < tableau.length && tableau[i].getEtiq() != valeur) {
i++;
}
return i;
}, // fin chercherDansTableau
// return element de tableau contant adr
chercherAdr: function (tableau, adr) {
let i = 0;
while (i < tableau.length && tableau[i].getAdr() != adr) {
i++;
}
return i;
},// chercherAdr
// supprimer les espaces dans une chaine de caractere
removeExtraSpaces: function (str) {
return str.replace(/\s+/g, " ");
}, // fin removeExtraSpaces
// incrementer n fois hex en hexadecimal
incrementHex: function (hex, n) {
let decimal = parseInt(hex, 16);
for (let i = 0; i < n; i++) { decimal = decimal + 1; }
return decimal.toString(16).toUpperCase();
}, // fin incrementHex
//
remplirZero: function (str, n, gd) {
//gd=0 -> des zeros à gauche , gd=1 -> à droite
var s = "";
const length = str.length;
if (gd == 0) {
for (let k = 0; k < n - length; k++) { s += "0"; }
return s + str;
} else {
if (gd == 1) {
for (let k = 0; k < n - length; k++) { str += "0"; }
return str;
} else { throw new Error("Le troisieme parametre doit etre 0 ou 1."); }
}
}, // fin remplirZero
// Find position of delimiter
getDel: function (str, delimiter) {
let position = str.indexOf(delimiter);
if (position !== -1) { // Check if delimiter is found
str = str.substring(0, position);
}// Delete characters after delimiter
return str;
}, //fin getDel
// faire l'addition en hexadecemal
additionHexa: function (x, y) {
return (parseInt(x, 16) + parseInt(y, 16)).toString(16).toUpperCase();
},// fin additionHexa
getSubstringBetweenChars: function (str, startChar, endChar) {
let startIndex = str.indexOf(startChar);
if (startIndex === -1) { return ""; }
startIndex += 1;
const endIndex = str.indexOf(endChar, startIndex);
if (endIndex === -1) { return ""; }
return str.substring(startIndex, endIndex);
}, // fin getSubstringBetweenChars
// convertir un nombre binaire en hexadecimal
binaryToHex: function (binary) {
const decimal = parseInt(binary, 2);
const hex = decimal.toString(16);
return hex.toUpperCase();
}, // fin binaryToHex
// verifie si un nombre est en hexadecimal
estHexadecimal: function (chaine) {
return !isNaN(parseInt(chaine, 16));
}, // fin estHexadecimal
// Verifie si le nom de la variable est valide
isVariableNameValid: function (str, nbLigne) {
let nbError=0;
let messageDiv = document.getElementById("messageDiv");
if (!str) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color: #9ca3af; '> : Il faut que le nom de la variable soit valide [chaine vide ] </span></p>";
nbError++;
} // Vérifier si la chaîne est vide ou null
// Vérifier si le premier caractère est une lettre, un underscore ou un dollar
let firstChar = str.charAt(0);
if (!/^[a-zA-Z_$]/.test(firstChar)) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color: #9ca3af; '> : Il faut que le nom de la variable soit valide [premier caractère invalide ] </span></p>";
nbError++;
}
// Vérifier si les autres caractères sont des lettres, des chiffres, des underscores ou des dollars
for (let i = 1; i < str.length; i++) {
let char = str.charAt(i);
if (!/^[a-zA-Z0-9_$]/.test(char)) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color: #9ca3af; '> : Il faut que le nom de la variable soit valide [caractères invalides ] </span></p>";
nbError++;
}
}
//verifier si ce n'est pas sun nom de registre
if (coding.regexi(str)) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color: #9ca3af; '> : Il faut que le nom de la variable soit valide , il ne faut pas que ça soit un nom de registre </span></p>";
nbError++;
}
// sinon : La chaîne est valide
return nbError;
}, // fin nom variable valide
// Verifie si le nombre est en hexadecimal et est valide
checkNumber: function (ligne_str, nbLigne) {
let nbError=0;
let messageDiv = document.getElementById("messageDiv");
if (ligne_str.indexOf("H") == -1) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color: #9ca3af; '> : Il faut ecrire 'H' qui signifie la base hexadecimal </span></p>";
nbError++;
} else {
ligne_str = ligne_str.slice(0, ligne_str.length - 1);
if (!this.estHexadecimal(ligne_str)) {
messageDiv.innerHTML += "<p><span style='color: red;'> Ligne " + nbLigne + "</span> <span style='color: #9ca3af; '> : Il faut donner une valeur hexadecimal valide </span></p>";
nbError++;
}
}
return nbError;
},// fin verification nombre
// verifie si l'instruction est à un seul opérande
instUnSeulOp: function (str) {
str = str.toUpperCase();
if (str == 'ADA' || str == 'ADAI' || str == "SBA" || str == "SBAI" || str == 'STOP' || str == 'START' || str == 'IN' || str == 'OUT' || str == 'JMP' ||
str == 'NOT' || str == 'DEC' || str == 'INC' || str == 'STORE' || str == 'LOAD' || str == 'LOADI' || str == 'JNE' || str == 'JE' ||
str == 'JNO' || str == 'JO' || str == 'JNS' || str == 'JS' || str == 'JNC' || str == 'JC' || str == 'JNZ' || str == 'JZ') return true;
else return false;
},// fin instruction à un seul opérande
// récupère le code ASCII d'un caractère et le retourne en HEX
getCodeASCIIHex: function (caractere) {
return caractere.charCodeAt(0).toString(16);
},// fin get code ASCII
}// fin de l'objet " Util"
const testButton = document.getElementById("compile_id");
const textDiv = document.getElementById("code");
testButton.addEventListener("click", () => {
let contents = textDiv.value; // récupère le contenu initial de la div
textDiv.addEventListener('input', function() {
contents = textDiv.value; // met à jour le contenu de la variable lorsque la div est modifiée
});
assembler.errorFunction(contents);
});
const fileInputButton = document.getElementById("Load")
fileInputButton.addEventListener("click",() => {
assembler.fromFileInputToTextZone("fileInput","code");
})
var saveButton = document.getElementById("save");
var codeTextarea = document.getElementById("code");
saveButton.addEventListener("click", function() {
let codeText = codeTextarea.value;
console.log(codeText);
let blob = new Blob([codeText], {type: "text/plain;charset=utf-8"});
saveAs(blob, "mon_fichier.PASSE");
});
/*document.getElementById("saveButton").addEventListener("click", function() {
var textToSave = document.getElementById("code").value;
var blob = new Blob([textToSave], {type: "text/plain;charset=utf-8"});
saveAs(blob, "firstTry.PASSE");
});*/
/*document.addEventListener("keydown", function(e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
var textToSave = document.getElementById("code").value;
var blob = new Blob([textToSave], {type: "text/plain;charset=utf-8"});
saveAs(blob, "firstTry.PASSE");
}
}, false);*/
const simulbtn = document.getElementById("simulate_id");
const emuleSave = document.getElementById("sml");
const simulArchi = document.getElementById("arch-id");
simulbtn.addEventListener("click", () => {
emuleSave.style.visibility ='hidden';
simulArchi.style.visibility='visible';
}) ;
var returnBtn = document.getElementById("return");
returnBtn.addEventListener("click",()=>{
simulArchi.style.visibility='hidden';
emuleSave.style.visibility='visible';
}) ;
const dataCOel= document.getElementById('dataCO');
const dataRAMel= document.getElementById('dataRAM');
const dataRIMel= document.getElementById('dataRIM');
const dataRIel= document.getElementById('dataRI');
const dataACCel= document.getElementById('dataACC');
const dataEUAL1el= document.getElementById('dataEUAL2');
const dataEUAL2el= document.getElementById('dataEUAL2');
const dataREGel= document.getElementById('dataREG');
const dataREG1el= document.getElementById('dataREG1');
const dataREG2el= document.getElementById('dataREG2');
const dataTDAel= document.getElementById('dataTDA');
const dataC1el= document.getElementById('dataC1');
const dataC2el= document.getElementById('dataC2');
const dataC3el= document.getElementById('dataC3');
const dataC4el= document.getElementById('dataC4');
const dataC5el= document.getElementById('dataC5');
const dataDATAel= document.getElementById('dataDATA');
//const g2=document.getElementById('g2');
function moveData(Bus){
var computedStyle = window.getComputedStyle(Bus);
var left = computedStyle.getPropertyValue('left');
console.log(left);
Bus.style.visibility='visible';
if (left === '0px'){
Bus.style.animation='slide-left 3s';
}
else {
console.log('hello world');
Bus.style.animation='slide-right 3s';
}
}
function selectElement(Element, delay ,string) {
setTimeout(function() {
if (Element.className==='dataflux'){
moveData(Element);
}else{
Element.textContent = string ;
}
}, delay);
}
var co = document.getElementById("CO");
var ram = document.getElementById("RAM");
var rim = document.getElementById("Rimcontent");
var ri = document.getElementById("RI");
var caseMemoire = document.getElementById("caseMemoire");
let instTAB = ['AAAA',1,2,3,4];
function premierePhase(adr){
var delay = 0;
selectElement(co, delay , adr);