summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/spectrum/speccydos.cpp
blob: 1afbb52da2610408e018a6f369b4b3b1d4255847 (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
// license:BSD-3-Clause
// copyright-holders:MetalliC
/*********************************************************************

    Speccy-DOS Interface
    (c) 1987? Philips(?)

    WD1770/2-based floppy drive interface with 1K RAM and 8K ROM,
    presumable developed by Philips & MBLE Belgium, had some popularity in Hungary.

    Main commands:
     LIST * - list disc contents
     LOAD *"filename" - load and run program
     SAVE *"filename" - save program
     FORMAT *"diskname"dd80 - format double side double density 80 tracks disk
     ASN *drive# - change drive

    Manual: https://sinclair.hu/speccyalista/konyvtar/kezikonyvek/SpeccyDOSv4_manual.pdf

    Partially compatible with Spectrum128, useable after "USR 0" from basic128 or switch to 48K mode.

    Notes / TODOs:
     - address decoding performed by large DIP28 chip, probably PROM, not dumped, exact logic is not known.
     - program ROMs have swapped data lines/bits 1 and 5.
     - original software disc mentioned in manual is missing.
     - Magic/NMI function require NMI routine preloaded in RAM by "MAGIC2.x" program (which is missing).

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

#include "emu.h"
#include "speccydos.h"


/***************************************************************************
    DEVICE DEFINITIONS
***************************************************************************/

DEFINE_DEVICE_TYPE(SPECTRUM_SPECCYDOS, spectrum_speccydos_device, "spectrum_speccydos", "Speccy-DOS Interface")


//-------------------------------------------------
//  INPUT_PORTS
//-------------------------------------------------

INPUT_PORTS_START(speccydos)
	PORT_START("BUTTON")
	PORT_BIT(0x01, IP_ACTIVE_HIGH, IPT_BUTTON1) PORT_NAME("Magic Button") PORT_CODE(KEYCODE_MINUS_PAD) PORT_CHANGED_MEMBER(DEVICE_SELF, spectrum_speccydos_device, magic_button, 0)
INPUT_PORTS_END

//-------------------------------------------------
//  input_ports - device-specific input ports
//-------------------------------------------------

ioport_constructor spectrum_speccydos_device::device_input_ports() const
{
	return INPUT_PORTS_NAME(speccydos);
}

//-------------------------------------------------
//  SLOT_INTERFACE( floppies )
//-------------------------------------------------

static void speccydos_floppies(device_slot_interface &device)
{
	device.option_add("525sssd_35t", FLOPPY_525_SSSD_35T);
	device.option_add("525ssdd", FLOPPY_525_SSDD);
	device.option_add("525dsdd", FLOPPY_525_DD);
	device.option_add("525ssqd", FLOPPY_525_SSQD);
	device.option_add("525dsqd", FLOPPY_525_QD);
	device.option_add("35ssdd", FLOPPY_35_SSDD);
	device.option_add("35dsdd", FLOPPY_35_DD);
}

//-------------------------------------------------
//  floppy_formats
//-------------------------------------------------

void spectrum_speccydos_device::floppy_formats(format_registration &fr)
{
	fr.add_mfm_containers();
	fr.add(FLOPPY_SDD_FORMAT);
}

//-------------------------------------------------
//  ROM( speccydos )
//-------------------------------------------------

ROM_START(speccydos)
	ROM_REGION(0x2000, "rom", 0)
	ROM_DEFAULT_BIOS("sdos42")

	ROM_SYSTEM_BIOS(0, "sdos41", "Speccy DOS v4.1")
	ROMX_LOAD("sdos41.bin", 0x0000, 0x2000, CRC(fcfa6dac) SHA1(bcae27b334bbbee257900682859132f476c3be99), ROM_BIOS(0))
	ROM_SYSTEM_BIOS(1, "sdos42", "Speccy DOS v4.2 (WD1770)")
	ROMX_LOAD("sdos42.bin", 0x0000, 0x2000, CRC(208e7baa) SHA1(80e82c1dd77ff04b86989f7d2a25d93c5aa1dc42), ROM_BIOS(1))
	ROM_SYSTEM_BIOS(2, "sdos422", "Speccy DOS v4.2 (WD1772)") // only 4 byte different from above - step rates for 1772
	ROMX_LOAD("sdos422.bin", 0x0000, 0x2000, CRC(a7317e1d) SHA1(e72d2ea19cbe8f3459e4fc9d3c57975dc9917bd6), ROM_BIOS(2))
ROM_END

//-------------------------------------------------
//  device_add_mconfig - add device configuration
//-------------------------------------------------

void spectrum_speccydos_device::device_add_mconfig(machine_config &config)
{
	WD1770(config, m_fdc, 8_MHz_XTAL);

	FLOPPY_CONNECTOR(config, "fdc:0", speccydos_floppies, "525dsqd", spectrum_speccydos_device::floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:1", speccydos_floppies, "525dsqd", spectrum_speccydos_device::floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:2", speccydos_floppies, nullptr, spectrum_speccydos_device::floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:3", speccydos_floppies, nullptr, spectrum_speccydos_device::floppy_formats).enable_sound(true);

	// passthru
	SPECTRUM_EXPANSION_SLOT(config, m_exp, spectrum_expansion_devices, nullptr);
	m_exp->irq_handler().set(DEVICE_SELF_OWNER, FUNC(spectrum_expansion_slot_device::irq_w));
	m_exp->nmi_handler().set(DEVICE_SELF_OWNER, FUNC(spectrum_expansion_slot_device::nmi_w));
	m_exp->fb_r_handler().set(DEVICE_SELF_OWNER, FUNC(spectrum_expansion_slot_device::fb_r));
}

const tiny_rom_entry *spectrum_speccydos_device::device_rom_region() const
{
	return ROM_NAME(speccydos);
}


//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  spectrum_speccydos_device - constructor
//-------------------------------------------------

spectrum_speccydos_device::spectrum_speccydos_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, SPECTRUM_SPECCYDOS, tag, owner, clock)
	, device_spectrum_expansion_interface(mconfig, *this)
	, m_rom(*this, "rom")
	, m_fdc(*this, "fdc")
	, m_floppy(*this, "fdc:%u", 0)
	, m_exp(*this, "exp")
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void spectrum_speccydos_device::device_start()
{
	memset(m_ram, 0, sizeof(m_ram));

	save_item(NAME(m_romcs));
	save_item(NAME(m_control));
	save_item(NAME(m_ram));
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void spectrum_speccydos_device::device_reset()
{
	m_romcs = 0;
	m_control = 0;
}

//**************************************************************************
//  IMPLEMENTATION
//**************************************************************************

READ_LINE_MEMBER(spectrum_speccydos_device::romcs)
{
	return m_romcs | m_exp->romcs();
}

void spectrum_speccydos_device::pre_opcode_fetch(offs_t offset)
{
	m_exp->pre_opcode_fetch(offset);

	if (!machine().side_effects_disabled())
	{
		switch (offset)
		{
		case 0x0066:
		case 0x1292:
		case 0x1b41:
			if (!BIT(m_control, 6))
				m_romcs = 1;
			break;
		}
	}
}

uint8_t spectrum_speccydos_device::mreq_r(offs_t offset)
{
	u8 data = 0xff;

	if (m_romcs)
	{
		if (offset < 0x2000)
		{
			data = m_rom->base()[offset];
			data = bitswap<8>(data, 7, 6, 1, 4, 3, 2, 5, 0);
		}
		else if (offset < 0x2400)
			data = m_ram[offset & 0x3ff];
		else if (offset >= 0x3290 && offset < 0x3294)
			data = m_fdc->read(offset & 3);
		else
			logerror("SpeccyDOS unhandled read %04X\n", offset);
	}

	if (m_exp->romcs())
		data &= m_exp->mreq_r(offset);

	return data;
}

void spectrum_speccydos_device::mreq_w(offs_t offset, uint8_t data)
{
	if (offset == 0x3b41)
	{
		m_control = data;
		floppy_image_device* floppy = m_floppy[data & 3]->get_device();
		m_fdc->set_floppy(floppy);

		if (floppy)
			floppy->ss_w(BIT(data, 2));
		m_fdc->dden_w(BIT(data, 7));

		m_romcs = BIT(data, 6) ? 0 : BIT(data, 3);
	}
	else
		if (m_romcs)
		{
			if (offset >= 0x2000 && offset < 0x2400)
				m_ram[offset & 0x3ff] = data;
			else if (offset >= 0x3290 && offset < 0x3294)
				m_fdc->write(offset & 3, data);
			else
			{
				logerror("SpeccyDOS unhandled write %04X %02X\n", offset, data);
				//machine().debug_break();
			}
		}

	if (m_exp->romcs())
		m_exp->mreq_w(offset, data);
}
} void updateTerrainMesh() { m_terrain.m_vertexCount = 0; for (uint32_t y = 0; y < s_terrainSize; y++) { for (uint32_t x = 0; x < s_terrainSize; x++) { PosTexCoord0Vertex* vert = &m_terrain.m_vertices[m_terrain.m_vertexCount]; vert->m_x = (float)x; vert->m_y = m_terrain.m_heightMap[(y * s_terrainSize) + x]; vert->m_z = (float)y; vert->m_u = (x + 0.5f) / s_terrainSize; vert->m_v = (y + 0.5f) / s_terrainSize; m_terrain.m_vertexCount++; } } m_terrain.m_indexCount = 0; for (uint16_t y = 0; y < (s_terrainSize - 1); y++) { uint16_t y_offset = (y * s_terrainSize); for (uint16_t x = 0; x < (s_terrainSize - 1); x++) { m_terrain.m_indices[m_terrain.m_indexCount + 0] = y_offset + x + 1; m_terrain.m_indices[m_terrain.m_indexCount + 1] = y_offset + x + s_terrainSize; m_terrain.m_indices[m_terrain.m_indexCount + 2] = y_offset + x; m_terrain.m_indices[m_terrain.m_indexCount + 3] = y_offset + x + s_terrainSize + 1; m_terrain.m_indices[m_terrain.m_indexCount + 4] = y_offset + x + s_terrainSize; m_terrain.m_indices[m_terrain.m_indexCount + 5] = y_offset + x + 1; m_terrain.m_indexCount += 6; } } } void updateTerrain() { const bgfx::Memory* mem; switch (m_terrain.m_mode) { default: // Vertex Buffer : Destroy and recreate a regular vertex buffer to update terrain. updateTerrainMesh(); if (bgfx::isValid(m_vbh) ) { bgfx::destroy(m_vbh); } mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount); m_vbh = bgfx::createVertexBuffer(mem, PosTexCoord0Vertex::ms_layout); if (bgfx::isValid(m_ibh) ) { bgfx::destroy(m_ibh); } mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount); m_ibh = bgfx::createIndexBuffer(mem); break; case 1: // Dynamic Vertex Buffer : Utilize dynamic vertex buffer to update terrain. updateTerrainMesh(); if (!bgfx::isValid(m_dvbh) ) { m_dvbh = bgfx::createDynamicVertexBuffer(m_terrain.m_vertexCount, PosTexCoord0Vertex::ms_layout); } mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount); bgfx::update(m_dvbh, 0, mem); if (!bgfx::isValid(m_dibh) ) { m_dibh = bgfx::createDynamicIndexBuffer(m_terrain.m_indexCount); } mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount); bgfx::update(m_dibh, 0, mem); break; case 2: // Height Texture: Update a height texture that is sampled in the terrain vertex shader. if (!bgfx::isValid(m_vbh) || !bgfx::isValid(m_ibh) ) { updateTerrainMesh(); mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount); m_vbh = bgfx::createVertexBuffer(mem, PosTexCoord0Vertex::ms_layout); mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount); m_ibh = bgfx::createIndexBuffer(mem); } if (!bgfx::isValid(m_heightTexture) ) { m_heightTexture = bgfx::createTexture2D(s_terrainSize, s_terrainSize, false, 1, bgfx::TextureFormat::R8); } mem = bgfx::makeRef(&m_terrain.m_heightMap[0], sizeof(uint8_t) * s_terrainSize * s_terrainSize); bgfx::updateTexture2D(m_heightTexture, 0, 0, 0, 0, s_terrainSize, s_terrainSize, mem); break; } } void paintTerrainHeight(uint32_t _x, uint32_t _y) { for (int32_t area_y = -m_brush.m_size; area_y < m_brush.m_size; ++area_y) { for (int32_t area_x = -m_brush.m_size; area_x < m_brush.m_size; ++area_x) { int32_t brush_x = _x + area_x; if (brush_x < 0 || brush_x >= (int32_t)s_terrainSize) { continue; } int32_t brush_y = _y + area_y; if (brush_y < 0 || brush_y >= (int32_t)s_terrainSize) { continue; } uint32_t heightMapPos = (brush_y * s_terrainSize) + brush_x; float height = (float)m_terrain.m_heightMap[heightMapPos]; // Brush attenuation float a2 = (float)(area_x * area_x); float b2 = (float)(area_y * area_y); float brushAttn = m_brush.m_size - bx::sqrt(a2 + b2); // Raise/Lower and scale by brush power. height += 0.0f < bx::clamp(brushAttn*m_brush.m_power, 0.0f, m_brush.m_power) && m_brush.m_raise ? 1.0f : -1.0f ; m_terrain.m_heightMap[heightMapPos] = (uint8_t)bx::clamp(height, 0.0f, 255.0f); m_terrain.m_dirty = true; } } } void mousePickTerrain() { float ray_clip[4]; ray_clip[0] = ( (2.0f * m_mouseState.m_mx) / m_width - 1.0f) * -1.0f; ray_clip[1] = ( (1.0f - (2.0f * m_mouseState.m_my) / m_height) ) * -1.0f; ray_clip[2] = -1.0f; ray_clip[3] = 1.0f; float invProjMtx[16]; bx::mtxInverse(invProjMtx, m_projMtx); float ray_eye[4]; bx::vec4MulMtx(ray_eye, ray_clip, invProjMtx); ray_eye[2] = -1.0f; ray_eye[3] = 0.0f; float invViewMtx[16]; bx::mtxInverse(invViewMtx, m_viewMtx); float ray_world[4]; bx::vec4MulMtx(ray_world, ray_eye, invViewMtx); const bx::Vec3 rayDir = bx::mul(bx::normalize(bx::load<bx::Vec3>(ray_world) ), -1.0f); bx::Vec3 pos = cameraGetPosition(); for (int i = 0; i < 1000; ++i) { pos = bx::add(pos, rayDir); if (pos.x < 0 || pos.x >= s_terrainSize || pos.z < 0 || pos.z >= s_terrainSize) { continue; } uint32_t heightMapPos = ( (uint32_t)pos.z * s_terrainSize) + (uint32_t)pos.x; if (pos.y < m_terrain.m_heightMap[heightMapPos]) { paintTerrainHeight( (uint32_t)pos.x, (uint32_t)pos.z); return; } } } bool update() override { if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) ) { int64_t now = bx::getHPCounter(); static int64_t last = now; const int64_t frameTime = now - last; last = now; const double freq = double(bx::getHPFrequency() ); const float deltaTime = float(frameTime/freq); imguiBeginFrame(m_mouseState.m_mx , m_mouseState.m_my , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0) | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0) | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0) , m_mouseState.m_mz , uint16_t(m_width) , uint16_t(m_height) ); showExampleDialog(this); ImGui::SetNextWindowPos( ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f) , ImGuiCond_FirstUseEver ); ImGui::SetNextWindowSize( ImVec2(m_width / 5.0f, m_height / 3.0f) , ImGuiCond_FirstUseEver ); ImGui::Begin("Settings" , NULL , 0 ); ImGui::Separator(); m_terrain.m_dirty |= ImGui::RadioButton("Vertex Buffer", &m_terrain.m_mode, 0); m_terrain.m_dirty |= ImGui::RadioButton("Dynamic Vertex Buffer", &m_terrain.m_mode, 1); m_terrain.m_dirty |= ImGui::RadioButton("Height Texture", &m_terrain.m_mode, 2); ImGui::Separator(); ImGui::Checkbox("Raise Terrain", &m_brush.m_raise); ImGui::SliderInt("Brush Size", &m_brush.m_size, 1, 50); ImGui::SliderFloat("Brush Power", &m_brush.m_power, 0.0f, 1.0f); ImGui::End(); imguiEndFrame(); if (!ImGui::MouseOverArea() ) { // Update camera. cameraUpdate(deltaTime, m_mouseState); if (!!m_mouseState.m_buttons[entry::MouseButton::Left]) { mousePickTerrain(); } } // Update terrain. if (m_terrain.m_dirty) { updateTerrain(); m_terrain.m_dirty = false; } // Set view 0 default viewport. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) ); cameraGetViewMtx(m_viewMtx); bx::mtxProj(m_projMtx, 60.0f, float(m_width) / float(m_height), 0.1f, 2000.0f, bgfx::getCaps()->homogeneousDepth); bgfx::setViewTransform(0, m_viewMtx, m_projMtx); bgfx::setTransform(m_terrain.m_transform); switch (m_terrain.m_mode) { default: bgfx::setVertexBuffer(0, m_vbh); bgfx::setIndexBuffer(m_ibh); bgfx::submit(0, m_terrainProgram); break; case 1: bgfx::setVertexBuffer(0, m_dvbh); bgfx::setIndexBuffer(m_dibh); bgfx::submit(0, m_terrainProgram); break; case 2: bgfx::setVertexBuffer(0, m_vbh); bgfx::setIndexBuffer(m_ibh); bgfx::setTexture(0, s_heightTexture, m_heightTexture); bgfx::submit(0, m_terrainHeightTextureProgram); break; } // Advance to next frame. Rendering thread will be kicked to // process submitted rendering primitives. bgfx::frame(); return true; } return false; } bgfx::VertexBufferHandle m_vbh; bgfx::IndexBufferHandle m_ibh; bgfx::DynamicVertexBufferHandle m_dvbh; bgfx::DynamicIndexBufferHandle m_dibh; bgfx::ProgramHandle m_terrainProgram; bgfx::ProgramHandle m_terrainHeightTextureProgram; bgfx::UniformHandle s_heightTexture; bgfx::TextureHandle m_heightTexture; float m_viewMtx[16]; float m_projMtx[16]; uint32_t m_width; uint32_t m_height; uint32_t m_debug; uint32_t m_reset; uint32_t m_oldWidth; uint32_t m_oldHeight; uint32_t m_oldReset; TerrainData m_terrain; BrushData m_brush; entry::MouseState m_mouseState; int64_t m_timeOffset; }; } // namespace ENTRY_IMPLEMENT_MAIN( ExampleTerrain , "27-terrain" , "Terrain painting example." , "https://bkaradzic.github.io/bgfx/examples.html#terrain" );