السلام عليكم ورحمة الله وبركاته
أرجو ان تساعدوني في هذا الكود وهو محاكاة لل CPU
لا أعرف أين ماهي الأخطاء أو سببها
#include <iostream>
using namespace std;
# include <string>
using std::string;
string A;// accumlater reister
int X;// index register
int B;// base register
int L;// linkage register
int PC =0; // program counter
int SW; // statuse register
int S; //general register
int T;// general regster
int C; // condition code
int f ;// floating point register
string op;// string containg the opcode for each instruction
int n,i,x,p,b,e;// flag bits
int m;// operand
string readline[32] ;//line read from the memory
string binaryline[];// save the binary convert from the machine code
string oparray[];// save the first 8 bit that represent opcode
int TA ;
int disp;// variable save the displacment value in decimal
int address;// variable save the address value in decimal
class cpu {
// this function take the srting read from file and convert it to binary code
void covert(string s1){// covert function
for (int j=0 ; j< 48;j++){
if (readline[j]=='0'){binaryline[j]='0000';}
else
if (readline[j]=="1"){binaryline[j]=="0001" ;}
else
if (readline[j]=="2"){binaryline[j]=="0010" ;}
else
if (readline[j]=="3"){binaryline[j]=="0011" ;}
else
if (readline[j]=="4"){bianryline[j]== "0100" ;}
else
if (readline[j]=="5"){binaryline[j]== "0101" ;}
else
if (readline[j]=="6"){binarytline[j]== "0110" ;}
else
if (readline[j]=="7"){binaryline[j]=="0111" ;}
else
if (readline[j]=="8"){binaryline[j]=="1000" ;}
else
if (readline[j]=="9"){binaryline[j]=="1001" ;}
else
if (readline[j]=="A" || readline[j]=="a"){binaryline[j]=="1010" ;}
else
if (readline[j]=="B"|| readline[j]=="b"){binaryline[j]=="1011" ;}
else
if (readline[j]=="C"|| readline[j]=="c"){binaryline[j]=="1100" ;}
else
if (readline[j]=="D"|| readline[j]=="d"){binaryline[j]=="1101" ;}
else
if (readline[j]=="E"|| readline[j]=="e"){binaryline[j]=="1110" ;}
else
if (readline[j]=="F"|| readline[j]=="f"){binaryline[j]=="1111" ;}
else
if (readline[j]=="_"|| readline[j]==" "){binaryline[j]==" " ;}
}
}
// the function of the Appendix
void LDT ( int m) {// load the address of m in the register T
m = &T;
}
void LDX (int m){// load the value of m in the register x
m=x;
}
double MUL (int m){ // Multiply the m with the value in A
A=A*m;
return A;
}
void MULR (int r1, int r2){ // Multiply floating point
r2=r2*r1;
}
int OR (int m){// give the valur of A after oring m and A
A=A||m;
return A;
}
void RMO(string r1, string r2){// move r1 and put it in the r2
r2=r1 ;
}
void RSUB (){
PC=L;
}
void SHIFTL (int r1, int n){
r1<< n; // n number of time to do shift left
}
void SHIFTR (string r, int n){
r1>> n;
}
void STA (int m){// store in the Accumalter
m=A;
}
void STB (int m){// store in the Base
m=B;
}
void STCH (string y){
while (int i=24,j=0; i>16,j<8 ;i--, j++)// store the rightmost bit in A into m
m[j]= A;
}
void STL (int m){ // store in the L register
m=L;
}
void STS (int m){// store in register S
m=S;
}
void STSW (int m){// store in register SW
m=SW;
}
void STT (int m){// store in register T
m=T;
}
void STX (int m){// store in register X
m=X;
}
double SUB (int m){// subtraction
A=A-m;
return A;
}
void SUBR (int r1,int r2){ // subtraction
r2= r2-r1;
}
void SUBF (int m){// subtraction floating point
F= F-m;
}
/* this function decide what format */
void detect(string bl){// format 2
for(int g=0 ,w=0;w<8 , g<8;g++)
binaryline[g]= oparray[w];
int r1 = contodecimal (readline,3);// to the decimal value of r1
int r2=contodecimal (readline,4);// to find the decimal value of r2
if (oparray=="10011000"){
MULR (r1,r2);
PC= PC+2;
}
else
if (oparray=="10101100"){
RMO (r1,r2);
PC=PC+2;
}
else
if (oparray=="10100100"){
n= contodecimal2 (readline[4]);
SHIFTL (r1,n);
PC=PC+2;
}
else
if (oparray=="10101000"){
n= contodecimal2 (readline[4]);
SIHFTR (r1,n);
PC=PC+2;
}
else
if (oparray=="10010100"){
SUBR (r1,r2);
PC=PC+2;
}
else // format 3
if ((oparray=="01110100")||( oparray=="0110101") ||( oparray=="01110110" )||( oparray=="01110111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
LDT (m);
}
else
if ((oparray =="00000100")|| (oparray=="00000101") ||( oparray=="00000110") ||( oparray=="00000111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
LDX (m);
}
else
if ((oparray =="11001000")||( oparray=="11001001") || (oparray=="11001010") ||( oparray=="11001011")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
OR (m);
}
else
if ((oparray =="01001100")||( oparray=="01001101") ||( oparray=="01001110") || (oparray=="01001111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
}
RSUB ();
}
else
if ((oparray =="11101100")||( oparray=="11101101") ||( oparray=="11101110") ||( oparray=="11101111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
SSK ();
}
else
if ((oparray =="00001100")|| (oparray=="00001101") || (oparray=="00001110") ||( oparray=="00001111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STA (m);
}
else
if ((oparray =="01111000")||( oparray=="01111001" )||( oparray=="01111010") ||( oparray=="01111011")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STB (m);
}
else
if ((oparray =="01010100")||( oparray=="01010101" )||( oparray=="01010110") ||( oparray=="01010111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STCH (m);
}
else
if ((oparray =="00010100")||( oparray=="00010101") ||( oparray=="00010110" )||( oparray=="00010111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STL (m);
}
else
if ((oparray =="01111100")|| (oparray=="01111101" )|| (oparray=="01111110") || (oparray=="01111111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STS (m);
}
else
if ((oparray =="11101000")|| (oparray=="11101001") ||( oparray=="11101010") || (oparray=="11101011")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STSW (m);
}
else
if ((oparray =="10000100")||( oparray=="10000101") ||( oparray=="10000110") ||( oparray=="10000111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STT (m);
}
else
if ((oparray =="00010000")||( oparray=="00010001" )||( oparray=="00010010") ||( oparray=="00010011")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
STX (m);
}
else
if ((oparray =="00011100")||( oparray=="00011101" )||( oparray=="00011110") ||( oparray=="00011111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
SUB (m);
}
else
if ((oparray =="01011100")||( oparray=="01011101" )||( oparray=="01011110") ||( oparray=="0101111")){
if (oparray[6]=='0'&& oparray[7]== '1'){// if immediate addressing
n=0,
i=1;
m=contodecimal(readline,3)+contodecimal(readline,4)+contodecimal(readline,4)+contodecimal(readline,6);
}
else
if (oparray[6]=='1'&& oparray[7]== '0'){// in direct addressing mode
decode ();
m = m[TA];// target address represent the address for the location where the value located in memory
}
else
if (oparray[6]=='1'&& oparray[7]== '1' ||oparray[6]=='0'&& oparray[7]== '0'){// simple addressing
decode();
m =
}
SUBF (m);
}
}// enf of detect
// this function use with format 3 to
// get the value of the x, b,p,e
void decode (){
if (readline[3]=='0'){
x=0;
b=0;
p=0;
e=0;
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA=disp;
}
else
if (readline[3]=='1'){
x=0;
b=0;
p=0;
e=1;
PC=PC+4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA=disp;
}
else
if (readline[3]== '2'){
x=0;
b=0;
p=1;
e=0;
PC+=3
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA = PC + disp;
}
else
if (readline [3]== '3'){
x=0;
b=0;
p=1;
e=1;
PC=PC +4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA = PC + address;
}
else
if (readline[3]== '4')
{
x=0;
b=1;
p=0;
e=0;
PC =PC+3;
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA= B+ disp;
}
else
if (readline [3]== '5'){
x=0;
b=1;
p=0;
e=1;
PC =PC+4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA = B+ address;
}
else
if (readline [3]== '6'){
x=0;
b=1;
p=1;
e=0;
PC=PC +3;
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA = B+PC+disp;
}
else
if (readline [3]== '7'){
x=0;
b=1;
p=1;
e=1;
PC=Pc+4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA=B+PC+address;
}
else
if (readline [3]== '8'){
x=1;
b=0;
p=0;
e=0;
PC=PC+3;
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA =X+ disp;
}
else
if (readline [3]== '9'){
x=1;
b=0;
p=0;
e=1;
PC=PC+4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA=X +address ;
}
else
if ((readline [3]== 'a')|| (readline [3]== 'A')){
x=1;
b=0;
p=1;
e=0;
PC= PC+3;
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA=PC +X+disp;
}
else
if ((readline [3]== 'b') ||( readline [3]== 'B')){
x=1;
b=0;
p=1;
e=1;
PC=PC +4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA= PC +address + X;
}
else
if ((readline [3]== 'c')||( readline [3]== 'C' )){
x=1;
b=1;
p=0;
e=0;
PC= PC+3;
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA = B + X+ disp;
}
else
if ((readline [3]== 'd')||( readline [3]== 'D') ){
x=1;
b=1;
p=0;
e=1;
PC= PC+ 4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA= B + X+address;
}
else
if ((readline [3]== 'e') || (readline [3]== 'E')){
x=1;
b=1;
p=1;
e=0;
PC =PC +3;
disp=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6);
TA = PC + B + X + disp;////////////??????????????????????
}
else
if ((readline [3]== 'f') || (readline [3]== 'F')){
x=1;
b=1;
p=1;
e=1;
PC=PC+ 4;
address=contodecimal(readline,4)+contodecimal(readline,5)+ contodecimal(readline,6)+contodecimal(readline,7)+contodecimal(readline,8);
TA= PC + X+ B + address;
}
}// end of decode
// this function convert to decimal so we can now what the parameter r1,
// use just in format 2
int contodecimal(string readline[] ,int i){
int var;
if (readline == '0')
var =0;
else
if (readline == '1')
var =1;
else
if (readline == '2')
var =2;
else
if (readline == '3')
var =3;
else
if (readline == '4')
var =4;
else
if (readline == '5')
var =5;
else
if (readline == '6')
var =6;
else
if (readline == '7')
var =7;
else
if (readline == '8')
var =8;
else
if (readline == '9')
var =9;
else
if ((readline == 'a') || (readline[]== 'A'))
var =10;
else
if ((readline == 'b') || (readline== 'B'))
var =11;
else
if ((readline == 'c')|| (readline== 'C'))
var =12;
else
if ((readline == 'd')||( readline== 'D'))
var =13;
else
if ((readline == 'e')|| (readline== 'E'))
var =14;
else
if ((readline == 'f')|| (readline== 'F'))
var =15;
return var;
}
};