Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
11
Добавлен:
01.05.2014
Размер:
18.3 Кб
Скачать
package interpretator_ASM;

import java.util.Vector;

import IO_base.IOBase;


/**
* @author Ephimov Andrey
*
* Сдесьсобраны все самые функции перехода из одного состояния в другое
* а также проверки и интерпритация комманд из файла
*
*/
public class Interpretator {
//--ОБРАЩЕНИЕ К ПАМЯТИ ПО ЧТЕНИЮ И ЗАПИСИ--
//чтение байта
public static final String RDB = "RDB";
public static final byte[] RDB_code = {0,0,0,0,0,0,0};
//чтение полуслова
public static final String RDHV = "RDHV";
public static final byte[] RDHV_code = {0,0,0,0,0,0,1};
//чтение слова
public static final String RDW = "RDW";
public static final byte[] RDW_code = {0,0,0,0,0,1,0};
//запись байта
public static final String WRB = "WRB";
public static final byte[] WRB_code = {0,0,0,0,0,1,1};
//запись полуслова
public static final String WRHW = "WRHV";
public static final byte[] WRHW_code = {0,0,0,0,1,0,0};
//запись слова
public static final String WRW = "WRW";
public static final byte[] WRW_code = {0,0,0,0,1,0,1};
//занесение в регистр значения
public static final String MOV = "MOV";
public static final byte[] MOV_code = {0,0,0,1,0,0,0};
//--АРИФМЕТИЧЕСКИЕ ОПЕРАЦИИ--
//сложение
public static final String ADD = "ADD";
public static final byte[] ADD_code = {0,0,0,1,0,1,0};
//вычитание
public static final String SUB = "SUB";
public static final byte[] SUB_code = {0,0,0,1,0,1,1};
//умножение
public static final String MUL = "MUL";
public static final byte[] MUL_code = {0,0,0,1,1,0,0};
//деление
public static final String DIV = "DIV";
public static final byte[] DIV_code = {0,0,0,1,1,0,1};
//--ЛОГИЧЕСКИЕ ОПЕРАЦИИ--
//"и"
public static final String AND = "AND";
public static final byte[] AND_code = {0,0,1,0,0,1,0};
//"или"
public static final String OR = "OR";
public static final byte[] OR_code = {0,0,1,0,0,1,1};
//исключающее "или"
public static final String XOR = "XOR";
public static final byte[] XOR_code = {0,0,1,0,1,0,0};
//"не"
public static final String NOT = "NOT";
public static final byte[] NOT_code = {0,0,1,0,1,0,1};
//циклический сдвиг влево
public static final String RCL = "RCL";
public static final byte[] RCL_code = {0,0,1,0,1,1,0};
//--КОМАНДЫ ПЕРЕХОДОВ И ПРЕРЫВАНИЙ--
//очистить флаг разрешения прерываний
public static final String CLI = "CLI";
public static final byte[] CLI_code = {0,0,1,0,1,1,1};
//вызов прерывания
public static final String INT = "INT";
public static final byte[] INT_code = {0,0,1,1,0,0,0};
//возврат из прерывания
public static final String IRET = "IRET";
public static final byte[] IRET_code = {0,0,1,1,0,0,1};
//вызов подпрограммы
public static final String CALL = "CALL";
public static final byte[] CALL_code = {0,0,1,1,0,1,0};
//возврат из подпрограммы
public static final String RET = "RET";
public static final byte[] RET_code = {0,0,1,1,0,1,1};
//безусловный переход
public static final String JMP = "JMP";
public static final byte[] JMP_code = {0,0,1,1,1,0,0};
//переход по нулю
public static final String JZ = "JZ";
public static final byte[] JZ_code = {0,0,1,1,1,0,1};
//переход не по нулю
public static final String JNZ = "JNZ";
public static final byte[] JNZ_code = {0,0,1,1,1,1,0};
//переход по переполнению
public static final String JO = "JO";
public static final byte[] JO_code = {0,0,1,1,1,1,1};
//переход по непереполнению
public static final String JNO = "JNO";
public static final byte[] JNO_code = {0,1,0,0,0,0,0};
//переход по переносу
public static final String JC = "JC";
public static final byte[] JC_code = {0,1,0,0,0,0,1};
//переход не по переносу
public static final String JNC = "JNC";
public static final byte[] JNC_code = {0,1,0,0,0,1,0};
//переход по меньше нуля
public static final String JS = "JS";
public static final byte[] JS_code = {0,1,0,0,0,1,1};
//переход по неменьше нуля
public static final String JNS = "JNS";
public static final byte[] JNS_code = {0,1,0,0,1,0,0};
//--СПЕЦИАЛЬНЫЕ ОПЕРАЦИИ--
//чтение флагов
public static final String RFL = "RFL";
public static final byte[] RFL_code = {0,1,0,0,1,0,1};
//запись флагов (супервизор)
public static final String WFL = "WFL";
public static final byte[] WFL_code = {0,1,0,0,1,1,0};
//переход в режим супервизора
public static final String SCALL = "SCALL";
public static final byte[] SCALL_code = {0,1,0,1,1,0,1};
//возврат в пользователя
public static final String RFE = "RFE";
public static final byte[] RFE_code = {0,1,0,1,1,0,0};
//ожидание прерывания
public static final String HALT = "HALT";
public static final byte[] HALT_code = {0,1,0,1,1,1,0};
//--АРИФМЕТИЧЕСКИЕ ОПЕРАЦИИ С ПТ--
//сложение
public static final String FADD = "FADD";
public static final byte[] FADD_code = {0,1,1,0,0,0,0};
//вычитание
public static final String FSUB = "FSUB";
public static final byte[] FSUB_code = {0,1,1,0,0,0,1};
//умножение
public static final String FMUL = "FMUL";
public static final byte[] FMUL_code = {0,1,1,0,0,1,0};
//деление
public static final String FDIV = "FDIV";
public static final byte[] FDIV_code = {0,1,1,0,0,1,1};
//копирование
public static final String FMOV = "FMOV";
public static final byte[] FMOV_code = {0,1,1,0,1,0,0};
//чтение
public static final String FRDW = "FRDW";
public static final byte[] FRDW_code = {0,1,1,0,1,0,1};
//запись
public static final String FWRW = "FWRW";
public static final byte[] FWRW_code = {0,1,1,0,1,1,0};
//преобразование ПТ-ФТ
public static final String MFC = "MFC";
public static final byte[] MFC_code = {0,1,1,0,1,1,1};
//преобразование ФТ-ПТ
public static final String MCF = "MCF";
public static final byte[] MCF_code = {0,1,1,1,0,0,0};
//--ТИПЫ ДАННЫХ--
//целочисленные
public static final String WORD = "WORD";
public static final String DWORD = "DWORD";
public static final String HWORD = "HWORD";
//с плавающей точкой
public static final String DOUBLE = "DOUBLE";
public static final String FLOAT = "FLOAT";

private static Vector<String> vars;

private Interpretator(){}

public static byte[] DecToBin(int dec, int len){
byte[] result = new byte[len];
int tmp;
int i=0;
if(dec==0){
return result;
}
if(dec<0){
result[result.length-1]=1;
dec=dec*(-1);
}
while(dec!=0){
tmp=Math.abs(dec/2);
if((tmp*2) < dec)result[i]=1;
else result[i]=0;
dec=tmp;
i++;
}
result=inverter(result);
return result;
}

public static int BinToDec(byte[] bin){
bin=inverter(bin);
int result=0;
for(int i=0;i<bin.length-1;i++){
result+=bin[i]*(int)Math.pow(2, i);
}
if(bin[bin.length-1]==1)result=result*(-1);
return result;
}

public static float BinToFloat(byte[] bin){
bin=inverter(bin);
int mantisa=0,por=0;
float res=0;
if(bin.length==32){
for(int i=0;i<23;i++)
mantisa+=bin[i]*(int)Math.pow(2, i);
for(int i=23;i<31;i++)
por+=bin[i]*(int)Math.pow(2, i);
res = (float)mantisa * (float)Math.pow(10, por);
if(bin[bin.length-1]==1)res=res*(-1);
return res;
}else if(bin.length==64){
for(int i=0;i<55;i++)
mantisa+=bin[i]*(int)Math.pow(2, i);
for(int i=55;i<63;i++)
por+=bin[i]*(int)Math.pow(2, i);
res = (float)mantisa * (float)Math.pow(10, por);
if(bin[bin.length-1]==1)res=res*(-1);
return res;
}else
System.err.println("Error float format!");
return 0;
}

private static byte[] inverter(byte[] block){
byte[] result = new byte[block.length];
for(int i=0; i<block.length;i++){
result[result.length-1-i]=block[i];
}
return result;
}

public static int BitToDec(byte[] reg){
int tmp = Math.abs(reg.length/2);
if(tmp*2 != reg.length) {
System.err.println("Bit format not valid for interpretation");
return 0;
}else{
Vector<byte[]> bits = new Vector<byte[]>();
for(int i=0;i<reg.length;i++){
byte[] t = DecToBin(reg[i],8);
bits.add(t);
}
byte[] res = new byte[bits.size()*8];
int k=0;
for(int i=0;i<bits.size();i++){
for(int j=0;j<8;j++){
res[k]=bits.elementAt(i)[j];
k++;
}
}
return BinToDec(res);
}
}

public static byte[] DecToBit(int dec, int len){
byte[] bitArr = DecToBin(dec, len);
byte[] res = new byte[len/8];
for(int i=0;i<res.length;i++){
byte[] tmp = new byte[8];
int l=0;
for(int j=8*i;j<(8*i+8);j++){
tmp[l]=bitArr[j];
l++;
}
res[i]=(byte)BinToDec(tmp);
}
return res;
}

public static String[] checkASMCode(String in){
String out[] = new String[5];
String command;
int i = in.indexOf(" ");
if (i!=(-1))
command = in.substring(0, i);
else return null;
//System.out.println("Interpretator: "+command);
if(command.equalsIgnoreCase(SCALL)){
out[0]=command;
out[1]="";
out[2]="";
out[3]="";
out[4]="";
for(int j=0;j<SCALL_code.length;j++)
out[4]+=SCALL_code[j];
}else if(command.equalsIgnoreCase(RFE)){
out[0]=command;
out[1]="";
out[2]="";
out[3]="";
out[4]="";
for(int j=0;j<SCALL_code.length;j++)
out[4]+=SCALL_code[j];
}else if(command.equalsIgnoreCase(WORD)){
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(!isRegName(command) && !isVar(command))
vars.add(command);
else return null;
in=in.substring(i+1);

if(in.equals("<?>"))
prepareVar(WORD,command,0);
else
prepareVar(WORD,command,Integer.parseInt(in));

}else if(command.equalsIgnoreCase(DWORD)){
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(!isRegName(command) && !isVar(command))
vars.add(command);
else return null;
in=in.substring(i+1);
if(in.equals("<?>"))
prepareVar(DWORD,command,0);
else
prepareVar(DWORD,command,Integer.getInteger(in));

}else if(command.equalsIgnoreCase(HWORD)){
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(!isRegName(command) && !isVar(command))
vars.add(command);
else return null;
in=in.substring(i+1);
if(in.equals("<?>"))
prepareVar(HWORD,command,0);
else
prepareVar(HWORD,command,Integer.getInteger(in));

}else if(command.equalsIgnoreCase(FLOAT)){
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(!isRegName(command) && !isVar(command))
vars.add(command);
else return null;
in=in.substring(i+1);
if(in.equals("<?>"))
prepareVar(FLOAT,command,0);
else
prepareVar(FLOAT,command,Float.parseFloat(in));

}else if(command.equalsIgnoreCase(DOUBLE)){
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);

if(!isRegName(command) && !isVar(command))
vars.add(command);
else return null;
in=in.substring(i+1);
if(in.equals("<?>"))
prepareVar(DOUBLE,command,0);
else
prepareVar(DOUBLE,command,Float.parseFloat(in));

}else if(command.equalsIgnoreCase(MOV)){
out[0]=command;
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(isRegName(command) || isVar(command))
out[1]=command;
else return null;
in=in.substring(i+1);
if(isRegName(in) || isVar(in))
out[2]=in;
else return null;
out[3]="";
out[4]="";
for(int j=0;j<MOV_code.length;j++)
out[4]+=MOV_code[j];

}else if(command.equalsIgnoreCase(ADD)){
out[0]=command;
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(isRegName(command) || isVar(command))
out[1]=command;
else return null;
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(isRegName(command) || isVar(command))
out[2]=command;
else return null;
in=in.substring(i+1);
if(isRegName(in) || isVar(in))
out[3]=in;
else return null;
out[4]="";
for(int j=0;j<ADD_code.length;j++)
out[4]+=ADD_code[j];

}else if(command.equalsIgnoreCase(MUL)){
out[0]=command;
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(isRegName(command) || isVar(command))
out[1]=command;
else return null;
in=in.substring(i+1);
i=in.indexOf(" ");
command=in.substring(0, i);
if(isRegName(command) || isVar(command))
out[2]=command;
else return null;
in=in.substring(i+1);
if(isRegName(in) || isVar(in))
out[3]=in;
else return null;
out[4]="";
for(int j=0;j<MUL_code.length;j++)
out[4]+=MUL_code[j];

}else return null;
return out;
}

