File: divide.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 (371 lines) | stat: -rw-r--r-- 10,591 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
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
/* divide.c: DivIDE interface routines
   Copyright (c) 2005-2017 Matthew Westcott, Philip Kendall
   Copyright (c) 2015 Stuart Brady

   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 "debugger/debugger.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 "unittests/unittests.h"
#include "divide.h"
#include "divxxx.h"

/* Private function prototypes */

static libspectrum_byte divide_ide_read( libspectrum_word port, libspectrum_byte *attached );
static void divide_ide_write( libspectrum_word port, libspectrum_byte data );
static void divide_control_write( libspectrum_word port, libspectrum_byte data );
static libspectrum_ide_register port_to_ide_register( libspectrum_byte port );
static void divide_activate( void );

/* Data */

static const periph_port_t divide_ports[] = {
  { 0x00e3, 0x00a3, divide_ide_read, divide_ide_write },
  { 0x00ff, 0x00e3, NULL, divide_control_write },
  { 0, 0, NULL, NULL }
};

static const periph_t divide_periph = {
  /* .option = */ &settings_current.divide_enabled,
  /* .ports = */ divide_ports,
  /* .hard_reset = */ 1,
  /* .activate = */ divide_activate,
};

int divide_automapping_enabled = 0;
static divxxx_t *divide_state;

static libspectrum_ide_channel *divide_idechn0;
static libspectrum_ide_channel *divide_idechn1;

#define DIVIDE_PAGES 4
#define DIVIDE_PAGE_LENGTH 0x2000

static void divide_reset( int hard_reset );
static void divide_memory_map( void );
static void divide_enabled_snapshot( libspectrum_snap *snap );
static void divide_from_snapshot( libspectrum_snap *snap );
static void divide_to_snapshot( libspectrum_snap *snap );

static module_info_t divide_module_info = {

  /* .reset = */ divide_reset,
  /* .romcs = */ divide_memory_map,
  /* .snapshot_enabled = */ divide_enabled_snapshot,
  /* .snapshot_from = */ divide_from_snapshot,
  /* .snapshot_to = */ divide_to_snapshot,

};

/* Debugger events */
static const char * const event_type_string = "divide";

/* Housekeeping functions */

static int
divide_init( void *context )
{
  int error;

  divide_idechn0 = libspectrum_ide_alloc( LIBSPECTRUM_IDE_DATA16 );
  divide_idechn1 = libspectrum_ide_alloc( LIBSPECTRUM_IDE_DATA16 );
  
  error = ide_init( divide_idechn0,
		    settings_current.divide_master_file,
		    UI_MENU_ITEM_MEDIA_IDE_DIVIDE_MASTER_EJECT,
		    settings_current.divide_slave_file,
		    UI_MENU_ITEM_MEDIA_IDE_DIVIDE_SLAVE_EJECT );
  if( error ) return error;

  module_register( &divide_module_info );

  periph_register( PERIPH_TYPE_DIVIDE, &divide_periph );

  divide_state = divxxx_alloc( "DivIDE EPROM", DIVIDE_PAGES, "DivIDE RAM",
      event_type_string, &settings_current.divide_enabled,
      &settings_current.divide_wp );

  return 0;
}

static void
divide_end( void )
{
  divxxx_free( divide_state );
  libspectrum_ide_free( divide_idechn0 );
  libspectrum_ide_free( divide_idechn1 );
}

void
divide_register_startup( void )
{
  startup_manager_module dependencies[] = {
    STARTUP_MANAGER_MODULE_DEBUGGER,
    STARTUP_MANAGER_MODULE_DISPLAY,
    STARTUP_MANAGER_MODULE_MEMORY,
    STARTUP_MANAGER_MODULE_SETUID,
  };
  startup_manager_register( STARTUP_MANAGER_MODULE_DIVIDE, dependencies,
                            ARRAY_SIZE( dependencies ), divide_init, NULL,
                            divide_end );
}

/* DivIDE does not page in immediately on a reset condition (we do that by
   trapping PC instead); however, it needs to perform housekeeping tasks upon
   reset */
