OSDN Git Service

93069ca1858844933f59f91bba747e1adefc2ca3
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz700 / floppy.cpp
1 /*
2         SHARP MZ-800 Emulator 'EmuZ-800'
3         SHARP MZ-1500 Emulator 'EmuZ-1500'
4
5         Author : Takeda.Toshiya
6         Date   : 2011.05.16-
7
8         [ floppy ]
9 */
10
11 #include "floppy.h"
12 #include "../mb8877.h"
13
14 #define EVENT_MOTOR_ON  0
15 #define EVENT_MOTOR_OFF 1
16
17 void FLOPPY::initialize()
18 {
19         prev_dc = 0;
20         motor_on = false;
21         
22         // always motor on (temporary)
23 //      d_fdc->write_signal(SIG_MB8877_MOTOR, 1, 1);
24 }
25
26 void FLOPPY::reset()
27 {
28         register_id = -1;
29         irq_enabled = false;
30 }
31
32 void FLOPPY::write_io8(uint32_t addr, uint32_t data)
33 {
34         switch(addr & 0xff) {
35         case 0xdc:
36                 // drive reg
37                 if(data & 4) {
38                         d_fdc->write_signal(SIG_MB8877_DRIVEREG, data, 3);
39                 }
40                 // motor on/off
41                 if(!(prev_dc & 0x80) && (data & 0x80)) {
42                         // L -> H
43                         if(register_id != -1) {
44                                 cancel_event(this, register_id);
45                                 register_id = -1;
46                         }
47                         if(!motor_on) {
48                                 register_event(this, EVENT_MOTOR_ON, 560000, false, &register_id);
49                         }
50                 } else if((prev_dc & 0x80) && !(data & 0x80)) {
51                         // H -> L
52                         if(register_id != -1) {
53                                 cancel_event(this, register_id);
54                                 register_id = -1;
55                         }
56                         if(motor_on) {
57                                 register_event(this, EVENT_MOTOR_OFF, 1500000, false, &register_id);
58                         }
59                 }
60                 prev_dc = data;
61                 break;
62         case 0xdd:
63                 // side reg
64                 d_fdc->write_signal(SIG_MB8877_SIDEREG, data, 1);
65                 break;
66         case 0xde:
67                 // ???
68                 break;
69         case 0xdf:
70                 // irq enable
71                 irq_enabled = ((data & 1) != 0);
72                 break;
73         }
74 }
75
76 void FLOPPY::event_callback(int event_id, int err)
77 {
78         if(event_id == EVENT_MOTOR_ON) {
79                 d_fdc->write_signal(SIG_MB8877_MOTOR, 1, 1);
80                 motor_on = true;
81         } else if(event_id == EVENT_MOTOR_OFF) {
82                 d_fdc->write_signal(SIG_MB8877_MOTOR, 0, 0);
83                 motor_on = false;
84         }
85         register_id = -1;
86 }
87
88 void FLOPPY::write_signal(int id, uint32_t data, uint32_t mask)
89 {
90         if(id == SIG_FLOPPY_DRQ) {
91                 if(irq_enabled && (data & mask) != 0) {
92                         d_cpu->set_intr_line(true, true, 0);
93                 }
94         }
95 }
96
97 #define STATE_VERSION   1
98
99 #include "../../statesub.h"
100
101 void FLOPPY::decl_state()
102 {
103         enter_decl_state(STATE_VERSION);
104         
105         DECL_STATE_ENTRY_UINT32(prev_dc);
106         DECL_STATE_ENTRY_INT32(register_id);
107         DECL_STATE_ENTRY_BOOL(motor_on);
108         DECL_STATE_ENTRY_BOOL(irq_enabled);
109
110         leave_decl_state();
111 }
112
113 void FLOPPY::save_state(FILEIO* state_fio)
114 {
115         if(state_entry != NULL) {
116                 state_entry->save_state(state_fio);
117         }
118 //      state_fio->FputUint32(STATE_VERSION);
119 //      state_fio->FputInt32(this_device_id);
120         
121 //      state_fio->FputUint32(prev_dc);
122 //      state_fio->FputInt32(register_id);
123 //      state_fio->FputBool(motor_on);
124 //      state_fio->FputBool(irq_enabled);
125 }
126
127 bool FLOPPY::load_state(FILEIO* state_fio)
128 {
129         bool mb = false;
130         if(state_entry != NULL) {
131                 mb = state_entry->load_state(state_fio);
132         }
133         if(!mb) {
134                 return false;
135         }
136 //      if(state_fio->FgetUint32() != STATE_VERSION) {
137 //              return false;
138 //      }
139 //      if(state_fio->FgetInt32() != this_device_id) {
140 //              return false;
141 //      }
142 //      prev_dc = state_fio->FgetUint32();
143 //      register_id = state_fio->FgetInt32();
144 //      motor_on = state_fio->FgetBool();
145 //      irq_enabled = state_fio->FgetBool();
146         return true;
147 }
148
149 bool FLOPPY::process_state(FILEIO* state_fio, bool loading)
150 {
151         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
152                 return false;
153         }
154         if(!state_fio->StateCheckInt32(this_device_id)) {
155                 return false;
156         }
157         state_fio->StateUint32(prev_dc);
158         state_fio->StateInt32(register_id);
159         state_fio->StateBool(motor_on);
160         state_fio->StateBool(irq_enabled);
161         return true;
162 }