artiq/soc/runtime/main.c

118 lines
2.5 KiB
C
Raw Normal View History

2014-07-04 23:49:08 +08:00
#include <stdio.h>
#include <string.h>
2014-07-04 23:49:08 +08:00
#include <irq.h>
#include <uart.h>
#include <system.h>
2014-09-15 22:56:23 +08:00
#include <time.h>
#include <generated/csr.h>
2014-07-04 23:49:08 +08:00
2014-10-19 23:51:49 +08:00
#include "comm.h"
2014-07-04 23:49:08 +08:00
#include "elf_loader.h"
2014-09-21 23:36:10 +08:00
#include "exceptions.h"
#include "services.h"
2014-08-28 16:56:48 +08:00
#include "rtio.h"
#include "dds.h"
2014-07-24 07:10:49 +08:00
static unsigned char kcode[256*1024];
static struct symbol symtab[128];
static int _symtab_count;
static char _symtab_strings[128*16];
static char *_symtab_strptr;
static void symtab_init(void)
{
memset(symtab, 0, sizeof(symtab));
_symtab_count = 0;
_symtab_strptr = _symtab_strings;
}
static int symtab_add(const char *name, void *target)
{
if(_symtab_count >= sizeof(symtab)/sizeof(symtab[0])) {
2014-10-19 23:51:49 +08:00
comm_log("Too many provided symbols in object");
symtab_init();
return 0;
}
symtab[_symtab_count].name = _symtab_strptr;
symtab[_symtab_count].target = target;
_symtab_count++;
while(1) {
if(_symtab_strptr >= &_symtab_strings[sizeof(_symtab_strings)]) {
2014-10-19 23:51:49 +08:00
comm_log("Provided symbol string table overflow");
symtab_init();
return 0;
}
*_symtab_strptr = *name;
_symtab_strptr++;
if(*name == 0)
break;
name++;
}
return 1;
}
static int load_object(void *buffer, int length)
{
symtab_init();
return load_elf(
resolve_service_symbol, symtab_add,
buffer, length, kcode, sizeof(kcode));
}
2014-07-06 04:47:23 +08:00
typedef void (*kernel_function)(void);
2014-07-04 23:49:08 +08:00
2014-09-21 23:36:10 +08:00
static int run_kernel(const char *kernel_name, int *eid)
2014-07-04 23:49:08 +08:00
{
kernel_function k;
2014-09-22 13:18:48 +08:00
void *jb;
2014-07-04 23:49:08 +08:00
k = find_symbol(symtab, kernel_name);
if(k == NULL) {
2014-10-19 23:51:49 +08:00
comm_log("Failed to find kernel entry point '%s' in object", kernel_name);
2014-09-21 23:36:10 +08:00
return KERNEL_RUN_STARTUP_FAILED;
}
2014-09-22 13:18:48 +08:00
jb = exception_push();
if(exception_setjmp(jb)) {
2014-09-22 13:18:48 +08:00
*eid = exception_getid();
2014-09-21 23:36:10 +08:00
return KERNEL_RUN_EXCEPTION;
2014-09-22 13:18:48 +08:00
} else {
2014-09-21 23:36:10 +08:00
rtio_init();
flush_cpu_icache();
k();
exception_pop(1);
2014-09-21 23:36:10 +08:00
return KERNEL_RUN_FINISHED;
}
}
2014-09-15 22:56:23 +08:00
static void blink_led(void)
{
int i, ev, p;
p = identifier_frequency_read()/10;
time_init();
for(i=0;i<3;i++) {
leds_out_write(1);
while(!elapsed(&ev, p));
leds_out_write(0);
while(!elapsed(&ev, p));
}
}
int main(void)
{
2014-09-05 12:03:22 +08:00
irq_setmask(0);
irq_setie(1);
uart_init();
2014-09-22 13:18:48 +08:00
2014-09-05 12:03:22 +08:00
puts("ARTIQ runtime built "__DATE__" "__TIME__"\n");
dds_init();
2014-09-15 22:56:23 +08:00
blink_led();
2014-10-19 23:51:49 +08:00
comm_serve(load_object, run_kernel);
2014-09-05 12:03:22 +08:00
return 0;
2014-07-04 23:49:08 +08:00
}