#include "task.h"
#include "spec.h"
#include "interface.h"
+#include "mem.h"
-struct task tasks[MAXTASKS];
+extern uint8_t *mem_top, *mem_bottom, *mem_task, *mem_sds;
-void task_init(void)
-{
- memset(&tasks, 0, sizeof(struct task)*MAXTASKS);
-}
+uint8_t taskid = 0;
void task_register(void)
{
- uint8_t ct;
- uint16_t i;
+ debug("free memory: %lu\n", mem_free());
+ int i;
+ if(mem_task+sizeof(struct task) > mem_sds){
+ die("Out of memory... Not enough to allocate taskstruct");
+ }
- for(ct = 0; ct<MAXTASKS; ct++)
- if(!tasks[ct].used)
- break;
- if(ct == MAXTASKS)
- die("Trying to add too much tasks...");
+ struct task *t = (struct task *)mem_task;
- memset(&tasks[ct], 0, sizeof(struct task));
//Read interval
- tasks[ct].interval = read16();
- debug("interval: %d\n", tasks[ct].interval);
+ t->interval = read16();
+ debug("interval: %d\n", t->interval);
//Interrupt task
- if(is_interrupt_task(&tasks[ct])) {
+ if(is_interrupt_task(t)) {
}
//Read tasklength
- tasks[ct].tlen = read16();
- debug("task interval: %d, length: %d\n",
- tasks[ct].interval, tasks[ct].tlen);
+ t->tasklength = read16();
+ debug("task interval: %d, length: %d\n", t->interval, t->tasklength);
+
+ if(mem_task+t->tasklength > mem_sds){
+ die("Out of memory... Not enough to allocate bytes");
+ }
+
+ mem_task += sizeof(struct task);
+ t->bc = mem_task;
+ mem_task += t->tasklength;
- if(tasks[ct].tlen > MAXTASKSIZE)
- die("Task is too long: %d", tasks[ct].tlen);
//Read task bytecode
- for(i = 0; i<tasks[ct].tlen; i++){
- tasks[ct].bc[i] = read_byte();
+ for(i = 0; i<t->tasklength; i++){
+ t->bc[i] = read_byte();
}
+
//Return the task number for later removal
- debug("Received a task of length %d", tasks[ct].tlen);
- tasks[ct].used = true;
- tasks[ct].lastrun = 0L;
+ debug("Received a task of length %d", t->tasklength);
+ t->lastrun = 0L;
+ t->taskid = taskid++;
+ t->value = 0;
write_byte('t');
- write16(ct);
+ write16(t->taskid);
+ write16(mem_free());
write_byte('\n');
+ debug("free memory: %lu\n", mem_free());
}
bool is_interrupt_task(struct task *t)
(void)t;
}
-void task_delete(void)
+struct task *task_head(void)
{
- uint8_t c = read16();
- tasks[c].used = false;
- write_byte('d');
- write16(c);
- write_byte('\n');
+ return mem_task == mem_bottom ? NULL : (struct task *)mem_bottom;
}
-struct task *task_get(int num)
+struct task *task_next(struct task *t)
{
- return tasks[num].used ? &tasks[num] : NULL;
+ uint8_t *next = (uint8_t *)t + sizeof(struct task) + t->tasklength;
+ return next >= mem_task ? NULL : (struct task *)next;
+}
+
+void task_delete(uint8_t c)
+{
+ debug("Going to delete task: %i", c);
+ struct task *t = task_head();
+ while(t != NULL){
+ if(t->taskid == c){
+ break;
+ }
+ t = task_next(t);
+ }
+
+ if(t != NULL){
+ //We found the task, now we move everything from the end of the task up
+ //to the spacepointer right
+ uint8_t *start = (uint8_t *)t;
+ uint8_t *end = start + sizeof(struct task) + t->tasklength;
+ debug("Moving %lu bytes\n", mem_task-end);
+ for(int i = 0; i<mem_task-end; i++){
+ start[i] = end[i];
+ }
+
+ //Decrement the spacepointer
+ mem_task -= end-start;
+ }
}