Permalink
Browse files

Add a more close to reality printf implementation

  • Loading branch information...
1 parent d6e1725 commit f3e5eb29d1bab85b7603f45dafc3b4c56f41f9e3 @fallen committed Mar 25, 2012
@@ -1,7 +1,7 @@
MMDIR=../..
include $(MMDIR)/software/include.mak
-OBJECTS=crt0.o main.o dtlb_load_test.o mmu.o isr.o
+OBJECTS=crt0.o main.o dtlb_load_test.o mmu.o isr.o vsnprintf-nofloat.o
SEGMENTS=-j .text -j .data -j .rodata
all: bios.bin
View
@@ -1,3 +1,45 @@
+
+ inline void irq_enable(unsigned int en)
+{
+ __asm__ __volatile__("wcsr IE, %0" : : "r" (en));
+}
+
+ inline unsigned int irq_getmask(void)
+{
+ unsigned int mask;
+ __asm__ __volatile__("rcsr %0, IM" : "=r" (mask));
+ return mask;
+}
+
+ inline void irq_setmask(unsigned int mask)
+{
+ __asm__ __volatile__("wcsr IM, %0" : : "r" (mask));
+}
+
+ inline unsigned int irq_pending(void)
+{
+ unsigned int pending;
+ __asm__ __volatile__("rcsr %0, IP" : "=r" (pending));
+ return pending;
+}
+
+ inline void irq_ack(unsigned int mask)
+{
+ __asm__ __volatile__("wcsr IP, %0" : : "r" (mask));
+}
+
+ inline unsigned int irq_getie(void)
+{
+ unsigned int ie;
+ __asm__ __volatile__("rcsr %0, IE" : "=r" (ie));
+ return ie;
+}
+
+ inline void irq_setie(unsigned int ie)
+{
+ __asm__ __volatile__("wcsr IE, %0" : : "r" (ie));
+}
+
void isr(void)
{
@@ -4,7 +4,7 @@ ENTRY(_start)
__DYNAMIC = 0;
MEMORY {
- sram : ORIGIN = 0x44000000, LENGTH = 0xFF0
+ sram : ORIGIN = 0x44000000, LENGTH = 0x2000
}
SECTIONS
View
@@ -1,17 +1,75 @@
-void puts(char *str)
+#include "main.h"
+
+
+void uart_write(char c)
{
+ unsigned int oldmask;
+
+ oldmask = irq_getmask();
+ irq_setmask(0);
- while (*str != '\0')
- {
- *((unsigned int *)0x44003000) = *str;
- str++;
+ CSR_UART_RXTX = c;
+ irq_setmask(oldmask);
+}
+
+static void writechar(char c)
+{
+ uart_write(c);
+}
+
+int puts(const char *s)
+{
+ unsigned int oldmask;
+
+ oldmask = irq_getmask();
+ irq_setmask(IRQ_UART); // HACK: prevent UART data loss
+
+ while(*s) {
+ writechar(*s);
+ s++;
}
+ writechar('\n');
+
+ irq_setmask(oldmask);
+ return 1;
+}
+
+void putsnonl(const char *s)
+{
+ unsigned int oldmask;
+
+ oldmask = irq_getmask();
+ irq_setmask(IRQ_UART); // HACK: prevent UART data loss
+
+ while(*s) {
+ writechar(*s);
+ s++;
+ }
+
+ irq_setmask(oldmask);
+}
+
+int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
+{
+ int i;
+ i=vsnprintf(buf,size,fmt,args);
+ return (i >= size) ? (size - 1) : i;
}
-void printf(char *str)
+int printf(const char *fmt, ...)
{
- puts(str);
+ va_list args;
+ int len;
+ char outbuf[256];
+
+ va_start(args, fmt);
+ len = vscnprintf(outbuf, sizeof(outbuf), fmt, args);
+ va_end(args);
+ outbuf[len] = 0;
+ putsnonl(outbuf);
+
+ return len;
}
int main(int argc, char **argv)
View
@@ -0,0 +1,22 @@
+typedef int size_t;
+#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4))
+#define va_start(v,l) __builtin_va_start((v),l)
+#else
+#define va_start(v,l) __builtin_stdarg_start((v),l)
+#endif
+
+#define va_arg(ap, type) \
+ __builtin_va_arg((ap), type)
+
+#define va_end(ap) \
+ __builtin_va_end(ap)
+
+#define va_list \
+ __builtin_va_list
+
+#define unlikely(x) x
+
+#define IRQ_UART (0x00000001)
+#define CSR_UART_RXTX *((volatile unsigned int *)0x44003000)
+
+int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
Oops, something went wrong.

0 comments on commit f3e5eb2

Please sign in to comment.