all repos — mgba @ 045417b5096f12301657f5c5d24ccd5769b36551

mGBA Game Boy Advance Emulator

opt/libgba/mgba.c (view raw)

 1/*
 2 mgba.h
 3 Copyright (c) 2016 Jeffrey Pfau
 4
 5 Redistribution and use in source and binary forms, with or without modification,
 6 are permitted provided that the following conditions are met:
 7  1. Redistributions of source code must retain the above copyright notice,
 8     this list of conditions and the following disclaimer.
 9  2. Redistributions in binary form must reproduce the above copyright notice,
10     this list of conditions and the following disclaimer in the documentation and/or
11     other materials provided with the distribution.
12 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
14 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
15 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
17 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
18 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
20 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21*/
22
23#include <sys/iosupport.h>
24#include <gba_types.h>
25
26#include <stdarg.h>
27#include <stdio.h>
28#include <string.h>
29#include <mgba.h>
30
31#define REG_DEBUG_ENABLE (vu16*) 0x4FFF780
32#define REG_DEBUG_FLAGS (vu16*) 0x4FFF700
33#define REG_DEBUG_STRING (char*) 0x4FFF600
34
35ssize_t mgba_stdout_write(struct _reent* r __attribute__((unused)), void* fd __attribute__((unused)), const char* ptr, size_t len) {
36	if (len > 0x100) {
37		len = 0x100;
38	}
39	strncpy(REG_DEBUG_STRING, ptr, len);
40	*REG_DEBUG_FLAGS = MGBA_LOG_INFO | 0x100;
41	return len;
42}
43
44ssize_t mgba_stderr_write(struct _reent* r __attribute__((unused)), void* fd __attribute__((unused)), const char* ptr, size_t len) {
45	if (len > 0x100) {
46		len = 0x100;
47	}
48	strncpy(REG_DEBUG_STRING, ptr, len);
49	*REG_DEBUG_FLAGS = MGBA_LOG_ERROR | 0x100;
50	return len;
51}
52
53void mgba_printf(int level, const char* ptr, ...) {
54	level &= 0x7;
55	va_list args;
56	va_start(args, ptr);
57	vsnprintf(REG_DEBUG_STRING, 0x100, ptr, args);
58	va_end(args);
59	*REG_DEBUG_FLAGS = level | 0x100;
60}
61
62static const devoptab_t dotab_mgba_stdout = {
63	"mgba_stdout",
64	0,
65	NULL,
66	NULL,
67	mgba_stdout_write
68};
69
70static const devoptab_t dotab_mgba_stderr = {
71	"mgba_stderr",
72	0,
73	NULL,
74	NULL,
75	mgba_stderr_write
76};
77
78bool mgba_console_open(void) {
79	if (!mgba_open()) {
80		return false;
81	}
82	devoptab_list[STD_OUT] = &dotab_mgba_stdout;
83	devoptab_list[STD_ERR] = &dotab_mgba_stderr;
84	return true;
85}
86
87bool mgba_open(void) {
88	*REG_DEBUG_ENABLE = 0xC0DE;
89	return *REG_DEBUG_ENABLE == 0x1DEA;
90}
91
92void mgba_close(void) {
93	*REG_DEBUG_ENABLE = 0;
94}