2024-10-14 19:48:56 +02:00
# include <stdint.h>
# include <stdio.h>
2024-10-15 13:30:45 +02:00
# include <string.h>
2024-10-15 17:07:18 +02:00
# include <stdlib.h>
2024-10-14 19:48:56 +02:00
struct AddressSpace_s {
// A pointer to a ROM array. The array can vary in length.
uint8_t * rom ;
// The size of ROM.
2024-10-15 17:07:18 +02:00
uint32_t romSize ; // TODO look into making it const
2024-10-14 19:48:56 +02:00
// A pointer to a RAM array. The array can vary in length.
uint8_t * ram ;
// The size of RAM.
uint32_t ramSize ;
} ;
typedef struct AddressSpace_s AddressSpace ;
2024-10-15 17:07:18 +02:00
AddressSpace * create_address_space ( const uint32_t romSize , const uint32_t ramSize ) {
uint8_t * rom = calloc ( romSize , 1 ) ;
uint8_t * ram = calloc ( ramSize , 1 ) ;
AddressSpace * addressSpace = malloc ( sizeof ( AddressSpace ) ) ;
2024-10-14 19:48:56 +02:00
2024-10-15 17:07:18 +02:00
if ( rom = = NULL | | ram = = NULL | | addressSpace = = NULL ) {
free ( rom ) ;
free ( ram ) ;
free ( addressSpace ) ;
return NULL ; // Memory allocation failed
}
addressSpace - > rom = rom ;
addressSpace - > romSize = romSize ;
addressSpace - > ram = ram ;
addressSpace - > ramSize = ramSize ;
2024-10-14 19:48:56 +02:00
return addressSpace ;
}
2024-10-15 17:07:18 +02:00
int read_address_space ( const AddressSpace * addressSpace , const uint32_t address , const int n , void * dest ) {
2024-10-14 19:48:56 +02:00
uint32_t romSize = addressSpace - > romSize ;
uint32_t ramSize = addressSpace - > ramSize ;
if ( address < romSize ) {
2024-10-15 17:07:18 +02:00
if ( address + n > = romSize ) {
2024-10-14 19:48:56 +02:00
fprintf ( stderr , " Reading %d bytes from %d will exceed ROM address space of %d \n " , n , address , romSize ) ; // TODO maybe move to perror
return 1 ;
} else {
2024-10-15 13:30:45 +02:00
memcpy ( dest , addressSpace - > rom + address , n ) ;
2024-10-14 19:48:56 +02:00
}
} else if ( address < romSize + ramSize ) {
2024-10-15 17:07:18 +02:00
if ( address + n > = romSize + ramSize ) {
2024-10-14 19:48:56 +02:00
fprintf ( stderr , " Reading %d bytes from %d (total %d) will exceed RAM address space of %d (total %d) \n " , n , address , address + romSize , ramSize , romSize + ramSize ) ;
return 1 ;
} else {
2024-10-15 17:07:18 +02:00
memcpy ( dest , addressSpace - > ram + address - romSize , n ) ;
2024-10-14 19:48:56 +02:00
}
} else {
// TODO IO
2024-10-16 15:34:06 +02:00
return 1 ;
2024-10-14 19:48:56 +02:00
}
return 0 ;
}
2024-10-15 17:07:18 +02:00
int write_address_space ( const AddressSpace * addressSpace , const uint32_t address , const int n , void * src ) {
2024-10-14 19:48:56 +02:00
uint32_t romSize = addressSpace - > romSize ;
uint32_t ramSize = addressSpace - > ramSize ;
if ( address < romSize ) {
2024-10-15 17:07:18 +02:00
if ( address + n > = romSize ) {
2024-10-14 19:48:56 +02:00
fprintf ( stderr , " Writing %d bytes to %d will exceed ROM address space of %d \n " , n , address , romSize ) ;
return 1 ;
} else {
2024-10-15 13:30:45 +02:00
memcpy ( addressSpace - > rom + address , src , n ) ;
2024-10-14 19:48:56 +02:00
}
} else if ( address < romSize + ramSize ) {
2024-10-15 17:07:18 +02:00
if ( address + n > = romSize + ramSize ) {
2024-10-14 19:48:56 +02:00
fprintf ( stderr , " Writing %d bytes to %d (total %d) will exceed RAM address space of %d (total %d) \n " , n , address , address + romSize , ramSize , romSize + ramSize ) ;
return 1 ;
} else {
2024-10-15 17:07:18 +02:00
memcpy ( addressSpace - > ram + address - romSize , src , n ) ;
2024-10-14 19:48:56 +02:00
}
} else {
// TODO IO
2024-10-16 15:34:06 +02:00
return 1 ;
2024-10-14 19:48:56 +02:00
}
return 0 ;
}