]> code.bitgloo.com Git - clyne/calculator.git/commitdiff
new initrd format, 2nd layer for keypad
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 30 Mar 2018 15:39:33 +0000 (11:39 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 30 Mar 2018 15:39:33 +0000 (11:39 -0400)
13 files changed:
Makefile
boot [deleted file]
include/initrd.h
initrd.c [deleted file]
initrd/boot [new file with mode: 0644]
libinterp.a
rba [deleted file]
src/initrd.c
src/keypad.c
src/main.c
src/script.c
src/stdlib.c
tools/rba [new file with mode: 0755]

index b3acc2af2576d524b4d28d58a9c694834c121d0f..d672a52b5695a0979c376fe66e7f5b2780152bdb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@
 CROSS = arm-none-eabi-
 CC = gcc
 AS = as
-AR = ar
+AR = tools/rba
 OBJCOPY = objcopy
 STRIP = strip
 
@@ -46,11 +46,10 @@ INITRD = initrd.img
 
 all: $(OUT)
 
-#@$(CROSS)$(STRIP) --only-keep-debug $(OUT)
 $(OUT): $(OFILES) initrd/init libinterp.a
        @echo "  INITRD " $(INITRD)
        @rm -f $(INITRD)
-       @$(CROSS)$(AR) r $(INITRD) initrd/*
+       @$(AR) $(INITRD) initrd/*
        @$(CROSS)$(OBJCOPY) -B arm -I binary -O elf32-littlearm $(INITRD) out/initrd.img.o
        @echo "  LINK   " $(OUT)
        @$(CROSS)$(CC) $(CFLAGS) $(LFLAGS) out/*.o -o $(OUT) -L. -linterp -lm
diff --git a/boot b/boot
deleted file mode 100644 (file)
index 13e5876..0000000
--- a/boot
+++ /dev/null
@@ -1,3 +0,0 @@
-choice = filemenu
-print("\nChoice: ")
-print(choice)
index 1f7c0b6a52589cb97ff60178ca53a281cf19c4e2..395706637baa9a7dc40ddebe3971599fd3a8a8d7 100644 (file)
 
 #include <stdint.h>
 
-/**
- * Structure for the archive's header.
- */
-typedef struct
-{
-       char signature[8]; /**< The archive's signature */
-} __attribute__ ((packed)) initrd_header;
-
-/**
- * Structure for a file entry in the archive.
- */
-typedef struct 
-{
-       char name[16];      /**< The name of the file */
-       uint8_t unused[32]; /**< Unused information */
-       char size[10];      /**< The file's size in bytes (as string) */
-       char sig[2];        /**< A signature to start file data */
-} __attribute__ ((packed)) initrd_file;
-
 /**
  * Confirms the initrd image is loaded and valid.
  * @return non-zero if valid image found
  */
 uint8_t initrd_validate(void);
 
+/**
+ * Gets the file name of the index'th file in the archive.
+ * @param index the index of the file
+ * @return the file's name, or zero if not found
+ */
+char *initrd_getfile(uint32_t index);
+
 /**
  * Gets contents of the given file.
  * @param name the file's name
  * @return pointer to file data, null if not found
  */
-char *initrd_getfile(const char *name);
+char *initrd_readfile(const char *name);
 
 /**
  * Gets the size of the given file.
  * @param name the file's name
  * @return the file's size, in bytes
  */
-uint32_t initrd_getfilesize(const char *name);
-
-char *initrd_getnfile(unsigned int index);
+uint32_t initrd_filesize(const char *name);
 
 #endif // INITRD_H_
diff --git a/initrd.c b/initrd.c
deleted file mode 100644 (file)
index 3f36cd9..0000000
--- a/initrd.c
+++ /dev/null
@@ -1,42 +0,0 @@
-#include <stdint.h>
-#include <string.h>
-
-extern uint8_t *initrd_start;
-extern uint32_t initrd_size;
-
-char *getfile(uint32_t offset)
-{
-       char *ptr = initrd_start;
-       for (uint32_t i = 0; i < offset; i++) {
-               uint32_t len = *((uint32_t *)ptr);
-               uint32_t datalen = *((uint32_t *)(ptr + 4 + len));
-               ptr += len + datalen + 8;
-               if (ptr >= (char *)(initrd_start + initrd_size))
-                       return 0;
-       }
-
-       return ptr;
-}
-
-char *readfile(const char *name)
-{
-       char *ptr;
-       for (uint32_t i = 0; ptr = getfile(i), ptr != 0; i++) {
-               uint32_t len = *((uint32_t *)ptr);
-               if (!strncmp(name, ptr + 4, len))
-                       return ptr + len + 8;
-       }
-       return 0;
-}
-
-uint32_t filesize(const char *name)
-{
-       char *ptr;
-       for (uint32_t i = 0; ptr = getfile(i), ptr != 0; i++) {
-               uint32_t len = *((uint32_t *)ptr);
-               if (!strncmp(name, ptr + 4, len))
-                       return *((uint32_t *)ptr + len + 4);
-       }
-       return 0;
-}
-
diff --git a/initrd/boot b/initrd/boot
new file mode 100644 (file)
index 0000000..13e5876
--- /dev/null
@@ -0,0 +1,3 @@
+choice = filemenu
+print("\nChoice: ")
+print(choice)
index 7c9e7a9a51dede0cc646cc5be8663838b5e2fc2f..04391e1deab3ed92d758cec68b99c0349ca1cd77 100644 (file)
Binary files a/libinterp.a and b/libinterp.a differ
diff --git a/rba b/rba
deleted file mode 100755 (executable)
index 52f37cb..0000000
Binary files a/rba and /dev/null differ
index b8e8d09a80c09902ebcd266e7801807eac19d7a3..153421075749b4ff5cec8df815936378eaf408ae 100644 (file)
@@ -1,9 +1,6 @@
 /**
  * @file initrd.c
  * Initrd image support
- * An archive file (made with ar) can be linked into the final executable to
- * allow files to be loaded in memory on boot. See mkinitrd.sh or the Makefile
- * for more info.
  *
  * Copyright (C) 2018 Clyne Sullivan
  *
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+/**
+ * The format of the initrd file is custom; made with the 'rba' utility.
+ */
+
 #include <initrd.h>
+#include <string.h>
 
 extern uint8_t _binary_initrd_img_start[];
 extern uint8_t _binary_initrd_img_size[];
 
-static const void *initrd_start = (void *)_binary_initrd_img_start;
+static const uint8_t *initrd_start = (uint8_t *)_binary_initrd_img_start;
 static const uint32_t initrd_size = (uint32_t)_binary_initrd_img_size;
 
-static const char *initrd_sig = "!<arch>\n";
-
 uint8_t initrd_validate(void)
 {
-       initrd_header *header = (initrd_header *)initrd_start;
-       for (uint8_t i = 0; i < 8; i++) {
-               if (header->signature[i] != initrd_sig[i])
-                       return 0;
-       }
-
-       return 1;
+       return 1; // TODO maybe add header/signature to archiver?
 }
 
-uint8_t initrd_nametest(char *file, const char *want)
+char *initrd_getfile(uint32_t offset)
 {
-       for (uint8_t i = 0; i < 16; i++) {
-               if (want[i] == '\0')
-                       return (file[i] == '/');
-               else if (want[i] != file[i])
+       char *ptr = initrd_start;
+       for (uint32_t i = 0; i < offset; i++) {
+               uint32_t len = *((uint32_t *)ptr);
+               uint32_t datalen = *((uint32_t *)(ptr + 4 + len));
+               ptr += len + datalen + 8;
+               if (ptr >= (char *)(initrd_start + initrd_size))
                        return 0;
        }
 
-       return 0;
-}
-
-uint32_t ipow10(uint8_t n)
-{
-       uint32_t i = 1;
-       while (n--)
-               i *= 10;
-       return i;
-}
-
-uint32_t initrd_getsize(initrd_file *file)
-{
-       uint32_t size = 0;
-       char *p = file->size + 10;
-       while (*--p == ' ');
-
-       for (int8_t i = p - file->size, j = 0; i >= 0; i--, j++)
-               size += (*p-- - '0') * ipow10(j);
-
-       return size;
+       return ptr;
 }
 
-initrd_file *initrd_getfileptr(const char *name)
+char *initrd_readfile(const char *name)
 {
-       initrd_file *file = (initrd_file *)((uint8_t *)initrd_start + sizeof(initrd_header));
-       uint32_t offset = sizeof(initrd_header);
-
-       while (offset < initrd_size) {
-               if (initrd_nametest(file->name, name))
-                       return file;
-               uint32_t size = initrd_getsize(file) + sizeof(initrd_file);
-               offset += size;
-               file = (initrd_file *)((uint8_t *)file + size + 1);
+       char *ptr;
+       for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
+               uint32_t len = *((uint32_t *)ptr);
+               if (!strncmp(name, ptr + 4, len))
+                       return ptr + len + 8;
        }
-
        return 0;
 }
 
-char *initrd_getnfile(unsigned int index)
+uint32_t initrd_filesize(const char *name)
 {
-       initrd_file *file = (initrd_file *)((uint8_t *)initrd_start + sizeof(initrd_header));
-       uint32_t offset = sizeof(initrd_header);
-
-       for (unsigned int i = 0; i < index; i++) {
-               uint32_t size = initrd_getsize(file) + sizeof(initrd_file);
-               offset += size;
-               file = (initrd_file *)((uint8_t *)file + size);
-               if (file->name[0] == '\n')
-                       file = (initrd_file *)((uint32_t)file + 1);
+       char *ptr;
+       for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
+               uint32_t len = *((uint32_t *)ptr);
+               if (!strncmp(name, ptr + 4, len))
+                       return *((uint32_t *)(ptr + len + 4));
        }
-       if ((uint32_t)file >= (uint32_t)initrd_start + initrd_size)
-               return 0;
-       return file->name;
-}
-
-char *initrd_getfile(const char *name)
-{
-       initrd_file *file = initrd_getfileptr(name);
-       if (file == 0)
-               return 0;
-
-
-       char *ptr = (char *)file + sizeof(initrd_file);
-       ptr[initrd_getsize(file) - 1] = 0;
-       return ptr;
-}
-
-uint32_t initrd_getfilesize(const char *name)
-{
-       initrd_file *file = initrd_getfileptr(name);
-       if (file == 0)
-               return 0;
-
-       return initrd_getsize(file);
+       return 0;
 }
 
index 5b04eec040b23bd70ab15270fbb578287b5b6775..22fc9670f9d75a38bc9b0211d31ef4d95828de4e 100644 (file)
@@ -53,16 +53,29 @@ static const port_t keypad_cols[COLS] = {
        { COL_3 }, { COL_4 }
 };
 
+#define K_2ND  0x000000FF
+#define K_HOLD 0x000001FF
+
 static const char keypad_map[ROWS * COLS * 4] = {
-       "&\0\0\0" "|\0\0\0" "pi\0\0"  "==\0\0"   "!=\0\0"
-       "x\0\0\0" "y\0\0\0" "z\0\0\0"  "=\0\0\0"  "sin\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" "\b\0\0\0" "\n\0\0\0" "+\0\0\0"
+       "\xFF\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"
+};
+
+static const char keypad_map_2nd[ROWS * COLS * 4] = {
+       "a\0\0\0" "b\0\0\0"  "c\0\0\0"  "d\0\0\0"  "e\0\0\0"
+       "f\0\0\0" "g\0\0\0"  "h\0\0\0"  "i\0\0\0"  "j\0\0\0"
+       "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"
 };
 
-#define KEY(r, c, i) keypad_map[r * COLS * 4 + c * 4 + i]
+#define KEY(r, c, i) map[r * COLS * 4 + c * 4 + i]
+#define KEYCODE(r, c) *((uint32_t *)(map + (r * COLS * 4 + c * 4)))
 
 #define BUFFER_SIZE 8
 static char keypad_buffer[BUFFER_SIZE];
@@ -71,12 +84,24 @@ static int keypad_buffer_pos = -1;
 void keypad_task(void)
 {
        unsigned int col = 0;
+       unsigned char use2nd = 0;
+       unsigned char hold = 0;
        while (1) {
                gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 1);
                delay(10);
                for (unsigned int row = 0; row < ROWS; row++) {
                        if (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) {
-                               if (keypad_buffer_pos < BUFFER_SIZE) {
+                               char *map = (use2nd == 0) ? keypad_map : keypad_map_2nd;
+                               if (KEYCODE(row, col) == K_2ND) {
+                                       use2nd = 1;
+                               } else if (KEYCODE(row, col) == K_HOLD) {
+                                       if (use2nd != 0) {
+                                               if ((hold ^= 1) == 0)
+                                                       use2nd = 0;
+                                       }
+                               } else if (keypad_buffer_pos < BUFFER_SIZE) {
+                                       if (use2nd != 0 && hold == 0)
+                                               use2nd = 0;
                                        for (unsigned int i = 0; KEY(row, col, i) != '\0'; i++)
                                                keypad_buffer[++keypad_buffer_pos] = KEY(row, col, i);
                                }
index d21407a18189c9bf06350c6211e51687ab5b53ba..b39053abca2036dcd2aa5baf38289b4fb081a373 100644 (file)
@@ -90,7 +90,7 @@ void task_interpreter(void)
        instance *it = inewinstance();\r
        script_loadlib(it);\r
 \r
-       char *s = initrd_getfile("init");\r
+       char *s = initrd_readfile("init");\r
        if (s == 0) {\r
                dsp_puts("can't find init");\r
                goto end;\r
@@ -98,7 +98,7 @@ void task_interpreter(void)
 \r
        char *linebuf = (char *)malloc(120);\r
        uint32_t i = 0, prev = 0, lc;\r
-       uint32_t size = initrd_getfilesize("init");\r
+       uint32_t size = initrd_filesize("init");\r
        int ret = 0;\r
        while (i < size) {\r
                for (; s[i] != '\n' && s[i] != '\0'; i++);\r
index 733bc4ac4bf548453d70fc78ea7cc9582e0ebeed..95d62c0a6103c2926a79f78cf5c9d615df4975d7 100644 (file)
@@ -107,7 +107,7 @@ int script_filemenu(instance *it)
        char *fname;
        strncpy(listbuf, " : \0", 4);
        dsp_puts("Choose a file: \n");
-       for (unsigned int i = 0; (fname = initrd_getnfile(i)) != 0; i++) {
+       for (unsigned int i = 0; (fname = initrd_getfile(i)) != 0; i++) {
                listbuf[0] = i + '0';
                dsp_puts(listbuf);
                dsp_puts(strncpy(buf, fname, 16));
index 950f3567cee60dc04dc947200a22c6193789cd4c..b7b11dc46decc61f4dee4de1b1524a868d05202b 100644 (file)
@@ -23,8 +23,8 @@
 #include <stdlib.h>
 
 #include <ctype.h>
+#include <heap.h>
 #include <stdarg.h>
-#include <stdint.h>
 #include <string.h>
 
 extern char *itoa(int, char *, int);
@@ -61,7 +61,7 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...)
                                itoa(va_arg(args, unsigned int), nbuf, 16);
                                break;
                        case 'f':
-                               itoa((int)va_arg(args, double), nbuf, 10);
+                               itoa(va_arg(args, double), nbuf, 10);
                                break;
                        default:
                                buf[off++] = format[i];
diff --git a/tools/rba b/tools/rba
new file mode 100755 (executable)
index 0000000..52f37cb
Binary files /dev/null and b/tools/rba differ