Merge pull request #176 from antonblanchard/console-improv
authorAnton Blanchard <anton@linux.ibm.com>
Tue, 19 May 2020 01:53:34 +0000 (11:53 +1000)
committerGitHub <noreply@github.com>
Tue, 19 May 2020 01:53:34 +0000 (11:53 +1000)
Console improvements from Ben

15 files changed:
hello_world/Makefile
hello_world/console.c [deleted file]
hello_world/console.h [deleted file]
hello_world/hello_world.c
include/console.h [new file with mode: 0644]
lib/console.c [new file with mode: 0644]
litedram/gen-src/sdram_init/Makefile
rust_lib_demo/console.c
rust_lib_demo/console.h
rust_lib_demo/hello_world.c
tests/Makefile.test
tests/decrementer/decrementer.c
tests/illegal/illegal.c
tests/sc/sc.c
tests/xics/xics.c

index 9051e7dc2b483d8ffe4d70eb4bf7236ed553491d..4c7d3acd3a0132696bdc54461e30108550f9adda 100644 (file)
@@ -15,6 +15,9 @@ LDFLAGS = -T powerpc.lds
 
 all: hello_world.hex
 
+console.o: ../lib/console.c
+       $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
+
 hello_world.elf: hello_world.o head.o console.o
        $(LD) $(LDFLAGS) -o $@ $^
 
diff --git a/hello_world/console.c b/hello_world/console.c
deleted file mode 100644 (file)
index 6c1c311..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-#include <stdint.h>
-#include <stdbool.h>
-
-#include "console.h"
-#include "microwatt_soc.h"
-#include "io.h"
-
-#define UART_FREQ 115200
-
-/*
- * Core UART functions to implement for a port
- */
-
-static uint64_t potato_uart_base;
-
-static uint64_t potato_uart_reg_read(int offset)
-{
-       return readq(potato_uart_base + offset);
-}
-
-static void potato_uart_reg_write(int offset, uint64_t val)
-{
-       writeq(val, potato_uart_base + offset);
-}
-
-static int potato_uart_rx_empty(void)
-{
-       uint64_t val;
-
-       val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
-
-       if (val & POTATO_CONSOLE_STATUS_RX_EMPTY)
-               return 1;
-
-       return 0;
-}
-
-static int potato_uart_tx_full(void)
-{
-       uint64_t val;
-
-       val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
-
-       if (val & POTATO_CONSOLE_STATUS_TX_FULL)
-               return 1;
-
-       return 0;
-}
-
-static char potato_uart_read(void)
-{
-       uint64_t val;
-
-       val = potato_uart_reg_read(POTATO_CONSOLE_RX);
-
-       return (char)(val & 0x000000ff);
-}
-
-static void potato_uart_write(char c)
-{
-       uint64_t val;
-
-       val = c;
-
-       potato_uart_reg_write(POTATO_CONSOLE_TX, val);
-}
-
-static unsigned long potato_uart_divisor(unsigned long proc_freq, unsigned long uart_freq)
-{
-       return proc_freq / (uart_freq * 16) - 1;
-}
-
-void potato_uart_init(void)
-{
-       uint64_t proc_freq;
-
-       potato_uart_base = UART_BASE;
-       proc_freq = readq(SYSCON_BASE + SYS_REG_CLKINFO);
-
-       potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(proc_freq, UART_FREQ));
-}
-
-void potato_uart_irq_en(void)
-{
-       potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0xff);
-}
-
-void potato_uart_irq_dis(void)
-{
-       potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0x00);
-}
-
-int getchar(void)
-{
-       while (potato_uart_rx_empty())
-               /* Do nothing */ ;
-
-       return potato_uart_read();
-}
-
-void putchar(unsigned char c)
-{
-       while (potato_uart_tx_full())
-               /* Do Nothing */;
-
-       potato_uart_write(c);
-}
-
-void putstr(const char *str, unsigned long len)
-{
-       for (unsigned long i = 0; i < len; i++) {
-               putchar(str[i]);
-       }
-}
-
-size_t strlen(const char *s)
-{
-       size_t len = 0;
-
-       while (*s++)
-               len++;
-
-       return len;
-}
diff --git a/hello_world/console.h b/hello_world/console.h
deleted file mode 100644 (file)
index 5cf303a..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <stddef.h>
-
-void potato_uart_init(void);
-void potato_uart_irq_en(void);
-void potato_uart_irq_dis(void);
-int getchar(void);
-void putchar(unsigned char c);
-void putstr(const char *str, unsigned long len);
-size_t strlen(const char *s);
index 7e853d8cadafe3dc85f510cd03ba33b45c9a84a7..6575667bcf92fb1d1edee7f9dc35eeb31d4714f1 100644 (file)
@@ -3,13 +3,13 @@
 
 #include "console.h"
 
