/*
 *  sm_utils.c
 *  Utility functions for the Simpletron simulator
 *
 */

#include "simpletron.h"

/* See documentation for these functions in the simpletron.h file */


long sm_input_word( int loc ) {
    int res = 0;
    long data;
    
    while ( res != 1 ) {
        /* Prints loc address in prompt only if loc >= 0 */
        if ( loc >= 0 ) printf( "%2.2d ? ", loc );
        else            printf( " ? " );
        res = scanf( "%d", &data );
        /* check for invalid input */
        if ( res != 1 ) {
            scanf("%*s");   /* eat up bad input on line */
            res = 0;
        }
        else if ( ( data < MINDVAL || data > MAXDVAL ) && data != EOINPUT ) 
            res = 0;
        
        if ( res == 0 ) sm_msg("Invalid input. Try again.");
    }
    return data;
}


int sm_set_accumulator( int* acc, int val ) {
    if ( val > MAXDVAL || val < MINDVAL ) {
        *acc = val % MAXDVAL;
        return 0;               /* overflow occured */
    } else {
        *acc = val;
    }
    return 1;
}


char sign_of( int v ) {
    return v >= 0 ? '+' : '-';
}
