Previous 15994 Revisions Next

r12049 Saturday 25th June, 2011 at 08:48:28 UTC by Miodrag Milanović
Moved coco_vhd to proper place and modernized (no whatsnew)
[src/mess]mess.mak
[src/mess/devices]coco_vhd.c coco_vhd.h
[src/mess/drivers]coco.c
[src/mess/machine]coco_vhd.c* coco_vhd.h*

src/mess/devices/coco_vhd.c
r12048r12049
1/***************************************************************************
2
3    coco_vhd.c
4
5    Color Computer Virtual Hard Drives
6
7****************************************************************************
8
9    Technical specs on the Virtual Hard Disk interface
10
11    Address       Description
12    -------       -----------
13    FF80          Logical record number (high byte)
14    FF81          Logical record number (middle byte)
15    FF82          Logical record number (low byte)
16    FF83          Command/status register
17    FF84          Buffer address (high byte)
18    FF85          Buffer address (low byte)
19
20    Set the other registers, and then issue a command to FF83 as follows:
21
22     0 = read 256-byte sector at LRN
23     1 = write 256-byte sector at LRN
24     2 = flush write cache (Closes and then opens the image file)
25
26    Error values:
27
28     0 = no error
29    -1 = power-on state (before the first command is recieved)
30    -2 = invalid command
31     2 = VHD image does not exist
32     4 = Unable to open VHD image file
33     5 = access denied (may not be able to write to VHD image)
34
35    IMPORTANT: The I/O buffer must NOT cross an 8K MMU bank boundary.
36
37 ***************************************************************************/
38
39#include "emu.h"
40#include "coco_vhd.h"
41#include "includes/coco.h"
42#include "machine/ram.h"
43
44
45/***************************************************************************
46    CONSTANTS
47***************************************************************************/
48
49#define VERBOSE 0
50
51#define VHDSTATUS_OK               0x00
52#define VHDSTATUS_NO_VHD_ATTACHED      0x02
53#define VHDSTATUS_ACCESS_DENIED         0x05
54#define VHDSTATUS_UNKNOWN_COMMAND      0xFE
55#define VHDSTATUS_POWER_ON_STATE      0xFF
56
57#define VHDCMD_READ      0
58#define VHDCMD_WRITE   1
59#define VHDCMD_FLUSH   2
60
61
62
63/***************************************************************************
64    TYPE DEFINITIONS
65***************************************************************************/
66
67typedef struct _vhd_info vhd_info;
68struct _vhd_info
69{
70   UINT32 logical_record_number;
71   UINT32 buffer_address;
72   UINT8 status;
73};
74
75
76
77/***************************************************************************
78    INLINE FUNCTIONS
79***************************************************************************/
80
81INLINE vhd_info *get_safe_token(device_t *device)
82{
83   assert(device != NULL);
84   assert(device->type() == COCO_VHD);
85   return (vhd_info *) downcast<legacy_device_base *>(device)->token();
86}
87
88
89
90/***************************************************************************
91    CORE IMPLEMENTATION
92***************************************************************************/
93
94static DEVICE_START( coco_vhd )
95{
96   vhd_info *vhd = get_safe_token(device);
97   vhd->status = VHDSTATUS_NO_VHD_ATTACHED;
98}
99
100
101
102static DEVICE_IMAGE_LOAD( coco_vhd )
103{
104   vhd_info *vhd = get_safe_token(&image.device());
105   vhd->status = VHDSTATUS_POWER_ON_STATE;
106   vhd->logical_record_number = 0;
107   vhd->buffer_address = 0;
108   return IMAGE_INIT_PASS;
109}
110
111
112
113static void coco_vhd_readwrite(device_t *device, UINT8 data)
114{
115   vhd_info *vhd = get_safe_token(device);
116   device_image_interface *image = dynamic_cast<device_image_interface *>(device);
117   int result;
118   int phyOffset;
119   UINT32 nBA = vhd->buffer_address;
120   UINT32 bytes_to_read;
121   UINT32 bytes_to_write;
122   UINT64 seek_position;
123   UINT64 total_size;
124   char buffer[1024];
125
126   /* access the image */
127   if (!image->exists())
128   {
129      vhd->status = VHDSTATUS_NO_VHD_ATTACHED;
130      return;
131   }
132
133   /* perform the seek */
134   seek_position = ((UINT64) 256) * vhd->logical_record_number;
135   total_size = image->length();
136   result = image->fseek(MIN(seek_position, total_size), SEEK_SET);
137   if (result < 0)
138   {
139      vhd->status = VHDSTATUS_ACCESS_DENIED;
140      return;
141   }
142
143   /* expand the disk, if necessary */
144   if (data == VHDCMD_WRITE)
145   {
146      while(total_size < seek_position)
147      {
148         memset(buffer, 0, sizeof(buffer));
149
150         bytes_to_write = (UINT32) MIN(seek_position - total_size, (UINT64) sizeof(buffer));
151         result = image->fwrite(buffer, bytes_to_write);
152         if (result != bytes_to_write)
153         {
154            vhd->status = VHDSTATUS_ACCESS_DENIED;
155            return;
156         }
157
158         total_size += bytes_to_write;
159      }
160   }
161
162   phyOffset = coco3_mmu_translate(device->machine(), (nBA >> 12 ) / 2, nBA % 8192 );
163
164   switch(data)
165   {
166      case VHDCMD_READ: /* Read sector */
167         memset(&ram_get_ptr(device->machine().device(RAM_TAG))[phyOffset], 0, 256);
168         if (total_size > seek_position)
169         {
170            bytes_to_read = (UINT32) MIN((UINT64) 256, total_size - seek_position);
171            result = image->fread(&ram_get_ptr(device->machine().device(RAM_TAG))[phyOffset], bytes_to_read);
172            if (result != bytes_to_read)
173            {
174               vhd->status = VHDSTATUS_ACCESS_DENIED;
175               return;
176            }
177         }
178
179         vhd->status = VHDSTATUS_OK;
180         break;
181
182      case VHDCMD_WRITE: /* Write Sector */
183         result = image->fwrite((&ram_get_ptr(device->machine().device(RAM_TAG))[phyOffset]), 256);
184
185         if (result != 256)
186         {
187            vhd->status = VHDSTATUS_ACCESS_DENIED;
188            return;
189         }
190
191         vhd->status = VHDSTATUS_OK;
192         break;
193
194      case VHDCMD_FLUSH: /* Flush file cache */
195         vhd->status = VHDSTATUS_OK;
196         break;
197
198      default:
199         vhd->status = VHDSTATUS_UNKNOWN_COMMAND;
200         break;
201   }
202}
203
204
205
206READ8_DEVICE_HANDLER(coco_vhd_io_r)
207{
208   vhd_info *vhd = get_safe_token(device);
209   UINT8 result = 0;
210
211   switch(offset)
212   {
213      case 0xff83 - 0xff80:
214         if (VERBOSE)
215            logerror("vhd: Status read: %d\n", vhd->status);
216         result = vhd->status;
217         break;
218   }
219   return result;
220}
221
222
223
224WRITE8_DEVICE_HANDLER(coco_vhd_io_w)
225{
226   vhd_info *vhd = get_safe_token(device);
227   int pos;
228
229   switch(offset)
230   {
231      case 0xff80 - 0xff80:
232      case 0xff81 - 0xff80:
233      case 0xff82 - 0xff80:
234         pos = ((0xff82 - 0xff80) - offset) * 8;
235         vhd->logical_record_number &= ~(0xFF << pos);
236         vhd->logical_record_number += data << pos;
237         if (VERBOSE)
238            logerror("vhd: LRN write: %6.6X\n", vhd->logical_record_number);
239         break;
240
241      case 0xff83 - 0xff80:
242         coco_vhd_readwrite(device, data);
243         if (VERBOSE)
244            logerror("vhd: Command: %d\n", data);
245         break;
246
247      case 0xff84 - 0xff80:
248         vhd->buffer_address &= 0xFFFF00FF;
249         vhd->buffer_address += data << 8;
250         if (VERBOSE)
251            logerror("vhd: BA write: %X (%2.2X..)\n", vhd->buffer_address, data);
252         break;
253
254      case 0xff85 - 0xff80:
255         vhd->buffer_address &= 0xFFFFFF00;
256         vhd->buffer_address += data;
257         if (VERBOSE)
258            logerror("vhd: BA write: %X (..%2.2X)\n", vhd->buffer_address, data);
259         break;
260   }
261}
262
263
264
265DEVICE_GET_INFO(coco_vhd)
266{
267   switch(state)
268   {
269      /* --- the following bits of info are returned as 64-bit signed integers --- */
270      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(vhd_info); break;
271      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0; break;
272      case DEVINFO_INT_IMAGE_TYPE:               info->i = IO_HARDDISK; break;
273      case DEVINFO_INT_IMAGE_READABLE:            info->i = 1; break;
274      case DEVINFO_INT_IMAGE_WRITEABLE:            info->i = 1; break;
275      case DEVINFO_INT_IMAGE_CREATABLE:            info->i = 1; break;
276
277      /* --- the following bits of info are returned as pointers to data or functions --- */
278      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(coco_vhd); break;
279      case DEVINFO_FCT_IMAGE_LOAD:               info->f = (genf *) DEVICE_IMAGE_LOAD_NAME(coco_vhd); break;
280
281      /* --- the following bits of info are returned as NULL-terminated strings --- */
282      case DEVINFO_STR_NAME:                     strcpy(info->s, "Virtual Hard Disk"); break;
283      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "Virtual Hard Disk"); break;
284      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__); break;
285      case DEVINFO_STR_IMAGE_FILE_EXTENSIONS:         strcpy(info->s, "vhd"); break;
286   }
287}
288
289DEFINE_LEGACY_IMAGE_DEVICE(COCO_VHD, coco_vhd);
src/mess/devices/coco_vhd.h
r12048r12049
1/***************************************************************************
2
3    coco_vhd.h
4
5    Color Computer Virtual Hard Drives
6
7***************************************************************************/
8
9#ifndef COCOVHD_H
10#define COCOVHD_H
11
12#include "image.h"
13
14READ8_DEVICE_HANDLER(coco_vhd_io_r);
15WRITE8_DEVICE_HANDLER(coco_vhd_io_w);
16
17
18DECLARE_LEGACY_IMAGE_DEVICE(COCO_VHD, coco_vhd);
19
20#define MCFG_COCO_VHD_ADD(_tag) \
21   MCFG_DEVICE_ADD(_tag, COCO_VHD, 0) \
22
23#endif /* COCOVHD_H */
src/mess/machine/coco_vhd.c
r0r12049
1/***************************************************************************
2
3    coco_vhd.c
4
5    Color Computer Virtual Hard Drives
6
7****************************************************************************
8
9    Technical specs on the Virtual Hard Disk interface
10
11    Address       Description
12    -------       -----------
13    FF80          Logical record number (high byte)
14    FF81          Logical record number (middle byte)
15    FF82          Logical record number (low byte)
16    FF83          Command/status register
17    FF84          Buffer address (high byte)
18    FF85          Buffer address (low byte)
19
20    Set the other registers, and then issue a command to FF83 as follows:
21
22     0 = read 256-byte sector at LRN
23     1 = write 256-byte sector at LRN
24     2 = flush write cache (Closes and then opens the image file)
25
26    Error values:
27
28     0 = no error
29    -1 = power-on state (before the first command is recieved)
30    -2 = invalid command
31     2 = VHD image does not exist
32     4 = Unable to open VHD image file
33     5 = access denied (may not be able to write to VHD image)
34
35    IMPORTANT: The I/O buffer must NOT cross an 8K MMU bank boundary.
36
37 ***************************************************************************/
38
39#include "emu.h"
40#include "coco_vhd.h"
41#include "includes/coco.h"
42#include "machine/ram.h"
43
44
45/***************************************************************************
46    CONSTANTS
47***************************************************************************/
48
49#define VERBOSE 0
50
51#define VHDSTATUS_OK               0x00
52#define VHDSTATUS_NO_VHD_ATTACHED      0x02
53#define VHDSTATUS_ACCESS_DENIED         0x05
54#define VHDSTATUS_UNKNOWN_COMMAND      0xFE
55#define VHDSTATUS_POWER_ON_STATE      0xFF
56
57#define VHDCMD_READ      0
58#define VHDCMD_WRITE   1
59#define VHDCMD_FLUSH   2
60
61
62/***************************************************************************
63    CORE IMPLEMENTATION
64***************************************************************************/
65
66const device_type COCO_VHD = &device_creator<coco_vhd_image_device>;
67
68//-------------------------------------------------
69//  coco_vhd_image_device - constructor
70//-------------------------------------------------
71
72coco_vhd_image_device::coco_vhd_image_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
73    : device_t(mconfig, COCO_VHD, "Virtual Hard Disk", tag, owner, clock),
74     device_image_interface(mconfig, *this)
75{
76
77}
78
79//-------------------------------------------------
80//  coco_vhd_image_device - destructor
81//-------------------------------------------------
82
83coco_vhd_image_device::~coco_vhd_image_device()
84{
85}
86
87//-------------------------------------------------
88//  device_config_complete - perform any
89//  operations now that the configuration is
90//  complete
91//-------------------------------------------------
92
93void coco_vhd_image_device::device_config_complete()
94{
95   // set brief and instance name
96   update_names();
97}
98
99//-------------------------------------------------
100//  device_start - device-specific startup
101//-------------------------------------------------
102
103void coco_vhd_image_device::device_start()
104{
105   m_status = VHDSTATUS_NO_VHD_ATTACHED;
106}
107
108bool coco_vhd_image_device::call_load()
109{
110   m_status = VHDSTATUS_POWER_ON_STATE;
111   m_logical_record_number = 0;
112   m_buffer_address = 0;
113   return IMAGE_INIT_PASS;
114}
115
116void coco_vhd_image_device::coco_vhd_readwrite(UINT8 data)
117{
118   int result;
119   int phyOffset;
120   UINT32 nBA = m_buffer_address;
121   UINT32 bytes_to_read;
122   UINT32 bytes_to_write;
123   UINT64 seek_position;
124   UINT64 total_size;
125   char buffer[1024];
126
127   /* access the image */
128   if (!exists())
129   {
130      m_status = VHDSTATUS_NO_VHD_ATTACHED;
131      return;
132   }
133
134   /* perform the seek */
135   seek_position = ((UINT64) 256) * m_logical_record_number;
136   total_size = length();
137   result = fseek(MIN(seek_position, total_size), SEEK_SET);
138   if (result < 0)
139   {
140      m_status = VHDSTATUS_ACCESS_DENIED;
141      return;
142   }
143
144   /* expand the disk, if necessary */
145   if (data == VHDCMD_WRITE)
146   {
147      while(total_size < seek_position)
148      {
149         memset(buffer, 0, sizeof(buffer));
150
151         bytes_to_write = (UINT32) MIN(seek_position - total_size, (UINT64) sizeof(buffer));
152         result = fwrite(buffer, bytes_to_write);
153         if (result != bytes_to_write)
154         {
155            m_status = VHDSTATUS_ACCESS_DENIED;
156            return;
157         }
158
159         total_size += bytes_to_write;
160      }
161   }
162
163   phyOffset = coco3_mmu_translate(device().machine(), (nBA >> 12 ) / 2, nBA % 8192 );
164
165   switch(data)
166   {
167      case VHDCMD_READ: /* Read sector */
168         memset(&ram_get_ptr(device().machine().device(RAM_TAG))[phyOffset], 0, 256);
169         if (total_size > seek_position)
170         {
171            bytes_to_read = (UINT32) MIN((UINT64) 256, total_size - seek_position);
172            result = fread(&ram_get_ptr(device().machine().device(RAM_TAG))[phyOffset], bytes_to_read);
173            if (result != bytes_to_read)
174            {
175               m_status = VHDSTATUS_ACCESS_DENIED;
176               return;
177            }
178         }
179
180         m_status = VHDSTATUS_OK;
181         break;
182
183      case VHDCMD_WRITE: /* Write Sector */
184         result = fwrite((&ram_get_ptr(device().machine().device(RAM_TAG))[phyOffset]), 256);
185
186         if (result != 256)
187         {
188            m_status = VHDSTATUS_ACCESS_DENIED;
189            return;
190         }
191
192         m_status = VHDSTATUS_OK;
193         break;
194
195      case VHDCMD_FLUSH: /* Flush file cache */
196         m_status = VHDSTATUS_OK;
197         break;
198
199      default:
200         m_status = VHDSTATUS_UNKNOWN_COMMAND;
201         break;
202   }
203}
204
205
206
207READ8_MEMBER(coco_vhd_image_device::read)
208{
209   UINT8 result = 0;
210
211   switch(offset)
212   {
213      case 0xff83 - 0xff80:
214         if (VERBOSE)
215            logerror("vhd: Status read: %d\n", m_status);
216         result = m_status;
217         break;
218   }
219   return result;
220}
221
222
223
224WRITE8_MEMBER(coco_vhd_image_device::write)
225{
226   int pos;
227
228   switch(offset)
229   {
230      case 0xff80 - 0xff80:
231      case 0xff81 - 0xff80:
232      case 0xff82 - 0xff80:
233         pos = ((0xff82 - 0xff80) - offset) * 8;
234         m_logical_record_number &= ~(0xFF << pos);
235         m_logical_record_number += data << pos;
236         if (VERBOSE)
237            logerror("vhd: LRN write: %6.6X\n", m_logical_record_number);
238         break;
239
240      case 0xff83 - 0xff80:
241         coco_vhd_readwrite(data);
242         if (VERBOSE)
243            logerror("vhd: Command: %d\n", data);
244         break;
245
246      case 0xff84 - 0xff80:
247         m_buffer_address &= 0xFFFF00FF;
248         m_buffer_address += data << 8;
249         if (VERBOSE)
250            logerror("vhd: BA write: %X (%2.2X..)\n", m_buffer_address, data);
251         break;
252
253      case 0xff85 - 0xff80:
254         m_buffer_address &= 0xFFFFFF00;
255         m_buffer_address += data;
256         if (VERBOSE)
257            logerror("vhd: BA write: %X (..%2.2X)\n", m_buffer_address, data);
258         break;
259   }
260}
src/mess/machine/coco_vhd.h
r0r12049
1/***************************************************************************
2
3    coco_vhd.h
4
5    Color Computer Virtual Hard Drives
6
7***************************************************************************/
8
9#ifndef COCOVHD_H
10#define COCOVHD_H
11
12#include "image.h"
13
14/***************************************************************************
15    TYPE DEFINITIONS
16***************************************************************************/
17
18// ======================> coco_vhd_image_device
19
20class coco_vhd_image_device :   public device_t,
21                        public device_image_interface
22{
23public:
24   // construction/destruction
25   coco_vhd_image_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
26   virtual ~coco_vhd_image_device();
27
28   // image-level overrides
29   virtual bool call_load();
30
31   virtual iodevice_t image_type() const { return IO_HARDDISK; }
32
33   virtual bool is_readable()  const { return 1; }
34   virtual bool is_writeable() const { return 1; }
35   virtual bool is_creatable() const { return 1; }
36   virtual bool must_be_loaded() const { return 0; }
37   virtual bool is_reset_on_load() const { return 0; }
38   virtual const char *image_interface() const { return NULL; }
39   virtual const char *file_extensions() const { return "vhd"; }
40   virtual const option_guide *create_option_guide() const { return NULL; }
41
42   // specific implementation
43   DECLARE_READ8_MEMBER(read);
44   DECLARE_WRITE8_MEMBER(write);
45
46protected:
47   // device-level overrides
48    virtual void device_config_complete();
49   virtual void device_start();
50
51   void coco_vhd_readwrite(UINT8 data);
52   
53   UINT32 m_logical_record_number;
54   UINT32 m_buffer_address;
55   UINT8  m_status;
56};
57
58// device type definition
59extern const device_type COCO_VHD;
60
61/***************************************************************************
62    DEVICE CONFIGURATION MACROS
63***************************************************************************/
64
65#define MCFG_COCO_VHD_ADD(_tag) \
66   MCFG_DEVICE_ADD(_tag, COCO_VHD, 0) \
67
68#endif /* COCOVHD_H */
src/mess/drivers/coco.c
r12048r12049
4242#include "imagedev/snapquik.h"
4343#include "imagedev/cartslot.h"
4444#include "devices/cococart.h"
45#include "devices/coco_vhd.h"
45#include "machine/coco_vhd.h"
4646#include "sound/ay8910.h"
4747#include "sound/dac.h"
4848#include "machine/ram.h"
r12048r12049
127127   AM_RANGE(0xff00, 0xff1f) AM_DEVREADWRITE_MODERN("pia_0", pia6821_device, read, write)
128128   AM_RANGE(0xff20, 0xff3f) AM_DEVREAD_MODERN("pia_1", pia6821_device, read) AM_DEVWRITE("pia_1", coco_pia_1_w)
129129   AM_RANGE(0xff40, 0xff7f) AM_DEVREADWRITE("coco_cartslot", coco_cartridge_r, coco_cartridge_w)
130   AM_RANGE(0xff80, 0xff85) AM_DEVREADWRITE("vhd", coco_vhd_io_r, coco_vhd_io_w)
130   AM_RANGE(0xff80, 0xff85) AM_DEVREADWRITE_MODERN("vhd", coco_vhd_image_device, read, write)
131131   AM_RANGE(0xff90, 0xff9f) AM_READWRITE(coco3_gime_r, coco3_gime_w)
132132   AM_RANGE(0xffa0, 0xffaf) AM_READWRITE(coco3_mmu_r, coco3_mmu_w)
133133   AM_RANGE(0xffb0, 0xffbf) AM_READ_BANK("bank10") AM_WRITE(coco3_palette_w)
src/mess/mess.mak
r12048r12049
15961596   $(MESS_DRIVERS)/coco.o      \
15971597   $(MESS_VIDEO)/coco3.o      \
15981598   $(MESS_FORMATS)/cocopak.o   \
1599   $(MESS_DEVICES)/coco_vhd.o   \
1599   $(MESS_MACHINE)/coco_vhd.o   \
16001600   $(MESS_DEVICES)/cococart.o   \
16011601   $(MESS_DEVICES)/coco_fdc.o   \
16021602   $(MESS_DEVICES)/coco_pak.o   \

Previous 15994 Revisions Next


© 1998-2018 The MESS Team