aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/initrd.c112
-rw-r--r--src/keypad.c41
-rw-r--r--src/main.c4
-rw-r--r--src/script.c2
-rw-r--r--src/stdlib.c4
5 files changed, 66 insertions, 97 deletions
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 <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;
}
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 <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];