THE EDGE CONNECTOR
==================

The Next's expansion bus is patterned after the original 48K Spectrum bus with a few signals omitted.  The video related signals VIDEO, Y, V, U are not present.  Only 5V and 9V (actually whatever your PSU is so 12V if you use a higher one) power are available on Lower pins 3 and 4 respectively.  -5V,12v and 12vAC on are NOT present on Upper pins 20,22 or 23.

A comparison of the two edge connectors:

https://faqwiki.zxnet.co.uk/wiki/ZX_Spectrum_16K/48K_edge_connector
	vs
Next

To ensure the longevity of the KS1 Next, a level shifter add-on / cable should be plugged in ahead of legacy peripherals but especially modern recreations which use CMOS technology.  A suitable design could provide the additional voltages needed by some peripherals.  Such an addon is also likely to prove useful anyway for many legacy devices to physically allow them to be used with the Digital Video and other Next ports that they would otherwise block.

The KS2 Next has some buffering to remove the need for the above safety feature but, you may still need something for large amounds of bus load or use of the missing voltages.

2D experiment
                                280
/INT COMPUTER SIDE ------+-----/\/\/-----+------------- /INT PERIPHERAL SIDE
                         |             B |
                         |            -------
                         |           E /   \ C
                         +------------V     +---------- 5V


LEGACY COMPATIBILITY
====================

There are two other issues to solve when connecting legacy peripherals to the Next.  One is that the Next can run at speeds up to 28MHz which is too fast for attached peripherals.  The other is that external devices may conflict with devices implemented in the Next itself.

To solve these problems, the expansion bus is not directly connected to the next's internal buses.  Instead it is separated from the next's signals by an enable/disable path.  While disabled, expansion bus signals are kept in a passive and neutral state.  While enabled, signals are propagated to the expansion bus.

Because legacy devices can only operate at 3.5MHz, the next is automatically slowed to 3.5MHz when the expansion bus is turned on.  This happens no matter what speed the cpu is set to in nextreg 0x07.  Reading back from nextreg 0x07, the lower nibble will reflect the set speed by the programmer and the upper nibble will reflect the actual machine speed.  When the expansion bus is turned off, the cpu speed will return to the set speed.

To solve the io port conflict problem, the next will filter io cycles delivered to the expansion bus.  If an internal device responds on a particular io port, the /iorq signal will be kept high throughout the entire io cycle on the expansion bus so that external peripherals do not respond on that io port.  In this way internal devices have priority and will never conflict with external devices.

The next maintains two sets of internal device enable bits containing 25 bits each.  These bits are normally all set but can be individually reset to disable specific devices so that they do not respond on their port addresses.

nextreg 0x85,0x84,0x83,0x82  (internal decoding active, all 1 on reset, 0x85 holds MSB)
nextreg 0x89,0x88,0x87,0x86  (expansion bus decoding active, all 1 on hard reset, 0x89 holds MSB)

So with the decimal numbers to use in BASIC OUT 9275,r:OUT 9531,v or NextZXOS REG r,v commands:

130 (0x82) internal and 134 (0x86) expansion 

 0 : port ff
 1 : port 7ffd
 2 : port dffd
 3 : port 1ffd
 4 : +3 floating bus
 5 : port 6b dma
 6 : port 1f kempston / md1
 7 : port 37 kempston 2 / md2
 
131 (0x83) internal and 135 (0x87) expansion
  
 8 : port e3 divmmc control
 9 : multiface (two variable ports)
10 : port 103b,113b i2c
11 : port e7,eb spi
12 : port 133b,143b,153b uart
13 : port fadf,fbdf,ffdf mouse
14 : port 57,5b,303b sprites
15 : port 123b layer2

132 (0x84) internal and 136 (0x88) expansion

16 : port fffd,bffd ay
17 : port 0f,1f,4f,5f dac soundrive mode 1
18 : port f1,f3,f9,fb dac soundrive mode 2
19 : port 3f,5f dac stereo profi covox
20 : port 0f,4f dac stereo covox
21 : port fb dac mono pentagon/atm (sd mode 2 off)
22 : port b3 dac mono gs covox
23 : port df dac mono specdrum

133 (0x85) internal and 137 (0x89) expansion

24 : port bf3b, ff3b ula+
25 : port 0b z80 dma
26 : port eff7 pentagon 1024 memory
27 : port 183b,193b,1a3b,1b3b,1c3b,1d3b,1e3b,1f3b z80 ctc

28-30 : reserved.

31 : register reset mode (soft or hard reset selection)

The internal decoding bits (nextreg 0x85 - 0x82) are always applied whether the expansion bus is enabled or not.  It will not be used often but it can be used for software compatibility where a program may write to io addresses that conflict with next devices.  Resetting the enable bit for that particular device can then allow the program to run normally.