private static void prepareVar(String type, String varName, float value) {
byte[] res;
if(type.equals(FLOAT)){
res=new byte[6];
res[0]=4;
res[1]=varName.getBytes()[0];
byte[] tmp = FloatToBit(value,32);
for(int i=0;i<tmp.length;i++)
res[i+2]=tmp[i];
IOBase.registrateVar(res);
}else if(type.equals(DOUBLE)){
res=new byte[10];
res[0]=5;
res[1]=varName.getBytes()[0];
byte[] tmp = FloatToBit(value,64);
for(int i=0;i<tmp.length;i++)
res[i+2]=tmp[i];
IOBase.registrateVar(res);
}
}

public static byte[] FloatToBit(float value, int len) {
byte[] res = new byte[len/8];
byte[] bit = new byte[len];
int por=0 ,mantisa=0;
if(value==0)
return res;
if(value<0){
bit[len-1]=1;
value=value*(-1);
}
while(value>1){
por++;
value=value/10;
}
while(value!=(int)value)
value=value*10;
mantisa=(int)value;
byte[] tmp = DecToBin(mantisa,len-9);
int l=0;
for(int i=0;i<tmp.length;i++){
bit[l]=tmp[i];
l++;
}
tmp = DecToBin(por,8);
for(int i=0;i<tmp.length;i++){
bit[l]=tmp[i];
l++;
}
bit = inverter(bit);
return DecToBit(BinToDec(bit),len);
}

