ifdef 48K
  @NAME = "ZX Spectrum 48K"
else
  ifdef PENTAGON
    @NAME = "Pentagon 128K (TR-DOS)"
  else
    @NAME = "ZX Spectrum 128K"
  endif
endif


ifndef CFG_AY_48K
  @CFG_AY_48K = OFF
endif

ifndef CFG_AY_TS
  @CFG_AY_TS = ON
endif

ifndef CFG_GS
  @CFG_GS = OFF
endif

ifndef CFG_BDI_48_128
  @CFG_BDI_48_128 = OFF
endif


if CFG_AY_TS == ON
  define TURBO_SOUND
endif

ifdef 48K
  if CFG_AY_48K == OFF
    define NO_AY
  endif
endif

if CFG_GS == ON
  ifndef 48K
    define GS
  endif
endif

ifdef PENTAGON
  define BDI
endif

if CFG_BDI_48_128 == ON
  define BDI
endif


# Файл справки
@HELP_FILE = zx/spectrum.html

# Тактовые частоты
@CPU_FREQUENCY = 3500000

# Основное ПЗУ
@ROM48 = "zx/48k.rom"

ifndef PENTAGON
  @ROM128 = "zx/128k.rom"
else
  @ROM128 = "zx/pentagon.rom"
endif

ifdef BDI
  @TRDOS_ROM = "zx/trd504tm.rom"
endif


######## Window ########

EmuWindow window
window.caption = @NAME

# Default window size for window.windowStyle = fixed (default = 800, 600)
window.defaultWindowSize = 800, 600

# Window style: autosize, resizable, fixed (default = autosise)
window.windowStyle = autosize
#window.windowStyle = resizable

# Window fullscreen mode: yes, no (default = no)
window.fullscreen = no

# Window scaling option: 1x, 2x, 3x, 4x, 5x, fit, bestFit (default = bestFit)
window.frameScale = 2x
#window.frameScale = bestFit

# Window fields mixing: none, mix, average, interlace, scanline (default = none)
#window.fieldsMixing = mix

# Image smoothing / post-processing: nearest, bilinear, sharp, custom (default = sharp)
window.smoothing = sharp

# Custom image shader: "shader name" or none (default = none)
#window.shader = crt-hyllian

# Convert image to grayscale
#window.desaturate = yes

# Aspect correction (default = no)
#window.aspectCorrection = no
window.aspectCorrection = yes

# Wide screen (16:9): yes, no, custom (default = no)
window.wideScreen = no

# Gray window background (default: no)
#window.grayBackground = yes

# Screen format for window.wideScreen = custom
#window.customScreenFormat = 1.111



######## Platform ########

platform.helpFile = @HELP_FILE

# Fast reset
platform.fastReset = yes
platform.fastResetCpuTicks = 5800000

# Code page: rk, koi8 (default = rk)
platform.codePage = koi8

ifdef 48K
  Rom rom = @ROM48, 0x4000
else
  Rom rom0 = @ROM128, 0x4000
  Rom rom1 = @ROM128, 0x4000, 0x4000
endif

ifdef BDI
  Rom trdosRom = @TRDOS_ROM, 0x4000

  AddrSpaceMapper switchedRom = 2
  ifndef 48K
    switchedRom.page = 0, &rom1
  else
    switchedRom.page = 0, &rom
  endif
  switchedRom.page = 1, &trdosRom
endif


#@CONTENDED_TAG = 1

Ram vRam0 = 0x4000
Ram ram1 = 0x4000
Ram ram2 = 0x4000

ZxVidMemAdapter ram0
ram0.mem = &vRam0
ram0.screenPage = 0

ifndef 48K
  Ram ram3 = 0x4000
  Ram ram4 = 0x4000
  Ram vRam5 = 0x4000
  Ram ram6 = 0x4000
  Ram vRam7 = 0x4000

  ZxVidMemAdapter ram5
  ram5.mem = &vRam5
  ram5.screenPage = 0

  ZxVidMemAdapter ram7
  ram7.mem = &vRam7
  ram7.screenPage = 1
endif


#ram0.tag = @CONTENDED_TAG

ZxRenderer crtRenderer
crtRenderer.visibleArea = yes

ifdef 48K
  crtRenderer.screenMemory = &vRam0
  crtRenderer.mode = "48k"
