summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/msx/cart/video80.cpp
blob: 2bdfff855dab782eb5fc0d4976701c5cf55b1511 (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
// license:BSD-3-Clause
// copyright-holders:F. Ulivi

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

    VIDEO80 homebrew 80-column video card

    This is a 80-column video card that I designed and built in 1988.
    Here's a summary of its features:
    - 80 columns by 24 rows of text video
    - 8x8 character cell
    - 16 MHz dot clock, 50 Hz frame rate
    - Based on MC6845 CRT controller
    - 4k of SRAM: half for the framebuffer & half for the character
      generator.
    - 4k of EPROM: it contains the firmware and the compressed font.
      FW supports seamless operation in both BASIC and MSXDOS
      environments. No driver or additional sw is needed.

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

#include "emu.h"
#include "video80.h"
#include "cpu/z80/z80.h"
#include "video/mc6845.h"

#include "emupal.h"
#include "screen.h"

// Debugging
#define VERBOSE 0
#include "logmacro.h"

namespace {

// Dot clock
constexpr auto DOT_CLOCK = 16_MHz_XTAL;

class msx_cart_video80_device : public device_t, public msx_cart_interface
{
public:
	msx_cart_video80_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
		: device_t(mconfig, MSX_CART_VIDEO80, tag, owner, clock)
		, msx_cart_interface(mconfig, *this)
		, m_palette(*this, "palette")
		, m_crtc(*this, "crtc")
		, m_screen(*this, "screen1")
		, m_cpu_waiting(false)
	{
	}

	virtual std::error_condition initialize_cartridge(std::string &message) override;

protected:
	virtual void device_start() override ATTR_COLD;
	virtual void device_add_mconfig(machine_config &config) override ATTR_COLD;

	void de_w(int state);
	uint8_t vram_r(offs_t addr);
	void vram_w(offs_t addr, uint8_t data);
	uint8_t crtc_r(offs_t addr);
	void crtc_w(offs_t addr, uint8_t data);

private:
	static inline constexpr unsigned VRAM_SIZE = 4096;

	required_device<palette_device> m_palette;
	required_device<mc6845_device> m_crtc;
	required_device<screen_device> m_screen;

	uint8_t m_vram[VRAM_SIZE];
	bool m_cpu_waiting;

	MC6845_UPDATE_ROW(crtc_update_row);
};

std::error_condition msx_cart_video80_device::initialize_cartridge(std::string &message)
{
	if (!cart_rom_region())
	{
		message = "msx_cart_video80_device: Required region 'rom' was not found.";
		return image_error::INTERNAL;
	}

	if (cart_rom_region()->bytes() != 0x1000)
	{
		message = "msx_cart_video80_device: Region 'rom' has invalid size.";
		return image_error::INVALIDLENGTH;
	}

	page(1)->install_rom(0x4000, 0x4fff, cart_rom_region()->base());
	page(1)->install_read_handler(0x5000, 0x5fff, emu::rw_delegate(*this, FUNC(msx_cart_video80_device::vram_r)));
	page(1)->install_write_handler(0x5000, 0x5fff, emu::rw_delegate(*this, FUNC(msx_cart_video80_device::vram_w)));
	page(1)->install_read_handler(0x6000, 0x6fff, emu::rw_delegate(*this, FUNC(msx_cart_video80_device::crtc_r)));
	page(1)->install_write_handler(0x6000, 0x6fff, emu::rw_delegate(*this, FUNC(msx_cart_video80_device::crtc_w)));

	return std::error_condition();
}

void msx_cart_video80_device::device_start()
{
	save_item(NAME(m_vram));
	save_item(NAME(m_cpu_waiting));
}

void msx_cart_video80_device::device_add_mconfig(machine_config &config)
{
	PALETTE(config, m_palette, palette_device::MONOCHROME);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER, rgb_t::green());
	m_screen->set_raw(DOT_CLOCK, 1024, 0, 640, 312, 0, 192);
	m_screen->set_screen_update(m_crtc, FUNC(mc6845_device::screen_update));

	MC6845(config, m_crtc, DOT_CLOCK / 8);
	m_crtc->set_char_width(8);
	m_crtc->set_show_border_area(false);
	m_crtc->set_screen(m_screen);
	m_crtc->set_update_row_callback(FUNC(msx_cart_video80_device::crtc_update_row));
	m_crtc->out_de_callback().set(FUNC(msx_cart_video80_device::de_w));
}

void msx_cart_video80_device::de_w(int state)
{
	if (m_cpu_waiting && !state)
	{
		maincpu().set_input_line(Z80_INPUT_LINE_WAIT, CLEAR_LINE);
		m_cpu_waiting = false;
	}
}

uint8_t msx_cart_video80_device::vram_r(offs_t addr)
{
	if (!machine().side_effects_disabled() && m_crtc->de_r())
	{
		maincpu().set_input_line(Z80_INPUT_LINE_WAIT, ASSERT_LINE);
		maincpu().defer_access();
		m_cpu_waiting = true;
		return 0;
	}
	else
	{
		return m_vram[addr];
	}
}

void msx_cart_video80_device::vram_w(offs_t addr, uint8_t data)
{
	if (!machine().side_effects_disabled() && m_crtc->de_r())
	{
		maincpu().set_input_line(Z80_INPUT_LINE_WAIT, ASSERT_LINE);
		maincpu().defer_access();
		m_cpu_waiting = true;
	}
	else
	{
		m_vram[addr] = data;
	}
}

uint8_t msx_cart_video80_device::crtc_r(offs_t addr)
{
	uint8_t res = 0;

	if (BIT(addr, 1))
	{
		if (BIT(addr, 0))
			res = m_crtc->register_r();
		else
			LOG("Reading from non-existing reg!\n");
	}
	else
	{
		LOG("Reading from a write-only address!\n");
	}

	return res;
}

void msx_cart_video80_device::crtc_w(offs_t addr, uint8_t data)
{
	if (BIT(addr, 1))
	{
		LOG("Writing to a read-only address!\n");
	}
	else
	{
		if (BIT(addr, 0))
			m_crtc->register_w(data);
		else
			m_crtc->address_w(data);
	}
}

MC6845_UPDATE_ROW(msx_cart_video80_device::crtc_update_row)
{
	pen_t const *const pen = m_palette->pens();
	for (int i = 0; i < x_count; i++)
	{
		uint8_t const char_code = m_vram[(ma + i) & 0x7ff];
		uint8_t pixels = m_vram[(unsigned(char_code) << 3) | 0x800 | (ra & 7)];
		bool const cursor = cursor_x == i;
		if (cursor)
			pixels = ~pixels;

		for (unsigned col = 0; col < 8; col++)
			bitmap.pix(y, i * 8 + col) = pen[BIT(pixels, 7 - col)];
	}
}

} // anonymous namespace

DEFINE_DEVICE_TYPE_PRIVATE(MSX_CART_VIDEO80, msx_cart_interface, msx_cart_video80_device, "msx_cart_video80", "MSX Cartridge - VIDEO80")