Skip to content

Commit

Permalink
fix #26
Browse files Browse the repository at this point in the history
  • Loading branch information
esm-tmori committed Dec 16, 2020
1 parent c58165b commit 210362f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/device/mpu/loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ static Std_ReturnType Elf_LoadProgram(const Elf32_Ehdr *elf_image, MemoryAddress
set_malloc_region(memap, i);
}
}
device_init_athrill_exdev();
for (i = 0; i < memap->dev_num; i++) {
ptr = mpu_address_set_dev(memap->dev[i].start, memap->dev[i].size * 1024, memap->dev[i].extdev_handle);
if (ptr == NULL) {
Expand Down
29 changes: 13 additions & 16 deletions src/device/mpu/mpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,28 +214,25 @@ uint8 *mpu_address_set_dev(uint32 addr, uint32 size, void *handler)
addr, size, region->start, region->size);
return NULL;
}
void (*devinit) (MpuAddressRegionType *, AthrillExDevOperationType *) = dlsym(handler, "ex_device_init");
if (devinit == NULL) {
AthrillExDeviceType *exdev = dlsym(handler, "athrill_ex_device");
if (exdev->devinit == NULL) {
printf("ERROR: addr=0x%x size=%u not found ex_device_init\n", addr, size);
return NULL;
}
void (*supply_clock) (DeviceClockType *) = dlsym(handler, "ex_device_supply_clock");
if (devinit == NULL) {
if (exdev->supply_clock == NULL) {
printf("ERROR: addr=0x%x size=%u not found ex_device_supply_clock\n", addr, size);
return NULL;
}
device_add_athrill_exdev(supply_clock);
MpuAddressRegionOperationType *ops = dlsym(handler, "ex_device_memory_operation");
if (ops == NULL) {

if (exdev->ops == NULL) {
printf("INFO: addr=0x%x size=%u: default memory operation\n", addr, size);
ops = &default_memory_operation;
exdev->ops = &default_memory_operation;
}
uint8 *datap = (uint8*)dlsym(handler, "ex_device_memory_data");
if (datap == NULL) {
if (exdev->datap == NULL) {
printf("INFO: addr=0x%x size=%u: default memory\n", addr, size);
datap = malloc(size);
ASSERT(datap != NULL);
memset(datap, 0, size);
exdev->datap = malloc(size);
ASSERT(exdev->datap != NULL);
memset(exdev->datap, 0, size);
}

mpu_address_map.dynamic_map_num++;
Expand All @@ -248,10 +245,10 @@ uint8 *mpu_address_set_dev(uint32 addr, uint32 size, void *handler)

mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1].permission = MPU_ADDRESS_REGION_PERM_ALL;
mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1].mask = MPU_ADDRESS_REGION_MASK_ALL;
mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1].data = datap;
mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1].ops = ops;
mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1].data = (uint8*)exdev->datap;
mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1].ops = exdev->ops;

devinit(&mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1], &athrill_exdev_operation);
device_add_athrill_exdev(exdev, &mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1]);
return mpu_address_map.dynamic_map[mpu_address_map.dynamic_map_num -1].data;
}
#endif /* OS_LINUX */
Expand Down
36 changes: 25 additions & 11 deletions src/device/peripheral/athrill_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ static Std_ReturnType athrill_device_get_memory(uint32 addr, uint8 **data)
{
return mpu_get_pointer(0U, addr, data);
}
typedef struct {
AthrillExDeviceType *devp;
MpuAddressRegionType *region;
} AthrillExtDevEntryType;
typedef struct {
uint32 num;
AthrillExtDevEntryType **exdevs;
} AthrillExtDevType;
static AthrillExtDevType athrill_exdev;

