Subversion Repositories KoE_projects

Rev

Blame | Last modification | View Log | Download | RSS feed | ?url?

  1. -- PENTAGON ver. 2.666 (LE) by koe
  2. -- top level design entity, started on 12.07.2008
  3.  
  4. library ieee;
  5. use ieee.std_logic_1164.all;
  6. use ieee.std_logic_unsigned.all;
  7. use ieee.std_logic_arith.all;
  8. USE ieee.numeric_std.ALL;
  9. library work;  
  10. use work.all;
  11.  
  12. -- **************************************************************
  13.  
  14. entity PaE is
  15.         port (
  16.                 clk                                                             : in std_logic;                                                        
  17.                 r_adr                                                           : out std_logic_vector(18 downto 0);   
  18.                 blk0_d                                                          : inout std_logic_vector(7 downto 0);  
  19.                 blk1_d                                                          : inout std_logic_vector(7 downto 0);  
  20.                 snd                                                             : inout std_logic_vector(7 downto 0);
  21.                 str_r                                                           : out std_logic;
  22.                 str_l                                                           : out std_logic;
  23.                 svetodiod                                                       : out std_logic;                                                               
  24.                 ssi                                                             : out std_logic;
  25.                 ksi                                                             : out std_logic;
  26.                 vid_r                                                           : out std_logic_vector(4 downto 0);
  27.                 vid_g                                                           : out std_logic_vector(4 downto 0);
  28.                 vid_b                                                           : out std_logic_vector(4 downto 0);
  29.                 we_blk0chp0                                                     : out std_logic;
  30.                 oe_blk0chp0                                                     : out std_logic;
  31.                 we_blk0chp1                                                     : out std_logic;
  32.                 oe_blk0chp1                                                     : out std_logic;
  33.                 we_blk1chp0                                                     : out std_logic;
  34.                 oe_blk1chp0                                                     : out std_logic;
  35.                 we_blk1chp1                                                     : out std_logic;
  36.                 oe_blk1chp1                                                     : out std_logic;
  37.                 i2c_scl                                                         : in std_logic;
  38.                 i2c_sda                                                         : inout std_logic:='Z';
  39.                 fpga_clk_output                                                 : out std_logic;
  40.                 fpga_mreq_output                                                : out std_logic;
  41.                 fpga_rfsh_output                                                : out std_logic;
  42.                 fpga_wr_output                                                  : out std_logic;
  43.                 fpga_iorq_output                                                : out std_logic;
  44.                 fpga_halt_output                                                : out std_logic;
  45.                 fpga_busack_output                                              : out std_logic;
  46.                 fpga_m1_output                                                  : out std_logic;
  47.                 fpga_rd_output                                                  : out std_logic;
  48.                 fpga_dos_output                                                 : out std_logic;
  49.                 fpga_f_output                                                   : out std_logic;
  50.                 fpga_int_output                                                 : out std_logic;
  51.                 fpga_csr_output                                                 : out std_logic;
  52.                 fpga_rs_output                                                  : out std_logic;
  53.                 fpga_rs_in                                                      : in std_logic;
  54.                 fpga_rdrom_input                                                : in std_logic;
  55.                 fpga_nmi_input                                                  : in std_logic;
  56.                 fpga_busrq_input                                                : in std_logic;
  57.                 fpga_res_input                                                  : in std_logic;
  58.                 fpga_wait_input                                                 : in std_logic;
  59.                 fpga_int_input                                                  : in std_logic;
  60.                 zetneg_oe                                                       : out std_logic;
  61.                 dbusoe                                                          : out std_logic;
  62.                 fpga_dir                                                        : out std_logic;
  63.                 fpga_a                                                          : out std_logic_vector(15 downto 0);
  64.                 fpga_d                                                          : inout std_logic_vector(7 downto 0);
  65.                 fpga_io0                                                        : in std_logic;
  66.                 fpga_io1                                                        : in std_logic;
  67.                 fpga_io2                                                        : in std_logic;
  68.                 fpga_ebl                                                        : out std_logic;
  69.                 fpga_ior                                                        : out std_logic;
  70.                 fpga_iow                                                        : out std_logic;
  71.                 fpga_wrh                                                        : out std_logic;
  72.                 fpga_rdh                                                        : out std_logic;
  73.                 sd_clk                                                          : out std_logic;
  74.                 sd_dataout                                                      : inout std_logic;
  75.                 sd_datain                                                       : in std_logic;
  76.                 sd_cs                                                           : out std_logic      
  77.     );
  78.    
  79. end PaE;
  80.  
  81. -- **************************************************************
  82.  
  83. architecture koe of PaE is
  84.  
  85. component zx_main is
  86.         port (
  87.                 pixel_clock                                                     : in  std_logic;
  88.                 ym_clk                                                          : out std_logic;
  89.                 main_state_counter                                              : out std_logic_vector(1 downto 0);
  90.                 blk0_d                                                          : in std_logic_vector(7 downto 0);
  91.                 portfe                                                          : in std_logic_vector(3 downto 0);
  92.                 gfx_mode                                                        : in std_logic_vector(5 downto 0);
  93.                 int_strobe                                                      : out std_logic;
  94.                 int_delay                                                       : in std_logic_vector(9 downto 0);
  95.                 ssii                                                            : out std_logic;
  96.                 ksii                                                            : out std_logic;
  97.                 vidr                                                            : out std_logic_vector(4 downto 0);
  98.                 vidg                                                            : out std_logic_vector(4 downto 0);
  99.                 vidb                                                            : out std_logic_vector(4 downto 0);
  100.                 pixelc                                                          : out std_logic_vector(9 downto 0);
  101.                 linec                                                           : out std_logic_vector(9 downto 0);
  102.                 flash                                                           : in std_logic;
  103.                 video_address                                                   : out std_logic_vector(12 downto 0);
  104.                 alco_address                                                    : out std_logic_vector(14 downto 0);
  105.                 pollitra_a                                                      : out std_logic_vector(3 downto 0);
  106.                 pollitra_awr                                                    : out std_logic_vector(3 downto 0);
  107.                 pollitra_d                                                      : in std_logic_vector(15 downto 0);
  108.                 border3                                                         : in std_logic;
  109.                 ega_address                                                     : out std_logic_vector(12 downto 0);
  110.                 otmtxt_address                                                  : out std_logic_vector(16 downto 0)
  111. );
  112. end component;
  113.  
  114. component pll1 is
  115.         port    (
  116.                 inclk0                                                          : IN STD_LOGIC  := '0';
  117.                 c0                                                              : OUT STD_LOGIC;
  118.                 c1                                                              : OUT STD_LOGIC        
  119.                         );
  120. end component;
  121.  
  122. component T80a
  123. port            (
  124.                 RESET_n                                                         : in std_logic;
  125.                 CLK_n                                                           : in std_logic;
  126.                 WAIT_n                                                          : in std_logic;
  127.                 INT_n                                                           : in std_logic;
  128.                 NMI_n                                                           : in std_logic;
  129.                 BUSRQ_n                                                         : in std_logic;
  130.                 M1_n                                                            : out std_logic;
  131.                 MREQ_n                                                          : out std_logic;
  132.                 IORQ_n                                                          : out std_logic;
  133.                 RD_n                                                            : out std_logic;
  134.                 WR_n                                                            : out std_logic;
  135.                 RFSH_n                                                          : out std_logic;
  136.                 HALT_n                                                          : out std_logic;
  137.                 BUSAK_n                                                         : out std_logic;
  138.                 A                                                               : out std_logic_vector(15 downto 0);
  139.                 D                                                               : inout std_logic_vector(7 downto 0);
  140.                 IOcycle                                                         : out std_logic;
  141.                 MEMcycle                                                        : out std_logic
  142.                 );
  143. end component;
  144.  
  145. component ym2149
  146.         port            (
  147.                 I_DA                                                            : in  std_logic_vector(7 downto 0);
  148.                 O_DA                                                            : out std_logic_vector(7 downto 0);
  149.                 output                                                          : out std_logic_vector(7 downto 0);
  150.                 strobe_a                                                        : out std_logic;
  151.                 strobe_b                                                        : out std_logic;
  152.                 strobe_c                                                        : out std_logic;
  153.                 RESET_L                                                         : in  std_logic;
  154.                 CLK                                                             : in std_logic;  -- note 6 Mhz
  155.                 wr_addr                                                         : in std_logic;
  156.                 wr_data                                                         : in std_logic;
  157.                 rd_data                                                         : in std_logic;
  158.                 state                                                           : out std_logic_vector(1 downto 0)
  159.                                 );
  160. end component;
  161.  
  162. component ram_cmos
  163.         port            (
  164.                 clock                                                           : IN  std_logic;
  165.                 data                                                            : IN  std_logic_vector(7 DOWNTO 0);
  166.                 write_address                                                   : IN  std_logic_vector(7 DOWNTO 0);
  167.                 read_address                                                    : IN  std_logic_vector(7 DOWNTO 0);
  168.                 we                                                              : IN  std_logic;
  169.                 q                                                               : OUT std_logic_vector(7 DOWNTO 0)
  170.                                 );
  171. end component;
  172.  
  173. component ram_pollitra is
  174.         port            (
  175.                 clock                                                           : IN STD_LOGIC ;
  176.                 data                                                            : IN STD_LOGIC_VECTOR (15 DOWNTO 0);
  177.                 rdaddress                                                       : IN STD_LOGIC_VECTOR (3 DOWNTO 0);
  178.                 wraddress                                                       : IN STD_LOGIC_VECTOR (3 DOWNTO 0);
  179.                 wren                                                            : IN STD_LOGIC  := '1';
  180.                 q                                                               : OUT STD_LOGIC_VECTOR (15 DOWNTO 0)
  181.                                 );
  182. end component;
  183.  
  184. component rom_pollitra is
  185.         port            (
  186.                 address                                                         : IN STD_LOGIC_VECTOR (4 DOWNTO 0);
  187.                 clock                                                           : IN STD_LOGIC ;
  188.                 q                                                               : OUT STD_LOGIC_VECTOR (15 DOWNTO 0)
  189.                                 );
  190. end component;
  191.  
  192. component bdi is
  193.         port            (
  194.                 vg93_cs                                                         : in std_logic;
  195.                 vg93_ram_addr                                                   : out std_logic_vector(19 downto 0);
  196.                 betadisk_transfer                                               : in std_logic := '1';
  197.                 write_byte_n                                                    : out std_logic_vector(7 downto 0);
  198.                 write_sector_n                                                  : out std_logic_vector(7 downto 0);
  199.                 read_sector_n                                                   : out std_logic_vector(7 downto 0);
  200.                 read_byte_n                                                     : out std_logic_vector(7 downto 0);
  201.                 track_f                                                         : out std_logic;
  202.                 sector_f                                                        : out std_logic;
  203.                 restore_f                                                       : out std_logic;
  204.                 vg93_O_data                                                     : out std_logic_vector(7 downto 0);
  205.                 force_interrupt_f                                               : out std_logic;
  206.                 track_pos                                                       : out std_logic_vector(7 downto 0);
  207.                 track_r                                                         : out std_logic_vector(7 downto 0);
  208.                 sector_r                                                        : out std_logic_vector(7 downto 0);
  209.                 status_r                                                        : out std_logic_vector(7 downto 0);
  210.                 betadisk_r                                                      : in std_logic_vector(7 downto 0);
  211.                 vg93intrq                                                       : out std_logic;
  212.                 seek_f                                                          : out std_logic;
  213.                 vg93drq                                                         : out std_logic;
  214.                 step_f                                                          : out std_logic;
  215.                 step_dir                                                        : out std_logic;
  216.                 read_addr_f                                                     : out std_logic;
  217.                 read_f                                                          : out std_logic;
  218.                 write_f                                                         : out std_logic;
  219.                 vg93_data_from_r                                                : out std_logic_vector(7 downto 0);
  220.                 vg_tormoz                                                       : in std_logic;
  221.                 read_t                                                          : out std_logic;
  222.                 write_t                                                         : out std_logic;
  223.                 cpu_rd                                                          : in std_logic;
  224.                 cpu_wr                                                          : in std_logic;
  225.                 cpu_a                                                           : in std_logic_vector(15 downto 0);
  226.                 cpu_d                                                           : in std_logic_vector(7 downto 0);
  227.                 pixel_clock                                                     : in std_logic;
  228.                 hardware_reset                                                  : in std_logic;
  229.                 vg93_data_for_cpu_o                                             : out std_logic_vector(7 downto 0);
  230.                 vg93_data_for_r                                                 : out std_logic_vector(7 downto 0);
  231.                 index                                                           : out std_logic    
  232.                                 );
  233. end component;
  234.  
  235. signal  main_state_counter                                                      : std_logic_vector(1 downto 0);
  236. signal  radr                                                                    : std_logic_vector(18 downto 0);
  237. signal  video_address                                                           : std_logic_vector(12 downto 0);
  238. signal  alco_address                                                            : std_logic_vector(14 downto 0);
  239. signal  ega_address                                                             : std_logic_vector(12 downto 0);
  240. signal  ram_blk                                                                 : std_logic;
  241. signal  pollitra_data_in                                                        : std_logic_vector (15 downto 0);
  242. signal  pollitra_rdaddress                                                      : std_logic_vector (3 downto 0);
  243. signal  pollitra_wraddress                                                      : std_logic_vector (3 downto 0);
  244. signal  pollitra_data_out                                                       : std_logic_vector (15 downto 0);
  245. signal  pollitra_a                                                              : std_logic_vector(3 downto 0);
  246. signal  pollitra_awr                                                            : std_logic_vector(3 downto 0);
  247. signal  pollitra_flag0                                                          : std_logic;
  248. signal  pollitra_flag1                                                          : std_logic;
  249. signal  pollitra_strobe                                                         : std_logic;
  250. signal  border3                                                                 : std_logic;
  251. signal  weblk0chp0                                                              : std_logic;
  252. signal  oeblk0chp0                                                              : std_logic;
  253. signal  weblk0chp1                                                              : std_logic;
  254. signal  oeblk0chp1                                                              : std_logic;
  255. signal  weblk1chp0                                                              : std_logic;
  256. signal  oeblk1chp0                                                              : std_logic;
  257. signal  weblk1chp1                                                              : std_logic;
  258. signal  oeblk1chp1                                                              : std_logic;
  259. signal  z80_full_adr                                                            : std_logic_vector(20 downto 0);
  260. signal  mcu_full_adr                                                            : std_logic_vector(20 downto 0);
  261. signal  video_full_adr                                                          : std_logic_vector(20 downto 0);
  262. signal  full_adr                                                                : std_logic_vector(20 downto 0);
  263. signal  ram_outdata                                                             : std_logic_vector(7 downto 0);
  264. signal  we_enable                                                               : std_logic;
  265. signal  command_from_mcu                                                        : std_logic_vector(6 downto 0);
  266. signal  data_from_mcu                                                           : std_logic_vector(7 downto 0);
  267. signal  counter                                                                 : std_logic_vector(29 downto 0);
  268. signal  pixel_clock                                                             : std_logic;
  269. signal  shifted_clock                                                           : std_logic;
  270. signal  ssii                                                                    : std_logic;
  271. signal  ksii                                                                    : std_logic;
  272. signal  pixel_c                                                                 : std_logic_vector(9 downto 0);
  273. signal  line_c                                                                  : std_logic_vector(9 downto 0);
  274. signal  upload_adr                                                              : std_logic_vector(13 downto 0);
  275. signal  download_adr                                                            : std_logic_vector(14 downto 0);
  276. signal  upload                                                                  : std_logic;
  277. signal  upload_in_process                                                       : std_logic;
  278. signal  svetodiod_on                                                            : std_logic;
  279. signal  svetodiod_off                                                           : std_logic;
  280. signal  ena                                                                     : std_logic;
  281. signal  download                                                                : std_logic;
  282. signal  download_in_process                                                     : std_logic;
  283. signal  download_strobe                                                         : std_logic;
  284. signal  mcu_in_data                                                             : std_logic_vector(7 downto 0);
  285. signal  hardware_reset                                                          : std_logic;
  286. signal  page_number                                                             : std_logic_vector(7 downto 0);
  287. signal  pagenum                                                                 : std_logic;
  288. signal  idle                                                                    : std_logic;
  289. signal  z80_to_ram                                                              : std_logic;
  290. signal  cmd07                                                                   : std_logic;
  291. signal  to_vz80_data                                                            : std_logic_vector(7 downto 0);
  292. signal  cmd05                                                                   : std_logic;
  293. signal  portfe                                                                  : std_logic_vector(7 downto 0) := (others => '1');
  294. signal  cmd02                                                                   : std_logic;
  295. signal  keymatrix0                                                              : std_logic_vector(7 downto 0):=b"11111111";
  296. signal  keymatrix1                                                              : std_logic_vector(7 downto 0):=b"11111111";
  297. signal  keymatrix2                                                              : std_logic_vector(7 downto 0):=b"11111111";
  298. signal  keymatrix3                                                              : std_logic_vector(7 downto 0):=b"11111111";
  299. signal  keymatrix4                                                              : std_logic_vector(7 downto 0):=b"11111111";
  300. signal  key_byte_number                                                         : std_logic_vector(6 downto 0);
  301. signal  port7ffd                                                                : std_logic_vector(7 downto 0);
  302. signal  port7ffdadd                                                             : std_logic_vector(7 downto 0);
  303. signal  atmwindow                                                               : std_logic_vector(2 downto 0);
  304. signal  atmpage                                                                 : std_logic_vector(9 downto 0);
  305. signal  rampage                                                                 : std_logic_vector(6 downto 0);
  306. signal  porteff7                                                                : std_logic_vector(7 downto 0);
  307. signal  portxx77                                                                : std_logic_vector(7 downto 0);
  308. signal  portbf                                                                  : std_logic_vector(7 downto 0):=b"00000000"; --Savelij
  309. signal  cpu3_0                                                                  : std_logic_vector(9 downto 0);
  310. signal  cpu3_1                                                                  : std_logic_vector(9 downto 0);
  311. signal  cpu2_0                                                                  : std_logic_vector(9 downto 0);
  312. signal  cpu2_1                                                                  : std_logic_vector(9 downto 0);
  313. signal  cpu1_0                                                                  : std_logic_vector(9 downto 0);
  314. signal  cpu1_1                                                                  : std_logic_vector(9 downto 0);
  315. signal  cpu0_0                                                                  : std_logic_vector(9 downto 0);
  316. signal  cpu0_1                                                                  : std_logic_vector(9 downto 0);
  317. signal  cpm                                                                     : std_logic:='1'; --Savelij
  318. signal  pen2                                                                    : std_logic;
  319. signal  rom_page                                                                : std_logic_vector(1 downto 0);
  320. signal  rom                                                                     : std_logic;
  321. signal  romram                                                                  : std_logic;
  322. signal  ram_from_c000                                                           : std_logic;
  323. signal  dosen                                                                   : std_logic:='1'; --3dxx etc --Savelij
  324. signal  dos                                                                     : std_logic:='1'; --from dosen and cp/m --Savelij
  325. signal  fpgadir                                                                 : std_logic;
  326. signal  z80_reset_from_mcu                                                      : std_logic;
  327. signal  ioycle                                                                  : std_logic;
  328. signal  cpu_iowr                                                                : std_logic;
  329. signal  cpu_iord                                                                : std_logic;
  330. signal  klovetura                                                               : std_logic_vector(7 downto 0);
  331. signal  romadr14                                                                : std_logic;
  332. signal  cpu_memrd                                                               : std_logic;
  333. signal  cpu_memwr                                                               : std_logic;
  334. signal  m1                                                                      : std_logic;
  335. signal  iorq_after_bus                                                          : std_logic;
  336. signal  read_ports                                                              : std_logic;
  337. signal  precounter                                                              : std_logic_vector(4 downto 0);
  338. signal  cpu_we_enable_res                                                       : std_logic;
  339. signal  cpu_we_enable                                                           : std_logic;
  340. signal  weblk                                                                   : std_logic;
  341. signal  int_strobe                                                              : std_logic;
  342. signal  i2c_scl_b                                                               : std_logic;
  343. signal  i2c_sda_b                                                               : std_logic;
  344. signal  i2c_bit_counter                                                         : std_logic_vector(3 downto 0);
  345. signal  i2c_data_buffer                                                         : std_logic_vector(7 downto 0);
  346. signal  i2c_command_buffer                                                      : std_logic_vector(6 downto 0);
  347. signal  i2c_start_condition                                                     : std_logic;
  348. signal  i2c_strobe                                                              : std_logic;
  349. signal  i2c_data_ena                                                            : std_logic;
  350. signal  i2c_data_strobe                                                         : std_logic;
  351. signal  i2cdstr                                                                 : std_logic;
  352. signal  i2c_to_m_flag                                                           : std_logic;
  353. signal  i2c_to_master                                                           : std_logic := '1';
  354. signal  i2c_mode                                                                : std_logic := '1';
  355. signal  i2c_ena_to_m                                                            : std_logic := '1';
  356. signal  i2c_out_data                                                            : std_logic_vector(7 downto 0);
  357. signal  data_from_fpga                                                          : std_logic_vector(7 downto 0);
  358. signal  i2c_ack                                                                 : std_logic := '1';
  359. signal  i2c_out_ena                                                             : std_logic := '1';
  360. signal  i2c_out_data_strobe                                                     : std_logic;
  361. signal  i2c_in_data_strobe                                                      : std_logic;
  362. signal  ena_st                                                                  : std_logic;
  363. signal  ena_f                                                                   : std_logic;
  364. signal  ebl                                                                     : std_logic;
  365. signal  ebl_iorq                                                                : std_logic;
  366. signal  ior                                                                     : std_logic;
  367. signal  rdh                                                                     : std_logic;
  368. signal  seconds                                                                 : std_logic_vector(7 downto 0);
  369. signal  minutes                                                                 : std_logic_vector(7 downto 0);
  370. signal  hours                                                                   : std_logic_vector(7 downto 0);
  371. signal  week                                                                    : std_logic_vector(7 downto 0);
  372. signal  days                                                                    : std_logic_vector(7 downto 0);
  373. signal  month                                                                   : std_logic_vector(7 downto 0);
  374. signal  year                                                                    : std_logic_vector(7 downto 0);
  375. signal  addr_dff7                                                               : std_logic;
  376. signal  addr_bff7                                                               : std_logic;
  377. signal  portdff7                                                                : std_logic_vector(7 downto 0);
  378. signal  portbff7                                                                : std_logic_vector(7 downto 0);
  379. signal  addr_eff7                                                               : std_logic;
  380. signal  int_counter                                                             : std_logic_vector(5 downto 0);
  381. signal  int_flag0                                                               : std_logic;
  382. signal  int_flag1                                                               : std_logic;
  383. signal  beeper                                                                  : std_logic;
  384. signal  kempston                                                                : std_logic_vector(4 downto 0);
  385. signal  in_l                                                                    : std_logic_vector(9 downto 0);
  386. signal  in_r                                                                    : std_logic_vector(9 downto 0);
  387. signal  mouse_x                                                                 : std_logic_vector(7 downto 0);
  388. signal  mouse_y                                                                 : std_logic_vector(7 downto 0);
  389. signal  mouse_b                                                                 : std_logic_vector(7 downto 0);
  390. --bdi
  391. signal  vg93_ram_addr                                                           : std_logic_vector(19 downto 0);
  392. signal  betadisk_transfer                                                       : std_logic;
  393. signal  betadisk_transmit_counter                                               : std_logic_vector(5 downto 0);
  394. signal  write_byte_number                                                       : std_logic_vector(7 downto 0);
  395. signal  write_sector_num                                                        : std_logic_vector(7 downto 0);
  396. signal  read_sector_num                                                         : std_logic_vector(7 downto 0);
  397. signal  read_byte_number                                                        : std_logic_vector(7 downto 0);
  398. signal  vg93_O_data                                                             : std_logic_vector(7 downto 0);
  399. signal  track_flag                                                              : std_logic;
  400. signal  sector_flag                                                             : std_logic;
  401. signal  restore_flag                                                            : std_logic;
  402. signal  force_interrupt_flag                                                    : std_logic;
  403. signal  track_position                                                          : std_logic_vector(7 downto 0);
  404. signal  track_reg                                                               : std_logic_vector(7 downto 0);
  405. signal  sector_reg                                                              : std_logic_vector(7 downto 0);
  406. signal  status_reg                                                              : std_logic_vector(7 downto 0);
  407. signal  betadisk_reg                                                            : std_logic_vector(7 downto 0);
  408. signal  seek_flag                                                               : std_logic;
  409. signal  vg93_intrq                                                              : std_logic;
  410. signal  step_flag                                                               : std_logic;
  411. signal  vg93_drq                                                                : std_logic;
  412. signal  step_direction                                                          : std_logic;
  413. signal  read_addr_flag                                                          : std_logic;
  414. signal  read_flag                                                               : std_logic;
  415. signal  write_flag                                                              : std_logic;
  416. signal  vg93_data_from_ram                                                      : std_logic_vector(7 downto 0);
  417. signal  vg93_cs                                                                 : std_logic;
  418. signal  read_trz                                                                : std_logic;
  419. signal  write_trz                                                               : std_logic;
  420. signal  vg93_data_for_cpu                                                       : std_logic_vector(7 downto 0);
  421. signal  betadisk_r                                                              : std_logic_vector(7 downto 0);
  422. signal  vg93_transaction                                                        : std_logic;
  423. signal  betadisk_full_adr                                                       : std_logic_vector(20 downto 0);
  424. signal  vg93_data_for_ram                                                       : std_logic_vector(7 downto 0);
  425. signal  vg_tormoz                                                               : std_logic;
  426. signal  betadisk_flags                                                          : std_logic_vector(11 downto 0);
  427. --cmos
  428. signal  cmos_clk                                                                : std_logic;
  429. signal  cmos_data_in                                                            : std_logic_vector(7 downto 0);
  430. signal  cmos_addr                                                               : std_logic_vector(7 downto 0);
  431. signal  cmos_we                                                                 : std_logic;
  432. signal  cmos_start                                                              : std_logic;
  433. signal  cmos_data_out                                                           : std_logic_vector(7 downto 0);
  434. signal  cmos_cpuread                                                            : std_logic;
  435. signal  cmos_cpuwrite                                                           : std_logic;
  436. signal  cmos_mode                                                               : std_logic_vector(3 downto 0);
  437. signal  cmos_nado                                                               : std_logic:='0';
  438. signal  cmos_upload                                                             : std_logic:='1';
  439. signal  cmos_download                                                           : std_logic :='1';
  440. signal  cmos_upload_counter                                                     : std_logic_vector(8 downto 0);
  441. signal  cmos_download_counter                                                   : std_logic_vector(8 downto 0);
  442. signal  seconds_flag                                                            : std_logic:='0';
  443. signal  minutes_flag                                                            : std_logic:='0';
  444. signal  hours_flag                                                              : std_logic:='0';
  445. signal  week_flag                                                               : std_logic:='0';
  446. signal  days_flag                                                               : std_logic:='0';
  447. signal  month_flag                                                              : std_logic:='0';
  448. signal  year_flag                                                               : std_logic:='0';
  449. signal  cmos_upload_strobe                                                      : std_logic:='0';
  450. signal  cmos_download_strobe                                                    : std_logic:='0';
  451. signal  cmos_strobe                                                             : std_logic:='0';
  452. signal  cpu_write_strobe0                                                       : std_logic;
  453. signal  cpu_write_strobe                                                        : std_logic;
  454. signal  cmos_flags                                                              : std_logic_vector(7 downto 0);
  455. signal  cmos_download_reset                                                     : std_logic_vector(1 downto 0);
  456. signal  cmos_download_buffer                                                    : std_logic_vector(7 downto 0);
  457. signal  cmos_cpu_buffer                                                         : std_logic_vector(7 downto 0);
  458. signal  cmos_data_buffer                                                        : std_logic_vector(7 downto 0);
  459. signal  fromcmos2fpga                                                           : std_logic;
  460. signal  fromfpga2cmos                                                           : std_logic;
  461. signal  fromfpga2cpu                                                            : std_logic;
  462. signal  fromcpu2fpga                                                            : std_logic;
  463. signal  iorq_onetime                                                            : std_logic;
  464. signal  cmos_start1                                                             : std_logic_vector(3 downto 0);
  465. signal  i2c_dataonetime                                                         : std_logic;
  466. signal  i2c_mcudataonetime                                                      : std_logic;
  467. -- cpu core signals:
  468. signal  cpu_d                                                                   : std_logic_vector(7 downto 0);
  469. signal  cpu_a                                                                   : std_logic_vector(15 downto 0);
  470. signal  cpu_int                                                                 : std_logic;
  471. signal  cpu_nmi                                                                 : std_logic;
  472. signal  cpu_mreq                                                                : std_logic;
  473. signal  cpu_iorq                                                                : std_logic;
  474. signal  cpu_rd                                                                  : std_logic;
  475. signal  cpu_notrd                                                               : std_logic;
  476. signal  cpu_wr                                                                  : std_logic;
  477. signal  cpu_wait                                                                : std_logic;
  478. signal  cpu_busrq                                                               : std_logic;
  479. signal  cpu_busack                                                              : std_logic;
  480. signal  cpu_res                                                                 : std_logic;
  481. signal  cpu_m1                                                                  : std_logic;
  482. signal  cpu_rfsh                                                                : std_logic;
  483. signal  cpu_halt                                                                : std_logic;
  484. signal  cpu_clk                                                                 : std_logic;
  485. signal  cpu_mc                                                                  : std_logic_vector(2 downto 0);
  486. signal  cpu_ts                                                                  : std_logic_vector(2 downto 0);
  487. signal  turbo                                                                   : std_logic;
  488. signal  cpu_clk_b                                                               : std_logic;
  489. signal  cpu_speed                                                               : std_logic_vector(2 downto 0);
  490. signal  tormoz                                                                  : std_logic;
  491. -- ym2149
  492. signal  ym_number                                                               : std_logic;
  493. signal  ym0_wr_data                                                             : std_logic;
  494. signal  ym0_rd_data                                                             : std_logic;
  495. signal  ym0_wr_addr                                                             : std_logic;
  496. signal  ym1_wr_data                                                             : std_logic;
  497. signal  ym1_rd_data                                                             : std_logic;
  498. signal  ym1_wr_addr                                                             : std_logic;
  499. signal  strobe0_a                                                               : std_logic;
  500. signal  strobe0_b                                                               : std_logic;
  501. signal  strobe0_c                                                               : std_logic;
  502. signal  strobe1_a                                                               : std_logic;
  503. signal  strobe1_b                                                               : std_logic;
  504. signal  strobe1_c                                                               : std_logic;
  505. signal  ym0_do                                                                  : std_logic_vector(7 downto 0);
  506. signal  ym1_do                                                                  : std_logic_vector(7 downto 0);
  507. signal  ym_do                                                                   : std_logic_vector(7 downto 0);
  508. signal  ym0_snd                                                                 : std_logic_vector(7 downto 0);
  509. signal  ym1_snd                                                                 : std_logic_vector(7 downto 0);
  510. signal  ym0a                                                                    : std_logic_vector(7 downto 0);
  511. signal  ym0b                                                                    : std_logic_vector(7 downto 0);
  512. signal  ym0c                                                                    : std_logic_vector(7 downto 0);
  513. signal  ym1a                                                                    : std_logic_vector(7 downto 0);
  514. signal  ym1b                                                                    : std_logic_vector(7 downto 0);
  515. signal  ym1c                                                                    : std_logic_vector(7 downto 0);
  516. signal  snd_right                                                               : std_logic_vector(7 downto 0);
  517. signal  snd_left                                                                : std_logic_vector(7 downto 0);
  518. signal  ym0_state                                                               : std_logic_vector(1 downto 0);
  519. signal  ym1_state                                                               : std_logic_vector(1 downto 0);
  520. signal  ym_clk                                                                  : std_logic;
  521. -- sd-card
  522. signal  spi_enable                                                              : std_logic := '1';
  523. signal  spi_cmd                                                                 : std_logic;
  524. signal  sd_clock                                                                : std_logic;
  525. signal  sd_sync                                                                 : std_logic;
  526. signal  port57wr                                                                : std_logic;
  527. signal  port57rd                                                                : std_logic;
  528. signal  spi_we_data                                                             : std_logic_vector(7 downto 0);
  529. signal  spi_rd_data                                                             : std_logic_vector(7 downto 0);
  530. signal  port57buffer                                                            : std_logic_vector(7 downto 0);
  531. signal  sd_config                                                               : std_logic_vector(7 downto 0);
  532. signal  sd_counter                                                              : std_logic_vector(3 downto 0);
  533. signal  sd_ena0                                                                 : std_logic;
  534. signal  sd_ena1                                                                 : std_logic;
  535. signal  sd_readflag                                                             : std_logic;
  536. signal  sd_writeflag                                                            : std_logic;
  537. signal  sd_active_flag                                                          : std_logic;
  538. signal  sd_stop                                                                 : std_logic;
  539. signal  vidr                                                                    : std_logic_vector(4 downto 0);
  540. signal  vidg                                                                    : std_logic_vector(4 downto 0);
  541. signal  vidb                                                                    : std_logic_vector(4 downto 0);
  542. signal  gfx_mode                                                                : std_logic_vector(5 downto 0); --atm (5..3). pent (2..0)
  543. -- gfx_mode(2 downto 0):
  544. -- 000: standard zx screen
  545. -- 001: alco
  546. -- 010..111 reserved
  547. -- gfx_mode(5 downto 3):
  548. -- 000: ega
  549. -- 010: multicolor 640x200
  550. -- 110: textmode
  551. -- 011: pentagon modes
  552. signal  otmtxt_addr                                                             : std_logic_vector(16 downto 0);
  553. signal  int_delay                                                               : std_logic_vector(9 downto 0):=b"1011000000";
  554. signal  index                                                                   : std_logic;
  555. signal  iorq_tormoz_ena                                                         : std_logic:='1';
  556. signal  iorq_tormoz_flag0                                                       : std_logic:='1';
  557. signal  iorq_tormoz_flag1                                                       : std_logic:='1';
  558. signal  iorq_tormoz_flag                                                        : std_logic:='1';
  559. signal  iorq_tormoz_counter                                                     : std_logic_vector(9 downto 0);
  560. signal  iorq_tormoz_phase                                                       : std_logic:='1';
  561. signal  speed_change                                                            : std_logic:='0';
  562. signal  resync                                                                  : std_logic:='0';
  563. signal  iorq_change                                                             : std_logic:='0';
  564. signal  sd_iorq_change                                                          : std_logic:='0';
  565. signal  change_tormoz_flag                                                      : std_logic:='0';
  566. signal  change_tormoz_flag0                                                     : std_logic:='0';
  567. signal  change_tormoz_flag1                                                     : std_logic:='0';
  568. signal  iocycle                                                                 : std_logic:='0';
  569. signal  memcycle                                                                : std_logic:='0';
  570. signal  ioflag0                                                                 : std_logic:='0';
  571. signal  ioflag1                                                                 : std_logic:='0';
  572. signal  ioflag2                                                                 : std_logic:='0';
  573. signal  ioflag3                                                                 : std_logic:='0';
  574. signal  iocycle1                                                                : std_logic:='0';
  575. signal  iocycle2                                                                : std_logic:='0';
  576. signal  memflag0                                                                : std_logic:='0';
  577. signal  memflag1                                                                : std_logic:='0';
  578. signal  memflag2                                                                : std_logic:='0';
  579. signal  memflag3                                                                : std_logic:='0';
  580. signal  memflag4                                                                : std_logic:='0';
  581. signal  memcycle1                                                               : std_logic:='0';
  582. signal  memcycle2                                                               : std_logic:='0';
  583. signal  memcycle3                                                               : std_logic:='0';
  584. signal  lowclk                                                                  : std_logic;
  585. signal  fastclk                                                                 : std_logic;
  586. signal  read_fe                                                                 : std_logic:='1';
  587. signal  tapeout                                                                 : std_logic:='0';
  588. signal  addiction                                                               : std_logic_vector(1 downto 0):=b"00";
  589. signal  wr_ports_single                                                         : std_logic:='0';
  590. signal  dos_1                                                                   : std_logic:='1';
  591.  
  592. constant key_byte_number_max: natural:= 120;
  593.            
  594.     begin
  595.    
  596. -- led flashing :)
  597. process (clk)
  598.         begin
  599.         if (clk'event and clk='1') then counter(28 downto 0) <= counter(28 downto 0)+'1';
  600.         counter(29) <= spi_we_data(7);
  601.         end if;
  602.         end process;
  603. svetodiod <= (not(counter(25)) and counter(24) and counter(23) and counter(22) and counter(19));      
  604.  
  605. -----------------------------------------------------------------------------------------------------
  606. -- i2c command/data transfer ------------------------------------------------------------------------
  607. -----------------------------------------------------------------------------------------------------
  608. process (pixel_clock, i2c_sda, i2c_scl)
  609. begin
  610. if (pixel_clock'event and pixel_clock = '1') then i2c_scl_b <= i2c_scl; i2c_sda_b <= i2c_sda;
  611. end if;
  612. end process;
  613.  
  614. process (i2c_sda_b, i2c_scl_b)
  615. begin
  616. if (i2c_scl_b = '0') then i2c_start_condition <= '1';
  617.     elsif (i2c_sda_b'event and i2c_sda_b = '0') then i2c_start_condition <= '0';
  618. end if;
  619. end process;
  620.  
  621. process (i2c_scl_b, i2c_sda_b, i2c_bit_counter, i2c_start_condition, i2c_data_ena)
  622. begin
  623. if ((i2c_start_condition='0') or (i2c_bit_counter = b"1001" and i2c_scl='0')) then i2c_bit_counter <= (others => '0');
  624.     elsif (i2c_scl_b'event and i2c_scl_b='1') then
  625.             i2c_bit_counter <= i2c_bit_counter+'1';
  626.         if ((i2c_bit_counter < 7) and (i2c_strobe='0')) then
  627.             i2c_command_buffer(6 downto 1) <= i2c_command_buffer(5 downto 0);
  628.             i2c_command_buffer(0) <= i2c_sda_b;
  629.         end if;
  630.         if (i2c_bit_counter < 8) then
  631.             i2c_data_buffer(7 downto 1) <= i2c_data_buffer(6 downto 0);
  632.             i2c_data_buffer(0) <= i2c_sda_b;
  633.         end if;
  634. end if;
  635. if (i2c_start_condition='0') then i2c_strobe <= '0';
  636.     elsif (i2c_scl_b'event and i2c_scl_b='0') then
  637.         if (i2c_bit_counter=b"1000") then      
  638.             i2c_strobe <='1';
  639.         end if;
  640. end if;
  641. if (i2c_start_condition='0') then i2c_data_ena <= '1';
  642.     elsif (i2c_scl_b'event and i2c_scl_b='1') then
  643.         if (i2c_bit_counter=b"1000") then
  644.             i2c_data_ena <= '0';
  645.         end if;
  646.     end if;
  647. if (i2c_bit_counter = 0) then i2c_data_strobe <='0';
  648.     elsif (i2c_scl_b'event and i2c_scl_b='0') then
  649.         if (i2c_bit_counter=b"1000" and i2c_data_ena = '0') then
  650.             data_from_mcu(7 downto 0) <= i2c_data_buffer(7 downto 0);
  651.             i2c_data_strobe <='1';
  652.         end if;
  653. end if;
  654. end process;        
  655.  
  656. process (i2c_scl_b)
  657. begin
  658. if(i2c_scl_b'event and i2c_scl_b='1') then i2cdstr <= i2c_data_strobe;
  659. end if;
  660. end process;
  661.  
  662. process (i2c_scl_b, i2c_bit_counter)
  663. begin
  664. if (i2c_scl_b'event and i2c_scl_b = '1') then
  665.     if (i2c_bit_counter = b"0111") then i2c_out_data_strobe <= '1';
  666.     else i2c_out_data_strobe <= '0'; end if;
  667. end if;
  668. end process;
  669.  
  670. process (i2c_scl_b, i2c_bit_counter)
  671. begin
  672. if (i2c_scl_b'event and i2c_scl_b='0') then
  673.     if (i2c_bit_counter=b"1000") then i2c_in_data_strobe <='1';
  674.     else i2c_in_data_strobe <='0'; end if;
  675. end if;
  676. end process;
  677.  
  678. process (i2c_scl_b, i2c_start_condition, i2c_to_master, i2c_bit_counter, i2c_ena_to_m)
  679. begin
  680. if (i2c_start_condition = '0' or i2c_to_master = '1') then i2c_ena_to_m <= '1';
  681.     elsif (i2c_scl_b'event and i2c_scl_b='1') then
  682.         if (i2c_bit_counter = b"1000") then
  683.             i2c_ena_to_m <= '0';
  684.         end if;
  685. end if;
  686.    
  687. if(i2c_start_condition = '0') then i2c_to_m_flag <= '1';
  688.     elsif (i2c_scl_b'event and i2c_scl_b = '1') then
  689.         if (i2c_bit_counter = b"1000" and i2c_to_m_flag = '1') then
  690.             i2c_to_m_flag <= '0';
  691.         end if;
  692. end if;
  693.  
  694. if(i2c_start_condition = '0') then i2c_to_master <= '1';
  695.     elsif (i2c_scl_b'event and i2c_scl_b = '1') then
  696.         if (i2c_bit_counter = b"0111" and i2c_to_m_flag='1' and i2c_sda_b = '1') then i2c_to_master <= '0'; end if;
  697.         if (i2c_bit_counter = b"1000" and i2c_sda_b = '1') then i2c_to_master <= '1'; end if; -- mcu returned nack
  698. end if;
  699.  
  700. if (i2c_start_condition = '0' or i2c_to_master = '1') then i2c_mode <= '1';
  701.     elsif (i2c_scl_b'event and i2c_scl_b='0') then
  702.         if (i2c_ena_to_m = '0') then i2c_mode <= i2c_to_master;
  703.         end if;
  704. end if;
  705.  
  706. if (i2c_scl_b'event and i2c_scl_b='0') then
  707.     if (i2c_bit_counter = b"1000") then i2c_out_data(7 downto 0) <= data_from_fpga(7 downto 0); end if;
  708.     if ((i2c_bit_counter > 0) and (i2c_bit_counter < 8)) then i2c_out_data(7 downto 1) <= i2c_out_data(6 downto 0); end if;
  709. end if;
  710. end process;
  711.  
  712. process (i2c_scl_b, i2c_bit_counter, i2c_mode, i2c_ena_to_m)
  713. begin
  714. if (i2c_scl_b'event and i2c_scl_b='0') then
  715.     if (i2c_mode ='1' and i2c_bit_counter=b"1000") then i2c_ack <= '0'; else i2c_ack <='1'; end if;
  716.     if (i2c_ena_to_m = '0' and not(i2c_bit_counter = b"1000")) then i2c_out_ena <='0'; else i2c_out_ena <='1'; end if;
  717. end if;
  718. end process;
  719.  
  720. i2c_sda <= '0' when (i2c_ack = '0')
  721. else i2c_out_data(7) when (i2c_out_ena = '0')
  722. else 'Z';
  723.  
  724. cmos_flags(7 downto 0) <= (year_flag & month_flag & days_flag & week_flag & hours_flag & minutes_flag & seconds_flag & cmos_nado);
  725. betadisk_flags(11 downto 0) <= (track_flag & force_interrupt_flag & vg93_intrq & vg93_drq & restore_flag & seek_flag & step_flag & step_direction & read_addr_flag & read_flag & write_flag & sector_flag);
  726.  
  727. process (i2c_scl_b, i2c_command_buffer, i2c_bit_counter, cmos_flags, betadisk_transfer,
  728.                         betadisk_reg, status_reg, sector_reg, track_reg, track_position, vg93_O_data, betadisk_flags,
  729.                         read_byte_number, read_sector_num, write_byte_number, write_sector_num, cpu_a,
  730.                          cmos_download_counter, cmos_download_buffer)
  731. begin
  732.         if (i2c_scl_b'event and i2c_scl_b = '0') then
  733.                 if (i2c_bit_counter = b"0111") then
  734.                         if(i2c_command_buffer = 9) then data_from_fpga(7 downto 0) <= cmos_flags(7 downto 0); end if;
  735.                        
  736.                         if (betadisk_transfer = '0') then
  737.                                 case betadisk_transmit_counter(5 downto 0) is
  738.                                         when b"000000" => data_from_fpga(7 downto 0) <= betadisk_reg(7 downto 0);
  739.                                         when b"000001" => data_from_fpga(7 downto 0) <= status_reg(7 downto 0);
  740.                                         when b"000010" => data_from_fpga(7 downto 0) <= sector_reg(7 downto 0);
  741.                                         when b"000011" => data_from_fpga(7 downto 0) <= cmos_flags(7 downto 0);
  742.                                         when b"000100" => data_from_fpga(7 downto 0) <= track_position(7 downto 0);
  743.                                         when b"000101" => data_from_fpga(7 downto 0) <= vg93_O_data(7 downto 0);
  744.                                         when b"000110" => data_from_fpga(7 downto 0) <= betadisk_flags(7 downto 0);
  745.                                         when b"000111" => data_from_fpga(7 downto 0) <= (betadisk_flags(11 downto 8) & "0000");
  746.                                         when b"001000" => data_from_fpga(7 downto 0) <= read_byte_number(7 downto 0);
  747.                                         when b"001001" => data_from_fpga(7 downto 0) <= read_sector_num(7 downto 0);
  748.                                         when b"001010" => data_from_fpga(7 downto 0) <= write_byte_number(7 downto 0);
  749.                                         when b"001011" => data_from_fpga(7 downto 0) <= write_sector_num(7 downto 0);
  750.                                         when b"001100" => data_from_fpga(7 downto 0) <= cpu_a(15 downto 8);
  751.                                         when b"001101" => data_from_fpga(7 downto 0) <= cpu_a(7 downto 0);
  752. --                                      when b"001110" => data_from_fpga(7 downto 0) <= breakpoints_status(7 downto 0);
  753.                                         when b"001111" => data_from_fpga(7 downto 0) <= cmos_download_counter(5 downto 0) & b"00";
  754.                                         when b"010000" => data_from_fpga(7 downto 0) <= cmos_download_buffer(7 downto 0);
  755.                                         when others => null;
  756.                                 end case;
  757.                                
  758.                         end if;
  759.                 end if;
  760.         end if;
  761. end process;
  762.  
  763. ---------------------------------------------------------------------------------------------------
  764. -- cmos -------------------------------------------------------------------------------------------
  765. ---------------------------------------------------------------------------------------------------
  766.  
  767. -- from mcu to fpga
  768. process (pixel_clock, i2c_in_data_strobe, hardware_reset, fromfpga2cmos, fromcmos2fpga, i2c_data_buffer)
  769. begin
  770. if (hardware_reset = '0' or fromcmos2fpga = '1') then cmos_upload <= '1';
  771.         elsif (i2c_in_data_strobe = '0') then i2c_dataonetime <= '0';
  772.                 elsif(pixel_clock'event and pixel_clock = '1') then
  773.                         if (i2c_dataonetime = '0' and i2c_in_data_strobe = '1') then                   
  774.                                 i2c_dataonetime <= '1';
  775.                                 if (i2c_command_buffer = 11) then cmos_upload_counter(8 downto 0) <= b"111111110"; end if; -- from mcu
  776.                                         if (i2c_command_buffer = 12) then
  777.                                                 if (not(cmos_upload_counter(8 downto 0) = b"000111111"))
  778.                                                         then cmos_upload_counter(8 downto 0) <= cmos_upload_counter(8 downto 0) + '1'; cmos_upload <= '0';
  779.                                                 end if;
  780.                                         end if;
  781.                         end if;
  782. end if;
  783. end process;
  784.  
  785. -- from fpga to mcu
  786. process (pixel_clock, i2c_out_data_strobe, hardware_reset, betadisk_transmit_counter, betadisk_transfer, fromfpga2cmos, fromcmos2fpga)
  787. begin
  788. if (hardware_reset = '0') then cmos_download <= '1'; cmos_download_counter(5 downto 0) <= b"111111";
  789.         elsif (fromfpga2cmos = '1') then cmos_download <= '1';
  790.                 elsif (i2c_out_data_strobe = '0') then i2c_mcudataonetime <= '0';
  791.                         elsif (pixel_clock'event and pixel_clock = '1') then
  792.                                 if (i2c_mcudataonetime = '0' and i2c_out_data_strobe = '1') then
  793.                                         i2c_mcudataonetime <= '1';
  794.                                         if(betadisk_transfer = '0') then
  795.                                                 if ((betadisk_transmit_counter(5 downto 0) = b"001110") and (cmos_download = '1')) then
  796.                                                         cmos_download_counter(5 downto 0) <= cmos_download_counter(5 downto 0) + '1';
  797.                                                         cmos_download <= '0';
  798.                                                 end if;
  799.                                         end if;
  800.                                 end if;
  801. end if;
  802. end process;
  803.  
  804. process (hardware_reset, pixel_clock, cpu_iorq, cpu_rd, ebl, addr_bff7, cpu_iowr, cpu_res, fromfpga2cpu, fromcpu2fpga, cpu_d, dos)
  805. begin
  806. if(hardware_reset = '0' or fromfpga2cpu = '1' or fromcpu2fpga = '1') then cmos_cpuread <= '1'; cmos_cpuwrite <= '1';
  807.         elsif(cpu_iorq = '1') then iorq_onetime <= '0';
  808.                 elsif (pixel_clock'event and pixel_clock = '1') then
  809.                         if (iorq_onetime = '0' and cpu_res = '1' and cpu_iorq = '0' and cpu_rd= '0' and ebl='1' and addr_bff7 = '0' and dos = '1')
  810.                                 then iorq_onetime <= '1'; cmos_cpuread <= '0';
  811.                         end if;
  812.                         if (iorq_onetime = '0' and cpu_iowr='0' and cpu_res ='1' and ebl='1' and addr_bff7 = '0' and dos = '1')
  813.                                 then iorq_onetime <= '1'; cmos_cpuwrite <= '0'; portbff7(7 downto 0) <= cpu_d(7 downto 0);
  814.                         end if;
  815. end if;
  816. end process;
  817.  
  818. cmos_mode(3 downto 0) <= cmos_upload & cmos_download & cmos_cpuread & cmos_cpuwrite;  
  819. cmos_start1(3 downto 0) <= fromcmos2fpga & fromfpga2cmos & fromfpga2cpu & fromcpu2fpga;
  820.  
  821. process (pixel_clock, hardware_reset, cmos_mode, cmos_upload_counter, cmos_download_counter, cpu_write_strobe, betadisk_transmit_counter, cmos_data_buffer)
  822. begin
  823.     if(hardware_reset='0') then cmos_we <= '1'; cmos_start <= '0';
  824.                 elsif (pixel_clock'event and pixel_clock='0') then
  825.                         if(cmos_start1 = b"0000") then
  826.             case cmos_mode(3 downto 0) is
  827.                 when b"0111" => -- from cmos to fpga buffer
  828.                         cmos_addr(7 downto 0) <= b"00" & cmos_upload_counter(5 downto 0);
  829.                         cmos_we <= '0';
  830.                         cmos_data_in(7 downto 0) <= data_from_mcu(7 downto 0);            
  831.                         cmos_start <= '1';
  832.                         fromcmos2fpga <= '1';
  833.                 when b"1011" => -- from fpga buffer to cmos
  834.                         cmos_addr(7 downto 0) <= b"00" & cmos_download_counter(5 downto 0);
  835.                         cmos_we <= '1';                
  836.                         cmos_start <= '1';
  837.                         cmos_download_reset(1 downto 0) <= b"11";  
  838.                         fromfpga2cmos <= '1';                                                  
  839.                 when b"1101" => -- to z80 from fpga buffer              
  840.                         cmos_addr(7 downto 0) <= portdff7(7 downto 0);
  841.                         cmos_we <= '1';
  842.                         cmos_start <= '1';
  843.                         fromfpga2cpu <= '1';
  844.                 when b"1110" => -- from z80 to fpga buffer
  845.                         cmos_addr(7 downto 0) <= portdff7(7 downto 0);
  846.                         cmos_we <= '0';
  847.                         cmos_data_in(7 downto 0) <= portbff7(7 downto 0);
  848.                         cmos_start <= '1';
  849.                         fromcpu2fpga <= '1';
  850.                 when others => null;
  851.             end case;
  852.                         end if;
  853.             if (cmos_start = '1') then cmos_start <= '0'; cmos_we <= '1'; end if;
  854.                                 if (fromcmos2fpga = '1' and cmos_clk = '0') then fromcmos2fpga <= '0'; end if;
  855.                                 if (fromfpga2cmos = '1' and cmos_clk = '0') then fromfpga2cmos <= '0'; cmos_download_buffer(7 downto 0) <= cmos_data_out(7 downto 0); end if;
  856.                                 if (fromfpga2cpu = '1' and cmos_clk = '0') then fromfpga2cpu <= '0'; cmos_cpu_buffer(7 downto 0) <= cmos_data_out(7 downto 0); end if;
  857.                                 if (fromcpu2fpga = '1' and cmos_clk = '0') then fromcpu2fpga <= '0'; end if;
  858.                            if (cmos_download_reset > 0) then cmos_download_reset(1 downto 0) <= cmos_download_reset(1 downto 0)-'1'; end if;
  859.     end if;
  860.     if (hardware_reset='0') then cmos_clk <= '0';
  861.         elsif(pixel_clock'event and pixel_clock = '1') then
  862.             if (cmos_start = '1') then cmos_clk <= '1';
  863.                 else cmos_clk <= '0';
  864.             end if;
  865.     end if;
  866. end process;
  867.  
  868. process (command_from_mcu, cpu_clk, hardware_reset, cpu_iowr, ebl, addr_bff7, porteff7)
  869. begin
  870. if(hardware_reset='0' or command_from_mcu = 10) then cmos_nado <= '0';
  871.         elsif (cpu_clk'event and cpu_clk='0') then
  872.                 if(cpu_iowr='0' and ebl='1') then
  873.                         if (addr_bff7='0' and porteff7(7) = '1') then cmos_nado <= '1'; end if;
  874.                 end if;
  875. end if;
  876. end process;                    
  877.  
  878. process (cmos_cpuwrite, command_from_mcu, portdff7)
  879. begin  
  880.     if (command_from_mcu = 10) then seconds_flag <= '0';
  881.                                     minutes_flag <= '0';
  882.                                     hours_flag <= '0';
  883.                                     week_flag <= '0';
  884.                                     days_flag <= '0';
  885.                                     month_flag <= '0';
  886.                                     year_flag <= '0';
  887.         elsif (cmos_cpuwrite'event and cmos_cpuwrite = '0') then
  888.             if (portdff7(7 downto 4) = b"0000") then
  889.                 case portdff7(3 downto 0) is
  890.                     when b"0000" => seconds_flag <= '1';
  891.                     when b"0010" => minutes_flag <= '1';
  892.                     when b"0100" => hours_flag <= '1';
  893.                     when b"0110" => week_flag <= '1';
  894.                     when b"0111" => days_flag <= '1';
  895.                     when b"1000" => month_flag <= '1';
  896.                     when b"1001" => year_flag <= '1';
  897.                     when others => null;
  898.                 end case;
  899.             end if;    
  900.     end if;
  901. end process;
  902.  
  903. process (i2c_strobe, i2c_command_buffer)
  904. begin
  905.     if (i2c_strobe'event and i2c_strobe='1') then
  906.         command_from_mcu(6 downto 0) <= i2c_command_buffer(6 downto 0);
  907.                  
  908.                   if (i2c_command_buffer = 0) then idle <= '0'; betadisk_transfer <= '1'; else idle <= '1'; end if;
  909.                   if (i2c_command_buffer = 1) then hardware_reset <= '0'; else hardware_reset <= '1'; end if;
  910.         if (i2c_command_buffer = 2) then cmd02 <= '0'; else cmd02 <= '1'; end if;  
  911.                   if (i2c_command_buffer = 3) then upload <= '0'; else upload <= '1'; end if;
  912.         if (i2c_command_buffer = 4) then download <= '0'; else download <= '1'; end if;
  913.         if (i2c_command_buffer = 5) then cmd05 <= '0'; else cmd05 <= '1'; end if;
  914.                   if (i2c_command_buffer = 6) then pagenum <= '0'; else pagenum <= '1'; end if;
  915.                   if (i2c_command_buffer = 7) then cmd07 <= '0'; else cmd07 <= '1'; end if;
  916.                   if (i2c_command_buffer = 8) then spi_cmd <= '0'; else spi_cmd <= '1'; end if;  
  917.                   if (i2c_command_buffer = 17) then betadisk_transfer <= '0'; end if;
  918.                   if (i2c_command_buffer = 29) then svetodiod_on <= '0'; else svetodiod_on <= '1'; end if;
  919.         if (i2c_command_buffer = 30) then svetodiod_off <= '0'; else svetodiod_off <= '1'; end if;
  920.        
  921.     end if;
  922. end process;
  923.  
  924. -------------------------------------------------
  925. -- spi sd-card
  926.  
  927. process (hardware_reset, spi_cmd, z80_to_ram)
  928. begin
  929.     if (hardware_reset = '0' or z80_to_ram = '1') then spi_enable <= '1';
  930.         elsif (spi_cmd'event and spi_cmd = '1') then spi_enable <= '0';
  931.     end if;
  932. end process;
  933.  
  934. process (cpu_clk, dos, ebl, cpu_a, iorq_after_bus, cpu_wr, cpu_m1)
  935. begin
  936. -- for Savelij
  937. if (cpu_clk'event and cpu_clk = '1') then
  938.     if (
  939.     -- dos = '1' and
  940.     cpu_a(7 downto 0) = x"57"
  941.     and ebl = '1'
  942.     and iorq_after_bus='0' and cpu_wr ='1' and cpu_m1='1')
  943.         then port57rd <='0';
  944.         else port57rd <='1';
  945.     end if;
  946.     if (((dos = '1' and cpu_a(7 downto 0) = x"57") or (dos = '0' and cpu_a(7 downto 0) = x"57" and cpu_a(15) = '0'))
  947.     and ebl = '1'
  948.     and iorq_after_bus='0' and cpu_wr ='0' and cpu_m1='1')
  949.         then port57wr <='0';
  950.         else port57wr <='1';
  951.     end if;
  952. end if;
  953. end process;
  954. sd_clock <= precounter(2);
  955. sd_sync <= port57wr and port57rd;
  956.  
  957. process (sd_clock, port57wr, port57rd, sd_writeflag, sd_readflag, sd_counter, port57buffer)
  958. begin
  959.     if (port57wr = '0') then spi_we_data(7 downto 0) <= port57buffer(7 downto 0);  sd_counter <= (others => '0');
  960.                             sd_ena0 <= '0'; sd_writeflag <= '1';  
  961.         elsif (port57rd = '0') then sd_counter <= (others => '0');
  962.                             sd_ena0 <= '0'; sd_readflag <= '1';  
  963.         elsif (sd_clock'event and sd_clock = '1') then
  964.               if ((sd_writeflag = '1' or sd_readflag = '1') and sd_counter < 8) then
  965.                    sd_ena0 <= '1'; sd_counter <= sd_counter + '1'; spi_rd_data(7 downto 1) <= spi_rd_data(6 downto 0); spi_rd_data(0) <= sd_datain;
  966.               end if;
  967.         elsif (sd_clock'event and sd_clock = '0') then
  968.                                   if (sd_writeflag = '1' and sd_counter > 0 and sd_counter < 8) then spi_we_data(7 downto 1) <= spi_we_data(6 downto 0); end if;
  969.               if (sd_counter = 8) then sd_writeflag <= '0'; sd_readflag <= '0'; sd_stop <='0'; end if;
  970.               if (sd_stop = '0') then sd_stop <= '1'; end if;
  971.     end if;
  972. end process;
  973.  
  974. process (sd_clock, sd_sync, sd_counter)
  975. begin
  976.     if(sd_sync = '0') then sd_ena1 <= '1';
  977.         elsif (sd_clock'event and sd_clock = '0' and sd_counter = 8) then sd_ena1 <= '0';
  978.     end if;
  979. end process;
  980. sd_clk <= (sd_ena0 and sd_ena1 and sd_clock) when (spi_enable = '0') else 'Z';
  981. sd_dataout <= spi_we_data(7) when (spi_enable = '0') else 'Z';
  982. sd_cs <= sd_config(1) when (spi_enable = '0') else 'Z';
  983.  
  984. sd_active_flag <= sd_writeflag or sd_readflag;
  985.  
  986. ------------------------------------------------------------
  987. process (i2c_data_strobe, i2c_start_condition)
  988. begin
  989.     if (i2c_start_condition = '0') then upload_adr <= (others => '1');
  990.         elsif (i2c_data_strobe'event and i2c_data_strobe = '1') then
  991.               if (not(upload_adr = 16384)) then upload_adr <= upload_adr + '1'; end if;
  992.     end if;
  993. end process;
  994.  
  995. process (upload, hardware_reset, upload_adr, weblk)
  996. begin
  997.     if ((hardware_reset = '0') or (upload_adr = 16383 and weblk='0')) then upload_in_process <= '1';
  998.         elsif (upload'event and upload = '1') then upload_in_process <= '0'; -- active low
  999.     end if;
  1000. end process;
  1001.  
  1002. process (i2cdstr, ena, upload_in_process)
  1003. begin
  1004.     if (ena='0') then ena_st <= '0';
  1005.         elsif (i2cdstr'event and i2cdstr='1') then
  1006.               if (upload_in_process = '0') then ena_st <='1'; end if;
  1007.     end if;
  1008. end process;
  1009.  
  1010. weblk <= weblk0chp0 and weblk0chp1 and weblk1chp0 and weblk1chp1;
  1011.  
  1012. process (weblk, ena_st)
  1013. begin
  1014.     if (ena_st='1') then ena <= '0';
  1015.         elsif (weblk'event and weblk ='1') then
  1016.               if (ena='0') then ena <= '1'; end if;
  1017.     end if;
  1018. end process;
  1019. process (pixel_clock)
  1020. begin
  1021.     if (pixel_clock'event and pixel_clock='0') then ena_f <= ena;
  1022. end if;
  1023. end process;
  1024.  
  1025. ---------------------------------------------------------
  1026. -- ext_ram
  1027.  
  1028. -- memory mapper
  1029. atmwindow(2 downto 0) <= port7ffd(4) & cpu_a(15 downto 14);
  1030. atmpage(9 downto 0) <= cpu3_0 when (atmwindow(2 downto 0) = b"011")
  1031.     else cpu3_1 when (atmwindow(2 downto 0) = b"111")
  1032.     else cpu2_0 when (atmwindow(2 downto 0) = b"010")
  1033.     else cpu2_1 when (atmwindow(2 downto 0) = b"110")
  1034.     else cpu1_0 when (atmwindow(2 downto 0) = b"001")
  1035.     else cpu1_1 when (atmwindow(2 downto 0) = b"101")
  1036.     else cpu0_0 when (atmwindow(2 downto 0) = b"000")
  1037.     else cpu0_1 when (atmwindow(2 downto 0) = b"100");
  1038. rampage(6 downto 0) <= not(atmpage(8) & atmpage(5 downto 0)) when (atmpage(7) = '0' and atmpage(6) = '1') --atm ram
  1039.     else b"10010" & atmpage(0) & atmpage(1) when (atmpage(7) = '0' and atmpage(6) = '0') --atm rom
  1040.     else port7ffdadd(0) & port7ffd(5) & port7ffd(7 downto 6) & port7ffd(2 downto 0) when (atmpage(7) = '1' and cpu_a(15 downto 14) = b"11")
  1041.     else b"0000010" when (atmpage(7) = '1' and cpu_a(15 downto 14) = b"10")
  1042.     else b"0000101" when (atmpage(7) = '1' and cpu_a(15 downto 14) = b"01")
  1043.     else b"10010" & dos & fpga_rs_in when (atmpage(7) = '1' and cpu_a(15 downto 14) = b"00" and romram ='0')
  1044.          else b"0000000" when (atmpage(7) = '1' and cpu_a(15 downto 14) = b"00" and romram ='1');
  1045.  
  1046. -- address bus
  1047. z80_full_adr(13 downto 0) <= cpu_a(13 downto 0);
  1048. z80_full_adr(20 downto 14) <= rampage(6 downto 0);
  1049.  
  1050. process (pixel_clock, upload_in_process, download_in_process, page_number, upload_adr, download_adr)
  1051. begin  
  1052.         if (pixel_clock'event and pixel_clock='0') then
  1053.                 if (upload_in_process ='0') then
  1054.                         mcu_full_adr(20 downto 0) <= page_number(6 downto 0) & upload_adr(13 downto 0);
  1055.                 end if;
  1056.                 if (download_in_process ='0') then
  1057.                         mcu_full_adr(20 downto 0) <= page_number(6 downto 0) & download_adr(13 downto 0);
  1058.                 end if;
  1059.                
  1060.                 case gfx_mode(5 downto 0) is
  1061.                         when b"011000" => video_full_adr(20 downto 0) <= (b"00001" & port7ffd(3) & b"10" & video_address(12 downto 0)); -- standard zx screen
  1062.                         when b"011001" => video_full_adr(20 downto 0) <= (b"00001" & port7ffd(3) & alco_address(14 downto 0)); -- pentagon-1024 16 colour
  1063.                         when b"000000" => video_full_adr(20 downto 0) <= (b"0000" & alco_address(14) & port7ffd(3) & '1' & alco_address(13) & ega_address(12 downto 0)); -- ega
  1064.                         when b"010000" => video_full_adr(20 downto 0) <= (b"0000" & alco_address(14) & port7ffd(3) & '1' & alco_address(13) & ega_address(12 downto 0)); -- mc 640x200
  1065.                         when b"110000" => video_full_adr(20 downto 0) <= (b"0000" & otmtxt_addr(16) & port7ffd(3) & '1' & otmtxt_addr(13 downto 0)); -- otm textmode
  1066.                         when others => null;
  1067.                 end case;
  1068.         end if;
  1069. end process;
  1070.          
  1071. betadisk_full_adr(20 downto 0) <= '1' & vg93_ram_addr(19 downto 0);
  1072.        
  1073. full_adr(20 downto 0) <= z80_full_adr(20 downto 0) when (z80_to_ram ='0' and vg93_transaction = '1')
  1074.     else betadisk_full_adr(20 downto 0) when (z80_to_ram ='0' and vg93_transaction = '0')
  1075.     else mcu_full_adr(20 downto 0) when (z80_to_ram ='1');
  1076.  
  1077. ram_outdata(7 downto 0) <= cpu_d(7 downto 0) when (z80_to_ram ='0' and vg93_transaction = '1')
  1078.     else vg93_data_for_ram(7 downto 0) when (z80_to_ram ='0' and vg93_transaction = '0')
  1079.     else data_from_mcu(7 downto 0) when (z80_to_ram ='1');
  1080.  
  1081. process (pixel_clock, main_state_counter)
  1082. begin
  1083.         if (pixel_clock'event and pixel_clock = '1') then
  1084.                 if (main_state_counter(1 downto 0) = b"11") then
  1085.                         radr(18 downto 0) <= video_full_adr(18 downto 0);
  1086.                         blk0_d(7 downto 0) <= (others => 'Z');
  1087.                         blk1_d(7 downto 0) <= (others => 'Z');
  1088.                 end if;
  1089.                
  1090.                 if (main_state_counter(1 downto 0) = b"01" or main_state_counter(1 downto 0) = b"10") then
  1091.                         radr(18 downto 0) <= full_adr(18 downto 0);
  1092.                         if ((z80_to_ram ='0' and cpu_memwr = '0') or (z80_to_ram ='1' and upload_in_process = '0') or (z80_to_ram ='0' and vg93_transaction = '0' and write_flag = '0' )) then -- åñëè â ïàìÿòü ïèøóò
  1093.  
  1094. --              if(full_adr(20) = '0') then
  1095.                     blk0_d(7 downto 0) <= ram_outdata(7 downto 0);
  1096. --                  else
  1097.                     blk1_d(7 downto 0) <= ram_outdata(7 downto 0);
  1098. --              end if;        
  1099.                         end if;
  1100.                 end if;
  1101.         end if;
  1102. end process;
  1103.  
  1104. -- ext ram oe/we
  1105. process (cpu_memwr, cpu_we_enable_res)
  1106. begin
  1107. if(cpu_we_enable_res='0') then cpu_we_enable <= '1';
  1108.     elsif(cpu_memwr'event and cpu_memwr = '0') then cpu_we_enable <= '0';
  1109. end if;
  1110. end process;
  1111.  
  1112. process (weblk, cpu_memwr, cpu_we_enable)
  1113. begin
  1114. if (cpu_memwr = '1') then cpu_we_enable_res <= '1';
  1115.     elsif (weblk'event and weblk = '1') then
  1116.         if(cpu_we_enable = '0') then cpu_we_enable_res <= '0';
  1117.         end if;
  1118. end if;
  1119. end process;
  1120.        
  1121. process (pixel_clock, main_state_counter, we_enable, full_adr, z80_to_ram, cpu_memrd, download_in_process, page_number, vg93_transaction, vg_tormoz, read_flag, ena_f, cpu_memwr, rampage, write_flag)
  1122. begin
  1123. if (pixel_clock'event and pixel_clock='1') then
  1124.     if ((main_state_counter(1 downto 0) = b"01" or main_state_counter(1 downto 0) = b"10") and (   (z80_to_ram = '1' and ena_f = '0') or (z80_to_ram = '0' and ((cpu_memwr = '0' and vg93_transaction = '1' and not((rampage(6 downto 2)=b"10010") or rampage(6 downto 0)=b"1001100"))  or    (vg93_transaction = '0' and write_flag = '0' and vg_tormoz = '1') ))  ) ) --we_enable ='0')
  1125.         then
  1126.             case full_adr(20 downto 19) is
  1127.                 when b"00" => weblk0chp0 <= '0';
  1128.                 when b"01" => weblk0chp1 <= '0';
  1129.                 when b"10" => weblk1chp0 <= '0';
  1130.                 when b"11" => weblk1chp1 <= '0';
  1131.                 when others => null;
  1132.             end case;
  1133.         elsif (main_state_counter(1 downto 0) = b"11") then
  1134.                 weblk0chp0 <= '1';
  1135.                 weblk0chp1 <= '1';
  1136.                 weblk1chp0 <= '1';
  1137.                 weblk1chp1 <= '1';
  1138.     end if;
  1139.  
  1140.     if(((main_state_counter(1 downto 0) = b"01" or main_state_counter(1 downto 0) = b"10") and z80_to_ram ='0' and cpu_memrd='0') or (z80_to_ram ='1' and download_in_process = '0') or (z80_to_ram ='0' and vg93_transaction = '0' and read_flag = '0' and vg_tormoz = '1'))
  1141.             then
  1142.                 case full_adr(20 downto 19) is
  1143.                     when b"00" => oeblk0chp0 <= '0'; oeblk0chp1 <= '1'; oeblk1chp0 <= '1'; oeblk1chp1 <= '1'; ram_blk <= '0';
  1144.                     when b"01" => oeblk0chp0 <= '1'; oeblk0chp1 <= '0'; oeblk1chp0 <= '1'; oeblk1chp1 <= '1'; ram_blk <= '0';
  1145.                     when b"10" => oeblk0chp0 <= '1'; oeblk0chp1 <= '1'; oeblk1chp0 <= '0'; oeblk1chp1 <= '1'; ram_blk <= '1';
  1146.                     when b"11" => oeblk0chp0 <= '1'; oeblk0chp1 <= '1'; oeblk1chp0 <= '1'; oeblk1chp1 <= '0'; ram_blk <= '1';
  1147.                     when others => null;
  1148.                 end case;
  1149.         elsif(main_state_counter(1 downto 0) = b"11" or main_state_counter(1 downto 0) = b"00")
  1150.             then
  1151.                 case video_full_adr(20 downto 19) is
  1152.                     when b"00" => oeblk0chp0 <= '0'; oeblk0chp1 <= '1'; oeblk1chp0 <= '1'; oeblk1chp1 <= '1';
  1153.                     when b"01" => oeblk0chp0 <= '1'; oeblk0chp1 <= '0'; oeblk1chp0 <= '1'; oeblk1chp1 <= '1';
  1154.                     when b"10" => oeblk0chp0 <= '1'; oeblk0chp1 <= '1'; oeblk1chp0 <= '0'; oeblk1chp1 <= '1';
  1155.                     when b"11" => oeblk0chp0 <= '1'; oeblk0chp1 <= '1'; oeblk1chp0 <= '1'; oeblk1chp1 <= '0';
  1156.                     when others => null;
  1157.                 end case;
  1158.         else        oeblk0chp0 <= '1';
  1159.                     oeblk0chp1 <= '1';
  1160.                     oeblk1chp0 <= '1';
  1161.                     oeblk1chp1 <= '1';
  1162.     end if;
  1163. end if;    
  1164. end process;
  1165.  
  1166. process (pixel_clock, main_state_counter, vg93_transaction, read_flag, ram_blk)
  1167. begin
  1168. if (pixel_clock'event and pixel_clock='0') then
  1169.         if (main_state_counter=b"00") then
  1170.                 if (ram_blk = '0')
  1171.                         then to_vz80_data(7 downto 0) <= blk0_d(7 downto 0);
  1172.                         else to_vz80_data(7 downto 0) <= blk1_d(7 downto 0);
  1173.                 end if;
  1174.         end if;
  1175. end if;
  1176.  
  1177. if (pixel_clock'event and pixel_clock='1') then
  1178.         if (main_state_counter=b"00") then
  1179.                 if (vg93_transaction = '0' and read_flag = '0') then vg93_data_from_ram(7 downto 0) <= to_vz80_data(7 downto 0); end if;
  1180.         end if;
  1181. end if;
  1182. end process;
  1183.  
  1184. process (i2c_data_strobe, download, upload_adr)
  1185. begin
  1186.     if (download='0') then download_adr <= (others => '0');
  1187.         elsif (i2c_data_strobe'event and i2c_data_strobe = '1') then
  1188.               if (not(upload_adr = 16384)) then download_adr <= download_adr + '1'; end if;
  1189.     end if;
  1190. end process;
  1191.  
  1192. process (download, download_adr, hardware_reset)
  1193. begin
  1194.     if ((hardware_reset = '0') or (download_adr = 16383)) then download_in_process <= '1';
  1195.         elsif (download'event and download = '1') then download_in_process <= '0'; -- active low
  1196.     end if;
  1197. end process;
  1198.  
  1199. process (pixel_clock, main_state_counter)
  1200. begin
  1201.     if (pixel_clock'event and pixel_clock = '0') then
  1202.         if (main_state_counter = b"11") then download_strobe <= '1';
  1203.         else download_strobe <= '0';
  1204.         end if;
  1205.     end if;
  1206. end process;
  1207.  
  1208. process (download_strobe, ram_blk)
  1209. begin
  1210.     if (download_strobe'event and download_strobe ='1') then
  1211.             if (ram_blk = '0')
  1212.             then mcu_in_data(7 downto 0) <= blk0_d(7 downto 0);
  1213.             else mcu_in_data(7 downto 0) <= blk1_d(7 downto 0);
  1214.             end if;
  1215.     end if;
  1216. end process;
  1217.  
  1218. process (pagenum, hardware_reset)
  1219. begin
  1220.     if (hardware_reset = '0') then page_number <= (others => '1');
  1221.         elsif (pagenum'event and pagenum = '1') then
  1222.         page_number(7 downto 0) <= data_from_mcu(7 downto 0);
  1223.     end if;
  1224. end process;
  1225.  
  1226. process (cmd07, hardware_reset)
  1227. begin
  1228.     if (hardware_reset = '0') then z80_to_ram <= '1';
  1229.         elsif (cmd07'event and cmd07 = '1') then z80_to_ram <='0';
  1230.     end if;
  1231. end process;
  1232.  
  1233. process (pixel_clock, cmd05, hardware_reset)
  1234. begin
  1235.     if (hardware_reset = '0') then z80_reset_from_mcu <= '0';
  1236.         elsif (pixel_clock'event and pixel_clock='0') then
  1237.               if (cmd05 = '0') then z80_reset_from_mcu <= '1'; end if;
  1238.     end if;
  1239. end process;
  1240.  
  1241. cpu_res <= z80_reset_from_mcu and fpga_res_input;
  1242.  
  1243. ------------------------------------------------------------------------
  1244. --  data transfer from mcu to fpga registers
  1245.  
  1246. process (i2c_data_strobe, cmd02)--, textbuffer_we)
  1247. begin
  1248. if (cmd02 = '1') then key_byte_number <= (others => '0');
  1249.                 elsif (i2c_data_strobe'event and i2c_data_strobe = '1') then
  1250.                         if (key_byte_number < key_byte_number_max) then key_byte_number <= key_byte_number + '1';
  1251.                                 if(key_byte_number < 55) then
  1252.                                         case key_byte_number(5 downto 0) is
  1253.                     when b"000000" => keymatrix0(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1254.                     when b"000001" => keymatrix1(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1255.                     when b"000010" => keymatrix2(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1256.                     when b"000011" => keymatrix3(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1257.                     when b"000100" => keymatrix4(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1258.                     when b"000101" => seconds(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1259.                     when b"000110" => minutes(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1260.                     when b"000111" => hours(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1261.                     when b"001000" => week(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1262.                     when b"001001" => days(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1263.                     when b"001010" => month(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1264.                     when b"001011" => year(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1265.                     when b"001100" => mouse_b(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1266.                     when b"001101" => mouse_x(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1267.                     when b"001110" => mouse_y(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1268.                     when b"001111" => kempston(4 downto 0) <= i2c_data_buffer(4 downto 0);
  1269.                     when b"010000" => in_l(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1270.                     when b"010001" => in_l(9 downto 8) <= i2c_data_buffer(1 downto 0);
  1271.                     when b"010010" => in_r(7 downto 0) <= i2c_data_buffer(7 downto 0);
  1272.                     when b"010011" => in_r(9 downto 8) <= i2c_data_buffer(1 downto 0);
  1273.                     when others => null;
  1274.                 end case;
  1275.             end if;
  1276.          end if;
  1277.     end if;
  1278. end process;
  1279.    
  1280. pll_config: pll1 port map (clk,pixel_clock, shifted_clock);
  1281.  
  1282. cpu: T80a port map (cpu_res, cpu_clk, cpu_wait, cpu_int, cpu_nmi,
  1283.     cpu_busrq, cpu_m1, cpu_mreq, cpu_iorq, cpu_rd, cpu_wr,
  1284.     cpu_rfsh, cpu_halt, cpu_busack, cpu_a, cpu_d, iocycle, memcycle);
  1285.  
  1286. cmos: ram_cmos port map (cmos_clk, cmos_data_in(7 downto 0), cmos_addr(7 downto 0), cmos_addr(7 downto 0), cmos_we, cmos_data_out(7 downto 0));
  1287.  
  1288. video_main: zx_main port map (pixel_clock, ym_clk, main_state_counter, blk0_d,
  1289.     portfe(3 downto 0), gfx_mode, int_strobe, int_delay, ssii, ksii,
  1290.     vidr, vidg, vidb, pixel_c, line_c, counter(23), video_address, alco_address,
  1291.     pollitra_a, pollitra_awr, pollitra_data_out, border3, ega_address, otmtxt_addr);
  1292.  
  1293. pollitra: ram_pollitra port map (pixel_clock, pollitra_data_in, pollitra_rdaddress,
  1294.     pollitra_wraddress, pollitra_flag0, pollitra_data_out);
  1295.  
  1296. bdi_emul: betadisk port map (vg93_cs, vg93_ram_addr,
  1297.     betadisk_transfer, write_byte_number, write_sector_num,
  1298.     read_sector_num, read_byte_number, track_flag, sector_flag, restore_flag, vg93_O_data,
  1299.     force_interrupt_flag, track_position, track_reg, sector_reg, status_reg,
  1300.     betadisk_reg, vg93_intrq, seek_flag, vg93_drq, step_flag, step_direction,
  1301.     read_addr_flag, read_flag, write_flag, vg93_data_from_ram,
  1302.     vg_tormoz, read_trz, write_trz, cpu_rd, cpu_wr, cpu_a,
  1303.     cpu_d, pixel_clock, hardware_reset, vg93_data_for_cpu, vg93_data_for_ram, index);
  1304.    
  1305. ---------------------------------------------------------------------------
  1306. ---------------------------------------------------------------------------
  1307.  
  1308. process (cpu_mreq, cpu_wr)
  1309. begin
  1310. if(cpu_mreq = '1') then cpu_memwr <='1';
  1311. elsif(cpu_wr'event and cpu_wr='0') then
  1312.     cpu_memwr <= cpu_mreq;
  1313. end if;
  1314. end process;
  1315.  
  1316. cpu_memrd <= cpu_mreq or cpu_rd;
  1317. cpu_iowr <= iorq_after_bus or cpu_wr;
  1318. cpu_iord <= iorq_after_bus or cpu_rd;
  1319.  
  1320. romadr14 <= fpga_rs_in;
  1321.  
  1322. ssi <= ssii;
  1323. ksi <= ksii;
  1324.  
  1325.  vid_r(4 downto 0) <= vidr(4 downto 0);
  1326.  vid_g(4 downto 0) <= vidg(4 downto 0);
  1327.  vid_b(4 downto 0) <= vidb(4 downto 0);
  1328.  
  1329. rom_page(0) <= port7ffd(4);
  1330. rom_page(1) <= dos;
  1331. rom <= cpu_a(14) or cpu_a(15);    -- 0 => ROM, 1 => RAM
  1332. ram_from_c000 <= cpu_a(14) and cpu_a(15);
  1333.  
  1334. ------------------------------------------------------------------------------
  1335. -- dos trigger (4 rom switching)
  1336. process (cpu_iorq, cpm, portbf)
  1337. begin
  1338. if (cpu_iorq'event and cpu_iorq='1') then
  1339.    dos_1 <= cpm and (not(portbf(0))); --Savelij
  1340. end if;
  1341. end process;
  1342.  
  1343. m1 <= cpu_mreq or cpu_m1 or cpu_rd;
  1344.  
  1345. process (cpu_res, m1, cpu_a, port7ffd, cpm, portbf)
  1346. begin
  1347. if (cpu_res = '0') then dosen <= '0';
  1348. elsif (m1'event and m1='0') then
  1349.     if (cpu_a(8)='1' and cpu_a(9)='0' and cpu_a(13 downto 10) = b"1111" and cpu_a(15 downto 14)=b"00" and port7ffd(4)='1')
  1350.     then dosen <= '0'; end if;
  1351.     if (not(cpu_a(15 downto 14)=b"00") )
  1352.     then dosen <= '1'; end if;
  1353. end if;
  1354. end process;
  1355.  
  1356. process (pixel_clock, dosen, dos_1)
  1357. begin
  1358. if (pixel_clock'event and pixel_clock='0') then
  1359.   dos <=dosen and dos_1;
  1360. end if;
  1361. end process;
  1362.  
  1363. ---------------------------------------------------------------------------------------
  1364. -- ports WR
  1365. process (cpu_clk, cpu_res, iorq_after_bus, cpu_iowr, cmos_download_reset, cpu_m1, ebl, cpu_a, porteff7, port7ffd, addr_bff7, portdff7, dos)
  1366. begin
  1367.     if (cpu_res ='0') then port7ffd(7 downto 0) <= (others => '0');
  1368.                                 porteff7(7 downto 0) <= (others => '0');
  1369.                                 portdff7(7 downto 0) <= (others => '0');
  1370.                                 port7ffdadd(7 downto 0) <= (others => '0');
  1371.                                 ---ATM
  1372.                                 portxx77(7 downto 0) <= b"10101011";
  1373.                                 portbf(0) <= '0'; --Savelij
  1374.                                 cpm <= '0';
  1375.                                 pen2 <= '1'; --access to the palette in dos(0)
  1376.                                 cpu3_0 <= b"1111111111"; --7ffd on
  1377.                                 cpu3_1 <= b"1111111111"; --7ffd on
  1378.                                 cpu2_0 <= b"1111111111"; --7ffd on
  1379.                                 cpu2_1 <= b"1111111111"; --7ffd on
  1380.                                 cpu1_0 <= b"1111111111"; --7ffd on
  1381.                                 cpu1_1 <= b"1111111111"; --7ffd on
  1382.                                 cpu0_0 <= b"1110111111"; --7ffd on
  1383.                                 cpu0_1 <= b"1110111111"; --7ffd on
  1384.                                                                                   wr_ports_single <= '0';
  1385.         elsif (iorq_after_bus = '1') then  pollitra_strobe <= '0'; vg93_cs<='1'; wr_ports_single <= '0';
  1386.            
  1387.                 elsif (cpu_clk'event and cpu_clk='0') then
  1388.                     if(cpu_iowr='0' and ebl='1') then
  1389.                                                                 wr_ports_single <= '1';
  1390.                         -- port fe:
  1391.                         if (cpu_a(0)='0') then portfe(7 downto 0) <= cpu_d(7 downto 0); border3 <= not(cpu_a(3)); end if;
  1392.                         -- port 7ffd (system control 0):
  1393.                         if (cpu_a(15)='0' and cpu_a(1)='0') then
  1394.                             if (porteff7(2)='0' or (porteff7(2)='1' and dos = '0')) then port7ffd(7 downto 0) <= cpu_d(7 downto 0);
  1395.                                 else if (port7ffd(5)= '0') then port7ffd(5 downto 0) <= cpu_d(5 downto 0); end if;
  1396.                             end if;                        
  1397.                         end if;
  1398.                         -- port eff7 (system control 1):
  1399.                         if (cpu_a(3)='0' and cpu_a(5)='1' and cpu_a(12)='0' and cpu_a(15 downto 13) = b"111" and cpu_a(0)='1') then porteff7(7 downto 0) <= cpu_d(7 downto 0); end if;
  1400.                         -- port dff7 (cmos)
  1401.                         if (cpu_a(15 downto 0) = x"DFF7" and porteff7(7) = '1') then portdff7(7 downto 0) <= cpu_d(7 downto 0); end if;
  1402.                         -- sd-card SPI interface (for Savelij):
  1403.                         if (
  1404.                             ((cpu_a(7 downto 0) = x"57") and (dos = '1'))
  1405.                             or((cpu_a(7 downto 0) = x"57") and (dos = '0') and (cpu_a(15) = '0'))
  1406.                         )
  1407.                             then port57buffer(7 downto 0) <= cpu_d(7 downto 0); end if;
  1408.                         if (
  1409.                             ((cpu_a(7 downto 0) = x"77") and (dos = '1') and wr_ports_single = '1')
  1410.                             or((cpu_a(7 downto 0) = x"57") and (dos = '0') and (cpu_a(15) = '1'))
  1411.                         )
  1412.                             then sd_config(7 downto 0) <= cpu_d(7 downto 0); end if;
  1413.                         -- port xxFF
  1414.                         if ((cpu_a(7 downto 0) = x"ff") and ((dos = '1') or (pen2 = '0')) and porteff7(2)='0') then pollitra_strobe <= '1'; end if;
  1415.                         --ATM
  1416.                         if ((cpu_a(7 downto 0) = x"77") and (dos = '0') and wr_ports_single = '1') then
  1417.                             portxx77(7 downto 0) <= cpu_d(7 downto 0);
  1418.                             cpm <= cpu_a(9);
  1419.                             pen2 <= cpu_a(14);
  1420.                         end if;
  1421.                         --Savelij
  1422.                         if (cpu_a(7 downto 0) = x"bf") then
  1423.                             --cpm <= not(cpu_d(0));
  1424.                             portbf(0) <= cpu_d(0);
  1425.                         end if;
  1426.                     end if;
  1427.                     if(dos='0') then
  1428.                         if (cpu_a(0)='1' and cpu_a(1)='1' and cpu_a(7)='0' and cpu_iorq='0' and cpu_m1='1') then vg93_cs<='0'; end if;
  1429.                         if (cpu_a(0)='1' and cpu_a(1)='1' and cpu_a(7)='1' and cpu_iowr='0' and cpu_m1='1') then betadisk_reg(7 downto 0) <= cpu_d(7 downto 0); end if;
  1430.                            
  1431.                         if(cpu_a = x"fff7") then
  1432.                             if(port7ffd(4)='0') then cpu3_0(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1433.                                 else cpu3_1(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1434.                             end if;
  1435.                         end if;
  1436.                         if(cpu_a = x"bff7") then
  1437.                             if(port7ffd(4)='0') then cpu2_0(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1438.                                 else cpu2_1(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1439.                             end if;
  1440.                         end if;
  1441.                         if(cpu_a = x"7ff7") then
  1442.                             if(port7ffd(4)='0') then cpu1_0(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1443.                                 else cpu1_1(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1444.                             end if;
  1445.                         end if;
  1446.                         if(cpu_a = x"3ff7") then
  1447.                             if(port7ffd(4)='0') then cpu0_0(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1448.                                 else cpu0_1(9 downto 0) <= b"11" & cpu_d(7 downto 0);
  1449.                             end if;
  1450.                         end if;
  1451.                         if(cpu_a = x"f7f7") then
  1452.                             if(port7ffd(4)='0') then cpu3_0(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1453.                                 else cpu3_1(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1454.                             end if;
  1455.                         end if;
  1456.                         if(cpu_a = x"b7f7") then
  1457.                             if(port7ffd(4)='0') then cpu2_0(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1458.                                 else cpu2_1(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1459.                             end if;
  1460.                         end if;
  1461.                         if(cpu_a = x"77f7") then
  1462.                             if(port7ffd(4)='0') then cpu1_0(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1463.                                 else cpu1_1(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1464.                             end if;
  1465.                         end if;
  1466.                         if(cpu_a = x"37f7") then
  1467.                             if(port7ffd(4)='0') then cpu0_0(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1468.                                 else cpu0_1(9 downto 0) <= cpu_d(7 downto 6) & b"01" & cpu_d(5 downto 0);
  1469.                             end if;
  1470.                         end if;
  1471.                        
  1472.                     end if;
  1473.                 end if;
  1474.                            
  1475. end process;                                    
  1476.  
  1477. romram <= porteff7(3);
  1478.  
  1479. process (pixel_clock, pollitra_strobe, pollitra_flag1)
  1480. begin
  1481. if(pollitra_strobe = '0') then pollitra_flag1 <= '0';
  1482.     elsif(pixel_clock'event and pixel_clock = '0') then
  1483.         if (pollitra_flag0 = '1') then pollitra_flag1 <= '1'; end if;
  1484. end if;
  1485.  
  1486. if(pollitra_flag1 = '1') then pollitra_flag0 <= '0';
  1487.     elsif(pixel_clock'event and pixel_clock = '0') then
  1488.         if(pollitra_strobe = '1' and pollitra_flag0 = '0') then pollitra_flag0 <= '1'; end if;
  1489. end if;
  1490. end process;
  1491.  
  1492. pollitra_data_in(15 downto 0) <= cpu_a(15 downto 8) & cpu_d(7 downto 0);
  1493. pollitra_rdaddress(3 downto 0) <= pollitra_a(3 downto 0);
  1494. pollitra_wraddress(3 downto 0) <= pollitra_awr(3 downto 0);
  1495.  
  1496. gfx_mode(2 downto 0) <= b"000" when porteff7(0) = '0'
  1497. else b"001" when porteff7(0) = '1';
  1498.  
  1499. gfx_mode(5 downto 3) <= portxx77(2 downto 0);
  1500.  
  1501. beeper <= portfe(4);
  1502. tapeout <= portfe(3);
  1503.  
  1504. r_adr(18 downto 0) <= radr(18 downto 0);
  1505.  
  1506. klovetura(0) <= (keymatrix0(0) or cpu_a(8)) and (keymatrix0(1) or cpu_a(9)) and (keymatrix0(2) or cpu_a(10)) and (keymatrix0(3) or cpu_a(11)) and (keymatrix0(4) or cpu_a(12)) and (keymatrix0(5) or cpu_a(13)) and (keymatrix0(6) or cpu_a(14)) and(keymatrix0(7) or cpu_a(15));
  1507. klovetura(1) <= (keymatrix1(0) or cpu_a(8)) and (keymatrix1(1) or cpu_a(9)) and (keymatrix1(2) or cpu_a(10)) and (keymatrix1(3) or cpu_a(11)) and (keymatrix1(4) or cpu_a(12)) and (keymatrix1(5) or cpu_a(13)) and (keymatrix1(6) or cpu_a(14)) and(keymatrix1(7) or cpu_a(15));
  1508. klovetura(2) <= (keymatrix2(0) or cpu_a(8)) and (keymatrix2(1) or cpu_a(9)) and (keymatrix2(2) or cpu_a(10)) and (keymatrix2(3) or cpu_a(11)) and (keymatrix2(4) or cpu_a(12)) and (keymatrix2(5) or cpu_a(13)) and (keymatrix2(6) or cpu_a(14)) and(keymatrix2(7) or cpu_a(15));
  1509. klovetura(3) <= (keymatrix3(0) or cpu_a(8)) and (keymatrix3(1) or cpu_a(9)) and (keymatrix3(2) or cpu_a(10)) and (keymatrix3(3) or cpu_a(11)) and (keymatrix3(4) or cpu_a(12)) and (keymatrix3(5) or cpu_a(13)) and (keymatrix3(6) or cpu_a(14)) and(keymatrix3(7) or cpu_a(15));
  1510. klovetura(4) <= (keymatrix4(0) or cpu_a(8)) and (keymatrix4(1) or cpu_a(9)) and (keymatrix4(2) or cpu_a(10)) and (keymatrix4(3) or cpu_a(11)) and (keymatrix4(4) or cpu_a(12)) and (keymatrix4(5) or cpu_a(13)) and (keymatrix4(6) or cpu_a(14)) and(keymatrix4(7) or cpu_a(15));
  1511.  
  1512. klovetura(6) <= snd(0);
  1513.  
  1514. klovetura(7) <= '1';
  1515. klovetura(5) <= '1';
  1516.  
  1517. addr_bff7 <= '0' when cpu_a(15 downto 0) = 49143
  1518. else '1';
  1519. addr_dff7 <= '0' when cpu_a(15 downto 0) = 57335
  1520. else '1';
  1521. addr_eff7 <= '0' when cpu_a(15 downto 0) = 61431
  1522. else '1';
  1523.  
  1524. -------------------------------------------------------------------------------------------
  1525. -- cpu data bus
  1526. read_ports <= not(ebl) or cpu_iord;
  1527. read_fe <= read_ports or cpu_a(0);
  1528.  
  1529. cpu_d(7 downto 0) <= to_vz80_data(7 downto 0) when ((cpu_memrd='0' and rom='1') or (cpu_memrd='0' and rom='0' and fpga_rdrom_input='0'))
  1530. else klovetura(7 downto 0) when (read_ports='0' and cpu_a(0)='0')
  1531. else ym_do(7 downto 0) when (read_ports='0' and cpu_a(15 downto 13)=b"111" and cpu_a(1 downto 0)=b"01")
  1532. -- cmos:
  1533. else x"AA" when (read_ports='0' and addr_bff7='0' and porteff7(7)='1' and portdff7(5 downto 0)=x"11")
  1534. else x"20" when (read_ports='0' and addr_bff7='0' and porteff7(7)='1' and portdff7(5 downto 0)=x"0A")
  1535. else x"02" when (read_ports='0' and addr_bff7='0' and porteff7(7)='1' and portdff7(5 downto 0)=x"0B")
  1536. else x"00" when (read_ports='0' and addr_bff7='0' and porteff7(7)='1' and portdff7(5 downto 0)=x"0C")
  1537. else x"80" when (read_ports='0' and addr_bff7='0' and porteff7(7)='1' and portdff7(5 downto 0)=x"0D")
  1538. else seconds(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0)=0 and cmos_flags(7 downto 1) = 0)
  1539. else minutes(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0)=2 and cmos_flags(7 downto 1) = 0)
  1540. else hours(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0)=4 and cmos_flags(7 downto 1) = 0)
  1541. else days(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0)=7 and cmos_flags(7 downto 1) = 0)
  1542. else week(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0)=6 and cmos_flags(7 downto 1) = 0)
  1543. else month(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0)=8 and cmos_flags(7 downto 1) = 0)
  1544. else year(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0)=9 and cmos_flags(7 downto 1) = 0)
  1545. else cmos_cpu_buffer(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and not((portdff7(7 downto 0) > x"30") and (portdff7(7 downto 0) < x"35" )) ) --and portdff7(7 downto 0) > 13
  1546. -- ADC
  1547. else in_l(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0) = x"31")
  1548. else "000000" & in_l(9 downto 8) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0) = x"32")
  1549. else in_r(7 downto 0) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0) = x"33")
  1550. else "000000" & in_r(9 downto 8) when (read_ports='0' and porteff7(7)='1' and addr_bff7='0' and portdff7(7 downto 0) = x"34")
  1551. -- memory config
  1552. else not(cpu0_0(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"00be")
  1553. else not(cpu1_0(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"01be")
  1554. else not(cpu2_0(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"02be")
  1555. else not(cpu3_0(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"03be")
  1556. else not(cpu0_1(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"04be")
  1557. else not(cpu1_1(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"05be")
  1558. else not(cpu2_1(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"06be")
  1559. else not(cpu3_1(7 downto 0)) when (read_ports='0' and cpu_a(15 downto 0)=x"07be")
  1560. else spi_rd_data(7 downto 0) when (read_ports='0'
  1561.                             --and dos = '1' --Savelij
  1562.                             and cpu_a(7 downto 0) = x"57")--else ("00000000") when (read_ports='0' and dos = '1' and cpu_a(7 downto 0) = x"1f")
  1563. else ("000" & kempston(4 downto 0)) when (read_ports='0' and dos = '1' and cpu_a(7 downto 0) = x"1f")
  1564.  
  1565. -- betadisk
  1566. else (status_reg(7 downto 0)) when (dos = '0' and read_ports='0' and cpu_a(7 downto 5) = b"000")
  1567. else (track_reg(7 downto 0))  when (dos = '0' and read_ports='0' and cpu_a(7 downto 5) = b"001")
  1568. else (sector_reg(7 downto 0)) when (dos = '0' and read_ports='0' and cpu_a(7 downto 5) = b"010")
  1569. else (vg93_data_for_cpu(7 downto 0)) when (dos = '0' and read_ports='0' and cpu_a(7 downto 5) = b"011")
  1570. else (vg93_intrq & vg93_drq & b"111111") when (dos = '0' and read_ports='0' and cpu_a(7 downto 5) = b"111")
  1571. -- mouse
  1572. else mouse_x(7 downto 0) when (read_ports='0' and cpu_a(15 downto 0) = x"FBDF")
  1573. else mouse_y(7 downto 0) when (read_ports='0' and cpu_a(15 downto 0) = x"FFDF")
  1574. else mouse_b(7 downto 0) when (read_ports='0' and cpu_a(15 downto 0) = x"FADF")
  1575. -- ide
  1576. else (fpga_d(7 downto 0)) when ((ior = '0') or (rdh = '0') or (cpu_iorq='0' and cpu_rd='0') or (cpu_mreq='0' and cpu_rd='0' and rom='0' and fpga_rdrom_input='1'))
  1577. else (others =>'1') when (cpu_mreq='1' and cpu_m1='0' and cpu_iorq='0' and cpu_rd='1' and cpu_wr='1')
  1578. else (others =>'Z');
  1579.        
  1580. ----------------------------------------------------------------------
  1581. -- ym2149 and turbosound
  1582. process (cpu_clk, cpu_iowr, cpu_m1, cpu_a, cpu_d, cpu_res)
  1583. begin
  1584.     if (cpu_res = '0') then ym_number <= '0'; ym0_wr_addr<='1'; ym0_wr_data<='1'; ym1_wr_addr<='1'; ym1_wr_data<='1';
  1585.         elsif (cpu_clk'event and cpu_clk ='0') then
  1586.             if (cpu_iowr='0' and cpu_m1='1') then
  1587.                 if (cpu_a(15 downto 14)=b"11" and cpu_a(1 downto 0)=b"01") then
  1588.                     case cpu_d(7 downto 0) is
  1589.                         when b"11111111" => ym_number <= '0';
  1590.                         when b"11111110" => ym_number <= '1';
  1591.                         when others => null;
  1592.                     end case;
  1593.                     case ym_number is
  1594.                         when '0' => ym0_wr_addr <='0';
  1595.                         when '1' => ym1_wr_addr <='0';
  1596.                                                                 when others => null;
  1597.                     end case;
  1598.                 end if;
  1599.                 if (cpu_a(15 downto 14)=b"10" and cpu_a(1 downto 0)=b"01") then
  1600.                     case ym_number is
  1601.                         when '0' => ym0_wr_data <='0';
  1602.                         when '1' => ym1_wr_data <='0';
  1603.                                                                 when others => null;
  1604.                     end case;
  1605.                 end if;
  1606.             else ym0_wr_addr<='1'; ym0_wr_data<='1'; ym1_wr_addr<='1'; ym1_wr_data<='1';
  1607.             end if;
  1608.     end if;
  1609. end process;
  1610.  
  1611. ym0_rd_data <='0'   when (ym_number = '0' and cpu_iorq='0' and cpu_rd='0' and cpu_a(15 downto 14)=b"11" and cpu_a(1 downto 0)=b"01")
  1612. else '1';
  1613. ym1_rd_data <='0'   when (ym_number = '1' and cpu_iorq='0' and cpu_rd='0' and cpu_a(15 downto 14)=b"11" and cpu_a(1 downto 0)=b"01")
  1614. else '1';
  1615.  
  1616. ym2149_0: ym2149 port map (cpu_d(7 downto 0), ym0_do(7 downto 0), ym0_snd(7 downto 0), strobe0_a, strobe0_b, strobe0_c, cpu_res, ym_clk, ym0_wr_addr, ym0_wr_data, ym0_rd_data, ym0_state(1 downto 0));      
  1617. ym2149_1: ym2149 port map (cpu_d(7 downto 0), ym1_do(7 downto 0), ym1_snd(7 downto 0), strobe1_a, strobe1_b, strobe1_c, cpu_res, ym_clk, ym1_wr_addr, ym1_wr_data, ym1_rd_data, ym1_state(1 downto 0));      
  1618.  
  1619. ym_do(7 downto 0) <= ym0_do(7 downto 0) when ym_number = '0'
  1620. else ym1_do(7 downto 0);
  1621.  
  1622. process (strobe0_a, cpu_res)
  1623. begin
  1624.     if(cpu_res = '0') then ym0a(7 downto 0) <= (others => '0');
  1625.         elsif(strobe0_a'event and strobe0_a = '1') then ym0a(7 downto 0) <= ym0_snd(7 downto 0);
  1626.     end if;
  1627. end process;
  1628. process (strobe0_b, cpu_res)
  1629. begin
  1630.     if(cpu_res = '0') then ym0b(7 downto 0) <= (others => '0');
  1631.         elsif(strobe0_b'event and strobe0_b = '1') then ym0b(7 downto 0) <= ym0_snd(7 downto 0);
  1632.     end if;
  1633. end process;
  1634. process (strobe0_c, cpu_res)
  1635. begin
  1636.     if(cpu_res = '0') then ym0c(7 downto 0) <= (others => '0');
  1637.         elsif(strobe0_c'event and strobe0_c = '1') then ym0c(7 downto 0) <= ym0_snd(7 downto 0);
  1638.     end if;
  1639. end process;
  1640. process (strobe1_a, cpu_res)
  1641. begin
  1642.     if(cpu_res = '0') then ym1a(7 downto 0) <= (others => '0');
  1643.         elsif(strobe1_a'event and strobe1_a = '1') then ym1a(7 downto 0) <= ym1_snd(7 downto 0);
  1644.     end if;
  1645. end process;
  1646. process (strobe1_b, cpu_res)
  1647. begin
  1648.     if(cpu_res = '0') then ym1b(7 downto 0) <= (others => '0');
  1649.         elsif(strobe1_b'event and strobe1_b = '1') then ym1b(7 downto 0) <= ym1_snd(7 downto 0);
  1650.     end if;
  1651. end process;
  1652. process (strobe1_c, cpu_res)
  1653. begin
  1654.     if(cpu_res = '0') then ym1c(7 downto 0) <= (others => '0');
  1655.         elsif(strobe1_c'event and strobe1_c = '1') then ym1c(7 downto 0) <= ym1_snd(7 downto 0);
  1656.     end if;
  1657. end process;
  1658.  
  1659. addiction <= beeper & tapeout;
  1660.  
  1661. snd_right <= ym0a + ym0b + ym1a + ym1b when addiction(1 downto 0) = b"00"       --beeper = '0'
  1662. else ym0a + ym0b + ym1a + ym1b + 50 when addiction(1 downto 0) = b"01" or addiction(1 downto 0) = b"10" --beeper = '1';
  1663. else ym0a + ym0b + ym1a + ym1b + 100 when addiction(1 downto 0) = b"11"; --beeper = '1';
  1664.  
  1665. snd_left <= ym0b + ym0c + ym1b + ym1c when addiction(1 downto 0) = b"00" --beeper = '0'
  1666. else ym0b + ym0c + ym1b + ym1c + 50 when        addiction(1 downto 0) = b"01" or addiction(1 downto 0) = b"10" --beeper = '1';
  1667. else ym0b + ym0c + ym1b + ym1c + 100 when       addiction(1 downto 0) = b"11";
  1668.  
  1669. process (ym_clk, ym0_state, read_fe)
  1670. begin
  1671. if (read_fe = '0') then snd(0) <= 'Z';
  1672.         elsif (ym_clk'event and ym_clk = '0') then
  1673.                 case ym0_state(1 downto 0) is
  1674.                         when b"11" => snd(7 downto 0) <= snd_right(7 downto 0); str_l <= '0';
  1675.                         when b"10" => str_r <= '1';
  1676.                         when b"01" => str_r <= '0'; snd(7 downto 0) <= snd_left(7 downto 0);
  1677.                         when b"00" => str_l <= '1';
  1678.                 end case;
  1679. end if;
  1680. end process;
  1681.  
  1682. -------------------------------------------------------------
  1683. -- cpu int
  1684. process (cpu_clk, int_flag1)
  1685. begin
  1686.     if (int_flag1='1') then int_counter(5 downto 0) <= b"011111";
  1687.         elsif(cpu_clk'event and cpu_clk='0') then
  1688.             if (not(int_counter(5 downto 0)=b"000000")) then
  1689.                 cpu_int <= '0';
  1690.                 int_counter(5 downto 0) <= int_counter(5 downto 0)-'1';
  1691.                 else cpu_int <= '1';
  1692.             end if;
  1693.     end if;
  1694. end process;
  1695.  
  1696. process(int_strobe, cpu_res, int_flag1)
  1697. begin
  1698. if (cpu_res='0' or int_flag1='1') then int_flag0 <='0';
  1699.     elsif (int_strobe'event and int_strobe = '0') then
  1700.     int_flag0 <='1';
  1701. end if;
  1702. end process;
  1703.  
  1704. process (cpu_clk, int_flag0)
  1705. begin
  1706. if(cpu_clk'event and cpu_clk='1') then
  1707.     if(int_flag0 = '1') then int_flag1 <= '1';
  1708.         else int_flag1 <= '0';
  1709.     end if;
  1710. end if;
  1711. end process;
  1712.  
  1713. ------------------------------------------------------------------------
  1714. -- cpu clocking and stopping
  1715. process (pixel_clock)
  1716. begin
  1717.         if (pixel_clock'event and pixel_clock = '1') then
  1718.                 precounter <= precounter + '1';
  1719.         end if;
  1720. end process;
  1721.  
  1722. cpu_speed(2 downto 0) <= dosen & portxx77(3) & porteff7(4);
  1723.  
  1724. resync <=       precounter(4) when (cpu_speed(1 downto 0)=b"01")
  1725. else                    (precounter(4) or ioflag0) when (iocycle2 = '1')
  1726. else                    (precounter(4)) when (memflag2 = '1')
  1727. else                    (precounter(2) or memflag0) when (cpu_speed(1 downto 0)=b"00" and iocycle2 = '0' and memflag2 = '0')
  1728. else                    (precounter(1) or memflag0) when (cpu_speed(1)='1' and iocycle2 = '0' and memflag2 = '0');
  1729.  
  1730. fastclk <= precounter(2) when (cpu_speed(1 downto 0)=b"00")
  1731. else precounter(1) when (cpu_speed(1)='1');
  1732.  
  1733. lowclk <= precounter(4);
  1734.  
  1735. iocycle1 <= iocycle and cpu_m1;
  1736. iocycle2 <= iocycle1 or ioflag2;
  1737. memcycle1 <= memcycle and not(cpu_a(15) or cpu_a(14)) and fpga_rdrom_input;
  1738.  
  1739. process (pixel_clock, resync, vg_tormoz)
  1740. begin
  1741. if(pixel_clock'event and pixel_clock = '1') then
  1742. cpu_clk_b <= resync;
  1743. cpu_clk <= resync or vg_tormoz;
  1744. end if;
  1745. end process;
  1746.  
  1747. process(iocycle, ioflag1)
  1748. begin
  1749. if(ioflag1 = '1') then ioflag0 <= '0';
  1750.         elsif(iocycle'event and iocycle = '1') then ioflag0 <= '1';
  1751. end if;
  1752. end process;
  1753.  
  1754. process(lowclk, ioflag0)
  1755. begin
  1756. if(ioflag0 = '0') then ioflag1 <= '0';
  1757.         elsif(lowclk'event and lowclk = '1') then
  1758.                 if(ioflag0 = '1') then ioflag1 <= '1';
  1759.                 end if;
  1760. end if;
  1761. end process;
  1762.  
  1763. process(iocycle, ioflag3)
  1764. begin
  1765. if(ioflag3 = '1') then ioflag2 <= '0';
  1766.         elsif(iocycle'event and iocycle = '0') then ioflag2 <= '1';
  1767. end if;
  1768. end process;
  1769.  
  1770. process(fastclk, ioflag2)
  1771. begin
  1772. if(ioflag2 = '0') then ioflag3 <= '0';
  1773.         elsif(fastclk'event and fastclk = '1') then
  1774.                 if(ioflag2 = '1') then ioflag3 <= '1';
  1775.                 end if;
  1776. end if;
  1777. end process;
  1778.  
  1779. process (fastclk, memcycle1, memflag1)
  1780. begin
  1781. if(memflag1 = '1') then memflag0 <= '0';
  1782.         elsif(memflag2 = '1') then memflag0 <= '0';
  1783.                 elsif(fastclk'event and fastclk = '1') then
  1784.                         if (memcycle1 = '1') then memflag0 <= '1'; end if;
  1785. end if;
  1786. end process;
  1787.  
  1788. process(fastclk, memcycle1, memflag0)
  1789. begin
  1790. if(memflag0 = '0') then memflag1 <= '0';
  1791.         elsif(fastclk'event and fastclk = '0') then
  1792.                 if(memcycle1 = '0' and memflag0 = '1') then memflag1 <= '1'; end if;
  1793. end if;
  1794. end process;
  1795.  
  1796. process(lowclk, memflag0, memcycle)
  1797. begin
  1798. if(memcycle = '0') then memflag2 <= '0';
  1799.         elsif(lowclk'event and lowclk = '1') then
  1800.                 if(memflag0 = '1') then memflag2 <= '1'; end if;
  1801. end if;
  1802. end process;
  1803.  
  1804.  
  1805. process (pixel_clock, cpu_clk_b, hardware_reset, read_trz, write_trz, cpu_wr, cpu_rd, cpu_m1, cpu_mreq, cpu_iorq, main_state_counter)
  1806. begin
  1807. if (hardware_reset = '0') then vg_tormoz <= '0';
  1808.     elsif(pixel_clock'event and pixel_clock = '0') then
  1809.         if (cpu_clk_b = '1' and (read_trz = '1' or write_trz = '1') and cpu_wr = '1' and cpu_rd = '1' and cpu_m1 = '1' and cpu_mreq = '1' and cpu_iorq ='1' and cpu_rfsh='1')
  1810.             then vg_tormoz <= '1'; end if;
  1811.         if (vg93_transaction = '0' and main_state_counter=b"11") then vg_tormoz <= '0'; end if;
  1812. end if;
  1813.  
  1814. if (hardware_reset = '0') then vg93_transaction <= '1';  
  1815. elsif(pixel_clock'event and pixel_clock = '1') then
  1816.     if (vg_tormoz = '1' and main_state_counter=b"00") then vg93_transaction <= '0'; end if;
  1817.     if (vg93_transaction = '0' and main_state_counter=b"01" and vg_tormoz = '0') then vg93_transaction <= '1'; end if;
  1818. end if;
  1819. end process;
  1820.  
  1821. ---------------------------------------------------------------------
  1822. -- ZX-BUS signals
  1823. cpu_wait <= '1';
  1824.  
  1825. process (cpu_busack)
  1826. begin
  1827. if (cpu_busack = '0') then cpu_wr <= '1';
  1828. cpu_rd <= '1'; cpu_mreq <= '1'; cpu_iorq <= '1';
  1829. else cpu_wr <= 'Z';
  1830. cpu_rd <= 'Z'; cpu_mreq <= 'Z'; cpu_iorq <= 'Z';
  1831. end if;
  1832. end process;
  1833.  
  1834. fpga_clk_output <= cpu_clk;        
  1835. fpga_mreq_output <= cpu_mreq;
  1836. fpga_rfsh_output <= cpu_rfsh;
  1837. fpga_wr_output <= cpu_wr;
  1838. fpga_iorq_output <= cpu_iorq;
  1839. fpga_halt_output <= cpu_halt;
  1840. fpga_busack_output <= cpu_busack;
  1841. fpga_m1_output <= cpu_m1;
  1842. fpga_rd_output  <= cpu_rd;
  1843. fpga_dos_output <= dos;
  1844. fpga_f_output <= cpu_clk;
  1845. fpga_int_output <= cpu_int;
  1846. fpga_rs_output <= port7ffd(4);
  1847. fpga_csr_output <= rom;
  1848.  
  1849. cpu_nmi <= fpga_nmi_input;
  1850. cpu_busrq <= '1' and fpga_busrq_input;
  1851.  
  1852. fpga_a(15 downto 0) <= cpu_a(15 downto 0);
  1853. zetneg_oe <= not(cpu_busack);
  1854. dbusoe <= not(cpu_busack);
  1855.  
  1856. fpgadir <= cpu_rd and (cpu_m1 or cpu_iorq);
  1857. fpga_dir <= fpgadir;      
  1858.  
  1859. iorq_after_bus <= (cpu_iorq or fpga_io0 or fpga_io1 or fpga_io2);-- or not(ebl)) ;
  1860.  
  1861. fpga_d(7 downto 0) <= (others =>'Z') when fpgadir='0'
  1862. else cpu_d(7 downto 0) when fpgadir='1';
  1863.  
  1864. --------------------------------
  1865. --------------------------------
  1866. -- IDE by Nemo
  1867.  
  1868. --10 30 50 70 90 b0 d0 f0 - first cs,
  1869. --08 28 48 68 88 a8 c8 e8 - second cs.
  1870.  
  1871. ebl <= (not(cpu_m1) or cpu_a(1) or cpu_a(2));        
  1872.  
  1873. ebl_iorq <= ebl or iorq_after_bus;
  1874. fpga_ebl <= ebl_iorq;
  1875.  
  1876. ior <= ebl_iorq or cpu_a(0) or cpu_rd;
  1877. fpga_ior <= ior;
  1878. rdh <= ebl_iorq or cpu_rd or not(cpu_a(0));
  1879. fpga_rdh <= rdh;
  1880.  
  1881. fpga_wrh <= cpu_iowr or ebl or not(cpu_a(0));
  1882. fpga_iow <= cpu_iowr or ebl or cpu_a(0);
  1883.  
  1884. we_blk0chp0 <= weblk0chp0;
  1885. oe_blk0chp0 <= oeblk0chp0;
  1886. we_blk0chp1 <= weblk0chp1;
  1887. oe_blk0chp1 <= oeblk0chp1;
  1888.  
  1889. we_blk1chp0 <= weblk1chp0;
  1890. oe_blk1chp0 <= oeblk1chp0;
  1891. we_blk1chp1 <= weblk1chp1;
  1892. oe_blk1chp1 <= oeblk1chp1;
  1893.  
  1894. end koe;
  1895.