Sim1 Complete up until Step 5

+- .gitignore
++ Sim01 Files
++ SimTimer stuff
++ SimProgGen
This commit is contained in:
Charles Descamps 2024-01-30 03:07:38 -07:00
parent 08677204ca
commit dc4eca7285
25 changed files with 2059 additions and 0 deletions

3
.gitignore vendored
View file

@ -65,3 +65,6 @@ compile_commands.json
CTestTestfile.cmake CTestTestfile.cmake
_deps _deps
# Text Docs
*.txt

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,397 @@
// header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "proggen.h"
// main program
int main()
{
// initialize program
// initialize variables
int procPercentage, numOps, numPrograms;
int progCtr, opCtr;
char fileName[ STD_STR_LEN ];
FILE *outFilePtr;
char opString[ STD_STR_LEN ];
char tempString[ STD_STR_LEN ];
// initialize random generator
// function: srand
srand( time( NULL ) );
// show title
// function: printf
printf( "Program Meta-Data Creation Program\n\n" );
// get file name for meta-data file
// function: printf, scanf
printf( "Enter file name to use: " );
scanf( "%s", fileName );
// get weight of processing
// function: printf, scanf
printf( "Enter percentage of processing: " );
scanf( "%d", &procPercentage );
// get number of actions
// function: printf, scanf
printf( "Enter number of operations per program: " );
scanf( "%d", &numOps );
// get number of programs to generate
// function: printf, scanf
printf( "Enter number of programs: " );
scanf( "%d", &numPrograms );
// open file
// function: fopen
outFilePtr = fopen( fileName, "w" );
// output file description header
// function: fputs
fputs( "Start Program Meta-Data Code:\r\n", outFilePtr );
// set temporary string with string literal
// function: strcpy
strcpy( tempString, "sys start\r\n" );
// output Operating System start
// function: fputs
fputs( tempString, outFilePtr );
// loop across number of programs
for( progCtr = 0; progCtr < numPrograms; progCtr++ )
{
// set temporary string with string literal
// function: strcpy
strcpy( tempString, "app start, 0\r\n" );
// show begin of program
// function: fputs
fputs( tempString, outFilePtr );
// get new op, force first item to be input
// function: getNewOp
getNewOp( procPercentage, opString, True );
// loop across number of operations
for( opCtr = 0; opCtr < numOps; opCtr++ )
{
// output new op
// function: fputs
fputs( opString, outFilePtr );
// get new op
// function: getNewOp
getNewOp( procPercentage, opString, False );
}
// end loop across number of operations
// set temporary string with string literal
// function: strcpy
strcpy( tempString, "app end\r\n" );
// add end of program
// function: fputs
fputs( tempString, outFilePtr );
}
// end number of programs loop
// set temporary string with string literal
// function: strcpy
strcpy( tempString, "sys end\r\n" );
// output end of operating system
// function: fputs
fputs( tempString, outFilePtr );
// output file description header
// function: fputs
fputs( "End Program Meta-Data Code.\n\n", outFilePtr );
// close file
// function: fclose
fclose( outFilePtr );
// shut down program
// return success
return 0;
}
// supporting function implementations
int getRandBetween( int low, int high )
{
// initialize range
int range = high - low + 1;
// set random result and return
// function: rand
return rand() % range + low;
}
Boolean getOdds( int oddPercent )
{
// initialize odds
// function: rand
int randVal = rand() % 100 + 1;
// check for odds less than/equal to specification
if( randVal <= oddPercent )
{
// return true
return True;
}
// otherwise, if odds not met, return false
return False;
}
void getNewOp( int percent, char opStr[], Boolean firstOpFlag )
{
// initialize recursive start index
int recStartIndex = 0;
int selectionChance = 25; // roughly 1 in 7 chance
// initialize speedFactor to 1, for processor
// speedFactor makes I/O devices relatively slower than the processor
// - although not as relatively slow as they would really be
int speedFactor = 1;
// declare other variables
int numCycles;
char numStr[ STD_STR_LEN ];
// check for chance of processing
// function: getOdds
if( firstOpFlag == False && getOdds( percent ) == True )
{
// place a processing action
// function: strcpy
strcpy( opStr, "cpu process, " );
}
// otherwise, assume some kind of I/O
else
{
// check for first I/O operations, or chance of input operation
// function: getOdds
if( firstOpFlag == True
|| getOdds( INPUT_CHANCE ) == True ) // input process
{
// check for odds of Ethernet
if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev in, ethernet, " );
// 10x slower than processor
speedFactor = 2;
}
// check for odds of hard drive
// function: getOdds
if( getOdds( selectionChance ) == True )
{
// place a hard drive operation
// function: strcpy
strcpy( opStr, "dev in, hard drive, " );
// 3x slower than processor
speedFactor = 3;
}
// otherwise, assume keyboard
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev in, keyboard, " );
// 10x slower than processor
speedFactor = 10;
}
// otherwise, assume serial
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev in, serial, " );
// 10x slower than processor
speedFactor = 4;
}
// otherwise, assume sound signal
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev in, sound signal, " );
// 10x slower than processor
speedFactor = 5;
}
// otherwise, assume USB
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev in, usb, " );
// 10x slower than processor
speedFactor = 2;
}
// otherwise, assume video signal
else
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev in, video signal, " );
// 10x slower than processor
speedFactor = 7;
}
}
// otherwise, assume output operation
else
{
// check for odds of Ethernet
if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev out, ethernet, " );
// 10x slower than processor
speedFactor = 2;
}
// check for odds of hard drive
// function: getOdds
if( getOdds( selectionChance ) == True )
{
// place a hard drive operation
// function: strcpy
strcpy( opStr, "dev out, hard drive, " );
// 3x slower than processor
speedFactor = 3;
}
// otherwise, assume keyboard
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev out, monitor, " );
// 10x slower than processor
speedFactor = 10;
}
// otherwise, assume serial
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev out, serial, " );
// 10x slower than processor
speedFactor = 4;
}
// otherwise, assume sound signal
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev out, sound signal, " );
// 10x slower than processor
speedFactor = 5;
}
// otherwise, assume USB
else if( getOdds( selectionChance ) == True )
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev out, usb, " );
// 10x slower than processor
speedFactor = 2;
}
// otherwise, assume video signal
else
{
// place a keyboard operation
// function: strcpy
strcpy( opStr, "dev out, video signal, " );
// 10x slower than processor
speedFactor = 7;
}
}
}
// find random number of I/O cycles
// function: getRandBetween
numCycles = getRandBetween( OPTIME_MIN, OPTIME_MAX ) * speedFactor;
// set number as string
// function: intToString
intToString( numCycles, numStr, recStartIndex );
// append string number to metadata item, with endlines
// function: strcat
strcat( opStr, numStr );
strcat( opStr, "\r\n" );
}
int intToString( int value, char valStr[], int index )
{
// initialize function, variables
char digit;
// check for digits remaining
if( value > 0 )
{
// convert digit to character
digit = (char) ( value % 10 + '0' );
// call recursive function to get next digt
// function: intToString
index = intToString( value / 10, valStr, index + 1 );
}
// otherwise, assume no digits remaining
else
{
// end string with NULL_CHAR
valStr[ index ] = NULL_CHAR;
// return uncounted character
return 0;
}
// add character to string
valStr[ index ] = digit;
// return updated string length
return index + 1;
}

