1 // license:BSD-3-Clause
2 // copyright-holders:Sandro Ronco
3 /*********************************************************************
4 
5     includes/x07.h
6 
7 *********************************************************************/
8 #ifndef MAME_INCLUDES_X07_H
9 #define MAME_INCLUDES_X07_H
10 
11 #pragma once
12 
13 #include "cpu/z80/z80.h"
14 #include "sound/beep.h"
15 #include "machine/nvram.h"
16 #include "machine/ram.h"
17 #include "machine/timer.h"
18 #include "imagedev/cassette.h"
19 #include "imagedev/printer.h"
20 #include "formats/x07_cas.h"
21 #include "bus/generic/slot.h"
22 #include "bus/generic/carts.h"
23 #include "emupal.h"
24 
25 //default value for user defined keys, taken for official documentation
26 static const char *const udk_ini[12] = {
27 	"tim?TIME$\r",
28 	"cldCLOAD\"",
29 	"locLOCATE ",
30 	"lstLIST ",
31 	"runRUN\r",
32 	"",
33 	"dat?DATE$\r",
34 	"csvCSAVE\"",
35 	"prtPRINT ",
36 	"slpSLEEP",
37 	"cntCONT\r",
38 	""
39 };
40 
41 static const uint16_t udk_offset[12] =
42 {
43 	0x0000, 0x002a, 0x0054, 0x007e, 0x00a8, 0x00d2,
44 	0x0100, 0x012a, 0x0154, 0x017e, 0x01a8, 0x01d2
45 };
46 
47 static const uint8_t printer_charcode[256] =
48 {
49 	0xff, 0x7f, 0xbf, 0x3f, 0xdf, 0x5f, 0x9f, 0x1f,
50 	0xef, 0x6f, 0xaf, 0x2f, 0xcf, 0x4f, 0x8f, 0x0f,
51 	0xf7, 0x77, 0xb7, 0x37, 0xd7, 0x57, 0x97, 0x17,
52 	0xe7, 0x67, 0xa7, 0x27, 0xc7, 0x47, 0x87, 0x07,
53 	0xfb, 0x7b, 0xbb, 0x3b, 0xdb, 0x5b, 0x9b, 0x1b,
54 	0xeb, 0x6b, 0xab, 0x2b, 0xcb, 0x4b, 0x8b, 0x0b,
55 	0xf3, 0x73, 0xb3, 0x33, 0xd3, 0x53, 0x93, 0x13,
56 	0xe3, 0x63, 0xa3, 0x23, 0xc3, 0x43, 0x83, 0x03,
57 	0xfd, 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, 0x08,
79 	0xf0, 0x70, 0xb0, 0x30, 0xd0, 0x50, 0x90, 0x10,
80 	0xe0, 0x60, 0xa0, 0x20, 0xc0, 0x40, 0x80, 0x00
81 };
82 
83 static const uint8_t t6834_cmd_len[0x47] =
84 {
85 	0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x04, 0x03,
86 	0x01, 0x02, 0x09, 0x01, 0x09, 0x01, 0x01, 0x02,
87 	0x03, 0x03, 0x03, 0x03, 0x05, 0x04, 0x82, 0x02,
88 	0x01, 0x01, 0x0a, 0x02, 0x01, 0x81, 0x81, 0x01,
89 	0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x01, 0x03,
90 	0x02, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
91 	0x01, 0x01, 0x01, 0x01, 0x01, 0x82, 0x01, 0x01,
92 	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
93 	0x01, 0x09, 0x01, 0x03, 0x03, 0x01, 0x01
94 };
95 
96 struct x07_kb
97 {
98 	const char *tag;        //input port tag
99 	uint8_t       mask;       //bit mask
100 	uint8_t       codes[7];   //port codes
101 };
102 
103 static const x07_kb x07_keycodes[56] =
104 {
105 	{"BZ", 0x01, {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}},
106 	{"S1", 0x01, {0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12}},
107 	{"S1", 0x02, {0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16}},
108 	{"S1", 0x04, {0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c}},
109 	{"S1", 0x08, {0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d}},
110 	{"S1", 0x10, {0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e}},
111 	{"S1", 0x20, {0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f}},
112 	{"S1", 0x40, {0x20, 0x20, 0x20, 0x00, 0x20, 0x20, 0x20}},
113 	{"S2", 0x01, {0x5a, 0x7a, 0x1a, 0x00, 0xc2, 0xaf, 0xe1}},
114 	{"S2", 0x02, {0x58, 0x78, 0x18, 0x00, 0xbb, 0x00, 0x98}},
115 	{"S2", 0x04, {0x43, 0x63, 0x03, 0x00, 0xbf, 0x00, 0xe4}},
116 	{"S2", 0x08, {0x56, 0x76, 0x16, 0x00, 0xcb, 0x00, 0x95}},
117 	{"S2", 0x10, {0x42, 0x62, 0x02, 0x00, 0xba, 0x00, 0xed}},
118 	{"S2", 0x20, {0x4e, 0x6e, 0x0e, 0x00, 0xd0, 0x00, 0x89}},
119 	{"S2", 0x40, {0x4d, 0x6d, 0x0d, 0x30, 0xd3, 0x00, 0xf5}},
120 	{"S2", 0x80, {0x2c, 0x3c, 0x00, 0x00, 0xc8, 0xa4, 0x9c}},
121 	{"S3", 0x01, {0x41, 0x61, 0x01, 0x00, 0xc1, 0x00, 0x88}},
122 	{"S3", 0x02, {0x53, 0x73, 0x13, 0x00, 0xc4, 0x00, 0x9f}},
123 	{"S3", 0x04, {0x44, 0x64, 0x04, 0x00, 0xbc, 0x00, 0xef}},
124 	{"S3", 0x08, {0x46, 0x66, 0x06, 0x00, 0xca, 0x00, 0xfd}},
125 	{"S3", 0x10, {0x47, 0x67, 0x07, 0x00, 0xb7, 0x00, 0x9d}},
126 	{"S3", 0x20, {0x48, 0x68, 0x08, 0x00, 0xb8, 0x00, 0xfe}},
127 	{"S3", 0x40, {0x4a, 0x6a, 0x0a, 0x31, 0xcf, 0x00, 0xe5}},
128 	{"S3", 0x80, {0x4b, 0x6b, 0x0b, 0x32, 0xc9, 0x00, 0x9b}},
129 	{"S4", 0x01, {0x51, 0x71, 0x11, 0x00, 0xc0, 0x00, 0x8b}},
130 	{"S4", 0x02, {0x57, 0x77, 0x17, 0x00, 0xc3, 0x00, 0xfb}},
131 	{"S4", 0x04, {0x45, 0x65, 0x05, 0x00, 0xb2, 0xa8, 0x99}},
132 	{"S4", 0x08, {0x52, 0x72, 0x12, 0x00, 0xbd, 0x00, 0xf6}},
133 	{"S4", 0x10, {0x54, 0x74, 0x14, 0x00, 0xb6, 0x00, 0x97}},
134 	{"S4", 0x20, {0x59, 0x79, 0x19, 0x35, 0xdd, 0x00, 0x96}},
135 	{"S4", 0x40, {0x55, 0x75, 0x15, 0x34, 0xc5, 0x00, 0x94}},
136 	{"S4", 0x80, {0x49, 0x69, 0x09, 0x00, 0xc6, 0x00, 0xf9}},
137 	{"S5", 0x01, {0x31, 0x21, 0x00, 0x00, 0xc7, 0x00, 0xe9}},
138 	{"S5", 0x02, {0x32, 0x22, 0x00, 0x00, 0xcc, 0x00, 0x90}},
139 	{"S5", 0x04, {0x33, 0x23, 0x00, 0x00, 0xb1, 0xa7, 0x91}},
140 	{"S5", 0x08, {0x34, 0x24, 0x00, 0x00, 0xb3, 0xa9, 0x92}},
141 	{"S5", 0x10, {0x35, 0x25, 0x00, 0x00, 0xb4, 0xaa, 0x93}},
142 	{"S5", 0x20, {0x36, 0x26, 0x00, 0x00, 0xb5, 0xab, 0xec}},
143 	{"S5", 0x40, {0x37, 0x27, 0x37, 0x00, 0xd4, 0xac, 0xe0}},
144 	{"S5", 0x80, {0x38, 0x28, 0x37, 0x00, 0xd5, 0xad, 0xf2}},
145 	{"S7", 0x01, {0x2e, 0x3e, 0x00, 0x2e, 0xd9, 0xa1, 0x9a}},
146 	{"S7", 0x02, {0x2f, 0x3f, 0x00, 0x00, 0xd2, 0xa5, 0x80}},
147 	{"S7", 0x04, {0x3f, 0xa5, 0x00, 0x00, 0x00, 0x00, 0x00}},
148 	{"S7", 0x08, {0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d}},
149 	{"S7", 0x10, {0x4f, 0x6f, 0x0f, 0x36, 0xd7, 0x00, 0x9e}},
150 	{"S7", 0x20, {0x50, 0x70, 0x10, 0x00, 0xbe, 0x00, 0xf7}},
151 	{"S7", 0x40, {0x40, 0x60, 0x00, 0x00, 0xde, 0x00, 0xe7}},
152 	{"S7", 0x80, {0x5b, 0x7b, 0x00, 0x00, 0xdf, 0xa2, 0x84}},
153 	{"S8", 0x01, {0x4c, 0x6c, 0x0c, 0x33, 0xd8, 0x00, 0xf4}},
154 	{"S8", 0x02, {0x3b, 0x2b, 0x00, 0x00, 0xda, 0x00, 0x82}},
155 	{"S8", 0x04, {0x3a, 0x2a, 0x00, 0x00, 0xb9, 0x00, 0x81}},
156 	{"S8", 0x08, {0x5d, 0x7d, 0x00, 0x00, 0xd1, 0xa3, 0x85}},
157 	{"S8", 0x10, {0x39, 0x29, 0x39, 0x00, 0xd6, 0xae, 0xf1}},
158 	{"S8", 0x20, {0x30, 0x7c, 0x00, 0x00, 0xdc, 0xa6, 0x8a}},
159 	{"S8", 0x40, {0x2d, 0x3d, 0x00, 0x00, 0xce, 0x00, 0xf0}},
160 	{"S8", 0x80, {0x3d, 0x7e, 0x00, 0x00, 0xcd, 0x00, 0xfc}}
161 };
162 
163 class x07_state : public driver_device
164 {
165 public:
x07_state(const machine_config & mconfig,device_type type,const char * tag)166 	x07_state(const machine_config &mconfig, device_type type, const char *tag)
167 		: driver_device(mconfig, type, tag)
168 		, m_maincpu(*this, "maincpu")
169 		, m_printer(*this, "printer")
170 		, m_beep(*this, "beeper")
171 		, m_ram(*this, RAM_TAG)
172 		, m_nvram1(*this, "nvram1")
173 		, m_nvram2(*this, "nvram2")
174 		, m_cassette(*this, "cassette")
175 		, m_card(*this, "cardslot")
176 		, m_warm_start(1)
177 	{ }
178 
179 	void x07(machine_config &config);
180 
181 	void init_x07();
182 
183 	DECLARE_INPUT_CHANGED_MEMBER( kb_keys );
184 	DECLARE_INPUT_CHANGED_MEMBER( kb_func_keys );
185 	DECLARE_INPUT_CHANGED_MEMBER( kb_break );
186 	DECLARE_INPUT_CHANGED_MEMBER( kb_update_udk );
187 
188 private:
189 	required_device<cpu_device> m_maincpu;
190 	required_device<printer_image_device> m_printer;
191 	required_device<beep_device> m_beep;
192 	required_device<ram_device> m_ram;
193 	required_device<nvram_device> m_nvram1;
194 	required_device<nvram_device> m_nvram2;
195 	required_device<cassette_image_device> m_cassette;
196 	required_device<generic_slot_device> m_card;
197 
198 	void machine_start() override;
199 	void machine_reset() override;
200 	uint32_t screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect);
201 	uint8_t x07_io_r(offs_t offset);
202 	void x07_io_w(offs_t offset, uint8_t data);
203 
204 	void nvram_init(nvram_device &nvram, void *data, size_t size);
205 
206 	void t6834_cmd(uint8_t cmd);
207 	void t6834_r();
208 	void t6834_w();
209 	void cassette_w();
210 	void cassette_r();
211 	void printer_w();
212 	void kb_irq();
213 	void cassette_load();
214 	void cassette_save();
215 	void receive_bit(int bit);
216 
217 	inline uint8_t get_char(uint16_t pos);
218 	inline uint8_t kb_get_index(uint8_t char_code);
219 	inline void draw_char(uint8_t x, uint8_t y, uint8_t char_pos);
220 	inline void draw_point(uint8_t x, uint8_t y, uint8_t color);
221 	inline void draw_udk();
222 
223 	DECLARE_DEVICE_IMAGE_LOAD_MEMBER( card_load );
224 
225 	/* general */
226 	uint8_t m_sleep;
227 	uint8_t m_warm_start;
228 	uint8_t m_t6834_ram[0x800];
229 	uint8_t m_regs_r[8];
230 	uint8_t m_regs_w[8];
231 	uint8_t m_alarm[8];
232 
233 	struct fifo_buffer
234 	{
235 		uint8_t data[0x100];
236 		uint8_t read;
237 		uint8_t write;
238 	};
239 	fifo_buffer m_in;
240 	fifo_buffer m_out;
241 
242 	uint8_t m_udk_on;
243 	uint8_t m_draw_udk;
244 	uint8_t m_sp_on;
245 	uint8_t m_font_code;
246 	emu_timer *m_rsta_clear;
247 	emu_timer *m_rstb_clear;
248 	emu_timer *m_beep_stop;
249 
250 	/* LCD */
251 	uint8_t m_lcd_on;
252 	uint8_t m_lcd_map[32][120];
253 	uint8_t m_scroll_min;
254 	uint8_t m_scroll_max;
255 	uint8_t m_blink;
256 
257 	struct lcd_position
258 	{
259 		uint8_t x;
260 		uint8_t y;
261 		uint8_t on;
262 	};
263 	lcd_position m_locate;
264 	lcd_position m_cursor;
265 
266 	/* keyboard */
267 	uint8_t m_kb_on;
268 	uint8_t m_repeat_key;         //not supported
269 	uint8_t m_kb_size;
270 
271 	/* cassette */
272 	uint8_t  m_cass_motor;
273 	uint8_t  m_cass_data;
274 	uint32_t m_cass_clk;
275 	uint8_t  m_bit_count;
276 	int    m_cass_state;
277 	emu_timer *m_cass_poll;
278 	emu_timer *m_cass_tick;
279 
280 	/* printer */
281 	uint8_t m_prn_sendbit;
282 	uint8_t m_prn_char_code;
283 	uint8_t m_prn_buffer[0x100];
284 	uint8_t m_prn_size;
285 	void x07_palette(palette_device &palette) const;
286 	TIMER_CALLBACK_MEMBER(cassette_tick);
287 	TIMER_CALLBACK_MEMBER(cassette_poll);
288 	TIMER_CALLBACK_MEMBER(rsta_clear);
289 	TIMER_CALLBACK_MEMBER(rstb_clear);
290 	TIMER_CALLBACK_MEMBER(beep_stop);
291 	TIMER_DEVICE_CALLBACK_MEMBER(blink_timer);
292 
293 	void x07_io(address_map &map);
294 	void x07_mem(address_map &map);
295 };
296 
297 #endif // MAME_INCLUDES_X07_H
298