ifdef KR03
  @NAME = "Электроника КР-02/03 (SD card / vinxru)"
  define SD_CARD
else
ifdef SD_CARD
  @NAME = "Радио-86РК (SD card / vinxru)"
else
ifdef SDOS
  @NAME = "Радио-86РК (SDOS)"
else
ifdef MIR
  @NAME = "Радио-86РК (CF/Мир)"
else
  @NAME = "Радио-86РК"
endif
endif
endif
endif


# Adapter types for wx/SDL (HWMPVV (default), HWMPVV_VV55, N8VEM, N8VEM_VV55, MSX)
#@CFG_SD_TYPE = HWMPVV


# Использовать ROM-диск по адресу A000 вместо таймера
#define ROM_DISK

# Использовать ROM-диск по адресу A000 со страничным доступом по схеме vinxru (ApogeyRomDisk)
#define EXT_ROM_DISK


# Использовать схему pixeltron
# https://zx-pk.ru/threads/35812-ot-chjornogo-k-belomu-(skhema-kod).html
#define PIXELTRON

# Использовать схему ramfont
# https://zx-pk.ru/threads/36206-ramfont-ruslana-alikberova-(skhema-kod).html
#define RAMFONT

# Использовать цвет для схемы ramfont
#define CC_COLOR


ifndef CFG_SD_TYPE
  @CFG_SD_TYPE = HWMPVV
endif

if CFG_SD_TYPE == HWMPVV
  @SD_TYPE = "hwmpvv"
  @SDOS_ROM = "rk86/sdos_hwmpvv.bin"
endif

if CFG_SD_TYPE == HWMPVV_VV55
  define SD_ON_VV55
  @SD_TYPE = "hwmpvv"
  @SDOS_ROM = "rk86/sdos_hwmpvv_vv55.bin"
endif

if CFG_SD_TYPE == N8VEM
  @SD_TYPE = "n8vem"
  @SDOS_ROM = "rk86/sdos_n8vem.bin"
endif

if CFG_SD_TYPE == N8VEM_VV55
  define SD_ON_VV55
  @SD_TYPE = "n8vem"
  @SDOS_ROM = "rk86/sdos_n8vem_vv55.bin"
endif

if CFG_SD_TYPE == MSX
  @SD_TYPE = "msx"
  @SDOS_ROM = "rk86/sdos_msx.bin"
endif

ifdef MIR
  define SD_CARD
  define RAMFONT
  define CC_COLOR
endif

ifndef SD_CARD
  ifndef ROM_DISK
    ifndef EXT_ROM_DISK
      ifndef SD_ON_VV55
        define TIMER
      endif
    endif
  endif
endif


# Файл справки
ifndef KR03
  @HELP_FILE = rk86/rk86.html
else
  @HELP_FILE = rk86/kr03.html
endif

# Тактовые частоты
@CPU_FREQUENCY = 1777777   # ВМ80, ВИ53, ВТ57
@CRT_FREQUENCY = 1333333   # ВГ75

# Основное ПЗУ и адреса перехвата
ifndef KR03
  @ROM = "rk86/rk86.rom"
else
  @ROM = "rk86/kr03.rom"
endif

@F806_ADDR = 0xfb98
@F80C_ADDR = 0xfc46
@F803_ADDR = 0xfe63

# Знакогенератор
ifndef PIXELTRON
  @SYM_GEN  = "rk86/sgr.bin"
else
  @SYM_GEN  = "rk86/pixeltron.bin"
endif


# Шрифты 8x8, 8x12, 8x16
@FONT     = "rk86/fontr.bin"

# RK-DOS / SDOS
ifdef SDOS
  @DOS = @SDOS_ROM
else
ifdef MIR
  @DOS = "rk86/bios_4e.rom"
else
  @DOS = "rk86/dos29.bin"
endif
endif

# Файл настроек
@OPT_FILE = "rk86/rk86.opt"

# Файл ROM-диска (если не подключен таймер)
ifndef CFG_ROMDISK_FILE
  @CFG_ROMDISK_FILE = "rk86/romdisk.bin"
endif

# Директория SD-карты (vinxru)
ifndef CFG_SD_DIR
  @CFG_SD_DIR = "rk86/sdcard"
endif

# Образ SD-карты (SDOS)
ifndef CFG_SD_IMG
  @CFG_SD_IMG = "rk86/sd_rk86.img"
endif


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

EmuWindow window
window.caption = @NAME

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

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

# 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

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

Ram ram = 0x8000

Rom rom = @ROM, 0x800
Rom extRom = @DOS, 0x1000

Ppi8255 ppi1

ifndef SDOS
  Ppi8255 fdcPpi
endif

ifndef TIMER
  Ppi8255 ppi2
endif

Dma8257 dma
dma.frequency = @CPU_FREQUENCY

Crt8275 crt
crt.frequency = @CRT_FREQUENCY
crt.dma = &dma, 2

ifdef TIMER
  Pit8253 pit
  pit.frequency = @CPU_FREQUENCY
 
  RkPit8253SoundSource sndSource
  sndSource.pit = &pit
endif

ifndef PIXELTRON
  ifndef RAMFONT
    Rk86Renderer crtRenderer
    crtRenderer.altFont = @FONT
    crtRenderer.altRenderer = no
  else
    RkRamFontRenderer crtRenderer

    ifdef CC_COLOR
      Register colorRegister
      connect colorRegister.output -> crtRenderer.colorRec
    endif
  endif
else
  RkPixeltronRenderer crtRenderer
endif

crtRenderer.font = @SYM_GEN
crtRenderer.visibleArea = no
crtRenderer.crt = &crt

GeneralSoundSource beepSoundSource

Rk86Core core
core.window = &window
core.crtRenderer = &crtRenderer
core.beepSoundSource = &beepSoundSource

crt.core = &core

ifndef SDOS
  ifndef MIR
    RkFddRegister fddReg
    RkFddController fdc
    fdc.fddRegister = &fddReg
    fdcPpi.circuit = &fdc

    AddrSpace fdcAddrSpace
    fdcAddrSpace.range = &fdcPpi, 0, 3
    fdcAddrSpace.range = &fddReg, 4, 4
    fdcAddrSpace.addrMask = 7
  else
    DiskImage hdd
    hdd.label = HDD
    hdd.filter = "Образы CF (*.img)|*.img;*.IMG|Все файлы (*.*)|*"
    hdd.fileName = "rk86/mir.img"

    AtaDrive ataDrive
    ataDrive.diskImage = &hdd

    Cf8bitAdapter cfAdapter
    cfAdapter.ataDrive = &ataDrive
  endif
else
  SdCard sdCard
  sdCard.imageFile = @CFG_SD_IMG

ifndef SD_ON_VV55
  SdAdapter sdAdapter
else
  PpiSdAdapter sdAdapter
endif

  sdAdapter.type = @SD_TYPE

  sdAdapter.sdCard = &sdCard
endif



AddrSpace addrSpace

addrSpace.range = &ram, 0, 0x7FFF
addrSpace.range = &ppi1, 0x8000, 0x9FFF

ifdef TIMER
  addrSpace.range = &pit, 0xA000, 0xBFFF
else
  addrSpace.range = &ppi2, 0xA000, 0xBFFF
endif

ifdef SDOS
  addrSpace.range = &crt, 0xC000, 0xCFFF
  addrSpace.range = &sdAdapter, 0xD000, 0xDFFF
else
  ifdef MIR
    addrSpace.range = &crt, 0xC000, 0xCDFF

    ifdef CC_COLOR
      addrSpace.range = &colorRegister, 0xCE00, 0xCEFF
    endif

    addrSpace.range = &cfAdapter, 0xD000, 0xDFFF
  else
    addrSpace.range = &crt, 0xC000, 0xDFFF
  endif
endif

addrSpace.writeRange = &dma, 0xE000, 0xEFFF
addrSpace.readRange = &extRom, 0xE000, 0xEFFF

ifndef SDOS
  ifndef MIR
    addrSpace.range = &fdcAddrSpace, 0xF000, 0xF7FF
  else
    addrSpace.writeRange = &dma, 0xF000, 0xF7FF
  endif
endif

addrSpace.readRange = &rom, 0xF800, 0xFFFF
addrSpace.writeRange = &dma, 0xF800, 0xFFFF


Cpu8080 cpu
cpu.frequency = @CPU_FREQUENCY
cpu.addrSpace = &addrSpace
cpu.core = &core

ifndef MIR
  cpu.startAddr = 0xF800
else
  cpu.startAddr = 0xE000
endif

dma.addrSpace = &addrSpace
dma.cpu = &cpu

GeneralSoundSource tapeSoundSource
#tapeSoundSource.muted = yes

