[PATCH 04/21] Add a banch of examples for MIPS architecture
Aleksandar Rikalo <[email protected]>
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <[email protected]> |
From: Jaydeep Patil <[email protected]> Signed-off-by: Jaydeep Patil <[email protected]> Signed-off-by: Aleksandar Rikalo <[email protected]> --- libgloss/mips/Makefile.in | 35 ++++ libgloss/mips/examples/custom_excpt/Makefile | 50 +++++ .../mips/examples/custom_excpt/README.txt | 15 ++ .../mips/examples/custom_excpt/custom_excpt.c | 112 ++++++++++ .../mips/examples/fault_recovery/Makefile | 50 +++++ .../mips/examples/fault_recovery/README.txt | 15 ++ .../examples/fault_recovery/fault_recovery.c | 77 +++++++ .../mips/examples/interrupt_handler/Makefile | 50 +++++ .../examples/interrupt_handler/README.txt | 14 ++ .../interrupt_handler/interrupt_handler.c | 72 +++++++ .../mips/examples/isr_vector_space/Makefile | 54 +++++ .../mips/examples/isr_vector_space/README.txt | 19 ++ .../examples/isr_vector_space/excpt_isr.S | 111 ++++++++++ .../isr_vector_space/isr_vector_space.c | 74 +++++++ libgloss/mips/examples/romable/Makefile | 61 ++++++ libgloss/mips/examples/romable/README.txt | 15 ++ libgloss/mips/examples/romable/romable.c | 38 ++++ .../mips/examples/romable_minimal/Makefile | 67 ++++++ .../mips/examples/romable_minimal/README.txt | 32 +++ .../examples/romable_minimal/makescripts.sh | 31 +++ .../romable_minimal/romable_minimal.c | 39 ++++ .../mips/examples/romable_predef/Makefile | 98 +++++++++ .../mips/examples/romable_predef/README.txt | 16 ++ .../examples/romable_predef/romable_predef.c | 38 ++++ libgloss/mips/rules/mipshal.mk | 135 ++++++++++++ libgloss/mips/rules/srec2hex.pl | 196 ++++++++++++++++++ 26 files changed, 1514 insertions(+) create mode 100644 libgloss/mips/examples/custom_excpt/Makefile create mode 100644 libgloss/mips/examples/custom_excpt/README.txt create mode 100644 libgloss/mips/examples/custom_excpt/custom_excpt.c create mode 100644 libgloss/mips/examples/fault_recovery/Makefile create mode 100644 libgloss/mips/examples/fault_recovery/README.txt create mode 100644 libgloss/mips/examples/fault_recovery/fault_recovery.c create mode 100644 libgloss/mips/examples/interrupt_handler/Makefile create mode 100644 libgloss/mips/examples/interrupt_handler/README.txt create mode 100644 libgloss/mips/examples/interrupt_handler/interrupt_handler.c create mode 100644 libgloss/mips/examples/isr_vector_space/Makefile create mode 100644 libgloss/mips/examples/isr_vector_space/README.txt create mode 100644 libgloss/mips/examples/isr_vector_space/excpt_isr.S create mode 100644 libgloss/mips/examples/isr_vector_space/isr_vector_space.c create mode 100644 libgloss/mips/examples/romable/Makefile create mode 100644 libgloss/mips/examples/romable/README.txt create mode 100644 libgloss/mips/examples/romable/romable.c create mode 100644 libgloss/mips/examples/romable_minimal/Makefile create mode 100644 libgloss/mips/examples/romable_minimal/README.txt create mode 100644 libgloss/mips/examples/romable_minimal/makescripts.sh create mode 100644 libgloss/mips/examples/romable_minimal/romable_minimal.c create mode 100644 libgloss/mips/examples/romable_predef/Makefile create mode 100644 libgloss/mips/examples/romable_predef/README.txt create mode 100644 libgloss/mips/examples/romable_predef/romable_predef.c create mode 100644 libgloss/mips/rules/mipshal.mk create mode 100755 libgloss/mips/rules/srec2hex.pl diff --git a/libgloss/mips/Makefile.in b/libgloss/mips/Makefile.in index 6e764dc14..c1e22f1e4 100644 --- a/libgloss/mips/Makefile.in +++ b/libgloss/mips/Makefile.in @@ -251,6 +251,41 @@ install: @for script in ${SCRIPTS}; do\ $(INSTALL_DATA) ${srcdir}/$${script}.ld $(DESTDIR)$(tooldir)/lib${MULTISUBDIR}/$${script}.ld; \ done + mkdir -p $(DESTDIR)$(tooldir)/include/mips + @for header in ${srcdir}/include/mips/*.h; do\ + $(INSTALL_DATA) $${header} $(DESTDIR)$(tooldir)/include/mips; \ + done + @for file in ${srcdir}/include/mips/*.S; do\ + $(INSTALL_DATA) $${file} $(DESTDIR)$(tooldir)/include/mips; \ + done + mkdir -p $(DESTDIR)$(exec_prefix)/share/mips/boot + @for file in ${srcdir}/boot/*.[hS] ; do\ + $(INSTALL_DATA) $${file} $(DESTDIR)$(exec_prefix)/share/mips/boot/; \ + done + mkdir -p $(DESTDIR)$(exec_prefix)/share/mips/hal + @for file in ${srcdir}/hal/*.[hSc]; do\ + $(INSTALL_DATA) $${file} $(DESTDIR)$(exec_prefix)/share/mips/hal/; \ + done + mkdir -p $(DESTDIR)$(exec_prefix)/share/mips/uhi + @for file in ${srcdir}/uhi/uhi_*.[hSc] ${srcdir}/uhi/yamon_*.[hSc]; do\ + $(INSTALL_DATA) $${file} $(DESTDIR)$(exec_prefix)/share/mips/uhi/; \ + done + mkdir -p $(DESTDIR)$(exec_prefix)/share/mips/rules + @$(INSTALL_DATA) ${srcdir}/rules/mipshal.mk $(DESTDIR)$(exec_prefix)/share/mips/rules/ + @$(INSTALL_PROGRAM) ${srcdir}/rules/srec2hex.pl $(DESTDIR)$(exec_prefix)/share/mips/rules/ + mkdir -p $(DESTDIR)$(exec_prefix)/share/mips/examples + for example in $(EXAMPLES); do\ + mkdir -p $(DESTDIR)$(exec_prefix)/share/mips/examples/$${example}; \ + $(INSTALL_DATA) ${srcdir}/examples/$${example}/Makefile \ + $(DESTDIR)$(exec_prefix)/share/mips/examples/$${example}/; \ + $(INSTALL_DATA) ${srcdir}/examples/$${example}/README.txt \ + $(DESTDIR)$(exec_prefix)/share/mips/examples/$${example}/; \ + for file in ${srcdir}/examples/$${example}/*.[cS]; do\ + $(INSTALL_DATA) $${file} $(DESTDIR)$(exec_prefix)/share/mips/examples/$${example}/; \ + done; \ + done + $(INSTALL_PROGRAM) ${srcdir}/examples/romable_minimal/makescripts.sh \ + $(DESTDIR)$(exec_prefix)/share/mips/examples/romable_minimal/makescripts.sh info: install-info: diff --git a/libgloss/mips/examples/custom_excpt/Makefile b/libgloss/mips/examples/custom_excpt/Makefile new file mode 100644 index 000000000..84b1b9a10 --- /dev/null +++ b/libgloss/mips/examples/custom_excpt/Makefile @@ -0,0 +1,50 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Variables that mipshal.mk will expand to compiler/linker arguments must be +# defined before inclusion of mipshal.mk + +include ${MIPS_ELF_ROOT}/share/mips/rules/mipshal.mk + +CFLAGS += -g -O1 +LDFLAGS += -g + +OBJS = custom_excpt.o +APP = custom_excpt.elf + +all: $(APP) + +$(APP): $(OBJS) + $(LD) $(LDFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $^ -o $@ + +.PHONY: clean +clean: + rm -f $(APP) $(OBJS) diff --git a/libgloss/mips/examples/custom_excpt/README.txt b/libgloss/mips/examples/custom_excpt/README.txt new file mode 100644 index 000000000..8df200334 --- /dev/null +++ b/libgloss/mips/examples/custom_excpt/README.txt @@ -0,0 +1,15 @@ +Demonstration of exception handling by emulating r0 style accesses for memory +with _mips_handle_exception. + +Set environment variable MIPS_ELF_ROOT + This should be set to the root of the installation directory for the + toolchain (that is, below the bin directory) + +To build big-endian + # make + +To build little-endian + # make ENDIAN=EL + +To delete temporary and built files + # make clean diff --git a/libgloss/mips/examples/custom_excpt/custom_excpt.c b/libgloss/mips/examples/custom_excpt/custom_excpt.c new file mode 100644 index 000000000..e459d4f2b --- /dev/null +++ b/libgloss/mips/examples/custom_excpt/custom_excpt.c @@ -0,0 +1,112 @@ +/* + * Copyright 2015, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <mips/hal.h> +#include <mips/cpu.h> +int +main () +{ + /* Some pointer setup to access memory address 0*/ + int b = -1; + int *a = 0; + /* Write to 0, triggering a TLB store error */ + *a = 0; + /* Read from 0, triggering a TLB load error */ + b = *a; + /* Return value should be 0 */ + return b; +} + +/* + * This exception handler emulates register zero style handling but for memory, + * i.e. writes to page zero are ignored and reads return a zero for select + * instructions. + */ +void +_mips_handle_exception (struct gpctx *ctx, int exception) +{ + /* + * This example is designed to work only on MIPS32 and does not handle the + * case where a memory operation occurs in a branch delay slot. + */ + if ((mips32_get_c0 (C0_CAUSE) & CR_BD) == 0 + && (ctx->epc & 0x1) == 0) + { + unsigned int fault_instruction = 0; + switch (exception) + { + case EXC_TLBS: + fault_instruction = *((unsigned int *) (ctx->epc)); + if ((fault_instruction >> 26) == 0x2b) + { + /* Store instruction, ensure it went to zero. */ + unsigned int store_reg = (fault_instruction >> 21) & 0x1f; + int16_t offset = fault_instruction & 0xffff; + unsigned int destination = ctx->r[C_CTX_REGNO(store_reg)] + offset; + + if (destination < 4096) + { + write (1, "Handled write to zero page\n", 27); + ctx->epc += 4; + return; + } + } + break; + + case EXC_TLBL: + fault_instruction = *((unsigned int *) (ctx->epc)); + if ((fault_instruction >> 26) == 0x23) + { + /* Load instruction, ensure it went to zero. */ + unsigned int load_reg = (fault_instruction >> 21) & 0x1f; + int16_t offset = fault_instruction & 0xffff; + unsigned int destination = ctx->r[C_CTX_REGNO(load_reg)] + offset; + if (destination < 4096) + { + write (1, "Handled read from zero page\n", 28); + ctx->epc += 4; + unsigned int rt = (fault_instruction >> 16) & 0x1f; + ctx->r[rt] = 0; + return; + } + } + break; + + default: + break; + } + } + + /* All other exceptions are passed to the default exception handler. */ + __exception_handle (ctx, exception); +} diff --git a/libgloss/mips/examples/fault_recovery/Makefile b/libgloss/mips/examples/fault_recovery/Makefile new file mode 100644 index 000000000..aa19c09fc --- /dev/null +++ b/libgloss/mips/examples/fault_recovery/Makefile @@ -0,0 +1,50 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Variables that mipshal.mk will expand to compiler/linker arguments must be +# defined before inclusion of mipshal.mk + +include ${MIPS_ELF_ROOT}/share/mips/rules/mipshal.mk + +CFLAGS += -g -O1 +LDFLAGS += -g + +OBJS = fault_recovery.o +APP = fault_recovery.elf + +all: $(APP) + +$(APP): $(OBJS) + $(LD) $(LDFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $^ -o $@ + +.PHONY: clean +clean: + rm -f $(APP) $(OBJS) diff --git a/libgloss/mips/examples/fault_recovery/README.txt b/libgloss/mips/examples/fault_recovery/README.txt new file mode 100644 index 000000000..d4414134e --- /dev/null +++ b/libgloss/mips/examples/fault_recovery/README.txt @@ -0,0 +1,15 @@ +Demonstration of exception handling by triggering a memory fault, recovering +and then continuing. + +Set environment variable MIPS_ELF_ROOT + This should be set to the root of the installation directory for the + toolchain (that is, below the bin directory) + +To build big-endian + # make + +To build little-endian + # make ENDIAN=EL + +To delete temporary and built files + # make clean diff --git a/libgloss/mips/examples/fault_recovery/fault_recovery.c b/libgloss/mips/examples/fault_recovery/fault_recovery.c new file mode 100644 index 000000000..6b509d63a --- /dev/null +++ b/libgloss/mips/examples/fault_recovery/fault_recovery.c @@ -0,0 +1,77 @@ +/* + * Copyright 2014, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +#include <stdio.h> +#include <setjmp.h> +#include <mips/hal.h> + +jmp_buf context; + +void fault_recover (); + +int +main () +{ + int error = setjmp (context); + + if (error) + { + printf ("Error handled %d\n", error); + /* Return success on this return path. */ + return 0; + } + + /* Initial continuation point from setjmp, cause an memory error. */ + int *a = 0; + *a = 0; + + /* Return error on this return path. */ + return -1; +} + +void +_mips_handle_exception (struct gpctx *ctx, int exception) +{ + /* If we're here due to a TLB Store exception, return to the + fault_recover function. Otherwise use the stand exception handler. */ + if (exception == EXC_TLBS) + { + printf ("exception caught\n"); + ctx->epc = (reg_t)&fault_recover; + } + else + __exception_handle (ctx, exception); +} + +void +fault_recover () +{ + longjmp (context, EXC_TLBS); +} diff --git a/libgloss/mips/examples/interrupt_handler/Makefile b/libgloss/mips/examples/interrupt_handler/Makefile new file mode 100644 index 000000000..65b8eece8 --- /dev/null +++ b/libgloss/mips/examples/interrupt_handler/Makefile @@ -0,0 +1,50 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Variables that mipshal.mk will expand to compiler/linker arguments must be +# defined before inclusion of mipshal.mk + +include ${MIPS_ELF_ROOT}/share/mips/rules/mipshal.mk + +CFLAGS += -g -O1 +LDFLAGS += -g + +OBJS = interrupt_handler.o +APP = interrupt_handler.elf + +all: $(APP) + +$(APP): $(OBJS) + $(LD) $(LDFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $^ -o $@ + +.PHONY: clean +clean: + rm -f $(APP) $(OBJS) diff --git a/libgloss/mips/examples/interrupt_handler/README.txt b/libgloss/mips/examples/interrupt_handler/README.txt new file mode 100644 index 000000000..44bbdfea6 --- /dev/null +++ b/libgloss/mips/examples/interrupt_handler/README.txt @@ -0,0 +1,14 @@ +Demonstration of installing an interrupt handler for software interrupts 0. + +Set environment variable MIPS_ELF_ROOT + This should be set to the root of the installation directory for the + toolchain (that is, below the bin directory). + +To build big-endian + # make + +To build little-endian + # make ENDIAN=EL + +To delete temporary and built files + # make clean diff --git a/libgloss/mips/examples/interrupt_handler/interrupt_handler.c b/libgloss/mips/examples/interrupt_handler/interrupt_handler.c new file mode 100644 index 000000000..4cd471c44 --- /dev/null +++ b/libgloss/mips/examples/interrupt_handler/interrupt_handler.c @@ -0,0 +1,72 @@ +/* + * Copyright 2015, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +#include <stdio.h> +#include <unistd.h> +#include <mips/cpu.h> +#include <mips/hal.h> +volatile int handled; + +int +main () +{ + /* Enable SW interrupt 0 */ + mips32_bs_c0 (C0_STATUS, SR_SINT0); + /* Trigger the interrupt */ + mips32_bs_c0 (C0_CAUSE, SR_SINT0); + /* Wait for handling */ + while (!handled) + { + }; + + printf ("SW0 interrupt handled\n"); + + return 1; +} + +/* Handle SW0 */ +void __attribute__ ((interrupt("vector=sw0"))) +_mips_isr_sw0 (void) +{ + /* Prove that semi-hosting still works! */ + write (1, "Hello World\n", 12); + /* Count the interrupt */ + handled += 1; + /* Clear the interrupt */ + mips32_bc_c0 (C0_CAUSE, SR_SINT0); +} + +/* Provide a fall-back handler if anything other than SW0 is raised */ +void __attribute__ ((interrupt, keep_interrupts_masked)) +_mips_interrupt (void) +{ + write (1, "Unhandled interrupt occurred\n", 29); + __exit (2); +} diff --git a/libgloss/mips/examples/isr_vector_space/Makefile b/libgloss/mips/examples/isr_vector_space/Makefile new file mode 100644 index 000000000..dbe804d5a --- /dev/null +++ b/libgloss/mips/examples/isr_vector_space/Makefile @@ -0,0 +1,54 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Variables that mipshal.mk will expand to compiler/linker arguments must be +# defined before inclusion of mipshal.mk + +ISR_VEC_SPACE = 64 +include ${MIPS_ELF_ROOT}/share/mips/rules/mipshal.mk + +CFLAGS += -g -O1 -DISR_VEC_SPACE=$(ISR_VEC_SPACE) +LDFLAGS += -g + +OBJS = isr_vector_space.o excpt_isr.o +APP = isr_vector_space.elf + +all: $(APP) + +$(APP): $(OBJS) + $(LD) $(LDFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $^ -o $@ + +%.o: %.S + $(CC) $(CFLAGS) -c $^ -o $@ + +.PHONY: clean +clean: + rm -f $(APP) $(OBJS) diff --git a/libgloss/mips/examples/isr_vector_space/README.txt b/libgloss/mips/examples/isr_vector_space/README.txt new file mode 100644 index 000000000..1f759a10e --- /dev/null +++ b/libgloss/mips/examples/isr_vector_space/README.txt @@ -0,0 +1,19 @@ +Demonstration of defining custom interrupt vector spacing and overriding the +interrupt vector. This extends the interrupt handler demo to include both +a C and assembly coded handler. + +Set environment variable MIPS_ELF_ROOT + This should be set to the root of the installation directory for the + toolchain (that is, below the bin directory). + +To build with different vector spacing (e.g. 128) + # make ISR_VEC_SPACE=128 + +To build big-endian + # make + +To build little-endian + # make ENDIAN=EL + +To delete temporary and built files + # make clean diff --git a/libgloss/mips/examples/isr_vector_space/excpt_isr.S b/libgloss/mips/examples/isr_vector_space/excpt_isr.S new file mode 100644 index 000000000..bcfadac85 --- /dev/null +++ b/libgloss/mips/examples/isr_vector_space/excpt_isr.S @@ -0,0 +1,111 @@ +/* + * Copyright 2015, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +# Keep each function in a separate named section +#define _FUNCTION_SECTIONS_ + +#include <mips/regdef.h> +#include <mips/asm.h> +#include <mips/cpu.h> + +#define VEC_SPACE ISR_VEC_SPACE + +LEAF(__isr_vec) + .set push + .set noat +AENT(__isr_vec_000) +.weak _mips_isr_sw0 + lw k1, %gp_rel(handledsw0)(gp) + addiu k1, k1, 1 + sw k1, %gp_rel(handledsw0)(gp) + mfc0 k1, C0_CAUSE + ins k1, zero, CR_IP_SHIFT + 0, 1 + mtc0 k1, C0_CAUSE + ehb + eret +.org VEC_SPACE +AENT(__isr_vec_001) +.weak _mips_isr_sw1 + li k0, 1 + LA k1, _mips_isr_sw1 + beqz k1, 1f + jr k1 +.org 2 * VEC_SPACE +AENT(__isr_vec_002) +.weak _mips_isr_hw0 + li k0, 2 + LA k1, _mips_isr_hw0 + beqz k1, 1f + jr k1 +.org 3 * VEC_SPACE +AENT(__isr_vec_003) +.weak _mips_isr_hw1 + li k0, 3 + LA k1, _mips_isr_hw1 + beqz k1, 1f + jr k1 +.org 4 * VEC_SPACE +AENT(__isr_vec_004) +.weak _mips_isr_hw2 + li k0, 4 + LA k1, _mips_isr_hw2 + beqz k1, 1f + jr k1 +.org 5 * VEC_SPACE +AENT(__isr_vec_005) +.weak _mips_isr_hw3 + li k0, 5 + LA k1, _mips_isr_hw3 + beqz k1, 1f + jr k1 +.org 6 * VEC_SPACE +AENT(__isr_vec_006) +.weak _mips_isr_hw4 + li k0, 6 + LA k1, _mips_isr_hw4 + beqz k1, 1f + jr k1 +.org 7 * VEC_SPACE +AENT(__isr_vec_007) +.weak _mips_isr_hw5 + li k0, 7 + LA k1, _mips_isr_hw5 + beqz k1, 1f + jr k1 +.org 8 * VEC_SPACE +AENT(__isr_vec_fallback) +.weak _mips_interrupt + li k0, -1 +1: + LA k1, _mips_interrupt + beqz k1, 1b + jr k1 + .set pop +END(__isr_vec) diff --git a/libgloss/mips/examples/isr_vector_space/isr_vector_space.c b/libgloss/mips/examples/isr_vector_space/isr_vector_space.c new file mode 100644 index 000000000..b6fd05fe3 --- /dev/null +++ b/libgloss/mips/examples/isr_vector_space/isr_vector_space.c @@ -0,0 +1,74 @@ +/* + * Copyright 2015, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +#include <stdio.h> +#include <unistd.h> +#include <mips/cpu.h> +volatile int handledsw0 = 0; +volatile int handledsw1 = 0; +void __exit(int); + +int +main () +{ + /* Enable SW interrupts 0/1 */ + mips32_bs_c0 (C0_STATUS, SR_SINT0 | SR_SINT1); + /* Trigger the interrupt */ + mips32_bs_c0 (C0_CAUSE, SR_SINT0); + mips32_bs_c0 (C0_CAUSE, SR_SINT1); + /* Wait for handling */ + while (!handledsw0 || !handledsw1) + { + }; + + printf ("SW0 and SW1 interrupts handled\n"); + + return 1; +} + +/* Handle SW1 */ +void __attribute__ ((interrupt("vector=sw1"))) +_mips_isr_sw1 (void) +{ + /* Prove that semi-hosting still works! */ + write (1, "Hello World\n", 12); + /* Count the interrupt */ + handledsw1 += 1; + /* Clear the interrupt */ + mips32_bc_c0 (C0_CAUSE, SR_SINT1); +} + +/* Provide a fall-back handler if anything other than SW0/SW1 is raised */ +void __attribute__ ((interrupt, keep_interrupts_masked)) +_mips_interrupt (void) +{ + write (1, "Generic interrupt trapped.\n", 27); + __exit(2); +} diff --git a/libgloss/mips/examples/romable/Makefile b/libgloss/mips/examples/romable/Makefile new file mode 100644 index 000000000..93c5fedbd --- /dev/null +++ b/libgloss/mips/examples/romable/Makefile @@ -0,0 +1,61 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Variables that mipshal.mk will expand to compiler/linker arguments must be +# defined before inclusion of mipshal.mk + +# Indicate the application should start via the reset vector +ROMABLE = 1 +# Default to o32 ABI +ABI ?= 32 + +# Set up a 1:1 mapping for physical to virtual addresses +ifeq ($(ABI), 64) +APP_START = 0xFFFFFFFF80000000 +else +APP_START = 0x80000000 +endif + +include ${MIPS_ELF_ROOT}/share/mips/rules/mipshal.mk + +CFLAGS += -g -O1 +LDFLAGS += -g +OBJS = romable.o +APP = romable.elf + +all: $(APP) + +$(APP): $(OBJS) + $(LD) $(LDFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $^ -o $@ + +.PHONY: clean +clean: + rm -f $(APP) $(OBJS) diff --git a/libgloss/mips/examples/romable/README.txt b/libgloss/mips/examples/romable/README.txt new file mode 100644 index 000000000..c446b24c4 --- /dev/null +++ b/libgloss/mips/examples/romable/README.txt @@ -0,0 +1,15 @@ +Demonstration of a bootable example suitable for placing in ROM. This includes +copying application code to RAM during boot. + +Set environment variable MIPS_ELF_ROOT + This should be set to the root of the installation directory for the + toolchain (that is, below the bin directory) + +To build big-endian + # make + +To build little-endian + # make ENDIAN=EL + +To delete temporary and built files + # make clean diff --git a/libgloss/mips/examples/romable/romable.c b/libgloss/mips/examples/romable/romable.c new file mode 100644 index 000000000..5ca9621b5 --- /dev/null +++ b/libgloss/mips/examples/romable/romable.c @@ -0,0 +1,38 @@ +/* + * Copyright 2015, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +#include <stdio.h> + +int +main () +{ + printf ("Hello world!\n"); + return 0; +} diff --git a/libgloss/mips/examples/romable_minimal/Makefile b/libgloss/mips/examples/romable_minimal/Makefile new file mode 100644 index 000000000..efbc63c38 --- /dev/null +++ b/libgloss/mips/examples/romable_minimal/Makefile @@ -0,0 +1,67 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Variables that mipshal.mk will expand to compiler/linker arguments must be +# defined before inclusion of mipshal.mk + +# Indicate the application should start via the reset vector +ROMABLE = 1 +# Default to o32 ABI +ABI ?= 32 + +# Set up a 1:1 mapping for physical to virtual addresses +ifeq ($(ABI), 64) + APP_START = 0xFFFFFFFF80000000 + LDSCRIPT = minimal64_64.ld +else + APP_START = 0x80000000 + ifeq ($(ABI), n32) + LDSCRIPT = minimal64_n32.ld + else + LDSCRIPT = minimal32.ld + endif +endif + +include ${MIPS_ELF_ROOT}/share/mips/rules/mipshal.mk + +CFLAGS += -g -O1 +LDFLAGS += -g +OBJS = romable_minimal.o +APP = romable_minimal.elf + +all: $(APP) + +$(APP): $(OBJS) + $(LD) $(LDFLAGS) $(OBJS) -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +.PHONY: clean +clean: + rm -f $(APP) $(OBJS) diff --git a/libgloss/mips/examples/romable_minimal/README.txt b/libgloss/mips/examples/romable_minimal/README.txt new file mode 100644 index 000000000..bfab210f2 --- /dev/null +++ b/libgloss/mips/examples/romable_minimal/README.txt @@ -0,0 +1,32 @@ +Demonstration of a bootable example without some of the support code normally +needed for debug, hosted or semi-hosted environments. + +The removed features are: +* No support for UHI argument handling operations (i.e. a0 == -1) +* No support for returning to a boot monitor or using a boot monitor's + UHI support +* Quiet unhandled exceptions, no output is produced but instead the UHI + exception operation is raised and a debugger (or application) can + respond to it + +These features are removed by modifying the the linker scripts which are +derived from uhi*.ld linker scripts, using makescripts.sh. + +Set environment variable MIPS_ELF_ROOT + This should be set to the root of the installation directory for the + toolchain (that is, below the bin directory). + +First prepare the linker scripts by running makescripts.sh + # ./makescripts.sh + +To build 32-bit big-endian + # make + +To build 32-bit little-endian + # make ENDIAN=EL + +To build 64-bit + # make ABI=64 + +To delete temporary and built files + # make clean diff --git a/libgloss/mips/examples/romable_minimal/makescripts.sh b/libgloss/mips/examples/romable_minimal/makescripts.sh new file mode 100644 index 000000000..99883d9a3 --- /dev/null +++ b/libgloss/mips/examples/romable_minimal/makescripts.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +if [ "x$MIPS_ELF_ROOT" = "x" ]; then + echo "Error: MIPS_ELF_ROOT is not set" + exit 1 +fi +if [ "x$MIPS_TOOLCHAIN" = "x" ]; then + echo "Error: MIPS_TOOLCHAIN is not set (i.e. mips-img-elf)" + exit 1 +fi +COMPILER=${MIPS_ELF_ROOT}/bin/${MIPS_TOOLCHAIN}-gcc + +uhi32=`${COMPILER} --print-file-name=uhi32.ld` +uhi64_64=`${COMPILER} --print-file-name=uhi64_64.ld` +uhi64_n32=`${COMPILER} --print-file-name=uhi64_n32.ld` + +cp $uhi32 minimal32.ld +cp $uhi64_64 minimal64_64.ld +cp $uhi64_n32 minimal64_n32.ld + +for i in minimal32.ld minimal64_64.ld minimal64_n32.ld; do + sed -i -e "s|\(EXTERN *(__getargs).*\)|/* \1 */|" $i + sed -i -e "s|\(EXTERN *(__exception_handle_verbose).*\)|"\ +"/* \1 */\n"\ +"/* Use the quiet exception handler for reduced code size */\n"\ +"EXTERN (__exception_handle_quiet)|" $i + sed -i -e "s|\(EXTERN *(__register_excpt_boot).*\)|"\ +"/* \1 */|" $i + sed -i -e "s|PROVIDE *(__use_excpt_boot = 1)|"\ +"PROVIDE (__use_excpt_boot = 0)|" $i +done diff --git a/libgloss/mips/examples/romable_minimal/romable_minimal.c b/libgloss/mips/examples/romable_minimal/romable_minimal.c new file mode 100644 index 000000000..a9ee9d704 --- /dev/null +++ b/libgloss/mips/examples/romable_minimal/romable_minimal.c @@ -0,0 +1,39 @@ +/* + * Copyright 2015, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +#include <stdio.h> +#include <unistd.h> + +int +main () +{ + write (1, "minimal\n", 8); + return 0; +} diff --git a/libgloss/mips/examples/romable_predef/Makefile b/libgloss/mips/examples/romable_predef/Makefile new file mode 100644 index 000000000..f32a34b11 --- /dev/null +++ b/libgloss/mips/examples/romable_predef/Makefile @@ -0,0 +1,98 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Variables that mipshal.mk will expand to compiler/linker arguments must be +# defined before inclusion of mipshal.mk + +# Indicate the application should start via the reset vector +ROMABLE = 1 + +ifeq ($(CORE), P5600) + DEFINES = -DC0_CONFIG0_VALUE=0xB6040483 + DEFINES += -DC0_CONFIG1_VALUE=0xFEA3519B + DEFINES += -DC0_CONFIG2_VALUE=0x80000000 + DEFINES += -DC0_CONFIG3_VALUE=0xBF8032A8 + DEFINES += -DC0_CONFIG4_VALUE=0xc01c0000 + DEFINES += -DC0_CONFIG5_VALUE=0x10000038 + DEFINES += -DC0_WATCHHI_VALUE=0x00000000 + OBJS += init_l23caches_predef.o + override ABI = 32 + override ENDIAN = EL + override MIPS_TOOLCHAIN = mips-mti-elf + APP_START = 0x80000000 +else + ifeq ($(CORE), I6400) + DEFINES = -DC0_CONFIG0_VALUE=0x80004A05 + DEFINES += -DC0_CONFIG1_VALUE=0x9EAB559B + DEFINES += -DC0_CONFIG2_VALUE=0x80000000 + DEFINES += -DC0_CONFIG3_VALUE=0xFC8031E9 + DEFINES += -DC0_CONFIG4_VALUE=0xD0FC0227 + DEFINES += -DC0_CONFIG5_VALUE=0x00000098 + DEFINES += -DC0_WATCHHI_VALUE=0x80000000 + DEFINES += -DC0_WATCHHI1_VALUE=0x80000000 + DEFINES += -DC0_WATCHHI2_VALUE=0x80000000 + DEFINES += -DC0_WATCHHI3_VALUE=0x00000000 + DEFINES += -DC0_CMGCRBASE_VALUE=0x01fbf800 + DEFINES += -DGCR_L2_CONFIG_VALUE=0x00000000 + OBJS = init_cm3l2_predef.o + override ABI = 64 + override ENDIAN = EL + override MIPS_TOOLCHAIN = mips-img-elf + APP_START = 0xFFFFFFFF80000000 + else + $(error Please specify a core using CORE=(P5600|I6400)) + endif +endif + +include ${MIPS_ELF_ROOT}/share/mips/rules/mipshal.mk + +ifeq ($(ENABLE_XPA), 1) + DEFINES += -DENABLE_XPA=1 +endif + +OBJS += romable_predef.o reset_predef.o init_caches_predef.o init_cp0_predef.o +OBJS += init_tlb_predef.o corecheck_predef.o +APP = romable_predef.elf + +CFLAGS += -g -O1 $(DEFINES) +LDFLAGS += -g + +all: $(APP) + +$(APP): $(OBJS) + $(LD) $(LDFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $^ -o $@ + +%.o: $(MIPS_ELF_ROOT)/share/mips/boot/%.S + $(CC) $(CFLAGS) -c $^ -o $@ + +.PHONY: clean +clean: + rm -f $(APP) $(OBJS) diff --git a/libgloss/mips/examples/romable_predef/README.txt b/libgloss/mips/examples/romable_predef/README.txt new file mode 100644 index 000000000..d86ac9030 --- /dev/null +++ b/libgloss/mips/examples/romable_predef/README.txt @@ -0,0 +1,16 @@ +Demonstration of a bootable example built for a specific cpu core, either the +P5600 or I6400, relying on compile time CONFIG_XXX values for compile time +specialization. + +Set environment variable MIPS_ELF_ROOT + This should be set to the root of the installation directory for the + toolchain (that is, below the bin directory) + +To build for the P5600 + # make CORE=P5600 + +To build for the I6400 + # make CORE=I6400 + +To delete temporary and built files + # make clean CORE=P5600 diff --git a/libgloss/mips/examples/romable_predef/romable_predef.c b/libgloss/mips/examples/romable_predef/romable_predef.c new file mode 100644 index 000000000..5ca9621b5 --- /dev/null +++ b/libgloss/mips/examples/romable_predef/romable_predef.c @@ -0,0 +1,38 @@ +/* + * Copyright 2015, Imagination Technologies Limited and/or its + * affiliated group companies. + * All rights reserved. + * + * 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. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +*/ + +#include <stdio.h> + +int +main () +{ + printf ("Hello world!\n"); + return 0; +} diff --git a/libgloss/mips/rules/mipshal.mk b/libgloss/mips/rules/mipshal.mk new file mode 100644 index 000000000..7d7f09991 --- /dev/null +++ b/libgloss/mips/rules/mipshal.mk @@ -0,0 +1,135 @@ +# Copyright 2015, Imagination Technologies Limited and/or its +# affiliated group companies. +# All rights reserved. +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. + +# Usage: +# +# "include mipshal.mk" should be included in the top level makefile of the +# project seeking to use this file. The variables CC and LD will be defined +# by this file. The variables CFLAGS and LDFLAGS will be extended by this +# file. +# +# Symbols that are to be overwridden should be defined in the the makefile +# before "include mipshal.mk" or passed to make on the command line in the +# form 'make STACK=0x1000' or 'make FLASH_START=0xBFC00000'. +# +# Care should be taken to ensure that no pre-existing environment variables +# collide with the set of variables that this file inspects as unintended +# results may happen. + +# Portable 'lowercase' func. +lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1)))))))))))))))))))))))))) + +# Default values for the linker script symbols listed below are +# defined in the linker script. + +# These are linker script symbols that are prefixed with '__" +priv_symbols = MEMORY_BASE MEMORY_SIZE STACK +priv_symbols += BEV_OVERRIDE +priv_symbols += ENABLE_XPA +priv_symbols += FLUSH_TO_ZERO +priv_symbols += FLASH_START APP_START FLASH_APP_START +priv_symbols += ISR_VEC_SPACE ISR_VECTOR_COUNT + +comma := , + +# A bit of makefile magic: +# foreach symbol in overridable ld-symbols : +# If symbol has a value, produce a linker argument for that symbol. +MIPS_HAL_LDFLAGS = $(foreach a,$(priv_symbols),$(if $($a),-Wl$(comma)--defsym$(comma)__$(call lc,$(a))=$($a))) + +# Linker scripts for 32 & 64bit and ABI flags. +LDSCRIPT32 ?= uhi32.ld +LDSCRIPT64 ?= uhi64_64.ld +LDSCRIPTN32 ?= uhi64_n32.ld + +ifdef ENDIAN + ifeq ($(ENDIAN), EL) + ENDIANF = -EL + else + ifeq ($(ENDIAN), EB) + ENDIANF = -EB + else + $(error Endian must be either EL, EB or undefined for default) + endif + endif +endif + +# Pick the appropiate flags based on $(ABI) +ifeq ($(ABI), 32) + MIPS_TOOLCHAIN ?= mips-mti-elf + LDSCRIPT ?= $(LDSCRIPT32) +else + ifeq ($(ABI), 64) + MIPS_TOOLCHAIN ?= mips-img-elf + LDSCRIPT ?= $(LDSCRIPT64) + else + ifeq ($(ABI), n32) + MIPS_TOOLCHAIN ?= mips-img-elf + LDSCRIPT ?= $(LDSCRIPTN32) + else + $(error ABI must one of 32,64,n32) + endif + endif +endif + +ifeq ($(ROMABLE),1) + MIPS_HAL_LDFLAGS += -T bootcode.ld +endif + +ifdef ELF_ENTRY + MIPS_HAL_LDFLAGS += -Wl,--entry,$(ELF_ENTRY) +endif + +ifndef MIPS_ELF_ROOT + $(error MIPS_ELF_ROOT must be set to point to toolkit installation root) +endif + +ifeq ($(OS),Windows_NT) + CROSS_COMPILE=$(subst \,/,$(MIPS_ELF_ROOT))/bin/$(MIPS_TOOLCHAIN)- + SREC2HEX=$(subst \,/,$(MIPS_ELF_ROOT))/share/mips/rules/srec2hex.pl +else + CROSS_COMPILE=$(MIPS_ELF_ROOT)/bin/$(MIPS_TOOLCHAIN)- + SREC2HEX=$(MIPS_ELF_ROOT)/share/mips/rules/srec2hex.pl +endif + +CC = $(CROSS_COMPILE)gcc +CFLAGS += -mabi=$(ABI) $(ENDIANF) +LD = $(CC) +LDFLAGS += $(MIPS_HAL_LDFLAGS) -mabi=$(ABI) $(ENDIANF) -T $(LDSCRIPT) + +OBJDUMP = $(CROSS_COMPILE)objdump +OBJCOPY = $(CROSS_COMPILE)objcopy +READELF = $(CROSS_COMPILE)readelf +NM = $(CROSS_COMPILE)nm +STRIP = $(CROSS_COMPILE)strip + +%.rec: %.elf + $(OBJCOPY) -O srec $^ $@ + +%.hex: %.rec + $(SREC2HEX) -EL $^ > $@ diff --git a/libgloss/mips/rules/srec2hex.pl b/libgloss/mips/rules/srec2hex.pl new file mode 100755 index 000000000..59ce1bf09 --- /dev/null +++ b/libgloss/mips/rules/srec2hex.pl @@ -0,0 +1,196 @@ +#!/bin/env perl + +# Copyright (C) 2018 MIPS Tech, LLC +# +# 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. +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +# +# File: srec2hex.pl +# +# Description: +# This script converts an S-record file to a MIPS HEX file useful for +# some simulation environments +# +# Usage: +# srec2hex.pl -EL|-EB inputfile [outputfile] +# + +use File::Basename; +use Cwd; + +# Init global variables +$Endian = ""; +$Debug = 0; +$Verbose = 0; +$IFile = ""; +$IFH = "STDIN"; +$OFile = ""; +$OFH = "STDOUT"; + +# Parse command line for switches +$Errors = 0; +while (@ARGV && ($ARGV[0] =~ /^-./)) # Check for switches +{ + $ARGV[0] =~ /^-help$/ && ($Errors++, shift(@ARGV), next); + $ARGV[0] =~ /^-verbose$/ && ($Verbose = 1, shift(@ARGV), next); + $ARGV[0] =~ /^-EL$/ && ($Endian = $ARGV[0], shift(@ARGV), next); + $ARGV[0] =~ /^-EB$/ && ($Endian = $ARGV[0], shift(@ARGV), next); + print STDERR "\n$0: Unknown qualifier: $ARGV[0]\n"; + $Errors++; + last; +} + +$Errors++ if ($Endian eq ""); +if ($#ARGV > 1) { + $Errors++; +} else { + $IFile = $ARGV[0] if ($#ARGV >= 0); + $OFile = $ARGV[1] if ($#ARGV == 1); + if ($IFile ne "") { + if (! -f $IFile) { + print STDERR "\n$0: ERROR: Input file $IFile does not exist\n"; + $Errors++; + } else { + if (! open (IFILE, "< $IFile")) { + printf "\n$0: ERROR: Failed to open input file $IFile - $!\n"; + $Errors++; + } + } + $IFH = "IFILE"; + } + if ($OFile ne "") { + if (! open (OFILE, "> $OFile")) { + printf "\n$0: ERROR: Failed to open output file $OFile - $!\n"; + close(IFILE); + $Errors++; + } + $OFH = "OFILE"; + } +} + +if ($Errors > 0) { + $usage = +"Usage: $0 -E{B|L} [qualifiers] [input-file] [[output-file]] + -EB Convert big-endian S-Record file + -EL Convert little-endian S-Record file + -help Output this message and exit + -verbose Emit verbose messages +"; + printf STDERR $usage; + exit(1); +} + +printf $OFH "\# Endian %s\n", ($Endian eq "-EB") ? "Big" : "Little"; + +my $word = ""; +my $start; +my $incr; + +if ($Endian eq "-EL") { + $start = 6; + $incr = -2; + $word = "00000000"; +} else { + $start = 0; + $incr = 2; +} +my $byteno = $start; +my $tofill = 4; +my $waddr = ""; +my $naddr = ""; + +while (<$IFH>) { + my $line = $_; + my $dbindex; + my $addr; + my $datablock; + chomp $line; + + last if $line =~ /^S[7-9]/ ; + next if $line =~ /^S0/ || $line =~ /^S[4-6]/ ; + + my $reclen = hex(substr $line, 2, 2); + + if ( $line =~ /^S1/ ) { + $addr = hex(substr $line, 4, 4); + $datablock = substr $line, 8; + $reclen -= 2; + } elsif ( $line =~ /^S2/ ) { + $addr = hex(substr $line, 4, 6); + $datablock = substr $line, 10; + $reclen -= 3; + } elsif ( $line =~ /^S3/ ) { + $addr = hex(substr $line, 4, 8); + $datablock = substr $line, 12; + $reclen -= 4; + } else { + printf STDERR "Bad s-record found: %s\n", $line; + exit(1); + } + $dbindex = 0; + # snip checksum byte + $reclen -= 1; + + unless ($addr == $naddr) { + if ($tofill != 4) { + while ($tofill--) { + substr($word, $byteno, 2) = "00"; + $byteno += $incr; + } + $word =~ tr/A-Z/a-z/; + printf $OFH "%x %s\n", $waddr / 4, $word; + $tofill = 4; + $byteno = $start; + $waddr = $waddr+4; + } + if (($addr & 0x3) != 0) { + $naddr = $addr & ~0x3; + $byteno = $start; + $tofill = 4; + while($naddr != $addr) { + substr($word, $byteno, 2) = "00"; + $byteno += $incr; + $naddr += 1; + $tofill -=1; + } + } + } + $naddr = $addr; + $waddr = $naddr; + while($reclen > 0) { + substr ($word, $byteno, 2) = substr ($datablock, $dbindex, 2); + $byteno += $incr; + $dbindex += 2; + $tofill -=1; + if ($tofill == 0) { + $word =~ tr/A-Z/a-z/; + printf $OFH "%x %s\n", $waddr / 4, $word; + $byteno = $start; + $tofill = 4; + $waddr = $waddr+4; + } + $reclen = $reclen - 1; + $naddr = $naddr + 1; + } +} -- 2.25.1