-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.cpp
159 lines (140 loc) · 4.81 KB
/
kernel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// kernel.cpp
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2014-2021 R. Stange <[email protected]>
//
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "kernel.h"
#include "msx2def.h"
#include <circle/multicore.h>
// defined in multicoremanager.cpp
int16_t* getPreviousSoundBuffer(size_t* size);
#define TAG "kernel"
int msxPad1 = 0;
uint16_t* hdmiBuffer;
int hdmiPitch;
CVCHIQSoundDevice* hdmiSoundDevice;
CLogger* clogger;
CKernel::CKernel(void) : screen(640, 480),
timer(&interrupt),
logger(options.GetLogLevel(), &timer),
usb(&interrupt, &timer, TRUE),
vchiq(CMemorySystem::Get(), &interrupt),
sound(&vchiq, (TVCHIQSoundDestination)options.GetSoundOption()),
mcm(CMemorySystem::Get()),
gamePad(nullptr)
{
led.Blink(5); // show we are alive
}
CKernel::~CKernel(void)
{
}
boolean CKernel::initialize(void)
{
boolean bOK = TRUE;
led.On();
if (bOK) {
bOK = screen.Initialize();
}
if (bOK) {
bOK = serial.Initialize(115200);
}
if (bOK) {
CDevice* target = deviceNameService.GetDevice(options.GetLogDevice(), FALSE);
if (target == 0) {
target = &screen;
}
bOK = logger.Initialize(target);
}
if (bOK) {
logger.Write(TAG, LogNotice, "init interrupt");
bOK = interrupt.Initialize();
}
if (bOK) {
logger.Write(TAG, LogNotice, "init timer");
bOK = timer.Initialize();
}
if (bOK) {
logger.Write(TAG, LogNotice, "init vchiq");
bOK = vchiq.Initialize();
}
if (bOK) {
logger.Write(TAG, LogNotice, "init usb");
bOK = usb.Initialize();
}
auto buffer = screen.GetFrameBuffer();
hdmiPitch = buffer->GetPitch() / sizeof(TScreenColor);
uint64_t bufferPointer = buffer->GetBuffer();
hdmiBuffer = (uint16_t*)bufferPointer;
if (bOK) {
logger.Write(TAG, LogNotice, "init mcm");
clogger = &logger;
bOK = mcm.Initialize();
}
if (bOK) {
led.Off();
}
return bOK;
}
TShutdownMode CKernel::run(void)
{
sound.SetControl(VCHIQ_SOUND_VOLUME_MAX);
hdmiSoundDevice = &sound;
int swap = 0;
while (1) {
// peripheral devices proc
if (usb.UpdatePlugAndPlay()) {
if (!gamePad) {
gamePad = (CUSBGamePadDevice*)deviceNameService.GetDevice("upad1", FALSE);
if (gamePad) {
gamePad->RegisterStatusHandler([](unsigned index, const TGamePadState* state) {
msxPad1 = 0;
msxPad1 |= state->axes[0].value == state->axes[0].minimum ? MSX2_JOY_LE : 0;
msxPad1 |= state->axes[0].value == state->axes[0].maximum ? MSX2_JOY_RI : 0;
msxPad1 |= state->axes[1].value == state->axes[1].minimum ? MSX2_JOY_UP : 0;
msxPad1 |= state->axes[1].value == state->axes[1].maximum ? MSX2_JOY_DW : 0;
msxPad1 |= (state->buttons & 0x0001) ? MSX2_JOY_T2 : 0;
msxPad1 |= (state->buttons & 0x0002) ? MSX2_JOY_T1 : 0;
msxPad1 |= (state->buttons & 0x0100) ? MSX2_JOY_S2 : 0;
msxPad1 |= (state->buttons & 0x0200) ? MSX2_JOY_S1 : 0;
});
}
} else {
if (!(CUSBGamePadDevice*)deviceNameService.GetDevice("upad1", FALSE)) {
gamePad = nullptr;
msxPad1 = 0;
}
}
}
// wait v-sync
swap = 480 - swap;
screen.GetFrameBuffer()->SetVirtualOffset(0, swap);
screen.GetFrameBuffer()->WaitForVerticalSync();
// output sound
size_t pcmSize;
int16_t* pcmData = getPreviousSoundBuffer(&pcmSize);
if (0 < pcmSize) {
// playback
while (sound.PlaybackActive()) {
;
}
sound.Playback(pcmData, pcmSize / 4, 2, 16);
}
// request MSX tick to core-1
CMultiCoreSupport::SendIPI(1, IPI_USER + 0);
}
return ShutdownHalt;
}