diff --git a/Makefile b/Makefile index 592fbda..1a870e2 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ dist/hpemung: src/bus.o \ src/keyboard.o \ src/main.o \ src/opcodes.o \ - src/files.o \ + src/persistence.o \ src/ports.o \ src/rpl.o \ src/config.o \ diff --git a/src/bus.c b/src/bus.c index 043b343..58f1a68 100644 --- a/src/bus.c +++ b/src/bus.c @@ -66,36 +66,33 @@ void bus_read( byte* buf, address adr, address len ) while ( true ) { if ( hdw_seg == SEG_OF( adr ) && ( ( bus_info.hdw_base ^ adr ) & 0xFFFC0 ) == 0 ) { n = MIN( len, 0x40 - ( adr & 0x3F ) ); - for ( i = 0; i < n; i++ ) { + for ( i = 0; i < n; i++ ) buf[ i ] = hdw_read_nibble( ( adr & 0x3F ) + i ); - } - if ( ( ( adr & 0x3F ) + n ) == 0x40 ) { + + if ( ( ( adr & 0x3F ) + n ) == 0x40 ) update_crc( buf[ n - 1 ] ); - } } else { - if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) { + if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) n = MIN( len, ( bus_info.hdw_base & 0xFFFC0 ) - adr ); - } else { + else n = MIN( len, 0x1000 - OFFSET_OF( adr ) ); - } - if ( CAN_READ( adr ) ) { + + if ( CAN_READ( adr ) ) memcpy( buf, MAP_READ( adr ), n ); - } else { - for ( i = 0; i < n; i++ ) { + else { + for ( i = 0; i < n; i++ ) buf[ i ] = ( ( i + adr ) & 1 ) ? 0xE : 0xD; - } - if ( bus_info.ce1_bs && bus_info.ce1_cfg && ( ( bus_info.ce1_base ^ adr ) & bus_info.ce1_size ) ) { + + if ( bus_info.ce1_bs && bus_info.ce1_cfg && ( ( bus_info.ce1_base ^ adr ) & bus_info.ce1_size ) ) ports_switch_bank( OFFSET_OF( adr + n ) ); - } } - for ( i = 0; i < n; i++ ) { + for ( i = 0; i < n; i++ ) update_crc( buf[ i ] ); - } } len -= n; - if ( !len ) { + if ( !len ) break; - } + buf += n; adr += n; adr &= 0xFFFFF; @@ -109,33 +106,31 @@ void bus_write( byte* buf, address adr, address len ) while ( true ) { if ( hdw_seg == SEG_OF( adr ) && ( ( bus_info.hdw_base ^ adr ) & 0xFFFC0 ) == 0 ) { n = MIN( len, 0x40 - ( adr & 0x3F ) ); - for ( i = 0; i < n; i++ ) { + for ( i = 0; i < n; i++ ) hdw_write_nibble( buf[ i ], ( adr & 0x3F ) + i ); - } } else { - if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) { + if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) n = MIN( len, ( bus_info.hdw_base & 0xFFFC0 ) - adr ); - } else { + else n = MIN( len, 0x1000 - OFFSET_OF( adr ) ); - } - if ( CAN_WRITE( adr ) ) { + + if ( CAN_WRITE( adr ) ) memcpy( MAP_WRITE( adr ), buf, n ); - } else if ( bus_info.ce1_bs ) { + else if ( bus_info.ce1_bs ) { if ( bus_info.ce1_cfg && ( ( bus_info.ce1_base ^ adr ) & bus_info.ce1_size ) ) { - if ( !bus_info.nce3_r_o ) { + if ( !bus_info.nce3_r_o ) ports_switch_bank( OFFSET_OF( adr + n - 1 ) ); - } else if ( ( adr + n ) & 1 ) { + else if ( ( adr + n ) & 1 ) ports_switch_bank( OFFSET_OF( adr + n - 1 ) ); - } else if ( adr & 0x1 ) { + else if ( adr & 0x1 ) ports_switch_bank( OFFSET_OF( adr ) ); - } } } } len -= n; - if ( !len ) { + if ( !len ) break; - } + buf += n; adr += n; adr &= 0xFFFFF; @@ -149,27 +144,25 @@ static void bus_peek( byte* buf, address adr, address len ) while ( true ) { if ( hdw_seg == SEG_OF( adr ) && ( ( bus_info.hdw_base ^ adr ) & 0xFFFC0 ) == 0 ) { n = MIN( len, 0x40 - ( adr & 0x3F ) ); - for ( i = 0; i < n; i++ ) { + for ( i = 0; i < n; i++ ) buf[ i ] = hdw_read_nibble( ( adr & 0x3F ) + i ); - } + } else { - if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) { + if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) n = MIN( len, ( bus_info.hdw_base & 0xFFFC0 ) - adr ); - } else { + else n = MIN( len, 0x1000 - OFFSET_OF( adr ) ); - } - if ( CAN_READ( adr ) ) { + + if ( CAN_READ( adr ) ) memcpy( buf, MAP_READ( adr ), n ); - } else { - for ( i = 0; i < n; i++ ) { + else + for ( i = 0; i < n; i++ ) buf[ i ] = ( ( i + adr ) & 1 ) ? 0xE : 0xD; - } - } } len -= n; - if ( !len ) { + if ( !len ) break; - } + buf += n; adr += n; adr &= 0xFFFFF; @@ -183,17 +176,16 @@ static void bus_peek_no_hdw( byte* buf, address adr, address len ) while ( true ) { n = MIN( len, 0x1000 - OFFSET_OF( adr ) ); - if ( CAN_READ( adr ) ) { + if ( CAN_READ( adr ) ) memcpy( buf, MAP_READ( adr ), n ); - } else { - for ( i = 0; i < n; i++ ) { + else + for ( i = 0; i < n; i++ ) buf[ i ] = ( ( i + adr ) & 1 ) ? 0xE : 0xD; - } - } + len -= n; - if ( !len ) { + if ( !len ) break; - } + buf += n; adr += n; adr &= 0xFFFFF; @@ -216,62 +208,60 @@ byte* bus_fast_peek( byte* buf, address adr, int* len ) static int tmp_len; address adr2; - if ( !buf ) { + if ( !buf ) buf = tmp_buf; - } + if ( !len ) { tmp_len = FAST_PEEK_MAX; len = &tmp_len; } - if ( *len > FAST_PEEK_MAX ) { + if ( *len > FAST_PEEK_MAX ) *len = FAST_PEEK_MAX; - } adr2 = adr + *len - 1; if ( ( SEG_OF( adr ) == hdw_seg || SEG_OF( adr2 ) == hdw_seg ) && ( ( ( bus_info.hdw_base ^ adr ) & 0xFFFC0 ) == 0 || ( ( bus_info.hdw_base ^ adr2 ) & 0xFFFC0 ) == 0 ) ) { bus_peek( buf, adr, *len ); + return buf; } else if ( !CAN_READ( adr ) ) { bus_peek_no_hdw( buf, adr, *len ); return buf; } else if ( SEG_OF( adr ) == SEG_OF( adr2 ) ) { - if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) { + if ( hdw_seg == SEG_OF( adr ) && ( bus_info.hdw_base & 0xFFFC0 ) - adr > 0 ) *len = ( bus_info.hdw_base & 0xFFFC0 ) - adr; - } else { + else *len = 0x1000 - OFFSET_OF( adr ); - } + return MAP_READ( adr ); } else if ( CAN_READ( adr2 ) && MAP_READ( adr ) + *len - 1 == MAP_READ( adr2 ) ) { - if ( hdw_seg == SEG_OF( adr2 ) ) { + if ( hdw_seg == SEG_OF( adr2 ) ) *len = ( bus_info.hdw_base & 0xFFFC0 ) - adr; - } else { + else *len = 0x2000 - OFFSET_OF( adr ); - } + return MAP_READ( adr ); } else { bus_peek_no_hdw( buf, adr, *len ); + return buf; } } void bus_remap( void ) { - int adr; - - for ( adr = 0; adr < 0x100000; adr += 0x01000 ) { + for ( int adr = 0; adr < 0x100000; adr += 0x01000 ) { if ( bus_info.ram_cfg && ( ( bus_info.ram_base ^ adr ) & bus_info.ram_size ) == 0 ) { read_map[ SEG_OF( adr ) ] = bus_info.ram_data + ( adr & bus_info.ram_mask ); write_map[ SEG_OF( adr ) ] = bus_info.ram_data + ( adr & bus_info.ram_mask ); } else if ( bus_info.ce2_cfg && ( ( bus_info.ce2_base ^ adr ) & bus_info.ce2_size ) == 0 ) { if ( bus_info.ce2_data ) { read_map[ SEG_OF( adr ) ] = bus_info.ce2_data + ( adr & bus_info.ce2_mask ); - if ( !bus_info.ce2_r_o ) { + if ( !bus_info.ce2_r_o ) write_map[ SEG_OF( adr ) ] = bus_info.ce2_data + ( adr & bus_info.ce2_mask ); - } else { + else write_map[ SEG_OF( adr ) ] = NULL; - } } else { read_map[ SEG_OF( adr ) ] = NULL; write_map[ SEG_OF( adr ) ] = NULL; @@ -279,11 +269,10 @@ void bus_remap( void ) } else if ( bus_info.ce1_cfg && ( ( bus_info.ce1_base ^ adr ) & bus_info.ce1_size ) == 0 ) { if ( bus_info.ce1_data ) { read_map[ SEG_OF( adr ) ] = bus_info.ce1_data + ( adr & bus_info.ce1_mask ); - if ( !bus_info.ce1_r_o ) { + if ( !bus_info.ce1_r_o ) write_map[ SEG_OF( adr ) ] = bus_info.ce1_data + ( adr & bus_info.ce1_mask ); - } else { + else write_map[ SEG_OF( adr ) ] = NULL; - } } else { read_map[ SEG_OF( adr ) ] = NULL; write_map[ SEG_OF( adr ) ] = NULL; @@ -291,11 +280,10 @@ void bus_remap( void ) } else if ( bus_info.nce3_cfg && ( ( bus_info.nce3_base ^ adr ) & bus_info.nce3_size ) == 0 ) { if ( bus_info.nce3_data && bus_info.ben && !bus_info.da19 ) { read_map[ SEG_OF( adr ) ] = bus_info.nce3_data + ( adr & bus_info.nce3_mask ); - if ( !bus_info.nce3_r_o ) { + if ( !bus_info.nce3_r_o ) write_map[ SEG_OF( adr ) ] = bus_info.nce3_data + ( adr & bus_info.nce3_mask ); - } else { + else write_map[ SEG_OF( adr ) ] = NULL; - } } else { read_map[ SEG_OF( adr ) ] = NULL; write_map[ SEG_OF( adr ) ] = NULL; @@ -401,25 +389,24 @@ void bus_reset( void ) address bus_get_id( void ) { - if ( !bus_info.hdw_cfg ) { + if ( !bus_info.hdw_cfg ) return bus_info.hdw_base | 0x00019; - } else if ( !bus_info.ram_sz_cfg ) { + else if ( !bus_info.ram_sz_cfg ) return bus_info.ram_size | 0x00003; - } else if ( !bus_info.ram_cfg ) { + else if ( !bus_info.ram_cfg ) return bus_info.ram_base | 0x000F4; - } else if ( !bus_info.ce1_sz_cfg ) { + else if ( !bus_info.ce1_sz_cfg ) return bus_info.ce1_size | 0x00005; - } else if ( !bus_info.ce1_cfg ) { + else if ( !bus_info.ce1_cfg ) return bus_info.ce1_base | 0x000F6; - } else if ( !bus_info.ce2_sz_cfg ) { + else if ( !bus_info.ce2_sz_cfg ) return bus_info.ce2_size | 0x00007; - } else if ( !bus_info.ce2_cfg ) { + else if ( !bus_info.ce2_cfg ) return bus_info.ce2_base | 0x000F8; - } else if ( !bus_info.nce3_sz_cfg ) { + else if ( !bus_info.nce3_sz_cfg ) return bus_info.nce3_size | 0x00001; - } else if ( !bus_info.nce3_cfg ) { + else if ( !bus_info.nce3_cfg ) return bus_info.nce3_base | 0x000F2; - } else { + else return 0x00000; - } } diff --git a/src/bus.h b/src/bus.h index 82d6f5b..9d0961c 100644 --- a/src/bus.h +++ b/src/bus.h @@ -50,9 +50,6 @@ extern word crc; // FAST_PEEK_MAX must not be greater than the size of the hdw registers (64) #define FAST_PEEK_MAX 64 -/* extern void bus_init( void ); */ -/* extern void bus_exit( void ); */ - extern void bus_read( byte* buf, address adr, address len ); extern void bus_write( byte* buf, address adr, address len ); extern byte* bus_fast_peek( byte* buf, address adr, address* len ); diff --git a/src/emulator.c b/src/emulator.c index 1ee23eb..8d1e8cf 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -10,7 +10,7 @@ #include "timers.h" #include "display.h" #include "emulator.h" -#include "files.h" +#include "persistence.h" #include "ports.h" #include "config.h" diff --git a/src/keyboard.c b/src/keyboard.c index bdc4403..8253410 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -1,6 +1,6 @@ #include "types.h" #include "cpu.h" -#include "files.h" +#include "persistence.h" bool kbd_on; diff --git a/src/files.c b/src/persistence.c similarity index 90% rename from src/files.c rename to src/persistence.c index 893f19f..66f1279 100644 --- a/src/files.c +++ b/src/persistence.c @@ -337,36 +337,36 @@ void load_file_on_stack( char* filename ) void bus_init( char* filename ) { + bus_reset(); + char fullpath[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath, "%s%s", absolute_working_dir_path, filename ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath ); int filesize = file_size( fullpath ); - /* if ( filesize ) { */ - /* FILE* fp; */ + if ( filesize ) { + FILE* fp; + /* BusInfo tmp_bus_info; */ - /* if ( NULL == ( fp = fopen( fullpath, "w" ) ) ) { */ - /* if ( config.verbose ) */ - /* fprintf( stderr, "can\'t open %s\n", fullpath ); */ - /* return; */ - /* } */ + if ( NULL == ( fp = fopen( fullpath, "w" ) ) ) { + if ( config.verbose ) + fprintf( stderr, "can\'t open %s\n", fullpath ); + return; + } + if ( config.verbose ) + fprintf( stderr, "Loading bus_info from %s\n", fullpath ); - /* fwrite( &bus_info, sizeof( BusInfo ), 1, fp ); */ + fread( &bus_info, sizeof( BusInfo ), 1, fp ); - /* fclose( fp ); */ - /* } else */ - bus_reset(); + fclose( fp ); + } } void bus_exit( char* filename ) { char fullpath[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath, "%s%s", absolute_working_dir_path, filename ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath ); FILE* fp; @@ -386,23 +386,23 @@ void cpu_init( char* filename ) char fullpath[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath, "%s%s", absolute_working_dir_path, filename ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath ); int filesize = file_size( fullpath ); if ( filesize ) { - /* FILE* fp; */ + FILE* fp; - /* if ( NULL == ( fp = fopen( fullpath, "w" ) ) ) { */ - /* if ( config.verbose ) */ - /* fprintf( stderr, "can\'t open %s\n", fullpath ); */ - /* return; */ - /* } */ + if ( NULL == ( fp = fopen( fullpath, "w" ) ) ) { + if ( config.verbose ) + fprintf( stderr, "can\'t open %s\n", fullpath ); + return; + } + if ( config.verbose ) + fprintf( stderr, "Loading cpu from %s\n", fullpath ); - /* fwrite( &cpu, sizeof( Cpu ), 1, fp ); */ + fread( &cpu, sizeof( Cpu ), 1, fp ); - /* fclose( fp ); */ + fclose( fp ); } } void cpu_exit( char* filename ) @@ -410,8 +410,6 @@ void cpu_exit( char* filename ) char fullpath[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath, "%s%s", absolute_working_dir_path, filename ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath ); FILE* fp; @@ -435,8 +433,6 @@ void rom_init( char* filename ) char fullpath[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath, "%s%s", absolute_working_dir_path, filename ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath ); size = file_size( fullpath ); if ( !size ) { @@ -501,8 +497,6 @@ void ram_init( char* filename ) char fullpath[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath, "%s%s", absolute_working_dir_path, filename ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath ); int filesize = file_size( fullpath ) * 2; if ( config.verbose ) @@ -531,10 +525,9 @@ void ram_exit( char* filename ) char fullpath[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath, "%s%s", absolute_working_dir_path, filename ); - if ( config.verbose ) { - fprintf( stderr, "fullpath = %s\n", fullpath ); + if ( config.verbose ) fprintf( stderr, "Saving RAM to %s\n", fullpath ); - } + write_mem_file( fullpath, bus_info.ram_data, ram_size ); free( bus_info.ram_data ); @@ -556,8 +549,6 @@ void ports_init( char* filename1, char* filename2 ) char fullpath1[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath1, "%s%s", absolute_working_dir_path, filename1 ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath1 ); int filesize = file_size( fullpath1 ) * 2; if ( filesize == port1_size ) @@ -572,8 +563,6 @@ void ports_init( char* filename1, char* filename2 ) char fullpath2[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath2, "%s%s", absolute_working_dir_path, filename2 ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath1 ); filesize = file_size( fullpath2 ) * 2; if ( filesize == port2_size ) @@ -591,14 +580,10 @@ void ports_exit( char* filename1, char* filename2 ) char fullpath1[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath1, "%s%s", absolute_working_dir_path, filename1 ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath1 ); char fullpath2[ MAX_LENGTH_FILENAME ]; get_absolute_working_dir_path(); sprintf( fullpath2, "%s%s", absolute_working_dir_path, filename2 ); - if ( config.verbose ) - fprintf( stderr, "fullpath = %s\n", fullpath1 ); write_mem_file( fullpath1, bus_info.ce2_data, port1_size ); write_mem_file( fullpath2, port2, port2_size ); diff --git a/src/files.h b/src/persistence.h similarity index 100% rename from src/files.h rename to src/persistence.h