file loading, switching, good!

master
Clyne Sullivan 7 years ago
parent 7eb572702c
commit 0726b60a6e

@ -23,6 +23,11 @@
#include <stdint.h>
typedef struct {
uint32_t size;
void *next;
} __attribute__ ((packed)) alloc_t;
/**
* Initializes memory management of the given heap.
* No overflow stuff is done, so...

@ -38,6 +38,7 @@ uint8_t initrd_validate(void);
* @return the file's name, or zero if not found
*/
char *initrd_getfile(uint32_t index);
char *initrd_getname(uint32_t index);
/**
* Gets contents of the given file.

@ -54,4 +54,6 @@ void task_start(void (*task)(void), uint16_t stackSize);
*/
void task_hold(uint8_t hold);
//int fork(void);
#endif // TASK_H_

@ -1,3 +0,0 @@
choice = filemenu
print("\nChoice: ")
print(choice)

@ -0,0 +1,8 @@
while (1) {
print("> ")
input = gets()
answer = solve(input)
print("\n")
print(answer)
print("\n")
}

@ -1,8 +1,10 @@
while (1) {
print("> ")
input = gets()
answer = solve(input)
print("\n")
print(answer)
rect(0, 0, 480, 320, 0)
ppos(0, 0)
print("Free mem: ")
print(freemem())
print("\n")
choice = filemenu()
program(choice)
}

@ -1,8 +1,9 @@
do
getkey > input
print input
delay 1000
while (1)
while (1) {
input = getkey()
print(input)
print("\n")
delay(1000)
}
#do
# getkey > input

@ -26,4 +26,7 @@ while (1) {
j = 50 + rand(219)
line(x, y, i, j, 511)
# for exiting
getkey()
}

Binary file not shown.

