diff --git a/main.c b/main.c index 30e431c60b6ed44e6242b084d9ca178498512b25..945008ca6166f469ae68ac020d66b5a2f255cee7 100644 --- a/main.c +++ b/main.c @@ -62,7 +62,7 @@ bool verbose; void run_simulator(char*, bool); void handle_line(char*, char*, char*, char*); int process_line(char*, int, int, bool); -void process_hit(char*, bool); +void process_hit(char*, int, int, bool); void process_miss(char*, int, int, int, bool); void process_eviction(char*, int, int, int, bool); void update_last_recently_used(int, int); @@ -213,45 +213,49 @@ void run_simulator(char* file_name, bool verbose) { // Read in file line by line. while (fgets(read_in_buffer, 255, file_pointer)) { read_in_buffer = strtok(read_in_buffer, "\n"); - // printf(" line: %s", read_in_buffer); - - char* full_line = calloc(255, sizeof(char)); - memcpy(full_line, read_in_buffer, strlen(read_in_buffer)); - char* line_operation = ""; - char* line_address = ""; - char* line_size = ""; - - // Parse values in line. - char* token_1 = strtok(read_in_buffer, " "); - while (token_1 != NULL) { - // printf(" token: %s", token_1); - - // Handle if operation not set. - if (strcmp(line_operation, "") == 0) { - line_operation = token_1; - } else { - // Operation set. Get address. - char* token_2 = strtok(token_1, ","); - line_address = token_2; - - // Get size. - token_2 = strtok(NULL, read_in_buffer); - // char* token_3 = strtok(token_2, "\n"); - line_size = token_2; + if (read_in_buffer != NULL && strcmp(read_in_buffer, "") != 0) { + // printf(" line: %s\n", read_in_buffer); + + char* full_line = calloc(255, sizeof(char)); + memcpy(full_line, read_in_buffer, strlen(read_in_buffer)); + char* line_operation = ""; + char* line_address = ""; + char* line_size = ""; + + // Parse values in line. + char* token_1 = strtok(read_in_buffer, " "); + while (token_1 != NULL) { + // printf(" token: %s", token_1); + + // Handle if operation not set. + if (strcmp(line_operation, "") == 0) { + line_operation = token_1; + } else { + // Operation set. Get address. + char* token_2 = strtok(token_1, ","); + line_address = token_2; + + // Get size. + token_2 = strtok(NULL, read_in_buffer); + // char* token_3 = strtok(token_2, "\n"); + line_size = token_2; + } + + token_1 = strtok(NULL, read_in_buffer); } - token_1 = strtok(NULL, read_in_buffer); - } + // printf(" line_operation: \"%s\"\n", line_operation); + // printf(" line_address: \"%s\"\n", line_address); + // printf(" line_size: \"%s\n", line_size); + // printf("\n\n"); + handle_line(full_line, line_operation, line_address, line_size); + // printf("\n"); - // printf(" line_operation: \"%s\"\n", line_operation); - // printf(" line_address: \"%s\"\n", line_address); - // printf(" line_size: \"%s\n", line_size); - // printf("\n\n"); - handle_line(full_line, line_operation, line_address, line_size); + free(full_line); + } // Reset buffer for next possible line read in. free(read_in_buffer); - free(full_line); read_in_buffer = calloc(sizeof(char*), 255); } @@ -272,8 +276,24 @@ void handle_line(char* line, char* line_operation, char* line_address, char* lin } // Not an instruction load. Keep processing. Start by parsing line info. - int address = (int)strtol(line_address, NULL, 16); - int size = (int)strtol(line_size, NULL, 16) - 1; + // Start by validating line. + char* char_pointer = line_address; + for (int index = 0; index < strlen(line_address); index++) { + // char tempchar[1]; + // printf("tolower: %c\n", tolower(*char_pointer)); + // printf("atoi: %d\n", tolower(*char_pointer) - '0'); + // strcpy(tempchar, char_pointer); + // printf("atoi: %d\n", atoi(tempchar)); + if ((tolower(*char_pointer) - '0') > 54) { + // printf("Updating index %d\n", index); + line_address[index] = 'f'; + } + } + // printf("Updated line address: %s\n", line_address); + + // Convert to integers. + long long address = strtoll(line_address, NULL, 16); + // int size = (int)strtol(line_size, NULL, 16) - 1; // printf(" Address: %d\n", address); // printf(" Size: %d\n", size); @@ -291,7 +311,7 @@ void handle_line(char* line, char* line_operation, char* line_address, char* lin // printf("tag_mask: %d\n", tag_mask); // Use masks to get address chunks. - int block = address & block_mask; + // int block = address & block_mask; int set = (address & set_mask) >> block_offset; int tag = (address & tag_mask) >> (block_offset + set_offset); @@ -328,9 +348,8 @@ void handle_line(char* line, char* line_operation, char* line_address, char* lin } } else { - // Unknown operation. - fprintf(stderr, "Unknown operation on line \"%s.\n", line); - exit(1); + // Unknown operation. Back out. + return ; } } @@ -338,7 +357,7 @@ void handle_line(char* line, char* line_operation, char* line_address, char* lin int process_line(char* line, int set, int tag, bool display_bool) { // Compare against all entries in the cache for match. for (int line_index = 0; line_index < E; line_index++) { - // printf("Set Index: %d Line_Index: %d\n", set, line_index); + // printf("Set Index: %d line_index: %d\n", set, line_index); int valid_bit = cache->set[set].line[line_index].valid; int tag_bits = cache->set[set].line[line_index].tag; // printf(" Set[%d][%d] valid bit: %d\n", set, line_index, valid_bit); @@ -349,7 +368,7 @@ int process_line(char* line, int set, int tag, bool display_bool) { if (valid_bit == true && tag_bits == tag) { // printf("tag_bits: %d line_tag: %d\n", tag_bits, tag); // Match found. - process_hit(line, display_bool); + process_hit(line, set, line_index, display_bool); return 0; } else { // Not a match. Examine why. @@ -365,21 +384,28 @@ int process_line(char* line, int set, int tag, bool display_bool) { // Get last used index. for (int line_index = 0; line_index < E; line_index++) { // Check if value is equal to or greater than total possible lines. If so, use value. + // printf("line_index: %d last_use: %d\n", line_index, cache->set[set].line[line_index].last_use); if (cache->set[set].line[line_index].last_use >= E - 1) { + process_eviction(line, set, line_index, tag, display_bool); return 2; } } + + fprintf(stderr, "Failed to process line \"%s\".\n", line); + exit(1); } -void process_hit(char* line, bool display_bool) { +void process_hit(char* line, int set_index, int line_index, bool display_bool) { hits += 1; // Print if verbose flag set. if (display_bool == true && verbose == true) { printf("%s hit\n", line); } + + update_last_recently_used(set_index, line_index); } @@ -420,8 +446,10 @@ void update_last_recently_used(int set_index, int line_index) { // Update all lines in set. for (int index = 0; index < E; index++) { cache->set[set_index].line[index].last_use += 1; + // printf('line_index: %d new last_use: %d', index, cache->set[set_index].line[index].last_use); } // Reset current line index back to 0, as it was the last used. cache->set[set_index].line[line_index].last_use = 0; + // printf("curr line_index: %d new last_use: %d", line_index, cache->set[set_index].line[line_index].last_use); }