View file

@ -0,0 +1,27 @@
// Header file for proggen.c
// Precompiler directive, to eliminate multiple compiles of h file
#ifndef PROGGEN_H
#define PROGGEN_H
// global constants
const int STD_STR_LEN = 45;
const int INPUT_CHANCE = 40;
const int OPTIME_MAX = 10;
const int OPTIME_MIN = 5;
const char NULL_CHAR = '\0';
const char ENDLINE_CHAR[] = "\n";
typedef enum { False, True } Boolean;
// function prototypes
void getNewOp( int percent, char opStr[], Boolean firstOpFlag );
Boolean getOdds( int oddPercent );
int getRandBetween( int low, int high );
int intToString( int value, char valStr[], int index );
#endif // PROGGEN_H

View file

@ -0,0 +1,14 @@
CC = gcc
DEBUG = -g
CFLAGS = -Wall -std=c99 -pedantic -c $(DEBUG)
LFLAGS = -Wall -std=c99 -pedantic $(DEBUG)
proggen : proggen.o
$(CC) $(LFLAGS) proggen.o -o proggen
proggen.o : proggen.c proggen.h
$(CC) $(CFLAGS) proggen.c
clean:
\rm *.o proggen

View file

@ -0,0 +1,40 @@
Start Program Meta-Data Code:
sys start
app start, 0
dev in, hard drive, 18
cpu process, 9
cpu process, 9
cpu process, 9
dev out, monitor, 60
app end
app start, 0
dev in, sound signal, 40
dev in, hard drive, 30
dev in, sound signal, 25
cpu process, 6
dev out, usb, 10
app end
app start, 0
dev in, video signal, 70
cpu process, 10
dev out, monitor, 70
dev in, hard drive, 18
cpu process, 9
app end
app start, 0
dev in, sound signal, 35
dev out, monitor, 100
dev in, keyboard, 50
cpu process, 9
dev out, video signal, 49
app end
app start, 0
dev in, keyboard, 90
dev out, sound signal, 40
dev out, serial, 32
cpu process, 10
dev in, hard drive, 15
app end
sys end
End Program Meta-Data Code.

246
Sim01/OS_SimDriver.c Normal file
View file

