Merge pull request #176 from antonblanchard/console-improv

Console improvements from Ben
pull/171/head
Anton Blanchard 4 years ago committed by GitHub
commit e9251544f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,6 +15,9 @@ LDFLAGS = -T powerpc.lds


all: hello_world.hex all: hello_world.hex


console.o: ../lib/console.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

hello_world.elf: hello_world.o head.o console.o hello_world.elf: hello_world.o head.o console.o
$(LD) $(LDFLAGS) -o $@ $^ $(LD) $(LDFLAGS) -o $@ $^



@ -3,13 +3,13 @@


#include "console.h" #include "console.h"


#define HELLO_WORLD "Hello World\r\n" #define HELLO_WORLD "Hello World\n"


int main(void) int main(void)
{ {
potato_uart_init(); potato_uart_init();


putstr(HELLO_WORLD, strlen(HELLO_WORLD)); puts(HELLO_WORLD);


while (1) { while (1) {
unsigned char c = getchar(); unsigned char c = getchar();

@ -4,6 +4,6 @@ void potato_uart_init(void);
void potato_uart_irq_en(void); void potato_uart_irq_en(void);
void potato_uart_irq_dis(void); void potato_uart_irq_dis(void);
int getchar(void); int getchar(void);
void putchar(unsigned char c); int putchar(int c);
void putstr(const char *str, unsigned long len); int puts(const char *str);
size_t strlen(const char *s); size_t strlen(const char *s);

@ -98,19 +98,26 @@ int getchar(void)
return potato_uart_read(); return potato_uart_read();
} }


void putchar(unsigned char c) int putchar(int c)
{ {
while (potato_uart_tx_full()) while (potato_uart_tx_full())
/* Do Nothing */; /* Do Nothing */;


potato_uart_write(c); potato_uart_write(c);
return c;
} }


void putstr(const char *str, unsigned long len) int puts(const char *str)
{ {
for (unsigned long i = 0; i < len; i++) { unsigned int i;
putchar(str[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 strlen(const char *s)

@ -4,7 +4,7 @@ include variables.mak
OBJ = $(BUILD_DIR)/obj OBJ = $(BUILD_DIR)/obj


PROGRAM = sdram_init 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 #### Compiler


@ -50,10 +50,12 @@ all: objdir $(OBJ)/$(PROGRAM).hex


$(OBJ)/sdram.o: $(LXSRC_DIR)/sdram.c $(OBJ)/sdram.o: $(LXSRC_DIR)/sdram.c
$(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@) $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@)
$(OBJ)/%.o : $(SRC_DIR)/%.S $(OBJ)/console.o: $(SRC_DIR)/../../../lib/console.c
$(call Q,AS, $(CC) $(ASFLAGS) -c $< -o $@, $@) $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@)
$(OBJ)/%.o : $(SRC_DIR)/%.c $(OBJ)/%.o : $(SRC_DIR)/%.c
$(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@) $(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 $(OBJ)/%.o : $(SRC_DIR)/libc/src/%.c
$(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@) $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@)



@ -1 +1 @@
../hello_world/console.c ../lib/console.c

@ -1 +1 @@
../hello_world/console.h ../include/console.h

@ -20,14 +20,14 @@ void init_bss()
} }
} }


#define HELLO_WORLD "Hello World\r\n" #define HELLO_WORLD "Hello World\n"


int main(void) int main(void)
{ {
init_bss(); init_bss();
potato_uart_init(); potato_uart_init();


putstr(HELLO_WORLD, strlen(HELLO_WORLD)); puts(HELLO_WORLD);


rust_main(); rust_main();
crash(); crash();

@ -9,14 +9,17 @@ CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld LD = $(CROSS_COMPILE)ld
OBJCOPY = $(CROSS_COMPILE)objcopy 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) ASFLAGS = $(CFLAGS)
LDFLAGS = -T powerpc.lds LDFLAGS = -T powerpc.lds


all: $(TEST).hex all: $(TEST).hex


$(TEST).elf: $(TEST).o head.o ../../hello_world/console.o console.o: ../../lib/console.c
$(LD) $(LDFLAGS) -o $(TEST).elf $(TEST).o head.o ../../hello_world/console.o $(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 $(TEST).bin: $(TEST).elf
$(OBJCOPY) -O binary $(TEST).elf $(TEST).bin $(OBJCOPY) -O binary $(TEST).elf $(TEST).bin

@ -5,8 +5,8 @@
#include "console.h" #include "console.h"


#define TEST "Test " #define TEST "Test "
#define PASS "PASS\r\n" #define PASS "PASS\n"
#define FAIL "FAIL\r\n" #define FAIL "FAIL\n"


extern int dec_test_1(void); extern int dec_test_1(void);
extern int dec_test_2(void); extern int dec_test_2(void);
@ -15,10 +15,10 @@ extern int dec_test_3(void);
// i < 100 // i < 100
void print_test_number(int i) void print_test_number(int i)
{ {
putstr(TEST, strlen(TEST)); puts(TEST);
putchar(48 + i/10); putchar(48 + i/10);
putchar(48 + i%10); putchar(48 + i%10);
putstr(":", 1); putchar(':');
} }


int main(void) int main(void)
@ -30,24 +30,24 @@ int main(void)
print_test_number(1); print_test_number(1);
if (dec_test_1() != 0) { if (dec_test_1() != 0) {
fail = 1; fail = 1;
putstr(FAIL, strlen(FAIL)); puts(FAIL);
} else } else
putstr(PASS, strlen(PASS)); puts(PASS);


print_test_number(2); print_test_number(2);
if (dec_test_2() != 0) { if (dec_test_2() != 0) {
fail = 1; fail = 1;
putstr(FAIL, strlen(FAIL)); puts(FAIL);
} else } else
putstr(PASS, strlen(PASS)); puts(PASS);




print_test_number(3); print_test_number(3);
if (dec_test_3() != 0) { if (dec_test_3() != 0) {
fail = 1; fail = 1;
putstr(FAIL, strlen(FAIL)); puts(FAIL);
} else } else
putstr(PASS, strlen(PASS)); puts(PASS);


return fail; return fail;
} }

@ -5,18 +5,18 @@
#include "console.h" #include "console.h"


#define TEST "Test " #define TEST "Test "
#define PASS "PASS\r\n" #define PASS "PASS\n"
#define FAIL "FAIL\r\n" #define FAIL "FAIL\n"


extern int ill_test_1(void); extern int ill_test_1(void);


// i < 100 // i < 100
void print_test_number(int i) void print_test_number(int i)
{ {
putstr(TEST, strlen(TEST)); puts(TEST);
putchar(48 + i/10); putchar(48 + i/10);
putchar(48 + i%10); putchar(48 + i%10);
putstr(":", 1); putchar(':');
} }


int main(void) int main(void)
@ -28,9 +28,9 @@ int main(void)
print_test_number(1); print_test_number(1);
if (ill_test_1() != 0) { if (ill_test_1() != 0) {
fail = 1; fail = 1;
putstr(FAIL, strlen(FAIL)); puts(FAIL);
} else } else
putstr(PASS, strlen(PASS)); puts(PASS);


return fail; return fail;
} }

