summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/iwm.h
blob: 1c8fed9190f7996d6ebde319f0ccf1d6cf476599 (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
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert
/*********************************************************************

    Apple IWM floppy disk controller

*********************************************************************/
#ifndef MAME_MACHINE_IWM_H
#define MAME_MACHINE_IWM_H

#pragma once

#include "applefdintf.h"


//**************************************************************************
//  TYPE DEFINITIONS
//**************************************************************************


class iwm_device: public applefdintf_device
{
public:
	// construction/destruction
	iwm_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, uint32_t q3_clock = 0);
	iwm_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, XTAL q3_clock) :
		iwm_device(mconfig, tag, owner, clock, q3_clock.value()) {}

	virtual u8 read(offs_t offset) override;
	virtual void write(offs_t offset, u8 data) override;

	virtual void set_floppy(floppy_image_device *floppy) override;
	virtual floppy_image_device *get_floppy() const override;

	virtual void sync() override;

protected:
	virtual void device_start() override;
	virtual void device_reset() override;
	virtual void device_timer(emu_timer &timer, device_timer_id id, int param) override;

private:
	enum {
		MODE_IDLE,
		MODE_ACTIVE, MODE_DELAY, // m_active modes
		MODE_READ, MODE_WRITE    // m_rw modes
	};

	// state machine states
	enum {
		S_IDLE,
		SR_WINDOW_EDGE_0,
		SR_WINDOW_EDGE_1,
		SW_WINDOW_LOAD,
		SW_WINDOW_MIDDLE,
		SW_WINDOW_END,
		SW_UNDERRUN,
	};

	floppy_image_device *m_floppy;
	emu_timer *m_timer = nullptr;
	double m_q3_fclk_ratio, m_fclk_q3_ratio;
	u64 m_last_sync = 0, m_next_state_change = 0, m_sync_update = 0, m_async_update = 0;
	u64 m_flux_write_start = 0;
	std::array<u64, 16> m_flux_write;
	u32 m_flux_write_count = 0;
	u32 m_q3_clock = 0;
	int m_active = 0, m_rw = 0, m_rw_state = 0;
	u8 m_data = 0, m_whd = 0, m_mode = 0, m_status = 0, m_control = 0, m_rw_bit_count = 0;
	u8 m_rsh = 0, m_wsh = 0;
	u8 m_devsel = 0;

	u8 control(int offset, u8 data);
	u64 time_to_cycles(const attotime &tm) const;
	attotime cycles_to_time(u64 cycles) const;
	u64 fclk_to_q3(u64 cycles) const;
	u64 q3_to_fclk(u64 cycles) const;

	void mode_w(u8 data);
	void data_w(u8 data);

	u64 window_size() const;
	u64 half_window_size() const;
	u64 read_register_update_delay() const;
	u64 write_sync_half_window_size() const;
	inline bool is_sync() const;
	void flush_write(u64 when = 0);
};

DECLARE_DEVICE_TYPE(IWM, iwm_device)

#endif  /* MAME_MACHINE_IWM_H */