Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages | Examples

ac_encoder.cpp

00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 
00003 /*  Generic encoder for arquitectures described in ArchC
00004     Copyright (C) 2002-2004  The ArchC Team
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 */
00016 
00017 /********************************************************/
00018 /* The ArchC Encoder Generator                          */
00019 /* Author: Marcelo de Almeida Oliveira                  */
00020 /* Modified by: Marcus Bartholomeu                      */
00021 /*                                                      */
00022 /* The ArchC Team                                       */
00023 /* Computer Systems Laboratory (LSC)                    */
00024 /* IC-UNICAMP                                           */
00025 /* http://www.lsc.ic.unicamp.br                         */
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   // Begin of extra tools
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 //   ac_decoder_full *decoder = mips1.mips1_mc->ISA.decoder;
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           //printf("Fields: ");
00146           //do {
00147           //  printf("%s:%d:%d ", decoder->formats[i].fields[j].name, decoder->formats[i].fields[j].size, decoder->formats[i].fields[j].first_bit);
00148           //  j++;
00149           //} while (decoder->formats[i].fields[j-1].next != NULL);
00150           //printf("\n");
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   // End of extra tools
00200 
00201 //   for(int k = 0; k < ac; k++)
00202 //     fprintf(stderr, "%d %s \n", k, av[k]);
00203 }

Generated on Thu Jun 24 08:30:05 2004 for ArchC by doxygen 1.3.4