@ -5,18 +5,18 @@
#include "console.h" #include "console.h"


#define TEST "Test " #define TEST "Test "
#define PASS "PASS\r\n" #define PASS "PASS\n"
#define FAIL "FAIL\r\n" #define FAIL "FAIL\n"


extern int sc_test_1(void); extern int sc_test_1(void);


// i < 100 // i < 100
void print_test_number(int i) void print_test_number(int i)
{ {
putstr(TEST, strlen(TEST)); puts(TEST);
putchar(48 + i/10); putchar(48 + i/10);
putchar(48 + i%10); putchar(48 + i%10);
putstr(":", 1); putchar(':');
} }


int main(void) int main(void)
@ -28,9 +28,9 @@ int main(void)
print_test_number(1); print_test_number(1);
if (sc_test_1() != 0) { if (sc_test_1() != 0) {
fail = 1; fail = 1;
putstr(FAIL, strlen(FAIL)); puts(FAIL);
} else } else
putstr(PASS, strlen(PASS)); puts(PASS);


return fail; return fail;
} }

@ -29,29 +29,29 @@ void print_number(unsigned int i) // only for i = 0-999
} }


#ifdef DEBUG #ifdef DEBUG
#define DEBUG_STR "\r\nDEBUG: " #define DEBUG_STR "\nDEBUG: "
void debug_print(int i) void debug_print(int i)
{ {
putstr(DEBUG_STR, strlen(DEBUG_STR)); puts(DEBUG_STR);
print_number(i); print_number(i);
putstr("\r\n", 2); puts("\n");
} }


#define debug_putstr(a, b) putstr(a,b) #define debug_puts(a) puts(a)
#else #else
#define debug_putstr(a, b) #define debug_puts(a)
#define debug_print(i) #define debug_print(i)
#endif #endif


#define ASSERT_FAIL "() ASSERT_FAILURE!\r\n " #define ASSERT_FAIL "() ASSERT_FAILURE!\n "
#define assert(cond) \ #define assert(cond) \
if (!(cond)) { \ if (!(cond)) { \
putstr(__FILE__, strlen(__FILE__)); \ puts(__FILE__); \
putstr(":", 1); \ putchar(':'); \
print_number(__LINE__); \ print_number(__LINE__); \
putstr(":", 1); \ putchar(':'); \
putstr(__FUNCTION__, strlen(__FUNCTION__));\ puts(__FUNCTION__);\
putstr(ASSERT_FAIL, strlen(ASSERT_FAIL)); \ puts(ASSERT_FAIL); \
__asm__ ("attn"); \ __asm__ ("attn"); \
} }


@ -62,17 +62,17 @@ volatile uint64_t isrs_run;
#define ISR_UART 0x0000000000000002 #define ISR_UART 0x0000000000000002
#define ISR_SPURIOUS 0x0000000000000004 #define ISR_SPURIOUS 0x0000000000000004


#define IPI "IPI\r\n" #define IPI "IPI\n"
void ipi_isr(void) { void ipi_isr(void) {
debug_putstr(IPI, strlen(IPI)); debug_puts(IPI);


isrs_run |= ISR_IPI; isrs_run |= ISR_IPI;
} }




#define UART "UART\r\n" #define UART "UART\n"
void uart_isr(void) { void uart_isr(void) {
debug_putstr(UART, strlen(UART)); debug_puts(UART);


potato_uart_irq_dis(); // disable interrupt to ack it 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. // 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) { void spurious_isr(void) {
debug_putstr(SPURIOUS, strlen(SPURIOUS)); debug_puts(SPURIOUS);


isrs_run |= ISR_SPURIOUS; isrs_run |= ISR_SPURIOUS;
} }
@ -113,9 +113,9 @@ void isr(void)
xirr = xics_read32(XICS_XIRR); // read hardware irq source xirr = xics_read32(XICS_XIRR); // read hardware irq source


#ifdef DEBUG #ifdef DEBUG
putstr(ISR, strlen(ISR)); puts(ISR);
print_number(xirr & 0xff); print_number(xirr & 0xff);
putstr("\r\n", 2); puts("\n");
#endif #endif


op = isr_table; op = isr_table;
@ -221,8 +221,8 @@ int xics_test_2(void)
} }


#define TEST "Test " #define TEST "Test "
#define PASS "PASS\r\n" #define PASS "PASS\n"
#define FAIL "FAIL\r\n" #define FAIL "FAIL\n"


int (*tests[])(void) = { int (*tests[])(void) = {
xics_test_0, xics_test_0,
@ -246,14 +246,14 @@ int main(void)
if (!t) if (!t)
break; break;


putstr(TEST, strlen(TEST)); puts(TEST);
print_number(i); print_number(i);
putstr(": ", 1); putchar(':');
if (t() != 0) { if (t() != 0) {
fail = 1; fail = 1;
putstr(FAIL, strlen(FAIL)); puts(FAIL);
} else } else
putstr(PASS, strlen(PASS)); puts(PASS);


i++; i++;
} }

Loading…
Cancel
Save