ifndef KR03
  RkKbdLayout kbdLayout
  kbdLayout.layout = smart
else
  KrKbdLayout kbdLayout
  kbdLayout.layout = qwerty
endif


ifndef KR03
  RkKeyboard keyboard
else
  KrKeyboard keyboard
endif


ifndef KR03
  RkPpi8255Circuit ppiCircuit
  ppiCircuit.rkKeyboard = &keyboard
  ppiCircuit.tapeSoundSource = &tapeSoundSource
else 
  Kr03Ppi8255Circuit ppiCircuit
  ppiCircuit.keyboard = &keyboard
  ppiCircuit.tapeSoundSource = &tapeSoundSource
endif


ppi1.circuit = &ppiCircuit

ifdef ROM_DISK
  RkRomDisk romDisk = @CFG_ROMDISK_FILE
  ppi2.circuit = &romDisk
endif

ifdef EXT_ROM_DISK
  ApogeyRomDisk romDisk = @CFG_ROMDISK_FILE
  # по умолчанию размер диска 512КБ, но если задан параметр sizeMB, то его значение должно быть 1,2,4 или 8 МБ
  romDisk.sizeMB = 1

  # альтернативный способ задания размера - количество доп. бит для переключения страниц в 32K
  # default = 4
  #romDisk.extBits = 5

  ppi2.circuit = &romDisk
endif

ifdef SD_CARD
  RkSdController sdCard = @CFG_SD_DIR
  ppi2.circuit = &sdCard
endif

ifdef SDOS
  ifdef SD_ON_VV55
    ppi2.circuit = &sdAdapter
  endif
endif

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


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

RkFileLoader loader
loader.addrSpace = &addrSpace
loader.filter = "Файлы РК-86 (*.rkr, *.gam)|*.rkr;*.gam;*.RKR;*.GAM|Файлы RK-совместимых ПК (*.rk?)|*.rk;*.rk?;*.RK;*.RK?|Все файлы (*.*)|*"

TapeRedirector tapeInFile
tapeInFile.mode = "r"
tapeInFile.filter = "Файлы РК-86 (*.rkr, *.gam)|*.rkr;*.gam;*.RKR;*.GAM|Файлы RK-совместимых ПК (*.rk?)|*.rk;*.rk?;*.RK;*.RK?|Файлы BSM (*.bsm)|*.bsm;*.BSM|Файлы EDM (*.edm)|*.edm;*.EDM|Все файлы (*.*)|*"
#tapeInFile.fileName = "program.gam"

TapeRedirector tapeOutFile
tapeOutFile.mode = "w"
tapeOutFile.filter = "Файлы РК-86 (*.rkr)|*.rkr|Файлы RK-совместимых ПК (*.rk)|*.rk|Файлы BSM (*.bsm)|*.bsm|Файлы EDM (*.edm)|*.edm|Все файлы (*.*)|*"

ifndef SDOS
  ifndef MIR
    FdImage diskA = 80, 2, 1, 3125
    diskA.label = A
    diskA.readOnly = yes
    diskA.filter = "Образы дисков РК-86 (*.rkdisk, *.rdi)|*.rkdisk;*.rdi;*.RDI|Все файлы (*.*)|*"
    #diskA.fileName = "rk86/RK86DOS29.rkdisk"

    FdImage diskB = 80, 2, 1, 3125
    diskB.label = B
    diskB.filter = "Образы дисков РК-86 (*.rkdisk, *.rdi)|*.rkdisk;*.rdi;*.RDI|Все файлы (*.*)|*"

    fdc.fdImage = 0, &diskA
    fdc.fdImage = 1, &diskB
  endif
endif

RkTapeInHook tapeInHook = @F806_ADDR
tapeInHook.tapeRedirector = &tapeInFile
cpu.addHook = &tapeInHook

RkTapeOutHook tapeOutHook = @F80C_ADDR
tapeOutHook.tapeRedirector = &tapeOutFile
cpu.addHook = &tapeOutHook

CloseFileHook closeFileHook = @F803_ADDR
closeFileHook.addTapeRedirector = &tapeInFile
closeFileHook.addTapeRedirector = &tapeOutFile
cpu.addHook = &closeFileHook

EmuObjectGroup tapeGrp
tapeGrp.addItem = &tapeOutHook
tapeGrp.addItem = &tapeInHook
tapeGrp.addItem = &closeFileHook
#tapeGrp.enabled = no



######## Include Saved Options ########

include @OPT_FILE



