-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUserPortBreakoutBox.cpp
473 lines (387 loc) · 11.5 KB
/
UserPortBreakoutBox.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/****************************************************************
BeebEm - BBC Micro and Master 128 Emulator
Copyright (C) 1994 David Alan Gilbert
Copyright (C) 1997 Mike Wyatt
Copyright (C) 2004 Ken Lowe
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 2
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, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
****************************************************************/
#include "UserPortBreakoutBox.h"
#include "beebemrc.h"
#include "main.h"
#include "Messages.h"
#include "SelectKeyDialog.h"
#include "uservia.h"
#include <Carbon/Carbon.h>
/****************************************************************************/
/* User Port Breakout Box */
#ifdef BEEBWIN
UserPortBreakoutDialog* userPortBreakoutDialog = nullptr;
#else
UserPortBreakoutDialog* userPortBreakoutDialog = new UserPortBreakoutDialog();
#endif
int BitKeys[8] = { 48, 49, 50, 51, 52, 53, 54, 55 };
static const UINT BitKeyButtonIDs[8] = {
IDK_BIT0, IDK_BIT1, IDK_BIT2, IDK_BIT3,
IDK_BIT4, IDK_BIT5, IDK_BIT6, IDK_BIT7,
};
/****************************************************************************/
UserPortBreakoutDialog::UserPortBreakoutDialog(
#ifdef BEEBWIN
HINSTANCE hInstance,
HWND hwndParent) :
m_hInstance(hInstance),
m_hwnd(nullptr),
m_hwndParent(hwndParent),
#else
):
#endif
m_BitKey(0),
m_LastInputData(0),
m_LastOutputData(0)
{
}
/****************************************************************************/
#ifdef BEEBWIN
bool UserPortBreakoutDialog::Open()
{
if (m_hwnd != nullptr)
{
// The dialog box is already open.
return false;
}
m_hwnd = CreateDialogParam(
m_hInstance,
MAKEINTRESOURCE(IDD_BREAKOUT),
m_hwndParent,
sDlgProc,
reinterpret_cast<LPARAM>(this)
);
if (m_hwnd != nullptr)
{
hCurrentDialog = m_hwnd;
return true;
}
return false;
}
/****************************************************************************/
void UserPortBreakoutDialog::Close()
{
if (m_hwnd == nullptr)
{
return;
}
if (selectKeyDialog != nullptr)
{
selectKeyDialog->Close(IDCANCEL);
}
EnableWindow(m_hwndParent, TRUE);
DestroyWindow(m_hwnd);
m_hwnd = nullptr;
hCurrentDialog = nullptr;
PostMessage(m_hwndParent, WM_USER_PORT_BREAKOUT_DIALOG_CLOSED, 0, 0);
}
#else
void BreakoutBoxOpenDialog()
{
//WM_INITDIALOG
for (int i = 0; i < _countof(BitKeyButtonIDs); i++)
{
userPortBreakoutDialog->ShowBitKey(i, BitKeyButtonIDs[i]);
}
}
void BreakoutBoxCloseDialog()
{
}
#endif
/****************************************************************************/
bool UserPortBreakoutDialog::KeyDown(int Key)
{
int mask = 0x01;
bool bit = false;
for (int i = 0; i < 8; ++i)
{
if (BitKeys[i] == Key)
{
if ((UserVIAState.ddrb & mask) == 0x00)
{
UserVIAState.irb &= ~mask;
ShowInputs((UserVIAState.orb & UserVIAState.ddrb) | (UserVIAState.irb & (~UserVIAState.ddrb)));
bit = true;
}
}
mask <<= 1;
}
return bit;
}
/****************************************************************************/
bool UserPortBreakoutDialog::KeyUp(int Key)
{
int mask = 0x01;
bool bit = false;
for (int i = 0; i < 8; ++i)
{
if (BitKeys[i] == Key)
{
if ((UserVIAState.ddrb & mask) == 0x00)
{
UserVIAState.irb |= mask;
ShowInputs((UserVIAState.orb & UserVIAState.ddrb) | (UserVIAState.irb & (~UserVIAState.ddrb)));
bit = true;
}
}
mask <<= 1;
}
return bit;
}
#ifdef BEEBWIN
/****************************************************************************/
INT_PTR CALLBACK UserPortBreakoutDialog::sDlgProc(
HWND hwnd,
UINT nMessage,
WPARAM wParam,
LPARAM lParam)
{
UserPortBreakoutDialog* dialog;
if (nMessage == WM_INITDIALOG)
{
SetWindowLongPtr(hwnd, DWLP_USER, lParam);
dialog = reinterpret_cast<UserPortBreakoutDialog*>(lParam);
}
else
{
dialog = reinterpret_cast<UserPortBreakoutDialog*>(
GetWindowLongPtr(hwnd, DWLP_USER)
);
}
return dialog->DlgProc(hwnd, nMessage, wParam, lParam);
}
INT_PTR UserPortBreakoutDialog::DlgProc(
HWND /* hwnd */,
UINT nMessage,
WPARAM wParam,
LPARAM /* lParam */)
{
bool bit;
switch (nMessage)
{
case WM_INITDIALOG:
for (int i = 0; i < _countof(BitKeyButtonIDs); i++)
{
ShowBitKey(i, BitKeyButtonIDs[i]);
}
return TRUE;
case WM_COMMAND:
switch (wParam)
{
case IDOK:
case IDCANCEL:
Close();
return TRUE;
case IDK_BIT0:
PromptForBitKeyInput(0);
break;
case IDK_BIT1:
PromptForBitKeyInput(1);
break;
case IDK_BIT2:
PromptForBitKeyInput(2);
break;
case IDK_BIT3:
PromptForBitKeyInput(3);
break;
case IDK_BIT4:
PromptForBitKeyInput(4);
break;
case IDK_BIT5:
PromptForBitKeyInput(5);
break;
case IDK_BIT6:
PromptForBitKeyInput(6);
break;
case IDK_BIT7:
PromptForBitKeyInput(7);
break;
case IDC_IB7:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x80) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x80; else UserVIAState.irb |= 0x80;
}
break;
case IDC_IB6:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x40) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x40; else UserVIAState.irb |= 0x40;
}
break;
case IDC_IB5:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x20) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x20; else UserVIAState.irb |= 0x20;
}
break;
case IDC_IB4:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x10) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x10; else UserVIAState.irb |= 0x10;
}
break;
case IDC_IB3:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x08) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x08; else UserVIAState.irb |= 0x08;
}
break;
case IDC_IB2:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x04) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x04; else UserVIAState.irb |= 0x04;
}
break;
case IDC_IB1:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x02) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x02; else UserVIAState.irb |= 0x02;
}
break;
case IDC_IB0:
bit = GetValue((int)wParam);
if ((UserVIAState.ddrb & 0x01) == 0x00)
{
if (bit) UserVIAState.irb &= ~0x01; else UserVIAState.irb |= 0x01;
}
break;
}
return TRUE;
case WM_SELECT_KEY_DIALOG_CLOSED:
if (wParam == IDOK)
{
return true;
}
return FALSE;
}
#endif
#ifdef BEEBWIN
int UserPortBreakoutDialog::Closed()
{
// Assign the BBC key to the PC key.
BitKeys[m_BitKey] = keycode_last;
ShowBitKey(m_BitKey, BitKeyButtonIDs[m_BitKey]);
return true;
}
#else
int UserPortBreakoutDialog::Closed()
{
printf("Assign the BBC key to the PC key \n");
ShowBitKey(m_BitKey, BitKeyButtonIDs[m_BitKey]);
return true;
}
#endif
/****************************************************************************/
bool UserPortBreakoutDialog::GetValue(int ctrlID)
{
#ifdef BEEBWIN
return SendDlgItemMessage(m_hwnd, ctrlID, BM_GETCHECK, 0, 0) == BST_CHECKED;
#else
printf("getvalue %d\n", ctrlID);
return false;
#endif
}
/****************************************************************************/
void UserPortBreakoutDialog::SetValue(int ctrlID, bool State)
{
#ifdef BEEBWIN
SendDlgItemMessage(m_hwnd, ctrlID, BM_SETCHECK, State ? 1 : 0, 0);
#else
printf("setvalue %d %d\n", ctrlID, State ? 1 : 0);
#endif
}
/****************************************************************************/
void UserPortBreakoutDialog::ShowOutputs(unsigned char data)
{
#ifdef BEEBWIN
if (m_hwnd != nullptr)
#endif
{
if (data != m_LastOutputData)
{
unsigned char changed_bits = data ^ m_LastOutputData;
if (changed_bits & 0x80) { if ((UserVIAState.ddrb & 0x80) == 0x80) SetValue(IDC_OB7, (data & 0x80) != 0); else SetValue(IDC_OB7, 0); }
if (changed_bits & 0x40) { if ((UserVIAState.ddrb & 0x40) == 0x40) SetValue(IDC_OB6, (data & 0x40) != 0); else SetValue(IDC_OB6, 0); }
if (changed_bits & 0x20) { if ((UserVIAState.ddrb & 0x20) == 0x20) SetValue(IDC_OB5, (data & 0x20) != 0); else SetValue(IDC_OB5, 0); }
if (changed_bits & 0x10) { if ((UserVIAState.ddrb & 0x10) == 0x10) SetValue(IDC_OB4, (data & 0x10) != 0); else SetValue(IDC_OB4, 0); }
if (changed_bits & 0x08) { if ((UserVIAState.ddrb & 0x08) == 0x08) SetValue(IDC_OB3, (data & 0x08) != 0); else SetValue(IDC_OB3, 0); }
if (changed_bits & 0x04) { if ((UserVIAState.ddrb & 0x04) == 0x04) SetValue(IDC_OB2, (data & 0x04) != 0); else SetValue(IDC_OB2, 0); }
if (changed_bits & 0x02) { if ((UserVIAState.ddrb & 0x02) == 0x02) SetValue(IDC_OB1, (data & 0x02) != 0); else SetValue(IDC_OB1, 0); }
if (changed_bits & 0x01) { if ((UserVIAState.ddrb & 0x01) == 0x01) SetValue(IDC_OB0, (data & 0x01) != 0); else SetValue(IDC_OB0, 0); }
m_LastOutputData = data;
}
}
}
/****************************************************************************/
void UserPortBreakoutDialog::ShowInputs(unsigned char data)
{
#ifdef BEEBWIN
if (m_hwnd != nullptr)
#endif
{
if (data != m_LastInputData)
{
unsigned char changed_bits = data ^ m_LastInputData;
if (changed_bits & 0x80) { if ((UserVIAState.ddrb & 0x80) == 0x00) SetValue(IDC_IB7, (data & 0x80) == 0); else SetValue(IDC_IB7, false); }
if (changed_bits & 0x40) { if ((UserVIAState.ddrb & 0x40) == 0x00) SetValue(IDC_IB6, (data & 0x40) == 0); else SetValue(IDC_IB6, false); }
if (changed_bits & 0x20) { if ((UserVIAState.ddrb & 0x20) == 0x00) SetValue(IDC_IB5, (data & 0x20) == 0); else SetValue(IDC_IB5, false); }
if (changed_bits & 0x10) { if ((UserVIAState.ddrb & 0x10) == 0x00) SetValue(IDC_IB4, (data & 0x10) == 0); else SetValue(IDC_IB4, false); }
if (changed_bits & 0x08) { if ((UserVIAState.ddrb & 0x08) == 0x00) SetValue(IDC_IB3, (data & 0x08) == 0); else SetValue(IDC_IB3, false); }
if (changed_bits & 0x04) { if ((UserVIAState.ddrb & 0x04) == 0x00) SetValue(IDC_IB2, (data & 0x04) == 0); else SetValue(IDC_IB2, false); }
if (changed_bits & 0x02) { if ((UserVIAState.ddrb & 0x02) == 0x00) SetValue(IDC_IB1, (data & 0x02) == 0); else SetValue(IDC_IB1, false); }
if (changed_bits & 0x01) { if ((UserVIAState.ddrb & 0x01) == 0x00) SetValue(IDC_IB0, (data & 0x01) == 0); else SetValue(IDC_IB0, false); }
m_LastInputData = data;
}
}
}
/****************************************************************************/
void UserPortBreakoutDialog::ShowBitKey(int key, int ctrlID)
{
#ifdef BEEBWIN
SetDlgItemText(m_hwnd, ctrlID, SelectKeyDialog::KeyName(BitKeys[key]));
#else
printf("showbitkey %d %s\n", ctrlID, SelectKeyDialog::KeyName(BitKeys[key]).c_str());
#endif
}
/****************************************************************************/
void UserPortBreakoutDialog::PromptForBitKeyInput(int bitKey)
{
m_BitKey = bitKey;
ShowBitKey(bitKey, BitKeyButtonIDs[bitKey]);
std::string UsedKey = SelectKeyDialog::KeyName(BitKeys[m_BitKey]);
#ifdef BEEBWIN
selectKeyDialog = new SelectKeyDialog(
m_hInstance,
m_hwnd,
"Press the key to use...",
UsedKey,
false
);
selectKeyDialog->Open();
#else
printf("Press the key to use... %d\n", UsedKey.c_str());
#endif
}