chmpgne

ARM emulator

Jun 1st, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 54.97 KB | None | 0 0
  1. //Copyright Joshua Hargreaves. Any questions [email protected] or [email protected]
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. #include "ctype.h"
  6. #include "stdint.h"
  7.  
  8. int32_t regs[16];
  9. int N, Z, C, V;
  10. int execute = 0;
  11. int trace = 0;
  12.  
  13. typedef struct {
  14. int32_t r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,
  15.      r12,r13,r14,r15,r16,r17,r18;
  16. } registers;
  17.  
  18. typedef struct {
  19. char address[33];
  20. char instruction[33];
  21. } Current;
  22.  
  23. typedef struct List {
  24.     char instruction[33];
  25.     int32_t address;
  26.     struct List *next;
  27. } list;
  28.  
  29. typedef struct {
  30.         list *head;
  31.         list *tail;
  32. }FullList;
  33.  
  34. FullList memList;
  35.  
  36. int getRegValue(int reg) {
  37.     if(reg == 15) {
  38.         return (regs[reg] + 4);
  39.     } else {
  40.         return (regs[reg]);
  41.     }
  42. }
  43.  
  44. list *addToList(list *List, char *c, int32_t x) {
  45.     list *l = malloc(sizeof(list));
  46.     l->next = calloc(1, sizeof(list));
  47.     l->next = List;
  48.     l->address = x;
  49.     strcpy(l->instruction, c);
  50.     return l;
  51. }
  52.  
  53. FullList addFullList ( FullList c, char *cha, int32_t x ) {
  54.     if (c.head == NULL){
  55.         c.head= addToList(c.head, cha, x);
  56.         c.tail = c.head;
  57.     }
  58.     else {
  59.         c.tail->next = addToList(NULL, cha, x);
  60.         c.tail = c.tail->next;
  61.     }
  62.     return c;
  63. }
  64.  
  65. int32_t searchList(list *List, int32_t x) {
  66.     while(List != NULL) {
  67.         if(List->address == x) {
  68.             return 0;
  69.         } else {
  70.             List = List->next;
  71.         }
  72.     }
  73.     return 1;
  74. }
  75.  
  76. store(list *List, int32_t x, char *c) {
  77.     int found = 0;
  78.     while(List != NULL) {
  79.         if(List->address == x) {
  80.             found = 1;
  81.             strcpy(List->instruction, c);
  82.             List = List->next;
  83.         } else {
  84.             List = List->next;
  85.         }
  86.     }
  87.     if(found == 0) {
  88.         addFullList(memList, c, x);
  89.     }
  90. }
  91.  
  92. char *fetched(list *List, int32_t x) {
  93.     while(List != NULL) {
  94.         if(List->address == x) {
  95.             return strdup(List->instruction);
  96.         } else {
  97.             List = List->next;
  98.         }
  99.     }
  100.     execute = 1;
  101.     return "0";  
  102. }
  103.  
  104. char *fetch(list *List, int32_t x) {
  105.     while(List != NULL) {
  106.         if(List->address == x) {
  107.             return strdup(List->instruction);
  108.         } else {
  109.             List = List->next;
  110.         }
  111.     }
  112.     return "0";  
  113. }
  114.  
  115. //Checks whether a file has a .emu extension
  116. int32_t isEmu(char *c) {
  117.     int32_t length = strlen(c);
  118.     if(c[length-1] != 'u' || c[length-2] != 'm' || c[length-3] != 'e' ||
  119.             c[length-4] != '.') {
  120.             return 1;    
  121.     } else {
  122.             return 0;
  123.     }    
  124. }
  125.  
  126. tracep() {
  127.     printf("R0=%08X ", regs[0]);
  128.     printf("R1=%08X ", regs[1]);
  129.     printf("R2=%08X ", regs[2]);
  130.     printf("R3=%08X ", regs[3]);
  131.     printf("R4=%08X ", regs[4]);
  132.     printf("R5=%08X ", regs[5]);
  133.     printf("R6=%08X ", regs[6]);
  134.     printf("R7=%08X ", regs[7]);
  135.     printf("R8=%08X ", regs[8]);
  136.     printf("R9=%08X ", regs[9]);
  137.     printf("R10=%08X ", regs[10]);
  138.     printf("R12=%08X ", regs[12]);
  139.     printf("SP=%08X ", regs[13]);
  140.     printf("LR=%08X ", regs[14]);
  141.     printf("PC=%08X\n",regs[15]);
  142.     printf("Flags: N = %d, Z = %d, C = %d, V = %d\n", N,Z,C,V);
  143. }
  144.  
  145. //converts to binary
  146. char* convertToBinary(char *c) {
  147.     char binary[32] = "";
  148.     char temp[32];
  149.     int i = 0;
  150.     strcpy(binary,"");
  151.     strcpy(temp,"");
  152.     for(i = (strlen(c) -1); i >= 0; i--) {
  153.     strcpy(temp,binary);
  154.         switch(c[i]) {
  155.             case '0':
  156.                 sprintf(binary, "0000%s", temp); break;
  157.             case '1':
  158.                 sprintf(binary, "0001%s", temp); break;
  159.             case '2':
  160.                 sprintf(binary, "0010%s", temp); break;
  161.             case '3':
  162.                 sprintf(binary, "0011%s", temp); break;
  163.             case '4':
  164.                 sprintf(binary, "0100%s", temp); break;
  165.             case '5':
  166.                 sprintf(binary, "0101%s", temp); break;
  167.             case '6':
  168.                 sprintf(binary, "0110%s", temp); break;
  169.             case '7':
  170.                 sprintf(binary, "0111%s", temp); break;
  171.             case '8':
  172.                 sprintf(binary, "1000%s", temp); break;
  173.             case '9':
  174.                 sprintf(binary, "1001%s", temp); break;
  175.             case 'A':
  176.                 sprintf(binary, "1010%s", temp); break;
  177.             case 'B':
  178.                 sprintf(binary, "1011%s", temp); break;
  179.             case 'C':
  180.                 sprintf(binary, "1100%s", temp); break;
  181.             case 'D':
  182.                 sprintf(binary, "1101%s", temp); break;
  183.             case 'E':
  184.                 sprintf(binary, "1110%s", temp); break;
  185.             case 'F':
  186.                 sprintf(binary, "1111%s", temp); break;
  187.             default :
  188.                 return "error"; break;  
  189.         }
  190.     }
  191.     return strdup(binary);
  192. }
  193.  
  194. char *intToBinary(int32_t x) {
  195.     int32_t y = 1;
  196.     int total = 2;
  197.     int i = 0;
  198.     char string[33];
  199.     char temp[33];
  200.     int32_t test = 0;
  201.     strcpy(string,"");
  202.     strcpy(temp, "");
  203.     for(i = 0; i<32; i++) {
  204.         if(getBit(x, i) == 0) {
  205.             strcpy(temp, string);
  206.             sprintf(string, "0%s", temp);  
  207.         } else {
  208.             strcpy(temp, string);
  209.             sprintf(string, "1%s", temp);  
  210.         }
  211.     }
  212.     return strdup(string);
  213. }
  214.  
  215. int32_t signedBinaryToInt(char *c) {
  216.     int32_t length = strlen(c);
  217.     int32_t i;
  218.     int32_t total = 1;
  219.     int32_t runningTotal = 0;
  220.     for(i = length -1; i>=0; i--) {
  221.         if(i != 0) {
  222.             if(c[i] == '1') {
  223.                 runningTotal = runningTotal + total;        
  224.             }
  225.             total = total * 2;
  226.         } else {
  227.             if(c[i] == '1') {
  228.                 runningTotal = -total + runningTotal;        
  229.             }    
  230.         }
  231.     }
  232.     //printf("%d\n", runningTotal);
  233.     return runningTotal;
  234. }
  235.  
  236. int32_t unsignedBinaryToInt(char *c) {
  237.     if(c != NULL) {
  238.         int32_t length = strlen(c);
  239.         int32_t i;
  240.         int32_t total = 1;
  241.         int32_t runningTotal = 0;
  242.         for(i = length -1; i>=0; i--) {
  243.                 if(c[i] == '1') {
  244.                     runningTotal = runningTotal + total;        
  245.                 }
  246.                 total = total * 2;
  247.         }
  248.         //printf("%d\n", runningTotal);
  249.         return runningTotal;
  250.     } else {
  251.         return 0;
  252.     }
  253. }
  254. char *rightRotate(char * c, int32_t k) {
  255.     char *temp = strdup(c);
  256.         int32_t i,j;
  257.         for(j = 0; j<k; j++) {
  258.             temp[0] = c[31];
  259.             for(i = 30; i >= 0; i--) {
  260.                 temp[i+1] = c[i];
  261.             }
  262.             //printf("12345678912345678912345678912345 rotated is: %s\n", temp);
  263.                     //12345678912345678912345678912345
  264.             c = strdup(temp);
  265.     }
  266.     return strdup(c);
  267. }
  268.  
  269. char *substring(char *c, int32_t x, int32_t y) {
  270.    
  271.     char string[(y-x)+3];
  272.     //printf("the array size is %d\n", (y-x)+3);
  273.     int32_t i = 0;
  274.     int32_t j = 0;
  275.     for(i = x; i<=y; i++) {
  276.         string[j] = c[i];
  277.         j++;    
  278.     }
  279.     string[j] = '\0';
  280.    // printf("termination character is: %d\n", (j+1));
  281.     return strdup(string);
  282. }
  283.  
  284. uint32_t RotateRight32Bit(uint32_t Data, uint32_t Bits) {
  285.   return ((Data >> Bits) | (Data << (32-Bits)));
  286. }
  287.  
  288. uint32_t RotateLeft32Bit(uint32_t Data, uint32_t Bits) {
  289.   return ((Data << Bits) | (Data >> (32-Bits)));
  290. }
  291.  
  292. int32_t addressModeOne(char *c) {
  293.     int32_t x = unsignedBinaryToInt("001");
  294.     int32_t k, y, z, a;
  295.     int32_t temp;
  296.     char *string;
  297.     char *amount;
  298.  
  299.     if(c[6] == '1') {
  300.         string = substring(c, 24, 31);
  301.         x = unsignedBinaryToInt(string);
  302.         amount = substring(c, 20, 23);
  303.         k = unsignedBinaryToInt(amount);
  304.         if(trace == 1) {
  305.             printf(" #%d", x);
  306.             if(k != 0) {
  307.                 printf(", %d\n", k*2);
  308.             } else {
  309.                 printf("\n");
  310.             }
  311.         }
  312.         y = RotateRight32Bit(x, 2*k);
  313.     } else if (c[25] == '0' && c[27] == '0'){
  314.         string = substring(c, 28, 31);
  315.         x = unsignedBinaryToInt(string);
  316.         temp = regs[x];
  317.         if(x == 15) {
  318.             temp = regs[x] + 4;
  319.         }
  320.         k = unsignedBinaryToInt(substring(c, 20, 24));
  321.         if(trace ==1) {
  322.             printf(" ");
  323.             printRegNum(x);
  324.         }
  325.         if(c[26] == '0'){
  326.             y = temp << k;
  327.             if(trace == 1) {
  328.                 if(k != 0) {
  329.                     printf(", LSL #%d\n", k);
  330.                 } else {
  331.                     printf("\n");
  332.                 }
  333.             }
  334.         } else {
  335.             if(k == 0) {
  336.                 y = 0;
  337.                 if(trace == 1) {
  338.                     printf(", LSR #32\n");
  339.                 }
  340.             } else {
  341.                 y = temp >> k;
  342.             }
  343.             if(k != 0) {
  344.                 if(trace == 1) {
  345.                     printf(", LSR #%d\n", k);
  346.                 }
  347.             } else {
  348.                 if(trace == 1) {
  349.                     printf("\n");
  350.                 }
  351.             }
  352.         }
  353.        
  354.     } else {
  355.         string = substring(c, 28, 31);
  356.         x = unsignedBinaryToInt(string);
  357.         temp = regs[x];
  358.         if(x == 15) {
  359.             temp = regs[x] + 4;
  360.         }
  361.         //printf("value in reg to be shifted %d\n", regs[x]);
  362.         k = unsignedBinaryToInt(substring(c, 20, 23));
  363.         //printf("the values in the shift register is %d\n", regs[k]);
  364.         if(trace == 1) {
  365.             printf(" ");
  366.             printRegNum(x);
  367.         }
  368.         if(c[26] == '0'){
  369.             if(trace == 1) {
  370.                 if(k != 0) {
  371.                     printf(", LSL ");
  372.                     printRegNum(k);
  373.                 }
  374.             }
  375.             y = temp << regs[k];
  376.         } else if(k != 0) {
  377.             if(trace == 1) {
  378.                 printf(", LSL ");
  379.                 printRegNum(k);
  380.             }
  381.         }
  382.             if(trace == 1) {
  383.                 printf("\n");
  384.             }
  385.         }
  386.     return y;    
  387. }
  388.  
  389. int getBit(int x, int k) {
  390.     return ((x >> k) & 1);
  391. }
  392.  
  393. STR(char *c, int flag) {
  394.     char *string, temp;
  395.     int32_t rn, rd, rm, immediate, address, shiftim;
  396.     string = substring(c, 20, 24);
  397.     shiftim = unsignedBinaryToInt(string);
  398.     string = substring(c, 16, 19);
  399.     rd = unsignedBinaryToInt(string);
  400.     string = substring(c, 12, 15);
  401.     rn = unsignedBinaryToInt(string);
  402.     string = substring(c, 20, 31);
  403.     immediate = unsignedBinaryToInt(string);
  404.     string = substring(c, 28, 31);
  405.     rm = unsignedBinaryToInt(string);
  406.     if(trace == 1) {
  407.         printRegNum(rd);
  408.     }
  409.     if(trace == 1) {
  410.         printf(", ");
  411.     }
  412.     if(c[6] == '0' && c[7] == '1' && c[10] == '0') {//Immediate offset
  413.         if(c[8] == '1') {
  414.             if(flag == 0) {
  415.                 address = getRegValue(rn) + immediate;
  416.                 store(memList.head, address, intToBinary(getRegValue(rd)));
  417.             }
  418.             if(trace == 1) {
  419.                 printf("[");
  420.                 printRegNum(rn);
  421.                 if(immediate != 0) {
  422.                      printf(", #%d]\n", immediate);
  423.                 } else {
  424.                         printf("]\n");
  425.                 }
  426.             }
  427.         } else {
  428.             if(flag == 0) {
  429.                 address = getRegValue(rn)- immediate;
  430.                 store(memList.head, address, intToBinary(getRegValue(rd)));
  431.             }
  432.             if(trace == 1) {
  433.                 printf("[");
  434.                 printRegNum(rn);
  435.                 if(immediate!= 0) {
  436.                      printf(", #-%d]\n", immediate);
  437.                 } else {
  438.                         printf("]\n");
  439.                 }
  440.             }
  441.         }
  442.        
  443.     } else if(c[6] == '0' && c[7] == '1' && c[10] == '1') {//immediate pre index
  444.         if(c[8] == '1') {// rn + immediate
  445.             if(flag == 0) {
  446.                 regs[rn] = getRegValue(rn) + immediate;
  447.                 store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
  448.             }
  449.             if(trace == 1) {
  450.                 printf("[");
  451.                 printRegNum(rn);
  452.                 if(address != 0) {
  453.                      printf(", #%d]!\n", immediate);
  454.                 } else {
  455.                         printf("]!\n");
  456.                 }
  457.             }
  458.         } else {
  459.             if(flag == 0) {
  460.                 regs[rn] = getRegValue(rn) - immediate;
  461.                 store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
  462.             }
  463.             if(trace == 1) {
  464.                 printf("[");
  465.                 printRegNum(rn);
  466.                 if(address != 0) {
  467.                      printf(", #-%d]!\n", immediate);
  468.                 } else {
  469.                         printf("]!\n");
  470.                 }
  471.             }
  472.         }
  473.     }  else if(c[6] == '0' && c[7] == '0' && c[10] == '0') {//immediate post index
  474.             if(c[8] == '1') {// rn + immediate
  475.                 if(flag == 0) {
  476.                     store(memList.head, getRegValue(rn), intToBinary(getRegValue(rd)));
  477.                     regs[rn] = getRegValue(rn) + immediate;
  478.                 }
  479.                 if(trace == 1) {
  480.                     if(trace == 1) {
  481.                         printf("[");
  482.                         printRegNum(rn);
  483.                         printf("]");
  484.                         if(immediate != 0) {
  485.                             printf(", #%d\n", immediate);
  486.                         } else {
  487.                             printf("\n");
  488.                         }
  489.                     }
  490.                 }
  491.             } else {
  492.             if(flag == 0) {
  493.                 store(memList.head, getRegValue(rn), intToBinary(getRegValue(rd)));
  494.                 regs[rn] = getRegValue(rn) - immediate;
  495.             }
  496.             if(trace == 1) {
  497.                 if(trace == 1) {
  498.                     printf("[");
  499.                     printRegNum(rn);
  500.                     printf("]\n");
  501.                     if(immediate != 0) {
  502.                         printf(", #-%d]\n", immediate);
  503.                     }
  504.                 }
  505.             }
  506.         }
  507.     } else if(c[6] == '1' && c[7] == '1' && c[10] == '0') {//register offset
  508.         if(c[8] == '1') {// rn + immediate
  509.             if(c[25] == '0' && c[26] == '0') {
  510.                 if(flag == 0) {
  511.                     address = getRegValue(rn) + (getRegValue(rm) << shiftim);
  512.                     store(memList.head, address, intToBinary(getRegValue(rd)));
  513.                 }
  514.                 if(trace == 1) {
  515.                     printf("[");
  516.                     printRegNum(rn);
  517.                     printf(", ");
  518.                     printRegNum(rm);
  519.                     if(shiftim == 0) {
  520.                         printf("]\n");    
  521.                     } else {
  522.                         printf(", LSL #%d]\n", shiftim);
  523.                    
  524.                     }
  525.                 }
  526.             } else {
  527.                 if(shiftim != 0) {
  528.                     address = getRegValue(rn) + (getRegValue(rm) >> shiftim);
  529.                 } else {
  530.                     address = getRegValue(rn);
  531.                 }
  532.                 if(trace == 1) {
  533.                                 printf("[");
  534.                                 printRegNum(rn);
  535.                                 printf(", ");
  536.                                 printRegNum(rm);
  537.                                 if(shiftim == 0) {
  538.                                     printf(", LSR #32]\n");    
  539.                                 } else {
  540.                                     printf(", LSR #%d]\n", shiftim);
  541.                                 }
  542.                     }
  543.                 if(flag == 0) {
  544.                    
  545.                 }
  546.            }
  547.         } else {
  548.             if(c[25] == '0' && c[26] == '0') {
  549.                 if(flag == 0) {
  550.                     address = getRegValue(rn) - (getRegValue(rm) << shiftim);
  551.                     store(memList.head, address, intToBinary(getRegValue(rd)));
  552.                 }
  553.                 if(trace == 1) {
  554.                         printf("[");
  555.                         printRegNum(rn);
  556.                         printf(", -");
  557.                         printRegNum(rm);
  558.                         if(shiftim == 0) {
  559.                             printf("]\n");    
  560.                         } else {
  561.                             printf(",, LSL #%d]\n", shiftim);
  562.                         }
  563.                 }
  564.             } else {
  565.                 if(shiftim != 0) {
  566.                     address = getRegValue(rn) - (getRegValue(rm) >> shiftim);
  567.                 } else {
  568.                     address = getRegValue(rn);
  569.                 }
  570.                 if(flag == 0) {
  571.                     store(memList.head, address, intToBinary(getRegValue(rd)));
  572.                 }
  573.                  if(trace == 1) {
  574.                         printf("[");
  575.                         printRegNum(rn);
  576.                         printf(", -");
  577.                         printRegNum(rm);
  578.                         if(shiftim == 0) {
  579.                             printf(", LSR #32]\n");    
  580.                         } else {
  581.                             printf(", LSR #%d]\n", shiftim);
  582.                         }
  583.                 }
  584.             }  
  585.         }
  586.     } else if(c[6] == '1' && c[7] == '1' && c[10] == '1') {//pre index register offset
  587.         if(c[8] == '1') {// rn + immediate
  588.             if(c[25] == '0' && c[26] == '0') {
  589.                 if(flag == 0) {
  590.                     regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
  591.                     store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
  592.                 }
  593.                 if(trace == 1) {
  594.                     printf("[");
  595.                     printRegNum(rn);
  596.                     printf(", ");
  597.                     printRegNum(rm);
  598.                     if(shiftim == 0) {
  599.                         printf("]!\n");    
  600.                     } else {
  601.                         printf(", LSL #%d]!\n", shiftim);
  602.                    
  603.                     }
  604.                 }
  605.             } else {
  606.                 if(shiftim != 0) {
  607.                     regs[rn] = getRegValue(rn) + (getRegValue(rm) >> shiftim);
  608.                 } else {
  609.                     regs[rn] = getRegValue(rn);
  610.                 }
  611.                 if(trace == 1) {
  612.                         printf("[");
  613.                         printRegNum(rn);
  614.                         printf(", ");
  615.                         printRegNum(rm);
  616.                         if(shiftim == 0) {
  617.                             printf(", LSR #32]!\n");    
  618.                         } else {
  619.                             printf(", LSR #%d]!\n", shiftim);
  620.                         }
  621.                 }
  622.                 if(flag == 0) {
  623.                     store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
  624.                 }
  625.             }  
  626.         } else {
  627.             if(c[25] == '0' && c[26] == '0') {
  628.                 if(flag == 0) {
  629.                     regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
  630.                     store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
  631.                 }
  632.                 if(trace == 1) {
  633.                         printf("[");
  634.                         printRegNum(rn);
  635.                         printf(", -");
  636.                         printRegNum(rm);
  637.                         if(shiftim == 0) {
  638.                             printf("]!\n");    
  639.                         } else {
  640.                             printf(", LSL #%d]!\n", shiftim);
  641.                         }
  642.                  }
  643.             } else {
  644.                 if(shiftim != 0) {
  645.                     regs[rn] = getRegValue(rn) - (getRegValue(rm) >> shiftim);
  646.                 } else {
  647.                     regs[rn] = getRegValue(rn);
  648.                 }
  649.                 if(flag == 0) {
  650.                     store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
  651.                 }
  652.                 if(trace == 1) {
  653.                         printf("[");
  654.                         printRegNum(rn);
  655.                         printf(", -");
  656.                         printRegNum(rm);
  657.                         if(shiftim == 0) {
  658.                             printf(", LSR #32]!\n");    
  659.                         } else {
  660.                             printf(", LSR #%d]!\n", shiftim);
  661.                         }
  662.                  }
  663.             }  
  664.         }
  665.     } else if(c[6] == '1' && c[7] == '0' && c[10] == '0') {// post register offset
  666.         if(flag == 0) {
  667.             store(memList.head, getRegValue(rn), intToBinary(getRegValue(rd)));
  668.         }
  669.         if(c[8] == '1') {// rn + immediate
  670.             if(c[25] == '0' && c[26] == '0') {
  671.                 if(flag == 0) {
  672.                     regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
  673.                 }
  674.                 if(trace == 1) {
  675.                         printf("[");
  676.                         printRegNum(rn);
  677.                         printf("], ");
  678.                         printRegNum(rm);
  679.                         if(shiftim != 0) {                        
  680.                             printf(", LSL #%d\n", shiftim);
  681.                         } else {
  682.                             printf("\n");
  683.                         }
  684.                  }
  685.             } else {
  686.                 if(shiftim != 0) {
  687.                     if(flag == 0) {
  688.                         regs[rn] = getRegValue(rn) + (regs[rm] >> shiftim);
  689.                     }
  690.                 } else {
  691.                     if(flag == 0) {
  692.                         regs[rn] = getRegValue(rn);
  693.                     }
  694.                 }
  695.                 if(trace == 1) {
  696.                    printf("[");
  697.                    printRegNum(rn);
  698.                    printf("], ");
  699.                    printRegNum(rm);
  700.                    if(shiftim != 0) {                        
  701.                        printf(", LSR #%d\n", shiftim);
  702.                    } else {
  703.                         printf(", LSR #32\n");
  704.                    }
  705.                 }    
  706.             }
  707.         } else {
  708.             if(c[25] == '0' && c[26] == '0') {
  709.                 if(flag == 0) {
  710.                     regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
  711.                 }
  712.                 if(trace == 1) {
  713.                         printf("[");
  714.                         printRegNum(rn);
  715.                         printf("], -");
  716.                         printRegNum(rm);
  717.                         if(shiftim != 0) {                        
  718.                             printf(", LSL #%d\n", shiftim);
  719.                         } else {
  720.                             printf("\n");
  721.                         }
  722.                  }
  723.             } else {
  724.                 if(shiftim != 0) {
  725.                     if(flag == 0) {
  726.                         regs[rn] = getRegValue(rn) - (regs[rm] >> shiftim);
  727.                     }
  728.                 } else {
  729.                     if(flag == 0) {
  730.                         regs[rn] = getRegValue(rn);
  731.                     }
  732.                 }
  733.                 if(trace == 1) {
  734.                     printf("[");
  735.                     printRegNum(rn);
  736.                     printf("], -");
  737.                     printRegNum(rm);
  738.                     if(shiftim != 0) {                        
  739.                         printf(", LSR %d\n", shiftim);
  740.                     } else {
  741.                         printf(", LSR #32\n");
  742.                     }
  743.                 }  
  744.             }  
  745.         }
  746.     }
  747. }
  748.  
  749. LDR(char *c, int flag) {
  750.     char *string, temp;
  751.     int32_t rn, rd, rm, immediate, address, shiftim;
  752.     string = substring(c, 20, 24);
  753.     shiftim = unsignedBinaryToInt(string);
  754.     string = substring(c, 16, 19);
  755.     rd = unsignedBinaryToInt(string);
  756.     string = substring(c, 12, 15);
  757.     rn = unsignedBinaryToInt(string);
  758.     string = substring(c, 20, 31);
  759.     immediate = unsignedBinaryToInt(string);
  760.     string = substring(c, 28, 31);
  761.     rm = unsignedBinaryToInt(string);
  762.     if(trace == 1) {
  763.         printRegNum(rd);
  764.     }
  765.     if(trace == 1) {
  766.         printf(", ");
  767.     }
  768.     if(c[6] == '0' && c[7] == '1' && c[10] == '0') {//Immediate offset
  769.         if(c[8] == '1') {
  770.             if(flag == 0) {
  771.                 address = getRegValue(rn) + immediate;
  772.                 string = fetch(memList.head, address);
  773.                 regs[rd] = signedBinaryToInt(string);
  774.             }
  775.             if(trace == 1) {
  776.                 printf("[");
  777.                 printRegNum(rn);
  778.                 if(immediate != 0) {
  779.                      printf(", #%d]\n", immediate);
  780.                 } else {
  781.                         printf("]\n");
  782.                 }
  783.             }
  784.         } else {
  785.             if(flag == 0) {
  786.                 address = getRegValue(rn)- immediate;
  787.                 string = fetch(memList.head, address);
  788.                 regs[rd] = signedBinaryToInt(string);
  789.             }
  790.             if(trace == 1) {
  791.                 printf("[");
  792.                 printRegNum(rn);
  793.                 if(immediate!= 0) {
  794.                      printf(", #-%d]\n", immediate);
  795.                 } else {
  796.                         printf("]\n");
  797.                 }
  798.             }
  799.         }
  800.        
  801.     } else if(c[6] == '0' && c[7] == '1' && c[10] == '1') {//immediate pre index
  802.         if(c[8] == '1') {// rn + immediate
  803.             if(flag == 0) {
  804.                 regs[rn] = getRegValue(rn) + immediate;
  805.                 string = fetch(memList.head, regs[rn]);
  806.                 regs[rd] = signedBinaryToInt(string);
  807.             }
  808.             if(trace == 1) {
  809.                 printf("[");
  810.                 printRegNum(rn);
  811.                 if(address != 0) {
  812.                      printf(", #%d]!\n", immediate);
  813.                 } else {
  814.                         printf("]!\n");
  815.                 }
  816.             }
  817.         } else {
  818.             if(flag == 0) {
  819.                 regs[rn] = getRegValue(rn) - immediate;
  820.                 string = fetch(memList.head, regs[rn]);
  821.                 regs[rd] = signedBinaryToInt(string);
  822.             }
  823.             if(trace == 1) {
  824.                 printf("[");
  825.                 printRegNum(rn);
  826.                 if(address != 0) {
  827.                      printf(", #-%d]!\n", immediate);
  828.                 } else {
  829.                         printf("]!\n");
  830.                 }
  831.             }
  832.         }
  833.     }  else if(c[6] == '0' && c[7] == '0' && c[10] == '0') {//immediate post index
  834.             if(c[8] == '1') {// rn + immediate
  835.                 if(flag == 0) {
  836.                     string = fetch(memList.head, getRegValue(rn));
  837.                     regs[rd] = signedBinaryToInt(string);
  838.                     regs[rn] = getRegValue(rn) + immediate;
  839.                 }
  840.                 if(trace == 1) {
  841.                     if(trace == 1) {
  842.                         printf("[");
  843.                         printRegNum(rn);
  844.                         printf("]");
  845.                         if(immediate != 0) {
  846.                             printf(", #%d\n", immediate);
  847.                         } else {
  848.                             printf("\n");
  849.                         }
  850.                     }
  851.                 }
  852.             } else {
  853.             if(flag == 0) {
  854.                 string = fetch(memList.head, getRegValue(rn));
  855.                 regs[rd] = signedBinaryToInt(string);
  856.                 regs[rn] = getRegValue(rn) - immediate;
  857.             }
  858.             if(trace == 1) {
  859.                 if(trace == 1) {
  860.                     printf("[");
  861.                     printRegNum(rn);
  862.                     printf("]\n");
  863.                     if(immediate != 0) {
  864.                         printf(", #-%d]\n", immediate);
  865.                     }
  866.                 }
  867.             }
  868.         }
  869.     } else if(c[6] == '1' && c[7] == '1' && c[10] == '0') {//register offset
  870.         if(c[8] == '1') {// rn + immediate
  871.             if(c[25] == '0' && c[26] == '0') {
  872.                 if(flag == 0) {
  873.                     address = getRegValue(rn) + (getRegValue(rm) << shiftim);
  874.                     string = fetch(memList.head, address);
  875.                     regs[rd] = signedBinaryToInt(string);
  876.                 }
  877.                 if(trace == 1) {
  878.                     printf("[");
  879.                     printRegNum(rn);
  880.                     printf(", ");
  881.                     printRegNum(rm);
  882.                     if(shiftim == 0) {
  883.                         printf("]\n");    
  884.                     } else {
  885.                         printf(", LSL #%d]\n", shiftim);
  886.                    
  887.                     }
  888.                 }
  889.             } else {
  890.                 if(shiftim != 0) {
  891.                     address = getRegValue(rn) + (getRegValue(rm) >> shiftim);
  892.                 } else {
  893.                     address = getRegValue(rn);
  894.                 }
  895.                 if(trace == 1) {
  896.                                 printf("[");
  897.                                 printRegNum(rn);
  898.                                 printf(", ");
  899.                                 printRegNum(rm);
  900.                                 if(shiftim == 0) {
  901.                                     printf(", LSR #32]\n");    
  902.                                 } else {
  903.                                     printf(", LSR #%d]\n", shiftim);
  904.                                 }
  905.                 }
  906.                 if(flag == 0) {
  907.                     string = fetch(memList.head, address);
  908.                     regs[rd] = signedBinaryToInt(string);
  909.                 }
  910.            }
  911.         } else {
  912.             if(c[25] == '0' && c[26] == '0') {
  913.                 if(flag == 0) {
  914.                     address = getRegValue(rn) - (getRegValue(rm) << shiftim);
  915.                     string = fetch(memList.head, address);
  916.                     regs[rd] = signedBinaryToInt(string);
  917.                 }
  918.                 if(trace == 1) {
  919.                         printf("[");
  920.                         printRegNum(rn);
  921.                         printf(", -");
  922.                         printRegNum(rm);
  923.                         if(shiftim == 0) {
  924.                             printf("]\n");    
  925.                         } else {
  926.                             printf(",, LSL #%d]\n", shiftim);
  927.                         }
  928.                 }
  929.             } else {
  930.                 if(shiftim != 0) {
  931.                     address = getRegValue(rn) - (getRegValue(rm) >> shiftim);
  932.                 } else {
  933.                     address = getRegValue(rn);
  934.                 }
  935.                 if(flag == 0) {
  936.                     string = fetch(memList.head, address);
  937.                     regs[rd] = signedBinaryToInt(string);
  938.                 }
  939.                  if(trace == 1) {
  940.                         printf("[");
  941.                         printRegNum(rn);
  942.                         printf(", -");
  943.                         printRegNum(rm);
  944.                         if(shiftim == 0) {
  945.                             printf(", LSR #32]\n");    
  946.                         } else {
  947.                             printf(", LSR #%d]\n", shiftim);
  948.                         }
  949.                 }
  950.             }  
  951.         }
  952.     } else if(c[6] == '1' && c[7] == '1' && c[10] == '1') {//pre index register offset
  953.         if(c[8] == '1') {// rn + immediate
  954.             if(c[25] == '0' && c[26] == '0') {
  955.                 if(flag == 0) {
  956.                     regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
  957.                     string = fetch(memList.head, regs[rn]);
  958.                     regs[rd] = signedBinaryToInt(string);
  959.                 }
  960.                 if(trace == 1) {
  961.                     printf("[");
  962.                     printRegNum(rn);
  963.                     printf(", ");
  964.                     printRegNum(rm);
  965.                     if(shiftim == 0) {
  966.                         printf("]!\n");    
  967.                     } else {
  968.                         printf(", LSL #%d]!\n", shiftim);
  969.                    
  970.                     }
  971.                 }
  972.             } else {
  973.                 if(shiftim != 0) {
  974.                     if(flag == 0) {
  975.                         regs[rn] = getRegValue(rn) + (getRegValue(rm) >> shiftim);
  976.                     }
  977.                 } else {
  978.                     if(flag == 0) {
  979.                         regs[rn] = getRegValue(rn);
  980.                     }
  981.                 }
  982.                 if(trace == 1) {
  983.                         printf("[");
  984.                         printRegNum(rn);
  985.                         printf(", ");
  986.                         printRegNum(rm);
  987.                         if(shiftim == 0) {
  988.                             printf(", LSR #32]!\n");    
  989.                         } else {
  990.                             printf(", LSR #%d]!\n", shiftim);
  991.                         }
  992.                 }
  993.                 if(flag == 0) {
  994.                     string = fetch(memList.head, regs[rn]);
  995.                     regs[rd] = signedBinaryToInt(string);
  996.                 }
  997.             }  
  998.         } else {
  999.             if(c[25] == '0' && c[26] == '0') {
  1000.                 if(flag == 0) {
  1001.                     regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
  1002.                     string = fetch(memList.head, regs[rn]);
  1003.                     regs[rd] = signedBinaryToInt(string);
  1004.                 }
  1005.                 if(trace == 1) {
  1006.                         printf("[");
  1007.                         printRegNum(rn);
  1008.                         printf(", -");
  1009.                         printRegNum(rm);
  1010.                         if(shiftim == 0) {
  1011.                             printf("]!\n");    
  1012.                         } else {
  1013.                             printf(", LSL #%d]!\n", shiftim);
  1014.                         }
  1015.                  }
  1016.             } else {
  1017.                 if(shiftim != 0) {
  1018.                     if(flag == 0) {
  1019.                         regs[rn] = getRegValue(rn) - (getRegValue(rm) >> shiftim);
  1020.                     }
  1021.                 } else {
  1022.                     if(flag == 0) {
  1023.                         regs[rn] = getRegValue(rn);
  1024.                     }
  1025.                 }
  1026.                 if(flag == 0) {
  1027.                     string = fetch(memList.head, regs[rn]);
  1028.                     regs[rd] = signedBinaryToInt(string);
  1029.                 }
  1030.                 if(trace == 1) {
  1031.                         printf("[");
  1032.                         printRegNum(rn);
  1033.                         printf(", -");
  1034.                         printRegNum(rm);
  1035.                         if(shiftim == 0) {
  1036.                             printf(", LSR #32]!\n");    
  1037.                         } else {
  1038.                             printf(", LSR #%d]!\n", shiftim);
  1039.                         }
  1040.                  }
  1041.             }  
  1042.         }
  1043.     } else if(c[6] == '1' && c[7] == '0' && c[10] == '0') {// post register offset
  1044.         if(flag == 0) {
  1045.             string = fetch(memList.head, getRegValue(getRegValue(rn)));
  1046.             regs[rd] = signedBinaryToInt(string);
  1047.         }
  1048.         if(c[8] == '1') {// rn + immediate
  1049.             if(c[25] == '0' && c[26] == '0') {
  1050.                 if(flag == 0) {
  1051.                     regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
  1052.                 }
  1053.                 if(trace == 1) {
  1054.                         printf("[");
  1055.                         printRegNum(rn);
  1056.                         printf("], ");
  1057.                         printRegNum(rm);
  1058.                         if(shiftim != 0) {                        
  1059.                             printf(", LSL #%d\n", shiftim);
  1060.                         } else {
  1061.                             printf("\n");
  1062.                         }
  1063.                  }
  1064.             } else {
  1065.                 if(shiftim != 0) {
  1066.                     if(flag == 0) {
  1067.                         regs[rn] = getRegValue(rn) + (regs[rm] >> shiftim);
  1068.                     }
  1069.                 } else {
  1070.                     if(flag == 0) {
  1071.                         regs[rn] = getRegValue(rn);
  1072.                     }
  1073.                 }
  1074.                 if(trace == 1) {
  1075.                    printf("[");
  1076.                    printRegNum(rn);
  1077.                    printf("], ");
  1078.                    printRegNum(rm);
  1079.                    if(shiftim != 0) {                        
  1080.                        printf(", LSR #%d\n", shiftim);
  1081.                    } else {
  1082.                         printf(", LSR #32\n");
  1083.                    }
  1084.                 }    
  1085.             }
  1086.         } else {
  1087.             if(c[25] == '0' && c[26] == '0') {
  1088.                 if(flag == 0) {
  1089.                     regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
  1090.                 }
  1091.                 if(trace == 1) {
  1092.                         printf("[");
  1093.                         printRegNum(rn);
  1094.                         printf("], -");
  1095.                         printRegNum(rm);
  1096.                         if(shiftim != 0) {                        
  1097.                             printf(", LSL #%d\n", shiftim);
  1098.                         } else {
  1099.                             printf("\n");
  1100.                         }
  1101.                  }
  1102.             } else {
  1103.                 if(shiftim != 0) {
  1104.                     if(flag == 0) {
  1105.                         regs[rn] = getRegValue(rn) - (regs[rm] >> shiftim);
  1106.                     }
  1107.                 } else {
  1108.                     if(flag == 0) {
  1109.                         regs[rn] = getRegValue(rn);
  1110.                     }
  1111.                 }
  1112.                 if(trace == 1) {
  1113.                     printf("[");
  1114.                     printRegNum(rn);
  1115.                     printf("], -");
  1116.                     printRegNum(rm);
  1117.                     if(shiftim != 0) {                        
  1118.                         printf(", LSR %d\n", shiftim);
  1119.                     } else {
  1120.                         printf(", LSR #32\n");
  1121.                     }
  1122.                 }  
  1123.             }  
  1124.         }
  1125.     }
  1126. }
  1127.  
  1128.  
  1129. MOV(char *c, int flag) {
  1130.     char *string = substring(c, 16, 19);//using inverse ordering
  1131.     int32_t rd = unsignedBinaryToInt(string);
  1132.     if(trace == 1) {
  1133.         printRegNum(rd);
  1134.         printf(",");
  1135.     }
  1136.     int32_t x = addressModeOne(c);
  1137.     if(flag == 0) {
  1138.         regs[rd] = x;
  1139.         if(c[11] == '1') {
  1140.             N = getBit(regs[rd], 31);
  1141.             if(regs[rd] == 0) {
  1142.                 Z = 1;
  1143.             } else {
  1144.                 Z = 0;
  1145.             }
  1146.         }
  1147.     }  
  1148. }
  1149.  
  1150. ADD(char *c, int flag) {
  1151.     char *temp = substring(c, 12, 15);
  1152.     int32_t rn = unsignedBinaryToInt(temp);
  1153.     temp = substring(c, 16, 19);
  1154.     int32_t rd = unsignedBinaryToInt(temp);
  1155.     if(trace == 1) {
  1156.         printRegNum(rd);
  1157.         printf(", ");
  1158.         printRegNum(rn);
  1159.         printf(",");
  1160.     }
  1161.     int32_t x = addressModeOne(c);
  1162.     if(flag == 0) {
  1163.         regs[rd] = getRegValue(rn) + x;    
  1164.         if(c[11] == '1') {
  1165.             N = getBit(regs[rd], 31);
  1166.             if(regs[rd] == 0) {
  1167.                 Z = 1;
  1168.             } else {
  1169.                 Z = 0;
  1170.             }
  1171.             if(getBit(getRegValue(rn), 31) == getBit(x, 31)) {
  1172.                 if((getBit(getRegValue(rn), 31) == 1) && (getBit(regs[rd], 31) != 1)) {
  1173.                     V = 1;
  1174.                 } else if ((getBit(getRegValue(rn), 31) == 0) && (getBit(regs[rd], 31) != 0)) {
  1175.                     V = 1;
  1176.                 } else {
  1177.                     V = 0;
  1178.                 }
  1179.             } else {
  1180.                 V = 0;
  1181.             }
  1182.         }  
  1183.     }
  1184. }
  1185.  
  1186. printRegNum(int reg) {
  1187. switch(reg) {
  1188.             case 0:
  1189.                 printf("R0"); break;
  1190.             case 1:
  1191.                 printf("R1"); break;
  1192.             case 2:
  1193.                 printf("R2"); break;
  1194.             case 3:
  1195.                 printf("R3"); break;
  1196.             case 4:
  1197.                 printf("R4"); break;
  1198.             case 5:
  1199.                 printf("R5"); break;
  1200.             case 6:
  1201.                 printf("R6"); break;
  1202.             case 7:
  1203.                 printf("R7"); break;
  1204.             case 8:
  1205.                 printf("R8"); break;
  1206.             case 9:
  1207.                 printf("R9"); break;
  1208.             case 10:
  1209.                  printf("R10"); break;
  1210.             case 11:
  1211.                  printf("R11"); break;
  1212.             case 12:
  1213.                  printf("R12"); break;
  1214.             case 13:
  1215.                  printf("SP"); break;
  1216.             case 14:
  1217.                  printf("LR"); break;
  1218.             case 15:
  1219.                  printf("PC"); break;
  1220.             default :
  1221.                 return printf("Not valid"); break;
  1222.             }    
  1223. }
  1224.  
  1225. SUB(char *c, int flag) {
  1226.     char *temp = substring(c, 12, 15);
  1227.     int32_t rn = unsignedBinaryToInt(temp);
  1228.     temp = substring(c, 16, 19);
  1229.     int32_t rd = unsignedBinaryToInt(temp);
  1230.     if(trace == 1) {
  1231.         printRegNum(rd);
  1232.         printf(", ");
  1233.         printRegNum(rn);
  1234.         printf(",");
  1235.     }
  1236.     int32_t x = addressModeOne(c);
  1237.     if(flag == 0) {
  1238.         regs[rd] = getRegValue(rn) - x;
  1239.  
  1240.         if(c[11] == '1') {
  1241.             N = getBit(regs[rd], 31);
  1242.             if(regs[rd] == 0) {
  1243.                 Z = 1;
  1244.             } else {
  1245.                 Z = 0;
  1246.             }
  1247.             if(getBit(getRegValue(rn), 31) != getBit(x, 31)) {
  1248.                 if((getBit(getRegValue(rn), 31)  != (getBit(regs[rd], 31)))) {
  1249.                     V = 1;
  1250.                 } else {
  1251.                     V = 0;
  1252.                 }
  1253.             } else {
  1254.                 V = 0;
  1255.             }
  1256.         }
  1257.     }
  1258. }
  1259.  
  1260. CMP(char *c, int flag) {
  1261.      char *temp = substring(c, 12, 15);
  1262.      int32_t rn = unsignedBinaryToInt(temp);
  1263.      if(trace == 1) {
  1264.          printRegNum(rn);
  1265.          printf(",");
  1266.      }
  1267.      int32_t x = addressModeOne(c);
  1268.      if(flag == 0) {
  1269.          int32_t rd = getRegValue(rn) - x;
  1270.          if(c[11] == '1') {
  1271.             N = getBit(rd, 31);
  1272.             if(rd == 0) {
  1273.                 Z = 1;
  1274.             } else {
  1275.                 Z = 0;
  1276.             }
  1277.             if(getBit(getRegValue(rn), 31) != getBit(x, 31)) {
  1278.                 if((getBit(getRegValue(rn), 31)  != (getBit(rd, 31)))) {
  1279.                     V = 1;
  1280.                 } else {
  1281.                     V = 0;
  1282.                 }
  1283.             } else {
  1284.                 V = 0;
  1285.             }
  1286.         }
  1287.     }
  1288. }
  1289.  
  1290. AND(char *c, int flag) {
  1291.     char *temp = substring(c, 12, 15);
  1292.     int32_t rn = unsignedBinaryToInt(temp);
  1293.     temp = substring(c, 16, 19);
  1294.     int32_t rd = unsignedBinaryToInt(temp);
  1295.     if(trace == 1) {
  1296.         printRegNum(rd);
  1297.         printf(", ");
  1298.         printRegNum(rn);
  1299.         printf(",");
  1300.     }
  1301.     int32_t x = addressModeOne(c);
  1302.     if(flag = 0) {
  1303.         regs[rd] = getRegValue(rn) & x;
  1304.         if(c[11] == '1') {
  1305.             N = getBit(regs[rd], 31);
  1306.             if(regs[rd] == 0) {
  1307.                 Z = 1;
  1308.             } else {
  1309.                 Z = 0;
  1310.             }
  1311.         }
  1312.     }    
  1313. }
  1314.  
  1315. EOR(char *c, int flag) {
  1316.     char *temp = substring(c, 12, 15);
  1317.     int32_t rn = unsignedBinaryToInt(temp);
  1318.     temp = substring(c, 16, 19);
  1319.     int32_t rd = unsignedBinaryToInt(temp);
  1320.     if(trace == 1) {
  1321.         printRegNum(rd);
  1322.         printf(", ");
  1323.         printRegNum(rn);
  1324.         printf(",");
  1325.     }
  1326.     int32_t x = addressModeOne(c);
  1327.     if(flag == 0) {
  1328.         regs[rd] = getRegValue(rn) ^ x;
  1329.         if(c[11] == '1') {
  1330.             N = getBit(regs[rd], 31);
  1331.             if(regs[rd] == 0) {
  1332.                 Z = 1;
  1333.             } else {
  1334.                 Z = 0;
  1335.             }
  1336.         }
  1337.     }
  1338. }
  1339.  
  1340. ORR(char *c, int flag) {
  1341.     char *temp = substring(c, 12, 15);
  1342.     int32_t rn = unsignedBinaryToInt(temp);
  1343.     temp = substring(c, 16, 19);
  1344.     int32_t rd = unsignedBinaryToInt(temp);
  1345.     if(trace == 1) {
  1346.         printRegNum(rd);
  1347.         printf(", ");
  1348.         printRegNum(rn);
  1349.         printf(",");
  1350.     }
  1351.     int32_t x = addressModeOne(c);
  1352.     if(flag == 0) {
  1353.         regs[rd] = getRegValue(rn) | x;
  1354.         if(c[11] == '1') {
  1355.             N = getBit(regs[rd], 31);
  1356.             if(regs[rd] == 0) {
  1357.                 Z = 1;
  1358.             } else {
  1359.                 Z = 0;
  1360.             }
  1361.         }
  1362.     }
  1363. }
  1364.  
  1365. BIC(char *c, int flag) {
  1366.     char *temp = substring(c, 12, 15);
  1367.     int32_t rn = unsignedBinaryToInt(temp);
  1368.     temp = substring(c, 16, 19);
  1369.     int32_t rd = unsignedBinaryToInt(temp);
  1370.     if(trace == 1) {
  1371.         printRegNum(rd);
  1372.         printf(", ");
  1373.         printRegNum(rn);
  1374.         printf(",");
  1375.     }
  1376.     int32_t x = addressModeOne(c);
  1377.     if(flag == 0) {
  1378.         regs[rd] = getRegValue(rn) & (~x);
  1379.         if(c[11] == '1') {
  1380.             N = getBit(regs[rd], 31);
  1381.             if(regs[rd] == 0) {
  1382.                 Z = 1;
  1383.             } else {
  1384.                 Z = 0;
  1385.             }
  1386.         }
  1387.     }  
  1388. }
  1389.  
  1390. MUL(char *c, int flag) {
  1391.     char *temp = substring(c, 12, 15);
  1392.     int32_t rd = unsignedBinaryToInt(temp);
  1393.     temp = substring(c, 20, 23);
  1394.     int32_t rs = unsignedBinaryToInt(temp);
  1395.     temp = substring(c, 28, 31);
  1396.     int32_t rm = unsignedBinaryToInt(temp);
  1397.     if(trace == 1) {
  1398.         printRegNum(rd);
  1399.         printf(", ");
  1400.         printRegNum(rm);
  1401.         printf(", ");
  1402.         printRegNum(rs);
  1403.         printf("\n");
  1404.     }
  1405.     if(flag == 0) {
  1406.         regs[rd] = (int32_t) regs[rm] * regs[rs];
  1407.         if(c[11] == '1') {
  1408.             N = getBit(regs[rd], 31);
  1409.             if(regs[rd] == 0) {
  1410.                 Z = 1;
  1411.             } else {
  1412.                 Z = 0;
  1413.             }
  1414.         }
  1415.     }  
  1416. }
  1417.  
  1418. MLA(char *c, int flag) {
  1419.     char *temp = substring(c, 12, 15);
  1420.     int32_t rd = unsignedBinaryToInt(temp);
  1421.     temp = substring(c, 20, 23);
  1422.     int32_t rs = unsignedBinaryToInt(temp);
  1423.     temp = substring(c, 28, 31);
  1424.     int32_t rm = unsignedBinaryToInt(temp);
  1425.     temp = substring(c, 16, 19);
  1426.     int32_t rn = unsignedBinaryToInt(temp);
  1427.     if(trace == 1) {
  1428.         printRegNum(rd);
  1429.         printf(", ");
  1430.         printRegNum(rm);
  1431.         printf(", ");
  1432.         printRegNum(rs);
  1433.         printf(", ");
  1434.         printRegNum(rn);
  1435.         printf("\n");
  1436.     }
  1437.     if(flag == 0) {
  1438.         regs[rd] = (int32_t) (regs[rm] * regs[rs]) + regs[rn];
  1439.         if(c[11] == '1') {
  1440.             N = getBit(regs[rd], 31);
  1441.             if(regs[rd] == 0) {
  1442.                 Z = 1;
  1443.             } else {
  1444.                 Z = 0;
  1445.             }
  1446.         }
  1447.     }    
  1448. }
  1449.  
  1450. B(char *c, int flag) {
  1451.     char *temp = substring(c, 8, 31);
  1452.     int32_t address = signedBinaryToInt(temp);
  1453.     address = address << 2;
  1454.     int32_t location = regs[15] + address + 4;
  1455.     if(flag == 0) {
  1456.         regs[15] = location;
  1457.     }
  1458.     if(trace == 1) {
  1459.         printf("%d\n", location);
  1460.     }
  1461. }
  1462.  
  1463. BL(char *c, int flag) {
  1464.     char *temp = substring(c, 8, 31);
  1465.     int32_t address = signedBinaryToInt(temp);
  1466.     address = address << 2;
  1467.     int32_t location = regs[15] + address + 4;
  1468.     if(flag == 0) {
  1469.         regs[14] = regs[15];
  1470.         regs[15] = location;
  1471.     }
  1472.     if(trace == 1) {
  1473.         printf("%d\n", location);
  1474.     }        
  1475. }
  1476.  
  1477. int condition(char *c, char *s) {
  1478.         if(c[0] == '1' && c[1] == '1' && c[2] == '1' && c[3] == '0') {
  1479.             s=strcpy(s,"");
  1480.             return 0;
  1481.         } else if(c[0] == '0' && c[1] == '0' && c[2] == '0' && c[3] == '0') {
  1482.             s=strcpy(s,"EQ");
  1483.             if(Z == 1) {
  1484.                 return 0;
  1485.             } else {
  1486.                 return 1;
  1487.             }
  1488.         } else if(c[0] == '0' && c[1] == '0' && c[2] == '0' && c[3] == '1') {
  1489.             s=strcpy(s,"NE");
  1490.             if(Z == 1) {
  1491.                 return 1;
  1492.             } else {
  1493.                 return 0;
  1494.             }
  1495.         } else if (c[0] == '0' && c[1] == '1' && c[2] == '0' && c[3] == '1') {
  1496.             s=strcpy(s,"PL");
  1497.             if(N == 1) {
  1498.                 return 0;
  1499.             } else {
  1500.                 return 1;
  1501.             }
  1502.         } else if (c[0] == '0' && c[1] == '1' && c[2] == '0' && c[3] == '0') {
  1503.             s=strcpy(s,"MI");
  1504.             if(N == 1) {
  1505.                 return 1;
  1506.             } else {
  1507.                 return 0;
  1508.             }
  1509.         } else if (c[0] == '1' && c[1] == '0' && c[2] == '1' && c[3] == '0') {
  1510.             s=strcpy(s,"GE");
  1511.             if(N == V) {
  1512.                 return 0;
  1513.             } else {
  1514.                 return 1;
  1515.             }
  1516.         } else if (c[0] == '1' && c[1] == '0' && c[2] == '1' && c[3] == '1') {
  1517.             s=strcpy(s,"LT");
  1518.             if(N != V) {
  1519.                 return 0;
  1520.             } else {
  1521.                 return 1;
  1522.             }
  1523.         } else if (c[0] == '1' && c[1] == '1' && c[2] == '0' && c[3] == '0') {
  1524.             s=strcpy(s,"GT");
  1525.             if((Z == 0) && (N == V)) {
  1526.                 return 0;
  1527.             } else {
  1528.                 return 1;
  1529.             }
  1530.         } else if (c[0] == '1' && c[1] == '1' && c[2] == '0' && c[3] == '1') {
  1531.             s=strcpy(s,"LE");
  1532.             if((Z == 1) || (N != V)) {
  1533.                 return 0;
  1534.             } else {
  1535.                 return 1;
  1536.             }
  1537.         }
  1538. }
  1539.  
  1540. SWI(char *c, int flag) {
  1541.     char *string = substring(c, 20, 31);
  1542.     int32_t value = unsignedBinaryToInt(string);
  1543.     if(trace == 1) {
  1544.         printf("%d\n", value);
  1545.     }
  1546.     if(flag == 0) {
  1547.         if(value == 0) {
  1548.             execute = 1;
  1549.         } else if(value == 1) {
  1550.             tracep();
  1551.         } else if(value == 2) {
  1552.                 printf("R0=%08X\n", regs[0]);  
  1553.         }  
  1554.     }
  1555. }
  1556. decode (char *c) {
  1557.     char s[3];
  1558.     if(trace == 1) {
  1559.         printf("Next Instruction=");
  1560.     }
  1561.     int flag = condition(c, s);
  1562.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '1' && c[31-23] == '1' && c[31-22] == '0' && c[31-21] == '1' && c[31-19] == '0' && c[31-18] == '0' && c[31-17] == '0' && c[31-16] == '0') {
  1563.         if(trace == 1) {
  1564.             printf("MOV%s ",s);
  1565.         }
  1566.         MOV(c, flag);
  1567.     }
  1568.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '0' && c[31-23] == '1' && c[31-22] == '0' && c[31-21] == '0') {
  1569.         if(trace == 1) {
  1570.             printf("ADD%s ",s);
  1571.         }
  1572.         ADD(c, flag);  
  1573.     }
  1574.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '0' && c[31-23] == '0' && c[31-22] == '1' && c[31-21] == '0') {
  1575.         if(trace == 1) {
  1576.             printf("SUB%s ",s);
  1577.         }
  1578.         SUB(c, flag);  
  1579.     }
  1580.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '1' && c[31-23] == '0' && c[31-22] == '1' && c[31-21] == '0' && c[31-20] == '1' && c[31-15] == '0' && c[31-14] == '0' && c[31-13] == '0' && c[31-12] == '0') {
  1581.         if(trace == 1) {
  1582.             printf("CMP%s ",s);
  1583.         }
  1584.         CMP(c, flag);  
  1585.     }
  1586.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '0' && c[31-23] == '0' && c[31-22] == '0' && c[31-21] == '0' && c[31-7] == '0') {
  1587.         if(trace == 1) {
  1588.             printf("AND%s ",s);
  1589.         }
  1590.         AND(c, flag);  
  1591.     }
  1592.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '0' && c[31-23] == '0' && c[31-22] == '0' && c[31-21] == '1' && c[31-7] == '0') {
  1593.         if(trace == 1) {
  1594.             printf("EOR%s ",s);
  1595.         }
  1596.         EOR(c, flag);  
  1597.     }
  1598.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '1' && c[31-23] == '1' && c[31-22] == '0' && c[31-21] == '0') {
  1599.         if(trace == 1) {
  1600.             printf("ORR%s ",s);
  1601.         }
  1602.         ORR(c, 0);  
  1603.     }
  1604.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-24] == '1' && c[31-23] == '1' && c[31-22] == '1' && c[31-21] == '0') {
  1605.         if(trace == 1) {
  1606.             printf("BIC%s ",s);
  1607.         }
  1608.         BIC(c, flag);  
  1609.     }
  1610.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-25] == '0' && c[31-24] == '0' && c[31-23] == '0' && c[31-22] == '0' && c[31-21] == '0' && c[31-15] == '0' && c[31-14] == '0' && c[31-13] == '0' && c[31-12] == '0' && c[31-7] == '1' && c[31-6] == '0' && c[31-5] == '0' && c[31-4] == '1') {
  1611.         if(trace == 1) {
  1612.             printf("MUL%s ",s);
  1613.         }
  1614.         MUL(c, flag);  
  1615.     }
  1616.     if(c[31-27] == '0' && c[31-26] == '0' && c[31-25] == '0' && c[31-24] == '0' && c[31-23] == '0' && c[31-22] == '0' && c[31-21] == '1' && c[31-7] == '1' && c[31-6] == '0' && c[31-5] == '0' && c[31-4] == '1') {
  1617.         if(trace == 1) {
  1618.             printf("MLA%s ",s);
  1619.         }
  1620.         MLA(c, flag);    
  1621.     }
  1622.     if(c[31-27] == '0' && c[31-26] == '1' && c[31-22] == '0' && c[31-20] == '1') {
  1623.         if(trace == 1) {
  1624.             printf("LDR%s ",s);
  1625.         }
  1626.         LDR(c, flag);
  1627.     }
  1628.     if(c[31-27] == '0' && c[31-26] == '1' && c[31-22] == '0' && c[31-20] == '0') {
  1629.         if(trace == 1) {
  1630.             printf("STR%s ",s);
  1631.            
  1632.         }
  1633.         STR(c, flag);
  1634.     }
  1635.     if(c[31-27] == '1' && c[31-26] == '0' && c[31-25] == '1' && c[31-24] == '0') {
  1636.         if(trace == 1) {
  1637.             printf("B%s ",s);
  1638.         }
  1639.         B(c, flag);
  1640.     }
  1641.     if(c[31-27] == '1' && c[31-26] == '0' && c[31-25] == '1' && c[31-24] == '1') {
  1642.         if(trace == 1) {
  1643.             printf("BL%s ",s);
  1644.         }
  1645.         BL(c, flag);
  1646.     }
  1647.     if(c[31-27] == '1' && c[31-26] == '1' && c[31-25] == '1' && c[31-24] == '1') {
  1648.         if(trace == 1) {
  1649.             printf("SWI%s ",s);
  1650.         }
  1651.         SWI(c, flag);
  1652.     }
  1653. }
  1654.  
  1655. testAddressmodeOne() {
  1656.     printf("rotate: %u\n", addressModeOne("00000011101100001000000100001110"));
  1657.     printf("reg shift: %u\n", addressModeOne("00000001101100001000000100101000"));
  1658.     printf("reg reg shift: %u\n", addressModeOne("00000001101100001000001000011110"));//14 left shifted twice. Reg Reg
  1659. }
  1660.  
  1661. executeLoop(FullList memList) {
  1662.     while(execute == 0) {
  1663.         if(trace == 1) {
  1664.             tracep();
  1665.         }
  1666.         char *instruction = fetched(memList.head, regs[15]);
  1667.         regs[15] = regs[15] + 4;
  1668.         if(instruction != "0") {
  1669.             decode(instruction);
  1670.         }
  1671.        
  1672.     }
  1673. }
  1674.  
  1675. dumpMem(list *mem) {
  1676.     while(mem != NULL) {
  1677.         int32_t ad = mem->address;
  1678.         int32_t ins = signedBinaryToInt(mem->instruction);
  1679.         printf("0x%08X 0x%08X\n", ad, ins);
  1680.         mem = mem->next;    
  1681.     }
  1682.     printf("\n");
  1683. }
  1684.  
  1685. int main(int argc, char *argv[]) {
  1686.     memList.head = NULL;
  1687.     int i, before, after;
  1688.     before = after = 0;
  1689.     for(i = 0; i<=15; i++) {
  1690.         regs[i] = 0;
  1691.     }
  1692.     FILE *ifp;
  1693.     char *test1 = intToBinary(9);
  1694.     int y = unsignedBinaryToInt("110001");
  1695.     unsigned int x;
  1696.     char test[100];
  1697.     if (argc < 2) {
  1698.         fprintf(stderr, "Error: Expected an [option(s)] and a .emu file\n");
  1699.         return 1;
  1700.     } else {
  1701.         if(isEmu(argv[argc-1]) == 1) {
  1702.             fprintf(stderr, "Error: Expected an [option(s)] and a .emu file\n");
  1703.             return 1;  
  1704.         }
  1705.         ifp = fopen (argv[argc-1], "r");
  1706.         if (ifp == NULL) {  //checks whether a valid text file has been entered.
  1707.             fprintf (stderr, "Please enter a Valid .emu File\n");
  1708.             return 1;
  1709.         }  
  1710.        
  1711.         for(i = 1; i<(argc-1); i++) {
  1712.             if(strcmp(argv[i], "-trace") == 0){
  1713.                 trace = 1;
  1714.             } else if(strcmp(argv[i], "-before") == 0) {
  1715.                 before = 1;
  1716.             } else if(strcmp(argv[i], "-after") == 0) {
  1717.                 after = 1;
  1718.             } else {
  1719.                 fprintf(stderr, "Valid options are \"-trace\", \"-before\", \"after.\"\n");
  1720.                 return 1;
  1721.             }
  1722.         }
  1723.         Current current;
  1724.         while ((fscanf (ifp, "%9s %9s", current.address, current.instruction) != EOF)) {
  1725.             if(strlen(current.address) != 8 || strlen(current.instruction) != 8) {
  1726.                 fprintf(stderr, "Adresses and instructions must both be 8 bits long\n");
  1727.                 return 1;
  1728.             }
  1729.             x = strtol(current.address,NULL,16);
  1730.             memList = addFullList(memList, convertToBinary(current.instruction), x);
  1731.         }
  1732.         if(before == 1) {
  1733.             printf("\n");
  1734.             dumpMem(memList.head);
  1735.         }
  1736.         executeLoop(memList);
  1737.         if(after == 1) {
  1738.             printf("\n");
  1739.             dumpMem(memList.head);
  1740.         }
  1741.     }
  1742. }
Advertisement
Add Comment
Please, Sign In to add comment