merge (current and future) files functions into files.{c,h}

This commit is contained in:
Gwenhael Le Moine 2024-04-13 14:00:40 +02:00
parent 8dd69b710e
commit 284f64c69f
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
17 changed files with 266 additions and 291 deletions

View file

@ -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 $@ $+

View file

@ -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

228
src/files.c Normal file
View file

@ -0,0 +1,228 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h> // dirname
#include <unistd.h> // readlink
#include <linux/limits.h> // 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 ) {}

20
src/files.h Normal file
View file

@ -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

View file

@ -5,9 +5,9 @@
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "color.h"
#include "gui_color.h"
#include "gui_buttons.h"
#include "pfiles.h"
#include "files.h"
#include "gui.h"
#include "emulator.h"

View file

@ -1,11 +1,11 @@
#include <stdlib.h>
#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;

View file

@ -1,6 +1,6 @@
#include <stdlib.h>
#include "color.h"
#include "gui_color.h"
#define RESERVED_LCD 128

View file

@ -3,16 +3,9 @@
#include <SDL2/SDL.h>
#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;

View file

@ -1,109 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h> // dirname
#include <unistd.h> // readlink
#include <linux/limits.h> // 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 );
}

View file

@ -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

View file

@ -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 )
{

View file

@ -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 );

View file

@ -1,26 +0,0 @@
#include <stdlib.h>
#include <string.h>
#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;
}

View file

@ -1,7 +0,0 @@
#ifndef __RAM_H
#define __RAM_H
extern void ram_init( void );
extern void ram_exit( void );
#endif

View file

@ -1,79 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h> // dirname
#include <unistd.h> // readlink
#include <linux/limits.h> // 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;
}

View file

@ -1,7 +0,0 @@
#ifndef __ROM_H
#define __ROM_H
extern void rom_init( void );
extern void rom_exit( void );
#endif