else
  ifndef PENTAGON
    crtRenderer.mode = "128k"
  else
    crtRenderer.mode = "pentagon"
  endif
  crtRenderer.screenMemory = &vRam5, 0
  crtRenderer.screenMemory = &vRam7, 1
endif

ram0.renderer = &crtRenderer

ifndef 48K
  ram5.renderer = &crtRenderer
  ram7.renderer = &crtRenderer
endif

ZxCore core
core.window = &window
core.crtRenderer = &crtRenderer

connect crtRenderer.int -> core.int

ifndef 48K
  AddrSpaceMapper cpu0 = 2
  AddrSpaceMapper cpu3 = 8

  cpu0.page = 0, &rom0

  ifndef BDI
    cpu0.page = 1, &rom1
  else
    cpu0.page = 1, &switchedRom
  endif

  cpu3.page = 0, &ram0
  cpu3.page = 1, &ram1
  cpu3.page = 2, &ram2
  cpu3.page = 3, &ram3
  cpu3.page = 4, &ram4
  cpu3.page = 5, &ram5
  cpu3.page = 6, &ram6
  cpu3.page = 7, &ram7
endif

ifndef BDI
  AddrSpace addrSpace
else
  ZxBdiAddrSpace addrSpace
  connect addrSpace.bdiActive -> switchedRom.page
endif

ifdef 48K
  ifndef BDI
    addrSpace.range = &rom, 0, 0x3fff
  else
    addrSpace.range = &switchedRom, 0, 0x3fff
  endif
  addrSpace.range = &ram0, 0x4000, 0x7fff
  addrSpace.range = &ram1, 0x8000, 0xbfff
  addrSpace.range = &ram2, 0xc000, 0xffff
else
  addrSpace.range = &cpu0, 0, 0x3fff
  addrSpace.range = &ram5, 0x4000, 0x7fff
  addrSpace.range = &ram2, 0x8000, 0xbfff
  addrSpace.range = &cpu3, 0xc000, 0xffff
endif

ZxPorts zxPorts

ifdef 48K
  zxPorts.mode = "48k"
#else
#  zxPorts.mode = "128k"
endif

connect zxPorts.portFE[0-2] -> crtRenderer.borderColor

ifndef 48K
  connect zxPorts.port7FFD[4] -> cpu0.page
  connect zxPorts.port7FFD[3] -> crtRenderer.screenPage
  connect zxPorts.port7FFD[0-2] -> cpu3.page
endif

ifdef BDI
  connect addrSpace.bdiActive -> zxPorts.bdiActive

  Fdc1793 fdc
  zxPorts.fdc = &fdc

  Register fddRegister
  fddRegister.defaultOutput = 0xFF
  fddRegister.defaultInput = 0xFF
  zxPorts.fddRegister = &fddRegister

  connect  fddRegister.output[0-1] -> fdc.drive
  connect ~fddRegister.output[2] -> fdc.reset
  connect  ~fddRegister.output[4] -> fdc.head

  connect fdc.drq -> fddRegister.inputBit(6)
  connect fdc.int -> fddRegister.inputBit(7)

  connect fddRegister.query -> fdc.updateState
endif


CpuZ80 cpu
cpu.frequency = @CPU_FREQUENCY
cpu.16bitPorts = yes
cpu.startAddr = 0
cpu.addrSpace = &addrSpace
cpu.ioAddrSpace = &zxPorts
cpu.core = &core

crtRenderer.cpu = &cpu


#ZxCpuWaits cpuWaits
#cpu.cpuWaits = &cpuWaits

#connect crtRenderer.int -> cpuWaits.int

ifdef BDI
  addrSpace.cpu = &cpu
endif

GeneralSoundSource beepSoundSource
GeneralSoundSource tapeSoundSource
tapeSoundSource.volume = 50
#tapeSoundSource.muted = yes

connect zxPorts.portFE[4] -> beepSoundSource.input
connect zxPorts.portFE[3] -> tapeSoundSource.input
connect zxPorts.portFE[3] -> core.tapeOut


ifndef NO_AY
  Psg3910 ay
  ay.frequency = 1773400

  Psg3910SoundSource psgSoundSource
  psgSoundSource.psg = &ay

  ifndef TURBO_SOUND
    zxPorts.ay = &ay
  else
    Psg3910 ay2
    ay2.frequency = 1773400

    Psg3910SoundSource psgSoundSource2
    psgSoundSource2.psg = &ay2
    psgSoundSource2.polarity = negative

    TurboSound ts
    ts.psg1 = &ay
    ts.psg2 = &ay2

    zxPorts.ay = &ts
  endif
