add tracing and stepping
authorMart Lubbers <mart@martlubbers.net>
Wed, 14 Dec 2016 09:55:20 +0000 (10:55 +0100)
committerMart Lubbers <mart@martlubbers.net>
Wed, 14 Dec 2016 09:55:20 +0000 (10:55 +0100)
int/Makefile
int/int.c

index 4539229..f6e3402 100644 (file)
@@ -1,6 +1,6 @@
 CFLAGS:=-g -Wall -Wextra -Werror
 all: mTaskSymbols.h int
 
-mTaskSymbols.h: 
+mTaskSymbols.h:
        CLMFLAGS=-nr make -BC .. mTaskInterpret
        ../mTaskInterpret > $@
index a2dd745..9d6fde3 100644 (file)
--- a/int/int.c
+++ b/int/int.c
 
 #include "mTaskSymbols.h"
 
+#define PORT 8123
+
 #define MAXTASKS 5
 #define MAXTASKSIZE 1024
 #define MAXSDS 50
 #define STACKSIZE 1024
 
+#define MSG_TASK 't'
+#define MSG_SDS 's'
+
 #define DEBUG
+
 #ifdef DEBUG
 #define debug(s, ...) printf(s, ##__VA_ARGS__);
+#define trace(op, ...) printf("pc: %d, sp: %d, op: " op, pc, sp, ##__VA_ARGS__);
 #else
 #define debug(s, ...) ;
+#define trace(pc, sp, op) ;
 #endif
 
 #define pdie(s) {perror(s); exit(1);}
 #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);}
 
-char tasks[MAXTASKS][MAXTASKSIZE] = {0};
-bool tasks_used[MAXTASKS] = {0};
+struct task {
+       uint8_t bc[MAXTASKSIZE];
+       uint16_t tlen;
+       bool used;
+};
+
+struct task tasks[MAXTASKS] = {0};
 int sock_fd = -1;
 int fd = -1;
 