The expansion bus decoding bits (nextreg 0x89 - 0x86) are logically ANDed together with the internal decoding bits and are applied whenever the expansion bus is enabled.  Where a 0 appears, that particular internal device will be deactivated and the associated io cycles will propagate to the expansion bus so that an external peripheral can respond.

As an example, let's imagine attaching a TurboSoundFM device.  These devices replace the AY with FM sound chips programmed through the same io ports 0xbffd and 0xfffd.  These can be attached to the Next if the internal AY chips are disabled.  The internal AY lies on bit 16 of the disables which is bit 0 in nextreg 0x88.  Writing 0xfe to nextreg 0x88 will cause the internal AYs to stop responding on their ports when the expansion bus is on.  Instead io for ports 0xbffd and 0xfffd will be propagated to the TurboSoundFM device so that the Next can run music on that device.  Note that programs can turn the expansion bus on and off at will so that it would be possible to turn the expansion bus on, write to the TurboSound FM device, then turn the expansion bus off and write to the internal AY chips.

A port propagate register allows special exemptions to the above.

nextreg 0x8a (138): expansion bus port propagate
Set bits indicate an io cycle for the indicated ports will always appear on the expansion bus
(R/W)
  bits 7:6 = Reserved, must be 0
  bit 5 = Propagate port 0xeff7 io cycles (hard reset = 0)
  bit 4 = Propagate port 0xff io cycles (hard reset = 0)
  bit 3 = Propagate port 0x1ffd io cycles (hard reset = 0)
  bit 2 = Propagate port 0xdffd io cycles (hard reset = 0)
  bit 1 = Propagate port 0x7ffd io cycles (hard reset = 0)
  bit 0 = Propagate port 0xfe io cycles (hard reset = 0)
  If any of the bits are set, io cycles for the corresponding ports are propagated to the expansion bus when the expansion bus is on.  If the internal port decode is still active, any response sent by devices on the expansion bus will be ignored.  The purpose here is to allow external peripherals to monitor changes in state inside the zx next.

Port 0xfe is treated specially so that external keyboards can be attached.  When its propagate bit is set, the value read from the bus will be mixed into keyboard reads on port 0xfe.

Note that this will affect Sinclair mode joysticks on things like the IF2 as they are effectively keyboards... It can also, if there are too many devices on the bus (especially if used unbuffered / without a level shifter as that can provide to great a load) give spurious Key reads...



POWER ON AND RESET SEQUENCE
===========================

On power on or hard reset, the expansion bus is disabled.  The expansion bus /reset signal will be asserted for a short time with other signals being maintained in a neutral state.

If the expansion bus is enabled or if the expansion bus is configured to enable on the next soft reset, all signals will be propagated to the expansion bus during a soft reset so that a reset sequence is seen by attached peripherals.

The /reset signal can also be asserted by writing a 1 to nextreg 0x02 bit 7.  This reset is not momentary - the program must explicitly reset this bit to de-assert the reset.  A hard reset will zero this bit.

Take note that the expansion bus's /reset signal and the esp wifi's reset are tied together.  A reset of one always involves a reset of the other.


ENABLING THE EXPANSION BUS
==========================

After optionally programming the device disable bits, the expansion bus can be enabled via nextreg 0x80:

0x80 (128) => Expansion Bus Enable
(R/W) (hard reset = 0)
IMMEDIATE
bit 7 = 1 to enable the expansion bus
bit 6 = 1 to enable romcs rom replacement from divmmc banks 14/15
bit 5 = 1 to disable i/o cycles & ignore iorqula
bit 4 = 1 to disable memory cycles & ignore romcs
AFTER SOFT RESET (copied into bits 7-4)
bit 3 = 1 to enable the expansion bus
bit 2 = 1 to enable romcs rom replacement from divmmc banks 14/15
bit 1 = 1 to disable i/o cycles & ignore iorqula
bit 0 = 1 to disable memory cycles & ignore romcs

Changes to the expansion bus can occur immediately or can be postponed until a soft reset.  For immediate changes, the IMMEDIATE bits should be set to 1.  Otherwise on a SOFT RESET the lower bits are copied to the upper bits and will become active - if the expansion bus isn't already enabled.

0x81 (129) => Expansion Bus Control
(R/W) (hard reset = 0)
bit 7 = 1 if ROMCS is asserted on the expansion bus (read only)
bit 6 = 1 to allow peripherals to override the ULA on some even port reads (rotronics wafadrive)
bit 5 = 1 to disable expansion bus nmi debounce (opus discovery)
bit 4 = 1 to propagate the max cpu clock at all times including when the expansion bus is off
bits 1-0 = max cpu speed when the expansion bus is on (currently fixed at 00 = 3.5MHz)