endif


ZxKbdLayout kbdLayout
kbdLayout.layout = smart
#kbdLayout.layout = qwerty

ZxKeyboard keyboard

connect zxPorts.kbdMatrixMask -> keyboard.matrixMask
connect keyboard.matrixData -> zxPorts.kbdMatrixData


KbdTapper kbdTapper
kbdTapper.pressTime = 100
kbdTapper.releaseTime = 100
kbdTapper.crDelay = 500



######## File I/O ########

ZxFileLoader loader
loader.filter = "Файлы лент и снапшотов (*.tap, *.tzx, *.sna)|*.tap;*.TAP;*.tzx;*.TZX;*.sna;*.SNA|Все файлы (*.*)|*"

TapeRedirector tapeInFile
tapeInFile.mode = "r"
tapeInFile.filter = "Файлы TAP и TZX (*.tap, *.tzx)|*.tap;*.TAP;*.tzx;*.TZX|Все файлы (*.*)|*"

ZxTapeInHook tapeInHook = 0x0562
tapeInHook.signature = DBFE1FE620
tapeInHook.tapeRedirector = &tapeInFile
cpu.addHook = &tapeInHook

loader.tapeRedirector = &tapeInFile

TapeRedirector tapeOutFile
tapeOutFile.mode = "w"
tapeOutFile.filter = "Файлы ZX Spectrum (*.tap)|*.tap;*.TAP|Все файлы (*.*)|*"
tapeOutFile.timeout = 1500

ZxTapeOutHook tapeOutHook = 0x04C2
tapeOutHook.signature = 213F05E521
tapeOutHook.tapeRedirector = &tapeOutFile
cpu.addHook = &tapeOutHook

CloseFileHook closeFileHookIn = 0x1303
closeFileHookIn.signature = 76FDCB01AE
closeFileHookIn.addTapeRedirector = &tapeInFile
cpu.addHook = &closeFileHookIn

CloseFileHook closeFileHookOut = 0x0018
closeFileHookOut.addTapeRedirector = &tapeOutFile
cpu.addHook = &closeFileHookOut


EmuObjectGroup tapeGrp
tapeGrp.addItem = &tapeInHook
tapeGrp.addItem = &tapeOutHook
tapeGrp.addItem = &closeFileHookIn
tapeGrp.addItem = &closeFileHookOut
##tapeGrp.enabled = no


ifdef BDI
  FdImage diskA = 80, 2, 16, 256
  diskA.label = A
  diskA.filter = "Образы дисков TR-DOS (*.trd)|*.trd;*.TRD|Все файлы (*.*)|*"

  FdImage diskB = 80, 2, 16, 256
  diskB.label = B
  diskB.filter = "Образы дисков TR-DOS (*.trd)|*.trd;*.TRD|Все файлы (*.*)|*"

  FdImage diskC = 80, 2, 16, 256
  diskC.label = C
  diskC.filter = "Образы дисков TR-DOS (*.trd)|*.trd;*.TRD|Все файлы (*.*)|*"

  FdImage diskD = 80, 2, 16, 256
  diskD.label = D
  diskD.filter = "Образы дисков TR-DOS (*.trd)|*.trd;*.TRD|Все файлы (*.*)|*"

  fdc.fdImage = 0, &diskA
  fdc.fdImage = 1, &diskB
  fdc.fdImage = 2, &diskC
  fdc.fdImage = 3, &diskD
endif


