2014-07-04 23:49:08 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-07-07 03:06:53 +08:00
|
|
|
#include <stdarg.h>
|
2014-07-04 23:49:08 +08:00
|
|
|
|
|
|
|
#include <irq.h>
|
|
|
|
#include <uart.h>
|
|
|
|
#include <console.h>
|
|
|
|
#include <system.h>
|
|
|
|
#include <generated/csr.h>
|
|
|
|
|
|
|
|
#include "elf_loader.h"
|
|
|
|
|
|
|
|
static void receive_sync(void)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int recognized;
|
|
|
|
|
|
|
|
recognized = 0;
|
|
|
|
while(1) {
|
|
|
|
c = readchar();
|
|
|
|
if(c == 0x5a) {
|
|
|
|
recognized++;
|
|
|
|
if(recognized == 4)
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
recognized = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int receive_length(void)
|
|
|
|
{
|
2014-07-06 01:39:49 +08:00
|
|
|
unsigned int r;
|
|
|
|
int i;
|
2014-07-04 23:49:08 +08:00
|
|
|
|
|
|
|
r = 0;
|
|
|
|
for(i=0;i<4;i++) {
|
|
|
|
r <<= 8;
|
2014-07-06 01:39:49 +08:00
|
|
|
r |= (unsigned char)readchar();
|
2014-07-04 23:49:08 +08:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int download_kernel(void *buffer, int maxlength)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int i;
|
|
|
|
unsigned char *_buffer = buffer;
|
|
|
|
|
|
|
|
receive_sync();
|
|
|
|
length = receive_length();
|
|
|
|
if(length > maxlength)
|
|
|
|
return -1;
|
|
|
|
for(i=0;i<length;i++)
|
|
|
|
_buffer[i] = readchar();
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2014-07-07 03:06:53 +08:00
|
|
|
static int rpc(int rpc_num, int n_args, ...)
|
2014-07-06 01:39:49 +08:00
|
|
|
{
|
2014-07-07 03:06:53 +08:00
|
|
|
printf("rpc_num=%d n_args=%d\n", rpc_num, n_args);
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, n_args);
|
|
|
|
while(n_args--)
|
|
|
|
printf("%d\n", va_arg(args, int));
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return 1;
|
2014-07-06 01:39:49 +08:00
|
|
|
}
|
|
|
|
|
2014-07-06 04:46:43 +08:00
|
|
|
static void gpio_set(int channel, int level)
|
|
|
|
{
|
|
|
|
leds_out_write(!!level);
|
|
|
|
}
|
|
|
|
|
2014-07-06 01:39:49 +08:00
|
|
|
static const struct symbol syscalls[] = {
|
2014-07-07 03:06:53 +08:00
|
|
|
{"__syscall_rpc", rpc},
|
2014-07-06 04:46:43 +08:00
|
|
|
{"__syscall_gpio_set", gpio_set},
|
2014-07-06 01:39:49 +08:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2014-07-06 04:47:23 +08:00
|
|
|
typedef void (*kernel_function)(void);
|
2014-07-04 23:49:08 +08:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
unsigned char kbuf[256*1024];
|
|
|
|
unsigned char kcode[256*1024];
|
|
|
|
kernel_function k = (kernel_function)kcode;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
irq_setmask(0);
|
|
|
|
irq_setie(1);
|
|
|
|
uart_init();
|
|
|
|
|
|
|
|
puts("ARTIQ runtime built "__DATE__" "__TIME__"\n");
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
length = download_kernel(kbuf, sizeof(kbuf));
|
|
|
|
if(length > 0) {
|
2014-07-06 01:39:49 +08:00
|
|
|
load_elf(syscalls, kbuf, length, kcode, sizeof(kcode));
|
2014-07-04 23:49:08 +08:00
|
|
|
flush_cpu_icache();
|
2014-07-06 04:47:23 +08:00
|
|
|
k();
|
2014-07-04 23:49:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|