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