summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/naomim1.cpp
blob: 68282faf8d934e282b81896ed660ef43f490c8c2 (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
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert

#include "emu.h"
#include "naomim1.h"

const device_type NAOMI_M1_BOARD = &device_creator<naomi_m1_board>;

DEVICE_ADDRESS_MAP_START(submap, 16, naomi_m1_board)
	AM_RANGE(0x0a, 0x0b) AM_READ(actel_id_r)

	AM_INHERIT_FROM(naomi_board::submap)
ADDRESS_MAP_END

naomi_m1_board::naomi_m1_board(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: naomi_board(mconfig, NAOMI_M1_BOARD, "Sega NAOMI M1 Board", tag, owner, clock, "naomi_m1_board", __FILE__),
		m_region(*this, DEVICE_SELF)
{
}

READ16_MEMBER(naomi_m1_board::actel_id_r)
{
	return actel_id;
}

void naomi_m1_board::device_start()
{
	naomi_board::device_start();

	std::string skey = parameter("key");
	if(!skey.empty())
		key = strtoll(skey.c_str(), nullptr, 16);
	else
	{
		logerror("%s: Warning: key not provided\n", tag());
		key = 0;
	}

	std::string sid = parameter("id");
	if (!sid.empty())
		actel_id = strtoll(sid.c_str(), nullptr, 16);
	else
	{
		logerror("%s: Warning: Actel ID not provided\n", tag());
		actel_id = 0;
	}

	buffer = std::make_unique<UINT8[]>(BUFFER_SIZE);

	save_pointer(NAME(buffer.get()), BUFFER_SIZE);
	save_item(NAME(dict));
	save_item(NAME(hist));
	save_item(NAME(rom_cur_address));
	save_item(NAME(buffer_actual_size));
	save_item(NAME(avail_bits));
	save_item(NAME(encryption));
	save_item(NAME(stream_ended));
	save_item(NAME(has_history));
}

void naomi_m1_board::device_reset()
{
	naomi_board::device_reset();
	encryption = false;
	rom_cur_address = 0;
	buffer_actual_size = 0;
	has_history = false;
	stream_ended = false;

	memset(dict, 0, sizeof(dict));
	memset(hist, 0, sizeof(hist));

	avail_val = 0;
	avail_bits = 0;
}

void naomi_m1_board::board_setup_address(UINT32 address, bool is_dma)
{
	rom_cur_address = address & 0x1fffffff;
	encryption = (!(address & 0x20000000)) && is_dma;

	if(encryption) {
		enc_reset();
		enc_fill();
	}
}

void naomi_m1_board::board_get_buffer(UINT8 *&base, UINT32 &limit)
{
	if(encryption) {
		base = buffer.get();
		limit = BUFFER_SIZE;

	} else {
		base = m_region->base() + rom_cur_address;
		limit = m_region->bytes() - rom_cur_address;
	}
}

void naomi_m1_board::board_advance(UINT32 size)
{
	if(encryption) {
		if(size < buffer_actual_size) {
			memmove(buffer.get(), buffer.get() + size, buffer_actual_size - size);
			buffer_actual_size -= size;
		} else {
			hist[0] = buffer[buffer_actual_size-2];
			hist[1] = buffer[buffer_actual_size-1];
			has_history = true;
			buffer_actual_size = 0;
		}
		enc_fill();

	} else
		rom_cur_address += size;
}

UINT32 naomi_m1_board::get_decrypted_32b()
{
	UINT8 *base = m_region->base() + rom_cur_address;
	UINT8 a = base[0];
	UINT8 b = base[1];
	UINT8 c = base[2];
	UINT8 d = base[3];

	rom_cur_address += 4;

	UINT32 res = key ^ (((b^d) << 24) | ((a^c) << 16) | (b << 8) | a);
	return res;
}

void naomi_m1_board::gb_reset()
{
	avail_val = 0;
	avail_bits = 0;
}

inline UINT32 naomi_m1_board::lookb(int bits)
{
	if(bits > avail_bits) {
		avail_val = (avail_val << 32) | get_decrypted_32b();
		avail_bits += 32;
	}
	return (avail_val >> (avail_bits - bits)) & ((1 << bits)-1);
}

inline void naomi_m1_board::skipb(int bits)
{
	avail_bits -= bits;
}

inline UINT32 naomi_m1_board::getb(int bits)
{
	UINT32 res = lookb(bits);
	skipb(bits);
	return res;
}

void naomi_m1_board::enc_reset()
{
	gb_reset();
	stream_ended = false;
	has_history = false;
	buffer_actual_size = 0;

	for(auto & elem : dict)
		elem = getb(8);
}

void naomi_m1_board::wb(UINT8 byte)
{
	if(dict[0] & 64)
		if(buffer_actual_size < 2)
			if(has_history)
				buffer[buffer_actual_size] = hist[buffer_actual_size] - byte;
			else
				buffer[buffer_actual_size] = byte;
		else
			buffer[buffer_actual_size] = buffer[buffer_actual_size-2] - byte;
	else
		buffer[buffer_actual_size] = byte;

	buffer_actual_size++;
}

void naomi_m1_board::enc_fill()
{
	while(buffer_actual_size < BUFFER_SIZE && !stream_ended) {
		switch(lookb(3)) {
			// 00+2 - 0000+esc
		case 0: case 1: {
			skipb(2);
			int addr = getb(2);
			if(addr)
				wb(dict[addr]);
			else
				wb(getb(8));
			break;
		}

			// 010+2
		case 2:
			skipb(3);
			wb(dict[getb(2)+4]);
			break;

			// 011+3
		case 3:
			skipb(3);
			wb(dict[getb(3)+8]);
			break;

			// 10+5
		case 4: case 5:
			skipb(2);
			wb(dict[getb(5)+16]);
			break;

			// 11+6
		case 6: case 7: {
			skipb(2);
			int addr = getb(6)+48;
			if(addr == 111)
				stream_ended = true;
			else
				wb(dict[addr]);
			break;
		}
		}
	}

	while(buffer_actual_size < BUFFER_SIZE)
		buffer[buffer_actual_size++] = 0;
}