######## Configuration Window ########

ConfigTab configTab = @NAME
configTab.optFileName = @OPT_FILE

#window.frameScale
ConfigRadioSelector scaleSelector = &window, frameScale, "Масштабирование"
scaleSelector.addItem = 1x, "1x"
scaleSelector.addItem = 2x, "2x"
scaleSelector.addItem = 3x, "3x"
scaleSelector.addItem = 4x, "4x"
scaleSelector.addItem = 5x, "5x"
scaleSelector.addItem = fit, "Растянуть во все окно"
scaleSelector.addItem = bestFit, "Растянуть с целым коэффициентом"
configTab.addControl = 1, &scaleSelector

#window.windowStyle
ConfigRadioSelector styleSelector = &window, windowStyle, "Размер окна"
styleSelector.addItem = autosize, "Автоматически подстраивать"
styleSelector.addItem = resizable, "Изменяемый пользователем"
styleSelector.addItem = fixed, "Фиксированный"
configTab.addControl = 1, &styleSelector

#window.smoothing
ConfigRadioSelector smSelector = &window, smoothing, "Сглаживание"
smSelector.addItem = nearest, "Выкл."
smSelector.addItem = bilinear, "Билинейное"
smSelector.addItem = sharp, "Границы пикселей (рекомендуется)"
configTab.addControl = 1, &smSelector

#window.aspectCorrection
ConfigRadioSelector arSelector = &window, aspectCorrection, "Учитывать Aspect Ratio"
arSelector.addItem = no, "Нет (1:1)"
arSelector.addItem = yes, "Оригинальный AR"
configTab.addControl = 2, &arSelector

#window.wideScreen
ConfigRadioSelector wsSelector = &window, wideScreen, "Формат экрана"
wsSelector.addItem = no, "4:3"
wsSelector.addItem = yes, "16:9"
wsSelector.addItem = custom, "Пользовательский"
configTab.addControl = 2, &wsSelector

#crtRenderer.visibleArea
ConfigRadioSelector cropSelector = &crtRenderer, visibleArea, "Отображать только видимую область"
cropSelector.addItem = no, "Нет"
cropSelector.addItem = yes, "Да"
configTab.addControl = 2, &cropSelector

#window.fieldsMixing
ConfigRadioSelector mixSelector = &window, fieldsMixing, "Смешивание полукадров"
mixSelector.addItem = none, "Выкл."
mixSelector.addItem = mix, "Вкл."
mixSelector.addItem = average, "Усреднять (старый метод)"
mixSelector.addItem = interlace, "Чередование"
mixSelector.addItem = scanline, "Имитация линий растра"
configTab.addControl = 1, &mixSelector

#crtRenderer.altRenderer
ConfigRadioSelector rendSelector = &crtRenderer, altRenderer, "Шрифт ВГ8275"
rendSelector.addItem = no, "Оригинальный шрифт"
rendSelector.addItem = yes, "8×16 / 8×12 / 8×8"
configTab.addControl = 2, &rendSelector

#crtRenderer.colorMode
ConfigRadioSelector colorSelector = &crtRenderer, colorMode, "Режим цвета"
colorSelector.addItem = original, "Ч/б оригинальный"
colorSelector.addItem = mono, "Ч/б"
colorSelector.addItem = color1, "Цветной (схема Толкалина)"
colorSelector.addItem = color2, "Самоцвет-М (схема Акименко)"
colorSelector.addItem = color3, "Цветной (схема Апогея)"
configTab.addControl = 2, &colorSelector

#kbdLayout.layout
ConfigRadioSelector laSelector = &kbdLayout, layout, "Раскладка клавиатуры"
laSelector.addItem = qwerty, "Qwerty"
laSelector.addItem = jcuken, "Йцукен"
laSelector.addItem = smart, "«Умная» раскладка"
configTab.addControl = 3, &laSelector

#tapeGrp.enabled
ConfigRadioSelector tapeSelector = &tapeGrp, enabled, "Перехват обращений к магнитофону"
tapeSelector.addItem = yes, "Вкл."
tapeSelector.addItem = no, "Выкл."
configTab.addControl = 3, &tapeSelector

#platform.codePage
ConfigRadioSelector codePageSelector = &platform, codePage, "Таблица символов отладчика"
codePageSelector.addItem = rk, "РК-86"
codePageSelector.addItem = koi8, "КОИ-8"
configTab.addControl = 3, &codePageSelector