ifdef GS

  @GS_ROM = "zx/gs105a.rom"

  GsPorts gsPorts
  zxPorts.gsPorts = &gsPorts

  CpuZ80 gsCpu
  gsCpu.frequency = 12000000

  GsInternalPorts gsIntPorts
  gsCpu.ioAddrSpace = &gsIntPorts

  # 1 Mb RAM
  gsIntPorts.pageBits = 5

  connect gsPorts.dataOut -> gsIntPorts.dataIn
  connect gsPorts.command -> gsIntPorts.command
  connect gsPorts.status -> gsIntPorts.status

  connect gsIntPorts.dataOut -> gsPorts.dataIn
  connect gsIntPorts.status -> gsPorts.status

  Rom gsRom = @GS_ROM, 0x8000

  Ram gsRam0 = 0x4000
  Ram gsRam1 = 0x8000
  Ram gsRam2 = 0x8000
  Ram gsRam3 = 0x8000

  Ram gsRam4 = 0x8000
  Ram gsRam5 = 0x8000
  Ram gsRam6 = 0x8000
  Ram gsRam7 = 0x8000

  Ram gsRam8 = 0x8000
  Ram gsRam9 = 0x8000
  Ram gsRamA = 0x8000
  Ram gsRamB = 0x8000

  Ram gsRamC = 0x8000
  Ram gsRamD = 0x8000
  Ram gsRamE = 0x8000
  Ram gsRamF = 0x8000

  Ram gsRam10 = 0x8000
  Ram gsRam11 = 0x8000
  Ram gsRam12 = 0x8000
  Ram gsRam13 = 0x8000

  Ram gsRam14 = 0x8000
  Ram gsRam15 = 0x8000
  Ram gsRam16 = 0x8000
  Ram gsRam17 = 0x8000

  Ram gsRam18 = 0x8000
  Ram gsRam19 = 0x8000
  Ram gsRam1A = 0x8000
  Ram gsRam1B = 0x8000

  Ram gsRam1C = 0x8000
  Ram gsRam1D = 0x8000
  Ram gsRam1E = 0x8000
  Ram gsRam1F = 0x8000

  GsSoundMem gsRam0Wrapper
  gsRam0Wrapper.addrSpace = &gsRam0

  AddrSpaceMapper gsHiMem = 32
  gsHiMem.page = 0, &gsRom
  gsHiMem.page = 1, &gsRam1
  gsHiMem.page = 2, &gsRam2
  gsHiMem.page = 3, &gsRam3

  gsHiMem.page = 4, &gsRam4
  gsHiMem.page = 5, &gsRam5
  gsHiMem.page = 6, &gsRam6
  gsHiMem.page = 7, &gsRam7

  gsHiMem.page = 8, &gsRam8
  gsHiMem.page = 9, &gsRam9
  gsHiMem.page = 10, &gsRamA
  gsHiMem.page = 11, &gsRamB

  gsHiMem.page = 12, &gsRamC
  gsHiMem.page = 13, &gsRamD
  gsHiMem.page = 14, &gsRamE
  gsHiMem.page = 15, &gsRamF

  gsHiMem.page = 16, &gsRam10
  gsHiMem.page = 17, &gsRam11
  gsHiMem.page = 18, &gsRam12
  gsHiMem.page = 19, &gsRam13

  gsHiMem.page = 20, &gsRam14
  gsHiMem.page = 21, &gsRam15
  gsHiMem.page = 22, &gsRam16
  gsHiMem.page = 23, &gsRam17

  gsHiMem.page = 24, &gsRam18
  gsHiMem.page = 25, &gsRam19
  gsHiMem.page = 26, &gsRam1A
  gsHiMem.page = 27, &gsRam1B

  gsHiMem.page = 28, &gsRam1C
  gsHiMem.page = 29, &gsRam1D
  gsHiMem.page = 30, &gsRam1E
  gsHiMem.page = 31, &gsRam1F

  AddrSpace gsAddrSpace
  gsAddrSpace.range = &gsRom, 0x0000, 0x3fff
  gsAddrSpace.range = &gsRam0Wrapper, 0x4000, 0x7fff
  gsAddrSpace.range = &gsHiMem, 0x8000, 0xffff

  gsCpu.addrSpace = &gsAddrSpace

  connect gsIntPorts.memPage -> gsHiMem.page

  GsSoundSource gsSoundSource

  connect gsRam0Wrapper.sample0 -> gsSoundSource.sample(0)
  connect gsRam0Wrapper.sample1 -> gsSoundSource.sample(1)
  connect gsRam0Wrapper.sample2 -> gsSoundSource.sample(2)
  connect gsRam0Wrapper.sample3 -> gsSoundSource.sample(3)

  connect gsIntPorts.volume0 -> gsSoundSource.volume(0)
  connect gsIntPorts.volume1 -> gsSoundSource.volume(1)
  connect gsIntPorts.volume2 -> gsSoundSource.volume(2)
  connect gsIntPorts.volume3 -> gsSoundSource.volume(3)

  PeriodicInt8080 gsInt = &gsCpu, 7, 37500
  gsInt.active = yes

endif


ifndef QT
  include "zx/spectrum_wx.inc"
endif
