-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathromconfig.cpp
523 lines (450 loc) · 13.2 KB
/
romconfig.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/****************************************************************
BeebEm - BBC Micro and Master 128 Emulator
Copyright (C) 2009 Mike Wyatt
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.
****************************************************************/
// BeebWin ROM Configuration Dialog
#include <stdio.h>
#ifdef BEEBWIN
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#endif
#include "beebemrc.h"
#include "main.h"
#include "beebwin.h"
#include "beebmem.h"
#ifdef BEEBWIN
#include "filedialog.h"
#endif
#ifdef BEEBWIN
static HWND hWndROMList = NULL;
static HWND hWndModel = NULL;
#else
#define LPCSTR std::string
#define LPTSTR char *
#define LPARAM int
#define TRUE true
#define FALSE false
#define HWND int
static HWND hWndROMList = NULL;
static HWND hWndModel = NULL;
static HWND hwndDlg = NULL;
#endif
static Model nModel = Model::B;
static const LPCSTR szModel[] = { "BBC B", "Integra-B", "B Plus", "Master 128" };
static ROMConfigFile ROMCfg;
static char szDefaultROMPath[MAX_PATH] = {0};
static char szDefaultROMConfigPath[MAX_PATH] = {0};
static bool LoadROMConfigFile(HWND hWnd);
static bool SaveROMConfigFile(HWND hWnd);
static bool GetROMFile(HWND hWnd, char *pFileName);
#ifdef BEEBWIN
static INT_PTR CALLBACK ROMConfigDlgProc(
HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
#else
static void FillROMList();
extern "C" void swift_RCSetModelText(const LPTSTR n);
extern "C" int swift_RCGetSelectionMark();
extern "C" void swift_RCSetFocus();
extern "C" enum FileFilter { DISC, UEF, IFD, KEYBOARD, DISCFILE, HARDDRIVE, PRINTFILE, ROMCFG };
extern "C" int swift_GetFilesWithPreview (const char *path, int bytes, const char *directory, FileFilter exts, bool multiFiles);
extern "C" int swift_SaveFile (const char *path, int bytes, FileFilter exts);
RCItem beeb_RCTable[16][2];
#endif
static bool WriteROMFile(const char *filename, ROMConfigFile RomConfig);
/****************************************************************************/
void BeebWin::EditROMConfig(void)
{
// Copy Rom config
memcpy(&ROMCfg, &RomConfig, sizeof(ROMConfigFile));
#ifdef BEEBWIN
INT_PTR nResult = DialogBox(hInst, MAKEINTRESOURCE(IDD_ROMCONFIG), m_hWnd, ROMConfigDlgProc);
if (nResult == TRUE)
{
// Copy in new config and read ROMs
memcpy(&RomConfig, &ROMCfg, sizeof(ROMConfigFile));
BeebReadRoms();
}
#else
FillROMList();
#endif
}
#ifndef BEEBWIN
void BeebWin::FinishROMConfig(void)
{
// Copy in new config and read ROMs
memcpy(&RomConfig, &ROMCfg, sizeof(ROMConfigFile));
BeebReadRoms();
}
#endif
#ifdef BEEBWIN
/****************************************************************************/
static int LVInsertColumn(
HWND hWnd, UINT uCol, const LPTSTR pszText, int iAlignment, UINT uWidth)
{
LVCOLUMN lc = {0};
lc.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
lc.fmt = iAlignment;
lc.pszText = pszText;
lc.iSubItem = uCol;
lc.cx = uWidth;
return ListView_InsertColumn(hWnd, uCol, &lc);
}
#endif
static int LVInsertItem(
HWND hWnd, UINT uRow, UINT uCol, const LPTSTR pszText, LPARAM lParam)
{
#ifdef BEEBWIN
LVITEM li = {0};
li.mask = LVIF_TEXT | LVIF_PARAM;
li.iItem = uRow;
li.iSubItem = uCol;
li.pszText = pszText;
li.lParam = (lParam ? lParam : uRow);
return ListView_InsertItem(hWnd, &li);
#else
beeb_RCTable[uRow][uCol].name = pszText;
beeb_RCTable[uRow][uCol].bank = lParam;
return 0;
#endif
}
static void LVSetItemText(
HWND hWnd, UINT uRow, UINT uCol, const LPTSTR pszText)
{
#ifdef BEEBWIN
ListView_SetItemText(hWnd, uRow, uCol, pszText);
#else
// change the row/column data which is read by swift dialogue
beeb_RCTable[uRow][uCol].name = pszText;
#endif
}
static void LVSetFocus(HWND hWnd)
{
#ifdef BEEBWIN
int row = ListView_GetSelectionMark(hWnd);
ListView_SetItemState(hWndROMList,
row,
LVIS_SELECTED | LVIS_FOCUSED,
LVIS_SELECTED | LVIS_FOCUSED);
SetFocus(hWnd);
#else
swift_RCSetFocus();
#endif
}
/****************************************************************************/
static void UpdateROMField(int row)
{
char szROMFile[_MAX_PATH];
bool unplugged = false;
int bank;
if (nModel == Model::Master128)
{
bank = 16 - row;
if (bank >= 0 && bank <= 7)
unplugged = (CMOSRAM[20] & (1 << bank)) ? false : true;
else if (bank >= 8 && bank <= 15)
unplugged = (CMOSRAM[21] & (1 << (bank-8))) ? false : true;
}
strncpy(szROMFile, ROMCfg[static_cast<int>(nModel)][row], _MAX_PATH);
if (unplugged)
strncat(szROMFile, " (unplugged)", _MAX_PATH);
LVSetItemText(hWndROMList, row, 1, (LPTSTR)szROMFile);
}
/****************************************************************************/
static void FillROMList(void)
{
#ifdef BEEBWIN
Edit_SetText(hWndModel, szModel[static_cast<int>(nModel)]);
ListView_DeleteAllItems(hWndROMList);
#else
swift_RCSetModelText(szModel[static_cast<int>(nModel)].c_str());
#endif
int row = 0;
LVInsertItem(hWndROMList, row, 0, (LPTSTR)"OS", 16);
LVSetItemText(hWndROMList, row, 1, (LPTSTR)ROMCfg[static_cast<int>(nModel)][0]);
for (row = 1; row <= 16; ++row)
{
int bank = 16 - row;
char str[20];
sprintf(str, "%02d (%X)", bank, bank);
LVInsertItem(hWndROMList, row, 0, (LPTSTR)str, bank);
UpdateROMField(row);
}
}
#ifdef BEEBWIN
/****************************************************************************/
static INT_PTR CALLBACK ROMConfigDlgProc(HWND hwndDlg, UINT message,
WPARAM wParam, LPARAM /* lParam */)
{
int row;
char *cfg;
switch (message)
{
case WM_INITDIALOG:
nModel = MachineType;
hWndModel = GetDlgItem(hwndDlg, IDC_MODEL);
hWndROMList = GetDlgItem(hwndDlg, IDC_ROMLIST);
ListView_SetExtendedListViewStyle(hWndROMList, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
LVInsertColumn(hWndROMList, 0, "Bank", LVCFMT_LEFT, 45);
LVInsertColumn(hWndROMList, 1, "ROM File", LVCFMT_LEFT, 283);
FillROMList();
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
#else
extern "C" bool RCWindowCommandHandler(int wParam)
{
int row;
char *cfg;
switch (wParam)
#endif
{
case IDC_BBCB:
nModel = Model::B;
FillROMList();
return TRUE;
case IDC_INTEGRAB:
nModel = Model::IntegraB;
FillROMList();
return TRUE;
case IDC_BBCBPLUS:
nModel = Model::BPlus;
FillROMList();
return TRUE;
case IDC_MASTER128:
nModel = Model::Master128;
FillROMList();
return TRUE;
case IDC_SELECTROM:
#ifdef BEEBWIN
row = ListView_GetSelectionMark(hWndROMList);
#else
row = swift_RCGetSelectionMark();
#endif
if (row >= 0 && row <= 16)
{
char szROMFile[MAX_PATH];
if (GetROMFile(hwndDlg, szROMFile))
{
strcpy(ROMCfg[static_cast<int>(nModel)][row], szROMFile);
UpdateROMField(row);
}
}
LVSetFocus(hWndROMList);
break;
case IDC_MARKWRITABLE:
#ifdef BEEBWIN
row = ListView_GetSelectionMark(hWndROMList);
#else
row = swift_RCGetSelectionMark();
#endif
if (row >= 1 && row <= 16)
{
cfg = ROMCfg[static_cast<int>(nModel)][row];
if (strcmp(cfg, BANK_EMPTY) != 0 && strcmp(cfg, BANK_RAM) != 0)
{
if (strlen(cfg) > 4 && strcmp(cfg + strlen(cfg) - 4, ROM_WRITABLE) == 0)
cfg[strlen(cfg) - 4] = 0;
else
strcat(cfg, ROM_WRITABLE);
UpdateROMField(row);
}
}
LVSetFocus(hWndROMList);
break;
case IDC_RAM:
#ifdef BEEBWIN
row = ListView_GetSelectionMark(hWndROMList);
#else
row = swift_RCGetSelectionMark();
#endif
if (row >= 1 && row <= 16)
{
strcpy(ROMCfg[static_cast<int>(nModel)][row], BANK_RAM);
UpdateROMField(row);
}
LVSetFocus(hWndROMList);
break;
case IDC_EMPTY:
#ifdef BEEBWIN
row = ListView_GetSelectionMark(hWndROMList);
#else
row = swift_RCGetSelectionMark();
#endif
if (row >= 1 && row <= 16)
{
strcpy(ROMCfg[static_cast<int>(nModel)][row], BANK_EMPTY);
UpdateROMField(row);
}
LVSetFocus(hWndROMList);
break;
case IDC_SAVE:
SaveROMConfigFile(hwndDlg);
LVSetFocus(hWndROMList);
break;
case IDC_LOAD:
LoadROMConfigFile(hwndDlg);
LVSetFocus(hWndROMList);
break;
#ifdef BEEBWIN
case IDOK:
EndDialog(hwndDlg, TRUE);
return TRUE;
case IDCANCEL:
EndDialog(hwndDlg, FALSE);
return TRUE;
}
break;
#endif
}
return FALSE;
}
/****************************************************************************/
static bool LoadROMConfigFile(HWND hWnd)
{
char DefaultPath[MAX_PATH];
char szROMConfigPath[MAX_PATH];
char *pFileName = szROMConfigPath;
bool success = false;
const char* filter = "ROM Config File (*.cfg)\0*.cfg\0";
szROMConfigPath[0] = 0;
mainWin->GetDataPath(mainWin->GetUserDataPath(), szROMConfigPath);
if (szDefaultROMConfigPath[0])
strcpy(DefaultPath, szDefaultROMConfigPath);
else
strcpy(DefaultPath, szROMConfigPath);
#ifdef BEEBWIN
FileDialog fileDialog(hWnd, pFileName, MAX_PATH, DefaultPath, filter);
if (fileDialog.Open())
#else
if (swift_GetFilesWithPreview(pFileName, MAX_PATH, DefaultPath, ROMCFG, false)==0)
#endif
{
// Save directory as default for next time
unsigned int PathLength = (unsigned int)(strrchr(pFileName, '\\') - pFileName);
strncpy(szDefaultROMConfigPath, pFileName, PathLength);
szDefaultROMConfigPath[PathLength] = 0;
// Read the file
ROMConfigFile LoadedROMCfg;
if (ReadROMFile(pFileName, LoadedROMCfg))
{
// Copy in loaded config
memcpy(&ROMCfg, &LoadedROMCfg, sizeof(ROMConfigFile));
FillROMList();
success = true;
}
}
return success;
}
/****************************************************************************/
static bool SaveROMConfigFile(HWND hWnd)
{
char DefaultPath[MAX_PATH];
char szROMConfigPath[MAX_PATH];
char *pFileName = szROMConfigPath;
bool success = false;
const char* filter = "ROM Config File (*.cfg)\0*.cfg\0";
szROMConfigPath[0] = 0;
mainWin->GetDataPath(mainWin->GetUserDataPath(), szROMConfigPath);
if (szDefaultROMConfigPath[0])
strcpy(DefaultPath, szDefaultROMConfigPath);
else
strcpy(DefaultPath, szROMConfigPath);
#ifdef BEEBWIN
FileDialog fileDialog(hWnd, pFileName, MAX_PATH, DefaultPath, filter);
if (fileDialog.Save())
#else
if (swift_SaveFile(pFileName, MAX_PATH, ROMCFG))
#endif
{
// Save directory as default for next time
#ifdef BEEBWIN
unsigned int PathLength = (unsigned int)(strrchr(pFileName, '\\') - pFileName);
#else
unsigned int PathLength = (unsigned int)(strrchr(pFileName, '/') - pFileName);
#endif
strncpy(szDefaultROMConfigPath, pFileName, PathLength);
szDefaultROMConfigPath[PathLength] = 0;
// Add a file extension if required
if (strchr(pFileName, '.') == NULL)
{
strcat(pFileName, ".cfg");
}
// Save the file
if (WriteROMFile(pFileName, ROMCfg))
{
success = true;
}
}
return success;
}
/****************************************************************************/
bool WriteROMFile(const char *filename, ROMConfigFile ROMConfig)
{
FILE *fd = fopen(filename, "w");
if (!fd)
{
mainWin->Report(MessageType::Error,
"Failed to write ROM configuration file:\n %s", filename);
return false;
}
for (int model = 0; model < 4; ++model)
{
for (int bank = 0; bank < 17; ++bank)
{
fprintf(fd, "%s\n", ROMConfig[model][bank]);
}
}
fclose(fd);
return true;
}
/****************************************************************************/
static bool GetROMFile(HWND hWnd, char *pFileName)
{
char DefaultPath[MAX_PATH];
char szROMPath[MAX_PATH];
bool success = false;
const char* filter = "ROM File (*.rom)\0*.rom\0";
strcpy(szROMPath, "BeebFile");
mainWin->GetDataPath(mainWin->GetUserDataPath(), szROMPath);
int nROMPathLen = (int)strlen(szROMPath);
if (szDefaultROMPath[0])
strcpy(DefaultPath, szDefaultROMPath);
else
strcpy(DefaultPath, szROMPath);
#ifdef BEEBWIN
FileDialog fileDialog(hWnd, pFileName, MAX_PATH, DefaultPath, filter);
if (fileDialog.Open())
#else
if (swift_GetFilesWithPreview(pFileName, MAX_PATH, DefaultPath, ROMCFG, false)==0)
#endif
{
// Save directory as default for next time
#ifdef BEEBWIN
unsigned int PathLength = (unsigned int)(strrchr(pFileName, '\\') - pFileName);
#else
unsigned int PathLength = (unsigned int)(strrchr(pFileName, '/') - pFileName);
#endif
strncpy(szDefaultROMPath, pFileName, PathLength);
szDefaultROMPath[PathLength] = 0;
// Strip user data path
if (strncmp(pFileName, szROMPath, nROMPathLen) == 0)
{
strcpy(pFileName, pFileName + nROMPathLen + 1);
}
success = true;
}
return success;
}