void device_init_athrill_exdev(void)
{
Expand Down Expand Up @@ -103,20 +112,25 @@ void device_init_athrill_exdev(void)
athrill_exdev_operation.libs.thread.start_proc = &mpthread_start_proc;
athrill_exdev_operation.libs.thread.wait_proc = &mpthread_wait_proc;
athrill_exdev_operation.libs.thread.timedwait_proc = &mpthread_timedwait_proc;

int i;
for (i = 0; i < athrill_exdev.num; i++) {
athrill_exdev.exdevs[i]->devp->devinit(athrill_exdev.exdevs[i]->region, &athrill_exdev_operation);
}

return;
}
typedef struct {
uint32 num;
void (**supply_clock) (DeviceClockType *);
} AthrillExtDevType;
static AthrillExtDevType athrill_exdev;
void device_add_athrill_exdev(void (*supply_clock) (DeviceClockType *))
void device_add_athrill_exdev(void *devp, void *region)
{
AthrillExtDevEntryType *entryp = malloc(sizeof(AthrillExtDevEntryType));
ASSERT(entryp != NULL);
entryp->devp = (AthrillExDeviceType*)devp;
entryp->region = (MpuAddressRegionType*)region;
athrill_exdev.num++;
athrill_exdev.supply_clock = realloc(athrill_exdev.supply_clock,
sizeof(void (**) (DeviceClockType *)) * athrill_exdev.num);
ASSERT(athrill_exdev.supply_clock != NULL);
athrill_exdev.supply_clock[athrill_exdev.num - 1] = supply_clock;
athrill_exdev.exdevs = realloc(athrill_exdev.exdevs,
sizeof(AthrillExtDevEntryType*) * athrill_exdev.num);
ASSERT(athrill_exdev.exdevs != NULL);
athrill_exdev.exdevs[athrill_exdev.num - 1] = entryp;
return;
}
void athrill_device_set_mmap_info(AthrillDeviceMmapInfoType *info)
Expand Down Expand Up @@ -230,7 +244,7 @@ void device_supply_clock_exdev(DeviceClockType *dev_clock)
{
int i;
for (i = 0; i < athrill_exdev.num; i++) {
athrill_exdev.supply_clock[i](dev_clock);
athrill_exdev.exdevs[i]->devp->supply_clock(dev_clock);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/inc/athrill_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

extern void device_init_athrill_device(void);
extern void device_init_athrill_exdev(void);
extern void device_add_athrill_exdev(void (*supply_clock) (DeviceClockType *));
extern void device_add_athrill_exdev(void *devp, void *region);

extern void device_supply_clock_athrill_device(void);
extern void device_supply_clock_exdev(DeviceClockType *dev_clock);
Expand Down
12 changes: 12 additions & 0 deletions src/inc/athrill_exdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ typedef struct {

extern AthrillExDevOperationType athrill_exdev_operation;

#include "athrill_exdev_header.h"
/*
* dynamic symbol name: "athrill_ex_device"
*/
typedef struct {
AthrillExDeviceHeaderType header;
char *datap;
MpuAddressRegionOperationType *ops;
void (*devinit) (MpuAddressRegionType *, AthrillExDevOperationType *);
void (*supply_clock) (DeviceClockType *);
} AthrillExDeviceType;

#endif /* _ATHRILL_EXDEV_H_ */
13 changes: 13 additions & 0 deletions src/inc/athrill_exdev_header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _ATHRILL_EXDEV_HEADER_H_
#define _ATHRILL_EXDEV_HEADER_H_

#define ATHRILL_EXTERNAL_DEVICE_MAGICNO 0xBEAFDEAD
#define ATHRILL_EXTERNAL_DEVICE_VERSION 0x00000001
typedef struct {
unsigned int magicno; /* ATHRILL_EXTERNAL_DEVICE_MAGICNO */
unsigned int version; /* ATHRILL_EXTERNAL_DEVICE_VERSION */
int memory_size; /* KB */
} AthrillExDeviceHeaderType;


#endif /* _ATHRILL_EXDEV_HEADER_H_ */
17 changes: 13 additions & 4 deletions src/main/cpuemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#endif /* OS_LINUX */
#include "athrill_device.h"
#include "assert.h"
#include "athrill_exdev_header.h"

static DeviceClockType cpuemu_dev_clock;
bool cpuemu_is_cui_mode = FALSE;
Expand Down Expand Up @@ -895,9 +896,17 @@ Std_ReturnType cpuemu_load_memmap(const char *path, MemoryAddressMapType *map)
printf("ERROR: Can not find shared library %s reason=%s\n", filepath, dlerror());
continue;
}
int *sizep = dlsym(handle, "ex_device_memory_size");
if (sizep == NULL) {
printf("ERROR: Can not find symbol(device_memory_size) on %s\n", filepath);
AthrillExDeviceHeaderType *ext_dev_headr = dlsym(handle, "athrill_ex_device");
if (ext_dev_headr == NULL) {
printf("ERROR: Can not find symbol(athrill_ex_device) on %s\n", filepath);
continue;
}
if (ext_dev_headr->magicno != ATHRILL_EXTERNAL_DEVICE_MAGICNO) {
printf("ERROR: magicno is invalid(0x%x) on %s\n", ext_dev_headr->magicno, filepath);
continue;
}
if (ext_dev_headr->version != ATHRILL_EXTERNAL_DEVICE_VERSION) {
printf("ERROR: version is invalid(0x%x) on %s\n", ext_dev_headr->version, filepath);
continue;
}
map->dev_num++;
Expand All @@ -906,7 +915,7 @@ Std_ReturnType cpuemu_load_memmap(const char *path, MemoryAddressMapType *map)
memp = &map->dev[map->dev_num - 1];
memp->type = MemoryAddressImplType_DEV;
memp->extdev_handle = handle;
memp->size = *sizep;
memp->size = ext_dev_headr->memory_size;
printf("DEV");
}
#endif /* OS_LINUX */
Expand Down

0 comments on commit 210362f

Please sign in to comment.