File: zxmmc.c

package info (click to toggle)
fuse-emulator 1.5.7%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,024 kB
  • sloc: ansic: 87,196; sh: 11,456; perl: 3,916; makefile: 991; yacc: 245; lex: 139
file content (243 lines) | stat: -rw-r--r-- 5,949 bytes parent folder | download | duplicates (3)
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
/* zxmmc.c: ZXMMC interface routines
   Copyright (c) 2017 Philip Kendall, Sergio BaldovĂ­

   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.

   Author contact information:

   E-mail: Philip Kendall <philip-fuse@shadowmagic.org.uk>

*/

#include <config.h>

#include <libspectrum.h>

#include <string.h>

#include "ide.h"
#include "infrastructure/startup_manager.h"
#include "machine.h"
#include "module.h"
#include "periph.h"
#include "settings.h"
#include "ui/ui.h"
#include "zxmmc.h"

/* Private function prototypes */

static void zxmmc_card_select( libspectrum_word port, libspectrum_byte data );
static libspectrum_byte zxmmc_mmc_read( libspectrum_word port,
                                        libspectrum_byte *attached );
static void zxmmc_mmc_write( libspectrum_word port, libspectrum_byte data );

/* Data */

static const periph_port_t zxmmc_ports[] = {
  { 0x00ff, 0x001f, NULL, zxmmc_card_select },
  { 0x00ff, 0x003f, zxmmc_mmc_read, zxmmc_mmc_write },
  { 0, 0, NULL, NULL }
};

static const periph_t zxmmc_periph = {
  /* .option = */ &settings_current.zxmmc_enabled,
  /* .ports = */ zxmmc_ports,
  /* .hard_reset = */ 1,
  /* .activate = */ NULL,
};

/* The card inserted into the ZXMMC. For now, we emulate only one card. */
static libspectrum_mmc_card *card;

/* The card currently selected via the "card select" call */
static libspectrum_mmc_card *current_card;

static void zxmmc_reset( int hard_reset );
static void zxmmc_enabled_snapshot( libspectrum_snap *snap );
static void zxmmc_to_snapshot( libspectrum_snap *snap );

static module_info_t zxmmc_module_info = {

  /* .reset = */ zxmmc_reset,
  /* .romcs = */ NULL,
  /* .snapshot_enabled = */ zxmmc_enabled_snapshot,
  /* .snapshot_from = */ NULL,
  /* .snapshot_to = */ zxmmc_to_snapshot,

};

/* Eject menu item */
static const ui_menu_item eject_menu_item = UI_MENU_ITEM_MEDIA_IDE_ZXMMC_EJECT;

/* Housekeeping functions */

static int
zxmmc_init( void *context )
{
  card = libspectrum_mmc_alloc();

  ui_menu_activate( eject_menu_item, 0 );

  if( settings_current.zxmmc_file ) {
    int error;

    error =
      libspectrum_mmc_insert( card, settings_current.zxmmc_file );
    if( error ) return error;

    error = ui_menu_activate( eject_menu_item, 1 );
    if( error ) return error;
  }

  module_register( &zxmmc_module_info );

  periph_register( PERIPH_TYPE_ZXMMC, &zxmmc_periph );

  return 0;
}

static void
zxmmc_end( void )
{
  libspectrum_mmc_free( card );
}

void
zxmmc_register_startup( void )
{
  startup_manager_module dependencies[] = {
    STARTUP_MANAGER_MODULE_DEBUGGER,
    STARTUP_MANAGER_MODULE_DISPLAY,
    STARTUP_MANAGER_MODULE_SETUID,
  };
  startup_manager_register( STARTUP_MANAGER_MODULE_ZXMMC, dependencies,
                            ARRAY_SIZE( dependencies ), zxmmc_init, NULL,
                            zxmmc_end );
}

static void
zxmmc_reset( int hard_reset )
{
  libspectrum_mmc_reset( card );
}

static int
dirty_fn_wrapper( void *context )
{
  return libspectrum_mmc_dirty( (libspectrum_mmc_card*)context );
}

static libspectrum_error
commit_fn_wrapper( void *context )
{
  libspectrum_mmc_commit( (libspectrum_mmc_card*)context );
  return LIBSPECTRUM_ERROR_NONE;
}

static libspectrum_error
eject_fn_wrapper( void *context )
{
  libspectrum_mmc_eject( (libspectrum_mmc_card*)context );
  return LIBSPECTRUM_ERROR_NONE;
}

static int
mmc_eject( libspectrum_mmc_card *card )
{
  return ide_eject_mass_storage( dirty_fn_wrapper, commit_fn_wrapper,
      eject_fn_wrapper, card,
      "Card has been modified.\nDo you want to save it?",
      &settings_current.zxmmc_file,
      eject_menu_item );
}

int
zxmmc_insert( const char *filename )
{
  int error;

  /* Remove any currently inserted card; abort if we want to keep the current
     card */
  if( settings_current.zxmmc_file )
    if( mmc_eject( card ) )
      return 0;

  settings_set_string( &settings_current.zxmmc_file, filename );

  error = libspectrum_mmc_insert( card, filename );
  if( error ) return error;
  return ui_menu_activate( eject_menu_item, 1 );
}

void
zxmmc_commit( void )
{
  libspectrum_mmc_commit( card );
}

int
zxmmc_eject( void )
{
  return mmc_eject( card );
}

/* Port read/writes */

static void
zxmmc_card_select( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
  /* D0 = MMC0, D1 = MMC1, active LOW
     somehow logic prevents enabling both cards at the same time */
  switch( data & 0x03 ) {
    case 0x02:
      current_card = card;
      break;
    case 0x01:
      /* TODO: select second card */
      current_card = NULL;
      break;
    default:
      current_card = NULL;
      break;
  }
}

static libspectrum_byte
zxmmc_mmc_read( libspectrum_word port GCC_UNUSED, libspectrum_byte *attached )
{
  *attached = 0xff;

  return current_card ? libspectrum_mmc_read( card ) : 0xff;
}

static void
zxmmc_mmc_write( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
  if( current_card ) libspectrum_mmc_write( card, data );
}

static void
zxmmc_enabled_snapshot( libspectrum_snap *snap )
{
  if( libspectrum_snap_zxmmc_active( snap ) )
    settings_current.zxmmc_enabled = 1;
}

static void
zxmmc_to_snapshot( libspectrum_snap *snap )
{
  if( !settings_current.zxmmc_enabled ) return;
  libspectrum_snap_set_zxmmc_active( snap, 1 );
}