29ab59db00bb5769ae5e13654ebd127ce43bba7d
[bertos.git] / bertos / emul / emul.cpp
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000, 2001 Bernardo Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \version $Id$
35  *
36  * \author Bernardo Innocenti <bernie@develer.com>
37  *
38  * \brief Qt-based emulator framework for embedded applications (implementation)
39  */
40
41 #include "emul.h"
42 #include "emulwin.h"
43
44 #include "cfg/cfg_kern.h"
45
46 #include <cfg/module.h>
47
48
49
50 #include <cstdlib> // std::exit()
51
52 #include <QtGui/qapplication.h>
53
54
55 /// The global emulator instance.
56 Emulator *emul;
57
58 #if CONFIG_KERNEL
59         #include <mware/list.h>
60
61         /// List of process stacks
62         List StackFreeList;
63
64         // HACK: Reserve 64KB of stack space for kernel processes
65         const int NPROC = 8;
66         int stacks[NPROC][(64 * 1024) / sizeof(int)];
67 #endif
68
69 Emulator::Emulator(int &argc, char **argv) :
70         emulApp(new QApplication(argc, argv)),
71         emulWin(new EmulWin(this))
72 {
73         emulWin->show();
74 }
75
76
77 Emulator::~Emulator()
78 {
79         // we don't delete emulWin because it automatically
80         // deletes itself when closed
81         delete emulApp;
82 }
83
84
85 NORETURN void Emulator::quit()
86 {
87         // WHAT A KLUDGE!
88         this->~Emulator();
89         emul = NULL;
90
91         // do we have a better way to shut down the emulation?
92         exit(0);
93 }
94
95 MOD_DEFINE(emul)
96
97 /// Main emulator entry point.
98 extern "C" void emul_init(int *argc, char *argv[])
99 {
100         // setup global emulator pointer
101         emul = new Emulator(*argc, argv);
102
103 #if CONFIG_KERNEL
104         LIST_INIT(&StackFreeList);
105         for (int i = 0; i < NPROC; i++)
106                 ADDTAIL(&StackFreeList, (Node *)stacks[i]);
107 #endif
108
109         MOD_INIT(emul);
110 }
111
112 extern "C" void emul_cleanup()
113 {
114         MOD_CLEANUP(emul);
115
116         delete emul;
117         emul = NULL;
118 }
119
120 extern "C" void emul_idle()
121 {
122         // We process GUI events when the application is idle.
123         emul->emulApp->processEvents();
124         usleep(1000);
125 }
126