diff --git a/src/mess/machine/psxcard.c b/src/mess/machine/psxcard.c index cf680960eda..76091c2549a 100644 --- a/src/mess/machine/psxcard.c +++ b/src/mess/machine/psxcard.c @@ -19,6 +19,9 @@ // // +static const int block_size = 128; +static const int card_size = block_size * 1024; + const device_type PSXCARD = &device_creator; enum transfer_states @@ -37,16 +40,13 @@ enum transfer_states }; psxcard_device::psxcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) - : device_t(mconfig, PSXCARD, "Sony PSX Memory Card", tag, owner, clock) + : device_t(mconfig, PSXCARD, "Sony PSX Memory Card", tag, owner, clock), + device_image_interface(mconfig, *this) { } void psxcard_device::device_start() { - cache=new unsigned char [128*1024]; - - memset(cache, 0, 128*1024); - m_owner = dynamic_cast(owner()); m_ack_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(psxcard_device::ack_timer), this)); @@ -77,6 +77,11 @@ void psxcard_device::device_reset() m_owner->ack(); } +void psxcard_device::device_config_complete() +{ + update_names(PSXCARD, "memcard", "mc"); +} + // // // @@ -88,7 +93,7 @@ bool psxcard_device::transfer(UINT8 to, UINT8 *from) switch (state) { case state_illegal: - if (to == 0x81) + if ((to == 0x81) && is_loaded()) { // printf("CARD: begin\n"); state = state_command; @@ -233,12 +238,13 @@ void psxcard_device::read_card(const unsigned short addr, unsigned char *buf) printf("card: read block %d\n",addr); #endif - if (addr<1024) + if (addr<(card_size/block_size)) { - memcpy(buf,cache+(addr*128),128); + fseek(addr*block_size, SEEK_SET); + fread(buf, block_size); } else { - memset(buf,0,128); + memset(buf,0,block_size); } } @@ -252,16 +258,13 @@ void psxcard_device::write_card(const unsigned short addr, unsigned char *buf) printf("card: write block %d\n",addr); #endif - if (addr<1024) + if (addr<(card_size/block_size)) { - memcpy(cache+(addr*128),buf,128); + fseek(addr*block_size, SEEK_SET); + fwrite(buf, block_size); } } -// -// -// - unsigned char psxcard_device::checksum_data(const unsigned char *buf, const unsigned int sz) { unsigned char chk=*buf++; @@ -270,6 +273,28 @@ unsigned char psxcard_device::checksum_data(const unsigned char *buf, const unsi return chk; } +bool psxcard_device::call_load() +{ + if(length() != card_size) + return IMAGE_INIT_FAIL; + return IMAGE_INIT_PASS; +} + +bool psxcard_device::call_create(int format_type, option_resolution *format_options) +{ + UINT8 block[block_size]; + int i, ret; + + memset(block, '\0', block_size); + for(i = 0; i < (card_size/block_size); i++) + { + ret = fwrite(block, block_size); + if(ret != block_size) + return IMAGE_INIT_FAIL; + } + return IMAGE_INIT_PASS; +} + void psxcard_device::do_card() { if(!m_bit) diff --git a/src/mess/machine/psxcard.h b/src/mess/machine/psxcard.h index c263cafb92d..0c4f670910b 100644 --- a/src/mess/machine/psxcard.h +++ b/src/mess/machine/psxcard.h @@ -10,13 +10,27 @@ class psx_controller_port_device; #define MCFG_PSXCARD_ADD(_tag) \ MCFG_DEVICE_ADD(_tag, PSXCARD, 0) -class psxcard_device : public device_t +class psxcard_device : public device_t, + public device_image_interface { public: psxcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); + virtual iodevice_t image_type() const { return IO_MEMCARD; } + + virtual bool is_readable() const { return 1; } + virtual bool is_writeable() const { return 1; } + virtual bool is_creatable() const { return 1; } + virtual bool must_be_loaded() const { return 0; } + virtual bool is_reset_on_load() const { return 0; } + virtual const char *file_extensions() const { return "mc"; } + virtual const option_guide *create_option_guide() const { return NULL; } + + virtual bool call_load(); + virtual bool call_create(int format_type, option_resolution *format_options); + private: - unsigned char pkt[0x8b], pkt_ptr, pkt_sz, cmd, *cache; + unsigned char pkt[0x8b], pkt_ptr, pkt_sz, cmd; unsigned short addr; int state; @@ -44,6 +58,7 @@ private: public: virtual void device_start(); virtual void device_reset(); + virtual void device_config_complete(); void clock_w(bool state) { if(m_clock && !m_sel && !state && !m_pad) do_card(); m_clock = state; } void sel_w(bool state);