Polar_Feather

aaaaaa

Mar 10th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int line_offset_bits;  //number of bits in the offset
  6. int slot_num_bits;     //number of bits in the slot number
  7. typedef struct slot { //Added for assignment.
  8.   char* tag;
  9.   int valid;
  10. }slot;
  11. //struct slot* simcache;
  12. //OR (syntax I'm currently using):
  13. struct slot (*simcache)[]; //INCREDIBLY ESOTERIC SYNTAX YEA
  14.  
  15. int binToInt(char* bin) {  //converts binary number string into integer
  16.   int retVal = 0;
  17.   while(*bin) {
  18.     retVal *= 2;
  19.     if(*bin == '1')
  20.       retVal += 1;
  21.     bin++;
  22.   }
  23.   return retVal;
  24. }
  25.  
  26. int read(char* addr) {
  27.   //called when there is a read on given address; returns whether it hit
  28.   char tag[16-line_offset_bits-slot_num_bits];
  29.   tag[16-line_offset_bits-slot_num_bits] = '\0';
  30.   char slot[slot_num_bits];
  31.   slot[slot_num_bits] = '\0';
  32.   char offset[line_offset_bits];
  33.   offset[line_offset_bits] = '\0';
  34.   //I figured that everything above would require the var +1, i.e slot_num_bits+1 - but that doesn't seem to be necessary? So,???
  35.  
  36.   char* curr = addr;
  37.   for (int i = 0; i < 16-line_offset_bits-slot_num_bits; i++){
  38.           tag[i] = *curr;
  39.           curr++;
  40.   }
  41.   printf("%s\n", tag);
  42.   for (int i = 0; i < slot_num_bits; i++){
  43.           slot[i] = *curr;
  44.           curr++;
  45.   }
  46.   printf("%s\n", slot);
  47.   for (int i = 0; i < line_offset_bits; i++){
  48.           offset[i] = *curr;
  49.           curr++;
  50.   }
  51.   printf("%s\n", offset);
  52.   printf("Read from address %s: ", addr);
  53.  
  54.   int intslot = binToInt(slot); //changes slot to an integer
  55.   if ((*simcache)[intslot].tag == tag) //doesn't catch anything, see issues
  56.     if ((*simcache)[intslot].valid == 1)
  57.       return 1;
  58.  
  59.   (*simcache)[intslot].tag = tag; //questionable? char* = char[]
  60.   (*simcache)[intslot].valid = 1;
  61.   return 0;
  62. }
  63.  
  64.  
  65. int write(char* addr) {
  66.   //called when there is a write on given address; returns whether it hit
  67.   //This will essentially just be read, again, when read works
  68.   printf("Write to address %s: ", addr);
  69.   return 0;
  70. }
  71.  
  72. void print() {  //print the cache contents
  73.   //Later assignment I can hopefully manage
  74. }
  75.  
  76. int main() {
  77.   line_offset_bits = 3;  //line size is 2^3=8 bytes
  78.   slot_num_bits = 4;     //2^4=16 slots
  79.   int temp = 0; //My own additions to main start here
  80.   for (int x = slot_num_bits; x > 0; x--){
  81.     for (int y = slot_num_bits; y > 0; y--){
  82.       temp++;}} //I mean alright that's reasonable homework prompt
  83.   simcache = malloc(temp * sizeof *simcache); //Thank for not telling me this, homework prompt
  84.   //Additions mostly end here, rest is provided
  85.  
  86.   char line[30];  //to read input
  87.   while(fgets(line, 30, stdin) != NULL) {
  88.     char* cmd = strtok(line, " \n");  //command
  89.     if(strcmp(cmd, "quit") == 0)
  90.       break;
  91.     if(strcmp(cmd, "print") == 0)
  92.       print();
  93.     else if(strcmp(cmd, "read") == 0) {
  94.       char* addr = strtok(NULL, " \n");  //address for operation
  95.       if(addr)
  96.         if(read(addr))
  97.           printf("Hit!\n");
  98.         else
  99.           printf("Miss\n");
  100.       else
  101.         printf("Missing address\n");
  102.     } else if(strcmp(cmd, "write") == 0) {
  103.       char* addr = strtok(NULL, " \n");  //address for operation
  104.       if(addr)
  105.         if(write(addr))
  106.           printf("Hit!\n");
  107.         else
  108.           printf("Miss\n");
  109.       else
  110.         printf("Missing address\n");
  111.     } else
  112.       printf("Unrecognized command: %s\n", cmd);
  113.   }
  114.   free(simcache); //only other part I added in main
  115. }
  116.  
  117. ISSUES
  118. -The last check to see if a cache "hits" in read never properly triggers if the tags in the simcache slot and the newly provided addr are different...because apparently the simcache slot's tag is overwritten while the local "tag" in the read function is being built, /even though nothing suggests this should happen./ (I checked, it still does if the local tag is a different name.) What am I missing?
  119. -The debug printout is accurate while read is running. After returning to main and being called with another addr, apparently the last digit of the tag gets overwritten with garbage at some point, even though nothing else touches the tag or read.
  120.  
  121. INPUT (provided, mostly) (Addr is always 16, commas added to show tag, slot, offset)
  122. read 000001000,0101,000 //slot 5
  123. read 000000001,0101,000
  124. read 000001000,0101,000
  125. read 000000000,0101,000
  126. print
  127. quit
  128.  
  129. OUTPUT (debug printouts included)
  130. Tag at slot: (null)
  131. Tag at slot: (null)
  132. Tag at slot: (null)
  133. Tag at slot: (null)
  134. Tag at slot: (null)
  135. Tag at slot: (null)
  136. Tag at slot: (null)
  137. Tag at slot: (null)
  138. Tag at slot: (null)
  139. Tag at slot: (null)
  140. Tag at slot: (null)
  141. 000001000 //tag
  142. 0101 //slot
  143. 000 //offset
  144. Read from address 0000010000101000: Miss
  145. Tag at slot 5: (null)
  146. Validity bit at slot 5: 0
  147. *adds to slot*
  148. Tag at slot 5: 000001000
  149.  
  150. Tag at slot: 00000100▒
  151. Tag at slot: 00000100▒
  152. Tag at slot: 00000100▒
  153. Tag at slot: 00000100▒
  154. Tag at slot: 00000100▒
  155. Tag at slot: 00000100▒
  156. Tag at slot: 00000000▒
  157. Tag at slot: 00000000▒
  158. Tag at slot: 00000000▒
  159. Tag at slot: 000000001
  160. Tag at slot: 000000001
  161. 000000001
  162. 0101
  163. 000
  164. Read from address 0000000010101000: Hit!
  165. Tag at slot 5: 000000001
  166. Validity bit at slot 5: 1
  167.  
  168. Tag at slot: 00000000▒
  169. Tag at slot: 00000000▒
  170. Tag at slot: 00000000▒
  171. Tag at slot: 00000000▒
  172. Tag at slot: 00000000▒
  173. Tag at slot: 00000000▒
  174. Tag at slot: 00000100▒
  175. Tag at slot: 00000100▒
  176. Tag at slot: 00000100▒
  177. Tag at slot: 000001000
  178. Tag at slot: 000001000
  179. 000001000
  180. 0101
  181. 000
  182. Read from address 0000010000101000: Hit!
  183. Tag at slot 5: 000001000
  184. Validity bit at slot 5: 1
  185.  
  186. Tag at slot: 00000100▒
  187. Tag at slot: 00000100▒
  188. Tag at slot: 00000100▒
  189. Tag at slot: 00000100▒
  190. Tag at slot: 00000100▒
  191. Tag at slot: 00000100▒
  192. Tag at slot: 00000000▒
  193. Tag at slot: 00000000▒
  194. Tag at slot: 00000000▒
  195. Tag at slot: 000000000
  196. Tag at slot: 000000000
  197. 000000000
  198. 0101
  199. 000
  200. Read from address 0000000000101000: Hit!
  201. Tag at slot 5: 000000000
  202. Validity bit at slot 5: 1
  203.  
  204. ((NONE OF THESE SHOULD BE HITS))
Add Comment
Please, Sign In to add comment