summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/cpu/z80/t6a84.cpp
blob: a3d2e5d1fb01b98ec376c26083fafd9e69dd362d (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
// license:BSD-3-Clause
// copyright-holders:QUFB
/***************************************************************************

    Toshiba T6A84, TLCS-Z80 ASSP Family

***************************************************************************/

#include "emu.h"
#include "t6a84.h"

#include "z80.inc"

#define LOG_INT    (1U << 1) // z80.lst
#define LOG_UNDOC  (1U << 2)
#define LOG_PAGE_R (1U << 3)
#define LOG_PAGE_W (1U << 4)
#define LOG_MEM    (1U << 5)

//#define VERBOSE (LOG_PAGE_R | LOG_PAGE_W | LOG_MEM)
#include "logmacro.h"


DEFINE_DEVICE_TYPE(T6A84, t6a84_device, "t6a84", "Toshiba T6A84")

void t6a84_device::internal_io_map(address_map &map) const
{
	map.global_mask(0xff);
	map(0xfc, 0xfc).rw(FUNC(t6a84_device::stack_page_r), FUNC(t6a84_device::stack_page_w));
	map(0xfd, 0xfd).rw(FUNC(t6a84_device::data_page_r), FUNC(t6a84_device::data_page_w));
	map(0xfe, 0xfe).rw(FUNC(t6a84_device::code_page_r), FUNC(t6a84_device::code_page_w));
	map(0xff, 0xff).rw(FUNC(t6a84_device::vector_page_r), FUNC(t6a84_device::vector_page_w));
}

t6a84_device::t6a84_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	t6a84_device(mconfig, T6A84, tag, owner, clock, address_map_constructor(FUNC(t6a84_device::internal_io_map), this))
{ }

t6a84_device::t6a84_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, address_map_constructor io_map)
	: z80_device(mconfig, type, tag, owner, clock)
	, m_program_space_config("program", ENDIANNESS_LITTLE, 8, 20, 0, 16, 0)
	, m_data_space_config("data", ENDIANNESS_LITTLE, 8, 20, 0, 16, 0)
	, m_stack_space_config("stack", ENDIANNESS_LITTLE, 8, 20, 0, 16, 0)
	, m_io_space_config("io", ENDIANNESS_LITTLE, 8, 16, 0, io_map)
	, m_code_page(0)
	, m_delay_code_page(0)
	, m_is_delay_code_page_set(false)
	, m_prev_code_page(0)
	, m_data_page(8)
	, m_stack_page(8)
	, m_vector_page(0)
{
}

// Interrupt vectors need to be fetched and executed from their corresponding page.
// For simplicity, we switch pages via callbacks, instead of using a dedicated address space.
// TODO: Find a better way to solve this, at least these hacks are isolated to t6a84.cpp for now.
void t6a84_device::paged_irqfetch()
{
	LOGMASKED(LOG_PAGE_W, "IRQ FETCH %02x => %02x\n", m_code_page, m_vector_page);
	m_prev_code_page = m_code_page;
	m_code_page = m_vector_page;
}

void t6a84_device::paged_reti()
{
	LOGMASKED(LOG_PAGE_W, "IRQ RET %02x => %02x\n", m_code_page, m_prev_code_page);
	m_code_page = m_prev_code_page;
}

void t6a84_device::paged_jump()
{
	/*
	    When setting a code page, it only becomes effective after jumping to a far address in that page.
	    Any instructions fetched and executed before that jump still use the previous code page.
	    This can be seen in Sega Ferie Kitten, when test program at page 7 gets mapped, as we are still
	    executing on page 0, but we expect to start executing that program when jumping to RST0:

	    ROM_00::1ea9 3e 07      LD   A,0x7
	    ROM_00::1eab d3 fe      OUT  (DAT_io_00fe),A
	    ROM_00::1ead c3 00 00   JP   RST0
	*/
	if (!machine().side_effects_disabled() && m_is_delay_code_page_set) {
		LOGMASKED(LOG_PAGE_W, "BRANCH %02x => %02x\n", m_code_page, m_prev_code_page);
		m_code_page = m_delay_code_page;
		m_is_delay_code_page_set = false;
	}
}

void t6a84_device::execute_run()
{
	#include "cpu/z80/t6a84.hxx"
}