@ -25,6 +25,11 @@
#include <task.h>
#include <clock.h>
#define C_WIDTH 12
#define C_HEIGHT 16
#define S_WIDTH 40
#define S_HEIGHT 20
volatile uint8_t lock = 0;
#define LOCK while (lock) { delay(5); } task_hold(1); lock = 1
#define UNLOCK task_hold(0); lock = 0
@ -33,27 +38,24 @@ static unsigned int curx = 0;
static unsigned int cury = 0;
static unsigned int curxo = 0;
static unsigned int curyo = 0;
//extern const unsigned char inconsolata24[192 * 156 * 2 + 1];
static unsigned char *inconsolata24;
static unsigned char *font;
void task_cursor(void)
{
while (1) {
int x = curxo + curx * 12;
int y = curyo + cury * 26;
dsp_rect(x, y + 24, 12, 1, 0xFFFF);
int x = curxo + curx * C_WIDTH;
int y = curyo + cury * C_HEIGHT;
dsp_rect(x, y + C_HEIGHT, C_WIDTH, 1, 0xFFFF);
delay(300);
dsp_rect(x, y + 24, 12, 1, 0);
dsp_rect(x, y + C_HEIGHT, C_WIDTH, 1, 0);
delay(300);
}
}
void dsp_cursoron(void)
{
inconsolata24 = malloc(192 * 156 * 2);
flash_read((char *)inconsolata24, 0, 192 * 156 * 2);
font = malloc(32 * 256);
flash_read((char *)font, 0, 32 * 256);
task_start(task_cursor, 512);
}
@ -62,7 +64,7 @@ void dsp_putchar(int c)
LOCK;
if (c == '\n') {
curx = 0;
if (++cury == 12) {
if (++cury == S_HEIGHT) {
UNLOCK;
dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, 0);
cury = 0;
@ -73,35 +75,35 @@ void dsp_putchar(int c)
if (curx > 0)
curx--;
UNLOCK;
dsp_rect(curxo + curx * 12, curyo + cury * 26, 12, 26, 0);
dsp_rect(curxo + curx * C_WIDTH, curyo + cury * C_HEIGHT,
C_WIDTH, C_HEIGHT, 0);
return;
}
if (c > 0x7F)
goto end;
unsigned int x = curxo + curx * C_WIDTH;
unsigned int y = curyo + cury * C_HEIGHT;
dsp_set_addr(x, y, x + C_WIDTH - 1, y + C_HEIGHT - 1);
unsigned int start = ((c - ' ') / 16 * 192 * 26 + (c % 16) * 12) * 2;
unsigned int x = curxo + curx * 12;
unsigned int y = curyo + cury * 26;
dsp_set_addr(x, y, x + 11, y + 25);
// for each row
for (unsigned int i = 0; i < 26; i++) {
// for each column
for (int j = 12 * 2 - 1; j >= 0; j--)
dsp_write_data(inconsolata24[start + (i * 192 * 2) + j]);
uint32_t base = c * 32;
for (unsigned int j = 0; j < 16; j++) {
uint16_t row = (font[base + j * 2] << 8) | font[base + j * 2 + 1];
for (int i = 4; i < 16; i++) {
uint8_t color = (row & (1 << i)) ? 0xFF : 0;
dsp_write_data(color);
dsp_write_data(color);
}
}
if (++curx == 40) {
if (++curx == S_WIDTH) {
curx = 0;
if (++cury == 12) {
if (++cury == S_HEIGHT) {
UNLOCK;
dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, 0);
LOCK;
cury = 0;
}
}
end:
UNLOCK;
}

File diff suppressed because it is too large Load Diff

@ -22,11 +22,6 @@
#define HEAP_ALIGN 4
typedef struct {
uint32_t size;
void *next;
} __attribute__ ((packed)) alloc_t;
static alloc_t *free_blocks;
static void *heap_end;
uint32_t heap_used;

@ -23,6 +23,7 @@
*/
#include <initrd.h>
#include <heap.h>
#include <string.h>
extern uint8_t _binary_initrd_img_start[];
@ -50,6 +51,18 @@ char *initrd_getfile(uint32_t offset)
return ptr;
}
char *initrd_getname(uint32_t offset)
{
char *file = initrd_getfile(offset);
if (file == 0)
return 0;
uint32_t len = *((uint32_t *)file);
char *buf = malloc(len + 1);
strncpy(buf, file + 4, len);
buf[len] = '\0';
return buf;
}
char *initrd_readfile(const char *name)
{
char *ptr;

@ -57,12 +57,12 @@ static const port_t keypad_cols[COLS] = {
#define K_HOLD 0x000001FF
static const char keypad_map[ROWS * COLS * 4] = {
"\xFF\0\0\0" ">\0\0\0" ">=\0\0" "==\0\0" "=\0\0\0"
"\x7F\0\0\0" ">\0\0\0" ">=\0\0" "==\0\0" "=\0\0\0"
"x\0\0\0" "<\0\0\0" "<=\0\0" "!=\0\0" "%\0\0\0"
"7\0\0\0" "8\0\0\0" "9\0\0\0" "(\0\0\0" ")\0\0\0"
"4\0\0\0" "5\0\0\0" "6\0\0\0" "/\0\0\0" "*\0\0\0"
"1\0\0\0" "2\0\0\0" "3\0\0\0" "-\0\0\0" "+\0\0\0"
".\0\0\0" "0\0\0\0" "\0\0\0\0" "\b\0\0\0" "\n\0\0\0"
".\0\0\0" "0\0\0\0" "\xFF\0\0\0" "\b\0\0\0" "\n\0\0\0"
};
static const char keypad_map_2nd[ROWS * COLS * 4] = {
@ -71,7 +71,7 @@ static const char keypad_map_2nd[ROWS * COLS * 4] = {
"k\0\0\0" "l\0\0\0" "m\0\0\0" "n\0\0\0" "o\0\0\0"
"p\0\0\0" "q\0\0\0" "r\0\0\0" "s\0\0\0" "t\0\0\0"
"u\0\0\0" "v\0\0\0" "w\0\0\0" "x\0\0\0" "y\0\0\0"
"z\0\0\0" "\0\0\0\0" "\0\0\0\0" "\0\0\0\0" "\xFF\x01\0\0"
"z\0\0\0" "\0\0\0\0" "\0\0\0\0" "\x7F\0\0\0" "\xFF\x01\0\0"
};
#define KEY(r, c, i) map[r * COLS * 4 + c * 4 + i]

@ -53,9 +53,9 @@ int main(void)
clock_init();
heap_init(&__bss_end__);
random_init();
gpio_init();
serial_init();
random_init();
keypad_init();
flash_init();
@ -73,16 +73,9 @@ int main(void)
void kmain(void)
{
dsp_init();
dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0));
dsp_cursoron();
/*extern const unsigned char inconsolata24[192 * 156 * 2 + 1];
for (uint32_t i = 0; i <= 192 * 156 * 2; i += 624) {
flash_write((char *)(inconsolata24 + i), i, 624);
dsp_puts(".");
}*/
keypad_start();
task_start(task_interpreter, 4096);
while (1) {
@ -93,22 +86,23 @@ void kmain(void)
}
}
void task_interpreter(void)
instance *load_program(const char *name)
{
instance *it = inewinstance();
script_loadlib(it);
// load '/init' file
char *s = initrd_readfile("init");
// load file
char *s = initrd_readfile(name);
if (s == 0) {
dsp_puts("can't find init");
goto end;
dsp_puts("can't find ");
dsp_puts(name);
goto fail;
}
instance *it = inewinstance();
script_loadlib(it);
// read in, parse into script code
char *linebuf = (char *)malloc(120);
uint32_t i = 0, prev = 0, lc;
uint32_t size = initrd_filesize("init");
uint32_t size = initrd_filesize(name);
int ret = 0;
while (i < size) {
for (; s[i] != '\n' && s[i] != '\0'; i++);
@ -125,21 +119,30 @@ void task_interpreter(void)
prev = ++i;
}
free(linebuf);
return it;
fail:
while (1);
return 0;
}
void task_interpreter(void)
{
instance *it = load_program("init");
// run the script
ret = irun(it);
if (ret != 0)
goto fail;
/*int ret =*/ irun(it);
//if (ret != 0)
//goto end;
idelinstance(it);
end:
//end:
while (1)
delay(10);
fail:
/*fail:
if (ret < 0) {
dsp_puts("\nError: ");
dsp_puts(itoa(ret, linebuf, 10));
}
goto end;
goto end;*/
}

@ -22,6 +22,7 @@
#include <it/builtins.h>
#include <clock.h>
#include <ctype.h>
#include <display.h>
#include <display_draw.h>
#include <heap.h>
@ -48,6 +49,8 @@ int script_getkey(instance *it);
int script_pixel(instance *it);
int script_menu(instance *it);
int script_filemenu(instance *it);
int script_program(instance *it);
int script_free(instance *it);
int math_sin(instance *it);
@ -71,6 +74,8 @@ void script_loadlib(instance *it)
inew_cfunc(it, "menu", script_menu);
inew_cfunc(it, "filemenu", script_filemenu);
inew_cfunc(it, "program", script_program);
inew_cfunc(it, "freemem", script_free);
inew_cfunc(it, "sin", math_sin);
}
@ -96,6 +101,14 @@ int script_menu(instance *it)
dsp_puts("\n");
resps[i] = igetarg(it, 2 + i * 2)->value.f;
}
int c;
do c = keypad_get();
while (c == 0);
variable *v = make_varf(0, isdigit(c) ? c - '0' : -1.0f);
ipush(it, (uint32_t)v);
free(resps);
return 0;
}
@ -103,18 +116,25 @@ int script_menu(instance *it)
int script_filemenu(instance *it)
{
char listbuf[4];
char *buf = calloc(17, 1);
char *fname;
strncpy(listbuf, " : \0", 4);
dsp_puts("Choose a file: \n");
for (unsigned int i = 0; (fname = initrd_getfile(i)) != 0; i++) {
for (unsigned int i = 0; (fname = initrd_getname(i)) != 0; i++) {
listbuf[0] = i + '0';
dsp_puts(listbuf);
dsp_puts(strncpy(buf, fname, 16));
dsp_puts(fname);
free(fname);
dsp_puts("\n");
}
free(buf);
return script_gets(it);
int c;
do c = keypad_get();
while (c == 0);
variable *v = make_varf(0, isdigit(c) ? c - '0' : -1.0f);
ipush(it, (uint32_t)v);
return 0;
}
int script_puts(instance *it)
@ -154,6 +174,12 @@ int script_gets(instance *it)
c[0] = keypad_get();
delay(1);
} while (c[0] == 0);
if (c[0] == 0x7F) {
it->lnidx = 998;
break;
}
//c[0] = serial_get();
s[index] = c[0];
if (c[0] == '\b' || c[0] == 127) {
@ -227,7 +253,10 @@ int script_rand(instance *it)
int script_getkey(instance *it)
{
variable *v = make_varf(0, (float)keypad_get());
char c = keypad_get();
if (c == 0x7F)
it->lnidx = 998;
variable *v = make_varf(0, c);
ipush(it, (uint32_t)v);
return 0;
}
@ -239,3 +268,30 @@ int script_pixel(instance *it)
return 0;
}
extern instance *load_program(const char *name);
int script_program(instance *it)
{
int initrdOffset = (int)igetarg(it, 0)->value.f;
char *name = initrd_getname(initrdOffset);
dsp_rect(0, 0, 480, 320, 0);
dsp_cpos(0, 0);
dsp_coff(0, 0);
instance *it2 = load_program(name);
free(name);
int ret = irun(it2);
if (ret != 0)
return -1;
idelinstance(it2);
return 0;
}
int script_free(instance *it)
{
extern uint32_t heap_used;
ipush(it, (uint32_t)make_varf(0, 98303 - heap_used));
return 0;
}

