Add some delay to slow things down.
[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.5  2006/05/27 22:30:56  bernie
19  *#* Add some delay to slow things down.
20  *#*
21  *#* Revision 1.4  2006/02/24 01:35:40  bernie
22  *#* Update for new emulator.
23  *#*
24  *#* Revision 1.3  2006/02/20 02:00:40  bernie
25  *#* Port to Qt 4.1.
26  *#*
27  *#* Revision 1.2  2006/01/16 03:51:51  bernie
28  *#* Fix boilerplate.
29  *#*
30  *#* Revision 1.1  2006/01/16 03:37:12  bernie
31  *#* Add emulator skeleton.
32  *#*
33  *#*/
34
35 #include "emul.h"
36 #include "emulwin.h"
37 #include <cfg/module.h>
38
39 #include <appconfig.h>
40 #if CONFIG_KERNEL
41         #include <config_kern.h>
42 #endif
43
44
45 #include <cstdlib> // std::exit()
46
47 #if _QT < 4
48         #include <qapplication.h>
49 #else
50         #include <QtGui/qapplication.h>
51 #endif
52
53
54 /// The global emulator instance.
55 Emulator *emul;
56
57 #if CONFIG_KERNEL
58         #include <mware/list.h>
59
60         /// List of process stacks
61         List StackFreeList;
62
63         // HACK: Reserve 64KB of stack space for kernel processes
64         const int NPROC = 8;
65         int stacks[NPROC][(64 * 1024) / sizeof(int)];
66 #endif
67
68 Emulator::Emulator(int &argc, char **argv) :
69         emulApp(new QApplication(argc, argv)),
70         emulWin(new EmulWin(this))
71 {
72         #if QT_VERSION < ((4 << 16) + (0 << 8) + 0)
73                 emulApp->setMainWidget(emulWin);
74         #endif
75         emulWin->show();
76 }
77
78
79 Emulator::~Emulator()
80 {
81         // we don't delete emulWin because it automatically
82         // deletes itself when closed
83         delete emulApp;
84 }
85
86
87 NORETURN void Emulator::quit()
88 {
89         // WHAT A KLUDGE!
90         this->~Emulator();
91         emul = NULL;
92
93         // do we have a better way to shut down the emulation?
94         exit(0);
95 }
96
97 MOD_DEFINE(emul)
98
99 /// Main emulator entry point.
100 extern "C" void emul_init(int *argc, char *argv[])
101 {
102         // setup global emulator pointer
103         emul = new Emulator(*argc, argv);
104
105 #if CONFIG_KERNEL
106         LIST_INIT(&StackFreeList);
107         for (int i = 0; i < NPROC; i++)
108                 ADDTAIL(&StackFreeList, (Node *)stacks[i]);
109 #endif
110
111         MOD_INIT(emul);
112 }
113
114 extern "C" void emul_cleanup()
115 {
116         MOD_CLEANUP(emul);
117
118         delete emul;
119         emul = NULL;
120 }
121
122 extern "C" void emul_idle()
123 {
124         // We process GUI events when the application is idle.
125         emul->emulApp->processEvents();
126         usleep(1000);
127 }
128