-#define HELLO_WORLD "Hello World\r\n"
+#define HELLO_WORLD "Hello World\n"
 
 int main(void)
 {
        potato_uart_init();
 
-       putstr(HELLO_WORLD, strlen(HELLO_WORLD));
+       puts(HELLO_WORLD);
 
        while (1) {
                unsigned char c = getchar();
diff --git a/include/console.h b/include/console.h
new file mode 100644 (file)
index 0000000..cfd9dc1
--- /dev/null
@@ -0,0 +1,9 @@
+#include <stddef.h>
+
+void potato_uart_init(void);
+void potato_uart_irq_en(void);
+void potato_uart_irq_dis(void);
+int getchar(void);
+int putchar(int c);
+int puts(const char *str);
+size_t strlen(const char *s);
diff --git a/lib/console.c b/lib/console.c
new file mode 100644 (file)
index 0000000..fa2ade3
--- /dev/null
@@ -0,0 +1,131 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "console.h"
+#include "microwatt_soc.h"
+#include "io.h"
+
+#define UART_FREQ 115200
+
+/*
+ * Core UART functions to implement for a port
+ */
+
+static uint64_t potato_uart_base;
+
+static uint64_t potato_uart_reg_read(int offset)
+{
+       return readq(potato_uart_base + offset);
+}
+
+static void potato_uart_reg_write(int offset, uint64_t val)
+{
+       writeq(val, potato_uart_base + offset);
+}
+
+static int potato_uart_rx_empty(void)
+{
+       uint64_t val;
+
+       val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
+
+       if (val & POTATO_CONSOLE_STATUS_RX_EMPTY)
+               return 1;
+
+       return 0;
+}
+
+static int potato_uart_tx_full(void)
+{
+       uint64_t val;
+
+       val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
+
+       if (val & POTATO_CONSOLE_STATUS_TX_FULL)
+               return 1;
+
+       return 0;
+}
+
+static char potato_uart_read(void)
+{
+       uint64_t val;
+
+       val = potato_uart_reg_read(POTATO_CONSOLE_RX);
+
+       return (char)(val & 0x000000ff);
+}
+
+static void potato_uart_write(char c)
+{
+       uint64_t val;
+
+       val = c;
+
+       potato_uart_reg_write(POTATO_CONSOLE_TX, val);
+}
+
+static unsigned long potato_uart_divisor(unsigned long proc_freq, unsigned long uart_freq)
+{
+       return proc_freq / (uart_freq * 16) - 1;
+}
+
+void potato_uart_init(void)
+{
+       uint64_t proc_freq;
+
+       potato_uart_base = UART_BASE;
+       proc_freq = readq(SYSCON_BASE + SYS_REG_CLKINFO);
+
+       potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(proc_freq, UART_FREQ));
+}
+
+void potato_uart_irq_en(void)
+{
+       potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0xff);
+}
+
+void potato_uart_irq_dis(void)
+{
+       potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0x00);
+}
+
+int getchar(void)
+{
+       while (potato_uart_rx_empty())
+               /* Do nothing */ ;
+
+       return potato_uart_read();
+}
+
+int putchar(int c)
+{
+       while (potato_uart_tx_full())
+               /* Do Nothing */;
+
+       potato_uart_write(c);
+       return c;
+}
+
+int puts(const char *str)
+{
+       unsigned int i;
+
+       for (i = 0; *str; i++) {
+               char c = *(str++);
+               if (c == 10)
+                       putchar(13);
+               putchar(c);
+       }
+       return 0;
+}
+
+size_t strlen(const char *s)
+{
+       size_t len = 0;
+
+       while (*s++)
+               len++;
+
+       return len;
+}
index 28395a36002350c5ac5c1fd00ea8c0d5e7393747..367b89dfeb3bcf24dd6566f015027f940b46b5fd 100644 (file)
@@ -4,7 +4,7 @@ include variables.mak
 OBJ = $(BUILD_DIR)/obj
 
 PROGRAM = sdram_init