public static float BitToFloat(byte[] reg){
int tmp = Math.abs(reg.length/2);
if(tmp*2 != reg.length) {
System.err.println("Bit format not valid for interpretation");
return 0;
}else{
Vector<byte[]> bits = new Vector<byte[]>();
for(int i=0;i<reg.length;i++){
byte[] t = DecToBin(reg[i],8);
bits.add(t);
}
byte[] res = new byte[bits.size()*8];
int k=0;
for(int i=0;i<bits.size();i++){
for(int j=0;j<8;j++){
res[k]=bits.elementAt(i)[j];
k++;
}
}
return BitToFloat(res);
}
}

private static void prepareVar(String type, String varName, int value) {
byte[] res;
if(type.equals(WORD)){
res=new byte[6];
res[0]=1;
res[1]=varName.getBytes()[0];
byte[] tmp = DecToBit(value,32);
for(int i=0;i<tmp.length;i++)
res[i+2]=tmp[i];
IOBase.registrateVar(res);
}else if(type.equals(DWORD)){
res=new byte[10];
res[0]=2;
res[1]=varName.getBytes()[0];
byte[] tmp = DecToBit(value,64);
for(int i=0;i<tmp.length;i++)
res[i+2]=tmp[i];
IOBase.registrateVar(res);
}else if(type.equals(HWORD)){
res=new byte[4];
res[0]=3;
res[1]=varName.getBytes()[0];
byte[] tmp = DecToBit(value,16);
for(int i=0;i<tmp.length;i++)
res[i+2]=tmp[i];
IOBase.registrateVar(res);
}else if(type.equals(DOUBLE)){
res=new byte[10];
res[0]=5;
res[1]=varName.getBytes()[0];
IOBase.registrateVar(res);
}else if(type.equals(FLOAT)){
res=new byte[6];
res[0]=4;
res[1]=varName.getBytes()[0];
IOBase.registrateVar(res);
}
}

