1
1
import javax .swing .*;
2
2
import java .awt .*;
3
3
import java .util .*;
4
+ import java .util .List ;
4
5
import java .awt .event .ActionEvent ;
5
6
import java .io .File ;
6
7
import java .io .FileNotFoundException ;
7
8
import java .io .FileReader ;
8
9
import java .io .IOException ;
10
+ import java .io .FileWriter ;
9
11
/* This file contains all of the front panel interface's GUI Components and event handlers. This file is also used for all input and output actions. */
10
12
11
13
public class GUI extends JFrame {
@@ -14,16 +16,25 @@ public class GUI extends JFrame {
14
16
private JLabel GPR [], X [], PC , MAR , MBR , IR , MFR , Priv ;
15
17
private Label gpr0_arr [], gpr1_arr [], gpr2_arr [], gpr3_arr [];
16
18
private Label XLabel [][], pclab [], marlab [], mbrlab [], mfrlab [], irlab [], privlab , hlt , Run ;
17
- private JButton LDarr [], store , st_plus , load , init , ss , run ;
19
+ private JButton LDarr [], store , st_plus , load , init , ss , run , assemble ;
18
20
private CPU cpu ;
19
21
private Memory mem ;
20
22
private File file ;
21
23
private ArrayList <StringStruct > Code ;
22
-
24
+ // private Assembler assem;
23
25
private ArrayList <JButton > switches ;
24
26
private JPanel Pan [];
25
27
private Devices dev ;
26
28
char swarr []; // Array for the switches pressed
29
+ private Map <String , String > labelMap = new HashMap <>();
30
+ // private Map<String, String> outMap = new HashMap<>();
31
+ private int pcount =0 ;
32
+ private String hexPcount = "" ;
33
+ private List <String > hexAdd = new ArrayList <String >();
34
+ private List <String > hexDat = new ArrayList <String >();
35
+
36
+ private Assembler assem ;
37
+
27
38
28
39
public GUI () throws NullPointerException {
29
40
super ();
@@ -36,6 +47,7 @@ public GUI() throws NullPointerException {
36
47
dev = new Devices ();
37
48
cpu = new CPU (dev );
38
49
mem = new Memory ();
50
+ assem = new Assembler ();
39
51
hlt .setBounds (530 , 520 , 20 , 20 );
40
52
hlt .setBackground (Color .BLACK );
41
53
Run = new Label ();
@@ -536,6 +548,24 @@ private void loadFile(ActionEvent e) {
536
548
}
537
549
}
538
550
551
+
552
+ private void loadFileForAssemble (ActionEvent e ) throws IOException {
553
+ /*This will prompt the user to search for and load a file into the simulator*/
554
+ JFileChooser fCh = new JFileChooser ();
555
+ fCh .setCurrentDirectory (new File (System .getProperty ("user.dir" )));
556
+ int res = fCh .showOpenDialog (this );
557
+ if (res == JFileChooser .APPROVE_OPTION ) {
558
+ file = new File (fCh .getSelectedFile ().getAbsolutePath ());
559
+ String filename = file .getAbsolutePath ();
560
+ JOptionPane .showMessageDialog (this , filename , "File Load Successful" , JOptionPane .PLAIN_MESSAGE );
561
+ try {
562
+ ProcessFileForAssemble ();
563
+ } catch (FileNotFoundException fnfe ) {
564
+ fnfe .printStackTrace ();
565
+ }
566
+ }
567
+ }
568
+
539
569
private void ProcessFile () throws FileNotFoundException {
540
570
/*
541
571
To scan wether a file has been selected to load into the simulator, if not, it will throw out an error
@@ -553,6 +583,91 @@ private void ProcessFile() throws FileNotFoundException {
553
583
s .close ();
554
584
}
555
585
586
+ private void ProcessFileForAssemble () throws IOException {
587
+ /*
588
+ To scan wether a file has been selected to load into the simulator, if not, it will throw out an error
589
+ */
590
+ Scanner s = new Scanner (file );
591
+ Scanner s2 = new Scanner (file );
592
+ while (s .hasNext ()) {
593
+
594
+ String loc = s .next ();
595
+ String val = s .next ();
596
+ if (loc .equals ("LOC" )){
597
+ pcount = Integer .parseInt (val );
598
+ hexPcount = cpu .inttoHexString (Integer .parseInt (val ));
599
+ }
600
+ if (loc .contains (":" )){
601
+ labelMap .put (loc .substring (0 , loc .length () - 1 ).toLowerCase (),Integer .toString (pcount ));
602
+ }
603
+ }
604
+ labelMap .forEach ((key , value ) -> System .out .println (key + " " + value ));
605
+
606
+ s .close ();
607
+ pcount =0 ;
608
+ hexPcount =cpu .inttoHexString (pcount );
609
+
610
+ while (s2 .hasNext ()) {
611
+ String loc = s2 .next ();
612
+ String val = s2 .next ();
613
+ if (loc .equals ("LOC" )){
614
+ pcount = Integer .parseInt (val );
615
+ hexPcount = cpu .inttoHexString (Integer .parseInt (val ));
616
+ }
617
+ if (loc .toLowerCase ().equals ("data" )){
618
+ if (val .chars ().allMatch ( Character ::isDigit )) {
619
+ hexAdd .add (hexPcount );
620
+ hexDat .add (cpu .inttoHexString (Integer .parseInt (val )));
621
+ }
622
+ else {
623
+ hexAdd .add (hexPcount );
624
+ hexDat .add (cpu .inttoHexString (Integer .parseInt (labelMap .get (val .toLowerCase ()))));
625
+ }
626
+ }
627
+ else if (labelMap .containsKey (loc .substring (0 , loc .length () - 1 ).toLowerCase ())){
628
+ hexAdd .add (hexPcount );
629
+ if (val .equals ("HLT" )){
630
+ hexDat .add ("0000" );
631
+ }
632
+
633
+ }
634
+ else {
635
+ String op = assem .getOpCode (loc .toUpperCase ());
636
+ if (op == "none" ) {
637
+ continue ;
638
+ }
639
+ String [] operands = val .split ("," );
640
+ int l = operands .length ;
641
+ String address = "" ;
642
+
643
+ if (l == 2 ) {
644
+ address = op + "00" + cpu .intstrtoBin (operands [0 ],2 ) + "0" + cpu .intstrtoBin (operands [1 ],5 );
645
+ }
646
+
647
+ if (l == 3 ) {
648
+ address = op + cpu .intstrtoBin (operands [0 ],2 ) + cpu .intstrtoBin (operands [1 ],2 ) + "0" + cpu .intstrtoBin (operands [2 ],5 );
649
+ }
650
+
651
+ if (l == 4 ) {
652
+ address = op + cpu .intstrtoBin (operands [0 ],2 ) + cpu .intstrtoBin (operands [1 ],2 ) + "1" + cpu .intstrtoBin (operands [2 ],5 );
653
+ System .out .println (address );
654
+ }
655
+
656
+ hexAdd .add (hexPcount );
657
+ hexDat .add (cpu .binaryToHex (address , 4 ));
658
+ }
659
+ pcount = pcount + 1 ;
660
+ hexPcount = cpu .inttoHexString (pcount );
661
+ }
662
+ final FileWriter outWriter = new FileWriter ("AssemblerOutput.txt" );
663
+
664
+ for (int i = 0 ; i < hexAdd .size (); i ++) {
665
+ outWriter .write (hexAdd .get (i )+" " + hexDat .get (i )+"\n " );
666
+ }
667
+ s2 .close ();
668
+ outWriter .close ();
669
+ }
670
+
556
671
/*
557
672
* Main Handler for updating LEDs and execution of CPU code
558
673
* Can be used independently as single step and part of Run Button
@@ -604,8 +719,10 @@ private void RunProg(ActionEvent e) throws InterruptedException {
604
719
} while (OpCode != CPU .HLT );
605
720
Run .setBackground (Color .black );
606
721
hlt .setBackground (Color .red );
722
+
607
723
}
608
724
725
+
609
726
private void runMainLoop () {
610
727
/* This will set create and set the size for the main background of the GUI */
611
728
this .setSize (1200 , 620 );
@@ -640,6 +757,22 @@ public void LoadGui() {
640
757
init .setBorderPainted (false );
641
758
init .addActionListener (e -> loadFile (e ));
642
759
this .add (init );
760
+
761
+ assemble = new JButton ("Assemble" );
762
+ assemble .setBounds (850 , 420 , 120 , 50 );
763
+ assemble .setBackground (Color .RED );
764
+ assemble .setForeground (Color .black );
765
+ assemble .setOpaque (true );
766
+ assemble .setBorderPainted (false );
767
+ assemble .addActionListener (e -> {
768
+ try {
769
+ loadFileForAssemble (e );
770
+ } catch (IOException e1 ) {
771
+ e1 .printStackTrace ();
772
+ }
773
+ });
774
+ this .add (assemble );
775
+
643
776
/** This creates the "SS" button to the GUI */
644
777
ss = new JButton ("SS" );
645
778
ss .setBounds (600 , 405 , 65 , 80 );
0 commit comments