OSDN Git Service

[VM][STATE] .
[csp-qt/common_source_project-fm7.git] / source / src / vm / scsi_hdd.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2016.03.01-
6
7         [ SCSI hard disk drive ]
8 */
9
10 #include "scsi_hdd.h"
11 #include "harddisk.h"
12 #include "../fifo.h"
13
14 void SCSI_HDD::release()
15 {
16         for(int i = 0; i < 8; i++) {
17                 if(disk[i] != NULL) {
18                         disk[i]->close();
19                         delete disk[i];
20                 }
21         }
22         SCSI_DEV::release();
23 }
24
25 bool SCSI_HDD::is_device_existing()
26 {
27         for(int i = 0; i < 8; i++) {
28                 if(disk[i] != NULL && disk[i]->mounted()) {
29                         return true;
30                 }
31         }
32         return false;
33 }
34
35 uint32_t SCSI_HDD::physical_block_size()
36 {
37         HARDDISK *unit = disk[get_logical_unit_number()];
38         
39         if(unit != NULL && unit->mounted()) {
40                 return unit->sector_size;
41         }
42         return 512;
43 }
44
45 uint32_t SCSI_HDD::logical_block_size()
46 {
47         HARDDISK *unit = disk[get_logical_unit_number()];
48         
49         if(unit != NULL && unit->mounted()) {
50                 return unit->sector_size;
51         }
52         return 512;
53 }
54
55 uint32_t SCSI_HDD::max_logical_block_addr()
56 {
57         HARDDISK *unit = disk[get_logical_unit_number()];
58         
59         if(unit != NULL && unit->mounted()) {
60                 return unit->cylinders * unit->surfaces * unit->sectors;
61         }
62         return 0;
63 }
64
65 void SCSI_HDD::read_buffer(int length)
66 {
67         HARDDISK *unit = disk[get_logical_unit_number()];
68         
69         if(unit != NULL && unit->mounted()) {
70                 while(length > 0) {
71                         uint8_t tmp_buffer[SCSI_BUFFER_SIZE];
72                         int tmp_length = min(length, (int)sizeof(tmp_buffer));
73                         
74                         unit->read_buffer((long)position, tmp_length, tmp_buffer);
75                         for(int i = 0; i < tmp_length; i++) {
76                                 buffer->write(tmp_buffer[i]);
77                         }
78                         length -= tmp_length;
79                         position += tmp_length;
80                 }
81         }
82 }
83
84 void SCSI_HDD::write_buffer(int length)
85 {
86         HARDDISK *unit = disk[get_logical_unit_number()];
87         
88         if(unit != NULL && unit->mounted()) {
89                 while(length > 0) {
90                         uint8_t tmp_buffer[SCSI_BUFFER_SIZE];
91                         int tmp_length = min(length, (int)sizeof(tmp_buffer));
92                         
93                         for(int i = 0; i < tmp_length; i++) {
94                                 tmp_buffer[i] = buffer->read();
95                         }
96                         unit->write_buffer((long)position, tmp_length, tmp_buffer);
97                         length -= tmp_length;
98                         position += tmp_length;
99                 }
100         }
101 }
102
103 #define STATE_VERSION   1
104
105 #include "../statesub.h"
106
107 void SCSI_HDD::decl_state()
108 {
109         enter_decl_state(STATE_VERSION);
110         
111         leave_decl_state();
112
113         SCSI_DEV::decl_state();
114 }
115
116 void SCSI_HDD::save_state(FILEIO* state_fio)
117 {
118         uint32_t crc_value = 0xffffffff;
119         if(state_entry != NULL) {
120                 state_entry->save_state(state_fio, &crc_value);
121         }
122 //      state_fio->FputUint32(STATE_VERSION);
123 //      state_fio->FputInt32(this_device_id);
124 /*
125         for(int i = 0; i < 8; i++) {
126                 if(disk[i] != NULL) {
127                         disk[i]->save_state(state_fio);
128                 }
129         }
130 */
131 //      SCSI_DEV::save_state(state_fio);
132 }
133
134 bool SCSI_HDD::load_state(FILEIO* state_fio)
135 {
136         uint32_t crc_value = 0xffffffff;
137         bool stat = false;
138         bool mb = false;
139         if(state_entry != NULL) {
140            mb = state_entry->load_state(state_fio, &crc_value);
141         }
142         if(!mb) return false;
143 //      if(state_fio->FgetUint32() != STATE_VERSION) {
144 //              return false;
145 //      }
146 //      if(state_fio->FgetInt32() != this_device_id) {
147 //              return false;
148 //      }
149 /*
150         for(int i = 0; i < 8; i++) {
151                 if(disk[i] != NULL) {
152                         if(!disk[i]->load_state(state_fio)) {
153                                 return false;
154                         }
155                 }
156         }
157 */
158 //      return SCSI_DEV::load_state(state_fio);
159         return true;
160 }
161