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
|
/* speccyboot.c: SpeccyBoot Ethernet emulation
See http://speccyboot.sourceforge.net/
Copyright (c) 2009-2011 Patrik Persson, Philip Kendall
$Id: speccyboot.c 4926 2013-05-05 07:58:18Z 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 "compat.h"
#include "machine.h"
#include "memory.h"
#include "nic/enc28j60.h"
#include "module.h"
#include "periph.h"
#include "settings.h"
#include "unittests/unittests.h"
#include "speccyboot.h"
#ifdef BUILD_SPECCYBOOT
/* Determine whether a bit has gone from high to low (or low to high) */
#define GONE_LO(prev, curr, mask) (((prev) & (mask)) && !((curr) & (mask)))
#define GONE_HI(prev, curr, mask) (((curr) & (mask)) && !((prev) & (mask)))
static nic_enc28j60_t *nic;
/* ---------------------------------------------------------------------------
* Spectrum I/O register state (IN/OUT from/to 0x9f)
* ------------------------------------------------------------------------ */
/* Individual bits when writing the register (OUT instructions) */
#define OUT_BIT_SPI_SCK (0x01)
#define OUT_BIT_ETH_CS (0x08)
#define OUT_BIT_ROM_CS (0x20)
#define OUT_BIT_ETH_RST (0x40)
#define OUT_BIT_SPI_MOSI (0x80)
static libspectrum_byte out_register_state;
static libspectrum_byte in_register_state;
static libspectrum_byte
speccyboot_register_read( libspectrum_word port GCC_UNUSED, int *attached );
static void
speccyboot_register_write( libspectrum_word port GCC_UNUSED,
libspectrum_byte val );
static const periph_port_t speccyboot_ports[] = {
{ 0x00e0, 0x0080, speccyboot_register_read, speccyboot_register_write },
{ 0, 0, NULL, NULL }
};
static const periph_t speccyboot_periph = {
&settings_current.speccyboot,
speccyboot_ports,
1,
NULL
};
/* ---------------------------------------------------------------------------
* ROM paging state
* ------------------------------------------------------------------------ */
static int speccyboot_rom_active = 0; /* SpeccyBoot ROM paged in? */
static int speccyboot_memory_source;
static memory_page speccyboot_memory_map_romcs[ MEMORY_PAGES_IN_8K ];
static void
speccyboot_memory_map( void )
{
if( !speccyboot_rom_active ) return;
memory_map_romcs_8k( 0x0000, speccyboot_memory_map_romcs );
}
static void
speccyboot_reset( int hard_reset GCC_UNUSED )
{
static int tap_opened = 0;
speccyboot_rom_active = 0;
if( !periph_is_active( PERIPH_TYPE_SPECCYBOOT ) )
return;
if( machine_load_rom_bank( speccyboot_memory_map_romcs, 0,
settings_current.rom_speccyboot,
settings_default.rom_speccyboot, 0x2000 ) )
return;
out_register_state = 0xff; /* force transitions to low */
speccyboot_register_write( 0, 0 );
/*
* Open TAP. If this fails, SpeccyBoot emulation won't work.
*
* This is done here rather than in speccyboot_init() to ensure any
* error messages are only displayed if SpeccyBoot emulation is
* actually requested.
*/
if( !tap_opened ) {
nic_enc28j60_init( nic );
tap_opened = 1;
}
}
static libspectrum_byte
speccyboot_register_read( libspectrum_word port GCC_UNUSED, int *attached )
{
*attached = 1;
return in_register_state;
}
static void
speccyboot_register_write( libspectrum_word port GCC_UNUSED,
libspectrum_byte val )
{
nic_enc28j60_poll( nic );
if( GONE_LO( out_register_state, val, OUT_BIT_ETH_RST ) )
nic_enc28j60_reset( nic );
if( !(val & OUT_BIT_ETH_CS) ) {
if( GONE_LO( out_register_state, val, OUT_BIT_ETH_CS ) )
nic_enc28j60_set_spi_state( nic, SPI_CMD );
/*
* NOTE: the ENC28J60 data sheet (figure 4-2) specifies that MISO
* is updated when SCK goes low, but we instead set it on the
* subsequent transition to high. This is to simplify the internal
* state logic for SPI RBM commands.
*
* In this design, we ignore the final SCK transition to low at
* the end of a transaction. That SCK transition, if occurring at
* the end of a RBM transaction, would otherwise trigger another
* read operation, and ERDPT would be increased one time too many.
*
* The SpeccyBoot stack reads MISO just after setting SCK high, so
* it works fine with this simplification.
*/
if( GONE_HI( out_register_state, val, OUT_BIT_SPI_SCK ) ) {
in_register_state = 0xfe | nic_enc28j60_spi_produce_bit( nic );
nic_enc28j60_spi_consume_bit( nic, (out_register_state & OUT_BIT_SPI_MOSI) ? 1 : 0 );
}
}
/* Update ROM paging status when the ROM_CS bit is cleared or set */
if( GONE_LO( out_register_state, val, OUT_BIT_ROM_CS ) ) {
speccyboot_rom_active = 1;
machine_current->ram.romcs = 1;
machine_current->memory_map();
} else if( GONE_HI( out_register_state, val, OUT_BIT_ROM_CS ) ) {
speccyboot_rom_active = 0;
machine_current->ram.romcs = 0;
machine_current->memory_map();
}
out_register_state = val;
}
void
speccyboot_init( void )
{
int i;
nic = nic_enc28j60_alloc();
static module_info_t speccyboot_module_info = {
speccyboot_reset,
speccyboot_memory_map,
NULL,
NULL,
NULL
};
module_register( &speccyboot_module_info );
speccyboot_memory_source = memory_source_register( "SpeccyBoot" );
for( i = 0; i < MEMORY_PAGES_IN_8K; i++ )
speccyboot_memory_map_romcs[i].source = speccyboot_memory_source;
periph_register( PERIPH_TYPE_SPECCYBOOT, &speccyboot_periph );
}
void
speccyboot_end( void )
{
nic_enc28j60_free( nic );
}
int
speccyboot_unittest( void )
{
int r = 0;
speccyboot_rom_active = 1;
speccyboot_memory_map();
r += unittests_assert_8k_page( 0x0000, speccyboot_memory_source, 0 );
r += unittests_assert_8k_page( 0x2000, memory_source_rom, 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 );
speccyboot_rom_active = 0;
machine_current->memory_map();
r += unittests_paging_test_48( 2 );
return r;
}
#else /* #ifdef BUILD_SPECCYBOOT */
/* No speccyboot support */
void
speccyboot_init( void )
{
}
void
speccyboot_end( void )
{
}
int
speccyboot_unittest( void )
{
return 0;
}
#endif /* #ifdef BUILD_SPECCYBOOT */
|