00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <stdlib.h>
00031
00032 #include "ac_encoder.H"
00033
00034
00035 unsigned int pow2(int expoent) {
00036 return 1 << expoent;
00037 }
00038
00039 void inttobin(unsigned int number, char * string, int size) {
00040
00041 int pos = 0;
00042
00043 while(pos < size) {
00044 if (number >= pow2(size-pos-1)) {
00045 string[pos] = '1';
00046 number = number - pow2(size-pos-1);
00047 } else {
00048 string[pos] = '0';
00049 }
00050 pos++;
00051 }
00052
00053 string[size] = '\0';
00054
00055 return;
00056 }
00057
00058 void stringtohexa(char * string, char * hexa, int size)
00059 {
00060 char aux[5];
00061 int b = 0, i = 0;
00062
00063 #ifdef USE_LITTLE_ENDIAN
00064 for(b = (size-2)*4; b >= 0; b -= 8)
00065 #else
00066 for(b = 0; b < 4*size; b += 8)
00067 #endif
00068 for(i = b; i < b+8; i += 4) {
00069
00070 aux[0] = string[i];
00071 aux[1] = string[i+1];
00072 aux[2] = string[i+2];
00073 aux[3] = string[i+3];
00074 aux[4] = '\0';
00075
00076 if (strstr(aux, "0000")) { hexa[i/4] = '0'; continue; }
00077 if (strstr(aux, "0001")) { hexa[i/4] = '1'; continue; }
00078 if (strstr(aux, "0010")) { hexa[i/4] = '2'; continue; }
00079 if (strstr(aux, "0011")) { hexa[i/4] = '3'; continue; }
00080 if (strstr(aux, "0100")) { hexa[i/4] = '4'; continue; }
00081 if (strstr(aux, "0101")) { hexa[i/4] = '5'; continue; }
00082 if (strstr(aux, "0110")) { hexa[i/4] = '6'; continue; }
00083 if (strstr(aux, "0111")) { hexa[i/4] = '7'; continue; }
00084 if (strstr(aux, "1000")) { hexa[i/4] = '8'; continue; }
00085 if (strstr(aux, "1001")) { hexa[i/4] = '9'; continue; }
00086 if (strstr(aux, "1010")) { hexa[i/4] = 'A'; continue; }
00087 if (strstr(aux, "1011")) { hexa[i/4] = 'B'; continue; }
00088 if (strstr(aux, "1100")) { hexa[i/4] = 'C'; continue; }
00089 if (strstr(aux, "1101")) { hexa[i/4] = 'D'; continue; }
00090 if (strstr(aux, "1110")) { hexa[i/4] = 'E'; continue; }
00091 if (strstr(aux, "1111")) { hexa[i/4] = 'F'; continue; }
00092 }
00093
00094 hexa[size] = '\0';
00095 }
00096
00097
00098
00099 void ac_encoder(int ac, char *av[], ac_decoder_full *decoder)
00100 {
00101
00102 printf("\n");
00103 printf("ac_encoder tools:\n");
00104 printf("--tree prints decoder tree\n");
00105 printf("--encode encodes instructions into hexa format\n");
00106 printf("--simulate simulates instructions created by encode\n");
00107 printf("\n");
00108
00109
00110
00111 for(int k = 0; k < ac; k++)
00112 if (strstr(av[k],"--tree"))
00113 ShowDecoder(decoder->decoder, 2);
00114
00115 for(int k = 0; k < ac; k++)
00116 if (strstr(av[k],"--encode")) {
00117 int i, j;
00118 char input[65];
00119
00120 int instructionType, fieldValue;
00121 unsigned int instruction;
00122 char binary[65];
00123 char hexa[18];
00124
00125 int pc = 0;
00126 char pc_hexa[5];
00127
00128 FILE *file;
00129
00130 file = fopen("hexa.code", "w");
00131 if (file == NULL) {
00132 printf("Error while opening file.");
00133 exit(1);
00134 }
00135
00136 while (true) {
00137 instruction = 0;
00138
00139 printf("\nAvailable instruction types: \n");
00140
00141 i = 0;
00142 do {
00143 j = 0;
00144 printf("(%d) Name: %s Size: %d \n", i, decoder->formats[i].name, decoder->formats[i].size);
00145
00146
00147
00148
00149
00150
00151 i++;
00152 } while (decoder->formats[i-1].next != NULL);
00153
00154 printf("\nChoose instruction type> ");
00155 scanf("%65s", &input);
00156 instructionType = atoi(input);
00157
00158 printf("Chosen instruction type: %s \n", decoder->formats[instructionType].name);
00159
00160 j = 0;
00161 do {
00162 printf("\nPlease enter in binary %s:%d value> ", decoder->formats[instructionType].fields[j].name, decoder->formats[instructionType].fields[j].size);
00163 scanf("%65s", &input);
00164 if ((strlen(input) != 0) &&
00165 (strlen(input) != (unsigned int) decoder->formats[instructionType].fields[j].size)) {
00166 printf("Incorrect number of bits for this field.");
00167 continue;
00168 }
00169 if (strlen(input) != 0) {
00170 fieldValue = strtol(input, NULL, 2);
00171 instruction += fieldValue << (decoder->formats[instructionType].size - decoder->formats[instructionType].fields[j].first_bit - 1);
00172 }
00173 j++;
00174 } while (decoder->formats[instructionType].fields[j-1].next != NULL);
00175
00176 inttobin(instruction, binary, decoder->formats[instructionType].size);
00177 stringtohexa(binary, hexa, decoder->formats[instructionType].size/4);
00178 printf("\nEncoded instruction = %s = %u = %s\n", binary, instruction, hexa);
00179
00180 inttobin(pc, binary, 16);
00181 stringtohexa(binary, pc_hexa, 4);
00182 pc += decoder->formats[instructionType].size/8;
00183
00184 fprintf(file, "%s %s\n", pc_hexa, hexa);
00185
00186 printf("\nPress enter for a new instruction or f to finish> ");
00187 scanf("%65s", &input);
00188 if (strstr(input,"f"))
00189 break;
00190 }
00191
00192 fclose(file);
00193 }
00194
00195 for(int k = 0; k < ac; k++)
00196 if (strstr(av[k],"--simulate"))
00197 av[1] = "--load=hexa.code";
00198
00199
00200
00201
00202
00203 }