Some byte shaving; lseek returns the offset.

This commit is contained in:
David Given 2019-06-16 20:04:07 +02:00
parent 01fdfef8c0
commit 2b013c34dc
3 changed files with 31 additions and 22 deletions

View file

@ -24,5 +24,5 @@ off_t lseek(int fd, off_t offset, int whence)
U16(fcbe->fcb.r) = offset>>7;
fcbe->offset = offset & 0x7f;
return 0;
return offset;
}

View file

@ -17,6 +17,7 @@ ssize_t read(int fd, void* buffer, size_t count)
struct FCBE* fcbe = &__fd[fd];
uint8_t olduser;
ssize_t result;
uint8_t* src;
__init_file_descriptors();
if (fcbe->fcb.dr == 0)
@ -36,8 +37,6 @@ ssize_t read(int fd, void* buffer, size_t count)
goto done;
if (fcbe->offset || !SECTOR_ALIGNED(count))
{
uint8_t delta;
/* We need to read bytes until we're at a sector boundary. */
cpm_set_dma(__transfer_buffer);
@ -46,13 +45,13 @@ ssize_t read(int fd, void* buffer, size_t count)
/* Copy enough bytes to reach the end of the sector. */
delta = 128 - fcbe->offset;
if (delta > count)
delta = count;
memcpy(bbuffer, __transfer_buffer+fcbe->offset, delta);
fcbe->offset += delta;
count -= delta;
bbuffer += delta;
src = __transfer_buffer + fcbe->offset;
while ((count != 0) && (fcbe->offset != 128))
{
*bbuffer++ = *src++;
fcbe->offset++;
count--;
}
/* If we've read enough bytes, advance to the next sector. */
@ -89,7 +88,13 @@ ssize_t read(int fd, void* buffer, size_t count)
if (cpm_read_random_safe(&fcbe->fcb) != 0)
goto eio;
memcpy(bbuffer, __transfer_buffer, count);
src = __transfer_buffer;
while (count != 0)
{
*bbuffer++ = *src++;
count--;
}
fcbe->offset = count;
}

View file

@ -19,10 +19,11 @@ void _sys_write_tty(char c)
ssize_t write(int fd, void* buffer, size_t count)
{
uint8_t* bbuffer = buffer;
const uint8_t* bbuffer = buffer;
struct FCBE* fcbe = &__fd[fd];
uint8_t olduser;
uint16_t result;
uint8_t* dest;
__init_file_descriptors();
if (fcbe->fcb.dr == 0)
@ -40,8 +41,6 @@ ssize_t write(int fd, void* buffer, size_t count)
if (fcbe->offset || !SECTOR_ALIGNED(count))
{
uint8_t delta;
/* We're not at a sector boundary, so we need to do a
* read/modify/write of the initial fragment. */
@ -51,13 +50,13 @@ ssize_t write(int fd, void* buffer, size_t count)
/* Copy enough bytes to reach the end of the sector. */
delta = 128 - fcbe->offset;
if (delta > count)
delta = count;
memcpy(__transfer_buffer+fcbe->offset, bbuffer, delta);
fcbe->offset += delta;
count -= delta;
bbuffer += delta;
dest = __transfer_buffer + fcbe->offset;
while ((count != 0) && (fcbe->offset != 128))
{
*dest++ = *bbuffer++;
fcbe->offset++;
count--;
}
/* Write back. */
@ -94,7 +93,12 @@ ssize_t write(int fd, void* buffer, size_t count)
if (cpm_read_random_safe(&fcbe->fcb) != 0)
goto eio;
memcpy(__transfer_buffer, bbuffer, count);
dest = __transfer_buffer;
while (count != 0)
{
*dest++ = *bbuffer++;
count--;
}
if (cpm_write_random(&fcbe->fcb) != 0)
goto eio;