break larger transfers into smaller blocks

Serial protocol is more stable with smaller transfers between
synchronization.  Add ability to write ProDOS disks.
This commit is contained in:
dschmenk 2013-05-31 11:14:36 -07:00
parent f126e00175
commit 28c563341f
3 changed files with 231 additions and 16 deletions

Binary file not shown.

View File

@ -24,21 +24,27 @@
exit(-1); \ exit(-1); \
} while(0) } while(0)
/* /*
* ASCII to scancode conversion * Apple II request entry
*/ */
#define MOD_ALT 0x40 #define MAX_XFER 32
#define MOD_CTRL 0x8000
#define MOD_SHIFT 0x4000
struct a2request { struct a2request {
int fd; int fd;
int type; int type;
int addr; int addr;
int count; int count;
int xfer;
char *buffer; char *buffer;
struct a2request *next; struct a2request *next;
} *a2reqlist = NULL, *a2reqfree = NULL; } *a2reqlist = NULL, *a2reqfree = NULL;
/*
* ASCII to scancode conversion
*/
#define MOD_ALT 0x40
#define MOD_CTRL 0x8000
#define MOD_SHIFT 0x4000
int scancode[256] = { int scancode[256] = {
/* /*
* normal scancode * normal scancode
@ -422,6 +428,7 @@ struct a2request *addreq(int a2fd, int reqfd, int type, int addr, int count, cha
a2req->type = type; a2req->type = type;
a2req->addr = addr; a2req->addr = addr;
a2req->count = count; a2req->count = count;
a2req->xfer = 0;
a2req->buffer = buffer; a2req->buffer = buffer;
a2req->next = NULL; a2req->next = NULL;
if (a2reqlist == NULL) if (a2reqlist == NULL)
@ -485,7 +492,7 @@ void main(int argc, char **argv)
struct uinput_user_dev uidev; struct uinput_user_dev uidev;
struct termios oldtio,newtio; struct termios oldtio,newtio;
char iopkt[16]; char iopkt[16];
int i, lastbtn; int i, c, lastbtn;
int a2fd, kbdfd, moufd, srvfd, reqfd, maxfd; int a2fd, kbdfd, moufd, srvfd, reqfd, maxfd;
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
fd_set readset, openset; fd_set readset, openset;
@ -688,15 +695,18 @@ void main(int argc, char **argv)
case 0x90: /* acknowledge read bytes request*/ case 0x90: /* acknowledge read bytes request*/
if (a2reqlist) /* better have an outstanding request */ if (a2reqlist) /* better have an outstanding request */
{ {
// printf("a2pid: read %d bytes from 0x%04X\n", a2reqlist->count, a2reqlist->addr); //printf("a2pid: read %d of %d bytes from 0x%04X\n", a2reqlist->xfer, a2reqlist->count, a2reqlist->addr);
newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */ newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */
tcsetattr(a2fd, TCSANOW, &newtio); tcsetattr(a2fd, TCSANOW, &newtio);
if (writeword(a2fd, a2reqlist->addr, 0x91) && writeword(a2fd, a2reqlist->count, 0x91)) c = a2reqlist->count - a2reqlist->xfer > MAX_XFER
? MAX_XFER
: a2reqlist->count - a2reqlist->xfer;
if (writeword(a2fd, a2reqlist->addr + a2reqlist->xfer, 0x91) && writeword(a2fd, c, 0x91))
{ {
for (i = 0; i < a2reqlist->count; i++) for (i = 0; i < c; i++)
{ {
if (read(a2fd, iopkt, 1) == 1) if (read(a2fd, iopkt, 1) == 1)
a2reqlist->buffer[i] = iopkt[0]; a2reqlist->buffer[a2reqlist->xfer++] = iopkt[0];
else else
{ {
stop = TRUE; stop = TRUE;
@ -715,12 +725,17 @@ void main(int argc, char **argv)
case 0x92: /* acknowledge write bytes */ case 0x92: /* acknowledge write bytes */
if (a2reqlist) /* better have an outstanding request */ if (a2reqlist) /* better have an outstanding request */
{ {
// printf("a2pid: wrote %d bytes to 0x%04X\n", a2reqlist->count, a2reqlist->addr); //printf("a2pid: wrote %d of %d bytes to 0x%04X\n", a2reqlist->xfer, a2reqlist->count, a2reqlist->addr);
newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */ newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */
tcsetattr(a2fd, TCSANOW, &newtio); tcsetattr(a2fd, TCSANOW, &newtio);
if (writeword(a2fd, a2reqlist->addr, 0x93) && writeword(a2fd, a2reqlist->count, 0x93)) c = a2reqlist->count - a2reqlist->xfer > MAX_XFER
? MAX_XFER
: a2reqlist->count - a2reqlist->xfer;
if (writeword(a2fd, a2reqlist->addr + a2reqlist->xfer, 0x93) && writeword(a2fd, c, 0x93))
{ {
if (write(a2fd, a2reqlist->buffer, a2reqlist->count) != a2reqlist->count) if (write(a2fd, a2reqlist->buffer + a2reqlist->xfer, c) == c)
a2reqlist->xfer += c;
else
stop = TRUE; stop = TRUE;
} }
else else
@ -747,8 +762,21 @@ void main(int argc, char **argv)
break; break;
case 0x9E: /* request complete ok */ case 0x9E: /* request complete ok */
case 0x9F: /* request complete error */ case 0x9F: /* request complete error */
if (a2reqlist) /* better have an outstanding request */
{
//printf("a2pid: complete request 0x%02X:0x%02X\n", (unsigned char)iopkt[0], (unsigned char)iopkt[1]);
if ((a2reqlist->type == 0x90 || a2reqlist->type == 0x92)
&& (a2reqlist->count > a2reqlist->xfer))
{
iopkt[0] = a2reqlist->type;
write(a2fd, iopkt, 1);
}
else
{
//printf("a2pid: finish request 0x%02X:0x%02X\n", (unsigned char)iopkt[0], (unsigned char)iopkt[1]); //printf("a2pid: finish request 0x%02X:0x%02X\n", (unsigned char)iopkt[0], (unsigned char)iopkt[1]);
finreq(a2fd, (unsigned char)iopkt[0], (unsigned char)iopkt[1]); finreq(a2fd, (unsigned char)iopkt[0], (unsigned char)iopkt[1]);
}
}
break; break;
default: default:
prlog("a2pid: Unknown Event\n"); prlog("a2pid: Unknown Event\n");
@ -799,6 +827,12 @@ void main(int argc, char **argv)
databuf = malloc(count); databuf = malloc(count);
addreq(a2fd, reqfd, 0x90, addr, count, databuf); addreq(a2fd, reqfd, 0x90, addr, count, databuf);
} }
else
{
iopkt[0] = 0x9E;
iopkt[1] = 0x00;
write(reqfd, iopkt, 2);
}
} }
break; break;
case 0x92: /* write bytes */ case 0x92: /* write bytes */
@ -812,6 +846,12 @@ void main(int argc, char **argv)
if (read(reqfd, databuf, count) == count) if (read(reqfd, databuf, count) == count)
addreq(a2fd, reqfd, 0x92, addr, count, databuf); addreq(a2fd, reqfd, 0x92, addr, count, databuf);
} }
else
{
iopkt[0] = 0x9E;
iopkt[1] = 0x00;
write(reqfd, iopkt, 2);
}
} }
break; break;
case 0x94: /* call */ case 0x94: /* call */

175
src/dskwrite.c Executable file
View File

@ -0,0 +1,175 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int a2open(char *ipaddr)
{
struct sockaddr_in piaddr;
int res;
int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0)
{
perror("Cannot create socket");
return -1;
}
memset(&piaddr, 0, sizeof(piaddr));
piaddr.sin_family = AF_INET;
piaddr.sin_port = htons(6502);
res = inet_pton(AF_INET, ipaddr, &piaddr.sin_addr);
if (res < 0)
{
perror("First parameter is not a valid address family");
close(fd);
return -1;
}
else if (res == 0)
{
perror("Char string (second parameter does not contain valid ipaddress)");
close(fd);
return -1;
}
if (connect(fd, (struct sockaddr *)&piaddr, sizeof(piaddr)) < 0)
{
perror("Connect failed");
close(fd);
return -1;
}
return fd;
}
void a2close(int fd)
{
char closepkt;
closepkt = 0xFF;
write(fd, &closepkt, 1);
shutdown(fd, SHUT_RDWR);
close(fd);
}
int a2read(int fd, int address, int count, char *buffer)
{
char readpkt[8];
readpkt[0] = 0x90; // read
readpkt[1] = address;
readpkt[2] = address >> 8;
readpkt[3] = count;
readpkt[4] = count >> 8;
write(fd, readpkt, 5);
read(fd, buffer, count);
read(fd, readpkt, 2);
return ((unsigned char)readpkt[0] == 0x9E);
}
int a2write(int fd, int address, int count, char *buffer)
{
char writepkt[8];
writepkt[0] = 0x92; // write
writepkt[1] = address;
writepkt[2] = address >> 8;
writepkt[3] = count;
writepkt[4] = count >> 8;
write(fd, writepkt, 5);
write(fd, buffer, count);
read(fd, writepkt, 2);
return ((unsigned char)writepkt[0] == 0x9E);
}
int a2call(int fd, int address, int *result)
{
char callpkt[4];
callpkt[0] = 0x94; // call
callpkt[1] = address;
callpkt[2] = address >> 8;
write(fd, callpkt, 3);
read(fd, callpkt, 2);
if (result)
*result = (unsigned char)callpkt[1];
return ((unsigned char)callpkt[0] == 0x9E);
}
char online[] = {
// ORG $300
0x20, 0x00, 0xBF, // JSR $BF00 (PRODOS)
0xC5, // DB ON_LINE
0x08, 0x03, // DW PARAMS
0x60, // RTS
0xEA,
// PARAMS @ $308
0x02, // PARAM_COUNT
0x60, // UNIT_NUM = DRIVE 0, SLOT 6
0x00, 0x20 // DATA_BUFFER = $2000
};
char writeblk[] = {
// ORG $300
0x20, 0x00, 0xBF, // JSR $BF00 (PRODOS)
0x81, // DB WRITE_BLOCK
0x08, 0x03, // DW PARAMS
0x60, // RTS
0xEA,
// PARAMS @ $308
0x03, // PARAM_COUNT
0x60, // UNIT_NUM = DRIVE 0, SLOT 6
0x00, 0x20, // DATA_BUFFER = $2000
0x00, 0x00 // BLOCK_NUM
};
#define ORG 0x0300
#define BLOCK_NUM 0x030C
#define DATA_BUFFER 0x2000
char dsk[280][512];
int main(int argc, char **argv)
{
FILE *dskfile;
char count[2], volname[21];
int i, result, fd;
int pifd = a2open(argc > 2 ? argv[2] : "127.0.0.1");
if (pifd < 0)
{
perror("Unable to connect to Apple II Pi");
exit(EXIT_FAILURE);
}
if (argc < 2)
{
perror("Usage: dskwrite <filename> [ip address]\n");
exit(EXIT_FAILURE);
}
a2write(pifd, ORG, sizeof(online), online);
a2call(pifd, ORG, &result);
if (result == 0)
{
a2read(pifd, DATA_BUFFER, 16, volname);
volname[(volname[0] & 0x0F) + 1] = '\0';
printf("Are you sure you want to overwrite volume :%s?", volname + 1);
fflush(stdout);
fgets(count, 2, stdin);
if (count[0] != 'y' && count[0] != 'Y')
{
a2close(pifd);
exit(EXIT_FAILURE);
}
}
if ((dskfile = fopen(argv[1], "rb")))
{
fread(dsk, 1, 280*512, dskfile);
fclose(dskfile);
}
else
{
perror("Unable to read .PO file\n");
a2close(pifd);
exit(EXIT_FAILURE);
}
a2write(pifd, ORG, sizeof(writeblk), writeblk);
for (i = 0; i < 280; i++)
{
printf("Writing block #%d\r", i);
fflush(stdout);
count[0] = i;
count[1] = i >> 8;
a2write(pifd, DATA_BUFFER, 512, dsk[i]);
a2write(pifd, BLOCK_NUM, 2, count);
a2call(pifd, ORG, &result);
}
a2close(pifd);
return EXIT_SUCCESS;
}