@ -21,6 +21,7 @@
#include <task.h>
#include <heap.h>
#include <stm32l476xx.h>
#include <string.h>
task_t *current;
static uint8_t task_disable = 0;
@ -35,9 +36,9 @@ void task_hold(uint8_t hold)
void task_exit(void)
{
// TODO free stack?
// TODO remove from chain
// hopefully current is preserved..?
free(current->stack);
// TODO remove from chain?
// hopefully this is fine...
while (1); // bye
}
@ -47,7 +48,7 @@ task_t *task_create(void (*code)(void), uint32_t stackSize)
t->next = 0;
t->stack = (uint32_t *)malloc(stackSize);
void *sp = (uint8_t *)t->stack + stackSize - 68; // excep. stack + regs
t->sp = (uint32_t *)sp;
t->sp = sp;
for (uint8_t i = 0; i < 14; i++)
t->sp[i] = 0;
t->sp[8] = 0xFFFFFFFD;
@ -67,7 +68,7 @@ void task_init(void (*init)(void))
asm("\
msr psp, %0; \
mrs r0, control; \
orr r0, r0, #3; \
orr r0, r0, #2; \
cpsie i; \
msr control, r0; \
isb; \
@ -84,6 +85,32 @@ void task_start(void (*task)(void), uint16_t stackSize)
task_hold(0);
}
/*int fork_ret(void)
{
return 1;
}
int fork(void)
{
void (*pc)(void) = (void (*)(void))((uint32_t)fork_ret & ~(3));
task_hold(1);
// duplicate task info
alloc_t *heapInfo = (alloc_t *)(current->stack - 2);
task_t *t = task_create(pc, heapInfo->size);
memcpy(t->stack, current->stack, heapInfo->size);
uint32_t *sp;
asm("mov %0, sp" : "=r" (sp));
t->sp = t->stack + (sp - current->stack);
t->next = current->next;
current->next = t;
current = t;
task_hold(0);
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
return 0;
}*/
__attribute__ ((naked))
void PendSV_Handler(void)
{
@ -105,28 +132,5 @@ void PendSV_Handler(void)
msr psp, r0; \
bx lr; \
");
/*// save state
asm("\
cpsid i; \
isb; \
dsb; \
mrs r0, psp; \
stmdb r0!, {r4-r11, lr}; \
mov %0, r0; \
" : "=r" (current->sp));
current = current->next;
// restore
asm("\
mov r0, %0; \
ldmia r0!, {r4-r11, lr}; \
msr psp, r0; \
isb; \
dsb; \
cpsie i; \
bx lr; \
" :: "r" (current->sp));*/
}

Loading…
Cancel
Save