semihost: Add SuperH support

This supports running tests under qemu using the r2d machine and
sh7785 CPU.

Signed-off-by: Keith Packard <keithp@keithp.com>
This commit is contained in:
Keith Packard 2023-10-25 21:53:44 -07:00
parent 4d3cf3ebfc
commit 4d976739c2
5 changed files with 393 additions and 0 deletions

View File

@ -0,0 +1,66 @@
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright © 2021 Keith Packard
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#
lib_semihost_srcs = [
'sh_stub.c',
'sh_exit.c',
'sh_io.c',
]
has_semihost = true
foreach target : targets
value = get_variable('target_' + target)
instdir = join_paths(lib_dir, value[0])
if target == ''
libsemihost_name = 'semihost'
else
libsemihost_name = join_paths(target, 'libsemihost')
endif
local_lib_semihost_target = static_library(libsemihost_name,
lib_semihost_srcs,
install : true,
install_dir : instdir,
include_directories : inc,
c_args : value[1] + c_args,
pic: false)
set_variable('lib_semihost' + target, local_lib_semihost_target)
endforeach

View File

@ -0,0 +1,55 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright © 2021 Keith Packard
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sh_semihost.h"
void _ATTRIBUTE((__noreturn__))
_exit(int code)
{
#ifdef SH_QEMU
char buf[15];
int n;
/* Can't use printf because stdout has already been cleaned up */
n = snprintf(buf, sizeof(buf), "%cexit %d\n", 0xe9, code);
write(1, buf, n);
while(1);
#else
sh_cons.halt = code;
#endif
__builtin_unreachable();
}

View File

@ -0,0 +1,81 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright © 2022 Keith Packard
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sh_semihost.h"
int
sh_putc(char c, FILE *file)
{
(void) file;
#ifdef SH_QEMU
sh_serial0.tdr = (unsigned char) c;
#else
sh_cons.data = c;
#endif
return (unsigned char) c;
}
int
sh_getc(FILE *file)
{
(void) file;
#ifdef SH_QEMU
return EOF;
#else
for (;;) {
uint8_t c;
c = sh_cons.data;
if (c != 0)
return (int) c;
}
#endif
}
#ifdef TINY_STDIO
static FILE __stdio = FDEV_SETUP_STREAM(sh_putc, sh_getc, NULL, _FDEV_SETUP_RW);
#ifdef __strong_reference
#define STDIO_ALIAS(x) __strong_reference(stdin, x);
#else
#define STDIO_ALIAS(x) FILE *const x = &__stdio;
#endif
FILE *const stdin = &__stdio;
STDIO_ALIAS(stdout);
STDIO_ALIAS(stderr);
#endif

View File

@ -0,0 +1,77 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright © 2023 Keith Packard
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SH_SEMIHOST_H_
#define _SH_SEMIHOST_H_
#include <stdio.h>
#include <stdint.h>
#define SH_QEMU
typedef volatile uint8_t vuint8_t;
typedef volatile uint32_t vuint32_t;
#ifdef SH_QEMU
struct sh_serial {
vuint32_t smr;
vuint32_t brr;
vuint32_t scr;
vuint32_t tdr;
};
#define sh_serial0 (*(struct sh_serial *) 0xffe00000)
#define sh_serial1 (*(struct sh_serial *) 0xffe80000)
#else
struct sh_cons {
vuint8_t data;
uint8_t unused_01[0xf];
vuint8_t halt;
};
#define sh_cons (*(struct sh_cons *) 0x10000000)
#endif
int
sh_putc(char c, FILE *file);
int
sh_getc(FILE *file);
#endif /* _SH_SEMIHOST_H_ */

View File

@ -0,0 +1,114 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright © 2021 Keith Packard
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "sh_semihost.h"
ssize_t
read(int fd, void *buf, size_t count)
{
char *b = buf;
(void) fd;
(void) count;
*b = sh_getc(NULL);
return 1;
}
ssize_t
write(int fd, const void *buf, size_t count)
{
const char *b = buf;
size_t c = count;
(void) fd;
while (c--)
sh_putc(*b++, NULL);
return count;
}
int
open(const char *pathname, int flags, ...)
{
(void) pathname;
(void) flags;
return -1;
}
int
close(int fd)
{
(void) fd;
return 0;
}
off_t lseek(int fd, off_t offset, int whence)
{
(void) fd;
(void) offset;
(void) whence;
return (off_t) -1;
}
_off64_t lseek64(int fd, _off64_t offset, int whence)
{
return (_off64_t) lseek(fd, (off_t) offset, whence);
}
int
unlink(const char *pathname)
{
(void) pathname;
return 0;
}
int
fstat (int fd, struct stat *sbuf)
{
(void) fd;
(void) sbuf;
return -1;
}
int
isatty (int fd)
{
(void) fd;
return 1;
}