Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int line_offset_bits; //number of bits in the offset
- int slot_num_bits; //number of bits in the slot number
- typedef struct slot { //Added for assignment.
- char* tag;
- int valid;
- }slot;
- //struct slot* simcache;
- //OR (syntax I'm currently using):
- struct slot (*simcache)[]; //INCREDIBLY ESOTERIC SYNTAX YEA
- int binToInt(char* bin) { //converts binary number string into integer
- int retVal = 0;
- while(*bin) {
- retVal *= 2;
- if(*bin == '1')
- retVal += 1;
- bin++;
- }
- return retVal;
- }
- int read(char* addr) {
- //called when there is a read on given address; returns whether it hit
- char tag[16-line_offset_bits-slot_num_bits];
- tag[16-line_offset_bits-slot_num_bits] = '\0';
- char slot[slot_num_bits];
- slot[slot_num_bits] = '\0';
- char offset[line_offset_bits];
- offset[line_offset_bits] = '\0';
- //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,???
- char* curr = addr;
- for (int i = 0; i < 16-line_offset_bits-slot_num_bits; i++){
- tag[i] = *curr;
- curr++;
- }
- printf("%s\n", tag);
- for (int i = 0; i < slot_num_bits; i++){
- slot[i] = *curr;
- curr++;
- }
- printf("%s\n", slot);
- for (int i = 0; i < line_offset_bits; i++){
- offset[i] = *curr;
- curr++;
- }
- printf("%s\n", offset);
- printf("Read from address %s: ", addr);
- int intslot = binToInt(slot); //changes slot to an integer
- if ((*simcache)[intslot].tag == tag) //doesn't catch anything, see issues
- if ((*simcache)[intslot].valid == 1)
- return 1;
- (*simcache)[intslot].tag = tag; //questionable? char* = char[]
- (*simcache)[intslot].valid = 1;
- return 0;
- }
- int write(char* addr) {
- //called when there is a write on given address; returns whether it hit
- //This will essentially just be read, again, when read works
- printf("Write to address %s: ", addr);
- return 0;
- }
- void print() { //print the cache contents
- //Later assignment I can hopefully manage
- }
- int main() {
- line_offset_bits = 3; //line size is 2^3=8 bytes
- slot_num_bits = 4; //2^4=16 slots
- int temp = 0; //My own additions to main start here
- for (int x = slot_num_bits; x > 0; x--){
- for (int y = slot_num_bits; y > 0; y--){
- temp++;}} //I mean alright that's reasonable homework prompt
- simcache = malloc(temp * sizeof *simcache); //Thank for not telling me this, homework prompt
- //Additions mostly end here, rest is provided
- char line[30]; //to read input
- while(fgets(line, 30, stdin) != NULL) {
- char* cmd = strtok(line, " \n"); //command
- if(strcmp(cmd, "quit") == 0)
- break;
- if(strcmp(cmd, "print") == 0)
- print();
- else if(strcmp(cmd, "read") == 0) {
- char* addr = strtok(NULL, " \n"); //address for operation
- if(addr)
- if(read(addr))
- printf("Hit!\n");
- else
- printf("Miss\n");
- else
- printf("Missing address\n");
- } else if(strcmp(cmd, "write") == 0) {
- char* addr = strtok(NULL, " \n"); //address for operation
- if(addr)
- if(write(addr))
- printf("Hit!\n");
- else
- printf("Miss\n");
- else
- printf("Missing address\n");
- } else
- printf("Unrecognized command: %s\n", cmd);
- }
- free(simcache); //only other part I added in main
- }
- ISSUES
- -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?
- -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.
- INPUT (provided, mostly) (Addr is always 16, commas added to show tag, slot, offset)
- read 000001000,0101,000 //slot 5
- read 000000001,0101,000
- read 000001000,0101,000
- read 000000000,0101,000
- print
- quit
- OUTPUT (debug printouts included)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- Tag at slot: (null)
- 000001000 //tag
- 0101 //slot
- 000 //offset
- Read from address 0000010000101000: Miss
- Tag at slot 5: (null)
- Validity bit at slot 5: 0
- *adds to slot*
- Tag at slot 5: 000001000
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 000000001
- Tag at slot: 000000001
- 000000001
- 0101
- 000
- Read from address 0000000010101000: Hit!
- Tag at slot 5: 000000001
- Validity bit at slot 5: 1
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 000001000
- Tag at slot: 000001000
- 000001000
- 0101
- 000
- Read from address 0000010000101000: Hit!
- Tag at slot 5: 000001000
- Validity bit at slot 5: 1
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000100▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 00000000▒
- Tag at slot: 000000000
- Tag at slot: 000000000
- 000000000
- 0101
- 000
- Read from address 0000000000101000: Hit!
- Tag at slot 5: 000000000
- Validity bit at slot 5: 1
- ((NONE OF THESE SHOULD BE HITS))
Add Comment
Please, Sign In to add comment