ARPDAS_QNX6 1.0
cache.c
Go to the documentation of this file.
00001 /* da_cache.c */
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005 #include "subbusd_int.h"
00006 #include "oui.h"
00007 #include "nortlib.h"
00008 
00009 /* *_max_addr is outside the range. This is different
00010    from the command-line option. The adjustment is made
00011    in main() after oui_init_options();
00012 */
00013 static unsigned short hw_min_addr, hw_max_addr;
00014 static unsigned short sw_min_addr, sw_max_addr;
00015 
00016 static unsigned short *hwcache, *swcache;
00017 
00018 char *cache_hw_range, *cache_sw_range;
00019 
00020 /* return:
00021    1 if HW cache (meaning caller should write to HW)
00022    0 if SW cache (meaning done)
00023    -1 if invalid address
00024  */
00025 int sb_cache_write( unsigned short addr, unsigned short data ) {
00026   int cache_addr;
00027   if ( addr < hw_max_addr &&
00028       addr >= hw_min_addr ) {
00029     cache_addr = ( addr - hw_min_addr ) / 2;
00030     hwcache[cache_addr] = data;
00031     nl_error( -3, "Write to HW Cache: %04X -> [%03X]",
00032       data, addr );
00033     return 1;
00034   } else if ( addr < sw_max_addr &&
00035               addr >= sw_min_addr ) {
00036     cache_addr = addr - sw_min_addr;
00037     swcache[cache_addr] = data;
00038     nl_error( -3, "Write to SW Cache: %d -> [%03X]",
00039       data, addr );
00040     return 0;
00041   } else {
00042     return -1;
00043   }
00044 }
00045 
00046 /* return:
00047    1 if HW cache (meaning done)
00048    0 if SW cache (meaning done)
00049    -1 if invalid address
00050  */
00051 int sb_cache_read( unsigned short addr, unsigned short *data ) {
00052   int cache_addr;
00053   if ( addr < hw_max_addr &&
00054       addr >= hw_min_addr ) {
00055     cache_addr = ( addr - hw_min_addr ) / 2;
00056     *data = hwcache[cache_addr];
00057     nl_error( -3, "Read from HW Cache: [%03X] -> 0x%04X",
00058       addr, *data );
00059     return 1;
00060   } else if ( addr < sw_max_addr &&
00061               addr >= sw_min_addr ) {
00062     cache_addr = addr - sw_min_addr;
00063     *data = swcache[cache_addr];
00064     nl_error( -3, "Read from SW Cache: %d <- [%03X]",
00065       *data, addr );
00066     return 0;
00067   } else {
00068     return -1;
00069   }
00070 }
00071 
00072 static void process_range( char *txt,
00073   unsigned short *min, unsigned short *max ) {
00074   if ( txt == NULL ) {
00075     *min = *max = 0;
00076   } else {
00077     char *s, *t;
00078     s = t = txt;
00079     if ( !isxdigit(*t) )
00080       nl_error( 3, "Expected hex digit at start of arg \"%s\"",
00081         txt );
00082     while ( isxdigit(*t) ) t++;
00083     if ( *t++ != '-' )
00084       nl_error( 3, "Expected '-' in arg \"%s\"", txt );
00085     *min = atoh(s);
00086     s = t;
00087     if ( !isxdigit(*t) )
00088       nl_error( 3, "Expected hex digit after '-': \"%s\"",
00089         txt );
00090     while ( isxdigit(*t) ) t++;
00091     if ( *t != '\0' )
00092       nl_error( 3, "Garbage after range: \"%s\"", txt );
00093     *max = atoh(s);
00094     if ( *min > *max )
00095       nl_error( 3, "Invalid range: \"%s\"", txt );
00096     if ( *max > 0xFFFD )
00097       nl_error( 3, "Range max too high: \"%s\"", txt );
00098   }
00099 }
00100 
00101 void sb_cache_init(void) {
00102   process_range( cache_hw_range, &hw_min_addr, &hw_max_addr );
00103   if ( cache_hw_range ) hw_max_addr = (hw_max_addr + 2) & ~1;
00104 
00105   process_range( cache_sw_range, &sw_min_addr, &sw_max_addr );
00106   if ( cache_sw_range ) sw_max_addr++;
00107   
00108   if ( sw_min_addr < hw_max_addr )
00109     nl_error( 3, "-S range must be above -H range" );
00110   
00111   if ( hw_max_addr > hw_min_addr ) {
00112     int n = (hw_max_addr - hw_min_addr)/2;
00113     hwcache = new_memory(n*sizeof(unsigned short));
00114     memset( hwcache, '\0', n*sizeof(unsigned short));
00115     nl_error( -2,
00116       "Established %d words of hardware Cache %03X-%03X",
00117       n, hw_min_addr, hw_max_addr );
00118   }
00119   if ( sw_max_addr > sw_min_addr ) {
00120     int n = (sw_max_addr - sw_min_addr);
00121     swcache = new_memory(n*sizeof(unsigned short));
00122     memset( swcache, '\0', n*sizeof(unsigned short));
00123     nl_error( -2,
00124       "Established %d words of software Cache %03X-%03X",
00125       n, sw_min_addr, sw_max_addr );
00126   }
00127 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines