diff --git a/Makefile b/Makefile index b3acc2a..d672a52 100644 --- 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/include/initrd.h b/include/initrd.h index 1f7c0b6..3957066 100644 --- a/include/initrd.h +++ b/include/initrd.h @@ -26,45 +26,31 @@ #include -/** - * 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 index 3f36cd9..0000000 --- a/initrd.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include - -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/boot b/initrd/boot similarity index 100% rename from boot rename to initrd/boot diff --git a/libinterp.a b/libinterp.a index 7c9e7a9..04391e1 100644 Binary files a/libinterp.a and b/libinterp.a differ diff --git a/src/initrd.c b/src/initrd.c index b8e8d09..1534210 100644 --- a/src/initrd.c +++ b/src/initrd.c @@ -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 * @@ -21,110 +18,57 @@ * along with this program. If not, see . */ +/** + * The format of the initrd file is custom; made with the 'rba' utility. + */ + #include +#include 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 = "!\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; } diff --git a/src/keypad.c b/src/keypad.c index 5b04eec..22fc967 100644 --- a/src/keypad.c +++ b/src/keypad.c @@ -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); } diff --git a/src/main.c b/src/main.c index d21407a..b39053a 100644 --- a/src/main.c +++ b/src/main.c @@ -90,7 +90,7 @@ void task_interpreter(void) instance *it = inewinstance(); script_loadlib(it); - char *s = initrd_getfile("init"); + char *s = initrd_readfile("init"); if (s == 0) { dsp_puts("can't find init"); goto end; @@ -98,7 +98,7 @@ void task_interpreter(void) char *linebuf = (char *)malloc(120); uint32_t i = 0, prev = 0, lc; - uint32_t size = initrd_getfilesize("init"); + uint32_t size = initrd_filesize("init"); int ret = 0; while (i < size) { for (; s[i] != '\n' && s[i] != '\0'; i++); diff --git a/src/script.c b/src/script.c index 733bc4a..95d62c0 100644 --- a/src/script.c +++ b/src/script.c @@ -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)); diff --git a/src/stdlib.c b/src/stdlib.c index 950f356..b7b11dc 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -23,8 +23,8 @@ #include #include +#include #include -#include #include 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/rba b/tools/rba similarity index 100% rename from rba rename to tools/rba