Skip to content

Commit 3b48871

Browse files
committedApr 18, 2013
Debug location added
1 parent ab15130 commit 3b48871

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed
 

‎debug.c

+61-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ void initialize_debug()
88
{
99
db_mode = DISABLE;
1010
step_flag = DISABLE;
11+
watch_flag = DISABLE;
1112
bzero(command,COMMAND_LENGTH);
1213
bzero( prev_command, COMMAND_LENGTH);
1314
}
@@ -58,6 +59,7 @@ int runCommand(char command[])
5859
char *name = strtok(command, " ");
5960
char *arg1, *arg2, *arg3;
6061
int arg1value, arg2value;
62+
struct address translatedAddr;
6163
if(strcmp(name,"help")==0 || strcmp(name,"h")==0) //"help" to display all commands
6264
{
6365
printf("\n step / s\n\t Single step the exection\n\n");
@@ -75,7 +77,7 @@ int runCommand(char command[])
7577
printf(" memfreelist / mf \n \t Displays the Memory Free List\n\n");
7678
printf(" diskfreelist / df \n \t Displays the Memory copy of Disk Free List\n\n");
7779
printf(" fat \n \t Displays the Memory Copy of File Allocation Table\n\n");
78-
printf(" word / w <register/address> \n \t Displays the Memory Copy of File Allocation Table\n\n");
80+
printf(" location / l <address> \n \t Displays the content at memory address (Translation takes place in USER mode)\n\n");
7981
printf(" exit / e \n\t Exit the interface and Halt the machine\n");
8082
printf(" help / h\n");
8183
}
@@ -236,8 +238,22 @@ int runCommand(char command[])
236238
printDiskFreeList();
237239
else if (strcmp(name,"fat")==0) //displays File Allocation Table
238240
printFAT();
239-
else if (strcmp(name,"word")==0 || strcmp(name,"w")==0 ) //displays a word
240-
printFAT();
241+
else if (strcmp(name,"location")==0 || strcmp(name,"l")==0 ) //displays a content of a memory location
242+
{
243+
arg1 = strtok(NULL, " ");
244+
if(arg1 == NULL)
245+
{
246+
printf("Insufficient argument for \"%s\". See \"help\" for more information",name);
247+
return -1;
248+
}
249+
translatedAddr = translate_debug(atoi(arg1));
250+
if(getType(arg1) == TYPE_STR || (translatedAddr.page_no == -1 && translatedAddr.word_no == -1) )
251+
{
252+
printf("Illegal argument for \"%s\". See \"help\" for more information",name);
253+
return -1;
254+
}
255+
printLocation(translatedAddr);
256+
}
241257
else if (strcmp(name,"exit")==0 || strcmp(name,"e")==0) //Exits the interface
242258
exit(0);
243259
else
@@ -469,3 +485,45 @@ void printPageTable(int ptbr)
469485
counter++;
470486
}
471487
}
488+
489+
/*
490+
* This function translates an address without
491+
* invoking execution on errors.
492+
* returns page_no and word_no as -1 on failure
493+
*/
494+
struct address translate_debug (int virtual_addr) {
495+
struct address resultant_addr;
496+
resultant_addr.page_no = -1;
497+
resultant_addr.word_no = -1;
498+
if(mode == USER_MODE)
499+
{
500+
int page_entry;
501+
if(getType(reg[PTBR_REG]) == TYPE_STR || getType(reg[PTLR_REG]) == TYPE_STR || virtual_addr < 0
502+
|| virtual_addr >= getInteger(reg[PTLR_REG]) * PAGE_SIZE)
503+
return resultant_addr;
504+
page_entry = getInteger(reg[PTBR_REG]) + (virtual_addr / PAGE_SIZE) * 2;
505+
if(page[(page_entry+1) / PAGE_SIZE].word[(page_entry+1) % PAGE_SIZE][1] == VALID )
506+
{
507+
resultant_addr.page_no = getInteger(page[page_entry / PAGE_SIZE].word[page_entry % PAGE_SIZE] );
508+
resultant_addr.word_no = virtual_addr % PAGE_SIZE;
509+
page[(page_entry+1) / PAGE_SIZE].word[(page_entry+1) % PAGE_SIZE][0] = REFERENCED;
510+
}
511+
return resultant_addr;
512+
}
513+
else
514+
{
515+
if( virtual_addr < 0 || virtual_addr >= SIZE_OF_MEM )
516+
return resultant_addr;
517+
resultant_addr.page_no = virtual_addr / PAGE_SIZE;
518+
resultant_addr.word_no = virtual_addr % PAGE_SIZE;
519+
return resultant_addr;
520+
}
521+
}
522+
523+
/*
524+
* This function prints the memory location
525+
*/
526+
void printLocation(struct address translatedAddr)
527+
{
528+
printf("%s\n", page[translatedAddr.page_no].word[translatedAddr.word_no]);
529+
}

‎debug.h

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
int db_mode; // flag to check whether machine is run in debug mode or not
1515
int step_flag; // flag to check whether debugging in single step mode
16+
int watch_flag; // flag to check whether debugging in watch mode
1617

1718
char command[COMMAND_LENGTH],prev_command[COMMAND_LENGTH]; //buffer to store command and previous command.
1819

@@ -78,5 +79,17 @@ void printPageTable(int);
7879
* This function prints the File Allocation table
7980
*/
8081
void printFAT();
82+
83+
/*
84+
* This function translates an address without
85+
* invoking execution on errors.
86+
* returns page_no and word_no as -1 on failure
87+
*/
88+
struct address translate_debug (int virtual_addr);
89+
90+
/*
91+
* This function prints the memory location
92+
*/
93+
void printLocation(struct address);
8194

8295
#endif

‎simulator.c

+3
Original file line numberDiff line numberDiff line change
@@ -1865,10 +1865,12 @@ void Executeoneinstr(int instr)
18651865
return;
18661866
}
18671867
char input[WORD_SIZE];
1868+
FLUSH_STDIN(input); // strip newline, flush extra chars
18681869
scanf("%s",input);
18691870
input[WORD_SIZE-1] = '\0';
18701871
strcpy(reg[result], input);
18711872
storeInteger(reg[IP_REG],getInteger(reg[IP_REG])+WORDS_PERINSTR);
1873+
FLUSH_STDIN(input); // strip newline, flush extra chars
18721874
break;
18731875

18741876
case OUT:
@@ -1936,6 +1938,7 @@ void Executeoneinstr(int instr)
19361938
return;
19371939
}
19381940
printf("%s\n",reg[result]);
1941+
fflush(stdout);
19391942
storeInteger(reg[IP_REG],getInteger(reg[IP_REG])+WORDS_PERINSTR);
19401943
break;
19411944
case LOAD:

‎utility.h

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
#include <string.h>
88
#include <stdio.h>
99
#include <stdlib.h>
10+
11+
// Macro to flush stdin
12+
#define FLUSH_STDIN(x) {if(x[strlen(x)-1]!='\n'){do fgets(Junk,16,stdin);while(Junk[strlen(Junk)-1]!='\n');}else x[strlen(x)-1]='\0';}
13+
char Junk[16]; // buffer for discarding excessive user input,
14+
// used by "FLUSH_STDIN" macro
15+
1016
/*
1117
* Gets the instruction pointed by IP, to the argument
1218
* Return 0 on success

0 commit comments

Comments
 (0)
Please sign in to comment.