USING THIS FROM BASIC
=====================

These registers can easily be set from BASIC (on 48K and 128K/+2 models) and sometimes NextZXOS to allow external devices to operate.  Note that the two OUT commands should always be on the same line with a colon (:) to ensure BASIC does not change registers half way.

On the latest NEXTZXOS you can of course use the REG command to set these registers. 

At least from NextZXOS 2.07i it is possible from the menu to select the new
Interface 2 option which does the reset and sets up a 48K BASIC ROM in place
if you want to use the IF2 Joysticks make sure to select that version of the option.

Then you just need to do some OUT's for each peripheral as follows:

IF1 - OUT 9275,135: OUT 9531,246  ; Do this as SOON as you enter BASIC to avoid SD card corruption

There seems to be a bug now with this that causes the Symbol Shift to be locked on
for Safety always do 134,5 and 6 to 0 and 137 to 128 - see below.


PRINTER - OUT 9275,136: OUT 9531,219 ; This will disable the onboard DAC.


The following describes in more detail how to do this from the personalities selected on the boot options:



Kempston Joysticks
------------------

To enable external Kempston joystick interface and prevent the internal from responding

OUT 9275,134:OUT 9531,191		or REG 134,191:REG 136,253
OUT 9275,136:OUT 9531,253

if the expansion bus is not already enabled then

OUT 9275,128:OUT 9531,128		or REG 128,128

will do so and allow the Kempston to be seen.  This program could also be saved as a small BASIC program on an external DivMMC using ESXDOS for example to use its onboard joystick.

Just remember that once the Bus is on the system is locked at 3.5Mhz

You can do this within NextZXOS to allow four player games to be run albeit at
the slower 3.5Mhz - one that works well is the four player version of BombJack
for a standard Spectrum called BombMunchies.

Four player light cycles should be possible to run as well although may need
adpating: 

https://impomatic.itch.io/four-player-light-cycles
https://spectrumcomputing.co.uk/index.php?cat=96&id=33391  

It uses the data table at line 90 to set the ports that are scanned for
each player so it should be possible to change two for 31 and 51 to use the
onboard Kempston for two of them and change two to use the number keys.

64510 - Q->T could be 63486 so 1-5
57342 - P->Y could be 61438 so 0-6
32766 - SP-B could be 31 for Kempston 1
65268 - CAP-V would be 51 for Kempston 2


There is also a four player game called Party_Worms...

External ROM devices like Interface 2
-------------------------------------

To use IF2, a Dandonator, Kempston, Ram TURBO etc. with an external ROM cartridge:

OUT 9275,128: OUT 9531,8
soft reset

NextZXOS has a Menu option that does this allowing you boot into a Cartridge program quickly.

If you need to use the joystick ports on these devices though you need to enable port forwarding as below
nextreg 0x8a is exposed in the nmi debugger so you can change the register to 01 to turn on port forwarding for 0xfe before using the menu option.

Note that the RESET buttons on external devices should not be used as they reset the ESP (even when the bus is disabled) you should use the one on the Next instead.  This is less relevant for example
on the Dandonator where the right hand button brings up the ROM menu so has to be used.  Only
the Next Reset held in can return to NextZXOS

Remember that the Sinclair Joystick ports on an Interface 2 or RAM Turbo equivalent will only if the expansion bus propagation register 138 (0x8a) is set to 1 on a reset E.g.

OUT 9275,138:OUT 9531,1

However, if you don't want to propogate the memory change IO's (if you get spurious keyboard data for example) then you must explicitly reset it. E.g. OUT 9275,138:OUT 9531,0

If you set the onboard ports to Kempston 1 and Kempston 2 or MD1/MD2 then you could have four joystick ports...

The IF2 ones can be read with IN 63486 and IN 61438 with the onboard Kempston 1 and 2 ones from IN 31 and IN 55 respectively.

There is a standard Spectrum game called MUNCHIES that can use four ports in this way for a great four player experience...

In the last case to just use the extra joystick ports REG 138,1:REG 128,128 can be used in NextZXOS as this will not need a reset to start the ROM.

Interface 1 or external DivMMC
------------------------------

To use these type of devices it is necessary to disable the onboard SD card interface and the onboard DivMMC system as they use the same ports.  These devices also rely on an original 48K/128K ROM so from a 48K or 128K machine identity:

This will ensure no spurious reads for keys from a heavily loaded bus (is default)
OUT 9275,138:OUT 9531,0

You should only need 
OUT 9275,135: OUT 9531,246

but, for some reason since 3.1.10 and NextZXOS 2.07h it needs:
OUT 9275, 137: OUT 9531, 128
OUT 9275, 136: OUT 9531, 0
OUT 9275, 135: OUT 9531, 0
OUT 9275, 134: OUT 9531, 0

