initial changes

This commit is contained in:
allamaaki80515
2025-11-18 11:19:21 +01:00
parent c90753aa23
commit 1e1ce6eb15
158 changed files with 21703 additions and 2 deletions
@@ -0,0 +1,98 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2017,2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <errno.h>
#include "sys/alt_alarm.h"
#include "sys/alt_irq.h"
/*
* alt_alarm_start is called to register an alarm with the system. The
* "alarm" structure passed as an input argument does not need to be
* initialised by the user. This is done within this function.
*
* The remaining input arguments are:
*
* nticks - The time to elapse until the alarm executes. This is specified in
* system clock ticks.
* callback - The function to run when the indicated time has elapsed.
* context - An opaque value, passed to the callback function.
*
* Care should be taken when defining the callback function since it is
* likely to execute in interrupt context. In particular, this mean that
* library calls like printf() should not be made, since they can result in
* deadlock.
*
* The interval to be used for the next callback is the return
* value from the callback function. A return value of zero indicates that the
* alarm should be unregistered.
*
* alt_alarm_start() will fail if the timer facility has not been enabled
* (i.e. there is no system clock). Failure is indicated by a negative return
* value.
*/
int alt_alarm_start (alt_alarm* alarm, alt_u32 nticks,
alt_u32 (*callback) (void* context),
void* context)
{
alt_irq_context irq_context;
alt_u64 current_nticks = 0;
if (alt_ticks_per_second ())
{
if (alarm)
{
alarm->callback = callback;
alarm->context = context;
irq_context = alt_irq_disable_all ();
current_nticks = alt_nticks();
alarm->time = (alt_u64)nticks + current_nticks + 1;
alt_llist_insert (&alt_alarm_list, &alarm->llist);
alt_irq_enable_all (irq_context);
return 0;
}
else
{
return -EINVAL;
}
}
else
{
return -ENOTSUP;
}
}
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2003-2004 Altera Corporation, San Jose, California, USA.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* ------------
*
* Altera does not recommend, suggest or require that this reference design
* file be used in conjunction or combination with any other product.
*
* alt_busy_sleep.c - Microsecond delay routine which uses a calibrated busy
* loop to perform the delay. This is used to implement
* usleep for both uC/OS-II and the standalone HAL.
*
* Author PRR
*
* Calibrated delay with no timer required
*
* The ASM instructions in the routine are equivalent to
*
* for (i=0;i<us*(ALT_CPU_FREQ/3);i++);
*
* and takes three cycles each time around the loop
*
*/
#include <limits.h>
#include <string.h>
#include "system.h"
#include "alt_types.h"
#include "priv/alt_busy_sleep.h"
unsigned int alt_busy_sleep (unsigned int us)
{
/*
* Only delay if ALT_SIM_OPTIMIZE is not defined; i.e., if software
* is built targetting ModelSim RTL simulation, the delay will be
* skipped to speed up simulation.
*/
#ifndef ALT_SIM_OPTIMIZE
int i;
int big_loops;
alt_u32 cycles_per_loop;
if (!strcmp(NIOS2_CPU_IMPLEMENTATION,"tiny"))
{
cycles_per_loop = 9;
}
else
{
cycles_per_loop = 3;
}
big_loops = us / (INT_MAX/
(ALT_CPU_FREQ/(cycles_per_loop * 1000000)));
if (big_loops)
{
for(i=0;i<big_loops;i++)
{
/*
* Do NOT Try to single step the asm statement below
* (single step will never return)
* Step out of this function or set a breakpoint after the asm statements
*/
__asm__ volatile (
"\n0:"
"\n\taddi %0,%0, -1"
"\n\tbne %0,zero,0b"
"\n1:"
"\n\t.pushsection .debug_alt_sim_info"
"\n\t.int 4, 0, 0b, 1b"
"\n\t.popsection"
:: "r" (INT_MAX));
us -= (INT_MAX/(ALT_CPU_FREQ/
(cycles_per_loop * 1000000)));
}
/*
* Do NOT Try to single step the asm statement below
* (single step will never return)
* Step out of this function or set a breakpoint after the asm statements
*/
__asm__ volatile (
"\n0:"
"\n\taddi %0,%0, -1"
"\n\tbne %0,zero,0b"
"\n1:"
"\n\t.pushsection .debug_alt_sim_info"
"\n\t.int 4, 0, 0b, 1b"
"\n\t.popsection"
:: "r" (us*(ALT_CPU_FREQ/(cycles_per_loop * 1000000))));
}
else
{
/*
* Do NOT Try to single step the asm statement below
* (single step will never return)
* Step out of this function or set a breakpoint after the asm statements
*/
__asm__ volatile (
"\n0:"
"\n\taddi %0,%0, -1"
"\n\tbgt %0,zero,0b"
"\n1:"
"\n\t.pushsection .debug_alt_sim_info"
"\n\t.int 4, 0, 0b, 1b"
"\n\t.popsection"
:: "r" (us*(ALT_CPU_FREQ/(cycles_per_loop * 1000000))));
}
#endif /* #ifndef ALT_SIM_OPTIMIZE */
return 0;
}
@@ -0,0 +1,103 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <unistd.h>
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "priv/alt_file.h"
#include "os/alt_syscall.h"
#ifdef ALT_USE_DIRECT_DRIVERS
int ALT_CLOSE (int fildes)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(close);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
#else /* !ALT_USE_DIRECT_DRIVERS */
/*
* close() is called by an application to release a file descriptor. If the
* associated file system/device has a close() callback function registered
* then this called. The file descriptor is then marked as free.
*
* ALT_CLOSE is mapped onto the close() system call in alt_syscall.h
*/
int ALT_CLOSE (int fildes)
{
alt_fd* fd;
int rval;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (fildes < 0) ? NULL : &alt_fd_list[fildes];
if (fd)
{
/*
* If the associated file system/device has a close function, call it so
* that any necessary cleanup code can run.
*/
rval = (fd->dev->close) ? fd->dev->close(fd) : 0;
/* Free the file descriptor structure and return. */
alt_release_fd (fildes);
if (rval < 0)
{
ALT_ERRNO = -rval;
return -1;
}
return 0;
}
else
{
ALT_ERRNO = EBADFD;
return -1;
}
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,70 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "nios2.h"
#include "system.h"
#include "alt_types.h"
#include "sys/alt_cache.h"
#define ALT_FLUSH_DATA(i) __asm__ volatile ("flushda (%0)" :: "r" (i));
/*
* alt_dcache_flush() is called to flush the data cache for a memory
* region of length "len" bytes, starting at address "start".
*
* Any dirty lines in the data cache are written back to memory.
*/
void alt_dcache_flush (void* start, alt_u32 len)
{
#if NIOS2_DCACHE_SIZE > 0
char* i;
char* end = ((char*) start) + len;
for (i = start; i < end; i+= NIOS2_DCACHE_LINE_SIZE)
{
ALT_FLUSH_DATA(i);
}
/*
* For an unaligned flush request, we've got one more line left.
* Note that this is dependent on NIOS2_DCACHE_LINE_SIZE to be a
* multiple of 2 (which it always is).
*/
if (((alt_u32) start) & (NIOS2_DCACHE_LINE_SIZE - 1))
{
ALT_FLUSH_DATA(i);
}
#endif /* NIOS2_DCACHE_SIZE > 0 */
}
@@ -0,0 +1,51 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "nios2.h"
#include "system.h"
#include "alt_types.h"
#include "sys/alt_cache.h"
/*
* alt_dcache_flush_all() is called to flush the entire data cache.
*/
void alt_dcache_flush_all (void)
{
#if NIOS2_DCACHE_SIZE > 0
char* i;
for (i = (char*) 0; i < (char*) NIOS2_DCACHE_SIZE; i+= NIOS2_DCACHE_LINE_SIZE)
{
__asm__ volatile ("flushd (%0)" :: "r" (i));
}
#endif /* NIOS2_DCACHE_SIZE > 0 */
}
@@ -0,0 +1,69 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2007 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "nios2.h"
#include "system.h"
#include "alt_types.h"
#include "sys/alt_cache.h"
#define ALT_FLUSH_DATA_NO_WRITEBACK(i) \
__asm__ volatile ("initda (%0)" :: "r" (i));
/*
* alt_dcache_flush_no_writeback() is called to flush the data cache for a
* memory region of length "len" bytes, starting at address "start".
*
* Any dirty lines in the data cache are NOT written back to memory.
* Make sure you really want this behavior. If you aren't 100% sure,
* use the alt_dcache_flush() routine instead.
*/
void alt_dcache_flush_no_writeback (void* start, alt_u32 len)
{
char* i;
char* end = ((char*) start) + len;
for (i = start; i < end; i+= NIOS2_DCACHE_LINE_SIZE)
{
ALT_FLUSH_DATA_NO_WRITEBACK(i);
}
/*
* For an unaligned flush request, we've got one more line left.
* Note that this is dependent on NIOS2_DCACHE_LINE_SIZE to be a
* multiple of 2 (which it always is).
*/
if (((alt_u32) start) & (NIOS2_DCACHE_LINE_SIZE - 1))
{
ALT_FLUSH_DATA_NO_WRITEBACK(i);
}
}
@@ -0,0 +1,149 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "sys/alt_dev.h"
#include "priv/alt_file.h"
#include "alt_types.h"
#include "system.h"
/*
* This file contains the data constructs used to control access to device and
* filesytems.
*/
/*
* "alt_fs_list" is the head of a linked list of registered filesystems. It is
* initialised as an empty list. New entries can be added using the
* alt_fs_reg() function.
*/
ALT_LLIST_HEAD(alt_fs_list);
/*
* "alt_dev_list" is the head of a linked list of registered devices. It is
* configured at startup to include a single device, "alt_dev_null". This
* device is discussed below.
*/
extern alt_dev alt_dev_null; /* forward declaration */
alt_llist alt_dev_list = {&alt_dev_null.llist, &alt_dev_null.llist};
/*
* alt_dev_null_write() is the implementation of the write() function used
* by the alt_dev_null device. It simple discards all data passed to it, and
* indicates that the data has been successfully transmitted.
*/
static int alt_dev_null_write (alt_fd* fd, const char* ptr, int len)
{
return len;
}
/*
* "alt_dev_null" is used to allow output to be redirected to nowhere. It is
* the only device registered before the call to alt_sys_init(). At startup
* stin, stdout & stderr are all directed towards this device so that library
* calls like printf() will be safe but inefectual.
*/
alt_dev alt_dev_null = {
{
&alt_dev_list,
&alt_dev_list
},
"/dev/null",
NULL, /* open */
NULL, /* close */
NULL, /* write */
alt_dev_null_write, /* write */
NULL, /* lseek */
NULL, /* fstat */
NULL /* ioctl */
};
/*
* "alt_fd_list_lock" is a semaphore used to control access to the file
* descriptor list. This is used to ensure that access to the list is thread
* safe.
*/
ALT_SEM(alt_fd_list_lock)
/*
* "alt_max_fd" is used to make access to the file descriptor list more
* efficent. It is set to be the value of the highest allocated file
* descriptor. This saves having to search the entire pool of unallocated
* file descriptors when looking for a match.
*/
alt_32 alt_max_fd = -1;
/*
* "alt_fd_list" is the file descriptor pool. The first three entries in the
* array are configured as standard in, standard out, and standard error. These
* are all initialised so that accesses are directed to the alt_dev_null
* device. The remaining file descriptors are initialised as unallocated.
*
* The maximum number of file descriptors within the system is specified by the
* user defined macro "ALT_MAX_FD". This is defined in "system.h", which is
* auto-genereated using the projects PTF and STF files.
*/
alt_fd alt_fd_list[ALT_MAX_FD] =
{
{
&alt_dev_null, /* standard in */
0,
0
},
{
&alt_dev_null, /* standard out */
0,
0
},
{
&alt_dev_null, /* standard error */
0,
0
}
/* all other elements are set to zero */
};
@@ -0,0 +1,59 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "priv/alt_dev_llist.h"
#include "sys/alt_errno.h"
/*
*
*/
int alt_dev_llist_insert (alt_dev_llist* dev, alt_llist* list)
{
/*
* check that the device exists, and that it has a valid name.
*/
if (!dev || !dev->name)
{
ALT_ERRNO = EINVAL;
return -EINVAL;
}
/*
* register the device.
*/
alt_llist_insert(list, &dev->llist);
return 0;
}
@@ -0,0 +1,63 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_dma.h"
#include "sys/alt_errno.h"
#include "priv/alt_file.h"
/*
* The list of registered DMA receive channels.
*/
ALT_LLIST_HEAD(alt_dma_rxchan_list);
/*
* alt_dma_txchan_open() is used to obtain an "alt_dma_txchan" descriptor for
* a DMA transmit device. The name is the name of the associated physical
* device (e.g. "/dev/dma_0").
*
* The return value will be NULL on failure, and non-NULL otherwise.
*/
alt_dma_rxchan alt_dma_rxchan_open (const char* name)
{
alt_dma_rxchan dev;
dev = (alt_dma_rxchan) alt_find_dev (name, &alt_dma_rxchan_list);
if (!dev)
{
ALT_ERRNO = ENODEV;
}
return dev;
}
@@ -0,0 +1,63 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_dma.h"
#include "sys/alt_errno.h"
#include "priv/alt_file.h"
/*
* The list of registered receive channels.
*/
ALT_LLIST_HEAD(alt_dma_txchan_list);
/*
* alt_dma_txchan_open() is used to obtain an "alt_dma_txchan" descriptor for
* a DMA transmit device. The name is the name of the associated physical
* device (e.g. "/dev/dma_0").
*
* The return value will be NULL on failure, and non-NULL otherwise.
*/
alt_dma_txchan alt_dma_txchan_open (const char* name)
{
alt_dma_txchan dev;
dev = (alt_dma_txchan) alt_find_dev (name, &alt_dma_txchan_list);
if (!dev)
{
ALT_ERRNO = ENODEV;
}
return dev;
}
@@ -0,0 +1,64 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
/******************************************************************************
* *
* THIS IS A LIBRARY READ-ONLY SOURCE FILE. DO NOT EDIT IT DIRECTLY. *
* *
* Overriding HAL Functions *
* *
* To provide your own implementation of a HAL function, include the file in *
* your Nios II IDE application project. When building the executable, the *
* Nios II IDE finds your function first, and uses it in place of the HAL *
* version. *
* *
******************************************************************************/
/*
*
*/
typedef void (*constructor) (void);
extern constructor __CTOR_LIST__[];
extern constructor __CTOR_END__[];
/*
* Run the C++ static constructors.
*/
void _do_ctors(void)
{
constructor* ctor;
for (ctor = &__CTOR_END__[-1]; ctor >= __CTOR_LIST__; ctor--)
(*ctor) ();
}
@@ -0,0 +1,64 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
/******************************************************************************
* *
* THIS IS A LIBRARY READ-ONLY SOURCE FILE. DO NOT EDIT IT DIRECTLY. *
* *
* Overriding HAL Functions *
* *
* To provide your own implementation of a HAL function, include the file in *
* your Nios II IDE application project. When building the executable, the *
* Nios II IDE finds your function first, and uses it in place of the HAL *
* version. *
* *
******************************************************************************/
/*
*
*/
typedef void (*destructor) (void);
extern destructor __DTOR_LIST__[];
extern destructor __DTOR_END__[];
/*
* Run the C++ static destructors.
*/
void _do_dtors(void)
{
destructor* dtor;
for (dtor = &__DTOR_END__[-1]; dtor >= __DTOR_LIST__; dtor--)
(*dtor) ();
}
@@ -0,0 +1,102 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2013 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
/*
* This is the code called at the beginning of the exception handler
* to detect a likely fatal ECC error exception and then jump to
* user-provided code to handle it.
*
* This code is pulled in from a .globl in alt_ecc_fatal_exception.c.
* This scheme is used so that if a handler is never registered, then this
* code will not appear in the generated executable, thereby improving
* code footprint.
*
* This code is located in its own section that the linker script
* explicitly mentions and ensures it gets linked at the beginning
* of the exception handler.
*/
/*
* Pull in the exception handler register save code.
*/
.globl alt_exception
.section .exceptions.entry.ecc_fatal, "xa"
/*
* This might be handling an unrecoverable ECC error exception
* in the register file and/or data cache.
* Must avoid reading registers or performing load/store instructions
* before this is determined because they could trigger another
* unrecoverable ECC error exception and create an infinite loop.
*
* The EXCEPTION register is always present when ECC is present.
* Bit 31 of this register indicates that there was an unrecoverable
* ECC error exception in the register file and/or data cache.
* Test this (using blt to check sign bit) to determine if this is
* what we are dealing with. Otherwise, just do normal processing.
*
* Jump to an application-provided routine to handle this condition.
* Pass in the return address in the et register in case this code
* can clean up the ECC error and then return here (unlikely).
*
* Runtime stack checking can't be enabled when ECC is present
* because they both want to use the et register.
*/
rdctl et, exception
bge et, r0, alt_exception_not_ecc_fatal /* Not ECCFTL if bit 31 is 0 */
/*
* Load ECC fatal handler pointer into et register.
* Using a ldwio is safe because it completely bypasses the data cache.
*/
movhi et, %hi(alt_exception_ecc_fatal_handler)
ori et, et, %lo(alt_exception_ecc_fatal_handler)
ldwio et, 0(et)
/*
* If ECC fatal handler pointer is not 0, assume a handler
* has been provided by the application.
*/
beq et, r0, alt_exception_not_ecc_fatal
/*
* The et register contains the address of the ECC fatal handler.
* Jump to this address to invoke the handler.
*/
jmp et
/*
* An ECC fatal handler can jump to this label if it able
* to recover from the fatal error (rare) and wants to continue
* with normal exception processing.
*/
.globl alt_exception_not_ecc_fatal
alt_exception_not_ecc_fatal:
@@ -0,0 +1,75 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2013 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "io.h"
#include "sys/alt_exceptions.h"
#include "sys/alt_cache.h"
/*
* This file implements support for calling a user-registered handler
* when a likely fatal ECC error exception occurs.
*/
/*
* Global variable containing address to jump to when likely fatal
* ECC error exception occurs.
*/
alt_u32 alt_exception_ecc_fatal_handler = 0x0;
/*
* Pull in the exception entry assembly code. This will not be linked in
* unless this object is linked into the executable (i.e. only if
* alt_ecc_fatal_exception_register() is called).
*/
__asm__( "\n\t.globl alt_exception" );
/*
* alt_ecc_fatal_exception_register() is called to register a handler to
* service likely fatal ECC error exceptions.
*
* Passing null (0x0) in the handler argument will disable a previously-
* registered handler.
*
* Note that if no handler is registered, just normal exception processing
* occurs on a likely fatal ECC exception and the exception processing
* code might trigger an infinite exception loop.
*/
void
alt_ecc_fatal_exception_register(alt_u32 handler)
{
alt_exception_ecc_fatal_handler = handler;
/*
* Flush this from the cache. Required because the exception handler uses ldwio
* to read this value to avoid trigger another data cache ECC error exception.
*/
alt_dcache_flush(&alt_exception_ecc_fatal_handler, sizeof(alt_exception_ecc_fatal_handler));
}
@@ -0,0 +1,53 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <reent.h>
/*
* These are the empty env lock/unlock stubs required by newlib. These are
* used to make accesses to environment variables thread safe. The default HAL
* configuration is single threaded, so there is nothing to do here. Note that
* this requires that environment variables are never manipulated by an interrupt
* service routine.
*/
void __env_lock ( struct _reent *_r )
{
}
/*
*
*/
void __env_unlock ( struct _reent *_r )
{
}
@@ -0,0 +1,42 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "os/alt_syscall.h"
/*
* These are the environment variables passed to the C code. By default there
* are no variables registered. An application can manipulate this list using
* getenv() and setenv().
*/
char *__env[1] = { 0 };
char **ALT_ENVIRON = __env;
@@ -0,0 +1,44 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
/*
* This file defines the alt_errno global variable. See comments in
* alt_errno.h for the use of this variable.
*/
#include "sys/alt_errno.h"
#include "alt_types.h"
extern int ALT_WEAK *__errno (void);
int* (*alt_errno) (void) = __errno;
@@ -0,0 +1,402 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2008 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "system.h"
/*
* This is the exception entry point code, which saves all the caller saved
* registers and then handles the appropriate exception. It should be pulled
* in using a .globl from all the exception handler routines. This scheme is
* used so that if an interrupt is never registered, then this code will not
* appear in the generated executable, thereby improving code footprint.
*
* If an external interrpt controller (EIC) is present, it will supply an
* interrupt vector address to the processor when an interrupt occurs. For
* The Altera Vectored Interrupt Controller (VIC) driver will establish a
* vector table and the processor will jump directly to the appropriate
* table entry, funnel routine, and then user ISR. This will bypass this code
* in entirety. This code might still be linked into a system with an EIC,
* but would then be used only for non-interrupt exceptions.
*/
/*
* Explicitly allow the use of r1 (the assembler temporary register)
* within this code. This register is normally reserved for the use of
* the assembler.
*/
.set noat
/*
* The top and bottom of the exception stack.
*/
#ifdef ALT_EXCEPTION_STACK
.globl __alt_exception_stack_pointer
#ifdef ALT_STACK_CHECK
.globl __alt_exception_stack_limit
/*
* Store the value of the stack limit after interrupt somewhere.
*/
.globl alt_exception_old_stack_limit
#endif /* ALT_STACK_CHECK */
#endif /* ALT_EXCEPTION_STACK */
/*
* The code at alt_exception is located at the Nios II exception
* handler address.
*/
.section .exceptions.entry.label, "xa"
.globl alt_exception
.type alt_exception, @function
alt_exception:
/*
* The code for detecting a likely fatal ECC exception is
* linked here before the normal exception handler code if required.
* This is handled by the linker script and putting that code
* in the .exceptions.entry.ecc_fatal section.
*/
/*
* Now start the normal exception handler code.
*/
.section .exceptions.entry, "xa"
#ifdef ALT_EXCEPTION_STACK
#ifdef ALT_STACK_CHECK
/*
* When runtime stack checking is enabled, the et register
* contains the stack limit. Save this in memory before
* overwriting the et register.
*/
stw et, %gprel(alt_exception_old_stack_limit)(gp)
#endif /* ALT_STACK_CHECK */
/*
* Switch to the exception stack and save the current stack pointer
* in memory. Uses the et register as a scratch register.
*/
movhi et, %hi(__alt_exception_stack_pointer - 80)
ori et, et, %lo(__alt_exception_stack_pointer - 80)
stw sp, 76(et)
mov sp, et
#ifdef ALT_STACK_CHECK
/*
* Restore the stack limit from memory to the et register.
*/
movhi et, %hi(__alt_exception_stack_limit)
ori et, et, %lo(__alt_exception_stack_limit)
stw et, %gprel(alt_stack_limit_value)(gp)
#endif /* ALT_STACK_CHECK */
#else /* ALT_EXCEPTION_STACK disabled */
/*
* Reserve space on normal stack for registers about to be pushed.
*/
addi sp, sp, -76
#ifdef ALT_STACK_CHECK
/* Ensure stack didn't just overflow. */
bltu sp, et, .Lstack_overflow
#endif /* ALT_STACK_CHECK */
#endif /* ALT_EXCEPTION_STACK */
/*
* Process an exception. For all exceptions we must preserve all
* caller saved registers on the stack (See the Nios II ABI
* documentation for details).
*
* Leave a gap in the stack frame at 4(sp) for the muldiv handler to
* store zero into.
*/
stw ra, 0(sp)
stw r1, 8(sp)
stw r2, 12(sp)
stw r3, 16(sp)
stw r4, 20(sp)
stw r5, 24(sp)
stw r6, 28(sp)
stw r7, 32(sp)
rdctl r5, estatus /* Read early to avoid usage stall */
stw r8, 36(sp)
stw r9, 40(sp)
stw r10, 44(sp)
stw r11, 48(sp)
stw r12, 52(sp)
stw r13, 56(sp)
stw r14, 60(sp)
stw r15, 64(sp)
/*
* ea-4 contains the address of the instruction being executed
* when the exception occured. For interrupt exceptions, we will
* will be re-issue the isntruction. Store it in 72(sp)
*/
stw r5, 68(sp) /* estatus */
addi r15, ea, -4 /* instruction that caused exception */
stw r15, 72(sp)
/*
* The interrupt testing code (.exceptions.irqtest) will be
* linked here. If the Internal Interrupt Controller (IIC) is
* present (an EIC is not present), the presense of an interrupt
* is determined by examining CPU control registers or an interrupt
* custom instruction, if present.
*
* If the IIC is used and an interrupt is active, the code linked
* here will call the HAL IRQ handler (alt_irq_handler()) which
* successively calls registered interrupt handler(s) until no
* interrupts remain pending. It then jumps to .exceptions.exit. If
* there is no interrupt then it continues to .exception.notirq, below.
*/
.section .exceptions.notirq, "xa"
/*
* Prepare to service unimplemtned instructions or traps,
* each of which is optionally inked into section .exceptions.soft,
* which will preceed .exceptions.unknown below.
*
* Unlike interrupts, we want to skip the exception-causing instructon
* upon completion, so we write ea (address of instruction *after*
* the one where the exception occured) into 72(sp). The actual
* instruction that caused the exception is written in r2, which these
* handlers will utilize.
*/
stw ea, 72(sp) /* EA is PC+4 so will skip over instruction causing exception */
#ifdef NIOS2_CDX_PRESENT
mov.n r4, ea /* EA contains PC+4 of instruction that caused the exception */
subi.n r4, r4, 4 /* Calculate PC */
ldhu.n r2, 0(r4) /* Load least-significant 16 bits of instruction */
andi r5, r2, 0x7 /* Mask off all bits except the 3 most-significant bits of OP field */
/*
* These instructions compare the MSB 3 bits of OP to 0x1, 0x3, and 0x5
* which is where all the 16-bit instructions live.
*/
subi.n r5, r5, 1
beqz.n r5, .Lunknown_16bit
subi.n r5, r5, 2
beqz.n r5, .Lunknown_16bit
subi.n r5, r5, 2
beqz.n r5, .Lunknown_16bit
.Lunknown_32bit:
stw ea, 72(sp) /* EA is PC+4 so will skip over instruction causing exception */
/* Load most-significant 16 bits of instruction */
ldhu.n r3, 2(r4)
slli.n r3, r3, 16
or.n r2, r2, r3 /* 32-bit instruction value that caused exception */
br.n .Lunknown_inst_loaded
.Lunknown_16bit:
addi.n r4, r4, 2 /* Need PC+2 to skip over instruction causing exception */
stw r4, 72(sp)
#else /* CDX is not Enabled and all instructions are 32bits */
ldw r2, -4(ea) /* Instruction value that caused exception */
#endif
.Lunknown_inst_loaded:
/*
* Other exception handling code, if enabled, will be linked here.
* This includes unimplemted (multiply/divide) instruction support
* (a BSP generaton option), and a trap handler (that would typically
* be augmented with user-specific code). These are not linked in by
* default.
*/
/*
* In the context of linker sections, "unknown" are all exceptions
* not handled by the built-in handlers above (interupt, and trap or
* unimplemented instruction decoding, if enabled).
*
* Advanced exception types can be serviced by registering a handler.
* To do so, enable the "Enable Instruction-related Exception API" HAL
* BSP setting. If this setting is disabled, this handler code will
* either break (if the debug core is present) or enter an infinite
* loop because we don't how how to handle the exception.
*/
.section .exceptions.unknown
#ifdef ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API
/*
* The C-based HAL routine alt_instruction_exception_entry() will
* attempt to service the exception by calling a user-registered
* exception handler using alt_instruction_exception_register().
* If no handler was registered it will either break (if the
* debugger is present) or go into an infinite loop since the
* handling behavior is undefined; in that case we will not return here.
*/
/* Load exception-causing address as first argument (r4) */
addi r4, ea, -4
/* Call the instruction-exception entry */
call alt_instruction_exception_entry
/*
* If alt_instruction_exception_entry() returned, the exception was
* serviced by a user-registered routine. Its return code (now in r2)
* indicates whether to re-issue or skip the exception-causing
* instruction
*
* Return code was 0: Skip. The instruction after the exception is
* already stored in 72(sp).
*/
bne r2, r0, .Lexception_exit
/*
* Otherwise, modify 72(sp) to re-issue the instruction that caused the
* exception.
*/
addi r15, ea, -4 /* instruction that caused exception */
stw r15, 72(sp)
#else /* ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API disabled */
/*
* We got here because an instruction-related exception occured, but the
* handler API was not compiled in. We do not presume to know how to
* handle it. If the debugger is present, break, otherwise hang.
*
* If you get here then one of the following could have happened:
*
* - An instruction-generated exception occured, and the processor
* does not have the extra exceptions feature enabled, or you
* have not registered a handler using
* alt_instruction_exception_register()
*
* Some examples of instruction-generated exceptions and why they
* might occur:
*
* - Your program could have been compiled for a full-featured
* Nios II core, but it is running on a smaller core, and
* instruction emulation has been disabled by defining
* ALT_NO_INSTRUCTION_EMULATION.
*
* You can work around the problem by re-enabling instruction
* emulation, or you can figure out why your program is being
* compiled for a system other than the one that it is running on.
*
* - Your program has executed a trap instruction, but has not
* implemented a handler for this instruction.
*
* - Your program has executed an illegal instruction (one which is
* not defined in the instruction set).
*
* - Your processor includes an MMU or MPU, and you have enabled it
* before registering an exception handler to service exceptions it
* generates.
*
* The problem could also be hardware related:
* - If your hardware is broken and is generating spurious interrupts
* (a peripheral which negates its interrupt output before its
* interrupt handler has been executed will cause spurious
* interrupts)
*/
alt_exception_unknown:
#ifdef NIOS2_HAS_DEBUG_STUB
/*
* Either tell the user now (if there is a debugger attached) or go into
* the debug monitor which will loop until a debugger is attached.
*/
break
#else /* NIOS2_HAS_DEBUG_STUB disabled */
/*
* If there is no debug stub, an infinite loop is more useful.
*/
br alt_exception_unknown
#endif /* NIOS2_HAS_DEBUG_STUB */
#endif /* ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API */
.section .exceptions.exit.label
.Lexception_exit:
.section .exceptions.exit, "xa"
/*
* Restore the saved registers, so that all general purpose registers
* have been restored to their state at the time the interrupt occured.
*/
ldw r5, 68(sp)
ldw ea, 72(sp) /* This becomes the PC once eret is executed */
ldw ra, 0(sp)
wrctl estatus, r5
ldw r1, 8(sp)
ldw r2, 12(sp)
ldw r3, 16(sp)
ldw r4, 20(sp)
ldw r5, 24(sp)
ldw r6, 28(sp)
ldw r7, 32(sp)
#if defined(ALT_EXCEPTION_STACK) && defined(ALT_STACK_CHECK)
ldw et, %gprel(alt_exception_old_stack_limit)(gp)
#endif
ldw r8, 36(sp)
ldw r9, 40(sp)
ldw r10, 44(sp)
ldw r11, 48(sp)
ldw r12, 52(sp)
ldw r13, 56(sp)
ldw r14, 60(sp)
ldw r15, 64(sp)
#ifdef ALT_EXCEPTION_STACK
#ifdef ALT_STACK_CHECK
stw et, %gprel(alt_stack_limit_value)(gp)
stw zero, %gprel(alt_exception_old_stack_limit)(gp)
#endif /* ALT_STACK_CHECK */
ldw sp, 76(sp)
#else /* ALT_EXCEPTION_STACK disabled */
addi sp, sp, 76
#endif /* ALT_EXCEPTION_STACK */
/*
* Return to the interrupted instruction.
*/
eret
#ifdef ALT_STACK_CHECK
.Lstack_overflow:
break 3
#endif /* ALT_STACK_CHECK */
@@ -0,0 +1,583 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
/*
* This is the software multiply/divide handler for Nios2.
*/
/*
* Provide a label which can be used to pull this file in.
*/
.section .exceptions.start
.globl alt_exception_muldiv
alt_exception_muldiv:
/*
* Pull in the entry/exit code.
*/
.globl alt_exception
.section .exceptions.soft, "xa"
/* INSTRUCTION EMULATION
* ---------------------
*
* Nios II processors generate exceptions for unimplemented instructions.
* The routines below emulate these instructions. Depending on the
* processor core, the only instructions that might need to be emulated
* are div, divu, mul, muli, mulxss, mulxsu, and mulxuu.
*
* The emulations match the instructions, except for the following
* limitations:
*
* 1) The emulation routines do not emulate the use of the exception
* temporary register (et) as a source operand because the exception
* handler already has modified it.
*
* 2) The routines do not emulate the use of the stack pointer (sp) or the
* exception return address register (ea) as a destination because
* modifying these registers crashes the exception handler or the
* interrupted routine.
*
* 3) To save code size, the routines do not emulate the use of the
* breakpoint registers (ba and bt) as operands.
*
* Detailed Design
* ---------------
*
* The emulation routines expect the contents of integer registers r0-r31
* to be on the stack at addresses sp, 4(sp), 8(sp), ... 124(sp). The
* routines retrieve source operands from the stack and modify the
* destination register's value on the stack prior to the end of the
* exception handler. Then all registers except the destination register
* are restored to their previous values.
*
* The instruction that causes the exception is found at address -4(ea).
* The instruction's OP and OPX fields identify the operation to be
* performed.
*
* One instruction, muli, is an I-type instruction that is identified by
* an OP field of 0x24.
*
* muli AAAAA,BBBBB,IIIIIIIIIIIIIIII,-0x24-
* 27 22 6 0 <-- LSB of field
*
* The remaining emulated instructions are R-type and have an OP field
* of 0x3a. Their OPX fields identify them.
*
* R-type AAAAA,BBBBB,CCCCC,XXXXXX,NNNNN,-0x3a-
* 27 22 17 11 6 0 <-- LSB of field
*
*
*/
/*
* Split the instruction into its fields. We need 4*A, 4*B, and 4*C as
* offsets to the stack pointer for access to the stored register values.
*/
/* r2 = AAAAA,BBBBB,IIIIIIIIIIIIIIII,PPPPPP */
roli r3, r2, 7 /* r3 = BBB,IIIIIIIIIIIIIIII,PPPPPP,AAAAA,BB */
roli r4, r3, 3 /* r4 = IIIIIIIIIIIIIIII,PPPPPP,AAAAA,BBBBB */
roli r6, r4, 2 /* r6 = IIIIIIIIIIIIII,PPPPPP,AAAAA,BBBBB,II */
srai r4, r4, 16 /* r4 = (sign-extended) IMM16 */
xori r6, r6, 0x42 /* r6 = CCC,XXXXXX,NNNNN,PPPPPP,AAAAA,bBBBB,cC */
roli r7, r6, 5 /* r7 = XXXX,NNNNN,PPPPPP,AAAAA,bBBBB,cCCCC,XX */
andi r5, r2, 0x3f /* r5 = 00000000000000000000000000,PPPPPP */
xori r3, r3, 0x40
andi r3, r3, 0x7c /* r3 = 0000000000000000000000000,aAAAA,00 */
andi r6, r6, 0x7c /* r6 = 0000000000000000000000000,bBBBB,00 */
andi r7, r7, 0x7c /* r7 = 0000000000000000000000000,cCCCC,00 */
/* Now either
* r5 = OP
* r3 = 4*(A^16)
* r4 = IMM16 (sign extended)
* r6 = 4*(B^16)
* r7 = 4*(C^16)
* or
* r5 = OP
*/
/*
* Save everything on the stack to make it easy for the emulation routines
* to retrieve the source register operands. The exception entry code has
* already saved some of this so we don't need to do it all again.
*/
addi sp, sp, -60
stw zero, 64(sp) /* Save zero on stack to avoid special case for r0. */
/* Register at and r2-r15 have already been saved. */
stw r16, 0(sp)
stw r17, 4(sp)
stw r18, 8(sp)
stw r19, 12(sp)
stw r20, 16(sp)
stw r21, 20(sp)
stw r22, 24(sp)
stw r23, 28(sp)
/* et @ 32 - Has already been changed.*/
/* bt @ 36 - Usually isn't an operand. */
stw gp, 40(sp)
stw sp, 44(sp)
stw fp, 48(sp)
/* ea @ 52 - Don't bother to save - it's already been changed */
/* ba @ 56 - Breakpoint register usually isn't an operand */
/* ra @ 60 - Has already been saved */
/*
* Prepare for either multiplication or division loop.
* They both loop 32 times.
*/
movi r14, 32
/*
* Get the operands.
*
* It is necessary to check for muli because it uses an I-type instruction
* format, while the other instructions are have an R-type format.
*/
add r3, r3, sp /* r3 = address of A-operand. */
ldw r3, 0(r3) /* r3 = A-operand. */
movi r15, 0x24 /* muli opcode (I-type instruction format) */
beq r5, r15, .Lmul_immed /* muli doesn't use the B register as a source */
add r6, r6, sp /* r6 = address of B-operand. */
ldw r6, 0(r6) /* r6 = B-operand. */
/* r4 = SSSSSSSSSSSSSSSS,-----IMM16------ */
/* IMM16 not needed, align OPX portion */
/* r4 = SSSSSSSSSSSSSSSS,CCCCC,-OPX--,00000 */
srli r4, r4, 5 /* r4 = 00000,SSSSSSSSSSSSSSSS,CCCCC,-OPX-- */
andi r4, r4, 0x3f /* r4 = 00000000000000000000000000,-OPX-- */
/* Now
* r5 = OP
* r3 = src1
* r6 = src2
* r4 = OPX (no longer can be muli)
* r7 = 4*(C^16)
* r14 = loop counter
*/
/* ILLEGAL-INSTRUCTION EXCEPTION
* -----------------------------
*
* This code is for Nios II cores that generate exceptions when attempting
* to execute illegal instructions. Nios II cores that support an
* illegal-instruction exception are identified by the presence of the
* macro definition NIOS2_HAS_ILLEGAL_INSTRUCTION_EXCEPTION in system.h .
*
* Remember that illegal instructions are different than unimplemented
* instructions. Illegal instructions are instruction encodings that
* have not been defined by the Nios II ISA. Unimplemented instructions
* are legal instructions that must be emulated by some Nios II cores.
*
* If we get here, all instructions except multiplies and divides
* are illegal.
*
* This code assumes that OP is not muli (because muli was tested above).
* All other multiplies and divides are legal. Anything else is illegal.
*/
movi r8, 0x3a /* OP for R-type mul* and div* */
bne r5, r8, .Lnot_muldiv
/* r15 already is 0x24 */ /* OPX of divu */
beq r4, r15, .Ldivide
movi r15,0x27 /* OPX of mul */
beq r4, r15, .Lmultiply
movi r15,0x07 /* OPX of mulxuu */
beq r4, r15, .Lmultiply
movi r15,0x17 /* OPX of mulxsu */
beq r4, r15, .Lmultiply
movi r15,0x1f /* OPX of mulxss */
beq r4, r15, .Lmultiply
movi r15,0x25 /* OPX of div */
bne r4, r15, .Lnot_muldiv
/* DIVISION
*
* Divide an unsigned dividend by an unsigned divisor using
* a shift-and-subtract algorithm. The example below shows
* 43 div 7 = 6 for 8-bit integers. This classic algorithm uses a
* single register to store both the dividend and the quotient,
* allowing both values to be shifted with a single instruction.
*
* remainder dividend:quotient
* --------- -----------------
* initialize 00000000 00101011:
* shift 00000000 0101011:_
* remainder >= divisor? no 00000000 0101011:0
* shift 00000000 101011:0_
* remainder >= divisor? no 00000000 101011:00
* shift 00000001 01011:00_
* remainder >= divisor? no 00000001 01011:000
* shift 00000010 1011:000_
* remainder >= divisor? no 00000010 1011:0000
* shift 00000101 011:0000_
* remainder >= divisor? no 00000101 011:00000
* shift 00001010 11:00000_
* remainder >= divisor? yes 00001010 11:000001
* remainder -= divisor - 00000111
* ----------
* 00000011 11:000001
* shift 00000111 1:000001_
* remainder >= divisor? yes 00000111 1:0000011
* remainder -= divisor - 00000111
* ----------
* 00000000 1:0000011
* shift 00000001 :0000011_
* remainder >= divisor? no 00000001 :00000110
*
* The quotient is 00000110.
*/
.Ldivide:
/*
* Prepare for division by assuming the result
* is unsigned, and storing its "sign" as 0.
*/
movi r17, 0
/* Which division opcode? */
xori r15, r4, 0x25 /* OPX of div */
bne r15, zero, .Lunsigned_division
/*
* OPX is div. Determine and store the sign of the quotient.
* Then take the absolute value of both operands.
*/
xor r17, r3, r6 /* MSB contains sign of quotient */
bge r3, zero, 0f
sub r3, zero, r3 /* -r3 */
0:
bge r6, zero, 0f
sub r6, zero, r6 /* -r6 */
0:
.Lunsigned_division:
/* Initialize the unsigned-division loop. */
movi r13, 0 /* remainder = 0 */
/* Now
* r3 = dividend : quotient
* r4 = 0x25 for div, 0x24 for divu
* r6 = divisor
* r13 = remainder
* r14 = loop counter (already initialized to 32)
* r17 = MSB contains sign of quotient
*/
/*
* for (count = 32; count > 0; --count)
* {
*/
.Ldivide_loop:
/*
* Division:
*
* (remainder:dividend:quotient) <<= 1;
*/
slli r13, r13, 1
cmplt r15, r3, zero /* r15 = MSB of r3 */
or r13, r13, r15
slli r3, r3, 1
/*
* if (remainder >= divisor)
* {
* set LSB of quotient
* remainder -= divisor;
* }
*/
bltu r13, r6, .Ldiv_skip
ori r3, r3, 1
sub r13, r13, r6
.Ldiv_skip:
/*
* }
*/
subi r14, r14, 1
bne r14, zero, .Ldivide_loop
mov r9, r3
/* Now
* r9 = quotient
* r4 = 0x25 for div, 0x24 for divu
* r7 = 4*(C^16)
* r17 = MSB contains sign of quotient
*/
/*
* Conditionally negate signed quotient. If quotient is unsigned,
* the sign already is initialized to 0.
*/
bge r17, zero, .Lstore_result
sub r9, zero, r9 /* -r9 */
br .Lstore_result
/* MULTIPLICATION
*
* A "product" is the number that one gets by summing a "multiplicand"
* several times. The "multiplier" specifies the number of copies of the
* multiplicand that are summed.
*
* Actual multiplication algorithms don't use repeated addition, however.
* Shift-and-add algorithms get the same answer as repeated addition, and
* they are faster. To compute the lower half of a product (pppp below)
* one shifts the product left before adding in each of the partial products
* (a * mmmm) through (d * mmmm).
*
* To compute the upper half of a product (PPPP below), one adds in the
* partial products (d * mmmm) through (a * mmmm), each time following the
* add by a right shift of the product.
*
* mmmm
* * abcd
* ------
* #### = d * mmmm
* #### = c * mmmm
* #### = b * mmmm
* #### = a * mmmm
* --------
* PPPPpppp
*
* The example above shows 4 partial products. Computing actual Nios II
* products requires 32 partials.
*
* It is possible to compute the result of mulxsu from the result of mulxuu
* because the only difference between the results of these two opcodes is
* the value of the partial product associated with the sign bit of rA.
*
* mulxsu = mulxuu - ((rA < 0) ? rB : 0);
*
* It is possible to compute the result of mulxss from the result of mulxsu
* because the only difference between the results of these two opcodes is
* the value of the partial product associated with the sign bit of rB.
*
* mulxss = mulxsu - ((rB < 0) ? rA : 0);
*
*/
.Lmul_immed:
/* Opcode is muli. Change it into mul for remainder of algorithm. */
mov r7, r6 /* Field B is dest register, not field C. */
mov r6, r4 /* Field IMM16 is src2, not field B. */
movi r4, 0x27 /* OPX of mul is 0x27 */
.Lmultiply:
/* Initialize the multiplication loop. */
movi r9, 0 /* mul_product = 0 */
movi r10, 0 /* mulxuu_product = 0 */
mov r11, r6 /* save original multiplier for mulxsu and mulxss */
mov r12, r6 /* mulxuu_multiplier (will be shifted) */
movi r16, 1 /* used to create "rori B,A,1" from "ror B,A,r16" */
/* Now
* r3 = multiplicand
* r6 = mul_multiplier
* r7 = 4 * dest_register (used later as offset to sp)
* r9 = mul_product
* r10 = mulxuu_product
* r11 = original multiplier
* r12 = mulxuu_multiplier
* r14 = loop counter (already initialized)
* r15 = temp
* r16 = 1
*/
/*
* for (count = 32; count > 0; --count)
* {
*/
.Lmultiply_loop:
/*
* mul_product <<= 1;
* lsb = multiplier & 1;
*/
slli r9, r9, 1
andi r15, r12, 1
/*
* if (lsb == 1)
* {
* mulxuu_product += multiplicand;
* }
*/
beq r15, zero, .Lmulx_skip
add r10, r10, r3
cmpltu r15, r10, r3 /* Save the carry from the MSB of mulxuu_product. */
ror r15, r15, r16 /* r15 = 0x80000000 on carry, or else 0x00000000 */
.Lmulx_skip:
/*
* if (MSB of mul_multiplier == 1)
* {
* mul_product += multiplicand;
* }
*/
bge r6, zero, .Lmul_skip
add r9, r9, r3
.Lmul_skip:
/*
* mulxuu_product >>= 1; logical shift
* mul_multiplier <<= 1; done with MSB
* mulx_multiplier >>= 1; done with LSB
*/
srli r10, r10, 1
or r10, r10, r15 /* OR in the saved carry bit. */
slli r6, r6, 1
srli r12, r12, 1
/*
* }
*/
subi r14, r14, 1
bne r14, zero, .Lmultiply_loop
/*
* Multiply emulation loop done.
*/
/* Now
* r3 = multiplicand
* r4 = OPX
* r7 = 4 * dest_register (used later as offset to sp)
* r9 = mul_product
* r10 = mulxuu_product
* r11 = original multiplier
* r15 = temp
*/
/*
* Select/compute the result based on OPX.
*/
/* OPX == mul? Then store. */
xori r15, r4, 0x27
beq r15, zero, .Lstore_result
/* It's one of the mulx.. opcodes. Move over the result. */
mov r9, r10
/* OPX == mulxuu? Then store. */
xori r15, r4, 0x07
beq r15, zero, .Lstore_result
/* Compute mulxsu
*
* mulxsu = mulxuu - ((rA < 0) ? rB : 0);
*/
bge r3, zero, .Lmulxsu_skip
sub r9, r9, r11
.Lmulxsu_skip:
/* OPX == mulxsu? Then store. */
xori r15, r4, 0x17
beq r15, zero, .Lstore_result
/* Compute mulxss
*
* mulxss = mulxsu - ((rB < 0) ? rA : 0);
*/
bge r11, zero, .Lmulxss_skip
sub r9, r9, r3
.Lmulxss_skip:
/* At this point, assume that OPX is mulxss, so store */
.Lstore_result:
add r7, r7, sp
stw r9, 0(r7)
ldw r16, 0(sp)
ldw r17, 4(sp)
ldw r18, 8(sp)
ldw r19, 12(sp)
ldw r20, 16(sp)
ldw r21, 20(sp)
ldw r22, 24(sp)
ldw r23, 28(sp)
/* bt @ 32 - Breakpoint register usually isn't an operand. */
/* et @ 36 - Don't corrupt et. */
/* gp @ 40 - Don't corrupt gp. */
/* sp @ 44 - Don't corrupt sp. */
ldw fp, 48(sp)
/* ea @ 52 - Don't corrupt ea. */
/* ba @ 56 - Breakpoint register usually isn't an operand. */
addi sp, sp, 60
br .Lexception_exit
.Lnot_muldiv:
addi sp, sp, 60
.section .exceptions.exit.label
.Lexception_exit:
@@ -0,0 +1,95 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
/*
* This is the trap exception handler for Nios2.
*/
/*
* Provide a label which can be used to pull this file in.
*/
.section .exceptions.start
.globl alt_exception_trap
alt_exception_trap:
/*
* Pull in the entry/exit code.
*/
.globl alt_exception
.section .exceptions.soft, "xa"
.Ltrap_handler:
/*
* Did a trap instruction cause the exception?
*
* The instruction which the exception occurred on has been loaded
* into r2 by code in alt_exception_entry.S
*
*/
#ifdef ALT_CPU_CPU_ARCH_NIOS2_R2
movhi r3,0xb41d /* upper half of trap opcode */
ori r3,r3,0x0020 /* lower half of trap opcode */
beq r2,r3,.Lis_trap
#ifdef NIOS2_CDX_PRESENT
mov r3,r2
andhi r3,r3,0xffff
ori r3,r3,0xd009 /* trap.n opcode */
beq r2,r3,.Lis_trap
#endif
br .Lnot_trap
#else
movhi r3,0x003b /* upper half of trap opcode */
ori r3,r3,0x683a /* lower half of trap opcode */
bne r2,r3,.Lnot_trap
#endif
.Lis_trap:
/*
* There is no trap handler defined here, and so executing a trap
* instruction causes a software break. If you provide a trap handler,
* then you must replace the break instruction below with your handler.
* Your handler must preserve ea and the usual callee saved registers.
*/
break
br .Lexception_exit
.Lnot_trap:
.section .exceptions.exit.label
.Lexception_exit:
@@ -0,0 +1,55 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "os/alt_syscall.h"
/*
* execve() is used by newlib to launch new processes. This is unsupported in
* the HAL environment. However a "do-nothing" implementation is still
* provied for newlib compatability.
*
* ALT_EXECVE is mapped onto the execve() system call in alt_syscall.h
*/
int ALT_EXECVE (char *name, char ** argv, char** env)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(execve);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
@@ -0,0 +1,71 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_irq.h"
#include "sys/alt_sim.h"
#include "os/alt_hooks.h"
#include "os/alt_syscall.h"
#include "alt_types.h"
#include "sys/alt_log_printf.h"
/*
* _exit() is called by exit() in order to terminate the current process.
* Typically this is called when main() completes. It should never return.
* Since there is nowhere to go once this process completes, this
* implementation simply blocks forever.
*
* Note that interrupts are not disabled so that execution outside of this
* thread is allowed to continue.
*
* ALT_EXIT is mapped onto the _exit() system call in alt_syscall.h
*/
void ALT_EXIT (int exit_code)
{
/* ALT_LOG - please see HAL/inc/alt_log_printf.h for details */
ALT_LOG_PRINT_BOOT("[alt_exit.c] Entering _exit() function.\r\n");
ALT_LOG_PRINT_BOOT("[alt_exit.c] Exit code from main was %d.\r\n",exit_code);
/* Stop all other threads */
ALT_LOG_PRINT_BOOT("[alt_exit.c] Calling ALT_OS_STOP().\r\n");
ALT_OS_STOP();
/* Provide notification to the simulator that we've stopped */
ALT_LOG_PRINT_BOOT("[alt_exit.c] Calling ALT_SIM_HALT().\r\n");
ALT_SIM_HALT(exit_code);
/* spin forever, since there's no where to go back to */
ALT_LOG_PRINT_BOOT("[alt_exit.c] Spinning forever.\r\n");
while (1);
}
@@ -0,0 +1,101 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include "sys/alt_errno.h"
#include "priv/alt_file.h"
#include "alt_types.h"
#include "os/alt_syscall.h"
#define ALT_FCNTL_FLAGS_MASK ((alt_u32) (O_APPEND | O_NONBLOCK))
/*
* fcntl() is a limited implementation of the standard fcntl() system call.
* It can be used to change the state of the flags associated with an open
* file descriptor. Normally these flags are set during the call to
* open(). It is anticipated that the main use of this function will be to
* change the state of a device from blocking to non-blocking (where this is
* supported).
*
* The input argument "fd" is the file descriptor to be manipulated. "cmd"
* is the command to execute. This can be either F_GETFL (return the
* current value of the flags) or F_SETFL (set the value of the flags).
*
* If "cmd" is F_SETFL then the argument "arg" is the new value of flags,
* otherwise "arg" is ignored. Only the flags: O_APPEND and O_NONBLOCK
* can be updated by a call to fcntl(). All other flags remain
* unchanged.
*
* ALT_FCNTL is mapped onto the fcntl() system call in alt_syscall.h
*/
int ALT_FCNTL (int file, int cmd, ...)
{
alt_fd* fd;
long flags;
va_list argp;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (file < 0) ? NULL : &alt_fd_list[file];
if (fd)
{
switch (cmd)
{
case F_GETFL:
return fd->fd_flags & ~((alt_u32) ALT_FD_FLAGS_MASK);
case F_SETFL:
va_start(argp, cmd);
flags = va_arg(argp, long);
fd->fd_flags &= ~ALT_FCNTL_FLAGS_MASK;
fd->fd_flags |= (flags & ALT_FCNTL_FLAGS_MASK);
va_end(argp);
return 0;
default:
ALT_ERRNO = EINVAL;
return -1;
}
}
ALT_ERRNO = EBADFD;
return -1;
}
@@ -0,0 +1,75 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <errno.h>
#include "priv/alt_file.h"
/*
* alt_fd_lock() is called as a consequence of an ioctl call to gain exclusive
* access to a device, i.e.:
*
* ioctl (fd, TIOCEXCL, NULL);
*
* If there are no other open file descriptors which reference the same
* device, then alt_fd_lock() will grant the lock. Further calls to open()
* for this device will fail until the lock is released.
*
* This is done by calling close() for this file descriptor, or by calling:
*
* ioctl (fd, TIOCNXCL, NULL);
*
* The return value is zero for success, or negative in the case of failure.
*/
int alt_fd_lock (alt_fd* fd)
{
int i;
int rc = 0;
ALT_SEM_PEND(alt_fd_list_lock, 0);
for (i = 0; i < alt_max_fd; i++)
{
if ((&alt_fd_list[i] != fd) && (alt_fd_list[i].dev == fd->dev))
{
rc = -EACCES;
goto alt_fd_lock_exit;
}
}
fd->fd_flags |= ALT_FD_EXCL;
alt_fd_lock_exit:
ALT_SEM_POST(alt_fd_list_lock);
return rc;
}
@@ -0,0 +1,56 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <errno.h>
#include "priv/alt_file.h"
/*
* alt_fd_unlock() is the inverse of alt_fd_lock(). It is called as a
* consequence of a TIOCNXCL ioctl request, e.g:
*
* ioctl (fd, TIOCNXCL, NULL);
*
* It enables multiple file descriptors to exist for the same device. This
* is normally the case, but it may have been disabled by a previous call to
* alt_fd_lock().
*
* Return zero on sucess, and a negative value on failure.
*
* The current implementation always succeeds.
*/
int alt_fd_unlock (alt_fd* fd)
{
fd->fd_flags &= ~ALT_FD_EXCL;
return 0;
}
@@ -0,0 +1,88 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "sys/alt_dev.h"
#include "priv/alt_file.h"
#include "alt_types.h"
/*
* alt_find_dev() is used by open() in order to locate a previously registered
* device with the name "name". The input argument "llist" is a pointer to the
* head of the device list to search.
*
* The return value is a pointer to the matching device, or NULL if there is
* no match.
*
* "name" must be an exact match for the devices registered name for a match to
* be found.
*/
alt_dev* alt_find_dev(const char* name, alt_llist* llist)
{
alt_dev* next = (alt_dev*) llist->next;
alt_32 len;
len = strlen(name) + 1;
/*
* Check each list entry in turn, until a match is found, or we reach the
* end of the list (i.e. next winds up pointing back to the list head).
*/
while (next != (alt_dev*) llist)
{
/*
* memcmp() is used here rather than strcmp() in order to reduce the size
* of the executable.
*/
if (!memcmp (next->name, name, len))
{
/* match found */
return next;
}
next = (alt_dev*) next->llist.next;
}
/* No match found */
return NULL;
}
@@ -0,0 +1,89 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "sys/alt_dev.h"
#include "priv/alt_file.h"
#include "alt_types.h"
/*
* alt_find_file() is used by open() in order to locate a previously registered
* filesystem that owns that mount point that contains the file named "name".
*
* The return value is a pointer to the matching filesystem, or NULL if there is
* no match.
*
* A match is considered to have been found if the filesystem name followed by
* either '/' or '\0' is the prefix of the filename. For example the filename:
* "/myfilesystem/junk.txt" would match: "/myfilesystem", but not: "/myfile".
*/
alt_dev* alt_find_file (const char* name)
{
alt_dev* next = (alt_dev*) alt_fs_list.next;
alt_32 len;
/*
* Check each list entry in turn, until a match is found, or we reach the
* end of the list (i.e. next winds up pointing back to the list head).
*/
while (next != (alt_dev*) &alt_fs_list)
{
len = strlen(next->name);
if (next->name[len-1] == '/')
{
len -= 1;
}
if (((name[len] == '/') || (name[len] == '\0')) &&
!memcmp (next->name, name, len))
{
/* match found */
return next;
}
next = (alt_dev*) next->llist.next;
}
/* No match found */
return NULL;
}
@@ -0,0 +1,69 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
/******************************************************************************
* *
* Alt_flash.c - Functions to register a flash device to the "generic" flash *
* interface *
* *
* Author PRR *
* *
******************************************************************************/
#include <errno.h>
#include "sys/alt_llist.h"
#include "sys/alt_flash_dev.h"
#include "priv/alt_file.h"
ALT_LLIST_HEAD(alt_flash_dev_list);
alt_flash_fd* alt_flash_open_dev(const char* name)
{
alt_flash_dev* dev = (alt_flash_dev*)alt_find_dev(name, &alt_flash_dev_list);
if ((dev) && dev->open)
{
return dev->open(dev, name);
}
return dev;
}
void alt_flash_close_dev(alt_flash_fd* fd)
{
if (fd && fd->close)
{
fd->close(fd);
}
return;
}
@@ -0,0 +1,57 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_warning.h"
#include "sys/alt_errno.h"
#include "os/alt_syscall.h"
/*
* The fork() system call is used by newlib to create a duplicate copy of the
* curent process. This is unsupported in the HAL environment. However a
* "do-nothing" implementation is still provied for newlib compatability.
*
* ALT_FORK is mapped onto the fork() system call in alt_syscall.h
*/
int ALT_FORK (void)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(fork);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
@@ -0,0 +1,75 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <errno.h>
#include "sys/alt_dev.h"
#include "priv/alt_file.h"
/*
* The alt_fs_reg() function is used to register a file system. Once registered
* a device can be accessed using the standard posix calls: open(), read(),
* write() etc.
*
* System behaviour is undefined in the event that a file system is registered
* with a name that conflicts with an existing device or file system.
*
* alt_fs_reg() is not thread safe in the sense that there should be no other
* thread using the file system list at the time that alt_dev_reg() is called. In
* practice this means that alt_fs_reg() should only be called while operating
* in a single threaded mode. The expectation is that it will only be called
* by the file system initilisation functions invoked by alt_sys_init(), which in
* turn should only be called by the single threaded C startup code.
*
* A return value of zero indicates success. A negative return value indicates
* failure.
*/
int alt_fs_reg (alt_dev* dev)
{
/*
* check that the device has a name.
*/
if (!dev->name)
{
return -ENODEV;
}
/*
* register the file system.
*/
alt_llist_insert(&alt_fs_list, &dev->llist);
return 0;
}
@@ -0,0 +1,128 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include <sys/stat.h>
#include "sys/alt_dev.h"
#include "sys/alt_warning.h"
#include "sys/alt_errno.h"
#include "priv/alt_file.h"
#include "os/alt_syscall.h"
/*
* The fstat() system call is used to obtain information about the capabilities
* of an open file descriptor. By default file descriptors are marked as
* being character devices. If a device or file system wishes to advertise
* alternative capabilities then they can register an fstat() function within
* their associated alt_dev structure. This will be called to fill in the
* entries in the imput "st" structure.
*
* This function is provided for compatability with newlib.
*
* ALT_FSTAT is mapped onto the fstat() system call in alt_syscall.h
*/
#ifdef ALT_USE_DIRECT_DRIVERS
#include "system.h"
/*
* Provide minimal version that just describes all file descriptors
* as character devices for provided stdio devices.
*/
int ALT_FSTAT (int file, struct stat *st)
{
switch (file) {
#ifdef ALT_STDIN_PRESENT
case 0: /* stdin file descriptor */
#endif /* ALT_STDIN_PRESENT */
#ifdef ALT_STDOUT_PRESENT
case 1: /* stdout file descriptor */
#endif /* ALT_STDOUT_PRESENT */
#ifdef ALT_STDERR_PRESENT
case 2: /* stderr file descriptor */
#endif /* ALT_STDERR_PRESENT */
st->st_mode = _IFCHR;
return 0;
default:
return -1;
}
#if !defined(ALT_STDIN_PRESENT) && !defined(ALT_STDOUT_PRESENT) && !defined(ALT_STDERR_PRESENT)
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(fstat);
#endif
}
#else /* !ALT_USE_DIRECT_DRIVERS */
int ALT_FSTAT (int file, struct stat *st)
{
alt_fd* fd;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (file < 0) ? NULL : &alt_fd_list[file];
if (fd)
{
/* Call the drivers fstat() function to fill out the "st" structure. */
if (fd->dev->fstat)
{
return fd->dev->fstat(fd, st);
}
/*
* If no function is provided, mark the fd as belonging to a character
* device.
*/
else
{
st->st_mode = _IFCHR;
return 0;
}
}
else
{
ALT_ERRNO = EBADFD;
return -1;
}
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,105 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include "sys/alt_dev.h"
#include "priv/alt_file.h"
#include "alt_types.h"
#include "system.h"
/*
* alt_get_fd() is called to allocate a new file descriptor from the file
* descriptor pool. If a file descriptor is succesfully allocated, it is
* configured to refer to device "dev".
*
* The return value is the index of the file descriptor structure (i.e.
* the offset of the file descriptor within the file descriptor array). A
* negative value indicates failure.
*/
int alt_get_fd (alt_dev* dev)
{
alt_32 i;
int rc = -EMFILE;
/*
* Take the alt_fd_list_lock semaphore in order to avoid races when
* accessing the file descriptor pool.
*/
ALT_SEM_PEND(alt_fd_list_lock, 0);
/*
* Search through the list of file descriptors, and allocate the first
* free descriptor that's found.
*
* If a free descriptor is found, then the value of "alt_max_fd" is
* updated accordingly. "alt_max_fd" is a 'highwater mark' which
* indicates the highest file descriptor ever allocated. This is used to
* improve efficency when searching the file descriptor list, and
* therefore reduce contention on the alt_fd_list_lock semaphore.
*/
for (i = 0; i < ALT_MAX_FD; i++)
{
if (!alt_fd_list[i].dev)
{
alt_fd_list[i].dev = dev;
if (i > alt_max_fd)
{
alt_max_fd = i;
}
rc = i;
goto alt_get_fd_exit;
}
}
alt_get_fd_exit:
/*
* Release the alt_fd_list_lock semaphore now that we are done with the
* file descriptor pool.
*/
ALT_SEM_POST(alt_fd_list_lock);
return rc;
}
@@ -0,0 +1,70 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2015 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stdio.h>
#ifdef ALT_USE_DIRECT_DRIVERS
#include "system.h"
#include "sys/alt_driver.h"
#include "sys/alt_stdio.h"
#include "priv/alt_file.h"
#include "unistd.h"
#endif
#ifdef ALT_SEMIHOSTING
#include "sys/alt_stdio.h"
#include "unistd.h"
#endif
/*
* Uses the ALT_DRIVER_READ() macro to call directly to driver if available.
* Otherwise, uses newlib provided getchar() routine.
*/
int
alt_getchar(void)
{
#ifdef ALT_SEMIHOSTING
char c;
read(STDIN_FILENO,&c,1);
return c;
#else
#ifdef ALT_USE_DIRECT_DRIVERS
ALT_DRIVER_READ_EXTERNS(ALT_STDIN_DEV);
char c;
if (ALT_DRIVER_READ(ALT_STDIN_DEV, &c, 1, alt_fd_list[STDIN_FILENO].fd_flags) <= 0) {
return -1;
}
return c;
#else
return getchar();
#endif
#endif
}
@@ -0,0 +1,47 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "os/alt_syscall.h"
/*
* The getpid() system call is used by newlib to obtain the current process
* id. Since there is only ever a single process in the HAL environment,
* this just returns a constant.
*
* ALT_GETPID is mapped onto the getpid() system call in alt_syscall.h
*/
int ALT_GETPID (void)
{
return 0;
}
@@ -0,0 +1,125 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2017,2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <sys/time.h>
#include <sys/times.h>
#include <errno.h>
#include "sys/alt_alarm.h"
#include "alt_types.h"
#include "os/alt_syscall.h"
/*
* Macro defining the number of micoseconds in a second.
*/
#define ALT_US (1000000)
/*
* "alt_timezone" and "alt_resettime" are the values of the the reset time and
* time zone set through the last call to settimeofday(). By default they are
* zero initialised.
*/
struct timezone alt_timezone = {0, 0};
struct timeval alt_resettime = {0, 0};
/*
* gettimeofday() can be called to obtain a time structure which indicates the
* current "wall clock" time. This is calculated using the elapsed number of
* system clock ticks, and the value of "alt_resettime" and "alt_timezone" set
* through the last call to settimeofday().
*
* Warning: if this function is called concurrently with a call to
* settimeofday(), the value returned by gettimeofday() will be unreliable.
*
* ALT_GETTIMEOFDAY is mapped onto the gettimeofday() system call in
* alt_syscall.h
*/
#if defined (__GNUC__) && (__GNUC__ >= 4)
int ALT_GETTIMEOFDAY (struct timeval *ptimeval, void *ptimezone_vptr)
{
struct timezone *ptimezone = (struct timezone*)ptimezone_vptr;
#else
int ALT_GETTIMEOFDAY (struct timeval *ptimeval, struct timezone *ptimezone)
{
#endif
alt_u64 nticks = alt_nticks ();
alt_u32 tick_rate = alt_ticks_per_second ();
/*
* Check to see if the system clock is running. This is indicated by a
* non-zero system clock rate. If the system clock is not running, an error
* is generated and the contents of "ptimeval" and "ptimezone" are not
* updated.
*/
if (tick_rate)
{
ptimeval->tv_sec = alt_resettime.tv_sec + nticks/tick_rate;
ptimeval->tv_usec = alt_resettime.tv_usec +
(alt_u32)((nticks*(ALT_US/tick_rate))%ALT_US);
while(ptimeval->tv_usec < 0) {
if (ptimeval->tv_sec <= 0)
{
ptimeval->tv_sec = 0;
ptimeval->tv_usec = 0;
break;
}
else
{
ptimeval->tv_sec--;
ptimeval->tv_usec += ALT_US;
}
}
while(ptimeval->tv_usec >= ALT_US) {
ptimeval->tv_sec++;
ptimeval->tv_usec -= ALT_US;
}
if (ptimezone)
{
ptimezone->tz_minuteswest = alt_timezone.tz_minuteswest;
ptimezone->tz_dsttime = alt_timezone.tz_dsttime;
}
return 0;
}
return -ENOTSUP;
}
@@ -0,0 +1,272 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include "priv/nios2_gmon_data.h"
#include "sys/alt_irq.h"
#include "sys/alt_alarm.h"
/* Macros */
/* How large should the bins be which we use to generate the histogram */
#define PCSAMPLE_BYTES_PER_BUCKET 32
#define NIOS2_READ_EA(dest) __asm__ ("mov %0, ea" : "=r" (dest))
/* The compiler inserts calls to mcount() at the start of
* every function call. The structure mcount_fn_arc records t
* he return address of the function called (in from_pc)
* and the return address of the mcount function
* (in self_pc). The number of times this arc is executed is
* recorded in the field count.
*/
struct mcount_fn_arc
{
struct mcount_fn_arc * next;
void * from_pc;
unsigned int count;
};
/* We need to maintain a list of pointers to the heads of each adjacency
* list so that we can find them when writing out the gmon.out file. Since
* we don't know at the start of program execution how many functions will
* be called we use a list structure to do this.
*/
struct mcount_fn_entry
{
struct mcount_fn_entry * next;
void * self_pc;
struct mcount_fn_arc * arc_head;
};
/* function prototypes */
void __mcount_record(void * self_pc, void * from_pc, struct mcount_fn_entry * fn_entry, struct mcount_fn_entry * * fn_head) __attribute__ ((no_instrument_function));
static __inline__ void * mcount_allocate(unsigned int size) __attribute__ ((no_instrument_function));
static int nios2_pcsample_init(void) __attribute__ ((no_instrument_function));
static alt_u32 nios2_pcsample(void* alarm) __attribute__ ((no_instrument_function));
/* global variables */
/* stext and etext are defined in the linker script */
extern char stext[];
extern char etext[];
/* Is the PC sampling stuff enabled yet? */
static int pcsample_need_init = 1;
#define HASH_BUCKETS 64 /* Must be a power of 2 */
/* This points to the list of adjacency list pointers. */
struct mcount_fn_entry * __mcount_fn_head[HASH_BUCKETS];
/* pointer to the in-memory buffer containing the histogram */
static unsigned short* s_pcsamples = 0;
/* the address of the start and end of text section */
static const unsigned int s_low_pc = (unsigned int)stext;
static const unsigned int s_high_pc = (unsigned int)etext;
/* the alarm structure to register for pc sampling */
static alt_alarm s_nios2_pcsample_alarm;
unsigned int alt_gmon_data[GMON_DATA_SIZE] =
{
0x6e6f6d67, /* "gmon" */
GMON_DATA_SIZE,
0,
(unsigned int)stext,
(unsigned int)etext,
PCSAMPLE_BYTES_PER_BUCKET,
0,
(unsigned int)__mcount_fn_head,
(unsigned int)(__mcount_fn_head + HASH_BUCKETS)
};
/* This holds the current slab of memory we're allocating out of */
static char * mcount_slab_ptr = 0;
static int mcount_slab_size = 0;
#define MCOUNT_SLAB_INCREMENT 1020
/*
* We can't use malloc to allocate memory because that's too complicated, and
* can't be called at interrupt time. Use the lower level allocator instead
* because that's interrupt safe (and because we never free anything).
*
* For speed, we allocate a block of data at once.
*/
static __inline__ void * mcount_allocate(unsigned int size)
{
void * data;
if (size > mcount_slab_size)
{
mcount_slab_ptr = sbrk(MCOUNT_SLAB_INCREMENT);
mcount_slab_size = MCOUNT_SLAB_INCREMENT;
}
data = mcount_slab_ptr;
mcount_slab_ptr += size;
mcount_slab_size -= size;
return data;
}
/*
* Add the arc with the values of frompc and topc given to the graph.
* This function might be called at interrupt time so must be able to
* cope with reentrancy.
*
* The fast case, where we have already allocated a function arc, has been
* handled by the assmebler code.
*/
void __mcount_record(void * self_pc, void * from_pc, struct mcount_fn_entry * fn_entry, struct mcount_fn_entry * * fn_head)
{
alt_irq_context context;
struct mcount_fn_arc * arc_entry;
/* Keep trying to start up the PC sampler until it is running.
* (It can't start until the timer is going).
*/
if (pcsample_need_init)
{
pcsample_need_init = 0;
pcsample_need_init = nios2_pcsample_init();
}
/*
* We must disable interrupts around the allocation and the list update to
* prevent corruption if the instrumented function is re-entrant.
*
* It's safe for the code above to be stepping through the chain and be
* interrupted by this code modifying it - there is an edge case which will
* leave two copies of the same arc on the list (both with count=1), but
* this is dealt with on the host.
*/
context = alt_irq_disable_all();
if (fn_entry == NULL)
{
/* Add it to the list of functions we must output later. */
fn_entry = (struct mcount_fn_entry *)mcount_allocate(sizeof(struct mcount_fn_entry));
fn_entry->self_pc = self_pc;
fn_entry->arc_head = NULL;
fn_entry->next = *fn_head;
*fn_head = fn_entry;
}
/* We will need a new list entry - if there was a list entry before
* then the assembler code would have handled it. */
arc_entry = (struct mcount_fn_arc *)mcount_allocate(sizeof(struct mcount_fn_arc));
arc_entry->from_pc = from_pc;
arc_entry->count = 1;
arc_entry->next = fn_entry->arc_head;
fn_entry->arc_head = arc_entry;
alt_irq_enable_all(context);
}
/*
* nios2_pcsample_init starts profiling.
* It is called the first time mcount is called, and on subsequent calls to
* mcount until it returns zero. It initializes the pc histogram and turns on
* timer driven pc sampling.
*/
static int nios2_pcsample_init(void)
{
unsigned int pcsamples_size;
/* We sample the PC every tick */
unsigned int prof_rate = alt_ticks_per_second();
if (prof_rate == 0)
return 1;
/* allocate the histogram buffer s_pcsamples */
pcsamples_size = (s_high_pc - s_low_pc)/PCSAMPLE_BYTES_PER_BUCKET;
s_pcsamples = (unsigned short*)sbrk(pcsamples_size * sizeof(unsigned short));
if (s_pcsamples != 0)
{
/* initialize the buffer to zero */
memset(s_pcsamples, 0, pcsamples_size * sizeof(unsigned short));
alt_gmon_data[GMON_DATA_PROFILE_DATA] = (int)s_pcsamples;
alt_gmon_data[GMON_DATA_PROFILE_RATE] = prof_rate;
/* Sample every tick (it's cheap) */
alt_alarm_start(&s_nios2_pcsample_alarm, 1, nios2_pcsample, 0);
}
return 0;
}
/*
* Sample the PC value and store it in the histogram
*/
static alt_u32 nios2_pcsample(void* context)
{
unsigned int pc=0;
unsigned int bucket;
/* read the exception return address - this will be
* inaccurate if there are nested interrupts but we
* assume that this is rare and the inaccuracy will
* not be great */
NIOS2_READ_EA(pc);
/*
* If we're within the profilable range then increment the relevant
* bucket in the histogram
*/
if (pc >= s_low_pc && pc < s_high_pc && s_pcsamples != 0)
{
bucket = (pc - s_low_pc)/PCSAMPLE_BYTES_PER_BUCKET;
s_pcsamples[bucket]++;
}
/* Sample every tick */
return 1;
}
@@ -0,0 +1,84 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "nios2.h"
#include "system.h"
#include "alt_types.h"
#include "sys/alt_cache.h"
/*
* alt_icache_flush() is called to flush the instruction cache for a memory
* region of length "len" bytes, starting at address "start".
*/
void alt_icache_flush (void* start, alt_u32 len)
{
#if NIOS2_ICACHE_SIZE > 0
char* i;
char* end;
/*
* This is the most we would ever need to flush.
*/
if (len > NIOS2_ICACHE_SIZE)
{
len = NIOS2_ICACHE_SIZE;
}
end = ((char*) start) + len;
for (i = start; i < end; i+= NIOS2_ICACHE_LINE_SIZE)
{
__asm__ volatile ("flushi %0" :: "r" (i));
}
/*
* For an unaligned flush request, we've got one more line left.
* Note that this is dependent on NIOS2_ICACHE_LINE_SIZE to be a
* multiple of 2 (which it always is).
*/
if (((alt_u32) start) & (NIOS2_ICACHE_LINE_SIZE - 1))
{
__asm__ volatile ("flushi %0" :: "r" (i));
}
/*
* Having flushed the cache, flush any stale instructions in the
* pipeline
*/
__asm__ volatile ("flushp");
#endif /* NIOS2_ICACHE_SIZE > 0 */
}
@@ -0,0 +1,46 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "nios2.h"
#include "system.h"
#include "alt_types.h"
#include "sys/alt_cache.h"
/*
* alt_icache_flush_all() is called to flush the entire instruction cache.
*/
void alt_icache_flush_all (void)
{
#if NIOS2_ICACHE_SIZE > 0
alt_icache_flush (0, NIOS2_ICACHE_SIZE);
#endif
}
@@ -0,0 +1,106 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2009 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "system.h"
/*
* This file implements the HAL Enhanced interrupt API for Nios II processors
* with an internal interrupt controller (IIC). For most routines, this serves
* as a wrapper layer over the legacy interrupt API (which must be used with
* the IIC only).
*
* Use of the enhanced API is recommended so that application and device
* drivers are compatible with a Nios II system configured with an external
* interrupt controller (EIC), or IIC. This will afford maximum portability.
*
* If an EIC is present, the EIC device driver must provide these routines,
* because their operation will be specific to that EIC type.
*/
#ifndef NIOS2_EIC_PRESENT
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
#include "sys/alt_irq.h"
#include "priv/alt_iic_isr_register.h"
#include "priv/alt_legacy_irq.h"
/** @Function Description: This function registers an interrupt handler.
* If the function is succesful, then the requested interrupt will be enabled upon
* return. Registering a NULL handler will disable the interrupt.
* @API Type: External
* @param ic_id Ignored.
* @param irq IRQ number
* @return 0 if successful, else error (-1)
*/
int alt_ic_isr_register(alt_u32 ic_id, alt_u32 irq, alt_isr_func isr,
void *isr_context, void *flags)
{
return alt_iic_isr_register(ic_id, irq, isr, isr_context, flags);
}
/** @Function Description: This function enables a single interrupt.
* @API Type: External
* @param ic_id Ignored.
* @param irq IRQ number
* @return 0 if successful, else error (-1)
*/
int alt_ic_irq_enable (alt_u32 ic_id, alt_u32 irq)
{
return alt_irq_enable(irq);
}
/** @Function Description: This function disables a single interrupt.
* @API Type: External
* @param ic_id Ignored.
* @param irq IRQ number
* @return 0 if successful, else error (-1)
*/
int alt_ic_irq_disable(alt_u32 ic_id, alt_u32 irq)
{
return alt_irq_disable(irq);
}
/** @Function Description: This function to determine if corresponding
* interrupt is enabled.
* @API Type: External
* @param ic_id Ignored.
* @param irq IRQ number
* @return Zero if corresponding interrupt is disabled and
* non-zero otherwise.
*/
alt_u32 alt_ic_irq_enabled(alt_u32 ic_id, alt_u32 irq)
{
alt_u32 irq_enabled;
NIOS2_READ_IENABLE(irq_enabled);
return (irq_enabled & (1 << irq)) ? 1: 0;
}
#endif /* ALT_ENHANCED_INTERRUPT_API_PRESENT */
#endif /* NIOS2_EIC_PRESENT */
@@ -0,0 +1,104 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2009 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <errno.h>
#include "system.h"
/*
* Provides an interrupt registry mechanism for the any CPUs internal interrupt
* controller (IIC) when the enhanced interrupt API is active.
*/
#ifndef ALT_CPU_EIC_PRESENT
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
#include "alt_types.h"
#include "sys/alt_irq.h"
#include "priv/alt_iic_isr_register.h"
/*
* The header, alt_irq_entry.h, contains the exception entry point, and is
* provided by the processor component. It is included here, so that the code
* will be added to the executable only if alt_irq_register() is present, i.e.
* if no interrupts are registered - there's no need to provide any
* interrupt handling.
*/
#include "sys/alt_irq_entry.h"
/*
* The header, alt_irq_table.h contains a table describing which function
* handles each interrupt.
*/
#include "priv/alt_irq_table.h"
/** @Function Description: This function registers an interrupt handler.
* If the function is succesful, then the requested interrupt will be enabled
* upon return. Registering a NULL handler will disable the interrupt.
*
* @API Type: External
* @param ic_id Interrupt controller ID
* @param irq IRQ ID number
* @param isr Pointer to interrupt service routine
* @param isr_context Opaque pointer passed to ISR
* @param flags
* @return 0 if successful, else error (-1)
*/
int alt_iic_isr_register(alt_u32 ic_id, alt_u32 irq, alt_isr_func isr,
void *isr_context, void *flags)
{
int rc = -EINVAL;
int id = irq; /* IRQ interpreted as the interrupt ID. */
alt_irq_context status;
if (id < ALT_NIRQ)
{
/*
* interrupts are disabled while the handler tables are updated to ensure
* that an interrupt doesn't occur while the tables are in an inconsistant
* state.
*/
status = alt_irq_disable_all();
alt_irq[id].handler = isr;
alt_irq[id].context = isr_context;
rc = (isr) ? alt_ic_irq_enable(ic_id, id) : alt_ic_irq_disable(ic_id, id);
alt_irq_enable_all(status);
}
return rc;
}
#endif /* ALT_ENHANCED_INTERRUPT_API_PRESENT */
#endif /* ALT_CPU_EIC_PRESENT */
@@ -0,0 +1,206 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_exceptions.h"
#include "nios2.h"
#include "alt_types.h"
#include "system.h"
/*
* This file implements support for calling user-registered handlers for
* instruction-generated exceptions. This handler could also be reached
* in the event of a spurious interrupt.
*
* The handler code is optionally enabled through the "Enable
* Instruction-related Exception API" HAL BSP setting, which will
* define the macro below.
*/
#ifdef ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API
/* Function pointer to exception callback routine */
alt_exception_result (*alt_instruction_exception_handler)
(alt_exception_cause, alt_u32, alt_u32) = 0x0;
/* Link entry routine to .exceptions section */
int alt_instruction_exception_entry (alt_u32 exception_pc)
__attribute__ ((section (".exceptions")));
/*
* This is the entry point for instruction-generated exceptions handling.
* This routine will be called by alt_exceptions_entry.S, after it determines
* that an exception could not be handled by handlers that preceed that
* of instruction-generated exceptions (such as interrupts).
*
* For this to function properly, you must register an exception handler
* using alt_instruction_exception_register(). This routine will call
* that handler if it has been registered. Absent a handler, it will
* break break or hang as discussed below.
*/
int
alt_instruction_exception_entry (alt_u32 exception_pc)
{
alt_u32 cause, badaddr;
/*
* If the processor hardware has the optional EXCEPTIONS & BADADDR registers,
* read them and pass their content to the user handler. These are always
* present if the MMU or MPU is enabled, and optionally for other advanced
* exception types via the "Extra exceptions information" setting in the
* processor (hardware) configuration.
*
* If these registers are not present, the cause field will be set to
* NIOS2_EXCEPTION_CAUSE_NOT_PRESENT. Your handling routine should
* check the validity of the cause argument before proceeding.
*/
#ifdef NIOS2_HAS_EXTRA_EXCEPTION_INFO
/* Get exception cause & "badaddr" */
NIOS2_READ_EXCEPTION(cause);
cause = ( (cause & NIOS2_EXCEPTION_REG_CAUSE_MASK) >>
NIOS2_EXCEPTION_REG_CAUSE_OFST );
NIOS2_READ_BADADDR(badaddr);
#else
cause = NIOS2_EXCEPTION_CAUSE_NOT_PRESENT;
badaddr = 0;
#endif /* NIOS2_HAS_EXTRA_EXCEPTION_INFO */
if(alt_instruction_exception_handler) {
/*
* Call handler. Its return value indicates whether the exception-causing
* instruction should be re-issued. The code that called us,
* alt_eceptions_entry.S, will look at this value and adjust the ea
* register as necessary
*/
return alt_instruction_exception_handler(cause, exception_pc, badaddr);
}
/*
* We got here because an instruction-generated exception occured, but no
* handler is present. We do not presume to know how to handle it. If the
* debugger is present, break, otherwise hang.
*
* If you've reached here in the debugger, consider examining the
* EXCEPTIONS register cause bit-field, which was read into the 'cause'
* variable above, and compare it against the exceptions-type enumeration
* in alt_exceptions.h. This register is availabe if the MMU or MPU is
* present, or if the "Extra exceptions information" hardware option is
* selected.
*
* If you get here then one of the following could have happened:
*
* - An instruction-generated exception occured, and the processor
* does not have the extra exceptions feature enabled, or you
* have not registered a handler using
* alt_instruction_exception_register()
*
* Some examples of instruction-generated exceptions and why they
* might occur:
*
* - Your program could have been compiled for a full-featured
* Nios II core, but it is running on a smaller core, and
* instruction emulation has been disabled by defining
* ALT_NO_INSTRUCTION_EMULATION.
*
* You can work around the problem by re-enabling instruction
* emulation, or you can figure out why your program is being
* compiled for a system other than the one that it is running on.
*
* - Your program has executed a trap instruction, but has not
* implemented a handler for this instruction.
*
* - Your program has executed an illegal instruction (one which is
* not defined in the instruction set).
*
* - Your processor includes an MMU or MPU, and you have enabled it
* before registering an exception handler to service exceptions it
* generates.
*
* The problem could also be hardware related:
* - If your hardware is broken and is generating spurious interrupts
* (a peripheral which negates its interrupt output before its
* interrupt handler has been executed will cause spurious interrupts)
*/
else {
#ifdef NIOS2_HAS_DEBUG_STUB
NIOS2_BREAK();
#else
while(1)
;
#endif /* NIOS2_HAS_DEBUG_STUB */
}
/* We should not get here. Remove compiler warning. */
return NIOS2_EXCEPTION_RETURN_REISSUE_INST;
}
#endif /* ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API */
/*
* This routine indicates whether a particular exception cause will have
* set a valid address into the BADADDR register, which is included
* in the arguments to a user-registered instruction-generated exception
* handler. Many exception types do not set valid contents in BADADDR;
* this is a convenience routine to easily test the validity of that
* argument in your handler.
*
* Note that this routine will return false (0) for causes
* NIOS2_EXCEPTION_TLB_MISS and NIOS2_EXCEPTION_ECC_TLB_ERR.
* You must read the TLBMISC.D field to determine if BADADDR
* is valid for these (valid if TLBMISC.D = 1).
*
* Arguments:
* cause: The 5-bit exception cause field of the EXCEPTIONS register,
* shifted to the LSB position. You may pass the 'cause' argument
* in a handler you registered directy to this routine.
*
* Return: 1: BADADDR (bad_addr argument to handler) is valid
* 0: BADADDR is not valid
*/
int
alt_exception_cause_generated_bad_addr(alt_exception_cause cause)
{
switch (cause) {
case NIOS2_EXCEPTION_SUPERVISOR_ONLY_DATA_ADDR:
case NIOS2_EXCEPTION_MISALIGNED_DATA_ADDR:
case NIOS2_EXCEPTION_MISALIGNED_TARGET_PC:
case NIOS2_EXCEPTION_TLB_READ_PERM_VIOLATION:
case NIOS2_EXCEPTION_TLB_WRITE_PERM_VIOLATION:
case NIOS2_EXCEPTION_MPU_DATA_REGION_VIOLATION:
case NIOS2_EXCEPTION_ECC_DATA_ERR:
return 1;
case NIOS2_EXCEPTION_TLB_MISS:
case NIOS2_EXCEPTION_ECC_TLB_ERR:
return 0;
default:
return 0;
}
}
@@ -0,0 +1,82 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_exceptions.h"
#include "alt_types.h"
#include "system.h"
/*
* This file implements support for calling user-registered handlers for
* instruction-generated exceptions.
*
* The registry API is optionally enabled through the "Enable
* Instruction-related Exception API" HAL BSP setting, which will
* define the macro below.
*/
#ifdef ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API
/*
* The header, alt_exception_handler_registry.h contains a struct describing
* the registered exception handler
*/
#include "priv/alt_exception_handler_registry.h"
/*
* Pull in the exception entry assembly code. This will not be linked in
* unless this object is linked into the executable (i.e. only if
* alt_instruction_exception_register() is called).
*/
__asm__( "\n\t.globl alt_exception" );
/*
* alt_instruction_exception_register() is called to register a handler to
* service instruction-generated exceptions that are not handled by the
* default exception handler code (interrupts, and optionally unimplemented
* instructions and traps).
*
* Passing null (0x0) in the handler argument will disable a previously-
* registered handler.
*
* Note that if no handler is registered, exceptions that are not processed
* using the built-in handler (interrupts, and optionally unimplemented
* instructions and traps) are treated as unknown exceptions, resulting
* in either a break or an infinite loop.
*/
void alt_instruction_exception_register (
alt_exception_result (*exception_handler)(
alt_exception_cause cause,
alt_u32 exception_pc,
alt_u32 bad_addr) )
{
alt_instruction_exception_handler = exception_handler;
}
#endif /* ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API */
@@ -0,0 +1,98 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "sys/alt_dev.h"
#include "priv/alt_file.h"
/*
* alt_open_fd() is similar to open() in that it is used to obtain a file
* descriptor for the file named "name". The "flags" and "mode" arguments are
* identical to the "flags" and "mode" arguments of open().
*
* The distinction between the two functions is that the file descriptor
* structure to use is passed in as an argument, rather than allocated from the
* list of free file descriptors.
*
* This is used by alt_io_redirect() to redirect the stdin, stdout and stderr
* file descriptors to point to new devices.
*
* If the device can not be succesfully opened, then the input file descriptor
* remains unchanged.
*/
static void alt_open_fd(alt_fd* fd, const char* name, int flags, int mode)
{
int old;
old = open (name, flags, mode);
if (old >= 0)
{
fd->dev = alt_fd_list[old].dev;
fd->priv = alt_fd_list[old].priv;
fd->fd_flags = alt_fd_list[old].fd_flags;
alt_release_fd (old);
}
}
/*
* alt_io_redirect() is called once the device/filesystem lists have been
* initialised, but before main(). Its function is to redirect standard in,
* standard out and standard error so that they point to the devices selected by
* the user (as defined in system.h).
*
* Prior to the call to this function, io is directed towards /dev/null. If
* i/o can not be redirected to the requested device, for example if the device
* does not exist, then it remains directed at /dev/null.
*/
void alt_io_redirect(const char* stdout_dev,
const char* stdin_dev,
const char* stderr_dev)
{
/* Redirect the channels */
alt_open_fd (&alt_fd_list[STDOUT_FILENO], stdout_dev, O_WRONLY, 0777);
alt_open_fd (&alt_fd_list[STDIN_FILENO], stdin_dev, O_RDONLY, 0777);
alt_open_fd (&alt_fd_list[STDERR_FILENO], stderr_dev, O_WRONLY, 0777);
}
@@ -0,0 +1,170 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include "sys/ioctl.h"
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "priv/alt_file.h"
#include "os/alt_syscall.h"
/*
* The ioctl() system call is provided so that application code can manipulate
* the i/o capabilities of a device in device specific ways. This is identical
* to the standard posix ioctl() function.
*
* In general this implementation simply vectors ioctl requests to the
* apropriate drivers ioctl function (as registered in the drivers alt_dev
* structure).
*
* However in the case of devices (as oposed to filesystem), the TIOCEXCL and
* TIOCNXCL requests are handled without reference to the driver. These
* requests are used to lock/release a device for exclusive access.
*
* Handling these requests centrally eases the task of device driver
* development.
*
* ALT_IOCTL is mapped onto the ioctl() system call in alt_syscall.h
*/
#ifdef ALT_USE_DIRECT_DRIVERS
#include "system.h"
#include "sys/alt_driver.h"
/*
* Provide minimal version that calls ioctl routine of provided stdio devices.
*/
int ALT_IOCTL (int file, int req, void* arg)
{
#ifdef ALT_STDIN_PRESENT
ALT_DRIVER_IOCTL_EXTERNS(ALT_STDIN_DEV);
#endif
#ifdef ALT_STDOUT_PRESENT
ALT_DRIVER_IOCTL_EXTERNS(ALT_STDOUT_DEV);
#endif
#ifdef ALT_STDERR_PRESENT
ALT_DRIVER_IOCTL_EXTERNS(ALT_STDERR_DEV);
#endif
#if !defined(ALT_STDIN_PRESENT) && !defined(ALT_STDOUT_PRESENT) && !defined(ALT_STDERR_PRESENT)
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(ioctl);
#endif
switch (file) {
#ifdef ALT_STDIN_PRESENT
case 0: /* stdin file descriptor */
return ALT_DRIVER_IOCTL(ALT_STDIN_DEV, req, arg);
#endif /* ALT_STDIN_PRESENT */
#ifdef ALT_STDOUT_PRESENT
case 1: /* stdout file descriptor */
return ALT_DRIVER_IOCTL(ALT_STDOUT_DEV, req, arg);
#endif /* ALT_STDOUT_PRESENT */
#ifdef ALT_STDERR_PRESENT
case 2: /* stderr file descriptor */
return ALT_DRIVER_IOCTL(ALT_STDERR_DEV, req, arg);
#endif /* ALT_STDERR_PRESENT */
default:
ALT_ERRNO = EBADFD;
return -1;
}
}
#else /* !ALT_USE_DIRECT_DRIVERS */
int ALT_IOCTL (int file, int req, void* arg)
{
alt_fd* fd;
int rc;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (file < 0) ? NULL : &alt_fd_list[file];
if (fd)
{
/*
* In the case of device drivers (not file systems) handle the TIOCEXCL
* and TIOCNXCL requests as special cases.
*/
if (fd->fd_flags & ALT_FD_DEV)
{
if (req == TIOCEXCL)
{
rc = alt_fd_lock (fd);
goto ioctl_done;
}
else if (req == TIOCNXCL)
{
rc = alt_fd_unlock (fd);
goto ioctl_done;
}
}
/*
* If the driver provides an ioctl() function, call that to handle the
* request, otherwise set the return code to indicate that the request
* could not be processed.
*/
if (fd->dev->ioctl)
{
rc = fd->dev->ioctl(fd, req, arg);
}
else
{
rc = -ENOTTY;
}
}
else
{
rc = -EBADFD;
}
ioctl_done:
if (rc < 0)
{
ALT_ERRNO = -rc;
}
return rc;
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,108 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "system.h"
/*
* This is the interrupt exception entry point code, which saves all the
* registers and calls the interrupt handler. It should be pulled in using
* a .globl from alt_irq_register.c. This scheme is used so that if an
* interrupt is never registered, then this code will not appear in the
* generated executable, thereby improving code footprint.
*/
/*
* Explicitly allow the use of r1 (the assembler temporary register)
* within this code. This register is normally reserved for the use of
* the compiler.
*/
.set noat
/*
* Pull in the exception handler register save code.
*/
.globl alt_exception
.globl alt_irq_entry
.section .exceptions.entry.label, "xa"
alt_irq_entry:
/*
* Section .exceptions.entry is in alt_exception_entry.S
* This saves all the caller saved registers and reads estatus into r5
*/
.section .exceptions.irqtest, "xa"
#ifdef ALT_CI_INTERRUPT_VECTOR_N
/*
* Use the interrupt vector custom instruction if present to accelerate
* this code.
* If the interrupt vector custom instruction returns a negative
* value, there are no interrupts active (estatus.pie is 0
* or ipending is 0) so assume it is a software exception.
*/
custom ALT_CI_INTERRUPT_VECTOR_N, r4, r0, r0
blt r4, r0, .Lnot_irq
#else
/*
* Test to see if the exception was a software exception or caused
* by an external interrupt, and vector accordingly.
*/
rdctl r4, ipending
andi r2, r5, 1
beq r2, zero, .Lnot_irq
beq r4, zero, .Lnot_irq
#endif /* ALT_CI_INTERRUPT_VECTOR_N */
.section .exceptions.irqhandler, "xa"
/*
* Now that all necessary registers have been preserved, call
* alt_irq_handler() to process the interrupts.
*/
call alt_irq_handler
.section .exceptions.irqreturn, "xa"
br .Lexception_exit
.section .exceptions.notirq.label, "xa"
.Lnot_irq:
/*
* Section .exceptions.exit is in alt_exception_entry.S
* This restores all the caller saved registers
*/
.section .exceptions.exit.label
.Lexception_exit:
@@ -0,0 +1,169 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2009 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <errno.h>
#include "system.h"
/*
* This interrupt handler only works with an internal interrupt controller
* (IIC). Processors with an external interrupt controller (EIC) use an
* implementation provided by an EIC driver.
*/
#ifndef ALT_CPU_EIC_PRESENT
#include "sys/alt_irq.h"
#include "os/alt_hooks.h"
#include "alt_types.h"
/*
* A table describing each interrupt handler. The index into the array is the
* interrupt id associated with the handler.
*
* When an interrupt occurs, the associated handler is called with
* the argument stored in the context member.
*/
struct ALT_IRQ_HANDLER
{
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
void (*handler)(void*);
#else
void (*handler)(void*, alt_u32);
#endif
void *context;
} alt_irq[ALT_NIRQ];
/*
* alt_irq_handler() is called by the interrupt exception handler in order to
* process any outstanding interrupts.
*
* It is defined here since it is linked in using weak linkage.
* This means that if there is never a call to alt_irq_register() (above) then
* this function will not get linked in to the executable. This is acceptable
* since if no handler is ever registered, then an interrupt can never occur.
*
* If Nios II interrupt vector custom instruction exists, use it to accelerate
* the dispatch of interrupt handlers. The Nios II interrupt vector custom
* instruction is present if the macro ALT_CI_INTERRUPT_VECTOR defined.
*/
void alt_irq_handler (void) __attribute__ ((section (".exceptions")));
void alt_irq_handler (void)
{
#ifdef ALT_CI_INTERRUPT_VECTOR
alt_32 offset;
char* alt_irq_base = (char*)alt_irq;
#else
alt_u32 active;
alt_u32 mask;
alt_u32 i;
#endif /* ALT_CI_INTERRUPT_VECTOR */
/*
* Notify the operating system that we are at interrupt level.
*/
ALT_OS_INT_ENTER();
#ifdef ALT_CI_INTERRUPT_VECTOR
/*
* Call the interrupt vector custom instruction using the
* ALT_CI_INTERRUPT_VECTOR macro.
* It returns the offset into the vector table of the lowest-valued pending
* interrupt (corresponds to highest priority) or a negative value if none.
* The custom instruction assumes that each table entry is eight bytes.
*/
while ((offset = ALT_CI_INTERRUPT_VECTOR) >= 0) {
struct ALT_IRQ_HANDLER* handler_entry =
(struct ALT_IRQ_HANDLER*)(alt_irq_base + offset);
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
handler_entry->handler(handler_entry->context);
#else
handler_entry->handler(handler_entry->context, offset >> 3);
#endif
}
#else /* ALT_CI_INTERRUPT_VECTOR */
/*
* Obtain from the interrupt controller a bit list of pending interrupts,
* and then process the highest priority interrupt. This process loops,
* loading the active interrupt list on each pass until alt_irq_pending()
* return zero.
*
* The maximum interrupt latency for the highest priority interrupt is
* reduced by finding out which interrupts are pending as late as possible.
* Consider the case where the high priority interupt is asserted during
* the interrupt entry sequence for a lower priority interrupt to see why
* this is the case.
*/
active = alt_irq_pending ();
do
{
i = 0;
mask = 1;
/*
* Test each bit in turn looking for an active interrupt. Once one is
* found, the interrupt handler asigned by a call to alt_irq_register() is
* called to clear the interrupt condition.
*/
do
{
if (active & mask)
{
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
alt_irq[i].handler(alt_irq[i].context);
#else
alt_irq[i].handler(alt_irq[i].context, i);
#endif
break;
}
mask <<= 1;
i++;
} while (1);
active = alt_irq_pending ();
} while (active);
#endif /* ALT_CI_INTERRUPT_VECTOR */
/*
* Notify the operating system that interrupt processing is complete.
*/
ALT_OS_INT_EXIT();
}
#endif /* ALT_CPU_EIC_PRESENT */
@@ -0,0 +1,102 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2009 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <errno.h>
#include "system.h"
/*
* This interrupt registry mechanism works with the Nios II internal interrupt
* controller (IIC) only. Systems with an external interrupt controller (EIC),
* or those with the IIC who are using the enhanced interrupt API will
* utilize the alt_ic_isr_register() routine to register an interrupt.
*/
#ifndef NIOS2_EIC_PRESENT
#include "sys/alt_irq.h"
#include "priv/alt_legacy_irq.h"
#include "os/alt_hooks.h"
#include "alt_types.h"
/*
* The header, alt_irq_entry.h, contains the exception entry point, and is
* provided by the processor component. It is included here, so that the code
* will be added to the executable only if alt_irq_register() is present, i.e.
* if no interrupts are registered - there's no need to provide any
* interrupt handling.
*/
#include "sys/alt_irq_entry.h"
/*
* The header, alt_irq_table.h contains a table describing which function
* handles each interrupt.
*/
#include "priv/alt_irq_table.h"
/*
* alt_irq_handler() is called to register an interrupt handler. If the
* function is succesful, then the requested interrupt will be enabled upon
* return. Registering a NULL handler will disable the interrupt.
*
* The return value is 0 if the interrupt handler was registered and the
* interrupt was enabled, otherwise it is negative.
*/
int alt_irq_register (alt_u32 id,
void* context,
alt_isr_func handler)
{
int rc = -EINVAL;
alt_irq_context status;
if (id < ALT_NIRQ)
{
/*
* interrupts are disabled while the handler tables are updated to ensure
* that an interrupt doesn't occur while the tables are in an inconsistant
* state.
*/
status = alt_irq_disable_all ();
alt_irq[id].handler = handler;
alt_irq[id].context = context;
rc = (handler) ? alt_irq_enable (id): alt_irq_disable (id);
alt_irq_enable_all(status);
}
return rc;
}
#endif /* NIOS2_EIC_PRESENT */
@@ -0,0 +1,47 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "alt_types.h"
#include "system.h"
/*
* These global variables are used to save the current list of enabled
* interrupts. See alt_irq.h for further details.
*/
volatile alt_u32 alt_irq_active = 0;
#ifndef ALT_EXCEPTION_STACK
volatile alt_u32 alt_priority_mask = (alt_u32) -1;
#endif
@@ -0,0 +1,125 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stddef.h>
#include <sys/stat.h>
#include "sys/alt_dev.h"
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "priv/alt_file.h"
#include "os/alt_syscall.h"
#ifdef ALT_USE_DIRECT_DRIVERS
#include "system.h"
/*
* Provide minimal version that just describes all file descriptors
* as tty devices for provided stdio devices.
*/
int ALT_ISATTY (int file)
{
switch (file) {
#ifdef ALT_STDIN_PRESENT
case 0: /* stdin file descriptor */
#endif /* ALT_STDIN_PRESENT */
#ifdef ALT_STDOUT_PRESENT
case 1: /* stdout file descriptor */
#endif /* ALT_STDOUT_PRESENT */
#ifdef ALT_STDERR_PRESENT
case 2: /* stderr file descriptor */
#endif /* ALT_STDERR_PRESENT */
return 1;
default:
return 0;
}
#if !defined(ALT_STDIN_PRESENT) && !defined(ALT_STDOUT_PRESENT) && !defined(ALT_STDERR_PRESENT)
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(isatty);
#endif
}
#else /* !ALT_USE_DIRECT_DRIVERS */
/*
* isatty() can be used to determine whether the input file descriptor "file"
* refers to a terminal device or not. If it is a terminal device then the
* return value is one, otherwise it is zero.
*
* ALT_ISATTY is mapped onto the isatty() system call in alt_syscall.h
*/
int ALT_ISATTY (int file)
{
alt_fd* fd;
struct stat stat;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (file < 0) ? NULL : &alt_fd_list[file];
if (fd)
{
/*
* If a device driver does not provide an fstat() function, then it is
* treated as a terminal device by default.
*/
if (!fd->dev->fstat)
{
return 1;
}
/*
* If a driver does provide an implementation of the fstat() function, then
* this is called so that the device can identify itself.
*/
else
{
fstat (file, &stat);
return (stat.st_mode == _IFCHR) ? 1 : 0;
}
}
else
{
ALT_ERRNO = EBADFD;
return 0;
}
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,121 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <signal.h>
#include <unistd.h>
#include "sys/alt_errno.h"
#include "os/alt_syscall.h"
/*
* kill() is used by newlib in order to send signals to processes. Since there
* is only a single process in the HAL, the only valid values for pid are
* either the current process id, or the broadcast values, i.e. pid must be
* less than or equal to zero.
*
* ALT_KILL is mapped onto the kill() system call in alt_syscall.h
*/
int ALT_KILL (int pid, int sig)
{
int status = 0;
if (pid <= 0)
{
switch (sig)
{
case 0:
/* The null signal is used to check that a pid is valid. */
break;
case SIGABRT:
case SIGALRM:
case SIGFPE:
case SIGILL:
case SIGKILL:
case SIGPIPE:
case SIGQUIT:
case SIGSEGV:
case SIGTERM:
case SIGUSR1:
case SIGUSR2:
case SIGBUS:
case SIGPOLL:
case SIGPROF:
case SIGSYS:
case SIGTRAP:
case SIGVTALRM:
case SIGXCPU:
case SIGXFSZ:
/*
* The Posix standard defines the default behaviour for all these signals
* as being eqivalent to a call to _exit(). No mechanism is provided to
* change this behaviour.
*/
_exit(0);
case SIGCHLD:
case SIGURG:
/*
* The Posix standard defines these signals to be ignored by default. No
* mechanism is provided to change this behaviour.
*/
break;
default:
/* Tried to send an unsupported signal */
status = EINVAL;
}
}
else if (pid > 0)
{
/* Attempted to signal a non-existant process */
status = ESRCH;
}
if (status)
{
ALT_ERRNO = status;
return -1;
}
return 0;
}
@@ -0,0 +1,56 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_warning.h"
#include "sys/alt_errno.h"
#include "os/alt_syscall.h"
/*
* link() is used by newlib to create a new link to an existing file. This is
* unsupported in the HAL environment. However a "do-nothing" implementation
* is still provied for newlib compatability.
*
* ALT_LINK is mapped onto the link() system call in alt_syscall.h
*/
int ALT_LINK ( char *existing, char *new)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(link);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
@@ -0,0 +1,99 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_load.h"
#include "sys/alt_cache.h"
/*
* Linker defined symbols.
These used to be
* extern alt_u32 __flash_rwdata_start;
* extern alt_u32 __ram_rwdata_start;
* extern alt_u32 __ram_rwdata_end;
* but that results in a fatal error when compiling -mgpopt=global
* because gcc assumes they are normal C variables in .sdata
* and therefore addressable from gp using a 16-bit offset,
* when in fact they are special values defined by linker.x
* and located nowhere near .sdata.
* Specifying __attribute__((section(".data"))) will force these
* in .data. (CASE:258384.)
*/
extern alt_u32 __flash_rwdata_start __attribute__((section(".data")));
extern alt_u32 __ram_rwdata_start __attribute__((section(".data")));
extern alt_u32 __ram_rwdata_end __attribute__((section(".data")));
extern alt_u32 __flash_rodata_start __attribute__((section(".data")));
extern alt_u32 __ram_rodata_start __attribute__((section(".data")));
extern alt_u32 __ram_rodata_end __attribute__((section(".data")));
extern alt_u32 __flash_exceptions_start __attribute__((section(".data")));
extern alt_u32 __ram_exceptions_start __attribute__((section(".data")));
extern alt_u32 __ram_exceptions_end __attribute__((section(".data")));
/*
* alt_load() is called when the code is executing from flash. In this case
* there is no bootloader, so this application is responsible for loading to
* RAM any sections that are required.
*/
void alt_load (void)
{
/*
* Copy the .rwdata section.
*/
alt_load_section (&__flash_rwdata_start,
&__ram_rwdata_start,
&__ram_rwdata_end);
/*
* Copy the exception handler.
*/
alt_load_section (&__flash_exceptions_start,
&__ram_exceptions_start,
&__ram_exceptions_end);
/*
* Copy the .rodata section.
*/
alt_load_section (&__flash_rodata_start,
&__ram_rodata_start,
&__ram_rodata_end);
/*
* Now ensure that the caches are in synch.
*/
alt_dcache_flush_all();
alt_icache_flush_all();
}
@@ -0,0 +1,60 @@
/* alt_log_macro.S
*
* Implements the function tx_log_str, called by the assembly macro
* ALT_LOG_PUTS(). The macro will be empty when logging is turned off,
* and this function will not be compiled. When logging is on,
* this function is used to print out the strings defined in the beginning
* of alt_log_printf.c, using port information taken from system.h and
* alt_log_printf.h.
*
* This routine only handles strings, and sends a character into the defined
* output device's output buffer when the device is ready. It's intended for
* debugging purposes, where messages can be set to print out at certain
* points in the boot code to indicate the progress of the program.
*
*/
#ifndef __ALT_LOG_MACROS__
#define __ALT_LOG_MACROS__
/* define this flag to skip assembly-incompatible parts
* of various include files. */
#define ALT_ASM_SRC
#ifdef ALT_LOG_ENABLE // only compile this function if this flag is defined.
#include "system.h"
#include "sys/alt_log_printf.h"
.global tx_log_str
tx_log_str:
/* load base uart / jtag uart address into r6 */
movhi r6, %hiadj(ALT_LOG_PORT_BASE)
addi r6, r6, %lo(ALT_LOG_PORT_BASE)
tx_next_char:
/* if pointer points to null, return
* r4 is the pointer to the str to be printed, set by ALT_LOG_PUTS */
ldb r7, (r4)
beq r0, r7, end_tx
/* check device transmit ready */
wait_tx_ready_loop:
ldwio r8, ALT_LOG_PRINT_REG_OFFSET(r6)
/*UART, ALT_LOG_PRINT_MSK == 0x40
JTAG UART, ALT_LOG_PRINT_MSK == 0xFFFF0000 */
andhi r5, r8, %hi(ALT_LOG_PRINT_MSK)
andi r8, r8, %lo(ALT_LOG_PRINT_MSK)
or r5, r5, r8
beq r5, r0, wait_tx_ready_loop
/* write char */
stwio r7, ALT_LOG_PRINT_TXDATA_REG_OFFSET (r6)
/* advance string pointer */
addi r4, r4, 1
br tx_next_char
end_tx:
ret
#endif
#endif /* __ALT_LOG_MACROS__ */
@@ -0,0 +1,479 @@
/* alt_log_printf.c
*
* This file implements the various C functions used for the
* alt_log logging/debugging print functions. The functions
* sit as is here - the job of hiding them from the compiler
* if logging is disabled is accomplished in the .h file.
*
* All the global variables for alt_log are defined here.
* These include the various flags that turn on additional
* logging options; the strings for assembly printing; and
* other globals needed by different logging options.
*
* There are 4 functions that handle the actual printing:
* alt_log_txchar: Actual function that puts 1 char to UART/JTAG UART.
* alt_log_repchar: Calls alt_log_txchar 'n' times - used by
* alt_log_private_printf for formatting.
* alt_log_private_printf:
* Stripped down implementation of printf - no floats.
* alt_log_printf_proc:
* Wrapper function for private_printf.
*
* The rest of the functions are called by the macros which
* were called by code in the other components. Each function
* is preceded by a comment, about which file it gets called
* in, and what its purpose is.
*
* author: gkwan
*/
/* skip all code if enable is off */
#ifdef ALT_LOG_ENABLE
#include <system.h>
#include <stdarg.h>
#include <string.h>
#ifdef __ALTERA_AVALON_JTAG_UART
#include "altera_avalon_jtag_uart.h"
#include <altera_avalon_jtag_uart_regs.h>
#endif
#include "sys/alt_log_printf.h"
/* strings for assembly puts */
char alt_log_msg_bss[] = "[crt0.S] Clearing BSS \r\n";;
char alt_log_msg_alt_main[] = "[crt0.S] Calling alt_main.\r\n";
char alt_log_msg_stackpointer[] \
= "[crt0.S] Setting up stack and global pointers.\r\n";
char alt_log_msg_cache[] = "[crt0.S] Inst & Data Cache Initialized.\r\n";
/* char array allocation for alt_write */
char alt_log_write_buf[ALT_LOG_WRITE_ECHO_LEN+2];
/* global variables for all 'on' flags */
/*
* CASE:368514 - The boot message flag is linked into the sdata section
* because if it is zero, it would otherwise be placed in the bss section.
* alt_log examines this variable before the BSS is cleared in the boot-up
* process.
*/
volatile alt_u32 alt_log_boot_on_flag \
__attribute__ ((section (".sdata"))) = ALT_LOG_BOOT_ON_FLAG_SETTING;
volatile alt_u8 alt_log_write_on_flag = ALT_LOG_WRITE_ON_FLAG_SETTING;
volatile alt_u8 alt_log_sys_clk_on_flag = ALT_LOG_SYS_CLK_ON_FLAG_SETTING;
volatile alt_u8 alt_log_jtag_uart_alarm_on_flag = \
ALT_LOG_JTAG_UART_ALARM_ON_FLAG_SETTING;
volatile alt_u8 alt_log_jtag_uart_isr_on_flag = \
ALT_LOG_JTAG_UART_ISR_ON_FLAG_SETTING;
volatile alt_u8 alt_log_jtag_uart_startup_info_on_flag = \
ALT_LOG_JTAG_UART_STARTUP_INFO_ON_FLAG_SETTING;
/* Global alarm object for recurrent JTAG UART status printing */
alt_alarm alt_log_jtag_uart_alarm_1;
/* Global ints for system clock printing and count */
volatile int alt_log_sys_clk_count;
volatile int alt_system_clock_in_sec;
/* enum used by alt_log_private_printf */
enum
{
pfState_chars,
pfState_firstFmtChar,
pfState_otherFmtChar
};
/* Function to put one char onto the UART/JTAG UART txdata register. */
void alt_log_txchar(int c,char *base)
{
/* Wait until the device is ready for a character */
while((ALT_LOG_PRINT_REG_RD(base) & ALT_LOG_PRINT_MSK) == 0)
;
/* And pop the character into the register */
ALT_LOG_PRINT_TXDATA_WR(base,c);
}
/* Called by alt_log_private_printf to print out characters repeatedly */
void alt_log_repchar(char c,int r,int base)
{
while(r-- > 0)
alt_log_txchar(c,(char*) base);
}
/* Stripped down printf function */
void alt_log_private_printf(const char *fmt,int base,va_list args)
{
const char *w;
char c;
int state;
int fmtLeadingZero = 0; /* init these all to 0 for -W warnings. */
int fmtLong = 0;
int fmtBeforeDecimal = 0;
int fmtAfterDecimal = 0;
int fmtBase = 0;
int fmtSigned = 0;
int fmtCase = 0; /* For hex format, if 1, A-F, else a-f. */
w = fmt;
state = pfState_chars;
while(0 != (c = *w++))
{
switch(state)
{
case pfState_chars:
if(c == '%')
{
fmtLeadingZero = 0;
fmtLong = 0;
fmtBase = 10;
fmtSigned = 1;
fmtCase = 0; /* Only %X sets this. */
fmtBeforeDecimal = -1;
fmtAfterDecimal = -1;
state = pfState_firstFmtChar;
}
else
{
alt_log_txchar(c,(char*)base);
}
break;
case pfState_firstFmtChar:
if(c == '0')
{
fmtLeadingZero = 1;
state = pfState_otherFmtChar;
}
else if(c == '%')
{
alt_log_txchar(c,(char*)base);
state = pfState_chars;
}
else
{
state = pfState_otherFmtChar;
goto otherFmtChar;
}
break;
case pfState_otherFmtChar:
otherFmtChar:
if(c == '.')
{
fmtAfterDecimal = 0;
}
else if('0' <= c && c <= '9')
{
c -= '0';
if(fmtAfterDecimal < 0) /* still before decimal */
{
if(fmtBeforeDecimal < 0)
{
fmtBeforeDecimal = 0;
}
else
{
fmtBeforeDecimal *= 10;
}
fmtBeforeDecimal += c;
}
else
{
fmtAfterDecimal = (fmtAfterDecimal * 10) + c;
}
}
else if(c == 'l')
{
fmtLong = 1;
}
else /* we're up to the letter which determines type */
{
switch(c)
{
case 'd':
case 'i':
doIntegerPrint:
{
unsigned long v;
unsigned long p; /* biggest power of fmtBase */
unsigned long vShrink; /* used to count digits */
int sign;
int digitCount;
/* Get the value */
if(fmtLong)
{
if (fmtSigned)
{
v = va_arg(args,long);
}
else
{
v = va_arg(args,unsigned long);
}
}
else
{
if (fmtSigned)
{
v = va_arg(args,int);
}
else
{
v = va_arg(args,unsigned int);
}
}
/* Strip sign */
sign = 0;
/* (assumes sign bit is #31) */
if( fmtSigned && (v & (0x80000000)) )
{
v = ~v + 1;
sign = 1;
}
/* Count digits, and get largest place value */
vShrink = v;
p = 1;
digitCount = 1;
while( (vShrink = vShrink / fmtBase) > 0 )
{
digitCount++;
p *= fmtBase;
}
/* Print leading characters & sign */
fmtBeforeDecimal -= digitCount;
if(fmtLeadingZero)
{
if(sign)
{
alt_log_txchar('-',(char*)base);
fmtBeforeDecimal--;
}
alt_log_repchar('0',fmtBeforeDecimal,base);
}
else
{
if(sign)
{
fmtBeforeDecimal--;
}
alt_log_repchar(' ',fmtBeforeDecimal,base);
if(sign)
{
alt_log_txchar('-',(char*)base);
}
}
/* Print numbery parts */
while(p)
{
unsigned char d;
d = v / p;
d += '0';
if(d > '9')
{
d += (fmtCase ? 'A' : 'a') - '0' - 10;
}
alt_log_txchar(d,(char*)base);
v = v % p;
p = p / fmtBase;
}
}
state = pfState_chars;
break;
case 'u':
fmtSigned = 0;
goto doIntegerPrint;
case 'o':
fmtSigned = 0;
fmtBase = 8;
goto doIntegerPrint;
case 'x':
fmtSigned = 0;
fmtBase = 16;
goto doIntegerPrint;
case 'X':
fmtSigned = 0;
fmtBase = 16;
fmtCase = 1;
goto doIntegerPrint;
case 'c':
alt_log_repchar(' ',fmtBeforeDecimal-1,base);
alt_log_txchar(va_arg(args,int),(char*)base);
break;
case 's':
{
char *s;
s = va_arg(args,char *);
alt_log_repchar(' ',fmtBeforeDecimal-strlen(s),base);
while(*s)
alt_log_txchar(*s++,(char*)base);
}
break;
} /* switch last letter of fmt */
state=pfState_chars;
}
break;
} /* switch */
} /* while chars left */
} /* printf */
/* Main logging printf function */
int alt_log_printf_proc(const char *fmt, ... )
{
va_list args;
va_start (args, fmt);
alt_log_private_printf(fmt,ALT_LOG_PORT_BASE,args);
return (0);
}
/* Below are the functions called by different macros in various components. */
/* If the system has a JTAG_UART, include JTAG_UART debugging functions */
#ifdef __ALTERA_AVALON_JTAG_UART
/* The alarm function in altera_avalon_jtag_uart.c.
* This function, when turned on, prints out the status
* of the JTAG UART Control register, every ALT_LOG_JTAG_UART_TICKS.
* If the flag is off, the alarm should never be registered, and this
* function should never run */
alt_u32 altera_avalon_jtag_uart_report_log(void * context)
{
if (alt_log_jtag_uart_alarm_on_flag) {
altera_avalon_jtag_uart_state* dev = (altera_avalon_jtag_uart_state*) context;
const char* header="JTAG Alarm:";
alt_log_jtag_uart_print_control_reg(dev, dev->base, header);
return ALT_LOG_JTAG_UART_TICKS;
}
else
{
/* If flag is not on, return 0 to disable future alarms.
* Should never be here, alarm should not be enabled at all. */
return 0;
}
}
void alt_log_jtag_uart_print_control_reg(altera_avalon_jtag_uart_state* dev, int base, const char* header)
{
unsigned int control, space, ac, wi, ri, we, re;
control = IORD_ALTERA_AVALON_JTAG_UART_CONTROL(base);
space = (control & ALTERA_AVALON_JTAG_UART_CONTROL_WSPACE_MSK) >>
ALTERA_AVALON_JTAG_UART_CONTROL_WSPACE_OFST;
we= (control & ALTERA_AVALON_JTAG_UART_CONTROL_WE_MSK) >>
ALTERA_AVALON_JTAG_UART_CONTROL_WE_OFST;
re= (control & ALTERA_AVALON_JTAG_UART_CONTROL_RE_MSK) >>
ALTERA_AVALON_JTAG_UART_CONTROL_RE_OFST;
ri= (control & ALTERA_AVALON_JTAG_UART_CONTROL_RI_MSK) >>
ALTERA_AVALON_JTAG_UART_CONTROL_RI_OFST;
wi= (control & ALTERA_AVALON_JTAG_UART_CONTROL_WI_MSK) >>
ALTERA_AVALON_JTAG_UART_CONTROL_WI_OFST;
ac= (control & ALTERA_AVALON_JTAG_UART_CONTROL_AC_MSK) >>
ALTERA_AVALON_JTAG_UART_CONTROL_AC_OFST;
#ifdef ALTERA_AVALON_JTAG_UART_SMALL
ALT_LOG_PRINTF(
"%s HW FIFO wspace=%d AC=%d WI=%d RI=%d WE=%d RE=%d\r\n",
header,space,ac,wi,ri,we,re);
#else
ALT_LOG_PRINTF(
"%s SW CirBuf = %d, HW FIFO wspace=%d AC=%d WI=%d RI=%d WE=%d RE=%d\r\n",
header,(dev->tx_out-dev->tx_in),space,ac,wi,ri,we,re);
#endif
return;
}
/* In altera_avalon_jtag_uart.c
* Same output as the alarm function above, but this is called in the driver
* init function. Hence, it gives the status of the JTAG UART control register
* right at the initialization of the driver */
void alt_log_jtag_uart_startup_info(altera_avalon_jtag_uart_state* dev, int base)
{
const char* header="JTAG Startup Info:";
alt_log_jtag_uart_print_control_reg(dev, base, header);
return;
}
/* In altera_avalon_jtag_uart.c
* When turned on, this function will print out the status of the jtag uart
* control register every time there is a jtag uart "almost-empty" interrupt. */
void alt_log_jtag_uart_isr_proc(int base, altera_avalon_jtag_uart_state* dev)
{
if (alt_log_jtag_uart_isr_on_flag) {
const char* header="JTAG IRQ:";
alt_log_jtag_uart_print_control_reg(dev, base, header);
}
return;
}
#endif /* __ALTERA_AVALON_JTAG_UART */
/* In alt_write.c
* When the alt_log_write_on_flag is turned on, this function gets called
* every time alt_write gets called. The first
* ALT_LOG_WRITE_ECHO_LEN characters of every printf command (or any command
* that eventually calls write()) gets echoed to the alt_log output. */
void alt_log_write(const void *ptr, size_t len)
{
if (alt_log_write_on_flag) {
int temp_cnt;
int length=(ALT_LOG_WRITE_ECHO_LEN>len) ? len : ALT_LOG_WRITE_ECHO_LEN;
if (length < 2) return;
strncpy (alt_log_write_buf,ptr,length);
alt_log_write_buf[length-1]='\n';
alt_log_write_buf[length]='\r';
alt_log_write_buf[length+1]='\0';
/* Escape Ctrl-D's. If the Ctrl-D gets sent it might kill the terminal
* connection of alt_log. It will get replaced by 'D'. */
for (temp_cnt=0;temp_cnt < length; temp_cnt++) {
if (alt_log_write_buf[temp_cnt]== 0x4) {
alt_log_write_buf[temp_cnt]='D';
}
}
ALT_LOG_PRINTF("Write Echo: %s",alt_log_write_buf);
}
}
/* In altera_avalon_timer_sc
* This function prints out a system clock is alive message
* every ALT_LOG_SYS_CLK_INTERVAL (in ticks). */
void alt_log_system_clock()
{
if (alt_log_sys_clk_on_flag) {
alt_log_sys_clk_count++;
if (alt_log_sys_clk_count > ALT_LOG_SYS_CLK_INTERVAL) {
alt_log_sys_clk_count = 0;
ALT_LOG_PRINTF("System Clock On %u\r\n",alt_system_clock_in_sec++);
}
}
}
#endif
@@ -0,0 +1,117 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <unistd.h>
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "priv/alt_file.h"
#include "os/alt_syscall.h"
#ifdef ALT_USE_DIRECT_DRIVERS
off_t ALT_LSEEK (int file, off_t ptr, int dir)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(lseek);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
#else /* !ALT_USE_DIRECT_DRIVERS */
/*
* lseek() can be called to move the read/write pointer associated with the
* file descriptor "file". This function simply vectors the call to the lseek()
* function provided by the driver associated with the file descriptor.
*
* If the driver does not provide an implementation of lseek() an error is
* indicated.
*
* lseek() corresponds to the standard lseek() function.
*
* ALT_LSEEK is mapped onto the lseek() system call in alt_syscall.h
*
*/
off_t ALT_LSEEK (int file, off_t ptr, int dir)
{
alt_fd* fd;
off_t rc = 0;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (file < 0) ? NULL : &alt_fd_list[file];
if (fd)
{
/*
* If the device driver provides an implementation of the lseek() function,
* then call that to process the request.
*/
if (fd->dev->lseek)
{
rc = fd->dev->lseek(fd, ptr, dir);
}
/*
* Otherwise return an error.
*/
else
{
rc = -ENOTSUP;
}
}
else
{
rc = -EBADFD;
}
if (rc < 0)
{
ALT_ERRNO = -rc;
rc = -1;
}
return rc;
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,161 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2007 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "sys/alt_dev.h"
#include "sys/alt_sys_init.h"
#include "sys/alt_irq.h"
#include "sys/alt_dev.h"
#include "os/alt_hooks.h"
#include "priv/alt_file.h"
#include "alt_types.h"
#include "system.h"
#include "sys/alt_log_printf.h"
extern void _do_ctors(void);
extern void _do_dtors(void);
/*
* Standard arguments for main. By default, no arguments are passed to main.
* However a device driver may choose to configure these arguments by calling
* alt_set_args(). The expectation is that this facility will only be used by
* the iclient/ihost utility.
*/
int alt_argc = 0;
char** alt_argv = {NULL};
char** alt_envp = {NULL};
/*
* Prototype for the entry point to the users application.
*/
extern int main (int, char **, char **);
/*
* alt_main is the C entry point for the HAL. It is called by the assembler
* startup code in the processor specific crt0.S. It is responsible for:
* completing the C runtime configuration; configuring all the
* devices/filesystems/components in the system; and call the entry point for
* the users application, i.e. main().
*/
void alt_main (void)
{
#ifndef ALT_NO_EXIT
int result;
#endif
/* ALT LOG - please see HAL/sys/alt_log_printf.h for details */
ALT_LOG_PRINT_BOOT("[alt_main.c] Entering alt_main, calling alt_irq_init.\r\n");
/* Initialize the interrupt controller. */
alt_irq_init (NULL);
/* Initialize the operating system */
ALT_LOG_PRINT_BOOT("[alt_main.c] Done alt_irq_init, calling alt_os_init.\r\n");
ALT_OS_INIT();
/*
* Initialize the semaphore used to control access to the file descriptor
* list.
*/
ALT_LOG_PRINT_BOOT("[alt_main.c] Done OS Init, calling alt_sem_create.\r\n");
ALT_SEM_CREATE (&alt_fd_list_lock, 1);
/* Initialize the device drivers/software components. */
ALT_LOG_PRINT_BOOT("[alt_main.c] Calling alt_sys_init.\r\n");
alt_sys_init();
ALT_LOG_PRINT_BOOT("[alt_main.c] Done alt_sys_init.\r\n");
#if !defined(ALT_USE_DIRECT_DRIVERS) && (defined(ALT_STDIN_PRESENT) || defined(ALT_STDOUT_PRESENT) || defined(ALT_STDERR_PRESENT))
/*
* Redirect stdio to the apropriate devices now that the devices have
* been initialized. This is only done if the user has requested these
* devices be present (not equal to /dev/null) and if direct drivers
* aren't being used.
*/
ALT_LOG_PRINT_BOOT("[alt_main.c] Redirecting IO.\r\n");
alt_io_redirect(ALT_STDOUT, ALT_STDIN, ALT_STDERR);
#endif
#ifndef ALT_NO_C_PLUS_PLUS
/*
* Call the C++ constructors
*/
ALT_LOG_PRINT_BOOT("[alt_main.c] Calling C++ constructors.\r\n");
_do_ctors ();
#endif /* ALT_NO_C_PLUS_PLUS */
#if !defined(ALT_NO_C_PLUS_PLUS) && !defined(ALT_NO_CLEAN_EXIT) && !defined(ALT_NO_EXIT)
/*
* Set the C++ destructors to be called at system shutdown. This is only done
* if a clean exit has been requested (i.e. the exit() function has not been
* redefined as _exit()). This is in the interest of reducing code footprint,
* in that the atexit() overhead is removed when it's not needed.
*/
ALT_LOG_PRINT_BOOT("[alt_main.c] Calling atexit.\r\n");
atexit (_do_dtors);
#endif
/*
* Finally, call main(). The return code is then passed to a subsequent
* call to exit() unless the application is never supposed to exit.
*/
ALT_LOG_PRINT_BOOT("[alt_main.c] Calling main.\r\n");
#ifdef ALT_NO_EXIT
main (alt_argc, alt_argv, alt_envp);
#else
result = main (alt_argc, alt_argv, alt_envp);
close(STDOUT_FILENO);
exit (result);
#endif
ALT_LOG_PRINT_BOOT("[alt_main.c] After main - we should not be here?.\r\n");
}
@@ -0,0 +1,52 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <reent.h>
/*
* These are the empty malloc lock/unlock stubs required by newlib. These are
* used to make newlib's malloc() function thread safe. The default HAL
* configuration is single threaded, so there is nothing to do here. Note that
* this requires that malloc is never called by an interrupt service routine.
*/
void __malloc_lock ( struct _reent *_r )
{
}
/*
*
*/
void __malloc_unlock ( struct _reent *_r )
{
}
@@ -0,0 +1,198 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2010 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
/* mcount or _mcount is inserted by GCC before the function prologue of every
* function when a program is compiled for profiling. At the start of mcount,
* we guarantee that:
* ra = self_pc (an address in the function which called mcount)
* r8 = from_pc (an address in the function which called mcount's caller)
*
* Because this is always called at the start of a function we can corrupt
* r2,r3 and r11-r15. We must not corrupt r4-r7 (because they might contain
* function arguments for the instrumented function) or r8 (which holds ra
* for the instrumented function).
*/
.global __mcount_fn_head
.global mcount
/* _mcount is used by gcc4 */
.global _mcount
_mcount:
mcount:
/* Use a hash to speed up locating fn_entry. We use bits 5 upwards to choose
* the bucket because bits 1:0 will always be 0, and because the distribution
* of values for bits 4:2 won't be even (aligning on cache line boundaries
* will skew it). Higher bits should be fairly random.
*/
/* fn_head = mcount_fn_head + (((unsigned int)self_pc >> 5) & (HASH_BUCKETS - 1)); */
srli r2, ra, 3
movhi r3, %hiadj(__mcount_fn_head)
addi r3, r3, %lo(__mcount_fn_head)
andi r2, r2, 0xFC
add r11, r2, r3
/* The fast case is where we have already allocated a function arc, and so
* also a function pointer.
*/
/* First find the function being called (using self_pc) */
mov r10, r11
0:
ldw r10, 0(r10)
beq r10, zero, .Lnew_arc
ldw r2, 4(r10)
bne r2, ra, 0b
/* Found a function entry for this PC. Now look for an arc with a matching
* from_pc value. There will always be at least one arc. */
ldw r3, 8(r10)
0:
ldw r2, 4(r3)
beq r2, r8, .Lfound_arc
ldw r3, 0(r3)
bne r3, zero, 0b
.Lnew_arc:
addi sp, sp, -24
.LCFI0:
stw ra, 0(sp)
stw r4, 4(sp)
stw r5, 8(sp)
stw r6, 12(sp)
stw r7, 16(sp)
stw r8, 20(sp)
.LCFI1:
/* __mcount_record(orig_ra, orig_r8, fn_entry, *fn_head); */
mov r4, ra
mov r5, r8
mov r6, r10
mov r7, r11
call __mcount_record
/* restore registers from the stack */
ldw ra, 0(sp)
ldw r4, 4(sp)
ldw r5, 8(sp)
ldw r6, 12(sp)
ldw r7, 16(sp)
ldw r8, 20(sp)
addi sp, sp, 24
.LCFI2:
ret
.Lfound_arc:
/* We've found the correct arc record. Increment the count and return */
ldw r2, 8(r3)
addi r2, r2, 1
stw r2, 8(r3)
ret
.Lmcount_end:
/*
* Dwarf2 debug information for the function. This provides GDB with the
* information it needs to backtrace out of this function.
*/
.section .debug_frame,"",@progbits
.LCIE:
.4byte 2f - 1f /* Length */
1:
.4byte 0xffffffff /* CIE id */
.byte 0x1 /* Version */
.string "" /* Augmentation */
.uleb128 0x1 /* Code alignment factor */
.sleb128 -4 /* Data alignment factor */
.byte 0x1f /* Return address register */
.byte 0xc /* Define CFA */
.uleb128 0x1b /* Register 27 (sp) */
.uleb128 0x0 /* Offset 0 */
.align 2 /* Padding */
2:
.LFDE_mcount:
.4byte 2f - 1f /* Length */
1:
.4byte .LCIE /* Pointer to CIE */
.4byte mcount /* Start of table entry */
.4byte .Lmcount_end - mcount /* Size of table entry */
.byte 0x4 /* Advance location */
.4byte .LCFI0 - mcount /* to .LCFI0 */
.byte 0xe /* Define CFA offset */
.uleb128 24 /* to 24 */
.byte 0x4 /* Advance location */
.4byte .LCFI1 - .LCFI0 /* to .LCFI1 */
.byte 0x9f /* Store ra */
.uleb128 0x6 /* at CFA-24 */
.byte 0x84 /* Store r4 */
.uleb128 0x5 /* at CFA-20 */
.byte 0x85 /* Store r5 */
.uleb128 0x4 /* at CFA-16 */
.byte 0x86 /* Store r6 */
.uleb128 0x3 /* at CFA-12 */
.byte 0x87 /* Store r7 */
.uleb128 0x2 /* at CFA-8 */
.byte 0x88 /* Store r8 */
.uleb128 0x1 /* at CFA-4 */
.byte 0x4 /* Advance location */
.4byte .LCFI2 - .LCFI1 /* to .LCFI2 */
.byte 0xe /* Define CFA offset */
.uleb128 0 /* to 0 */
.byte 0x8 /* Same value */
.uleb128 31 /* for ra */
.byte 0x8 /* Same value */
.uleb128 4 /* for r4 */
.byte 0x8 /* Same value */
.uleb128 5 /* for r5 */
.byte 0x8 /* Same value */
.uleb128 6 /* for r6 */
.byte 0x8 /* Same value */
.uleb128 7 /* for r7 */
.byte 0x8 /* Same value */
.uleb128 8 /* for r8 */
.align 2
2:
@@ -0,0 +1,173 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "priv/alt_file.h"
#include "alt_types.h"
#include "os/alt_syscall.h"
#ifdef ALT_USE_DIRECT_DRIVERS
int ALT_OPEN (const char* file, int flags, int mode)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(open);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
#else /* !ALT_USE_DIRECT_DRIVERS */
extern alt_llist alt_dev_list;
/*
* alt_file_locked() is used by open() to ensure that a device has not been
* previously locked for exclusive access using ioctl(). This test is only
* performed for devices. Filesystems are required to handle the ioctl() call
* themselves, and report the error from the filesystems open() function.
*/
static int alt_file_locked (alt_fd* fd)
{
alt_u32 i;
/*
* Mark the file descriptor as belonging to a device.
*/
fd->fd_flags |= ALT_FD_DEV;
/*
* Loop through all current file descriptors searching for one that's locked
* for exclusive access. If a match is found, generate an error.
*/
for (i = 0; i <= alt_max_fd; i++)
{
if ((alt_fd_list[i].dev == fd->dev) &&
(alt_fd_list[i].fd_flags & ALT_FD_EXCL) &&
(&alt_fd_list[i] != fd))
{
return -EACCES;
}
}
/* The device is not locked */
return 0;
}
/*
* open() is called in order to get a file descriptor that reference the file
* or device named "name". This descriptor can then be used to manipulate the
* file/device using the standard system calls, e.g. write(), read(), ioctl()
* etc.
*
* This is equivalent to the standard open() system call.
*
* ALT_OPEN is mapped onto the open() system call in alt_syscall.h
*/
int ALT_OPEN (const char* file, int flags, int mode)
{
alt_dev* dev;
alt_fd* fd;
int index = -1;
int status = -ENODEV;
int isafs = 0;
/*
* Check the device list, to see if a device with a matching name is
* registered.
*/
if (!(dev = alt_find_dev (file, &alt_dev_list)))
{
/* No matching device, so try the filesystem list */
dev = alt_find_file (file);
isafs = 1;
}
/*
* If a matching device or filesystem is found, allocate a file descriptor.
*/
if (dev)
{
if ((index = alt_get_fd (dev)) < 0)
{
status = index;
}
else
{
fd = &alt_fd_list[index];
fd->fd_flags = (flags & ~ALT_FD_FLAGS_MASK);
/* If this is a device, ensure it isn't already locked */
if (isafs || ((status = alt_file_locked (fd)) >= 0))
{
/*
* If the device or filesystem provides an open() callback function,
* call it now to perform any device/filesystem specific operations.
*/
status = (dev->open) ? dev->open(fd, file, flags, mode): 0;
}
}
}
else
{
status = -ENODEV;
}
/* Allocation failed, so clean up and return an error */
if (status < 0)
{
alt_release_fd (index);
ALT_ERRNO = -status;
return -1;
}
/* return the reference upon success */
return index;
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,132 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2015 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
/*
* This file provides a very minimal printf implementation for use with very
* small applications. Only the following format strings are supported:
* %x
* %s
* %c
* %%
*/
#include <stdarg.h>
#include "sys/alt_stdio.h"
#ifdef ALT_SEMIHOSTING
#define alt_putchar(x) alt_putcharbuf(x)
#endif
/*
* ALT printf function
*/
void
alt_printf(const char* fmt, ... )
{
va_list args;
va_start(args, fmt);
const char *w;
char c;
/* Process format string. */
w = fmt;
while ((c = *w++) != 0)
{
/* If not a format escape character, just print */
/* character. Otherwise, process format string. */
if (c != '%')
{
alt_putchar(c);
}
else
{
/* Get format character. If none */
/* available, processing is complete. */
if ((c = *w++) != 0)
{
if (c == '%')
{
/* Process "%" escape sequence. */
alt_putchar(c);
}
else if (c == 'c')
{
int v = va_arg(args, int);
alt_putchar(v);
}
else if (c == 'x')
{
/* Process hexadecimal number format. */
unsigned long v = va_arg(args, unsigned long);
unsigned long digit;
int digit_shift;
/* If the number value is zero, just print and continue. */
if (v == 0)
{
alt_putchar('0');
continue;
}
/* Find first non-zero digit. */
digit_shift = 28;
while (!(v & (0xF << digit_shift)))
digit_shift -= 4;
/* Print digits. */
for (; digit_shift >= 0; digit_shift -= 4)
{
digit = (v & (0xF << digit_shift)) >> digit_shift;
if (digit <= 9)
c = '0' + digit;
else
c = 'a' + digit - 10;
alt_putchar(c);
}
}
else if (c == 's')
{
/* Process string format. */
char *s = va_arg(args, char *);
while(*s)
alt_putchar(*s++);
}
}
else
{
break;
}
}
}
#ifdef ALT_SEMIHOSTING
alt_putbufflush();
#endif
}
@@ -0,0 +1,68 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2015 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stdio.h>
#ifdef ALT_USE_DIRECT_DRIVERS
#include "system.h"
#include "sys/alt_driver.h"
#include "sys/alt_stdio.h"
#endif
#ifdef ALT_SEMIHOSTING
#include "sys/alt_stdio.h"
#include "unistd.h"
#endif
/*
* Uses the ALT_DRIVER_WRITE() macro to call directly to driver if available.
* Otherwise, uses newlib provided putchar() routine.
*/
int
alt_putchar(int c)
{
#ifdef ALT_SEMIHOSTING
char c1 = (char)(c & 0xff);
return write(STDOUT_FILENO,&c1,1);
#else
#ifdef ALT_USE_DIRECT_DRIVERS
ALT_DRIVER_WRITE_EXTERNS(ALT_STDOUT_DEV);
char c1 = (char)(c & 0xff);
if (ALT_DRIVER_WRITE(ALT_STDOUT_DEV, &c1, 1, 0) == -1) {
return -1;
}
return c;
#else
return putchar(c);
#endif
#endif
}
@@ -0,0 +1,80 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2015 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#ifdef ALT_SEMIHOSTING
#include "sys/alt_stdio.h"
#include "unistd.h"
#ifndef ALT_PUTBUF_SIZE
#define ALT_PUTBUF_SIZE 64
#endif
// Buffer for the printed chars
static char buf[ALT_PUTBUF_SIZE] ={0};
// index into the buffer
static unsigned int fill_index;
/*
* ALT putcharbuf funtion
* Used only for semihosting.
* Not thread safe!
* This fucntion buffers up chars to be printed until either alt_putbufflush()
* is called or the buffer is full.
* It is called by alt_printf when semihosting is turned on
* Its purpose is to minimize the number of Break 1 issuesd by the semihosting
* libraries.
*/
int
alt_putcharbuf(int c)
{
buf[fill_index++] = (char)(c & 0xff);
if(fill_index >= ALT_PUTBUF_SIZE)
alt_putbufflush();
return c;
}
/*
* ALT putbufflush
* used only for smehosting
* Not thread safe!
* Dumps all the chars in the buffer to STDOUT
*/
int
alt_putbufflush()
{
int results;
results = write(STDOUT_FILENO,buf,fill_index);
fill_index = 0;
return results;
}
#endif
@@ -0,0 +1,64 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2015 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <stdio.h>
#ifdef ALT_USE_DIRECT_DRIVERS
#include <string.h>
#include "system.h"
#include "sys/alt_driver.h"
#include "sys/alt_stdio.h"
#endif
#ifdef ALT_SEMIHOSTING
#include <string.h>
#include "sys/alt_stdio.h"
#include "unistd.h"
#endif
/*
* Uses the ALT_DRIVER_WRITE() macro to call directly to driver if available.
* Otherwise, uses newlib provided fputs() routine.
*/
int
alt_putstr(const char* str)
{
#ifdef ALT_SEMIHOSTING
return write(STDOUT_FILENO,str,strlen(str));
#else
#ifdef ALT_USE_DIRECT_DRIVERS
ALT_DRIVER_WRITE_EXTERNS(ALT_STDOUT_DEV);
return ALT_DRIVER_WRITE(ALT_STDOUT_DEV, str, strlen(str), 0);
#else
return fputs(str, stdout);
#endif
#endif
}
@@ -0,0 +1,125 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <unistd.h>
#include <fcntl.h>
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "priv/alt_file.h"
#include "os/alt_syscall.h"
/*
* The read() system call is used to read a block of data from a file or device.
* This function simply vectors the request to the device driver associated
* with the input file descriptor "file".
*
* ALT_READ is mapped onto the read() system call in alt_syscall.h
*/
#ifdef ALT_USE_DIRECT_DRIVERS
#include "system.h"
#include "sys/alt_driver.h"
/*
* Provide minimal version that just reads from the stdin device when provided.
*/
int ALT_READ (int file, void *ptr, size_t len)
{
#ifdef ALT_STDIN_PRESENT
ALT_DRIVER_READ_EXTERNS(ALT_STDIN_DEV);
#endif
#if !defined(ALT_STDIN_PRESENT)
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(read);
#endif
switch (file) {
#ifdef ALT_STDIN_PRESENT
case 0: /* stdin file descriptor */
return ALT_DRIVER_READ(ALT_STDIN_DEV, ptr, len, 0);
#endif /* ALT_STDIN_PRESENT */
default:
ALT_ERRNO = EBADFD;
return -1;
}
}
#else /* !ALT_USE_DIRECT_DRIVERS */
int ALT_READ (int file, void *ptr, size_t len)
{
alt_fd* fd;
int rval;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (file < 0) ? NULL : &alt_fd_list[file];
if (fd)
{
/*
* If the file has not been opened with read access, or if the driver does
* not provide an implementation of read(), generate an error. Otherwise
* call the drivers read() function to process the request.
*/
if (((fd->fd_flags & O_ACCMODE) != O_WRONLY) &&
(fd->dev->read))
{
if ((rval = fd->dev->read(fd, ptr, len)) < 0)
{
ALT_ERRNO = -rval;
return -1;
}
return rval;
}
else
{
ALT_ERRNO = EACCES;
}
}
else
{
ALT_ERRNO = EBADFD;
}
return -1;
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,54 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_dev.h"
#include "priv/alt_file.h"
/*
* alt_release_fd() is called to free an allocated file descriptor. This is
* done by setting the device pointer in the file descriptor structure to zero.
*
* File descriptors correcponding to standard in, standard out and standard
* error cannont be released backed to the pool. They are always reserved.
*/
void alt_release_fd (int fd)
{
if (fd > 2)
{
alt_fd_list[fd].fd_flags = 0;
alt_fd_list[fd].dev = 0;
}
}
@@ -0,0 +1,55 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003,2007 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "sys/alt_warning.h"
#include "sys/alt_cache.h"
#include "system.h"
/*
* Convert a pointer to a block of uncached memory into a block of cached memory.
* Return a pointer that should be used to access the cached memory.
*/
void*
alt_remap_cached(volatile void* ptr, alt_u32 len)
{
#if ALT_CPU_DCACHE_SIZE > 0
#ifdef ALT_CPU_DCACHE_BYPASS_MASK
return (void*) (((alt_u32)ptr) & ~ALT_CPU_DCACHE_BYPASS_MASK);
#else /* No address mask option enabled. */
/* Generate a link time error, should this function ever be called. */
ALT_LINK_ERROR("alt_remap_cached() is not available because CPU is not configured to use bit 31 of address to bypass data cache");
return NULL;
#endif /* No address mask option enabled. */
#else /* No data cache */
/* Nothing needs to be done to the pointer. */
return (void*) ptr;
#endif /* No data cache */
}
@@ -0,0 +1,54 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003,2007 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "sys/alt_warning.h"
#include "sys/alt_cache.h"
#include "system.h"
/*
* Convert a pointer to a block of cached memory into a block of uncached memory.
* Return a pointer that should be used to access the uncached memory.
*
* This routine was created for Nios II Gen1 cores which allow mixing cacheable and
* uncachable data in the same data cache line. So, they could take any memory region
* and make it uncached. However, Nios II Gen2 cores don't support mixing cacheable
* and uncachable data in the same data cache line so require the memory region to
* be aligned to a cache line boundary and must be an integer number of cache line
* bytes in size. So, software on a Nios II Gen2 core shouldn't really be using this
* function so it fails with a link error.
*/
volatile void*
alt_remap_uncached(void* ptr, alt_u32 len)
{
/* Generate a link time error, should this function ever be called. */
ALT_LINK_ERROR("alt_remap_uncached() is not available because Nios II Gen2 cores with data caches don't support mixing cacheable and uncacheable data on the same line.");
return NULL;
}
@@ -0,0 +1,55 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "os/alt_syscall.h"
/*
* _rename() is used by newlib to rename an existing file. This is unsupported
* in the HAL environment. However a "do-nothing" implementation is still
* provied for newlib compatability.
*
* ALT_RENAME is mapped onto the _rename() system call in alt_syscall.h
*/
int ALT_RENAME (char *existing, char *new)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(_rename);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
@@ -0,0 +1,136 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <sys/types.h>
#include "os/alt_syscall.h"
#include "sys/alt_irq.h"
#include "sys/alt_stack.h"
#include "system.h"
/*
* sbrk() is called to dynamically extend the data segment for the application.
* Thie input argument "incr" is the size of the block to allocate.
*
* This simple implementation does not perform any bounds checking. Memory will
* be allocated, even if the request region colides with the stack or overflows
* the available physical memory.
*
* ALT_SBRK is mapped onto the sbrk() system call in alt_syscall.h
*
* This function is called by the profiling code to allocate memory so must be
* safe if called from an interrupt context. It must also not be instrumented
* because that would lead to an infinate loop.
*/
extern char __alt_heap_start[]; /* set by linker */
extern char __alt_heap_limit[]; /* set by linker */
static char *heap_end = __alt_heap_start;
#if defined(ALT_EXCEPTION_STACK) && defined(ALT_STACK_CHECK)
char * alt_exception_old_stack_limit = NULL;
#endif
caddr_t ALT_SBRK (int incr) __attribute__ ((no_instrument_function ));
caddr_t ALT_SBRK (int incr)
{
alt_irq_context context;
char *prev_heap_end;
context = alt_irq_disable_all();
/* Always return data aligned on a word boundary */
heap_end = (char *)(((unsigned int)heap_end + 3) & ~3);
#ifdef ALT_MAX_HEAP_BYTES
/*
* User specified a maximum heap size. Return -1 if it would
* be exceeded by this sbrk call.
*/
if (((heap_end + incr) - __alt_heap_start) > ALT_MAX_HEAP_BYTES) {
alt_irq_enable_all(context);
return (caddr_t)-1;
}
#else
if ((heap_end + incr) > __alt_heap_limit) {
alt_irq_enable_all(context);
return (caddr_t)-1;
}
#endif
prev_heap_end = heap_end;
heap_end += incr;
#ifdef ALT_STACK_CHECK
/*
* If the stack and heap are contiguous then extending the heap reduces the
* space available for the stack. If we are still using the default stack
* then adjust the stack limit to note this, while checking for stack
* pointer overflow.
* If the stack limit isn't pointing at the top of the heap then the code
* is using a different stack so none of this needs to be done.
*/
if (alt_stack_limit() == prev_heap_end)
{
if (alt_stack_pointer() <= heap_end)
alt_report_stack_overflow();
alt_set_stack_limit(heap_end);
}
#ifdef ALT_EXCEPTION_STACK
/*
* If we are executing from the exception stack then compare against the
* stack we switched away from as well. The exception stack is a fixed
* size so doesn't need to be checked.
*/
if (alt_exception_old_stack_limit == prev_heap_end)
{
if (alt_exception_old_stack_limit <= heap_end)
alt_report_stack_overflow();
alt_exception_old_stack_limit = heap_end;
}
#endif
#endif
alt_irq_enable_all(context);
return (caddr_t) prev_heap_end;
}
@@ -0,0 +1,96 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2017,2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <sys/time.h>
#include <sys/times.h>
#include "sys/alt_errno.h"
#include "sys/alt_alarm.h"
#include "os/alt_syscall.h"
/*
* "alt_timezone" and "alt_resettime" are the values of the the reset time and
* time zone set through the last call to settimeofday(). By default they are
* zero initialised.
*/
extern struct timezone alt_timezone;
extern struct timeval alt_resettime;
/*
* Macro defining the number of micoseconds in a second.
*/
#define ALT_US (1000000)
/*
* settimeofday() can be called to calibrate the system clock, so that
* subsequent calls to gettimeofday() will return the elapsed "wall clock"
* time.
*
* This is done by updating the global structures "alt_resettime" and
* "alt_timezone" so that an immediate call to gettimeofday() would return
* the value specified by "t" and "tz".
*
* Warning: if this function is called concurrently with a call to
* gettimeofday(), the value returned by gettimeofday() will be unreliable.
*
* ALT_SETTIMEOFDAY is mapped onto the settimeofday() system call in
* alt_syscall.h
*/
int ALT_SETTIMEOFDAY (const struct timeval *t,
const struct timezone *tz)
{
alt_u64 nticks = alt_nticks ();
alt_u32 tick_rate = alt_ticks_per_second ();
/* If there is a system clock available, update the current time */
if (tick_rate)
{
alt_resettime.tv_sec = t->tv_sec - nticks/tick_rate;
alt_resettime.tv_usec = t->tv_usec -
((nticks*(ALT_US/tick_rate))%ALT_US);
alt_timezone.tz_minuteswest = tz->tz_minuteswest;
alt_timezone.tz_dsttime = tz->tz_dsttime;
return 0;
}
/* There's no system clock available */
ALT_ERRNO = ENOSYS;
return -1;
}
@@ -0,0 +1,53 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003-2005 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
/*
* This file provides the global symbol: software_exception. It is provided to
* support legacy code, and should not be used by new software.
*
* It is used by legacy code to invoke the software exception handler as
* defined by version 1.0 of the Nios II kit. It should only be used when you
* are providing your own interrupt entry point, i.e. you are not using
* alt_irq_entry.
*/
#include "system.h"
/*
* Pull in the exception handler.
*/
.globl alt_exception
.section .exceptions.entry.label, "xa"
.globl software_exception
.type software_exception, @function
software_exception:
@@ -0,0 +1,59 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "os/alt_syscall.h"
/*
* The stat() function is similar to the fstat() function in that it is used to
* obtain status information about a file. Instead of using an open file
* descriptor (like fstat()), stat() takes the name of a file as an input
* argument.
*
* ALT_STAT is mapped onto the stat() system call in alt_syscall.h
*/
int ALT_STAT (const char *file, struct stat *st)
{
int fd;
int rc;
fd = open (file, 0);
rc = fstat (fd, st);
close (fd);
return rc;
}
@@ -0,0 +1,129 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2017,2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_irq.h"
#include "sys/alt_alarm.h"
#include "os/alt_hooks.h"
#include "alt_types.h"
/*
* "_alt_tick_rate" is used to store the value of the system clock frequency
* in ticks per second. It is initialised to zero, which corresponds to there
* being no system clock facility available.
*/
alt_u32 _alt_tick_rate = 0;
/*
* "_alt_nticks" is the number of system clock ticks that have elapsed since
* reset.
*/
volatile alt_u64 _alt_nticks = 0;
/*
* "alt_alarm_list" is the head of a linked list of registered alarms. This is
* initialised to be an empty list.
*/
ALT_LLIST_HEAD(alt_alarm_list);
/*
* alt_alarm_stop() is called to remove an alarm from the list of registered
* alarms. Alternatively an alarm can unregister itself by returning zero when
* the alarm executes.
*/
void alt_alarm_stop (alt_alarm* alarm)
{
alt_irq_context irq_context;
irq_context = alt_irq_disable_all();
alt_llist_remove (&alarm->llist);
alt_irq_enable_all (irq_context);
}
/*
* alt_tick() is periodically called by the system clock driver in order to
* process the registered list of alarms. Each alarm is registed with a
* callback interval, and a callback function, "callback".
*
* The return value of the callback function indicates how many ticks are to
* elapse until the next callback. A return value of zero indicates that the
* alarm should be deactivated.
*
* alt_tick() is expected to run at interrupt level.
*/
void alt_tick (void)
{
alt_alarm* next;
alt_alarm* alarm = (alt_alarm*) alt_alarm_list.next;
alt_u32 next_callback;
/* update the tick counter */
_alt_nticks++;
/* process the registered callbacks */
while (alarm != (alt_alarm*) &alt_alarm_list)
{
next = (alt_alarm*) alarm->llist.next;
/* if the alarm period has expired, make the callback */
if (alarm->time <= _alt_nticks)
{
next_callback = alarm->callback (alarm->context);
/* deactivate the alarm if the return value is zero */
if (next_callback == 0)
{
alt_alarm_stop (alarm);
}
else
{
alarm->time += next_callback;
}
}
alarm = next;
}
/*
* Update the operating system specific timer facilities.
*/
ALT_OS_TIME_TICK();
}
@@ -0,0 +1,71 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <sys/times.h>
#include "sys/alt_errno.h"
#include "sys/alt_alarm.h"
#include "os/alt_syscall.h"
/*
* The times() function is used by newlib to obtain elapsed time information.
* The return value is the elapsed time since reset in system clock ticks. Note
* that this is distinct from the strict Posix version of times(), which should
* return the time since: 0 hours, 0 minutes, 0 seconds, January 1, 1970, GMT.
*
* The input structure is filled in with time accounting information. This
* implementation attributes all cpu time to the system.
*
* ALT_TIMES is mapped onto the times() system call in alt_syscall.h
*/
clock_t ALT_TIMES (struct tms *buf)
{
clock_t ticks = alt_nticks();
/* If there is no system clock present, generate an error */
if (!alt_ticks_per_second())
{
ALT_ERRNO = ENOSYS;
return 0;
}
/* Otherwise return the elapsed time */
buf->tms_utime = 0;
buf->tms_stime = ticks;
buf->tms_cutime = 0;
buf->tms_cstime = 0;
return ticks;
}
@@ -0,0 +1,53 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003,2007 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "sys/alt_warning.h"
#include "sys/alt_cache.h"
#include "system.h"
/*
* Free a block of uncached memory.
*/
void
alt_uncached_free(volatile void* ptr)
{
#if ALT_CPU_DCACHE_SIZE > 0
#ifdef ALT_CPU_DCACHE_BYPASS_MASK
free((void*) (((alt_u32)ptr) & ~ALT_CPU_DCACHE_BYPASS_MASK));
#else /* No address mask option enabled. */
/* Generate a link time error, should this function ever be called. */
ALT_LINK_ERROR("alt_uncached_free() is not available because CPU is not configured to use bit 31 of address to bypass data cache");
#endif /* No address mask option enabled. */
#else /* No data cache */
/* Nothing needs to be done to the pointer. */
free((void*)ptr);
#endif /* No data cache */
}
@@ -0,0 +1,77 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003,2007 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include <malloc.h>
#include "sys/alt_warning.h"
#include "sys/alt_cache.h"
#include "system.h"
/*
* Allocate a block of uncached memory.
* Return pointer to the block of memory or NULL if can't allocate it.
*/
volatile void*
alt_uncached_malloc(size_t size)
{
#if ALT_CPU_DCACHE_SIZE > 0
#ifdef ALT_CPU_DCACHE_BYPASS_MASK
void* ptr;
/* Round up size to an integer number of data cache lines. Required to guarantee that
* cacheable and non-cacheable data won't be mixed on the same cache line. */
const size_t num_lines = (size + ALT_CPU_DCACHE_LINE_SIZE - 1) / ALT_CPU_DCACHE_LINE_SIZE;
const size_t aligned_size = num_lines * ALT_CPU_DCACHE_LINE_SIZE;
/* Use memalign() Newlib routine to allocate starting on a data cache aligned address.
* Required to guarantee that cacheable and non-cacheable data won't be mixed on the
* same cache line. */
ptr = memalign(ALT_CPU_DCACHE_LINE_SIZE, aligned_size);
if (ptr == NULL) {
return NULL; /* Out of memory */
}
/* Ensure that the memory region isn't in the data cache. */
alt_dcache_flush(ptr, aligned_size);
return (volatile void*) (((alt_u32)ptr) | ALT_CPU_DCACHE_BYPASS_MASK);
#else /* No address mask option enabled. */
/* Generate a link time error, should this function ever be called. */
ALT_LINK_ERROR("alt_uncached_malloc() is not available because CPU is not configured to use bit 31 of address to bypass data cache");
return NULL;
#endif /* No address mask option enabled. */
#else /* No data cache */
/* Just use regular malloc. */
return malloc(size);
#endif /* No data cache */
}
@@ -0,0 +1,55 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "os/alt_syscall.h"
/*
* unlink() is used by newlib to delete an existing link to a file. This is
* unsupported in the HAL environment. However a "do-nothing" implementation
* is still provied for newlib compatability.
*
* ALT_UNLINK is mapped onto the unlink() system call in alt_syscall.h
*/
int ALT_UNLINK (char *name)
{
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(unlink);
/* Indicate an error */
ALT_ERRNO = ENOSYS;
return -1;
}
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2003 Altera Corporation, San Jose, California, USA.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* ------------
*
* Altera does not recommend, suggest or require that this reference design
* file be used in conjunction or combination with any other product.
*
* usleep.c - Microsecond delay routine
*/
#include <unistd.h>
#include "priv/alt_busy_sleep.h"
#include "os/alt_syscall.h"
/*
* This function simply calls alt_busy_sleep() to perform the delay. This
* function implements the delay as a calibrated "busy loop".
*
* ALT_USLEEP is mapped onto the usleep() system call in alt_syscall.h
*/
#if defined (__GNUC__) && __GNUC__ >= 4
int ALT_USLEEP (useconds_t us)
#else
unsigned int ALT_USLEEP (unsigned int us)
#endif
{
return alt_busy_sleep(us);
}
@@ -0,0 +1,52 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include "sys/alt_errno.h"
#include "os/alt_syscall.h"
/*
* wait() is used by newlib to wait for all child processes to exit. Since the
* HAL does not support spawning child processes, this returns immediately as
* there can't be anythign to wait for.
*
* ALT_WAIT is mapped onto the wait() system call in alt_syscall.h
*/
int ALT_WAIT (int *status)
{
*status = 0;
ALT_ERRNO = ECHILD;
return -1;
}
@@ -0,0 +1,138 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
#include <unistd.h>
#include <fcntl.h>
#include "sys/alt_errno.h"
#include "sys/alt_warning.h"
#include "priv/alt_file.h"
#include "os/alt_syscall.h"
#include "sys/alt_log_printf.h"
/*
* The write() system call is used to write a block of data to a file or
* device. This function simply vectors the request to the device driver
* associated with the input file descriptor "file".
*
* ALT_WRITE is mapped onto the write() system call in alt_syscall.h
*/
#ifdef ALT_USE_DIRECT_DRIVERS
#include "system.h"
#include "sys/alt_driver.h"
/*
* Provide minimal version that just writes to the stdout/stderr devices
* when provided.
*/
int ALT_WRITE (int file, const void *ptr, size_t len)
{
#ifdef ALT_STDOUT_PRESENT
ALT_DRIVER_WRITE_EXTERNS(ALT_STDOUT_DEV);
#endif
#ifdef ALT_STDERR_PRESENT
ALT_DRIVER_WRITE_EXTERNS(ALT_STDERR_DEV);
#endif
#if !defined(ALT_STDOUT_PRESENT) && !defined(ALT_STDERR_PRESENT)
/* Generate a link time warning, should this function ever be called. */
ALT_STUB_WARNING(write);
#endif
switch (file) {
#ifdef ALT_STDOUT_PRESENT
case 1: /* stdout file descriptor */
return ALT_DRIVER_WRITE(ALT_STDOUT_DEV, ptr, len, 0);
#endif /* ALT_STDOUT_PRESENT */
#ifdef ALT_STDERR_PRESENT
case 2: /* stderr file descriptor */
return ALT_DRIVER_WRITE(ALT_STDERR_DEV, ptr, len, 0);
#endif /* ALT_STDERR_PRESENT */
default:
ALT_ERRNO = EBADFD;
return -1;
}
}
#else /* !ALT_USE_DIRECT_DRIVERS */
int ALT_WRITE (int file, const void *ptr, size_t len)
{
alt_fd* fd;
int rval;
/*
* A common error case is that when the file descriptor was created, the call
* to open() failed resulting in a negative file descriptor. This is trapped
* below so that we don't try and process an invalid file descriptor.
*/
fd = (file < 0) ? NULL : &alt_fd_list[file];
if (fd)
{
/*
* If the file has not been opened with write access, or if the driver does
* not provide an implementation of write(), generate an error. Otherwise
* call the drivers write() function to process the request.
*/
if (((fd->fd_flags & O_ACCMODE) != O_RDONLY) && fd->dev->write)
{
/* ALT_LOG - see altera_hal/HAL/inc/sys/alt_log_printf.h */
ALT_LOG_WRITE_FUNCTION(ptr,len);
if ((rval = fd->dev->write(fd, ptr, len)) < 0)
{
ALT_ERRNO = -rval;
return -1;
}
return rval;
}
else
{
ALT_ERRNO = EACCES;
}
}
else
{
ALT_ERRNO = EBADFD;
}
return -1;
}
#endif /* ALT_USE_DIRECT_DRIVERS */
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2009 Altera Corporation, San Jose, California, USA.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* altera_nios2_gen2_irq.c - Support for Nios II internal interrupt controller.
*
*/
#include "sys/alt_irq.h"
#include "altera_nios2_gen2_irq.h"
/*
* To initialize the internal interrupt controller, just clear the IENABLE
* register so that all possible IRQs are disabled.
*/
void altera_nios2_gen2_irq_init(void)
{
NIOS2_WRITE_IENABLE(0);
}
@@ -0,0 +1,521 @@
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* *
******************************************************************************/
#include "system.h"
#include "nios2.h"
/* Setup header files to work with assembler code. */
#define ALT_ASM_SRC
/* Debug logging facility */
#include "sys/alt_log_printf.h"
/*************************************************************************\
| MACROS |
\*************************************************************************/
/*
* The new build tools explicitly define macros when alt_load()
* must be called. The define ALT_LOAD_EXPLICITLY_CONTROLLED tells us that
* those macros are controlling if alt_load() needs to be called.
*/
#ifdef ALT_LOAD_EXPLICITLY_CONTROLLED
/* Need to call alt_load() if any of these sections are being copied. */
#if defined(ALT_LOAD_COPY_RODATA) || defined(ALT_LOAD_COPY_RWDATA) || defined(ALT_LOAD_COPY_EXCEPTIONS)
#define CALL_ALT_LOAD
#endif
#else /* !ALT_LOAD_EXPLICITLY_CONTROLLED */
/*
* The legacy build tools use the following macros to detect when alt_load()
* needs to be called.
*/
#define __ALT_LOAD_SECTIONS(res, text, rodata, exc) \
((res##_BASE != rodata##_BASE) || \
(res##_BASE != rwdata##_BASE) || \
(res##_BASE != exc##_BASE))
#define _ALT_LOAD_SECTIONS(res, text, rodata, exc) \
__ALT_LOAD_SECTIONS(res, text, rodata, exc)
#define ALT_LOAD_SECTIONS _ALT_LOAD_SECTIONS(ALT_RESET_DEVICE, \
ALT_RODATA_DEVICE, \
ALT_RWDATA_DEVICE, \
ALT_EXCEPTIONS_DEVICE)
/* Call alt_load() if there is no bootloader and ALT_LOAD_SECTIONS isn't 0. */
#if defined(ALT_NO_BOOTLOADER) && ALT_LOAD_SECTIONS
#define CALL_ALT_LOAD
#endif
#endif /* !ALT_LOAD_EXPLICITLY_CONTROLLED */
/*
* When the legacy build tools define a macro called ALT_NO_BOOTLOADER,
* it indicates that initialization code is allowed at the reset address.
* The new build tools define a macro called ALT_ALLOW_CODE_AT_RESET for
* the same purpose.
*/
#ifdef ALT_NO_BOOTLOADER
#define ALT_ALLOW_CODE_AT_RESET
#endif
/*************************************************************************\
| EXTERNAL REFERENCES |
\*************************************************************************/
/*
* The entry point for user code is either "main" in hosted mode, or
* "alt_main" in standalone mode. These are explicitly referenced here,
* to ensure they are built into the executable. This allows the user
* to build them into libraries, rather than supplying them in object
* files at link time.
*/
.globl main
.globl alt_main
/*
* Create a reference to the software multiply/divide and trap handers,
* so that if they are provided, they will appear in the executable.
*/
#ifndef ALT_NO_INSTRUCTION_EMULATION
.globl alt_exception_muldiv
#endif
#ifdef ALT_TRAP_HANDLER
.globl alt_exception_trap
#endif
/*
* Linker defined symbols used to initialize bss.
*/
.globl __bss_start
.globl __bss_end
/*************************************************************************\
| RESET SECTION (.entry) |
\*************************************************************************/
/*
* This is the reset entry point for Nios II.
*
* At reset, only the cache line which contain the reset vector is
* initialized by the hardware. The code within the first cache line
* initializes the remainder of the instruction cache.
*/
.section .entry, "xa"
.align 5
/*
* Explicitly allow the use of r1 (the assembler temporary register)
* within this code. This register is normally reserved for the use of
* the assembler.
*/
.set noat
/*
* Some tools want to know where the reset vector is.
* Code isn't always provided at the reset vector but at least the
* __reset label always contains the reset vector address because
* it is defined at the start of the .entry section.
*/
.globl __reset
.type __reset, @function
__reset:
/*
* Initialize the instruction cache if present (i.e. size > 0) and
* reset code is allowed unless optimizing for RTL simulation.
* RTL simulations can ensure the instruction cache is already initialized
* so skipping this loop speeds up RTL simulation.
*
* When ECC is present, need to execute initi for each word address
* to ensure ECC parity bits in cache RAM get initialized.
*/
#if NIOS2_ICACHE_SIZE > 0 && defined(ALT_ALLOW_CODE_AT_RESET) && (!defined(ALT_SIM_OPTIMIZE) || defined(NIOS2_ECC_PRESENT))
/* Assume the instruction cache size is always a power of two. */
#if NIOS2_ICACHE_SIZE > 0x8000
movhi r2, %hi(NIOS2_ICACHE_SIZE)
#else
movui r2, NIOS2_ICACHE_SIZE
#endif
0:
initi r2
addi r2, r2, -NIOS2_ICACHE_LINE_SIZE
bgt r2, zero, 0b
1:
/*
* The following debug information tells the ISS not to run the loop above
* but to perform its actions using faster internal code.
*/
.pushsection .debug_alt_sim_info
.int 1, 1, 0b, 1b
.popsection
#endif /* Initialize Instruction Cache */
/*
* Jump to the _start entry point in the .text section if reset code
* is allowed or if optimizing for RTL simulation.
*/
#if defined(ALT_ALLOW_CODE_AT_RESET) || defined(ALT_SIM_OPTIMIZE)
/* Jump to the _start entry point in the .text section. */
movhi r1, %hi(_start)
ori r1, r1, %lo(_start)
jmp r1
.size __reset, . - __reset
#endif /* Jump to _start */
/*
* When not using exit, provide an _exit symbol to prevent unresolved
* references to _exit from the linker script.
*/
#ifdef ALT_NO_EXIT
.globl _exit
_exit:
#endif
/*************************************************************************\
| TEXT SECTION (.text) |
\*************************************************************************/
/*
* Start of the .text section, and also the code entry point when
* the code is executed by a bootloader rather than directly from reset.
*/
.section .text
.align 2
.globl _start
.type _start, @function
_start:
#if (NIOS2_NUM_OF_SHADOW_REG_SETS > 0)
/*
* Ensure that the current register set is 0 upon
* entry to this code. Switch register set to 0 by
* writing zero to SSTATUS register and executing an ERET instruction
* to set STATUS.CRS to 0.
*/
/* Get the current register set number (STATUS.CRS). */
rdctl r2, status
andi r2, r2, NIOS2_STATUS_CRS_MSK
/* Skip switching register set if STATUS.CRS is 0. */
beq r2, zero, 0f
/* Set SSTATUS to 0 to get to set SSTATUS.PRS to 0. */
.set nobreak
movui sstatus, 0
.set break
/* Switch to register set 0 and jump to label. */
movhi ea, %hi(0f)
ori ea, ea, %lo(0f)
eret
0:
#endif /* NIOS2_NUM_OF_SHADOW_REG_SETS > 0 */
/*
* Initialize the data cache if present (i.e. size > 0).
* Skip initialization if optimizing for RTL simulation and ECC isn't present.
* RTL simulations can ensure the data cache tag RAM is already initialized
* (but not the data RAM for ECC) so skipping this speeds up RTL simulation.
*
* When ECC is present, need to execute initd for each word address
* to ensure ECC parity bits in data RAM get initialized.
* Otherwise, only need to execute initd for each line address.
*/
#if NIOS2_DCACHE_SIZE > 0 && (!defined(ALT_SIM_OPTIMIZE) || defined(NIOS2_ECC_PRESENT))
/* Assume the data cache size is always a power of two. */
#if NIOS2_DCACHE_SIZE > 0x8000
movhi r2, %hi(NIOS2_DCACHE_SIZE)
#else
movui r2, NIOS2_DCACHE_SIZE
#endif
0:
initd 0(r2)
#ifdef NIOS2_ECC_PRESENT
addi r2, r2, -4
#else
addi r2, r2, -NIOS2_DCACHE_LINE_SIZE
#endif
bgt r2, zero, 0b
1:
/*
* The following debug information tells the ISS not to run the loop above
* but to perform its actions using faster internal code.
*/
.pushsection .debug_alt_sim_info
.int 2, 1, 0b, 1b
.popsection
#endif /* Initialize Data Cache */
/* Log that caches have been initialized. */
ALT_LOG_PUTS(alt_log_msg_cache)
/* Log that the stack pointer is about to be setup. */
ALT_LOG_PUTS(alt_log_msg_stackpointer)
/*
* Now that the caches are initialized, set up the stack pointer and global pointer.
* The values provided by the linker are assumed to be correctly aligned.
*/
movhi sp, %hi(__alt_stack_pointer)
ori sp, sp, %lo(__alt_stack_pointer)
movhi gp, %hi(_gp)
ori gp, gp, %lo(_gp)
#ifdef NIOS2_ECC_PRESENT
/*
* Initialize all general-purpose registers so that ECC can be enabled
* later without accidentally triggering a spurious ECC error.
*/
movui r1, 0
movui r2, 0
movui r3, 0
movui r4, 0
movui r5, 0
movui r6, 0
movui r7, 0
movui r8, 0
movui r9, 0
movui r10, 0
movui r11, 0
movui r12, 0
movui r13, 0
movui r14, 0
movui r15, 0
movui r16, 0
movui r17, 0
movui r18, 0
movui r19, 0
movui r20, 0
movui r21, 0
movui r22, 0
movui r23, 0
/* Skip r24 (et) because only exception handler should write it. */
/* Skip r25 (bt) because only debugger should write it. */
/* Skip r26 (gp) because it is already been initialized. */
/* Skip r27 (sp) because it is already been initialized. */
movui r28, 0 /* fp */
movui r29, 0 /* ea */
.set nobreak
movui r30, 0 /* sstatus */
.set break
movui r31, 0 /* ra */
#endif /* NIOS2_ECC_PRESENT */
#if (NIOS2_NUM_OF_SHADOW_REG_SETS > 0)
/*
* Setup registers in shadow register sets
* from 1 to NIOS2_NUM_OF_SHADOW_REG_SETS.
*/
movui r2, 0 /* Contains value written into STATUS */
movui r3, NIOS2_NUM_OF_SHADOW_REG_SETS /* counter */
movhi r4, 1 /* Constant to increment STATUS.PRS */
.Linitialize_shadow_registers:
/* Increment STATUS.PRS */
add r2, r2, r4
wrctl status, r2
/* Clear r0 in the shadow register set (not done by hardware) */
wrprs r0, r0
/* Write the GP in previous register set */
wrprs gp, gp
/*
* Only write the SP in previous register set
* if using the separate exception stack. For normal case (single stack),
* funnel code would read the SP from previous register set with a RDPRS.
*/
#ifdef ALT_INTERRUPT_STACK
movhi et, %hiadj(__alt_interrupt_stack_pointer)
addi et, et, %lo(__alt_interrupt_stack_pointer)
wrprs sp, et
#endif /* ALT_INTERRUPT_STACK */
#ifdef NIOS2_ECC_PRESENT
/*
* Initialize all general-purpose registers so that ECC can be enabled
* later without accidentally triggering a spurious ECC error.
*/
wrprs r1, r0
wrprs r2, r0
wrprs r3, r0
wrprs r4, r0
wrprs r5, r0
wrprs r6, r0
wrprs r7, r0
wrprs r8, r0
wrprs r9, r0
wrprs r10, r0
wrprs r11, r0
wrprs r12, r0
wrprs r13, r0
wrprs r14, r0
wrprs r15, r0
wrprs r16, r0
wrprs r17, r0
wrprs r18, r0
wrprs r19, r0
wrprs r20, r0
wrprs r21, r0
wrprs r22, r0
wrprs r23, r0
/* Skip r24 (et) because only exception handler should write it. */
/* Skip r25 (bt) because only debugger should write it. */
/* Skip r26 (gp) because it is already been initialized. */
/* Skip r27 (sp) because it was initialized above or will be by a rdprs if not above */
wrprs r28, r0 /* fp */
wrprs r29, r0 /* ea */
wrprs r30, r0 /* ba */
wrprs r31, r0 /* ra */
#endif /* NIOS2_ECC_PRESENT */
/* Decrement shadow register set counter */
addi r3, r3, -1
/* Done if index is 0. */
bne r3, zero, .Linitialize_shadow_registers
#endif /* (NIOS2_NUM_OF_SHADOW_REG_SETS > 0) */
/*
* Clear the BSS if not optimizing for RTL simulation.
*
* This uses the symbols: __bss_start and __bss_end, which are defined
* by the linker script. They mark the begining and the end of the bss
* region. The linker script guarantees that these values are word aligned.
*/
#ifndef ALT_SIM_OPTIMIZE
/* Log that the BSS is about to be cleared. */
ALT_LOG_PUTS(alt_log_msg_bss)
movhi r2, %hi(__bss_start)
ori r2, r2, %lo(__bss_start)
movhi r3, %hi(__bss_end)
ori r3, r3, %lo(__bss_end)
beq r2, r3, 1f
0:
stw zero, (r2)
addi r2, r2, 4
bltu r2, r3, 0b
1:
/*
* The following debug information tells the ISS not to run the loop above
* but to perform its actions using faster internal code.
*/
.pushsection .debug_alt_sim_info
.int 3, 1, 0b, 1b
.popsection
#endif /* ALT_SIM_OPTIMIZE */
/*
* Turn off the use of r1 (the assembler temporary register)
* so that call instructions can be safely relaxed across a
* 256MB boundary if needed
*/
.set at
/*
* The alt_load() facility is normally used when there is no bootloader.
* It copies some sections into RAM so it acts like a mini-bootloader.
*/
#ifdef CALL_ALT_LOAD
#ifdef ALT_STACK_CHECK
/*
* If the user has selected stack checking then we need to set up a safe
* value in the stack limit register so that the relocation functions
* don't think the stack has overflowed (the contents of the rwdata
* section aren't defined until alt_load() has been called).
*/
mov et, zero
#endif
call alt_load
#endif /* CALL_ALT_LOAD */
#ifdef ALT_STACK_CHECK
/*
* Set up the stack limit (if required). The linker has set up the
* copy of the variable which is in memory.
*/
ldw et, %gprel(alt_stack_limit_value)(gp)
#endif
/* Log that alt_main is about to be called. */
ALT_LOG_PUTS(alt_log_msg_alt_main)
/* Call the C entry point. It should never return. */
call alt_main
/* Wait in infinite loop in case alt_main does return. */
alt_after_alt_main:
br alt_after_alt_main
.size _start, . - _start
/*
* Add information about the stack base if stack overflow checking is enabled.
*/
#ifdef ALT_STACK_CHECK
.globl alt_stack_limit_value
.section .sdata,"aws",@progbits
.align 2
.type alt_stack_limit_value, @object
.size alt_stack_limit_value, 4
alt_stack_limit_value:
.long __alt_stack_limit
#endif