1 // license:GPL-2.0+
2 // copyright-holders:Kevin Thacker,Sandro Ronco
3 /*****************************************************************************
4  *
5  * includes/kc.h
6  *
7  ****************************************************************************/
8 #ifndef MAME_INCLUDES_KC_H
9 #define MAME_INCLUDES_KC_H
10 
11 #pragma once
12 
13 /* Devices */
14 #include "imagedev/cassette.h"
15 #include "machine/ram.h"
16 #include "machine/timer.h"
17 
18 // Components
19 #include "cpu/z80/z80.h"
20 #include "machine/z80daisy.h"
21 #include "machine/z80ctc.h"
22 #include "machine/z80pio.h"
23 #include "machine/ram.h"
24 #include "machine/kc_keyb.h"
25 #include "machine/rescap.h"
26 #include "sound/spkrdev.h"
27 #include "emupal.h"
28 #include "screen.h"
29 
30 // Devices
31 #include "imagedev/cassette.h"
32 #include "imagedev/snapquik.h"
33 
34 // Expansions
35 #include "bus/kc/kc.h"
36 #include "bus/kc/ram.h"
37 #include "bus/kc/rom.h"
38 #include "bus/kc/d002.h"
39 #include "bus/kc/d004.h"
40 
41 // Formats
42 #include "formats/kc_cas.h"
43 
44 // from service manual
45 #define KC85_3_CLOCK 1751938
46 #define KC85_4_CLOCK 1773447
47 
48 #define KC85_4_SCREEN_PIXEL_RAM_SIZE 0x04000
49 #define KC85_4_SCREEN_COLOUR_RAM_SIZE 0x04000
50 
51 #define KC85_PALETTE_SIZE 24
52 #define KC85_SCREEN_WIDTH 320
53 #define KC85_SCREEN_HEIGHT 256
54 
55 // cassette input polling frequency
56 #define KC_CASSETTE_TIMER_FREQUENCY attotime::from_hz(44100)
57 
58 class kc_state : public driver_device
59 {
60 public:
kc_state(const machine_config & mconfig,device_type type,const char * tag)61 	kc_state(const machine_config &mconfig, device_type type, const char *tag)
62 		: driver_device(mconfig, type, tag)
63 		, m_maincpu(*this, "maincpu")
64 		, m_z80pio(*this, "z80pio")
65 		, m_z80ctc(*this, "z80ctc")
66 		, m_ram(*this, RAM_TAG)
67 		, m_speaker(*this, "speaker")
68 		, m_cassette(*this, "cassette")
69 		, m_screen(*this, "screen")
70 		, m_expansions(*this, {"m8", "mc", "exp"})
71 	{ }
72 
73 	required_device<z80_device> m_maincpu;
74 	required_device<z80pio_device> m_z80pio;
75 	required_device<z80ctc_device> m_z80ctc;
76 	required_device<ram_device> m_ram;
77 	required_device<speaker_sound_device> m_speaker;
78 	required_device<cassette_image_device> m_cassette;
79 	required_device<screen_device> m_screen;
80 	required_device_array<kcexp_slot_device, 3> m_expansions;
81 
82 	// defined in machine/kc.cpp
83 	virtual void machine_start() override;
84 	virtual void machine_reset() override;
85 
86 	// modules read/write
87 	uint8_t expansion_read(offs_t offset);
88 	void expansion_write(offs_t offset, uint8_t data);
89 	uint8_t expansion_4000_r(offs_t offset);
90 	void expansion_4000_w(offs_t offset, uint8_t data);
91 	uint8_t expansion_8000_r(offs_t offset);
92 	void expansion_8000_w(offs_t offset, uint8_t data);
93 	uint8_t expansion_c000_r(offs_t offset);
94 	void expansion_c000_w(offs_t offset, uint8_t data);
95 	uint8_t expansion_e000_r(offs_t offset);
96 	void expansion_e000_w(offs_t offset, uint8_t data);
97 	uint8_t expansion_io_read(offs_t offset);
98 	void expansion_io_write(offs_t offset, uint8_t data);
99 
100 	// bankswitch
101 	virtual void update_0x00000();
102 	virtual void update_0x04000();
103 	virtual void update_0x08000();
104 	virtual void update_0x0c000();
105 	virtual void update_0x0e000();
106 
107 	// PIO callback
108 	uint8_t pio_porta_r();
109 	uint8_t pio_portb_r();
110 	DECLARE_WRITE_LINE_MEMBER( pio_ardy_cb);
111 	DECLARE_WRITE_LINE_MEMBER( pio_brdy_cb);
112 	void pio_porta_w(uint8_t data);
113 	void pio_portb_w(uint8_t data);
114 
115 	// CTC callback
116 	DECLARE_WRITE_LINE_MEMBER( ctc_zc0_callback );
117 	DECLARE_WRITE_LINE_MEMBER( ctc_zc1_callback );
118 
119 	// keyboard
120 	DECLARE_WRITE_LINE_MEMBER( keyboard_cb );
121 
122 	// cassette
123 	void update_cassette(int state);
124 	void cassette_set_motor(int motor_state);
125 
126 	// speaker
127 	void speaker_update();
128 
129 	// defined in video/kc.cpp
130 	virtual void video_start() override;
131 	virtual uint32_t screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect);
132 	DECLARE_WRITE_LINE_MEMBER( video_toggle_blink_state );
133 	void video_draw_8_pixels(bitmap_ind16 &bitmap, int x, int y, uint8_t colour_byte, uint8_t gfx_byte);
134 
135 	// driver state
136 	uint8_t *             m_ram_base;
137 	std::unique_ptr<uint8_t[]> m_video_ram;
138 	int                 m_pio_data[2];
139 	int                 m_high_resolution;
140 	uint8_t               m_ardy;
141 	uint8_t               m_brdy;
142 	int                 m_kc85_blink_state;
143 	int                 m_k0_line;
144 	int                 m_k1_line;
145 	uint8_t               m_speaker_level;
146 
147 	// cassette
148 	emu_timer *         m_cassette_timer;
149 	emu_timer *         m_cassette_oneshot_timer;
150 	int                 m_astb;
151 	int                 m_cassette_in;
152 
153 	void kc85_palette(palette_device &palette) const;
154 	TIMER_CALLBACK_MEMBER(kc_cassette_oneshot_timer);
155 	TIMER_CALLBACK_MEMBER(kc_cassette_timer_callback);
156 	TIMER_DEVICE_CALLBACK_MEMBER(kc_scanline);
157 
158 	DECLARE_QUICKLOAD_LOAD_MEMBER(quickload_cb);
159 	void kc85_slots(machine_config &config);
160 
161 	void kc85_3(machine_config &config);
162 	void kc85_3_io(address_map &map);
163 	void kc85_3_mem(address_map &map);
164 };
165 
166 
167 class kc85_4_state : public kc_state
168 {
169 public:
kc85_4_state(const machine_config & mconfig,device_type type,const char * tag)170 	kc85_4_state(const machine_config &mconfig, device_type type, const char *tag)
171 		: kc_state(mconfig, type, tag)
172 	{ }
173 
174 	// defined in machine/kc.cpp
175 	virtual void machine_reset() override;
176 
177 	virtual void update_0x04000() override;
178 	virtual void update_0x08000() override;
179 	virtual void update_0x0c000() override;
180 
181 	uint8_t kc85_4_86_r();
182 	uint8_t kc85_4_84_r();
183 	void kc85_4_86_w(uint8_t data);
184 	void kc85_4_84_w(uint8_t data);
185 
186 	// defined in video/kc.cpp
187 	virtual void video_start() override;
188 	virtual uint32_t screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect) override;
189 	void video_control_w(int data);
190 
191 	// driver state
192 	uint8_t               m_port_84_data;
193 	uint8_t               m_port_86_data;
194 	uint8_t *             m_display_video_ram;
195 	void kc85_4(machine_config &config);
196 	void kc85_5(machine_config &config);
197 	void kc85_4_io(address_map &map);
198 	void kc85_4_mem(address_map &map);
199 };
200 
201 #endif // MAME_INCLUDES_KC_H
202