/*
  Copyright (c) 2011  John Lee (j.y.lee@yeah.net)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

  * 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.

  * Neither the name of the copyright holders nor the names of
    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 OWNER 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 __SEMIHOSTING_H
#define __SEMIHOSTING_H

#include <cstdint>

namespace semihosting {
	class console_t {
	public:
		__attribute__((always_inline)) inline console_t();
		__attribute__((always_inline)) inline int write(const void* str, uintptr_t len);
		__attribute__((always_inline)) inline int read(void* str, uintptr_t len);
		uintptr_t handle;
	};

	enum operation_t {
		SYS_OPEN = 1,
		SYS_WRITE = 5,
		SYS_READ = 6,
	};

	__attribute__((always_inline)) static inline intptr_t call0(operation_t op, const void* arg1 = 0);
	template<typename T1>
	__attribute__((always_inline)) static inline intptr_t call(operation_t op, T1 arg1);
	template<typename T1, typename T2>
	__attribute__((always_inline)) static inline intptr_t call(operation_t op, T1 arg1, T2 arg2);
	template<typename T1, typename T2, typename T3>
	__attribute__((always_inline)) static inline intptr_t call(operation_t op, T1 arg1, T2 arg2, T3 arg3);
	template<typename T1, typename T2, typename T3, typename T4>
	__attribute__((always_inline)) static inline intptr_t call(operation_t op, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}

inline intptr_t semihosting::call0(operation_t op, const void* arg1)
{
	register const void* p asm ("a2") = arg1;
	__asm__ __volatile__("" ::: "memory");
	register uintptr_t c asm ("a1") = op;
	register uintptr_t r asm ("r0");
	__asm__ __volatile__(
		"bkpt #0xab"
		: "=r" (r)
		: "0" (c), "r" (p)
	);
	return r;
}

template<typename T1>
inline intptr_t semihosting::call(operation_t op, T1 arg1)
{
	uintptr_t block[1] = { uintptr_t(arg1) };
	return call0(op, block);
}

template<typename T1, typename T2>
inline intptr_t semihosting::call(operation_t op, T1 arg1, T2 arg2)
{
	uintptr_t block[2] = { uintptr_t(arg1), uintptr_t(arg2) };
	return call0(op, block);
}

template<typename T1, typename T2, typename T3>
inline intptr_t semihosting::call(operation_t op, T1 arg1, T2 arg2, T3 arg3)
{
	uintptr_t block[3] = { uintptr_t(arg1), uintptr_t(arg2), uintptr_t(arg3) };
	return call0(op, block);
}

template<typename T1, typename T2, typename T3, typename T4>
inline intptr_t semihosting::call(operation_t op, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
	uintptr_t block[4] = { uintptr_t(arg1), uintptr_t(arg2), uintptr_t(arg3), uintptr_t(arg4) };
	return call0(op, block);
}

inline semihosting::console_t::console_t()
{
	handle = call(SYS_OPEN, ":tt", 3, 3);
}

inline int semihosting::console_t::write(const void* buffer, uintptr_t len)
{
	return call(SYS_WRITE, handle, buffer, len);
}

inline int semihosting::console_t::read(void* buffer, uintptr_t len)
{
	return call(SYS_READ, handle, buffer, len);
}

#endif	// __SEMIHOSTING_H
