summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/nabupc/adapter.cpp
blob: 0c6ecef575f3dfe5dd86c50d96df6712b0a4aac5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// license:BSD-3-Clause
// copyright-holders:Brian Johnson
/*******************************************************************
 *
 * NABU PC - Network Adapter Settop Box
 *
 *******************************************************************/

#include "emu.h"
#include "adapter.h"

#include "emuopts.h"
#include "unzip.h"

#include <tuple>

#define VERBOSE 0
#include "logmacro.h"


//**************************************************************************
//  NABU PC NETWORK ADAPTER DEVICE
//**************************************************************************

DEFINE_DEVICE_TYPE(NABUPC_NETWORK_ADAPTER, bus::nabupc::network_adapter, "nabu_net_adapter", "NABU Network Adapter")

namespace bus::nabupc {

//**************************************************************************
//  SEGMENT FILE LOADING
//**************************************************************************

// Load segment file from disk
std::error_condition network_adapter::segment_file::read_archive(util::core_file &stream, uint32_t segment_id)
{
	segment_id &= 0xffffff;

	util::core_file::ptr proxy;
	std::error_condition err = util::core_file::open_proxy(stream, proxy);
	if (err)
		return err;

	util::archive_file::ptr zipfile;
	err = util::archive_file::open_zip(std::move(proxy), zipfile);
	if (err)
		return err;

	std::string segment_filename = util::string_format("%06d.nabu", segment_id);

	if (zipfile->search(segment_filename, false) < 0)
		return std::errc::no_such_file_or_directory;

	// determine the uncompressed length
	uint64_t uncompressed_length_uint64 = zipfile->current_uncompressed_length();
	size_t uncompressed_length = (size_t)uncompressed_length_uint64;
	if (uncompressed_length != uncompressed_length_uint64)
		return std::errc::not_enough_memory;

	// prepare a buffer for the segment file
	std::unique_ptr<char[]> segment_buffer(new (std::nothrow) char[uncompressed_length]);
	if (!segment_buffer)
		return std::errc::not_enough_memory;

	err = zipfile->decompress(&segment_buffer[0], uncompressed_length);
	if (err)
		return err;

	m_segment_id = segment_id;
	return parse_segment(&segment_buffer[0], uncompressed_length);
}

std::error_condition network_adapter::segment_file::parse_segment(char *data, size_t length)
{
	util::core_file::ptr fd;
	pak current;
	uint64_t offset = 0;
	size_t actual;
	uint16_t crc;
	uint8_t npak = 0;
	std::error_condition err;

	pak_list.clear();
	err = util::core_file::open_ram(data, length, OPEN_FLAG_READ, fd);
	if (err)
		return err;

	memset(current.data, 0, 991);
	current.segment_id[0] = (m_segment_id & 0xff0000) >> 16;
	current.segment_id[1] = (m_segment_id & 0x00ff00) >> 8;
	current.segment_id[2] = (m_segment_id & 0x0000ff);
	current.owner         = 0x01;
	current.tier[0]       = 0x7f;
	current.tier[1]       = 0xff;
	current.tier[2]       = 0xff;
	current.tier[3]       = 0xff;
	current.mbytes[0]     = 0x7f;
	current.mbytes[1]     = 0x80;
	std::tie(err, actual) = read_at(*fd, offset, current.data, 991);
	do {
		crc = 0xffff;
		if (err) {
			return err;
		}
		if (actual > 0) {
			current.packet_number = npak;
			current.pak_number[0] = npak;
			current.pak_number[1] = 0;
			current.type = 0x20;
			if (offset == 0)
				current.type |= 0x81;
			if (actual < 991)
				current.type |= 0x10;
			current.offset[0] = ((offset) >> 8) & 0xff;
			current.offset[1] = offset & 0xff;
			for (int i = 0; i < 1007; ++i) {
				crc = update_crc(crc, ((char *)&current)[i]);
			}
			crc ^= 0xffff;
			current.crc[0] = (crc >> 8) & 0xff;
			current.crc[1] = crc & 0xff;
			pak_list.push_back(current);
			offset = (++npak * 991);
			memset(current.data, 0, 991);
			std::tie(err, actual) = read_at(*fd, offset, current.data, 991);
		}
	} while(actual > 0);

	return err;
}

const network_adapter::segment_file::pak& network_adapter::segment_file::operator[](const int index) const
{
	assert(index >= 0 && index < size());

	return pak_list[index];
}

// crc16 calculation
uint16_t network_adapter::segment_file::update_crc(uint16_t crc, uint8_t data)
{
	uint8_t bc;

	bc = (crc >> 8) ^ data;

	crc <<= 8;
	crc ^= crc_table[bc];

	return crc;
}

//**************************************************************************
//  INPUT PORTS
//**************************************************************************

// CONFIG
static INPUT_PORTS_START( nabu_network_adapter )
	PORT_START("CONFIG")
	PORT_CONFNAME(0x01, 0x00, "Prompt for channel?")
	PORT_CONFSETTING(0x01, "Yes")
	PORT_CONFSETTING(0x00, "No")
INPUT_PORTS_END

//**************************************************************************
//  DEVICE INITIALIZATION
//**************************************************************************

network_adapter::network_adapter(machine_config const &mconfig, char const *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, NABUPC_NETWORK_ADAPTER, tag, owner, clock)
	, device_buffered_serial_interface(mconfig, *this)
	, device_rs232_port_interface(mconfig, *this)
	, device_image_interface(mconfig, *this)
	, m_config(*this, "CONFIG")
	, m_channel(0)
	, m_packet(0)
	, m_segment(0)
	, m_state(state::START)
	, m_substate(0)
	, m_segment_timer(nullptr)
{
}

void network_adapter::device_start()
{
	m_segment_timer = timer_alloc(FUNC(network_adapter::segment_tick), this);

	machine().save().register_postload(save_prepost_delegate(FUNC(network_adapter::postload), this));

	save_item(NAME(m_state));
	save_item(NAME(m_substate));
	save_item(NAME(m_channel));
	save_item(NAME(m_packet));
	save_item(NAME(m_segment));
}

void network_adapter::device_reset()
{
	m_state = state::START;
	m_substate = 0;
	// initialise state
	clear_fifo();

	// configure device_buffered_serial_interface
	set_data_frame(START_BIT_COUNT, DATA_BIT_COUNT, PARITY, STOP_BITS);
	set_rate(BAUD);
	receive_register_reset();
	transmit_register_reset();
}

void network_adapter::postload()
{
	if (is_loaded()) {
		m_cache.read_archive(image_core_file(), m_segment);
	}
}

std::pair<std::error_condition, std::string> network_adapter::call_load()
{
	if (is_filetype("pak")) {
		return std::make_pair(std::error_condition(), std::string());
	}
	return std::make_pair(image_error::INVALIDIMAGE, std::string());
}

//**************************************************************************
//  DEVICE CONFIGURATION
//**************************************************************************

ioport_constructor network_adapter::device_input_ports() const
{
	return INPUT_PORTS_NAME( nabu_network_adapter );
}

//**************************************************************************
//  SERIAL PROTOCOL
//**************************************************************************

void network_adapter::input_txd(int state)
{
	device_buffered_serial_interface::rx_w(state);
}

void network_adapter::tra_callback()
{
	output_rxd(transmit_register_get_data_bit());
}

void network_adapter::received_byte(uint8_t byte)
{
	LOG("Received Byte 0x%02X\n", byte);
	switch (m_state) {
	case state::START:
		connect(byte, bool(m_config->read() & 1));
		break;
	case state::IDLE:
		idle(byte);
		break;
	case state::CHANNEL_REQUEST:
		channel_request(byte);
		break;
	case state::SEGMENT_REQUEST:
		segment_request(byte);
		break;
	case state::HEX81_REQUEST:
		hex81_request(byte);
		break;
	case state::SEND_SEGMENT:
		send_segment(byte);
		break;
	}
}

//**************************************************************************
//  STATE MACHINE
//**************************************************************************

void network_adapter::connect(uint8_t byte, bool channel_request = true)
{
	if (byte == 0x83 && m_substate == 0) {
		transmit_byte(0x10);
		transmit_byte(0x06);
		transmit_byte(0xe4);
	} else if (byte == 0x82 && m_substate == 1) {
		transmit_byte(0x10);
		transmit_byte(0x06);
	} else if (byte == 0x01 && m_substate == 2) {
		transmit_byte(channel_request ? 0x9f : 0x1f);
		transmit_byte(0x10);
		transmit_byte(0xe1);
		m_state = state::IDLE;
	} else {
		LOG("Unexpected byte: 0x%02X (%d), restarting Adapter.\n", byte, m_substate);
		m_state = state::START;
		m_substate = 0;
		return;
	}
	++m_substate;
}

void network_adapter::idle(uint8_t byte)
{
	m_substate = 0;
	switch (byte) {
	case 0x85:
		transmit_byte(0x10);
		transmit_byte(0x06);
		m_state = state::CHANNEL_REQUEST;
		break;
	case 0x84:
		transmit_byte(0x10);
		transmit_byte(0x06);
		m_state = state::SEGMENT_REQUEST;
		break;
	case 0x83:
		transmit_byte(0x10);
		transmit_byte(0x06);
		break;
	case 0x82:
		transmit_byte(0x10);
		transmit_byte(0x06);
		break;
	case 0x81:
		transmit_byte(0x10);
		transmit_byte(0x06);
		m_state = state::HEX81_REQUEST;
		break;
	case 0x01:
		transmit_byte(0x10);
		transmit_byte(0x06);
		break;
	}

}

void network_adapter::hex81_request(uint8_t byte)
{
	if (m_substate == 1) {
		transmit_byte(0xe4);
		m_state = state::IDLE;
	}
	++m_substate;

}

void network_adapter::channel_request(uint8_t byte)
{
	if (m_substate == 0) {
		m_channel = (m_channel & 0xff00) | (byte);
	} else if (m_substate == 1) {
		m_channel = (m_channel & 0xff) | (byte << 8);
		transmit_byte(0xe4);
		LOG("Channel: 0x%04X\n", m_channel);
		m_state = state::IDLE;
	}
	++m_substate;
}

void network_adapter::segment_request(uint8_t byte)
{
	if (m_substate == 0) {
		m_packet = byte;
	} else if (m_substate == 1) {
		m_segment = (m_segment & 0xffff00) | (byte);
	} else if (m_substate == 2) {
		m_segment = (m_segment & 0xff00ff) | (byte << 8);
	} else if (m_substate == 3) {
		m_segment = (m_segment & 0xffff) | (byte << 16);
		transmit_byte(0xe4);
		transmit_byte(0x91);
		m_state = state::SEND_SEGMENT;
		m_substate = 0;
		LOG("Segment: 0x%06X, Packet: 0x%02X\n", m_segment, m_packet);
		return;
	}
	++m_substate;
}

void network_adapter::send_segment(uint8_t byte)
{
	if (m_substate == 0) {
		if (byte != 0x10) {
			LOG("Expecting byte 0x10 got %02X, restarting.\n", byte);
			m_state = state::START;
			m_substate = 0;
			return;
		}
	} else if (m_substate == 1) {
		if (byte != 0x06) {
			LOG("Expecting byte 0x06 got %02X, restarting.\n", byte);
			m_state = state::START;
			m_substate = 0;
			return;
		}
		m_pak_offset = 0;
		if (is_loaded() && !m_cache.read_archive(image_core_file(), m_segment)) {
			m_segment_timer->adjust(attotime::zero, 0, attotime::from_hz(1'000));
			LOG("Segment sending, returning to idle state\n");
		} else {
			LOG("Failed to find segment: %06d, restarting\n", m_segment);
			transmit_byte(0x10);
			transmit_byte(0x06);
			transmit_byte(0xe4);
		}
		m_state = state::IDLE;
	}

	++m_substate;
}

TIMER_CALLBACK_MEMBER(network_adapter::segment_tick)
{
	char * data = (char*)&m_cache[m_packet];
	if (data[m_pak_offset] == 0x10) {
		transmit_byte(data[m_pak_offset]);
	}
	transmit_byte(data[m_pak_offset++]);
	if (m_pak_offset >= sizeof(segment_file::pak)) {
		transmit_byte(0x10);
		transmit_byte(0xe1);
		m_segment_timer->reset();
	}
}

} // bus::nabupc