From 85d33abdd9bb482a0b5caff928004b87e7978cf9 Mon Sep 17 00:00:00 2001 From: Gavan Fantom Date: Wed, 4 May 2011 22:50:57 +0000 Subject: [PATCH] Silly interactive loop to test things out. --- main.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index f7e6ee7..497aa39 100644 --- a/main.c +++ b/main.c @@ -19,6 +19,7 @@ #define REG(x) (((volatile unsigned char *)UARTBASE)[x]) #define U0THRE ((REG(LSR) & (1<<5))) /* UART0 transmitter holding register is empty */ +#define U0DR ((REG(LSR) & (1<<0))) /* UART0 data ready */ #define PINSEL0 (*((volatile unsigned char *) 0xE002C000)) @@ -44,8 +45,67 @@ void putstr(char *s) { while (*s) putch(*s++); } +void putint(unsigned int n) { + char s[11]; + int i; + + i = 10; + s[i] = '\0'; + + do { + s[--i] = n % 10 + '0'; + } while ((n /= 10) > 0); + + putstr(s+i); +} + +char getch(void) { + while (!U0DR); + return REG(RBR); +} + +void reply(char *str) +{ + putstr(str); + putstr("\r\n"); +} + +unsigned int count = 0; + int main(void) { init_uart(); - putstr("Your entire life has been a mathematical error... a mathematical error I'm about to correct!\n"); + putstr("Your entire life has been a mathematical error... a mathematical error I'm about to correct!\r\n"); + + while (1) { + char c; + putstr("prompt> "); + c = getch(); + if (c == 0x0a) + continue; + putch(c); + putstr("\r\n"); + switch (c & 0xdf) { + case 0x0a: + case 0x0d: + break; + case 'A': + reply("apple"); + break; + case 'C': + count++; + putstr("The current count is "); + putint(count); + reply("."); + break; + case 'H': + case '?': + reply("Help is not available. Try a psychiatrist."); + break; + default: + reply("Unrecognised command."); + break; + } + } + return 0; }