@ -0,0 +1,246 @@
// header files
#include "OS_SimDriver.h"
int main(int argc, char **argv)
{
// initialize program (variables and title)
ConfigDataType *configDataPtr = NULL;
OpCodeType *metaDataPtr = NULL;
char errorMsg[MAX_STR_LEN];
CmdLineData cmdLineData;
bool configUploadSuccess = false;
// show title
// fct: printf
printf("\nSimulator Program\n");
printf("=================\n\n");
// process command line arguments
// check for at least one correct switch and config file name verified
// fct: processCmdLine
if(processCmdLine(argc, argv, &cmdLineData))
{
// upload config file, check for success
// fct: getConfigData
if(getConfigData(cmdLineData.fileName, &configDataPtr, errorMsg))
{
// check for config display flag
if(cmdLineData.configDisplayFlag)
{
// display config display flag
// fct: displayConfigData
displayConfigData(configDataPtr);
}
// set config upload success flag
configUploadSuccess = true;
}
// otherwise, assume config file upload failure
else
{
// show error message, end program
// fct: printf
printf("\nConfig Upload Error: %s, program aborted\n\n", errorMsg);
}
// check for config success and need for metadata
if(configUploadSuccess &&
(cmdLineData.mdDisplayFlag || cmdLineData.runSimFlag))
{
// upload metadata file, check for success
// fct: getMetaData
if(getMetaData(configDataPtr->metaDataFileName,
&metaDataPtr, errorMsg))
{
// check metadata display flag
if(cmdLineData.mdDisplayFlag)
{
// display metadata
// fct: displayMetaData
displayMetaData(metaDataPtr);
}
// check run simulator flag
if(cmdLineData.runSimFlag)
{
// run simulator
// fct: runSim
runSim(configDataPtr, metaDataPtr);
}
}
// otherwise, assume metadata upload failure
else
{
// show error message, end program
// fct: printf
printf("\nMetadata Upload Error: %s, program aborted\n",
errorMsg);
}
}
// end check for config success and need for metadata
// clean up config data as needed
// fct: clearConfigData
configDataPtr = clearConfigData(configDataPtr);
// clean up metadata as needed
// fct: clearMetaDataList
metaDataPtr = clearMetaDataList(metaDataPtr);
}
// end process command line arguments
// otherwise, assume command line failure
else
{
// show command line argument requirements
// fct: showCommandLineFormat
showCommandLineFormat();
}
// show program end
// fct: printf
printf("\nSimulator Program End.\n");
// return program success
return 0;
}
/*
Name: clearCmdLineStruct
Process: sets command line structure data to defaults
Booleans to false, fileName to empty string
Function Input/Parameters: pointer to command line structure (CmdLineData *)
Function Output/Parameters: pointer to command line structure (CmdLineData *)
with updated members
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: none
*/
void clearCmdLineStruct(CmdLineData *clDataPtr)
{
// set all struct members to default
clDataPtr->programRunFlag = false;
clDataPtr->configDisplayFlag = false;
clDataPtr->mdDisplayFlag = false;
clDataPtr->runSimFlag = false;
clDataPtr->fileName[0] = NULL_CHAR;
// void function, no return.
}
/*
Name: processCmdLine
Process: checks for at least two arguments,
then sets Booleans depending on command line switches
which can be in any order,
also captures config file name which must be last arugment
Function Input/Parameters: number of arguments (int),
vector of arguments (char **)
Function Output/Parameters: pointer to command line structure (CmdLineData *)
with updated members,
set to default values if failure
to capture arguments
Function Output/Returned: Boolean result of argument capture
true if at least one switch and config file name,
false otherwise
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
bool processCmdLine(int numArgs, char **strVector, CmdLineData *clDataPtr)
{
// initialize function and variables
// initialize structure to defaults
// fct: clearCmdLineStruct
clearCmdLineStruct(clDataPtr);
// initialize success flags to false
bool atLeastOneSwitchFlag = false;
bool correctConfigFileFlag = false;
// initialize first arg index to one
int argIndex = 1;
// iniitialize file variables
int fileStrLen, fileStrSubLoc;
// must have program name, one switch, config file name at minimum
if(numArgs >= MIN_NUM_ARGS)
{
// loop across args (starting at 1) and program run flag
while(argIndex < numArgs)
{
// check for -dc
if(compareString(strVector[argIndex], "-dc") == 0)
{
// set config display flag
clDataPtr->configDisplayFlag = true;
// set at least one switch flag
atLeastOneSwitchFlag = true;
}
// otherwise, check for -dm
else if(compareString(strVector[argIndex], "-dm") == 0)
{
// set metadata display flag
clDataPtr->mdDisplayFlag = true;
// set at least one switch flag
atLeastOneSwitchFlag = true;
}
// otherwise, check for -rs
else if(compareString(strVector[argIndex], "-rs") == 0)
{
// set run simulator flag
clDataPtr->runSimFlag = true;
// set at least one switch flag
atLeastOneSwitchFlag = true;
}
// otherwise, check for file name, ending in .cfg (must be last)
else
{
// find lengths to verify file name
// file name must be last argument, have '.cnf' extension
fileStrLen = getStringLength(strVector[numArgs - 1]);
fileStrSubLoc = findSubString(strVector[numArgs - 1], ".cnf");
// check for file existence and correct file format
if(fileStrSubLoc != SUBSTRING_NOT_FOUND &&
fileStrSubLoc == fileStrLen - LAST_FOUR_LETTERS)
{
// set file name to variable
copyString(clDataPtr->fileName, strVector[numArgs - 1]);
// set success flag to true
correctConfigFileFlag = true;
}
// otherwise, assume bad config file name
else
{
// reset struct, correct config file flag stays false
// fct: clearCmdLineStruct
clearCmdLineStruct(clDataPtr);
}
}
// update arg index
argIndex = argIndex + 1;
}
// end loop across args and program run flag
}
// return verification of at least one switch and file name
return atLeastOneSwitchFlag && correctConfigFileFlag;
}
/*
Name: showCommandLineFormat
Process: displays command line format as assistance to user
Function Input/Parameters: none
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Monitor: data displayed as spec'd
Dependencies: printf
*/
void showCommandLineFormat()
{
// display command line format
// fct: printf
printf("Command Line Format:\n");
printf(" sim_01 [-dc] [-dm] [-rs] <configfile.cnf>\n");
printf(" -dc [optional] displays configuration data\n");
printf(" -dm [optional] displays metadata\n");
printf(" -rs [optional] runs the simulator\n");
printf(" required configfile.cnf\n");
// void function, no return.
}

74
Sim01/OS_SimDriver.h Normal file
View file

@ -0,0 +1,74 @@
#ifndef OS_DRIVER
#define OS_DRIVER
// header files
#include <stdio.h>
#include "StringUtils.h"
#include "configops.h"
#include "metadataops.h"
#include "simulator.h"
// ??? #include "datatypes.h"
// program constants
typedef enum {SUBSTRING_NOT_FOUND = 0, MIN_NUM_ARGS = 3, LAST_FOUR_LETTERS = 4} PRG_CONSTANTS;
typedef struct CmdLineDataStruct
{
bool programRunFlag;
bool configDisplayFlag;
bool mdDisplayFlag;
bool runSimFlag;
char fileName[STD_STR_LEN];
} CmdLineData;
// function prototypes
/*
Name: clearCmdLineStruct
Process: sets command line structure data to defaults
Booleans to false, fileName to empty string
Function Input/Parameters: pointer to command line structure (CmdLineData *)
Function Output/Parameters: pointer to command line structure (CmdLineData *)
with updated members
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: none
*/
void clearCmdLineStruct(CmdLineData *clDataPtr);
/*
Name: processCmdLine
Process: checks for at least two arguments,
then sets Booleans depending on command line switches
which can be in any order,
also captures config file name which must be last arugment
Function Input/Parameters: number of arguments (int),
vector of arguments (char **)
Function Output/Parameters: pointer to command line structure (CmdLineData *)
with updated members,
set to default values if failure
to capture arguments
Function Output/Returned: Boolean result of argument capture
true if at least one switch and config file name,
false otherwise
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
bool processCmdLine(int numArgs, char **strVector, CmdLineData *clDataPtr);
/*
Name: showCommandLineFormat
Process: displays command line format as assistance to user
Function Input/Parameters: none
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Monitor: data displayed as spec'd
Dependencies: printf
*/
void showCommandLineFormat();
#endif //OS_DRIVER

73
Sim01/StandardConstants.h Normal file
View file

@ -0,0 +1,73 @@
// Preprocessor directives
#ifndef STANDARD_CONSTANTS_H
#define STANDARD_CONSTANTS_H
// Include Boolean
#include <stdbool.h>
// global constants
// constant for end of c-string null character
#define CARRIAGE_RETURN_CHAR '\r'
//constant for a COLON
#define COLON ':'
//constant for a COMMA
#define COMMA ','
//Constant to print a dash
#define DASH '-'
// highest printable character
#define HIGHEST_PRINTABLE_CHAR '~'
// constant for huge string length
#define HUGE_STR_LEN 256
// lowest printable character
#define LOWEST_PRINTABLE_CHAR ' '
// constant for small string length
#define MIN_STR_LEN 32
// constant for maximum string length
#define MAX_STR_LEN 128
// constant for end of c-string null character
#define NEWLINE_CHAR '\n'
// first non-printable above ASCII
#define DELETE_KEY 127
// constant for end of c-string null character
#define NULL_CHAR '\0'
// columns found in grade sheet before grading columns
#define NUM_BASE_COLS 5
// constant for number of grading frequency bins
#define NUM_FREQUENCY_BINS 10
//Constant for printing a pipe
#define PIPE '|'
//Constant for a period
#define PERIOD '.'
//Constant for PRECISION, 2 digits
#define PRECISION_2 2
//Constant for PRECISION, 4 digits
#define PRECISION_4 4
// constant for space character
#define SPACE ' '
// constant for standard string length
#define STD_STR_LEN 64
// constant for smallest string length
#define TINY_STR_LEN 16
#endif // STANDARD_CONSTANTS

454
Sim01/StringUtils.c Normal file
View file

@ -0,0 +1,454 @@
// header files
#include "StringUtils.h"
// global constants
const int SUBSTRING_NOT_FOUND = 0;
// list of utils:
// string length
// getStringLength
// string copy
// copyString
// string concatenate
// catString
// string compare (<, >, =)
// compareString
// string substring get
// getSubString
// string substring find
// findSubString
// string set to lower
// toLowerCase
// string get to delimiter
// stringDelimiter
// string get to end of line
// stringEOL
// function implementations
/*
Name: catString
Process: concatenates two strings
Function input/parameters: c-style string (char *)
Function output/parameters: c-style string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: malloc, free, getStringLength
*/
void catString(char *outString, const char *inString)
{
// initialize variables
// set dest index
int destIndex = getStringLength(outString);
// set source str length
int sourceStrLen = getStringLength(inString);
// create temp string pointer
char *tempString;
int sourceIndex = 0;
// copy source string in case of aliasing
tempString = (char *)malloc(sizeof(char) * sourceStrLen);
copyString(tempString, inString);
// loop to end of source string
while(tempString[sourceIndex] != NULL_CHAR)
{
// assign characters to end of destination string
outString[destIndex] = inString[sourceIndex];
// update indices
destIndex = destIndex + 1;
sourceIndex = sourceIndex + 1;
}
// end loop to end of source string
// set temporary end of destination string
outString[destIndex] = NULL_CHAR;
// release memory used for temp string
free(tempString);
// no return, void.
}
/*
Name: compareString
Process: compares two strings as follows:
- if left string is alphabetically greater than the right string,
returns value greater than zero
- if left string is alphabetically less than the right string,
returns value less than zero
- if strings are alphabetically equal but one is longer,
longer one is greater
- otherwise, returns zero
Function input/parameters: c-style strings (char *) [first and second/left&right]
Function output/parameters: NONE
Function output/returned: results as specfifed above (int)
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength
*/
int compareString(const char *firstString, const char *secondString)
{
// initialize variables
int diff, index = 0;
// loop to end of string (within bounds)
while(firstString[index] != NULL_CHAR && secondString[index] != NULL_CHAR)
{
// get diff of characters
diff = firstString[index] - secondString[index];
// check diff of characters
if(diff != 0)
{
// return diff
return diff;
}
// increment index
index = index + 1;
}
// end loop to end of string
// return difference in length of strings, if needed
return getStringLength(firstString) - getStringLength(secondString);
}
/*
Name: copyString
Process: copies string into new string variable, overwrites destination.
Function input/parameters: c-style string (char *)
Function output/parameters: c-style string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength
*/
void copyString(char *outString, const char *inString)
{
// initialize variables
int index = 0;
// check to see if outString is not the same as inString.
if(outString != inString)
{
// loop to end of inString
while(inString[index] != NULL_CHAR)
{
// assign characters to end of outString
outString[index] = inString[index];
// increment index
index = index + 1;
}
// end loop to end of inString
// set end of the outString
outString[index] = NULL_CHAR;
}
// no return, void.
}
/*
Name: findSubString
Process: linear search for given substring within another string
Function input/parameters: c-style test string (char *),
c-style source string (char *)
Function output/parameters: NONE
Function output/returned: index of found substring, or
SUBSTRING_NOT_FOUND constant
if string not found
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength
*/
int findSubString(const char *testStr, const char *searchSubStr)
{
// initialize variables
// initialize test string length
int testStrLength = getStringLength(testStr);
// initialize master index (location of substring start point)
int masterIndex = 0;
int searchIndex, internalIndex;
// loop across testStr
while(masterIndex < testStrLength)
{
// set internal loop index to current test string index
internalIndex = masterIndex;
// set internal search index to zero
searchIndex = 0;
// loop to end of testStr
// while testStr/searchSubStr characters are the same
while(internalIndex <= testStrLength &&
testStr[internalIndex] == searchSubStr[searchIndex])
{
// increment testStr and substring indices
internalIndex = internalIndex + 1;
searchIndex = searchIndex + 1;
// check for end of substring (search complete)
if(searchSubStr[searchIndex] == NULL_CHAR)
{
// return beginning location of substring
return masterIndex;
}
}
// end while testStr/searchSubStr characters are the same
// increment current location starting index
masterIndex = masterIndex + 1;
}
// end loop across testStr
// return SUBSTRING_NOT_FOUND
return SUBSTRING_NOT_FOUND;
}
/*
Name: getStringConstrained
Process: captures a string from the file stream with various constraints
Function input/parameters: input stream (FILE *),
clears leading non printable (bool),
clears leading space (bool),
stops at non printable (bool),
stops at specified delimiter (char)
will consume the delimiter.
Function output/parameters: c-style string (char *)
Function output/returned: success of operation (bool)
Device input/---: NONE
Device output/---: NONE
Dependencies: fgetc
*/
bool getStringConstrained(FILE *inStream, bool clearLeadingNonPrintable,
bool clearLeadingSpace, bool stopAtNonPrintable,
char delimiter, char *capString)
{
// initialize variables
int intChar = EOF, index = 0;
// initialize output string
capString[index] = NULL_CHAR;
// capture first value in stream using fgetc
intChar = fgetc(inStream);
// loop to clear non-printable or space if indicated
while((((intChar != EOF) &&
(clearLeadingNonPrintable && (intChar < (int)SPACE)))
|| (clearLeadingSpace && (intChar == (int)SPACE))))
{
// get next character in stream using fgetc
intChar = fgetc(inStream);
}
// end loop to clear non-printable or space if indicated
// check for end of file found
if(intChar == EOF)
{
// return false (failed)
return false;
}
// loop to get input from file stream
while(((intChar != EOF && index < MAX_STR_LEN - 1) &&
(stopAtNonPrintable && intChar >= (int)SPACE))
|| ((!stopAtNonPrintable) && (intChar != (char)delimiter)))
{
// [continues if not at end of file and max string len not reached ]
// [AND continues if not printable flag set and characters printable ]
// [OR continues if not printable flag not set ]
// [AND continues if specified delimited is not found ]
// place character in array element
capString[index] = (char)intChar;
// increment array index
index = index + 1;
// set next element to null character
capString[index] = NULL_CHAR;
// get next character as integer using fgetc
intChar = fgetc(inStream);
}
// end loop to get input from file stream
// return success
return true;
}
/*
Name: getStringLength
Process: gets length of string from start to NULL_CHAR ('\0')
Function input/parameters: c-style string (char *)
Function output/parameters: NONE
Function output/returned: length of string (int)
Device input/---: NONE
Device output/---: NONE
Dependencies: included by standard libraries
*/
int getStringLength(const char *stringIn)
{
// define variables
int stringLength = 0, currentCharacter = 0;
// get user input (not needed)
// do calculations
while (currentCharacter < STD_STR_LEN && stringIn[currentCharacter] != NULL_CHAR)
{
// increment string length
stringLength = stringLength + 1;
// increment currentCharacter
currentCharacter = currentCharacter + 1;
}
// display results (not needed)
// return success
return stringLength;
}
/*
Name: getStringToDelimiter
Process: captures a string from the input stream to a specified delimiter.
will consume the delimiter.
Function input/parameters: input stream (FILE *),
stops at specified character
Function output/parameters: c-style string (char *)
Function output/returned: success of operation (bool)
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringConstrained
*/
bool getStringToDelimiter(FILE *inStream, char delimiter, char *capString)
{
// use getStringConstrained with delimiter to get string
return getStringConstrained(inStream, true, true, true, delimiter, capString);
}
/*
Name: getStringToLineEnd
Process: captures a string from the input stream to the end of the line
Function input/parameters: input file stream (FILE *)
Function output/parameters: c-style string (char *)
Function output/returned: success of operation (bool)
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringConstrained
*/
bool getStringToLineEnd(FILE *inStream, char *capString)
{
// use getStringConstrained with line end character to get string
return getStringConstrained(inStream, true, true, true, '\n', capString);
}
/*
Name: getSubString
Process: captures sub string within larger string
between two inclusive indices.
returns empty string if either index is out of range,
assumes enough memory in dest string
Function input/parameters: c-style string (char *)
start and end indices (int)
Function output/parameters: c-style destination string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength, malloc, copyString, free
*/
void getSubString(char *destStr, const char *sourceStr,
int startIndex, int endIndex)
{
// initialize variables
// set length of source string using getStringLength
int sourceStrLen = getStringLength(sourceStr);
// initialize the destination index to zero
int destIndex = 0;
// initialize source index to start index (parameter)
int sourceIndex = startIndex;
// create pointer for temp string
char *tempStr;
// check for indices within limits
if(startIndex >= 0 && startIndex <= endIndex && endIndex < sourceStrLen)
{
// create temp string using malloc and copyString
tempStr = (char *)malloc(sourceStrLen + 1);
copyString(tempStr, sourceStr);
// loop across requested substring indices
while(sourceIndex <= endIndex)
{
// assign source character to destination element
destStr[destIndex] = tempStr[sourceIndex];
// increment indices
destIndex = destIndex + 1;
sourceIndex = sourceIndex + 1;
// set temporary end of destination string
destStr[destIndex] = NULL_CHAR;
}
// end loop across requested substring indices
// free temp string
free(tempStr);
}
// no return, void.
}
/*
Name: strToLowerCase
Process: iterates through string, sets any uppercase letter to lowercase
equivalent, does not affect any letter with an upper/lower diff.
Function input/parameters: c-style source string (char *)
Function output/parameters: c-style dest string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: toLowerCase
*/
void strToLowerCase(char *outString, const char *inString)
{
// initialize variables
// get source string length
int sourceStrLen = getStringLength(inString);
// create temporary string pointer
char *tempStr;
int index = 0;
// copy source string in case of aliasing using malloc and copyString
tempStr = (char *)malloc((sizeof(char) * (sourceStrLen + 1)));
copyString(tempStr, inString);
// loop across source string
while(tempStr[index] != NULL_CHAR && index < MAX_STR_LEN)
{
// set individual character to lowercase as needed, assigning back
outString[index] = toLowerCase(tempStr[index]);
// update index
index = index + 1;
// set temporary end of destination string
outString[index] = NULL_CHAR;
}
// end loop across source string
// free temporary string
free(tempStr);
// no return, void.
}
/*
Name: toLowerCase
Process: sets character to lowercase letter if uppercase letter,
returns character
Function input/parameters: character input (char)
Function output/parameters: NONE
Function output/returned: character output (char)
Device input/---: NONE
Device output/---: NONE
Dependencies: none
*/
char toLowerCase(char testChar)
{
// check for uppercase letter
if(testChar >= 'A' && testChar <= 'Z')
{
// return lowercase letter
return testChar - 'A' + 'a';
}
// otherwise, assume not uppercase letter, return unchanged
return testChar;
}
/*
Name: ???
Process: ???
Function input/parameters: ???
Function output/parameters: NONE
Function output/returned: ???
Device input/---: NONE
Device output/---: NONE
Dependencies: included by standard libraries
*/

176
Sim01/StringUtils.h Normal file
View file

@ -0,0 +1,176 @@
#ifndef STRINGUTILS
#define STRINGUTILS
// header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include "StandardConstants.h"
// data structures
// program constants
// function prototypes
/*
Name: catString
Process: concatenates two strings
Function input/parameters: c-style string (char *)
Function output/parameters: c-style string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: malloc, free, getStringLength
*/
void catString(char *outString, const char *inString);
/*
Name: compareString
Process: compares two strings as follows:
- if left string is alphabetically greater than the right string,
returns value greater than zero
- if left string is alphabetically less than the right string,
returns value less than zero
- if strings are alphabetically equal but one is longer,
longer one is greater
- otherwise, returns zero
Function input/parameters: c-style strings (char *) [first and second/left&right]
Function output/parameters: NONE
Function output/returned: results as specfifed above (int)
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength
*/
int compareString(const char *firstString, const char *secondString);
/*
Name: copyString
Process: copies string into new string variable, overwrites destination.
Function input/parameters: c-style string (char *)
Function output/parameters: c-style string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength
*/
void copyString(char *outString, const char *inString);
/*
Name: findSubString
Process: linear search for given substring within another string
Function input/parameters: c-style test string (char *),
c-style source string (char *)
Function output/parameters: NONE
Function output/returned: index of found substring, or
SUBSTRING_NOT_FOUND constant
if string not found
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength
*/
int findSubString(const char *testStr, const char *searchSubStr);
/*
Name: getStringConstrained
Process: captures a string from the file stream with various constraints
Function input/parameters: input stream (FILE *),
clears leading non printable (bool),
clears leading space (bool),
stops at non printable (bool),
stops at specified delimiter (char)
will consume the delimiter.
Function output/parameters: c-style string (char *)
Function output/returned: success of operation (bool)
Device input/---: NONE
Device output/---: NONE
Dependencies: fgetc
*/
bool getStringConstrained(FILE *inStream, bool clearLeadingNonPrintable,
bool clearLeadingSpace, bool stopAtNonPrintable,
char delimiter, char *capString);
/*
Name: getStringLength
Process: gets length of string from start to NULL_CHAR ('\0')
Function input/parameters: c-style string (char *)
Function output/parameters: NONE
Function output/returned: length of string (int)
Device input/---: NONE
Device output/---: NONE
Dependencies: included by standard libraries
*/
int getStringLength(const char *stringIn);
/*
Name: getStringToDelimiter
Process: captures a string from the input stream to a specified delimiter.
will consume the delimiter.
Function input/parameters: input stream (FILE *),
stops at specified character
Function output/parameters: c-style string (char *)
Function output/returned: success of operation (bool)
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringConstrained
*/
bool getStringToDelimiter(FILE *inStream, char delimiter, char *capString);
/*
Name: getStringToLineEnd
Process: captures a string from the input stream to the end of the line
Function input/parameters: input file stream (FILE *)
Function output/parameters: c-style string (char *)
Function output/returned: success of operation (bool)
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringConstrained
*/
bool getStringToLineEnd(FILE *inStream, char *capString);
/*
Name: getSubString
Process: captures sub string within larger string
between two inclusive indices.
returns empty string if either index is out of range,
assumes enough memory in dest string
Function input/parameters: c-style string (char *)
start and end indices (int)
Function output/parameters: c-style destination string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength, malloc, copyString, free
*/
void getSubString(char *destStr, const char *sourceStr,
int startIndex, int endIndex);
/*
Name: strToLowerCase
Process: iterates through string, sets any uppercase letter to lowercase
equivalent, does not affect any letter with an upper/lower diff.
Function input/parameters: c-style source string (char *)
Function output/parameters: c-style dest string (char *)
Function output/returned: NONE
Device input/---: NONE
Device output/---: NONE
Dependencies: toLowerCase
*/
void strToLowerCase(char *outString, const char *inString);
/*
Name: toLowerCase
Process: sets character to lowercase letter if uppercase letter,
returns character
Function input/parameters: character input (char)
Function output/parameters: NONE
Function output/returned: character output (char)
Device input/---: NONE
Device output/---: NONE
Dependencies: none
*/
char toLowerCase(char testChar);
#endif // STRINGUTILS

53
Sim01/configops.c Normal file
View file

@ -0,0 +1,53 @@
#include "configops.h"
#include "StandardConstants.h"
#include "StringUtils.h"
/*
Name: clearConfigData
Process: frees dynamically allocated config data structure
if it has not already been freed
Function Input/Parameters: pointer to config data structure (ConfigDataType *)
Function Output/Parameters: none
Function Output/Returned: NULL (ConfigDataType *)
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
ConfigDataType *clearConfigData(ConfigDataType *configData)
{
// return NULL (temp)
return NULL;
}
/*
Name: displayConfigData
Process: screen dump/display of all config data
Function Input/Parameters: pointer to config data structure (ConfigDataType *)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Monitor: displayed as spec'd
Dependencies: ???
*/
void displayConfigData(ConfigDataType *configData)
{
// void function, no return.
}
/*
Name: getConfigData
Process: driver function for capturing config data from config file
Function Input/Parameters: file name (const char *)
Function Output/Parameters: pointer to config data pointer (ConfigDataType **)
end/result state message pointer (char *)
Function Output/Returned: Boolean result of data access operation (bool)
Device Input/File: config data uploaded
Device Output/Device: none
Dependencies: ???
*/
bool getConfigData(const char *fileName,
ConfigDataType **configData, char *endStateMsg)
{
// return false (temp)
return false;
}

50
Sim01/configops.h Normal file
View file

@ -0,0 +1,50 @@
#ifndef CONFIGOPS
#define CONFIGOPS
#include "StandardConstants.h"
typedef struct ConfigDataTypeStruct
{
char metaDataFileName[STD_STR_LEN];
} ConfigDataType;
/*
Name: clearConfigData
Process: frees dynamically allocated config data structure
if it has not already been freed
Function Input/Parameters: pointer to config data structure (ConfigDataType *)
Function Output/Parameters: none
Function Output/Returned: NULL (ConfigDataType *)
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
ConfigDataType *clearConfigData(ConfigDataType *configData);
/*
Name: displayConfigData
Process: screen dump/display of all config data
Function Input/Parameters: pointer to config data structure (ConfigDataType *)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Monitor: displayed as spec'd
Dependencies: ???
*/
void displayConfigData(ConfigDataType *configData);
/*
Name: getConfigData
Process: driver function for capturing config data from config file
Function Input/Parameters: file name (const char *)
Function Output/Parameters: pointer to config data pointer (ConfigDataType **)
end/result state message pointer (char *)
Function Output/Returned: Boolean result of data access operation (bool)
Device Input/File: config data uploaded
Device Output/Device: none
Dependencies: ???
*/
bool getConfigData(const char *fileName,
ConfigDataType **configData, char *endStateMsg);
#endif // CONFIGOPS

56
Sim01/metadataops.c Normal file
View file

@ -0,0 +1,56 @@
#include "metadataops.h"
#include "StandardConstants.h"
#include "StringUtils.h"
/*
Name: clearMetaDataList
Process: recursively traverses list, frees dynamically allocated nodes
Function Input/Parameters: node opcode (const OpCodeType *)
Function Output/Parameters: none
Function Output/Returned: NULL (OpCodeType *)
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
OpCodeType *clearMetaDataList(OpCodeType *localPtr)
{
// return NULL (temp)
return NULL;
}
/*
Name: displayMetaData
Process: data dump/display of all opcode items
Function Input/Parameters: pointer to
head of opcode/metadata list (const OpCodeType *)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void displayMetaData(const OpCodeType *localPtr)
{
// void function, no return.
}
/*
Name: getMetaData
Process: main driver function to upload, parse, and store list
of op code commands in a linked list
Function Input/Parameters: file name (const char *)
Function Output/Parameters: ptr to opcode linked list head ptr (OpCodeType **),
result msg of fct state after completion (char *)
Function Output/Returned: Boolean result of operation (bool)
Device Input/File: op code list uploaded
Device Output/Device: none
Dependencies: copyString fopen, getStringToDelimiter, compareString, fclose,
malloc, getOpCommand, updateStartCount, updateEndCount,
clearMetaDataList, free, addNode
*/
bool getMetaData(const char *fileName,
OpCodeType **opCodeDataHead, char *endStateMsg)
{
// return false (temp)
return false;
}

53
Sim01/metadataops.h Normal file
View file

@ -0,0 +1,53 @@
#ifndef METADATAOPS
#define METADATAOPS
#include "StandardConstants.h"
typedef struct OpCodeTypeStruct
{
int temp;
} OpCodeType;
/*
Name: clearMetaDataList
Process: recursively traverses list, frees dynamically allocated nodes
Function Input/Parameters: node opcode (const OpCodeType *)
Function Output/Parameters: none
Function Output/Returned: NULL (OpCodeType *)
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
OpCodeType *clearMetaDataList(OpCodeType *localPtr);
/*
Name: displayMetaData
Process: data dump/display of all opcode items
Function Input/Parameters: pointer to
head of opcode/metadata list (const OpCodeType *)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void displayMetaData(const OpCodeType *localPtr);
/*
Name: getMetaData
Process: main driver function to upload, parse, and store list
of op code commands in a linked list
Function Input/Parameters: file name (const char *)
Function Output/Parameters: ptr to opcode linked list head ptr (OpCodeType **),
result msg of fct state after completion (char *)
Function Output/Returned: Boolean result of operation (bool)
Device Input/File: op code list uploaded
Device Output/Device: none
Dependencies: copyString fopen, getStringToDelimiter, compareString, fclose,
malloc, getOpCommand, updateStartCount, updateEndCount,
clearMetaDataList, free, addNode
*/
bool getMetaData(const char *fileName,
OpCodeType **opCodeDataHead, char *endStateMsg);
#endif // METADATAOPS

29
Sim01/sim01_mf Normal file
View file

@ -0,0 +1,29 @@
CC = gcc -Wall -std=c99 -pedantic -g
SOURCES = StringUtils.c simulator.c metadataops.c configops.c OS_SimDriver.c
OBJECTS = OS_SimDriver.o simulator.o metadataops.o configops.o StringUtils.o
all: Simulator
Simulator: $(OBJECTS)
$(CC) $(OBJECTS) -o sim01
StringUtils.o: StringUtils.c
$(CC) -c StringUtils.c -o StringUtils.o
simulator.o: simulator.c
$(CC) -c simulator.c -o simulator.o
metadataops.o: metadataops.c
$(CC) -c metadataops.c -o metadataops.o
configops.o: configops.c
$(CC) -c configops.c -o configops.o
OS_SimDriver.o: OS_SimDriver.c
$(CC) -c OS_SimDriver.c -o OS_SimDriver.o
clean:
rm -f $(OBJECTS) sim01 sim01.out
clean_objects:
rm -f $(OBJECTS)

19
Sim01/simulator.c Normal file
View file

@ -0,0 +1,19 @@
#include "configops.h"
#include "metadataops.h"
#include "StandardConstants.h"
/*
Name: runSim
Process: primary simulation driver
Function Input/Parameters: configuration data (ConfigDataType *),
metadata (OpCodeType *)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
{
// void function, no return.
}

19
Sim01/simulator.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef SIMULATORX
#define SIMULATORX
#include "StandardConstants.h"
/*
Name: runSim
Process: primary simulation driver
Function Input/Parameters: configuration data (ConfigDataType *),
metadata (OpCodeType *)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr);
#endif // SIMULATORX

BIN
Sim01_GradingForm_v01.xlsx Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
SimTimerCode.zip Normal file

Binary file not shown.

231
SimTimerCode/simtimer.c Normal file
View file

@ -0,0 +1,231 @@
// Program Information ////////////////////////////////////////////////////////
/**
* @file SimpleTimer.c
*
* @brief Implementation file for using a timer with micro-second precision
*
* @author Michael Leverington
*
* @details Implements member methods for timing
*
* @version 3.00 (02 February 2017) Update to simulator timer
2.00 (13 January 2017) Update to C language
* 1.00 (11 September 2015)
*
* @Note Requires SimpleTimer.h.
*
*/
// Precompiler directives /////////////////////////////////////////////////////
#ifndef SIMTIMER_C
#define SIMTIMER_C
// Header files ///////////////////////////////////////////////////////////////
#include "simtimer.h"
// Constants /////////////////////////////////////////////////////////////////
const char RADIX_POINT = '.';
void runTimer( int milliSeconds )
{
struct timeval startTime, endTime;
int startSec, startUSec, endSec, endUSec;
int uSecDiff, mSecDiff, secDiff, timeDiff;
gettimeofday( &startTime, NULL );
startSec = startTime.tv_sec;
startUSec = startTime.tv_usec;
timeDiff = 0;
while( timeDiff < milliSeconds )
{
gettimeofday( &endTime, NULL );
endSec = endTime.tv_sec;
endUSec = endTime.tv_usec;
uSecDiff = endUSec - startUSec;
if( uSecDiff < 0 )
{
uSecDiff = uSecDiff + 1000000;
endSec = endSec - 1;
}
mSecDiff = uSecDiff / 1000;
secDiff = ( endSec - startSec ) * 1000;
timeDiff = secDiff + mSecDiff;
}
}
double accessTimer( int controlCode, char *timeStr )
{
static Boolean running = False;
static int startSec = 0, endSec = 0, startUSec = 0, endUSec = 0;
static int lapSec = 0, lapUSec = 0;
struct timeval startData, lapData, endData;
double fpTime = 0.0;
switch( controlCode )
{
case ZERO_TIMER:
gettimeofday( &startData, NULL );
running = True;
startSec = startData.tv_sec;
startUSec = startData.tv_usec;
fpTime = 0.000000000;
lapSec = 0.000000000;
lapUSec = 0.000000000;
timeToString( lapSec, lapUSec, timeStr );
break;
case LAP_TIMER:
if( running == True )
{
gettimeofday( &lapData, NULL );
lapSec = lapData.tv_sec;
lapUSec = lapData.tv_usec;
fpTime = processTime( startSec, lapSec,
startUSec, lapUSec, timeStr );
}
else
{
fpTime = 0.000000000;
}
break;
case STOP_TIMER:
if( running == True )
{
gettimeofday( &endData, NULL );
running = False;
endSec = endData.tv_sec;
endUSec = endData.tv_usec;
fpTime = processTime( startSec, endSec,
startUSec, endUSec, timeStr );
}
// assume timer not running
else
{
fpTime = 0.000000000;
}
break;
}
return fpTime;
}
double processTime( double startSec, double endSec,
double startUSec, double endUSec, char *timeStr )
{
double secDiff = endSec - startSec;
double uSecDiff = endUSec - startUSec;
double fpTime;
fpTime = (double) secDiff + (double) uSecDiff / 1000000;
if( uSecDiff < 0 )
{
uSecDiff = uSecDiff + 1000000;
secDiff = secDiff - 1;
}
timeToString( secDiff, uSecDiff, timeStr );
return fpTime;
}
/* This is a bit of a drawn-out function, but it is written
to force the time result to always be in the form x.xxxxxxx
when printed as a string; this will not always be the case
if the time is presented as a floating point number
*/
void timeToString( int secTime, int uSecTime, char *timeStr )
{
int low, high, index = 0;
char temp;
while( uSecTime > 0 )
{
timeStr[ index ] = (char) ( uSecTime % 10 + '0' );
uSecTime /= 10;
index++;
}
while( index < 6 )
{
timeStr[ index ] = '0';
index++;
}
timeStr[ index ] = RADIX_POINT;
index++;
if( secTime < 10 ) // single digit in display
{
timeStr[ index ] = (char) ( secTime % 10 + '0' );
index++;
timeStr[ index ] = SPACE;
index++;
secTime = 0;
}
/*
if( secTime == 0 )
{
timeStr[ index ] = '0';
index++;
}
*/
while( secTime > 0 )
{
timeStr[ index ] = (char) ( secTime % 10 + '0' );
secTime /= 10;
index++;
}
timeStr[ index ] = NULL_CHAR;
low = 0; high = index - 1;
while( low < high )
{
temp = timeStr[ low ];
timeStr[ low ] = timeStr[ high ];
timeStr[ high ] = temp;
low++; high--;
}
}
#endif // ifndef SIMTIMER_C

45
SimTimerCode/simtimer.h Normal file
View file

@ -0,0 +1,45 @@
// Program Information ////////////////////////////////////////////////////////
/**
* @file SimpleTimer.h
*
* @brief Header file for micro-second precision timer
*
* @author Michael Leverington
*
* @details Specifies all member methods of the SimpleTimer
*
* @version 2.00 (13 January 2017)
* 1.00 (11 September 2015)
*
* @Note None
*/
// Precompiler directives /////////////////////////////////////////////////////
#ifndef SIMTIMER_H
#define SIMTIMER_H
// Header files ///////////////////////////////////////////////////////////////
#include <sys/time.h>
#include <string.h>
#include <math.h>
#include "mainprog.h"
// Global Constants //////////////////////////////////////////////////////////
enum TIMER_CTRL_CODES { ZERO_TIMER, LAP_TIMER, STOP_TIMER };
extern const char RADIX_POINT;
extern const char SPACE;
// Function Prototyp /////////////////////////////////////////////////////////
void runTimer( int milliSeconds );
double accessTimer( int controlCode, char *timeStr );
double processTime( double startSec, double endSec,
double startUSec, double endUSec, char *timeStr );
void timeToString( int secTime, int uSecTime, char *timeStr );
#endif // ifndef SIMTIMER_H