1 /*****************************************************************************
2 ** $Source: /cygdrive/d/Private/_SVNROOT/bluemsx/blueMSX/Src/Memory/romMapperOpcodeSlotManager.c,v $
3 **
4 ** $Revision: 1.1 $
5 **
6 ** $Date: 2008-11-23 20:26:12 $
7 **
8 ** More info: http://www.bluemsx.com
9 **
10 ** Copyright (C) 2003-2006 Daniel Vik
11 **
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ** This program is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ** GNU General Public License for more details.
21 **
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 **
26 ******************************************************************************
27 */
28 #include "romMapperOpcodeSlotManager.h"
29 #include "MediaDb.h"
30 #include "DeviceManager.h"
31 #include "DebugDeviceManager.h"
32 #include "SlotManager.h"
33 #include "SaveState.h"
34 #include "AY8910.h"
35 #include "Board.h"
36 #include "IoPort.h"
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 
41 typedef struct {
42     UInt8   slotSelect;
43 
44     int    deviceHandle;
45     int    debugHandle;
46 } RomMapperOpcodeSlotManager;
47 
48 
slotUpdate(RomMapperOpcodeSlotManager * rm)49 static void slotUpdate(RomMapperOpcodeSlotManager* rm)
50 {
51     int i;
52 
53     for (i = 0; i < 4; i++) {
54         slotMapRamPage((rm->slotSelect >> (2 * i)) & 3, 0, 4 + i);
55     }
56 }
57 
saveState(RomMapperOpcodeSlotManager * rm)58 static void saveState(RomMapperOpcodeSlotManager* rm)
59 {
60     SaveState* state = saveStateOpenForWrite("mapperOpcodeSlotManager");
61     saveStateSet(state, "slotSelect", rm->slotSelect);
62     saveStateClose(state);
63 }
64 
loadState(RomMapperOpcodeSlotManager * rm)65 static void loadState(RomMapperOpcodeSlotManager* rm)
66 {
67     SaveState* state = saveStateOpenForRead("mapperOpcodeSlotManager");
68     rm->slotSelect      = (UInt8)saveStateGet(state, "slotSelect",  0);
69     saveStateClose(state);
70 }
71 
destroy(RomMapperOpcodeSlotManager * rm)72 static void destroy(RomMapperOpcodeSlotManager* rm)
73 {
74     deviceManagerUnregister(rm->deviceHandle);
75     debugDeviceUnregister(rm->debugHandle);
76 
77     ioPortUnregister(0x41);
78 
79     free(rm);
80 }
81 
peek(RomMapperOpcodeSlotManager * rm,UInt16 ioPort)82 static UInt8 peek(RomMapperOpcodeSlotManager* rm, UInt16 ioPort)
83 {
84     return rm->slotSelect;
85 }
86 
read(RomMapperOpcodeSlotManager * rm,UInt16 ioPort)87 static UInt8 read(RomMapperOpcodeSlotManager* rm, UInt16 ioPort)
88 {
89     return rm->slotSelect;
90 }
91 
write(RomMapperOpcodeSlotManager * rm,UInt16 ioPort,UInt8 value)92 static void write(RomMapperOpcodeSlotManager* rm, UInt16 ioPort, UInt8 value)
93 {
94     rm->slotSelect = value;
95     slotUpdate(rm);
96 }
97 
reset(RomMapperOpcodeSlotManager * rm)98 static void reset(RomMapperOpcodeSlotManager* rm)
99 {
100     rm->slotSelect = 0;
101     slotUpdate(rm);
102 }
103 
getDebugInfo(RomMapperOpcodeSlotManager * rm,DbgDevice * dbgDevice)104 static void getDebugInfo(RomMapperOpcodeSlotManager* rm, DbgDevice* dbgDevice)
105 {
106     DbgIoPorts* ioPorts;
107 
108     ioPorts = dbgDeviceAddIoPorts(dbgDevice, "SLOTSELECT", 1);
109     dbgIoPortsAddPort(ioPorts, 0, 0x41, DBG_IO_READWRITE, peek(rm, 0x41));
110 }
111 
romMapperOpcodeSlotManagerCreate()112 int romMapperOpcodeSlotManagerCreate()
113 {
114     DeviceCallbacks callbacks = { destroy, reset, saveState, loadState };
115     DebugCallbacks dbgCallbacks = { getDebugInfo, NULL, NULL, NULL };
116 
117     RomMapperOpcodeSlotManager* rm = malloc(sizeof(RomMapperOpcodeSlotManager));
118 
119     rm->deviceHandle = deviceManagerRegister(ROM_OPCODESLOT, &callbacks, rm);
120     rm->debugHandle = debugDeviceRegister(DBGTYPE_BIOS, "SLOTSELECT", &dbgCallbacks, rm);
121 
122     ioPortRegister(0x41, read, write, rm);
123 
124     reset(rm);
125 
126     return 1;
127 }
128 
129