if CFG_EXT_STORAGE != NONE
  define SDCARD
endif

ifdef SDCARD
  @NAME = "Электроника КР-04 (SD card)"
else
  @NAME = "Электроника КР-04"
endif


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

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

# Основное ПЗУ и адреса перехвата
@ROM0 = "kr04/rom0.bin"
@ROM1 = "kr04/rom1.bin"
@ROM2 = "kr04/rom2.bin"

# Директория SD-карты
ifndef CFG_SD_DIR
  @CFG_SD_DIR = "kr04/sdcard/"
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 = yes
#window.aspectCorrection = no

# 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 = 0xF800
Ram ram2 = 0x800

ram.tag = 1

Rom rom0 = @ROM0, 0x800
Rom rom1 = @ROM1, 0x800
Rom rom2 = @ROM2, 0x800

rom0.addrMask = 0x7FF

AddrSpace page0
page0.range = &ram, 0, 0xF7FF
page0.readRange = &rom0, 0xF800, 0xFFFF
page0.writeRange = &ram2, 0xF800, 0xFFFF

AddrSpace page1
page1.range = &ram, 0, 0xF7FF
page1.readRange = &rom1, 0xF800, 0xFFFF
page1.writeRange = &ram2, 0xF800, 0xFFFF

AddrSpace page2
page2.range = &ram, 0, 0xF7FF
page2.readRange = &rom2, 0xF800, 0xFFFF
page2.writeRange = &ram2, 0xF800, 0xFFFF

AddrSpace page3
page3.range = &rom0, 0, 0xF7FF
page3.readRange = &rom0, 0xF800, 0xFFFF
page3.writeRange = &ram, 0xF800, 0xFFFF

AddrSpaceMapper addrSpace = 4
addrSpace.page = 0, &page0
addrSpace.page = 1, &page1
addrSpace.page = 2, &page2
addrSpace.page = 3, &page3

AddrSpace dmaAddrSpace
dmaAddrSpace.range = &ram, 0, 0xF7FF
dmaAddrSpace.range = &ram2, 0xF800, 0xFFFF

Ppi8255 ppi1
Ppi8255 ppi2
Ppi8255 colorPpi

Dma8257 dma
dma.swapRW = no
dma.frequency = @CPU_FREQUENCY

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

Pit8253 pit
pit.frequency = @CRT_FREQUENCY

Kr04Pit8253SoundSource sndSource
sndSource.pit = &pit

Kr04Renderer crtRenderer
crtRenderer.addrSpace = &dmaAddrSpace
crtRenderer.visibleArea = no
crtRenderer.crt = &crt

Kr04Core core
core.window = &window
core.crtRenderer = &crtRenderer
core.kr04PitSoundSource = &sndSource
core.pit = &pit

pit.core = 0, &core
crt.core = &core

Pit8253Helper pitHelper
pit.helper = 0, &pitHelper

AddrSpace ioAddrSpace
ioAddrSpace.range = &ppi1, 0xC0, 0xC3
ioAddrSpace.range = &ppi2, 0x10, 0x13
ioAddrSpace.range = &pit, 0xD0, 0xD3
ioAddrSpace.range = &crt, 0xE0, 0xE1
ioAddrSpace.range = &dma, 0xF0, 0xFF
ioAddrSpace.range = &colorPpi, 0x00, 0x03


Cpu8080 cpu
cpu.frequency = @CPU_FREQUENCY
#cpu.startAddr = 0xF800
cpu.addrSpace = &addrSpace
cpu.ioAddrSpace = &ioAddrSpace
cpu.core = &core
core.cpu = &cpu

Kr04CpuWaits cpuWaits
cpu.cpuWaits = &cpuWaits

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

GeneralSoundSource tapeSoundSource


KrKbdLayout kbdLayout
kbdLayout.layout = smart

KrKeyboard keyboard

KbdLayoutHelper kbdLayoutHelper
kbdLayoutHelper.delay = 5000
kbdLayout.helper = &kbdLayoutHelper

Kr04Ppi8255Circuit ppiCircuit
ppiCircuit.mapper = &addrSpace
ppiCircuit.keyboard = &keyboard
ppiCircuit.tapeSoundSource = &tapeSoundSource
ppiCircuit.crtRenderer = &crtRenderer

ppi1.circuit = &ppiCircuit

Kr04PpiColor8255Circuit colorCircuit
colorPpi.circuit = &colorCircuit
crtRenderer.colorCircuit = &colorCircuit


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


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

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

TapeRedirector tapeInFile
tapeInFile.mode = "r"
tapeInFile.filter = "Файлы КР-04 и РК-86 (*.rk4, *.rkr)|*.rk4;*.RK4;*.rkr;*.RKR|Файлы 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 = "Файлы КР-04 (*.rk4)|*.rk4;*.RK4|Файлы РК-86 (*.rkr)|*.rkr;*.RKR|Файлы RK-совместимых ПК (*.rk)|*.rk|Файлы BSM (*.bsm)|*.bsm|Файлы EDM (*.edm)|*.edm|Все файлы (*.*)|*"


RkTapeInHook tapeInHook = 0xF96B
tapeInHook.signature = E5D5C54707
tapeInHook.tapeRedirector = &tapeInFile
cpu.addHook = &tapeInHook

RkTapeOutHook tapeOutHook = 0xFA89
tapeOutHook.signature = E5D5C5F53E
tapeOutHook.tapeRedirector = &tapeOutFile
cpu.addHook = &tapeOutHook

CloseFileHook closeFileHook = 0xF8FA
closeFileHook.signature = CD36F9B7CA
closeFileHook.addTapeRedirector = &tapeInFile
closeFileHook.addTapeRedirector = &tapeOutFile
cpu.addHook = &closeFileHook

EmuObjectGroup tapeGrp
tapeGrp.addItem = &tapeOutHook
tapeGrp.addItem = &tapeInHook
tapeGrp.addItem = &closeFileHook



ifdef WX
  include "kr04/kr04_wx.inc"
endif
