mpw/mpw/mpw_io.cpp

146 lines
3.4 KiB
C++
Raw Normal View History

2013-07-30 05:06:19 +00:00
/*
* Copyright (c) 2013, Kelvin W Sherlock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2013-07-30 05:06:19 +00:00
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
2013-07-30 05:06:19 +00:00
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
2013-07-30 05:06:19 +00:00
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
2013-02-16 20:37:37 +00:00
#include "mpw.h"
#include "mpw_internal.h"
2014-12-21 13:25:40 +00:00
#include "mpw_errno.h"
2013-02-16 20:37:37 +00:00
#include <algorithm>
#include <memory>
#include <cerrno>
#include <cstdio>
#include <unistd.h>
#include <cpu/defs.h>
#include <cpu/fmem.h>
2017-10-03 01:16:12 +00:00
#include <cpu/CpuModule.h>
2013-02-16 20:37:37 +00:00
2013-05-19 01:44:02 +00:00
#include <macos/errors.h>
2013-02-16 20:37:37 +00:00
#include <toolbox/os.h>
2013-02-26 01:16:55 +00:00
#include <toolbox/os_internal.h>
2013-02-16 20:37:37 +00:00
namespace MPW
{
void ftrap_read(uint16_t trap)
{
uint32_t d0;
uint32_t sp = cpuGetAReg(7);
uint32_t parm = memoryReadLong(sp + 4);
MPWFile f;
f.flags = memoryReadWord(parm);
f.error = memoryReadWord(parm + 2);
f.device = memoryReadLong(parm + 4);
f.cookie = memoryReadLong(parm + 8);
f.count = memoryReadLong(parm + 12);
f.buffer = memoryReadLong(parm + 16);
2013-03-02 22:39:13 +00:00
Log("%04x Read(%08x)\n", trap, parm);
2013-02-16 20:37:37 +00:00
d0 = 0;
2013-02-26 01:16:55 +00:00
int fd = f.cookie;
ssize_t size;
2013-02-16 20:37:37 +00:00
Log(" read(%04x, %08x, %08x)\n", fd, f.buffer, f.count);
2013-02-26 01:16:55 +00:00
size = OS::Internal::FDEntry::read(fd, memoryPointer(f.buffer), f.count);
2013-03-02 22:39:13 +00:00
//Log(" -> %ld\n", size);
2013-02-26 01:16:55 +00:00
if (size < 0)
2013-02-16 20:37:37 +00:00
{
2013-02-26 01:16:55 +00:00
//f.count = 0;
2013-05-19 01:44:02 +00:00
f.error = MacOS::ioErr; // ioErr
2014-12-21 13:25:40 +00:00
d0 = mpw_errno_from_errno();
2013-02-16 20:37:37 +00:00
}
2013-02-26 01:16:55 +00:00
else
{
f.count -= size;
f.error = 0;
}
// write back...
memoryWriteWord(f.error, parm + 2);
memoryWriteLong(f.count, parm + 12);
2013-02-16 20:37:37 +00:00
cpuSetDReg(0, d0);
}
void ftrap_write(uint16_t trap)
{
uint32_t d0;
uint32_t sp = cpuGetAReg(7);
uint32_t parm = memoryReadLong(sp + 4);
MPWFile f;
f.flags = memoryReadWord(parm);
f.error = memoryReadWord(parm + 2);
f.device = memoryReadLong(parm + 4);
f.cookie = memoryReadLong(parm + 8);
f.count = memoryReadLong(parm + 12);
f.buffer = memoryReadLong(parm + 16);
2013-02-16 23:51:18 +00:00
Log("%04x Write(%08x)\n", trap, parm);
2013-02-16 20:37:37 +00:00
d0 = 0;
2013-02-26 01:16:55 +00:00
int fd = f.cookie;
ssize_t size;
2013-03-02 22:39:13 +00:00
Log(" write(%04x, %08x, %08x)\n", fd, f.buffer, f.count);
2013-02-26 01:16:55 +00:00
size = OS::Internal::FDEntry::write(fd, memoryPointer(f.buffer), f.count);
if (size < 0)
2013-02-16 20:37:37 +00:00
{
2013-02-26 01:16:55 +00:00
//f.count = 0;
2013-05-19 01:44:02 +00:00
f.error = MacOS::ioErr; // ioErr
2014-12-21 13:25:40 +00:00
d0 = mpw_errno_from_errno();
2013-02-16 20:37:37 +00:00
}
2013-02-26 01:16:55 +00:00
else
{
f.count -= size;
f.error = 0;
}
// write back...
memoryWriteWord(f.error, parm + 2);
memoryWriteLong(f.count, parm + 12);
cpuSetDReg(0, d0);
2013-02-16 20:37:37 +00:00
}
}