Move Console to a library: We can now compile a standard Hello World program.

This commit is contained in:
Wolfgang Thaller 2014-09-17 03:35:18 +02:00
parent d83818476d
commit 534ef17a1f
9 changed files with 201 additions and 64 deletions

View File

@ -1,4 +1,4 @@
# Copyright 2012 Wolfgang Thaller.
# Copyright 2014 Wolfgang Thaller.
#
# This file is part of Retro68.
#
@ -17,11 +17,22 @@
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-std=c++11") # -fomit-frame-pointer")
add_library(RetroConsole
Console.cc
Console.h
MacUtils.h
InitConsole.cc
)
add_executable(Test
test.cc
Console.cc
Console.h
MacUtils.h
test.cc
Console.cc
Console.h
MacUtils.h
)
add_executable(HelloWorld
hello.c
)
#target_link_libraries(Test :retrocrt.o)
@ -32,6 +43,14 @@ add_custom_command(
DEPENDS Test)
add_custom_target(TestAPPL ALL DEPENDS Test.bin)
target_link_libraries(HelloWorld RetroConsole retrocrt)
add_custom_command(
OUTPUT HelloWorld.bin
COMMAND ${MAKE_APPL} -c HelloWorld -o HelloWorld
DEPENDS HelloWorld)
add_custom_target(HelloWorldAPPL ALL DEPENDS HelloWorld.bin)
set(UPLOAD_URL "" CACHE STRING "ftp url to upload to")
if(UPLOAD_URL)
add_custom_target(upload

83
App2/InitConsole.cc Normal file
View File

@ -0,0 +1,83 @@
/*
Copyright 2014 Wolfgang Thaller.
This file is part of Retro68.
Retro68 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Retro68 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Retro68. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <Quickdraw.h>
#include <MacMemory.h>
#include <Sound.h>
#include <Events.h>
#include <Fonts.h>
#include <sys/types.h>
#include <string.h>
#include "MacUtils.h"
#include "Console.h"
QDGlobals qd;
void InitConsole()
{
if(Console::currentInstance)
return;
WindowPtr win;
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
Rect r;
SetRect(&r, qd.screenBits.bounds.left + 5, qd.screenBits.bounds.top + 45, qd.screenBits.bounds.right - 5, qd.screenBits.bounds.bottom -5);
win = NewWindow(NULL, &r, PSTR("Retro68 Console"), true, 0, (WindowPtr)-1, false, 0);
SetPort(win);
EraseRect(&win->portRect);
Console *console = new Console(win, win->portRect);
}
extern "C" ssize_t consolewrite(int fd, const void *buf, size_t count)
{
if(!Console::currentInstance)
InitConsole();
const char *p = (const char*)buf;
for(int i = 0; i < count; i++)
Console::currentInstance->putch(*p++);
return count;
}
extern "C" ssize_t consoleread(int fd, void *buf, size_t count)
{
if(!Console::currentInstance)
InitConsole();
static std::string consoleBuf;
if(consoleBuf.size() == 0)
{
consoleBuf = Console::currentInstance->ReadLine() + "\n";
}
if(count > consoleBuf.size())
count = consoleBuf.size();
memcpy(buf, consoleBuf.data(), count);
consoleBuf = consoleBuf.substr(count);
return count;
}

28
App2/hello.c Normal file
View File

@ -0,0 +1,28 @@
/*
Copyright 2014 Wolfgang Thaller.
This file is part of Retro68.
Retro68 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Retro68 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Retro68. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Hello, world.\n");
printf("\n(Press Return)\n");
getchar();
return 0;
}

View File

@ -1,4 +1,4 @@
# Copyright 2012 Wolfgang Thaller.
# Copyright 2014 Wolfgang Thaller.
#
# This file is part of Retro68.
#
@ -35,8 +35,8 @@ if(APPLE)
target_link_libraries(Raytracer "-framework Carbon")
target_link_libraries(Raytracer2 "-framework Carbon")
else()
add_executable(FixedBenchmark fixedbenchmark.cc ../App2/Console.cc ../App2/Console.h fixed.h fixed.cc)
target_link_libraries(FixedBenchmark retrocrt)
add_executable(FixedBenchmark fixedbenchmark.cc fixed.h fixed.cc)
target_link_libraries(FixedBenchmark RetroConsole retrocrt)
add_custom_command(
OUTPUT FixedBenchmark.bin
COMMAND ${MAKE_APPL} -c FixedBenchmark -o FixedBenchmark

View File

@ -1,5 +1,5 @@
/*
Copyright 2012 Wolfgang Thaller.
Copyright 2014 Wolfgang Thaller.
This file is part of Retro68.
@ -20,30 +20,14 @@
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <Quickdraw.h>
#include <MacMemory.h>
#include <Sound.h>
#include <Events.h>
#include <Fonts.h>
#include "MacUtils.h"
#include "Console.h"
QDGlobals qd;
extern ssize_t (*__write_hook)(int fd, const void*buf, size_t count);
extern "C" ssize_t consolewrite(int fd, const void *buf, size_t count)
{
const char *p = (const char*)buf;
for(int i = 0; i < count; i++)
Console::currentInstance->putch(*p++);
return count;
}
#include "fixed.h"
#include <cmath>
using std::sqrt;
class timer
{
@ -52,38 +36,17 @@ public:
timer() : t(TickCount()) {}
float elapsed() { return (TickCount() - t) / 60.15f; }
};
int main(int argc, char** argv)
template<class number>
void runTests(std::string type, std::vector<number>& numbers)
{
//GrafPort port;
WindowPtr win;
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
Rect r;
SetRect(&r, qd.screenBits.bounds.left + 5, qd.screenBits.bounds.top + 45, qd.screenBits.bounds.right - 5, qd.screenBits.bounds.bottom -5);
win = NewWindow(NULL, &r, PSTR("Retro68 Console"), true, 0, (WindowPtr)-1, false, 0);
SetPort(win);
EraseRect(&win->portRect);
new char[32];
Console console(win, win->portRect);
__write_hook = &consolewrite;
std::cout << "Hello, world.\n";
std::cout << "Generating numbers..." << std::flush;
const int n = 1000;
std::vector<fixed> numbers(n);
for(int i = 0; i < numbers.size(); i++)
numbers[i] = fixed(std::rand(), fixed::raw());
std::vector<fixed> outputs(n);
std::cout << "done.\n";
std::cout << "***********************************\n";
std::cout << "Running tests on type " << type << ":\n";
std::cout << "***********************************\n";
int n = numbers.size();
std::vector<number> outputs(n);
std::cout << "Testing Multiplication..." << std::flush;
{
timer t;
@ -93,7 +56,7 @@ int main(int argc, char** argv)
}
std::cout << 1000 * t.elapsed() / n << "ms\n";
}
std::cout << "Testing Division..." << std::flush;
{
timer t;
@ -113,10 +76,36 @@ int main(int argc, char** argv)
}
std::cout << 1000 * t.elapsed() / n << "ms\n";
}
std::cout << std::endl;
}
int main(int argc, char** argv)
{
std::cout << "Hello, world.\n";
std::cout << "Generating numbers..." << std::flush;
const int n = 1000;
std::vector<fixed> numbers(n);
std::vector<float> floats(n);
std::vector<double> doubles(n);
for(int i = 0; i < numbers.size(); i++)
{
numbers[i] = fixed(std::rand(), fixed::raw());
floats[i] = float(std::rand()) / RAND_MAX;
doubles[i] = double(std::rand()) / RAND_MAX;
}
std::vector<fixed> outputs(n);
std::cout << "done.\n\n";
runTests("float", floats);
runTests("double", doubles);
runTests("fixed", numbers);
std::cout << "Press Enter to Exit ;-)\n";
console.ReadLine();
std::cin.get();
return 0;
}

View File

@ -27,6 +27,6 @@ add_custom_command(
COMMAND ${CMAKE_LINKER} -r -o retrocrt.o
--whole-archive libretrocrt_multi.a
)
add_library(retrocrt retrocrt.o glue.c)
add_library(retrocrt retrocrt.o glue.c consolehooks.c)
#add_custom_target(retrocrt_o ALL DEPENDS retrocrt.o)
install(TARGETS retrocrt DESTINATION lib)

11
libretro/consolehooks.c Normal file
View File

@ -0,0 +1,11 @@
#include <sys/types.h>
__attribute__((weak)) ssize_t consolewrite(int fd, const void *buf, size_t count)
{
return -1;
}
__attribute__((weak)) ssize_t consoleread(int fd, void *buf, size_t count)
{
return -1;
}

View File

@ -42,10 +42,15 @@ void _exit(int status)
ssize_t (*__write_hook)(int fd, const void*buf, size_t count) = NULL;
ssize_t (*__read_hook)(int fd, void*buf, size_t count) = NULL;
ssize_t consolewrite(int fd, const void *buf, size_t count);
ssize_t consoleread(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count)
{
if(__write_hook)
return (*__write_hook)(fd,buf,count);
else
return consolewrite(fd,buf,count);
return -1;
}
@ -53,6 +58,8 @@ ssize_t read(int fd, void *buf, size_t count)
{
if(__read_hook)
return (*__read_hook)(fd,buf,count);
else
return consoleread(fd,buf,count);
return -1;
}

View File

@ -26,5 +26,5 @@ set( MAKE_APPL "${RETRO68_ROOT}/bin/MakeAPPL" )
set( CMAKE_C_COMPILER "${RETRO68_ROOT}/bin/m68k-unknown-elf-gcc" )
set( CMAKE_CXX_COMPILER "${RETRO68_ROOT}/bin/m68k-unknown-elf-g++" )
set( CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-elf2flt -Wl,-q -Wl,-Map=linkmap.txt" )
set( CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,-elf2flt -Wl,-q -Wl,-Map=linkmap.txt -Wl,-undefined=consolewrite" )