Don't like ads? PRO users don't see any ads ;-)
Guest

cacheSimulator

By: edisonhund on Jun 1st, 2012  |  syntax: C  |  size: 2.47 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2. Arquitetura e Organização de Computadores
  3. Simulação de memória cache
  4. Arquivos (cache.c, lib.c, lib.h, makefile)
  5. Exemplo de saída: ./cache 512 direto 16 memEnd.txt
  6. OUT/2011
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include "lib.h"
  14.  
  15. /*
  16. <tamanho da cache> <mapeamento> <tamanho do bloco> <arquivo>
  17. */
  18. int main(int argc, char* argv[]){    
  19.  
  20.     if (argc != 5){
  21.         printf("\nNumero de parametros invalido!\n");
  22.         printf("./cache <tamanho da cache> <associatividade> <tamanho do bloco> <arquivo>\n\n");
  23.         exit(1);
  24.     }
  25.  
  26.     Arquivo = fopen(argv[4], "r");
  27.  
  28.     if(Arquivo == NULL){
  29.         printf("\nErro na leitura do arquivo!\n\n");
  30.         exit(1);
  31.     }
  32.  
  33.     map = argv[2];
  34.    
  35.     if(strcmp(map,"conjassoc") == 0){    
  36.        printf("\nInforme o numero de vias: ");
  37.        scanf("%d",&vias);
  38.     }
  39.    
  40.     tamCache = atoi(argv[1]);
  41.     tamBloco = atoi(argv[3]);
  42.  
  43.     if(strcmp(map,"direto") == 0){
  44.  
  45.        setnumber = (tamCache/tamBloco);
  46.    
  47.        setbits = (round((log(setnumber))/(log(2))));
  48.  
  49.        blockbits = log(tamBloco)/log(2);
  50.        printf("\nOffset: %.f bits\n", log(tamBloco)/log(2));
  51.    
  52.        indice = log((setnumber+1)*1024)/log(2);
  53.        printf("Indice: %.f bits\n", log((setnumber+1)*1024)/log(2));
  54.    
  55.        tagsize = 32 - (blockbits + indice);
  56.        printf("Tag: %d bits\n", tagsize);  
  57.    
  58.     }else if(strcmp(map,"associativo") == 0){
  59.      
  60.        setnumber = (tamCache/tamBloco);
  61.    
  62.        setbits = (round((log(setnumber))/(log(2))));
  63.  
  64.        blockbits = log(tamBloco)/log(2);
  65.        printf("\nOffset: %.f bits\n", log(tamBloco)/log(2));
  66.    
  67.        indice = log((setnumber+1)*1024)/log(2);      
  68.        
  69.        tagsize = 32 - (blockbits);
  70.        printf("Tag: %d bits\n", tagsize);  
  71.      
  72.     }else if(strcmp(map,"conjassoc") == 0){
  73.          
  74.        setnumber = (tamCache/tamBloco);
  75.    
  76.        setbits = (round((log(setnumber))/(log(2))));
  77.  
  78.        blockbits = log(tamBloco)/log(2);
  79.        printf("\nOffset: %.f bits\n", log(tamBloco)/log(2));
  80.    
  81.        indice = log(((setnumber+1)/vias)*1024)/log(2);
  82.        printf("Indice: %d bits\n", indice);
  83.    
  84.        tagsize = 32 - (blockbits + indice);
  85.        printf("Tag: %d bits\n", tagsize);          
  86.     }
  87.    
  88.     HitMiss();
  89.  
  90.     printf("\nHits: %d\n", cacheHit);
  91.     printf("Misses: %d\n", cacheMiss);
  92.     printf("Writes: %d\n", write);
  93.     printf("Reads: %d\n\n", read);
  94. }