static void
divide_reset( int hard_reset )
{
  divxxx_reset( divide_state, hard_reset );

  libspectrum_ide_reset( divide_idechn0 );
  libspectrum_ide_reset( divide_idechn1 );
}

int
divide_insert( const char *filename, libspectrum_ide_unit unit )
{
  return ide_master_slave_insert(
    divide_idechn0, unit, filename,
    &settings_current.divide_master_file,
    UI_MENU_ITEM_MEDIA_IDE_DIVIDE_MASTER_EJECT,
    &settings_current.divide_slave_file,
    UI_MENU_ITEM_MEDIA_IDE_DIVIDE_SLAVE_EJECT );
}

int
divide_commit( libspectrum_ide_unit unit )
{
  int error;

  error = libspectrum_ide_commit( divide_idechn0, unit );

  return error;
}

int
divide_eject( libspectrum_ide_unit unit )
{
  return ide_master_slave_eject(
    divide_idechn0, unit,
    &settings_current.divide_master_file,
    UI_MENU_ITEM_MEDIA_IDE_DIVIDE_MASTER_EJECT,
    &settings_current.divide_slave_file,
    UI_MENU_ITEM_MEDIA_IDE_DIVIDE_SLAVE_EJECT );
}

/* Port read/writes */

static libspectrum_ide_register
port_to_ide_register( libspectrum_byte port )
{
  switch( port & 0xff ) {
    case 0xa3:
      return LIBSPECTRUM_IDE_REGISTER_DATA;
    case 0xa7:
      return LIBSPECTRUM_IDE_REGISTER_ERROR_FEATURE;
    case 0xab:
      return LIBSPECTRUM_IDE_REGISTER_SECTOR_COUNT;
    case 0xaf:
      return LIBSPECTRUM_IDE_REGISTER_SECTOR;
    case 0xb3:
      return LIBSPECTRUM_IDE_REGISTER_CYLINDER_LOW;
    case 0xb7:
      return LIBSPECTRUM_IDE_REGISTER_CYLINDER_HIGH;
    case 0xbb:
      return LIBSPECTRUM_IDE_REGISTER_HEAD_DRIVE;
    default: /* 0xbf */
      return LIBSPECTRUM_IDE_REGISTER_COMMAND_STATUS;
  }
}

libspectrum_byte
divide_ide_read( libspectrum_word port, libspectrum_byte *attached )
{
  int ide_register;

  *attached = 0xff; /* TODO: check this */
  ide_register = port_to_ide_register( port );

  return libspectrum_ide_read( divide_idechn0, ide_register );
}

static void
divide_ide_write( libspectrum_word port, libspectrum_byte data )
{
  int ide_register;
  
  ide_register = port_to_ide_register( port );
  
  libspectrum_ide_write( divide_idechn0, ide_register, data );
}

static void
divide_control_write( libspectrum_word port GCC_UNUSED, libspectrum_byte data )
{
  divxxx_control_write( divide_state, data );
}

void
divide_set_automap( int state )
{
  divxxx_set_automap( divide_state, state );
}

void
divide_refresh_page_state( void )
{
  divxxx_refresh_page_state( divide_state );
}

void
divide_memory_map( void )
{
  divxxx_memory_map( divide_state );
}

static void
divide_enabled_snapshot( libspectrum_snap *snap )
{
  settings_current.divide_enabled = libspectrum_snap_divide_active( snap );
}

static void
divide_from_snapshot( libspectrum_snap *snap )
{
  size_t i;

  if( !libspectrum_snap_divide_active( snap ) ) return;

  settings_current.divide_wp =
    libspectrum_snap_divide_eprom_writeprotect( snap );
  divxxx_control_write_internal( divide_state, libspectrum_snap_divide_control( snap ) );

  if( libspectrum_snap_divide_eprom( snap, 0 ) ) {
    memcpy( divxxx_get_eprom( divide_state ),
	    libspectrum_snap_divide_eprom( snap, 0 ), DIVIDE_PAGE_LENGTH );
  }

  for( i = 0; i < libspectrum_snap_divide_pages( snap ); i++ )
    if( libspectrum_snap_divide_ram( snap, i ) ) {
      libspectrum_byte *ram = divxxx_get_ram( divide_state, i );
      memcpy( ram, libspectrum_snap_divide_ram( snap, i ), DIVIDE_PAGE_LENGTH );
    }

  if( libspectrum_snap_divide_paged( snap ) ) {
    divxxx_page( divide_state );
  } else {
    divxxx_unpage( divide_state );
  }
}

