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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
/* spectranet.c: Spectranet emulation
Copyright (c) 2011-2013 Philip Kendall
$Id: spectranet.c 4919 2013-04-20 07:47:56Z sbaldovi $
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-fuse@shadowmagic.org.uk
*/
#include "config.h"
#include <string.h>
#include "compat.h"
#include "debugger/debugger.h"
#include "flash/am29f010.h"
#include "machine.h"
#include "memory.h"
#include "module.h"
#include "nic/w5100.h"
#include "periph.h"
#include "peripherals/ula.h"
#include "settings.h"
#include "ui/ui.h"
#ifdef BUILD_SPECTRANET
#define SPECTRANET_PAGES 256
#define SPECTRANET_PAGE_LENGTH 0x1000
#define SPECTRANET_ROM_LENGTH 0x20000
#define SPECTRANET_ROM_BASE 0
#define SPECTRANET_BUFFER_LENGTH 0x8000
#define SPECTRANET_BUFFER_BASE 0x40
#define SPECTRANET_RAM_LENGTH 0x20000
#define SPECTRANET_RAM_BASE 0xc0
static const int SPECTRANET_CPLD_VERSION = 0x03;
static memory_page spectranet_full_map[SPECTRANET_PAGES * MEMORY_PAGES_IN_4K];
static memory_page spectranet_current_map[MEMORY_PAGES_IN_16K];
static int spectranet_memory_allocated = 0;
static nic_w5100_t *w5100;
static flash_am29f010_t *flash_rom;
#endif
int spectranet_available = 0;
int spectranet_paged;
int spectranet_paged_via_io;
int spectranet_w5100_paged_a = 0, spectranet_w5100_paged_b = 0;
/* Whether the programmable trap is active */
int spectranet_programmable_trap_active;
/* Where the programmable trap will trigger if active */
libspectrum_word spectranet_programmable_trap;
#ifdef BUILD_SPECTRANET
/* True if the next write to 0x023b will set the MSB of the programmable trap */
static int trap_write_msb;
/* Whether the Spectranet's "suppress NMI" flipflop is set */
static int nmi_flipflop = 0;
static int spectranet_source;
/* Debugger events */
static const char *event_type_string = "spectranet";
static int page_event, unpage_event;
void
spectranet_page( int via_io )
{
if( spectranet_paged )
return;
spectranet_paged = 1;
spectranet_paged_via_io = via_io;
machine_current->ram.romcs = 1;
machine_current->memory_map();
debugger_event( page_event );
}
void
spectranet_nmi( void )
{
nmi_flipflop = 1;
spectranet_page( 0 );
}
void
spectranet_unpage( void )
{
if( !spectranet_paged )
return;
spectranet_paged = 0;
spectranet_paged_via_io = 0;
machine_current->ram.romcs = 0;
machine_current->memory_map();
debugger_event( unpage_event );
}
void
spectranet_retn( void )
{
nmi_flipflop = 0;
}
int
spectranet_nmi_flipflop( void )
{
return nmi_flipflop;
}
static void
spectranet_map_page( int dest, int source )
{
int i;
int w5100_page = source >= 0x40 && source < 0x48;
for( i = 0; i < MEMORY_PAGES_IN_4K; i++ )
spectranet_current_map[dest * MEMORY_PAGES_IN_4K + i] =
spectranet_full_map[source * MEMORY_PAGES_IN_4K + i];
switch( dest )
{
case 1: spectranet_w5100_paged_a = w5100_page; break;
case 2: spectranet_w5100_paged_b = w5100_page; break;
}
}
static void
spectranet_hard_reset( void )
{
spectranet_map_page( 1, 0xff ); /* Map something into 0x1000 to 0x1fff */
spectranet_map_page( 2, 0xff ); /* And 0x2000 to 0x2fff */
spectranet_programmable_trap = 0x0000;
spectranet_programmable_trap_active = 0;
trap_write_msb = 0;
nmi_flipflop = 0;
}
static void
spectranet_reset( int hard_reset )
{
if( !periph_is_active( PERIPH_TYPE_SPECTRANET ) ) {
spectranet_available = 0;
spectranet_paged = 0;
return;
}
spectranet_available = 1;
spectranet_paged = !settings_current.spectranet_disable;
if( hard_reset )
spectranet_hard_reset();
if( spectranet_paged ) {
machine_current->ram.romcs = 1;
machine_current->memory_map();
}
nic_w5100_reset( w5100 );
}
static void
spectranet_memory_map( void )
{
if( !spectranet_paged ) return;
memory_map_romcs( spectranet_current_map );
}
static void
spectranet_activate( void )
{
if( !spectranet_memory_allocated ) {
int i, j;
libspectrum_byte *fake_bank =
memory_pool_allocate_persistent( 0x1000, 1 );
memset( fake_bank, 0xff, 0x1000 );
/* Start of by mapping the fake data in everywhere */
for( i = 0; i < SPECTRANET_PAGES; i++ )
for( j = 0; j < MEMORY_PAGES_IN_4K; j++ ) {
memory_page *page = &spectranet_full_map[i * MEMORY_PAGES_IN_4K + j];
page->writable = 0;
page->contended = 0;
page->source = spectranet_source;
page->save_to_snapshot = 0;
page->page_num = i;
page->offset = j * MEMORY_PAGE_SIZE;
page->page = fake_bank + page->offset;
}
/* Pages 0x00 to 0x1f are the flash ROM */
libspectrum_byte *rom =
memory_pool_allocate_persistent( SPECTRANET_ROM_LENGTH, 1 );
memset( rom, 0xff, SPECTRANET_ROM_LENGTH );
for( i = 0; i < SPECTRANET_ROM_LENGTH / SPECTRANET_PAGE_LENGTH; i++ ) {
int base = (SPECTRANET_ROM_BASE + i) * MEMORY_PAGES_IN_4K;
for( j = 0; j < MEMORY_PAGES_IN_4K; j++ ) {
memory_page *page = &spectranet_full_map[base + j];
page->page = rom + (i * MEMORY_PAGES_IN_4K + j) * MEMORY_PAGE_SIZE;
}
}
flash_am29f010_init( flash_rom, rom );
/* Pages 0x40 to 0x47 are the W5100 registers - handled in readbyte()
and writebyte() */
/* Pages 0xc0 to 0xff are the RAM */
libspectrum_byte *ram =
memory_pool_allocate_persistent( SPECTRANET_RAM_LENGTH, 1 );
for( i = 0; i < SPECTRANET_RAM_LENGTH / SPECTRANET_PAGE_LENGTH; i++ ) {
int base = (SPECTRANET_RAM_BASE + i) * MEMORY_PAGES_IN_4K;
for( j = 0; j < MEMORY_PAGES_IN_4K; j++ ) {
memory_page *page = &spectranet_full_map[base + j];
page->writable = 1;
page->page = ram + (i * MEMORY_PAGES_IN_4K + j) * MEMORY_PAGE_SIZE;
}
}
spectranet_memory_allocated = 1;
spectranet_map_page( 0, 0x00 );
spectranet_map_page( 3, 0xc0 );
spectranet_hard_reset();
}
}
static void
spectranet_enabled_snapshot( libspectrum_snap *snap )
{
if( libspectrum_snap_spectranet_active( snap ) )
settings_current.spectranet = 1;
}
static void
spectranet_from_snapshot( libspectrum_snap *snap )
{
if( !libspectrum_snap_spectranet_active( snap ) )
return;
if( periph_is_active( PERIPH_TYPE_SPECTRANET ) ) {
spectranet_programmable_trap =
libspectrum_snap_spectranet_programmable_trap( snap );
spectranet_programmable_trap_active =
libspectrum_snap_spectranet_programmable_trap_active( snap );
trap_write_msb =
libspectrum_snap_spectranet_programmable_trap_msb( snap );
settings_current.spectranet_disable =
libspectrum_snap_spectranet_all_traps_disabled( snap );
spectranet_map_page( 1, libspectrum_snap_spectranet_page_a( snap ) );
spectranet_map_page( 2, libspectrum_snap_spectranet_page_b( snap ) );
nmi_flipflop = libspectrum_snap_spectranet_nmi_flipflop( snap );
if( libspectrum_snap_spectranet_paged( snap ) ) {
spectranet_page( libspectrum_snap_spectranet_paged_via_io( snap ) );
memory_map_romcs( spectranet_current_map );
}
else
spectranet_unpage();
nic_w5100_from_snapshot( w5100,
libspectrum_snap_spectranet_w5100( snap, 0 ) );
memcpy(
spectranet_full_map[SPECTRANET_ROM_BASE * MEMORY_PAGES_IN_4K].page,
libspectrum_snap_spectranet_flash( snap, 0 ), SPECTRANET_ROM_LENGTH );
memcpy(
spectranet_full_map[SPECTRANET_RAM_BASE * MEMORY_PAGES_IN_4K].page,
libspectrum_snap_spectranet_ram( snap, 0 ), SPECTRANET_RAM_LENGTH );
}
}
static void
spectranet_to_snapshot( libspectrum_snap *snap )
{
libspectrum_byte *snap_buffer, *src;
int active = periph_is_active( PERIPH_TYPE_SPECTRANET );
libspectrum_snap_set_spectranet_active( snap, active );
if( !active )
return;
libspectrum_snap_set_spectranet_paged( snap, spectranet_paged );
libspectrum_snap_set_spectranet_paged_via_io( snap, spectranet_paged_via_io );
libspectrum_snap_set_spectranet_programmable_trap( snap,
spectranet_programmable_trap );
libspectrum_snap_set_spectranet_programmable_trap_active( snap,
spectranet_programmable_trap_active );
libspectrum_snap_set_spectranet_programmable_trap_msb( snap, trap_write_msb );
libspectrum_snap_set_spectranet_deny_downstream_a15( snap, 0 );
libspectrum_snap_set_spectranet_nmi_flipflop( snap, nmi_flipflop );
libspectrum_snap_set_spectranet_all_traps_disabled( snap,
settings_current.spectranet_disable );
libspectrum_snap_set_spectranet_rst8_trap_disabled( snap, 0 );
libspectrum_snap_set_spectranet_page_a(
snap, spectranet_current_map[1 * MEMORY_PAGES_IN_4K].page_num );
libspectrum_snap_set_spectranet_page_b(
snap, spectranet_current_map[2 * MEMORY_PAGES_IN_4K].page_num );
libspectrum_snap_set_spectranet_w5100( snap, 0,
nic_w5100_to_snapshot( w5100 ) );
snap_buffer = malloc( SPECTRANET_ROM_LENGTH * sizeof( libspectrum_byte ) );
if( !snap_buffer ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
src = spectranet_full_map[SPECTRANET_ROM_BASE * MEMORY_PAGES_IN_4K].page;
memcpy( snap_buffer, src, SPECTRANET_ROM_LENGTH );
libspectrum_snap_set_spectranet_flash( snap, 0, snap_buffer );
snap_buffer = malloc( SPECTRANET_RAM_LENGTH * sizeof( libspectrum_byte ) );
if( !snap_buffer ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
src = spectranet_full_map[SPECTRANET_RAM_BASE * MEMORY_PAGES_IN_4K].page;
memcpy( snap_buffer, src, SPECTRANET_RAM_LENGTH );
libspectrum_snap_set_spectranet_ram( snap, 0, snap_buffer );
}
static module_info_t spectranet_module_info = {
spectranet_reset,
spectranet_memory_map,
spectranet_enabled_snapshot,
spectranet_from_snapshot,
spectranet_to_snapshot
};
static void
spectranet_page_a( libspectrum_word port, libspectrum_byte data )
{
spectranet_map_page( 1, data );
memory_map_romcs( spectranet_current_map );
}
static void
spectranet_page_b( libspectrum_word port, libspectrum_byte data )
{
spectranet_map_page( 2, data );
memory_map_romcs( spectranet_current_map );
}
static libspectrum_byte
spectranet_cpld_version( libspectrum_word port, int *attached )
{
*attached = 1;
return SPECTRANET_CPLD_VERSION;
}
static void
spectranet_trap( libspectrum_word port, libspectrum_byte data )
{
if( trap_write_msb )
spectranet_programmable_trap =
(spectranet_programmable_trap & 0x00ff) | (data << 8);
else
spectranet_programmable_trap =
(spectranet_programmable_trap & 0xff00) | data;
trap_write_msb = !trap_write_msb;
}
static libspectrum_byte
spectranet_control_read( libspectrum_word port, int *attached )
{
libspectrum_byte b = ula_last_byte() & 0x07;
if( spectranet_programmable_trap_active )
b |= 0x08;
if( ( machine_current->capabilities &
LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY ) &&
machine_current->ram.last_byte & 0x08 )
b |= 0x10;
*attached = 1;
return b;
}
static void
spectranet_control_write( libspectrum_word port, libspectrum_byte data )
{
if( data & 0x01 )
spectranet_page( 1 );
else if( spectranet_paged_via_io )
spectranet_unpage();
spectranet_programmable_trap_active = data & 0x08;
}
static const periph_port_t spectranet_ports[] = {
{ 0xffff, 0x003b, NULL, spectranet_page_a },
{ 0xffff, 0x013b, NULL, spectranet_page_b },
{ 0xffff, 0x023b, spectranet_cpld_version, spectranet_trap },
{ 0xffff, 0x033b, spectranet_control_read, spectranet_control_write },
{ 0, 0, NULL, NULL }
};
static const periph_t spectranet_periph = {
&settings_current.spectranet,
spectranet_ports,
1,
spectranet_activate
};
void
spectranet_init( void )
{
module_register( &spectranet_module_info );
spectranet_source = memory_source_register( "Spectranet" );
periph_register( PERIPH_TYPE_SPECTRANET, &spectranet_periph );
periph_register_paging_events( event_type_string, &page_event,
&unpage_event );
w5100 = nic_w5100_alloc();
flash_rom = flash_am29f010_alloc();
}
void
spectranet_end( void )
{
nic_w5100_free( w5100 );
flash_am29f010_free( flash_rom );
}
static libspectrum_word
get_w5100_register( memory_page *page, libspectrum_word address )
{
libspectrum_word base_address =
( page->page_num - SPECTRANET_BUFFER_BASE ) * SPECTRANET_PAGE_LENGTH;
return base_address + ( address & 0xfff );
}
libspectrum_byte
spectranet_w5100_read( memory_page *page, libspectrum_word address )
{
return nic_w5100_read( w5100, get_w5100_register( page, address ) );
}
void
spectranet_w5100_write( memory_page *page, libspectrum_word address, libspectrum_byte b )
{
address &= 0xfff;
nic_w5100_write( w5100, get_w5100_register( page, address ), b );
}
void
spectranet_flash_rom_write( libspectrum_word address, libspectrum_byte b )
{
int pageb_page = spectranet_current_map[2 * MEMORY_PAGES_IN_4K].page_num;
if( pageb_page < SPECTRANET_ROM_LENGTH / SPECTRANET_PAGE_LENGTH ) {
/* Which 16Kb flash page are we accessing */
int flash_page = pageb_page / 4;
/* And at what offset into that page */
libspectrum_word flash_address = (pageb_page % 4) * SPECTRANET_PAGE_LENGTH + (address & 0xfff);
flash_am29f010_write( flash_rom, flash_page, flash_address, b );
}
}
#else /* #ifdef BUILD_SPECTRANET */
/* No spectranet support */
void
spectranet_init( void )
{
}
void
spectranet_end( void )
{
}
void
spectranet_page( int via_io )
{
}
void
spectranet_nmi( void )
{
}
void
spectranet_unpage( void )
{
}
void
spectranet_retn( void )
{
}
int
spectranet_nmi_flipflop( void )
{
return 0;
}
libspectrum_byte
spectranet_w5100_read( memory_page *page GCC_UNUSED,
libspectrum_word address GCC_UNUSED )
{
return 0xff;
}
void
spectranet_w5100_write( memory_page *page GCC_UNUSED,
libspectrum_word address GCC_UNUSED,
libspectrum_byte b GCC_UNUSED )
{
}
void
spectranet_flash_rom_write( libspectrum_word address GCC_UNUSED,
libspectrum_byte b GCC_UNUSED )
{
}
#endif /* #ifdef BUILD_SPECTRANET */
|