Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Copyright Joshua Hargreaves. Any questions [email protected] or [email protected]
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "ctype.h"
- #include "stdint.h"
- int32_t regs[16];
- int N, Z, C, V;
- int execute = 0;
- int trace = 0;
- typedef struct {
- int32_t r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,
- r12,r13,r14,r15,r16,r17,r18;
- } registers;
- typedef struct {
- char address[33];
- char instruction[33];
- } Current;
- typedef struct List {
- char instruction[33];
- int32_t address;
- struct List *next;
- } list;
- typedef struct {
- list *head;
- list *tail;
- }FullList;
- FullList memList;
- int getRegValue(int reg) {
- if(reg == 15) {
- return (regs[reg] + 4);
- } else {
- return (regs[reg]);
- }
- }
- list *addToList(list *List, char *c, int32_t x) {
- list *l = malloc(sizeof(list));
- l->next = calloc(1, sizeof(list));
- l->next = List;
- l->address = x;
- strcpy(l->instruction, c);
- return l;
- }
- FullList addFullList ( FullList c, char *cha, int32_t x ) {
- if (c.head == NULL){
- c.head= addToList(c.head, cha, x);
- c.tail = c.head;
- }
- else {
- c.tail->next = addToList(NULL, cha, x);
- c.tail = c.tail->next;
- }
- return c;
- }
- int32_t searchList(list *List, int32_t x) {
- while(List != NULL) {
- if(List->address == x) {
- return 0;
- } else {
- List = List->next;
- }
- }
- return 1;
- }
- store(list *List, int32_t x, char *c) {
- int found = 0;
- while(List != NULL) {
- if(List->address == x) {
- found = 1;
- strcpy(List->instruction, c);
- List = List->next;
- } else {
- List = List->next;
- }
- }
- if(found == 0) {
- addFullList(memList, c, x);
- }
- }
- char *fetched(list *List, int32_t x) {
- while(List != NULL) {
- if(List->address == x) {
- return strdup(List->instruction);
- } else {
- List = List->next;
- }
- }
- execute = 1;
- return "0";
- }
- char *fetch(list *List, int32_t x) {
- while(List != NULL) {
- if(List->address == x) {
- return strdup(List->instruction);
- } else {
- List = List->next;
- }
- }
- return "0";
- }
- //Checks whether a file has a .emu extension
- int32_t isEmu(char *c) {
- int32_t length = strlen(c);
- if(c[length-1] != 'u' || c[length-2] != 'm' || c[length-3] != 'e' ||
- c[length-4] != '.') {
- return 1;
- } else {
- return 0;
- }
- }
- tracep() {
- printf("R0=%08X ", regs[0]);
- printf("R1=%08X ", regs[1]);
- printf("R2=%08X ", regs[2]);
- printf("R3=%08X ", regs[3]);
- printf("R4=%08X ", regs[4]);
- printf("R5=%08X ", regs[5]);
- printf("R6=%08X ", regs[6]);
- printf("R7=%08X ", regs[7]);
- printf("R8=%08X ", regs[8]);
- printf("R9=%08X ", regs[9]);
- printf("R10=%08X ", regs[10]);
- printf("R12=%08X ", regs[12]);
- printf("SP=%08X ", regs[13]);
- printf("LR=%08X ", regs[14]);
- printf("PC=%08X\n",regs[15]);
- printf("Flags: N = %d, Z = %d, C = %d, V = %d\n", N,Z,C,V);
- }
- //converts to binary
- char* convertToBinary(char *c) {
- char binary[32] = "";
- char temp[32];
- int i = 0;
- strcpy(binary,"");
- strcpy(temp,"");
- for(i = (strlen(c) -1); i >= 0; i--) {
- strcpy(temp,binary);
- switch(c[i]) {
- case '0':
- sprintf(binary, "0000%s", temp); break;
- case '1':
- sprintf(binary, "0001%s", temp); break;
- case '2':
- sprintf(binary, "0010%s", temp); break;
- case '3':
- sprintf(binary, "0011%s", temp); break;
- case '4':
- sprintf(binary, "0100%s", temp); break;
- case '5':
- sprintf(binary, "0101%s", temp); break;
- case '6':
- sprintf(binary, "0110%s", temp); break;
- case '7':
- sprintf(binary, "0111%s", temp); break;
- case '8':
- sprintf(binary, "1000%s", temp); break;
- case '9':
- sprintf(binary, "1001%s", temp); break;
- case 'A':
- sprintf(binary, "1010%s", temp); break;
- case 'B':
- sprintf(binary, "1011%s", temp); break;
- case 'C':
- sprintf(binary, "1100%s", temp); break;
- case 'D':
- sprintf(binary, "1101%s", temp); break;
- case 'E':
- sprintf(binary, "1110%s", temp); break;
- case 'F':
- sprintf(binary, "1111%s", temp); break;
- default :
- return "error"; break;
- }
- }
- return strdup(binary);
- }
- char *intToBinary(int32_t x) {
- int32_t y = 1;
- int total = 2;
- int i = 0;
- char string[33];
- char temp[33];
- int32_t test = 0;
- strcpy(string,"");
- strcpy(temp, "");
- for(i = 0; i<32; i++) {
- if(getBit(x, i) == 0) {
- strcpy(temp, string);
- sprintf(string, "0%s", temp);
- } else {
- strcpy(temp, string);
- sprintf(string, "1%s", temp);
- }
- }
- return strdup(string);
- }
- int32_t signedBinaryToInt(char *c) {
- int32_t length = strlen(c);
- int32_t i;
- int32_t total = 1;
- int32_t runningTotal = 0;
- for(i = length -1; i>=0; i--) {
- if(i != 0) {
- if(c[i] == '1') {
- runningTotal = runningTotal + total;
- }
- total = total * 2;
- } else {
- if(c[i] == '1') {
- runningTotal = -total + runningTotal;
- }
- }
- }
- //printf("%d\n", runningTotal);
- return runningTotal;
- }
- int32_t unsignedBinaryToInt(char *c) {
- if(c != NULL) {
- int32_t length = strlen(c);
- int32_t i;
- int32_t total = 1;
- int32_t runningTotal = 0;
- for(i = length -1; i>=0; i--) {
- if(c[i] == '1') {
- runningTotal = runningTotal + total;
- }
- total = total * 2;
- }
- //printf("%d\n", runningTotal);
- return runningTotal;
- } else {
- return 0;
- }
- }
- char *rightRotate(char * c, int32_t k) {
- char *temp = strdup(c);
- int32_t i,j;
- for(j = 0; j<k; j++) {
- temp[0] = c[31];
- for(i = 30; i >= 0; i--) {
- temp[i+1] = c[i];
- }
- //printf("12345678912345678912345678912345 rotated is: %s\n", temp);
- //12345678912345678912345678912345
- c = strdup(temp);
- }
- return strdup(c);
- }
- char *substring(char *c, int32_t x, int32_t y) {
- char string[(y-x)+3];
- //printf("the array size is %d\n", (y-x)+3);
- int32_t i = 0;
- int32_t j = 0;
- for(i = x; i<=y; i++) {
- string[j] = c[i];
- j++;
- }
- string[j] = '\0';
- // printf("termination character is: %d\n", (j+1));
- return strdup(string);
- }
- uint32_t RotateRight32Bit(uint32_t Data, uint32_t Bits) {
- return ((Data >> Bits) | (Data << (32-Bits)));
- }
- uint32_t RotateLeft32Bit(uint32_t Data, uint32_t Bits) {
- return ((Data << Bits) | (Data >> (32-Bits)));
- }
- int32_t addressModeOne(char *c) {
- int32_t x = unsignedBinaryToInt("001");
- int32_t k, y, z, a;
- int32_t temp;
- char *string;
- char *amount;
- if(c[6] == '1') {
- string = substring(c, 24, 31);
- x = unsignedBinaryToInt(string);
- amount = substring(c, 20, 23);
- k = unsignedBinaryToInt(amount);
- if(trace == 1) {
- printf(" #%d", x);
- if(k != 0) {
- printf(", %d\n", k*2);
- } else {
- printf("\n");
- }
- }
- y = RotateRight32Bit(x, 2*k);
- } else if (c[25] == '0' && c[27] == '0'){
- string = substring(c, 28, 31);
- x = unsignedBinaryToInt(string);
- temp = regs[x];
- if(x == 15) {
- temp = regs[x] + 4;
- }
- k = unsignedBinaryToInt(substring(c, 20, 24));
- if(trace ==1) {
- printf(" ");
- printRegNum(x);
- }
- if(c[26] == '0'){
- y = temp << k;
- if(trace == 1) {
- if(k != 0) {
- printf(", LSL #%d\n", k);
- } else {
- printf("\n");
- }
- }
- } else {
- if(k == 0) {
- y = 0;
- if(trace == 1) {
- printf(", LSR #32\n");
- }
- } else {
- y = temp >> k;
- }
- if(k != 0) {
- if(trace == 1) {
- printf(", LSR #%d\n", k);
- }
- } else {
- if(trace == 1) {
- printf("\n");
- }
- }
- }
- } else {
- string = substring(c, 28, 31);
- x = unsignedBinaryToInt(string);
- temp = regs[x];
- if(x == 15) {
- temp = regs[x] + 4;
- }
- //printf("value in reg to be shifted %d\n", regs[x]);
- k = unsignedBinaryToInt(substring(c, 20, 23));
- //printf("the values in the shift register is %d\n", regs[k]);
- if(trace == 1) {
- printf(" ");
- printRegNum(x);
- }
- if(c[26] == '0'){
- if(trace == 1) {
- if(k != 0) {
- printf(", LSL ");
- printRegNum(k);
- }
- }
- y = temp << regs[k];
- } else if(k != 0) {
- if(trace == 1) {
- printf(", LSL ");
- printRegNum(k);
- }
- }
- if(trace == 1) {
- printf("\n");
- }
- }
- return y;
- }
- int getBit(int x, int k) {
- return ((x >> k) & 1);
- }
- STR(char *c, int flag) {
- char *string, temp;
- int32_t rn, rd, rm, immediate, address, shiftim;
- string = substring(c, 20, 24);
- shiftim = unsignedBinaryToInt(string);
- string = substring(c, 16, 19);
- rd = unsignedBinaryToInt(string);
- string = substring(c, 12, 15);
- rn = unsignedBinaryToInt(string);
- string = substring(c, 20, 31);
- immediate = unsignedBinaryToInt(string);
- string = substring(c, 28, 31);
- rm = unsignedBinaryToInt(string);
- if(trace == 1) {
- printRegNum(rd);
- }
- if(trace == 1) {
- printf(", ");
- }
- if(c[6] == '0' && c[7] == '1' && c[10] == '0') {//Immediate offset
- if(c[8] == '1') {
- if(flag == 0) {
- address = getRegValue(rn) + immediate;
- store(memList.head, address, intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(immediate != 0) {
- printf(", #%d]\n", immediate);
- } else {
- printf("]\n");
- }
- }
- } else {
- if(flag == 0) {
- address = getRegValue(rn)- immediate;
- store(memList.head, address, intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(immediate!= 0) {
- printf(", #-%d]\n", immediate);
- } else {
- printf("]\n");
- }
- }
- }
- } else if(c[6] == '0' && c[7] == '1' && c[10] == '1') {//immediate pre index
- if(c[8] == '1') {// rn + immediate
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + immediate;
- store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(address != 0) {
- printf(", #%d]!\n", immediate);
- } else {
- printf("]!\n");
- }
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - immediate;
- store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(address != 0) {
- printf(", #-%d]!\n", immediate);
- } else {
- printf("]!\n");
- }
- }
- }
- } else if(c[6] == '0' && c[7] == '0' && c[10] == '0') {//immediate post index
- if(c[8] == '1') {// rn + immediate
- if(flag == 0) {
- store(memList.head, getRegValue(rn), intToBinary(getRegValue(rd)));
- regs[rn] = getRegValue(rn) + immediate;
- }
- if(trace == 1) {
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("]");
- if(immediate != 0) {
- printf(", #%d\n", immediate);
- } else {
- printf("\n");
- }
- }
- }
- } else {
- if(flag == 0) {
- store(memList.head, getRegValue(rn), intToBinary(getRegValue(rd)));
- regs[rn] = getRegValue(rn) - immediate;
- }
- if(trace == 1) {
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("]\n");
- if(immediate != 0) {
- printf(", #-%d]\n", immediate);
- }
- }
- }
- }
- } else if(c[6] == '1' && c[7] == '1' && c[10] == '0') {//register offset
- if(c[8] == '1') {// rn + immediate
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- address = getRegValue(rn) + (getRegValue(rm) << shiftim);
- store(memList.head, address, intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]\n");
- } else {
- printf(", LSL #%d]\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- address = getRegValue(rn) + (getRegValue(rm) >> shiftim);
- } else {
- address = getRegValue(rn);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]\n");
- } else {
- printf(", LSR #%d]\n", shiftim);
- }
- }
- if(flag == 0) {
- }
- }
- } else {
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- address = getRegValue(rn) - (getRegValue(rm) << shiftim);
- store(memList.head, address, intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]\n");
- } else {
- printf(",, LSL #%d]\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- address = getRegValue(rn) - (getRegValue(rm) >> shiftim);
- } else {
- address = getRegValue(rn);
- }
- if(flag == 0) {
- store(memList.head, address, intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]\n");
- } else {
- printf(", LSR #%d]\n", shiftim);
- }
- }
- }
- }
- } else if(c[6] == '1' && c[7] == '1' && c[10] == '1') {//pre index register offset
- if(c[8] == '1') {// rn + immediate
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
- store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]!\n");
- } else {
- printf(", LSL #%d]!\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- regs[rn] = getRegValue(rn) + (getRegValue(rm) >> shiftim);
- } else {
- regs[rn] = getRegValue(rn);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]!\n");
- } else {
- printf(", LSR #%d]!\n", shiftim);
- }
- }
- if(flag == 0) {
- store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
- }
- }
- } else {
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
- store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]!\n");
- } else {
- printf(", LSL #%d]!\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- regs[rn] = getRegValue(rn) - (getRegValue(rm) >> shiftim);
- } else {
- regs[rn] = getRegValue(rn);
- }
- if(flag == 0) {
- store(memList.head, regs[rn], intToBinary(getRegValue(rd)));
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]!\n");
- } else {
- printf(", LSR #%d]!\n", shiftim);
- }
- }
- }
- }
- } else if(c[6] == '1' && c[7] == '0' && c[10] == '0') {// post register offset
- if(flag == 0) {
- store(memList.head, getRegValue(rn), intToBinary(getRegValue(rd)));
- }
- if(c[8] == '1') {// rn + immediate
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], ");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSL #%d\n", shiftim);
- } else {
- printf("\n");
- }
- }
- } else {
- if(shiftim != 0) {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + (regs[rm] >> shiftim);
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn);
- }
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], ");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSR #%d\n", shiftim);
- } else {
- printf(", LSR #32\n");
- }
- }
- }
- } else {
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], -");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSL #%d\n", shiftim);
- } else {
- printf("\n");
- }
- }
- } else {
- if(shiftim != 0) {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - (regs[rm] >> shiftim);
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn);
- }
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], -");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSR %d\n", shiftim);
- } else {
- printf(", LSR #32\n");
- }
- }
- }
- }
- }
- }
- LDR(char *c, int flag) {
- char *string, temp;
- int32_t rn, rd, rm, immediate, address, shiftim;
- string = substring(c, 20, 24);
- shiftim = unsignedBinaryToInt(string);
- string = substring(c, 16, 19);
- rd = unsignedBinaryToInt(string);
- string = substring(c, 12, 15);
- rn = unsignedBinaryToInt(string);
- string = substring(c, 20, 31);
- immediate = unsignedBinaryToInt(string);
- string = substring(c, 28, 31);
- rm = unsignedBinaryToInt(string);
- if(trace == 1) {
- printRegNum(rd);
- }
- if(trace == 1) {
- printf(", ");
- }
- if(c[6] == '0' && c[7] == '1' && c[10] == '0') {//Immediate offset
- if(c[8] == '1') {
- if(flag == 0) {
- address = getRegValue(rn) + immediate;
- string = fetch(memList.head, address);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(immediate != 0) {
- printf(", #%d]\n", immediate);
- } else {
- printf("]\n");
- }
- }
- } else {
- if(flag == 0) {
- address = getRegValue(rn)- immediate;
- string = fetch(memList.head, address);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(immediate!= 0) {
- printf(", #-%d]\n", immediate);
- } else {
- printf("]\n");
- }
- }
- }
- } else if(c[6] == '0' && c[7] == '1' && c[10] == '1') {//immediate pre index
- if(c[8] == '1') {// rn + immediate
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + immediate;
- string = fetch(memList.head, regs[rn]);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(address != 0) {
- printf(", #%d]!\n", immediate);
- } else {
- printf("]!\n");
- }
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - immediate;
- string = fetch(memList.head, regs[rn]);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- if(address != 0) {
- printf(", #-%d]!\n", immediate);
- } else {
- printf("]!\n");
- }
- }
- }
- } else if(c[6] == '0' && c[7] == '0' && c[10] == '0') {//immediate post index
- if(c[8] == '1') {// rn + immediate
- if(flag == 0) {
- string = fetch(memList.head, getRegValue(rn));
- regs[rd] = signedBinaryToInt(string);
- regs[rn] = getRegValue(rn) + immediate;
- }
- if(trace == 1) {
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("]");
- if(immediate != 0) {
- printf(", #%d\n", immediate);
- } else {
- printf("\n");
- }
- }
- }
- } else {
- if(flag == 0) {
- string = fetch(memList.head, getRegValue(rn));
- regs[rd] = signedBinaryToInt(string);
- regs[rn] = getRegValue(rn) - immediate;
- }
- if(trace == 1) {
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("]\n");
- if(immediate != 0) {
- printf(", #-%d]\n", immediate);
- }
- }
- }
- }
- } else if(c[6] == '1' && c[7] == '1' && c[10] == '0') {//register offset
- if(c[8] == '1') {// rn + immediate
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- address = getRegValue(rn) + (getRegValue(rm) << shiftim);
- string = fetch(memList.head, address);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]\n");
- } else {
- printf(", LSL #%d]\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- address = getRegValue(rn) + (getRegValue(rm) >> shiftim);
- } else {
- address = getRegValue(rn);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]\n");
- } else {
- printf(", LSR #%d]\n", shiftim);
- }
- }
- if(flag == 0) {
- string = fetch(memList.head, address);
- regs[rd] = signedBinaryToInt(string);
- }
- }
- } else {
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- address = getRegValue(rn) - (getRegValue(rm) << shiftim);
- string = fetch(memList.head, address);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]\n");
- } else {
- printf(",, LSL #%d]\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- address = getRegValue(rn) - (getRegValue(rm) >> shiftim);
- } else {
- address = getRegValue(rn);
- }
- if(flag == 0) {
- string = fetch(memList.head, address);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]\n");
- } else {
- printf(", LSR #%d]\n", shiftim);
- }
- }
- }
- }
- } else if(c[6] == '1' && c[7] == '1' && c[10] == '1') {//pre index register offset
- if(c[8] == '1') {// rn + immediate
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
- string = fetch(memList.head, regs[rn]);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]!\n");
- } else {
- printf(", LSL #%d]!\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + (getRegValue(rm) >> shiftim);
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn);
- }
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", ");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]!\n");
- } else {
- printf(", LSR #%d]!\n", shiftim);
- }
- }
- if(flag == 0) {
- string = fetch(memList.head, regs[rn]);
- regs[rd] = signedBinaryToInt(string);
- }
- }
- } else {
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
- string = fetch(memList.head, regs[rn]);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf("]!\n");
- } else {
- printf(", LSL #%d]!\n", shiftim);
- }
- }
- } else {
- if(shiftim != 0) {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - (getRegValue(rm) >> shiftim);
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn);
- }
- }
- if(flag == 0) {
- string = fetch(memList.head, regs[rn]);
- regs[rd] = signedBinaryToInt(string);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf(", -");
- printRegNum(rm);
- if(shiftim == 0) {
- printf(", LSR #32]!\n");
- } else {
- printf(", LSR #%d]!\n", shiftim);
- }
- }
- }
- }
- } else if(c[6] == '1' && c[7] == '0' && c[10] == '0') {// post register offset
- if(flag == 0) {
- string = fetch(memList.head, getRegValue(getRegValue(rn)));
- regs[rd] = signedBinaryToInt(string);
- }
- if(c[8] == '1') {// rn + immediate
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + (getRegValue(rm) << shiftim);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], ");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSL #%d\n", shiftim);
- } else {
- printf("\n");
- }
- }
- } else {
- if(shiftim != 0) {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) + (regs[rm] >> shiftim);
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn);
- }
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], ");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSR #%d\n", shiftim);
- } else {
- printf(", LSR #32\n");
- }
- }
- }
- } else {
- if(c[25] == '0' && c[26] == '0') {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - (getRegValue(rm) << shiftim);
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], -");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSL #%d\n", shiftim);
- } else {
- printf("\n");
- }
- }
- } else {
- if(shiftim != 0) {
- if(flag == 0) {
- regs[rn] = getRegValue(rn) - (regs[rm] >> shiftim);
- }
- } else {
- if(flag == 0) {
- regs[rn] = getRegValue(rn);
- }
- }
- if(trace == 1) {
- printf("[");
- printRegNum(rn);
- printf("], -");
- printRegNum(rm);
- if(shiftim != 0) {
- printf(", LSR %d\n", shiftim);
- } else {
- printf(", LSR #32\n");
- }
- }
- }
- }
- }
- }
- MOV(char *c, int flag) {
- char *string = substring(c, 16, 19);//using inverse ordering
- int32_t rd = unsignedBinaryToInt(string);
- if(trace == 1) {
- printRegNum(rd);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag == 0) {
- regs[rd] = x;
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- }
- }
- }
- ADD(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rn = unsignedBinaryToInt(temp);
- temp = substring(c, 16, 19);
- int32_t rd = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rn);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag == 0) {
- regs[rd] = getRegValue(rn) + x;
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- if(getBit(getRegValue(rn), 31) == getBit(x, 31)) {
- if((getBit(getRegValue(rn), 31) == 1) && (getBit(regs[rd], 31) != 1)) {
- V = 1;
- } else if ((getBit(getRegValue(rn), 31) == 0) && (getBit(regs[rd], 31) != 0)) {
- V = 1;
- } else {
- V = 0;
- }
- } else {
- V = 0;
- }
- }
- }
- }
- printRegNum(int reg) {
- switch(reg) {
- case 0:
- printf("R0"); break;
- case 1:
- printf("R1"); break;
- case 2:
- printf("R2"); break;
- case 3:
- printf("R3"); break;
- case 4:
- printf("R4"); break;
- case 5:
- printf("R5"); break;
- case 6:
- printf("R6"); break;
- case 7:
- printf("R7"); break;
- case 8:
- printf("R8"); break;
- case 9:
- printf("R9"); break;
- case 10:
- printf("R10"); break;
- case 11:
- printf("R11"); break;
- case 12:
- printf("R12"); break;
- case 13:
- printf("SP"); break;
- case 14:
- printf("LR"); break;
- case 15:
- printf("PC"); break;
- default :
- return printf("Not valid"); break;
- }
- }
- SUB(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rn = unsignedBinaryToInt(temp);
- temp = substring(c, 16, 19);
- int32_t rd = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rn);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag == 0) {
- regs[rd] = getRegValue(rn) - x;
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- if(getBit(getRegValue(rn), 31) != getBit(x, 31)) {
- if((getBit(getRegValue(rn), 31) != (getBit(regs[rd], 31)))) {
- V = 1;
- } else {
- V = 0;
- }
- } else {
- V = 0;
- }
- }
- }
- }
- CMP(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rn = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rn);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag == 0) {
- int32_t rd = getRegValue(rn) - x;
- if(c[11] == '1') {
- N = getBit(rd, 31);
- if(rd == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- if(getBit(getRegValue(rn), 31) != getBit(x, 31)) {
- if((getBit(getRegValue(rn), 31) != (getBit(rd, 31)))) {
- V = 1;
- } else {
- V = 0;
- }
- } else {
- V = 0;
- }
- }
- }
- }
- AND(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rn = unsignedBinaryToInt(temp);
- temp = substring(c, 16, 19);
- int32_t rd = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rn);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag = 0) {
- regs[rd] = getRegValue(rn) & x;
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- }
- }
- }
- EOR(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rn = unsignedBinaryToInt(temp);
- temp = substring(c, 16, 19);
- int32_t rd = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rn);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag == 0) {
- regs[rd] = getRegValue(rn) ^ x;
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- }
- }
- }
- ORR(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rn = unsignedBinaryToInt(temp);
- temp = substring(c, 16, 19);
- int32_t rd = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rn);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag == 0) {
- regs[rd] = getRegValue(rn) | x;
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- }
- }
- }
- BIC(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rn = unsignedBinaryToInt(temp);
- temp = substring(c, 16, 19);
- int32_t rd = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rn);
- printf(",");
- }
- int32_t x = addressModeOne(c);
- if(flag == 0) {
- regs[rd] = getRegValue(rn) & (~x);
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- }
- }
- }
- MUL(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rd = unsignedBinaryToInt(temp);
- temp = substring(c, 20, 23);
- int32_t rs = unsignedBinaryToInt(temp);
- temp = substring(c, 28, 31);
- int32_t rm = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rm);
- printf(", ");
- printRegNum(rs);
- printf("\n");
- }
- if(flag == 0) {
- regs[rd] = (int32_t) regs[rm] * regs[rs];
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- }
- }
- }
- MLA(char *c, int flag) {
- char *temp = substring(c, 12, 15);
- int32_t rd = unsignedBinaryToInt(temp);
- temp = substring(c, 20, 23);
- int32_t rs = unsignedBinaryToInt(temp);
- temp = substring(c, 28, 31);
- int32_t rm = unsignedBinaryToInt(temp);
- temp = substring(c, 16, 19);
- int32_t rn = unsignedBinaryToInt(temp);
- if(trace == 1) {
- printRegNum(rd);
- printf(", ");
- printRegNum(rm);
- printf(", ");
- printRegNum(rs);
- printf(", ");
- printRegNum(rn);
- printf("\n");
- }
- if(flag == 0) {
- regs[rd] = (int32_t) (regs[rm] * regs[rs]) + regs[rn];
- if(c[11] == '1') {
- N = getBit(regs[rd], 31);
- if(regs[rd] == 0) {
- Z = 1;
- } else {
- Z = 0;
- }
- }
- }
- }
- B(char *c, int flag) {
- char *temp = substring(c, 8, 31);
- int32_t address = signedBinaryToInt(temp);
- address = address << 2;
- int32_t location = regs[15] + address + 4;
- if(flag == 0) {
- regs[15] = location;
- }
- if(trace == 1) {
- printf("%d\n", location);
- }
- }
- BL(char *c, int flag) {
- char *temp = substring(c, 8, 31);
- int32_t address = signedBinaryToInt(temp);
- address = address << 2;
- int32_t location = regs[15] + address + 4;
- if(flag == 0) {
- regs[14] = regs[15];
- regs[15] = location;
- }
- if(trace == 1) {
- printf("%d\n", location);
- }
- }
- int condition(char *c, char *s) {
- if(c[0] == '1' && c[1] == '1' && c[2] == '1' && c[3] == '0') {
- s=strcpy(s,"");
- return 0;
- } else if(c[0] == '0' && c[1] == '0' && c[2] == '0' && c[3] == '0') {
- s=strcpy(s,"EQ");
- if(Z == 1) {
- return 0;
- } else {
- return 1;
- }
- } else if(c[0] == '0' && c[1] == '0' && c[2] == '0' && c[3] == '1') {
- s=strcpy(s,"NE");
- if(Z == 1) {
- return 1;
- } else {
- return 0;
- }
- } else if (c[0] == '0' && c[1] == '1' && c[2] == '0' && c[3] == '1') {
- s=strcpy(s,"PL");
- if(N == 1) {
- return 0;
- } else {
- return 1;
- }
- } else if (c[0] == '0' && c[1] == '1' && c[2] == '0' && c[3] == '0') {
- s=strcpy(s,"MI");
- if(N == 1) {
- return 1;
- } else {
- return 0;
- }
- } else if (c[0] == '1' && c[1] == '0' && c[2] == '1' && c[3] == '0') {
- s=strcpy(s,"GE");
- if(N == V) {
- return 0;
- } else {
- return 1;
- }
- } else if (c[0] == '1' && c[1] == '0' && c[2] == '1' && c[3] == '1') {
- s=strcpy(s,"LT");
- if(N != V) {
- return 0;
- } else {
- return 1;
- }
- } else if (c[0] == '1' && c[1] == '1' && c[2] == '0' && c[3] == '0') {
- s=strcpy(s,"GT");
- if((Z == 0) && (N == V)) {
- return 0;
- } else {
- return 1;
- }
- } else if (c[0] == '1' && c[1] == '1' && c[2] == '0' && c[3] == '1') {
- s=strcpy(s,"LE");
- if((Z == 1) || (N != V)) {
- return 0;
- } else {
- return 1;
- }
- }
- }
- SWI(char *c, int flag) {
- char *string = substring(c, 20, 31);
- int32_t value = unsignedBinaryToInt(string);
- if(trace == 1) {
- printf("%d\n", value);
- }
- if(flag == 0) {
- if(value == 0) {
- execute = 1;
- } else if(value == 1) {
- tracep();
- } else if(value == 2) {
- printf("R0=%08X\n", regs[0]);
- }
- }
- }
- decode (char *c) {
- char s[3];
- if(trace == 1) {
- printf("Next Instruction=");
- }
- int flag = condition(c, s);
- 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') {
- if(trace == 1) {
- printf("MOV%s ",s);
- }
- MOV(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("ADD%s ",s);
- }
- ADD(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("SUB%s ",s);
- }
- SUB(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("CMP%s ",s);
- }
- CMP(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("AND%s ",s);
- }
- AND(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("EOR%s ",s);
- }
- EOR(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("ORR%s ",s);
- }
- ORR(c, 0);
- }
- 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') {
- if(trace == 1) {
- printf("BIC%s ",s);
- }
- BIC(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("MUL%s ",s);
- }
- MUL(c, flag);
- }
- 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') {
- if(trace == 1) {
- printf("MLA%s ",s);
- }
- MLA(c, flag);
- }
- if(c[31-27] == '0' && c[31-26] == '1' && c[31-22] == '0' && c[31-20] == '1') {
- if(trace == 1) {
- printf("LDR%s ",s);
- }
- LDR(c, flag);
- }
- if(c[31-27] == '0' && c[31-26] == '1' && c[31-22] == '0' && c[31-20] == '0') {
- if(trace == 1) {
- printf("STR%s ",s);
- }
- STR(c, flag);
- }
- if(c[31-27] == '1' && c[31-26] == '0' && c[31-25] == '1' && c[31-24] == '0') {
- if(trace == 1) {
- printf("B%s ",s);
- }
- B(c, flag);
- }
- if(c[31-27] == '1' && c[31-26] == '0' && c[31-25] == '1' && c[31-24] == '1') {
- if(trace == 1) {
- printf("BL%s ",s);
- }
- BL(c, flag);
- }
- if(c[31-27] == '1' && c[31-26] == '1' && c[31-25] == '1' && c[31-24] == '1') {
- if(trace == 1) {
- printf("SWI%s ",s);
- }
- SWI(c, flag);
- }
- }
- testAddressmodeOne() {
- printf("rotate: %u\n", addressModeOne("00000011101100001000000100001110"));
- printf("reg shift: %u\n", addressModeOne("00000001101100001000000100101000"));
- printf("reg reg shift: %u\n", addressModeOne("00000001101100001000001000011110"));//14 left shifted twice. Reg Reg
- }
- executeLoop(FullList memList) {
- while(execute == 0) {
- if(trace == 1) {
- tracep();
- }
- char *instruction = fetched(memList.head, regs[15]);
- regs[15] = regs[15] + 4;
- if(instruction != "0") {
- decode(instruction);
- }
- }
- }
- dumpMem(list *mem) {
- while(mem != NULL) {
- int32_t ad = mem->address;
- int32_t ins = signedBinaryToInt(mem->instruction);
- printf("0x%08X 0x%08X\n", ad, ins);
- mem = mem->next;
- }
- printf("\n");
- }
- int main(int argc, char *argv[]) {
- memList.head = NULL;
- int i, before, after;
- before = after = 0;
- for(i = 0; i<=15; i++) {
- regs[i] = 0;
- }
- FILE *ifp;
- char *test1 = intToBinary(9);
- int y = unsignedBinaryToInt("110001");
- unsigned int x;
- char test[100];
- if (argc < 2) {
- fprintf(stderr, "Error: Expected an [option(s)] and a .emu file\n");
- return 1;
- } else {
- if(isEmu(argv[argc-1]) == 1) {
- fprintf(stderr, "Error: Expected an [option(s)] and a .emu file\n");
- return 1;
- }
- ifp = fopen (argv[argc-1], "r");
- if (ifp == NULL) { //checks whether a valid text file has been entered.
- fprintf (stderr, "Please enter a Valid .emu File\n");
- return 1;
- }
- for(i = 1; i<(argc-1); i++) {
- if(strcmp(argv[i], "-trace") == 0){
- trace = 1;
- } else if(strcmp(argv[i], "-before") == 0) {
- before = 1;
- } else if(strcmp(argv[i], "-after") == 0) {
- after = 1;
- } else {
- fprintf(stderr, "Valid options are \"-trace\", \"-before\", \"after.\"\n");
- return 1;
- }
- }
- Current current;
- while ((fscanf (ifp, "%9s %9s", current.address, current.instruction) != EOF)) {
- if(strlen(current.address) != 8 || strlen(current.instruction) != 8) {
- fprintf(stderr, "Adresses and instructions must both be 8 bits long\n");
- return 1;
- }
- x = strtol(current.address,NULL,16);
- memList = addFullList(memList, convertToBinary(current.instruction), x);
- }
- if(before == 1) {
- printf("\n");
- dumpMem(memList.head);
- }
- executeLoop(memList);
- if(after == 1) {
- printf("\n");
- dumpMem(memList.head);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment