// ================================================================================================================================= /** * \file index-test.c * \brief Within a simulated space, fill an array and then jump to its locations, making sure its contents are correct. * \author Prof. Scott F. Kaplan * \date Fall 2019 **/ // ================================================================================================================================= // ================================================================================================================================= // INCLUDES #include #include #include #include #include #include #include "vmsim.h" // ================================================================================================================================= // ================================================================================================================================= // CONSTANTS AND MACRO FUNCTIONS /** The number of bytes in an kilobyte. */ #define BYTES_PER_KB 1024 // ================================================================================================================================= // ================================================================================================================================= // GLOBALS #if !defined (IT_DEBUG) static bool debug = false; #else static bool debug = true; #endif // ================================================================================================================================= // ================================================================================================================================= /** * \brief Display the proper usage and end the process with an error code. * \param invocation The command-line text given to run the executable. */ void show_usage_and_exit (char* invocation) { fprintf(stderr, "USAGE: %s \n", invocation); exit(1); } // show_usage_and_exit () // ================================================================================================================================= // ================================================================================================================================= /** * \brief First fill the array, then revisit a fraction of the entries randomly spots that they contain what they should. * \param size The number of entries in the array. */ void go (unsigned int size) { // Use the whole real space as an array. Initialize each entry with its own position. for (int i = 0; i < size; i += 1) { vmsim_addr_t addr = i * sizeof(i); vmsim_write(&i, addr, sizeof(i)); } // Randomly select elements and checking their value. unsigned int reps = size; while (reps > 0) { unsigned int i = random() % size; unsigned int j; vmsim_addr_t addr = i * sizeof(i); vmsim_read(&j, addr, sizeof(j)); if (i != j) { fprintf(stderr, "ERROR: [%d] = %d\n", i, j); } reps -= 1; } } // go () // ================================================================================================================================= // ================================================================================================================================= /** * \brief The entry point to the simulator. * \param argc The length of the command-line argument vector. * \param argv The vector of command-line arguments. * \return the exit code for the process, where 0 indicates success, any other value indicates error. */ int main (int argc, char** argv) { // Check usage. if (argc != 2) { show_usage_and_exit(argv[0]); } // Extract the arguments. errno = 0; unsigned int size = atoi(argv[1]); if (errno != 0 || size < 0) { show_usage_and_exit(argv[0]); } go(size); } // main () // =================================================================================================================================