Добавил:
Medi
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Всем Арх ВС , пацаны! / КУрсачи Архитектура (2) / хз кто 2007г / kurs / CPU / Main_Frame
.java import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
public class Main_Frame extends JFrame
{
public static int i=0,comand_count=0;
public static String OPENED_FILE = "NONE";
public Universal_Registers universal_registers = new Universal_Registers();
public System_Registers1 system_registers1 = new System_Registers1();
public System_Registers2 system_registers2 = new System_Registers2();
public System_Registers3 system_registers3 = new System_Registers3();
public static ArrayList command=new ArrayList();
public static ArrayList operand1=new ArrayList();
public static ArrayList operand2=new ArrayList();
public TLB tlb = new TLB();
public Instructions instructions = new Instructions();
public Memory memory = new Memory();
public Co_Processor_Registers co_processor_registers = new Co_Processor_Registers();
public Processor_Flags processor_flags = new Processor_Flags();
public Co_Processor_Flags co_processor_flags = new Co_Processor_Flags();
public External_Functions functionality = new External_Functions(this);
private JToolBar top_toolbar = new JToolBar(),
bottom_toolbar = new JToolBar();
private JButton open_file_button = new JButton("Open file"),
exit_button = new JButton("Exit");
private JButton reset_button = new JButton("Reset"),
next_button = new JButton("Next");
public static DefaultListModel lProgram = new DefaultListModel();
private JFileChooser file_chooser = new JFileChooser();
private static ArrayList pcr=new ArrayList();
public Main_Frame() {
top_toolbar.setFloatable(false);
bottom_toolbar.setFloatable(false);
file_chooser.setBounds(200, 150, 150, 150);
file_chooser.setAcceptAllFileFilterUsed(false);
Toolkit thekit = getToolkit();
Dimension wndSize = thekit.getScreenSize();
setBounds(wndSize.width / 4, wndSize.height / 4, 700, 600);
setBackground(Color.gray);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Architecture");
setLayout();
set_top_ToolbarActions();
set_bottom_ToolbarActions();
}
public static void main(String[] args) {
Main_Frame obj = new Main_Frame();
obj.setVisible(true);
}
private void setLayout() {
for (byte i = 0; i < 3; i++) {
top_toolbar.addSeparator();
}
top_toolbar.add(open_file_button);
for (byte i = 0; i < 5; i++) {
top_toolbar.addSeparator();
}
top_toolbar.add(exit_button);
/* Central part layout */
JPanel left_panel = new JPanel(new BorderLayout()), center_panel = new JPanel(
new BorderLayout()), right_panel = new JPanel(
new BorderLayout());
left_panel.setBorder(new TitledBorder(new EtchedBorder(),
"Universal Reg"));
center_panel.setBorder(new TitledBorder(new EtchedBorder(),
"System Reg"));
right_panel.setBorder(new TitledBorder(new EtchedBorder(),
"Co-processor Reg"));
JPanel left_panel3 = new JPanel(new BorderLayout()), center_panel3 = new JPanel(
new BorderLayout()), right_panel3 = new JPanel(
new BorderLayout());
left_panel3.setBorder(new TitledBorder(new EtchedBorder(), "Memory"));
center_panel3.setBorder(new TitledBorder(new EtchedBorder(),
"Instructions"));
right_panel3.setBorder(new TitledBorder(new EtchedBorder(), "TLB"));
JPanel left_panel2 = new JPanel(new BorderLayout()), right_panel2 = new JPanel(
new BorderLayout());
left_panel2.setBorder(new TitledBorder(new EtchedBorder(),
"Processor Flags"));
right_panel2.setBorder(new TitledBorder(new EtchedBorder(),
"Co-Processor Flags"));
//
Box horizontal1 = Box.createHorizontalBox();
horizontal1.add(Box.createHorizontalStrut(10));
Box horizontal1_vertical_1 = Box.createVerticalBox();
horizontal1_vertical_1.add(Box.createVerticalStrut(35));
horizontal1_vertical_1.add(universal_registers);
horizontal1_vertical_1.add(Box.createVerticalGlue());
horizontal1_vertical_1.add(Box.createVerticalStrut(20));
left_panel.add(horizontal1_vertical_1, BorderLayout.CENTER);
horizontal1.add(left_panel);
horizontal1.add(Box.createHorizontalGlue());
horizontal1.add(Box.createHorizontalStrut(20));
Box vertical_system_registers = Box.createVerticalBox();
vertical_system_registers.add(system_registers1);
vertical_system_registers.add(Box.createVerticalStrut(10));
vertical_system_registers.add(system_registers2);
vertical_system_registers.add(Box.createVerticalStrut(10));
vertical_system_registers.add(system_registers3);
center_panel.add(vertical_system_registers, BorderLayout.CENTER);
horizontal1.add(center_panel);
horizontal1.add(Box.createHorizontalGlue());
horizontal1.add(Box.createHorizontalStrut(70));
Box horizontal1_vertical2 = Box.createVerticalBox();
horizontal1_vertical2.add(Box.createVerticalStrut(20));
horizontal1_vertical2.add(co_processor_registers);
horizontal1_vertical2.add(Box.createVerticalGlue());
horizontal1_vertical2.add(Box.createVerticalStrut(20));
right_panel.add(horizontal1_vertical2, BorderLayout.CENTER);
horizontal1.add(right_panel);
horizontal1.add(Box.createHorizontalGlue());
horizontal1.add(Box.createHorizontalStrut(10));
//
Box horizontal2 = Box.createHorizontalBox();
horizontal2.add(Box.createHorizontalStrut(10));
left_panel2.add(processor_flags, BorderLayout.CENTER);
horizontal2.add(left_panel2);
horizontal2.add(Box.createHorizontalStrut(290));
Box horizontal222 = Box.createHorizontalBox();
horizontal222.add(Box.createHorizontalStrut(5));
horizontal222.add(co_processor_flags);
horizontal222.add(Box.createHorizontalGlue());
horizontal222.add(Box.createHorizontalStrut(5));
right_panel2.add(horizontal222, BorderLayout.CENTER);
horizontal2.add(right_panel2);
horizontal2.add(Box.createHorizontalGlue());
horizontal2.add(Box.createHorizontalStrut(10));
//
Box horizontal3 = Box.createHorizontalBox();
horizontal3.add(Box.createHorizontalStrut(10));
left_panel3.add(memory, BorderLayout.CENTER);
horizontal3.add(left_panel3);
horizontal3.add(Box.createHorizontalStrut(80));
center_panel3.add(instructions, BorderLayout.CENTER);
horizontal3.add(center_panel3);
horizontal3.add(Box.createHorizontalStrut(10));
right_panel3.add(tlb, BorderLayout.CENTER);
horizontal3.add(right_panel3);
horizontal3.add(Box.createHorizontalGlue());
horizontal3.add(Box.createHorizontalStrut(10));
//
Box main_Layout = Box.createVerticalBox();
main_Layout.add(horizontal1);
main_Layout.add(Box.createVerticalStrut(20));
main_Layout.add(horizontal2);
main_Layout.add(Box.createVerticalStrut(20));
main_Layout.add(horizontal3);
main_Layout.add(Box.createVerticalGlue());
main_Layout.add(Box.createVerticalStrut(20));
for (byte i = 0; i < 10; i++) {
bottom_toolbar.addSeparator();
}
bottom_toolbar.add(reset_button);
for (byte i2 = 0; i2 < 15; i2++) {
bottom_toolbar.addSeparator();
}
bottom_toolbar.add(next_button);
Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(top_toolbar, BorderLayout.NORTH);
content.add(main_Layout, BorderLayout.CENTER);
content.add(bottom_toolbar, BorderLayout.SOUTH);
}
/*Функция копирования входного файла в список команд*/
private static void copyToProgramList(){
if (OPENED_FILE.equals("") == true)
return;
try{
BufferedReader inputFile = new BufferedReader(
new FileReader(OPENED_FILE));
String s, lineNumber;
lProgram.clear();
int i = 0;
long index = 0;
char array[] = {'0','0','0','0'};
while ( (s = inputFile.readLine()) != null) {
if (!s.equals("")) {
for (int j = 0; j < 4; j++) {
index = ((long)( i * 4 / Math.pow(16,3-j))) % 16;
if (index > 9 ) {array[j] = (char)('A' + index - 10);}
else {array[j] = (char)('0' + index);}
}
lineNumber = "0x" + String.copyValueOf(array) + ": ";
pcr.add(lineNumber);
lProgram.add(i, s);
i++;
}
}
inputFile.close();
Main_Frame.commandParse();
Instructions.setz( lProgram);
}
catch(IOException e) {lProgram.add(0,"IOExeception");}
}
private void set_top_ToolbarActions() {
/* Funcion sets a file filter */
FileFilter textFilt = new FileFilter() {
public boolean accept(File fileName) {
if (fileName.getAbsolutePath().toLowerCase().endsWith(".txt")
| (fileName.isDirectory()))
return true;
return false;
}
public String getDescription() {
return "Text files";
}
};
file_chooser.setFileFilter(textFilt);
ActionListener openFileChooser = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFrame container = new JFrame();
try {
file_chooser.showOpenDialog(container);
OPENED_FILE = file_chooser.getSelectedFile()
.getAbsolutePath();
//setTitle("Architecture: " + OPENED_FILE);
// извлечение текста из файла
System.out.println(" "+OPENED_FILE);
if (OPENED_FILE!=""){
functionality.get_text_from_file();
}
Main_Frame.copyToProgramList();
} catch (java.lang.NullPointerException e) {
}
container.dispose();
}
};
open_file_button.addActionListener(openFileChooser);
ActionListener exitAction = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Main_Frame.this.dispose();
}
};
exit_button.addActionListener(exitAction);
}
// Распарсивание строки
public static void commandParse()
{
int i,j;
//
for (i=0;i<lProgram.getSize();i++)
{
String buffer=(String) lProgram.getElementAt(i);
int rem,rem2;
for(j=0;j<buffer.length();j++)
{
if(buffer.substring(j, j+1).equals(" "))
{
String s=buffer.substring(0,j+1);
command.add(s);
System.err.println(" "+s);
break;
}
}
rem=j+1;
int k=rem;
for(;k<buffer.length();k++)
{
if(buffer.substring(k, k+1).equals(" "))
{
String s=buffer.substring(rem,k+1);
System.err.println("1-- "+s);
operand1.add(s);
break;
}
}
rem2=k+1;
int k2=rem2;
for(;k2<buffer.length();k2++)
{
if(buffer.substring(k2, k2+1).equals(" ")||buffer.substring(k2, k2+1).equals("\n"))
{
String s=buffer.substring(rem2,k2+1);
System.err.println("2-- "+s);
operand2.add(s);
break;
}
}
System.out.println(" "+buffer);
comand_count=i;
}
}
public void executeCommand(int index)
{
String identificator=(String) command.get(index);
if(identificator.equalsIgnoreCase("MOVI "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
universal_registers.upd_register(v_p, v2);
}
if(identificator.equalsIgnoreCase("FMOV "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
co_processor_registers.upd_register(v_p, v2);
}
if(identificator.equals("MOV "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) universal_registers.reg_value(v_p);
universal_registers.upd_register(v_p,v2+" ");
}
if(identificator.equals("DIV "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1/a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("ADD "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1+a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("ADDS "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
if(v2.indexOf("0x1")==0)
{
v2="0x0"+v2.substring(3);
}
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1-a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("SUBS "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
if(v2.indexOf("0x1")==0)
{
v2="0x0"+v2.substring(3);
}
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1+a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("DIVS "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
if(v2.indexOf("0x1")==0)
{
v2="0x0"+v2.substring(3);
}
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1/a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("MULS "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
if(v2.indexOf("0x1")==0)
{
v2="0x0"+v2.substring(3);
}
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1*a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("SUB "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1-a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("MUL "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) universal_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1*a2);
universal_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("XOR "))
{
String v=(String)operand1.get(index);
int v_p1=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
int v_p2=Integer.parseInt(v2.substring(2, v2.length()-1));
String result;
result=""+(v_p1^v_p2);
universal_registers.upd_register(v_p1,result+" ");
}
if(identificator.equals("RCL "))
{
String v=(String)operand1.get(index);
int v_p1=Integer.parseInt(v.substring(2, v.length()-1));
String result;
result="0x0000000"+(v_p1<<1);
universal_registers.upd_register(v_p1,result+" ");
}
if(identificator.equals("RCR "))
{
String v=(String)operand1.get(index);
int v_p1=Integer.parseInt(v.substring(2, v.length()-1));
String result;
result="0x0000000"+(v_p1>>1);
universal_registers.upd_register(v_p1,result+" ");
}
if(identificator.equals("INT "))
{
int indicator=processor_flags.value_flag(5);
if(indicator==0)
{
JOptionPane.showMessageDialog(null,"Interrupts are impossible","Error",JOptionPane.ERROR_MESSAGE);
}
if(indicator==1)
{
String v=(String)operand1.get(index);
System.out.println(" "+v.length());
int v_p1=Integer.parseInt(v.substring(0, v.length()-2));
String s=pcr.get(i+1).toString();
system_registers2.upd_regs(2, s);
system_registers2.upd_regs(3, processor_flags.flags());
JOptionPane.showMessageDialog(null,"Call the "+v_p1+" interrupt","Information" ,JOptionPane.PLAIN_MESSAGE);
}
}
if(identificator.equals("IRET "))
{
system_registers2.upd_regs(2, "0x00000");
system_registers2.upd_regs(3, "0x00000");
}
if(identificator.equals("OR "))
{
String v=(String)operand1.get(index);
int v_p1=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
int v_p2=Integer.parseInt(v2.substring(2, v2.length()-1));
String result;
result=""+(v_p1 | v_p2);
universal_registers.upd_register(v_p1,result+" ");
}
if(identificator.equals("AND "))
{
String v=(String)operand1.get(index);
int v_p1=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
int v_p2=Integer.parseInt(v2.substring(2, v2.length()-1));
String result;
result=""+(v_p1 & v_p2);
universal_registers.upd_register(v_p1,result+" ");
}
if(identificator.equals("FDIV "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) co_processor_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1/a2);
co_processor_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("FADD "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) co_processor_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1+a2);
co_processor_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("FSUB "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) co_processor_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1-a2);
co_processor_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("CLI "))
{
processor_flags.upd_flag(5, "0");
}
if(identificator.equals("STI "))
{
processor_flags.upd_flag(5, "1");
}
if(identificator.equals("JZ "))
{
processor_flags.upd_flag(3, "1");
}
if(identificator.equals("JNZ "))
{
processor_flags.upd_flag(3, "0");
}
if(identificator.equals("JO "))
{
processor_flags.upd_flag(1, "1");
}
if(identificator.equals("JNO "))
{
processor_flags.upd_flag(1, "0");
}
if(identificator.equals("JS "))
{
processor_flags.upd_flag(2, "1");
}
if(identificator.equals("JNS "))
{
processor_flags.upd_flag(2, "0");
}
if(identificator.equals("JC "))
{
processor_flags.upd_flag(0, "1");
}
if(identificator.equals("JCO "))
{
processor_flags.upd_flag(0, "0");
}
if(identificator.equals("RTLB "))
{
String s= system_registers3.read_reg(1);
}
if(identificator.equals("WTLB "))
{
String v=(String)operand1.get(index);
system_registers3.upd_reg(1, v);
}
//-----------------------------------
if(identificator.equals("RTIN "))
{
String s= system_registers3.read_reg(0);
}
if(identificator.equals("WTIN "))
{
String v=(String)operand1.get(index);
system_registers3.upd_reg(0, v);
}
//---------------------------------------
if(identificator.equals("RBVA "))
{
String s= system_registers3.read_reg(2);
}
if(identificator.equals("WBVA "))
{
String v=(String)operand1.get(index);
system_registers3.upd_reg(2, v);
}
//---------------------------------------
if(identificator.equals("FMUL "))
{
String v=(String)operand1.get(index);
int v_p=Integer.parseInt(v.substring(2, v.length()-1));
String v2=(String)operand2.get(index);
String a=(String) co_processor_registers.reg_value(v_p);
int a1=HexToInt(a);
int a2=HexToInt(v2);
String result=IntToHex(a1*a2);
co_processor_registers.upd_register(v_p,result+" ");
}
if(identificator.equals("SCALL "))
{
processor_flags.upd_flag(6, "0");
}
if(identificator.equals("RFE "))
{
processor_flags.upd_flag(6, "1");
}
}
public static int HexToInt(String hex)
{
int i;
int c=0;
if (hex.indexOf("0x") == 0)
hex = hex.substring(2);
for(i=hex.length();i>2;i--)
{
if(hex.indexOf("A")==0)
c+=10*Math.pow(10,i-9);
if(hex.indexOf("B")==0)
c+=11*Math.pow(10,i-9);
if(hex.indexOf("C")==0)
c+=12*Math.pow(10,i-9);
if(hex.indexOf("D")==0)
c+=13*Math.pow(10,i-9);
if(hex.indexOf("E")==0)
c+=14*Math.pow(10,i-9);
if(hex.indexOf("F")==0)
c+=15*Math.pow(10,i-9);
c+=Integer.parseInt(hex.substring(i-2, i-1))*Math.pow(16, 9-i);
}
return c;
}
public static String IntToHex(int value)
{
String hex = new String("0x");
if (value >= 0) {
long index;
for (int i = 0; i <8; i++) {
index = (long)(value / Math.pow(16, 8 - 1 - i)) % 16;
if (index > 9) hex += (char)('A' + index - 10);
else hex += (char)(index + '0');
}
}
return hex;
}
private void set_bottom_ToolbarActions() {
ActionListener next_action = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// вызов функции обработки нажатия кнопки
if(i<comand_count)
{
String s=pcr.get(i+1).toString();
system_registers2.upd_counter(s);
Instructions.counter(i);
executeCommand(i);
i++;
}
else
{
JOptionPane.showMessageDialog(null, "End of command");
next_button.setEnabled(false);
}
//functionality.next_action();
}
};
next_button.addActionListener(next_action);
ActionListener reset_action = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//вызов функции обработки нажатия кнопки
next_button.setEnabled(true);
comand_count=0;
i=0;
functionality.reset_action();
}
};
reset_button.addActionListener(reset_action);
}
}
Соседние файлы в папке CPU