@@ -54,146 +67,176 @@ void read_message(int fd_in, int fd_out)
 {
        //Find next task
        uint8_t c, ct;
-       uint16_t tasklen;
 
        for(ct = 0; ct<MAXTASKS; ct++)
-               if(!tasks_used[ct])
+               if(!tasks[ct].used)
                        break;
        if(ct == MAXTASKS)
                die("Trying to add too much tasks...\n");
 
        debug("Receiving input for task %d\n", ct);
        read(fd_in, &c, 1);
-       if(c == 's') {
+       switch(c){
+       case MSG_SDS:
                debug("Receiving an sds\n");
-       } else if(c == 't'){
+               break;
+       case MSG_TASK:
                read(fd_in, &c, 1);
-               tasklen = 256*c;
+               tasks[ct].tlen = 256*c;
                read(fd_in, &c, 1);
-               tasklen += c;
-               if(tasklen > MAXTASKSIZE)
-                       die("Task is too long: %d\n", tasklen);
-               for(int i = 0; i<tasklen; i++){
+               tasks[ct].tlen += c;
+               if(tasks[ct].tlen > MAXTASKSIZE)
+                       die("Task is too long: %d\n", tasks[ct].tlen);
+               for(int i = 0; i<tasks[ct].tlen; i++){
                        debug("Read %d\n", i);
-                       read(fd_in, tasks[ct]+i, 1);
+                       read(fd_in, tasks[ct].bc+i, 1);
                        debug("t[][%i]: %d\n", i,
-                               tasks[ct][i]);
+                               tasks[ct].bc[i]);
                }
                //Return the task number for later removal
                write(fd_out, (void *)&ct, 1);
-               debug("Received a task of length %d\n", tasklen);
-               tasks_used[ct] = true;
-       } else {
+               debug("Received a task of length %d\n", tasks[ct].tlen);
+               tasks[ct].used = true;
+               break;
+       default:
                debug("Unknown message: %X?\n", c);
        }
 }
 
-void run_task(char *program)
+void run_task(int tasknum)
 {
+       uint8_t *program = tasks[tasknum].bc;
+       int plen = tasks[tasknum].tlen;
        int pc = 0;
        int sp = 0;
-       int plen = strlen(program);
-       char stack[1024] = {0};
+       char stack[STACKSIZE] = {0};
+       printf("Running task: %d\nWith length: %d\n", tasknum, plen);
        while(pc != plen){
+               printf("program: %x\n", program[pc]);
                switch(program[pc++]){
                case BCNOP:
+                       trace("nop\n");
                        break;
                case BCPUSH:
+                       trace("push %d\n", program[pc]);
                        stack[sp++] = program[pc++];
                        break;
                case BCPOP:
+                       trace("pop\n");
                        sp--;
                        break;
                case BCNOT:
+                       trace("not\n");
                        stack[sp] = stack[sp] > 0 ? 0 : 1;
                        break;
                case BCADD:
+                       trace("add\n");
                        stack[sp-1] = stack[sp] + stack[sp-1];
                        sp -= 1;
                        break;
                case BCSUB:
+                       trace("sub\n");
                        stack[sp-1] = stack[sp] - stack[sp-1];
                        sp -= 1;
                        break;
                case BCMUL:
+                       trace("mul\n");
                        stack[sp-1] = stack[sp] * stack[sp-1];
                        sp -= 1;
                        break;
                case BCDIV:
+                       trace("div\n");
                        stack[sp-1] = stack[sp] / stack[sp-1];
                        sp -= 1;
                        break;
                case BCAND:
+                       trace("and\n");
                        stack[sp-1] = stack[sp] && stack[sp-1];
                        sp -= 1;
                        break;
                case BCOR:
+                       trace("or\n");
                        stack[sp-1] = stack[sp] || stack[sp-1];
                        sp -= 1;
                        break;
                case BCEQ:
+                       trace("eq\n");
                        stack[sp-1] = stack[sp] == stack[sp-1];
                        sp -= 1;
                        break;
                case BCNEQ:
+                       trace("neq\n");
                        stack[sp-1] = stack[sp] != stack[sp-1];
                        sp -= 1;
                        break;
-               case BCLES: stack[sp-1] = stack[sp] < stack[sp-1];
+               case BCLES:
+                       trace("les\n");
+                       stack[sp-1] = stack[sp] < stack[sp-1];
                        sp -= 1;
                        break;
                case BCGRE:
+                       trace("gre\n");
                        stack[sp-1] = stack[sp] > stack[sp-1];
                        sp -= 1;
                        break;
                case BCLEQ:
+                       trace("leq\n");
                        stack[sp-1] = stack[sp] <= stack[sp-1];
                        sp -= 1;
                        break;
                case BCGEQ:
+                       trace("geq\n");
                        stack[sp-1] = stack[sp] >= stack[sp-1];
                        sp -= 1;
                        break;
                case BCJMP:
+                       trace("jmp to %d\n", program[pc]);
                        pc = pc + program[pc];
                        break;
                case BCJMPT:
+                       trace("jmpt to %d\n", program[pc]);
                        pc += stack[sp] ? program[pc] : 1;
                        break;
                case BCJMPF:
+                       trace("jmpf to %d\n", program[pc]);
                        pc += stack[sp] ? 1 : program[pc];
                        break;
                case BCSERIALAVAIL:
-                       printf("SerialAvailable()\n");
+                       trace("SerialAvailable()\n");
                        break;
                case BCSERIALPRINT:
-                       printf("SerialPrint()\n");
+                       trace("SerialPrint()\n");
                        break;
                case BCSERIALPRINTLN:
-                       printf("SerialPrintln()\n");
+                       trace("SerialPrintln()\n");
                        break;
                case BCSERIALREAD:
-                       printf("SerialRead()\n");
+                       trace("SerialRead()\n");
                        break;
                case BCSERIALPARSEINT:
-                       printf("SerialParseInt()\n");
+                       trace("SerialParseInt()\n");
                        break;
                case BCANALOGREAD:
-                       printf("AnalogRead(%d)\n", program[pc++]);
+                       trace("AnalogRead(%d)\n", program[pc]);
+                       pc++;
                        break;
                case BCANALOGWRITE:
-                       printf("AnalogWrite(%d)\n", program[pc++]);
+                       trace("AnalogWrite(%d)\n", program[pc]);
+                       pc++;
                        break;
                case BCDIGITALREAD:
-                       printf("DigitalRead(%d)\n", program[pc++]);
+                       trace("DigitalRead(%d)\n", program[pc]);
+                       pc++;
                        break;
                case BCDIGITALWRITE:
-                       printf("DigitalWrite(%d)\n", program[pc++]);
+                       trace("DigitalWrite(%d)\n", program[pc]);
+                       pc++;
                        break;
                default:
                        die("Unrecognized command: %d\n", program[--pc]);
                }
        }
+       debug("Task %d terminated\n", tasknum);
 }
 
 void open_filedescriptors()
@@ -203,7 +246,7 @@ void open_filedescriptors()
        bzero((char *) &sa, sizeof(sa));
        sa.sin_family = AF_INET;
        sa.sin_addr.s_addr = INADDR_ANY;
-       sa.sin_port = htons(8124);
+       sa.sin_port = htons(PORT);
 
        if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
                pdie("socket");
@@ -212,7 +255,7 @@ void open_filedescriptors()
        if(listen(sock_fd, 10) == -1)
                pdie("bind\n");
 
-       printf("Listinging on 8123\n");
+       printf("Listening on %d\n", PORT);
        fflush(stdout);
        if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
                pdie("accept");
@@ -234,38 +277,30 @@ int main(void)
 
        while(true){
                //Check for new tasks
-               if(input_available(fd)){
+               if(input_available(fd))
                        read_message(fd, fd);
-               }
                //Run tasks
                for(ct = 0; ct<MAXTASKS; ct++){
-                       if(!tasks_used[ct]){
+                       if(!tasks[ct].used){
                                debug("Task %d not implemented\n", ct);
                                continue;
                        }
-                       debug("Going to execute task %d\n", ct);
-                       run_task(tasks[ct]);
-                       exit(1);
+#ifdef DEBUG
+                       printf("Current task to run: %d\n", ct);
+                       printf("Enter to continue, s to step\n");
+                       switch(getchar()){
+                       case 'd':
+                               die("Killed\n");
+                       case 's':
+                               printf("step\n");
+                               break;
+                       default:
+                               break;
+                       }
+#endif
+                       run_task(ct);
                }
-//             exit(1);
                usleep(10);
        }
-
-/*     //Read program
-       pc = 0;
-       while ((c = getchar()) != EOF && pc < PROGRAMSIZE)
-               program[pc++] = c;
-       if (pc >= PROGRAMSIZE)
-               die("Max program size: %d\n", PROGRAMSIZE);
-       pl = pc;
-       debug("Done reading, program length: %d\n", pl);
-
-       //Evaluate program
-       //Reset program counter and stack counter
-       pc = 0;
-       sp = 0;
-       while(pc != pl){
-       }
-*/
        return 0;
 }