-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
1135 lines (941 loc) · 41.1 KB
/
Main.java
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
package gfg;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.Scanner;
public class Main implements ActionListener
{
private JFrame frame;
private JLabel jLabel ;
private JTextPane jTextArea ;
private JScrollPane jScrollPane ;
private JButton Sc_btn ;
private JButton jbt1 ;
private JButton jbt2;
private JButton jbt3;
private JButton jbt4;
private JButton jbt5;
private JButton jbt6;
private JButton jbt7;
private JButton jbt8;
private JButton give_input ;
private JTextField input ;
private int T = -1;
private String [] M ;
private JButton play ;
private JMenuBar mb;
private JMenu file,edit,help;
private JMenuItem cut,copy,paste,selectAll;
JMenuItem item_hint , show_mem ;
private JTextArea hint_jtext_area;
private boolean R = false ;
private int I = 0 ;
private int [] D ;
private boolean Hlt = true ;
boolean overwrite = false ;
private String [] AR ;//field 0 to 2 refers to control bits and field 3 refers to data stored .
private String [] PC ;
private String [] DR ;
private String [] AC ;
private String [] IR ; // IR is different from others , it will be have just two field ,one is LD ,other is ist s name .
private String [] TR ;
private String [] Bus_dates = new String[2];
private boolean FGO = false;
private boolean FGI = false;
private boolean IEN = false;
private boolean E = false;
private int D_E = 0 ;
private boolean Write_Mem = false ;
private int D_R = 0 ;
private int D_IEN = 0 ;
private int flag_out = 0 ;
private int AC_code = 0 ;
private String OUTR = "0";
public Main()
{
// String a = "110001";
//int fg = Integer.parseInt(a);
// fg = ~fg ;
//System.out.println(fg);
/* String ar = "1"+"0".repeat(3);
int g = Integer.parseInt(ar,2) ;
String fr = "1"+"0".repeat(3);
int f= Integer.parseInt(fr,2);
int decc = f+g;
System.out.println(Integer.toBinaryString(decc));
*/
//String fg = ar.substring(0,3); [)
//ar = pc = dr = ac = ir = tr = "0".repeat(16);
set_graphic();
initial_registers();
M = new String[4096];
String adress101 = Integer.toBinaryString(101);
// M[100] = "0001" + "0".repeat(12-adress101.length()) + adress101;
// input to AC
/* M[100] = "0101" + "0".repeat(12-adress101.length()) + adress101;
M[101] = "0".repeat(14) + "11" ; // data
M[1] = "11111" + "0".repeat(11);*/
String adress103 = Integer.toBinaryString(103);
String adress200 = Integer.toBinaryString(200);
String adress300 = Integer.toBinaryString(300);
String adress105 = Integer.toBinaryString(105);
String adress500 = Integer.toBinaryString(500);
// LD 3 in AC + 4 with ADD instruction + our input with STA , pB11 , BUN , ADD . Finally we will get 1000 or another numbers in order to the time of the interrupt at AC int this special example
// M[1] = "11111" + "0".repeat(11) ;
// M[2] = "1100" + "0".repeat(12) ; // BUN
/* M[100] = "0010" + "0".repeat(12-adress200.length()) + adress200 ; // LDA
M[101] = "1111" + "000010000000" ; // ION
M[200] = "0".repeat(14) + "11" ; // number 3
M[102] = "0001" + "0".repeat(12-adress300.length()) + adress300 ; // ADD
M[300] = "0".repeat(13) + "100" ;
M[103] = "0011" + "0".repeat(12-adress105.length()) + adress105 ; // STA
//M[105] will store 7 // in this example we should request for interrupt here .
M[104] = "0001" + "0".repeat(12-adress105.length()) + adress105 ; // ADD
*/
/* M[100] = "1111" + "000010000000" ; // ION
M[101] = "0010" + "0".repeat(12-adress200.length()) + adress200 ; // LDA */ // interrupt have to occur after this instruction
/* M[100] = "0111" + "00001" + "0".repeat(7) ; // shr
// M[101] = "0111" + "0001" + "0".repeat(8) ; // CME
M[101] = "0111" + "01" + "0".repeat(10) ; // CLE
M[102] = "0111" + "0001" + "0".repeat(8) ; // CME
M[103] = "0111" + "00001" + "0".repeat(7) ; // shr
M[104] = "0111" + "00001" + "0".repeat(7) ; // shr*/
/* M[100] = "0111" + "0001" + "0".repeat(8) ; // CME
M[101] = "0111" + "0001" + "0".repeat(8) ; // CME
M[102] = "0111" + "01" + "0".repeat(10) ; // CLE
M[103] = "0111" + "0001" + "0".repeat(8) ; // CME
M[104] = "0111" + "01" + "0".repeat(10) ; // CLE*/
/* M[200] = "0".repeat(14) + "11" ; // number 3
M[100] = "0001" + "0".repeat(12-adress200.length()) + adress200 ; // ADD
M[101] = "0000" + "0".repeat(12-adress300.length()) + adress300 ; // AND
M[300] = "1".repeat(16);
M[100] = "0111" + "0".repeat(7) + "1" + "0".repeat(4); // SPA*/
//shift to left
// M[100] = "0111" + "000001000000" ;
// M[101] = "0111" + "000001000000" ;
// M[102] = "0111" + "000001000000" ;
// M[103] = "0111" + "0".repeat(11) + "1"; // Hlt instruction
// shift to right
// M[100] = "0111" + "000010000000" ;
// M[101] = "0111" + "000010000000" ;
// M[102] = "0111" + "000010000000" ;
//M[103] = "0111" + "0".repeat(11) + "1"; //Hlt instruction
/* M[100] = "0101" + "0".repeat(12-adress500.length()) + adress500 ; // BSA to 500 , then in 501 add AC with M[200] then in 502 BUN I to 500
M[501] = "0001" + "0".repeat(12-adress200.length()) + adress200 ; // ADD
M[502] = "1100" + "0".repeat(12-adress500.length()) + adress500 ; // BUM I to 500
M[101] = "0000" + "0".repeat(12-adress300.length()) + adress300 ; // then in 101 , AND AC with M[300]
M[102] = "0111" + "0".repeat(11) + "1";
M[200] = "0".repeat(13) + "111" ; // number 7 ;
M[300] = "0".repeat(13) + "100" ; // number 4*/
// at last we have to get 0 in AC ; because 10 and 4 will be 0 .
Bus_dates [0] = "PC"; // -- Fetch _Bus_datas
Bus_dates [1] = "0" ;
play.addActionListener(e->
{
Runtime runtime = Runtime.getRuntime();
Process process;
try {
String code = jTextArea.getText();
FileWriter myWriter = new FileWriter("source.asm");
myWriter.flush();
myWriter.write(code);
myWriter.close();
String[] command = {"C:/Users/Azarbadgan/AppData/Local/Programs/Python/Python39python.exe", "process.py", "source.asm", "output.txt"};
ProcessBuilder probuilder = new ProcessBuilder( command );
//You can set up your work directory
process = probuilder.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
File myObj = new File("output.txt");
Scanner myReader = new Scanner(myObj);
if(myReader.hasNextLine())
{
String first = myReader.nextLine() ;
String [] s = first.split(" ");
if (M[Integer.parseInt(s[0])] != null)
{
overwrite = true ;
}
M[Integer.parseInt(s[0])] = s[1] ;
String bi = Integer.toBinaryString(Integer.parseInt(s[0]));
PC[3] = "0".repeat(16 - bi.length()) + bi ;
}
while (myReader.hasNextLine())
{
String data = myReader.nextLine();
//System.out.println(data);
String [] p = data.split(" ");
if (M[Integer.parseInt(p[0])] != null)
{
overwrite = true ;
}
M[Integer.parseInt(p[0])] = p[1] ;
}
myReader.close();
} catch (FileNotFoundException ee) {ee.printStackTrace(); }
Hlt = false ;
if(overwrite)
{
//custom title, warning icon
JOptionPane.showMessageDialog(frame,
"Overwriting memory ? Be careful!",
"Overwriting memory warning",
JOptionPane.WARNING_MESSAGE);
}
});
input.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
if((input.getText().length() >= 8) || (e.getKeyChar() !='0' && e.getKeyChar() !='1') )
{
e.consume();
}
}
});
give_input.addActionListener(e ->
{
FGI = true ;
String inp_txt = input.getText();
jbt7.setText("0".repeat(8 - inp_txt.length()) + inp_txt);
});
Sc_btn.addActionListener(e ->
{
if(Hlt)
{
jLabel.setText("S <- 0");
}
else
{
if (T >= 3 && IEN && (FGI || FGO))
D_R = 1;
if (T >= 0)
resume();
boolean p = D[7] == 1 && I == 1 && T == 3;
boolean r = D[7] == 1 && I == 0 && T == 3;
if (/* (R && T ==2 )|| */ (D[0] == 1 && T == 5) || (D[1] == 1 && T == 5) || (D[2] == 1 && T == 5) || (D[3] == 1 && T == 4) || (D[4] == 1 && T == 4) || (D[5] == 1 && T == 5) || (D[6] == 1 && T == 6) || (r) || (p)) // conditions for Sc <- 0 :
{
T = -1;
}
T++;
//all for next clock
p = D[7] == 1 && I == 1 && T == 3;
r = D[7] == 1 && I == 0 && T == 3;
get_Buses_data();
AR[0] = ((T == 0 && !R) || (!R && T == 2) || (D[7] == 0 && I == 1 && T == 3)) ? "1" : "0"; // LD AR
AR[1] = (D[5] == 1 && T == 4) ? "1" : "0"; // INC AR
AR[2] = (R && T == 0) ? "1" : "0"; // CLR AR
PC[0] = ((D[4] == 1 && T == 4) || (D[5] == 1 && T == 5)) ? "1" : "0"; // LD PC
PC[1] = ((T == 1 && !R) || (R && T == 2) || (p && IR[2].charAt(6) == '1' && FGI) || (p && IR[2].charAt(7) == '1' && FGO) || (r && IR[2].charAt(11) == '1' && AC[3].charAt(0) == '0') || (r && IR[2].charAt(12) == '1' && AC[3].charAt(0) == '1') || (r && IR[2].charAt(14) == '1' && !E) || (D[6] == 1 && T == 6 && Integer.parseInt(DR[3], 2) == 0) || (r && IR[2].charAt(13) == '1' && Integer.parseInt(AC[3], 2) == 0)) ? "1" : "0"; // INC PC
PC[2] = (R && T == 1) ? "1" : "0"; // CLR PC
DR[0] = ((D[0] == 1 && T == 4) || (D[1] == 1 && T == 4) || (D[2] == 1 && T == 4) || (D[6] == 1 && T == 4)) ? "1" : "0"; // LD DR
DR[1] = (D[6] == 1 && T == 5) ? "1" : "0"; // INC DR
DR[2] = (false) ? "1" : "0"; // CLR DR // yokh
if(r && IR[2].charAt(15) == '1')
{
Hlt = true ;
overwrite = false ;
M = new String[4096] ;
T = -1 ;
PrintWriter writer = null;
try
{
writer = new PrintWriter("output.txt");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
assert writer != null;
writer.print("");
writer.close();
}
//AC[0] = ((D[0] == 1 && T==5) || (D[1] == 1 && T==5) || (D[2]==1 && T==5 ) || (r && IR[2].charAt(6) == '1') || (p && IR[2].charAt(4)=='1') ||(r && IR[2].charAt(8)=='1' ) || (r && IR[2].charAt(9)=='1') ) ? "1" : "0" ; // LD AC
if (D[0] == 1 && T == 5) {
AC[0] = "1"; // AND
AC_code = 1;
} else if (D[1] == 1 && T == 5) {
AC[0] = "1";
AC_code = 2;
} else if (D[2] == 1 && T == 5) {
AC[0] = "1";
AC_code = 3;
} else if (r && IR[2].charAt(6) == '1') {
AC[0] = "1";
AC_code = 4;
} else if (r && IR[2].charAt(8) == '1') {
AC[0] = "1";
D_E = (AC[3].charAt(15) == '1') ? 1 : 0;
AC_code = 5;
} else if (r && IR[2].charAt(9) == '1') {
AC[0] = "1";
D_E = (AC[3].charAt(0) == '1') ? 1 : 0;
AC_code = 6;
} else if (p && IR[2].charAt(4) == '1') {
AC[0] = "1";
AC_code = 7;
} else {
AC[0] = "0";
}
AC[1] = (r && IR[2].charAt(10) == '1') ? "1" : "0"; // INC AC
AC[2] = (IR[2].charAt(4) == '1' && r) ? "1" : "0"; // CLR AC
IR[0] = ((T == 1 && !R)) ? "1" : "0"; // LD IR
TR[0] = (R && T == 0) ? "1" : "0"; // LD TR
TR[1] = (false) ? "1" : "0"; // INC TR //yokh
TR[2] = (false) ? "1" : "0"; // CLR TR //yokh
flag_out = (p && IR[2].charAt(5) == '1') ? 1 : 0;
if (R && T == 2) {
D_R = 0;
D_IEN = 0;
// resume(); // definitely wrong !
} else if (T >= 3 && IEN && (FGI || FGO) && !IR[2].equals("1111" + "000001000000")) // Solve main bug of original computer in book
D_R = 1;
if (p && IR[2].charAt(8) == '1') ///////*****************
{
D_IEN = 1;
}
if (p && IR[2].charAt(9) == '1') {
D_IEN = 0;
}
Write_Mem = (R && T == 1) || (D[3] == 1 && T == 4) || (D[5] == 1 && T == 4) || (D[6] == 1 && T == 6);
if (r && IR[2].charAt(5) == '1') {
D_E = 0;
} else if (r && IR[2].charAt(7) == '1') {
D_E = (D_E == 0) ? 1 : 0;
}
// Decode D[0:7] this will be work at next clock
else if (!R && T == 2) {
for (int i = 0; i < 8; i++) {
D[i] = 0; // should be reset
}
String binary = IR[2].substring(1, 4);
int dec = Integer.parseInt(binary, 2);
D[dec] = 1;
I = (IR[2].charAt(0) == '1') ? 1 : 0;
} // End of Decode
}
}); // Action _ Listener
} // End of constructor
private void get_Buses_data()
{
boolean r = D[7] == 1 && I == 0 && T == 3;
boolean p = D[7]==1 && I==1 && T==3 ;
String B7 = Character.toString((IR[2].charAt(8)));
String B6 = Character.toString(IR[2].charAt(9));
if ((D[4]==1 && T==4)||(D[5]==1 &&T==5))
{
//x1 AR
Bus_dates[0] = "AR";
Bus_dates[1] = AR[3]; /* in registers, 3 refers to data except IR */
}
else if((T==0) || (D[5]==1 && T==4))
{
//x2 PC
Bus_dates[0] = "PC" ;
Bus_dates[1] = PC[3] ;
}
else if((D[2] ==1 && T==5) || (D[6] ==1 && T==6))
{
//x3 DR
Bus_dates[0] = "DR";
Bus_dates[1] = DR[3];
}
else if((D[3] ==1 && T==4 )||(p && IR[2].charAt(5) == '1') || (r && B6.equals("1")) || (r && B7.equals("1")) ) ///////
{
//x4 AC
Bus_dates[0] = "AC";
Bus_dates[1] = AC[3];
}
else if(!R && T==2)
{
//x5 IR
Bus_dates[0] = "IR" ;
Bus_dates[1] = IR[2];
}
else if(R && T==1)
{
//x6 TR
Bus_dates[0] = "TR";
Bus_dates[1] = TR[3];
}
else if((!R&&T==1) || (D[7]==0 && I==1 && T==3) || ((D[0]==1 || D[1]==1 || D[2]==1 ||D[6]==1) && T==4))
{
//x7 M[AR]
Bus_dates[0] = "M[AR]";
// Bus_dates[1] = M[Integer.parseInt(AR[3])];
Bus_dates[1] = M[Integer.parseInt(AR[3].substring(4,16) , 2)];
}
}
private void set_graphic()
{
frame = new JFrame();
frame.setTitle("Mano_Simple_Computer");
JPanel panel = new JPanel();
set_coloring_at_momemt();
String txt_hint = " IN the name of God \n" +
"the mano computer introduced in book , has a main bug : \n suppose that it is important for us to don,t let interrupt to come " +
"from mem 200 to mem 300 \n so that we use IOF instruction to do this at memory 199 , but if we had interrupt \n exactly in " +
"the time that we are executing this instruction , what will happen ? \n " +
"we will have interrupt in next cycle and the IOF instruction at the end of interrupt subroutine service \n " +
"will annul our IOF that we wrote it at mem 199 . this bug has been passed and trubleshooted in this program \n by And with " +
"not IOF instruction into another inputs of R _flip_flop \n" +
"pay attention that : \n if you want to give input to make interrupt in a embedded text_field , \n" +
"you can just give binary numbers (0 or 1 ) other characters will be ignored \n" +
"it is also okay giving less than 8 bit , automatically it will get up . \n" +
"GOOD LUCK" ;
hint_jtext_area = new JTextArea(txt_hint , 10,10);
hint_jtext_area.setSize(300 , 300);
// hint_jtext_area.setLineWrap(true);
hint_jtext_area.setFont(new Font("Serif" , Font.ITALIC , 16));
jScrollPane = new JScrollPane(jTextArea);
jScrollPane.setBounds(500,20,270,340);
cut=new JMenuItem("cut");
copy=new JMenuItem("copy");
paste=new JMenuItem("paste");
selectAll=new JMenuItem("selectAll");
item_hint = new JMenuItem("Hint");
show_mem = new JMenuItem("Show_memory");
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
item_hint.addActionListener(this);
show_mem.addActionListener(this);
mb=new JMenuBar();
file=new JMenu("File");
edit=new JMenu("Edit");
help=new JMenu("Help");
edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);
file.add(item_hint);
file.add(show_mem);
mb.add(file);mb.add(edit);mb.add(help);
frame.getContentPane();
jLabel = new JLabel("T 0...7");
JLabel hint_input = new JLabel("input :") ;
Dimension S = jLabel.getPreferredSize();
jLabel.setBounds(210, 460, S.width*8, S.height);
//hint_input.setBounds(520 , 150 , S.width*8, S.height*8);
// hint_input.setIcon(new ImageIcon("G:\\images.png"));
jbt1 = new JButton("0".repeat(8));
jbt2 = new JButton("0".repeat(8));
jbt3 = new JButton("0".repeat(8));
jbt4 = new JButton("0".repeat(8));
jbt5 = new JButton("0".repeat(8));
jbt6 = new JButton("0".repeat(8));
jbt7 = new JButton("0".repeat(8));
jbt8 = new JButton("0".repeat(8));
play = new JButton();
try {
ImageIcon imageIcon = new ImageIcon(Main.class.getResource("pu.png"));
play.setIcon(imageIcon);
play.setBounds(600,370,imageIcon.getIconWidth() - 85,imageIcon.getIconHeight() - 85);
} catch (Exception e)
{
e.printStackTrace();
}
JButton AR_reg = new JButton("AR");
JButton PC_reg = new JButton("PC");
JButton DR_reg = new JButton("DR");
JButton AC_reg = new JButton("AC");
JButton IR_reg = new JButton("IR");
JButton TR_reg = new JButton("TR");
JButton INP_reg = new JButton("INPUT");
JButton OUT_reg = new JButton("OUTPUT") ;
Dimension btn = jbt1.getPreferredSize();
input = new JTextField(8) ;
input.setBounds(550,460,200,30);
hint_input.setBounds(500 , 465 , S.width*2, S.height);
Font fo = new Font("Serif", Font.BOLD, 20);
input.setFont(fo);
give_input = new JButton("submit" );
give_input.setBounds(605,505,btn.width,btn.height);
Sc_btn = new JButton("seq_counter");
jbt1.setBounds(110,20,btn.width*4,btn.height);
jbt1.setBackground(Color.MAGENTA);
AR_reg.setBounds(10,20,btn.width,btn.height);
AR_reg.setBackground(Color.magenta);
jbt2.setBounds(110,60,btn.width*4,btn.height);
jbt2.setBackground(Color.GREEN);
PC_reg.setBounds(10,60,btn.width,btn.height);
PC_reg.setBackground(Color.green);
jbt3.setBounds(110,100,btn.width*4,btn.height);
jbt3.setBackground(Color.orange);
DR_reg.setBounds(10,100,btn.width,btn.height);
DR_reg.setBackground(Color.orange);
jbt4.setBounds(110,140,btn.width*4,btn.height);
jbt4.setBackground(Color.CYAN);
AC_reg.setBounds(10,140,btn.width,btn.height);
AC_reg.setBackground(Color.cyan);
jbt5.setBounds(110,180,btn.width*4,btn.height);
jbt5.setBackground(Color.YELLOW);
IR_reg.setBounds(10,180,btn.width,btn.height);
IR_reg.setBackground(Color.yellow);
jbt6.setBounds(110,220,btn.width*4,btn.height);
jbt6.setBackground(Color.BLUE);
TR_reg.setBounds(10,220,btn.width,btn.height);
TR_reg.setBackground(Color.blue);
jbt7.setBounds(110,260,btn.width*4,btn.height);
//jbt7.setBackground(Color.GREEN);
INP_reg.setBounds(10,260,btn.width,btn.height);
jbt8.setBounds(110,300,btn.width*4,btn.height);
jbt8.setBackground(Color.red);
OUT_reg.setBounds(10,300,btn.width,btn.height);
OUT_reg.setBackground(Color.red);
Sc_btn.setBounds(35,400,btn.width,btn.height);
/* Box box = Box.createVerticalBox();
box.add(jbt1);
box.add(Box.createVerticalStrut(20));
box.add(jbt2);
box.add(Box.createVerticalStrut(20));
box.add(jbt3);
box.add(Box.createVerticalStrut(20));
box.add(jbt4);
box.add(Box.createVerticalStrut(20));
Dimension size = box.getPreferredSize();
box.setBounds(15,20,size.width*4,size.height);
*/
panel.setLayout(null);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(jbt1);
panel.add(jbt2);
panel.add(jbt3);
panel.add(jbt4);
panel.add(jbt5);
panel.add(jbt6);
panel.add(jbt7);
panel.add(jbt8);
panel.add(Sc_btn);
try
{
panel.add(play);
} catch (Exception e)
{
e.printStackTrace();
}
panel.add(jLabel);
panel.add(input);
panel.add(give_input);
panel.add(jScrollPane);
panel.add(AR_reg);
panel.add(PC_reg);
panel.add(DR_reg);
panel.add(AC_reg);
panel.add(IR_reg);
panel.add(TR_reg);
panel.add(INP_reg);
panel.add(OUT_reg);
panel.add(hint_input);
frame.add(panel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setJMenuBar(mb);
frame.setSize(870, 620);
frame.setVisible(true);
// JOptionPane.showMessageDialog(null, jLabel);
}
private void set_coloring_at_momemt()
{
final StyleContext cont = StyleContext.getDefaultStyleContext();
Color my_blue = new Color(145, 27, 212) ;
Color my_green = new Color(29, 209, 143);
Color my_orange = new Color(255, 160, 0);
final AttributeSet attr = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, my_blue);
final AttributeSet org_end = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, my_green);
final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
final AttributeSet NBR = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, my_orange);
DefaultStyledDocument doc = new DefaultStyledDocument()
{
public void insertString (int offset, String str, AttributeSet a) throws BadLocationException
{
super.insertString(offset, str, a);
String text = getText(0, getLength());
int before = findLastNonWordChar(text, offset);
if (before < 0) before = 0;
int after = findFirstNonWordChar(text, offset + str.length());
int wordL = before;
int wordR = before;
while (wordR <= after)
{
if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W"))
{
if (text.substring(wordL, wordR).matches("(\\W)*(STA|LDA|AND|ADD|BUN|BSA|ISZ|CLA|CLE|CMA|CME|CIR|CIL|INC|SPA|SNA|SZA|SZE|HLT|INP|OUT|SKI|SKO|ION|IOF|,)") )
setCharacterAttributes(wordL, wordR - wordL, attr, false);
else if (text.substring(wordL, wordR).matches("(\\W)*(ORG|org|end|END)"))
setCharacterAttributes(wordL, wordR - wordL, org_end, false);
else if (text.substring(wordL, wordR).matches("(\\W)*(HEX|DEC)"))
setCharacterAttributes(wordL, wordR - wordL, NBR, false);
else
setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
wordL = wordR;
}
wordR++;
}
}
public void remove (int offs, int len) throws BadLocationException
{
super.remove(offs, len);
String text = getText(0, getLength());
int before = findLastNonWordChar(text, offs);
if (before < 0) before = 0;
int after = findFirstNonWordChar(text, offs);
if (text.substring(before, after).matches("(\\W)*(private|public|protected)"))
{
setCharacterAttributes(before, after - before, attr, false);
} else
{
setCharacterAttributes(before, after - before, attrBlack, false);
}
}
};
jTextArea = new JTextPane(doc);
// jTextArea.setBounds(10,30,100,100);
jTextArea.setFont(new Font("TimesRoman" , Font.BOLD, 14));
jTextArea.setText("// Write your code here");
}
private void initial_registers()
{
D = new int[]{0,0,0,0,0,0,0,0};
AR = new String[]{"0" , "0" , "0" , "0".repeat(16),"AR"};
// PC = new String[]{"0" , "0" , "0" , "0".repeat(15) + "1","PC"};
String tt = Integer.toBinaryString(100);
PC = new String[]{"0" , "0" , "0" , "0".repeat(16-tt.length()) + tt ,"PC"};
DR = new String[]{"0" , "0" , "0" , "1".repeat(16),"DR"};
//AC = new String[]{"0" , "0" , "0" , "1".repeat(16),"AC"};
AC = new String[]{"0" , "0" , "0" , "0".repeat(12)+"1".repeat(4),"AC"};
// AC = new String[]{"0" , "0" , "0" , "1".repeat(13)+"1".repeat(3),"AC"};
jbt4.setText(AC[3]);
IR = new String[]{"0","IR","0".repeat(16)}; //third is IR s data
//TR = new String[]{"0" , "0" , "0" , "1".repeat(16),"TR"};
TR = new String[]{"0" , "0" , "0" , "0001"+"0".repeat(12),"TR"};
}
private void resume()
{
String concat = "";
if(Write_Mem)
{
concat += "M[AR] <- " + Bus_dates[0] + ", " ;
String index_Mem = AR[3].substring(4,16) ;
M[Integer.parseInt(index_Mem , 2)] = Bus_dates[1] ;
// System.out.println(Integer.parseInt(index_Mem , 2));
// System.out.println( M[Integer.parseInt(index_Mem , 2)]);
}
concat += Control_Registers( AR);
concat += Control_Registers( PC);
concat += Control_Registers( DR);
concat += Control_Registers( AC);
concat += Control_Registers( TR);
concat += Control_Registers( IR);
if(flag_out == 1)
{
concat += "OUTR <- " + "AC" + " ," ;
OUTR = Bus_dates[1] .substring(8,16) ;
jbt8.setText(OUTR);
}
boolean r = D[7] == 1 && I == 0 && T==3 ;
boolean p = D[7] == 1 && I == 1 && T==3 ;
if(p && IR[2].charAt(8) == '1')
{
concat += "IEN <- 1 ," ;
}
else if(p && IR[2].charAt(9) == '1')
{
concat += "IEN <- 0 ," ;
}
else if(r && IR[2].charAt(5) == '1')
{
concat += "E <- 0 ," ;
}
else if(r && IR[2].charAt(7) == '1')
{
concat += "E <- ~E ," ;
}
if(R && T==2) //////////// ***************************
{
concat += "R <- 0 ," ;
jLabel.setText("T" + T + " : " + concat);
T = -1 ;
}
else
jLabel.setText("T" + T + " : " + concat);
R = (D_R == 1) ;
IEN = (D_IEN == 1) ;
E = (D_E == 1) ;
}
private String Control_Registers( String [] reg)
{
String concat ="" ;
if (reg.length == 3)
{
// we are running with IR
if(reg[0].equals("1")) // LD_IR
{
concat += ( reg[1] +" <- "+ Bus_dates[0] + ", ");
reg[2] = Bus_dates[1];
jbt5.setText(reg[2]);
}
}
else // if we are not running IR
{
if(reg[0].equals("1")) // LD_Reg here is not IR
{
if (!reg[4].equals("AC")) // !LD AC
{
concat += ( reg[4] +" <- "+Bus_dates[0] + ", ");
if(reg[4].equals("AR") && Bus_dates[0].equals("IR")) // 16 to 12 one exception creates this
{
reg[3]="0".repeat(4);
reg[3]+= Bus_dates[1].substring(4,16);
}
else // else with feragh bal assign et
reg[3] = Bus_dates[1] ;
} // end of if ! LD_AC
else // LD_AC
{
concat += "AC <- " ;
boolean r = D[7] == 1 && I == 0 && T == 3;
boolean p = D[7]==1 && I==1 && T==3 ;
if (AC_code == 1 ) // AND
{
StringBuilder tempo = new StringBuilder();
for (int i = 0 ;i < 16 ;i++)
{
if(AC[3].charAt(i) == '1' && DR[3].charAt(i) == '1')
{
tempo.append("1");
}
else
{
tempo.append("0");
}
}
// long ac = Long.parseLong(AC[3]); long dr = Long.parseLong(DR[3]); long result = ac & dr ;
reg[3] = tempo.toString() ;
concat+= "AC ^ DR ,";
}
else if(AC_code == 2 ) // ADD
{
/* int ac = Integer.parseInt(AC[3] , 2);
int dr = Integer.parseInt(DR[3] , 2);
int result = ac + dr ; // decimal form
String new_data = Integer.toBinaryString(result); // binary form
int C_out = 0 ;
if(new_data.length() < 16)
{
reg[3] = "0".repeat(16-new_data.length());
reg[3] += new_data ;
}
else if(new_data.length() == 17)
{
reg[3] = new_data.substring(1,17);
C_out = 1 ;
}
else // new data has exactly 16 az size
{
reg[3] = new_data ;
}
concat += "AC + DR ," ;*/
StringBuilder tmp = new StringBuilder();
char carry ='0';
for (int i = 0 ;i < 16 ;i++)
{
char cur_dr = DR[3].charAt(15-i) ;
char cur_ac = AC[3].charAt(15-i) ;
if((cur_dr == '1' && cur_ac == '1') || (cur_ac == '1' && carry == '1') || (cur_dr == '1' && carry =='1') )
{
if(cur_ac == '1' && cur_dr == '1' && carry == '1')
{
tmp.append("1");
}
else
{
tmp.append("0");
}
carry = '1';
}
else
{
if(cur_ac == '1' || cur_dr == '1' || carry == '1' )
{
tmp.append("1");
}
else
{
tmp.append("0");
}
carry = '0' ;
}
}
E = (carry == '1') ;
reg[3] = tmp.reverse().toString();
concat += "AC + DR ," ;
}
else if(AC_code == 3 ) // AC <- DR
{
reg[3] = DR[3];
concat += "DR ," ;
}
else if(AC_code == 4 ) // Complement AC
{
StringBuilder tmp = new StringBuilder();
for (int i = 0; i < AC[3].length() ; i++)
{
if(AC[3].charAt(i) == '1')
{
tmp.append("0");
}
else
{
tmp.append("1");
}
}
reg[3] = tmp.toString();
concat += "~AC ,";
}
else if(AC_code == 7 ) // input galip
{
String temp = input.getText();
String ac = AC[3].substring(0,8);
String in = "0".repeat(8-temp.length()) + temp ;
AC[3] = ac + in ;
FGI = false ;
concat += "INPR ," ;
}
else if(AC_code == 5 ) // shift R
{
String tp = E ? "1" : "0" ;
D_E = (AC[3].charAt(15) == '1') ? 1 : 0 ; // abs ish oustah megdardehi olunop line 200 ishizaki!
AC[3] = tp + AC[3].substring(0,15) ;
concat += "E , E <- AC(0) , SHR ,";
}