From 284f64c69f11d89092ce703dd361ab624cf7284a Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Sat, 13 Apr 2024 14:00:40 +0200 Subject: [PATCH] merge (current and future) files functions into files.{c,h} --- Makefile | 6 +- src/emulator.c | 3 +- src/files.c | 228 +++++++++++++++++++++++++++++++++++ src/files.h | 20 +++ src/gui.c | 4 +- src/gui_buttons.c | 4 +- src/{color.c => gui_color.c} | 2 +- src/{color.h => gui_color.h} | 0 src/main.c | 7 -- src/pfiles.c | 109 ----------------- src/pfiles.h | 11 -- src/ports.c | 35 +----- src/ports.h | 9 +- src/ram.c | 26 ---- src/ram.h | 7 -- src/rom.c | 79 ------------ src/rom.h | 7 -- 17 files changed, 266 insertions(+), 291 deletions(-) create mode 100644 src/files.c create mode 100644 src/files.h rename src/{color.c => gui_color.c} (98%) rename src/{color.h => gui_color.h} (100%) delete mode 100644 src/pfiles.c delete mode 100644 src/pfiles.h delete mode 100644 src/ram.c delete mode 100644 src/ram.h delete mode 100644 src/rom.c delete mode 100644 src/rom.h diff --git a/Makefile b/Makefile index 38a5d0c..1bc56a5 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CFLAGS = -Wall -Werror -O3 -Wno-error=unused-function -Wno-error=unused-variable all: dist/hpemu dist/hpemu: src/bus.o \ - src/color.o \ + src/gui_color.o \ src/cpu.o \ src/display.o \ src/emulator.o \ @@ -17,10 +17,8 @@ dist/hpemu: src/bus.o \ src/main.o \ src/opcodes.o \ src/gui_buttons.o \ - src/pfiles.o \ + src/files.o \ src/ports.o \ - src/ram.o \ - src/rom.o \ src/rpl.o \ src/timers.o $(CC) $(CFLAGS) $(LIBS) -o $@ $+ diff --git a/src/emulator.c b/src/emulator.c index a56a54a..cb97056 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -11,8 +11,7 @@ #include "display.h" #include "gui.h" #include "emulator.h" -#include "rom.h" -#include "ram.h" +#include "files.h" #include "ports.h" #define MAX_DELTA 4000 diff --git a/src/files.c b/src/files.c new file mode 100644 index 0000000..7d05f38 --- /dev/null +++ b/src/files.c @@ -0,0 +1,228 @@ +#include +#include +#include +#include // dirname +#include // readlink +#include // PATH_MAX + +#include "gui.h" +#include "rpl.h" +#include "files.h" +#include "bus.h" +#include "ports.h" + +extern byte current_bank; +extern byte* port2; +extern address port2mask; + +char WorkingPath[ 512 ]; + +static address ram_size = 256 * 1024; // in nibbles, not bytes! + +void getExePath() +{ + char programPath[ 1024 ]; + char temp[ 1024 ]; + memset( programPath, 0, sizeof( programPath ) ); + memset( temp, 0, sizeof( temp ) ); + + char result[ PATH_MAX ]; + ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX ); + const char* path; + if ( count != -1 ) { + path = dirname( result ); + strcpy( programPath, path ); + } + + memset( WorkingPath, 0, sizeof( WorkingPath ) ); + strcpy( WorkingPath, programPath ); +} + +int file_size( char* name ) +{ + memset( WorkingPath, 0, sizeof( WorkingPath ) ); + getExePath(); + + FILE* f; + char fullpath[ 1024 ]; + sprintf( fullpath, "%s/%s", WorkingPath, name ); + printf( "%s\n", fullpath ); + f = fopen( fullpath, "r" ); + if ( !f ) + return 0; + + fseek( f, 0, SEEK_END ); // seek to end of file + int size = ( int )ftell( f ); // get current file pointer + fseek( f, 0, SEEK_SET ); // seek back to beginning of file + // proceed with allocating memory and reading the file + fclose( f ); + return size; +} + +void load_file_on_stack( char* name ) +{ + FILE* f; + byte* buf; + byte* obj; + int i, j; + int fsize; + address size; + + fsize = file_size( name ); + if ( fsize < 11 ) // "PHPH48-X" + prologue (8 + 2.5) + return; + + buf = malloc( fsize ); + if ( !buf ) + return; + + f = fopen( name, "r" ); + if ( !f ) { + free( buf ); + return; + } + int res = ( int )fread( buf, sizeof( char ), fsize, f ); + if ( res != fsize ) { + free( buf ); + fclose( f ); + return; + } + fclose( f ); + + if ( memcmp( buf, "HPHP48-", 7 ) ) { + free( buf ); + return; + } + + obj = malloc( ( fsize - 8 ) * 2 ); + if ( !obj ) { + free( buf ); + return; + } + + for ( i = 8, j = 0; i < fsize; i++ ) { + obj[ j++ ] = buf[ i ] & 0x0F; + obj[ j++ ] = ( buf[ i ] >> 4 ) & 0x0F; + } + free( buf ); + + size = rpl_object_size( obj ); + if ( size > ( fsize - 8 ) * 2 ) { + free( obj ); + return; + } + rpl_push_object( obj, size ); + free( obj ); +} + +void rom_init( void ) +{ + int size; + char* name = "rom"; + byte *buf, *ptr1, *ptr2; + FILE* f; + + size = file_size( name ); + if ( !size ) { + printf( "rom_init failed\n" ); + exit( 0x10 ); + } + if ( size != 256 * 1024 && size != 512 * 1024 && size != 1024 * 1024 ) { + printf( "rom_init failed2..\n" ); + exit( 0x11 ); + } + buf = malloc( size ); + if ( !buf ) { + printf( "rom_init failed3..\n" ); + exit( 0x12 ); + } + + char fullpath[ 1024 ]; + sprintf( fullpath, "%s/%s", WorkingPath, name ); + + // f = pack_fopen(name, "r"); + f = fopen( fullpath, "r" ); + if ( !f ) { + printf( "rom_init failed4..\n" ); + exit( 0x13 ); + } + int r = ( int )fread( buf, sizeof( char ), size, f ); + if ( r != size ) { // pack_fread + printf( "rom_init failed5..\n" ); + exit( 0x14 ); + } + // pack_fclose(f); + fclose( f ); + if ( buf[ 0 ] & 0xF0 || buf[ 1 ] & 0xF0 ) { + if ( size == 1024 * 1024 ) { + printf( "rom_init failed6..\n" ); + exit( 0x15 ); + } + buf = realloc( buf, size * 2 ); + if ( !buf ) { + printf( "rom_init failed7..\n" ); + exit( 0x16 ); + } + ptr1 = buf + size - 1; + size *= 2; + ptr2 = buf + size - 1; + do { + *( ptr2-- ) = ( *ptr1 >> 4 ) & 0x0F; + *( ptr2-- ) = *( ptr1-- ) & 0x0F; + } while ( ptr1 != ptr2 ); + } + bus_info.rom_data = buf; + bus_info.rom_mask = size - 1; + printf( "rom_init succeed!\n" ); +} + +void rom_exit( void ) +{ + free( bus_info.rom_data ); + bus_info.rom_data = NULL; + bus_info.rom_mask = 0x00000; +} + +void ram_init( void ) +{ + byte* buf = malloc( ram_size ); + if ( !buf ) + exit( 0x20 ); + + memset( buf, 0, ram_size ); + bus_info.ram_data = buf; + bus_info.ram_mask = ram_size - 1; +} + +void ram_exit( void ) +{ + free( bus_info.ram_data ); + bus_info.ram_data = NULL; + bus_info.ram_mask = 0x00000; +} + +void ports_init( void ) +{ + // ce1 = bank switcher + bus_info.ce1_data = NULL; + bus_info.ce1_mask = 0x0007F; + bus_info.ce1_r_o = true; + bus_info.ce1_bs = true; + + // ce2 = port1 (plugged) + bus_info.ce2_data = malloc( PORT1_SIZE ); + bus_info.ce2_mask = PORT1_SIZE - 1; + bus_info.ce2_r_o = false; + + // nce3 = port2 (plugged) + port2 = malloc( PORT2_SIZE ); + port2mask = PORT2_SIZE - 1; + bus_info.nce3_data = port2; + bus_info.nce3_mask = port2mask & 0x3FFFF; + bus_info.nce3_r_o = false; + + bus_info.ben = false; + current_bank = 0; +} + +void ports_exit( void ) {} diff --git a/src/files.h b/src/files.h new file mode 100644 index 0000000..18466cc --- /dev/null +++ b/src/files.h @@ -0,0 +1,20 @@ +#ifndef __PFILES_H +#define __PFILES_H + +#include "types.h" + +extern char WorkingPath[ 512 ]; + +extern int file_size( char* name ); +extern void load_file_on_stack( char* name ); + +extern void rom_init( void ); +extern void rom_exit( void ); + +extern void ram_init( void ); +extern void ram_exit( void ); + +extern void ports_init( void ); +extern void ports_exit( void ); + +#endif diff --git a/src/gui.c b/src/gui.c index 5fe1294..7636ad9 100644 --- a/src/gui.c +++ b/src/gui.c @@ -5,9 +5,9 @@ #include #include -#include "color.h" +#include "gui_color.h" #include "gui_buttons.h" -#include "pfiles.h" +#include "files.h" #include "gui.h" #include "emulator.h" diff --git a/src/gui_buttons.c b/src/gui_buttons.c index 53f5dd5..4e1831b 100644 --- a/src/gui_buttons.c +++ b/src/gui_buttons.c @@ -1,11 +1,11 @@ #include -#include "color.h" +#include "gui_color.h" #include "display.h" #include "keyboard.h" #include "gui.h" #include "gui_buttons.h" -#include "pfiles.h" +#include "files.h" const int std_flags = BUTTON_B1RELEASE | BUTTON_B2TOGGLE; diff --git a/src/color.c b/src/gui_color.c similarity index 98% rename from src/color.c rename to src/gui_color.c index 1e7a309..f1ef3e2 100644 --- a/src/color.c +++ b/src/gui_color.c @@ -1,6 +1,6 @@ #include -#include "color.h" +#include "gui_color.h" #define RESERVED_LCD 128 diff --git a/src/color.h b/src/gui_color.h similarity index 100% rename from src/color.h rename to src/gui_color.h diff --git a/src/main.c b/src/main.c index 37e1848..a09320f 100644 --- a/src/main.c +++ b/src/main.c @@ -3,16 +3,9 @@ #include -#include "types.h" #include "emulator.h" #include "gui.h" -#include "color.h" #include "display.h" -#include "gui.h" -#include "timers.h" -#include "keyboard.h" -#include "gui_buttons.h" -#include "pfiles.h" unsigned int currentTime; diff --git a/src/pfiles.c b/src/pfiles.c deleted file mode 100644 index 0e20ed0..0000000 --- a/src/pfiles.c +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -#include -#include // dirname -#include // readlink -#include // PATH_MAX - -#include "color.h" -#include "gui.h" -#include "rpl.h" -#include "pfiles.h" - -char WorkingPath[ 512 ]; - -void getExePath() -{ - char programPath[ 1024 ]; - char temp[ 1024 ]; - memset( programPath, 0, sizeof( programPath ) ); - memset( temp, 0, sizeof( temp ) ); - - char result[ PATH_MAX ]; - ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX ); - const char* path; - if ( count != -1 ) { - path = dirname( result ); - strcpy( programPath, path ); - } - - memset( WorkingPath, 0, sizeof( WorkingPath ) ); - strcpy( WorkingPath, programPath ); -} - -int file_size( char* name ) -{ - memset( WorkingPath, 0, sizeof( WorkingPath ) ); - getExePath(); - - FILE* f; - char fullpath[ 1024 ]; - sprintf( fullpath, "%s/%s", WorkingPath, name ); - printf( "%s\n", fullpath ); - f = fopen( fullpath, "r" ); - if ( !f ) - return 0; - - fseek( f, 0, SEEK_END ); // seek to end of file - int size = ( int )ftell( f ); // get current file pointer - fseek( f, 0, SEEK_SET ); // seek back to beginning of file - // proceed with allocating memory and reading the file - fclose( f ); - return size; -} - -void load_file_on_stack( char* name ) -{ - FILE* f; - byte* buf; - byte* obj; - int i, j; - int fsize; - address size; - - fsize = file_size( name ); - if ( fsize < 11 ) // "PHPH48-X" + prologue (8 + 2.5) - return; - - buf = malloc( fsize ); - if ( !buf ) - return; - - f = fopen( name, "r" ); - if ( !f ) { - free( buf ); - return; - } - int res = ( int )fread( buf, sizeof( char ), fsize, f ); - if ( res != fsize ) { - free( buf ); - fclose( f ); - return; - } - fclose( f ); - - if ( memcmp( buf, "HPHP48-", 7 ) ) { - free( buf ); - return; - } - - obj = malloc( ( fsize - 8 ) * 2 ); - if ( !obj ) { - free( buf ); - return; - } - - for ( i = 8, j = 0; i < fsize; i++ ) { - obj[ j++ ] = buf[ i ] & 0x0F; - obj[ j++ ] = ( buf[ i ] >> 4 ) & 0x0F; - } - free( buf ); - - size = rpl_object_size( obj ); - if ( size > ( fsize - 8 ) * 2 ) { - free( obj ); - return; - } - rpl_push_object( obj, size ); - free( obj ); -} diff --git a/src/pfiles.h b/src/pfiles.h deleted file mode 100644 index 5f57e48..0000000 --- a/src/pfiles.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __PFILES_H -#define __PFILES_H - -#include "types.h" - -extern char WorkingPath[ 512 ]; - -extern int file_size( char* name ); -extern void load_file_on_stack( char* name ); - -#endif diff --git a/src/ports.c b/src/ports.c index 2801ac3..ade0425 100644 --- a/src/ports.c +++ b/src/ports.c @@ -4,38 +4,9 @@ #include "bus.h" #include "ports.h" -#define PORT1_SIZE ( 256 * 1024 ) /* 128Kio in nibbles */ -#define PORT2_SIZE ( 256 * 1024 ) /* 128Kio in nibbles */ - -static byte current_bank; -static byte* port2; -static address port2mask; - -void ports_init( void ) -{ - // ce1 = bank switcher - bus_info.ce1_data = NULL; - bus_info.ce1_mask = 0x0007F; - bus_info.ce1_r_o = true; - bus_info.ce1_bs = true; - - // ce2 = port1 (plugged) - bus_info.ce2_data = malloc( PORT1_SIZE ); - bus_info.ce2_mask = PORT1_SIZE - 1; - bus_info.ce2_r_o = false; - - // nce3 = port2 (plugged) - port2 = malloc( PORT2_SIZE ); - port2mask = PORT2_SIZE - 1; - bus_info.nce3_data = port2; - bus_info.nce3_mask = port2mask & 0x3FFFF; - bus_info.nce3_r_o = false; - - bus_info.ben = false; - current_bank = 0; -} - -void ports_exit( void ) {} +byte current_bank; +byte* port2; +address port2mask; void ports_switch_bank( address adr ) { diff --git a/src/ports.h b/src/ports.h index 3eedbaf..188731f 100644 --- a/src/ports.h +++ b/src/ports.h @@ -3,8 +3,13 @@ #include "types.h" -extern void ports_init( void ); -extern void ports_exit( void ); +#define PORT1_SIZE ( 256 * 1024 ) /* 128Kio in nibbles */ +#define PORT2_SIZE ( 256 * 1024 ) /* 128Kio in nibbles */ + +extern byte current_bank; +extern byte* port2; +extern address port2mask; + extern void ports_switch_bank( address adr ); extern byte ports_card_detect( void ); diff --git a/src/ram.c b/src/ram.c deleted file mode 100644 index 16e556c..0000000 --- a/src/ram.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include - -#include "types.h" -#include "bus.h" -#include "ram.h" - -static address ram_size = 256 * 1024; // in nibbles, not bytes! - -void ram_init( void ) -{ - byte* buf = malloc( ram_size ); - if ( !buf ) - exit( 0x20 ); - - memset( buf, 0, ram_size ); - bus_info.ram_data = buf; - bus_info.ram_mask = ram_size - 1; -} - -void ram_exit( void ) -{ - free( bus_info.ram_data ); - bus_info.ram_data = NULL; - bus_info.ram_mask = 0x00000; -} diff --git a/src/ram.h b/src/ram.h deleted file mode 100644 index 0f30a15..0000000 --- a/src/ram.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef __RAM_H -#define __RAM_H - -extern void ram_init( void ); -extern void ram_exit( void ); - -#endif diff --git a/src/rom.c b/src/rom.c deleted file mode 100644 index 974b38c..0000000 --- a/src/rom.c +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#include -#include // dirname -#include // readlink -#include // PATH_MAX - -#include "types.h" -#include "bus.h" -#include "rom.h" -#include "pfiles.h" - -void rom_init( void ) -{ - int size; - char* name = "rom"; - byte *buf, *ptr1, *ptr2; - FILE* f; - - size = file_size( name ); - if ( !size ) { - printf( "rom_init failed\n" ); - exit( 0x10 ); - } - if ( size != 256 * 1024 && size != 512 * 1024 && size != 1024 * 1024 ) { - printf( "rom_init failed2..\n" ); - exit( 0x11 ); - } - buf = malloc( size ); - if ( !buf ) { - printf( "rom_init failed3..\n" ); - exit( 0x12 ); - } - - char fullpath[ 1024 ]; - sprintf( fullpath, "%s/%s", WorkingPath, name ); - - // f = pack_fopen(name, "r"); - f = fopen( fullpath, "r" ); - if ( !f ) { - printf( "rom_init failed4..\n" ); - exit( 0x13 ); - } - int r = ( int )fread( buf, sizeof( char ), size, f ); - if ( r != size ) { // pack_fread - printf( "rom_init failed5..\n" ); - exit( 0x14 ); - } - // pack_fclose(f); - fclose( f ); - if ( buf[ 0 ] & 0xF0 || buf[ 1 ] & 0xF0 ) { - if ( size == 1024 * 1024 ) { - printf( "rom_init failed6..\n" ); - exit( 0x15 ); - } - buf = realloc( buf, size * 2 ); - if ( !buf ) { - printf( "rom_init failed7..\n" ); - exit( 0x16 ); - } - ptr1 = buf + size - 1; - size *= 2; - ptr2 = buf + size - 1; - do { - *( ptr2-- ) = ( *ptr1 >> 4 ) & 0x0F; - *( ptr2-- ) = *( ptr1-- ) & 0x0F; - } while ( ptr1 != ptr2 ); - } - bus_info.rom_data = buf; - bus_info.rom_mask = size - 1; - printf( "rom_init succeed!\n" ); -} - -void rom_exit( void ) -{ - free( bus_info.rom_data ); - bus_info.rom_data = NULL; - bus_info.rom_mask = 0x00000; -} diff --git a/src/rom.h b/src/rom.h deleted file mode 100644 index 31fe943..0000000 --- a/src/rom.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef __ROM_H -#define __ROM_H - -extern void rom_init( void ); -extern void rom_exit( void ); - -#endif