diff --git a/buffer.c b/buffer.c deleted file mode 100644 index a98a901..0000000 --- a/buffer.c +++ /dev/null @@ -1,48 +0,0 @@ -#include - -#include "buffer.h" -#include "types.h" - -void initBuffer(Buffer* b, tiny length) { - b->maxLength = length; - allocBuffer(b); - b->beginPointer = 0; - b->readPointer = 0; -} - -int overflowIndex (tiny length, tiny index) { - return index % length; -} - -tiny getBufferLength(Buffer* b) { - return overflowIndex(b->maxLength, b->beginPointer - b->readPointer); -} - -bool readBuffer (Buffer* b, char* c) { - if (!(getBufferLength(b) > 0)) - return false; - *c = b->array[b->readPointer]; - b->readPointer = overflowIndex(b->maxLength, b->readPointer + 1); - return true; -} - -bool writeFIFO (Buffer* b, char c) { - if (!(getBufferLength(b) < (b->maxLength - 1))) - return false; - b->array[b->beginPointer] = c; - b->beginPointer = overflowIndex(b->maxLength, (b->beginPointer + 1)); - return true; -} - -bool writeLIFO (Buffer* b, char c) { - if (!(getBufferLength(b) < (b->maxLength - 1))) - return false; - b->array[b->readPointer - 1] = c; - b->readPointer = overflowIndex(b->maxLength, (b->readPointer - 1)); - return true; -} - -void allocBuffer (Buffer* b) { - free(b->array); - b->array = (char*) malloc (b->maxLength * sizeof(char)); -} diff --git a/buffer.h b/buffer.h deleted file mode 100644 index 96aec0c..0000000 --- a/buffer.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef BUFFER_H -#define BUFFER_H - -#include "buffer.h" -#include "types.h" - -typedef struct { - tiny maxLength; - tiny beginPointer; - tiny readPointer; - char *array; -} Buffer; - -void initBuffer(Buffer*, tiny); -int overflowIndex (tiny, tiny); -tiny getBufferLength(Buffer*); -bool readBuffer (Buffer*, char*); -bool writeFIFO (Buffer* , char ); -bool writeLIFO (Buffer*, char); -void allocBuffer (Buffer*); - -#endif diff --git a/cmd/ash.c b/cmd/ash.c deleted file mode 100644 index 79e776f..0000000 --- a/cmd/ash.c +++ /dev/null @@ -1,70 +0,0 @@ -#include - -#include - -#include "ash.h" -#include "../stdio.h" -#include "../types.h" -#include "../string.h" - -void interpret(char***, tiny*, char*, tiny); - -#define clLength 64 - -int ashMain () { - char input[clLength]; - - char *prompt = "ash> "; - - tiny position = 0; - - puts(prompt); - - while (true) { - char c = getch(); - if (c == '\r') { - input[position] = '\0'; - puts(nl); - char **args; - tiny argc; - interpret(&args, &argc, input, position); - position = 0; - puts(args[0]); - puts(nl); - puts(prompt); - free(args); - PORTB |= 2; - continue; - } - if (position >= clLength - 1) - continue; - putc(c); - input[position] = c; - position++; - } - return 0; -} - -void interpret(char*** args, tiny* argc, char* input, tiny length) { - (*argc) = 0; - (*args) = (char**) malloc(sizeof(char*)); - (*args)[0] = (char*) malloc(sizeof(char)); - tiny paramLength = 0; - for (tiny i = 0; i < length; i++) { - if (input[i] == ' ') { - if (paramLength == 0) - continue; - (*args)[(*argc)] = (char*) realloc((*args)[(*argc)], (paramLength + 1) * sizeof(char)); - (*args)[(*argc)][paramLength] = '\0'; - (*argc)++; - (*args) = (char**) realloc((*args), ((*argc) + 1) * sizeof(char*)); - (*args)[(*argc)] = (char*) malloc(sizeof(char)); - paramLength = 0; - } - (*args)[(*argc)] = (char*) realloc((*args)[(*argc)], (paramLength + 1) * sizeof(char)); - (*args)[(*argc)][paramLength] = input[i]; - paramLength++; - } - (*args)[(*argc)] = (char*) realloc((*args)[(*argc)], (paramLength + 1) * sizeof(char)); - (*args)[(*argc)][paramLength] = '\0'; -} diff --git a/cmd/ash.h b/cmd/ash.h deleted file mode 100644 index c791b0c..0000000 --- a/cmd/ash.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef ASH_H -#define ASH_H - -int ashMain(); - -#endif diff --git a/main.c b/main.c deleted file mode 100644 index 1ef9137..0000000 --- a/main.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include - -#include "stdio.h" -#include "types.h" -#include "string.h" -#include "serial.h" -#include "cmd/ash.h" - -void setup () { - DDRB = 0xff; - PORTB = 0; - initSerial(b9600, d8, even, s1); - initOutput(); - sei(); // FACEPALM!!!! - - puts(nl); - puts("init [serial]"); - puts(nl); - puts("init [output]"); - puts(nl); - - initInput(); - puts("init [input]"); - puts(nl); - - TIMSK = 0; - puts("reset timer interrupts"); - puts(nl); - - puts("watchdog flag: "); - putc(((MCUCSR & WDRF) >> WDRF) + '0'); - puts(nl); - - MCUCSR &= ~(1 << WDRF); - puts("watchdog flag reset"); - puts(nl); - - PORTB |= 1; -} - -int main () { - setup(); - - while (true) - ashMain(); - - return 0; -} diff --git a/serial.c b/serial.c deleted file mode 100644 index 3b721bb..0000000 --- a/serial.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "serial.h" - -void initSerial(enum Baudrate baudrate, enum Databits data, enum Parity parity, enum Stopbits stop) { - switch (baudrate) { - case b2400: - UBRRL = 416 & 0xff; - UBRRH = 416 >> 8; - break; - case b4800: - UBRRL = 207 & 0xff; - UBRRH = 207 >> 8; - break; - case b9600: - UBRRL = 103 & 0xff; - UBRRH = 103 >> 8; - break; - case b19200: - UBRRL = 51 & 0xff; - UBRRH = 51 >> 8; - break; - case b38400: - UBRRL = 25 & 0xff; - UBRRH = 25 >> 8; - break; - case b57600: - UBRRL = 16 & 0xff; - UBRRH = 16 >> 8; - break; - case b155200: - UBRRL = 8 & 0xff; - UBRRH = 8 >> 8; - break; - default: - break; - } - - UCSRC &= ~((0 << URSEL) | (1 << USBS)); - if (stop == s2) - UCSRC |= (1 << URSEL) | (1 << USBS); - - UCSRB &= ~((0 << URSEL) | (1 << UCSZ2)); - UCSRC &= ~((0 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0)); - - switch (data) { - case d8: - UCSRB |= (0 << UCSZ2); - UCSRC |= ((1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0)); - break; - case d9: - UCSRB |= (1 << UCSZ2); - UCSRC |= ((1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0)); - break; - default: - break; - } - - UCSRC &= ~((0 << URSEL) | (1 << UPM1) | (1 << UPM0)); - - switch (parity) { - case no: - UCSRC |= (0 << UPM1) | (0 << UPM0); - break; - case even: - UCSRC |= (1 << UPM1) | (0 << UPM0); - break; - case odd: - UCSRC |= (1 << UPM1) | (1 << UPM0); - break; - default: - break; - } -} diff --git a/serial.h b/serial.h deleted file mode 100644 index 0ad8a51..0000000 --- a/serial.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef SERIAL_H -#define SERIAL_H - -#include - -enum Parity {no, even, odd}; -enum Baudrate {b2400, b4800, b9600, b19200, b38400, b57600, b155200}; -enum Stopbits {s1, s2}; -enum Databits {d8, d9}; - -void initSerial(enum Baudrate, enum Databits, enum Parity, enum Stopbits); - -#define enableTransmitter() (UCSRB |= (1 << TXEN)) -#define disableTransmitter() (UCSRB &= ~(1 << TXEN)) - -#define enableReceiver() (UCSRB |= (1 << RXEN)) -#define disableReceiver() (UCSRB &= ~(1 << RXEN)) - -#define enableReceiveInterrupt() (UCSRB |= (1 << RXCIE)) -#define disableReceiveInterrupt() (UCSRB &= ~(1 << RXCIE)) - -#define enableTransmitCompleteInterrupt() (UCSRB |= (1 << TXCIE)) -#define disableTransmitCompleteInterrupt() (UCSRB &= ~(1 << TXCIE)) - -#define enableDataRegisterEmptyInterrupt() (UCSRB |= (1 << UDRIE)) -#define disableDataRegisterEmptyInterrupt() (UCSRB &= ~(1 << UDRIE)) - -#define characterGot() (UCSRA & (1 << RXC)) -#define dataRegisterEmpty() (UCSRA & (1 << UDRE)) - -#define sRead() (UDR) -#define sWrite(c) (UDR = c) - -#endif diff --git a/stdio.h b/stdio.h deleted file mode 100644 index 85bc054..0000000 --- a/stdio.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef STDIO_H -#define STDIO_H - -#include "stdio/input.h" -#include "stdio/output.h" - -#endif diff --git a/stdio/input.c b/stdio/input.c deleted file mode 100644 index 4043eb5..0000000 --- a/stdio/input.c +++ /dev/null @@ -1,36 +0,0 @@ -#include - -#include "input.h" -#include "../serial.h" - -#define safe 3 - -Buffer stdin; - -void initInput () { - initBuffer(&stdin, 64); - enableReceiver(); - enableReceiveInterrupt(); -} - -char getch() { - char c; - while (!(readBuffer(&stdin, &c))); - return c; -} - -void ungetc(char c) { - while (!writeLIFO(&stdin, c)); -} - -bool kbhit() { - return getBufferLength(&stdin); -} - -// on serial receive -ISR (USART_RXC_vect) { - PORTB |= 8; - if (getBufferLength(&stdin) < stdin.maxLength - safe) { - writeFIFO(&stdin, sRead()); - } -} diff --git a/stdio/input.h b/stdio/input.h deleted file mode 100644 index 726d6e0..0000000 --- a/stdio/input.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef INPUT_H -#define INPUT_H - -#include "../buffer.h" - -extern Buffer stdin; - -void initInput (); -char getch(); -void ungetc(char c); -bool kbhit(); - -#endif diff --git a/stdio/output.c b/stdio/output.c deleted file mode 100644 index 71ccce1..0000000 --- a/stdio/output.c +++ /dev/null @@ -1,47 +0,0 @@ -#include - -#include "output.h" -#include "../string.h" -#include "../serial.h" - -#define safe 3 - -Buffer stdout; - -void initOutput () { - initBuffer(&stdout, 64); - enableTransmitter(); - enableTransmitCompleteInterrupt(); - enableDataRegisterEmptyInterrupt(); -} - -void putc (char c) { - if (dataRegisterEmpty()) { - sWrite(c); - return; - } - while (getBufferLength(&stdout) > stdout.maxLength - safe); - writeFIFO(&stdout, c); -} - -void puts (char* string) { - int length = getStringLength(string); - for (int i = 0; i < length; i++) { - putc(string[i]); - } -} - - - -// on serial transmit -ISR (USART_TXC_vect) { -} - -// on data register empty -ISR (USART_UDRE_vect) { - if (getBufferLength(&stdout)) { - char c; - readBuffer(&stdout, &c); - sWrite(c); - } -} diff --git a/stdio/output.h b/stdio/output.h deleted file mode 100644 index 33ec756..0000000 --- a/stdio/output.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef OUTPUT_H -#define OUTPUT_H - -#include "../buffer.h" - -extern Buffer stdout; - -void initOutput (); -void putc (char); -void puts (char*); - -#endif diff --git a/string.c b/string.c deleted file mode 100644 index 599f904..0000000 --- a/string.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "string.h" - -int getStringLength (char* string) { - for (int i = 0; ; i++) { - if (string[i] == '\0') - return i + 1; - } - return 0; -} diff --git a/string.h b/string.h deleted file mode 100644 index 004e3fc..0000000 --- a/string.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef STRING_H -#define STRING_H - -#define nl "\r\n" - -int getStringLength (char*); - -#endif diff --git a/types.h b/types.h deleted file mode 100644 index 1cff07d..0000000 --- a/types.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef TYPES_H -#define TYPES_H - -#include - -#define bool uint8_t -#define false 0 -#define true !false - -#define tiny uint8_t - -#endif