void t6a84_device::device_start()
{
	z80_device::device_start();

	space(AS_PROGRAM).cache(m_args);
	space(AS_DATA).specific(m_data);
	space(AS_STACK).specific(m_stack);

	save_item(NAME(m_code_page));
	save_item(NAME(m_delay_code_page));
	save_item(NAME(m_is_delay_code_page_set));
	save_item(NAME(m_prev_code_page));
	save_item(NAME(m_data_page));
	save_item(NAME(m_stack_page));
	save_item(NAME(m_vector_page));
}

void t6a84_device::device_reset()
{
	m_code_page = 0;
	m_delay_code_page = 0;
	m_is_delay_code_page_set = false;
	m_prev_code_page = 0;
	m_data_page = 8;
	m_stack_page = 8;
	m_vector_page = 0;

	z80_device::device_reset();
}

device_memory_interface::space_config_vector t6a84_device::memory_space_config() const
{
	auto r = z80_device::memory_space_config();
	r.emplace_back(AS_DATA, &m_data_space_config);
	r.emplace_back(AS_STACK, &m_stack_space_config);
	for (auto it = r.begin(); it != r.end(); ++it) {
		if ((*it).first == AS_IO) {
			(*it).second = &m_io_space_config;
		} else if ((*it).first == AS_PROGRAM) {
			(*it).second = &m_program_space_config;
		}
	}

	return r;
}

bool t6a84_device::memory_translate(int spacenum, int intention, offs_t &address, address_space *&target_space)
{
	if (spacenum == AS_PROGRAM) {
		address = code_address(address);
	} else if (spacenum == AS_DATA) {
		address = data_address(address);
	} else if (spacenum == AS_STACK) {
		address = stack_address(address);
	}

	target_space = &space(spacenum);

	return true;
}

uint32_t t6a84_device::code_address(uint16_t address)
{
	const uint32_t page_address = m_code_page << 16 | address;
	LOGMASKED(LOG_MEM, "CODE @ %06x => %06x\n", address, page_address);

	return page_address;
}

uint32_t t6a84_device::data_address(uint16_t address)
{
	const uint32_t page_address = m_data_page << 16 | address;
	LOGMASKED(LOG_MEM, "DATA @ %06x => %06x\n", address, page_address);

	return page_address;
}

uint32_t t6a84_device::stack_address(uint16_t address)
{
	const uint32_t page_address = m_stack_page << 16 | address;
	LOGMASKED(LOG_MEM, "STACK @ %06x => %06x\n", address, page_address);

	return page_address;
}

uint8_t t6a84_device::stack_read(uint16_t addr)
{
	return m_stack.read_byte(addr);
}

void t6a84_device::stack_write(uint16_t addr, uint8_t value)
{
	m_stack.write_byte(addr, value);
}

uint8_t t6a84_device::data_page_r()
{
	LOGMASKED(LOG_PAGE_R, "data_page_r: %02x @ %06x\n", m_data_page, pc());
	return m_data_page;
}

uint8_t t6a84_device::stack_page_r()
{
	LOGMASKED(LOG_PAGE_R, "stack_page_r: %02x @ %06x\n", m_stack_page, pc());
	return m_stack_page;
}

uint8_t t6a84_device::code_page_r()
{
	LOGMASKED(LOG_PAGE_R, "code_page_r: %02x @ %06x\n", m_code_page, pc());
	return m_code_page;
}

uint8_t t6a84_device::vector_page_r()
{
	LOGMASKED(LOG_PAGE_R, "vector_page_r: %02x @ %06x\n", m_vector_page, pc());
	return m_vector_page;
}

void t6a84_device::data_page_w(uint8_t page)
{
	LOGMASKED(LOG_PAGE_W, "data_page_w: %02x @ %06x\n", page, pc());
	m_data_page = page;
}

void t6a84_device::stack_page_w(uint8_t page)
{
	LOGMASKED(LOG_PAGE_W, "stack_page_w: %02x @ %06x\n", page, pc());
	m_stack_page = page;
}

void t6a84_device::code_page_w(uint8_t page)
{
	LOGMASKED(LOG_PAGE_W, "code_page_w: %02x @ %06x\n", page, pc());
	m_delay_code_page = page;
	m_is_delay_code_page_set = true;
}

void t6a84_device::vector_page_w(uint8_t page)
{
	LOGMASKED(LOG_PAGE_W, "vector_page_w: %02x @ %06x\n", page, pc());
	m_vector_page = page;
}