Advertisement
xopsuei

Untitled

Apr 1st, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include "CacheSim.h"
  2. #define true 1
  3. #defien false 0
  4. /* Posa aqui les teves estructures de dades globals
  5.  * per mantenir la informacio necesaria de la cache
  6.  * */
  7.  
  8. const int cacheSize = 128;
  9. const int bytes = 5;
  10. const int lines = 7;
  11. const int tag = 20;
  12.  
  13. int cache[cacheSize];
  14. int valid[cacheSize];
  15.  
  16. /* La rutina init_cache es cridada pel programa principal per
  17.  * inicialitzar la cache.
  18.  * La cache es inicialitzada al començar cada un dels tests.
  19.  * */
  20. void init_cache ()
  21. {
  22.     /* Escriu aqui el teu codi */
  23.     malloc(cache,0,sizeof cache);
  24.     malloc(valid,0,sizeof valid);
  25. }
  26.  
  27. /* La rutina reference es cridada per cada referencia a simular */
  28. void reference (unsigned int address, unsigned int LE)
  29. {
  30.     unsigned int byte;
  31.     unsigned int linea_mp;
  32.     unsigned int linea_mc;
  33.     unsigned int tag;
  34.     unsigned int miss;
  35.     unsigned int lec_mp;
  36.     unsigned int mida_lec_mp;
  37.     unsigned int esc_mp;
  38.     unsigned int mida_esc_mp;
  39.     unsigned int replacement;
  40.     unsigned int tag_out;
  41.  
  42.     /* Escriu aqui el teu codi */
  43.     byte = address&((1<<bytes)-1);
  44.     linea_mp = address>>bytes;
  45.     linea_mc = (address&((1<<(bytes+lines))-1))>>bytes;
  46.     tag = address>>(bytes+lines);
  47.  
  48.     if (LE == 1) //lectura
  49.     {
  50.         if (!v[tag] || cache[tag] != tag)
  51.         {
  52.             miss = true;
  53.             lec_mp = true;
  54.             mida_lec_mp = 4; //lectura int
  55.             esc_mp = false;
  56.             mida_esc_mp = 0;
  57.  
  58.             if (v[tag])
  59.                 tag_out = cache[tag];
  60.             else
  61.                 tag_out = 0;
  62.             v[tag] = true;
  63.             cache[tag] = tag;
  64.             replacement = tag;
  65.         }
  66.         else
  67.         {
  68.             miss = false;
  69.             lec_mp = false;
  70.             mida_lec_mp = 0; //lectura int
  71.             esc_mp = false;
  72.             mida_esc_mp = 0;
  73.  
  74.             tag_out = 0;
  75.             replacement = 0;
  76.         }
  77.     }
  78.     else //esriptura
  79.     {
  80.         if (!v[tag] || cache[tag] != tag)
  81.             miss = true;
  82.            
  83.         miss = false;
  84.         lec_mp = false;
  85.         mida_lec_mp = 0;
  86.         esc_mp = true;
  87.         mida_esc_mp = 4; //escriptura int
  88.  
  89.         tag_out = 0;
  90.         replacement = 0;
  91.     }
  92.  
  93.     /* La funcio test_and_print escriu el resultat de la teva simulacio
  94.      * per pantalla (si s'escau) i comproba si hi ha algun error
  95.      * per la referencia actual
  96.      * */
  97.     test_and_print (address, LE, byte, linea_mp, linea_mc, tag,
  98.             miss, lec_mp, mida_lec_mp, esc_mp, mida_esc_mp,
  99.             replacement, tag_out);
  100. }
  101.  
  102. /* La rutina final es cridada al final de la simulacio */
  103. void final ()
  104. {
  105.     /* Escriu aqui el teu codi */
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement