BIOS: hello world
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Sun, 5 Feb 2012 19:01:28 +0000 (20:01 +0100)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Sun, 5 Feb 2012 19:01:28 +0000 (20:01 +0100)
software/bios/Makefile [new file with mode: 0644]
software/bios/crt0.S [new file with mode: 0644]
software/bios/isr.c [new file with mode: 0644]
software/bios/linker.ld [new file with mode: 0644]
software/bios/main.c [new file with mode: 0644]
software/include.mak
software/libbase/Makefile

diff --git a/software/bios/Makefile b/software/bios/Makefile
new file mode 100644 (file)
index 0000000..5a5ac66
--- /dev/null
@@ -0,0 +1,30 @@
+M2DIR=../..
+include $(M2DIR)/software/include.mak
+
+OBJECTS=crt0.o isr.o main.o
+
+all: bios.bin
+
+# pull in dependency info for *existing* .o files
+-include $(OBJECTS:.o=.d)
+
+%.bin: %.elf
+       $(MAKE) -C $(M2DIR)/tools
+       $(OBJCOPY) -O binary $< $@
+       chmod -x $@
+       $(M2DIR)/tools/mkmmimg $@ write
+
+bios.elf: linker.ld $(OBJECTS) libs
+bios-rescue.elf: linker-rescue.ld $(OBJECTS) libs
+
+%.elf:
+       $(LD) $(LDFLAGS) -T $< -N -o $@ $(OBJECTS) -L$(M2DIR)/software/libbase -lbase
+       chmod -x $@
+
+libs:
+       make -C $(M2DIR)/software/libbase
+
+.PHONY: clean libs
+
+clean:
+       rm -f $(OBJECTS) $(OBJECTS:.o=.d) bios.elf bios.bin bios-rescue.elf bios-rescue.bin .*~ *~
diff --git a/software/bios/crt0.S b/software/bios/crt0.S
new file mode 100644 (file)
index 0000000..6279720
--- /dev/null
@@ -0,0 +1,167 @@
+/*
+ * LatticeMico32 C startup code.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Exception handlers - Must be 32 bytes long. */
+.section    .text, "ax", @progbits
+.global     _start
+_start:
+_reset_handler:
+       xor     r0, r0, r0
+       wcsr    IE, r0
+       mvhi    r1, hi(_reset_handler)
+       ori     r1, r1, lo(_reset_handler)
+       wcsr    EBA, r1
+       bi      _crt0
+       nop
+       nop
+
+_breakpoint_handler:
+       bi _breakpoint_handler
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+
+_instruction_bus_error_handler:
+       bi _instruction_bus_error_handler
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+
+_watchpoint_hander:
+       bi _watchpoint_hander
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+
+_data_bus_error_handler:
+       bi _data_bus_error_handler
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+
+_divide_by_zero_handler:
+       bi _divide_by_zero_handler
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+       nop
+
+_interrupt_handler:
+       sw      (sp+0), ra
+       calli   .save_all
+       calli   isr
+       bi      .restore_all_and_eret
+       nop
+       nop
+       nop
+       nop
+
+macaddress:
+       .byte 0x10
+       .byte 0xe2
+       .byte 0xd5
+       .byte 0x00
+       .byte 0x00
+       .byte 0x00
+
+       /* padding to align to a 32-bit boundary */
+       .byte 0x00
+       .byte 0x00
+
+_crt0:
+       /* Setup stack and global pointer */
+       mvhi    sp, hi(_fstack)
+       ori     sp, sp, lo(_fstack)
+       mvhi    gp, hi(_gp)
+       ori     gp, gp, lo(_gp)
+
+       /* Clear BSS */
+       mvhi    r1, hi(_fbss)
+       ori     r1, r1, lo(_fbss)
+       mvhi    r3, hi(_ebss)
+       ori     r3, r3, lo(_ebss)
+.clearBSS:
+       be      r1, r3, .callMain
+       sw      (r1+0), r0
+       addi    r1, r1, 4
+       bi      .clearBSS
+
+.callMain:
+       bi      main
+
+.save_all:
+       addi    sp, sp, -56
+       sw      (sp+4), r1
+       sw      (sp+8), r2
+       sw      (sp+12), r3
+       sw      (sp+16), r4
+       sw      (sp+20), r5
+       sw      (sp+24), r6
+       sw      (sp+28), r7
+       sw      (sp+32), r8
+       sw      (sp+36), r9
+       sw      (sp+40), r10
+       sw      (sp+48), ea
+       sw      (sp+52), ba
+       /* ra needs to be moved from initial stack location */
+       lw      r1, (sp+56)
+       sw      (sp+44), r1
+       ret
+
+.restore_all_and_eret:
+       lw      r1, (sp+4)
+       lw      r2, (sp+8)
+       lw      r3, (sp+12)
+       lw      r4, (sp+16)
+       lw      r5, (sp+20)
+       lw      r6, (sp+24)
+       lw      r7, (sp+28)
+       lw      r8, (sp+32)
+       lw      r9, (sp+36)
+       lw      r10, (sp+40)
+       lw      ra, (sp+44)
+       lw      ea, (sp+48)
+       lw      ba, (sp+52)
+       addi    sp, sp, 56
+       eret
diff --git a/software/bios/isr.c b/software/bios/isr.c
new file mode 100644 (file)
index 0000000..25badec
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Milkymist SoC (Software)
+ * Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <hw/interrupts.h>
+#include <irq.h>
+
+void isr(void)
+{
+       unsigned int irqs;
+       irqs = irq_pending() & irq_getmask();
+}
diff --git a/software/bios/linker.ld b/software/bios/linker.ld
new file mode 100644 (file)
index 0000000..f1d4cc1
--- /dev/null
@@ -0,0 +1,57 @@
+OUTPUT_FORMAT("elf32-lm32")
+ENTRY(_start)
+
+__DYNAMIC = 0;
+
+MEMORY {
+       flash : ORIGIN = 0x00860000, LENGTH = 0x20000 /* 128K */
+       sram  : ORIGIN = 0x10000000, LENGTH = 0x01000 /* 4K */
+}
+
+SECTIONS
+{
+       .text :
+       {
+               _ftext = .;
+               *(.text .stub .text.* .gnu.linkonce.t.*)
+               _etext = .;
+       } > flash
+
+       .rodata :
+       {
+               . = ALIGN(4);
+               _frodata = .;
+               *(.rodata .rodata.* .gnu.linkonce.r.*)
+               *(.rodata1)
+               _erodata = .;
+       } > flash
+
+       /* We shouldn't have a .data section, but the GNU crapchain whines if we don't */
+       .data :
+       {
+               . = ALIGN(4);
+               _fdata = .;
+               *(.data .data.* .gnu.linkonce.d.*)
+               *(.data1)
+               _gp = ALIGN(16);
+               *(.sdata .sdata.* .gnu.linkonce.s.*)
+               _edata = .;
+       } > flash
+
+       .bss :
+       {
+               . = ALIGN(4);
+               _fbss = .;
+               *(.dynsbss)
+               *(.sbss .sbss.* .gnu.linkonce.sb.*)
+               *(.scommon)
+               *(.dynbss)
+               *(.bss .bss.* .gnu.linkonce.b.*)
+               *(COMMON)
+               . = ALIGN(4);
+               _ebss = .;
+               _end = .;
+       } > sram
+}
+
+PROVIDE(_fstack = ORIGIN(sram) + LENGTH(sram) - 4);
diff --git a/software/bios/main.c b/software/bios/main.c
new file mode 100644 (file)
index 0000000..c479dfc
--- /dev/null
@@ -0,0 +1,16 @@
+#include <hw/uart.h>
+
+static void print(const char *s)
+{
+       while(*s) {
+               while(!(CSR_UART_STAT & UART_STAT_THRE));
+               CSR_UART_RXTX = *s;
+               s++;
+       }
+}
+
+int main(void)
+{
+       print("Hello World\n");
+       while(1);
+}
index f417bba00c93c744861aa0df928feff78ff60097..e7129063c3da09841cd53a30b43ce5ac05124fdc 100644 (file)
@@ -34,8 +34,8 @@ endif
 
 # Toolchain options
 #
-INCLUDES_NOLIBC ?= -nostdinc -I$(MMDIR)/software/include/base
-INCLUDES = $(INCLUDES_NOLIBC) -I$(MMDIR)/software/include -I$(MMDIR)/tools
+INCLUDES_NOLIBC ?= -nostdinc -I$(M2DIR)/software/include/base
+INCLUDES = $(INCLUDES_NOLIBC) -I$(M2DIR)/software/include -I$(M2DIR)/tools
 ASFLAGS = $(INCLUDES) -nostdinc
 # later: -Wmissing-prototypes
 CFLAGS = -O9 -Wall -Wstrict-prototypes -Wold-style-definition -Wshadow \
index b01fc7f9f920b038f0a49426672db7ce3ba252f9..0ee64cc6debb45d3ed0638c088e7a22ca639f83c 100644 (file)
@@ -1,13 +1,13 @@
-MMDIR=../..
-include $(MMDIR)/software/include.mak
+M2DIR=../..
+include $(M2DIR)/software/include.mak
 
 OBJECTS=divsi3.o libc.o console.o system.o board.o uart.o softfloat.o softfloat-glue.o vsnprintf.o atof.o
 
+all: libbase.a
+
 # pull in dependency info for *existing* .o files
 -include $(OBJECTS:.o=.d)
 
-all: libbase.a
-
 libbase.a: $(OBJECTS)
        $(AR) clr libbase.a $(OBJECTS)
        $(RANLIB) libbase.a
@@ -15,4 +15,4 @@ libbase.a: $(OBJECTS)
 .PHONY: clean
 
 clean:
-       rm -f $(OBJECTS) $(OBJECTS:.o=.d) libbase.a .*~ *~ Makefile.bak
+       rm -f $(OBJECTS) $(OBJECTS:.o=.d) libbase.a .*~ *~