static void
divide_to_snapshot( libspectrum_snap *snap )
{
  size_t i;
  libspectrum_byte *buffer;

  if( !settings_current.divide_enabled ) return;

  libspectrum_snap_set_divide_active( snap, 1 );
  libspectrum_snap_set_divide_eprom_writeprotect( snap,
                                                  settings_current.divide_wp );
  libspectrum_snap_set_divide_paged( snap, divxxx_get_active( divide_state ) );
  libspectrum_snap_set_divide_control( snap, divxxx_get_control( divide_state ) );

  buffer = libspectrum_new( libspectrum_byte, DIVIDE_PAGE_LENGTH );

  memcpy( buffer, divxxx_get_eprom( divide_state ), DIVIDE_PAGE_LENGTH );
  libspectrum_snap_set_divide_eprom( snap, 0, buffer );

  libspectrum_snap_set_divide_pages( snap, DIVIDE_PAGES );

  for( i = 0; i < DIVIDE_PAGES; i++ ) {

    buffer = libspectrum_new( libspectrum_byte, DIVIDE_PAGE_LENGTH );

    memcpy( buffer, divxxx_get_ram( divide_state, i ), DIVIDE_PAGE_LENGTH );
    libspectrum_snap_set_divide_ram( snap, i, buffer );
  }
}

static void
divide_activate( void )
{
  divxxx_activate( divide_state );
}

int
divide_unittest( void )
{
  int r = 0;
  int eprom_memory_source = divxxx_get_eprom_memory_source( divide_state );
  int ram_memory_source = divxxx_get_ram_memory_source( divide_state );

  divide_set_automap( 1 );

  divide_control_write( 0x00e3, 0x80 );
  r += unittests_assert_8k_page( 0x0000, eprom_memory_source, 0 );
  r += unittests_assert_8k_page( 0x2000, ram_memory_source, 0 );
  r += unittests_assert_16k_ram_page( 0x4000, 5 );
  r += unittests_assert_16k_ram_page( 0x8000, 2 );
  r += unittests_assert_16k_ram_page( 0xc000, 0 );

  divide_control_write( 0x00e3, 0x83 );
  r += unittests_assert_8k_page( 0x0000, eprom_memory_source, 0 );
  r += unittests_assert_8k_page( 0x2000, ram_memory_source, 3 );
  r += unittests_assert_16k_ram_page( 0x4000, 5 );
  r += unittests_assert_16k_ram_page( 0x8000, 2 );
  r += unittests_assert_16k_ram_page( 0xc000, 0 );

  divide_control_write( 0x00e3, 0x40 );
  r += unittests_assert_8k_page( 0x0000, ram_memory_source, 3 );
  r += unittests_assert_8k_page( 0x2000, ram_memory_source, 0 );
  r += unittests_assert_16k_ram_page( 0x4000, 5 );
  r += unittests_assert_16k_ram_page( 0x8000, 2 );
  r += unittests_assert_16k_ram_page( 0xc000, 0 );

  divide_control_write( 0x00e3, 0x02 );
  r += unittests_assert_8k_page( 0x0000, ram_memory_source, 3 );
  r += unittests_assert_8k_page( 0x2000, ram_memory_source, 2 );
  r += unittests_assert_16k_ram_page( 0x4000, 5 );
  r += unittests_assert_16k_ram_page( 0x8000, 2 );
  r += unittests_assert_16k_ram_page( 0xc000, 0 );

  divide_set_automap( 0 );

  r += unittests_paging_test_48( 2 );

  return r;
}