-OBJECTS = $(OBJ)/head.o $(OBJ)/main.o $(OBJ)/sdram.o
+OBJECTS = $(OBJ)/head.o $(OBJ)/main.o $(OBJ)/sdram.o $(OBJ)/console.o
 
 #### Compiler
 
@@ -50,10 +50,12 @@ all: objdir $(OBJ)/$(PROGRAM).hex
 
 $(OBJ)/sdram.o: $(LXSRC_DIR)/sdram.c
        $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@)
-$(OBJ)/%.o : $(SRC_DIR)/%.S
-       $(call Q,AS, $(CC) $(ASFLAGS) -c $< -o $@, $@)
+$(OBJ)/console.o: $(SRC_DIR)/../../../lib/console.c
+       $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@)
 $(OBJ)/%.o : $(SRC_DIR)/%.c
        $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@)
+$(OBJ)/%.o : $(SRC_DIR)/%.S
+       $(call Q,AS, $(CC) $(ASFLAGS) -c $< -o $@, $@)
 $(OBJ)/%.o : $(SRC_DIR)/libc/src/%.c
        $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@)
 
index 112b085495079f367830ec3afd6586d1c02a3c9b..1c80e349cbffbf8c5e4a21306d6850df2311bee3 120000 (symlink)
@@ -1 +1 @@
-../hello_world/console.c
\ No newline at end of file
+../lib/console.c
\ No newline at end of file
index fbe66d586730509942439c4061726465051afb75..7a6f2c38af6b50b7e6099db9886daac83dba9e8a 120000 (symlink)
@@ -1 +1 @@
-../hello_world/console.h
\ No newline at end of file
+../include/console.h
\ No newline at end of file
index f46140d185533b39c2dd74f4065e444ff3a54d0e..c8797fd71d1a872e77556ddb78761fe1074d45f2 100644 (file)
@@ -20,14 +20,14 @@ void init_bss()
        }
 }
 
-#define HELLO_WORLD "Hello World\r\n"
+#define HELLO_WORLD "Hello World\n"
 
 int main(void)
 {
        init_bss();
        potato_uart_init();
 
-       putstr(HELLO_WORLD, strlen(HELLO_WORLD));
+       puts(HELLO_WORLD);
 
        rust_main();
        crash();
index 9241e3f06af4998d77f33a8b0a73df98fadbcaea..3b800d0492d483340cad8916c60b85788060468d 100644 (file)
@@ -9,14 +9,17 @@ CC = $(CROSS_COMPILE)gcc
 LD = $(CROSS_COMPILE)ld
 OBJCOPY = $(CROSS_COMPILE)objcopy
 
-CFLAGS = -Os -g -Wall -std=c99 -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections -I ../../hello_world -I ../../include
+CFLAGS = -Os -g -Wall -std=c99 -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections -I ../../include
 ASFLAGS = $(CFLAGS)
 LDFLAGS = -T powerpc.lds
 
 all: $(TEST).hex
 
-$(TEST).elf: $(TEST).o head.o ../../hello_world/console.o
-       $(LD) $(LDFLAGS) -o $(TEST).elf $(TEST).o head.o ../../hello_world/console.o
+console.o: ../../lib/console.c
+       $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
+
+$(TEST).elf: $(TEST).o head.o console.o
+       $(LD) $(LDFLAGS) -o $(TEST).elf $(TEST).o head.o console.o
 
 $(TEST).bin: $(TEST).elf
        $(OBJCOPY) -O binary $(TEST).elf $(TEST).bin
index 2617a9462f2ea2113377daca6acfe9d74898a437..e6cff2cc0342ccfc3e04fbf948391b18a792ccd5 100644 (file)
@@ -5,8 +5,8 @@
 #include "console.h"
 
 #define TEST "Test "
-#define PASS "PASS\r\n"
-#define FAIL "FAIL\r\n"
+#define PASS "PASS\n"
+#define FAIL "FAIL\n"
 
 extern int dec_test_1(void);
 extern int dec_test_2(void);
@@ -15,10 +15,10 @@ extern int dec_test_3(void);
 // i < 100
 void print_test_number(int i)
 {
-       putstr(TEST, strlen(TEST));
+       puts(TEST);
        putchar(48 + i/10);
        putchar(48 + i%10);
-       putstr(":", 1);
+       putchar(':');
 }
 
 int main(void)
@@ -30,24 +30,24 @@ int main(void)
        print_test_number(1);
        if (dec_test_1() != 0) {
                fail = 1;
-               putstr(FAIL, strlen(FAIL));
+               puts(FAIL);
        } else
-               putstr(PASS, strlen(PASS));
+               puts(PASS);
 
        print_test_number(2);
        if (dec_test_2() != 0) {
                fail = 1;
-               putstr(FAIL, strlen(FAIL));
+               puts(FAIL);
        } else
-               putstr(PASS, strlen(PASS));
+               puts(PASS);
 
 
        print_test_number(3);
        if (dec_test_3() != 0) {
                fail = 1;
-               putstr(FAIL, strlen(FAIL));
+               puts(FAIL);
        } else
-               putstr(PASS, strlen(PASS));
+               puts(PASS);
 
        return fail;
 }
index a1ea3255d211da01f7bbb2c82d72618f023aa57b..af0c04bcd7abff311ee8c59108b10d65001cbde3 100644 (file)
@@ -5,18 +5,18 @@
 #include "console.h"
 
 #define TEST "Test "
-#define PASS "PASS\r\n"
-#define FAIL "FAIL\r\n"
+#define PASS "PASS\n"
+#define FAIL "FAIL\n"
 
 extern int ill_test_1(void);
 
 // i < 100
 void print_test_number(int i)
 {
-       putstr(TEST, strlen(TEST));
+       puts(TEST);
        putchar(48 + i/10);
        putchar(48 + i%10);
-       putstr(":", 1);
+       putchar(':');
 }
 
 int main(void)
@@ -28,9 +28,9 @@ int main(void)
        print_test_number(1);
        if (ill_test_1() != 0) {
                fail = 1;
-               putstr(FAIL, strlen(FAIL));
+               puts(FAIL);
        } else
-               putstr(PASS, strlen(PASS));
+               puts(PASS);
 
        return fail;
 }
index 2914291e78fb6eacc261d17a6562be7d47537c72..a705d19550deecec94d59d600f2a258af70e4fc9 100644 (file)
@@ -5,18 +5,18 @@
 #include "console.h"
 
 #define TEST "Test "
-#define PASS "PASS\r\n"
-#define FAIL "FAIL\r\n"
+#define PASS "PASS\n"
+#define FAIL "FAIL\n"
 
 extern int sc_test_1(void);
 
 // i < 100
 void print_test_number(int i)
 {
-       putstr(TEST, strlen(TEST));
+       puts(TEST);
        putchar(48 + i/10);
        putchar(48 + i%10);
-       putstr(":", 1);
+       putchar(':');
 }
 
 int main(void)
@@ -28,9 +28,9 @@ int main(void)
        print_test_number(1);
        if (sc_test_1() != 0) {
                fail = 1;
-               putstr(FAIL, strlen(FAIL));
+               puts(FAIL);
        } else
-               putstr(PASS, strlen(PASS));
+               puts(PASS);
 
        return fail;
 }
index 6652cbfefa39f733c545a2fc5180f8f1529a1a97..2ff4c541b3af103c8af56a64266232255fc4e07b 100644 (file)
@@ -29,29 +29,29 @@ void print_number(unsigned int i) // only for i = 0-999
 }
 
 #ifdef DEBUG
-#define DEBUG_STR "\r\nDEBUG: "
+#define DEBUG_STR "\nDEBUG: "
 void debug_print(int i)
 {
-       putstr(DEBUG_STR, strlen(DEBUG_STR));
+       puts(DEBUG_STR);
        print_number(i);
-       putstr("\r\n", 2);
+       puts("\n");
 }
 
-#define debug_putstr(a, b) putstr(a,b)
+#define debug_puts(a) puts(a)
 #else
-#define debug_putstr(a, b)
+#define debug_puts(a)
 #define debug_print(i)
 #endif
 
-#define ASSERT_FAIL "() ASSERT_FAILURE!\r\n "
+#define ASSERT_FAIL "() ASSERT_FAILURE!\n "
 #define assert(cond)   \
        if (!(cond))  { \
-               putstr(__FILE__, strlen(__FILE__)); \
-               putstr(":", 1);     \
+               puts(__FILE__); \
+               putchar(':');       \
                print_number(__LINE__); \
-               putstr(":", 1);     \
-               putstr(__FUNCTION__, strlen(__FUNCTION__));\
-               putstr(ASSERT_FAIL, strlen(ASSERT_FAIL)); \
+               putchar(':');       \
+               puts(__FUNCTION__);\
+               puts(ASSERT_FAIL); \
                __asm__ ("attn"); \
        }
 
@@ -62,17 +62,17 @@ volatile uint64_t isrs_run;
 #define ISR_UART     0x0000000000000002
 #define ISR_SPURIOUS 0x0000000000000004
 
-#define IPI "IPI\r\n"
+#define IPI "IPI\n"
 void ipi_isr(void) {
-       debug_putstr(IPI, strlen(IPI));
+       debug_puts(IPI);
 
        isrs_run |= ISR_IPI;
 }
 
 
-#define UART "UART\r\n"
+#define UART "UART\n"
 void uart_isr(void) {
-       debug_putstr(UART, strlen(UART));
+       debug_puts(UART);
 
        potato_uart_irq_dis(); // disable interrupt to ack it
 
@@ -80,9 +80,9 @@ void uart_isr(void) {
 }
 
 // The hardware doesn't support this but it's part of XICS so add it.
-#define SPURIOUS "SPURIOUS\r\n"
+#define SPURIOUS "SPURIOUS\n"
 void spurious_isr(void) {
-       debug_putstr(SPURIOUS, strlen(SPURIOUS));
+       debug_puts(SPURIOUS);
 
        isrs_run |= ISR_SPURIOUS;
 }
@@ -113,9 +113,9 @@ void isr(void)
        xirr = xics_read32(XICS_XIRR); // read hardware irq source
 
 #ifdef DEBUG
-       putstr(ISR, strlen(ISR));
+       puts(ISR);
        print_number(xirr & 0xff);
-       putstr("\r\n", 2);
+       puts("\n");
 #endif
 
        op = isr_table;
@@ -221,8 +221,8 @@ int xics_test_2(void)
 }
 
 #define TEST "Test "
-#define PASS "PASS\r\n"
-#define FAIL "FAIL\r\n"
+#define PASS "PASS\n"
+#define FAIL "FAIL\n"
 
 int (*tests[])(void) = {
        xics_test_0,
@@ -246,14 +246,14 @@ int main(void)
                if (!t)
                        break;
 
-               putstr(TEST, strlen(TEST));
+               puts(TEST);
                print_number(i);
-               putstr(": ", 1);
+               putchar(':');
                if (t() != 0) {
                        fail = 1;
-                       putstr(FAIL, strlen(FAIL));
+                       puts(FAIL);
                } else
-                       putstr(PASS, strlen(PASS));
+                       puts(PASS);
 
                i++;
        }