Update for new emulator.
[bertos.git] / emul / emul.cpp
1 /**
2  * \file
3  * <!--
4  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000, 2001 Bernardo Innocenti <bernie@codewiz.org>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief Qt-based emulator framework for embedded applications (implementation)
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.4  2006/02/24 01:35:40  bernie
19  *#* Update for new emulator.
20  *#*
21  *#* Revision 1.3  2006/02/20 02:00:40  bernie
22  *#* Port to Qt 4.1.
23  *#*
24  *#* Revision 1.2  2006/01/16 03:51:51  bernie
25  *#* Fix boilerplate.
26  *#*
27  *#* Revision 1.1  2006/01/16 03:37:12  bernie
28  *#* Add emulator skeleton.
29  *#*
30  *#*/
31
32 #include "emul.h"
33 #include "emulwin.h"
34 #include <cfg/module.h>
35
36 #include <appconfig.h>
37 #if CONFIG_KERNEL
38         #include <config_kern.h>
39 #endif
40
41
42 #include <cstdlib> // std::exit()
43
44 #if _QT < 4
45         #include <qapplication.h>
46 #else
47         #include <QtGui/qapplication.h>
48 #endif
49
50
51 /// The global emulator instance.
52 Emulator *emul;
53
54 #if CONFIG_KERNEL
55         #include <mware/list.h>
56
57         /// List of process stacks
58         List StackFreeList;
59
60         // HACK: Reserve 64KB of stack space for kernel processes
61         const int NPROC = 8;
62         int stacks[NPROC][(64 * 1024) / sizeof(int)];
63 #endif
64
65 Emulator::Emulator(int &argc, char **argv) :
66         emulApp(new QApplication(argc, argv)),
67         emulWin(new EmulWin(this))
68 {
69         #if QT_VERSION < ((4 << 16) + (0 << 8) + 0)
70                 emulApp->setMainWidget(emulWin);
71         #endif
72         emulWin->show();
73 }
74
75
76 Emulator::~Emulator()
77 {
78         // we don't delete emulWin because it automatically
79         // deletes itself when closed
80         delete emulApp;
81 }
82
83
84 NORETURN void Emulator::quit()
85 {
86         // WHAT A KLUDGE!
87         this->~Emulator();
88         emul = NULL;
89
90         // do we have a better way to shut down the emulation?
91         exit(0);
92 }
93
94 MOD_DEFINE(emul)
95
96 /// Main emulator entry point.
97 extern "C" void emul_init(int *argc, char *argv[])
98 {
99         // setup global emulator pointer
100         emul = new Emulator(*argc, argv);
101
102 #if CONFIG_KERNEL
103         LIST_INIT(&StackFreeList);
104         for (int i = 0; i < NPROC; i++)
105                 ADDTAIL(&StackFreeList, (Node *)stacks[i]);
106 #endif
107
108         MOD_INIT(emul);
109 }
110
111 extern "C" void emul_cleanup()
112 {
113         MOD_CLEANUP(emul);
114
115         delete emul;
116         emul = NULL;
117 }
118
119 extern "C" void emul_idle()
120 {
121         // We process GUI events when the application is idle.
122         emul->emulApp->processEvents();
123 }
124