]> code.bitgloo.com Git - clyne/calculator.git/commitdiff
various fixes; new interpreter library
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 21 Mar 2018 16:20:44 +0000 (12:20 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 21 Mar 2018 16:20:44 +0000 (12:20 -0400)
include/clock.h
include/display.h
include/display_draw.h
include/it
include/script.h
initrd/init
libinterp.a
src/heap.c
src/main.c
src/script.c

index 77ab7e921a309a8aece8a846a4004a911e4406e8..c12c41effc55486482f0ceb5d8bdf4a693e840a4 100644 (file)
@@ -10,7 +10,6 @@
 
 /**
  * Sets HCLK (system clock) to 80MHz, the maximum.
- * @param none
  */
 extern void clock_init(void);
 
index 6004f19bfb5c1f0fee0f92a0f6a35ae600746936..55f69746f65311de3930fa6280555de10ff76c36 100644 (file)
@@ -1,18 +1,55 @@
+/*
+ * @file display.h
+ * Display library for ILI9481 display.
+ */
+
 #ifndef DISPLAY_H_
 #define DISPLAY_H_
 
 #include <stdint.h>
 
+/**
+ * The screen's width, in pixels.
+ */
 #define LCD_WIDTH  480
-#define LCD_HEIGHT 320
 
-#define COLOR_MAX 31
+/**
+ * The screen's height, in pixels.
+ */
+#define LCD_HEIGHT 320
 
+/**
+ * Returns the color integer for the given RGB values.
+ * Converts 8RGB to 5-6-5.
+ * @param r red value, 0-255
+ * @param g green value, 0-255
+ * @param b blue value, 0-255
+ * @return the 5-6-5 color value
+ */
 uint16_t dsp_color(uint8_t r, uint8_t g, uint8_t b);
 
+/**
+ * Sets the direction of IO, for reading or writing to the screen.
+ * @param mode INPUT or OUPUT (defined in gpio.h)
+ */
 void dsp_dmode(int mode);
+
+/**
+ * Writes the command byte to the display.
+ * @param data the command to write
+ */
 void dsp_write_cmd(uint8_t data);
+
+/**
+ * Writes the data byte to the display.
+ * @param data the data to write
+ */
 void dsp_write_data(uint8_t data);
+
+/**
+ * Reads a byte of data from the display.
+ * @return the data byte
+ */
 uint8_t dsp_read_data(void);
 
 void dsp_set_addr(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
index 823c697fab39555cba4a580bdeb81ce2833acde3..9d86437f84d69b7014def59d85381afaf7ae2742 100644 (file)
@@ -1,16 +1,65 @@
+/**
+ * @file display_draw.h
+ * Provides functions for drawing to the display
+ */
+
 #ifndef DISPLAY_DRAW_H_
 #define DISPLAY_DRAW_H_
 
 #include <stdint.h>
 
+/**
+ * Starts the task for a blinking text cursor.
+ */
 void dsp_cursoron(void);
 
+/**
+ * Sets the chosen pixel to the given color.
+ * @param x x-coord of the pixel
+ * @param y y-coord of the pixel
+ * @param color the color to use (5-6-5)
+ */
 void dsp_pixel(int x, int y, uint16_t color);
+
+/**
+ * Draws a line between the two coordinates.
+ * @param x start x-coord
+ * @param y start y-coord
+ * @param i end x-coord
+ * @param j end y-coord
+ * @param color the color to use (5-6-5)
+ */
 void dsp_line(int x, int y, int i, int j, uint16_t color);
+
+/**
+ * Draws a rectangle with the given dimensions.
+ * @param x top left x-coord
+ * @param y top left y-coord
+ * @param w width of the rectangle
+ * @param h height of the rectangle
+ * @param color the color to use (5-6-5)
+ */
 void dsp_rect(int x, int y, int w, int h, uint16_t color);
 
+/**
+ * Sets the text cursor's position, in characters NOT pixels.
+ * @param x text column to move to
+ * @param y text row to move to
+ */
 void dsp_cpos(int x, int y);
+
+/**
+ * Sets the pixel offset of the text cursor.
+ * @param x x-pixel offset from (0, 0)
+ * @param y y-pixel offset from (0, 0)
+ */
 void dsp_coff(int x, int y);
+
+/**
+ * Puts a string to the screen. Text position if kept track of internally, so
+ * multiple calls will print the strings in one line.
+ * @param s the string to print
+ */
 void dsp_puts(const char *s);
 
 #endif // DISPLAY_DRAW_H_
index 7c2b8d49b9d43f2d6043af84f9c0a69371c84276..a9d20d80f97aa39d4a506073b30abb6c54bef113 120000 (symlink)
@@ -1 +1 @@
-../../interpreter
\ No newline at end of file
+../../interpreter3
\ No newline at end of file
index 7901c9cf4fb189dea7cb048fede921453867a224..ab5fa7e3f6b77f67fe778ed7bacea3106dd641c0 100644 (file)
@@ -12,6 +12,6 @@
  * Loads the library for the given interpreter.
  * @param it the interpreter to use
  */
-void script_loadlib(interpreter *it);
+void script_loadlib(instance *it);
 
 #endif // SCRIPT_H_
index 49087471dca99fad4743d233a0fd3dd4059c7b2a..3379d8137457148d75227df88e55f24fb7ea48f6 100644 (file)
@@ -1,69 +1,67 @@
 # graph area
-set plotx 50
-set ploty 50
-set plotw 380
-set ploth 220
-set cx (plotx + (plotw / 2))
-set cy (ploty + (ploth / 2))
+plotx = 50
+ploty = 50
+plotw = 380
+ploth = 220
+cx = plotx + plotw / 2
+cy = ploty + ploth / 2
 
 # graph range
-set xmin (0 - 10)
-set xmax 10
-set ymin (0 - 10)
-set ymax 10
+xmin = 0 - 10
+xmax = 10
+ymin = 0 - 10
+ymax = 10
 
-set xinc (plotw / (xmax - xmin))
-set yinc (ploth / (ymax - ymin))
+xinc = plotw / (xmax - xmin)
+yinc = ploth / (ymax - ymin)
 
-color 3 3 3 > mlines
+mlines = color(3, 3, 3)
 
-# print axis
+func(makegrid) {
+       rect(plotx, ploty, plotw, ploth, 0)
 
-func makegrid
-       rect plotx ploty plotw ploth 0
+       x = plotx
+       while (x <= plotx + plotw) {
+               line(x, ploty, x, ploty + ploth, mlines)
+               x = x + xinc
+       }
 
-       set x plotx
-       do
-               line x ploty x (ploty + ploth) mlines
-               set x (x + xinc)
-       while (x <= plotx + plotw)
+       y = ploty
+       while (y <= ploty + ploth) {
+               line(plotx, y, plotx + plotw, y, mlines)
+               y = y + yinc
+       }
 
-       set y ploty
-       do
-               line plotx y (plotx + plotw) y mlines
-               set y (y + yinc)
-       while (y <= ploty + ploth)
-
-       line cx ploty cx (ploty + ploth) 32767
-       line plotx cy (plotx + plotw) cy 32767
-end
+       line(cx, ploty, cx, ploty + ploth, 32767)
+       line(plotx, cy, plotx + plotw, cy, 32767)
+}
 
 #
 # BIG LOOP - ask for equ, graph it
 #
 
-makegrid
-set clearcmd "clear"
-do
-       rect 0 0 480 40
-
-       print "f(x) = "
-       gets > Fx
+makegrid()
+clearcmd = "clear"
+while (1) {
+       rect(0, 0, 480, 40, 0)
+       print("f(x) = ")
+       Fx = gets()
 
-       if (Fx == clearcmd)
-               makegrid
-       else
+       if (Fx == clearcmd) {
+               makegrid()
+       } else {
                # do function
-               set x xmin
-               do
-                       solve Fx > y
-                       set y (0 - y)
-                       if ((y >= ymin) & (y <= ymax))
-                               pixel (cx + x * xinc) (cy + y * yinc) 511
-                       end
-                       set x (x + 1 / xinc)
-               while (x < xmax)
-       end
+               x = xmin
+               while (x < xmax) {
+                       y = solve(Fx)
+                       y = 0 - y
+                       if ((y >= ymin) & (y <= ymax)) {
+                               pixel(cx + x * xinc, cy + y * yinc, 511)
+                       }
+                       x = x + 1 / xinc
+               }
+       }
+
+       ppos(0, 0)
+}
 
-       ppos 0 0
-while (1)
index 11ca083c3e1cf243d002cf66fcb3a122ca5e1f27..36331ee4f65c17c7a7de80ed64a464178598ee2c 100644 (file)
Binary files a/libinterp.a and b/libinterp.a differ
index 52b52533110cb63c6e8fce4a235e065f40fabb29..9a11cba271f9b25fffb71e523f2734349ba77179 100644 (file)
@@ -76,6 +76,8 @@ void free(void *buf)
                return;
 
        alloc_t *alloc = (alloc_t *)((uint8_t *)buf - sizeof(alloc_t));
+       if (alloc->next != 0)
+               return;
        heap_used -= alloc->size;
        alloc->next = free_blocks;
        free_blocks = alloc;
index 71ce68fc172b08bff652faf0e497c158e6c454f2..fc64edd30bd619dea10ba1b201efca6ffd210c80 100644 (file)
@@ -82,9 +82,8 @@ void kmain(void)
 \r
 void task_interpreter(void)\r
 {\r
-       interpreter it;\r
-       iinit(&it);\r
-       script_loadlib(&it);\r
+       instance *it = inewinstance();\r
+       script_loadlib(it);\r
 \r
        char *s = initrd_getfile("init");\r
        if (s == 0) {\r
@@ -105,7 +104,7 @@ void task_interpreter(void)
                }\r
                strncpy(linebuf, s + prev, lc + 1);\r
                linebuf[lc] = '\0';\r
-               ret = idoline(&it, linebuf);\r
+               ret = idoline(it, linebuf);\r
                if (ret < 0)\r
                        break;\r
                prev = ++i;\r
index 8fad2f33d2cd480696f708c10f36d58576683fc7..1cd0d7164ffbbcec9e07ad6a39549a1fdc5f47d5 100644 (file)
@@ -7,23 +7,25 @@
 #include <heap.h>
 #include <random.h>
 #include <serial.h>
-#include <stack.h>
+#include <stdlib.h>
 #include <keypad.h>
 
 #include <string.h>
 
-int script_puts(interpreter *it);
-int script_gets(interpreter *it);
-int script_delay(interpreter *it);
-int script_rect(interpreter *it);
-int script_ppos(interpreter *it);
-int script_line(interpreter *it);
-int script_color(interpreter *it);
-int script_rand(interpreter *it);
-int script_getkey(interpreter *it);
-int script_pixel(interpreter *it);
-
-void script_loadlib(interpreter *it)
+#define igetarg_integer(it, n) ((int)igetarg(it, n)->value.f)
+
+int script_puts(instance *it);
+int script_gets(instance *it);
+int script_delay(instance *it);
+int script_rect(instance *it);
+int script_ppos(instance *it);
+int script_line(instance *it);
+int script_color(instance *it);
+int script_rand(instance *it);
+int script_getkey(instance *it);
+int script_pixel(instance *it);
+
+void script_loadlib(instance *it)
 {
        inew_cfunc(it, "print", script_puts);
        inew_cfunc(it, "gets", script_gets);
@@ -37,14 +39,20 @@ void script_loadlib(interpreter *it)
        inew_cfunc(it, "pixel", script_pixel);
 }
 
-int script_puts(interpreter *it)
+int script_puts(instance *it)
 {
-       const char *s = igetarg_string(it, 0);
-       dsp_puts(s);
+       variable *v = igetarg(it, 0);
+       if (v->type == NUMBER) {
+               char buf[33];
+               snprintf(buf, 33, "%f", v->value.f);
+               dsp_puts(buf);
+       } else if (v->type == STRING) {
+               dsp_puts((const char *)v->value.p);
+       }
        return 0;
 }
 
-int script_gets(interpreter *it)
+int script_gets(instance *it)
 {
        char *s = malloc(64);
        char c[2] = {0, 0};
@@ -66,20 +74,19 @@ int script_gets(interpreter *it)
        s[index] = '\0';
 
        variable *r = make_vars(0, s);
-       iret(it, r);
+       ipush(it, (uint32_t)r);
        free(s);
-       free(r);
        return 0;
 }
 
-int script_delay(interpreter *it)
+int script_delay(instance *it)
 {
-       int ms = igetarg_integer(it, 0);
+       int ms = (int)igetarg(it, 0)->value.f;
        delay(ms);
        return 0;
 }
 
-int script_rect(interpreter *it)
+int script_rect(instance *it)
 {
        dsp_rect(igetarg_integer(it, 0), igetarg_integer(it, 1),
                igetarg_integer(it, 2), igetarg_integer(it, 3),
@@ -87,7 +94,7 @@ int script_rect(interpreter *it)
        return 0;
 }
 
-int script_line(interpreter *it)
+int script_line(instance *it)
 {
        int x = igetarg_integer(it, 0);
        int y = igetarg_integer(it, 1);
@@ -98,42 +105,42 @@ int script_line(interpreter *it)
        return 0;
 }
 
-int script_ppos(interpreter *it)
+int script_ppos(instance *it)
 {
        dsp_cpos(0, 0);
        dsp_coff(igetarg_integer(it, 0), igetarg_integer(it, 1));
        return 0;
 }
 
-int script_color(interpreter *it)
+int script_color(instance *it)
 {
        uint16_t c = dsp_color(igetarg_integer(it, 0), igetarg_integer(it, 1),
                igetarg_integer(it, 2));
-       variable *v = make_varn(0, (float)c);
-       iret(it, v);
+       variable *v = make_varf(0, (float)c);
+       ipush(it, (uint32_t)v);
        free(v);
        return 0;
 }
 
-int script_rand(interpreter *it)
+int script_rand(instance *it)
 {
        unsigned int mod = igetarg_integer(it, 0);
        unsigned int val = random_get();
-       variable *v = make_varn(0, (float)(mod % val));
-       iret(it, v);
+       variable *v = make_varf(0, (float)(mod % val));
+       ipush(it, (uint32_t)v);
        free(v);
        return 0;
 }
 
-int script_getkey(interpreter *it)
+int script_getkey(instance *it)
 {
-       variable *v = make_varn(0, (float)keypad_get());
-       iret(it, v);
+       variable *v = make_varf(0, (float)keypad_get());
+       ipush(it, (uint32_t)v);
        free(v);
        return 0;
 }
 
-int script_pixel(interpreter *it)
+int script_pixel(instance *it)
 {
        dsp_pixel(igetarg_integer(it, 0), igetarg_integer(it, 1),
                igetarg_integer(it, 2));