private static boolean isVar(String command) {
if(vars.size()!=0){
for(int i=0;i<vars.size();i++){
if(command.equals(vars.elementAt(i)))
return true;
}
}
return false;
}

private static boolean isRegName(String command) {
for(int i=1;i<=64;i++){
String tmp = "R"+i;
if(command.equalsIgnoreCase(tmp))
return true;
}
return false;
}

public static void callCommand(String[] read) {
byte[] operation = new byte[32];
byte[] KOP = codeOfOperation(read[0]);
byte[] R0 = codeR0(read[1]);
byte[] R1 = codeR0(read[2]);
byte[] R2 = codeR1(read[3]);
int i,l = 1;

for(i=0;i<KOP.length;i++){
operation[l]=KOP[i];
l++;
}
for(i=0;i<R0.length;i++){
operation[l]=R0[i];
l++;
}
for(i=0;i<R1.length;i++){
operation[l]=R1[i];
l++;
}
for(i=0;i<R2.length;i++){
operation[l]=R2[i];
l++;
}

IOBase.sendOperation(DecToBit(BinToDec(operation),32));
}

private static byte[] codeR0(String str) {
byte res[] = new byte[8];
if(str=="" || str==null)
return res;
if(isRegName(str)){
str=str.substring(1);
//System.out.println(str);
int tmp = Integer.parseInt(str);
//System.out.println(tmp);
res = DecToBin(tmp,8);
}else if(isVar(str)){
System.out.println(str);
byte [] tmp = str.getBytes();
System.out.println(tmp[0]);
res = DecToBin(tmp[0],8);
}else{
System.err.println("Error generating operation");
System.exit(1);
}
return res;
}

