2022-09-19 02:51:11 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 - Tillitis AB
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib.h"
|
2023-02-27 11:00:23 -05:00
|
|
|
#include "assert.h"
|
2023-02-27 11:11:03 -05:00
|
|
|
#include "types.h"
|
2022-09-19 02:51:11 -04:00
|
|
|
|
2023-02-27 11:50:14 -05:00
|
|
|
#ifndef NOCONSOLE
|
2022-09-19 02:51:11 -04:00
|
|
|
struct {
|
|
|
|
uint32_t arr[2];
|
|
|
|
} volatile tohost __attribute__((section(".htif")));
|
|
|
|
struct {
|
|
|
|
uint32_t arr[2];
|
|
|
|
} volatile fromhost __attribute__((section(".htif")));
|
|
|
|
|
|
|
|
static void htif_send(uint8_t dev, uint8_t cmd, int64_t data)
|
|
|
|
{
|
|
|
|
/* endian neutral encoding with ordered 32-bit writes */
|
|
|
|
union {
|
|
|
|
uint32_t arr[2];
|
|
|
|
uint64_t val;
|
|
|
|
} encode = {.val = (uint64_t)dev << 56 | (uint64_t)cmd << 48 | data};
|
|
|
|
tohost.arr[0] = encode.arr[0];
|
|
|
|
tohost.arr[1] = encode.arr[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void htif_set_tohost(uint8_t dev, uint8_t cmd, int64_t data)
|
|
|
|
{
|
|
|
|
/* send data with specified device and command */
|
|
|
|
while (tohost.arr[0]) {
|
|
|
|
asm volatile("" : : "r"(fromhost.arr[0]));
|
|
|
|
asm volatile("" : : "r"(fromhost.arr[1]));
|
|
|
|
}
|
|
|
|
htif_send(dev, cmd, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int htif_putchar(int ch)
|
|
|
|
{
|
|
|
|
htif_set_tohost(1, 1, ch & 0xff);
|
|
|
|
return ch & 0xff;
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
int htif_puts(const char *s)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
while (*s)
|
|
|
|
htif_putchar(*s++);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
void htif_hexdump(uint8_t *buf, int len)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
uint8_t *row;
|
|
|
|
uint8_t *byte;
|
|
|
|
uint8_t *max;
|
|
|
|
|
|
|
|
row = buf;
|
|
|
|
max = &buf[len];
|
|
|
|
for (byte = 0; byte != max; row = byte) {
|
|
|
|
for (byte = row; byte != max && byte != (row + 16); byte++) {
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_puthex(*byte);
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_lf();
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
void htif_putc(int ch)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
htif_putchar(ch);
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
void htif_lf()
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
htif_putchar('\n');
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
void htif_puthex(uint8_t c)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
unsigned int upper = (c >> 4) & 0xf;
|
|
|
|
unsigned int lower = c & 0xf;
|
|
|
|
|
|
|
|
htif_putchar(upper < 10 ? '0' + upper : 'a' - 10 + upper);
|
|
|
|
htif_putchar(lower < 10 ? '0' + lower : 'a' - 10 + lower);
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
void htif_putinthex(const uint32_t n)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
2023-02-27 10:37:35 -05:00
|
|
|
uint8_t *buf = (uint8_t *)&n;
|
2022-09-19 02:51:11 -04:00
|
|
|
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_puts("0x");
|
2022-09-19 02:51:11 -04:00
|
|
|
for (int i = 3; i > -1; i--) {
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_puthex(buf[i]);
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *memset(void *dest, int c, unsigned n)
|
|
|
|
{
|
|
|
|
uint8_t *s = dest;
|
|
|
|
|
|
|
|
for (; n; n--, s++)
|
|
|
|
*s = c;
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:00:23 -05:00
|
|
|
void memcpy_s(void *dest, size_t destsize, const void *src, size_t n)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
2023-02-27 11:00:23 -05:00
|
|
|
assert(dest != NULL);
|
|
|
|
assert(src != NULL);
|
|
|
|
assert(destsize >= n);
|
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
uint8_t *src_byte = (uint8_t *)src;
|
|
|
|
uint8_t *dest_byte = (uint8_t *)dest;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
dest_byte[i] = src_byte[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:00:23 -05:00
|
|
|
void wordcpy_s(void *dest, size_t destsize, const void *src, size_t n)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
2023-02-27 11:00:23 -05:00
|
|
|
assert(dest != NULL);
|
|
|
|
assert(src != NULL);
|
|
|
|
assert(destsize >= n);
|
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
uint32_t *src_word = (uint32_t *)src;
|
|
|
|
uint32_t *dest_word = (uint32_t *)dest;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
dest_word[i] = src_word[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int memeq(void *dest, const void *src, unsigned n)
|
|
|
|
{
|
|
|
|
uint8_t *src_byte = (uint8_t *)src;
|
|
|
|
uint8_t *dest_byte = (uint8_t *)dest;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
if (dest_byte[i] != src_byte[i]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|