OUT 9275,128: OUT 9531,8

soft reset or

OUT 9275,2:OUT 9531,1

The fastest way is to type in as program lines 1-6 and use EDIT to add each line
modifying the register no and/or value

Again it is best to use the Next RESET button not the one on the device.  You can however, use either the NMI on the device or the DRIVE (green) button on the Next - they are the same.

IF1 ROM version does not matter (CLOSE#0:PRINT PEEK 23729 - 0=v1, 80=v2)

If you want to transfer to SD Card it is possible to use ESXDOS to switch between them as the
two ROM's are not mutually exclusive - see Tutorial from Phoebus and Allen - repeated later

Alphacom Printer (and probably ZX Printer)
------------------------------------------

These units conflict with both software (the 128K Spectrums used the printer buffer for extra variables so were incompatible) and hardware ports but, NextZXOS has built in support for them using its own printer driver and buffer (which also disables the Sound DAC temp. while printing) so that you can just print from it.

To use on a 48K personality you need to disable the onboard DAC sound system using port $FB by resetting two bits in register 136.

OUT 9275,136: OUT 9531,219

(11011011 - 219)

Of course you will need to enable the BUS as well if you just wish to use the printer
OUT 9275,128: OUT 9531,8 (followed by a soft reset) it is not needed a second time
if you are using with the Interface one or two as above.


Using the ALT rom functionality to switch
-----------------------------------------

In Next mode there is a way of forcing an original ROM into position

0x8C (140) => Alternate ROM 
(R/W) (hard reset = 0)
IMMEDIATE
  bit 7 = 1 to enable alt rom
  bit 6 = 1 to make alt rom visible only during writes, otherwise replaces rom during reads
  bit 5 = 1 to lock ROM1 (48K rom)
  bit 4 = 1 to lock ROM0 (128K rom)
AFTER SOFT RESET (copied into bits 7-4)
  bit 3 = 1 to enable alt rom
  bit 2 = 1 to make alt rom visible only during writes, otherwise replaces rom during reads
  bit 1 = 1 to lock ROM1 (48K rom)
  bit 0 = 1 to lock ROM0 (128K rom)
The locking mechanism also applies if the alt rom is not enabled. For the +3 and zx next, if the two lock bits are not
zero, then the corresponding rom page is locked in place. Other models use the bits to preferentially lock the corresponding
48K rom or the 128K rom.

In theory a REG 140,10 should then allow you to jump into an IF1 enabled Spectrum 48K on next reset.

(Use 12 for 128K mode)

This is effectively what NextZXOS 2.07i and onwards do for the IF2 Cart options.


Transferring files with ESXDOS and IF1
-----------------------------------------
Phoebus Dokos · 1 June  ·
Here's a short tutorial on using esxDOS together with Interface 1. Thanks to Allen Albright
for putting it together
This won't work with the Looking Glass rom; I haven't figured out why; I'll contact Geoff Wearmouth to check with him today.
NOTE: NextReg 137 (0x89) is a new one and bit 7 set to 1 makes sure that changes persist through soft resets. It is NOT in the manual, it is however present in the SD documentation.

You'll need to start with a standard 48K personality (with installed esxDOS - follow the manual Chapter 20 on how to install this).

    Boot up as a 48k machine with esxDOS
    Change the expansion bus enables to disable everything (essential for a 48K machine to be as stock as possible):
    nr 137,128; nr 136,0; nr 135,0; nr 134,0
    (commands follow)
    OUT 9275, 137: OUT 9531, 128
    OUT 9275, 136: OUT 9531, 0
    OUT 9275, 135: OUT 9531, 0
    OUT 9275, 134: OUT 9531, 0
    Reset into microdrives with esxDOS disabled:
    nr 128,8; nr 2,1 (commands follow)
    OUT 9275, 128: OUT 9531, 8
    OUT 9275, 2: OUT 9531, 1
    Load from microdrive
    Switch back to esxdos and save:
    nr 128,0 (commands follow)
    OUT 9275, 128: OUT 9531, 0
    SAVE *"blah"
    Switch back to microdrive:
    Nr 128,136 (commands follow)
    OUT 9275, 128: OUT 9531, 136
    Switch back to esxDOS:
    nr 128,0 (commands follow)
    OUT 9275, 128: OUT 9531, 0
    Switch back to microdrive:
    nr 128,136 (commands follow)
    OUT 9275, 128: OUT 9531, 136

Note
----
The above register data may change and you should always check on the config with the latest cores...

https://gitlab.com/SpectrumNext/ZX_Spectrum_Next_FPGA/-/blob/master/cores/zxnext/nextreg.txt