private static byte[] codeR1(String str) {
byte res[] = new byte[8];
if(str=="" || str==null)
return res;
if(isRegName(str)){
str=str.substring(1);
int tmp = Integer.parseInt(str);
System.out.println(tmp);
res = DecToBin(tmp,8);
}else if(isVar(str)){
byte [] tmp = str.getBytes();
res = DecToBin(tmp[0],8);
}else if(str.length()!=0){
int tmp = Integer.parseInt(str);
System.out.println(tmp);
res = DecToBin(tmp,8);
}
return res;
}

private static byte[] codeOfOperation(String str) {
if(str.equalsIgnoreCase(SCALL)){
return SCALL_code;
}else if(str.equalsIgnoreCase(RFE)){
return RFE_code;
}else if(str.equalsIgnoreCase(MOV)){
return MOV_code;
}else if(str.equalsIgnoreCase(MUL)){
return MUL_code;
}else if(str.equalsIgnoreCase(ADD)){
return ADD_code;
}else if(str.equalsIgnoreCase(FMOV)){
return FMOV_code;
}else if(str.equalsIgnoreCase(FMUL)){
return FMUL_code;
}else if(str.equalsIgnoreCase(FADD)){
return FADD_code;
}
return null;
}

public static void resetVars(){
vars = new Vector<String>();
}
}