Skip to content

Commit 55d43c9

Browse files
CSCI6461 Phase 2
1 parent 27dc53c commit 55d43c9

12 files changed

+357
-4
lines changed

.DS_Store

2 KB
Binary file not shown.

Assembler.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Assembler extends Converter {
2+
public String getOpCode(String op){
3+
switch(op){
4+
case "LDR": return "000001";
5+
case "STR": return "000010";
6+
case "LDA": return "000011";
7+
case "LDX": return "100001";
8+
case "STX": return "101010";
9+
case "JZ": return "001000";
10+
default: return "none";
11+
}
12+
}
13+
}

AssemblerInput.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
LOC 6
2+
Data 10
3+
Data 3
4+
Data End
5+
Data 0
6+
Data 12
7+
Data 9
8+
Data 18
9+
Data 12
10+
LDX 2,7
11+
LDR 3,0,10
12+
LDR 2,2,10
13+
LDR 1,2,10,1
14+
LDA 0,0,0
15+
LDX 1,9
16+
JZ 0,1,0
17+
LOC 1024
18+
End: HLT

AssemblerOutput.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
0006 000A
2+
0007 0003
3+
0008 0400
4+
0009 0000
5+
000A 000C
6+
000B 0009
7+
000C 0012
8+
000D 000C
9+
000E 8487
10+
000F 070A
11+
0010 068A
12+
0011 05AA
13+
0012 0C00
14+
0013 8449
15+
0014 2040
16+
0400 0000

CPU.java

-1
Original file line numberDiff line numberDiff line change
@@ -366,5 +366,4 @@ public void MFHandle(Memory mem){
366366
MFR[3] = 0;
367367
mem.Data[4] = BinaryToDecimal(PC, 12);
368368
}
369-
370369
}

Converter.java

+42
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,46 @@ public short HexToDecimal(String s){
5353
}
5454
return val;
5555
}
56+
57+
public static int hexStringToInt(String hexString) {
58+
// Parse the hexadecimal string to an integer
59+
return Integer.parseInt(hexString, 16);
60+
61+
}
62+
63+
public String inttoHexString(int x) {
64+
String input = Integer.toHexString(x).toUpperCase(); // The input string
65+
int desiredLength = 4; // The desired length of the padded string
66+
// Use String.format to left-pad with zeros
67+
return String.format("%" + desiredLength + "s", input).replace(' ', '0');
68+
}
69+
public String inttoHexString(int x, int desiredLength) {
70+
String input = Integer.toHexString(x).toUpperCase(); // The input string
71+
// The desired length of the padded string
72+
// Use String.format to left-pad with zeros
73+
return String.format("%" + desiredLength + "s", input).replace(' ', '0');
74+
}
75+
public static boolean isAllowedHex(char c) {
76+
char[] charArray = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e','f'};
77+
for (char chr:
78+
charArray) {
79+
if (chr == c) {
80+
return true;
81+
}
82+
}
83+
return false;
84+
}
85+
public String binaryToHex(String binary, int desiredLength) {
86+
int decimalValue = Integer.parseInt(binary, 2);
87+
String input = Integer.toHexString(decimalValue).toUpperCase();
88+
return String.format("%" + desiredLength + "s", input).replace(' ', '0');
89+
}
90+
public static String hexToBinary(String hex) {
91+
int decimalValue = Integer.parseInt(hex, 16);
92+
return Integer.toBinaryString(decimalValue);
93+
}
94+
public String intstrtoBin(String x, int desiredLength){
95+
String input = Integer.toBinaryString(Integer.parseInt(x));
96+
return String.format("%" + desiredLength + "s", input).replace(' ', '0');
97+
}
5698
}

GUI.java

+135-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import javax.swing.*;
22
import java.awt.*;
33
import java.util.*;
4+
import java.util.List;
45
import java.awt.event.ActionEvent;
56
import java.io.File;
67
import java.io.FileNotFoundException;
78
import java.io.FileReader;
89
import java.io.IOException;
10+
import java.io.FileWriter;
911
/* 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. */
1012

1113
public class GUI extends JFrame {
@@ -14,16 +16,25 @@ public class GUI extends JFrame {
1416
private JLabel GPR[], X[], PC, MAR, MBR, IR, MFR, Priv;
1517
private Label gpr0_arr[], gpr1_arr[], gpr2_arr[], gpr3_arr[];
1618
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;
1820
private CPU cpu;
1921
private Memory mem;
2022
private File file;
2123
private ArrayList<StringStruct> Code;
22-
24+
// private Assembler assem;
2325
private ArrayList<JButton> switches;
2426
private JPanel Pan[];
2527
private Devices dev;
2628
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+
2738

2839
public GUI() throws NullPointerException {
2940
super();
@@ -36,6 +47,7 @@ public GUI() throws NullPointerException {
3647
dev = new Devices();
3748
cpu = new CPU(dev);
3849
mem = new Memory();
50+
assem = new Assembler();
3951
hlt.setBounds(530, 520, 20, 20);
4052
hlt.setBackground(Color.BLACK);
4153
Run = new Label();
@@ -536,6 +548,24 @@ private void loadFile(ActionEvent e) {
536548
}
537549
}
538550

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+
539569
private void ProcessFile() throws FileNotFoundException {
540570
/*
541571
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 {
553583
s.close();
554584
}
555585

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+
556671
/*
557672
* Main Handler for updating LEDs and execution of CPU code
558673
* Can be used independently as single step and part of Run Button
@@ -604,8 +719,10 @@ private void RunProg(ActionEvent e) throws InterruptedException {
604719
} while (OpCode != CPU.HLT);
605720
Run.setBackground(Color.black);
606721
hlt.setBackground(Color.red);
722+
607723
}
608724

725+
609726
private void runMainLoop() {
610727
/* This will set create and set the size for the main background of the GUI */
611728
this.setSize(1200, 620);
@@ -640,6 +757,22 @@ public void LoadGui() {
640757
init.setBorderPainted(false);
641758
init.addActionListener(e -> loadFile(e));
642759
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+
643776
/** This creates the "SS" button to the GUI */
644777
ss = new JButton("SS");
645778
ss.setBounds(600, 405, 65, 80);

Main.java

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
public class Main
1212
{
1313
public Main(){
14-
1514
}
1615
public static void main(String args[]){
1716
GUI gui = new GUI();
File renamed without changes.
File renamed without changes.

input.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
LOC 6
2+
Data 10
3+
Data 3
4+
Data End
5+
Data 0
6+
Data 12
7+
Data 9
8+
Data 18
9+
Data 12
10+
LDX 2,7
11+
LDR 3,0,10
12+
LDR 2,2,10
13+
LDR 1,2,10,1
14+
LDA 0,0,0
15+
LDX 1,9
16+
JZ 0,1,0
17+
LOC 1024
18+
End: HLT

0 commit comments

Comments
 (0)